aboutsummaryrefslogtreecommitdiffstats
path: root/components/util
diff options
context:
space:
mode:
authorJosh Matthews <josh@joshmatthews.net>2015-08-26 12:35:38 -0400
committerJosh Matthews <josh@joshmatthews.net>2015-08-28 10:30:04 -0400
commita3ee9b5dd9105a141a45de8f5e3ca8818c217fa3 (patch)
treedd376ffa5811ce14c8b0c2a3386867b6e979f852 /components/util
parent6431e8da43817e8a6b1e4757afbcf45c1a629707 (diff)
downloadservo-a3ee9b5dd9105a141a45de8f5e3ca8818c217fa3.tar.gz
servo-a3ee9b5dd9105a141a45de8f5e3ca8818c217fa3.zip
Replace catch-all experimental flag with fine-grained boolean preferences initialized from a JSON document.
Diffstat (limited to 'components/util')
-rw-r--r--components/util/lib.rs1
-rw-r--r--components/util/opts.rs26
-rw-r--r--components/util/prefs.rs50
3 files changed, 53 insertions, 24 deletions
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..7c42d60380e 100644
--- a/components/util/opts.rs
+++ b/components/util/opts.rs
@@ -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
+ }
};
}
diff --git a/components/util/prefs.rs b/components/util/prefs.rs
new file mode 100644
index 00000000000..8c38b6b8152
--- /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<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);
+}