Skip to content

Embed default resources in Servo applications using a servo-default-resources crate#43182

Merged
jschwe merged 12 commits intoservo:mainfrom
jschwe:move-embedder-resources
Apr 1, 2026
Merged

Embed default resources in Servo applications using a servo-default-resources crate#43182
jschwe merged 12 commits intoservo:mainfrom
jschwe:move-embedder-resources

Conversation

@jschwe
Copy link
Copy Markdown
Member

@jschwe jschwe commented Mar 11, 2026

This PR considers the following constraints:

  • Resources must be available when building servo via a published crates.io package (i.e. no ../../../resources/<file> file references).
  • Minimal setup when writing tests (nextest spawns each test in its own process, so we don't want to explicitly initialize the resource handler for every #[test] fn)
  • Use local resources when developing locally
  • Support loading the resources from a proper resource directory if the embedder wishes so, including via a custom mechanism, not necessarily as files

(File) Resources that are only accessed from servoshell are out of scope of this PR, since it mainly focusses on unblocking publishing libservo to crates.io.

Baking the resources into the binary by default simplifies the setup a lot. We already supported that before, but only for testing purposes and explicitly not for production builds.

Using inventory adds a simple way for the embedder to replace the default baked in resources, while also keeping the test usage of baked in resources simple.

rippy.png is also referenced from image_cache - We simply duplicate it, since the image is small, to avoid adding unnecessarily complex solutions like adding a dedicated crate.

Testing: Covered by existing tests. mach try full
Fixes: Part of #43145

Comment thread ports/servoshell/Cargo.toml Outdated
@jschwe
Copy link
Copy Markdown
Member Author

jschwe commented Mar 12, 2026

So, there is a bit of a tricky bit here:

  • We have the named resources (now placed in embedder-traits)
  • We have a limited number of FS based resources, we read from the filesystem in the "resource dir" (default user-agent-js and files served by the resource protocol handler in servoshell)

I'm tending towards splitting resource protocol handler related things into a separate folder in our repository, and am considering having two different resource readers. One in embedder-traits for the named resources. And the one in servoshell, is "embedder-specific" and would handle the resource protocol. Perhaps a helpful abstraction like FsResourceReader could be made, to reduce boilerplate, but some embedders (ohos / android), might prefer to not read resources (resource protocol resources) from an actual file, but use platform specific APIs to read data from the app-bundle.

The user-agent-js file could probably be changed to be a named resource too.

I'm also thinking about how to search for the resources directory, since "next to the binary" doesn't work well for the standard in-tree approach, and probably we need to have code to support both developer experience for in-tree builds, while also handling production builds appropriatly.

@webbeef
Copy link
Copy Markdown
Contributor

webbeef commented Mar 12, 2026

The resource:// protocol handler already reads only from resources/resource_protocol so that should be easy to move somewhere else.

@mrobinson
Copy link
Copy Markdown
Member

Perhaps we should just remove support for the "user-agent-js" directory. This seems like something that should be handled by the embedder using the UserContentManager API.

github-merge-queue Bot pushed a commit that referenced this pull request Mar 23, 2026
In #43182 it was [suggested to remove the
userscripts](#43182 (comment))
option, since embedders can use the (new) `UserContentManager` API
instead.

Testing: This removes a commandline flag from servoshell. Existing tests
passing is sufficient.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
@jschwe jschwe force-pushed the move-embedder-resources branch 2 times, most recently from 04463a9 to 7810e2a Compare March 24, 2026 14:31
@jschwe
Copy link
Copy Markdown
Member Author

jschwe commented Mar 24, 2026

I'm also thinking about how to search for the resources directory, since "next to the binary" doesn't work well for the standard in-tree approach, and probably we need to have code to support both developer experience for in-tree builds, while also handling production builds appropriatly.

I pushed a proposal / draft for this, which uses mach (well an environment variable really) to determine if we are a developer build or not and use the in-tree resources. When packaging we package the named resources into the global resource directory, which servoshell will search for. I'm not really happy, but I'm also been going a bit in circles and don't have a great idea otherwise on how to load the resources from the embedder-traits (supporting both crates.io builds and local mach builds) when developing and from somewhere else when shipping a packaged servo.

Comment thread ports/servoshell/resources/named.rs Outdated
@jschwe jschwe force-pushed the move-embedder-resources branch from 7810e2a to b40690e Compare March 30, 2026 07:41
@jschwe jschwe marked this pull request as ready for review March 30, 2026 07:41
@jschwe jschwe requested a review from atbrakhi as a code owner March 30, 2026 07:41
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Mar 30, 2026
Comment thread components/shared/embedder/resources.rs Outdated
jschwe added 3 commits March 31, 2026 12:03
We want to bake the resources in, since it simplifies our tests.
This requires us to ship the resources in the package.
rippy.png is also referenced from image_cache - We simply duplicate
it, since the image is small, to avoid adding unnecessarily complex
solutions like adding a dedicated crate.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
@jschwe jschwe force-pushed the move-embedder-resources branch from b40690e to da9506c Compare March 31, 2026 10:08
@jschwe
Copy link
Copy Markdown
Member Author

jschwe commented Mar 31, 2026

I pushed a different proposal (loosely based on a suggestion of @mrobinson ) baking in resources on most platforms by default (but optional, as feature flag. To make it convenient to override, this uses the inventory crate which internally is basically like attribute(__constructor__) in C/C++ but with a safe API.
Embedders can disable the baked-in-resources default feature and use inventory::submit!() to submit a ResourceReader implementation.

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
@jschwe jschwe requested a review from mrobinson March 31, 2026 11:49
jschwe added 2 commits March 31, 2026 14:01
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Comment thread ports/servoshell/desktop/protocols/resource.rs
Copy link
Copy Markdown
Member

@mrobinson mrobinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me! I have a few comments and one request: Let's move default-resources out of shared as that is meant for the *-traits and *-api crates. It will likely be eliminated sometime in the future.

Comment thread components/shared/default-resources/Cargo.toml Outdated
Comment thread components/shared/embedder/resources.rs Outdated
Comment thread components/shared/embedder/resources.rs Outdated
Comment thread components/storage/Cargo.toml Outdated
use servo_base::generic_channel as base_channel;
use servo_base::generic_channel::GenericSend;
use servo_base::id::TEST_WEBVIEW_ID;
use servo_default_resources as _;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this necessary just so that this doesn't show up in tools like machete?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's necessary to make sure rust links the crate (and hence the resource reader is registered). I think extern crate syntax would also work. I'll check machete out and see if it warns about this. I see there is a way to ignore false positives, so probably we can do something. (I'm wondering if machete is being used by rust-rover under the hood)/

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

machete doesn't complain about these usages. But we still need this, otherwise the default resources crate will not be linked (declaring the dependency in Cargo.toml is not enough, which was a bit surprising to me, but I guess makes sense).

Comment thread ports/servoshell/resources/mod.rs Outdated
Comment thread python/servo/bootstrap_commands.py Outdated
Comment thread python/servo/bootstrap_commands.py Outdated
jschwe added 2 commits March 31, 2026 17:40
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
jschwe added 4 commits March 31, 2026 17:40
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
broken by "Move default-resource out of shared"

Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
@jschwe
Copy link
Copy Markdown
Member Author

jschwe commented Apr 1, 2026

@mrobinson I believe I have addressed all comments. Let me know if this PR is good to go.

Copy link
Copy Markdown
Member

@mrobinson mrobinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In many ways this seems simpler than what we had before which is really nice.

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Apr 1, 2026
@mrobinson mrobinson changed the title Move embedder resources to embedder_traits Embed default resources in Servo applications using a servo-default-resources crate Apr 1, 2026
@jschwe jschwe added this pull request to the merge queue Apr 1, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 1, 2026
Merged via the queue into servo:main with commit f4877c1 Apr 1, 2026
32 checks passed
@jschwe jschwe deleted the move-embedder-resources branch April 1, 2026 08:05
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants