diff --git a/apps/desktop/src-tauri/Cargo.toml b/apps/desktop/src-tauri/Cargo.toml index 3c591a4..f2dfd24 100644 --- a/apps/desktop/src-tauri/Cargo.toml +++ b/apps/desktop/src-tauri/Cargo.toml @@ -22,6 +22,7 @@ tauri = { version = "2", features = [] } tauri-plugin-opener = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" +crowser = "0.4.1" +dirs = "6.0.0" uuid = { version = "1", features = ["v4", "serde"] } - diff --git a/apps/desktop/src-tauri/src/browser_details.rs b/apps/desktop/src-tauri/src/browser_details.rs new file mode 100644 index 0000000..af26107 --- /dev/null +++ b/apps/desktop/src-tauri/src/browser_details.rs @@ -0,0 +1,141 @@ +use crowser::{browser}; +use std::{fs, path::PathBuf, io::{self, Read}}; +use dirs::{config_dir, data_local_dir}; +use serde_json::Value; + +pub enum Browsers { + Chrome, + Edge, + Brave, + FireFox, + Safari +} + +pub fn get_browsers() -> Vec { + let browser_vector = browser::get_all_existing_browsers(); + let browser_names: Vec = browser_vector.iter().map(|s| s.name.to_owned()).collect(); + + return browser_names; +} + +pub fn get_chrome_based_profiles(os_paths: Vec<&str>) -> Result, Box> { + + let base_dir = if cfg!(target_os = "windows") || cfg!(target_os = "macos") { + data_local_dir() + } else { + config_dir() + }; + + if let Some(mut path) = base_dir { + + #[cfg(target_os = "windows")] + path.push(os_paths[0]); + + #[cfg(target_os = "macos")] + path.push(os_paths[1]); + + #[cfg(target_os = "linux")] + path.push(os_paths[2]); + + if path.exists() { + + let mut file = fs::File::open(path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + + let json_value: Value = serde_json::from_str(&contents)?; + + let info_cache = json_value + .get("profile") + .and_then(|p| p.get("info_cache")) + .and_then(|ic| ic.as_object()) + .ok_or_else(|| {Box::new(io::Error::new(io::ErrorKind::InvalidData, "Could not find 'profile' or 'info_cache' in JSON.")) as Box})?; + + let mut profile_names: Vec = Vec::new(); + + for (_profile_key, profile_data) in info_cache.iter() { + if let Some(name_value) = profile_data.get("gaia_name") { + if let Some(name_str) = name_value.as_str() { + profile_names.push(name_str.to_owned()); + } + } + } + + return Ok(profile_names); + + } + } + + Ok(Vec::new()) +} + +pub fn get_chrome_profiles(kind: Browsers) -> Result, Box> { + + let paths: Vec<&str> = match kind { + Browsers::Chrome => vec![ + "Google\\Chrome\\User Data\\Local State", + "Google/Chrome/Local State", + "google-chrome/Local State", + ], + Browsers::Edge => vec![ + "Microsoft\\Edge\\User Data\\Local State", + "Microsoft/Edge/Local State", + "microsoft-edge/Local State", + ], + Browsers::Brave => vec![ + "BraveSoftware\\Brave-Browser\\User Data\\Local State", + "BraveSoftware/Brave-Browser/Local State", + "brave/Local State", + ], + _ => return Ok(Vec::new()), + }; + + return get_chrome_based_profiles(paths); +} + +pub fn get_firefox_profiles() -> Result, Box> { + + let base_dir = if cfg!(target_os = "windows") || cfg!(target_os = "macos") { + data_local_dir() + } else { + dirs::home_dir() + }; + + if let Some(mut path) = base_dir { + + #[cfg(target_os = "windows")] + path.push("Mozilla\\Firefox\\Profiles"); + + #[cfg(target_os = "macos")] + path.push("Firefox/Profiles"); + + #[cfg(target_os = "linux")] + path.push("~/.mozilla/firefox"); + + if path.exists() { + match fs::read_dir(path) { + Ok(entries) => { + let profile_names: Vec = entries + .filter_map(Result::ok) + .filter_map(|entry| { + match entry.file_type() { + Ok(file_type) if file_type.is_dir() => { + Some(entry.file_name().to_string_lossy().into_owned()) + } + _ => None, + } + }) + .collect(); + return Ok(profile_names); + } + Err(e) => { + eprintln!("Error reading directory: {}", e); + return Ok(Vec::new()); + } + } + } + } + + return Ok(Vec::new()); + +} \ No newline at end of file diff --git a/apps/desktop/src-tauri/src/commands.rs b/apps/desktop/src-tauri/src/commands.rs new file mode 100644 index 0000000..fa9ae7c --- /dev/null +++ b/apps/desktop/src-tauri/src/commands.rs @@ -0,0 +1,4 @@ +#[tauri::command] +pub fn get_available_browser() { + get_browsers(); +} \ No newline at end of file