Replace catch-all experimental flag with fine-grained boolean preferences initialized from a JSON document.

This commit is contained in:
Josh Matthews 2015-08-26 12:35:38 -04:00
parent 6431e8da43
commit a3ee9b5dd9
11 changed files with 95 additions and 46 deletions

View file

@ -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<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
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<LTF: LayoutTaskFactory, STF: ScriptTaskFactory> Constellation<LTF, STF> {
// 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);

View file

@ -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,

View file

@ -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() {

View file

@ -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);
@ -302,7 +306,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 {
@ -312,7 +316,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);
}

View file

@ -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

View file

@ -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

View file

@ -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(())

View file

@ -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;

View file

@ -17,7 +17,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 +49,6 @@ pub struct Opts {
/// and cause it to produce output on that interval (`-m`).
pub mem_profiler_period: Option<f64>,
/// 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 +380,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 +429,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");
@ -589,7 +583,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,
@ -632,19 +625,6 @@ 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)
}
// Make Opts available globally. This saves having to clone and pass
// opts everywhere it is used, which gets particularly cumbersome
// when passing through the DOM structures.
@ -653,7 +633,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 +642,7 @@ lazy_static! {
};
DEFAULT_OPTIONS = INVALID_OPTIONS;
initial
};
set_experimental_enabled(opts.enable_experimental);
opts
}
};
}

50
components/util/prefs.rs Normal file
View file

@ -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<Mutex<HashMap<String, bool>>> = {
let prefs = read_prefs().unwrap_or(HashMap::new());
Arc::new(Mutex::new(prefs))
};
}
fn read_prefs() -> Result<HashMap<String, bool>, ()> {
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: String, value: bool) {
let _ = PREFS.lock().unwrap().insert(name, value);
}

13
resources/prefs.json Normal file
View file

@ -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
}