Skip to main content

browser_control/detect/
linux.rs

1//! Linux browser detection.
2
3use super::{Installed, Kind, Probe};
4use std::path::PathBuf;
5
6enum Cand<'a> {
7    Which(&'a str),
8    Abs(&'a str),
9}
10
11fn candidates_for(kind: Kind) -> &'static [Cand<'static>] {
12    match kind {
13        Kind::Chrome => &[
14            Cand::Which("google-chrome"),
15            Cand::Which("google-chrome-stable"),
16            Cand::Abs("/usr/bin/google-chrome"),
17            Cand::Abs("/opt/google/chrome/chrome"),
18        ],
19        Kind::Edge => &[
20            Cand::Which("microsoft-edge"),
21            Cand::Which("microsoft-edge-stable"),
22            Cand::Abs("/usr/bin/microsoft-edge"),
23        ],
24        Kind::Chromium => &[
25            Cand::Which("chromium"),
26            Cand::Which("chromium-browser"),
27            Cand::Abs("/usr/bin/chromium"),
28            Cand::Abs("/snap/bin/chromium"),
29        ],
30        Kind::Brave => &[
31            Cand::Which("brave-browser"),
32            Cand::Which("brave"),
33            Cand::Abs("/usr/bin/brave-browser"),
34        ],
35        Kind::Firefox => &[
36            Cand::Which("firefox"),
37            Cand::Abs("/usr/bin/firefox"),
38            Cand::Abs("/snap/bin/firefox"),
39        ],
40    }
41}
42
43fn find_one<P: Probe>(probe: &P, kind: Kind) -> Option<PathBuf> {
44    for c in candidates_for(kind) {
45        match c {
46            Cand::Which(name) => {
47                if let Some(p) = probe.which(name) {
48                    return Some(p);
49                }
50            }
51            Cand::Abs(path) => {
52                let p = PathBuf::from(path);
53                if probe.exists(&p) {
54                    return Some(p);
55                }
56            }
57        }
58    }
59    None
60}
61
62pub(super) fn detect<P: Probe>(probe: &P) -> Vec<Installed> {
63    let mut out = Vec::new();
64    for kind in [
65        Kind::Chrome,
66        Kind::Edge,
67        Kind::Chromium,
68        Kind::Brave,
69        Kind::Firefox,
70    ] {
71        if let Some(exe) = find_one(probe, kind) {
72            let version = probe
73                .run_version(&exe)
74                .unwrap_or_else(|| "unknown".to_string());
75            out.push(Installed {
76                kind,
77                executable: exe,
78                version,
79                engine: kind.engine(),
80            });
81        }
82    }
83    out
84}