mirror of
https://github.com/servo/servo.git
synced 2025-08-06 14:10:11 +01:00
Auto merge of #10114 - matthewbentley:master, r=Manishearth
Load prefs.json from profile-dir if --profile-dir is specified at launch In response to #10098 Tries to load `prefs.json` from the profile-dir and merge them into the preferences if `--profile-dir` is specified at launch. The profile-dir preferences take precedence over the default preferences, but command line preferences still take precedence over everything. Also adds some tests for `prefs.rs`. These rely on the contents of `resources/prefs.json` (at least `test_get_set_reset_extend()` does), so they may need to be re-worked a bit. <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10114) <!-- Reviewable:end -->
This commit is contained in:
commit
ea24389b85
4 changed files with 89 additions and 10 deletions
|
@ -805,8 +805,12 @@ pub fn from_cmdline_args(args: &[String]) -> ArgumentParsingResult {
|
||||||
|
|
||||||
set_defaults(opts);
|
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.
|
// on the resource path.
|
||||||
|
// Note that command line preferences have the highest precedence
|
||||||
|
if get().profile_dir.is_some() {
|
||||||
|
prefs::add_user_prefs();
|
||||||
|
}
|
||||||
for pref in opt_match.opt_strs("pref").iter() {
|
for pref in opt_match.opt_strs("pref").iter() {
|
||||||
let split: Vec<&str> = pref.splitn(2, '=').collect();
|
let split: Vec<&str> = pref.splitn(2, '=').collect();
|
||||||
let pref_name = split[0];
|
let pref_name = split[0];
|
||||||
|
|
|
@ -2,11 +2,14 @@
|
||||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
* 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/. */
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
use opts;
|
||||||
use resource_files::resources_dir_path;
|
use resource_files::resources_dir_path;
|
||||||
use rustc_serialize::json::{Json, ToJson};
|
use rustc_serialize::json::{Json, ToJson};
|
||||||
use std::borrow::ToOwned;
|
use std::borrow::ToOwned;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::io::{Read, Write, stderr};
|
||||||
|
use std::path::PathBuf;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
|
@ -66,7 +69,8 @@ impl ToJson for PrefValue {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Pref {
|
#[derive(Debug)]
|
||||||
|
pub enum Pref {
|
||||||
NoDefault(Arc<PrefValue>),
|
NoDefault(Arc<PrefValue>),
|
||||||
WithDefault(Arc<PrefValue>, Option<Arc<PrefValue>>)
|
WithDefault(Arc<PrefValue>, Option<Arc<PrefValue>>)
|
||||||
}
|
}
|
||||||
|
@ -118,14 +122,8 @@ impl ToJson for Pref {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_prefs() -> Result<HashMap<String, Pref>, ()> {
|
pub fn read_prefs_from_file<T>(mut file: T)
|
||||||
let mut path = resources_dir_path();
|
-> Result<HashMap<String, Pref>, ()> where T: Read {
|
||||||
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| {
|
let json = try!(Json::from_reader(&mut file).or_else(|e| {
|
||||||
println!("Ignoring invalid JSON in preferences: {:?}.", e);
|
println!("Ignoring invalid JSON in preferences: {:?}.", e);
|
||||||
Err(())
|
Err(())
|
||||||
|
@ -145,6 +143,38 @@ fn read_prefs() -> Result<HashMap<String, Pref>, ()> {
|
||||||
Ok(prefs)
|
Ok(prefs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn extend_prefs(extension: HashMap<String, Pref>) {
|
||||||
|
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 {
|
||||||
|
writeln!(&mut stderr(), "Error opening prefs.json from profile_dir")
|
||||||
|
.expect("failed printing to stderr");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_prefs() -> Result<HashMap<String, Pref>, ()> {
|
||||||
|
let mut path = resources_dir_path();
|
||||||
|
path.push("prefs.json");
|
||||||
|
|
||||||
|
let file = try!(File::open(path).or_else(|e| {
|
||||||
|
writeln!(&mut stderr(), "Error opening preferences: {:?}.", e)
|
||||||
|
.expect("failed printing to stderr");
|
||||||
|
Err(())
|
||||||
|
}));
|
||||||
|
|
||||||
|
read_prefs_from_file(file)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_pref(name: &str) -> Arc<PrefValue> {
|
pub fn get_pref(name: &str) -> Arc<PrefValue> {
|
||||||
PREFS.lock().unwrap().get(name).map_or(Arc::new(PrefValue::Missing), |x| x.value().clone())
|
PREFS.lock().unwrap().get(name).map_or(Arc::new(PrefValue::Missing), |x| x.value().clone())
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,3 +16,4 @@ extern crate util;
|
||||||
#[cfg(test)] mod opts;
|
#[cfg(test)] mod opts;
|
||||||
#[cfg(test)] mod str;
|
#[cfg(test)] mod str;
|
||||||
#[cfg(test)] mod thread;
|
#[cfg(test)] mod thread;
|
||||||
|
#[cfg(test)] mod prefs;
|
||||||
|
|
44
tests/unit/util/prefs.rs
Normal file
44
tests/unit/util/prefs.rs
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/* 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 util::prefs::{PrefValue, extend_prefs, read_prefs_from_file, get_pref, set_pref, reset_pref, reset_all_prefs};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_create_pref() {
|
||||||
|
let json_str = "{\
|
||||||
|
\"layout.writing-mode.enabled\": true,\
|
||||||
|
\"net.mime.sniff\": false,\
|
||||||
|
\"shell.homepage\": \"http://servo.org\"\
|
||||||
|
}";
|
||||||
|
|
||||||
|
let prefs = read_prefs_from_file(json_str.as_bytes());
|
||||||
|
assert!(prefs.is_ok());
|
||||||
|
let prefs = prefs.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(prefs.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_get_set_reset_extend() {
|
||||||
|
let json_str = "{\
|
||||||
|
\"layout.writing-mode.enabled\": true,\
|
||||||
|
\"extra.stuff\": false,\
|
||||||
|
\"shell.homepage\": \"https://google.com\"\
|
||||||
|
}";
|
||||||
|
|
||||||
|
assert_eq!(*get_pref("test"), PrefValue::Missing);
|
||||||
|
set_pref("test", PrefValue::String("hi".to_owned()));
|
||||||
|
assert_eq!(*get_pref("test"), PrefValue::String("hi".to_owned()));
|
||||||
|
assert_eq!(*get_pref("shell.homepage"), PrefValue::String("http://servo.org".to_owned()));
|
||||||
|
set_pref("shell.homepage", PrefValue::Boolean(true));
|
||||||
|
assert_eq!(*get_pref("shell.homepage"), PrefValue::Boolean(true));
|
||||||
|
reset_pref("shell.homepage");
|
||||||
|
assert_eq!(*get_pref("shell.homepage"), PrefValue::String("http://servo.org".to_owned()));
|
||||||
|
|
||||||
|
let extension = read_prefs_from_file(json_str.as_bytes()).unwrap();
|
||||||
|
extend_prefs(extension);
|
||||||
|
assert_eq!(*get_pref("shell.homepage"), PrefValue::String("https://google.com".to_owned()));
|
||||||
|
assert_eq!(*get_pref("layout.writing-mode.enabled"), PrefValue::Boolean(true));
|
||||||
|
assert_eq!(*get_pref("extra.stuff"), PrefValue::Boolean(false));
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue