diff --git a/components/util/opts.rs b/components/util/opts.rs index e8d5283b9b4..0fd41e4ea96 100644 --- a/components/util/opts.rs +++ b/components/util/opts.rs @@ -799,8 +799,12 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult { set_defaults(opts); - // This must happen after setting the default options, since the prefs rely on + // These must happen after setting the default options, since the prefs rely on // on the resource path. + // Note that command line preferences have the highest precedent + if get().profile_dir.is_some() { + prefs::add_user_prefs(); + } for pref in opt_match.opt_strs("pref").iter() { let split: Vec<&str> = pref.splitn(2, '=').collect(); let pref_name = split[0]; diff --git a/components/util/prefs.rs b/components/util/prefs.rs index 0d6a4c94660..6aa831f9687 100644 --- a/components/util/prefs.rs +++ b/components/util/prefs.rs @@ -4,9 +4,12 @@ use resource_files::resources_dir_path; use rustc_serialize::json::{Json, ToJson}; +use opts; use std::borrow::ToOwned; use std::collections::HashMap; use std::fs::File; +use std::io::Read; +use std::path::PathBuf; use std::sync::{Arc, Mutex}; lazy_static! { @@ -66,7 +69,8 @@ impl ToJson for PrefValue { } } -enum Pref { +#[derive(Debug)] +pub enum Pref { NoDefault(Arc), WithDefault(Arc, Option>) } @@ -118,14 +122,8 @@ impl ToJson for Pref { } } -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(()) - })); +pub fn read_prefs_from_file(mut file: T) + -> Result, ()> where T: Read { let json = try!(Json::from_reader(&mut file).or_else(|e| { println!("Ignoring invalid JSON in preferences: {:?}.", e); Err(()) @@ -145,6 +143,36 @@ fn read_prefs() -> Result, ()> { Ok(prefs) } +pub fn extend_prefs(extension: HashMap) { + PREFS.lock().unwrap().extend(extension); +} + +pub fn add_user_prefs() { + if let Some(ref dir) = opts::get().profile_dir { + let mut path = PathBuf::from(dir); + path.push("prefs.json"); + if let Ok(file) = File::open(path) { + if let Ok(prefs) = read_prefs_from_file(file) { + extend_prefs(prefs); + } + } else { + println!("Error opening prefs.json from profile_dir"); + } + } +} + +fn read_prefs() -> Result, ()> { + let mut path = resources_dir_path(); + path.push("prefs.json"); + + let file = try!(File::open(path).or_else(|e| { + println!("Error opening preferences: {:?}.", e); + Err(()) + })); + + read_prefs_from_file(file) +} + pub fn get_pref(name: &str) -> Arc { PREFS.lock().unwrap().get(name).map_or(Arc::new(PrefValue::Missing), |x| x.value().clone()) }