Everything in Rust on Servo 0.7 (trimmed: no WebXR, WebGPU, multiprocess): tabs and windows, address bar with suggestions, history, bookmarks and reading list, downloads with resume and external download managers, encrypted password vault with autofill, ad blocker (adblock-rust, 22 lists), VPN through Tor or a proxy and secure DNS via a local bridge proxy, picture in picture, Discord Rich Presence, media player and visualizer, spell check, site apps, user scripts and styles, import from other browsers, gamepads, Web Bluetooth, and every setting of the original Nacre in a plain egui interface. Not compiled yet; docs/HANDOFF.md has the first build checklist. Co-Authored-By: Claude Opus 5.5 <[email protected]>
210 lines
8.9 KiB
Rust
210 lines
8.9 KiB
Rust
//! Where Nacre keeps its files, and which profile is in use.
|
|
//!
|
|
//! Layout (Linux; other systems use their own base folders through `dirs`):
|
|
//! ~/.config/nacre-servo/profiles.json list of profiles, which one starts by default
|
|
//! ~/.config/nacre-servo/<profile>/settings.json settings of one profile
|
|
//! ~/.local/share/nacre-servo/<profile>/ history, bookmarks, vault, session, engine storage
|
|
//! ~/.cache/nacre-servo/<profile>/ filter lists, favicons, dictionaries
|
|
//!
|
|
//! `NACRE_HOME` puts all three under one folder (tests use a throwaway one). An incognito
|
|
//! process uses a temporary folder that is removed when it exits.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use crate::util;
|
|
|
|
pub const APP_DIR: &str = "nacre-servo";
|
|
pub const DEFAULT_PROFILE: &str = "Default";
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Paths {
|
|
pub profile: String,
|
|
pub incognito: bool,
|
|
/// Base folders shared by every profile.
|
|
pub config_root: PathBuf,
|
|
pub data_root: PathBuf,
|
|
pub cache_root: PathBuf,
|
|
/// Folders of the profile in use.
|
|
pub config: PathBuf,
|
|
pub data: PathBuf,
|
|
pub cache: PathBuf,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default, Serialize, Deserialize)]
|
|
pub struct ProfileList {
|
|
#[serde(default)]
|
|
pub profiles: Vec<String>,
|
|
#[serde(default)]
|
|
pub last_used: String,
|
|
}
|
|
|
|
fn roots() -> (PathBuf, PathBuf, PathBuf) {
|
|
if let Some(home) = std::env::var_os("NACRE_HOME").filter(|v| !v.is_empty()) {
|
|
let home = PathBuf::from(home);
|
|
return (home.join("config"), home.join("data"), home.join("cache"));
|
|
}
|
|
let fallback = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
|
|
let config = dirs::config_dir().unwrap_or_else(|| fallback.join(".config")).join(APP_DIR);
|
|
let data = dirs::data_dir().unwrap_or_else(|| fallback.join(".local/share")).join(APP_DIR);
|
|
let cache = dirs::cache_dir().unwrap_or_else(|| fallback.join(".cache")).join(APP_DIR);
|
|
(config, data, cache)
|
|
}
|
|
|
|
/// Profile names become folder names, so keep them simple.
|
|
pub fn clean_profile_name(name: &str) -> String {
|
|
let cleaned: String = name.trim().chars().filter(|c| c.is_alphanumeric() || matches!(c, ' ' | '-' | '_')).take(40).collect();
|
|
let cleaned = cleaned.trim().to_owned();
|
|
if cleaned.is_empty() { DEFAULT_PROFILE.to_owned() } else { cleaned }
|
|
}
|
|
|
|
impl Paths {
|
|
/// Folders for a normal run. `profile` of `None` means the profile used last time.
|
|
pub fn for_profile(profile: Option<&str>) -> Paths {
|
|
let (config_root, data_root, cache_root) = roots();
|
|
let mut list = Self::read_profile_list(&config_root);
|
|
let name = match profile {
|
|
Some(name) => clean_profile_name(name),
|
|
None if !list.last_used.is_empty() => clean_profile_name(&list.last_used),
|
|
None => DEFAULT_PROFILE.to_owned(),
|
|
};
|
|
if !list.profiles.contains(&name) {
|
|
list.profiles.push(name.clone());
|
|
}
|
|
list.last_used = name.clone();
|
|
let _ = util::save_json(&config_root.join("profiles.json"), &list);
|
|
let paths = Paths {
|
|
config: config_root.join(&name),
|
|
data: data_root.join(&name),
|
|
cache: cache_root.join(&name),
|
|
profile: name,
|
|
incognito: false,
|
|
config_root,
|
|
data_root,
|
|
cache_root,
|
|
};
|
|
paths.create();
|
|
paths
|
|
}
|
|
|
|
/// Folders for an incognito run: everything under one temporary folder. Settings are copied
|
|
/// from `from` so the window behaves like the user's browser, but nothing is written back.
|
|
pub fn incognito(from: &Paths) -> Paths {
|
|
let base = std::env::temp_dir().join(format!("nacre-incognito-{}", util::random_hex(8)));
|
|
let paths = Paths {
|
|
profile: from.profile.clone(),
|
|
incognito: true,
|
|
config_root: base.join("config"),
|
|
data_root: base.join("data"),
|
|
cache_root: from.cache_root.clone(),
|
|
config: base.join("config").join("p"),
|
|
data: base.join("data").join("p"),
|
|
// Filter lists and dictionaries are not private; share the real cache.
|
|
cache: from.cache.clone(),
|
|
};
|
|
paths.create();
|
|
#[cfg(unix)]
|
|
{
|
|
use std::os::unix::fs::PermissionsExt;
|
|
let _ = std::fs::set_permissions(&base, std::fs::Permissions::from_mode(0o700));
|
|
}
|
|
for name in ["settings.json", "shortcuts.json", "scripts.json"] {
|
|
let _ = std::fs::copy(from.config.join(name), paths.config.join(name));
|
|
}
|
|
// Ad-block allow list, permissions defaults and the like live in settings; bookmarks are
|
|
// shown read-only from a copy.
|
|
let _ = std::fs::copy(from.data.join("bookmarks.json"), paths.data.join("bookmarks.json"));
|
|
paths
|
|
}
|
|
|
|
fn create(&self) {
|
|
for dir in [&self.config, &self.data, &self.cache] {
|
|
if let Err(error) = std::fs::create_dir_all(dir) {
|
|
log::error!("cannot create {}: {error}", dir.display());
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Remove an incognito run's folder. Does nothing for a normal profile.
|
|
pub fn remove_incognito(&self) {
|
|
if !self.incognito {
|
|
return;
|
|
}
|
|
if let Some(base) = self.config_root.parent()
|
|
&& base.file_name().is_some_and(|n| n.to_string_lossy().starts_with("nacre-incognito-"))
|
|
{
|
|
let _ = std::fs::remove_dir_all(base);
|
|
}
|
|
}
|
|
|
|
fn read_profile_list(config_root: &Path) -> ProfileList {
|
|
let mut list: ProfileList = util::load_json(&config_root.join("profiles.json"));
|
|
if list.profiles.is_empty() {
|
|
list.profiles.push(DEFAULT_PROFILE.to_owned());
|
|
}
|
|
list
|
|
}
|
|
|
|
pub fn profile_list(&self) -> ProfileList {
|
|
Self::read_profile_list(&self.config_root)
|
|
}
|
|
|
|
pub fn add_profile(&self, name: &str) -> String {
|
|
let name = clean_profile_name(name);
|
|
let mut list = self.profile_list();
|
|
if !list.profiles.contains(&name) {
|
|
list.profiles.push(name.clone());
|
|
let _ = util::save_json(&self.config_root.join("profiles.json"), &list);
|
|
}
|
|
name
|
|
}
|
|
|
|
/// Delete a profile and its files. The profile in use and the last one left cannot be deleted.
|
|
pub fn delete_profile(&self, name: &str) -> Result<(), String> {
|
|
if name == self.profile {
|
|
return Err("This profile is in use. Switch to another one first.".to_owned());
|
|
}
|
|
let mut list = self.profile_list();
|
|
if list.profiles.len() <= 1 {
|
|
return Err("The last profile cannot be deleted.".to_owned());
|
|
}
|
|
let name = clean_profile_name(name);
|
|
list.profiles.retain(|p| p != &name);
|
|
util::save_json(&self.config_root.join("profiles.json"), &list).map_err(|e| e.to_string())?;
|
|
for root in [&self.config_root, &self.data_root, &self.cache_root] {
|
|
let dir = root.join(&name);
|
|
if dir.is_dir() {
|
|
let _ = std::fs::remove_dir_all(dir);
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
// Files of the profile in use.
|
|
pub fn settings(&self) -> PathBuf { self.config.join("settings.json") }
|
|
pub fn shortcuts(&self) -> PathBuf { self.config.join("shortcuts.json") }
|
|
pub fn scripts(&self) -> PathBuf { self.config.join("scripts.json") }
|
|
pub fn scripts_dir(&self) -> PathBuf { self.config.join("scripts") }
|
|
pub fn history(&self) -> PathBuf { self.data.join("history.json") }
|
|
pub fn bookmarks(&self) -> PathBuf { self.data.join("bookmarks.json") }
|
|
pub fn reading_list(&self) -> PathBuf { self.data.join("reading.json") }
|
|
pub fn downloads(&self) -> PathBuf { self.data.join("downloads.json") }
|
|
pub fn session(&self) -> PathBuf { self.data.join("session.json") }
|
|
pub fn vault(&self) -> PathBuf { self.data.join("vault.json") }
|
|
pub fn vault_key(&self) -> PathBuf { self.data.join("vault.key") }
|
|
pub fn permissions(&self) -> PathBuf { self.data.join("permissions.json") }
|
|
pub fn apps(&self) -> PathBuf { self.data.join("apps.json") }
|
|
pub fn app_icons(&self) -> PathBuf { self.data.join("app-icons") }
|
|
pub fn engine(&self) -> PathBuf { self.data.join("engine") }
|
|
pub fn tor_data(&self) -> PathBuf { self.data.join("tor") }
|
|
pub fn filter_lists(&self) -> PathBuf { self.cache.join("filters") }
|
|
pub fn adblock_engine(&self) -> PathBuf { self.cache.join("adblock-engine.bin") }
|
|
pub fn favicons(&self) -> PathBuf { self.cache.join("favicons") }
|
|
pub fn dictionaries(&self) -> PathBuf { self.cache_root.join("dictionaries") }
|
|
pub fn crash_log(&self) -> PathBuf { self.data.join("pages.log") }
|
|
/// Present while the browser runs; left behind by a crash, which triggers session recovery.
|
|
pub fn running_marker(&self) -> PathBuf { self.data.join("running") }
|
|
/// Port and token of the running instance for this profile (single instance).
|
|
pub fn instance_file(&self) -> PathBuf { self.data.join("instance.json") }
|
|
}
|