Embed user agent stylesheets and media control resouces in libservo (#36803)

Embed user agent stylesheets and media control resouces in libservo as
decided in
https://github.com/servo/servo/pull/36788#issuecomment-2845332210

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2025-05-04 11:48:09 -07:00 committed by GitHub
parent 7e2d2ed0ce
commit 3db0194e5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 26 additions and 73 deletions

View file

@ -16,7 +16,6 @@ use base::Epoch;
use base::id::{PipelineId, WebViewId};
use compositing_traits::CrossProcessCompositorApi;
use constellation_traits::ScrollState;
use embedder_traits::resources::{self, Resource};
use embedder_traits::{UntrustedNodeAddress, ViewportDetails};
use euclid::default::{Point2D as UntypedPoint2D, Rect as UntypedRect, Size2D as UntypedSize2D};
use euclid::{Point2D, Scale, Size2D, Vector2D};
@ -100,6 +99,18 @@ thread_local!(static SEEN_POINTERS: LazyCell<RefCell<HashSet<*const c_void>>> =
LazyCell::new(|| RefCell::new(HashSet::new()))
});
/// A CSS file to style the user agent stylesheet.
static USER_AGENT_CSS: &[u8] = include_bytes!("./stylesheets/user-agent.css");
/// A CSS file to style the Servo browser.
static SERVO_CSS: &[u8] = include_bytes!("./stylesheets/servo.css");
/// A CSS file to style the presentational hints.
static PRESENTATIONAL_HINTS_CSS: &[u8] = include_bytes!("./stylesheets/presentational-hints.css");
/// A CSS file to style the quirks mode.
static QUIRKS_MODE_CSS: &[u8] = include_bytes!("./stylesheets/quirks-mode.css");
/// Information needed by layout.
pub struct LayoutThread {
/// The ID of the pipeline that we belong to.
@ -983,20 +994,12 @@ fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
// FIXME: presentational-hints.css should be at author origin with zero specificity.
// (Does it make a difference?)
let mut user_or_user_agent_stylesheets = vec![
parse_ua_stylesheet(
shared_lock,
"user-agent.css",
&resources::read_bytes(Resource::UserAgentCSS),
)?,
parse_ua_stylesheet(
shared_lock,
"servo.css",
&resources::read_bytes(Resource::ServoCSS),
)?,
parse_ua_stylesheet(shared_lock, "user-agent.css", USER_AGENT_CSS)?,
parse_ua_stylesheet(shared_lock, "servo.css", SERVO_CSS)?,
parse_ua_stylesheet(
shared_lock,
"presentational-hints.css",
&resources::read_bytes(Resource::PresentationalHintsCSS),
PRESENTATIONAL_HINTS_CSS,
)?,
];
@ -1017,11 +1020,8 @@ fn get_ua_stylesheets() -> Result<UserAgentStylesheets, &'static str> {
)));
}
let quirks_mode_stylesheet = parse_ua_stylesheet(
shared_lock,
"quirks-mode.css",
&resources::read_bytes(Resource::QuirksModeCSS),
)?;
let quirks_mode_stylesheet =
parse_ua_stylesheet(shared_lock, "quirks-mode.css", QUIRKS_MODE_CSS)?;
Ok(UserAgentStylesheets {
shared_lock: shared_lock.clone(),

View file

@ -225,7 +225,7 @@ fn test_fetch_blob() {
#[test]
fn test_file() {
let path = Path::new("../../resources/servo.css")
let path = Path::new("../../resources/ahem.css")
.canonicalize()
.unwrap();
let url = ServoUrl::from_file_path(path.clone()).unwrap();

View file

@ -12,7 +12,6 @@ use std::{f64, mem};
use compositing_traits::{CrossProcessCompositorApi, ImageUpdate, SerializableImageData};
use content_security_policy as csp;
use dom_struct::dom_struct;
use embedder_traits::resources::{self, Resource as EmbedderResource};
use embedder_traits::{MediaPositionState, MediaSessionEvent, MediaSessionPlaybackState};
use euclid::default::Size2D;
use headers::{ContentLength, ContentRange, HeaderMapExt};
@ -110,6 +109,12 @@ use crate::realms::{InRealm, enter_realm};
use crate::script_runtime::CanGc;
use crate::script_thread::ScriptThread;
/// A CSS file to style the media controls.
static MEDIA_CONTROL_CSS: &str = include_str!("../resources/media-controls.css");
/// A JS file to control the media controls.
static MEDIA_CONTROL_JS: &str = include_str!("../resources/media-controls.js");
#[derive(PartialEq)]
enum FrameStatus {
Locked,
@ -1949,14 +1954,13 @@ impl HTMLMediaElement {
ElementCreator::ScriptCreated,
can_gc,
);
let mut media_controls_script = resources::read_string(EmbedderResource::MediaControlsJS);
// This is our hacky way to temporarily workaround the lack of a privileged
// JS context.
// The media controls UI accesses the document.servoGetMediaControls(id) API
// to get an instance to the media controls ShadowRoot.
// `id` needs to match the internally generated UUID assigned to a media element.
let id = document.register_media_controls(&shadow_root);
let media_controls_script = media_controls_script.as_mut_str().replace("@@@id@@@", &id);
let media_controls_script = MEDIA_CONTROL_JS.replace("@@@id@@@", &id);
*self.media_controls_id.borrow_mut() = Some(id);
script
.upcast::<Node>()
@ -1969,7 +1973,6 @@ impl HTMLMediaElement {
return;
}
let media_controls_style = resources::read_string(EmbedderResource::MediaControlsCSS);
let style = HTMLStyleElement::new(
local_name!("script"),
None,
@ -1980,7 +1983,7 @@ impl HTMLMediaElement {
);
style
.upcast::<Node>()
.SetTextContent(Some(DOMString::from(media_controls_style)), can_gc);
.SetTextContent(Some(DOMString::from(MEDIA_CONTROL_CSS)), can_gc);
if let Err(e) = shadow_root
.upcast::<Node>()

View file

@ -91,18 +91,6 @@ pub enum Resource {
/// The message can contain a placeholder `${reason}` for the error code.
/// It can be empty but then nothing will be displayed when an internal error occurs.
NetErrorHTML,
/// A CSS file to style the user agent stylesheet.
/// It can be empty but then there's simply no user agent stylesheet.
UserAgentCSS,
/// A CSS file to style the Servo browser.
/// It can be empty but several features might not work as expected.
ServoCSS,
/// A CSS file to style the presentational hints.
/// It can be empty but then presentational hints will not be styled.
PresentationalHintsCSS,
/// A CSS file to style the quirks mode.
/// It can be empty but then quirks mode will not be styled.
QuirksModeCSS,
/// A placeholder image to display if we couldn't get the requested image.
///
/// ## Panic
@ -110,12 +98,6 @@ pub enum Resource {
/// If the resource is not provided, servo will fallback to a baked in default (See resources/rippy.png).
/// However, if the image is provided but invalid, Servo will crash.
RippyPNG,
/// A CSS file to style the media controls.
/// It can be empty but then media controls will not be styled.
MediaControlsCSS,
/// A JS file to control the media controls.
/// It can be empty but then media controls will not work.
MediaControlsJS,
/// A placeholder HTML page to display when the code responsible for rendering a page panics and the original
/// page can no longer be displayed.
/// The message can contain a placeholder `${details}` for the error details.
@ -137,13 +119,7 @@ impl Resource {
Resource::HstsPreloadList => "hsts_preload.json",
Resource::BadCertHTML => "badcert.html",
Resource::NetErrorHTML => "neterror.html",
Resource::UserAgentCSS => "user-agent.css",
Resource::ServoCSS => "servo.css",
Resource::PresentationalHintsCSS => "presentational-hints.css",
Resource::QuirksModeCSS => "quirks-mode.css",
Resource::RippyPNG => "rippy.png",
Resource::MediaControlsCSS => "media-controls.css",
Resource::MediaControlsJS => "media-controls.js",
Resource::CrashHTML => "crash.html",
Resource::DirectoryListingHTML => "directory-listing.html",
Resource::AboutMemoryHTML => "about-memory.html",
@ -183,21 +159,7 @@ fn resources_for_tests() -> Box<dyn ResourceReaderMethods + Sync + Send> {
},
Resource::BadCertHTML => &include_bytes!("../../../resources/badcert.html")[..],
Resource::NetErrorHTML => &include_bytes!("../../../resources/neterror.html")[..],
Resource::UserAgentCSS => &include_bytes!("../../../resources/user-agent.css")[..],
Resource::ServoCSS => &include_bytes!("../../../resources/servo.css")[..],
Resource::PresentationalHintsCSS => {
&include_bytes!("../../../resources/presentational-hints.css")[..]
},
Resource::QuirksModeCSS => {
&include_bytes!("../../../resources/quirks-mode.css")[..]
},
Resource::RippyPNG => &include_bytes!("../../../resources/rippy.png")[..],
Resource::MediaControlsCSS => {
&include_bytes!("../../../resources/media-controls.css")[..]
},
Resource::MediaControlsJS => {
&include_bytes!("../../../resources/media-controls.js")[..]
},
Resource::CrashHTML => &include_bytes!("../../../resources/crash.html")[..],
Resource::DirectoryListingHTML => {
&include_bytes!("../../../resources/directory-listing.html")[..]

View file

@ -21,23 +21,11 @@ impl ResourceReaderMethods for ResourceReaderInstance {
},
Resource::BadCertHTML => &include_bytes!("../../../../resources/badcert.html")[..],
Resource::NetErrorHTML => &include_bytes!("../../../../resources/neterror.html")[..],
Resource::UserAgentCSS => &include_bytes!("../../../../resources/user-agent.css")[..],
Resource::ServoCSS => &include_bytes!("../../../../resources/servo.css")[..],
Resource::PresentationalHintsCSS => {
&include_bytes!("../../../../resources/presentational-hints.css")[..]
},
Resource::QuirksModeCSS => &include_bytes!("../../../../resources/quirks-mode.css")[..],
Resource::RippyPNG => &include_bytes!("../../../../resources/rippy.png")[..],
Resource::DomainList => &include_bytes!("../../../../resources/public_domains.txt")[..],
Resource::BluetoothBlocklist => {
&include_bytes!("../../../../resources/gatt_blocklist.txt")[..]
},
Resource::MediaControlsCSS => {
&include_bytes!("../../../../resources/media-controls.css")[..]
},
Resource::MediaControlsJS => {
&include_bytes!("../../../../resources/media-controls.js")[..]
},
Resource::CrashHTML => &include_bytes!("../../../../resources/crash.html")[..],
Resource::DirectoryListingHTML => {
&include_bytes!("../../../../resources/directory-listing.html")[..]