mirror of
https://github.com/servo/servo
synced 2026-04-25 17:15:48 +02:00
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`](https://crates.io/crates/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](https://github.com/jschwe/servo/actions/runs/23811669469) Fixes: Part of #43145 --------- Signed-off-by: Jonathan Schwender <schwenderjonathan@gmail.com>
47 lines
1.4 KiB
Rust
47 lines
1.4 KiB
Rust
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* 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::fs;
|
|
use std::path::PathBuf;
|
|
use std::sync::OnceLock;
|
|
|
|
use servo::resources::{Resource, ResourceReaderMethods};
|
|
|
|
pub(crate) struct ResourceReaderImpl {
|
|
resource_dir: OnceLock<PathBuf>,
|
|
}
|
|
|
|
static RESOURCE_READER: ResourceReaderImpl = ResourceReaderImpl {
|
|
resource_dir: OnceLock::new(),
|
|
};
|
|
|
|
servo::submit_resource_reader!(&RESOURCE_READER);
|
|
|
|
pub(crate) fn set_resource_dir(resource_dir: PathBuf) {
|
|
if let Err(e) = RESOURCE_READER.resource_dir.set(resource_dir) {
|
|
log::warn!("Failed to set resource dir: {:?}", e);
|
|
}
|
|
}
|
|
|
|
impl ResourceReaderMethods for ResourceReaderImpl {
|
|
fn read(&self, res: Resource) -> Vec<u8> {
|
|
let file_path = RESOURCE_READER
|
|
.resource_dir
|
|
.get()
|
|
.expect("Attempted to read resources before reader initialized")
|
|
.join("named_resources")
|
|
.join(res.filename());
|
|
fs::read(&file_path).unwrap_or_else(|e| {
|
|
panic!("Failed to read resource file: {:?}: {:?}", file_path, e);
|
|
})
|
|
}
|
|
|
|
fn sandbox_access_files(&self) -> Vec<PathBuf> {
|
|
vec![]
|
|
}
|
|
|
|
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> {
|
|
vec![]
|
|
}
|
|
}
|