mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
Embedding API: prefs r/w
This commit is contained in:
parent
7df4655b60
commit
479afcfb8e
10 changed files with 460 additions and 157 deletions
|
@ -5,7 +5,6 @@
|
|||
//! Configuration options for a single run of the servo application. Created
|
||||
//! from command line arguments.
|
||||
|
||||
use crate::prefs::{self, PrefValue};
|
||||
use euclid::Size2D;
|
||||
use getopts::{Matches, Options};
|
||||
use servo_geometry::DeviceIndependentPixel;
|
||||
|
@ -640,18 +639,6 @@ pub fn from_cmdline_args(mut opts: Options, args: &[String]) -> ArgumentParsingR
|
|||
"Run as a content process and connect to the given pipe",
|
||||
"servo-ipc-channel.abcdefg",
|
||||
);
|
||||
opts.optmulti(
|
||||
"",
|
||||
"pref",
|
||||
"A preference to set to enable",
|
||||
"dom.bluetooth.enabled",
|
||||
);
|
||||
opts.optmulti(
|
||||
"",
|
||||
"pref",
|
||||
"A preference to set to enable",
|
||||
"dom.webgpu.enabled",
|
||||
);
|
||||
opts.optflag("b", "no-native-titlebar", "Do not use native titlebar");
|
||||
opts.optflag("w", "webrender", "Use webrender backend");
|
||||
opts.optopt("G", "graphics", "Select graphics backend (gl or es2)", "gl");
|
||||
|
@ -926,16 +913,6 @@ pub fn from_cmdline_args(mut opts: Options, args: &[String]) -> ArgumentParsingR
|
|||
|
||||
set_options(opts);
|
||||
|
||||
// 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 precedence
|
||||
|
||||
prefs::add_user_prefs();
|
||||
|
||||
for pref in opt_match.opt_strs("pref").iter() {
|
||||
parse_pref_from_command_line(pref);
|
||||
}
|
||||
|
||||
if let Some(layout_threads) = layout_threads {
|
||||
set_pref!(layout.threads, layout_threads as i64);
|
||||
}
|
||||
|
@ -965,31 +942,6 @@ pub fn get() -> RwLockReadGuard<'static, Opts> {
|
|||
OPTIONS.read().unwrap()
|
||||
}
|
||||
|
||||
pub fn parse_pref_from_command_line(pref: &str) {
|
||||
let split: Vec<&str> = pref.splitn(2, '=').collect();
|
||||
let pref_name = split[0];
|
||||
let pref_value = parse_cli_pref_value(split.get(1).cloned());
|
||||
prefs::pref_map()
|
||||
.set(pref_name, pref_value)
|
||||
.expect(format!("Error setting preference: {}", pref).as_str());
|
||||
}
|
||||
|
||||
fn parse_cli_pref_value(input: Option<&str>) -> PrefValue {
|
||||
match input {
|
||||
Some("true") | None => PrefValue::Bool(true),
|
||||
Some("false") => PrefValue::Bool(false),
|
||||
Some(string) => {
|
||||
if let Some(int) = string.parse::<i64>().ok() {
|
||||
PrefValue::Int(int)
|
||||
} else if let Some(float) = string.parse::<f64>().ok() {
|
||||
PrefValue::Float(float)
|
||||
} else {
|
||||
PrefValue::from(string)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_url_or_filename(cwd: &Path, input: &str) -> Result<ServoUrl, ()> {
|
||||
match ServoUrl::parse(input) {
|
||||
Ok(url) => Ok(url),
|
||||
|
|
|
@ -220,6 +220,17 @@ impl<'m, P: Clone> Preferences<'m, P> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Has the preference been modified from its original value?
|
||||
pub fn is_default(&self, key: &str) -> Result<bool, PrefError> {
|
||||
if let Some(accessor) = self.accessors.get(key) {
|
||||
let user = (accessor.getter)(&self.default_prefs);
|
||||
let default = (accessor.getter)(&self.user_prefs.read().unwrap());
|
||||
Ok(default == user)
|
||||
} else {
|
||||
Err(PrefError::NoSuchPref(String::from(key)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Creates an iterator over all keys and values
|
||||
pub fn iter<'a>(&'a self) -> impl Iterator<Item = (String, PrefValue)> + 'a {
|
||||
let prefs = self.user_prefs.read().unwrap();
|
||||
|
|
|
@ -2,15 +2,10 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use crate::basedir::default_config_dir;
|
||||
use crate::opts;
|
||||
use embedder_traits::resources::{self, Resource};
|
||||
use serde_json::{self, Value};
|
||||
use std::borrow::ToOwned;
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::{stderr, Read, Write};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::pref_util::Preferences;
|
||||
pub use crate::pref_util::{PrefError, PrefValue};
|
||||
|
@ -58,42 +53,12 @@ pub fn pref_map() -> &'static Preferences<'static, Prefs> {
|
|||
&PREFS
|
||||
}
|
||||
|
||||
pub(crate) fn add_user_prefs() {
|
||||
if let Some(path) = user_prefs_path() {
|
||||
init_user_prefs(path);
|
||||
pub fn add_user_prefs(prefs: HashMap<String, PrefValue>) {
|
||||
if let Err(error) = PREFS.set_all(prefs.into_iter()) {
|
||||
panic!("Error setting preference: {:?}", error);
|
||||
}
|
||||
}
|
||||
|
||||
fn user_prefs_path() -> Option<PathBuf> {
|
||||
opts::get()
|
||||
.config_dir
|
||||
.clone()
|
||||
.or_else(|| default_config_dir())
|
||||
.map(|path| path.join("prefs.json"))
|
||||
.filter(|path| path.exists())
|
||||
}
|
||||
|
||||
fn init_user_prefs(path: PathBuf) {
|
||||
if let Ok(mut file) = File::open(&path) {
|
||||
let mut txt = String::new();
|
||||
file.read_to_string(&mut txt)
|
||||
.expect("Can't read user prefs");
|
||||
match read_prefs_map(&txt) {
|
||||
Ok(prefs) => {
|
||||
if let Err(error) = PREFS.set_all(prefs.into_iter()) {
|
||||
writeln!(&mut stderr(), "Error setting preference: {:?}", error)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
},
|
||||
Err(error) => writeln!(&mut stderr(), "Error parsing prefs.json: {:?}", error),
|
||||
}
|
||||
} else {
|
||||
writeln!(&mut stderr(), "Error opening user prefs from {:?}", path)
|
||||
}
|
||||
.expect("failed printing to stderr");
|
||||
}
|
||||
|
||||
pub fn read_prefs_map(txt: &str) -> Result<HashMap<String, PrefValue>, PrefError> {
|
||||
let prefs: HashMap<String, Value> =
|
||||
serde_json::from_str(txt).map_err(|e| PrefError::JsonParseErr(e))?;
|
||||
|
|
|
@ -2,11 +2,7 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#[macro_use]
|
||||
extern crate servo_config;
|
||||
|
||||
use servo_config::opts::{parse_pref_from_command_line, parse_url_or_filename};
|
||||
use servo_config::{prefs, prefs::PrefValue};
|
||||
use servo_config::opts::parse_url_or_filename;
|
||||
use std::path::Path;
|
||||
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
|
@ -88,54 +84,3 @@ fn test_argument_parsing_special() {
|
|||
assert_eq!(url.query(), None);
|
||||
assert_eq!(url.fragment(), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_prefs_from_command_line_panics() {
|
||||
let err_msg = std::panic::catch_unwind(|| {
|
||||
parse_pref_from_command_line("doesntexist=true");
|
||||
})
|
||||
.err()
|
||||
.and_then(|a| a.downcast_ref::<String>().cloned())
|
||||
.expect("Should panic");
|
||||
assert!(
|
||||
err_msg.starts_with("Error setting preference"),
|
||||
"Message should describe the problem"
|
||||
);
|
||||
assert!(
|
||||
err_msg.contains("doesntexist"),
|
||||
"Message should mention the name of the preference"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_pref_from_command_line() {
|
||||
// Test with boolean values.
|
||||
parse_pref_from_command_line("dom.bluetooth.enabled=true");
|
||||
assert_eq!(
|
||||
prefs::pref_map().get("dom.bluetooth.enabled"),
|
||||
PrefValue::Bool(true)
|
||||
);
|
||||
assert_eq!(pref!(dom.bluetooth.enabled), true);
|
||||
|
||||
parse_pref_from_command_line("dom.bluetooth.enabled=false");
|
||||
assert_eq!(
|
||||
prefs::pref_map().get("dom.bluetooth.enabled"),
|
||||
PrefValue::Bool(false)
|
||||
);
|
||||
assert_eq!(pref!(dom.bluetooth.enabled), false);
|
||||
|
||||
// Test with numbers
|
||||
parse_pref_from_command_line("layout.threads=42");
|
||||
assert_eq!(pref!(layout.threads), 42);
|
||||
|
||||
// Test string.
|
||||
parse_pref_from_command_line("shell.homepage=str");
|
||||
assert_eq!(pref!(shell.homepage), "str");
|
||||
|
||||
// Test with no value (defaults to true).
|
||||
prefs::pref_map()
|
||||
.set("dom.bluetooth.enabled", false)
|
||||
.unwrap();
|
||||
parse_pref_from_command_line("dom.bluetooth.enabled");
|
||||
assert_eq!(pref!(dom.bluetooth.enabled), true);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue