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

@ -56,6 +56,7 @@ webxr = ["dep:webxr", "libservo/webxr"]
webgpu = ["libservo/webgpu"]
[dependencies]
euclid = { workspace = true }
libc = { workspace = true }
libservo = { path = "../../components/servo" }
cfg-if = { workspace = true }
@ -104,10 +105,10 @@ webxr = { workspace = true, optional = true }
# For optional feature servo_allocator/use-system-allocator
servo_allocator = { path = "../../components/allocator" }
arboard = { version = "3" }
dirs = "5.0"
egui = { version = "0.30.0" }
egui_glow = { version = "0.30.0", features = ["winit"] }
egui-winit = { version = "0.30.0", default-features = false, features = ["clipboard", "wayland"] }
euclid = { workspace = true }
gilrs = "0.11.0"
gleam = { workspace = true }
glow = "0.16.0"
@ -116,6 +117,7 @@ http = { workspace = true }
net = { path = "../../components/net" }
net_traits = { workspace = true }
raw-window-handle = "0.6"
serde_json = { workspace = true }
shellwords = "1.0.0"
surfman = { workspace = true, features = ["sm-x11", "sm-raw-window-handle-06"] }
tinyfiledialogs = "3.0"

View file

@ -15,7 +15,8 @@ use raw_window_handle::HasDisplayHandle;
use servo::base::id::WebViewId;
use servo::compositing::windowing::EmbedderEvent;
use servo::compositing::CompositeTarget;
use servo::config::opts;
use servo::config::opts::Opts;
use servo::config::prefs::Preferences;
use servo::embedder_traits::EventLoopWaker;
use servo::servo_config::pref;
use servo::url::ServoUrl;
@ -38,21 +39,22 @@ use crate::desktop::embedder::{EmbedderCallbacks, XrDiscovery};
use crate::desktop::tracing::trace_winit_event;
use crate::desktop::window_trait::WindowPortsMethods;
use crate::parser::get_default_url;
use crate::prefs::ServoShellPreferences;
pub struct App {
opts: Opts,
preferences: Preferences,
servo_shell_preferences: ServoShellPreferences,
servo: Option<Servo<dyn WindowPortsMethods>>,
webviews: Option<WebViewManager<dyn WindowPortsMethods>>,
event_queue: Vec<EmbedderEvent>,
suspended: Cell<bool>,
windows: HashMap<WindowId, Rc<dyn WindowPortsMethods>>,
minibrowser: Option<Minibrowser>,
user_agent: Option<String>,
waker: Box<dyn EventLoopWaker>,
initial_url: ServoUrl,
t_start: Instant,
t: Instant,
do_not_use_native_titlebar: bool,
device_pixel_ratio_override: Option<f32>,
}
enum Present {
@ -73,38 +75,40 @@ enum PumpResult {
impl App {
pub fn new(
opts: Opts,
preferences: Preferences,
servo_shell_preferences: ServoShellPreferences,
events_loop: &EventsLoop,
user_agent: Option<String>,
url: Option<String>,
do_not_use_native_titlebar: bool,
device_pixel_ratio_override: Option<f32>,
) -> Self {
// Handle browser state.
let initial_url = get_default_url(url.as_deref(), env::current_dir().unwrap(), |path| {
fs::metadata(path).is_ok()
});
let initial_url = get_default_url(
servo_shell_preferences.url.as_deref(),
env::current_dir().unwrap(),
|path| fs::metadata(path).is_ok(),
&servo_shell_preferences,
);
let t = Instant::now();
App {
opts,
preferences,
servo_shell_preferences,
event_queue: vec![],
webviews: None,
servo: None,
suspended: Cell::new(false),
windows: HashMap::new(),
minibrowser: None,
user_agent,
waker: events_loop.create_event_loop_waker(),
initial_url: initial_url.clone(),
t_start: t,
t,
do_not_use_native_titlebar,
device_pixel_ratio_override,
}
}
/// Initialize Application once event loop start running.
pub fn init(&mut self, event_loop: Option<&ActiveEventLoop>) {
// Create rendering context
let rendering_context = if opts::get().headless {
let rendering_context = if self.opts.headless {
let connection = Connection::new().expect("Failed to create connection");
let adapter = connection
.create_software_adapter()
@ -112,7 +116,7 @@ impl App {
RenderingContext::create(
&connection,
&adapter,
Some(opts::get().initial_window_size.to_untyped().to_i32()),
Some(self.opts.initial_window_size.to_untyped().to_i32()),
)
.expect("Failed to create WR surfman")
} else {
@ -129,18 +133,20 @@ impl App {
.expect("Failed to create WR surfman")
};
let window = if opts::get().headless {
let window = if self.opts.headless {
headless_window::Window::new(
opts::get().initial_window_size,
self.device_pixel_ratio_override,
self.opts.initial_window_size,
self.servo_shell_preferences.device_pixel_ratio_override,
self.opts.screen_size_override,
)
} else {
Rc::new(headed_window::Window::new(
&self.opts,
&rendering_context,
opts::get().initial_window_size,
self.opts.initial_window_size,
event_loop.unwrap(),
self.do_not_use_native_titlebar,
self.device_pixel_ratio_override,
self.servo_shell_preferences.no_native_titlebar,
self.servo_shell_preferences.device_pixel_ratio_override,
))
};
@ -171,7 +177,7 @@ impl App {
self.event_queue.push(EmbedderEvent::Idle);
let (_, window) = self.windows.iter().next().unwrap();
let xr_discovery = if pref!(dom.webxr.openxr.enabled) && !opts::get().headless {
let xr_discovery = if pref!(dom_webxr_openxr_enabled) && !self.opts.headless {
#[cfg(target_os = "windows")]
let openxr = {
let app_info = AppInfo::new("Servoshell", 0, "Servo", 0);
@ -181,7 +187,7 @@ impl App {
let openxr = None;
openxr
} else if pref!(dom.webxr.glwindow.enabled) && !opts::get().headless {
} else if pref!(dom_webxr_glwindow_enabled) && !self.opts.headless {
let window = window.new_glwindow(event_loop.unwrap());
Some(XrDiscovery::GlWindow(GlWindowDiscovery::new(window)))
} else {
@ -198,10 +204,12 @@ impl App {
CompositeTarget::Window
};
let mut servo = Servo::new(
self.opts.clone(),
self.preferences.clone(),
rendering_context,
embedder,
window.clone(),
self.user_agent.clone(),
self.servo_shell_preferences.user_agent.clone(),
composite_target,
);
@ -243,7 +251,7 @@ impl App {
// If the Gamepad API is enabled, handle gamepad events from GilRs.
// Checking for focused_webview_id should ensure we'll have a valid browsing context.
if pref!(dom.gamepad.enabled) && webviews.focused_webview_id().is_some() {
if pref!(dom_gamepad_enabled) && webviews.focused_webview_id().is_some() {
webviews.handle_gamepad_events();
}
@ -254,7 +262,7 @@ impl App {
let mut need_update = false;
loop {
// Consume and handle those embedder messages.
let servo_event_response = webviews.handle_servo_events(embedder_messages);
let servo_event_response = webviews.handle_servo_events(&self.opts, embedder_messages);
need_present |= servo_event_response.need_present;
need_update |= servo_event_response.need_update;
@ -524,7 +532,11 @@ impl ApplicationHandler<WakerEvent> for App {
if let Some(ref minibrowser) = self.minibrowser {
let webviews = &mut self.webviews.as_mut().unwrap();
let app_event_queue = &mut self.event_queue;
minibrowser.queue_embedder_events_for_minibrowser_events(webviews, app_event_queue);
minibrowser.queue_embedder_events_for_minibrowser_events(
webviews,
app_event_queue,
&self.servo_shell_preferences,
);
}
self.handle_events_with_winit(event_loop, window);
@ -565,7 +577,11 @@ impl ApplicationHandler<WakerEvent> for App {
if let Some(ref minibrowser) = self.minibrowser {
let webviews = &mut self.webviews.as_mut().unwrap();
let app_event_queue = &mut self.event_queue;
minibrowser.queue_embedder_events_for_minibrowser_events(webviews, app_event_queue);
minibrowser.queue_embedder_events_for_minibrowser_events(
webviews,
app_event_queue,
&self.servo_shell_preferences,
);
}
self.handle_events_with_winit(event_loop, window);

View file

@ -2,16 +2,12 @@
* 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::{env, panic, process};
use getopts::Options;
use log::error;
use servo::config::opts::{self, ArgumentParsingResult};
use servo::servo_config::pref;
use std::{env, panic};
use crate::desktop::app::App;
use crate::desktop::events_loop::EventsLoop;
use crate::panic_hook;
use crate::prefs::{parse_command_line_arguments, ArgumentParsingResult};
pub fn main() {
crate::crash_handler::install();
@ -19,98 +15,23 @@ pub fn main() {
crate::init_crypto();
crate::resources::init();
// Parse the command line options and store them globally
let args: Vec<String> = env::args().collect();
let mut opts = Options::new();
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(
"",
"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.",
"--prefs-file /path/to/prefs.json",
);
let opts_matches;
let content_process_token;
match opts::from_cmdline_args(opts, &args) {
ArgumentParsingResult::ContentProcess(matches, token) => {
opts_matches = matches;
content_process_token = Some(token);
},
ArgumentParsingResult::ChromeProcess(matches) => {
opts_matches = matches;
content_process_token = None;
},
};
crate::prefs::register_user_prefs(&opts_matches);
// TODO: once log-panics is released, can this be replaced by
// log_panics::init()?
panic::set_hook(Box::new(panic_hook::panic_hook));
if let Some(token) = content_process_token {
return servo::run_content_process(token);
}
if opts::get().is_printing_version {
println!("{}", crate::servo_version());
process::exit(0);
}
let clean_shutdown = opts_matches.opt_present("clean-shutdown");
let do_not_use_native_titlebar =
opts_matches.opt_present("no-native-titlebar") || !(pref!(shell.native_titlebar.enabled));
let device_pixel_ratio_override = opts_matches.opt_str("device-pixel-ratio").map(|dppx_str| {
dppx_str.parse().unwrap_or_else(|err| {
error!("Error parsing option: --device-pixel-ratio ({})", err);
process::exit(1);
})
});
let user_agent = opts_matches.opt_str("u");
let url_opt = if !opts_matches.free.is_empty() {
Some(&opts_matches.free[0][..])
} else {
None
let args = env::args().collect();
let (opts, preferences, servoshell_preferences) = match parse_command_line_arguments(args) {
ArgumentParsingResult::ContentProcess(token) => return servo::run_content_process(token),
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => {
(opts, preferences, servoshell_preferences)
},
};
let event_loop = EventsLoop::new(opts::get().headless, opts::get().output_file.is_some())
let clean_shutdown = servoshell_preferences.clean_shutdown;
let event_loop = EventsLoop::new(opts.headless, opts.output_file.is_some())
.expect("Failed to create events loop");
let mut app = App::new(
&event_loop,
user_agent,
url_opt.map(|s| s.to_string()),
do_not_use_native_titlebar,
device_pixel_ratio_override,
);
let mut app = App::new(opts, preferences, servoshell_preferences, &event_loop);
event_loop.run_app(&mut app);

View file

@ -48,7 +48,7 @@ impl EmbedderMethods for EmbedderCallbacks {
xr: &mut webxr::MainThreadRegistry,
_embedder_proxy: EmbedderProxy,
) {
if pref!(dom.webxr.test) {
if pref!(dom_webxr_test) {
xr.register_mock(webxr::headless::HeadlessMockDiscovery::new());
} else if let Some(xr_discovery) = self.xr_discovery.take() {
match xr_discovery {

View file

@ -8,7 +8,6 @@ use std::sync::{Arc, Condvar, Mutex};
use std::time;
use log::warn;
use servo::config::{pref, set_pref};
use servo::embedder_traits::EventLoopWaker;
use winit::error::EventLoopError;
use winit::event_loop::EventLoop as WinitEventLoop;
@ -90,10 +89,6 @@ impl EventsLoop {
},
EventLoop::Headless(ref data) => {
let (flag, condvar) = &**data;
if pref!(media.glvideo.enabled) {
warn!("GL video rendering is not supported on headless windows.");
set_pref!(media.glvideo.enabled, false);
}
app.init(None);
loop {

View file

@ -14,10 +14,11 @@ use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
use servo::compositing::windowing::{
AnimationState, EmbedderCoordinates, EmbedderEvent, MouseWindowEvent, WindowMethods,
};
use servo::config::opts::Opts;
use servo::embedder_traits::Cursor;
use servo::keyboard_types::{Key, KeyState, KeyboardEvent};
use servo::script_traits::{TouchEventType, WheelDelta, WheelMode};
use servo::servo_config::{opts, pref};
use servo::servo_config::pref;
use servo::servo_geometry::DeviceIndependentPixel;
use servo::webrender_api::units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize, DevicePixel};
use servo::webrender_api::ScrollLocation;
@ -57,14 +58,13 @@ pub struct Window {
impl Window {
pub fn new(
opts: &Opts,
rendering_context: &RenderingContext,
window_size: Size2D<u32, DeviceIndependentPixel>,
event_loop: &ActiveEventLoop,
no_native_titlebar: bool,
device_pixel_ratio_override: Option<f32>,
) -> Window {
let opts = opts::get();
// If there's no chrome, start off with the window invisible. It will be set to visible in
// `load_end()`. This avoids an ugly flash of unstyled content (especially important since
// unstyled content is white and chrome often has a transparent background). See issue
@ -625,13 +625,13 @@ impl webxr::glwindow::GlWindow for XRWindow {
}
fn get_mode(&self) -> webxr::glwindow::GlWindowMode {
if pref!(dom.webxr.glwindow.red_cyan) {
if pref!(dom_webxr_glwindow_red_cyan) {
webxr::glwindow::GlWindowMode::StereoRedCyan
} else if pref!(dom.webxr.glwindow.left_right) {
} else if pref!(dom_webxr_glwindow_left_right) {
webxr::glwindow::GlWindowMode::StereoLeftRight
} else if pref!(dom.webxr.glwindow.spherical) {
} else if pref!(dom_webxr_glwindow_spherical) {
webxr::glwindow::GlWindowMode::Spherical
} else if pref!(dom.webxr.glwindow.cubemap) {
} else if pref!(dom_webxr_glwindow_cubemap) {
webxr::glwindow::GlWindowMode::Cubemap
} else {
webxr::glwindow::GlWindowMode::Blit

View file

@ -13,7 +13,6 @@ use euclid::{Box2D, Length, Point2D, Scale, Size2D};
use servo::compositing::windowing::{
AnimationState, EmbedderCoordinates, EmbedderEvent, WindowMethods,
};
use servo::config::opts;
use servo::servo_geometry::DeviceIndependentPixel;
use servo::webrender_api::units::{DeviceIntSize, DevicePixel};
@ -34,6 +33,7 @@ impl Window {
pub fn new(
size: Size2D<u32, DeviceIndependentPixel>,
device_pixel_ratio_override: Option<f32>,
screen_size_override: Option<Size2D<u32, DeviceIndependentPixel>>,
) -> Rc<dyn WindowPortsMethods> {
let device_pixel_ratio_override: Option<Scale<f32, DeviceIndependentPixel, DevicePixel>> =
device_pixel_ratio_override.map(Scale::new);
@ -43,7 +43,7 @@ impl Window {
let inner_size = Cell::new((size.to_f32() * hidpi_factor).to_i32());
let window_rect = Box2D::from_origin_and_size(Point2D::zero(), size);
let screen_size = opts::get().screen_size_override.map_or_else(
let screen_size = screen_size_override.map_or_else(
|| window_rect.size(),
|screen_size_override| screen_size_override.to_i32(),
);

View file

@ -36,6 +36,7 @@ use super::geometry::winit_position_to_euclid_point;
use super::webview::{LoadStatus, WebViewManager};
use super::window_trait::WindowPortsMethods;
use crate::parser::location_bar_input_to_url;
use crate::prefs::ServoShellPreferences;
pub struct Minibrowser {
pub context: EguiGlow,
@ -497,18 +498,20 @@ impl Minibrowser {
&self,
browser: &WebViewManager<dyn WindowPortsMethods>,
app_event_queue: &mut Vec<EmbedderEvent>,
preferences: &ServoShellPreferences,
) {
for event in self.event_queue.borrow_mut().drain(..) {
let browser_id = browser.focused_webview_id().unwrap();
match event {
MinibrowserEvent::Go => {
let location = self.location.borrow();
if let Some(url) = location_bar_input_to_url(&location.clone()) {
app_event_queue.push(EmbedderEvent::LoadUrl(browser_id, url));
} else {
let Some(url) =
location_bar_input_to_url(&location.clone(), &preferences.searchpage)
else {
warn!("failed to parse location");
break;
}
};
app_event_queue.push(EmbedderEvent::LoadUrl(browser_id, url));
},
MinibrowserEvent::Back => {
app_event_queue.push(EmbedderEvent::Navigation(

View file

@ -19,6 +19,7 @@ use keyboard_types::{Key, KeyboardEvent, Modifiers, ShortcutMatcher};
use log::{debug, error, info, trace, warn};
use servo::base::id::TopLevelBrowsingContextId as WebViewId;
use servo::compositing::windowing::{EmbedderEvent, WebRenderDebugOption};
use servo::config::opts::Opts;
use servo::embedder_traits::{
CompositorEventVariant, ContextMenuResult, DualRumbleEffectParams, EmbedderMsg, FilterPattern,
GamepadHapticEffectType, PermissionPrompt, PermissionRequest, PromptCredentialsInput,
@ -29,7 +30,6 @@ use servo::script_traits::{
GamepadEvent, GamepadIndex, GamepadInputBounds, GamepadSupportedHapticEffects,
GamepadUpdateType, TouchEventType, TraversalDirection,
};
use servo::servo_config::opts;
use servo::servo_url::ServoUrl;
use servo::webrender_api::units::DeviceRect;
use servo::webrender_api::ScrollLocation;
@ -630,6 +630,7 @@ where
/// Returns true if the caller needs to manually present a new frame.
pub fn handle_servo_events(
&mut self,
opts: &Opts,
events: Drain<'_, (Option<WebViewId>, EmbedderMsg)>,
) -> ServoEventResponse {
let mut need_present = self.load_status() != LoadStatus::LoadComplete;
@ -685,7 +686,7 @@ where
self.window.request_inner_size(size);
},
EmbedderMsg::Prompt(definition, origin) => {
let res = if opts::get().headless {
let res = if opts.headless {
match definition {
PromptDefinition::Alert(_message, sender) => sender.send(()),
PromptDefinition::YesNo(_message, sender) => {
@ -927,10 +928,7 @@ where
};
},
EmbedderMsg::SelectFiles(patterns, multiple_files, sender) => {
let res = match (
opts::get().headless,
get_selected_files(patterns, multiple_files),
) {
let res = match (opts.headless, get_selected_files(patterns, multiple_files)) {
(true, _) | (false, None) => sender.send(None),
(false, Some(files)) => sender.send(Some(files)),
};
@ -941,8 +939,10 @@ where
};
},
EmbedderMsg::PromptPermission(prompt, sender) => {
let permission_state = prompt_user(prompt);
let _ = sender.send(permission_state);
let _ = sender.send(match opts.headless {
true => PermissionRequest::Denied,
false => prompt_user(prompt),
});
},
EmbedderMsg::ShowIME(_kind, _text, _multiline, _rect) => {
debug!("ShowIME received");
@ -1007,10 +1007,6 @@ where
#[cfg(target_os = "linux")]
fn prompt_user(prompt: PermissionPrompt) -> PermissionRequest {
if opts::get().headless {
return PermissionRequest::Denied;
}
let message = match prompt {
PermissionPrompt::Request(permission_name) => {
format!("Do you want to grant permission for {:?}?", permission_name)

View file

@ -9,7 +9,6 @@ use std::rc::Rc;
use euclid::{Length, Scale};
use servo::compositing::windowing::{EmbedderEvent, WindowMethods};
use servo::config::opts;
use servo::embedder_traits::Cursor;
use servo::servo_geometry::DeviceIndependentPixel;
use servo::webrender_api::units::{DeviceIntPoint, DeviceIntSize, DevicePixel};
@ -22,10 +21,7 @@ pub trait WindowPortsMethods: WindowMethods {
fn id(&self) -> winit::window::WindowId;
fn hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel> {
self.device_pixel_ratio_override()
.unwrap_or_else(|| match opts::get().output_file {
Some(_) => Scale::new(1.0),
None => self.device_hidpi_factor(),
})
.unwrap_or_else(|| self.device_hidpi_factor())
}
fn device_hidpi_factor(&self) -> Scale<f32, DeviceIndependentPixel, DevicePixel>;
fn device_pixel_ratio_override(

View file

@ -7,7 +7,6 @@
mod resources;
mod simpleservo;
use std::collections::HashMap;
use std::os::raw::{c_char, c_int, c_void};
use std::sync::Arc;
@ -830,17 +829,6 @@ fn get_options<'local>(
let native_window =
unsafe { ANativeWindow_fromSurface(env.get_native_interface(), surface.as_raw()) };
// FIXME: enable JIT compilation on 32-bit Android after the startup crash issue (#31134) is fixed.
let prefs = if cfg!(target_pointer_width = "32") {
let mut prefs = HashMap::new();
prefs.insert("js.baseline_interpreter.enabled".to_string(), false.into());
prefs.insert("js.baseline_jit.enabled".to_string(), false.into());
prefs.insert("js.ion.enabled".to_string(), false.into());
Some(prefs)
} else {
None
};
let opts = InitOptions {
args: args.unwrap_or(vec![]),
url,
@ -848,7 +836,6 @@ fn get_options<'local>(
density,
xr_discovery: None,
surfman_integration: simpleservo::SurfmanIntegration::Widget(native_window),
prefs,
};
Ok((opts, log, log_str, gst_debug_str))

View file

@ -16,7 +16,6 @@ impl ResourceReaderInstance {
impl ResourceReaderMethods for ResourceReaderInstance {
fn read(&self, res: Resource) -> Vec<u8> {
Vec::from(match res {
Resource::Preferences => &include_bytes!("../../../../resources/prefs.json")[..],
Resource::HstsPreloadList => {
&include_bytes!("../../../../resources/hsts_preload.json")[..]
},

View file

@ -3,23 +3,19 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::cell::RefCell;
use std::collections::HashMap;
use std::mem;
use std::os::raw::c_void;
use std::rc::Rc;
use getopts::Options;
use servo::base::id::WebViewId;
use servo::compositing::windowing::EmbedderEvent;
use servo::compositing::CompositeTarget;
pub use servo::config::prefs::{add_user_prefs, PrefValue};
use servo::embedder_traits::resources;
/// The EventLoopWaker::wake function will be called from any thread.
/// It will be called to notify embedder that some events are available,
/// and that perform_updates need to be called
pub use servo::embedder_traits::EventLoopWaker;
pub use servo::embedder_traits::{InputMethodType, MediaSessionPlaybackState, PromptResult};
use servo::servo_config::{opts, pref};
use servo::servo_url::ServoUrl;
pub use servo::webrender_api::units::DeviceIntRect;
use servo::webrender_traits::RenderingContext;
@ -31,6 +27,7 @@ use crate::egl::host_trait::HostTrait;
use crate::egl::servo_glue::{
Coordinates, ServoEmbedderCallbacks, ServoGlue, ServoWindowCallbacks,
};
use crate::prefs::{parse_command_line_arguments, ArgumentParsingResult};
thread_local! {
pub static SERVO: RefCell<Option<ServoGlue>> = RefCell::new(None);
@ -44,7 +41,6 @@ pub struct InitOptions {
#[cfg(feature = "webxr")]
pub xr_discovery: Option<webxr::Discovery>,
pub surfman_integration: SurfmanIntegration,
pub prefs: Option<HashMap<String, PrefValue>>,
}
/// Controls how this embedding's rendering will integrate with the embedder.
@ -64,17 +60,24 @@ pub fn init(
crate::init_crypto();
resources::set(Box::new(ResourceReaderInstance::new()));
if let Some(prefs) = init_opts.prefs {
add_user_prefs(prefs);
}
// `parse_command_line_arguments` expects the first argument to be the binary name.
let mut args = mem::replace(&mut init_opts.args, vec![]);
// opts::from_cmdline_args expects the first argument to be the binary name.
args.insert(0, "servo".to_string());
opts::from_cmdline_args(Options::new(), &args);
let (opts, preferences, servoshell_preferences) = match parse_command_line_arguments(args) {
ArgumentParsingResult::ContentProcess(..) => {
unreachable!("Android does not have support for multiprocess yet.")
},
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => {
(opts, preferences, servoshell_preferences)
},
};
let embedder_url = init_opts.url.as_ref().and_then(|s| ServoUrl::parse(s).ok());
let pref_url = ServoUrl::parse(&pref!(shell.homepage)).ok();
let pref_url = servoshell_preferences
.url
.as_ref()
.and_then(|s| ServoUrl::parse(s).ok());
let blank_url = ServoUrl::parse("about:blank").ok();
let url = embedder_url.or(pref_url).or(blank_url).unwrap();
@ -117,6 +120,8 @@ pub fn init(
));
let servo = Servo::new(
opts,
preferences,
rendering_context.clone(),
embedder_callbacks,
window_callbacks.clone(),
@ -125,7 +130,12 @@ pub fn init(
);
SERVO.with(|s| {
let mut servo_glue = ServoGlue::new(rendering_context, servo, window_callbacks, None);
let mut servo_glue = ServoGlue::new(
rendering_context,
servo,
window_callbacks,
servoshell_preferences,
);
let _ = servo_glue.process_event(EmbedderEvent::NewWebView(url, WebViewId::new()));
*s.borrow_mut() = Some(servo_glue);
});

View file

@ -17,8 +17,6 @@ use servo::embedder_traits::resources;
/// and that perform_updates need to be called
pub use servo::embedder_traits::EventLoopWaker;
use servo::euclid::Size2D;
use servo::servo_config::opts;
use servo::servo_config::opts::ArgumentParsingResult;
use servo::servo_url::ServoUrl;
use servo::webrender_traits::RenderingContext;
use servo::{self, Servo};
@ -31,6 +29,7 @@ use crate::egl::ohos::InitOpts;
use crate::egl::servo_glue::{
Coordinates, ServoEmbedderCallbacks, ServoGlue, ServoWindowCallbacks,
};
use crate::prefs::{parse_command_line_arguments, ArgumentParsingResult};
/// Initialize Servo. At that point, we need a valid GL context.
/// In the future, this will be done in multiple steps.
@ -46,53 +45,27 @@ pub fn init(
crate::init_crypto();
let resource_dir = PathBuf::from(&options.resource_dir).join("servo");
resources::set(Box::new(ResourceReaderInstance::new(resource_dir)));
let mut args = vec!["servoshell".to_string()];
// It would be nice if `from_cmdline_args()` could accept str slices, to avoid allocations here.
// Then again, this code could and maybe even should be disabled in production builds.
let split_args: Vec<String> = options
.commandline_args
.split("\u{1f}")
.map(|arg| arg.to_string())
.collect();
args.extend(split_args);
let mut args = vec!["servoshell".to_string()];
args.extend(
options
.commandline_args
.split("\u{1f}")
.map(|arg| arg.to_string()),
);
debug!("Servo commandline args: {:?}", args);
let mut opts = getopts::Options::new();
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(
"",
"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.",
"--prefs-file /path/to/prefs.json",
);
let opts_matches = match opts::from_cmdline_args(opts, &args) {
ArgumentParsingResult::ContentProcess(matches, _token) => {
error!("Content Process mode not supported / tested yet on OpenHarmony!");
matches
let (opts, preferences, servoshell_preferences) = match parse_command_line_arguments(args) {
ArgumentParsingResult::ContentProcess(..) => {
unreachable!("OHOS does not have support for multiprocess yet.")
},
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences) => {
(opts, preferences, servoshell_preferences)
},
ArgumentParsingResult::ChromeProcess(matches) => matches,
};
crate::prefs::register_user_prefs(&opts_matches);
// Initialize surfman
let connection = Connection::new().or(Err("Failed to create connection"))?;
let adapter = connection
@ -144,11 +117,12 @@ pub fn init(
));
let servo = Servo::new(
opts,
preferences,
rendering_context.clone(),
embedder_callbacks,
window_callbacks.clone(),
// User agent: Mozilla/5.0 (<Phone|PC|Tablet>; HarmonyOS 5.0) bla bla
None,
None, /* user_agent */
CompositeTarget::Window,
);
@ -156,7 +130,7 @@ pub fn init(
rendering_context,
servo,
window_callbacks,
Some(options.resource_dir),
servoshell_preferences,
);
let initial_url = ServoUrl::parse(options.url.as_str())

View file

@ -31,6 +31,7 @@ use servo::webrender_traits::RenderingContext;
use servo::{Servo, TopLevelBrowsingContextId};
use crate::egl::host_trait::HostTrait;
use crate::prefs::ServoShellPreferences;
#[derive(Clone, Debug)]
pub struct Coordinates {
@ -84,7 +85,6 @@ pub struct ServoGlue {
need_present: bool,
callbacks: Rc<ServoWindowCallbacks>,
events: Vec<EmbedderEvent>,
resource_dir: Option<String>,
context_menu_sender: Option<IpcSender<ContextMenuResult>>,
/// List of top-level browsing contexts.
@ -98,6 +98,9 @@ pub struct ServoGlue {
/// The webview that is currently focused.
/// Modified by EmbedderMsg::WebViewFocused and EmbedderMsg::WebViewBlurred.
focused_webview_id: Option<WebViewId>,
/// servoshell specific preferences created during startup of the application.
servoshell_preferences: ServoShellPreferences,
}
#[allow(unused)]
@ -106,7 +109,7 @@ impl ServoGlue {
rendering_context: RenderingContext,
servo: Servo<ServoWindowCallbacks>,
callbacks: Rc<ServoWindowCallbacks>,
resource_dir: Option<String>,
servoshell_preferences: ServoShellPreferences,
) -> Self {
Self {
rendering_context,
@ -115,11 +118,11 @@ impl ServoGlue {
need_present: false,
callbacks,
events: vec![],
resource_dir,
context_menu_sender: None,
webviews: HashMap::default(),
creation_order: vec![],
focused_webview_id: None,
servoshell_preferences,
}
}
@ -173,7 +176,7 @@ impl ServoGlue {
/// Load an URL.
pub fn load_uri(&mut self, url: &str) -> Result<(), &'static str> {
info!("load_uri: {}", url);
crate::parser::location_bar_input_to_url(url)
crate::parser::location_bar_input_to_url(url, &self.servoshell_preferences.searchpage)
.ok_or("Can't parse URL")
.and_then(|url| {
let browser_id = self.get_browser_id()?;

View file

@ -41,6 +41,8 @@ pub(crate) fn panic_hook(info: &PanicHookInfo) {
}
drop(stderr);
// TODO: This shouldn't be using internal Servo options here. Perhaps this functionality should
// move into libservo itself.
if opts::get().hard_fail && !opts::get().multiprocess {
// When we are exiting due to a hard-failure mode, we trigger a segfault so that crash
// tests detect that we crashed. If we exit normally it just looks like a non-crash exit.

View file

@ -6,7 +6,6 @@
use std::path::{Path, PathBuf};
use servo::net_traits::pub_domains::is_reg_domain;
use servo::servo_config::pref;
use servo::servo_url::ServoUrl;
#[cfg(not(any(target_os = "android", target_env = "ohos")))]
@ -25,6 +24,7 @@ pub fn get_default_url(
url_opt: Option<&str>,
cwd: impl AsRef<Path>,
exists: impl FnOnce(&PathBuf) -> bool,
preferences: &crate::prefs::ServoShellPreferences,
) -> ServoUrl {
// If the url is not provided, we fallback to the homepage in prefs,
// or a blank page in case the homepage is not set either.
@ -48,13 +48,10 @@ pub fn get_default_url(
}
if new_url.is_none() && url_opt.is_some() {
new_url = location_bar_input_to_url(url_opt.unwrap());
new_url = location_bar_input_to_url(url_opt.unwrap(), &preferences.searchpage);
}
let pref_url = {
let homepage_url = pref!(shell.homepage);
parse_url_or_filename(cwd.as_ref(), &homepage_url).ok()
};
let pref_url = parse_url_or_filename(cwd.as_ref(), &preferences.homepage).ok();
let blank_url = ServoUrl::parse("about:blank").ok();
new_url.or(pref_url).or(blank_url).unwrap()
@ -64,21 +61,15 @@ pub fn get_default_url(
///
/// If this is not a valid URL, try to "fix" it by adding a scheme or if all else fails,
/// interpret the string as a search term.
pub fn location_bar_input_to_url(request: &str) -> Option<ServoUrl> {
pub(crate) fn location_bar_input_to_url(request: &str, searchpage: &str) -> Option<ServoUrl> {
let request = request.trim();
ServoUrl::parse(request)
.ok()
.or_else(|| {
if request.starts_with('/') {
ServoUrl::parse(&format!("file://{}", request)).ok()
} else if request.contains('/') || is_reg_domain(request) {
ServoUrl::parse(&format!("https://{}", request)).ok()
} else {
None
}
})
.or_else(|| {
let url = pref!(shell.searchpage).replace("%s", request);
ServoUrl::parse(&url).ok()
})
ServoUrl::parse(request).ok().or_else(|| {
if request.starts_with('/') {
ServoUrl::parse(&format!("file://{}", request)).ok()
} else if request.contains('/') || is_reg_domain(request) {
ServoUrl::parse(&format!("https://{}", request)).ok()
} else {
ServoUrl::parse(&searchpage.replace("%s", request)).ok()
}
})
}

View file

@ -3,123 +3,737 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::collections::HashMap;
use std::fs::File;
use std::fs::{read_to_string, File};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::{env, fs, process};
use getopts::Matches;
use servo::config::opts;
use servo::config::prefs::{self, PrefValue};
use servo::servo_config::basedir;
use euclid::Size2D;
use getopts::{Matches, Options};
use log::{error, warn};
use serde_json::Value;
use servo::config::opts::{DebugOptions, Opts, OutputOptions};
use servo::config::prefs::{PrefValue, Preferences};
use servo::url::ServoUrl;
use url::Url;
pub fn register_user_prefs(opts_matches: &Matches) {
// Read user's prefs.json and then parse --pref command line args.
pub(crate) struct ServoShellPreferences {
/// The user agent to use for servoshell.
pub user_agent: Option<String>,
/// A URL to load when starting servoshell.
pub url: Option<String>,
/// An override value for the device pixel ratio.
pub device_pixel_ratio_override: Option<f32>,
/// Whether or not to attempt clean shutdown.
pub clean_shutdown: bool,
/// Enable native window's titlebar and decorations.
pub no_native_titlebar: bool,
/// URL string of the homepage.
pub homepage: String,
/// URL string of the search engine page with '%s' standing in for the search term.
/// For example <https://duckduckgo.com/html/?q=%s>.
pub searchpage: String,
}
let user_prefs_path = opts::get()
.config_dir
impl Default for ServoShellPreferences {
fn default() -> Self {
Self {
user_agent: None,
url: None,
device_pixel_ratio_override: None,
clean_shutdown: false,
homepage: "https://servo.org".into(),
no_native_titlebar: true,
searchpage: "https://duckduckgo.com/html/?q=%s".into(),
}
}
}
#[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)
}
/// 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`
/// command-line argument, if given.
fn get_preferences(opts_matches: &Matches, config_dir: &Option<PathBuf>) -> Preferences {
// Do not read any preferences files from the disk when testing as we do not want it
// to throw off test results.
if cfg!(test) {
return Preferences::default();
}
let user_prefs_path = config_dir
.clone()
.or_else(basedir::default_config_dir)
.or_else(default_config_dir)
.map(|path| path.join("prefs.json"))
.filter(|path| path.exists());
let user_prefs_hash = user_prefs_path.map(read_prefs_file).unwrap_or_default();
let mut userprefs = if let Some(path) = user_prefs_path {
read_prefs_file(path.to_str().expect("Failed to read user prefs"))
} else {
HashMap::new()
let apply_preferences =
|preferences: &mut Preferences, preferences_hash: HashMap<String, PrefValue>| {
for (key, value) in preferences_hash.iter() {
preferences.set_value(key, value.clone());
}
};
let mut preferences = Preferences::default();
apply_preferences(&mut preferences, user_prefs_hash);
for pref_file_path in opts_matches.opt_strs("prefs-file").iter() {
apply_preferences(&mut preferences, read_prefs_file(pref_file_path))
}
preferences
}
fn read_prefs_file<P: AsRef<Path>>(path: P) -> HashMap<String, PrefValue> {
read_prefs_map(&read_to_string(path).expect("Error opening user prefs"))
}
pub fn read_prefs_map(txt: &str) -> HashMap<String, PrefValue> {
let prefs: HashMap<String, Value> = serde_json::from_str(txt)
.map_err(|_| panic!("Could not parse preferences JSON"))
.unwrap();
prefs
.into_iter()
.map(|(key, value)| {
let value = (&value)
.try_into()
.map_err(|error| panic!("{error}"))
.unwrap();
(key, value)
})
.collect()
}
#[allow(clippy::large_enum_variant)]
pub(crate) enum ArgumentParsingResult {
ChromeProcess(Opts, Preferences, ServoShellPreferences),
ContentProcess(String),
}
pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsingResult {
let (app_name, args) = args.split_first().unwrap();
let mut opts = Options::new();
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");
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(
"",
"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.",
"--prefs-file /path/to/prefs.json",
);
let opt_match = match opts.parse(args) {
Ok(m) => m,
Err(f) => args_fail(&f.to_string()),
};
let prefs_from_files: Vec<HashMap<String, PrefValue>> = opts_matches
.opt_strs("prefs-file")
.iter()
.map(|path| read_prefs_file(path))
.collect();
if opt_match.opt_present("v") || opt_match.opt_present("version") {
println!("{}", crate::servo_version());
process::exit(0);
}
let argprefs: HashMap<String, PrefValue> = opts_matches
.opt_strs("pref")
.iter()
.map(|pref| {
let split: Vec<&str> = pref.splitn(2, '=').collect();
let pref_name = split[0];
let pref_value = match split.get(1).cloned() {
Some("true") | None => PrefValue::Bool(true),
Some("false") => PrefValue::Bool(false),
Some(string) => {
if let Ok(int) = string.parse::<i64>() {
PrefValue::Int(int)
} else if let Ok(float) = string.parse::<f64>() {
PrefValue::Float(float)
} else {
PrefValue::from(string)
}
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);
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);
}
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);
}
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)),
},
};
(pref_name.to_string(), pref_value)
},
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);
}
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')
.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);
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();
// Apply --prefs-file prefs first
for prefs in prefs_from_files {
userprefs.extend(prefs);
// Handle all command-line preferences overrides.
for pref in opt_match.opt_strs("pref") {
let split: Vec<&str> = pref.splitn(2, '=').collect();
let pref_name = split[0];
let pref_value = PrefValue::from_booleanish_str(split.get(1).copied().unwrap_or("true"));
preferences.set_value(pref_name, pref_value);
}
// Then apply individually passed prefs from --pref
userprefs.extend(argprefs);
let legacy_layout = opt_match.opt_present("legacy-layout");
if legacy_layout {
preferences.layout_legacy_layout = true;
}
prefs::add_user_prefs(userprefs);
if let Some(layout_threads) = layout_threads {
preferences.layout_threads = layout_threads as i64;
}
let no_native_titlebar = opt_match.opt_present("no-native-titlebar");
let mut device_pixel_ratio_override = opt_match.opt_str("device-pixel-ratio").map(|dppx_str| {
dppx_str.parse().unwrap_or_else(|err| {
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_file = opt_match.opt_str("o");
if output_file.is_some() {
device_pixel_ratio_override = Some(1.0);
}
let url = if !opt_match.free.is_empty() {
Some(opt_match.free[0][..].into())
} else {
None
};
// 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") {
preferences.js_baseline_interpreter_enabled = false;
preferences.js_baseline_jit_enabled = false;
preferences.js_ion_enabled = false;
}
let servoshell_preferences = ServoShellPreferences {
user_agent: opt_match.opt_str("u"),
url,
no_native_titlebar,
device_pixel_ratio_override,
clean_shutdown: opt_match.opt_present("clean-shutdown"),
..Default::default()
};
let headless = opt_match.opt_present("z");
if headless && preferences.media_glvideo_enabled {
warn!("GL video rendering is not supported on headless windows.");
preferences.media_glvideo_enabled = false;
}
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,
headless,
hard_fail: opt_match.opt_present("f") && !opt_match.opt_present("F"),
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,
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"),
};
ArgumentParsingResult::ChromeProcess(opts, preferences, servoshell_preferences)
}
fn read_prefs_file(path: &str) -> HashMap<String, PrefValue> {
let mut file = File::open(path).expect("Error opening user prefs");
let mut txt = String::new();
file.read_to_string(&mut txt)
.expect("Can't read user prefs file");
prefs::read_prefs_map(&txt).expect("Can't parse user prefs file")
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_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)
}
#[cfg(test)]
fn test_parse_pref(arg: &str) {
let mut opts = getopts::Options::new();
opts.optmulti("", "pref", "", "");
opts.optmulti("", "prefs-file", "", "");
fn test_parse_pref(arg: &str) -> Preferences {
let args = vec!["servo".to_string(), "--pref".to_string(), arg.to_string()];
let matches = match opts::from_cmdline_args(opts, &args) {
opts::ArgumentParsingResult::ContentProcess(m, _) => m,
opts::ArgumentParsingResult::ChromeProcess(m) => m,
};
register_user_prefs(&matches);
match parse_command_line_arguments(args) {
ArgumentParsingResult::ContentProcess(..) => {
unreachable!("No preferences for content process")
},
ArgumentParsingResult::ChromeProcess(_, preferences, _) => preferences,
}
}
#[test]
fn test_parse_pref_from_command_line() {
use servo::servo_config::pref;
// Test with boolean values.
test_parse_pref("dom.bluetooth.enabled=true");
assert_eq!(
prefs::pref_map().get("dom.bluetooth.enabled"),
PrefValue::Bool(true)
);
assert!(pref!(dom.bluetooth.enabled));
let preferences = test_parse_pref("dom_bluetooth_enabled=true");
assert!(preferences.dom_bluetooth_enabled);
test_parse_pref("dom.bluetooth.enabled=false");
assert_eq!(
prefs::pref_map().get("dom.bluetooth.enabled"),
PrefValue::Bool(false)
);
assert_eq!(pref!(dom.bluetooth.enabled), false);
let preferences = test_parse_pref("dom_bluetooth_enabled=false");
assert!(!preferences.dom_bluetooth_enabled);
// Test with numbers
test_parse_pref("layout.threads=42");
assert_eq!(pref!(layout.threads), 42);
let preferences = test_parse_pref("layout_threads=42");
assert_eq!(preferences.layout_threads, 42);
// Test string.
test_parse_pref("shell.homepage=str");
assert_eq!(pref!(shell.homepage), "str");
let preferences = test_parse_pref("fonts_default=Lucida");
assert_eq!(preferences.fonts_default, "Lucida");
// Test with no value (defaults to true).
prefs::pref_map()
.set("dom.bluetooth.enabled", false)
.unwrap();
test_parse_pref("dom.bluetooth.enabled");
assert!(pref!(dom.bluetooth.enabled));
let preferences = test_parse_pref("dom_bluetooth_enabled");
assert!(preferences.dom_bluetooth_enabled);
}
#[test]
@ -130,12 +744,18 @@ fn test_invalid_prefs_from_command_line_panics() {
.err()
.and_then(|a| a.downcast_ref::<String>().cloned())
.expect("Should panic");
assert!(
err_msg.starts_with("Error setting preference"),
assert_eq!(
err_msg, "Unknown preference: \"doesntexist\"",
"Message should describe the problem"
);
assert!(
err_msg.contains("doesntexist"),
"Message should mention the name of the preference"
);
)
}
#[test]
fn test_create_prefs_map() {
let json_str = "{
\"layout.writing-mode.enabled\": true,
\"network.mime.sniff\": false,
\"shell.homepage\": \"https://servo.org\"
}";
assert_eq!(read_prefs_map(json_str).len(), 3);
}

View file

@ -89,15 +89,17 @@ fn test_argument_parsing_special() {
// Helper function to test url
fn test_url(input: &str, location: &str, cmdline_if_exists: &str, cmdline_otherwise: &str) {
assert_eq!(
location_bar_input_to_url(input).unwrap().into_string(),
location_bar_input_to_url(input, "https://duckduckgo.com/html/?q=%s")
.unwrap()
.into_string(),
location
);
assert_eq!(
get_default_url(Some(input), FAKE_CWD, |_| true).into_string(),
get_default_url(Some(input), FAKE_CWD, |_| true, &Default::default()).into_string(),
cmdline_if_exists
);
assert_eq!(
get_default_url(Some(input), FAKE_CWD, |_| false).into_string(),
get_default_url(Some(input), FAKE_CWD, |_| false, &Default::default()).into_string(),
cmdline_otherwise
);
}