This commit is contained in:
Narfinger 2025-06-04 09:28:05 +02:00 committed by GitHub
commit 177228f653
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 471 additions and 471 deletions

31
Cargo.lock generated
View file

@ -667,6 +667,26 @@ dependencies = [
"hex", "hex",
] ]
[[package]]
name = "bpaf"
version = "0.9.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "473976d7a8620bb1e06dcdd184407c2363fe4fec8e983ee03ed9197222634a31"
dependencies = [
"bpaf_derive",
]
[[package]]
name = "bpaf_derive"
version = "0.5.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fefb4feeec9a091705938922f26081aad77c64cd2e76cd1c4a9ece8e42e1618a"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "brotli" name = "brotli"
version = "8.0.1" version = "8.0.1"
@ -2497,15 +2517,6 @@ dependencies = [
"windows-targets 0.48.5", "windows-targets 0.48.5",
] ]
[[package]]
name = "getopts"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14dbbfd5c71d70241ecf9e6f13737f7b5ce823821063188d7e46c41d371eebd5"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "getrandom" name = "getrandom"
version = "0.2.16" version = "0.2.16"
@ -7008,6 +7019,7 @@ version = "0.0.1"
dependencies = [ dependencies = [
"android_logger", "android_logger",
"backtrace", "backtrace",
"bpaf",
"cc", "cc",
"cfg-if", "cfg-if",
"dirs", "dirs",
@ -7018,7 +7030,6 @@ dependencies = [
"egui_glow", "egui_glow",
"env_filter", "env_filter",
"euclid", "euclid",
"getopts",
"gilrs", "gilrs",
"glow", "glow",
"headers 0.4.1", "headers 0.4.1",

View file

@ -58,7 +58,6 @@ fnv = "1.0"
fonts_traits = { path = "components/shared/fonts" } fonts_traits = { path = "components/shared/fonts" }
freetype-sys = "0.20" freetype-sys = "0.20"
fxhash = "0.2" fxhash = "0.2"
getopts = "0.2.11"
gleam = "0.15" gleam = "0.15"
glib = "0.19" glib = "0.19"
glow = "0.16" glow = "0.16"

View file

@ -172,7 +172,7 @@ impl DebugOptions {
} }
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub enum OutputOptions { pub enum OutputOptions {
/// Database connection config (hostname, name, user, pass) /// Database connection config (hostname, name, user, pass)
FileName(String), FileName(String),

View file

@ -54,10 +54,10 @@ webgpu = ["libservo/webgpu"]
webxr = ["libservo/webxr"] webxr = ["libservo/webxr"]
[dependencies] [dependencies]
bpaf = { version = "0.9.20", features = ["derive"] }
cfg-if = { workspace = true } cfg-if = { workspace = true }
dpi = { workspace = true } dpi = { workspace = true }
euclid = { workspace = true } euclid = { workspace = true }
getopts = { workspace = true }
hitrace = { workspace = true, optional = true } hitrace = { workspace = true, optional = true }
image = { workspace = true } image = { workspace = true }
keyboard-types = { workspace = true } keyboard-types = { workspace = true }

View file

@ -24,6 +24,12 @@ pub fn main() {
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => { ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => {
(opts, preferences, servoshell_preferences) (opts, preferences, servoshell_preferences)
}, },
ArgumentParsingResult::Exit => {
std::process::exit(0);
},
ArgumentParsingResult::ErrorParsing => {
std::process::exit(1);
},
}; };
crate::init_tracing(servoshell_preferences.tracing_filter.as_deref()); crate::init_tracing(servoshell_preferences.tracing_filter.as_deref());

View file

@ -55,6 +55,10 @@ pub fn init(
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => { ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => {
(opts, preferences, servoshell_preferences) (opts, preferences, servoshell_preferences)
}, },
ArgumentParsingResult::Exit => {
std::process::exit(0);
},
ArgumentParsingResult::ErrorParsing => std::process::exit(1),
}; };
crate::init_tracing(servoshell_preferences.tracing_filter.as_deref()); crate::init_tracing(servoshell_preferences.tracing_filter.as_deref());

View file

@ -88,6 +88,8 @@ pub fn init(
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => { ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => {
(opts, preferences, servoshell_preferences) (opts, preferences, servoshell_preferences)
}, },
ArgumentParsingResult::Exit => std::process::exit(0),
ArgumentParsingResult::ErrorParsing => std::process::exit(1),
}; };
crate::init_tracing(servoshell_preferences.tracing_filter.as_deref()); crate::init_tracing(servoshell_preferences.tracing_filter.as_deref());

View file

@ -96,9 +96,7 @@ pub fn init_tracing(filter_directives: Option<&str>) {
} }
} }
pub fn servo_version() -> String { pub const VERSION: &str = concat!("Servo ", env!("CARGO_PKG_VERSION"), "-", env!("GIT_SHA"));
format!("Servo {}-{}", env!("CARGO_PKG_VERSION"), env!("GIT_SHA"))
}
/// Plumbs tracing spans into HiTrace, with the following caveats: /// Plumbs tracing spans into HiTrace, with the following caveats:
/// ///

View file

@ -2,17 +2,18 @@
* 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 https://mozilla.org/MPL/2.0/. */ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use core::panic;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{File, read_to_string}; use std::fs::{self, File, read_to_string};
use std::io::Read; use std::io::Read;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
#[cfg(any(target_os = "android", target_env = "ohos"))] #[cfg(any(target_os = "android", target_env = "ohos"))]
use std::sync::OnceLock; use std::sync::OnceLock;
use std::{env, fs, process}; use std::{env, process};
use bpaf::*;
use euclid::Size2D; use euclid::Size2D;
use getopts::{Matches, Options}; use log::warn;
use log::{error, warn};
use serde_json::Value; use serde_json::Value;
use servo::config::opts::{DebugOptions, Opts, OutputOptions}; use servo::config::opts::{DebugOptions, Opts, OutputOptions};
use servo::config::prefs::{PrefValue, Preferences}; use servo::config::prefs::{PrefValue, Preferences};
@ -20,6 +21,8 @@ use servo::servo_geometry::DeviceIndependentPixel;
use servo::servo_url::ServoUrl; use servo::servo_url::ServoUrl;
use url::Url; use url::Url;
use crate::VERSION;
#[cfg_attr(any(target_os = "android", target_env = "ohos"), allow(dead_code))] #[cfg_attr(any(target_os = "android", target_env = "ohos"), allow(dead_code))]
#[derive(Clone)] #[derive(Clone)]
pub(crate) struct ServoShellPreferences { pub(crate) struct ServoShellPreferences {
@ -127,7 +130,7 @@ pub fn default_config_dir() -> Option<PathBuf> {
/// Get a Servo [`Preferences`] to use when initializing Servo by first reading the user /// Get a Servo [`Preferences`] to use when initializing Servo by first reading the user
/// preferences file and then overriding these preferences with the ones from the `--prefs-file` /// preferences file and then overriding these preferences with the ones from the `--prefs-file`
/// command-line argument, if given. /// command-line argument, if given.
fn get_preferences(opts_matches: &Matches, config_dir: &Option<PathBuf>) -> Preferences { fn get_preferences(prefs_files: &[PathBuf], config_dir: &Option<PathBuf>) -> Preferences {
// Do not read any preferences files from the disk when testing as we do not want it // Do not read any preferences files from the disk when testing as we do not want it
// to throw off test results. // to throw off test results.
if cfg!(test) { if cfg!(test) {
@ -149,7 +152,7 @@ fn get_preferences(opts_matches: &Matches, config_dir: &Option<PathBuf>) -> Pref
let mut preferences = Preferences::default(); let mut preferences = Preferences::default();
apply_preferences(&mut preferences, user_prefs_hash); apply_preferences(&mut preferences, user_prefs_hash);
for pref_file_path in opts_matches.opt_strs("prefs-file").iter() { for pref_file_path in prefs_files.iter() {
apply_preferences(&mut preferences, read_prefs_file(pref_file_path)) apply_preferences(&mut preferences, read_prefs_file(pref_file_path))
} }
@ -181,393 +184,253 @@ pub fn read_prefs_map(txt: &str) -> HashMap<String, PrefValue> {
pub(crate) enum ArgumentParsingResult { pub(crate) enum ArgumentParsingResult {
ChromeProcess(Opts, Preferences, ServoShellPreferences), ChromeProcess(Opts, Preferences, ServoShellPreferences),
ContentProcess(String), ContentProcess(String),
Exit,
ErrorParsing,
} }
pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsingResult { /// Parse a resolution string into a Size2D
let (app_name, args) = args.split_first().unwrap(); fn parse_resolution_string(
string: String,
let mut opts = Options::new(); ) -> Result<Option<Size2D<u32, DeviceIndependentPixel>>, std::num::ParseIntError> {
opts.optopt( if string.is_empty() {
"o", Ok(None)
"output",
"Path to an output image. The format of the image is determined by the extension. \
Supports all formats that `rust-image` does.",
"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.optflag(
"x",
"exit",
"Exit after Servo has loaded the page and detected a stable output image",
);
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");
opts.optflag(
"",
"clean-shutdown",
"Do not shutdown until all threads have finished (macos only)",
);
opts.optflag("b", "no-native-titlebar", "Do not use native titlebar");
opts.optopt("", "device-pixel-ratio", "Device pixels per px", "");
opts.optopt(
"u",
"user-agent",
"Set custom user agent string (or ios / android / desktop for platform default)",
"NCSA Mosaic/1.0 (X11;SunOS 4.1.4 sun4m)",
);
opts.optmulti(
"",
"tracing-filter",
"Define a custom filter for traces. Overrides `SERVO_TRACING` if set.",
"FILTER",
);
#[cfg(target_env = "ohos")]
opts.optmulti(
"",
"log-filter",
"Define a custom filter for logging.",
"FILTER",
);
opts.optflag(
"",
"enable-experimental-web-platform-features",
"Whether or not to enable experimental web platform features.",
);
opts.optmulti(
"",
"pref",
"A preference to set to enable",
"dom_bluetooth_enabled",
);
opts.optmulti(
"",
"pref",
"A preference to set to disable",
"dom_webgpu_enabled=false",
);
opts.optmulti(
"",
"prefs-file",
"Load in additional prefs from a file.",
"/path/to/prefs.json",
);
let opt_match = match opts.parse(args) {
Ok(m) => m,
Err(f) => args_fail(&f.to_string()),
};
if opt_match.opt_present("v") || opt_match.opt_present("version") {
println!("{}", crate::servo_version());
process::exit(0);
}
if opt_match.opt_present("h") || opt_match.opt_present("help") {
print_usage(app_name, &opts);
process::exit(0);
};
let config_dir = opt_match
.opt_str("config-dir")
.map(Into::into)
.or_else(default_config_dir)
.inspect(|path| {
if !path.exists() {
fs::create_dir_all(path).unwrap_or_else(|e| {
error!("Failed to create config directory at {:?}: {:?}", path, e)
})
}
});
let mut preferences = get_preferences(&opt_match, &config_dir);
// 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") {
return ArgumentParsingResult::ContentProcess(content_process);
}
// Env-Filter directives are comma seperated.
let filters = opt_match.opt_strs("tracing-filter").join(",");
let tracing_filter = (!filters.is_empty()).then_some(filters);
#[cfg(target_env = "ohos")]
let log_filter = {
let filters = opt_match.opt_strs("log-filter").join(",");
let log_filter = (!filters.is_empty()).then_some(filters).or_else(|| {
(!preferences.log_filter.is_empty()).then_some(preferences.log_filter.clone())
});
log::debug!("Set log_filter to: {:?}", log_filter);
log_filter
};
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 {
print_debug_options_usage(app_name);
}
// 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 { } else {
// if the p option doesn't exist: let components = string
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 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 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(preferences.devtools_server_port);
preferences.devtools_server_enabled = true;
preferences.devtools_server_port = port;
}
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') .split('x')
.map(|component| { .map(|component| component.parse::<u32>())
component.parse().unwrap_or_else(|error| { .collect::<Result<Vec<_>, std::num::ParseIntError>>()?;
args_fail(&format!("Error parsing resolution '{string}': {error}")); Ok(Some(Size2D::new(components[0], components[1])))
}) }
}) }
.collect();
Size2D::new(components[0], components[1])
};
let screen_size_override = opt_match /// Parse stylesheets into the byte stream.
.opt_str("screen-size") fn parse_user_stylesheets(string: String) -> Result<Vec<(Vec<u8>, ServoUrl)>, std::io::Error> {
.map(parse_resolution_string); Ok(string
.split_whitespace()
// 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);
let user_stylesheets = opt_match
.opt_strs("user-stylesheet")
.iter()
.map(|filename| { .map(|filename| {
let cwd = env::current_dir().unwrap(); let cwd = env::current_dir().unwrap();
let path = cwd.join(filename); let path = cwd.join(filename);
let url = ServoUrl::from_url(Url::from_file_path(&path).unwrap()); let url = ServoUrl::from_url(Url::from_file_path(&path).unwrap());
let mut contents = Vec::new(); let mut contents = Vec::new();
File::open(path) File::open(path)
.unwrap_or_else(|err| args_fail(&format!("Couldn't open {}: {}", filename, err))) .unwrap()
.read_to_end(&mut contents) .read_to_end(&mut contents)
.unwrap_or_else(|err| args_fail(&format!("Couldn't read {}: {}", filename, err))); .unwrap();
(contents, url) (contents, url)
}) })
.collect(); .collect())
}
if opt_match.opt_present("enable-experimental-web-platform-features") { /// parses the profiling string and returns the correct value
fn profile() -> impl Parser<Option<OutputOptions>> {
let profile_flag = short('p')
.long("profile")
.help("uses 5.0 output as standard if no argument supplied")
.req_flag(None);
let profile_arg = short('p')
.long("profile")
.argument::<String>("10 or time.tsv")
.map(Some);
let profile = construct!([profile_arg, profile_flag]);
// This implements Parser<Option<Option<String>>
let opt_opt_string_parser = profile.optional();
opt_opt_string_parser.map(|val| match val {
Some(Some(parsed_string)) => {
if let Ok(float) = parsed_string.parse::<f64>() {
Some(OutputOptions::Stdout(float))
} else {
Some(OutputOptions::FileName(parsed_string))
}
},
Some(None) => Some(OutputOptions::Stdout(5.0)),
_ => None,
})
}
fn map_debug_options(arg: String) -> Vec<String> {
arg.split(',').map(|s| s.to_owned()).collect()
}
#[derive(Bpaf, Clone, Debug)]
#[bpaf(options, version(VERSION))]
struct CmdArgs {
/// Background Hang Monitor enabled
#[bpaf(long("bhm"))]
background_hang_monitor: bool,
/// Path to find SSL certificates
#[bpaf(argument("/home/servo/resources/certs"))]
certificate_path: Option<PathBuf>,
/// Do not shutdown until all threads have finished (macos only)
#[bpaf(long)]
clean_shutdown: bool,
/// config directory following xdg spec on linux platform
#[bpaf(argument("~/.config/servo"))]
config_dir: Option<PathBuf>,
/// Run as a content process and connect to the given pipe
#[bpaf(argument("servo-ipc-channel.abcdefg"))]
content_process: Option<String>,
/// A comma-separated string of debug options. Pass help to show available options.
#[bpaf(
short('Z'),
argument("layout_grid_enabled=true,dom_async_clipboard_enabled"),
long,
map(map_debug_options),
fallback(vec![])
)]
debug: Vec<String>,
#[bpaf(argument("1.0"))]
device_pixel_ratio_override: Option<f32>,
/// Start remote devtools using server on port
#[bpaf(argument("0"))]
devtools: Option<u16>,
/// Wether or not to enable experimental web platform features.
#[bpaf(long)]
enable_experimental_web_platform_features: bool,
// Exit after Servo has loaded the page and detected stable output image
#[bpaf(short('x'), long)]
exit: bool,
/// Exit on thread failure instead of displaying about:failure
#[bpaf(short('f'), long)]
hard_fail: bool,
/// Headless mode
#[bpaf(short('z'), long)]
headless: bool,
/// Wether or not to completely ignore certificate errors
#[bpaf(long)]
ignore_certificate_errors: bool,
/// Number of threads to use for layout
#[bpaf(short('y'), long, argument("1"))]
layout_threads: Option<i64>,
/// Directory root with unminified scripts
#[bpaf(argument("~/.local/share/servo"))]
local_script_source: Option<PathBuf>,
#[cfg(target_env = "ohos")]
/// Define a custom filter for logging
#[bpaf(argument("FILTER"))]
log_filter: Option<String>,
/// Run in multiprocess mode
#[bpaf(short('M'), long, flag(true, false))]
multiprocess: bool,
/// Enable to turn off incremental layout
#[bpaf(short('i'), long, flag(false, true))]
nonincremental_layout: bool,
/// Path to an output image. The format of the image is determined
/// by the extension. Supports all formats that rust-image does.
#[bpaf(short('o'), argument("test.png"), long)]
output_image: Option<PathBuf>,
/// Time profiler flag and either a TSV output filename OR and interval
/// for output to Stdout
#[bpaf(external)]
profile: Option<OutputOptions>,
/// Path to dump a self-contained HTML timeline of profiler traces
#[bpaf(argument("trace.html"), long("profiler-trace-path"))]
time_profiler_trace_path: Option<PathBuf>,
/// A preference to set
#[bpaf(argument("dom_bluetooth_enabled"), many)]
pref: Vec<String>,
/// Load an additional prefs from a file.
#[bpaf(long, argument("/path/to/prefs.json"), many)]
prefs_files: Vec<PathBuf>,
print_pwm: bool,
/// Probability of randomly closing a pipeline (for testing constellation hardening)
#[bpaf(argument("0.25"))]
random_pipeline_closure_probability: Option<f32>,
/// A fixed seed for repeatability of random pipeline closure.
random_pipeline_closure_seed: Option<usize>,
/// Run in sandbox if multiprocess
#[bpaf(short('S'), long, flag(true, false))]
sandbox: bool,
/// Shaders will be loaded from the specified directory instead of using the builtin ones.
shaders: Option<PathBuf>,
/// Override the screen resolution in logical (device independent) pixels
#[bpaf(short('x'), long("screen-size"), argument::<String>("1024x768"),
parse(parse_resolution_string), fallback(None))]
screen_size_override: Option<Size2D<u32, DeviceIndependentPixel>>,
/// Define a custom filter for traces. Overridees `SERVO_TRACING` if set.
#[bpaf(long("tracing-filter"), argument("FILTER"))]
tracing_filter: Option<String>,
#[bpaf(long)]
no_native_titlebar: bool,
/// Unminify Javascript
#[bpaf(long)]
unminify_js: bool,
/// Unminify Css
#[bpaf(long)]
unminify_css: bool,
/// Set custom user agent string (or ios/ android / desktop for platform default)""
#[bpaf(argument::<String>("NCSA mosaic/1.0 (X11;SunOS 4.1.4 sun4m"))]
user_agent: Option<String>,
/// Uses userscripts in resources/user-agent-js or a specified full path
#[bpaf(
long,
argument("resources/user-agent-js"),
fallback(PathBuf::from("resources/user-agent-js"))
)]
userscripts_directory: PathBuf,
/// A user stylesheet ot be added to every document
#[bpaf(argument::<String>("path.css"), parse(parse_user_stylesheets), fallback(vec![]))]
user_stylesheets: Vec<(Vec<u8>, ServoUrl)>,
/// Start remote WebDriver server on port
#[bpaf(argument("7000"))]
webdriver_port: Option<u16>,
/// Set the initial window size in logical (device independent) pixels"
#[bpaf(argument::<String>("1024x740"), parse(parse_resolution_string), fallback(None))]
window_size: Option<Size2D<u32, DeviceIndependentPixel>>,
/// The url we should load
#[bpaf(positional("URL"), fallback(String::from("https://www.servo.org")))]
url: String,
}
fn update_preferences_from_command_line_arguemnts(
preferences: &mut Preferences,
cmd_args: &CmdArgs,
) {
if let Some(port) = cmd_args.devtools {
preferences.devtools_server_enabled = true;
preferences.devtools_server_port = port as i64;
}
if cmd_args.enable_experimental_web_platform_features {
vec![ vec![
"dom_async_clipboard_enabled", "dom_async_clipboard_enabled",
"dom_fontface_enabled", "dom_fontface_enabled",
@ -591,37 +454,73 @@ pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsing
.for_each(|pref| preferences.set_value(pref, PrefValue::Bool(true))); .for_each(|pref| preferences.set_value(pref, PrefValue::Bool(true)));
} }
// Handle all command-line preferences overrides. for pref in &cmd_args.pref {
for pref in opt_match.opt_strs("pref") {
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];
let pref_value = PrefValue::from_booleanish_str(split.get(1).copied().unwrap_or("true")); let pref_value = PrefValue::from_booleanish_str(split.get(1).copied().unwrap_or("true"));
preferences.set_value(pref_name, pref_value); preferences.set_value(pref_name, pref_value);
} }
if let Some(layout_threads) = layout_threads { if let Some(layout_threads) = cmd_args.layout_threads {
preferences.layout_threads = layout_threads as i64; preferences.layout_threads = layout_threads;
} }
let no_native_titlebar = opt_match.opt_present("no-native-titlebar"); if cmd_args.headless && preferences.media_glvideo_enabled {
let mut device_pixel_ratio_override = opt_match.opt_str("device-pixel-ratio").map(|dppx_str| { warn!("GL video rendering is not supported on headless windows.");
dppx_str.parse().unwrap_or_else(|err| { preferences.media_glvideo_enabled = false;
error!("Error parsing option: --device-pixel-ratio ({})", err);
process::exit(1);
})
});
// If an output file is specified the device pixel ratio is always 1.
let output_image_path = opt_match.opt_str("o");
if output_image_path.is_some() {
device_pixel_ratio_override = Some(1.0);
} }
let url = if !opt_match.free.is_empty() { if let Some(user_agent) = cmd_args.user_agent.clone() {
Some(opt_match.free[0][..].into()) preferences.user_agent = user_agent;
} else { }
None }
};
pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsingResult {
// we do not want the binary name in the arguments
let args_without_binary = args
.split_first()
.expect("Expectd executable name and and arguments")
.1;
let cmd_args = cmd_args().run_inner(Args::from(args_without_binary));
if let Err(error) = cmd_args {
error.print_message(80);
return ArgumentParsingResult::ErrorParsing;
}
let cmd_args = cmd_args.unwrap();
if cmd_args
.debug
.iter()
.any(|debug_option| debug_option.contains("help"))
{
print_debug_options_usage("servo");
return ArgumentParsingResult::Exit;
}
// If this is the content process, we'll receive the real options over IPC. So fill in some dummy options for now
if let Some(content_process) = cmd_args.content_process {
return ArgumentParsingResult::ContentProcess(content_process);
}
let config_dir = cmd_args
.config_dir
.clone()
.or_else(default_config_dir)
.inspect(|config_dir| {
if !config_dir.exists() {
fs::create_dir_all(config_dir).expect("Could not create config_dir");
}
});
if let Some(ref time_profiler_trace_path) = cmd_args.time_profiler_trace_path {
let mut path = PathBuf::from(time_profiler_trace_path);
path.pop();
fs::create_dir_all(&path).expect("Error in creating profiler trace path");
}
let mut preferences = get_preferences(&cmd_args.prefs_files, &config_dir);
update_preferences_from_command_line_arguemnts(&mut preferences, &cmd_args);
// FIXME: enable JIT compilation on 32-bit Android after the startup crash issue (#31134) is fixed. // FIXME: enable JIT compilation on 32-bit Android after the startup crash issue (#31134) is fixed.
if cfg!(target_os = "android") && cfg!(target_pointer_width = "32") { if cfg!(target_os = "android") && cfg!(target_pointer_width = "32") {
@ -630,76 +529,85 @@ pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsing
preferences.js_ion_enabled = false; preferences.js_ion_enabled = false;
} }
let exit_after_load = opt_match.opt_present("x") || output_image_path.is_some(); let device_pixel_ratio_override = if cmd_args.output_image.is_some() {
let wait_for_stable_image = exit_after_load; Some(1.0)
} else {
cmd_args.device_pixel_ratio_override
};
// 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 = cmd_args
.screen_size_override
.map_or(default_window_size, |screen_size_override| {
default_window_size.min(screen_size_override)
});
let servoshell_preferences = ServoShellPreferences { let servoshell_preferences = ServoShellPreferences {
url, url: Some(cmd_args.url),
no_native_titlebar, no_native_titlebar: cmd_args.no_native_titlebar,
device_pixel_ratio_override, device_pixel_ratio_override,
clean_shutdown: opt_match.opt_present("clean-shutdown"), clean_shutdown: cmd_args.clean_shutdown,
headless: opt_match.opt_present("z"), headless: cmd_args.headless,
tracing_filter, tracing_filter: cmd_args.tracing_filter,
initial_window_size, initial_window_size: cmd_args.window_size.unwrap_or(default_window_size),
screen_size_override, screen_size_override: cmd_args.screen_size_override,
output_image_path, output_image_path: cmd_args
exit_after_stable_image: exit_after_load, .output_image
userscripts_directory: opt_match .map(|p| p.to_string_lossy().into_owned()),
.opt_default("userscripts", "resources/user-agent-js") exit_after_stable_image: cmd_args.exit,
.map(PathBuf::from), userscripts_directory: Some(cmd_args.userscripts_directory),
#[cfg(target_env = "ohos")] #[cfg(target_env = "ohos")]
log_filter, log_filter: Some(
cmd_args
.log_filter
.unwrap_or(preferences.log_filter.clone()),
),
..Default::default() ..Default::default()
}; };
if servoshell_preferences.headless && preferences.media_glvideo_enabled { let mut debug_options = DebugOptions::default();
warn!("GL video rendering is not supported on headless windows."); for debug_string in cmd_args.debug {
preferences.media_glvideo_enabled = false; let result = debug_options.extend(debug_string);
} if let Err(error) = result {
println!("error: urnecognized debug option: {}", error);
if let Some(user_agent) = opt_match.opt_str("user-agent") { return ArgumentParsingResult::ErrorParsing;
preferences.user_agent = user_agent; }
} }
let opts = Opts { let opts = Opts {
debug: debug_options.clone(), debug: debug_options,
wait_for_stable_image, wait_for_stable_image: cmd_args.exit,
time_profiling, time_profiling: cmd_args.profile,
time_profiler_trace_path: opt_match.opt_str("profiler-trace-path"), time_profiler_trace_path: cmd_args
nonincremental_layout, .time_profiler_trace_path
user_stylesheets, .map(|p| p.to_string_lossy().into_owned()),
hard_fail: opt_match.opt_present("f") && !opt_match.opt_present("F"), nonincremental_layout: cmd_args.nonincremental_layout,
webdriver_port, user_stylesheets: cmd_args.user_stylesheets,
multiprocess: opt_match.opt_present("M"), hard_fail: cmd_args.hard_fail,
background_hang_monitor: opt_match.opt_present("B"), webdriver_port: cmd_args.webdriver_port,
sandbox: opt_match.opt_present("S"), multiprocess: cmd_args.multiprocess,
random_pipeline_closure_probability, background_hang_monitor: cmd_args.background_hang_monitor,
random_pipeline_closure_seed, sandbox: cmd_args.sandbox,
config_dir, random_pipeline_closure_probability: cmd_args.random_pipeline_closure_probability,
shaders_dir: opt_match.opt_str("shaders").map(Into::into), random_pipeline_closure_seed: cmd_args.random_pipeline_closure_seed,
certificate_path: opt_match.opt_str("certificate-path"), config_dir: config_dir.clone(),
ignore_certificate_errors: opt_match.opt_present("ignore-certificate-errors"), shaders_dir: cmd_args.shaders,
unminify_js: opt_match.opt_present("unminify-js"), certificate_path: cmd_args
local_script_source: opt_match.opt_str("local-script-source"), .certificate_path
unminify_css: opt_match.opt_present("unminify-css"), .map(|p| p.to_string_lossy().into_owned()),
print_pwm: opt_match.opt_present("print-pwm"), ignore_certificate_errors: cmd_args.ignore_certificate_errors,
unminify_js: cmd_args.unminify_js,
local_script_source: cmd_args
.local_script_source
.map(|p| p.to_string_lossy().into_owned()),
unminify_css: cmd_args.unminify_css,
print_pwm: cmd_args.print_pwm,
}; };
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences)
} }
fn args_fail(msg: &str) -> ! {
eprintln!("{}", msg);
process::exit(1)
}
fn print_usage(app: &str, opts: &Options) {
let message = format!(
"Usage: {} [ options ... ] [URL]\n\twhere options include",
app
);
println!("{}", opts.usage(&message));
}
fn print_debug_options_usage(app: &str) { fn print_debug_options_usage(app: &str) {
fn print_option(name: &str, description: &str) { fn print_option(name: &str, description: &str) {
println!("\t{:<35} {}", name, description); println!("\t{:<35} {}", name, description);
@ -791,6 +699,12 @@ fn test_parse_pref(arg: &str) -> Preferences {
unreachable!("No preferences for content process") unreachable!("No preferences for content process")
}, },
ArgumentParsingResult::ChromeProcess(_, preferences, _) => preferences, ArgumentParsingResult::ChromeProcess(_, preferences, _) => preferences,
ArgumentParsingResult::Exit => {
panic!("we supplied a --pref argument above which should be parsed")
},
ArgumentParsingResult::ErrorParsing => {
unreachable!("we supplied a --pref argument above which should be parsed")
},
} }
} }
@ -839,3 +753,69 @@ fn test_create_prefs_map() {
}"; }";
assert_eq!(read_prefs_map(json_str).len(), 3); assert_eq!(read_prefs_map(json_str).len(), 3);
} }
#[cfg(test)]
fn test_parse(arg: &str) -> (Opts, Preferences, ServoShellPreferences) {
let mut args = vec!["servo".to_string()];
// bpaf requires the arguments that are separated by whitespace to be different elements of the vector.
let mut args_split = arg.split_whitespace().map(|s| s.to_owned()).collect();
args.append(&mut args_split);
match parse_command_line_arguments(args) {
ArgumentParsingResult::ContentProcess(..) => {
unreachable!("No preferences for content process")
},
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => {
(opts, preferences, servoshell_preferences)
},
ArgumentParsingResult::Exit | ArgumentParsingResult::ErrorParsing => {
unreachable!("We always have valid preference in our test cases")
},
}
}
#[test]
fn test_profiling_args() {
assert_eq!(
test_parse("-p").0.time_profiling.unwrap(),
OutputOptions::Stdout(5_f64)
);
assert_eq!(
test_parse("-p 10").0.time_profiling.unwrap(),
OutputOptions::Stdout(10_f64)
);
assert_eq!(
test_parse("-p 10.0").0.time_profiling.unwrap(),
OutputOptions::Stdout(10_f64)
);
assert_eq!(
test_parse("-p foo.txt").0.time_profiling.unwrap(),
OutputOptions::FileName(String::from("foo.txt"))
);
}
#[test]
fn test_servoshell_cmd() {
assert_eq!(
test_parse("-o foo.png").2.device_pixel_ratio_override,
Some(1.0)
);
assert_eq!(
test_parse("--screen-size=1000x1000")
.2
.screen_size_override
.unwrap(),
Size2D::new(1000, 1000)
);
assert_eq!(
test_parse("--certificate-path=/tmp/test")
.0
.certificate_path
.unwrap(),
String::from("/tmp/test")
);
}