GeezerVison is showing github.com — feat(browser): offer sign-ins and history from another browser on the… · anomalyco/opencode-rune@74b2ca7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/plugin_host/native/browser.rs +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -579,6 +579,7 @@ pub fn version(app: &App) -> u64 { | 579 | 579 | app.workspace_panes.browser.host.share_sign_ins().hash(&mut h); | 580 | 580 | app.workspace_panes.browser.host.engine_preview().hash(&mut h); | 581 | 581 | app.workspace_panes.browser.app_icons.finished().hash(&mut h); | | 582 | + app.workspace_panes.browser.app_icons.sources.generation().hash(&mut h); | 582 | 583 | for t in app.workspace_panes.browser.host.tabs() { | 583 | 584 | if let Some(d) = &t.device { (&d.preset, d.width, d.height, d.scale.map(f32::to_bits)).hash(&mut h); } | 584 | 585 | (t.id, &t.url, &t.title, t.loading, t.can_go_back, t.can_go_forward, t.zoom.to_bits(), t.inspecting, &t.load_error, &t.favicon, &t.refusal, t.rejected).hash(&mut h); | |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/plugin_host/native/browser/cookies.rs +18-1Lines changed: 18 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -44,11 +44,19 @@ pub(super) fn service_methods(lua: &Lua, vmx: &std::rc::Rc<Vmx>, t: &Table) -> m | 44 | 44 | .set_name("=Browser:ImportCookieFileAsync") | 45 | 45 | .call(raw)?; | 46 | 46 | t.raw_set("ImportCookieFileAsync", call)?; | | 47 | + // `Browser:ImportSignInsAsync(source, profile, sites?)`: another browser's sign-ins (`cookie_import`). | | 48 | + let raw: Function = vmx.asyncs.raw_get("Browser.ImportSignInsAsync")?; | | 49 | + let call: Function = lua | | 50 | + .load("local raw = ... return function(self, source, profile, sites) return raw(source, profile, sites) end") | | 51 | + .set_name("=Browser:ImportSignInsAsync") | | 52 | + .call(raw)?; | | 53 | + t.raw_set("ImportSignInsAsync", call)?; | 47 | 54 | Ok(()) | 48 | 55 | } | 49 | 56 |
| 50 | 57 | pub(super) fn asyncs(host: &Host<'_>, t: &Table) -> mlua::Result<()> { | 51 | | - t.raw_set("Browser.ImportCookieFileAsync", host.async_function("Browser:ImportCookieFileAsync", import_cookie_file)?) | | 58 | + t.raw_set("Browser.ImportCookieFileAsync", host.async_function("Browser:ImportCookieFileAsync", import_cookie_file)?)?; | | 59 | + t.raw_set("Browser.ImportSignInsAsync", host.async_function("Browser:ImportSignInsAsync", import_sign_ins)?) | 52 | 60 | } | 53 | 61 |
| 54 | 62 | fn import_cookie_file(lua: &Lua, a: MultiValue, done: Completer) -> mlua::Result<()> { | @@ -59,3 +67,12 @@ fn import_cookie_file(lua: &Lua, a: MultiValue, done: Completer) -> mlua::Result | 59 | 67 | Ok(()) | 60 | 68 | }) | 61 | 69 | } | | 70 | + | | 71 | +fn import_sign_ins(lua: &Lua, a: MultiValue, done: Completer) -> mlua::Result<()> { | | 72 | + let (source, profile, sites): (String, String, Option<Vec<String>>) = mlua::FromLuaMulti::from_lua_multi(a, lua)?; | | 73 | + require_settings_caller(lua, "Browser:ImportSignInsAsync")?; | | 74 | + with_app("Browser:ImportSignInsAsync", |app| { | | 75 | + app.later(Box::new(move |s: &mut App, _: &mut Tx| s.start_browser_import(source, profile, sites, done))); | | 76 | + Ok(()) | | 77 | + }) | | 78 | +} |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/plugin_host/native/browser/history.rs +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -8,7 +8,7 @@ use std::sync::{Mutex, OnceLock}; | 8 | 8 |
| 9 | 9 | use browser_pane::cookies::BrowserEngineKind; | 10 | 10 | use browser_pane::cookies::icons::AppIcon; | 11 | | -use browser_pane::cookies::sources::{list_sources, source_history_database}; | | 11 | +use browser_pane::cookies::sources::source_history_database; | 12 | 12 | use browser_pane::history::{ | 13 | 13 | HistoryStore, ImportEngine, PageVisit, read_profile_history_in, sweep_stale_import_dirs, | 14 | 14 | }; | @@ -155,7 +155,10 @@ pub(super) fn service_methods(lua: &Lua, vmx: &Vmx, t: &Table) -> mlua::Result<( | 155 | 155 | // profile counts when it has a cookie database, named from `Local State` or `profiles.ini`. | 156 | 156 | method(lua, t, "HistoryImportSources", |lua, ()| { | 157 | 157 | require_builtin_browser(lua, "Browser:HistoryImportSources")?; | 158 | | - let list: Vec<Data> = list_sources() | | 158 | + // Listed off the UI thread; the page draws again when a listing lands. | | 159 | + cx::read(Dep::Browser); | | 160 | + let sources = with_app("Browser:HistoryImportSources", |app| Ok(app.workspace_panes.browser.app_icons.sources.get((*app.waker).clone())))?; | | 161 | + let list: Vec<Data> = sources | 159 | 162 | .into_iter() | 160 | 163 | .map(|b| { | 161 | 164 | let profiles: Vec<Data> = b | |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/surfaces/browser/app_icons.rs +49Lines changed: 49 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,8 @@ pub type Known = Option<Arc<AppIcon>>; | 16 | 16 | pub struct AppIcons { | 17 | 17 | done: Arc<Mutex<HashMap<String, Known>>>, | 18 | 18 | asked: RefCell<HashSet<String>>, | | 19 | + /// The other browsers and their profiles (`ImportSources`). | | 20 | + pub sources: ImportSources, | 19 | 21 | } | 20 | 22 |
| 21 | 23 | impl AppIcons { | @@ -52,3 +54,50 @@ impl AppIcons { | 52 | 54 | self.done.lock().map(|d| d.len()).unwrap_or(0) | 53 | 55 | } | 54 | 56 | } | | 57 | + | | 58 | +/// The other browsers and their profiles (`cookies::sources::list_sources`), listed off the UI | | 59 | +/// thread: it reads each browser's `Local State` / `profiles.ini`, which can be large. An ask returns | | 60 | +/// the last listing at once (empty before the first) and, when that is older than `FRESH`, starts | | 61 | +/// another; its end wakes the app and moves `Dep::Browser`'s version, so the page and the pane's | | 62 | +/// sign-in offer draw again with it. | | 63 | +#[derive(Default)] | | 64 | +pub struct ImportSources { | | 65 | + listed: Arc<Mutex<Option<(rune::time::Instant, Vec<browser_pane::cookies::BrowserSource>)>>>, | | 66 | + listing: Arc<std::sync::atomic::AtomicBool>, | | 67 | + generation: Arc<std::sync::atomic::AtomicUsize>, | | 68 | +} | | 69 | + | | 70 | +const FRESH: std::time::Duration = std::time::Duration::from_secs(5); | | 71 | + | | 72 | +impl ImportSources { | | 73 | + pub fn get(&self, waker: Option<rune::Waker>) -> Vec<browser_pane::cookies::BrowserSource> { | | 74 | + use std::sync::atomic::Ordering; | | 75 | + let current = self.listed.lock().ok().and_then(|l| l.clone()); | | 76 | + let stale = current.as_ref().is_none_or(|(at, _)| at.elapsed() >= FRESH); | | 77 | + if stale && !self.listing.swap(true, Ordering::AcqRel) { | | 78 | + let listed = self.listed.clone(); | | 79 | + let listing = self.listing.clone(); | | 80 | + let generation = self.generation.clone(); | | 81 | + let spawned = std::thread::Builder::new().name("browser-import-sources".into()).spawn(move || { | | 82 | + let sources = browser_pane::cookies::sources::list_sources(); | | 83 | + if let Ok(mut l) = listed.lock() { | | 84 | + *l = Some((rune::time::Instant::now(), sources)); | | 85 | + } | | 86 | + generation.fetch_add(1, Ordering::AcqRel); | | 87 | + listing.store(false, Ordering::Release); | | 88 | + if let Some(w) = &waker { | | 89 | + w(); | | 90 | + } | | 91 | + }); | | 92 | + if spawned.is_err() { | | 93 | + self.listing.store(false, Ordering::Release); | | 94 | + } | | 95 | + } | | 96 | + current.map(|(_, s)| s).unwrap_or_default() | | 97 | + } | | 98 | + | | 99 | + /// Moves each time a listing lands: part of `Dep::Browser`'s version. | | 100 | + pub fn generation(&self) -> usize { | | 101 | + self.generation.load(std::sync::atomic::Ordering::Acquire) | | 102 | + } | | 103 | +} |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/surfaces/browser/cookie_import.rs +61-1Lines changed: 61 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -14,7 +14,9 @@ use std::collections::HashSet; | 14 | 14 | use std::sync::{Arc, Mutex}; | 15 | 15 | use std::time::Duration; | 16 | 16 |
| | 17 | +use browser_pane::cookies::database::SnapshotScratch; | 17 | 18 | use browser_pane::cookies::format::prepare_for_import; | | 19 | +use browser_pane::cookies::sources::read_source_cookies; | 18 | 20 | use browser_pane::cookies::{CookieRead, ImportFailure, ProfileEvent, read_cookie_file}; | 19 | 21 | use browser_pane::protocol::WireCookie; | 20 | 22 | use rune::time::Instant; | @@ -44,6 +46,8 @@ enum Reading { | 44 | 46 | pub struct CookieImport { | 45 | 47 | done: Completer, | 46 | 48 | read: Reading, | | 49 | + /// Another browser's sign-ins (its read failures keep their own names), not a file. | | 50 | + browser: bool, | 47 | 51 | /// Batches still to send, the requests the engine has yet to answer, and its answers so far. | 48 | 52 | batches: Vec<Vec<WireCookie>>, | 49 | 53 | waiting: HashSet<u64>, | @@ -57,6 +61,17 @@ fn failure(reason: &str) -> Data { | 57 | 61 | map([("error", Data::String(reason.into()))]) | 58 | 62 | } | 59 | 63 |
| | 64 | +/// A browser's read failure as official names it (`ReadFailure`: `key`, `access`, `locked`, `read`). | | 65 | +fn browser_reason(f: ImportFailure) -> &'static str { | | 66 | + match f { | | 67 | + ImportFailure::Key => "key", | | 68 | + ImportFailure::Access => "access", | | 69 | + ImportFailure::Locked => "locked", | | 70 | + ImportFailure::Storage => "storage", | | 71 | + ImportFailure::Read | ImportFailure::Format => "read", | | 72 | + } | | 73 | +} | | 74 | + | 60 | 75 | fn reason(f: ImportFailure) -> &'static str { | 61 | 76 | match f { | 62 | 77 | ImportFailure::Read | ImportFailure::Key | ImportFailure::Access | ImportFailure::Locked => "read", | @@ -127,6 +142,7 @@ impl App { | 127 | 142 | pane.cookie_import = Some(CookieImport { | 128 | 143 | done, | 129 | 144 | read: Reading::Pending(slot), | | 145 | + browser: false, | 130 | 146 | batches: Vec::new(), | 131 | 147 | waiting: HashSet::new(), | 132 | 148 | sent: false, | @@ -137,6 +153,50 @@ impl App { | 137 | 153 | }); | 138 | 154 | } | 139 | 155 |
| | 156 | + /// Imports the sign-ins of browser `source`'s profile `profile` (ids from `list_sources`, never | | 157 | + /// a path), or with `sites` only those sites' (`profile/index.ts:478-486` `importBrowser`): `#286`'s | | 158 | + /// `read_source_cookies` reads it on a worker thread (it copies databases and may ask the OS key | | 159 | + /// store), then the cookies take the file's path: `prepare_for_import`, batches, the engine. | | 160 | + /// `done` gets what a file import's does, with `error` one of `key`, `access`, `locked`, `read`, | | 161 | + /// `storage`, `busy`, `failed`. | | 162 | + pub(crate) fn start_browser_import(&mut self, source: String, profile: String, sites: Option<Vec<String>>, done: Completer) { | | 163 | + if self.workspace_panes.browser.cookie_import.is_some() { | | 164 | + return done.complete(failure("busy")); | | 165 | + } | | 166 | + let slot: Arc<Mutex<Option<Read>>> = Arc::new(Mutex::new(None)); | | 167 | + let filled = slot.clone(); | | 168 | + let waker = (*self.waker).clone(); | | 169 | + let spawned = std::thread::Builder::new().name("browser-signin-import".into()).spawn(move || { | | 170 | + let scratch = SnapshotScratch::new(SnapshotScratch::default_dir()); | | 171 | + let read = read_source_cookies(&source, &profile, &scratch, sites.as_deref()); | | 172 | + if let Ok(mut s) = filled.lock() { | | 173 | + *s = Some(read); | | 174 | + } | | 175 | + if let Some(w) = &waker { | | 176 | + w(); | | 177 | + } | | 178 | + }); | | 179 | + if spawned.is_err() { | | 180 | + return done.complete(failure("failed")); | | 181 | + } | | 182 | + let start = !self.use_mock_browser() && !self.browser_unsupported(); | | 183 | + let pane = &mut self.workspace_panes.browser; | | 184 | + if !pane.host.engine_cookies() && start { | | 185 | + pane.host.prepare(ANSWER_TIMEOUT); | | 186 | + } | | 187 | + pane.cookie_import = Some(CookieImport { | | 188 | + done, | | 189 | + read: Reading::Pending(slot), | | 190 | + browser: true, | | 191 | + batches: Vec::new(), | | 192 | + waiting: HashSet::new(), | | 193 | + sent: false, | | 194 | + imported: 0, | | 195 | + failed: 0, | | 196 | + deadline: Instant::now() + ANSWER_TIMEOUT, | | 197 | + }); | | 198 | + } | | 199 | + | 140 | 200 | /// Moves the waiting import on: takes the worker's read, sends its batches once the engine's | 141 | 201 | /// cookie store is up, and answers once every batch is answered. Takes only its own profile | 142 | 202 | /// events; others stay for whoever asked. True when an import ended. | @@ -154,7 +214,7 @@ impl App { | 154 | 214 | job.read = Reading::Ready(CookieRead { cookies: Vec::new(), skipped: read.skipped + skipped, ..read }); | 155 | 215 | } | 156 | 216 | Some(Err(f)) => { | 157 | | - job.done.complete(failure(reason(f))); | | 217 | + job.done.complete(failure(if job.browser { browser_reason(f) } else { reason(f) })); | 158 | 218 | return true; | 159 | 219 | } | 160 | 220 | None if Instant::now() >= job.deadline => { | |
Collapse file Copy file name to clipboardExpand all lines: apps/desktop/src/surfaces/browser/mod.rs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -136,7 +136,7 @@ mod native { | 136 | 136 | pub devtools: HashMap<u64, PendingDevtools>, | 137 | 137 | /// Settings > Browser's cookie-file import waiting on the engine (`cookie_import`). | 138 | 138 | pub cookie_import: Option<super::cookie_import::CookieImport>, | 139 | | - /// Each other browser's icon for Settings > Browser, read off the UI thread (`app_icons`). | | 139 | + /// Each other browser's icon and the browsers' list for Settings > Browser, read off the UI thread (`app_icons`). | 140 | 140 | pub app_icons: super::app_icons::AppIcons, | 141 | 141 | /// Last observed Luau store version for `plugin.browser/rune.sitePermissions`. | 142 | 142 | pub site_permissions_version: u64, | |
|