api: Flatten and simplify Servo preferences (#34966)

Flatten and simplify Servo's preferences code. In addition, have both
preferences and options passed in as arguments to `Servo::new()` and
make sure not to use the globally set preferences in `servoshell` (as
much as possible now).

Instead of a complex procedural macro to generate preferences, just
expose a very simple derive macro that adds string based getters and
setters.

- All command-line parsing is moved to servoshell.
- There is no longer the concept of a missing preference.
- Preferences no longer have to be part of the resources bundle because
  they now have reasonable default values.
- servoshell specific preferences are no longer part of the preferences
  exposed by the Servo API.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-01-14 14:54:06 +01:00 committed by GitHub
parent c4c85affb5
commit 0e616e0c5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
316 changed files with 2088 additions and 3235 deletions

View file

@ -12,18 +12,11 @@ name = "servo_config"
path = "lib.rs"
[dependencies]
embedder_traits = { workspace = true }
euclid = { workspace = true }
getopts = { workspace = true }
log = { workspace = true }
num_cpus = { workspace = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true }
servo_config_plugins = { path = "../config_plugins" }
servo_config_macro = { path = "macro" }
servo_geometry = { path = "../geometry" }
servo_url = { path = "../url" }
style_config = { workspace = true }
url = { workspace = true }
[target.'cfg(not(any(target_os = "android", target_env = "ohos")))'.dependencies]
dirs = "5.0"

View file

@ -1,44 +0,0 @@
/* 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 https://mozilla.org/MPL/2.0/. */
//! Contains routines for retrieving default config directories.
//! For linux based platforms, it uses the XDG base directory spec but provides
//! similar abstractions for non-linux platforms.
use std::path::PathBuf;
#[cfg(all(
unix,
not(target_os = "macos"),
not(target_os = "ios"),
not(target_os = "android"),
not(target_env = "ohos")
))]
pub fn default_config_dir() -> Option<PathBuf> {
let mut config_dir = ::dirs::config_dir().unwrap();
config_dir.push("servo");
config_dir.push("default");
Some(config_dir)
}
#[cfg(any(target_os = "android", target_env = "ohos"))]
pub fn default_config_dir() -> Option<PathBuf> {
None
}
#[cfg(target_os = "macos")]
pub fn default_config_dir() -> Option<PathBuf> {
// FIXME: use `config_dir()` ($HOME/Library/Preferences)
// instead of `data_dir()` ($HOME/Library/Application Support) ?
let mut config_dir = ::dirs::data_dir().unwrap();
config_dir.push("Servo");
Some(config_dir)
}
#[cfg(target_os = "windows")]
pub fn default_config_dir() -> Option<PathBuf> {
let mut config_dir = ::dirs::config_dir().unwrap();
config_dir.push("Servo");
Some(config_dir)
}

View file

@ -4,9 +4,6 @@
#![deny(unsafe_code)]
pub mod opts;
pub mod pref_util;
pub mod prefs;
pub mod basedir;
#[allow(unsafe_code)]
pub mod opts;

View file

@ -0,0 +1,19 @@
[package]
name = "servo_config_macro"
version.workspace = true
authors.workspace = true
license.workspace = true
edition.workspace = true
publish.workspace = true
rust-version.workspace = true
[lib]
name = "servo_config_macro"
proc-macro = true
path = "lib.rs"
[dependencies]
proc-macro2 = { workspace = true }
quote = { workspace = true }
syn = { workspace = true }
synstructure = { workspace = true }

View file

@ -0,0 +1,55 @@
/* 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 https://mozilla.org/MPL/2.0/. */
use proc_macro2::TokenStream;
use quote::quote;
use syn::{Data, Fields};
use synstructure::decl_derive;
decl_derive!([ServoPreferences] => servo_preferences_derive);
/// A derive macro that adds string-based getter and setter for each field of this struct
/// (enums and other types are not supported). Each field must be able to be convertable
/// (with `into()`) into a `PrefValue`.
fn servo_preferences_derive(input: synstructure::Structure) -> TokenStream {
let ast = input.ast();
let Data::Struct(ref data) = ast.data else {
unimplemented!();
};
let Fields::Named(ref named_fields) = data.fields else {
unimplemented!()
};
let mut get_match_cases = quote!();
for field in named_fields.named.iter() {
let name = field.ident.as_ref().unwrap();
get_match_cases.extend(quote!(stringify!(#name) => self.#name.clone().into(),))
}
let mut set_match_cases = quote!();
for field in named_fields.named.iter() {
let name = field.ident.as_ref().unwrap();
set_match_cases.extend(quote!(stringify!(#name) => self.#name = value.try_into().unwrap(),))
}
let structure_name = &ast.ident;
quote! {
impl #structure_name {
pub fn get_value(&self, name: &str) -> PrefValue {
match name {
#get_match_cases
_ => { panic!("Unknown preference: {:?}", name); }
}
}
pub fn set_value(&mut self, name: &str, value: PrefValue) {
match name {
#set_match_cases
_ => { panic!("Unknown preference: {:?}", name); }
}
}
}
}
}

View file

@ -6,22 +6,13 @@
//! from command line arguments.
use std::default::Default;
use std::fs::{self, File};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::path::PathBuf;
use std::sync::{LazyLock, RwLock, RwLockReadGuard};
use std::{env, process};
use euclid::Size2D;
use getopts::{Matches, Options};
use log::error;
use serde::{Deserialize, Serialize};
use servo_geometry::DeviceIndependentPixel;
use servo_url::ServoUrl;
use url::{self, Url};
use crate::{pref, set_pref};
/// Global flags for Servo, currently set on the command line.
#[derive(Clone, Debug, Deserialize, Serialize)]
@ -69,13 +60,6 @@ pub struct Opts {
/// behavior for debugging purposes.
pub debug: DebugOptions,
/// Port number to start a server to listen to remote Firefox devtools connections.
/// 0 for random port.
pub devtools_port: u16,
/// Start the devtools server at startup
pub devtools_server_enabled: bool,
/// `None` to disable WebDriver or `Some` with a port number to start a server to listen to
/// remote WebDriver commands.
pub webdriver_port: Option<u16>,
@ -113,9 +97,6 @@ pub struct Opts {
/// Directory for a default config directory
pub config_dir: Option<PathBuf>,
/// Print the version and exit.
pub is_printing_version: bool,
/// Path to PEM encoded SSL CA certificate store.
pub certificate_path: Option<String>,
@ -137,14 +118,6 @@ pub struct Opts {
pub print_pwm: bool,
}
fn print_usage(app: &str, opts: &Options) {
let message = format!(
"Usage: {} [ options ... ] [URL]\n\twhere options include",
app
);
println!("{}", opts.usage(&message));
}
/// Debug options for Servo, currently set on the command line with -Z
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct DebugOptions {
@ -278,104 +251,6 @@ impl DebugOptions {
Ok(())
}
fn print_usage(app: &str) {
fn print_option(name: &str, description: &str) {
println!("\t{:<35} {}", name, description);
}
println!(
"Usage: {} debug option,[options,...]\n\twhere options include\n\nOptions:",
app
);
print_option(
"bubble-inline-sizes-separately",
"Bubble intrinsic widths separately like other engines.",
);
print_option(
"convert-mouse-to-touch",
"Send touch events instead of mouse events",
);
print_option(
"disable-canvas-aa",
"Disable antialiasing on the HTML canvas element.",
);
print_option(
"disable-share-style-cache",
"Disable the style sharing cache.",
);
print_option(
"disable-subpixel-aa",
"Disable subpixel text antialiasing overriding preference.",
);
print_option("disable-text-aa", "Disable antialiasing of rendered text.");
print_option(
"dump-stacking-context-tree",
"Print the stacking context tree after each layout.",
);
print_option(
"dump-display-list",
"Print the display list after each layout.",
);
print_option(
"dump-display-list-json",
"Print the display list in JSON form.",
);
print_option(
"dump-flow-tree",
"Print the flow tree (Layout 2013) or fragment tree (Layout 2020) after each layout.",
);
print_option(
"dump-rule-tree",
"Print the style rule tree after each layout.",
);
print_option(
"dump-style-tree",
"Print the DOM with computed styles after each restyle.",
);
print_option("dump-style-stats", "Print style statistics each restyle.");
print_option("gc-profile", "Log GC passes and their durations.");
print_option(
"load-webfonts-synchronously",
"Load web fonts synchronously to avoid non-deterministic network-driven reflows",
);
print_option(
"parallel-display-list-building",
"Build display lists in parallel.",
);
print_option("precache-shaders", "Compile all shaders during init.");
print_option(
"profile-script-events",
"Enable profiling of script-related events.",
);
print_option(
"relayout-event",
"Print notifications when there is a relayout.",
);
print_option("replace-surrogates", "Replace unpaires surrogates in DOM strings with U+FFFD. See https://github.com/servo/servo/issues/6564");
print_option(
"show-fragment-borders",
"Paint borders along fragment boundaries.",
);
print_option(
"show-parallel-layout",
"Mark which thread laid each flow out with colors.",
);
print_option(
"signpost",
"Emit native OS signposts for profile events (currently macOS only)",
);
print_option(
"trace-layout",
"Write layout trace to an external file for debugging.",
);
print_option("wr-stats", "Show WebRender profiler on screen.");
println!();
process::exit(0)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
@ -385,18 +260,6 @@ pub enum OutputOptions {
Stdout(f64),
}
fn args_fail(msg: &str) -> ! {
eprintln!("{}", msg);
process::exit(1)
}
static MULTIPROCESS: AtomicBool = AtomicBool::new(false);
#[inline]
pub fn multiprocess() -> bool {
MULTIPROCESS.load(Ordering::Relaxed)
}
pub fn default_opts() -> Opts {
Opts {
legacy_layout: false,
@ -410,8 +273,6 @@ pub fn default_opts() -> Opts {
output_file: None,
headless: false,
hard_fail: true,
devtools_port: 0,
devtools_server_enabled: false,
webdriver_port: None,
initial_window_size: Size2D::new(1024, 740),
screen_size_override: None,
@ -423,7 +284,6 @@ pub fn default_opts() -> Opts {
debug: Default::default(),
exit_after_load: false,
config_dir: None,
is_printing_version: false,
shaders_dir: None,
certificate_path: None,
ignore_certificate_errors: false,
@ -434,392 +294,12 @@ pub fn default_opts() -> Opts {
}
}
pub fn from_cmdline_args(mut opts: Options, args: &[String]) -> ArgumentParsingResult {
let (app_name, args) = args.split_first().unwrap();
opts.optflag("", "legacy-layout", "Use the legacy layout engine");
opts.optopt("o", "output", "Output file", "output.png");
opts.optopt("s", "size", "Size of tiles", "512");
opts.optflagopt(
"p",
"profile",
"Time profiler flag and either a TSV output filename \
OR an interval for output to Stdout (blank for Stdout with interval of 5s)",
"10 \
OR time.tsv",
);
opts.optflagopt(
"",
"profiler-trace-path",
"Path to dump a self-contained HTML timeline of profiler traces",
"",
);
opts.optflagopt(
"m",
"memory-profile",
"Memory profiler flag and output interval",
"10",
);
opts.optflag("x", "exit", "Exit after load flag");
opts.optopt(
"y",
"layout-threads",
"Number of threads to use for layout",
"1",
);
opts.optflag(
"i",
"nonincremental-layout",
"Enable to turn off incremental layout.",
);
opts.optflagopt(
"",
"userscripts",
"Uses userscripts in resources/user-agent-js, or a specified full path",
"",
);
opts.optmulti(
"",
"user-stylesheet",
"A user stylesheet to be added to every document",
"file.css",
);
opts.optopt(
"",
"shaders",
"Shaders will be loaded from the specified directory instead of using the builtin ones.",
"",
);
opts.optflag("z", "headless", "Headless mode");
opts.optflag(
"f",
"hard-fail",
"Exit on thread failure instead of displaying about:failure",
);
opts.optflag(
"F",
"soft-fail",
"Display about:failure on thread failure instead of exiting",
);
opts.optflagopt("", "devtools", "Start remote devtools server on port", "0");
opts.optflagopt(
"",
"webdriver",
"Start remote WebDriver server on port",
"7000",
);
opts.optopt(
"",
"window-size",
"Set the initial window size in logical (device independenrt) pixels",
"1024x740",
);
opts.optopt(
"",
"screen-size",
"Override the screen resolution in logical (device independent) pixels",
"1024x768",
);
opts.optflag("M", "multiprocess", "Run in multiprocess mode");
opts.optflag("B", "bhm", "Background Hang Monitor enabled");
opts.optflag("S", "sandbox", "Run in a sandbox if multiprocess");
opts.optopt(
"",
"random-pipeline-closure-probability",
"Probability of randomly closing a pipeline (for testing constellation hardening).",
"0.0",
);
opts.optopt(
"",
"random-pipeline-closure-seed",
"A fixed seed for repeatbility of random pipeline closure.",
"",
);
opts.optmulti(
"Z",
"debug",
"A comma-separated string of debug options. Pass help to show available options.",
"",
);
opts.optflag("h", "help", "Print this message");
opts.optopt(
"",
"resources-path",
"Path to find static resources",
"/home/servo/resources",
);
opts.optopt(
"",
"certificate-path",
"Path to find SSL certificates",
"/home/servo/resources/certs",
);
opts.optflag(
"",
"ignore-certificate-errors",
"Whether or not to completely ignore certificate errors",
);
opts.optopt(
"",
"content-process",
"Run as a content process and connect to the given pipe",
"servo-ipc-channel.abcdefg",
);
opts.optopt(
"",
"config-dir",
"config directory following xdg spec on linux platform",
"",
);
opts.optflag("v", "version", "Display servo version information");
opts.optflag("", "unminify-js", "Unminify Javascript");
opts.optflag("", "print-pwm", "Print Progressive Web Metrics");
opts.optopt(
"",
"local-script-source",
"Directory root with unminified scripts",
"",
);
opts.optflag("", "unminify-css", "Unminify Css");
let opt_match = match opts.parse(args) {
Ok(m) => m,
Err(f) => args_fail(&f.to_string()),
};
if opt_match.opt_present("h") || opt_match.opt_present("help") {
print_usage(app_name, &opts);
process::exit(0);
};
// If this is the content process, we'll receive the real options over IPC. So just fill in
// some dummy options for now.
if let Some(content_process) = opt_match.opt_str("content-process") {
MULTIPROCESS.store(true, Ordering::SeqCst);
return ArgumentParsingResult::ContentProcess(opt_match, content_process);
}
let mut debug_options = DebugOptions::default();
for debug_string in opt_match.opt_strs("Z") {
if let Err(e) = debug_options.extend(debug_string) {
args_fail(&format!("error: unrecognized debug option: {}", e));
}
}
if debug_options.help {
DebugOptions::print_usage(app_name)
}
let tile_size: usize = match opt_match.opt_str("s") {
Some(tile_size_str) => tile_size_str
.parse()
.unwrap_or_else(|err| args_fail(&format!("Error parsing option: -s ({})", err))),
None => 512,
};
// If only the flag is present, default to a 5 second period for both profilers
let time_profiling = if opt_match.opt_present("p") {
match opt_match.opt_str("p") {
Some(argument) => match argument.parse::<f64>() {
Ok(interval) => Some(OutputOptions::Stdout(interval)),
Err(_) => match ServoUrl::parse(&argument) {
Ok(_) => panic!("influxDB isn't supported anymore"),
Err(_) => Some(OutputOptions::FileName(argument)),
},
},
None => Some(OutputOptions::Stdout(5.0)),
}
} else {
// if the p option doesn't exist:
None
};
if let Some(ref time_profiler_trace_path) = opt_match.opt_str("profiler-trace-path") {
let mut path = PathBuf::from(time_profiler_trace_path);
path.pop();
if let Err(why) = fs::create_dir_all(&path) {
error!(
"Couldn't create/open {:?}: {:?}",
Path::new(time_profiler_trace_path).to_string_lossy(),
why
);
}
}
let mem_profiler_period = opt_match.opt_default("m", "5").map(|period| {
period
.parse()
.unwrap_or_else(|err| args_fail(&format!("Error parsing option: -m ({})", err)))
});
let mut layout_threads: Option<usize> = opt_match.opt_str("y").map(|layout_threads_str| {
layout_threads_str
.parse()
.unwrap_or_else(|err| args_fail(&format!("Error parsing option: -y ({})", err)))
});
let nonincremental_layout = opt_match.opt_present("i");
let random_pipeline_closure_probability = opt_match
.opt_str("random-pipeline-closure-probability")
.map(|prob| {
prob.parse().unwrap_or_else(|err| {
args_fail(&format!(
"Error parsing option: --random-pipeline-closure-probability ({})",
err
))
})
});
let random_pipeline_closure_seed =
opt_match
.opt_str("random-pipeline-closure-seed")
.map(|seed| {
seed.parse().unwrap_or_else(|err| {
args_fail(&format!(
"Error parsing option: --random-pipeline-closure-seed ({})",
err
))
})
});
if debug_options.trace_layout {
layout_threads = Some(1);
}
let (devtools_server_enabled, devtools_port) = if opt_match.opt_present("devtools") {
let port = opt_match
.opt_str("devtools")
.map(|port| {
port.parse().unwrap_or_else(|err| {
args_fail(&format!("Error parsing option: --devtools ({})", err))
})
})
.unwrap_or(pref!(devtools.server.port));
(true, port as u16)
} else {
(
pref!(devtools.server.enabled),
pref!(devtools.server.port) as u16,
)
};
let webdriver_port = opt_match.opt_default("webdriver", "7000").map(|port| {
port.parse().unwrap_or_else(|err| {
args_fail(&format!("Error parsing option: --webdriver ({})", err))
})
});
let parse_resolution_string = |string: String| {
let components: Vec<u32> = string
.split('x')
.map(|component| {
component.parse().unwrap_or_else(|error| {
args_fail(&format!("Error parsing resolution '{string}': {error}"));
})
})
.collect();
Size2D::new(components[0], components[1])
};
let screen_size_override = opt_match
.opt_str("screen-size")
.map(parse_resolution_string);
// Make sure the default window size is not larger than any provided screen size.
let default_window_size = Size2D::new(1024, 740);
let default_window_size = screen_size_override
.map_or(default_window_size, |screen_size_override| {
default_window_size.min(screen_size_override)
});
let initial_window_size = opt_match
.opt_str("window-size")
.map_or(default_window_size, parse_resolution_string);
if opt_match.opt_present("M") {
MULTIPROCESS.store(true, Ordering::SeqCst)
}
let user_stylesheets = opt_match
.opt_strs("user-stylesheet")
.iter()
.map(|filename| {
let cwd = env::current_dir().unwrap();
let path = cwd.join(filename);
let url = ServoUrl::from_url(Url::from_file_path(&path).unwrap());
let mut contents = Vec::new();
File::open(path)
.unwrap_or_else(|err| args_fail(&format!("Couldn't open {}: {}", filename, err)))
.read_to_end(&mut contents)
.unwrap_or_else(|err| args_fail(&format!("Couldn't read {}: {}", filename, err)));
(contents, url)
})
.collect();
let is_printing_version = opt_match.opt_present("v") || opt_match.opt_present("version");
let legacy_layout = opt_match.opt_present("legacy-layout");
if legacy_layout {
set_pref!(layout.legacy_layout, true);
}
let opts = Opts {
debug: debug_options.clone(),
legacy_layout,
tile_size,
time_profiling,
time_profiler_trace_path: opt_match.opt_str("profiler-trace-path"),
mem_profiler_period,
nonincremental_layout,
userscripts: opt_match.opt_default("userscripts", ""),
user_stylesheets,
output_file: opt_match.opt_str("o"),
headless: opt_match.opt_present("z"),
hard_fail: opt_match.opt_present("f") && !opt_match.opt_present("F"),
devtools_port,
devtools_server_enabled,
webdriver_port,
initial_window_size,
screen_size_override,
multiprocess: opt_match.opt_present("M"),
background_hang_monitor: opt_match.opt_present("B"),
sandbox: opt_match.opt_present("S"),
random_pipeline_closure_probability,
random_pipeline_closure_seed,
exit_after_load: opt_match.opt_present("x"),
config_dir: opt_match.opt_str("config-dir").map(Into::into),
is_printing_version,
shaders_dir: opt_match.opt_str("shaders").map(Into::into),
certificate_path: opt_match.opt_str("certificate-path"),
ignore_certificate_errors: opt_match.opt_present("ignore-certificate-errors"),
unminify_js: opt_match.opt_present("unminify-js"),
local_script_source: opt_match.opt_str("local-script-source"),
unminify_css: opt_match.opt_present("unminify-css"),
print_pwm: opt_match.opt_present("print-pwm"),
};
set_options(opts);
if let Some(layout_threads) = layout_threads {
set_pref!(layout.threads, layout_threads as i64);
}
ArgumentParsingResult::ChromeProcess(opt_match)
}
pub enum ArgumentParsingResult {
ChromeProcess(Matches),
ContentProcess(Matches, String),
}
// 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.
static OPTIONS: LazyLock<RwLock<Opts>> = LazyLock::new(|| RwLock::new(default_opts()));
pub fn set_options(opts: Opts) {
MULTIPROCESS.store(opts.multiprocess, Ordering::SeqCst);
*OPTIONS.write().unwrap() = opts;
}

View file

@ -2,11 +2,6 @@
* 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 std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use std::sync::RwLock;
use serde::{Deserialize, Serialize};
use serde_json::Value;
@ -17,70 +12,46 @@ pub enum PrefValue {
Str(String),
Bool(bool),
Array(Vec<PrefValue>),
Missing,
}
impl PrefValue {
pub fn as_str(&self) -> Option<&str> {
if let PrefValue::Str(val) = self {
Some(val)
} else {
None
}
}
pub fn as_i64(&self) -> Option<i64> {
if let PrefValue::Int(val) = self {
Some(*val)
} else {
None
}
}
pub fn as_f64(&self) -> Option<f64> {
if let PrefValue::Float(val) = self {
Some(*val)
} else {
None
}
}
pub fn as_bool(&self) -> Option<bool> {
if let PrefValue::Bool(val) = self {
Some(*val)
} else {
None
}
}
pub fn is_missing(&self) -> bool {
matches!(self, PrefValue::Missing)
}
pub fn from_json_value(value: &Value) -> Option<Self> {
match value {
Value::Bool(b) => Some(PrefValue::Bool(*b)),
Value::Number(n) if n.is_i64() => Some(PrefValue::Int(n.as_i64().unwrap())),
Value::Number(n) if n.is_f64() => Some(PrefValue::Float(n.as_f64().unwrap())),
Value::String(s) => Some(PrefValue::Str(s.to_owned())),
_ => None,
pub fn from_booleanish_str(input: &str) -> Self {
match input {
"false" => PrefValue::Bool(false),
"true" => PrefValue::Bool(true),
_ => input
.parse::<i64>()
.map(PrefValue::Int)
.or_else(|_| input.parse::<f64>().map(PrefValue::Float))
.unwrap_or_else(|_| PrefValue::from(input)),
}
}
}
impl FromStr for PrefValue {
type Err = PrefError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "false" {
Ok(PrefValue::Bool(false))
} else if s == "true" {
Ok(PrefValue::Bool(true))
} else if let Ok(float) = s.parse::<f64>() {
Ok(PrefValue::Float(float))
} else if let Ok(integer) = s.parse::<i64>() {
Ok(PrefValue::Int(integer))
} else {
Ok(PrefValue::from(s))
impl TryFrom<&Value> for PrefValue {
type Error = String;
fn try_from(value: &Value) -> Result<Self, Self::Error> {
match value {
Value::Null => Err("Cannot turn null into preference".into()),
Value::Bool(value) => Ok((*value).into()),
Value::Number(number) => number
.as_i64()
.map(Into::into)
.or_else(|| number.as_f64().map(Into::into))
.map(Ok)
.unwrap_or(Err("Could not parse number from JSON".into())),
Value::String(value) => Ok(value.clone().into()),
Value::Array(array) => {
let mut array = array.iter().map(TryInto::try_into);
if !array.all(|v| v.is_ok()) {
return Err(format!(
"Cannot turn all array avlues into preference: {array:?}"
));
}
Ok(PrefValue::Array(array.map(Result::unwrap).collect()))
},
Value::Object(_) => Err("Cannot turn object into preference".into()),
}
}
}
@ -94,37 +65,18 @@ macro_rules! impl_pref_from {
}
}
)+
$(
impl From<Option<$t>> for PrefValue {
fn from(other: Option<$t>) -> Self {
other.map(|val| $variant(val.into())).unwrap_or(PrefValue::Missing)
}
}
)+
}
}
macro_rules! impl_from_pref {
($($variant: path => $t: ty,)*) => {
$(
impl From<PrefValue> for $t {
#[allow(unsafe_code)]
fn from(other: PrefValue) -> Self {
if let $variant(value) = other {
value.into()
} else {
panic!("Cannot convert {:?} to {:?}", other, std::any::type_name::<$t>())
}
}
}
)+
$(
impl From<PrefValue> for Option<$t> {
fn from(other: PrefValue) -> Self {
if let PrefValue::Missing = other {
None
} else {
Some(other.into())
impl TryFrom<PrefValue> for $t {
type Error = String;
fn try_from(other: PrefValue) -> Result<Self, Self::Error> {
match other {
$variant(value) => Ok(value.into()),
_ => Err(format!("Cannot convert {other:?} to {}", std::any::type_name::<$t>())),
}
}
}
@ -157,9 +109,13 @@ impl From<PrefValue> for [f64; 4] {
fn from(other: PrefValue) -> [f64; 4] {
match other {
PrefValue::Array(values) if values.len() == 4 => {
let f = values.into_iter().map(Into::into).collect::<Vec<f64>>();
if f.len() == 4 {
[f[0], f[1], f[2], f[3]]
let values: Vec<f64> = values
.into_iter()
.map(TryFrom::try_from)
.filter_map(Result::ok)
.collect();
if values.len() == 4 {
[values[0], values[1], values[2], values[3]]
} else {
panic!(
"Cannot convert PrefValue to {:?}",
@ -176,144 +132,20 @@ impl From<PrefValue> for [f64; 4] {
}
}
#[derive(Debug)]
pub enum PrefError {
NoSuchPref(String),
InvalidValue(String),
JsonParseErr(serde_json::error::Error),
}
impl fmt::Display for PrefError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PrefError::NoSuchPref(s) | PrefError::InvalidValue(s) => f.write_str(s),
PrefError::JsonParseErr(e) => e.fmt(f),
}
}
}
impl std::error::Error for PrefError {}
pub struct Accessor<P, V> {
pub getter: Box<dyn Fn(&P) -> V + Sync + Send>,
#[allow(clippy::type_complexity)]
pub setter: Box<dyn Fn(&mut P, V) + Sync + Send>,
}
impl<P, V> Accessor<P, V> {
pub fn new<G, S>(getter: G, setter: S) -> Self
where
G: Fn(&P) -> V + Sync + Send + 'static,
S: Fn(&mut P, V) + Sync + Send + 'static,
{
Accessor {
getter: Box::new(getter),
setter: Box::new(setter),
}
}
}
pub struct Preferences<'m, P> {
user_prefs: RwLock<P>,
default_prefs: P,
accessors: &'m HashMap<String, Accessor<P, PrefValue>>,
}
impl<'m, P: Clone> Preferences<'m, P> {
/// Create a new `Preferences` object. The values provided in `default_prefs` are immutable and
/// can always be restored using `reset` or `reset_all`.
pub fn new(default_prefs: P, accessors: &'m HashMap<String, Accessor<P, PrefValue>>) -> Self {
Self {
user_prefs: RwLock::new(default_prefs.clone()),
default_prefs,
accessors,
}
}
/// Access to the data structure holding the preference values.
pub fn values(&self) -> &RwLock<P> {
&self.user_prefs
}
/// Retrieve a preference using its key
pub fn get(&self, key: &str) -> PrefValue {
if let Some(accessor) = self.accessors.get(key) {
let prefs = self.user_prefs.read().unwrap();
(accessor.getter)(&prefs)
} else {
PrefValue::Missing
}
}
/// 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(&self) -> impl Iterator<Item = (String, PrefValue)> + '_ {
let prefs = self.user_prefs.read().unwrap();
self.accessors
.iter()
.map(move |(k, accessor)| (k.clone(), (accessor.getter)(&prefs)))
}
/// Creates an iterator over all keys
pub fn keys(&self) -> impl Iterator<Item = &'_ str> {
self.accessors.keys().map(String::as_str)
}
fn set_inner<V>(&self, key: &str, prefs: &mut P, val: V) -> Result<(), PrefError>
where
V: Into<PrefValue>,
{
if let Some(accessor) = self.accessors.get(key) {
(accessor.setter)(prefs, val.into());
Ok(())
} else {
Err(PrefError::NoSuchPref(String::from(key)))
}
}
/// Set a new value for a preference, using its key.
pub fn set<V>(&self, key: &str, val: V) -> Result<(), PrefError>
where
V: Into<PrefValue>,
{
let mut prefs = self.user_prefs.write().unwrap();
self.set_inner(key, &mut prefs, val)
}
pub fn set_all<M>(&self, values: M) -> Result<(), PrefError>
where
M: IntoIterator<Item = (String, PrefValue)>,
{
let mut prefs = self.user_prefs.write().unwrap();
for (k, v) in values.into_iter() {
self.set_inner(&k, &mut prefs, v)?;
}
Ok(())
}
pub fn reset(&self, key: &str) -> Result<PrefValue, PrefError> {
if let Some(accessor) = self.accessors.get(key) {
let mut prefs = self.user_prefs.write().unwrap();
let old_pref = (accessor.getter)(&prefs);
let default_pref = (accessor.getter)(&self.default_prefs);
(accessor.setter)(&mut prefs, default_pref);
Ok(old_pref)
} else {
Err(PrefError::NoSuchPref(String::from(key)))
}
}
pub fn reset_all(&self) {
*self.user_prefs.write().unwrap() = self.default_prefs.clone();
}
#[test]
fn test_pref_value_from_str() {
let value = PrefValue::from_booleanish_str("21");
assert_eq!(value, PrefValue::Int(21));
let value = PrefValue::from_booleanish_str("12.5");
assert_eq!(value, PrefValue::Float(12.5));
let value = PrefValue::from_booleanish_str("a string");
assert_eq!(value, PrefValue::Str("a string".into()));
let value = PrefValue::from_booleanish_str("false");
assert_eq!(value, PrefValue::Bool(false));
let value = PrefValue::from_booleanish_str("true");
assert_eq!(value, PrefValue::Bool(true));
}

File diff suppressed because it is too large Load diff

View file

@ -1,333 +0,0 @@
/* 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 https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::error::Error;
use std::fs::{self, File};
use std::io::{Read, Write};
use servo_config::basedir;
use servo_config::pref_util::Preferences;
use servo_config::prefs::{read_prefs_map, PrefValue};
#[test]
fn test_create_prefs_map() {
let json_str = "{
\"layout.writing-mode.enabled\": true,
\"network.mime.sniff\": false,
\"shell.homepage\": \"https://servo.org\"
}";
let prefs = read_prefs_map(json_str);
assert!(prefs.is_ok());
let prefs = prefs.unwrap();
assert_eq!(prefs.len(), 3);
}
#[test]
fn test_generated_accessors_get() {
let prefs: gen::TestPrefs = serde_json::from_str(DEF_JSON_STR).unwrap();
let map: HashMap<String, PrefValue> = gen::TEST_PREF_ACCESSORS
.iter()
.map(move |(key, accessor)| {
let pref_value = (accessor.getter)(&prefs);
(key.clone(), pref_value)
})
.collect();
assert_eq!(&PrefValue::from("hello"), map.get("pref_string").unwrap());
assert_eq!(&PrefValue::from(23_i64), map.get("pref_i64").unwrap());
assert_eq!(&PrefValue::from(1.5_f64), map.get("pref_f64").unwrap());
assert_eq!(&PrefValue::from(true), map.get("pref_bool").unwrap());
assert_eq!(
&PrefValue::from(333_i64),
map.get("group.nested.nested_i64").unwrap()
);
assert_eq!(&PrefValue::from(42_i64), map.get("a.renamed.pref").unwrap());
}
#[test]
fn test_generated_accessors_set() {
let mut prefs: gen::TestPrefs = serde_json::from_str(DEF_JSON_STR).unwrap();
let setters: HashMap<String, _> = gen::TEST_PREF_ACCESSORS
.iter()
.map(|(key, accessor)| (key.clone(), &accessor.setter))
.collect();
(setters.get("pref_string").unwrap())(&mut prefs, PrefValue::Str(String::from("boo")));
(setters.get("pref_i64").unwrap())(&mut prefs, PrefValue::Int(-25));
(setters.get("pref_f64").unwrap())(&mut prefs, PrefValue::Float(-1.9));
(setters.get("pref_bool").unwrap())(&mut prefs, PrefValue::Bool(false));
(setters.get("group.nested.nested_i64").unwrap())(&mut prefs, PrefValue::Int(10));
(setters.get("a.renamed.pref").unwrap())(&mut prefs, PrefValue::Int(11));
assert_eq!("boo", prefs.pref_string);
assert_eq!(-25, prefs.pref_i64);
assert_eq!(-1.9, prefs.pref_f64);
assert_eq!(false, prefs.pref_bool);
assert_eq!(10, prefs.group.nested.nested_i64);
assert_eq!(11, prefs.group.nested.renamed);
}
#[test]
fn test_static_struct() {
let prefs: gen::TestPrefs = serde_json::from_str(DEF_JSON_STR).unwrap();
assert_eq!("hello", prefs.pref_string);
assert_eq!(23, prefs.pref_i64);
assert_eq!(1.5, prefs.pref_f64);
assert_eq!(true, prefs.pref_bool);
assert_eq!(333, prefs.group.nested.nested_i64);
assert_eq!(42, prefs.group.nested.renamed);
}
#[test]
fn test_set_pref() {
let prefs = Preferences::new(gen::TestPrefs::default(), &gen::TEST_PREF_ACCESSORS);
assert_eq!(Some(0), prefs.get("group.nested.nested_i64").as_i64());
let result = prefs.set("group.nested.nested_i64", 1);
assert_eq!(true, result.is_ok());
assert_eq!(Some(1), prefs.get("group.nested.nested_i64").as_i64());
assert_eq!(1, prefs.values().read().unwrap().group.nested.nested_i64);
}
#[test]
fn test_set_unknown_pref_is_err() -> Result<(), Box<dyn Error>> {
let prefs = Preferences::new(gen::TestPrefs::default(), &gen::TEST_PREF_ACCESSORS);
let result = prefs.set("unknown_pref", 1);
assert_eq!(true, result.is_err());
Ok(())
}
#[test]
fn test_reset_pref() -> Result<(), Box<dyn Error>> {
let mut def_prefs = gen::TestPrefs::default();
def_prefs.group.nested.nested_i64 = 999;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
assert_eq!(Some(999), prefs.get("group.nested.nested_i64").as_i64());
prefs.set("group.nested.nested_i64", 1)?;
assert_eq!(Some(1), prefs.get("group.nested.nested_i64").as_i64());
prefs.reset("group.nested.nested_i64")?;
assert_eq!(Some(999), prefs.get("group.nested.nested_i64").as_i64());
assert_eq!(999, prefs.values().read().unwrap().group.nested.nested_i64);
Ok(())
}
#[test]
fn test_default_values() -> Result<(), Box<dyn Error>> {
let def_prefs: gen::TestPrefs = serde_json::from_str(DEF_JSON_STR)?;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
assert_eq!(Some(0), prefs.get("default_value").as_i64());
assert_eq!(Some(555), prefs.get("computed_default_value").as_i64());
Ok(())
}
#[test]
fn test_override_default_values() -> Result<(), Box<dyn Error>> {
let def_prefs: gen::TestPrefs = serde_json::from_str(WITHOUT_DEFAULTS_JSON_STR)?;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
assert_eq!(Some(-1), prefs.get("default_value").as_i64());
assert_eq!(Some(-1), prefs.get("computed_default_value").as_i64());
Ok(())
}
#[test]
fn test_update_reset_default_values() -> Result<(), Box<dyn Error>> {
let def_prefs: gen::TestPrefs = serde_json::from_str(DEF_JSON_STR)?;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
prefs.set("default_value", 99)?;
prefs.set("computed_default_value", 199)?;
assert_eq!(Some(99), prefs.get("default_value").as_i64());
assert_eq!(Some(199), prefs.get("computed_default_value").as_i64());
prefs.reset("default_value")?;
prefs.reset("computed_default_value")?;
assert_eq!(Some(0), prefs.get("default_value").as_i64());
assert_eq!(Some(555), prefs.get("computed_default_value").as_i64());
Ok(())
}
#[test]
fn test_update_reset_overridden_default_values() -> Result<(), Box<dyn Error>> {
let def_prefs: gen::TestPrefs = serde_json::from_str(WITHOUT_DEFAULTS_JSON_STR)?;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
prefs.set("default_value", 99)?;
prefs.set("computed_default_value", 199)?;
assert_eq!(Some(99), prefs.get("default_value").as_i64());
assert_eq!(Some(199), prefs.get("computed_default_value").as_i64());
prefs.reset("default_value")?;
prefs.reset("computed_default_value")?;
assert_eq!(Some(-1), prefs.get("default_value").as_i64());
assert_eq!(Some(-1), prefs.get("computed_default_value").as_i64());
Ok(())
}
#[test]
fn test_user_prefs_override_and_reset() -> Result<(), Box<dyn Error>> {
let mut def_prefs = gen::TestPrefs::default();
def_prefs.group.nested.nested_i64 = 999;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
prefs.set("group.nested.nested_i64", 45)?;
assert_eq!(Some(45), prefs.get("group.nested.nested_i64").as_i64());
prefs.reset("group.nested.nested_i64")?;
assert_eq!(Some(999), prefs.get("group.nested.nested_i64").as_i64());
Ok(())
}
#[test]
fn test_reset_all() -> Result<(), Box<dyn Error>> {
let def_prefs: gen::TestPrefs = serde_json::from_str(DEF_JSON_STR)?;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
prefs.set_all(read_prefs_map(USER_JSON_STR)?)?;
let values = prefs.values();
assert_eq!("bye", values.read().unwrap().pref_string);
assert_eq!(-1, values.read().unwrap().pref_i64);
assert_eq!(-1.0, values.read().unwrap().pref_f64);
assert_eq!(false, values.read().unwrap().pref_bool);
assert_eq!(-1, values.read().unwrap().group.nested.nested_i64);
assert_eq!(-1, values.read().unwrap().group.nested.renamed);
prefs.reset_all();
let values = prefs.values();
assert_eq!("hello", values.read().unwrap().pref_string);
assert_eq!(23, values.read().unwrap().pref_i64);
assert_eq!(1.5, values.read().unwrap().pref_f64);
assert_eq!(true, values.read().unwrap().pref_bool);
assert_eq!(333, values.read().unwrap().group.nested.nested_i64);
assert_eq!(42, values.read().unwrap().group.nested.renamed);
Ok(())
}
#[test]
fn test_set_all_from_map() -> Result<(), Box<dyn Error>> {
let def_prefs: gen::TestPrefs = serde_json::from_str(DEF_JSON_STR)?;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
prefs.set_all(read_prefs_map(USER_JSON_STR)?)?;
let mut overrides = HashMap::new();
overrides.insert(String::from("pref_string"), PrefValue::from("new value"));
overrides.insert(
String::from("group.nested.nested_i64"),
PrefValue::from(1001),
);
overrides.insert(String::from("a.renamed.pref"), PrefValue::from(47));
let result = prefs.set_all(overrides.into_iter());
assert_eq!(true, result.is_ok());
let values = prefs.values();
assert_eq!("new value", values.read().unwrap().pref_string);
assert_eq!(1001, values.read().unwrap().group.nested.nested_i64);
assert_eq!(47, values.read().unwrap().group.nested.renamed);
Ok(())
}
#[test]
fn test_set_all_error_on_unknown_field() -> Result<(), Box<dyn Error>> {
let def_prefs: gen::TestPrefs = serde_json::from_str(DEF_JSON_STR)?;
let prefs = Preferences::new(def_prefs, &gen::TEST_PREF_ACCESSORS);
let mut overrides = HashMap::new();
overrides.insert(String::from("doesnt_exist"), PrefValue::from(1001));
let result = prefs.set_all(overrides.into_iter());
assert_eq!(true, result.is_err());
Ok(())
}
#[cfg(not(target_os = "android"))]
#[test]
fn test_default_config_dir_create_read_write() {
let json_str = "{\
\"layout.writing-mode.enabled\": true,\
\"extra.stuff\": false,\
\"shell.homepage\": \"https://google.com\"\
}";
let mut expected_json = String::new();
let config_path = basedir::default_config_dir().unwrap();
if !config_path.exists() {
fs::create_dir_all(&config_path).unwrap();
}
let json_path = config_path.join("test_config.json");
let mut fd = File::create(&json_path).unwrap();
assert_eq!(json_path.exists(), true);
fd.write_all(json_str.as_bytes()).unwrap();
let mut fd = File::open(&json_path).unwrap();
fd.read_to_string(&mut expected_json).unwrap();
assert_eq!(json_str, expected_json);
fs::remove_file(&json_path).unwrap();
}
static DEF_JSON_STR: &'static str = r#"{
"pref_string": "hello",
"pref_i64": 23,
"pref_f64": 1.5,
"pref_bool": true,
"group.nested.nested_i64": 333,
"a.renamed.pref": 42
}"#;
static USER_JSON_STR: &'static str = r#"{
"pref_string": "bye",
"pref_i64": -1,
"pref_f64": -1.0,
"pref_bool": false,
"group.nested.nested_i64": -1,
"a.renamed.pref": -1
}"#;
static WITHOUT_DEFAULTS_JSON_STR: &'static str = r#"{
"pref_string": "bye",
"pref_i64": -1,
"pref_f64": -1.0,
"pref_bool": false,
"group.nested.nested_i64": -1,
"a.renamed.pref": -1,
"computed_default_value": -1,
"default_value": -1
}"#;
mod gen {
use serde::{Deserialize, Serialize};
use servo_config::pref_util::{Accessor, PrefValue};
use servo_config_plugins::build_structs;
fn compute_default() -> i64 {
555
}
build_structs! {
accessor_type = Accessor::<TestPrefs, PrefValue>,
gen_accessors = TEST_PREF_ACCESSORS,
gen_types = TestPrefs {
pref_string: String,
pref_i64: i64,
pref_f64: f64,
pref_bool: bool,
#[serde(default)]
default_value: i64,
#[serde(default = "compute_default")]
computed_default_value: i64,
group: {
nested: {
nested_i64: i64,
#[serde(rename = "a.renamed.pref")]
renamed: i64,
}
}
}
}
}