decrypt_cookies/browser/
firefox.rs

1use std::path::PathBuf;
2
3use super::CACHE_PATH;
4
5pub trait FirefoxPath {
6    /// Suffix for data path
7    const BASE: &'static str;
8    /// Name for [`std::fmt::Display`]
9    const NAME: &'static str;
10    /// Suffix for cookies data path (sqlite3 database)
11    const COOKIES: &str = "cookies.sqlite";
12    /// Suffix for login data path (json)
13    const LOGIN_DATA: &str = "logins.json";
14    /// Suffix for decryption key path (sqlite3 database)
15    const KEY: &str = "key4.db";
16
17    /// Decryption key path (sqlite3 database)
18    fn key(mut base: PathBuf) -> PathBuf {
19        push_exact!(base, Self::KEY);
20
21        base
22    }
23    /// Copy the decryption key file to a location to avoid conflicts with the browser over access to it.
24    fn key_temp() -> Option<PathBuf> {
25        push_temp!(cache, Self::KEY);
26
27        cache.into()
28    }
29
30    /// Cookies path (sqlite3 database)
31    fn cookies(mut base: PathBuf) -> PathBuf {
32        push_exact!(base, Self::COOKIES);
33
34        base
35    }
36    /// Copy the cookies file to a location to avoid conflicts with the browser over access to it.
37    fn cookies_temp() -> Option<PathBuf> {
38        push_temp!(cache, Self::COOKIES);
39
40        cache.into()
41    }
42
43    /// Login data path (json)
44    fn login_data(mut base: PathBuf) -> PathBuf {
45        push_exact!(base, Self::LOGIN_DATA);
46
47        base
48    }
49    /// Copy the login data file to a location to avoid conflicts with the browser over access to it.
50    fn login_data_temp() -> Option<PathBuf> {
51        push_temp!(cache, Self::LOGIN_DATA);
52
53        cache.into()
54    }
55}
56
57/// Register a Firefox based browser info
58///
59/// It accept
60/// - `platform`
61/// - `browser`: Generate a struct
62/// - `base: <path>`: A browser all data location relative to home dir.
63/// - `cookies: <path>`, `login_data: <path>`, `key: <path>`: Relative to profile dir. (optional)
64///
65/// # Example:
66///
67/// ```rust, no_run
68/// firefox!(
69///     "linux",
70///     Firefox,
71///     base: ".mozilla/firefox",
72///     cookies: "cookies.sqlite",
73///     login_data: "logins.json",
74///     key: "key4.db"
75/// );
76/// // or omit use default value
77/// firefox!("linux", Firefox, base: ".mozilla/firefox");
78/// firefox!("macos", Firefox, base: "Library/Application Support/Firefox");
79/// firefox!("windows", Firefox, base: r"AppData\Roaming\Mozilla\Firefox");
80/// ```
81#[macro_export]
82macro_rules! firefox {
83    (
84        $platform:literal,
85        $browser:ident,
86        base: $base:literal
87        $(, cookies: $cookies:literal)?
88        $(, login_data: $login_data:literal)?
89        $(, key = $key:literal)?
90    ) => {
91        #[cfg(target_os = $platform)]
92        #[derive(Clone, Copy)]
93        #[derive(Debug)]
94        #[derive(Default)]
95        #[derive(PartialEq, Eq, PartialOrd, Ord)]
96        #[expect(clippy::exhaustive_structs, reason = "unit struct")]
97        pub struct $browser;
98
99        #[cfg(target_os = $platform)]
100        impl FirefoxPath for $browser {
101            const BASE: &'static str = $base;
102            const NAME: &'static str = stringify!($browser);
103            $(const COOKIES: &str = $cookies;)?
104            $(const LOGIN_DATA: &str = $login_data;)?
105            $(const KEY: &str = $key;)?
106        }
107
108        #[cfg(target_os = $platform)]
109        impl std::fmt::Display for $browser {
110            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111                f.write_str(Self::NAME)
112            }
113        }
114    };
115}
116
117firefox!("linux", Firefox  , base: ".mozilla/firefox");
118firefox!("linux", Librewolf, base: ".librewolf"      );
119firefox!("linux", Floorp   , base: ".floorp"         );
120firefox!("linux", Zen      , base: ".zen"            );
121
122firefox!("macos", Firefox  , base: "Library/Application Support/Firefox"  );
123firefox!("macos", Librewolf, base: "Library/Application Support/librewolf");
124firefox!("macos", Floorp   , base: "Library/Application Support/Floorp"   );
125firefox!("macos", Zen      , base: "Library/Application Support/zen"      );
126
127firefox!("windows", Firefox  , base: r"AppData\Roaming\Mozilla\Firefox");
128firefox!("windows", Librewolf, base: r"AppData\Roaming\librewolf"      );
129firefox!("windows", Floorp   , base: r"AppData\Roaming\Floorp"         );
130firefox!("windows", Zen      , base: r"AppData\Roaming\zen"            );