support custom userdata
This commit is contained in:
@@ -1,30 +1,47 @@
|
||||
use std::{path::PathBuf, process::Command};
|
||||
|
||||
use crate::{
|
||||
browsers::{browser_definition_by_id, resolve_browser_executable},
|
||||
models::ScanResponse,
|
||||
config_store,
|
||||
models::{BrowserConfigListResponse, CreateCustomBrowserConfigInput, ScanResponse},
|
||||
scanner,
|
||||
utils::local_app_data_dir,
|
||||
};
|
||||
use tauri::AppHandle;
|
||||
|
||||
#[tauri::command]
|
||||
pub fn scan_browsers() -> Result<ScanResponse, String> {
|
||||
scanner::scan_browsers()
|
||||
pub fn scan_browsers(app: AppHandle) -> Result<ScanResponse, String> {
|
||||
scanner::scan_browsers(&app)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn open_browser_profile(browser_id: String, profile_id: String) -> Result<(), String> {
|
||||
let definition = browser_definition_by_id(&browser_id)
|
||||
.ok_or_else(|| format!("Unsupported browser: {browser_id}"))?;
|
||||
let executable_path = resolve_browser_executable(&browser_id)
|
||||
.ok_or_else(|| format!("Unable to locate executable for browser: {browser_id}"))?;
|
||||
let local_app_data = local_app_data_dir().ok_or_else(|| {
|
||||
"Unable to resolve the LOCALAPPDATA directory for the current user.".to_string()
|
||||
})?;
|
||||
let user_data_dir = definition
|
||||
.local_app_data_segments
|
||||
.iter()
|
||||
.fold(local_app_data, |path, segment| path.join(segment));
|
||||
pub fn list_browser_configs(app: AppHandle) -> Result<BrowserConfigListResponse, String> {
|
||||
config_store::load_browser_config_list(&app)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn create_custom_browser_config(
|
||||
app: AppHandle,
|
||||
input: CreateCustomBrowserConfigInput,
|
||||
) -> Result<BrowserConfigListResponse, String> {
|
||||
config_store::create_custom_browser_config(&app, input)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn delete_custom_browser_config(
|
||||
app: AppHandle,
|
||||
config_id: String,
|
||||
) -> Result<BrowserConfigListResponse, String> {
|
||||
config_store::delete_custom_browser_config(&app, &config_id)
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn open_browser_profile(
|
||||
app: AppHandle,
|
||||
browser_id: String,
|
||||
profile_id: String,
|
||||
) -> Result<(), String> {
|
||||
let config = config_store::find_browser_config(&app, &browser_id)?;
|
||||
let executable_path = PathBuf::from(&config.executable_path);
|
||||
let user_data_dir = PathBuf::from(&config.user_data_path);
|
||||
let profile_directory = user_data_dir.join(&profile_id);
|
||||
|
||||
if !user_data_dir.is_dir() {
|
||||
|
||||
188
src-tauri/src/config_store.rs
Normal file
188
src-tauri/src/config_store.rs
Normal file
@@ -0,0 +1,188 @@
|
||||
use std::{
|
||||
fs,
|
||||
path::PathBuf,
|
||||
time::{SystemTime, UNIX_EPOCH},
|
||||
};
|
||||
|
||||
use tauri::{AppHandle, Manager};
|
||||
|
||||
use crate::{
|
||||
browsers::{browser_definitions, resolve_browser_executable},
|
||||
models::{
|
||||
BrowserConfigEntry, BrowserConfigListResponse, BrowserConfigSource,
|
||||
CreateCustomBrowserConfigInput, CustomBrowserConfigRecord, StoredBrowserConfigs,
|
||||
},
|
||||
utils::local_app_data_dir,
|
||||
};
|
||||
|
||||
const CONFIG_FILE_NAME: &str = "browser-configs.json";
|
||||
|
||||
pub fn load_browser_config_list(app: &AppHandle) -> Result<BrowserConfigListResponse, String> {
|
||||
Ok(BrowserConfigListResponse {
|
||||
configs: resolve_browser_configs(app)?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn resolve_browser_configs(app: &AppHandle) -> Result<Vec<BrowserConfigEntry>, String> {
|
||||
let mut configs = default_browser_configs()?;
|
||||
let stored = load_stored_configs(app)?;
|
||||
|
||||
configs.extend(
|
||||
stored
|
||||
.custom_configs
|
||||
.into_iter()
|
||||
.map(|config| BrowserConfigEntry {
|
||||
id: config.id,
|
||||
source: BrowserConfigSource::Custom,
|
||||
browser_family_id: None,
|
||||
name: config.name,
|
||||
executable_path: config.executable_path,
|
||||
user_data_path: config.user_data_path,
|
||||
deletable: true,
|
||||
}),
|
||||
);
|
||||
|
||||
Ok(configs)
|
||||
}
|
||||
|
||||
pub fn create_custom_browser_config(
|
||||
app: &AppHandle,
|
||||
input: CreateCustomBrowserConfigInput,
|
||||
) -> Result<BrowserConfigListResponse, String> {
|
||||
let name = input.name.trim();
|
||||
let executable_path = input.executable_path.trim();
|
||||
let user_data_path = input.user_data_path.trim();
|
||||
|
||||
if name.is_empty() {
|
||||
return Err("Name is required.".to_string());
|
||||
}
|
||||
if executable_path.is_empty() {
|
||||
return Err("Executable path is required.".to_string());
|
||||
}
|
||||
if user_data_path.is_empty() {
|
||||
return Err("User data path is required.".to_string());
|
||||
}
|
||||
|
||||
let mut stored = load_stored_configs(app)?;
|
||||
stored.custom_configs.push(CustomBrowserConfigRecord {
|
||||
id: generate_custom_config_id(),
|
||||
name: name.to_string(),
|
||||
executable_path: executable_path.to_string(),
|
||||
user_data_path: user_data_path.to_string(),
|
||||
});
|
||||
|
||||
save_stored_configs(app, &stored)?;
|
||||
load_browser_config_list(app)
|
||||
}
|
||||
|
||||
pub fn delete_custom_browser_config(
|
||||
app: &AppHandle,
|
||||
config_id: &str,
|
||||
) -> Result<BrowserConfigListResponse, String> {
|
||||
let mut stored = load_stored_configs(app)?;
|
||||
let original_len = stored.custom_configs.len();
|
||||
stored
|
||||
.custom_configs
|
||||
.retain(|config| config.id != config_id);
|
||||
|
||||
if stored.custom_configs.len() == original_len {
|
||||
return Err(format!("Custom browser config not found: {config_id}"));
|
||||
}
|
||||
|
||||
save_stored_configs(app, &stored)?;
|
||||
load_browser_config_list(app)
|
||||
}
|
||||
|
||||
pub fn find_browser_config(app: &AppHandle, config_id: &str) -> Result<BrowserConfigEntry, String> {
|
||||
resolve_browser_configs(app)?
|
||||
.into_iter()
|
||||
.find(|config| config.id == config_id)
|
||||
.ok_or_else(|| format!("Browser config not found: {config_id}"))
|
||||
}
|
||||
|
||||
fn default_browser_configs() -> Result<Vec<BrowserConfigEntry>, String> {
|
||||
let local_app_data = local_app_data_dir().ok_or_else(|| {
|
||||
"Unable to resolve the LOCALAPPDATA directory for the current user.".to_string()
|
||||
})?;
|
||||
|
||||
Ok(browser_definitions()
|
||||
.into_iter()
|
||||
.map(|definition| {
|
||||
let user_data_path = definition
|
||||
.local_app_data_segments
|
||||
.iter()
|
||||
.fold(local_app_data.clone(), |path, segment| path.join(segment));
|
||||
|
||||
BrowserConfigEntry {
|
||||
id: definition.id.to_string(),
|
||||
source: BrowserConfigSource::Default,
|
||||
browser_family_id: Some(definition.id.to_string()),
|
||||
name: definition.name.to_string(),
|
||||
executable_path: resolve_browser_executable(definition.id)
|
||||
.map(|path| path.display().to_string())
|
||||
.unwrap_or_default(),
|
||||
user_data_path: user_data_path.display().to_string(),
|
||||
deletable: false,
|
||||
}
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
fn load_stored_configs(app: &AppHandle) -> Result<StoredBrowserConfigs, String> {
|
||||
let path = config_file_path(app)?;
|
||||
if !path.is_file() {
|
||||
return Ok(StoredBrowserConfigs {
|
||||
custom_configs: Vec::new(),
|
||||
});
|
||||
}
|
||||
|
||||
let content = fs::read_to_string(&path).map_err(|error| {
|
||||
format!(
|
||||
"Failed to read browser config file {}: {error}",
|
||||
path.display()
|
||||
)
|
||||
})?;
|
||||
serde_json::from_str(&content).map_err(|error| {
|
||||
format!(
|
||||
"Failed to parse browser config file {}: {error}",
|
||||
path.display()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn save_stored_configs(app: &AppHandle, stored: &StoredBrowserConfigs) -> Result<(), String> {
|
||||
let path = config_file_path(app)?;
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|error| {
|
||||
format!(
|
||||
"Failed to create browser config directory {}: {error}",
|
||||
parent.display()
|
||||
)
|
||||
})?;
|
||||
}
|
||||
|
||||
let content = serde_json::to_string_pretty(stored)
|
||||
.map_err(|error| format!("Failed to serialize browser configs: {error}"))?;
|
||||
fs::write(&path, content).map_err(|error| {
|
||||
format!(
|
||||
"Failed to write browser config file {}: {error}",
|
||||
path.display()
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn config_file_path(app: &AppHandle) -> Result<PathBuf, String> {
|
||||
let app_data_dir = app
|
||||
.path()
|
||||
.app_data_dir()
|
||||
.map_err(|error| format!("Failed to resolve app data directory: {error}"))?;
|
||||
Ok(app_data_dir.join(CONFIG_FILE_NAME))
|
||||
}
|
||||
|
||||
fn generate_custom_config_id() -> String {
|
||||
let timestamp = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|duration| duration.as_millis())
|
||||
.unwrap_or(0);
|
||||
format!("custom-{timestamp}")
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
mod browsers;
|
||||
mod commands;
|
||||
mod config_store;
|
||||
mod models;
|
||||
mod scanner;
|
||||
mod utils;
|
||||
@@ -10,6 +11,9 @@ pub fn run() {
|
||||
.plugin(tauri_plugin_opener::init())
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
commands::scan_browsers,
|
||||
commands::list_browser_configs,
|
||||
commands::create_custom_browser_config,
|
||||
commands::delete_custom_browser_config,
|
||||
commands::open_browser_profile
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -12,6 +12,7 @@ pub struct ScanResponse {
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BrowserView {
|
||||
pub browser_id: String,
|
||||
pub browser_family_id: Option<String>,
|
||||
pub browser_name: String,
|
||||
pub data_root: String,
|
||||
pub profiles: Vec<ProfileSummary>,
|
||||
@@ -57,6 +58,54 @@ pub struct BookmarkSummary {
|
||||
pub profile_ids: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BrowserConfigListResponse {
|
||||
pub configs: Vec<BrowserConfigEntry>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct BrowserConfigEntry {
|
||||
pub id: String,
|
||||
pub source: BrowserConfigSource,
|
||||
pub browser_family_id: Option<String>,
|
||||
pub name: String,
|
||||
pub executable_path: String,
|
||||
pub user_data_path: String,
|
||||
pub deletable: bool,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum BrowserConfigSource {
|
||||
Default,
|
||||
Custom,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CreateCustomBrowserConfigInput {
|
||||
pub name: String,
|
||||
pub executable_path: String,
|
||||
pub user_data_path: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct StoredBrowserConfigs {
|
||||
pub custom_configs: Vec<CustomBrowserConfigRecord>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct CustomBrowserConfigRecord {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub executable_path: String,
|
||||
pub user_data_path: String,
|
||||
}
|
||||
|
||||
pub struct BrowserDefinition {
|
||||
pub id: &'static str,
|
||||
pub name: &'static str,
|
||||
|
||||
@@ -1,43 +1,32 @@
|
||||
use std::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
fs,
|
||||
path::Path,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use serde_json::Value;
|
||||
use tauri::AppHandle;
|
||||
|
||||
use crate::{
|
||||
browsers::browser_definitions,
|
||||
config_store,
|
||||
models::{
|
||||
BookmarkSummary, BrowserDefinition, BrowserStats, BrowserView, ExtensionSummary,
|
||||
BookmarkSummary, BrowserConfigEntry, BrowserStats, BrowserView, ExtensionSummary,
|
||||
ProfileSummary, ScanResponse, TempBookmark, TempExtension,
|
||||
},
|
||||
utils::{
|
||||
first_non_empty, load_image_as_data_url, local_app_data_dir, pick_latest_subdirectory,
|
||||
read_json_file,
|
||||
},
|
||||
utils::{first_non_empty, load_image_as_data_url, pick_latest_subdirectory, read_json_file},
|
||||
};
|
||||
|
||||
pub fn scan_browsers() -> Result<ScanResponse, String> {
|
||||
let local_app_data = local_app_data_dir().ok_or_else(|| {
|
||||
"Unable to resolve the LOCALAPPDATA directory for the current user.".to_string()
|
||||
})?;
|
||||
|
||||
let browsers = browser_definitions()
|
||||
pub fn scan_browsers(app: &AppHandle) -> Result<ScanResponse, String> {
|
||||
let browsers = config_store::resolve_browser_configs(app)?
|
||||
.into_iter()
|
||||
.filter_map(|definition| scan_browser(&local_app_data, definition))
|
||||
.filter_map(scan_browser)
|
||||
.collect();
|
||||
|
||||
Ok(ScanResponse { browsers })
|
||||
}
|
||||
|
||||
fn scan_browser(local_app_data: &Path, definition: BrowserDefinition) -> Option<BrowserView> {
|
||||
let root = definition
|
||||
.local_app_data_segments
|
||||
.iter()
|
||||
.fold(local_app_data.to_path_buf(), |path, segment| {
|
||||
path.join(segment)
|
||||
});
|
||||
fn scan_browser(config: BrowserConfigEntry) -> Option<BrowserView> {
|
||||
let root = PathBuf::from(&config.user_data_path);
|
||||
|
||||
if !root.is_dir() {
|
||||
return None;
|
||||
@@ -99,8 +88,9 @@ fn scan_browser(local_app_data: &Path, definition: BrowserDefinition) -> Option<
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
Some(BrowserView {
|
||||
browser_id: definition.id.to_string(),
|
||||
browser_name: definition.name.to_string(),
|
||||
browser_id: config.id,
|
||||
browser_family_id: config.browser_family_id,
|
||||
browser_name: config.name,
|
||||
data_root: root.display().to_string(),
|
||||
stats: BrowserStats {
|
||||
profile_count: profiles.len(),
|
||||
|
||||
Reference in New Issue
Block a user