Rewild Your Web
web browser dweb
at main 49 lines 1.4 kB view raw
1/* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5use std::path::PathBuf; 6use std::{env, fs}; 7 8use servo::resources::{self, Resource}; 9 10struct ResourceReader { 11 resources_dir: PathBuf, 12} 13 14pub fn init() { 15 resources::set(Box::new(ResourceReader { 16 resources_dir: ancestor_dir_path("resources"), 17 })); 18} 19 20pub(crate) fn ancestor_dir_path(needle: &str) -> PathBuf { 21 // Try ./resources relative to the directory containing the 22 // canonicalised executable path, then each of its ancestors. 23 let mut path = env::current_exe().unwrap().canonicalize().unwrap(); 24 while path.pop() { 25 path.push(needle); 26 if path.is_dir() { 27 return path.clone(); 28 } 29 path.pop(); 30 } 31 32 panic!("Could not find {needle} directory"); 33} 34 35impl resources::ResourceReaderMethods for ResourceReader { 36 fn read(&self, file: Resource) -> Vec<u8> { 37 let mut path = self.resources_dir.clone(); 38 path.push(file.filename()); 39 fs::read(path).expect("Can't read file") 40 } 41 42 fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> { 43 vec![self.resources_dir.clone()] 44 } 45 46 fn sandbox_access_files(&self) -> Vec<PathBuf> { 47 vec![] 48 } 49}