diff options
Diffstat (limited to 'ports/servoshell/resources.rs')
-rw-r--r-- | ports/servoshell/resources.rs | 68 |
1 files changed, 30 insertions, 38 deletions
diff --git a/ports/servoshell/resources.rs b/ports/servoshell/resources.rs index 91ad584c26d..d27a1714969 100644 --- a/ports/servoshell/resources.rs +++ b/ports/servoshell/resources.rs @@ -4,81 +4,73 @@ use std::path::PathBuf; use std::sync::Mutex; -use std::{env, fs, io}; +use std::{env, fs}; use servo::embedder_traits::resources::{self, Resource}; lazy_static::lazy_static! { - static ref CMD_RESOURCE_DIR: Mutex<Option<String>> = Mutex::new(None); + static ref CMD_RESOURCE_DIR: Mutex<Option<PathBuf>> = Mutex::new(None); } struct ResourceReader; -fn filename(file: Resource) -> &'static str { - match file { - Resource::Preferences => "prefs.json", - Resource::BluetoothBlocklist => "gatt_blocklist.txt", - Resource::DomainList => "public_domains.txt", - 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", - } -} - pub fn init() { resources::set(Box::new(ResourceReader)); } -fn resources_dir_path() -> io::Result<PathBuf> { +fn resources_dir_path() -> PathBuf { // This needs to be called before the process is sandboxed // as we only give permission to read inside the resources directory, // not the permissions the "search" for the resources directory. let mut dir = CMD_RESOURCE_DIR.lock().unwrap(); if let Some(ref path) = *dir { - return Ok(PathBuf::from(path)); + return PathBuf::from(path); } - // FIXME: Find a way to not rely on the executable being - // under `<servo source>[/$target_triple]/target/debug` - // or `<servo source>[/$target_triple]/target/release`. - let mut path = env::current_exe()?; - // Follow symlink - path = path.canonicalize()?; - + // Try ./resources and ./Resources relative to the directory containing the + // canonicalised executable path, then each of its ancestors. + let mut path = env::current_exe().unwrap().canonicalize().unwrap(); while path.pop() { path.push("resources"); if path.is_dir() { - break; + *dir = Some(path); + return dir.clone().unwrap(); } path.pop(); + // Check for Resources on mac when using a case sensitive filesystem. path.push("Resources"); if path.is_dir() { - break; + *dir = Some(path); + return dir.clone().unwrap(); } path.pop(); } - *dir = Some(path.to_str().unwrap().to_owned()); - Ok(path) + + // Try ./resources in the current directory, then each of its ancestors. + let mut path = std::env::current_dir().unwrap(); + loop { + path.push("resources"); + if path.is_dir() { + *dir = Some(path); + return dir.clone().unwrap(); + } + path.pop(); + + if !path.pop() { + panic!("Can't find resources directory") + } + } } impl resources::ResourceReaderMethods for ResourceReader { fn read(&self, file: Resource) -> Vec<u8> { - let file = filename(file); - let mut path = resources_dir_path().expect("Can't find resources directory"); - path.push(file); + let mut path = resources_dir_path(); + path.push(file.filename()); fs::read(path).expect("Can't read file") } fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> { - vec![resources_dir_path().expect("Can't find resources directory")] + vec![resources_dir_path()] } fn sandbox_access_files(&self) -> Vec<PathBuf> { vec![] |