/* 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::path::PathBuf; use std::{env, fs}; use servo::resources::{self, Resource}; struct ResourceReader { resources_dir: PathBuf, } pub fn init() { resources::set(Box::new(ResourceReader { resources_dir: ancestor_dir_path("resources"), })); } pub(crate) fn ancestor_dir_path(needle: &str) -> PathBuf { // Try ./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(needle); if path.is_dir() { return path.clone(); } path.pop(); } panic!("Could not find {needle} directory"); } impl resources::ResourceReaderMethods for ResourceReader { fn read(&self, file: Resource) -> Vec { let mut path = self.resources_dir.clone(); path.push(file.filename()); fs::read(path).expect("Can't read file") } fn sandbox_access_files_dirs(&self) -> Vec { vec![self.resources_dir.clone()] } fn sandbox_access_files(&self) -> Vec { vec![] } }