mirror of
https://github.com/servo/servo.git
synced 2025-08-03 04:30:10 +01:00
Replace catch-all experimental flag with fine-grained boolean preferences initialized from a JSON document.
This commit is contained in:
parent
6431e8da43
commit
a3ee9b5dd9
11 changed files with 95 additions and 46 deletions
50
components/util/prefs.rs
Normal file
50
components/util/prefs.rs
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue