Skip to main content

browser_paths/
browser_path.rs

1use crate::browser_name::{BrowserChannel, BrowserKind, BrowserName, Platform};
2use std::path::PathBuf;
3use which::which;
4
5fn get_browser_path_on_linux(
6    browser_kind: BrowserKind,
7    channel: BrowserChannel,
8) -> Option<PathBuf> {
9    let browser_name = BrowserName(Platform::Linux, browser_kind, channel);
10    which(browser_name.to_str()).ok().map(|p| p.to_path_buf())
11}
12
13fn get_browser_path_on_windows(
14    browser_kind: BrowserKind,
15    channel: BrowserChannel,
16) -> Option<PathBuf> {
17    let browser_name = BrowserName(Platform::Windows, browser_kind, channel);
18    let suffix = match browser_kind {
19        BrowserKind::Chrome => {
20            format!("Google\\{}\\Application\\chrome.exe", browser_name.to_str())
21        }
22        BrowserKind::Edge => format!(
23            "Microsoft\\{}\\Application\\msedge.exe",
24            browser_name.to_str()
25        )
26    };
27    let prefixes: Vec<PathBuf> = [
28        std::env::var_os("LOCALAPPDATA").map(PathBuf::from),
29        std::env::var_os("PROGRAMFILES").map(PathBuf::from),
30        std::env::var_os("ProgramFiles(x86)").map(PathBuf::from),
31    ]
32    .into_iter()
33    .flatten()
34    .collect();
35    for prefix in prefixes {
36        let path = prefix.join(&suffix);
37        if path.exists() {
38            return Some(path);
39        }
40    }
41    None
42}
43
44fn get_browser_path_on_macos(
45    browser_kind: BrowserKind,
46    channel: BrowserChannel,
47) -> Option<PathBuf> {
48    let browser_name = BrowserName(Platform::MacOS, browser_kind, channel);
49    let default_path = PathBuf::from(format!(
50        "/Applications/{}.app/Contents/MacOS/{}",
51        browser_name.to_str(),
52        browser_name.to_str()
53    ));
54    if default_path.exists() {
55        return Some(default_path.to_path_buf());
56    }
57    None
58}
59
60pub fn get_browser_path(browser_kind: BrowserKind) -> Option<PathBuf> {
61    #[cfg(target_os = "linux")]
62    {
63        return get_browser_path_on_linux(browser_kind, BrowserChannel::Stable);
64    }
65    #[cfg(target_os = "windows")]
66    {
67        return get_browser_path_on_windows(browser_kind, BrowserChannel::Stable);
68    }
69    #[cfg(target_os = "macos")]
70    {
71        return get_browser_path_on_macos(browser_kind, BrowserChannel::Stable);
72    }
73    None
74}
75
76pub fn get_browser_dev_path(browser_kind: BrowserKind) -> Option<PathBuf> {
77    #[cfg(target_os = "linux")]
78    {
79        return get_browser_path_on_linux(browser_kind, BrowserChannel::Dev);
80    }
81    #[cfg(target_os = "windows")]
82    {
83        return get_browser_path_on_windows(browser_kind, BrowserChannel::Dev);
84    }
85    #[cfg(target_os = "macos")]
86    {
87        return get_browser_path_on_macos(browser_kind, BrowserChannel::Dev);
88    }
89    None
90}
91
92pub fn get_browser_beta_path(browser_kind: BrowserKind) -> Option<PathBuf> {
93    #[cfg(target_os = "linux")]
94    {
95        return get_browser_path_on_linux(browser_kind, BrowserChannel::Beta);
96    }
97    #[cfg(target_os = "windows")]
98    {
99        return get_browser_path_on_windows(browser_kind, BrowserChannel::Beta);
100    }
101    #[cfg(target_os = "macos")]
102    {
103        return get_browser_path_on_macos(browser_kind, BrowserChannel::Beta);
104    }
105    None
106}
107
108pub fn get_browser_canary_path(browser_kind: BrowserKind) -> Option<PathBuf> {
109    #[cfg(target_os = "linux")]
110    {
111        return get_browser_path_on_linux(browser_kind, BrowserChannel::Canary);
112    }
113    #[cfg(target_os = "windows")]
114    {
115        return get_browser_path_on_windows(browser_kind, BrowserChannel::Canary);
116    }
117    #[cfg(target_os = "macos")]
118    {
119        return get_browser_path_on_macos(browser_kind, BrowserChannel::Canary);
120    }
121    None
122}
123
124pub fn get_any_browser_latest(browser_kind: BrowserKind) -> Option<PathBuf> {
125    if let Some(path) = get_browser_canary_path(browser_kind) {
126        return Some(path);
127    }
128    if let Some(path) = get_browser_dev_path(browser_kind) {
129        return Some(path);
130    }
131    if let Some(path) = get_browser_beta_path(browser_kind) {
132        return Some(path);
133    }
134    if let Some(path) = get_browser_path(browser_kind) {
135        return Some(path);
136    }
137    None
138}
139
140pub fn get_any_browser_stable(browser_kind: BrowserKind) -> Option<PathBuf> {
141    if let Some(path) = get_browser_path(browser_kind) {
142        return Some(path);
143    }
144    if let Some(path) = get_browser_beta_path(browser_kind) {
145        return Some(path);
146    }
147    if let Some(path) = get_browser_dev_path(browser_kind) {
148        return Some(path);
149    }
150    if let Some(path) = get_browser_canary_path(browser_kind) {
151        return Some(path);
152    }
153    None
154}