From 84407da2618a7d4ba2db258b9d54b10a27b2985e Mon Sep 17 00:00:00 2001 From: astar76 Date: Sat, 14 Nov 2026 08:00:49 -0600 Subject: [PATCH] S051: WIP clear data with time range, site info shows cookies and stored data Co-Authored-By: Claude Opus 5.5 --- .../S051-profile-cookies-media.md | 6 +-- src/actions.rs | 15 +++++-- src/app.rs | 14 +++++- src/engine/host.rs | 44 +++++++++++++++++-- src/engine/mod.rs | 11 +++++ src/selftest.rs | 9 +++- src/ui/menus.rs | 26 +++++++++++ src/ui/pages/misc.rs | 2 +- tests/smoke/site-data.txt | 34 ++++++++++++++ 9 files changed, 149 insertions(+), 12 deletions(-) create mode 100644 tests/smoke/site-data.txt diff --git a/Plans/08 WebKit engine/S051-profile-cookies-media.md b/Plans/08 WebKit engine/S051-profile-cookies-media.md index 48846b8..88cf884 100644 --- a/Plans/08 WebKit engine/S051-profile-cookies-media.md +++ b/Plans/08 WebKit engine/S051-profile-cookies-media.md @@ -14,8 +14,8 @@ one MPRIS player. ## Steps - [x] `paths.rs`: `APP_DIR = "nacre-browser"`; migration (rename config/data/cache once if new missing and old present; delete Servo-only engine files after; log it); unit test with temp dirs; `~/.config/nacre` never touched. -- [ ] Cookies: persistent storage path in `$XDG_RUNTIME_DIR/nacre-/cookies.sqlite`; at start decrypt `cookies.bin` → `add_cookie` (or write the SQLite) ; seal at quit and 60 s after `changed`; no keyring → run-only; convert Servo-era `cookies.bin` when it parses, else move aside. -- [ ] Clear data / clear on exit / site info flyout on `WebsiteDataManager` (types + since-time) and `CookieManager::get_cookies`; drop the "Servo keeps no dates" text. +- [x] Cookies: persistent storage path in `$XDG_RUNTIME_DIR/nacre-/cookies.sqlite`; at start decrypt `cookies.bin` → `add_cookie` (or write the SQLite) ; seal at quit and 60 s after `changed`; no keyring → run-only; convert Servo-era `cookies.bin` when it parses, else move aside. +- [x] Clear data / clear on exit / site info flyout on `WebsiteDataManager` (types + since-time) and `CookieManager::get_cookies`; drop the "Servo keeps no dates" text. - [ ] Incognito: ephemeral session, nothing written; check the throwaway home after a run. - [ ] Media: probe events + `is-playing-audio` → media state; mute via `set_is_muted`; check whether WebKitGTK publishes its own MPRIS player (`busctl --user list | grep mpris` in the nested bus) and apply D-039 (one player). - [ ] Smoke: `cookies.txt` → `cookies-restart.txt` → `cookies-cleared.txt` with `SMOKE_KEYRING=1`, `cookies-nokeyring.txt`, `restore.txt`, `privacy-incognito.txt` green hidden; `grep -c` of a test cookie value in the home dir = 0. @@ -32,4 +32,4 @@ one MPRIS player. Migration behaviour, seal timing, MPRIS outcome (which player). ## Resume note (WIP) -- Box 2 code done (store/cookies.rs: tmpfs `nacre--/cookies.sqlite` written with rusqlite from cookies.bin, sealed by reading the DB; engine `Setup::cookie_db`, `Event::CookiesChanged`, App `cookies` tick/finish); unit tests green. Next: release build, cookies smokes to verify box 2, then box 3. +- Boxes 1–3 done (site-data.txt smoke new, green). Next: box 4 incognito check, box 5 media. diff --git a/src/actions.rs b/src/actions.rs index f33bb91..02cb63c 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -150,6 +150,8 @@ pub enum Command { OpenUpdate, ClearData { since: u64, what: Vec }, ClearSiteData(String), + /// Ask the engine what a host keeps, for the site info flyout. + SiteData(String), SwitchProfile(String), NewWindow, NewIncognito, @@ -702,11 +704,18 @@ impl App { Command::ClearData { since, what } => self.clear_data(since, &what), Command::ClearSiteData(host) => { if let Some(engine) = &self.engine { - engine.clear_data(true, false, Some(vec![host.clone()])); + engine.clear_data(true, false, 0, Some(vec![host.clone()])); } self.shared.permissions.borrow_mut().clear_host(&host); + self.shared.site_data.borrow_mut().0.clear(); self.shared.toast(format!("Data for {host} deleted")); }, + Command::SiteData(host) => { + let url = self.active_url(window); + if let Some(engine) = &self.engine { + engine.site_data(host, &url); + } + }, Command::SwitchProfile(name) => { if let Ok(exe) = std::env::current_exe() { let _ = std::process::Command::new(exe).args(["--profile", &name]).spawn(); @@ -745,7 +754,7 @@ impl App { } } if let Some(engine) = &self.engine { - engine.clear_data(false, true, None); + engine.clear_data(false, true, 0, None); } self.shared.toast(format!("{freed} background tabs unloaded; they load again when you open them")); }, @@ -2226,7 +2235,7 @@ impl App { self.shared.downloads.borrow_mut().clear_inactive(); } if let Some(engine) = &self.engine { - engine.clear_data(has("cookies"), has("cache"), None); + engine.clear_data(has("cookies"), has("cache"), since, None); } if has("permissions") { self.shared.permissions.borrow_mut().clear_all(); diff --git a/src/app.rs b/src/app.rs index b14b5b9..0d70bdb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -172,6 +172,8 @@ pub struct Shared { pub presence_status: RefCell, /// Page sources for nacre://source: url -> text or error (None while loading). pub sources: RefCell>>>, + /// Site info flyout: the host asked about and what it keeps (None while the engine answers). + pub site_data: RefCell<(String, Option)>, } #[derive(Clone, Debug)] @@ -329,6 +331,7 @@ impl App { cava: RefCell::new(Cava::new()), art_images: RefCell::new(HashMap::new()), sources: RefCell::new(HashMap::new()), + site_data: RefCell::default(), presence_status: RefCell::new("Off".into()), incognito, paths, @@ -812,7 +815,16 @@ impl App { Event::Notification(id, notification) => self.on_notification(id, notification), Event::Page(id, message) => self.on_page_message(id, message), Event::Find(id, count) => self.on_find(id, count), + Event::SiteData(host, data) => { + let mut current = self.shared.site_data.borrow_mut(); + if current.0 == host { + current.1 = Some(data); + drop(current); + self.redraw_all(); + } + }, Event::CookiesChanged => { + self.shared.site_data.borrow_mut().0.clear(); if let Some(cookies) = &mut self.cookies { cookies.changed(Instant::now()); } @@ -1697,7 +1709,7 @@ impl App { self.shared.downloads.borrow_mut().clear_inactive(); } if let Some(engine) = &self.engine { - engine.clear_data(what.iter().any(|w| w == "cookies"), what.iter().any(|w| w == "cache"), None); + engine.clear_data(what.iter().any(|w| w == "cookies"), what.iter().any(|w| w == "cache"), 0, None); } } diff --git a/src/engine/host.rs b/src/engine/host.rs index c5e4d36..3f2d47e 100644 --- a/src/engine/host.rs +++ b/src/engine/host.rs @@ -462,8 +462,10 @@ impl Engine { self.views.borrow().values().filter_map(|w| w.upgrade()).collect() } - /// Delete every cookie and stored site data (`what`: cookies and/or cache). - pub fn clear_data(&self, cookies: bool, cache: bool, hosts: Option>) { + /// Delete cookies and stored site data and/or the cache: changed since `since` (Unix seconds, 0 = all; cookies always + /// all), or every + /// item of `hosts` (and their subdomains) whatever its age. + pub fn clear_data(&self, cookies: bool, cache: bool, since: u64, hosts: Option>) { let Some(manager) = self.session.website_data_manager() else { return }; let mut types = webkit::WebsiteDataTypes::empty(); if cookies { @@ -476,7 +478,16 @@ impl Engine { return; } match hosts { - None => manager.clear(types, glib::TimeSpan::from_seconds(0), None::<>k::gio::Cancellable>, |_| {}), + None => { + // WebKit takes "modified in the last "; 0 clears everything. Its libsoup cookie store keeps no + // dates and ignores a span, so cookies go whatever the range (the Clear data page says so). + let span = if since == 0 { 0 } else { crate::util::now().saturating_sub(since).max(1) as i64 }; + if span != 0 && types.contains(webkit::WebsiteDataTypes::COOKIES) { + manager.clear(webkit::WebsiteDataTypes::COOKIES, glib::TimeSpan::from_seconds(0), None::<>k::gio::Cancellable>, |_| {}); + types.remove(webkit::WebsiteDataTypes::COOKIES); + } + manager.clear(types, glib::TimeSpan::from_seconds(span), None::<>k::gio::Cancellable>, |_| {}); + }, Some(hosts) => { let m = manager.clone(); manager.fetch(types, None::<>k::gio::Cancellable>, move |result| { @@ -491,6 +502,33 @@ impl Engine { } } + /// What `host` keeps (site info): the cookies sent to `url` and the kinds of stored data under the host's name. + /// Answers with `Event::SiteData`. + pub fn site_data(&self, host: String, url: &str) { + let (Some(manager), Some(cookies)) = (self.session.website_data_manager(), self.session.cookie_manager()) else { return }; + let hub = self.hub.clone(); + cookies.cookies(url, None::<>k::gio::Cancellable>, move |result| { + let count = result.map(|c| c.len()).unwrap_or(0); + let stored = webkit::WebsiteDataTypes::ALL & !(webkit::WebsiteDataTypes::MEMORY_CACHE | webkit::WebsiteDataTypes::DISK_CACHE | webkit::WebsiteDataTypes::COOKIES); + manager.fetch(stored, None::<>k::gio::Cancellable>, move |result| { + let mut kinds = webkit::WebsiteDataTypes::empty(); + for d in result.unwrap_or_default() { + if d.name().is_some_and(|n| crate::util::host_matches(&host, &n)) { + kinds |= d.types(); + } + } + let labels = [ + (webkit::WebsiteDataTypes::LOCAL_STORAGE, "local storage"), + (webkit::WebsiteDataTypes::INDEXEDDB_DATABASES, "databases"), + (webkit::WebsiteDataTypes::SERVICE_WORKER_REGISTRATIONS, "service workers"), + (webkit::WebsiteDataTypes::DOM_CACHE, "offline files"), + ]; + let stored = labels.iter().filter(|(t, _)| kinds.contains(*t)).map(|(_, l)| *l).collect(); + hub.push(Event::SiteData(host, super::SiteData { cookies: count, stored })); + }); + }); + } + /// A new web view for a tab (`related`: a pop-up sharing its opener's web process). pub fn create_view(self: &Rc, id: ViewId, related: Option<&View>) -> View { // The message handler is registered before the view exists, so the first document can already post. diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 52cdc70..b89a467 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -89,6 +89,17 @@ pub enum Event { Find(ViewId, u32), /// A cookie was added, changed or deleted. CookiesChanged, + /// What a host keeps (`Engine::site_data`). + SiteData(String, SiteData), +} + +/// Cookies and stored data of one site, for the site info flyout. +#[derive(Clone, Debug, Default)] +pub struct SiteData { + /// Cookies the page's address gets. + pub cookies: usize, + /// Kinds of stored data under the host's name ("local storage", …). + pub stored: Vec<&'static str>, } /// A user style sheet (`store::scripts`), added at author level so it acts like the page's own last `