diff --git a/components/compositing/constellation.rs b/components/compositing/constellation.rs index 497dbcbe81f..8d0c2fee4c6 100644 --- a/components/compositing/constellation.rs +++ b/components/compositing/constellation.rs @@ -54,8 +54,8 @@ use style::viewport::ViewportConstraints; use url::Url; use util::cursor::Cursor; use util::geometry::PagePx; -use util::opts; use util::task::spawn_named; +use util::{opts, prefs}; /// Maintains the pipelines and navigation context and grants permission to composite. /// @@ -891,7 +891,7 @@ impl Constellation { containing_pipeline_id: PipelineId, subpage_id: SubpageId, event: MozBrowserEvent) { - assert!(opts::experimental_enabled()); + assert!(prefs::get_pref("dom.mozbrowser.enabled", false)); // Find the script channel for the given parent pipeline, // and pass the event to that script task. @@ -1373,7 +1373,7 @@ impl Constellation { // https://developer.mozilla.org/en-US/docs/Web/Events/mozbrowserlocationchange fn trigger_mozbrowserlocationchange(&self, pipeline_id: PipelineId) { - if opts::experimental_enabled() { + if prefs::get_pref("dom.mozbrowser.enabled", false) { // Work around borrow checker let event_info = { let pipeline = self.pipeline(pipeline_id); diff --git a/components/compositing/pipeline.rs b/components/compositing/pipeline.rs index fe707d5ad3d..59b9b32473d 100644 --- a/components/compositing/pipeline.rs +++ b/components/compositing/pipeline.rs @@ -31,7 +31,7 @@ use url::Url; use util; use util::geometry::{PagePx, ViewportPx}; use util::ipc::OptionalIpcSender; -use util::opts; +use util::prefs; /// A uniquely-identifiable pipeline of script task, layout task, and paint task. pub struct Pipeline { @@ -269,7 +269,7 @@ impl Pipeline { pub fn trigger_mozbrowser_event(&self, subpage_id: SubpageId, event: MozBrowserEvent) { - assert!(opts::experimental_enabled()); + assert!(prefs::get_pref("dom.mozbrowser.enabled", false)); let event = ConstellationControlMsg::MozBrowserEvent(self.id, subpage_id, diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index c0d07bdfc43..0ff28055e9d 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -50,7 +50,7 @@ use dom::htmlcollection::{HTMLCollection, CollectionFilter}; use dom::htmlelement::{HTMLElement, HTMLElementTypeId}; use dom::htmlheadelement::HTMLHeadElement; use dom::htmlhtmlelement::HTMLHtmlElement; -use dom::htmliframeelement::HTMLIFrameElement; +use dom::htmliframeelement::{self, HTMLIFrameElement}; use dom::htmlscriptelement::HTMLScriptElement; use dom::keyboardevent::KeyboardEvent; use dom::location::Location; @@ -79,7 +79,6 @@ use net_traits::CookieSource::NonHTTP; use net_traits::{Metadata, PendingAsyncLoad, AsyncResponseTarget}; use script_task::Runnable; use script_traits::{MouseButton, UntrustedNodeAddress}; -use util::opts; use util::str::{DOMString, split_html_space_chars}; use euclid::point::Point2D; @@ -856,7 +855,7 @@ impl Document { } pub fn trigger_mozbrowser_event(&self, event: MozBrowserEvent) { - if opts::experimental_enabled() { + if htmliframeelement::mozbrowser_enabled() { let window = self.window.root(); if let Some((containing_pipeline_id, subpage_id)) = window.r().parent_info() { diff --git a/components/script/dom/htmliframeelement.rs b/components/script/dom/htmliframeelement.rs index 99eb73f7367..6abf4f7b877 100644 --- a/components/script/dom/htmliframeelement.rs +++ b/components/script/dom/htmliframeelement.rs @@ -30,7 +30,7 @@ use msg::constellation_msg::IFrameSandboxState::{IFrameSandboxed, IFrameUnsandbo use msg::constellation_msg::Msg as ConstellationMsg; use msg::constellation_msg::{PipelineId, SubpageId, ConstellationChan, MozBrowserEvent, NavigationDirection}; use string_cache::Atom; -use util::opts; +use util::prefs; use util::str::DOMString; use js::jsapi::{RootedValue, JSAutoRequest, JSAutoCompartment}; @@ -41,6 +41,10 @@ use std::cell::Cell; use url::{Url, UrlParser}; use util::str::{self, LengthOrPercentageOrAuto}; +pub fn mozbrowser_enabled() -> bool { + prefs::get_pref("dom.mozbrowser.enabled", false) +} + #[derive(HeapSizeOf)] enum SandboxAllowance { AllowNothing = 0x00, @@ -117,7 +121,7 @@ impl HTMLIFrameElement { old_subpage_id, sandboxed)).unwrap(); - if opts::experimental_enabled() { + if mozbrowser_enabled() { // https://developer.mozilla.org/en-US/docs/Web/Events/mozbrowserloadstart self.dispatch_mozbrowser_event(MozBrowserEvent::LoadStart); } @@ -136,7 +140,7 @@ impl HTMLIFrameElement { // TODO(gw): Support mozbrowser event types that have detail which is not a string. // See https://developer.mozilla.org/en-US/docs/Web/API/Using_the_Browser_API // for a list of mozbrowser events. - assert!(opts::experimental_enabled()); + assert!(mozbrowser_enabled()); if self.Mozbrowser() { let window = window_from_node(self); @@ -298,7 +302,7 @@ impl HTMLIFrameElementMethods for HTMLIFrameElement { // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-mozbrowser fn Mozbrowser(&self) -> bool { - if opts::experimental_enabled() { + if mozbrowser_enabled() { let element = ElementCast::from_ref(self); element.has_attribute(&Atom::from_slice("mozbrowser")) } else { @@ -308,7 +312,7 @@ impl HTMLIFrameElementMethods for HTMLIFrameElement { // https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe#attr-mozbrowser fn SetMozbrowser(&self, value: bool) -> ErrorResult { - if opts::experimental_enabled() { + if mozbrowser_enabled() { let element = ElementCast::from_ref(self); element.set_bool_attribute(&Atom::from_slice("mozbrowser"), value); } diff --git a/components/script/dom/mouseevent.rs b/components/script/dom/mouseevent.rs index c3722fa0adf..5fcd232826e 100644 --- a/components/script/dom/mouseevent.rs +++ b/components/script/dom/mouseevent.rs @@ -16,7 +16,7 @@ use dom::uievent::{UIEvent, UIEventTypeId}; use dom::window::Window; use std::cell::Cell; use std::default::Default; -use util::opts; +use util::prefs; use util::str::DOMString; #[dom_struct] @@ -171,7 +171,7 @@ impl MouseEventMethods for MouseEvent { // This returns the same result as current gecko. // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/which fn Which(&self) -> i32 { - if opts::experimental_enabled() { + if prefs::get_pref("dom.mouseevent.which.enabled", false) { (self.button.get() + 1) as i32 } else { 0 diff --git a/components/style/properties.mako.rs b/components/style/properties.mako.rs index 33508b4220a..c6a5296c739 100644 --- a/components/style/properties.mako.rs +++ b/components/style/properties.mako.rs @@ -51,7 +51,7 @@ class Longhand(object): self.ident = to_rust_ident(name) self.camel_case = to_camel_case(self.ident) self.style_struct = THIS_STYLE_STRUCT - self.experimental = experimental + self.experimental = ("layout.%s.enabled" % name) if experimental else None self.custom_cascade = custom_cascade if derived_from is None: self.derived_from = None @@ -64,7 +64,7 @@ class Shorthand(object): self.ident = to_rust_ident(name) self.camel_case = to_camel_case(self.ident) self.derived_from = None - self.experimental = experimental + self.experimental = ("layout.%s.enabled" % name) if experimental else None self.sub_properties = [LONGHANDS_BY_NAME[s] for s in sub_properties] class StyleStruct(object): @@ -450,7 +450,9 @@ pub mod longhands { % for value in values[:-1]: "${value}" => { % if value in experimental_values: - if !::util::opts::experimental_enabled() { return Err(()) } + if !::util::prefs::get_pref("layout.${value}.enabled", false) { + return Err(()) + } % endif Ok(computed_value::T::${to_rust_ident(value)}) }, @@ -458,7 +460,9 @@ pub mod longhands { % for value in values[-1:]: "${value}" => { % if value in experimental_values: - if !::util::opts::experimental_enabled() { return Err(()) } + if !::util::prefs::get_pref("layout.${value}.enabled", false) { + return Err(()) + } % endif Ok(computed_value::T::${to_rust_ident(value)}) } @@ -5720,7 +5724,7 @@ impl PropertyDeclaration { % if property.derived_from is None: "${property.name}" => { % if property.experimental: - if !::util::opts::experimental_enabled() { + if !::util::prefs::get_pref("${property.experimental}", false) { return PropertyDeclarationParseResult::ExperimentalProperty } % endif @@ -5739,7 +5743,7 @@ impl PropertyDeclaration { % for shorthand in SHORTHANDS: "${shorthand.name}" => { % if shorthand.experimental: - if !::util::opts::experimental_enabled() { + if !::util::prefs::get_pref("${shorthand.experimental}", false) { return PropertyDeclarationParseResult::ExperimentalProperty } % endif diff --git a/components/style/stylesheets.rs b/components/style/stylesheets.rs index 839f002955e..7f625af7282 100644 --- a/components/style/stylesheets.rs +++ b/components/style/stylesheets.rs @@ -429,7 +429,7 @@ impl<'a, 'b> AtRuleParser for NestedRuleParser<'a, 'b> { Ok(AtRuleType::WithBlock(AtRulePrelude::FontFace)) }, "viewport" => { - if ::util::opts::experimental_enabled() { + if ::util::prefs::get_pref("layout.viewport.enabled", false) { Ok(AtRuleType::WithBlock(AtRulePrelude::Viewport)) } else { Err(()) diff --git a/components/util/lib.rs b/components/util/lib.rs index 4c2ef7f6746..b10d5b2a2d1 100644 --- a/components/util/lib.rs +++ b/components/util/lib.rs @@ -60,6 +60,7 @@ pub mod logical_geometry; pub mod mem; pub mod opts; pub mod persistent_list; +pub mod prefs; pub mod range; pub mod resource_files; pub mod str; diff --git a/components/util/opts.rs b/components/util/opts.rs index af8c629fcdc..10a2bd84a78 100644 --- a/components/util/opts.rs +++ b/components/util/opts.rs @@ -10,6 +10,7 @@ use geometry::ScreenPx; use euclid::size::{Size2D, TypedSize2D}; use getopts::Options; use num_cpus; +use prefs; use std::cmp; use std::default::Default; use std::env; @@ -17,7 +18,6 @@ use std::fs::{File, PathExt}; use std::io::{self, Read, Write}; use std::path::Path; use std::process; -use std::sync::atomic::{AtomicBool, Ordering, ATOMIC_BOOL_INIT}; use url::{self, Url}; /// Global flags for Servo, currently set on the command line. @@ -50,9 +50,6 @@ pub struct Opts { /// and cause it to produce output on that interval (`-m`). pub mem_profiler_period: Option, - /// Enable experimental web features (`-e`). - pub enable_experimental: bool, - /// The number of threads to use for layout (`-y`). Defaults to 1, which results in a recursive /// sequential algorithm. pub layout_threads: usize, @@ -384,7 +381,6 @@ pub fn default_opts() -> Opts { device_pixels_per_px: None, time_profiler_period: None, mem_profiler_period: None, - enable_experimental: false, layout_threads: 1, nonincremental_layout: false, nossl: false, @@ -434,7 +430,6 @@ pub fn from_cmdline_args(args: &[String]) { opts.optopt("o", "output", "Output file", "output.png"); opts.optopt("s", "size", "Size of tiles", "512"); opts.optopt("", "device-pixel-ratio", "Device pixels per px", ""); - opts.optflag("e", "experimental", "Enable experimental web features"); opts.optopt("t", "threads", "Number of paint threads", "1"); opts.optflagopt("p", "profile", "Profiler flag and output interval", "10"); opts.optflagopt("m", "memory-profile", "Memory profiler flag and output interval", "10"); @@ -461,6 +456,8 @@ pub fn from_cmdline_args(args: &[String]) { opts.optflag("h", "help", "Print this message"); opts.optopt("", "resources-path", "Path to find static resources", "/home/servo/resources"); opts.optflag("", "sniff-mime-types" , "Enable MIME sniffing"); + opts.optmulti("", "pref", + "A preference to set to enable", "dom.mozbrowser.enabled"); let opt_match = match opts.parse(args) { Ok(m) => m, @@ -589,7 +586,6 @@ pub fn from_cmdline_args(args: &[String]) { device_pixels_per_px: device_pixels_per_px, time_profiler_period: time_profiler_period, mem_profiler_period: mem_profiler_period, - enable_experimental: opt_match.opt_present("e"), layout_threads: layout_threads, nonincremental_layout: nonincremental_layout, nossl: nossl, @@ -630,19 +626,12 @@ pub fn from_cmdline_args(args: &[String]) { }; set_defaults(opts); -} -static EXPERIMENTAL_ENABLED: AtomicBool = ATOMIC_BOOL_INIT; - -/// Turn on experimental features globally. Normally this is done -/// during initialization by `set` or `from_cmdline_args`, but -/// tests that require experimental features will also set it. -pub fn set_experimental_enabled(new_value: bool) { - EXPERIMENTAL_ENABLED.store(new_value, Ordering::SeqCst); -} - -pub fn experimental_enabled() -> bool { - EXPERIMENTAL_ENABLED.load(Ordering::SeqCst) + // This must happen after setting the default options, since the prefs rely on + // on the resource path. + for pref in opt_match.opt_strs("pref").iter() { + prefs::set_pref(pref, true); + } } // Make Opts available globally. This saves having to clone and pass @@ -653,7 +642,7 @@ const INVALID_OPTIONS: *mut Opts = 0x01 as *mut Opts; lazy_static! { static ref OPTIONS: Opts = { - let opts = unsafe { + unsafe { let initial = if !DEFAULT_OPTIONS.is_null() { let opts = Box::from_raw(DEFAULT_OPTIONS); *opts @@ -662,9 +651,7 @@ lazy_static! { }; DEFAULT_OPTIONS = INVALID_OPTIONS; initial - }; - set_experimental_enabled(opts.enable_experimental); - opts + } }; } diff --git a/components/util/prefs.rs b/components/util/prefs.rs new file mode 100644 index 00000000000..357d7d1481f --- /dev/null +++ b/components/util/prefs.rs @@ -0,0 +1,50 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use resource_files::resources_dir_path; +use rustc_serialize::json::Json; +use std::collections::HashMap; +use std::fs::File; +use std::sync::{Arc, Mutex}; + +lazy_static! { + static ref PREFS: Arc>> = { + let prefs = read_prefs().unwrap_or(HashMap::new()); + Arc::new(Mutex::new(prefs)) + }; +} + +fn read_prefs() -> Result, ()> { + let mut path = resources_dir_path(); + path.push("prefs.json"); + + let mut file = try!(File::open(path).or_else(|e| { + println!("Error opening preferences: {:?}.", e); + Err(()) + })); + let json = try!(Json::from_reader(&mut file).or_else(|e| { + println!("Ignoring invalid JSON in preferences: {:?}.", e); + Err(()) + })); + + let mut prefs = HashMap::new(); + if let Some(obj) = json.as_object() { + for (name, value) in obj.iter() { + if let Some(bool_value) = value.as_boolean() { + prefs.insert(name.clone(), bool_value); + } else { + println!("Ignoring non-boolean preference value for {:?}", name); + } + } + } + Ok(prefs) +} + +pub fn get_pref(name: &str, default: bool) -> bool { + *PREFS.lock().unwrap().get(name).unwrap_or(&default) +} + +pub fn set_pref(name: &str, value: bool) { + let _ = PREFS.lock().unwrap().insert(name.to_owned(), value); +} diff --git a/resources/prefs.json b/resources/prefs.json new file mode 100644 index 00000000000..3f7ad3a9cbb --- /dev/null +++ b/resources/prefs.json @@ -0,0 +1,13 @@ +{ + "dom.mouseevent.which.enabled": false, + "dom.mozbrowser.enabled": false, + "layout.columns.enabled": false, + "layout.column-width.enabled": false, + "layout.column-count.enabled": false, + "layout.column-gap.enabled": false, + "layout.flex.enabled": false, + "layout.flex-direction.enabled": false, + "layout.text-orientation.enabled": false, + "layout.viewport.enabled": false, + "layout.writing-mode.enabled": false +} diff --git a/tests/ref/basic.list b/tests/ref/basic.list index 01dc9d43cab..ed4e0d91a4b 100644 --- a/tests/ref/basic.list +++ b/tests/ref/basic.list @@ -98,8 +98,8 @@ flaky_cpu == append_style_a.html append_style_b.html == first_child_pseudo_a.html first_child_pseudo_b.html == first_of_type_pseudo_a.html first_of_type_pseudo_b.html == fixed_width_overrides_child_intrinsic_width_a.html fixed_width_overrides_child_intrinsic_width_ref.html -experimental == flex_column_direction.html flex_column_direction_ref.html -experimental == flex_row_direction.html flex_row_direction_ref.html +prefs:"layout.flex-direction.enabled,layout.flex.enabled" == flex_column_direction.html flex_column_direction_ref.html +prefs:"layout.flex.enabled" == flex_row_direction.html flex_row_direction_ref.html == float_clearance_a.html float_clearance_ref.html == float_clearance_intrinsic_width_a.html float_clearance_intrinsic_width_ref.html == float_intrinsic_height.html float_intrinsic_height_ref.html @@ -138,7 +138,7 @@ experimental == flex_row_direction.html flex_row_direction_ref.html == iframe/simple_inline_width_height.html iframe/simple_inline_width_height_ref.html == iframe/simple_inline_width_percentage.html iframe/simple_inline_width_percentage_ref.html == iframe/size_attributes.html iframe/size_attributes_ref.html -experimental == iframe/size_attributes_vertical_writing_mode.html iframe/size_attributes_vertical_writing_mode_ref.html +prefs:"layout.writing-mode.enabled" == iframe/size_attributes_vertical_writing_mode.html iframe/size_attributes_vertical_writing_mode_ref.html == iframe/stacking_context.html iframe/stacking_context_ref.html != image_rendering_auto_a.html image_rendering_pixelated_a.html @@ -357,7 +357,7 @@ flaky_cpu == linebreak_simple_a.html linebreak_simple_b.html == transform_simple_a.html transform_simple_ref.html == transform_stacking_context_a.html transform_stacking_context_ref.html == upper_id_attr.html upper_id_attr_ref.html -flaky_cpu,experimental == vertical-lr-blocks.html vertical-lr-blocks_ref.html +flaky_cpu,prefs:"layout.writing-mode.enabled" == vertical-lr-blocks.html vertical-lr-blocks_ref.html == vertical_align_bottom_a.html vertical_align_bottom_ref.html == vertical_align_inline_block_a.html vertical_align_inline_block_ref.html == vertical_align_inside_table_a.html vertical_align_inside_table_ref.html @@ -373,7 +373,7 @@ resolution=800x600 == viewport_percentage_vmin_vmax.html viewport_percentage_vmi # resolution=600x800 == viewport_percentage_vmin_vmax.html viewport_percentage_vmin_vmax_b.html resolution=800x600 == viewport_percentage_vw_vh.html viewport_percentage_vw_vh_a.html # resolution=600x800 == viewport_percentage_vw_vh.html viewport_percentage_vw_vh_b.html -experimental == viewport_rule.html viewport_rule_ref.html +prefs:"layout.viewport.enabled" == viewport_rule.html viewport_rule_ref.html == visibility_hidden.html visibility_hidden_ref.html == webgl-context/clearcolor.html webgl-context/clearcolor_ref.html diff --git a/tests/reftest.rs b/tests/reftest.rs index 5104ed06128..05fc484bfc1 100644 --- a/tests/reftest.rs +++ b/tests/reftest.rs @@ -140,7 +140,7 @@ struct Reftest { servo_args: Vec, render_mode: RenderMode, is_flaky: bool, - experimental: bool, + prefs: Vec, fragment_identifier: Option, resolution: Option, } @@ -198,7 +198,7 @@ fn parse_lists(file: &Path, servo_args: &[String], render_mode: RenderMode, id_o let conditions_list = test_line.conditions.split(','); let mut flakiness = RenderMode::empty(); - let mut experimental = false; + let mut prefs = vec![]; let mut fragment_identifier = None; let mut resolution = None; for condition in conditions_list { @@ -207,8 +207,12 @@ fn parse_lists(file: &Path, servo_args: &[String], render_mode: RenderMode, id_o "flaky_gpu" => flakiness.insert(GPU_RENDERING), "flaky_linux" => flakiness.insert(LINUX_TARGET), "flaky_macos" => flakiness.insert(MACOS_TARGET), - "experimental" => experimental = true, - _ => (), + _ => () + } + if condition.starts_with("prefs:\"") { + if let Some(joined) = condition.split("\"").nth(1) { + prefs.extend(joined.split(",").map(str::to_owned)); + } } if condition.starts_with("fragment=") { fragment_identifier = Some(condition["fragment=".len()..].to_string()); @@ -226,7 +230,7 @@ fn parse_lists(file: &Path, servo_args: &[String], render_mode: RenderMode, id_o render_mode: render_mode, servo_args: servo_args.to_vec(), is_flaky: render_mode.intersects(flakiness), - experimental: experimental, + prefs: prefs, fragment_identifier: fragment_identifier, resolution: resolution, }; @@ -275,8 +279,9 @@ fn capture(reftest: &Reftest, side: usize) -> (u32, u32, Vec) { if reftest.render_mode.contains(GPU_RENDERING) { command.arg("-g"); } - if reftest.experimental { - command.arg("--experimental"); + for pref in &reftest.prefs { + command.arg("--pref"); + command.arg(pref); } if let Some(ref resolution) = reftest.resolution { command.arg("--resolution"); diff --git a/tests/unit/style/viewport.rs b/tests/unit/style/viewport.rs index 5cb8cb6c64d..283a453109b 100644 --- a/tests/unit/style/viewport.rs +++ b/tests/unit/style/viewport.rs @@ -25,7 +25,7 @@ fn test_viewport_rule(css: &str, callback: F) where F: Fn(&Vec, &str) { - ::util::opts::set_experimental_enabled(true); + ::util::prefs::set_pref("layout.viewport.enabled", true); let stylesheet = stylesheet!(css, Author); let mut rule_count = 0; @@ -172,7 +172,7 @@ fn cascading_within_viewport_rule() { #[test] fn multiple_stylesheets_cascading() { - ::util::opts::set_experimental_enabled(true); + ::util::prefs::set_pref("layout.viewport.enabled", true); let device = Device::new(MediaType::Screen, Size2D::typed(800., 600.)); let stylesheets = vec![