1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-11 22:13:32 +00:00

refactor rust code

• adding/removing `BroswerConfig` entries in platform specific `SUPPORTED_BROWSERS` is now the only step needed to enable/disable chromium importer for these browsers
• change visibility of structs defined in `chromium.rs` to be pub and add this module to crate root
• add `util.rs` to crate root
• determine platform and re-export `SUPPORTED_BROWSERS` at crate root
This commit is contained in:
John Harrington
2025-09-19 14:19:29 -07:00
parent 49d52863a4
commit 5b08bf9982
6 changed files with 64 additions and 61 deletions

View File

@@ -104,10 +104,10 @@ pub async fn import_logins(
// Private
//
#[derive(Debug)]
struct BrowserConfig {
name: &'static str,
data_dir: &'static str,
#[derive(Debug, Clone, Copy)]
pub struct BrowserConfig {
pub name: &'static str,
pub data_dir: &'static str,
}
static SUPPORTED_BROWSER_MAP: LazyLock<
@@ -132,12 +132,12 @@ fn get_browser_data_dir(config: &BrowserConfig) -> Result<PathBuf> {
//
#[async_trait]
trait CryptoService: Send {
pub trait CryptoService: Send {
async fn decrypt_to_string(&mut self, encrypted: &[u8]) -> Result<String>;
}
#[derive(serde::Deserialize, Clone)]
struct LocalState {
pub struct LocalState {
profile: AllProfiles,
#[allow(dead_code)]
os_crypt: Option<OsCrypt>,

View File

@@ -1,2 +1,18 @@
#[cfg(target_os = "windows")]
pub mod windows;
#[cfg(target_os = "windows")]
pub use crate::windows::SUPPORTED_BROWSERS as PLATFORM_SUPPORTED_BROWSERS;
#[cfg(target_os = "macos")]
pub mod macos;
#[cfg(target_os = "macos")]
pub use crate::macos::SUPPORTED_BROWSERS as PLATFORM_SUPPORTED_BROWSERS;
#[cfg(target_os = "linux")]
pub mod linux;
#[cfg(target_os = "linux")]
pub use crate::linux::SUPPORTED_BROWSERS as PLATFORM_SUPPORTED_BROWSERS;
pub mod chromium;
pub mod metadata;
pub mod util;

View File

@@ -6,7 +6,7 @@ use oo7::XDG_SCHEMA_ATTRIBUTE;
use crate::chromium::{BrowserConfig, CryptoService, LocalState};
mod util;
use crate::util;
//
// Public API

View File

@@ -3,8 +3,7 @@ use async_trait::async_trait;
use security_framework::passwords::get_generic_password;
use crate::chromium::{BrowserConfig, CryptoService, LocalState};
mod util;
use crate::util;
//
// Public API

View File

@@ -1,62 +1,49 @@
use serde::Serialize;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use crate::PLATFORM_SUPPORTED_BROWSERS;
#[derive(Serialize)]
/// Mechanisms that load data into the importer
pub struct ImporterMetadata {
/// Identifies the importer
pub id: String,
/// Describes the strategies used to obtain imported data
pub loaders: Vec<&'static str>,
/// Identifies the instructions for the importer
pub instructions: &'static str,
}
#[cfg(target_os = "windows")]
fn chrome_loaders() -> Vec<&'static str> {
vec!["file"]
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn chrome_loaders() -> Vec<&'static str> {
vec!["file", "chromium"]
}
#[cfg(target_os = "windows")]
fn brave_loaders() -> Vec<&'static str> {
vec!["file"]
}
#[cfg(any(target_os = "macos", target_os = "linux"))]
fn brave_loaders() -> Vec<&'static str> {
vec!["file", "chromium"]
}
/// Returns a map of supported importers based on the current platform.
///
/// Only browsers listed in PLATFORM_SUPPORTED_BROWSERS will have the "chromium" loader.
/// All importers will have the "file" loader.
pub fn get_supported_importers() -> HashMap<String, ImporterMetadata> {
let mut map = HashMap::new();
// force chrome to use target_os dependent loaders
map.insert(
"chromecsv".to_string(),
ImporterMetadata {
id: "chromecsv".to_string(),
loaders: chrome_loaders(),
instructions: "chromium",
},
);
// force brave to use target_os dependent loaders
map.insert(
"bravecsv".to_string(),
ImporterMetadata {
id: "bravecsv".to_string(),
loaders: brave_loaders(),
instructions: "chromium",
},
);
const IMPORTERS: [(&str, &str); 6] = [
("chromecsv", "Chrome"),
("chromiumcsv", "Chromium"),
("bravecsv", "Brave"),
("operacsv", "Opera"),
("vivaldicsv", "Vivaldi"),
("edgecsv", "Microsoft Edge"),
];
let supported: HashSet<&'static str> =
PLATFORM_SUPPORTED_BROWSERS.iter().map(|b| b.name).collect();
for (id, browser_name) in IMPORTERS {
let mut loaders: Vec<&'static str> = vec!["file"];
if supported.contains(browser_name) {
loaders.push("chromium");
}
// all other chromium based browsers support file & chromium loaders on all platforms
for id in ["operacsv", "vivaldicsv", "edgecsv"] {
map.insert(
id.to_string(),
ImporterMetadata {
id: id.to_string(),
loaders: vec!["file", "chromium"],
loaders,
instructions: "chromium",
},
);

View File

@@ -10,17 +10,18 @@ use windows::Win32::Foundation::{LocalFree, HLOCAL};
use crate::chromium::{BrowserConfig, CryptoService, LocalState};
#[allow(dead_code)]
mod util;
use crate::util;
//
// Public API
//
pub const SUPPORTED_BROWSERS: [BrowserConfig; 6] = [
BrowserConfig {
name: "Chrome",
data_dir: "AppData/Local/Google/Chrome/User Data",
},
// IMPORTANT adjust array size when enabling / disabling chromium importers here
pub const SUPPORTED_BROWSERS: [BrowserConfig; 4] = [
// BrowserConfig {
// name: "Chrome",
// data_dir: "AppData/Local/Google/Chrome/User Data",
// },
BrowserConfig {
name: "Chromium",
data_dir: "AppData/Local/Chromium/User Data",
@@ -29,10 +30,10 @@ pub const SUPPORTED_BROWSERS: [BrowserConfig; 6] = [
name: "Microsoft Edge",
data_dir: "AppData/Local/Microsoft/Edge/User Data",
},
BrowserConfig {
name: "Brave",
data_dir: "AppData/Local/BraveSoftware/Brave-Browser/User Data",
},
// BrowserConfig {
// name: "Brave",
// data_dir: "AppData/Local/BraveSoftware/Brave-Browser/User Data",
// },
BrowserConfig {
name: "Opera",
data_dir: "AppData/Roaming/Opera Software/Opera Stable",