From e227e0913bf1728c2fead8ae49c7511ddaabe996 Mon Sep 17 00:00:00 2001 From: Martin Robinson Date: Fri, 7 Feb 2025 21:04:31 +0100 Subject: [PATCH] servoshell: Move `headless` setting to ServoShellPreferences (#35377) This is only used in servoshell, even though it was plumbed through script previously. It's just about how the `RenderingContext` is set up, which is something managed entirely outside of servo itself. In addition, make the name of `servo_shell_preferences` in `app.rs` more consistent with the rest of the codebase. Signed-off-by: Martin Robinson --- components/config/opts.rs | 3 -- .../script/dom/dissimilaroriginwindow.rs | 1 - components/script/dom/globalscope.rs | 9 ------ components/script/dom/window.rs | 2 -- components/script/dom/workerglobalscope.rs | 2 -- components/script/dom/workletglobalscope.rs | 3 -- components/script/script_thread.rs | 6 ---- components/shared/script/lib.rs | 2 -- ports/servoshell/desktop/app.rs | 29 +++++++++---------- ports/servoshell/desktop/cli.rs | 2 +- ports/servoshell/prefs.rs | 9 ++++-- 11 files changed, 20 insertions(+), 48 deletions(-) diff --git a/components/config/opts.rs b/components/config/opts.rs index 212ce7cebc5..2bc90696a43 100644 --- a/components/config/opts.rs +++ b/components/config/opts.rs @@ -48,8 +48,6 @@ pub struct Opts { pub output_file: Option, - pub headless: bool, - /// True to exit on thread failure instead of displaying about:failure. pub hard_fail: bool, @@ -218,7 +216,6 @@ impl Default for Opts { userscripts: None, user_stylesheets: Vec::new(), output_file: None, - headless: false, hard_fail: true, webdriver_port: None, initial_window_size: Size2D::new(1024, 740), diff --git a/components/script/dom/dissimilaroriginwindow.rs b/components/script/dom/dissimilaroriginwindow.rs index 1eb5d3b2c67..4e34cc6e52d 100644 --- a/components/script/dom/dissimilaroriginwindow.rs +++ b/components/script/dom/dissimilaroriginwindow.rs @@ -64,7 +64,6 @@ impl DissimilarOriginWindow { // FIXME(nox): The microtask queue is probably not important // here, but this whole DOM interface is a hack anyway. global_to_clone_from.microtask_queue().clone(), - global_to_clone_from.is_headless(), global_to_clone_from.get_user_agent(), #[cfg(feature = "webgpu")] global_to_clone_from.wgpu_id_hub(), diff --git a/components/script/dom/globalscope.rs b/components/script/dom/globalscope.rs index 51d6b06b6f4..16d48a130d7 100644 --- a/components/script/dom/globalscope.rs +++ b/components/script/dom/globalscope.rs @@ -316,9 +316,6 @@ pub(crate) struct GlobalScope { #[allow(clippy::vec_box)] consumed_rejections: DomRefCell>>>, - /// True if headless mode. - is_headless: bool, - /// An optional string allowing the user agent to be set for testing. user_agent: Cow<'static, str>, @@ -716,7 +713,6 @@ impl GlobalScope { origin: MutableOrigin, creation_url: Option, microtask_queue: Rc, - is_headless: bool, user_agent: Cow<'static, str>, #[cfg(feature = "webgpu")] gpu_id_hub: Arc, inherited_secure_context: Option, @@ -751,7 +747,6 @@ impl GlobalScope { event_source_tracker: DOMTracker::new(), uncaught_rejections: Default::default(), consumed_rejections: Default::default(), - is_headless, user_agent, #[cfg(feature = "webgpu")] gpu_id_hub, @@ -2911,10 +2906,6 @@ impl GlobalScope { ); } - pub(crate) fn is_headless(&self) -> bool { - self.is_headless - } - pub(crate) fn get_user_agent(&self) -> Cow<'static, str> { self.user_agent.clone() } diff --git a/components/script/dom/window.rs b/components/script/dom/window.rs index 2315e92eba1..f496a1b8191 100644 --- a/components/script/dom/window.rs +++ b/components/script/dom/window.rs @@ -2778,7 +2778,6 @@ impl Window { unminify_css: bool, local_script_source: Option, userscripts_path: Option, - is_headless: bool, replace_surrogates: bool, user_agent: Cow<'static, str>, player_context: WindowGLContext, @@ -2807,7 +2806,6 @@ impl Window { origin, Some(creator_url), microtask_queue, - is_headless, user_agent, #[cfg(feature = "webgpu")] gpu_id_hub, diff --git a/components/script/dom/workerglobalscope.rs b/components/script/dom/workerglobalscope.rs index fe6e2c40c00..1cefa6972a8 100644 --- a/components/script/dom/workerglobalscope.rs +++ b/components/script/dom/workerglobalscope.rs @@ -79,7 +79,6 @@ pub(crate) fn prepare_workerscope_init( pipeline_id: global.pipeline_id(), origin: global.origin().immutable().clone(), creation_url: global.creation_url().clone(), - is_headless: global.is_headless(), user_agent: global.get_user_agent(), inherited_secure_context: Some(global.is_secure_context()), }; @@ -165,7 +164,6 @@ impl WorkerGlobalScope { MutableOrigin::new(init.origin), init.creation_url, runtime.microtask_queue.clone(), - init.is_headless, init.user_agent, #[cfg(feature = "webgpu")] gpu_id_hub, diff --git a/components/script/dom/workletglobalscope.rs b/components/script/dom/workletglobalscope.rs index 629bcd9a470..62c69e80509 100644 --- a/components/script/dom/workletglobalscope.rs +++ b/components/script/dom/workletglobalscope.rs @@ -96,7 +96,6 @@ impl WorkletGlobalScope { MutableOrigin::new(ImmutableOrigin::new_opaque()), None, Default::default(), - init.is_headless, init.user_agent.clone(), #[cfg(feature = "webgpu")] init.gpu_id_hub.clone(), @@ -186,8 +185,6 @@ pub(crate) struct WorkletGlobalScopeInit { pub(crate) to_constellation_sender: IpcSender<(PipelineId, ScriptMsg)>, /// The image cache pub(crate) image_cache: Arc, - /// True if in headless mode - pub(crate) is_headless: bool, /// An optional string allowing the user agent to be set for testing pub(crate) user_agent: Cow<'static, str>, /// Identity manager for WebGPU resources diff --git a/components/script/script_thread.rs b/components/script/script_thread.rs index 22eea4d8c90..9fe4b10e9b6 100644 --- a/components/script/script_thread.rs +++ b/components/script/script_thread.rs @@ -318,9 +318,6 @@ pub struct ScriptThread { /// won't be loaded userscripts_path: Option, - /// True if headless mode. - headless: bool, - /// Replace unpaired surrogates in DOM strings with U+FFFD. /// See replace_surrogates: bool, @@ -753,7 +750,6 @@ impl ScriptThread { .pipeline_to_constellation_sender .clone(), image_cache: script_thread.image_cache.clone(), - is_headless: script_thread.headless, user_agent: script_thread.user_agent.clone(), #[cfg(feature = "webgpu")] gpu_id_hub: script_thread.gpu_id_hub.clone(), @@ -961,7 +957,6 @@ impl ScriptThread { local_script_source: opts.local_script_source.clone(), unminify_css: opts.unminify_css, userscripts_path: opts.userscripts.clone(), - headless: opts.headless, replace_surrogates: opts.debug.replace_surrogates, user_agent, player_context: state.player_context, @@ -3151,7 +3146,6 @@ impl ScriptThread { self.unminify_css, self.local_script_source.clone(), self.userscripts_path.clone(), - self.headless, self.replace_surrogates, self.user_agent.clone(), self.player_context.clone(), diff --git a/components/shared/script/lib.rs b/components/shared/script/lib.rs index bc00f582921..9ec97d717f0 100644 --- a/components/shared/script/lib.rs +++ b/components/shared/script/lib.rs @@ -770,8 +770,6 @@ pub struct WorkerGlobalScopeInit { pub origin: ImmutableOrigin, /// The creation URL pub creation_url: Option, - /// True if headless mode - pub is_headless: bool, /// An optional string allowing the user agnet to be set for testing. pub user_agent: Cow<'static, str>, /// True if secure context diff --git a/ports/servoshell/desktop/app.rs b/ports/servoshell/desktop/app.rs index 2712e796b4e..8df09c0f90d 100644 --- a/ports/servoshell/desktop/app.rs +++ b/ports/servoshell/desktop/app.rs @@ -44,7 +44,7 @@ use crate::prefs::ServoShellPreferences; pub struct App { opts: Opts, preferences: Preferences, - servo_shell_preferences: ServoShellPreferences, + servoshell_preferences: ServoShellPreferences, suspended: Cell, windows: HashMap>, minibrowser: Option, @@ -88,7 +88,7 @@ impl App { App { opts, preferences, - servo_shell_preferences, + servoshell_preferences: servo_shell_preferences, suspended: Cell::new(false), windows: HashMap::new(), minibrowser: None, @@ -103,7 +103,7 @@ impl App { /// Initialize Application once event loop start running. pub fn init(&mut self, event_loop: Option<&ActiveEventLoop>) { // Create rendering context - let rendering_context = if self.opts.headless { + let rendering_context = if self.servoshell_preferences.headless { let connection = Connection::new().expect("Failed to create connection"); let adapter = connection .create_software_adapter() @@ -128,10 +128,11 @@ impl App { .expect("Failed to create WR surfman") }; - let window = if self.opts.headless { + let headless = self.servoshell_preferences.headless; + let window = if headless { headless_window::Window::new( self.opts.initial_window_size, - self.servo_shell_preferences.device_pixel_ratio_override, + self.servoshell_preferences.device_pixel_ratio_override, self.opts.screen_size_override, ) } else { @@ -140,8 +141,8 @@ impl App { &rendering_context, self.opts.initial_window_size, event_loop.unwrap(), - self.servo_shell_preferences.no_native_titlebar, - self.servo_shell_preferences.device_pixel_ratio_override, + self.servoshell_preferences.no_native_titlebar, + self.servoshell_preferences.device_pixel_ratio_override, )) }; @@ -158,7 +159,7 @@ impl App { self.suspended.set(false); let (_, window) = self.windows.iter().next().unwrap(); - let xr_discovery = if pref!(dom_webxr_openxr_enabled) && !self.opts.headless { + let xr_discovery = if pref!(dom_webxr_openxr_enabled) && !headless { #[cfg(target_os = "windows")] let openxr = { let app_info = AppInfo::new("Servoshell", 0, "Servo", 0); @@ -168,7 +169,7 @@ impl App { let openxr = None; openxr - } else if pref!(dom_webxr_glwindow_enabled) && !self.opts.headless { + } else if pref!(dom_webxr_glwindow_enabled) && !headless { let window = window.new_glwindow(event_loop.unwrap()); Some(XrDiscovery::GlWindow(GlWindowDiscovery::new(window))) } else { @@ -202,16 +203,12 @@ impl App { Rc::new(rendering_context), embedder, Rc::new(UpcastedWindow(window.clone())), - self.servo_shell_preferences.user_agent.clone(), + self.servoshell_preferences.user_agent.clone(), composite_target, ); servo.setup_logging(); - let running_state = Rc::new(RunningAppState::new( - servo, - window.clone(), - self.opts.headless, - )); + let running_state = Rc::new(RunningAppState::new(servo, window.clone(), headless)); running_state.new_toplevel_webview(self.initial_url.clone().into_url()); if let Some(ref mut minibrowser) = self.minibrowser { @@ -332,7 +329,7 @@ impl App { minibrowser.update_location_dirty(false); let Some(url) = location_bar_input_to_url( &location.clone(), - &self.servo_shell_preferences.searchpage, + &self.servoshell_preferences.searchpage, ) else { warn!("failed to parse location"); break; diff --git a/ports/servoshell/desktop/cli.rs b/ports/servoshell/desktop/cli.rs index 0e6c06368ba..588a2f04a9d 100644 --- a/ports/servoshell/desktop/cli.rs +++ b/ports/servoshell/desktop/cli.rs @@ -28,7 +28,7 @@ pub fn main() { }; let clean_shutdown = servoshell_preferences.clean_shutdown; - let event_loop = EventsLoop::new(opts.headless, opts.output_file.is_some()) + let event_loop = EventsLoop::new(servoshell_preferences.headless, opts.output_file.is_some()) .expect("Failed to create events loop"); { diff --git a/ports/servoshell/prefs.rs b/ports/servoshell/prefs.rs index 560e6514f1a..38829bf4e96 100644 --- a/ports/servoshell/prefs.rs +++ b/ports/servoshell/prefs.rs @@ -34,6 +34,9 @@ pub(crate) struct ServoShellPreferences { /// URL string of the search engine page with '%s' standing in for the search term. /// For example . pub searchpage: String, + /// Whether or not to run servoshell in headless mode. While running in headless + /// mode, image output is supported. + pub headless: bool, } impl Default for ServoShellPreferences { @@ -46,6 +49,7 @@ impl Default for ServoShellPreferences { homepage: "https://servo.org".into(), no_native_titlebar: true, searchpage: "https://duckduckgo.com/html/?q=%s".into(), + headless: false, } } } @@ -539,11 +543,11 @@ pub(crate) fn parse_command_line_arguments(args: Vec) -> ArgumentParsing no_native_titlebar, device_pixel_ratio_override, clean_shutdown: opt_match.opt_present("clean-shutdown"), + headless: opt_match.opt_present("z"), ..Default::default() }; - let headless = opt_match.opt_present("z"); - if headless && preferences.media_glvideo_enabled { + if servoshell_preferences.headless && preferences.media_glvideo_enabled { warn!("GL video rendering is not supported on headless windows."); preferences.media_glvideo_enabled = false; } @@ -558,7 +562,6 @@ pub(crate) fn parse_command_line_arguments(args: Vec) -> ArgumentParsing 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,