1use std::path::PathBuf;
6use std::sync::Mutex;
7use std::{env, fs};
8
9use cfg_if::cfg_if;
10use servo::resources::{self, Resource};
11
12static CMD_RESOURCE_DIR: Mutex<Option<PathBuf>> = Mutex::new(None);
13
14struct ResourceReader;
15
16pub fn init() {
17 resources::set(Box::new(ResourceReader));
18}
19
20pub(crate) fn resources_dir_path() -> PathBuf {
21 let mut dir = CMD_RESOURCE_DIR.lock().unwrap();
25 if let Some(ref path) = *dir {
26 return PathBuf::from(path);
27 }
28
29 let mut path = env::current_exe().unwrap().canonicalize().unwrap();
32 while path.pop() {
33 path.push("resources");
34 if path.is_dir() {
35 *dir = Some(path);
36 return dir.clone().unwrap();
37 }
38 path.pop();
39
40 path.push("Resources");
42 if path.is_dir() {
43 *dir = Some(path);
44 return dir.clone().unwrap();
45 }
46 path.pop();
47 }
48
49 cfg_if! {
50 if #[cfg(servo_production)] {
51 panic!("Can't find resources directory")
52 } else {
53 const _: () = assert!(cfg!(servo_do_not_use_in_production));
56
57 let mut path = std::env::current_dir().unwrap();
60 loop {
61 path.push("resources");
62 if path.is_dir() {
63 *dir = Some(path);
64 return dir.clone().unwrap();
65 }
66 path.pop();
67
68 if !path.pop() {
69 panic!("Can't find resources directory")
70 }
71 }
72 }
73 }
74}
75
76impl resources::ResourceReaderMethods for ResourceReader {
77 fn read(&self, file: Resource) -> Vec<u8> {
78 let mut path = resources_dir_path();
79 path.push(file.filename());
80 fs::read(path).expect("Can't read file")
81 }
82 fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> {
83 vec![resources_dir_path()]
84 }
85 fn sandbox_access_files(&self) -> Vec<PathBuf> {
86 vec![]
87 }
88}