Rewild Your Web
web
browser
dweb
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::sync::Mutex;
7use std::{env, fs};
8
9use servo::resources::{self, Resource};
10
11static CMD_RESOURCE_DIR: Mutex<Option<PathBuf>> = Mutex::new(None);
12
13struct ResourceReader;
14
15pub fn init() {
16 resources::set(Box::new(ResourceReader));
17}
18
19pub(crate) fn resources_dir_path() -> PathBuf {
20 // This needs to be called before the process is sandboxed
21 // as we only give permission to read inside the resources directory,
22 // not the permissions the "search" for the resources directory.
23 let mut dir = CMD_RESOURCE_DIR.lock().unwrap();
24 if let Some(ref path) = *dir {
25 return PathBuf::from(path);
26 }
27
28 // Try ./resources and ./Resources relative to the directory containing the
29 // canonicalised executable path, then each of its ancestors.
30 let mut path = env::current_exe().unwrap().canonicalize().unwrap();
31 while path.pop() {
32 path.push("resources");
33 if path.is_dir() {
34 *dir = Some(path);
35 return dir.clone().unwrap();
36 }
37 path.pop();
38
39 // Check for Resources on mac when using a case sensitive filesystem.
40 path.push("Resources");
41 if path.is_dir() {
42 *dir = Some(path);
43 return dir.clone().unwrap();
44 }
45 path.pop();
46 }
47
48 panic!("Could not find resources directory");
49}
50
51impl resources::ResourceReaderMethods for ResourceReader {
52 fn read(&self, file: Resource) -> Vec<u8> {
53 let mut path = resources_dir_path();
54 path.push(file.filename());
55 fs::read(path).expect("Can't read file")
56 }
57
58 fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> {
59 vec![resources_dir_path()]
60 }
61
62 fn sandbox_access_files(&self) -> Vec<PathBuf> {
63 vec![]
64 }
65}