Navigation Menu

Skip to content

Commit

Permalink
Move the user agent and user stylesheets to layout_thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ms2ger committed Aug 23, 2016
1 parent f84e01e commit 992391d
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 95 deletions.
1 change: 1 addition & 0 deletions components/layout_thread/Cargo.toml
Expand Up @@ -21,6 +21,7 @@ heapsize_plugin = "0.1.2"
ipc-channel = "0.5"
layout = {path = "../layout"}
layout_traits = {path = "../layout_traits"}
lazy_static = "0.2"
log = "0.3.5"
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
Expand Down
60 changes: 56 additions & 4 deletions components/layout_thread/lib.rs
Expand Up @@ -25,6 +25,9 @@ extern crate ipc_channel;
#[macro_use]
extern crate layout;
extern crate layout_traits;
#[allow(unused_extern_crates)]
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate msg;
Expand Down Expand Up @@ -95,21 +98,22 @@ use std::borrow::ToOwned;
use std::collections::HashMap;
use std::hash::BuildHasherDefault;
use std::ops::{Deref, DerefMut};
use std::process;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::mpsc::{channel, Sender, Receiver};
use std::sync::{Arc, Mutex, MutexGuard, RwLock};
use style::animation::Animation;
use style::computed_values::{filter, mix_blend_mode};
use style::context::{ReflowGoal, LocalStyleContextCreationInfo, SharedStyleContext};
use style::dom::{TDocument, TElement, TNode};
use style::error_reporting::ParseErrorReporter;
use style::error_reporting::{ParseErrorReporter, StdoutErrorReporter};
use style::logical_geometry::LogicalPoint;
use style::media_queries::{Device, MediaType};
use style::parallel::WorkQueueData;
use style::parser::ParserContextExtraData;
use style::refcell::RefCell;
use style::selector_matching::Stylist;
use style::servo_selector_impl::USER_OR_USER_AGENT_STYLESHEETS;
use style::stylesheets::{Stylesheet, CSSRuleIteratorExt};
use style::stylesheets::{Stylesheet, UserAgentStylesheets, CSSRuleIteratorExt, Origin};
use style::thread_state;
use style::timer::Timer;
use style::workqueue::WorkQueue;
Expand All @@ -118,6 +122,7 @@ use util::geometry::max_rect;
use util::ipc::OptionalIpcSender;
use util::opts;
use util::prefs::PREFS;
use util::resource_files::read_resource_file;
use util::thread;

/// The number of screens we have to traverse before we decide to generate new display lists.
Expand Down Expand Up @@ -417,7 +422,7 @@ impl LayoutThread {

let stylist = Arc::new(Stylist::new(device));
let outstanding_web_fonts_counter = Arc::new(AtomicUsize::new(0));
for stylesheet in &*USER_OR_USER_AGENT_STYLESHEETS {
for stylesheet in &*UA_STYLESHEETS.user_or_user_agent_stylesheets {
add_font_face_rules(stylesheet,
&stylist.device,
&font_cache_thread,
Expand Down Expand Up @@ -1132,6 +1137,7 @@ impl LayoutThread {

// If the entire flow tree is invalid, then it will be reflowed anyhow.
needs_dirtying |= Arc::get_mut(&mut rw_data.stylist).unwrap().update(&data.document_stylesheets,
Some(&*UA_STYLESHEETS),
data.stylesheets_changed);
let needs_reflow = viewport_size_changed && !needs_dirtying;
unsafe {
Expand Down Expand Up @@ -1575,3 +1581,49 @@ fn get_root_flow_background_color(flow: &mut Flow) -> AzColor {
.resolve_color(kid_block_flow.fragment.style.get_background().background_color)
.to_gfx_color()
}

fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
fn parse_ua_stylesheet(filename: &'static str) -> Result<Stylesheet, &'static str> {
let res = try!(read_resource_file(filename).map_err(|_| filename));
Ok(Stylesheet::from_bytes(
&res,
Url::parse(&format!("chrome://resources/{:?}", filename)).unwrap(),
None,
None,
Origin::UserAgent,
Box::new(StdoutErrorReporter),
ParserContextExtraData::default()))
}

let mut user_or_user_agent_stylesheets = vec!();
// FIXME: presentational-hints.css should be at author origin with zero specificity.
// (Does it make a difference?)
for &filename in &["user-agent.css", "servo.css", "presentational-hints.css"] {
user_or_user_agent_stylesheets.push(try!(parse_ua_stylesheet(filename)));
}
for &(ref contents, ref url) in &opts::get().user_stylesheets {
user_or_user_agent_stylesheets.push(Stylesheet::from_bytes(
&contents, url.clone(), None, None, Origin::User, Box::new(StdoutErrorReporter),
ParserContextExtraData::default()));
}

let quirks_mode_stylesheet = try!(parse_ua_stylesheet("quirks-mode.css"));

Ok(UserAgentStylesheets {
user_or_user_agent_stylesheets: user_or_user_agent_stylesheets,
quirks_mode_stylesheet: quirks_mode_stylesheet,
})
}


lazy_static! {
static ref UA_STYLESHEETS: UserAgentStylesheets = {
match get_ua_stylesheets() {
Ok(stylesheets) => stylesheets,
Err(filename) => {
error!("Failed to load UA stylesheet {}!", filename);
process::exit(1);
}
}
};
}
1 change: 1 addition & 0 deletions components/servo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 0 additions & 11 deletions components/style/gecko_selector_impl.rs
Expand Up @@ -9,7 +9,6 @@ use selector_impl::{attr_exists_selector_is_shareable, attr_equals_selector_is_s
use selectors::parser::{ParserContext, SelectorImpl, AttrSelector};
use std::fmt;
use string_cache::{Atom, WeakAtom, Namespace, WeakNamespace};
use stylesheets::Stylesheet;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GeckoSelectorImpl;
Expand Down Expand Up @@ -252,14 +251,4 @@ impl GeckoSelectorImpl {
pub fn pseudo_class_state_flag(pc: &NonTSPseudoClass) -> ElementState {
pc.state_flag()
}

#[inline]
pub fn get_user_or_user_agent_stylesheets() -> &'static [Stylesheet] {
&[]
}

#[inline]
pub fn get_quirks_mode_stylesheet() -> Option<&'static Stylesheet> {
None
}
}
19 changes: 11 additions & 8 deletions components/style/selector_matching.rs
Expand Up @@ -29,7 +29,7 @@ use std::slice;
use std::sync::Arc;
use string_cache::Atom;
use style_traits::viewport::ViewportConstraints;
use stylesheets::{CSSRule, CSSRuleIteratorExt, Origin, Stylesheet};
use stylesheets::{CSSRule, CSSRuleIteratorExt, Origin, Stylesheet, UserAgentStylesheets};
use viewport::{MaybeNew, ViewportRuleCascade};

pub type FnvHashMap<K, V> = HashMap<K, V, BuildHasherDefault<::fnv::FnvHasher>>;
Expand Down Expand Up @@ -116,7 +116,10 @@ impl Stylist {
stylist
}

pub fn update(&mut self, doc_stylesheets: &[Arc<Stylesheet>], stylesheets_changed: bool) -> bool {
pub fn update(&mut self,
doc_stylesheets: &[Arc<Stylesheet>],
ua_stylesheets: Option<&UserAgentStylesheets>,
stylesheets_changed: bool) -> bool {
if !(self.is_device_dirty || stylesheets_changed) {
return false;
}
Expand All @@ -135,13 +138,13 @@ impl Stylist {
self.sibling_affecting_selectors.clear();
self.non_common_style_affecting_attributes_selectors.clear();

for ref stylesheet in TheSelectorImpl::get_user_or_user_agent_stylesheets().iter() {
self.add_stylesheet(&stylesheet);
}
if let Some(ua_stylesheets) = ua_stylesheets {
for stylesheet in &ua_stylesheets.user_or_user_agent_stylesheets {
self.add_stylesheet(&stylesheet);
}

if self.quirks_mode {
if let Some(s) = TheSelectorImpl::get_quirks_mode_stylesheet() {
self.add_stylesheet(s);
if self.quirks_mode {
self.add_stylesheet(&ua_stylesheets.quirks_mode_stylesheet);
}
}

Expand Down
71 changes: 0 additions & 71 deletions components/style/servo_selector_impl.rs
Expand Up @@ -5,20 +5,13 @@
use attr::{AttrIdentifier, AttrValue};
use cssparser::ToCss;
use element_state::ElementState;
use error_reporting::StdoutErrorReporter;
use parser::ParserContextExtraData;
use restyle_hints::ElementSnapshot;
use selector_impl::{ElementExt, PseudoElementCascadeType, TheSelectorImpl};
use selector_impl::{attr_exists_selector_is_shareable, attr_equals_selector_is_shareable};
use selectors::parser::{AttrSelector, ParserContext, SelectorImpl};
use selectors::{Element, MatchAttrGeneric};
use std::fmt;
use std::process;
use string_cache::{Atom, Namespace};
use stylesheets::{Stylesheet, Origin};
use url::Url;
use util::opts;
use util::resource_files::read_resource_file;

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
Expand Down Expand Up @@ -240,16 +233,6 @@ impl ServoSelectorImpl {
pub fn pseudo_is_before_or_after(pseudo: &PseudoElement) -> bool {
pseudo.is_before_or_after()
}

#[inline]
pub fn get_user_or_user_agent_stylesheets() -> &'static [Stylesheet] {
&*USER_OR_USER_AGENT_STYLESHEETS
}

#[inline]
pub fn get_quirks_mode_stylesheet() -> Option<&'static Stylesheet> {
Some(&*QUIRKS_MODE_STYLESHEET)
}
}

/// Servo's version of an element snapshot.
Expand Down Expand Up @@ -336,57 +319,3 @@ impl<E: Element<Impl=TheSelectorImpl>> ElementExt for E {
self.match_non_ts_pseudo_class(NonTSPseudoClass::AnyLink)
}
}

lazy_static! {
pub static ref USER_OR_USER_AGENT_STYLESHEETS: Vec<Stylesheet> = {
let mut stylesheets = vec!();
// FIXME: presentational-hints.css should be at author origin with zero specificity.
// (Does it make a difference?)
for &filename in &["user-agent.css", "servo.css", "presentational-hints.css"] {
match read_resource_file(filename) {
Ok(res) => {
let ua_stylesheet = Stylesheet::from_bytes(
&res,
Url::parse(&format!("chrome://resources/{:?}", filename)).unwrap(),
None,
None,
Origin::UserAgent,
Box::new(StdoutErrorReporter),
ParserContextExtraData::default());
stylesheets.push(ua_stylesheet);
}
Err(..) => {
error!("Failed to load UA stylesheet {}!", filename);
process::exit(1);
}
}
}
for &(ref contents, ref url) in &opts::get().user_stylesheets {
stylesheets.push(Stylesheet::from_bytes(
&contents, url.clone(), None, None, Origin::User, Box::new(StdoutErrorReporter),
ParserContextExtraData::default()));
}
stylesheets
};
}

lazy_static! {
pub static ref QUIRKS_MODE_STYLESHEET: Stylesheet = {
match read_resource_file("quirks-mode.css") {
Ok(res) => {
Stylesheet::from_bytes(
&res,
Url::parse("chrome://resources/quirks-mode.css").unwrap(),
None,
None,
Origin::UserAgent,
Box::new(StdoutErrorReporter),
ParserContextExtraData::default())
},
Err(..) => {
error!("Stylist failed to load 'quirks-mode.css'!");
process::exit(1);
}
}
};
}
7 changes: 7 additions & 0 deletions components/style/stylesheets.rs
Expand Up @@ -54,6 +54,13 @@ pub struct Stylesheet {
}


/// This structure holds the user-agent and user stylesheets.
pub struct UserAgentStylesheets {
pub user_or_user_agent_stylesheets: Vec<Stylesheet>,
pub quirks_mode_stylesheet: Stylesheet,
}


#[derive(Debug, PartialEq)]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub enum CSSRule {
Expand Down
1 change: 1 addition & 0 deletions ports/cef/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ports/geckolib/data.rs
Expand Up @@ -83,7 +83,7 @@ impl PerDocumentStyleData {
// need to detect the latter case and trigger a flush as well.
if self.stylesheets_changed {
let _ = Arc::get_mut(&mut self.stylist).unwrap()
.update(&self.stylesheets, true);
.update(&self.stylesheets, None, true);
self.stylesheets_changed = false;
}
}
Expand Down

0 comments on commit 992391d

Please sign in to comment.