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

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