browser_paths/
browser_path.rs1use crate::browser_name::{BrowserChannel, BrowserKind, BrowserName, Platform};
2use std::path::PathBuf;
3use which::which;
4
5#[cfg(target_os = "linux")]
6fn get_browser_path_on_linux(
7 browser_kind: BrowserKind,
8 channel: BrowserChannel,
9) -> Option<PathBuf> {
10 let browser_name = BrowserName(Platform::Linux, browser_kind, channel);
11 which(browser_name.to_str()).ok()
12}
13
14#[cfg(target_os = "windows")]
15fn get_browser_path_on_windows(
16 browser_kind: BrowserKind,
17 channel: BrowserChannel,
18) -> Option<PathBuf> {
19 let browser_name = BrowserName(Platform::Windows, browser_kind, channel);
20 let suffix = match browser_kind {
21 BrowserKind::Chrome => {
22 format!("Google\\{}\\Application\\chrome.exe", browser_name.to_str())
23 }
24 BrowserKind::Edge => format!(
25 "Microsoft\\{}\\Application\\msedge.exe",
26 browser_name.to_str()
27 ),
28 };
29 [
30 std::env::var_os("LOCALAPPDATA").map(PathBuf::from),
31 std::env::var_os("PROGRAMFILES").map(PathBuf::from),
32 std::env::var_os("ProgramFiles(x86)").map(PathBuf::from),
33 ]
34 .into_iter()
35 .flatten()
36 .map(|prefix| prefix.join(&suffix))
37 .find(|path| path.exists())
38}
39
40#[cfg(target_os = "macos")]
41fn get_browser_path_on_macos(
42 browser_kind: BrowserKind,
43 channel: BrowserChannel,
44) -> Option<PathBuf> {
45 let browser_name = BrowserName(Platform::MacOS, browser_kind, channel);
46 let default_path = PathBuf::from(format!(
47 "/Applications/{}.app/Contents/MacOS/{}",
48 browser_name.to_str(),
49 browser_name.to_str()
50 ));
51 default_path.exists().then_some(default_path)
52}
53
54#[cfg(target_os = "linux")]
55fn get_browser_path_for_channel(
56 browser_kind: BrowserKind,
57 channel: BrowserChannel,
58) -> Option<PathBuf> {
59 get_browser_path_on_linux(browser_kind, channel)
60}
61
62#[cfg(target_os = "windows")]
63fn get_browser_path_for_channel(
64 browser_kind: BrowserKind,
65 channel: BrowserChannel,
66) -> Option<PathBuf> {
67 get_browser_path_on_windows(browser_kind, channel)
68}
69
70#[cfg(target_os = "macos")]
71fn get_browser_path_for_channel(
72 browser_kind: BrowserKind,
73 channel: BrowserChannel,
74) -> Option<PathBuf> {
75 get_browser_path_on_macos(browser_kind, channel)
76}
77
78#[cfg(not(any(target_os = "linux", target_os = "windows", target_os = "macos")))]
79fn get_browser_path_for_channel(
80 _browser_kind: BrowserKind,
81 _channel: BrowserChannel,
82) -> Option<PathBuf> {
83 None
84}
85
86pub fn get_browser_path(browser_kind: BrowserKind) -> Option<PathBuf> {
87 get_browser_path_for_channel(browser_kind, BrowserChannel::Stable)
88}
89
90pub fn get_browser_dev_path(browser_kind: BrowserKind) -> Option<PathBuf> {
91 get_browser_path_for_channel(browser_kind, BrowserChannel::Dev)
92}
93
94pub fn get_browser_beta_path(browser_kind: BrowserKind) -> Option<PathBuf> {
95 get_browser_path_for_channel(browser_kind, BrowserChannel::Beta)
96}
97
98pub fn get_browser_canary_path(browser_kind: BrowserKind) -> Option<PathBuf> {
99 get_browser_path_for_channel(browser_kind, BrowserChannel::Canary)
100}
101
102pub fn get_any_browser_latest(browser_kind: BrowserKind) -> Option<PathBuf> {
103 [
104 BrowserChannel::Canary,
105 BrowserChannel::Dev,
106 BrowserChannel::Beta,
107 BrowserChannel::Stable,
108 ]
109 .into_iter()
110 .find_map(|channel| get_browser_path_for_channel(browser_kind, channel))
111}
112
113pub fn get_any_browser_stable(browser_kind: BrowserKind) -> Option<PathBuf> {
114 [
115 BrowserChannel::Stable,
116 BrowserChannel::Beta,
117 BrowserChannel::Dev,
118 BrowserChannel::Canary,
119 ]
120 .into_iter()
121 .find_map(|channel| get_browser_path_for_channel(browser_kind, channel))
122}