servoshell: Support runtime preference manipulation (#38159)

These changes add a custom servo:preferences URL that allows modifying
selected preferences at runtime. The goal of this work is to make it
easy to test pages while toggling experimental web platform features,
and support quickly changing the User-Agent header.

Testing: Manually verified that spacex.com loads correctly after
changing the user agent, and that https://polygon.io/ displays grid
elements correctly and no console errors with the experimental prefs
enabled.
Fixes: #35862

<img width="1136" height="880" alt="Screenshot 2025-07-18 at 1 06 23 AM"
src="https://github.com/user-attachments/assets/2d27c321-6ca0-43c3-a347-7bc4b55272df"
/>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-08-30 12:51:58 -04:00 committed by GitHub
parent c97ec1b2fb
commit 6565d982bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 308 additions and 25 deletions

View file

@ -4,22 +4,37 @@
//! Loads resources using a mapping from well-known shortcuts to resource: urls.
//! Recognized shortcuts:
//! - servo:default-user-agent
//! - servo:experimental-preferences
//! - servo:newtab
//! - servo:preferences
use std::future::Future;
use std::pin::Pin;
use headers::{ContentType, HeaderMapExt};
use net::fetch::methods::{DoneChannel, FetchContext};
use net::protocols::ProtocolHandler;
use net_traits::ResourceFetchTiming;
use net_traits::request::Request;
use net_traits::response::Response;
use net_traits::response::{Response, ResponseBody};
use servo::config::prefs::UserAgentPlatform;
use crate::desktop::protocols::resource::ResourceProtocolHandler;
use crate::prefs::EXPERIMENTAL_PREFS;
#[derive(Default)]
pub struct ServoProtocolHandler {}
impl ProtocolHandler for ServoProtocolHandler {
fn privileged_paths(&self) -> &'static [&'static str] {
&["preferences"]
}
fn is_fetchable(&self) -> bool {
true
}
fn load(
&self,
request: &mut Request,
@ -35,9 +50,44 @@ impl ProtocolHandler for ServoProtocolHandler {
context,
"/newtab.html",
),
"preferences" => ResourceProtocolHandler::response_for_path(
request,
done_chan,
context,
"/preferences.html",
),
"experimental-preferences" => {
let pref_list = EXPERIMENTAL_PREFS
.iter()
.map(|pref| format!("\"{pref}\""))
.collect::<Vec<String>>()
.join(",");
json_response(request, format!("[{pref_list}]"))
},
"default-user-agent" => {
let user_agent = UserAgentPlatform::default().to_user_agent_string();
json_response(request, format!("\"{user_agent}\""))
},
_ => Box::pin(std::future::ready(Response::network_internal_error(
"Invalid shortcut",
))),
}
}
}
fn json_response(
request: &Request,
body: String,
) -> Pin<Box<dyn Future<Output = Response> + Send>> {
let mut response = Response::new(
request.current_url(),
ResourceFetchTiming::new(request.timing_type()),
);
response.headers.typed_insert(ContentType::json());
*response.body.lock().unwrap() = ResponseBody::Done(body.into_bytes());
Box::pin(std::future::ready(response))
}

View file

@ -20,6 +20,25 @@ use servo::servo_geometry::DeviceIndependentPixel;
use servo::servo_url::ServoUrl;
use url::Url;
pub(crate) static EXPERIMENTAL_PREFS: &[&str] = &[
"dom_async_clipboard_enabled",
"dom_fontface_enabled",
"dom_intersection_observer_enabled",
"dom_mouse_event_which_enabled",
"dom_navigator_sendbeacon_enabled",
"dom_notification_enabled",
"dom_offscreen_canvas_enabled",
"dom_permissions_enabled",
"dom_resize_observer_enabled",
"dom_trusted_types_enabled",
"dom_webgl2_enabled",
"dom_webgpu_enabled",
"dom_xpath_enabled",
"layout_columns_enabled",
"layout_container_queries_enabled",
"layout_grid_enabled",
];
#[cfg_attr(any(target_os = "android", target_env = "ohos"), allow(dead_code))]
#[derive(Clone)]
pub(crate) struct ServoShellPreferences {
@ -587,26 +606,9 @@ pub(crate) fn parse_command_line_arguments(args: Vec<String>) -> ArgumentParsing
.collect();
if opt_match.opt_present("enable-experimental-web-platform-features") {
vec![
"dom_async_clipboard_enabled",
"dom_fontface_enabled",
"dom_intersection_observer_enabled",
"dom_mouse_event_which_enabled",
"dom_navigator_sendbeacon_enabled",
"dom_notification_enabled",
"dom_offscreen_canvas_enabled",
"dom_permissions_enabled",
"dom_resize_observer_enabled",
"dom_trusted_types_enabled",
"dom_webgl2_enabled",
"dom_webgpu_enabled",
"dom_xpath_enabled",
"layout_columns_enabled",
"layout_container_queries_enabled",
"layout_grid_enabled",
]
.iter()
.for_each(|pref| preferences.set_value(pref, PrefValue::Bool(true)));
for pref in EXPERIMENTAL_PREFS {
preferences.set_value(pref, PrefValue::Bool(true));
}
}
// Handle all command-line preferences overrides.