From 760ab8f965f640253d529ccadc13b659a194e2c0 Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 01:17:49 -0500 Subject: [PATCH 1/8] build: add serde_json and zune-jpeg for the CDP browser engine serde_json builds and parses Chrome DevTools Protocol messages (the pane layer may not import src/api/json.rs). zune-jpeg decodes screencast frames in pure Rust. Both pass cargo deny (MIT / Apache-2.0 / Zlib). Co-Authored-By: Claude Fable 5.2 --- Cargo.lock | 36 ++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ 2 files changed, 38 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 18b487c2..4edcea63 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -94,6 +94,7 @@ dependencies = [ "portable-pty", "ratatui", "serde", + "serde_json", "tempfile", "thiserror 2.0.21", "tokio", @@ -104,6 +105,7 @@ dependencies = [ "tracing-subscriber", "unicode-width", "vt100-ctt", + "zune-jpeg", ] [[package]] @@ -1502,6 +1504,19 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + [[package]] name = "serde_spanned" version = "0.6.9" @@ -2329,3 +2344,24 @@ name = "wit-bindgen" version = "0.46.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" + +[[package]] +name = "zune-core" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56377fd46368984a170bc5aac5567e52ca5da874caa60bea39fcbca78fb658b" + +[[package]] +name = "zune-jpeg" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27bc9d5b815bc103f142aa054f561d9187d191692ec7c2d1e2b4737f8dbd7296" +dependencies = [ + "zune-core", +] diff --git a/Cargo.toml b/Cargo.toml index 39f59ef7..718b36c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,8 @@ serde = { version = "1.0.219", features = ["derive"] } toml = "0.8.20" toml_edit = "0.22" nucleo-matcher = "0.3.1" +serde_json = "1.0.154" +zune-jpeg = { version = "0.5.15", default-features = false, features = ["std", "x86", "neon"] } [dev-dependencies] insta = "1.42.2" From 382e26d9cb86de5ef2ed295931f3d8b2a7d2cf18 Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 01:17:49 -0500 Subject: [PATCH 2/8] feat(browser): drive real headless Chrome over the DevTools pipe The browser canvas was a stub: Chrome was launched with no debugging channel and every BrowserEngine call went to the synthetic backend. Engine - cdp_launch: start Chrome with --headless=new --remote-debugging-pipe (fd 3/4 via pre_exec dup2), own process group, per-launch profile dir (pid + pane + random) in the private runtime dir; creation failure aborts the launch; drop kills the group and reaps off the UI thread. - cdp_proto / cdp_session: NUL-framed JSON with a 32 MiB cap, a bounded coalescing command queue, a reader thread (handshake, events, JPEG decode, newest frame only) and a writer thread. The UI thread only enqueues and reads shared state. Garbage or oversize input is a fatal backend error, never a panic. - Navigation, real history (Page.getNavigationHistory), reload, stop, viewport (setDeviceMetricsOverride on open, pane resize and zoom), downloads denied, popups loaded in the one visible page. - url_policy: http, https and about:blank only; file:// behind BILTERM_BROWSER_ALLOW_FILE=1; the URL never goes on Chrome's argv. Wiring - WindowManager::poll_all_ptys ticks media canvases and redraws only when a visible pane changed. - Keys and bracketed paste go to the page while the viewport is focused; Ctrl+] is the escape hatch; Ctrl+L is no longer taken from the shell when the page is not focused. Mouse cells map to frame pixels by proportion instead of a fixed 10x18 cell. - Status line, nav bar and browser.state report what really runs: cdp, cdp (starting), cdp (crashed), synthetic (Chrome not found), synthetic (launch failed: ...). Lock icon only for https; nav-bar hit-testing uses display columns. - Test binaries never launch a browser unless BILTERM_BROWSER_CDP=1 (cfg!(test) is false for integration tests). Co-Authored-By: Claude Fable 5.2 --- src/api/rpc_media.rs | 10 +- src/colon_command/execute_media.rs | 86 +-- src/input/browser_keys.rs | 199 +++++++ src/input/mod.rs | 23 +- src/input/mouse_media.rs | 35 +- src/pane/browser_canvas/cdp.rs | 343 +++++++++-- src/pane/browser_canvas/cdp_input.rs | 334 +++++++++++ src/pane/browser_canvas/cdp_launch.rs | 283 +++++++++ src/pane/browser_canvas/cdp_proto.rs | 497 ++++++++++++++++ src/pane/browser_canvas/cdp_session.rs | 591 +++++++++++++++++++ src/pane/browser_canvas/cdp_session_tests.rs | 448 ++++++++++++++ src/pane/browser_canvas/chrome_ui.rs | 364 +++++++++--- src/pane/browser_canvas/geometry.rs | 76 +++ src/pane/browser_canvas/mod.rs | 315 +++------- src/pane/browser_canvas/pane_hooks.rs | 45 ++ src/pane/browser_canvas/state.rs | 422 +++++++++++++ src/pane/browser_canvas/url_policy.rs | 177 ++++++ src/pane/mod.rs | 8 +- src/ui/mod.rs | 1 + src/wm/media_tick.rs | 103 ++++ src/wm/mod.rs | 2 + tests/browser_canvas_tui_and_url_edit.rs | 84 +++ tests/phase64_layer_dag.rs | 2 + tests/video_canvas_tui_and_ffmpeg.rs | 10 +- 24 files changed, 3984 insertions(+), 474 deletions(-) create mode 100644 src/input/browser_keys.rs create mode 100644 src/pane/browser_canvas/cdp_input.rs create mode 100644 src/pane/browser_canvas/cdp_launch.rs create mode 100644 src/pane/browser_canvas/cdp_proto.rs create mode 100644 src/pane/browser_canvas/cdp_session.rs create mode 100644 src/pane/browser_canvas/cdp_session_tests.rs create mode 100644 src/pane/browser_canvas/geometry.rs create mode 100644 src/pane/browser_canvas/pane_hooks.rs create mode 100644 src/pane/browser_canvas/state.rs create mode 100644 src/pane/browser_canvas/url_policy.rs create mode 100644 src/wm/media_tick.rs diff --git a/src/api/rpc_media.rs b/src/api/rpc_media.rs index f1b8bcfd..5f222701 100644 --- a/src/api/rpc_media.rs +++ b/src/api/rpc_media.rs @@ -21,8 +21,16 @@ impl AutomationControlPlane { }; crate::colon_command::execute_media::execute_browser_command(&mut self.wm, op); + // A URL refused by the policy opens nothing (or leaves a notice on an open canvas). + let refused = match self.wm.pane(target_id).and_then(|p| p.browser_canvas()) { + Some(b) => b.notice().map(str::to_string), + None => Some("browser canvas did not open (URL refused)".to_string()), + }; let mut res = BTreeMap::new(); - res.insert("ok".to_string(), JsonValue::Bool(true)); + res.insert("ok".to_string(), JsonValue::Bool(refused.is_none())); + if let Some(why) = refused { + res.insert("error".to_string(), JsonValue::String(why)); + } res.insert("url".to_string(), JsonValue::String(url.to_string())); res.insert("pane_id".to_string(), JsonValue::Number(target_id as i64)); Ok(JsonValue::Object(res)) diff --git a/src/colon_command/execute_media.rs b/src/colon_command/execute_media.rs index fc4d66e9..f510f59b 100644 --- a/src/colon_command/execute_media.rs +++ b/src/colon_command/execute_media.rs @@ -1,7 +1,7 @@ //! Execution for `:browser` (`BrowserOp`) and `:video` (`VideoOp`) actions (`WEB-01..02`, `VID-01..02`). use crate::action::{BrowserOp, VideoOp}; -use crate::pane::browser_canvas::{MediaMouseEvent, MediaMouseEventKind}; +use crate::pane::browser_canvas::{MediaMouseEvent, MediaMouseEventKind, browser_error_text}; use crate::wm::WindowManager; use crossterm::event::{KeyModifiers, MouseButton}; @@ -11,13 +11,15 @@ fn surface_hint(wm: &WindowManager) -> &'static str { if wm.gui_surface { "" } else { TUI_SURFACE_HINT } } -fn browser_backend_label(name: &str, url: &str) -> &'static str { - if name == "cdp-chromium" { - "CDP" - } else if url.starts_with("demo://") || url.starts_with("about:") || url.starts_with("test://") { - "synthetic preview" - } else { - "synthetic preview (Chrome not found)" +/// Status-line label for what is really running, e.g. `cdp`, `cdp (starting)`, +/// `synthetic (Chrome not found)`. +fn browser_backend_label(pane: &crate::pane::CompositePane) -> Option { + pane.browser_canvas().map(|b| b.backend_name()) +} + +fn report(pane: &mut crate::pane::CompositePane, result: crate::error::Result<()>) { + if let Err(e) = result { + pane.set_status_message(Some(format!("Browser: {}", browser_error_text(&e)))); } } @@ -52,43 +54,46 @@ pub fn execute_browser_command(wm: &mut WindowManager, op: BrowserOp) -> bool { pane.set_status_message(Some("Closed browser canvas".to_string())); } else { pane.open_browser("https://example.com"); - let label = pane - .browser_canvas() - .map(|b| browser_backend_label(b.backend_name(), "https://example.com")) - .unwrap_or("none"); - pane.set_status_message(Some(format!("Opened browser canvas: {label}{hint}"))); + if let Some(label) = browser_backend_label(pane) { + pane.set_status_message(Some(format!("Opened browser canvas: {label}{hint}"))); + } } } BrowserOp::Open { url, .. } => { - if let Some(b) = pane.browser_canvas_mut() { - let _ = b.navigate(&url); - } else { - pane.open_browser(&url); + let navigated = match pane.browser_canvas_mut() { + Some(b) => b.navigate(&url), + None => { + pane.open_browser(&url); + Ok(()) + } + }; + match (navigated, browser_backend_label(pane)) { + (Err(e), _) => pane.set_status_message(Some(format!("Browser: {}", browser_error_text(&e)))), + (Ok(()), Some(label)) => { + pane.set_status_message(Some(format!("Browser opened: {url} ({label}){hint}"))); + } + // `open_browser` refused the URL and already reported why. + (Ok(()), None) => {} } - let label = pane - .browser_canvas() - .map(|b| browser_backend_label(b.backend_name(), &url)) - .unwrap_or("none"); - pane.set_status_message(Some(format!("Browser opened: {url} ({label}){hint}"))); } BrowserOp::Back { .. } => { - if let Some(b) = pane.browser_canvas_mut() { - let _ = b.go_back(); + if let Some(r) = pane.browser_canvas_mut().map(|b| b.go_back()) { + report(pane, r); } } BrowserOp::Forward { .. } => { - if let Some(b) = pane.browser_canvas_mut() { - let _ = b.go_forward(); + if let Some(r) = pane.browser_canvas_mut().map(|b| b.go_forward()) { + report(pane, r); } } BrowserOp::Reload { .. } => { - if let Some(b) = pane.browser_canvas_mut() { - let _ = b.reload(); + if let Some(r) = pane.browser_canvas_mut().map(|b| b.reload()) { + report(pane, r); } } BrowserOp::Stop { .. } => { - if let Some(b) = pane.browser_canvas_mut() { - let _ = b.stop(); + if let Some(r) = pane.browser_canvas_mut().map(|b| b.stop()) { + report(pane, r); } } BrowserOp::FocusViewport { focused, .. } => { @@ -97,11 +102,16 @@ pub fn execute_browser_command(wm: &mut WindowManager, op: BrowserOp) -> bool { Some(f) => b.set_focus(f), None => b.toggle_focus(), } + if b.viewport_focused { + pane.set_status_message(Some(crate::input::browser_keys::FOCUS_HINT.to_string())); + } } } BrowserOp::Zoom { delta_pct, .. } => { if let Some(b) = pane.browser_canvas_mut() { b.zoom_delta(delta_pct); + let zoom = b.zoom_pct; + pane.set_status_message(Some(format!("Browser zoom: {zoom}%"))); } } BrowserOp::Click { x_px, y_px, button, .. } => { @@ -111,20 +121,20 @@ pub fn execute_browser_command(wm: &mut WindowManager, op: BrowserOp) -> bool { 2 => MouseButton::Middle, _ => MouseButton::Left, }; - let _ = b.dispatch_mouse(MediaMouseEvent { - kind: MediaMouseEventKind::Down(mb), - x_px, - y_px, - modifiers: KeyModifiers::NONE, - }); + // A click is a press followed by a release. + for kind in [MediaMouseEventKind::Down(mb), MediaMouseEventKind::Up(mb)] { + let _ = b.dispatch_mouse(MediaMouseEvent { kind, x_px, y_px, modifiers: KeyModifiers::NONE }); + } } } BrowserOp::Scroll { delta_x, delta_y, .. } => { if let Some(b) = pane.browser_canvas_mut() { + // Scroll at the centre of the page so the wheel reaches the main scroller. + let (w, h) = b.cached_frame().map_or(b.viewport_px(), |f| (f.width_px, f.height_px)); let _ = b.dispatch_mouse(MediaMouseEvent { kind: MediaMouseEventKind::Wheel { delta_x, delta_y }, - x_px: 0, - y_px: 0, + x_px: w / 2, + y_px: h / 2, modifiers: KeyModifiers::NONE, }); } diff --git a/src/input/browser_keys.rs b/src/input/browser_keys.rs new file mode 100644 index 00000000..294510ca --- /dev/null +++ b/src/input/browser_keys.rs @@ -0,0 +1,199 @@ +//! Keyboard and paste routing for the browser canvas (`WEB-02`). +//! +//! - While the URL bar is being edited every key goes to the editor. +//! - `Ctrl+]` toggles viewport focus on a pane that shows a browser canvas. It is the escape +//! hatch: it always hands the keyboard back to bilterm. +//! - With the viewport focused, keys go to the page (`Input.dispatchKeyEvent`). `Ctrl+L` edits the +//! URL, and `r` reloads a crashed page. Leader chords, user bindings and reserved chords are +//! resolved before this, so they keep working. +//! - Without focus nothing is taken: `Ctrl+L` still reaches the shell. + +use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; + +use crate::input::AppAction; +use crate::wm::WindowManager; + +/// Status-line hint shown when the page takes the keyboard. +pub const FOCUS_HINT: &str = "Browser focus: keys go to the page. Ctrl+] returns to the prompt"; + +/// `Ctrl+]`. Legacy terminals report it as `Ctrl+5` (byte 0x1D). +pub fn is_focus_toggle_key(key: KeyEvent) -> bool { + key.modifiers == KeyModifiers::CONTROL && matches!(key.code, KeyCode::Char(']') | KeyCode::Char('5')) +} + +/// URL-bar editing and the focus toggle. Runs before modal and leader handling. +pub(super) fn handle_browser_chrome_key(wm: &mut WindowManager, key: KeyEvent) -> Option { + let pane = wm.active_pane_mut(); + let b = pane.browser_canvas_mut()?; + if b.is_editing_url() { + if key.kind != KeyEventKind::Release { + b.handle_url_edit_key(key); + } + return Some(AppAction::Handled); + } + if is_focus_toggle_key(key) { + if key.kind == KeyEventKind::Press { + b.toggle_focus(); + let focused = b.viewport_focused; + pane.set_status_message(Some(if focused { FOCUS_HINT.to_string() } else { "Browser focus: prompt".to_string() })); + } + return Some(AppAction::Handled); + } + None +} + +/// Forwards a key to the page when the browser viewport has focus. +pub(super) fn forward_browser_viewport_key(wm: &mut WindowManager, key: KeyEvent) -> Option { + let b = wm.active_pane_mut().browser_canvas_mut()?; + if !b.viewport_focused { + return None; + } + if key.kind == KeyEventKind::Release { + return Some(AppAction::Handled); + } + if key.modifiers == KeyModifiers::CONTROL && key.code == KeyCode::Char('l') { + b.begin_url_edit(); + } else if b.is_crashed() && key.modifiers.is_empty() && key.code == KeyCode::Char('r') { + let _ = b.reload(); + } else { + let _ = b.dispatch_key(key); + } + Some(AppAction::Handled) +} + +/// Bracketed paste: into the URL editor when it is open, into the page when it has focus. +pub(super) fn handle_browser_paste(wm: &mut WindowManager, text: &str) -> Option { + let b = wm.active_pane_mut().browser_canvas_mut()?; + if !b.is_editing_url() && !b.viewport_focused { + return None; + } + let _ = b.paste_text(text); + Some(AppAction::Handled) +} + +#[cfg(test)] +mod tests { + use std::sync::{Arc, Mutex}; + + use super::*; + use crate::error::Result; + use crate::pane::browser_canvas::{BrowserCanvasState, BrowserEngine, EngineHealth, MediaMouseEvent, MediaRgbaFrame}; + + #[derive(Default)] + struct Log { + keys: Vec, + pasted: Vec, + crashed: bool, + } + + struct RecordingEngine(Arc>); + + impl BrowserEngine for RecordingEngine { + fn navigate(&mut self, _url: &str) -> Result<()> { Ok(()) } + fn go_back(&mut self) -> Result<()> { Ok(()) } + fn go_forward(&mut self) -> Result<()> { Ok(()) } + fn reload(&mut self) -> Result<()> { Ok(()) } + fn stop(&mut self) -> Result<()> { Ok(()) } + fn resize_viewport(&mut self, _w: u16, _h: u16, _s: f32) -> Result<()> { Ok(()) } + fn dispatch_mouse(&mut self, _e: MediaMouseEvent) -> Result<()> { Ok(()) } + fn dispatch_key(&mut self, k: KeyEvent) -> Result<()> { + self.0.lock().unwrap().keys.push(k); + Ok(()) + } + fn insert_text(&mut self, text: &str) -> Result<()> { + self.0.lock().unwrap().pasted.push(text.to_string()); + Ok(()) + } + fn latest_frame(&mut self) -> Option { None } + fn current_url(&self) -> &str { "https://example.com/" } + fn page_title(&self) -> &str { "" } + fn is_loading(&self) -> bool { false } + fn backend_name(&self) -> &'static str { "cdp" } + fn health(&self) -> EngineHealth { + if self.0.lock().unwrap().crashed { EngineHealth::Crashed("page crashed".into()) } else { EngineHealth::Ready } + } + } + + fn wm_with_engine() -> (WindowManager, Arc>) { + let mut wm = WindowManager::new_headless(24, 80); + let log = Arc::new(Mutex::new(Log::default())); + wm.active_pane_mut().browser_canvas = Some(BrowserCanvasState::new(Box::new(RecordingEngine(Arc::clone(&log))))); + (wm, log) + } + + fn press(wm: &mut WindowManager, code: KeyCode, mods: KeyModifiers) { + crate::input::handle_wm_key_event(wm, KeyEvent::new(code, mods)).unwrap(); + } + + fn browser(wm: &mut WindowManager) -> &mut BrowserCanvasState { + wm.active_pane_mut().browser_canvas_mut().unwrap() + } + + #[test] + fn keys_reach_the_engine_only_while_the_viewport_is_focused() { + let (mut wm, log) = wm_with_engine(); + press(&mut wm, KeyCode::Char('a'), KeyModifiers::NONE); + press(&mut wm, KeyCode::Char('l'), KeyModifiers::CONTROL); + assert!(log.lock().unwrap().keys.is_empty()); + assert!(!browser(&mut wm).is_editing_url(), "Ctrl+L is left to the shell"); + + press(&mut wm, KeyCode::Char(']'), KeyModifiers::CONTROL); + assert!(browser(&mut wm).viewport_focused); + for (code, mods) in [ + (KeyCode::Char('a'), KeyModifiers::NONE), + (KeyCode::Char('A'), KeyModifiers::SHIFT), + (KeyCode::Tab, KeyModifiers::NONE), + (KeyCode::Esc, KeyModifiers::NONE), + (KeyCode::F(5), KeyModifiers::NONE), + (KeyCode::Char('a'), KeyModifiers::CONTROL), + ] { + press(&mut wm, code, mods); + } + let codes: Vec = log.lock().unwrap().keys.iter().map(|k| k.code).collect(); + assert_eq!( + codes, + [KeyCode::Char('a'), KeyCode::Char('A'), KeyCode::Tab, KeyCode::Esc, KeyCode::F(5), KeyCode::Char('a')] + ); + + // Key releases (kitty protocol) are swallowed, not sent twice. + let mut release = KeyEvent::new(KeyCode::Char('z'), KeyModifiers::NONE); + release.kind = KeyEventKind::Release; + crate::input::handle_wm_key_event(&mut wm, release).unwrap(); + assert_eq!(log.lock().unwrap().keys.len(), 6); + + press(&mut wm, KeyCode::Char(']'), KeyModifiers::CONTROL); + assert!(!browser(&mut wm).viewport_focused); + press(&mut wm, KeyCode::Char('q'), KeyModifiers::NONE); + assert_eq!(log.lock().unwrap().keys.len(), 6, "escape hatch returned the keyboard"); + } + + #[test] + fn ctrl_l_edits_the_url_only_when_focused_and_paste_follows_focus() { + let (mut wm, log) = wm_with_engine(); + assert!(handle_browser_paste(&mut wm, "nope").is_none(), "unfocused paste goes to the prompt"); + browser(&mut wm).set_focus(true); + crate::input::handle_wm_paste_event(&mut wm, "hello\nworld").unwrap(); + assert_eq!(log.lock().unwrap().pasted, ["hello\nworld"]); + + press(&mut wm, KeyCode::Char('l'), KeyModifiers::CONTROL); + assert!(browser(&mut wm).is_editing_url()); + press(&mut wm, KeyCode::Char('u'), KeyModifiers::CONTROL); + crate::input::handle_wm_paste_event(&mut wm, "exa\nmple.org").unwrap(); + assert_eq!(browser(&mut wm).url_edit().unwrap().text, "example.org", "control characters are stripped"); + assert_eq!(log.lock().unwrap().pasted.len(), 1, "paste went to the URL editor, not the page"); + assert!(log.lock().unwrap().keys.is_empty(), "Ctrl+L and Ctrl+U were not sent to the page"); + } + + #[test] + fn r_reloads_a_crashed_page_and_is_not_typed_into_it() { + let (mut wm, log) = wm_with_engine(); + browser(&mut wm).set_focus(true); + log.lock().unwrap().crashed = true; + assert_eq!(browser(&mut wm).overlay_message().as_deref(), Some("page crashed — press r to reload")); + assert_eq!(browser(&mut wm).backend_name(), "cdp (crashed)"); + press(&mut wm, KeyCode::Char('r'), KeyModifiers::NONE); + assert!(log.lock().unwrap().keys.is_empty()); + assert!(!browser(&mut wm).is_crashed(), "reload replaced the dead engine"); + assert!(browser(&mut wm).viewport_focused, "focus survives the relaunch"); + } +} diff --git a/src/input/mod.rs b/src/input/mod.rs index b5a01f65..fbbea61d 100644 --- a/src/input/mod.rs +++ b/src/input/mod.rs @@ -2,6 +2,7 @@ pub mod agents_hud; pub mod agents_list; pub mod agents_list_keys; mod agents_window; +pub mod browser_keys; pub mod chord; pub mod clipboard_window; pub mod clipboard_window_keys; @@ -604,6 +605,9 @@ pub fn handle_wm_paste_event(wm: &mut WindowManager, pasted_text: &str) -> Resul if let Some(action) = agents_window::handle_agents_window_paste(wm, pasted_text) { return Ok(action); } + if let Some(action) = browser_keys::handle_browser_paste(wm, pasted_text) { + return Ok(action); + } if !pasted_text.is_empty() { wm.active_pane_mut().record_clipboard_event( crate::wm::clipboard_registry::ClipboardSource::SystemPaste, @@ -709,7 +713,7 @@ pub fn handle_wm_key_event(wm: &mut WindowManager, raw_key: KeyEvent) -> Result< return Ok(AppAction::Handled); } - if let Some(action) = handle_browser_url_edit_key(wm, key) { + if let Some(action) = browser_keys::handle_browser_chrome_key(wm, key) { return Ok(action); } @@ -734,6 +738,9 @@ pub fn handle_wm_key_event(wm: &mut WindowManager, raw_key: KeyEvent) -> Result< if let Some(action) = keymap::reserved_global_action(&km, key).or_else(|| km.primary_action(key)) { return wm.apply(action); } + if let Some(action) = browser_keys::forward_browser_viewport_key(wm, key) { + return Ok(action); + } // Refresh active pane line discipline before deciding whether F1..F10 / Alt+ should pass through to Raw PTY wm.active_pane_mut().refresh_line_discipline(); @@ -763,20 +770,6 @@ pub fn handle_wm_key_event(wm: &mut WindowManager, raw_key: KeyEvent) -> Result< dispatch_pane_key(wm, key) } -/// Routes keys to the browser URL bar editor (active editing, or `Ctrl+L` to start it). -fn handle_browser_url_edit_key(wm: &mut WindowManager, key: KeyEvent) -> Option { - let b = wm.active_pane_mut().browser_canvas_mut()?; - if b.is_editing_url() { - b.handle_url_edit_key(key); - return Some(AppAction::Handled); - } - if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('l') { - b.begin_url_edit(); - return Some(AppAction::Handled); - } - None -} - fn dispatch_pane_key(wm: &mut WindowManager, key: KeyEvent) -> Result { let smart = wm.config().smart_ctrl_v_paste; let km = wm.keymap(); diff --git a/src/input/mouse_media.rs b/src/input/mouse_media.rs index de17e749..2dc32007 100644 --- a/src/input/mouse_media.rs +++ b/src/input/mouse_media.rs @@ -4,6 +4,7 @@ use crossterm::event::{MouseButton, MouseEvent, MouseEventKind}; use ratatui::layout::Rect; use crate::input::AppAction; +use crate::pane::browser_canvas::geometry::viewport_body; use crate::pane::browser_canvas::{ BrowserNavHit, MediaMouseEvent, MediaMouseEventKind, hit_test_browser_nav_bar, }; @@ -38,7 +39,8 @@ pub fn handle_media_canvas_mouse( let _ = pane.browser_canvas_mut().map(|b| b.go_forward()); } BrowserNavHit::Reload => { - let _ = pane.browser_canvas_mut().map(|b| b.reload()); + // The button shows "stop" while the page is loading. + let _ = pane.browser_canvas_mut().map(|b| if b.is_loading() { b.stop() } else { b.reload() }); } BrowserNavHit::ToggleFocus => { if let Some(b) = pane.browser_canvas_mut() { @@ -105,14 +107,14 @@ pub fn handle_media_canvas_mouse( // 3. Viewport forwarding if pane.browser_canvas().is_some() && row > canvas_rect.y && row < canvas_rect.bottom() { + let pressed = matches!(mouse.kind, MouseEventKind::Down(_)); if in_prompt { + // A click on the floating prompt hands the keyboard back to it. + if pressed && let Some(b) = pane.browser_canvas_mut() { + b.set_focus(false); + } return None; } - let rel_x = col.saturating_sub(canvas_rect.x); - let rel_y = row.saturating_sub(canvas_rect.y + 1); - let x_px = rel_x.saturating_mul(10); - let y_px = rel_y.saturating_mul(18); - let kind = match mouse.kind { MouseEventKind::Down(mb) => Some(MediaMouseEventKind::Down(mb)), MouseEventKind::Up(mb) => Some(MediaMouseEventKind::Up(mb)), @@ -122,15 +124,18 @@ pub fn handle_media_canvas_mouse( MouseEventKind::ScrollUp => Some(MediaMouseEventKind::Wheel { delta_x: 0, delta_y: -53 }), _ => None, }; - - if let Some(k) = kind { - let ev = MediaMouseEvent { - kind: k, - x_px, - y_px, - modifiers: mouse.modifiers, - }; - let _ = pane.browser_canvas_mut().map(|b| b.dispatch_mouse(ev)); + // The frame is stretched over the viewport cells, so the cell maps to a frame pixel by + // proportion (same in the TUI and the GUI); no fixed cell size is assumed. + let body = viewport_body(canvas_rect); + if let Some(k) = kind + && let Some(b) = pane.browser_canvas_mut() + && let Some((x_px, y_px)) = b.frame_px_for_cell(body, col, row) + { + if pressed { + // Clicking the page gives it the keyboard (Ctrl+] gives it back). + b.set_focus(true); + } + let _ = b.dispatch_mouse(MediaMouseEvent { kind: k, x_px, y_px, modifiers: mouse.modifiers }); return Some(AppAction::Handled); } } diff --git a/src/pane/browser_canvas/cdp.rs b/src/pane/browser_canvas/cdp.rs index 3e32f585..0ff5809d 100644 --- a/src/pane/browser_canvas/cdp.rs +++ b/src/pane/browser_canvas/cdp.rs @@ -1,16 +1,25 @@ //! Out-of-process Chromium engine bridge via Chrome DevTools Protocol (`WEB-01..02`). +//! +//! Layout: [`super::cdp_launch`] starts Chrome with `--remote-debugging-pipe`, +//! [`super::cdp_session`] runs the reader/writer threads and the protocol state machine, +//! [`super::cdp_proto`] and [`super::cdp_input`] build and parse messages. This file is the +//! [`BrowserEngine`] the pane talks to. +use std::fmt; use std::path::PathBuf; -use std::process::{Child, Command, Stdio}; -use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::Arc; +use std::time::{Duration, Instant}; + use crossterm::event::KeyEvent; -use crate::error::{BiltermError, Result}; +use crate::error::Result; use crate::pane::PaneId; -use crate::secure_fs::ensure_private_dir; -use super::{BrowserEngine, MediaMouseEvent, MediaRgbaFrame, SyntheticBrowserBackend}; +use super::cdp_input::{key_commands, mouse_command}; +use super::cdp_proto::{self as proto, Cmd}; +use super::cdp_session::{Boot, PageState, Phase, PushError, SCREENCAST_QUALITY, Shared, Viewport, spawn_session}; +use super::url_policy::{file_urls_enabled, validate_url}; +use super::{BrowserEngine, EngineHealth, MediaMouseEvent, MediaMouseEventKind, MediaRgbaFrame, browser_error}; /// Probe order for finding an installed Chromium-family browser binary. pub fn discover_chromium_binary() -> Option { @@ -70,121 +79,331 @@ pub fn discover_chromium_binary() -> Option { None } -/// Out-of-process headless Chromium backend using CDP screencast and input dispatch. +/// Time allowed between launching Chrome and a finished DevTools handshake. +pub const HANDSHAKE_TIMEOUT: Duration = Duration::from_secs(5); +const DOUBLE_CLICK: Duration = Duration::from_millis(400); +/// How often the tick re-reads navigation history. Chrome sends no event when a script changes +/// `document.title`, so the committed history entry is polled for title and back/forward state. +const HISTORY_REFRESH: Duration = Duration::from_millis(400); +/// The refresh is skipped while this many commands are still waiting for the writer. +const REFRESH_MAX_BACKLOG: usize = 32; + +/// Why real Chrome could not be started (shown as `synthetic ()`). +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum SpawnError { + NotFound, + Launch(String), +} + +impl fmt::Display for SpawnError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::NotFound => write!(f, "Chrome not found"), + Self::Launch(why) => write!(f, "launch failed: {why}"), + } + } +} + +/// Real browser engine: headless Chrome driven over the DevTools pipe. +/// +/// Every method only enqueues a command or reads shared state, so the UI thread never waits for +/// Chrome. `backend_name` is `"cdp"`; [`BrowserEngine::health`] is `Starting` until the handshake. pub struct CdpBrowserBackend { - child: Option, - profile_dir: PathBuf, - synthetic: SyntheticBrowserBackend, - running: Arc, + shared: Arc, + #[cfg(unix)] + process: Option, + page: PageState, + viewport: Viewport, + started: Instant, + allow_file: bool, + last_click: Option<(Instant, u16, u16, u8)>, + last_history_refresh: Instant, } impl CdpBrowserBackend { + /// Launches Chrome and starts the handshake in the background. Returns at once. pub fn try_spawn( initial_url: &str, width_px: u16, height_px: u16, pane_id: PaneId, - ) -> Result { - let bin = discover_chromium_binary().ok_or_else(|| { - BiltermError::Io(std::io::Error::new( - std::io::ErrorKind::NotFound, - "No Chromium-compatible browser binary found on system", - )) - })?; - - let profile_base = std::env::temp_dir().join(format!("bilterm-chrome-pane-{pane_id}")); - let _ = ensure_private_dir(&profile_base); - - let mut cmd = Command::new(bin); - cmd.arg("--headless=new") - .arg(format!("--user-data-dir={}", profile_base.to_string_lossy())) - .arg(format!("--window-size={width_px},{height_px}")) - .arg("--hide-scrollbars") - .arg("--mute-audio") - .arg("--no-first-run") - .arg("--no-default-browser-check") - .arg("--disable-background-networking") - .arg(initial_url) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::null()); - - let child = cmd.spawn().map_err(BiltermError::Io)?; - let synthetic = SyntheticBrowserBackend::new(initial_url, width_px, height_px); + ) -> std::result::Result { + let allow_file = file_urls_enabled(); + let url = validate_url(initial_url, allow_file).map_err(|e| SpawnError::Launch(format!("blocked URL: {e}")))?; + let bin = discover_chromium_binary().ok_or(SpawnError::NotFound)?; + Self::spawn_process(&bin, &url, width_px, height_px, pane_id, allow_file) + } + + #[cfg(unix)] + fn spawn_process( + bin: &std::path::Path, + url: &str, + width_px: u16, + height_px: u16, + pane_id: PaneId, + allow_file: bool, + ) -> std::result::Result { + use super::cdp_launch::{launch, profiles_root, sweep_stale_profiles}; + let io_err = |what: &str, e: std::io::Error| SpawnError::Launch(format!("{what}: {e}")); + let root = profiles_root().map_err(|e| io_err("private profile dir", e))?; + let sweep_root = root.clone(); + let _ = std::thread::Builder::new().name("bilterm-chrome-sweep".into()).spawn(move || sweep_stale_profiles(&sweep_root)); + let viewport = Viewport::from_frame_px(width_px, height_px, 1.0); + let (process, pipes) = launch(bin, &root, pane_id, viewport.css_width, viewport.css_height) + .map_err(|e| io_err("could not start Chrome", e))?; + let mut backend = Self::with_transport(pipes.events, pipes.commands, url, viewport, allow_file) + .map_err(|e| io_err("could not start worker threads", e))?; + backend.process = Some(process); + Ok(backend) + } + + #[cfg(not(unix))] + fn spawn_process( + _bin: &std::path::Path, + _url: &str, + _width_px: u16, + _height_px: u16, + _pane_id: PaneId, + _allow_file: bool, + ) -> std::result::Result { + Err(SpawnError::Launch("the DevTools pipe transport needs a Unix host".to_string())) + } + /// Runs the engine over an already connected transport (tests use in-memory pipes). + pub fn with_transport(reader: R, writer: W, url: &str, viewport: Viewport, allow_file: bool) -> std::io::Result + where + R: std::io::Read + Send + 'static, + W: std::io::Write + Send + 'static, + { + let shared = Shared::new(Boot { url: url.to_string(), viewport, allow_file }); + spawn_session(reader, writer, Arc::clone(&shared))?; + let page = shared.page(); Ok(Self { - child: Some(child), - profile_dir: profile_base, - synthetic, - running: Arc::new(AtomicBool::new(true)), + shared, + #[cfg(unix)] + process: None, + page, + viewport, + started: Instant::now(), + allow_file, + last_click: None, + last_history_refresh: Instant::now(), }) } + + /// Pixel size of the newest screencast frame (`(0, 0)` before the first one). + pub fn frame_size_px(&self) -> (u16, u16) { + self.page.frame_px + } + + fn sync(&mut self) -> bool { + match self.shared.page_if_changed(self.page.generation) { + Some(page) => { + self.page = page; + true + } + None => false, + } + } + + fn send(&mut self, cmd: Cmd) -> Result<()> { + match self.shared.queue.push(cmd) { + Ok(()) => Ok(()), + Err(PushError::Busy) => Err(browser_error(std::io::ErrorKind::WouldBlock, "browser is busy; input dropped")), + Err(PushError::Closed) => Err(browser_error(std::io::ErrorKind::BrokenPipe, "browser is not running")), + } + } + + fn is_ready(&self) -> bool { + self.page.phase == Phase::Ready + } + + /// Frame pixels to CSS pixels, from the newest frame's metadata (falls back to the zoom scale). + fn css_point(&self, x_px: u16, y_px: u16) -> (f64, f64) { + let (fw, fh) = self.page.frame_px; + let (dw, dh) = self.page.device_px; + if fw > 0 && fh > 0 && dw > 0.0 && dh > 0.0 { + return (f64::from(x_px) * dw / f64::from(fw), f64::from(y_px) * dh / f64::from(fh)); + } + (f64::from(x_px) / self.viewport.scale, f64::from(y_px) / self.viewport.scale) + } + + fn click_count(&mut self, button: u8, x: u16, y: u16) -> u8 { + let now = Instant::now(); + let count = match self.last_click { + Some((at, lx, ly, n)) if now.duration_since(at) <= DOUBLE_CLICK && lx.abs_diff(x) <= 8 && ly.abs_diff(y) <= 8 => { + if n >= 3 { 1 } else { n + 1 } + } + _ => 1, + }; + let _ = button; + self.last_click = Some((now, x, y, count)); + count + } } impl Drop for CdpBrowserBackend { fn drop(&mut self) { - self.running.store(false, Ordering::SeqCst); - if let Some(mut child) = self.child.take() { - let _ = child.kill(); - let _ = child.wait(); - } - if self.profile_dir.exists() { - let _ = std::fs::remove_dir_all(&self.profile_dir); - } + // Closing the queue stops the writer; killing Chrome (ChromeProcess::drop) ends the reader. + self.shared.queue.close(); } } impl BrowserEngine for CdpBrowserBackend { fn navigate(&mut self, url: &str) -> Result<()> { - self.synthetic.navigate(url) + self.sync(); + let url = validate_url(url, self.allow_file) + .map_err(|e| browser_error(std::io::ErrorKind::InvalidInput, format!("blocked URL: {e}")))?; + match self.page.phase { + Phase::Starting => self.shared.update_boot(|b| b.url = url.clone()), + Phase::Ready => self.send(proto::navigate(&url))?, + Phase::Crashed(_) | Phase::Failed(_) => { + return Err(browser_error(std::io::ErrorKind::BrokenPipe, "browser is not running")); + } + } + self.shared.update_page(|p| { + p.url = url; + p.loading = true; + p.nav_error = None; + }); + self.sync(); + Ok(()) } fn go_back(&mut self) -> Result<()> { - self.synthetic.go_back() + self.send(proto::get_history(-1)) } fn go_forward(&mut self) -> Result<()> { - self.synthetic.go_forward() + self.send(proto::get_history(1)) } fn reload(&mut self) -> Result<()> { - self.synthetic.reload() + self.send(proto::reload()) } fn stop(&mut self) -> Result<()> { - self.synthetic.stop() + self.send(proto::stop_loading()) } fn resize_viewport(&mut self, width_px: u16, height_px: u16, scale_factor: f32) -> Result<()> { - self.synthetic.resize_viewport(width_px, height_px, scale_factor) + let viewport = Viewport::from_frame_px(width_px, height_px, scale_factor); + if viewport == self.viewport { + return Ok(()); + } + self.viewport = viewport; + self.sync(); + if !self.is_ready() { + self.shared.update_boot(|b| b.viewport = viewport); + return Ok(()); + } + let (w, h) = viewport.frame_px(); + self.send(proto::set_device_metrics(viewport.css_width, viewport.css_height, viewport.scale))?; + self.send(proto::stop_screencast())?; + self.send(proto::start_screencast(w, h, SCREENCAST_QUALITY)) } fn dispatch_mouse(&mut self, event: MediaMouseEvent) -> Result<()> { - self.synthetic.dispatch_mouse(event) + if !self.is_ready() { + return Ok(()); + } + let (x, y) = self.css_point(event.x_px, event.y_px); + let clicks = match event.kind { + MediaMouseEventKind::Down(b) => self.click_count(b as u8, event.x_px, event.y_px), + MediaMouseEventKind::Up(_) => self.last_click.map_or(1, |c| c.3), + _ => 0, + }; + self.send(mouse_command(&event, x, y, clicks)) } fn dispatch_key(&mut self, key: KeyEvent) -> Result<()> { - self.synthetic.dispatch_key(key) + if !self.is_ready() { + return Ok(()); + } + for cmd in key_commands(key) { + self.send(cmd)?; + } + Ok(()) + } + + fn insert_text(&mut self, text: &str) -> Result<()> { + if !self.is_ready() || text.is_empty() { + return Ok(()); + } + self.send(proto::insert_text(text)) } fn latest_frame(&mut self) -> Option { - self.synthetic.latest_frame() + self.shared.latest_frame() } fn current_url(&self) -> &str { - self.synthetic.current_url() + &self.page.url } fn page_title(&self) -> &str { - self.synthetic.page_title() + &self.page.title } fn is_loading(&self) -> bool { - self.synthetic.is_loading() + self.page.loading } fn backend_name(&self) -> &'static str { - "cdp-chromium" + "cdp" + } + + fn poll(&mut self) -> bool { + if self.page.phase == Phase::Starting && self.started.elapsed() > HANDSHAKE_TIMEOUT { + #[cfg(unix)] + let detail = self.process.as_ref().and_then(|p| { + p.kill_group(); + p.stderr_tail() + }); + #[cfg(not(unix))] + let detail: Option = None; + let secs = HANDSHAKE_TIMEOUT.as_secs(); + self.shared.mark_dead(&match detail { + Some(d) => format!("no DevTools handshake within {secs} s ({d})"), + None => format!("no DevTools handshake within {secs} s"), + }); + } + if self.is_ready() + && self.last_history_refresh.elapsed() >= HISTORY_REFRESH + && self.shared.queue.len() < REFRESH_MAX_BACKLOG + { + self.last_history_refresh = Instant::now(); + let _ = self.shared.queue.push(proto::get_history(0)); + } + self.sync() + } + + fn health(&self) -> EngineHealth { + match &self.page.phase { + Phase::Starting => EngineHealth::Starting, + Phase::Ready => EngineHealth::Ready, + Phase::Crashed(why) => EngineHealth::Crashed(why.clone()), + Phase::Failed(why) => EngineHealth::Failed(why.clone()), + } + } + + fn can_go_back(&self) -> bool { + self.page.history_idx > 0 + } + + fn can_go_forward(&self) -> bool { + self.page.history_idx + 1 < self.page.history.len() + } + + fn nav_error(&self) -> Option<&str> { + self.page.nav_error.as_deref() + } + + fn process_info(&self) -> Option<(i32, PathBuf)> { + #[cfg(unix)] + return self.process.as_ref().map(|p| (p.pgid(), p.profile_dir().to_path_buf())); + #[cfg(not(unix))] + None } } diff --git a/src/pane/browser_canvas/cdp_input.rs b/src/pane/browser_canvas/cdp_input.rs new file mode 100644 index 00000000..362b9f7d --- /dev/null +++ b/src/pane/browser_canvas/cdp_input.rs @@ -0,0 +1,334 @@ +//! Terminal key and mouse events to CDP `Input.*` commands (`WEB-02`). + +use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers, MouseButton}; +use serde_json::{Map, Value, json}; + +use super::cdp_proto::{Class, Cmd, Scope, Tag, insert_text}; +use super::{MediaMouseEvent, MediaMouseEventKind}; + +/// CDP modifier bit field: Alt=1, Ctrl=2, Meta=4, Shift=8. +pub fn cdp_modifiers(m: KeyModifiers) -> u8 { + let mut bits = 0; + if m.contains(KeyModifiers::ALT) { + bits |= 1; + } + if m.contains(KeyModifiers::CONTROL) { + bits |= 2; + } + if m.intersects(KeyModifiers::SUPER | KeyModifiers::META) { + bits |= 4; + } + if m.contains(KeyModifiers::SHIFT) { + bits |= 8; + } + bits +} + +/// DOM `key`, DOM `code`, Windows virtual key code and text for one key. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct KeySpec { + pub key: String, + pub code: String, + pub vk: u32, + pub text: Option, +} + +fn named(key: &str, code: &str, vk: u32, text: Option<&str>) -> KeySpec { + KeySpec { key: key.to_string(), code: code.to_string(), vk, text: text.map(str::to_string) } +} + +/// `(code, virtual key)` for an ASCII character on a US layout. +fn us_layout(c: char) -> Option<(String, u32)> { + let lower = c.to_ascii_lowercase(); + if lower.is_ascii_lowercase() { + return Some((format!("Key{}", lower.to_ascii_uppercase()), lower.to_ascii_uppercase() as u32)); + } + if c.is_ascii_digit() { + return Some((format!("Digit{c}"), c as u32)); + } + let (code, vk) = match c { + ' ' => ("Space", 32), + ')' => ("Digit0", 48), + '!' => ("Digit1", 49), + '@' => ("Digit2", 50), + '#' => ("Digit3", 51), + '$' => ("Digit4", 52), + '%' => ("Digit5", 53), + '^' => ("Digit6", 54), + '&' => ("Digit7", 55), + '*' => ("Digit8", 56), + '(' => ("Digit9", 57), + ';' | ':' => ("Semicolon", 186), + '=' | '+' => ("Equal", 187), + ',' | '<' => ("Comma", 188), + '-' | '_' => ("Minus", 189), + '.' | '>' => ("Period", 190), + '/' | '?' => ("Slash", 191), + '`' | '~' => ("Backquote", 192), + '[' | '{' => ("BracketLeft", 219), + '\\' | '|' => ("Backslash", 220), + ']' | '}' => ("BracketRight", 221), + '\'' | '"' => ("Quote", 222), + _ => return None, + }; + Some((code.to_string(), vk)) +} + +/// Maps a terminal key to its DOM description. `None` for keys with no browser meaning. +pub fn key_spec(code: KeyCode, mods: KeyModifiers) -> Option { + let spec = match code { + KeyCode::Enter => named("Enter", "Enter", 13, Some("\r")), + KeyCode::Tab => named("Tab", "Tab", 9, None), + KeyCode::BackTab => named("Tab", "Tab", 9, None), + KeyCode::Backspace => named("Backspace", "Backspace", 8, None), + KeyCode::Delete => named("Delete", "Delete", 46, None), + KeyCode::Insert => named("Insert", "Insert", 45, None), + KeyCode::Esc => named("Escape", "Escape", 27, None), + KeyCode::Left => named("ArrowLeft", "ArrowLeft", 37, None), + KeyCode::Up => named("ArrowUp", "ArrowUp", 38, None), + KeyCode::Right => named("ArrowRight", "ArrowRight", 39, None), + KeyCode::Down => named("ArrowDown", "ArrowDown", 40, None), + KeyCode::Home => named("Home", "Home", 36, None), + KeyCode::End => named("End", "End", 35, None), + KeyCode::PageUp => named("PageUp", "PageUp", 33, None), + KeyCode::PageDown => named("PageDown", "PageDown", 34, None), + KeyCode::F(n @ 1..=12) => named(&format!("F{n}"), &format!("F{n}"), 111 + u32::from(n), None), + KeyCode::Char(c) => { + let (dom_code, vk) = us_layout(c)?; + let chord = mods.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT | KeyModifiers::SUPER | KeyModifiers::META); + KeySpec { key: c.to_string(), code: dom_code, vk, text: (!chord).then(|| c.to_string()) } + } + _ => return None, + }; + Some(spec) +} + +/// Editing commands Chrome expects alongside Ctrl chords (headless has no native key bindings). +fn editing_command(code: KeyCode, mods: KeyModifiers) -> Option<&'static str> { + if !mods.contains(KeyModifiers::CONTROL) || mods.contains(KeyModifiers::ALT) { + return None; + } + match code { + KeyCode::Char('a') => Some("selectAll"), + KeyCode::Char('c') => Some("copy"), + KeyCode::Char('v') => Some("paste"), + KeyCode::Char('z') => Some("undo"), + KeyCode::Char('y') => Some("redo"), + _ => None, + } +} + +fn key_cmd(kind: &str, spec: &KeySpec, modifiers: u8, with_text: bool, command: Option<&str>) -> Cmd { + let mut p = Map::new(); + p.insert("type".into(), json!(kind)); + p.insert("key".into(), json!(spec.key)); + p.insert("code".into(), json!(spec.code)); + p.insert("windowsVirtualKeyCode".into(), json!(spec.vk)); + p.insert("nativeVirtualKeyCode".into(), json!(spec.vk)); + p.insert("modifiers".into(), json!(modifiers)); + if with_text && let Some(t) = &spec.text { + p.insert("text".into(), json!(t)); + p.insert("unmodifiedText".into(), json!(t)); + } + if let Some(c) = command { + p.insert("commands".into(), json!([c])); + } + Cmd { method: "Input.dispatchKeyEvent", params: Value::Object(p), scope: Scope::Page, tag: Tag::None, class: Class::Normal } +} + +/// Commands for one terminal key press: `keyDown` (with text when printable) then `keyUp`. +/// +/// Key releases produce nothing (the press already sent both halves). Characters outside the US +/// layout table (accents, CJK, emoji) are sent with `Input.insertText`. +pub fn key_commands(key: KeyEvent) -> Vec { + if key.kind == KeyEventKind::Release { + return Vec::new(); + } + let mut mods = key.modifiers; + if key.code == KeyCode::BackTab { + mods |= KeyModifiers::SHIFT; + } + let bits = cdp_modifiers(mods); + let Some(spec) = key_spec(key.code, mods) else { + return match key.code { + KeyCode::Char(c) if !mods.intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) => { + vec![insert_text(&c.to_string())] + } + _ => Vec::new(), + }; + }; + let down_kind = if spec.text.is_some() { "keyDown" } else { "rawKeyDown" }; + vec![ + key_cmd(down_kind, &spec, bits, true, editing_command(key.code, mods)), + key_cmd("keyUp", &spec, bits, false, None), + ] +} + +fn button_name(b: MouseButton) -> (&'static str, u8) { + match b { + MouseButton::Left => ("left", 1), + MouseButton::Right => ("right", 2), + MouseButton::Middle => ("middle", 4), + } +} + +/// Builds `Input.dispatchMouseEvent` for a mouse event at CSS pixel `(x, y)`. +pub fn mouse_command(event: &MediaMouseEvent, x_css: f64, y_css: f64, click_count: u8) -> Cmd { + let mut p = Map::new(); + p.insert("x".into(), json!(x_css)); + p.insert("y".into(), json!(y_css)); + p.insert("modifiers".into(), json!(cdp_modifiers(event.modifiers))); + let mut class = Class::Normal; + let kind = match event.kind { + MediaMouseEventKind::Down(b) | MediaMouseEventKind::Up(b) => { + let (name, mask) = button_name(b); + let down = matches!(event.kind, MediaMouseEventKind::Down(_)); + p.insert("button".into(), json!(name)); + p.insert("buttons".into(), json!(if down { mask } else { 0 })); + p.insert("clickCount".into(), json!(click_count.max(1))); + if down { "mousePressed" } else { "mouseReleased" } + } + MediaMouseEventKind::Drag(b) => { + let (name, mask) = button_name(b); + p.insert("button".into(), json!(name)); + p.insert("buttons".into(), json!(mask)); + class = Class::MouseMove; + "mouseMoved" + } + MediaMouseEventKind::Move => { + p.insert("button".into(), json!("none")); + p.insert("buttons".into(), json!(0)); + class = Class::MouseMove; + "mouseMoved" + } + MediaMouseEventKind::Wheel { delta_x, delta_y } => { + p.insert("deltaX".into(), json!(delta_x)); + p.insert("deltaY".into(), json!(delta_y)); + "mouseWheel" + } + }; + p.insert("type".into(), json!(kind)); + Cmd { method: "Input.dispatchMouseEvent", params: Value::Object(p), scope: Scope::Page, tag: Tag::None, class } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn press(code: KeyCode, mods: KeyModifiers) -> Vec { + key_commands(KeyEvent::new(code, mods)) + } + + #[test] + fn printable_letter_sends_keydown_with_text_then_keyup() { + let cmds = press(KeyCode::Char('a'), KeyModifiers::NONE); + assert_eq!(cmds.len(), 2); + let d = &cmds[0].params; + assert_eq!(d["type"], "keyDown"); + assert_eq!(d["key"], "a"); + assert_eq!(d["code"], "KeyA"); + assert_eq!(d["windowsVirtualKeyCode"], 65); + assert_eq!(d["text"], "a"); + assert_eq!(d["modifiers"], 0); + assert_eq!(cmds[1].params["type"], "keyUp"); + assert!(cmds[1].params.get("text").is_none()); + } + + #[test] + fn shifted_and_punctuation_keys_map_to_us_layout() { + let up = press(KeyCode::Char('A'), KeyModifiers::SHIFT); + assert_eq!(up[0].params["key"], "A"); + assert_eq!(up[0].params["code"], "KeyA"); + assert_eq!(up[0].params["modifiers"], 8); + assert_eq!(up[0].params["text"], "A"); + let q = press(KeyCode::Char('?'), KeyModifiers::SHIFT); + assert_eq!(q[0].params["code"], "Slash"); + assert_eq!(q[0].params["windowsVirtualKeyCode"], 191); + let sp = press(KeyCode::Char(' '), KeyModifiers::NONE); + assert_eq!(sp[0].params["code"], "Space"); + assert_eq!(sp[0].params["text"], " "); + let d7 = press(KeyCode::Char('7'), KeyModifiers::NONE); + assert_eq!(d7[0].params["code"], "Digit7"); + assert_eq!(d7[0].params["windowsVirtualKeyCode"], 55); + } + + #[test] + fn special_keys_use_raw_key_down_except_enter() { + let enter = press(KeyCode::Enter, KeyModifiers::NONE); + assert_eq!(enter[0].params["type"], "keyDown"); + assert_eq!(enter[0].params["key"], "Enter"); + assert_eq!(enter[0].params["windowsVirtualKeyCode"], 13); + assert_eq!(enter[0].params["text"], "\r"); + for (code, key, vk) in [ + (KeyCode::Backspace, "Backspace", 8), + (KeyCode::Tab, "Tab", 9), + (KeyCode::Esc, "Escape", 27), + (KeyCode::Left, "ArrowLeft", 37), + (KeyCode::Down, "ArrowDown", 40), + (KeyCode::PageDown, "PageDown", 34), + (KeyCode::Delete, "Delete", 46), + (KeyCode::F(5), "F5", 116), + ] { + let c = press(code, KeyModifiers::NONE); + assert_eq!(c[0].params["type"], "rawKeyDown", "{key}"); + assert_eq!(c[0].params["key"], key); + assert_eq!(c[0].params["windowsVirtualKeyCode"], vk); + assert!(c[0].params.get("text").is_none()); + } + let bt = press(KeyCode::BackTab, KeyModifiers::NONE); + assert_eq!(bt[0].params["key"], "Tab"); + assert_eq!(bt[0].params["modifiers"], 8, "BackTab is Shift+Tab"); + } + + #[test] + fn ctrl_chords_have_no_text_and_carry_editing_commands() { + let a = press(KeyCode::Char('a'), KeyModifiers::CONTROL); + assert_eq!(a[0].params["type"], "rawKeyDown"); + assert_eq!(a[0].params["modifiers"], 2); + assert!(a[0].params.get("text").is_none()); + assert_eq!(a[0].params["commands"], json!(["selectAll"])); + let k = press(KeyCode::Char('k'), KeyModifiers::CONTROL | KeyModifiers::ALT); + assert_eq!(k[0].params["modifiers"], 3); + assert!(k[0].params.get("commands").is_none()); + } + + #[test] + fn non_ascii_text_uses_insert_text_and_releases_are_dropped() { + let e = press(KeyCode::Char('é'), KeyModifiers::NONE); + assert_eq!(e.len(), 1); + assert_eq!(e[0].method, "Input.insertText"); + assert_eq!(e[0].params["text"], "é"); + assert!(press(KeyCode::Char('é'), KeyModifiers::CONTROL).is_empty()); + let mut rel = KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE); + rel.kind = KeyEventKind::Release; + assert!(key_commands(rel).is_empty()); + assert!(press(KeyCode::CapsLock, KeyModifiers::NONE).is_empty()); + } + + #[test] + fn mouse_events_map_to_dispatch_mouse_event() { + let ev = |kind| MediaMouseEvent { kind, x_px: 0, y_px: 0, modifiers: KeyModifiers::SHIFT }; + let down = mouse_command(&ev(MediaMouseEventKind::Down(MouseButton::Left)), 12.5, 40.0, 2); + assert_eq!(down.method, "Input.dispatchMouseEvent"); + assert_eq!(down.params["type"], "mousePressed"); + assert_eq!(down.params["button"], "left"); + assert_eq!(down.params["buttons"], 1); + assert_eq!(down.params["clickCount"], 2); + assert_eq!(down.params["x"], 12.5); + assert_eq!(down.params["modifiers"], 8); + let up = mouse_command(&ev(MediaMouseEventKind::Up(MouseButton::Right)), 1.0, 2.0, 1); + assert_eq!(up.params["type"], "mouseReleased"); + assert_eq!(up.params["button"], "right"); + assert_eq!(up.params["buttons"], 0); + let mv = mouse_command(&ev(MediaMouseEventKind::Move), 1.0, 2.0, 0); + assert_eq!(mv.params["type"], "mouseMoved"); + assert_eq!(mv.class, Class::MouseMove); + let drag = mouse_command(&ev(MediaMouseEventKind::Drag(MouseButton::Middle)), 1.0, 2.0, 0); + assert_eq!(drag.params["buttons"], 4); + assert_eq!(drag.class, Class::MouseMove); + let wheel = mouse_command(&ev(MediaMouseEventKind::Wheel { delta_x: 0, delta_y: -53 }), 5.0, 6.0, 0); + assert_eq!(wheel.params["type"], "mouseWheel"); + assert_eq!(wheel.params["deltaY"], -53); + assert_eq!(wheel.class, Class::Normal); + } +} diff --git a/src/pane/browser_canvas/cdp_launch.rs b/src/pane/browser_canvas/cdp_launch.rs new file mode 100644 index 00000000..a72fc287 --- /dev/null +++ b/src/pane/browser_canvas/cdp_launch.rs @@ -0,0 +1,283 @@ +//! Launches headless Chrome with `--remote-debugging-pipe` and owns its lifetime (`WEB-01`). +//! +//! Chrome reads commands on fd 3 and writes events on fd 4. The child runs in its own process +//! group so one `SIGKILL` to the group takes down the renderer, GPU and zygote processes too. The +//! profile lives in a per-process, per-pane directory inside the private runtime dir and is removed +//! when the browser is dropped. Child-process pipes, not terminal traffic. + +use std::fs::File; +use std::io; +use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd}; +use std::os::unix::process::CommandExt; +use std::path::{Path, PathBuf}; +use std::process::{Child, Command, Stdio}; + +use crate::pane::PaneId; +use crate::secure_fs::{private_dir_in, private_runtime_dir}; + +const PROFILE_PREFIX: &str = "profile-"; +const STDERR_LOG: &str = "chrome-stderr.log"; + +/// A running Chrome and the directory it owns. Dropping it kills the process group. +pub struct ChromeProcess { + child: Option, + pgid: i32, + profile_dir: PathBuf, +} + +/// Pipe ends connected to Chrome: read events from `events`, write commands to `commands`. +pub struct ChromePipes { + pub events: File, + pub commands: File, +} + +fn random_u64() -> u64 { + use std::hash::{BuildHasher, Hasher}; + let mut h = std::collections::hash_map::RandomState::new().build_hasher(); + h.write_u128(std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_nanos())); + h.finish() +} + +/// Directory that holds all browser profiles of this user (`/browser`, mode 0700). +pub fn profiles_root() -> io::Result { + private_dir_in(&private_runtime_dir()?, "browser") +} + +/// Creates a fresh private profile directory unique to this process and pane. +pub fn create_profile_dir(root: &Path, pane_id: PaneId) -> io::Result { + let name = format!("{PROFILE_PREFIX}{}-{pane_id}-{:016x}", std::process::id(), random_u64()); + let dir = root.join(&name); + if dir.exists() { + return Err(io::Error::new(io::ErrorKind::AlreadyExists, "browser profile dir already exists")); + } + private_dir_in(root, &name) +} + +/// Owning pid encoded in a profile directory name, if it is one of ours. +pub fn profile_owner_pid(name: &str) -> Option { + name.strip_prefix(PROFILE_PREFIX)?.split('-').next()?.parse().ok() +} + +fn pid_is_alive(pid: i32) -> bool { + // SAFETY: signal 0 only checks for existence. + pid > 0 && (unsafe { libc::kill(pid, 0) } == 0 || io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)) +} + +/// Removes profile directories left behind by bilterm processes that no longer exist. +pub fn sweep_stale_profiles(root: &Path) -> usize { + let Ok(entries) = std::fs::read_dir(root) else { return 0 }; + let mut removed = 0; + for entry in entries.flatten() { + let name = entry.file_name(); + let Some(pid) = name.to_str().and_then(profile_owner_pid) else { continue }; + let is_dir = entry.file_type().is_ok_and(|t| t.is_dir()); + if is_dir && !pid_is_alive(pid) && std::fs::remove_dir_all(entry.path()).is_ok() { + removed += 1; + } + } + removed +} + +/// Duplicates `fd` to a descriptor >= 10 (close-on-exec) so it can never collide with fd 3 or 4. +fn dup_high(fd: RawFd) -> io::Result { + // SAFETY: plain fcntl on a descriptor we own; the result is a new owned descriptor. + let new = unsafe { libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, 10) }; + if new < 0 { + return Err(io::Error::last_os_error()); + } + // SAFETY: `new` was just returned by fcntl and is owned by nobody else. + Ok(unsafe { OwnedFd::from_raw_fd(new) }) +} + +/// Chrome arguments. The page URL is never passed on argv: navigation goes through CDP after +/// validation. The sandbox stays on (no `--no-sandbox`). +pub fn chrome_args(profile_dir: &Path, width_px: u32, height_px: u32) -> Vec { + vec![ + "--headless=new".to_string(), + "--remote-debugging-pipe".to_string(), + format!("--user-data-dir={}", profile_dir.to_string_lossy()), + format!("--window-size={width_px},{height_px}"), + "--hide-scrollbars".to_string(), + "--mute-audio".to_string(), + "--no-first-run".to_string(), + "--no-default-browser-check".to_string(), + "--disable-background-networking".to_string(), + "--disable-component-update".to_string(), + "--disable-sync".to_string(), + "--disable-breakpad".to_string(), + "--disable-default-apps".to_string(), + "--password-store=basic".to_string(), + "about:blank".to_string(), + ] +} + +/// Launches `bin` with a new profile under `root`. Profile creation failure aborts the launch. +pub fn launch(bin: &Path, root: &Path, pane_id: PaneId, width_px: u32, height_px: u32) -> io::Result<(ChromeProcess, ChromePipes)> { + let profile_dir = create_profile_dir(root, pane_id)?; + let result = spawn_chrome(bin, &profile_dir, width_px, height_px); + if result.is_err() { + let _ = std::fs::remove_dir_all(&profile_dir); + } + let (child, pipes) = result?; + let pgid = child.id() as i32; + Ok((ChromeProcess { child: Some(child), pgid, profile_dir }, pipes)) +} + +fn spawn_chrome(bin: &Path, profile_dir: &Path, width_px: u32, height_px: u32) -> io::Result<(Child, ChromePipes)> { + // commands: we write, Chrome reads on fd 3. events: Chrome writes on fd 4, we read. + let (cmd_read, cmd_write) = io::pipe()?; + let (evt_read, evt_write) = io::pipe()?; + let child_in = dup_high(cmd_read.as_raw_fd())?; + let child_out = dup_high(evt_write.as_raw_fd())?; + drop((cmd_read, evt_write)); + + let stderr = crate::secure_fs::create_private_file(&profile_dir.join(STDERR_LOG)).map_or_else(|_| Stdio::null(), Stdio::from); + let mut cmd = Command::new(bin); + cmd.args(chrome_args(profile_dir, width_px, height_px)) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(stderr) + .process_group(0); + let (fd_in, fd_out) = (child_in.as_raw_fd(), child_out.as_raw_fd()); + // SAFETY: only async-signal-safe dup2 runs between fork and exec; both source descriptors are + // >= 10, so neither dup2 can clobber the other's source. dup2 clears close-on-exec on 3 and 4. + unsafe { + cmd.pre_exec(move || { + if libc::dup2(fd_in, 3) < 0 || libc::dup2(fd_out, 4) < 0 { + return Err(io::Error::last_os_error()); + } + Ok(()) + }); + } + let child = cmd.spawn()?; + drop((child_in, child_out)); + Ok((child, ChromePipes { events: File::from(OwnedFd::from(evt_read)), commands: File::from(OwnedFd::from(cmd_write)) })) +} + +impl ChromeProcess { + pub fn pgid(&self) -> i32 { + self.pgid + } + + pub fn profile_dir(&self) -> &Path { + &self.profile_dir + } + + /// Last line Chrome wrote to stderr (bounded), for launch-failure messages. + pub fn stderr_tail(&self) -> Option { + use std::io::{Read, Seek, SeekFrom}; + let mut f = File::open(self.profile_dir.join(STDERR_LOG)).ok()?; + let len = f.metadata().ok()?.len(); + f.seek(SeekFrom::Start(len.saturating_sub(2048))).ok()?; + let mut buf = Vec::new(); + f.take(2048).read_to_end(&mut buf).ok()?; + let text = String::from_utf8_lossy(&buf); + let line = text.lines().rev().find(|l| !l.trim().is_empty())?.trim(); + Some(line.chars().filter(|c| !c.is_control()).take(160).collect()) + } + + /// Sends `SIGKILL` to the whole process group. Safe to call more than once. + pub fn kill_group(&self) { + if self.child.is_some() && self.pgid > 1 { + // SAFETY: the group id is our own child's pid (it leads its group); the child has not + // been reaped yet, so the id cannot have been reused. + unsafe { libc::kill(-self.pgid, libc::SIGKILL) }; + } + } +} + +impl Drop for ChromeProcess { + fn drop(&mut self) { + self.kill_group(); + let Some(mut child) = self.child.take() else { return }; + let dir = std::mem::take(&mut self.profile_dir); + // Reap and delete off the calling (UI) thread: wait() and remove_dir_all can take a while. + // The group is not signalled again after the reap: its id could have been reused by then. + let reap = move || { + let _ = child.wait(); + for _ in 0..50 { + if std::fs::remove_dir_all(&dir).is_ok() || !dir.exists() { + break; + } + std::thread::sleep(std::time::Duration::from_millis(20)); + } + }; + if let Err(e) = std::thread::Builder::new().name("bilterm-chrome-reaper".into()).spawn(reap) { + tracing::warn!("browser: could not spawn reaper thread: {e}"); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn profile_dirs_are_unique_private_and_carry_the_pid() { + let tmp = tempfile::tempdir().unwrap(); + let a = create_profile_dir(tmp.path(), 7).unwrap(); + let b = create_profile_dir(tmp.path(), 7).unwrap(); + assert_ne!(a, b); + let name = a.file_name().unwrap().to_str().unwrap(); + assert_eq!(profile_owner_pid(name), Some(std::process::id() as i32)); + use std::os::unix::fs::PermissionsExt; + assert_eq!(std::fs::metadata(&a).unwrap().permissions().mode() & 0o777, 0o700); + } + + #[test] + fn profile_creation_failure_aborts_the_launch() { + let missing = Path::new("/nonexistent-bilterm-browser-root"); + assert!(create_profile_dir(missing, 1).is_err()); + assert!(launch(Path::new("/bin/true"), missing, 1, 800, 600).is_err()); + } + + #[test] + fn sweep_removes_only_profiles_of_dead_processes() { + let tmp = tempfile::tempdir().unwrap(); + let live = create_profile_dir(tmp.path(), 1).unwrap(); + // pid_max is at most 2^22 on Linux, so this pid cannot exist. + let dead = tmp.path().join(format!("{PROFILE_PREFIX}2000000000-1-00")); + std::fs::create_dir(&dead).unwrap(); + let other = tmp.path().join("unrelated"); + std::fs::create_dir(&other).unwrap(); + assert_eq!(sweep_stale_profiles(tmp.path()), 1); + assert!(live.exists() && other.exists() && !dead.exists()); + assert_eq!(profile_owner_pid("unrelated"), None); + } + + #[test] + fn args_keep_the_sandbox_and_never_carry_a_page_url() { + let args = chrome_args(Path::new("/run/x/profile"), 800, 460); + assert!(args.iter().any(|a| a == "--remote-debugging-pipe")); + assert!(args.iter().any(|a| a == "--headless=new")); + assert!(!args.iter().any(|a| a.contains("no-sandbox") || a.contains("remote-debugging-port"))); + assert_eq!(args.last().map(String::as_str), Some("about:blank")); + } + + #[test] + fn pipes_reach_the_child_on_fd_3_and_4_and_drop_kills_the_group() { + // `sh` stands in for Chrome: it echoes fd 3 to fd 4, proving the descriptor plumbing. + let tmp = tempfile::tempdir().unwrap(); + let script = tmp.path().join("fake-chrome.sh"); + std::fs::write(&script, "#!/bin/sh\nread line <&3\nprintf '%s!' \"$line\" >&4\nsleep 60\n").unwrap(); + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o700)).unwrap(); + let (proc_, mut pipes) = launch(&script, tmp.path(), 3, 800, 600).unwrap(); + use std::io::{Read, Write}; + pipes.commands.write_all(b"ping\n").unwrap(); + let mut got = [0u8; 5]; + pipes.events.read_exact(&mut got).unwrap(); + assert_eq!(&got, b"ping!"); + let (pgid, dir) = (proc_.pgid(), proc_.profile_dir().to_path_buf()); + assert!(dir.exists()); + drop(proc_); + let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); + // SAFETY: signal 0 only probes the group. + while (unsafe { libc::kill(-pgid, 0) } == 0 || dir.exists()) && std::time::Instant::now() < deadline { + std::thread::sleep(std::time::Duration::from_millis(20)); + } + assert!(!dir.exists(), "profile dir removed after drop"); + // SAFETY: as above. + assert_ne!(unsafe { libc::kill(-pgid, 0) }, 0, "process group is gone"); + } +} diff --git a/src/pane/browser_canvas/cdp_proto.rs b/src/pane/browser_canvas/cdp_proto.rs new file mode 100644 index 00000000..3f8320a3 --- /dev/null +++ b/src/pane/browser_canvas/cdp_proto.rs @@ -0,0 +1,497 @@ +//! Chrome DevTools Protocol wire format for `--remote-debugging-pipe`: NUL-terminated JSON +//! messages, command builders and event parsing (`WEB-01..02`). +//! +//! Pure data code: no I/O, no threads. Nothing here trusts Chrome: every field is optional and a +//! malformed message is an error value, never a panic. + +use std::fmt; + +use serde_json::{Map, Value, json}; + +/// Largest single CDP message accepted from Chrome. A bigger one is a fatal backend error. +pub const MAX_MESSAGE_BYTES: usize = 32 * 1024 * 1024; + +/// Fatal protocol error raised by the framer or the parser. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ProtoError { + Oversize(usize), + BadJson(String), + NotAnObject, +} + +impl fmt::Display for ProtoError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Oversize(n) => write!(f, "CDP message exceeds {n} bytes"), + Self::BadJson(e) => write!(f, "CDP message is not JSON: {e}"), + Self::NotAnObject => write!(f, "CDP message is not a JSON object"), + } + } +} + +/// Splits a byte stream into NUL-terminated messages with a size cap. +pub struct Framer { + buf: Vec, + max: usize, +} + +impl Default for Framer { + fn default() -> Self { + Self::new(MAX_MESSAGE_BYTES) + } +} + +impl Framer { + pub fn new(max: usize) -> Self { + Self { buf: Vec::new(), max } + } + + /// Feeds `chunk`; complete messages (without the NUL) are appended to `out`. + pub fn push(&mut self, chunk: &[u8], out: &mut Vec>) -> Result<(), ProtoError> { + let mut rest = chunk; + while let Some(pos) = rest.iter().position(|&b| b == 0) { + if self.buf.len() + pos > self.max { + return Err(ProtoError::Oversize(self.max)); + } + self.buf.extend_from_slice(&rest[..pos]); + if !self.buf.is_empty() { + out.push(std::mem::take(&mut self.buf)); + } + rest = &rest[pos + 1..]; + } + if self.buf.len() + rest.len() > self.max { + return Err(ProtoError::Oversize(self.max)); + } + self.buf.extend_from_slice(rest); + Ok(()) + } + + /// Bytes buffered for an unfinished message. + pub fn pending(&self) -> usize { + self.buf.len() + } +} + +/// Which CDP session a command is sent on. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Scope { + Browser, + Page, +} + +/// What to do when the response to a command arrives. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Tag { + None, + GetTargets, + CreateTarget, + Attach, + Navigate, + /// `Page.getNavigationHistory`; the offset is `-1` back, `+1` forward, `0` refresh only. + History(i8), +} + +/// Queue class: decides coalescing and what may be dropped under pressure. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Class { + Normal, + MouseMove, + Viewport, + /// Protocol bookkeeping (acks, handshake). Never dropped. + Internal, +} + +/// One outgoing CDP command, before an id is assigned. +#[derive(Clone, Debug, PartialEq)] +pub struct Cmd { + pub method: &'static str, + pub params: Value, + pub scope: Scope, + pub tag: Tag, + pub class: Class, +} + +impl Cmd { + fn page(method: &'static str, params: Value) -> Self { + Self { method, params, scope: Scope::Page, tag: Tag::None, class: Class::Normal } + } + + fn browser(method: &'static str, params: Value) -> Self { + Self { method, params, scope: Scope::Browser, tag: Tag::None, class: Class::Internal } + } + + fn tagged(mut self, tag: Tag) -> Self { + self.tag = tag; + self + } + + pub fn with_class(mut self, class: Class) -> Self { + self.class = class; + self + } + + /// Serializes to the wire form: one JSON object followed by a NUL byte. + pub fn encode(&self, id: u64, session: Option<&str>) -> Vec { + let mut obj = Map::new(); + obj.insert("id".into(), json!(id)); + obj.insert("method".into(), json!(self.method)); + obj.insert("params".into(), self.params.clone()); + if self.scope == Scope::Page + && let Some(s) = session + { + obj.insert("sessionId".into(), json!(s)); + } + let mut bytes = serde_json::to_vec(&Value::Object(obj)).unwrap_or_default(); + bytes.push(0); + bytes + } +} + +pub fn get_targets() -> Cmd { + Cmd::browser("Target.getTargets", json!({})).tagged(Tag::GetTargets) +} + +pub fn create_target() -> Cmd { + Cmd::browser("Target.createTarget", json!({ "url": "about:blank" })).tagged(Tag::CreateTarget) +} + +pub fn attach_to_target(target_id: &str) -> Cmd { + Cmd::browser("Target.attachToTarget", json!({ "targetId": target_id, "flatten": true })).tagged(Tag::Attach) +} + +pub fn discover_targets() -> Cmd { + Cmd::browser("Target.setDiscoverTargets", json!({ "discover": true })) +} + +pub fn close_target(target_id: &str) -> Cmd { + Cmd::browser("Target.closeTarget", json!({ "targetId": target_id })) +} + +pub fn deny_downloads() -> Cmd { + Cmd::browser("Browser.setDownloadBehavior", json!({ "behavior": "deny" })) +} + +pub fn page_enable() -> Cmd { + Cmd::page("Page.enable", json!({})).with_class(Class::Internal) +} + +pub fn navigate(url: &str) -> Cmd { + Cmd::page("Page.navigate", json!({ "url": url })).tagged(Tag::Navigate) +} + +pub fn reload() -> Cmd { + Cmd::page("Page.reload", json!({})) +} + +pub fn stop_loading() -> Cmd { + Cmd::page("Page.stopLoading", json!({})) +} + +pub fn get_history(offset: i8) -> Cmd { + let class = if offset == 0 { Class::Internal } else { Class::Normal }; + Cmd::page("Page.getNavigationHistory", json!({})).tagged(Tag::History(offset)).with_class(class) +} + +pub fn navigate_to_history_entry(entry_id: i64) -> Cmd { + Cmd::page("Page.navigateToHistoryEntry", json!({ "entryId": entry_id })).with_class(Class::Internal) +} + +pub fn handle_dialog(accept: bool) -> Cmd { + Cmd::page("Page.handleJavaScriptDialog", json!({ "accept": accept })).with_class(Class::Internal) +} + +/// `width`/`height` are CSS pixels; the screencast frame is `scale` times larger. +pub fn set_device_metrics(css_width: u32, css_height: u32, scale: f64) -> Cmd { + Cmd::page( + "Emulation.setDeviceMetricsOverride", + json!({ "width": css_width, "height": css_height, "deviceScaleFactor": scale, "mobile": false }), + ) + .with_class(Class::Viewport) +} + +pub fn start_screencast(max_width: u32, max_height: u32, quality: u8) -> Cmd { + Cmd::page( + "Page.startScreencast", + json!({ "format": "jpeg", "quality": quality, "maxWidth": max_width, "maxHeight": max_height, "everyNthFrame": 1 }), + ) + .with_class(Class::Viewport) +} + +/// Chrome refuses `Page.startScreencast` while one is active, so a resize stops it first. +pub fn stop_screencast() -> Cmd { + Cmd::page("Page.stopScreencast", json!({})).with_class(Class::Viewport) +} + +pub fn screencast_ack(ack_id: i64) -> Cmd { + Cmd::page("Page.screencastFrameAck", json!({ "sessionId": ack_id })).with_class(Class::Internal) +} + +pub fn insert_text(text: &str) -> Cmd { + Cmd::page("Input.insertText", json!({ "text": text })) +} + +/// One parsed message from Chrome. +#[derive(Clone, Debug, PartialEq)] +pub enum Incoming { + Response { id: u64, result: Value, error: Option }, + Event(Event), + /// A well-formed message this client does not use. + Ignored, +} + +/// One entry of `Page.getNavigationHistory`. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HistoryEntry { + pub id: i64, + pub url: String, + pub title: String, +} + +/// CDP events this client reacts to. +#[derive(Clone, Debug, PartialEq)] +pub enum Event { + FrameNavigated { frame_id: String, is_main: bool, url: String }, + NavigatedWithinDocument { frame_id: String, url: String }, + LoadingStarted { frame_id: String }, + LoadingStopped { frame_id: String }, + LoadEventFired, + TargetCreated { target_id: String, kind: String, opener: Option, url: String }, + TargetInfoChanged { target_id: String, title: String, url: String }, + ScreencastFrame { data: String, ack_id: i64, device_width: f64, device_height: f64 }, + DialogOpening { kind: String }, + TargetCrashed, + Detached, +} + +fn take_str(map: &mut Map, key: &str) -> String { + match map.remove(key) { + Some(Value::String(s)) => s, + _ => String::new(), + } +} + +fn take_obj(map: &mut Map, key: &str) -> Map { + match map.remove(key) { + Some(Value::Object(m)) => m, + _ => Map::new(), + } +} + +/// Parses one framed message. Unknown methods are [`Incoming::Ignored`]; non-JSON is an error. +pub fn parse_incoming(bytes: &[u8]) -> Result { + let value: Value = serde_json::from_slice(bytes).map_err(|e| ProtoError::BadJson(e.to_string()))?; + let Value::Object(mut msg) = value else { + return Err(ProtoError::NotAnObject); + }; + if let Some(id) = msg.get("id").and_then(Value::as_u64) { + let error = msg.get("error").map(|e| { + e.get("message").and_then(Value::as_str).map_or_else(|| e.to_string(), str::to_string) + }); + let result = msg.remove("result").unwrap_or(Value::Null); + return Ok(Incoming::Response { id, result, error }); + } + let method = take_str(&mut msg, "method"); + let mut p = take_obj(&mut msg, "params"); + let event = match method.as_str() { + "Page.frameNavigated" => { + let mut frame = take_obj(&mut p, "frame"); + let is_main = !frame.contains_key("parentId"); + Event::FrameNavigated { frame_id: take_str(&mut frame, "id"), is_main, url: take_str(&mut frame, "url") } + } + "Page.navigatedWithinDocument" => { + Event::NavigatedWithinDocument { frame_id: take_str(&mut p, "frameId"), url: take_str(&mut p, "url") } + } + "Page.frameStartedLoading" => Event::LoadingStarted { frame_id: take_str(&mut p, "frameId") }, + "Page.frameStoppedLoading" => Event::LoadingStopped { frame_id: take_str(&mut p, "frameId") }, + "Page.loadEventFired" => Event::LoadEventFired, + "Target.targetCreated" | "Target.targetInfoChanged" => { + let mut info = take_obj(&mut p, "targetInfo"); + let target_id = take_str(&mut info, "targetId"); + let url = take_str(&mut info, "url"); + if method == "Target.targetCreated" { + let opener = match info.remove("openerId") { + Some(Value::String(s)) => Some(s), + _ => None, + }; + Event::TargetCreated { target_id, kind: take_str(&mut info, "type"), opener, url } + } else { + Event::TargetInfoChanged { target_id, title: take_str(&mut info, "title"), url } + } + } + "Page.screencastFrame" => { + let meta = take_obj(&mut p, "metadata"); + Event::ScreencastFrame { + ack_id: p.get("sessionId").and_then(Value::as_i64).unwrap_or(0), + data: take_str(&mut p, "data"), + device_width: meta.get("deviceWidth").and_then(Value::as_f64).unwrap_or(0.0), + device_height: meta.get("deviceHeight").and_then(Value::as_f64).unwrap_or(0.0), + } + } + "Page.javascriptDialogOpening" => Event::DialogOpening { kind: take_str(&mut p, "type") }, + "Inspector.targetCrashed" | "Target.targetCrashed" => Event::TargetCrashed, + "Inspector.detached" | "Target.detachedFromTarget" => Event::Detached, + _ => return Ok(Incoming::Ignored), + }; + Ok(Incoming::Event(event)) +} + +/// Picks the first `page` target id out of a `Target.getTargets` result. +pub fn first_page_target(result: &Value) -> Option { + result + .get("targetInfos")? + .as_array()? + .iter() + .find(|t| t.get("type").and_then(Value::as_str) == Some("page")) + .and_then(|t| t.get("targetId").and_then(Value::as_str)) + .map(str::to_string) +} + +/// Parses a `Page.getNavigationHistory` result into `(current index, entries)`. +pub fn parse_history(result: &Value) -> Option<(usize, Vec)> { + let idx = usize::try_from(result.get("currentIndex")?.as_i64()?).ok()?; + let entries: Vec = result + .get("entries")? + .as_array()? + .iter() + .filter_map(|e| { + Some(HistoryEntry { + id: e.get("id")?.as_i64()?, + url: e.get("url").and_then(Value::as_str).unwrap_or("").to_string(), + title: e.get("title").and_then(Value::as_str).unwrap_or("").to_string(), + }) + }) + .collect(); + (idx < entries.len()).then_some((idx, entries)) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn framer_splits_nul_terminated_messages_across_chunks() { + let mut f = Framer::default(); + let mut out = Vec::new(); + f.push(b"{\"a\":1}\0{\"b\"", &mut out).unwrap(); + assert_eq!(out, vec![b"{\"a\":1}".to_vec()]); + assert_eq!(f.pending(), 4); + f.push(b":2}\0\0{\"c\":3}\0", &mut out).unwrap(); + assert_eq!(out.len(), 3, "empty message between two NULs is skipped"); + assert_eq!(out[1], b"{\"b\":2}"); + assert_eq!(out[2], b"{\"c\":3}"); + assert_eq!(f.pending(), 0); + } + + #[test] + fn framer_rejects_oversize_messages() { + let mut f = Framer::new(8); + let mut out = Vec::new(); + f.push(b"12345", &mut out).unwrap(); + assert_eq!(f.push(b"6789", &mut out), Err(ProtoError::Oversize(8))); + let mut g = Framer::new(8); + assert_eq!(g.push(b"123456789\0", &mut out), Err(ProtoError::Oversize(8))); + assert!(out.is_empty()); + } + + #[test] + fn commands_encode_with_id_session_and_nul() { + let bytes = navigate("https://example.com/").encode(7, Some("S1")); + assert_eq!(bytes.last(), Some(&0)); + let v: Value = serde_json::from_slice(&bytes[..bytes.len() - 1]).unwrap(); + assert_eq!(v["id"], 7); + assert_eq!(v["method"], "Page.navigate"); + assert_eq!(v["params"]["url"], "https://example.com/"); + assert_eq!(v["sessionId"], "S1"); + + let b = get_targets().encode(1, Some("S1")); + let v: Value = serde_json::from_slice(&b[..b.len() - 1]).unwrap(); + assert!(v.get("sessionId").is_none(), "browser-scope commands carry no session"); + assert!(!b[..b.len() - 1].contains(&0), "JSON never embeds a raw NUL"); + } + + #[test] + fn command_builders_carry_expected_params() { + let m = set_device_metrics(800, 460, 1.5); + assert_eq!(m.params, json!({ "width": 800, "height": 460, "deviceScaleFactor": 1.5, "mobile": false })); + assert_eq!(m.class, Class::Viewport); + let s = start_screencast(1200, 690, 70); + assert_eq!(s.params["format"], "jpeg"); + assert_eq!(s.params["maxWidth"], 1200); + assert_eq!(s.params["everyNthFrame"], 1); + assert_eq!(screencast_ack(42).params, json!({ "sessionId": 42 })); + assert_eq!(deny_downloads().params, json!({ "behavior": "deny" })); + assert_eq!(attach_to_target("T").params, json!({ "targetId": "T", "flatten": true })); + assert_eq!(insert_text("a\0b").encode(1, None).iter().filter(|&&b| b == 0).count(), 1); + assert_eq!(get_history(-1).tag, Tag::History(-1)); + } + + #[test] + fn parses_responses_and_errors() { + let ok = parse_incoming(br#"{"id":3,"result":{"frameId":"F"}}"#).unwrap(); + assert_eq!(ok, Incoming::Response { id: 3, result: json!({ "frameId": "F" }), error: None }); + let err = parse_incoming(br#"{"id":4,"error":{"code":-32000,"message":"boom"}}"#).unwrap(); + assert_eq!(err, Incoming::Response { id: 4, result: Value::Null, error: Some("boom".into()) }); + } + + #[test] + fn parses_page_and_target_events() { + let ev = |s: &str| match parse_incoming(s.as_bytes()).unwrap() { + Incoming::Event(e) => e, + other => panic!("expected event, got {other:?}"), + }; + assert_eq!( + ev(r#"{"method":"Page.frameNavigated","params":{"frame":{"id":"F","url":"https://a.b/"}}}"#), + Event::FrameNavigated { frame_id: "F".into(), is_main: true, url: "https://a.b/".into() } + ); + assert_eq!( + ev(r#"{"method":"Page.frameNavigated","params":{"frame":{"id":"C","parentId":"F","url":"https://ad/"}}}"#), + Event::FrameNavigated { frame_id: "C".into(), is_main: false, url: "https://ad/".into() } + ); + assert_eq!( + ev(r#"{"method":"Page.frameStartedLoading","params":{"frameId":"F"}}"#), + Event::LoadingStarted { frame_id: "F".into() } + ); + assert_eq!(ev(r#"{"method":"Page.loadEventFired","params":{"timestamp":1}}"#), Event::LoadEventFired); + assert_eq!( + ev(r#"{"method":"Target.targetInfoChanged","params":{"targetInfo":{"targetId":"T","title":"Hi","url":"https://a.b/"}}}"#), + Event::TargetInfoChanged { target_id: "T".into(), title: "Hi".into(), url: "https://a.b/".into() } + ); + assert_eq!( + ev(r#"{"method":"Target.targetCreated","params":{"targetInfo":{"targetId":"P","type":"page","openerId":"T","url":""}}}"#), + Event::TargetCreated { target_id: "P".into(), kind: "page".into(), opener: Some("T".into()), url: String::new() } + ); + assert_eq!( + ev(r#"{"method":"Page.screencastFrame","params":{"data":"QUJD","sessionId":9,"metadata":{"deviceWidth":800,"deviceHeight":460.5}}}"#), + Event::ScreencastFrame { data: "QUJD".into(), ack_id: 9, device_width: 800.0, device_height: 460.5 } + ); + assert_eq!(ev(r#"{"method":"Inspector.targetCrashed","params":{}}"#), Event::TargetCrashed); + assert_eq!(ev(r#"{"method":"Page.javascriptDialogOpening","params":{"type":"alert"}}"#), Event::DialogOpening { kind: "alert".into() }); + } + + #[test] + fn garbage_is_an_error_and_unknown_events_are_ignored() { + assert!(matches!(parse_incoming(b"not json"), Err(ProtoError::BadJson(_)))); + assert_eq!(parse_incoming(b"[1,2]"), Err(ProtoError::NotAnObject)); + assert_eq!(parse_incoming(br#"{"method":"Network.requestWillBeSent","params":{}}"#).unwrap(), Incoming::Ignored); + // Wrong field types never panic: they degrade to empty values. + let e = parse_incoming(br#"{"method":"Page.frameNavigated","params":{"frame":7}}"#).unwrap(); + assert_eq!(e, Incoming::Event(Event::FrameNavigated { frame_id: String::new(), is_main: true, url: String::new() })); + assert_eq!(parse_incoming(br#"{"method":5}"#).unwrap(), Incoming::Ignored); + } + + #[test] + fn history_and_target_helpers() { + let h = json!({ "currentIndex": 1, "entries": [ + { "id": 10, "url": "https://a/", "title": "A" }, + { "id": 11, "url": "https://b/", "title": "B" } + ]}); + let (idx, entries) = parse_history(&h).unwrap(); + assert_eq!(idx, 1); + assert_eq!(entries[0], HistoryEntry { id: 10, url: "https://a/".into(), title: "A".into() }); + assert!(parse_history(&json!({ "currentIndex": 5, "entries": [] })).is_none()); + assert!(parse_history(&json!({ "currentIndex": -1, "entries": [] })).is_none()); + let t = json!({ "targetInfos": [ { "targetId": "B", "type": "browser" }, { "targetId": "P", "type": "page" } ] }); + assert_eq!(first_page_target(&t).as_deref(), Some("P")); + assert_eq!(first_page_target(&json!({})), None); + } +} diff --git a/src/pane/browser_canvas/cdp_session.rs b/src/pane/browser_canvas/cdp_session.rs new file mode 100644 index 00000000..b347eeb1 --- /dev/null +++ b/src/pane/browser_canvas/cdp_session.rs @@ -0,0 +1,591 @@ +//! CDP session: bounded command queue, protocol state machine and the reader/writer threads +//! that keep the UI thread off Chrome's pipe (`WEB-01..02`). +//! +//! The UI thread only ever takes short mutexes: it enqueues commands ([`CmdQueue::push`]) and reads +//! the shared page state and the newest frame. The reader thread parses events, decodes screencast +//! JPEGs and drives the handshake; the writer thread drains the queue into the pipe. Generic over +//! `Read`/`Write` so tests run it against in-memory pipes. + +use std::collections::{HashMap, VecDeque}; +use std::io::{Read, Write}; +use std::sync::{Arc, Condvar, Mutex, MutexGuard, OnceLock}; +use std::thread::JoinHandle; +use std::time::Instant; + +use super::MediaRgbaFrame; +use super::cdp_proto::{self as proto, Class, Cmd, Event, Framer, Incoming, ProtoError, Scope, Tag}; +use super::url_policy::validate_url; + +/// Commands queued for Chrome before user input is refused (`Busy`). +pub const QUEUE_CAP: usize = 512; +/// JPEG quality requested for screencast frames. +pub const SCREENCAST_QUALITY: u8 = 70; +/// Largest decoded frame edge accepted from Chrome. +pub const MAX_FRAME_EDGE: usize = 4096; +const MAX_PENDING_TAGS: usize = 1024; +const MAX_HISTORY: usize = 256; +const MAX_POPUPS: usize = 16; + +fn lock(m: &Mutex) -> MutexGuard<'_, T> { + m.lock().unwrap_or_else(|e| e.into_inner()) +} + +/// Lifecycle of the CDP backend. +#[derive(Clone, Debug, PartialEq, Eq, Default)] +pub enum Phase { + /// Chrome was launched; the attach handshake has not finished. + #[default] + Starting, + /// Attached to the page target: the engine is really CDP. + Ready, + /// Was `Ready`, then the page or Chrome died. + Crashed(String), + /// Never became `Ready`. + Failed(String), +} + +/// Viewport request: CSS pixel size and device scale factor (zoom). Frames are `css * scale`. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct Viewport { + pub css_width: u32, + pub css_height: u32, + pub scale: f64, +} + +impl Viewport { + /// Frame size `width_px x height_px` shown at `scale` (CSS size shrinks as zoom grows). + pub fn from_frame_px(width_px: u16, height_px: u16, scale: f32) -> Self { + let scale = if scale.is_finite() { f64::from(scale).clamp(0.25, 5.0) } else { 1.0 }; + let css = |px: u16| ((f64::from(px) / scale).round() as u32).max(16); + Self { css_width: css(width_px), css_height: css(height_px), scale } + } + + /// Largest frame asked of the screencast. Chrome captures at CSS-pixel resolution, so a + /// zoomed-in page (scale > 1) comes back smaller than this and is stretched by the renderer. + pub fn frame_px(&self) -> (u32, u32) { + let px = |css: u32| ((f64::from(css) * self.scale).round() as u32).clamp(16, MAX_FRAME_EDGE as u32); + (px(self.css_width), px(self.css_height)) + } + + fn commands(&self) -> [Cmd; 2] { + let (w, h) = self.frame_px(); + [ + proto::set_device_metrics(self.css_width, self.css_height, self.scale), + proto::start_screencast(w, h, SCREENCAST_QUALITY), + ] + } +} + +/// Page state published by the reader thread. +#[derive(Clone, Debug, Default, PartialEq)] +pub struct PageState { + pub phase: Phase, + pub url: String, + pub title: String, + pub loading: bool, + pub nav_error: Option, + pub history: Vec, + pub history_idx: usize, + /// Size of the newest frame in pixels and the CSS size it covers. + pub frame_px: (u16, u16), + pub device_px: (f64, f64), + /// Bumped on every change so readers can poll cheaply. + pub generation: u64, +} + +/// Why a command was not queued. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PushError { + Busy, + Closed, +} + +struct QueueInner { + q: VecDeque<(u64, Cmd)>, + next_id: u64, + pending: HashMap, + closed: bool, +} + +/// Bounded, coalescing command queue between the UI thread and the writer thread. +pub struct CmdQueue { + inner: Mutex, + cv: Condvar, +} + +impl Default for CmdQueue { + fn default() -> Self { + Self { + inner: Mutex::new(QueueInner { q: VecDeque::new(), next_id: 1, pending: HashMap::new(), closed: false }), + cv: Condvar::new(), + } + } +} + +impl CmdQueue { + /// Queues `cmd` without blocking. Mouse moves coalesce and are dropped when the queue is full; + /// a viewport command replaces a queued one of the same method; other user input gets `Busy`. + pub fn push(&self, cmd: Cmd) -> Result<(), PushError> { + let mut g = lock(&self.inner); + if g.closed { + return Err(PushError::Closed); + } + match cmd.class { + Class::MouseMove => { + if let Some(back) = g.q.back_mut() + && back.1.class == Class::MouseMove + { + back.1 = cmd; + return Ok(()); + } + if g.q.len() >= QUEUE_CAP { + return Ok(()); + } + } + Class::Viewport => g.q.retain(|(_, c)| !(c.class == Class::Viewport && c.method == cmd.method)), + Class::Normal if g.q.len() >= QUEUE_CAP => return Err(PushError::Busy), + Class::Normal | Class::Internal => {} + } + let id = g.next_id; + g.next_id += 1; + if cmd.tag != Tag::None { + if g.pending.len() >= MAX_PENDING_TAGS { + g.pending.clear(); + } + g.pending.insert(id, cmd.tag); + } + g.q.push_back((id, cmd)); + self.cv.notify_one(); + Ok(()) + } + + fn pop_blocking(&self) -> Option<(u64, Cmd)> { + let mut g = lock(&self.inner); + loop { + if g.closed { + return None; + } + if let Some(item) = g.q.pop_front() { + return Some(item); + } + g = self.cv.wait(g).unwrap_or_else(|e| e.into_inner()); + } + } + + fn take_tag(&self, id: u64) -> Tag { + lock(&self.inner).pending.remove(&id).unwrap_or(Tag::None) + } + + pub fn close(&self) { + let mut g = lock(&self.inner); + g.closed = true; + g.q.clear(); + g.pending.clear(); + self.cv.notify_all(); + } + + pub fn len(&self) -> usize { + lock(&self.inner).q.len() + } + + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + + pub fn is_closed(&self) -> bool { + lock(&self.inner).closed + } + + /// Removes and returns everything queued, with ids (tests drive the protocol without threads). + #[cfg(test)] + pub(crate) fn drain_for_test(&self) -> Vec<(u64, Cmd)> { + lock(&self.inner).q.drain(..).collect() + } +} + +/// What to load and how big, read by the handshake when the page target is attached. +#[derive(Clone, Debug)] +pub struct Boot { + pub url: String, + pub viewport: Viewport, + pub allow_file: bool, +} + +/// State shared between the engine (UI thread) and the session threads. +pub struct Shared { + page: Mutex, + frame: Mutex>, + boot: Mutex, + session: OnceLock, + pub queue: CmdQueue, +} + +impl Shared { + pub fn new(boot: Boot) -> Arc { + let page = PageState { url: boot.url.clone(), loading: true, ..PageState::default() }; + Arc::new(Self { + page: Mutex::new(page), + frame: Mutex::new(None), + boot: Mutex::new(boot), + session: OnceLock::new(), + queue: CmdQueue::default(), + }) + } + + pub fn page(&self) -> PageState { + lock(&self.page).clone() + } + + /// Returns the page state only when it changed since `seen_generation`. + pub fn page_if_changed(&self, seen_generation: u64) -> Option { + let g = lock(&self.page); + (g.generation != seen_generation).then(|| g.clone()) + } + + pub fn phase(&self) -> Phase { + lock(&self.page).phase.clone() + } + + pub fn latest_frame(&self) -> Option { + lock(&self.frame).clone() + } + + pub fn update_page(&self, f: impl FnOnce(&mut PageState)) { + let mut g = lock(&self.page); + f(&mut g); + g.generation += 1; + } + + /// Like [`Self::update_page`], but leaves the generation alone when nothing changed, so + /// periodic refreshes do not cause redraws. + pub fn update_page_if_changed(&self, f: impl FnOnce(&mut PageState)) { + let mut g = lock(&self.page); + let before = g.clone(); + f(&mut g); + if *g != before { + g.generation += 1; + } + } + + /// Updates what the handshake will load (used while the backend is still starting). + pub fn update_boot(&self, f: impl FnOnce(&mut Boot)) { + f(&mut lock(&self.boot)); + } + + fn boot(&self) -> Boot { + lock(&self.boot).clone() + } + + /// Marks the backend dead: `Crashed` when it had been ready, `Failed` otherwise. Idempotent. + pub fn mark_dead(&self, reason: &str) { + { + let mut g = lock(&self.page); + match g.phase { + Phase::Starting => g.phase = Phase::Failed(reason.to_string()), + Phase::Ready => g.phase = Phase::Crashed(reason.to_string()), + Phase::Crashed(_) | Phase::Failed(_) => return, + } + g.loading = false; + g.generation += 1; + } + *lock(&self.frame) = None; + self.queue.close(); + } +} + +/// Decodes a base64 screencast JPEG to RGBA. `None` for anything undecodable or oversize. +pub fn decode_jpeg_frame(b64: &str, seq: u64, pts_ms: u64) -> Option { + use zune_jpeg::JpegDecoder; + use zune_jpeg::zune_core::bytestream::ZCursor; + use zune_jpeg::zune_core::colorspace::ColorSpace; + use zune_jpeg::zune_core::options::DecoderOptions; + + let jpeg = crate::vt::codec::base64_decode(b64); + let options = DecoderOptions::default() + .jpeg_set_out_colorspace(ColorSpace::RGBA) + .set_max_width(MAX_FRAME_EDGE) + .set_max_height(MAX_FRAME_EDGE); + let mut decoder = JpegDecoder::new_with_options(ZCursor::new(&jpeg[..]), options); + let rgba = decoder.decode().ok()?; + let info = decoder.info()?; + let (w, h) = (usize::from(info.width), usize::from(info.height)); + if w == 0 || h == 0 || w > MAX_FRAME_EDGE || h > MAX_FRAME_EDGE || rgba.len() != w * h * 4 { + return None; + } + Some(MediaRgbaFrame::new(seq, info.width, info.height, pts_ms, rgba)) +} + +/// Protocol state machine owned by the reader thread. +pub struct Protocol { + shared: Arc, + target_id: Option, + main_frame: Option, + popups: Vec, + frame_seq: u64, + started: Instant, +} + +impl Protocol { + pub fn new(shared: Arc) -> Self { + Self { shared, target_id: None, main_frame: None, popups: Vec::new(), frame_seq: 0, started: Instant::now() } + } + + fn send(&self, cmd: Cmd) { + let _ = self.shared.queue.push(cmd); + } + + /// Starts the handshake: find the page target. + pub fn begin(&self) { + self.send(proto::get_targets()); + } + + /// Handles one framed message from Chrome. + pub fn handle(&mut self, bytes: &[u8]) -> Result<(), ProtoError> { + match proto::parse_incoming(bytes)? { + Incoming::Response { id, result, error } => { + let tag = self.shared.queue.take_tag(id); + self.on_response(tag, &result, error); + } + Incoming::Event(event) => self.on_event(event), + Incoming::Ignored => {} + } + Ok(()) + } + + fn on_response(&mut self, tag: Tag, result: &serde_json::Value, error: Option) { + let str_field = |key: &str| result.get(key).and_then(serde_json::Value::as_str).map(str::to_string); + match tag { + Tag::None => {} + Tag::GetTargets => match (error, proto::first_page_target(result)) { + (Some(e), _) => self.shared.mark_dead(&format!("Target.getTargets failed: {e}")), + (None, Some(target)) => self.attach(target), + (None, None) => self.send(proto::create_target()), + }, + Tag::CreateTarget => match str_field("targetId") { + Some(target) if error.is_none() => self.attach(target), + _ => self.shared.mark_dead("Chrome has no page target"), + }, + Tag::Attach => match str_field("sessionId") { + Some(session) if error.is_none() => self.on_attached(session), + _ => self.shared.mark_dead(&format!("attach failed: {}", error.unwrap_or_default())), + }, + Tag::Navigate => { + let failure = error.or_else(|| str_field("errorText").filter(|t| !t.is_empty())); + if let Some(text) = failure { + self.shared.update_page(|p| { + p.nav_error = Some(text); + p.loading = false; + }); + } + } + Tag::History(offset) => { + let Some((idx, entries)) = proto::parse_history(result) else { return }; + // Chrome is launched on about:blank before the first navigation; that launch entry + // is not part of the user's history. + let base = usize::from(entries.len() > 1 && entries[0].url == "about:blank"); + if offset != 0 { + let target = usize::try_from(idx as i64 + i64::from(offset)).ok().filter(|t| *t >= base); + if let Some(entry) = target.and_then(|t| entries.get(t)) { + self.send(proto::navigate_to_history_entry(entry.id)); + } + return; + } + let skip = entries.len().saturating_sub(MAX_HISTORY).max(base).min(idx); + self.shared.update_page_if_changed(|p| { + // `Target.targetInfoChanged` does not fire for `document.title` changes, so the + // committed history entry is the source of truth for the title. + if !entries[idx].title.is_empty() { + p.title = entries[idx].title.clone(); + } + p.history = entries[skip..].iter().take(MAX_HISTORY).map(|e| e.url.clone()).collect(); + p.history_idx = idx - skip; + }); + } + } + } + + fn attach(&mut self, target: String) { + self.send(proto::attach_to_target(&target)); + self.target_id = Some(target); + } + + fn on_attached(&mut self, session: String) { + let _ = self.shared.session.set(session); + let boot = self.shared.boot(); + self.send(proto::page_enable()); + self.send(proto::deny_downloads()); + self.send(proto::discover_targets()); + for cmd in boot.viewport.commands() { + self.send(cmd); + } + self.send(proto::navigate(&boot.url)); + self.shared.update_page(|p| { + p.phase = Phase::Ready; + p.url = boot.url; + p.loading = true; + }); + } + + fn is_main_frame(&self, frame_id: &str) -> bool { + self.main_frame.as_deref() == Some(frame_id) || self.target_id.as_deref() == Some(frame_id) + } + + fn on_event(&mut self, event: Event) { + match event { + Event::FrameNavigated { frame_id, is_main, url } => { + if !is_main { + return; + } + self.main_frame = Some(frame_id); + self.shared.update_page(|p| { + p.url = url; + p.nav_error = None; + }); + self.send(proto::get_history(0)); + } + Event::NavigatedWithinDocument { frame_id, url } => { + if self.is_main_frame(&frame_id) { + self.shared.update_page(|p| p.url = url); + self.send(proto::get_history(0)); + } + } + Event::LoadingStarted { frame_id } => { + if self.is_main_frame(&frame_id) { + self.shared.update_page(|p| p.loading = true); + } + } + Event::LoadingStopped { frame_id } => { + if self.is_main_frame(&frame_id) { + self.shared.update_page(|p| p.loading = false); + } + } + Event::LoadEventFired => { + self.shared.update_page(|p| p.loading = false); + self.send(proto::get_history(0)); + } + Event::TargetInfoChanged { target_id, title, url } => { + if self.target_id.as_deref() == Some(target_id.as_str()) { + self.shared.update_page(|p| { + p.title = title; + if !url.is_empty() { + p.url = url; + } + }); + } else if self.popups.contains(&target_id) { + self.adopt_popup(target_id, &url); + } + } + Event::TargetCreated { target_id, kind, opener, url } => { + if kind == "page" && opener.is_some() && opener == self.target_id { + self.adopt_popup(target_id, &url); + } + } + Event::ScreencastFrame { data, ack_id, device_width, device_height } => { + self.send(proto::screencast_ack(ack_id)); + self.frame_seq += 1; + let pts_ms = self.started.elapsed().as_millis() as u64; + if let Some(frame) = decode_jpeg_frame(&data, self.frame_seq, pts_ms) { + let size = (frame.width_px, frame.height_px); + *lock(&self.shared.frame) = Some(frame); + self.shared.update_page(|p| { + p.frame_px = size; + p.device_px = (device_width, device_height); + }); + } + } + Event::DialogOpening { kind } => { + self.send(proto::handle_dialog(matches!(kind.as_str(), "alert" | "beforeunload"))); + } + Event::TargetCrashed => self.shared.mark_dead("page crashed"), + Event::Detached => self.shared.mark_dead("page target detached"), + } + } + + /// A link opened a new window: load it in the one visible page instead and close the popup. + fn adopt_popup(&mut self, popup_id: String, url: &str) { + let allow_file = self.shared.boot().allow_file; + match validate_url(url, allow_file) { + Ok(u) if u != "about:blank" && !url.is_empty() => { + self.popups.retain(|p| p != &popup_id); + self.send(proto::navigate(&u).with_class(Class::Internal)); + self.send(proto::close_target(&popup_id)); + } + _ => { + if !self.popups.contains(&popup_id) { + if self.popups.len() >= MAX_POPUPS { + let dropped = self.popups.remove(0); + self.send(proto::close_target(&dropped)); + } + self.popups.push(popup_id); + } + } + } + } +} + +/// Reader and writer threads of one browser. +pub struct SessionThreads { + pub reader: JoinHandle<()>, + pub writer: JoinHandle<()>, +} + +fn reader_loop(mut reader: R, shared: Arc) { + let mut protocol = Protocol::new(Arc::clone(&shared)); + protocol.begin(); + let mut framer = Framer::default(); + let mut buf = vec![0u8; 64 * 1024]; + let mut messages = Vec::new(); + loop { + let n = match reader.read(&mut buf) { + Ok(0) => return shared.mark_dead("Chrome exited"), + Ok(n) => n, + Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue, + Err(e) => return shared.mark_dead(&format!("read from Chrome failed: {e}")), + }; + if let Err(e) = framer.push(&buf[..n], &mut messages) { + return shared.mark_dead(&e.to_string()); + } + for msg in messages.drain(..) { + tracing::trace!(target: "bilterm::browser", bytes = msg.len(), "cdp <- {}", String::from_utf8_lossy(&msg[..msg.len().min(160)])); + if let Err(e) = protocol.handle(&msg) { + return shared.mark_dead(&e.to_string()); + } + } + if shared.queue.is_closed() { + return; + } + } +} + +fn writer_loop(mut writer: W, shared: Arc) { + while let Some((id, cmd)) = shared.queue.pop_blocking() { + let session = shared.session.get().map(String::as_str); + if cmd.scope == Scope::Page && session.is_none() { + continue; + } + let bytes = cmd.encode(id, session); + tracing::trace!(target: "bilterm::browser", "cdp -> {}", String::from_utf8_lossy(&bytes[..bytes.len().min(160)])); + if let Err(e) = writer.write_all(&bytes).and_then(|()| writer.flush()) { + return shared.mark_dead(&format!("write to Chrome failed: {e}")); + } + } +} + +/// Spawns the reader and writer threads over an already connected transport. +pub fn spawn_session(reader: R, writer: W, shared: Arc) -> std::io::Result +where + R: Read + Send + 'static, + W: Write + Send + 'static, +{ + let (rs, ws) = (Arc::clone(&shared), Arc::clone(&shared)); + let writer = std::thread::Builder::new().name("bilterm-cdp-writer".into()).spawn(move || writer_loop(writer, ws))?; + let reader = std::thread::Builder::new() + .name("bilterm-cdp-reader".into()) + .spawn(move || reader_loop(reader, rs)) + .inspect_err(|_| shared.queue.close())?; + Ok(SessionThreads { reader, writer }) +} + +#[cfg(test)] +#[path = "cdp_session_tests.rs"] +mod tests; diff --git a/src/pane/browser_canvas/cdp_session_tests.rs b/src/pane/browser_canvas/cdp_session_tests.rs new file mode 100644 index 00000000..323c6ed9 --- /dev/null +++ b/src/pane/browser_canvas/cdp_session_tests.rs @@ -0,0 +1,448 @@ +//! Unit tests for the CDP session and engine over a fake transport (in-memory pipes). + +use std::io::{Read, Write}; +use std::sync::mpsc::{Receiver, Sender, channel}; +use std::time::{Duration, Instant}; + +use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton}; +use serde_json::{Value, json}; + +use super::*; +use crate::pane::browser_canvas::cdp::CdpBrowserBackend; +use crate::pane::browser_canvas::{BrowserEngine, EngineHealth, MediaMouseEvent, MediaMouseEventKind}; + +/// 16x8 solid-colour JPEG, quality 70. +const TINY_JPEG_B64: &str = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAoHBwgHBgoICAgLCgoLDhgQDg0NDh0VFhEYIx8lJCIfIiEmKzcvJik0KSEiMEExNDk7Pj4+JS5ESUM8SDc9Pjv/2wBDAQoLCw4NDhwQEBw7KCIoOzs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozv/wAARCAAIABADASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAb/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFQEBAQAAAAAAAAAAAAAAAAAABQf/xAAUEQEAAAAAAAAAAAAAAAAAAAAA/9oADAMBAAIRAxEAPwCaAGKG/9k="; + +struct MemReader { + rx: Receiver>, + left: Vec, +} + +struct MemWriter(Sender>); + +/// One direction of an in-memory pipe. Dropping the writer is EOF for the reader. +fn mem_pipe() -> (MemWriter, MemReader) { + let (tx, rx) = channel(); + (MemWriter(tx), MemReader { rx, left: Vec::new() }) +} + +impl Read for MemReader { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + if self.left.is_empty() { + match self.rx.recv() { + Ok(chunk) => self.left = chunk, + Err(_) => return Ok(0), + } + } + let n = buf.len().min(self.left.len()); + buf[..n].copy_from_slice(&self.left[..n]); + self.left.drain(..n); + Ok(n) + } +} + +impl Write for MemWriter { + fn write(&mut self, buf: &[u8]) -> std::io::Result { + self.0.send(buf.to_vec()).map_err(|_| std::io::Error::from(std::io::ErrorKind::BrokenPipe))?; + Ok(buf.len()) + } + + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} + +fn boot(url: &str) -> Boot { + Boot { url: url.to_string(), viewport: Viewport::from_frame_px(800, 460, 1.0), allow_file: false } +} + +fn reply(protocol: &mut Protocol, id: u64, result: Value) { + protocol.handle(json!({ "id": id, "result": result }).to_string().as_bytes()).unwrap(); +} + +fn event(protocol: &mut Protocol, method: &str, params: Value) { + protocol.handle(json!({ "method": method, "params": params }).to_string().as_bytes()).unwrap(); +} + +/// Runs the handshake by hand and returns the protocol attached to target `T`. +fn attached(url: &str) -> (std::sync::Arc, Protocol) { + let shared = Shared::new(boot(url)); + let mut p = Protocol::new(std::sync::Arc::clone(&shared)); + p.begin(); + let q = shared.queue.drain_for_test(); + assert_eq!(q[0].1.method, "Target.getTargets"); + reply(&mut p, q[0].0, json!({ "targetInfos": [{ "targetId": "T", "type": "page" }] })); + let q = shared.queue.drain_for_test(); + assert_eq!(q[0].1.method, "Target.attachToTarget"); + assert_eq!(shared.phase(), Phase::Starting, "not cdp until the attach answer arrives"); + reply(&mut p, q[0].0, json!({ "sessionId": "S" })); + (shared, p) +} + +fn methods(shared: &Shared) -> Vec<&'static str> { + shared.queue.drain_for_test().into_iter().map(|(_, c)| c.method).collect() +} + +#[test] +fn handshake_attaches_configures_and_navigates() { + let (shared, _p) = attached("https://example.com/"); + assert_eq!(shared.phase(), Phase::Ready); + assert_eq!( + methods(&shared), + [ + "Page.enable", + "Browser.setDownloadBehavior", + "Target.setDiscoverTargets", + "Emulation.setDeviceMetricsOverride", + "Page.startScreencast", + "Page.navigate", + ] + ); + assert!(shared.page().loading); +} + +#[test] +fn handshake_creates_a_page_target_when_none_exists_and_fails_cleanly() { + let shared = Shared::new(boot("about:blank")); + let mut p = Protocol::new(std::sync::Arc::clone(&shared)); + p.begin(); + let q = shared.queue.drain_for_test(); + reply(&mut p, q[0].0, json!({ "targetInfos": [{ "targetId": "B", "type": "browser" }] })); + let q = shared.queue.drain_for_test(); + assert_eq!(q[0].1.method, "Target.createTarget"); + p.handle(json!({ "id": q[0].0, "error": { "message": "nope" } }).to_string().as_bytes()).unwrap(); + assert_eq!(shared.phase(), Phase::Failed("Chrome has no page target".into())); + assert!(shared.queue.is_closed()); +} + +#[test] +fn page_events_update_url_title_loading_and_history() { + let (shared, mut p) = attached("https://a.example/"); + methods(&shared); + event(&mut p, "Page.frameStartedLoading", json!({ "frameId": "T" })); + assert!(shared.page().loading); + // A page-initiated navigation: the address bar must follow it. + event(&mut p, "Page.frameNavigated", json!({ "frame": { "id": "T", "url": "https://b.example/next" } })); + event(&mut p, "Page.frameNavigated", json!({ "frame": { "id": "AD", "parentId": "T", "url": "https://ads/" } })); + assert_eq!(shared.page().url, "https://b.example/next"); + let q = shared.queue.drain_for_test(); + assert_eq!(q.len(), 1, "sub-frame navigations are ignored"); + assert_eq!(q[0].1.method, "Page.getNavigationHistory"); + reply(&mut p, q[0].0, json!({ "currentIndex": 1, "entries": [ + { "id": 1, "url": "https://a.example/", "title": "A" }, + { "id": 2, "url": "https://b.example/next", "title": "B" }, + ]})); + let page = shared.page(); + assert_eq!(page.history, ["https://a.example/", "https://b.example/next"]); + assert_eq!(page.history_idx, 1); + assert_eq!(page.title, "B", "the committed history entry supplies the title"); + event(&mut p, "Target.targetInfoChanged", json!({ "targetInfo": { "targetId": "T", "title": "Real title", "url": "https://b.example/next" } })); + event(&mut p, "Target.targetInfoChanged", json!({ "targetInfo": { "targetId": "OTHER", "title": "x", "url": "https://x/" } })); + assert_eq!(shared.page().title, "Real title"); + event(&mut p, "Page.frameStoppedLoading", json!({ "frameId": "OTHER" })); + assert!(shared.page().loading, "only the main frame controls the loading state"); + event(&mut p, "Page.loadEventFired", json!({})); + assert!(!shared.page().loading); + event(&mut p, "Page.navigatedWithinDocument", json!({ "frameId": "T", "url": "https://b.example/next#x" })); + assert_eq!(shared.page().url, "https://b.example/next#x"); +} + +#[test] +fn back_and_forward_use_real_history_entries() { + let (shared, mut p) = attached("https://a.example/"); + methods(&shared); + let entries = json!({ "currentIndex": 1, "entries": [ + { "id": 11, "url": "https://a/", "title": "" }, + { "id": 12, "url": "https://b/", "title": "" }, + { "id": 13, "url": "https://c/", "title": "" }, + ]}); + for (offset, entry) in [(-1, 11), (1, 13)] { + shared.queue.push(proto::get_history(offset)).unwrap(); + let q = shared.queue.drain_for_test(); + reply(&mut p, q[0].0, entries.clone()); + let q = shared.queue.drain_for_test(); + assert_eq!(q[0].1.method, "Page.navigateToHistoryEntry"); + assert_eq!(q[0].1.params, json!({ "entryId": entry })); + } + // The about:blank entry Chrome was launched on is hidden: no going back to it. + let launch = json!({ "currentIndex": 1, "entries": [ + { "id": 1, "url": "about:blank", "title": "" }, + { "id": 2, "url": "https://a/", "title": "A" }, + { "id": 3, "url": "https://b/", "title": "B" }, + ]}); + shared.queue.push(proto::get_history(-1)).unwrap(); + let q = shared.queue.drain_for_test(); + reply(&mut p, q[0].0, launch.clone()); + assert!(shared.queue.is_empty(), "back from the first real page goes nowhere"); + shared.queue.push(proto::get_history(0)).unwrap(); + let q = shared.queue.drain_for_test(); + let generation = shared.page().generation; + reply(&mut p, q[0].0, launch.clone()); + let page = shared.page(); + assert_eq!((page.history.clone(), page.history_idx), (vec!["https://a/".to_string(), "https://b/".to_string()], 0)); + assert_eq!(page.title, "A"); + assert_ne!(page.generation, generation); + // An identical refresh changes nothing, so it must not request a redraw. + shared.queue.push(proto::get_history(0)).unwrap(); + let q = shared.queue.drain_for_test(); + reply(&mut p, q[0].0, launch); + assert_eq!(shared.page().generation, page.generation); + // Back at the first entry does nothing. + shared.queue.push(proto::get_history(-1)).unwrap(); + let q = shared.queue.drain_for_test(); + reply(&mut p, q[0].0, json!({ "currentIndex": 0, "entries": [{ "id": 11, "url": "https://a/", "title": "" }] })); + assert!(shared.queue.is_empty()); +} + +#[test] +fn screencast_frames_are_acked_decoded_and_only_the_newest_is_kept() { + let (shared, mut p) = attached("https://a.example/"); + methods(&shared); + for ack in [5, 6] { + event(&mut p, "Page.screencastFrame", json!({ + "data": TINY_JPEG_B64, "sessionId": ack, "metadata": { "deviceWidth": 8.0, "deviceHeight": 4.0 } + })); + let q = shared.queue.drain_for_test(); + assert_eq!(q[0].1.method, "Page.screencastFrameAck"); + assert_eq!(q[0].1.params, json!({ "sessionId": ack })); + } + let frame = shared.latest_frame().expect("decoded frame"); + assert_eq!((frame.seq, frame.width_px, frame.height_px), (2, 16, 8)); + assert_eq!(frame.rgba.len(), 16 * 8 * 4); + assert!(frame.rgba[0] > 150 && frame.rgba[1] < 90 && frame.rgba[3] == 255, "red-ish opaque pixel"); + assert_eq!(shared.page().frame_px, (16, 8)); + assert_eq!(shared.page().device_px, (8.0, 4.0)); + // A corrupt frame is acked and skipped; the last good frame stays. + event(&mut p, "Page.screencastFrame", json!({ "data": "AAAA", "sessionId": 7, "metadata": {} })); + assert_eq!(methods(&shared), ["Page.screencastFrameAck"]); + assert_eq!(shared.latest_frame().unwrap().seq, 2); + assert!(decode_jpeg_frame("", 1, 0).is_none()); +} + +#[test] +fn crash_dialog_and_navigation_errors() { + let (shared, mut p) = attached("https://a.example/"); + methods(&shared); + event(&mut p, "Page.javascriptDialogOpening", json!({ "type": "confirm" })); + let q = shared.queue.drain_for_test(); + assert_eq!(q[0].1.params, json!({ "accept": false })); + shared.queue.push(proto::navigate("https://nope.invalid/")).unwrap(); + let q = shared.queue.drain_for_test(); + reply(&mut p, q[0].0, json!({ "frameId": "T", "errorText": "net::ERR_NAME_NOT_RESOLVED" })); + assert_eq!(shared.page().nav_error.as_deref(), Some("net::ERR_NAME_NOT_RESOLVED")); + event(&mut p, "Page.screencastFrame", json!({ "data": TINY_JPEG_B64, "sessionId": 1, "metadata": {} })); + event(&mut p, "Inspector.targetCrashed", json!({})); + assert_eq!(shared.phase(), Phase::Crashed("page crashed".into())); + assert!(shared.latest_frame().is_none(), "no frozen frame after a crash"); + assert_eq!(shared.queue.push(proto::reload()), Err(PushError::Closed)); +} + +#[test] +fn popups_load_in_the_visible_page_and_are_closed() { + let (shared, mut p) = attached("https://a.example/"); + methods(&shared); + event(&mut p, "Target.targetCreated", json!({ "targetInfo": { "targetId": "POP", "type": "page", "openerId": "T", "url": "" } })); + assert!(shared.queue.is_empty(), "blank popup waits for its URL"); + event(&mut p, "Target.targetInfoChanged", json!({ "targetInfo": { "targetId": "POP", "title": "", "url": "https://c.example/" } })); + let q = shared.queue.drain_for_test(); + assert_eq!(q[0].1.method, "Page.navigate"); + assert_eq!(q[0].1.params, json!({ "url": "https://c.example/" })); + assert_eq!(q[1].1.method, "Target.closeTarget"); + assert_eq!(q[1].1.params, json!({ "targetId": "POP" })); + // Unrelated targets and disallowed schemes are left alone. + event(&mut p, "Target.targetCreated", json!({ "targetInfo": { "targetId": "X", "type": "page", "url": "https://x/" } })); + event(&mut p, "Target.targetCreated", json!({ "targetInfo": { "targetId": "J", "type": "page", "openerId": "T", "url": "javascript:alert(1)" } })); + assert!(shared.queue.is_empty()); +} + +#[test] +fn queue_coalesces_mouse_moves_and_viewport_and_is_bounded() { + let q = CmdQueue::default(); + let mv = || proto::insert_text("m").with_class(Class::MouseMove); + for _ in 0..50 { + q.push(mv()).unwrap(); + } + assert_eq!(q.len(), 1, "consecutive mouse moves coalesce"); + for size in [100, 200, 300] { + q.push(proto::set_device_metrics(size, size, 1.0)).unwrap(); + q.push(proto::start_screencast(size, size, 70)).unwrap(); + } + let drained = q.drain_for_test(); + assert_eq!(drained.len(), 3, "only the newest viewport pair is kept"); + assert_eq!(drained[1].1.params["width"], 300); + assert_eq!(drained[2].1.params["maxWidth"], 300); + + for _ in 0..QUEUE_CAP { + q.push(proto::insert_text("x")).unwrap(); + } + assert_eq!(q.push(proto::insert_text("x")), Err(PushError::Busy), "user input is refused when full"); + q.push(mv()).unwrap(); + assert_eq!(q.len(), QUEUE_CAP, "mouse moves are dropped under pressure"); + q.push(proto::screencast_ack(1)).unwrap(); + assert_eq!(q.len(), QUEUE_CAP + 1, "protocol bookkeeping is never dropped"); + q.close(); + assert_eq!(q.push(proto::reload()), Err(PushError::Closed)); + assert!(q.is_empty()); +} + +#[test] +fn viewport_zoom_shrinks_css_size_and_keeps_frame_size() { + let v = Viewport::from_frame_px(800, 460, 1.0); + assert_eq!((v.css_width, v.css_height, v.frame_px()), (800, 460, (800, 460))); + let z = Viewport::from_frame_px(800, 460, 2.0); + assert_eq!((z.css_width, z.css_height, z.scale, z.frame_px()), (400, 230, 2.0, (800, 460))); + let out = Viewport::from_frame_px(800, 460, 0.5); + assert_eq!((out.css_width, out.frame_px()), (1600, (800, 460))); + assert_eq!(Viewport::from_frame_px(800, 460, f32::NAN).scale, 1.0); +} + +// ---- threaded tests: the real reader/writer threads against a scripted fake Chrome ---- + +struct FakeChrome { + to_client: MemWriter, + from_client: MemReader, + framer: Framer, + inbox: std::collections::VecDeque, +} + +impl FakeChrome { + fn send(&mut self, v: Value) { + let mut bytes = v.to_string().into_bytes(); + bytes.push(0); + self.to_client.write_all(&bytes).unwrap(); + } + + /// Next command written by the client (blocks; the client threads are live). + fn next(&mut self) -> Value { + let mut buf = [0u8; 4096]; + while self.inbox.is_empty() { + let n = self.from_client.read(&mut buf).unwrap(); + assert!(n > 0, "client closed the pipe"); + let mut msgs = Vec::new(); + self.framer.push(&buf[..n], &mut msgs).unwrap(); + self.inbox.extend(msgs.iter().map(|m| serde_json::from_slice::(m).unwrap())); + } + self.inbox.pop_front().unwrap() + } + + /// Reads commands until `method` arrives, answering nothing in between. + fn expect(&mut self, method: &str) -> Value { + loop { + let v = self.next(); + if v["method"] == method { + return v; + } + } + } + + fn handshake(&mut self) { + let get = self.expect("Target.getTargets"); + self.send(json!({ "id": get["id"], "result": { "targetInfos": [{ "targetId": "T", "type": "page" }] } })); + let attach = self.expect("Target.attachToTarget"); + assert_eq!(attach["params"]["flatten"], true); + self.send(json!({ "id": attach["id"], "result": { "sessionId": "S" } })); + } +} + +fn engine_with_fake_chrome(url: &str) -> (CdpBrowserBackend, FakeChrome) { + let (to_client, client_reader) = mem_pipe(); + let (client_writer, from_client) = mem_pipe(); + let engine = + CdpBrowserBackend::with_transport(client_reader, client_writer, url, Viewport::from_frame_px(800, 460, 1.0), false) + .unwrap(); + (engine, FakeChrome { to_client, from_client, framer: Framer::default(), inbox: Default::default() }) +} + +fn wait_until(engine: &mut CdpBrowserBackend, what: &str, cond: impl Fn(&CdpBrowserBackend) -> bool) { + let deadline = Instant::now() + Duration::from_secs(5); + loop { + engine.poll(); + if cond(engine) { + return; + } + assert!(Instant::now() < deadline, "timed out waiting for {what}"); + std::thread::sleep(Duration::from_millis(2)); + } +} + +#[test] +fn engine_reports_cdp_only_after_the_handshake_and_drives_the_page() { + let (mut engine, mut chrome) = engine_with_fake_chrome("https://example.com/"); + assert_eq!(engine.health(), EngineHealth::Starting); + // Input before the handshake is dropped, never queued for a page that is not attached. + engine.dispatch_key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE)).unwrap(); + chrome.handshake(); + wait_until(&mut engine, "ready", |e| e.health() == EngineHealth::Ready); + assert_eq!(engine.backend_name(), "cdp"); + + let nav = chrome.expect("Page.navigate"); + assert_eq!(nav["params"]["url"], "https://example.com/"); + assert_eq!(nav["sessionId"], "S"); + + chrome.send(json!({ "method": "Page.screencastFrame", "sessionId": "S", "params": { + "data": TINY_JPEG_B64, "sessionId": 3, "metadata": { "deviceWidth": 8.0, "deviceHeight": 4.0 } } })); + let ack = chrome.expect("Page.screencastFrameAck"); + assert_eq!(ack["params"]["sessionId"], 3); + wait_until(&mut engine, "frame", |e| e.frame_size_px() == (16, 8)); + assert_eq!(engine.latest_frame().unwrap().width_px, 16); + + // Frame is 16x8 px for an 8x4 CSS viewport: frame pixel (8, 4) is CSS (4, 2). + engine.dispatch_mouse(MediaMouseEvent { + kind: MediaMouseEventKind::Down(MouseButton::Left), x_px: 8, y_px: 4, modifiers: KeyModifiers::NONE, + }).unwrap(); + let click = chrome.expect("Input.dispatchMouseEvent"); + assert_eq!((click["params"]["x"].as_f64(), click["params"]["y"].as_f64()), (Some(4.0), Some(2.0))); + assert_eq!(click["params"]["clickCount"], 1); + + engine.dispatch_key(KeyEvent::new(KeyCode::Char('h'), KeyModifiers::NONE)).unwrap(); + let key = chrome.expect("Input.dispatchKeyEvent"); + assert_eq!((key["params"]["type"].as_str(), key["params"]["text"].as_str()), (Some("keyDown"), Some("h"))); + engine.insert_text("pasted").unwrap(); + assert_eq!(chrome.expect("Input.insertText")["params"]["text"], "pasted"); + + engine.resize_viewport(1000, 600, 2.0).unwrap(); + let metrics = chrome.expect("Emulation.setDeviceMetricsOverride"); + assert_eq!(metrics["params"], json!({ "width": 500, "height": 300, "deviceScaleFactor": 2.0, "mobile": false })); + chrome.expect("Page.stopScreencast"); + assert_eq!(chrome.expect("Page.startScreencast")["params"]["maxWidth"], 1000); + + assert!(engine.navigate("javascript:alert(1)").is_err(), "policy is enforced at the engine"); + engine.navigate("example.org/x").unwrap(); + assert_eq!(engine.current_url(), "https://example.org/x"); + assert_eq!(chrome.expect("Page.navigate")["params"]["url"], "https://example.org/x"); + + chrome.send(json!({ "method": "Target.targetInfoChanged", "params": { + "targetInfo": { "targetId": "T", "title": "Example", "url": "https://example.org/x" } } })); + wait_until(&mut engine, "title", |e| e.page_title() == "Example"); + + // Chrome goes away: crashed state, not a frozen frame. + drop(chrome); + wait_until(&mut engine, "crash", |e| matches!(e.health(), EngineHealth::Crashed(_))); + assert_eq!(engine.health(), EngineHealth::Crashed("Chrome exited".into())); + assert!(engine.latest_frame().is_none()); + assert!(engine.reload().is_err()); +} + +#[test] +fn exit_or_garbage_before_the_handshake_is_a_launch_failure_not_a_panic() { + let (mut engine, chrome) = engine_with_fake_chrome("https://example.com/"); + drop(chrome); + wait_until(&mut engine, "failed", |e| matches!(e.health(), EngineHealth::Failed(_))); + assert_eq!(engine.health(), EngineHealth::Failed("Chrome exited".into())); + + let (mut engine, mut chrome) = engine_with_fake_chrome("https://example.com/"); + chrome.to_client.write_all(b"not devtools\0").unwrap(); + wait_until(&mut engine, "failed", |e| matches!(e.health(), EngineHealth::Failed(_))); + let EngineHealth::Failed(why) = engine.health() else { unreachable!() }; + assert!(why.contains("not JSON"), "{why}"); +} + +#[test] +fn navigation_before_the_handshake_replaces_the_boot_url() { + let (mut engine, mut chrome) = engine_with_fake_chrome("https://first.example/"); + engine.navigate("https://second.example/").unwrap(); + assert_eq!(engine.current_url(), "https://second.example/"); + chrome.handshake(); + assert_eq!(chrome.expect("Page.navigate")["params"]["url"], "https://second.example/"); +} diff --git a/src/pane/browser_canvas/chrome_ui.rs b/src/pane/browser_canvas/chrome_ui.rs index ddee5af3..5bb4686d 100644 --- a/src/pane/browser_canvas/chrome_ui.rs +++ b/src/pane/browser_canvas/chrome_ui.rs @@ -1,10 +1,14 @@ //! 1-row Browser Navigation Bar renderer and hit-testing (`WEB-01`). +//! +//! Rendering and hit-testing share one layout ([`nav_layout`]) measured in display columns, so a +//! click always lands on the button that is drawn there. use ratatui::buffer::Buffer; use ratatui::layout::Rect; use ratatui::style::{Color, Modifier, Style}; +use unicode_width::{UnicodeWidthChar, UnicodeWidthStr}; -use super::BrowserCanvasState; +use super::{BrowserCanvasState, EngineHealth}; /// Hit-test target on the 1-row Browser Navigation Bar. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -17,97 +21,189 @@ pub enum BrowserNavHit { Close, } +pub const BACK_LABEL: &str = "[◄]"; +pub const FORWARD_LABEL: &str = "[►]"; +pub const RELOAD_LABEL: &str = "[↻]"; +pub const STOP_LABEL: &str = "[■]"; +pub const CLOSE_LABEL: &str = "[✕]"; +pub const FOCUS_WEB_LABEL: &str = "[FOCUS:WEB]"; +pub const FOCUS_PROMPT_LABEL: &str = "[FOCUS:PROMPT]"; + +/// A horizontal zone on the nav bar: start column and width, in display columns. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Zone { + pub x: u16, + pub width: u16, +} + +impl Zone { + fn contains(&self, col: u16) -> bool { + col >= self.x && col < self.x.saturating_add(self.width) + } +} + +/// Column layout of the nav bar. The focus zone has one fixed width for both labels. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct NavLayout { + pub back: Zone, + pub forward: Zone, + pub reload: Zone, + pub url: Zone, + pub focus: Option, + pub close: Option, +} + +fn cols(s: &str) -> u16 { + UnicodeWidthStr::width(s) as u16 +} + +/// Computes the nav bar zones for `nav_rect` from the display width of each label. +pub fn nav_layout(nav_rect: Rect) -> NavLayout { + let mut x = nav_rect.x; + let mut button = |label: &str| { + let zone = Zone { x, width: cols(label) }; + x = x.saturating_add(zone.width + 1); + zone + }; + let back = button(BACK_LABEL); + let forward = button(FORWARD_LABEL); + let reload = button(RELOAD_LABEL); + let url_start = x; + + let close_w = cols(CLOSE_LABEL); + let focus_w = cols(FOCUS_WEB_LABEL).max(cols(FOCUS_PROMPT_LABEL)); + let right_reserve = close_w + 1 + focus_w + 1; + let right = nav_rect.right(); + let has_right = right.saturating_sub(url_start) >= right_reserve; + let (focus, close, url_end) = if has_right { + let close = Zone { x: right - close_w, width: close_w }; + let focus = Zone { x: close.x - 1 - focus_w, width: focus_w }; + (Some(focus), Some(close), focus.x.saturating_sub(1)) + } else { + (None, None, right) + }; + let url = Zone { x: url_start, width: url_end.saturating_sub(url_start) }; + NavLayout { back, forward, reload, url, focus, close } +} + +/// Truncates `text` to at most `max_cols` display columns. +fn fit_cols(text: &str, max_cols: usize) -> String { + let mut used = 0; + let mut out = String::new(); + for c in text.chars() { + let w = UnicodeWidthChar::width(c).unwrap_or(0); + if used + w > max_cols { + break; + } + used += w; + out.push(c); + } + out +} + +/// Text shown in the URL zone: status, lock (https only), URL and page title. +pub fn url_zone_text(state: &BrowserCanvasState) -> String { + let url = state.current_url(); + let mut text = String::new(); + match state.health() { + EngineHealth::Starting => text.push_str("◌ starting… "), + EngineHealth::Crashed(_) => text.push_str("✖ crashed "), + _ if state.is_loading() => text.push_str("◌ "), + _ => {} + } + if url.starts_with("https://") { + text.push_str("🔒 "); + } + text.push_str(url); + if let Some(notice) = state.notice() { + text.push_str(" ⚠ "); + text.push_str(notice); + } else if !state.page_title().is_empty() { + text.push_str(" — "); + text.push_str(state.page_title()); + } + text +} + /// Renders the 1-row Browser Navigation Bar into `buf` across `nav_rect`. -pub fn render_browser_nav_bar( - buf: &mut Buffer, - nav_rect: Rect, - state: &BrowserCanvasState, - active: bool, -) { +pub fn render_browser_nav_bar(buf: &mut Buffer, nav_rect: Rect, state: &BrowserCanvasState, active: bool) { if nav_rect.height == 0 || nav_rect.width == 0 { return; } - let y = nav_rect.y; - let bg = if active { - Color::Rgb(24, 38, 64) - } else { - Color::Rgb(18, 24, 36) - }; + let bg = if active { Color::Rgb(24, 38, 64) } else { Color::Rgb(18, 24, 36) }; let btn_style = Style::default().fg(Color::Cyan).bg(bg).add_modifier(Modifier::BOLD); + let dim_style = Style::default().fg(Color::DarkGray).bg(bg); let text_style = Style::default().fg(Color::White).bg(bg); let alert_style = Style::default().fg(Color::LightRed).bg(bg).add_modifier(Modifier::BOLD); - // Fill background for col in nav_rect.x..nav_rect.right() { buf[(col, y)].set_char(' ').set_style(Style::default().bg(bg)); } - - let mut cur_x = nav_rect.x; - - // [◄] Back - let back_str = "[◄]"; - buf.set_string(cur_x, y, back_str, btn_style); - cur_x += back_str.len() as u16 + 1; - - // [►] Forward - let fwd_str = "[►]"; - buf.set_string(cur_x, y, fwd_str, btn_style); - cur_x += fwd_str.len() as u16 + 1; - - // [↻] Reload - let reload_str = "[↻]"; - buf.set_string(cur_x, y, reload_str, btn_style); - cur_x += reload_str.len() as u16 + 1; - - // Right-aligned buttons: [✕] Close, [FOCUS/PROMPT] - let close_str = "[✕]"; - let focus_str = if state.viewport_focused { - "[FOCUS:WEB]" - } else { - "[FOCUS:PROMPT]" + let layout = nav_layout(nav_rect); + let right = nav_rect.right(); + let mut put = |zone: Zone, label: &str, style: Style| { + if zone.x < right { + buf.set_stringn(zone.x, y, label, usize::from(right - zone.x), style); + } }; - let right_reserve = close_str.len() as u16 + 1 + focus_str.len() as u16 + 1; - - // Center/URL Bar - let max_url_w = nav_rect.right().saturating_sub(cur_x).saturating_sub(right_reserve) as usize; - if max_url_w > 6 { - if let Some(edit) = state.url_edit() { - let edit_bg = Color::Rgb(250, 250, 250); - let edit_style = Style::default().fg(Color::Black).bg(edit_bg); - for col in cur_x..cur_x + max_url_w as u16 { - buf[(col, y)].set_char(' ').set_style(edit_style); - } - // Scroll so the cursor stays visible. - let start = (edit.cursor + 1).saturating_sub(max_url_w); - let visible: String = edit.text.chars().skip(start).take(max_url_w).collect(); - buf.set_string(cur_x, y, &visible, edit_style); - let cx = cur_x + (edit.cursor - start) as u16; - if cx < cur_x + max_url_w as u16 { - buf[(cx, y)].set_style( - Style::default().fg(Color::White).bg(Color::Black).add_modifier(Modifier::REVERSED), - ); - } + put(layout.back, BACK_LABEL, if state.can_go_back() { btn_style } else { dim_style }); + put(layout.forward, FORWARD_LABEL, if state.can_go_forward() { btn_style } else { dim_style }); + put(layout.reload, if state.is_loading() { STOP_LABEL } else { RELOAD_LABEL }, btn_style); + if let Some(focus) = layout.focus { + let (label, style) = if state.viewport_focused { + (FOCUS_WEB_LABEL, Style::default().fg(Color::Green).bg(bg).add_modifier(Modifier::BOLD)) } else { - let display_url = format!("🔒 {}", state.current_url()); - let truncated: String = display_url.chars().take(max_url_w).collect(); - buf.set_string(cur_x, y, &truncated, text_style); - } + (FOCUS_PROMPT_LABEL, Style::default().fg(Color::Yellow).bg(bg)) + }; + put(focus, label, style); + } + if let Some(close) = layout.close { + put(close, CLOSE_LABEL, alert_style); } - // Right buttons - if nav_rect.right() > right_reserve { - let fx = nav_rect.right().saturating_sub(right_reserve); - let focus_style = if state.viewport_focused { - Style::default().fg(Color::Green).bg(bg).add_modifier(Modifier::BOLD) - } else { - Style::default().fg(Color::Yellow).bg(bg) - }; - buf.set_string(fx, y, focus_str, focus_style); + let url = layout.url; + let max_url_w = usize::from(url.width); + if max_url_w <= 6 { + return; + } + if let Some(edit) = state.url_edit() { + let edit_style = Style::default().fg(Color::Black).bg(Color::Rgb(250, 250, 250)); + for col in url.x..url.x + url.width { + buf[(col, y)].set_char(' ').set_style(edit_style); + } + // Scroll so the cursor stays visible. + let start = (edit.cursor + 1).saturating_sub(max_url_w); + let visible: String = edit.text.chars().skip(start).collect(); + let visible = fit_cols(&visible, max_url_w); + buf.set_string(url.x, y, &visible, edit_style); + let before: String = edit.text.chars().skip(start).take(edit.cursor - start).collect(); + let cx = url.x + cols(&before); + if cx < url.x + url.width { + buf[(cx, y)].set_style(Style::default().fg(Color::White).bg(Color::Black).add_modifier(Modifier::REVERSED)); + } + } else { + let style = if state.notice().is_some() || state.is_crashed() { alert_style } else { text_style }; + buf.set_string(url.x, y, fit_cols(&url_zone_text(state), max_url_w), style); + } +} - let cx = nav_rect.right().saturating_sub(close_str.len() as u16); - buf.set_string(cx, y, close_str, alert_style); +/// Draws the "starting" / "crashed" message over the viewport when there is no frame to show. +pub fn render_browser_status_overlay(buf: &mut Buffer, body: Rect, state: &BrowserCanvasState) { + let Some(message) = state.overlay_message() else { return }; + if body.width == 0 || body.height == 0 { + return; + } + let fill = Style::default().fg(Color::Gray).bg(Color::Rgb(18, 24, 36)); + for row in body.y..body.bottom() { + for col in body.x..body.right() { + buf[(col, row)].set_char(' ').set_style(fill); + } } + let text = fit_cols(&message, usize::from(body.width)); + let x = body.x + (body.width - cols(&text)) / 2; + let style = if state.is_crashed() { fill.fg(Color::LightRed).add_modifier(Modifier::BOLD) } else { fill }; + buf.set_string(x, body.y + body.height / 2, text, style); } /// Hit-tests a mouse coordinate `(col, row)` against the 1-row Browser Navigation Bar. @@ -115,34 +211,116 @@ pub fn hit_test_browser_nav_bar(nav_rect: Rect, col: u16, row: u16) -> Option
= nav_rect.right() { return None; } - - let rel_x = col.saturating_sub(nav_rect.x); - - // [◄] width 3: cols 0..3 - if rel_x < 3 { + let layout = nav_layout(nav_rect); + if layout.close.is_some_and(|z| z.contains(col)) { + return Some(BrowserNavHit::Close); + } + if layout.focus.is_some_and(|z| z.contains(col)) { + return Some(BrowserNavHit::ToggleFocus); + } + if layout.back.contains(col) { return Some(BrowserNavHit::Back); } - // space at 3, [►] width 3: cols 4..7 - if (4..7).contains(&rel_x) { + if layout.forward.contains(col) { return Some(BrowserNavHit::Forward); } - // space at 7, [↻] width 3: cols 8..11 (or 3 UTF-8 / char width) - if (8..12).contains(&rel_x) { + if layout.reload.contains(col) { return Some(BrowserNavHit::Reload); } + Some(BrowserNavHit::UrlBar) +} - let close_w = 3u16; - let focus_w = 14u16; - let total_right = close_w + 1 + focus_w; +#[cfg(test)] +mod tests { + use super::*; - if col >= nav_rect.right().saturating_sub(close_w) { - return Some(BrowserNavHit::Close); + fn row(buf: &Buffer, rect: Rect) -> Vec { + (rect.x..rect.right()).map(|c| buf[(c, rect.y)].symbol().to_string()).collect() } - if col >= nav_rect.right().saturating_sub(total_right) - && col < nav_rect.right().saturating_sub(close_w + 1) - { - return Some(BrowserNavHit::ToggleFocus); + + /// Column where `label` is drawn, found by matching its first cell. + fn drawn_at(cells: &[String], label: &str) -> usize { + let chars: Vec = label.chars().map(|c| c.to_string()).collect(); + (0..cells.len()) + .find(|&i| chars.iter().enumerate().all(|(k, ch)| cells.get(i + k) == Some(ch))) + .unwrap_or_else(|| panic!("{label} not drawn in {cells:?}")) } - Some(BrowserNavHit::UrlBar) + #[test] + fn every_drawn_button_is_hit_where_it_is_drawn() { + for focused in [false, true] { + let mut state = BrowserCanvasState::new_synthetic("test://page", 320, 200); + state.set_focus(focused); + let rect = Rect::new(3, 2, 70, 1); + let mut buf = Buffer::empty(Rect::new(0, 0, 80, 4)); + render_browser_nav_bar(&mut buf, rect, &state, true); + let cells = row(&buf, rect); + let focus_label = if focused { FOCUS_WEB_LABEL } else { FOCUS_PROMPT_LABEL }; + for (label, hit) in [ + (BACK_LABEL, BrowserNavHit::Back), + (FORWARD_LABEL, BrowserNavHit::Forward), + (RELOAD_LABEL, BrowserNavHit::Reload), + (focus_label, BrowserNavHit::ToggleFocus), + (CLOSE_LABEL, BrowserNavHit::Close), + ] { + let start = drawn_at(&cells, label); + for k in 0..label.chars().count() { + let col = rect.x + (start + k) as u16; + assert_eq!(hit_test_browser_nav_bar(rect, col, rect.y), Some(hit), "{label} col {k} focused={focused}"); + } + } + } + } + + #[test] + fn gaps_and_url_zone_hit_the_url_bar_and_zones_use_display_columns() { + let rect = Rect::new(0, 0, 60, 1); + let l = nav_layout(rect); + // "[◄]" is 3 columns wide although it is 5 bytes long. + assert_eq!(l.back, Zone { x: 0, width: 3 }); + assert_eq!(l.forward, Zone { x: 4, width: 3 }); + assert_eq!(l.reload, Zone { x: 8, width: 3 }); + assert_eq!(l.url.x, 12); + assert_eq!(l.close, Some(Zone { x: 57, width: 3 })); + assert_eq!(l.focus, Some(Zone { x: 42, width: 14 })); + assert_eq!(hit_test_browser_nav_bar(rect, 11, 0), Some(BrowserNavHit::UrlBar), "column after reload is not reload"); + assert_eq!(hit_test_browser_nav_bar(rect, 20, 0), Some(BrowserNavHit::UrlBar)); + assert_eq!(hit_test_browser_nav_bar(rect, 56, 0), Some(BrowserNavHit::UrlBar), "gap before close"); + assert_eq!(hit_test_browser_nav_bar(rect, 20, 1), None); + assert_eq!(hit_test_browser_nav_bar(rect, 60, 0), None); + } + + #[test] + fn narrow_bars_drop_the_right_buttons_instead_of_overlapping() { + let rect = Rect::new(0, 0, 20, 1); + let l = nav_layout(rect); + assert_eq!((l.focus, l.close), (None, None)); + assert_eq!(hit_test_browser_nav_bar(rect, 19, 0), Some(BrowserNavHit::UrlBar)); + let state = BrowserCanvasState::new_synthetic("test://page", 320, 200); + let mut buf = Buffer::empty(rect); + render_browser_nav_bar(&mut buf, rect, &state, false); + assert!(!row(&buf, rect).concat().contains('✕')); + } + + #[test] + fn lock_icon_only_for_https_and_title_is_shown() { + let https = BrowserCanvasState::new_synthetic("test://x", 320, 200); + assert!(!url_zone_text(&https).contains('🔒'), "non-https has no lock"); + let mut s = BrowserCanvasState::new_synthetic("test://x", 320, 200); + s.navigate("https://example.com").unwrap(); + let text = url_zone_text(&s); + assert!(text.starts_with("🔒 https://example.com"), "{text}"); + assert!(text.contains("— Synthetic - https://example.com"), "title in the nav bar: {text}"); + s.navigate("http://example.com").unwrap(); + assert!(!url_zone_text(&s).contains('🔒')); + assert!(s.navigate("javascript:alert(1)").is_err()); + assert!(url_zone_text(&s).contains("⚠ blocked URL"), "{}", url_zone_text(&s)); + } + + #[test] + fn fit_cols_never_splits_a_wide_glyph() { + assert_eq!(fit_cols("🔒 ab", 2), "🔒"); + assert_eq!(fit_cols("🔒 ab", 1), ""); + assert_eq!(fit_cols("abc", 10), "abc"); + } } diff --git a/src/pane/browser_canvas/geometry.rs b/src/pane/browser_canvas/geometry.rs new file mode 100644 index 00000000..b995a9ce --- /dev/null +++ b/src/pane/browser_canvas/geometry.rs @@ -0,0 +1,76 @@ +//! Cell-to-pixel geometry for the browser viewport (`WEB-02`). +//! +//! The frame is always stretched over the whole viewport cell rect (half-blocks in the TUI, a +//! scaled texture in the GUI), so a cell maps to the frame by proportion. No fixed cell size. + +use ratatui::layout::Rect; + +/// Nominal pixels rendered per terminal cell when sizing the page (cells are about 1:2). +pub const CELL_PX_W: u16 = 10; +pub const CELL_PX_H: u16 = 20; +pub const MIN_VIEWPORT_PX: (u16, u16) = (320, 200); +pub const MAX_VIEWPORT_PX: (u16, u16) = (3840, 2160); + +/// Viewport cell rect below the 1-row nav bar. +pub fn viewport_body(canvas_rect: Rect) -> Rect { + Rect::new(canvas_rect.x, canvas_rect.y.saturating_add(1), canvas_rect.width, canvas_rect.height.saturating_sub(1)) +} + +/// Page size in pixels for a canvas of `canvas_cols x canvas_rows` cells (nav bar row excluded). +pub fn viewport_px_for_cells(canvas_cols: u16, canvas_rows: u16) -> (u16, u16) { + let w = canvas_cols.saturating_mul(CELL_PX_W).clamp(MIN_VIEWPORT_PX.0, MAX_VIEWPORT_PX.0); + let h = canvas_rows.saturating_sub(1).saturating_mul(CELL_PX_H).clamp(MIN_VIEWPORT_PX.1, MAX_VIEWPORT_PX.1); + (w, h) +} + +/// Maps the centre of cell `(col, row)` inside `body` to a pixel of a `frame_w x frame_h` frame. +pub fn cell_to_frame_px(body: Rect, col: u16, row: u16, frame_w: u16, frame_h: u16) -> Option<(u16, u16)> { + if body.width == 0 || body.height == 0 || frame_w == 0 || frame_h == 0 { + return None; + } + if col < body.x || col >= body.right() || row < body.y || row >= body.bottom() { + return None; + } + let centre = |cell: u16, cells: u16, px: u16| { + let v = (u32::from(cell) * 2 + 1) * u32::from(px) / (u32::from(cells) * 2); + v.min(u32::from(px) - 1) as u16 + }; + Some((centre(col - body.x, body.width, frame_w), centre(row - body.y, body.height, frame_h))) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn cells_map_to_their_centre_in_the_frame() { + let body = Rect::new(2, 5, 80, 23); + // 800x460 frame: each cell covers 10x20 px. + assert_eq!(cell_to_frame_px(body, 2, 5, 800, 460), Some((5, 10))); + assert_eq!(cell_to_frame_px(body, 81, 27, 800, 460), Some((795, 450))); + // A GUI frame of another size maps by proportion, not by a fixed cell size. + assert_eq!(cell_to_frame_px(body, 42, 5, 1600, 920), Some((810, 20))); + assert_eq!(cell_to_frame_px(body, 42, 16, 400, 230), Some((202, 115))); + } + + #[test] + fn points_outside_the_body_or_degenerate_frames_map_to_nothing() { + let body = Rect::new(2, 5, 80, 23); + assert_eq!(cell_to_frame_px(body, 1, 5, 800, 460), None); + assert_eq!(cell_to_frame_px(body, 82, 5, 800, 460), None); + assert_eq!(cell_to_frame_px(body, 2, 4, 800, 460), None); + assert_eq!(cell_to_frame_px(body, 2, 28, 800, 460), None); + assert_eq!(cell_to_frame_px(body, 2, 5, 0, 460), None); + assert_eq!(cell_to_frame_px(Rect::new(0, 0, 0, 0), 0, 0, 800, 460), None); + // A frame narrower than the cell grid still yields in-range pixels. + assert_eq!(cell_to_frame_px(body, 81, 27, 40, 10), Some((39, 9))); + } + + #[test] + fn viewport_size_follows_the_cell_grid_within_limits() { + assert_eq!(viewport_px_for_cells(80, 24), (800, 460)); + assert_eq!(viewport_px_for_cells(10, 3), MIN_VIEWPORT_PX); + assert_eq!(viewport_px_for_cells(1000, 400), MAX_VIEWPORT_PX); + assert_eq!(viewport_body(Rect::new(1, 2, 80, 24)), Rect::new(1, 3, 80, 23)); + } +} diff --git a/src/pane/browser_canvas/mod.rs b/src/pane/browser_canvas/mod.rs index 750e306f..77e8f129 100644 --- a/src/pane/browser_canvas/mod.rs +++ b/src/pane/browser_canvas/mod.rs @@ -8,10 +8,20 @@ use crate::error::Result; use crate::pane::PaneId; pub mod cdp; +pub mod cdp_input; +#[cfg(unix)] +pub mod cdp_launch; +pub mod cdp_proto; +pub mod cdp_session; pub mod chrome_ui; +pub mod geometry; +mod pane_hooks; +pub mod state; +pub mod url_policy; pub use cdp::CdpBrowserBackend; -pub use chrome_ui::{BrowserNavHit, hit_test_browser_nav_bar, render_browser_nav_bar}; +pub use chrome_ui::{BrowserNavHit, hit_test_browser_nav_bar, render_browser_nav_bar, render_browser_status_overlay}; +pub use state::{BrowserCanvasState, UrlEditState}; /// Shared RGBA frame buffer with monotonic sequence counter for zero-copy GUI texture upload. #[derive(Clone, Debug, PartialEq, Eq)] @@ -112,254 +122,57 @@ pub trait BrowserEngine: Send { fn current_url(&self) -> &str; fn page_title(&self) -> &str; fn is_loading(&self) -> bool; + /// `"cdp"` or `"synthetic"`: the engine that is really running. fn backend_name(&self) -> &'static str; -} - -/// Stateful browser canvas manager owned by [`crate::pane::CompositePane`]. -pub struct BrowserCanvasState { - engine: Box, - pub viewport_focused: bool, - pub zoom_pct: u16, - pub history: Vec, - pub history_idx: usize, - cached_frame: Option, - url_edit: Option, -} - -/// In-progress URL bar edit: text buffer plus cursor (in chars). -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct UrlEditState { - pub text: String, - pub cursor: usize, -} - -impl BrowserCanvasState { - pub fn new(mut engine: Box) -> Self { - let initial_url = engine.current_url().to_string(); - let cached_frame = engine.latest_frame(); - Self { - engine, - viewport_focused: false, - zoom_pct: 100, - history: vec![initial_url], - history_idx: 0, - cached_frame, - url_edit: None, - } - } - - pub fn new_synthetic(initial_url: &str, width_px: u16, height_px: u16) -> Self { - let engine = Box::new(SyntheticBrowserBackend::new(initial_url, width_px, height_px)); - Self::new(engine) - } - - pub fn new_cdp_or_synthetic( - initial_url: &str, - width_px: u16, - height_px: u16, - pane_id: PaneId, - ) -> Self { - if initial_url.starts_with("demo://") - || initial_url.starts_with("about:") - || initial_url.starts_with("test://") - || std::env::var("BILTERM_BROWSER_SYNTHETIC").is_ok() - || std::env::var("BILTERM_SYNTHETIC_MEDIA").is_ok() - || (cfg!(test) && std::env::var("BILTERM_BROWSER_CDP").is_err()) - { - return Self::new_synthetic(initial_url, width_px, height_px); - } - match cdp::CdpBrowserBackend::try_spawn(initial_url, width_px, height_px, pane_id) { - Ok(backend) => Self::new(Box::new(backend)), - Err(_) => Self::new_synthetic(initial_url, width_px, height_px), - } - } - - pub fn navigate(&mut self, url: &str) -> Result<()> { - self.engine.navigate(url)?; - if self.history_idx + 1 < self.history.len() { - self.history.truncate(self.history_idx + 1); - } - self.history.push(url.to_string()); - self.history_idx = self.history.len().saturating_sub(1); - self.cached_frame = self.engine.latest_frame(); - Ok(()) - } - - pub fn go_back(&mut self) -> Result<()> { - if self.history_idx > 0 { - self.history_idx -= 1; - let url = self.history[self.history_idx].clone(); - self.engine.navigate(&url)?; - self.cached_frame = self.engine.latest_frame(); - } else { - self.engine.go_back()?; - } - Ok(()) + /// Pulls state published by a background engine. `true` when url/title/loading/health changed. + fn poll(&mut self) -> bool { + false } - - pub fn go_forward(&mut self) -> Result<()> { - if self.history_idx + 1 < self.history.len() { - self.history_idx += 1; - let url = self.history[self.history_idx].clone(); - self.engine.navigate(&url)?; - self.cached_frame = self.engine.latest_frame(); - } else { - self.engine.go_forward()?; - } - Ok(()) + fn health(&self) -> EngineHealth { + EngineHealth::Ready } - - pub fn reload(&mut self) -> Result<()> { - self.engine.reload()?; - self.cached_frame = self.engine.latest_frame(); + /// Inserts pasted text at the page's focused element. + fn insert_text(&mut self, _text: &str) -> Result<()> { Ok(()) } - - pub fn stop(&mut self) -> Result<()> { - self.engine.stop() + fn can_go_back(&self) -> bool { + false } - - pub fn resize_viewport(&mut self, width_px: u16, height_px: u16, scale_factor: f32) -> Result<()> { - self.engine.resize_viewport(width_px, height_px, scale_factor)?; - self.cached_frame = self.engine.latest_frame(); - Ok(()) + fn can_go_forward(&self) -> bool { + false } - - pub fn set_zoom(&mut self, zoom_pct: u16) { - self.zoom_pct = zoom_pct.clamp(25, 500); + /// Process group id and profile directory of an out-of-process engine. + fn process_info(&self) -> Option<(i32, std::path::PathBuf)> { + None } - - pub fn zoom_delta(&mut self, delta_pct: i16) { - let current = self.zoom_pct as i32; - let next = (current + delta_pct as i32).clamp(25, 500) as u16; - self.zoom_pct = next; - } - - pub fn toggle_focus(&mut self) { - self.viewport_focused = !self.viewport_focused; - } - - pub fn set_focus(&mut self, focused: bool) { - self.viewport_focused = focused; - } - - pub fn dispatch_mouse(&mut self, event: MediaMouseEvent) -> Result<()> { - self.engine.dispatch_mouse(event)?; - self.cached_frame = self.engine.latest_frame(); - Ok(()) - } - - pub fn dispatch_key(&mut self, key: KeyEvent) -> Result<()> { - self.engine.dispatch_key(key)?; - self.cached_frame = self.engine.latest_frame(); - Ok(()) - } - - pub fn poll_screencast_frame(&mut self) -> bool { - if let Some(f) = self.engine.latest_frame() { - let changed = self.cached_frame.as_ref().map(|c| c.seq) != Some(f.seq); - self.cached_frame = Some(f); - changed - } else { - false - } - } - - pub fn is_editing_url(&self) -> bool { - self.url_edit.is_some() - } - - pub fn url_edit(&self) -> Option<&UrlEditState> { - self.url_edit.as_ref() - } - - /// Enters URL edit mode prefilled with the current URL (cursor at end). - pub fn begin_url_edit(&mut self) { - let text = self.engine.current_url().to_string(); - let cursor = text.chars().count(); - self.url_edit = Some(UrlEditState { text, cursor }); - } - - pub fn cancel_url_edit(&mut self) { - self.url_edit = None; - } - - /// Commits the edit, navigating to the buffer (prefixing `https://` when no scheme). - pub fn commit_url_edit(&mut self) -> Result<()> { - let Some(edit) = self.url_edit.take() else { - return Ok(()); - }; - let t = edit.text.trim(); - if t.is_empty() { - return Ok(()); - } - let url = if t.contains("://") || t.starts_with("about:") { - t.to_string() - } else { - format!("https://{t}") - }; - self.navigate(&url) - } - - /// Routes a key to the URL editor. Returns `true` when consumed (always while editing). - pub fn handle_url_edit_key(&mut self, key: KeyEvent) -> bool { - use crossterm::event::KeyCode; - let Some(edit) = self.url_edit.as_mut() else { - return false; - }; - let ctrl = key.modifiers.contains(KeyModifiers::CONTROL); - let len = edit.text.chars().count(); - let byte_at = |t: &str, i: usize| t.char_indices().nth(i).map_or(t.len(), |(b, _)| b); - match key.code { - KeyCode::Enter => { - let _ = self.commit_url_edit(); - } - KeyCode::Esc => self.url_edit = None, - KeyCode::Char('u') if ctrl => { - edit.text.clear(); - edit.cursor = 0; - } - KeyCode::Char(c) if !ctrl => { - let b = byte_at(&edit.text, edit.cursor); - edit.text.insert(b, c); - edit.cursor += 1; - } - KeyCode::Backspace if edit.cursor > 0 => { - let b = byte_at(&edit.text, edit.cursor - 1); - edit.text.remove(b); - edit.cursor -= 1; - } - KeyCode::Delete if edit.cursor < len => { - let b = byte_at(&edit.text, edit.cursor); - edit.text.remove(b); - } - KeyCode::Left => edit.cursor = edit.cursor.saturating_sub(1), - KeyCode::Right => edit.cursor = (edit.cursor + 1).min(len), - KeyCode::Home => edit.cursor = 0, - KeyCode::End => edit.cursor = len, - _ => {} - } - true - } - - pub fn cached_frame(&self) -> Option<&MediaRgbaFrame> { - self.cached_frame.as_ref() - } - - pub fn current_url(&self) -> &str { - self.engine.current_url() + /// Last navigation error reported by the engine (for example `net::ERR_NAME_NOT_RESOLVED`). + fn nav_error(&self) -> Option<&str> { + None } +} - pub fn page_title(&self) -> &str { - self.engine.page_title() - } +/// Health of a browser engine, as seen from the UI thread. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum EngineHealth { + /// Launched, handshake not finished yet. + Starting, + Ready, + /// Was running, then the page or the browser process died. + Crashed(String), + /// Never came up; the canvas falls back to the synthetic engine. + Failed(String), +} - pub fn is_loading(&self) -> bool { - self.engine.is_loading() - } +/// Builds the error used for browser failures (shown in the status line without an I/O prefix). +pub fn browser_error(kind: std::io::ErrorKind, msg: impl Into) -> crate::error::BiltermError { + crate::error::BiltermError::Io(std::io::Error::new(kind, msg.into())) +} - pub fn backend_name(&self) -> &'static str { - self.engine.backend_name() +/// Text of a browser error for status lines. +pub fn browser_error_text(err: &crate::error::BiltermError) -> String { + match err { + crate::error::BiltermError::Io(e) => e.to_string(), + other => other.to_string(), } } @@ -373,6 +186,8 @@ pub struct SyntheticBrowserBackend { frame_seq: u64, last_click: Option<(u16, u16)>, latest_frame: Option, + history: Vec, + history_idx: usize, } impl SyntheticBrowserBackend { @@ -386,11 +201,22 @@ impl SyntheticBrowserBackend { frame_seq: 0, last_click: None, latest_frame: None, + history: vec![initial_url.to_string()], + history_idx: 0, }; backend.render_synthetic_frame(); backend } + fn show_history_entry(&mut self, idx: usize) { + if let Some(url) = self.history.get(idx).cloned() { + self.history_idx = idx; + self.title = format!("Synthetic - {url}"); + self.url = url; + } + self.render_synthetic_frame(); + } + fn render_synthetic_frame(&mut self) { self.frame_seq += 1; let w = self.width_px as usize; @@ -464,20 +290,31 @@ impl BrowserEngine for SyntheticBrowserBackend { fn navigate(&mut self, url: &str) -> Result<()> { self.url = url.to_string(); self.title = format!("Synthetic - {url}"); + self.history.truncate(self.history_idx + 1); + self.history.push(url.to_string()); + self.history_idx = self.history.len() - 1; self.render_synthetic_frame(); Ok(()) } fn go_back(&mut self) -> Result<()> { - self.render_synthetic_frame(); + self.show_history_entry(self.history_idx.saturating_sub(1)); Ok(()) } fn go_forward(&mut self) -> Result<()> { - self.render_synthetic_frame(); + self.show_history_entry((self.history_idx + 1).min(self.history.len().saturating_sub(1))); Ok(()) } + fn can_go_back(&self) -> bool { + self.history_idx > 0 + } + + fn can_go_forward(&self) -> bool { + self.history_idx + 1 < self.history.len() + } + fn reload(&mut self) -> Result<()> { self.render_synthetic_frame(); Ok(()) diff --git a/src/pane/browser_canvas/pane_hooks.rs b/src/pane/browser_canvas/pane_hooks.rs new file mode 100644 index 00000000..1f05982d --- /dev/null +++ b/src/pane/browser_canvas/pane_hooks.rs @@ -0,0 +1,45 @@ +//! `CompositePane` hooks for the browser canvas: opening and viewport sizing (`WEB-01`). +//! +//! Kept here so `src/pane/mod.rs` only carries one-line calls. + +use crate::pane::CompositePane; + +use super::geometry::viewport_px_for_cells; +use super::{BrowserCanvasState, browser_error_text}; + +impl CompositePane { + /// Page size in pixels for the current canvas (`last_canvas_size` is `(rows, cols)`). + fn browser_viewport_px(&self) -> (u16, u16) { + let (rows, cols) = self.last_canvas_size; + viewport_px_for_cells(cols, rows) + } + + /// Opens the browser canvas on `url`. A URL refused by the policy opens nothing and is + /// reported in the status line. + pub(in crate::pane) fn open_browser_canvas(&mut self, url: &str) { + let url = match BrowserCanvasState::check_url(url) { + Ok(u) => u, + Err(e) => { + self.set_status_message(Some(format!("Browser: {}", browser_error_text(&e)))); + return; + } + }; + let (width_px, height_px) = self.browser_viewport_px(); + self.browser_canvas = Some(BrowserCanvasState::new_cdp_or_synthetic(&url, width_px, height_px, self.id)); + } + + /// Resizes the page to the pane's canvas. Called whenever the canvas cell size changes. + pub(in crate::pane) fn sync_browser_viewport(&mut self) { + let (width_px, height_px) = self.browser_viewport_px(); + if let Some(b) = self.browser_canvas.as_mut() { + b.set_viewport_px(width_px, height_px); + } + } +} + +impl CompositePane { + /// `true` when this pane shows a browser or video canvas. + pub fn has_media_canvas(&self) -> bool { + self.browser_canvas.is_some() || self.video_canvas.is_some() + } +} diff --git a/src/pane/browser_canvas/state.rs b/src/pane/browser_canvas/state.rs new file mode 100644 index 00000000..2acc35ec --- /dev/null +++ b/src/pane/browser_canvas/state.rs @@ -0,0 +1,422 @@ +//! `BrowserCanvasState`: the browser canvas owned by a pane (engine, focus, zoom, URL editor). + +use std::sync::OnceLock; + +use crossterm::event::{KeyEvent, KeyModifiers}; +use ratatui::layout::Rect; + +use crate::error::Result; +use crate::pane::PaneId; + +use super::url_policy::{file_urls_enabled, validate_url}; +use super::{ + BrowserEngine, EngineHealth, MediaMouseEvent, MediaRgbaFrame, SyntheticBrowserBackend, browser_error, + browser_error_text, cdp, geometry, +}; + +/// In-progress URL bar edit: text buffer plus cursor (in chars). +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct UrlEditState { + pub text: String, + pub cursor: usize, +} + +/// `true` inside a cargo test binary (unit or integration): those live in `target//deps`. +/// `cfg!(test)` alone is false for integration tests, which link the library without it. +pub fn under_test_harness() -> bool { + static HARNESS: OnceLock = OnceLock::new(); + cfg!(test) + || *HARNESS.get_or_init(|| { + std::env::var_os("NEXTEST").is_some() + || std::env::current_exe() + .ok() + .and_then(|exe| exe.parent().and_then(|d| d.file_name()).map(|n| n == "deps")) + .unwrap_or(false) + }) +} + +/// Schemes served by the synthetic engine only; they never reach Chrome. +pub fn is_synthetic_url(url: &str) -> bool { + url.starts_with("demo://") || url.starts_with("test://") || url.starts_with("about:") +} + +/// Why the synthetic engine must be used, or `None` when real Chrome may be launched. +/// +/// Under a test harness a real browser is launched only with `BILTERM_BROWSER_CDP=1`. +fn forced_synthetic(url: &str) -> bool { + is_synthetic_url(url) + || std::env::var_os("BILTERM_BROWSER_SYNTHETIC").is_some() + || std::env::var_os("BILTERM_SYNTHETIC_MEDIA").is_some() + || (under_test_harness() && std::env::var("BILTERM_BROWSER_CDP").as_deref() != Ok("1")) +} + +/// Stateful browser canvas manager owned by [`crate::pane::CompositePane`]. +pub struct BrowserCanvasState { + engine: Box, + pub viewport_focused: bool, + pub zoom_pct: u16, + cached_frame: Option, + url_edit: Option, + /// Why the synthetic engine runs instead of Chrome (`None`: by request, or CDP is running). + fallback: Option, + /// Last refused or failed navigation, shown in the nav bar. + notice: Option, + pane_id: Option, + viewport_px: (u16, u16), +} + +impl BrowserCanvasState { + pub fn new(mut engine: Box) -> Self { + let cached_frame = engine.latest_frame(); + let viewport_px = cached_frame.as_ref().map_or((800, 600), |f| (f.width_px, f.height_px)); + Self { + engine, + viewport_focused: false, + zoom_pct: 100, + cached_frame, + url_edit: None, + fallback: None, + notice: None, + pane_id: None, + viewport_px, + } + } + + pub fn new_synthetic(initial_url: &str, width_px: u16, height_px: u16) -> Self { + let mut state = Self::new(Box::new(SyntheticBrowserBackend::new(initial_url, width_px, height_px))); + state.viewport_px = (width_px, height_px); + state + } + + /// Opens `initial_url` in real Chrome when possible, otherwise in the synthetic engine with + /// the reason recorded (see [`Self::backend_name`]). Never blocks on Chrome: the CDP engine + /// reports `Starting` until its handshake finishes. + pub fn new_cdp_or_synthetic(initial_url: &str, width_px: u16, height_px: u16, pane_id: PaneId) -> Self { + let mut state = if forced_synthetic(initial_url) { + Self::new_synthetic(initial_url, width_px, height_px) + } else { + match cdp::CdpBrowserBackend::try_spawn(initial_url, width_px, height_px, pane_id) { + Ok(backend) => Self::new(Box::new(backend)), + Err(reason) => { + let mut s = Self::new_synthetic(initial_url, width_px, height_px); + s.fallback = Some(reason.to_string()); + s + } + } + }; + state.pane_id = Some(pane_id); + state.viewport_px = (width_px, height_px); + state + } + + /// Applies the URL policy. Synthetic-only schemes pass when the synthetic engine is running. + fn admit(&self, url: &str) -> Result { + if self.engine.backend_name() == "synthetic" && is_synthetic_url(url.trim()) { + return Ok(url.trim().to_string()); + } + validate_url(url, file_urls_enabled()) + .map_err(|e| browser_error(std::io::ErrorKind::InvalidInput, format!("blocked URL: {e}"))) + } + + /// Checks `url` against the URL policy without navigating. + pub fn check_url(url: &str) -> Result { + if is_synthetic_url(url.trim()) { + return Ok(url.trim().to_string()); + } + validate_url(url, file_urls_enabled()) + .map_err(|e| browser_error(std::io::ErrorKind::InvalidInput, format!("blocked URL: {e}"))) + } + + fn track(&mut self, result: Result) -> Result { + self.notice = result.as_ref().err().map(browser_error_text); + result + } + + pub fn navigate(&mut self, url: &str) -> Result<()> { + let result = self.admit(url).and_then(|u| self.engine.navigate(&u)); + self.cached_frame = self.engine.latest_frame().or(self.cached_frame.take()); + self.track(result) + } + + pub fn go_back(&mut self) -> Result<()> { + let result = self.engine.go_back(); + self.refresh_frame(); + self.track(result) + } + + pub fn go_forward(&mut self) -> Result<()> { + let result = self.engine.go_forward(); + self.refresh_frame(); + self.track(result) + } + + /// Reloads the page. After a crash this relaunches the browser on the last URL. + pub fn reload(&mut self) -> Result<()> { + if matches!(self.engine.health(), EngineHealth::Crashed(_)) { + let url = self.engine.current_url().to_string(); + let (focused, zoom) = (self.viewport_focused, self.zoom_pct); + let (w, h) = self.viewport_px; + *self = match self.pane_id { + Some(id) => Self::new_cdp_or_synthetic(&url, w, h, id), + None => Self::new_synthetic(&url, w, h), + }; + self.viewport_focused = focused; + self.zoom_pct = zoom; + return self.apply_viewport(); + } + let result = self.engine.reload(); + self.refresh_frame(); + self.track(result) + } + + pub fn stop(&mut self) -> Result<()> { + self.engine.stop() + } + + fn refresh_frame(&mut self) { + if let Some(f) = self.engine.latest_frame() { + self.cached_frame = Some(f); + } + } + + fn apply_viewport(&mut self) -> Result<()> { + let (w, h) = self.viewport_px; + let result = self.engine.resize_viewport(w, h, f32::from(self.zoom_pct) / 100.0); + self.refresh_frame(); + result + } + + pub fn resize_viewport(&mut self, width_px: u16, height_px: u16, scale_factor: f32) -> Result<()> { + self.viewport_px = (width_px, height_px); + let result = self.engine.resize_viewport(width_px, height_px, scale_factor); + self.refresh_frame(); + result + } + + /// Sets the page size in frame pixels (zoom is applied on top). No-op when unchanged. + pub fn set_viewport_px(&mut self, width_px: u16, height_px: u16) -> bool { + if self.viewport_px == (width_px, height_px) { + return false; + } + self.viewport_px = (width_px, height_px); + let _ = self.apply_viewport(); + true + } + + pub fn viewport_px(&self) -> (u16, u16) { + self.viewport_px + } + + pub fn set_zoom(&mut self, zoom_pct: u16) { + let next = zoom_pct.clamp(25, 500); + if next != self.zoom_pct { + self.zoom_pct = next; + let _ = self.apply_viewport(); + } + } + + pub fn zoom_delta(&mut self, delta_pct: i16) { + let next = (i32::from(self.zoom_pct) + i32::from(delta_pct)).clamp(25, 500) as u16; + self.set_zoom(next); + } + + pub fn toggle_focus(&mut self) { + self.viewport_focused = !self.viewport_focused; + } + + pub fn set_focus(&mut self, focused: bool) { + self.viewport_focused = focused; + } + + pub fn dispatch_mouse(&mut self, event: MediaMouseEvent) -> Result<()> { + let result = self.engine.dispatch_mouse(event); + self.refresh_frame(); + result + } + + pub fn dispatch_key(&mut self, key: KeyEvent) -> Result<()> { + let result = self.engine.dispatch_key(key); + self.refresh_frame(); + result + } + + /// Pastes `text` into the URL editor when it is open, otherwise into the page. + pub fn paste_text(&mut self, text: &str) -> Result<()> { + if let Some(edit) = self.url_edit.as_mut() { + let clean: String = text.chars().filter(|c| !c.is_control()).collect(); + let at = edit.text.char_indices().nth(edit.cursor).map_or(edit.text.len(), |(b, _)| b); + edit.text.insert_str(at, &clean); + edit.cursor += clean.chars().count(); + return Ok(()); + } + self.engine.insert_text(text) + } + + /// Frame pixel under cell `(col, row)` of the viewport `body` rect, for mouse forwarding. + pub fn frame_px_for_cell(&self, body: Rect, col: u16, row: u16) -> Option<(u16, u16)> { + let (w, h) = self.cached_frame.as_ref().map_or(self.viewport_px, |f| (f.width_px, f.height_px)); + geometry::cell_to_frame_px(body, col, row, w, h) + } + + /// Pulls engine state and the newest frame. `true` when something visible changed. + pub fn poll_screencast_frame(&mut self) -> bool { + let mut changed = self.engine.poll(); + match self.engine.health() { + EngineHealth::Failed(reason) => { + let url = self.engine.current_url().to_string(); + let (w, h) = self.viewport_px; + self.engine = Box::new(SyntheticBrowserBackend::new(&url, w, h)); + self.fallback = Some(format!("launch failed: {reason}")); + self.cached_frame = self.engine.latest_frame(); + return true; + } + EngineHealth::Crashed(_) => { + // Never leave a frozen frame on screen after a crash. + return self.cached_frame.take().is_some() || changed; + } + EngineHealth::Starting | EngineHealth::Ready => {} + } + if let Some(f) = self.engine.latest_frame() + && self.cached_frame.as_ref().map(|c| c.seq) != Some(f.seq) + { + self.cached_frame = Some(f); + changed = true; + } + changed + } + + pub fn is_editing_url(&self) -> bool { + self.url_edit.is_some() + } + + pub fn url_edit(&self) -> Option<&UrlEditState> { + self.url_edit.as_ref() + } + + /// Enters URL edit mode prefilled with the current URL (cursor at end). + pub fn begin_url_edit(&mut self) { + let text = self.engine.current_url().to_string(); + let cursor = text.chars().count(); + self.url_edit = Some(UrlEditState { text, cursor }); + } + + pub fn cancel_url_edit(&mut self) { + self.url_edit = None; + } + + /// Commits the edit, navigating to the buffer (bare hosts get `https://`). + pub fn commit_url_edit(&mut self) -> Result<()> { + let Some(edit) = self.url_edit.take() else { + return Ok(()); + }; + let t = edit.text.trim(); + if t.is_empty() { + return Ok(()); + } + self.navigate(t) + } + + /// Routes a key to the URL editor. Returns `true` when consumed (always while editing). + pub fn handle_url_edit_key(&mut self, key: KeyEvent) -> bool { + use crossterm::event::KeyCode; + let Some(edit) = self.url_edit.as_mut() else { + return false; + }; + let ctrl = key.modifiers.contains(KeyModifiers::CONTROL); + let len = edit.text.chars().count(); + let byte_at = |t: &str, i: usize| t.char_indices().nth(i).map_or(t.len(), |(b, _)| b); + match key.code { + KeyCode::Enter => { + let _ = self.commit_url_edit(); + } + KeyCode::Esc => self.url_edit = None, + KeyCode::Char('u') if ctrl => { + edit.text.clear(); + edit.cursor = 0; + } + KeyCode::Char(c) if !ctrl => { + let b = byte_at(&edit.text, edit.cursor); + edit.text.insert(b, c); + edit.cursor += 1; + } + KeyCode::Backspace if edit.cursor > 0 => { + let b = byte_at(&edit.text, edit.cursor - 1); + edit.text.remove(b); + edit.cursor -= 1; + } + KeyCode::Delete if edit.cursor < len => { + let b = byte_at(&edit.text, edit.cursor); + edit.text.remove(b); + } + KeyCode::Left => edit.cursor = edit.cursor.saturating_sub(1), + KeyCode::Right => edit.cursor = (edit.cursor + 1).min(len), + KeyCode::Home => edit.cursor = 0, + KeyCode::End => edit.cursor = len, + _ => {} + } + true + } + + pub fn cached_frame(&self) -> Option<&MediaRgbaFrame> { + self.cached_frame.as_ref() + } + + pub fn current_url(&self) -> &str { + self.engine.current_url() + } + + pub fn page_title(&self) -> &str { + self.engine.page_title() + } + + pub fn is_loading(&self) -> bool { + self.engine.is_loading() + } + + pub fn can_go_back(&self) -> bool { + self.engine.can_go_back() + } + + pub fn can_go_forward(&self) -> bool { + self.engine.can_go_forward() + } + + pub fn health(&self) -> EngineHealth { + self.engine.health() + } + + pub fn is_crashed(&self) -> bool { + matches!(self.engine.health(), EngineHealth::Crashed(_)) + } + + /// What is really running: `cdp` only after the DevTools handshake succeeded. + pub fn backend_name(&self) -> String { + let name = self.engine.backend_name(); + match (self.engine.health(), &self.fallback) { + (EngineHealth::Starting, _) => format!("{name} (starting)"), + (EngineHealth::Crashed(_), _) => format!("{name} (crashed)"), + (EngineHealth::Failed(_), _) => format!("{name} (failed)"), + (EngineHealth::Ready, Some(why)) if name == "synthetic" => format!("synthetic ({why})"), + (EngineHealth::Ready, _) => name.to_string(), + } + } + + /// Process group id and profile directory of the browser process, when one is running. + pub fn process_info(&self) -> Option<(i32, std::path::PathBuf)> { + self.engine.process_info() + } + + /// Message that replaces the page while there is nothing truthful to show. + pub fn overlay_message(&self) -> Option { + match self.engine.health() { + EngineHealth::Starting if self.cached_frame.is_none() => Some("starting Chrome…".to_string()), + EngineHealth::Crashed(reason) => Some(format!("{reason} — press r to reload")), + _ => None, + } + } + + /// Last refused URL or navigation error (`net::ERR_…`), for the nav bar. + pub fn notice(&self) -> Option<&str> { + self.notice.as_deref().or_else(|| self.engine.nav_error()) + } +} diff --git a/src/pane/browser_canvas/url_policy.rs b/src/pane/browser_canvas/url_policy.rs new file mode 100644 index 00000000..79d1792d --- /dev/null +++ b/src/pane/browser_canvas/url_policy.rs @@ -0,0 +1,177 @@ +//! URL policy for the browser canvas: what may be handed to Chrome (`WEB-01`). +//! +//! Only `http://`, `https://` and `about:blank` are allowed by default. `file://` needs the explicit +//! opt-in `BILTERM_BROWSER_ALLOW_FILE=1`. Everything else (`javascript:`, `data:`, `chrome:`, +//! `view-source:`, anything starting with `-`, control characters) is rejected before it reaches the +//! browser process. + +use std::fmt; + +/// Longest URL accepted from the user, in bytes. +pub const MAX_URL_BYTES: usize = 8 * 1024; + +/// Environment opt-in that allows `file://` URLs. +pub const ALLOW_FILE_ENV: &str = "BILTERM_BROWSER_ALLOW_FILE"; + +/// Why a URL was refused. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum UrlRejection { + Empty, + TooLong, + LeadingDash, + ControlCharacter, + MissingHost, + FileNotEnabled, + SchemeNotAllowed(String), +} + +impl fmt::Display for UrlRejection { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Empty => write!(f, "empty URL"), + Self::TooLong => write!(f, "URL longer than {MAX_URL_BYTES} bytes"), + Self::LeadingDash => write!(f, "URL must not start with '-'"), + Self::ControlCharacter => write!(f, "URL contains a control character or space"), + Self::MissingHost => write!(f, "URL has no host"), + Self::FileNotEnabled => write!(f, "file:// URLs need {ALLOW_FILE_ENV}=1"), + Self::SchemeNotAllowed(s) => write!(f, "scheme '{s}:' is not allowed (http, https, about:blank only)"), + } + } +} + +/// `true` when the user opted in to `file://` URLs through the environment. +pub fn file_urls_enabled() -> bool { + matches!(std::env::var(ALLOW_FILE_ENV).as_deref(), Ok("1") | Ok("true") | Ok("yes")) +} + +/// Splits `scheme:rest` when `text` starts with a URL scheme. `host:8080` is not a scheme. +fn split_scheme(text: &str) -> Option<(&str, &str)> { + let colon = text.find(':')?; + let (scheme, rest) = (&text[..colon], &text[colon + 1..]); + let mut chars = scheme.chars(); + let first = chars.next()?; + if !first.is_ascii_alphabetic() || !chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '-' | '.')) { + return None; + } + if rest.starts_with("//") { + return Some((scheme, rest)); + } + // `localhost:8080/path` is host:port. A dotted or `localhost` prefix followed by digits is a port. + let port_len = rest.bytes().take_while(u8::is_ascii_digit).count(); + let after_port = &rest[port_len..]; + if port_len > 0 && (after_port.is_empty() || after_port.starts_with('/') || after_port.starts_with('?')) { + return None; + } + Some((scheme, rest)) +} + +/// Validates a user-supplied URL and returns the exact string to navigate to. +/// +/// A bare host (`example.com`, `localhost:8080/x`) gets the `https://` default. +pub fn validate_url(input: &str, allow_file: bool) -> Result { + let text = input.trim(); + if text.is_empty() { + return Err(UrlRejection::Empty); + } + if text.len() > MAX_URL_BYTES { + return Err(UrlRejection::TooLong); + } + if text.starts_with('-') { + return Err(UrlRejection::LeadingDash); + } + if text.chars().any(|c| c.is_control() || c.is_whitespace()) { + return Err(UrlRejection::ControlCharacter); + } + let Some((scheme, rest)) = split_scheme(text) else { + let host = text.split(['/', '?', '#']).next().unwrap_or(""); + if host.is_empty() { + return Err(UrlRejection::MissingHost); + } + return Ok(format!("https://{text}")); + }; + let scheme_lc = scheme.to_ascii_lowercase(); + match scheme_lc.as_str() { + "http" | "https" => { + let after = rest.strip_prefix("//").ok_or(UrlRejection::MissingHost)?; + let authority = after.split(['/', '?', '#']).next().unwrap_or(""); + let host = authority.rsplit('@').next().unwrap_or(""); + if host.is_empty() || host.starts_with(':') { + return Err(UrlRejection::MissingHost); + } + Ok(format!("{scheme_lc}:{rest}")) + } + "about" if rest.eq_ignore_ascii_case("blank") => Ok("about:blank".to_string()), + "file" => { + if !allow_file { + return Err(UrlRejection::FileNotEnabled); + } + if !rest.starts_with("//") { + return Err(UrlRejection::MissingHost); + } + Ok(format!("file:{rest}")) + } + _ => Err(UrlRejection::SchemeNotAllowed(scheme_lc)), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn allows_http_https_and_about_blank() { + assert_eq!(validate_url("https://example.com/a?b#c", false).unwrap(), "https://example.com/a?b#c"); + assert_eq!(validate_url("HTTP://Example.com", false).unwrap(), "http://Example.com"); + assert_eq!(validate_url(" about:blank ", false).unwrap(), "about:blank"); + assert_eq!(validate_url("http://127.0.0.1:8080/x", false).unwrap(), "http://127.0.0.1:8080/x"); + assert_eq!(validate_url("https://user@host.example/", false).unwrap(), "https://user@host.example/"); + } + + #[test] + fn bare_hosts_default_to_https() { + assert_eq!(validate_url("example.com", false).unwrap(), "https://example.com"); + assert_eq!(validate_url("localhost:8080/x", false).unwrap(), "https://localhost:8080/x"); + assert_eq!(validate_url("rust-lang.org/learn", false).unwrap(), "https://rust-lang.org/learn"); + } + + #[test] + fn rejects_dangerous_schemes() { + for bad in [ + "javascript:alert(1)", + "JavaScript:alert(1)", + "data:text/html,

x

", + "chrome://settings", + "chrome:version", + "view-source:https://example.com", + "about:config", + "blob:https://example.com/1", + "ftp://example.com", + "vbscript:x", + ] { + assert!( + matches!(validate_url(bad, true), Err(UrlRejection::SchemeNotAllowed(_))), + "{bad} must be rejected" + ); + } + } + + #[test] + fn rejects_flags_control_chars_and_empty() { + assert_eq!(validate_url("--remote-debugging-port=9222", false), Err(UrlRejection::LeadingDash)); + assert_eq!(validate_url("-", false), Err(UrlRejection::LeadingDash)); + assert_eq!(validate_url("", false), Err(UrlRejection::Empty)); + assert_eq!(validate_url(" ", false), Err(UrlRejection::Empty)); + assert_eq!(validate_url("https://a.b/\u{0}", false), Err(UrlRejection::ControlCharacter)); + assert_eq!(validate_url("https://a.b/x y", false), Err(UrlRejection::ControlCharacter)); + assert_eq!(validate_url("https://", false), Err(UrlRejection::MissingHost)); + assert_eq!(validate_url("https://:80/", false), Err(UrlRejection::MissingHost)); + assert_eq!(validate_url(&format!("https://a.b/{}", "x".repeat(MAX_URL_BYTES)), false), Err(UrlRejection::TooLong)); + } + + #[test] + fn file_urls_need_the_opt_in() { + assert_eq!(validate_url("file:///etc/hostname", false), Err(UrlRejection::FileNotEnabled)); + assert_eq!(validate_url("file:///etc/hostname", true).unwrap(), "file:///etc/hostname"); + assert_eq!(validate_url("file:etc", true), Err(UrlRejection::MissingHost)); + } +} diff --git a/src/pane/mod.rs b/src/pane/mod.rs index f87be00e..b266f6f6 100644 --- a/src/pane/mod.rs +++ b/src/pane/mod.rs @@ -551,6 +551,7 @@ impl CompositePane { self.last_canvas_size = new_size; self.resize_events += 1; self.vt.set_size(new_size.0, new_size.1); + self.sync_browser_viewport(); self.transport.resize(new_size.0, new_size.1) } @@ -1149,12 +1150,7 @@ impl CompositePane { } pub fn open_browser(&mut self, url: &str) { - let (cols, rows) = self.vt.size(); - let width_px = cols.saturating_mul(10).max(320); - let height_px = rows.saturating_mul(18).max(240); - self.browser_canvas = Some(browser_canvas::BrowserCanvasState::new_cdp_or_synthetic( - url, width_px, height_px, self.id, - )); + self.open_browser_canvas(url); } pub fn close_browser(&mut self) { diff --git a/src/ui/mod.rs b/src/ui/mod.rs index 0ac36183..3e0ef841 100644 --- a/src/ui/mod.rs +++ b/src/ui/mod.rs @@ -159,6 +159,7 @@ impl Widget for CompositePaneWidget<'_> { { let body = Rect::new(canvas.x, canvas.y + 1, canvas.width, canvas.height - 1); rasterize_frame_halfblocks(buf, body, browser.cached_frame()); + crate::pane::browser_canvas::render_browser_status_overlay(buf, body, browser); } if let Some(video) = self.pane.video_canvas() && canvas.height > 1 diff --git a/src/wm/media_tick.rs b/src/wm/media_tick.rs new file mode 100644 index 00000000..0275679c --- /dev/null +++ b/src/wm/media_tick.rs @@ -0,0 +1,103 @@ +//! Window-manager tick for browser and video canvases (`WEB-01..02`, `VID-01..02`). +//! +//! Every pane with a media canvas is polled so its URL, title and health stay current, but a +//! redraw is requested only when a pane that can be on screen got a new frame or state. + +use super::WindowManager; + +impl WindowManager { + /// Polls media canvases. `true` when a visible pane changed and the screen must be redrawn. + pub(crate) fn tick_media_canvases(&mut self, now_ms: u64) -> bool { + let zoomed = self.zoomed_pane; + let mut redraw = false; + for (&id, pane) in self.panes.iter_mut() { + if !pane.has_media_canvas() { + continue; + } + let changed = pane.tick_media_canvases(now_ms); + let visible = zoomed.is_none_or(|z| z == id); + redraw |= changed && visible; + } + let quick = &mut self.quick_terminal; + if quick.pane.has_media_canvas() { + let changed = quick.pane.tick_media_canvases(now_ms); + redraw |= changed && quick.visible; + } + redraw + } +} + +#[cfg(test)] +mod tests { + use std::sync::Arc; + use std::sync::atomic::{AtomicU64, Ordering}; + + use crossterm::event::KeyEvent; + + use crate::error::Result; + use crate::pane::browser_canvas::{BrowserCanvasState, BrowserEngine, MediaMouseEvent, MediaRgbaFrame}; + use crate::wm::WindowManager; + + /// Engine whose frame sequence is driven by the test. + struct ScriptedEngine(Arc); + + impl BrowserEngine for ScriptedEngine { + fn navigate(&mut self, _url: &str) -> Result<()> { Ok(()) } + fn go_back(&mut self) -> Result<()> { Ok(()) } + fn go_forward(&mut self) -> Result<()> { Ok(()) } + fn reload(&mut self) -> Result<()> { Ok(()) } + fn stop(&mut self) -> Result<()> { Ok(()) } + fn resize_viewport(&mut self, _w: u16, _h: u16, _s: f32) -> Result<()> { Ok(()) } + fn dispatch_mouse(&mut self, _e: MediaMouseEvent) -> Result<()> { Ok(()) } + fn dispatch_key(&mut self, _k: KeyEvent) -> Result<()> { Ok(()) } + fn latest_frame(&mut self) -> Option { + Some(MediaRgbaFrame::new(self.0.load(Ordering::SeqCst), 2, 2, 0, vec![0; 16])) + } + fn current_url(&self) -> &str { "test://scripted" } + fn page_title(&self) -> &str { "" } + fn is_loading(&self) -> bool { false } + fn backend_name(&self) -> &'static str { "synthetic" } + } + + fn wm_with_scripted_browser() -> (WindowManager, Arc) { + let mut wm = WindowManager::new_headless(24, 80); + let seq = Arc::new(AtomicU64::new(1)); + let id = wm.active_pane_id(); + wm.pane_mut(id).unwrap().browser_canvas = + Some(BrowserCanvasState::new(Box::new(ScriptedEngine(Arc::clone(&seq))))); + (wm, seq) + } + + #[test] + fn tick_requests_a_redraw_only_when_a_frame_changed() { + let mut plain = WindowManager::new_headless(24, 80); + assert!(!plain.tick_media_canvases(0), "no media canvas, nothing to redraw"); + + let (mut wm, seq) = wm_with_scripted_browser(); + assert!(!wm.tick_media_canvases(1), "the frame cached at open is not new"); + seq.store(2, Ordering::SeqCst); + assert!(wm.tick_media_canvases(2), "a new frame requests a redraw"); + assert!(!wm.tick_media_canvases(3), "the same frame does not"); + } + + #[test] + fn hidden_panes_are_polled_but_do_not_request_a_redraw() { + let (mut wm, seq) = wm_with_scripted_browser(); + let id = wm.active_pane_id(); + wm.zoomed_pane = Some(id + 1000); + seq.store(7, Ordering::SeqCst); + assert!(!wm.tick_media_canvases(1), "pane hidden behind a zoomed pane"); + let cached = wm.pane(id).unwrap().browser_canvas().unwrap().cached_frame().map(|f| f.seq); + assert_eq!(cached, Some(7), "state is still pulled while hidden"); + } + + #[test] + fn poll_all_ptys_runs_the_media_tick() { + let (mut wm, seq) = wm_with_scripted_browser(); + wm.poll_all_ptys(); + seq.store(5, Ordering::SeqCst); + wm.poll_all_ptys(); + let id = wm.active_pane_id(); + assert_eq!(wm.pane(id).unwrap().browser_canvas().unwrap().cached_frame().map(|f| f.seq), Some(5)); + } +} diff --git a/src/wm/mod.rs b/src/wm/mod.rs index 21d048f9..c340c6be 100644 --- a/src/wm/mod.rs +++ b/src/wm/mod.rs @@ -57,6 +57,7 @@ pub mod traffic_monitor; pub mod traffic_monitor_geometry; mod traffic_view; pub mod v2_runtime; +mod media_tick; pub mod window_manage; pub mod window_style; pub mod z_stack; @@ -965,6 +966,7 @@ impl WindowManager { if self.tick_agent_ui(Instant::now()) { changed = true; } + changed |= self.tick_media_canvases(tm_ms); registry::capture::tick(self, Instant::now()); self.sync_clipboard_records(); self.maybe_rebalance_scrollback(Instant::now()); diff --git a/tests/browser_canvas_tui_and_url_edit.rs b/tests/browser_canvas_tui_and_url_edit.rs index c636a557..b2486799 100644 --- a/tests/browser_canvas_tui_and_url_edit.rs +++ b/tests/browser_canvas_tui_and_url_edit.rs @@ -112,6 +112,10 @@ fn url_bar_click_enters_edit_and_enter_navigates_with_scheme() { #[test] fn ctrl_l_edits_and_esc_cancels() { let (mut wm, id, _screen) = setup(); + // Ctrl+L belongs to the shell until the browser viewport has focus. + handle_wm_key_event(&mut wm, KeyEvent::new(KeyCode::Char('l'), KeyModifiers::CONTROL)).unwrap(); + assert!(!wm.pane(id).unwrap().browser_canvas().unwrap().is_editing_url(), "unfocused Ctrl+L is not stolen"); + wm.pane_mut(id).unwrap().browser_canvas_mut().unwrap().set_focus(true); handle_wm_key_event(&mut wm, KeyEvent::new(KeyCode::Char('l'), KeyModifiers::CONTROL)).unwrap(); assert!(wm.pane(id).unwrap().browser_canvas().unwrap().is_editing_url()); handle_wm_key_event(&mut wm, key(KeyCode::Char('x'))).unwrap(); @@ -120,3 +124,83 @@ fn ctrl_l_edits_and_esc_cancels() { assert!(!b.is_editing_url()); assert_eq!(b.current_url(), "https://example.com"); } + +fn frame_seq(wm: &WindowManager, id: usize) -> u64 { + wm.pane(id).unwrap().browser_canvas().unwrap().cached_frame().map_or(0, |f| f.seq) +} + +#[test] +fn focused_viewport_gets_keys_and_ctrl_bracket_returns_them() { + let (mut wm, id, _screen) = setup(); + let ctrl_bracket = KeyEvent::new(KeyCode::Char(']'), KeyModifiers::CONTROL); + + // Unfocused: the key goes to the prompt, not to the page. + let before = frame_seq(&wm, id); + handle_wm_key_event(&mut wm, key(KeyCode::Char('a'))).unwrap(); + assert_eq!(frame_seq(&wm, id), before, "page saw no key"); + assert_eq!(wm.pane(id).unwrap().prompt().text(), "a"); + + // Ctrl+] gives the keyboard to the page. + handle_wm_key_event(&mut wm, ctrl_bracket).unwrap(); + assert!(wm.pane(id).unwrap().browser_canvas().unwrap().viewport_focused); + handle_wm_key_event(&mut wm, key(KeyCode::Char('b'))).unwrap(); + handle_wm_key_event(&mut wm, key(KeyCode::Enter)).unwrap(); + assert_eq!(frame_seq(&wm, id), before + 2, "both keys reached the engine"); + assert_eq!(wm.pane(id).unwrap().prompt().text(), "a", "prompt untouched while the page has focus"); + + // Ctrl+] (legacy terminals send it as Ctrl+5) is the escape hatch back to bilterm. + handle_wm_key_event(&mut wm, KeyEvent::new(KeyCode::Char('5'), KeyModifiers::CONTROL)).unwrap(); + assert!(!wm.pane(id).unwrap().browser_canvas().unwrap().viewport_focused); + handle_wm_key_event(&mut wm, key(KeyCode::Char('c'))).unwrap(); + assert_eq!(wm.pane(id).unwrap().prompt().text(), "ac"); +} + +#[test] +fn leader_chords_still_work_while_the_page_has_focus() { + let (mut wm, id, _screen) = setup(); + wm.pane_mut(id).unwrap().browser_canvas_mut().unwrap().set_focus(true); + handle_wm_key_event(&mut wm, KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL)).unwrap(); + assert!(wm.is_leader_pending(), "leader is resolved before keys are forwarded to the page"); + handle_wm_key_event(&mut wm, key(KeyCode::Char('.'))).unwrap(); + assert!(wm.pane(id).unwrap().browser_canvas().is_none(), "leader . closed the browser"); +} + +#[test] +fn clicking_the_page_focuses_it_and_maps_cells_to_frame_pixels() { + let (mut wm, id, screen) = setup(); + let canvas = wm.pane(id).unwrap().canvas_rect(screen); + let (col, row) = (canvas.x + canvas.width - 1, canvas.bottom() - 1); + let b = wm.pane(id).unwrap().browser_canvas().unwrap(); + let frame = b.cached_frame().unwrap(); + let body = ratatui::layout::Rect::new(canvas.x, canvas.y + 1, canvas.width, canvas.height - 1); + let (x, y) = b.frame_px_for_cell(body, col, row).unwrap(); + assert!(x >= frame.width_px - frame.width_px / canvas.width && x < frame.width_px, "last column maps to the right edge: {x}"); + assert!(y >= frame.height_px - frame.height_px / body.height && y < frame.height_px, "last row maps to the bottom edge: {y}"); + assert_eq!(b.frame_px_for_cell(body, canvas.x, canvas.y), None, "the nav bar row is not part of the page"); + + let seq = frame.seq; + let click = MouseEvent { kind: MouseEventKind::Down(MouseButton::Left), column: col, row, modifiers: KeyModifiers::NONE }; + handle_wm_mouse_event(&mut wm, click, screen).unwrap(); + let b = wm.pane(id).unwrap().browser_canvas().unwrap(); + assert!(b.viewport_focused, "a click on the page gives it the keyboard"); + assert!(b.cached_frame().unwrap().seq > seq, "the click reached the engine"); +} + +#[test] +fn blocked_urls_never_open_or_navigate() { + let (mut wm, id, _screen) = setup(); + for bad in ["javascript:alert(1)", "data:text/html,x", "chrome://settings", "--no-sandbox", "file:///etc/passwd"] { + let cmd = bilterm::colon_command::parse_colon_command(&format!(":browser open {bad}")); + if let Some(Ok(action)) = cmd { + wm.apply(action).unwrap(); + } + assert_eq!(wm.pane(id).unwrap().browser_canvas().unwrap().current_url(), "https://example.com", "{bad}"); + } + // With no canvas open, a refused URL opens nothing. + wm.pane_mut(id).unwrap().close_browser(); + wm.pane_mut(id).unwrap().open_browser("javascript:alert(1)"); + assert!(wm.pane(id).unwrap().browser_canvas().is_none()); + // The synthetic engine is reported as what it is, and tests never start a real browser. + wm.pane_mut(id).unwrap().open_browser("https://example.com"); + assert_eq!(wm.pane(id).unwrap().browser_canvas().unwrap().backend_name(), "synthetic"); +} diff --git a/tests/phase64_layer_dag.rs b/tests/phase64_layer_dag.rs index 1d56b1a2..554fd3fa 100644 --- a/tests/phase64_layer_dag.rs +++ b/tests/phase64_layer_dag.rs @@ -132,6 +132,8 @@ pub const BOUNDARY_EXEMPT: &[(&str, &str)] = &[ ("src/stt/audio.rs", "microphone capture child process, not terminal traffic"), ("src/stt/mod.rs", "connect-only reachability probe of the STT endpoint in VoicePreflight; no bytes are exchanged and the stream is dropped"), ("src/pane/video_canvas/ffmpeg.rs", "video decode child process pipes, not terminal traffic"), + ("src/pane/browser_canvas/cdp_launch.rs", "headless Chrome child process and its DevTools pipe (fd 3/4), not terminal traffic"), + ("src/pane/browser_canvas/cdp_session.rs", "reader/writer threads on the Chrome DevTools pipe, not terminal traffic"), ("src/swarm/heal.rs", "self-heal verify-command child process pipes, not terminal traffic"), ("src/daemon/mod.rs", "connect-only liveness probe of the daemon socket; no bytes are exchanged and the stream is dropped (64-02 Task 0 audit)"), ("src/swarm/heal.rs", "verify_cmd child-process stdout/stderr pipes for the Phase 69 self-heal loop, not terminal traffic"), diff --git a/tests/video_canvas_tui_and_ffmpeg.rs b/tests/video_canvas_tui_and_ffmpeg.rs index 5a995513..6cbd608c 100644 --- a/tests/video_canvas_tui_and_ffmpeg.rs +++ b/tests/video_canvas_tui_and_ffmpeg.rs @@ -130,13 +130,13 @@ fn browser_status_names_backend_and_surface() { let mut wm = WindowManager::new_headless(24, 80); run(&mut wm, ":browser open demo://page"); let s = status(&wm); - assert!(s.contains("synthetic preview"), "{s}"); + assert!(s.contains("(synthetic)"), "{s}"); assert!(s.contains("[TUI half-block; run bilterm --gui for GPU canvas]"), "{s}"); + // Under a test harness a real browser is never launched (needs BILTERM_BROWSER_CDP=1), and + // the status line says which engine is really running. run(&mut wm, ":browser open https://example.com"); let s = status(&wm); - assert!( - s.contains("CDP") || s.contains("synthetic preview (Chrome not found)"), - "{s}" - ); + assert!(s.contains("https://example.com (synthetic)"), "{s}"); + assert!(!s.contains("cdp"), "{s}"); } From 7ff23adcf84e8f4bb25e16d656336a3a1c43f1ca Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 01:17:49 -0500 Subject: [PATCH 3/8] test(browser): live end-to-end test against real Chrome Gated on BILTERM_BROWSER_CDP=1. Serves two pages from a loopback TcpListener, then checks frame, title and URL, a click through the mouse path, typing and paste through the key path, back/forward, the URL policy, resize, zoom, crash detection with reload, and that Chrome and its profile dir are gone after close. Co-Authored-By: Claude Fable 5.2 --- tests/browser_cdp_live.rs | 258 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 tests/browser_cdp_live.rs diff --git a/tests/browser_cdp_live.rs b/tests/browser_cdp_live.rs new file mode 100644 index 00000000..da0a1cb3 --- /dev/null +++ b/tests/browser_cdp_live.rs @@ -0,0 +1,258 @@ +//! Live end-to-end test of the browser canvas against a real headless Chrome (`WEB-01..02`). +//! +//! Opt-in: runs only with `BILTERM_BROWSER_CDP=1` (and a Chromium-family browser installed, or +//! `BILTERM_CHROME_PATH`). It needs no external network: the pages come from a `TcpListener` on +//! 127.0.0.1. +//! +//! `BILTERM_BROWSER_CDP=1 cargo test --test browser_cdp_live -- --nocapture` +#![cfg(unix)] + +use std::io::{Read, Write}; +use std::net::TcpListener; +use std::time::{Duration, Instant}; + +use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind}; +use ratatui::layout::Rect; + +use bilterm::action::{Action, BrowserOp}; +use bilterm::colon_command::parse_colon_command; +use bilterm::input::mouse::handle_wm_mouse_event; +use bilterm::input::{handle_wm_key_event, handle_wm_paste_event}; +use bilterm::pane::browser_canvas::BrowserCanvasState; +use bilterm::wm::WindowManager; + +const PAGE_ONE: &str = "Page One\ +go"; +const PAGE_TWO: &str = "Page Two\ +\ +"; + +/// Serves the two pages on 127.0.0.1 and returns the port. +fn serve() -> u16 { + let listener = TcpListener::bind("127.0.0.1:0").expect("bind loopback"); + let port = listener.local_addr().unwrap().port(); + std::thread::spawn(move || { + for stream in listener.incoming() { + let Ok(mut stream) = stream else { continue }; + std::thread::spawn(move || { + let mut req = Vec::new(); + let mut buf = [0u8; 1024]; + while !req.windows(4).any(|w| w == b"\r\n\r\n") { + match stream.read(&mut buf) { + Ok(0) | Err(_) => return, + Ok(n) => req.extend_from_slice(&buf[..n]), + } + } + let line = String::from_utf8_lossy(&req); + let path = line.split_whitespace().nth(1).unwrap_or("/"); + let (status, body) = match path { + "/" => ("200 OK", PAGE_ONE), + "/two" => ("200 OK", PAGE_TWO), + _ => ("404 Not Found", ""), + }; + let _ = write!( + stream, + "HTTP/1.1 {status}\r\nContent-Type: text/html\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}", + body.len() + ); + }); + } + }); + port +} + +fn browser(wm: &WindowManager) -> &BrowserCanvasState { + wm.active_pane().browser_canvas().expect("browser canvas open") +} + +/// Drives the window-manager tick until `cond` holds. The tick is the only thing pulling state. +fn wait_for(wm: &mut WindowManager, what: &str, cond: impl Fn(&WindowManager) -> bool) { + let start = Instant::now(); + loop { + wm.poll_all_ptys(); + if cond(wm) { + eprintln!(" ok {what} ({} ms)", start.elapsed().as_millis()); + return; + } + if start.elapsed() > Duration::from_secs(20) { + let b = wm.active_pane().browser_canvas(); + panic!( + "timed out waiting for {what}: backend={:?} url={:?} title={:?} loading={:?} notice={:?}", + b.map(|b| b.backend_name()), + b.map(|b| b.current_url().to_string()), + b.map(|b| b.page_title().to_string()), + b.map(|b| b.is_loading()), + b.and_then(|b| b.notice().map(str::to_string)), + ); + } + std::thread::sleep(Duration::from_millis(10)); + } +} + +fn colon(wm: &mut WindowManager, cmd: &str) { + let action = parse_colon_command(cmd).expect("colon command").expect("valid syntax"); + wm.apply(action).unwrap(); +} + +/// RGB of the frame pixel at the given fraction of the frame. +fn pixel(wm: &WindowManager, fx: f32, fy: f32) -> Option<(u8, u8, u8)> { + let f = browser(wm).cached_frame()?; + let x = ((f32::from(f.width_px) * fx) as usize).min(usize::from(f.width_px) - 1); + let y = ((f32::from(f.height_px) * fy) as usize).min(usize::from(f.height_px) - 1); + let i = (y * usize::from(f.width_px) + x) * 4; + Some((f.rgba[i], f.rgba[i + 1], f.rgba[i + 2])) +} + +fn is_blue(p: Option<(u8, u8, u8)>) -> bool { + p.is_some_and(|(r, g, b)| r < 60 && (70..140).contains(&g) && b > 160) +} + +fn is_white(p: Option<(u8, u8, u8)>) -> bool { + p.is_some_and(|(r, g, b)| r > 230 && g > 230 && b > 230) +} + +fn group_alive(pgid: i32) -> bool { + // SAFETY: signal 0 only probes whether any process is left in the group. + unsafe { libc::kill(-pgid, 0) == 0 } +} + +fn wait_gone(pgid: i32, dir: &std::path::Path) { + let deadline = Instant::now() + Duration::from_secs(10); + while (group_alive(pgid) || dir.exists()) && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(20)); + } + assert!(!group_alive(pgid), "Chrome process group {pgid} is still alive"); + assert!(!dir.exists(), "profile dir {} was not removed", dir.display()); +} + +fn click_cell(wm: &mut WindowManager, screen: Rect, column: u16, row: u16) { + for kind in [MouseEventKind::Down(MouseButton::Left), MouseEventKind::Up(MouseButton::Left)] { + handle_wm_mouse_event(wm, MouseEvent { kind, column, row, modifiers: KeyModifiers::NONE }, screen).unwrap(); + } +} + +#[test] +fn live_chrome_end_to_end() { + if std::env::var("BILTERM_BROWSER_CDP").as_deref() != Ok("1") { + eprintln!("skipped: set BILTERM_BROWSER_CDP=1 to run against a real Chrome"); + return; + } + let port = serve(); + let root = format!("http://127.0.0.1:{port}/"); + let two = format!("http://127.0.0.1:{port}/two"); + let screen = Rect::new(0, 0, 80, 24); + let mut wm = WindowManager::new_headless(24, 80); + + eprintln!("open {root}"); + colon(&mut wm, &format!(":browser open {root}")); + assert!(browser(&wm).backend_name().starts_with("cdp"), "real engine expected, got {}", browser(&wm).backend_name()); + wait_for(&mut wm, "DevTools handshake (backend == cdp)", |wm| browser(wm).backend_name() == "cdp"); + wait_for(&mut wm, "non-placeholder frame (blue link above white body)", |wm| { + is_blue(pixel(wm, 0.5, 0.25)) && is_white(pixel(wm, 0.5, 0.75)) + }); + wait_for(&mut wm, "title 'Page One'", |wm| browser(wm).page_title() == "Page One"); + wait_for(&mut wm, "load finished", |wm| !browser(wm).is_loading()); + assert_eq!(browser(&wm).current_url(), root); + let (vw, vh) = browser(&wm).viewport_px(); + let frame = browser(&wm).cached_frame().unwrap(); + assert_eq!((frame.width_px, frame.height_px), (vw, vh), "frame matches the pane viewport"); + eprintln!(" frame {}x{} seq {}", frame.width_px, frame.height_px, frame.seq); + + // Lifecycle facts while it runs. + let (pgid, profile) = browser(&wm).process_info().expect("real Chrome process"); + assert!(group_alive(pgid)); + let runtime = bilterm::secure_fs::private_runtime_dir().unwrap(); + assert!(profile.starts_with(&runtime), "profile {} is inside the private runtime dir", profile.display()); + let name = profile.file_name().unwrap().to_string_lossy().to_string(); + assert!(name.starts_with(&format!("profile-{}-", std::process::id())), "{name}"); + use std::os::unix::fs::PermissionsExt; + assert_eq!(std::fs::metadata(&profile).unwrap().permissions().mode() & 0o777, 0o700); + + eprintln!("click the link through the mouse path"); + let canvas = wm.active_pane().canvas_rect(screen); + let prompt = wm.active_pane().prompt_rect(screen); + let body = Rect::new(canvas.x, canvas.y + 1, canvas.width, canvas.height - 1); + // A cell in the upper half of the page (the link) that the floating prompt does not cover. + let row = (body.y..body.y + body.height / 2).rev().find(|r| *r < prompt.y || *r >= prompt.bottom()).expect("free row"); + let col = body.x + body.width / 2; + click_cell(&mut wm, screen, col, row); + wait_for(&mut wm, "URL changed to /two", |wm| browser(wm).current_url() == two); + wait_for(&mut wm, "title 'Page Two'", |wm| browser(wm).page_title() == "Page Two"); + wait_for(&mut wm, "history has a back entry", |wm| browser(wm).can_go_back() && !browser(wm).can_go_forward()); + assert!(browser(&wm).viewport_focused, "clicking the page focused it"); + + eprintln!("type into the input through the key path"); + wait_for(&mut wm, "page two loaded and painted (input replaces the blue link)", |wm| { + !browser(wm).is_loading() && is_white(pixel(wm, 0.5, 0.25)) + }); + click_cell(&mut wm, screen, col, row); + for (code, mods) in [ + (KeyCode::Char('h'), KeyModifiers::NONE), + (KeyCode::Char('i'), KeyModifiers::NONE), + (KeyCode::Char('!'), KeyModifiers::SHIFT), + ] { + handle_wm_key_event(&mut wm, KeyEvent::new(code, mods)).unwrap(); + } + wait_for(&mut wm, "typed text read back via title 'typed:hi!'", |wm| browser(wm).page_title() == "typed:hi!"); + handle_wm_key_event(&mut wm, KeyEvent::new(KeyCode::Backspace, KeyModifiers::NONE)).unwrap(); + wait_for(&mut wm, "Backspace edits the input ('typed:hi')", |wm| browser(wm).page_title() == "typed:hi"); + handle_wm_paste_event(&mut wm, " there").unwrap(); + wait_for(&mut wm, "paste inserted ('typed:hi there')", |wm| browser(wm).page_title() == "typed:hi there"); + assert!(wm.active_pane().prompt().text().is_empty(), "nothing leaked into the prompt"); + + eprintln!("escape hatch, back/forward, URL policy"); + handle_wm_key_event(&mut wm, KeyEvent::new(KeyCode::Char(']'), KeyModifiers::CONTROL)).unwrap(); + assert!(!browser(&wm).viewport_focused, "Ctrl+] returned the keyboard"); + colon(&mut wm, ":browser back"); + wait_for(&mut wm, "back to Page One", |wm| browser(wm).current_url() == root && browser(wm).page_title() == "Page One"); + wait_for(&mut wm, "forward entry available", |wm| browser(wm).can_go_forward() && !browser(wm).can_go_back()); + colon(&mut wm, ":browser forward"); + wait_for(&mut wm, "forward to /two", |wm| browser(wm).current_url() == two); + wm.apply(Action::Browser(BrowserOp::Open { url: "javascript:document.title='pwned'".into(), pane_id: None })).unwrap(); + assert_eq!(browser(&wm).current_url(), two, "javascript: URL never reached Chrome"); + assert!(browser(&wm).notice().is_some_and(|n| n.contains("blocked URL"))); + + eprintln!("resize and zoom"); + // Forward restores the page from the back/forward cache, typed text and title included. + wait_for(&mut wm, "restored page keeps its title", |wm| browser(wm).page_title() == "typed:hi there"); + let id = wm.active_pane_id(); + wm.pane_mut(id).unwrap().resize_viewport(Rect::new(0, 0, 100, 30)).unwrap(); + let (rw, rh) = browser(&wm).viewport_px(); + assert_ne!((rw, rh), (vw, vh), "pane resize changed the page size"); + wait_for(&mut wm, "page saw the resize (title w:)", |wm| browser(wm).page_title() == format!("w:{rw}")); + wait_for(&mut wm, "frame follows the resize", |wm| { + browser(wm).cached_frame().is_some_and(|f| (f.width_px, f.height_px) == (rw, rh)) + }); + wm.apply(Action::Browser(BrowserOp::Zoom { delta_pct: 100, pane_id: None })).unwrap(); + assert_eq!(browser(&wm).zoom_pct, 200); + wait_for(&mut wm, "zoom 200% halves the CSS width", |wm| browser(wm).page_title() == format!("w:{}", rw / 2)); + // Chrome captures the screencast at CSS-pixel resolution, so a zoomed-in page arrives as a + // smaller frame that the pane stretches; it never exceeds the size that was asked for. + wait_for(&mut wm, "zoomed frame arrives within the requested size", |wm| { + browser(wm).cached_frame().is_some_and(|f| f.width_px == rw / 2 || (f.width_px, f.height_px) == (rw, rh)) + }); + let f = browser(&wm).cached_frame().unwrap(); + eprintln!(" frame after zoom {}x{}", f.width_px, f.height_px); + + eprintln!("crash detection and reload"); + // SAFETY: kills only the Chrome process group this test started. + unsafe { libc::kill(-pgid, libc::SIGKILL) }; + wait_for(&mut wm, "crash detected", |wm| browser(wm).is_crashed()); + assert_eq!(browser(&wm).backend_name(), "cdp (crashed)"); + assert!(browser(&wm).overlay_message().is_some_and(|m| m.ends_with("press r to reload"))); + assert!(browser(&wm).cached_frame().is_none(), "no frozen frame after the crash"); + colon(&mut wm, ":browser reload"); + wait_for(&mut wm, "relaunched after reload", |wm| browser(wm).backend_name() == "cdp" && browser(wm).cached_frame().is_some()); + wait_for(&mut wm, "page back after relaunch", |wm| browser(wm).page_title() == "Page Two"); + assert_eq!(browser(&wm).zoom_pct, 200, "zoom survives the relaunch"); + wait_gone(pgid, &profile); + let (pgid2, profile2) = browser(&wm).process_info().expect("relaunched Chrome"); + assert_ne!(profile2, profile, "a fresh profile dir per launch"); + + eprintln!("close: Chrome gone and profile removed"); + colon(&mut wm, ":browser close"); + assert!(wm.active_pane().browser_canvas().is_none()); + wait_gone(pgid2, &profile2); + eprintln!(" ok process group {pgid2} gone, {} removed", profile2.display()); +} From f7b15afafceaeb47f8027f5bf2df44160cce1c1f Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 01:17:50 -0500 Subject: [PATCH 4/8] docs(browser): user guide chapter 14, changelog and requirement notes Describes the real engine: requirements, focus and the Ctrl+] escape hatch, status labels, allowed URLs, opt-ins and known limits. Corrects the WEB-01/02 notes, which claimed a CDP engine that did not exist. Co-Authored-By: Claude Fable 5.2 --- .planning/REQUIREMENTS.md | 2 + CHANGELOG.md | 17 ++++ docs/user-guide/14-browser-canvas.md | 115 +++++++++++++++++++++++++++ docs/user-guide/index.md | 2 + 4 files changed, 136 insertions(+) create mode 100644 docs/user-guide/14-browser-canvas.md diff --git a/.planning/REQUIREMENTS.md b/.planning/REQUIREMENTS.md index 37b60378..3b975c96 100644 --- a/.planning/REQUIREMENTS.md +++ b/.planning/REQUIREMENTS.md @@ -244,7 +244,9 @@ Which phases cover which requirements. Updated during roadmap creation. ## GUI Chrome Engine Web Browser Canvas & 60fps Video Player Canvas (Phase 59) - [x] **WEB-01**: Embedded Chromium engine web browser canvas via CDP Screencast frame-sink (and optional CEF off-screen renderer) rendering 60fps RGBA frames into composite pane canvases under private profile isolation (`0700`). + - Note: Phase 59 shipped a placeholder (Chrome was launched with no debugging channel and every call went to the synthetic engine). The real CDP engine (DevTools pipe, screencast frames decoded off the UI thread, per-launch `0700` profile, process-group cleanup) landed with `feat/browser-real-cdp`. No CEF renderer exists. Frame rate is whatever the screencast delivers, not a fixed 60fps. - [x] **WEB-02**: Full interactive browser navigation (URL bar, mouse clicks/scrolls, keyboard input forwarded via CDP, history back/forward/reload, zoom, TUI sub-cell fallback). + - Note: real as of `feat/browser-real-cdp`: keys, paste, mouse, wheel, resize and zoom reach the page; history comes from `Page.getNavigationHistory`. The TUI view is a half-block preview (no kitty/sixel output yet); tabs, find-in-page, link hints and the agent surface (`browser.text|screenshot|click|type|eval`) are not built. - [x] **VID-01**: Embedded 60fps RGBA hardware-accelerated video player canvas powered by `ffmpeg` pipe / libavcodec with monotonic audio sync. - [x] **VID-02**: On-screen transport controls (play/pause, seek, volume, loop, speed), Borland/OIA status bar integration, TUI sub-cell fallback (Kitty/Sixel/HalfBlock), and surface parity (`UNI-R01`). diff --git a/CHANGELOG.md b/CHANGELOG.md index 93ff4df3..fee1270e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,23 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed +- **Browser canvas is now a real browser (`WEB-01..02`).** `:browser open ` drives headless Chrome over the DevTools pipe (`--remote-debugging-pipe`, no TCP debugging port) instead of painting a placeholder. Frames come from the screencast and are decoded off the UI thread; the window-manager tick redraws only when a frame or the page state changed. URL, title, loading state and back/forward follow the page, including navigations the page starts itself. See [user guide chapter 14](docs/user-guide/14-browser-canvas.md). +- **Keyboard and mouse reach the page.** With the viewport focused (click the page, `Ctrl+]`, or `:browser focus on`) keys, bracketed paste, clicks, drags and the wheel are forwarded. `Ctrl+]` always returns the keyboard to bilterm. `Ctrl+L` edits the URL only while the page has focus; it is no longer taken from the shell otherwise. Mouse positions are derived from the frame and pane size, so clicks land correctly in the terminal and in `--gui`. +- **Resize and zoom work.** The page follows the pane size, and `:browser zoom` reflows the page (25% to 500%). +- **Honest status.** The status line and `browser.state` report `cdp` only after the DevTools handshake, `cdp (starting)` before it, `cdp (crashed)` with "press r to reload" after a crash, and `synthetic (Chrome not found)` or `synthetic (launch failed: …)` when the placeholder engine is used. The lock icon is shown only for `https://`. `browser.open` returns `ok: false` with an `error` when the URL is refused. +- Nav-bar buttons are hit-tested by display column, so every click lands on the button drawn there. + +### Security +- Browser URLs are validated before they reach Chrome: only `http://`, `https://` and `about:blank`; `file://` needs `BILTERM_BROWSER_ALLOW_FILE=1`; `javascript:`, `data:`, `chrome:` and anything starting with `-` are refused. The URL is never passed on Chrome's command line. +- Each browser gets its own profile directory (pid, pane and random suffix) inside the private runtime dir (`0700`); a failure to create it aborts the launch. Closing the canvas kills Chrome's whole process group and removes the profile. Downloads are denied and the sandbox stays on. + +### Dependencies +- Added `serde_json` (DevTools messages) and `zune-jpeg` (screencast frame decoding, pure Rust). + +### Requirements +- A Chromium-family browser on Linux or macOS for the real engine (`BILTERM_CHROME_PATH` overrides discovery). Without one the placeholder engine is used and the status line says so. + ## [3.3.0] - 2026-11-09 Milestone v3.3.0 "Beyond Herdr": always-on foreground RPC, first-class agent/pane/workspace CLI, semantic agent introspection, worktree swarm DAG runner, Mission Control HUD, and hierarchical `D:(W,S,Y,P)` coordinates (Phases 65–71, 71 phases total). diff --git a/docs/user-guide/14-browser-canvas.md b/docs/user-guide/14-browser-canvas.md new file mode 100644 index 00000000..8473d5d3 --- /dev/null +++ b/docs/user-guide/14-browser-canvas.md @@ -0,0 +1,115 @@ +# Chapter 14: The Browser Canvas + +`:browser open ` shows a real web page inside a pane. Bilterm starts a headless Chrome for +that pane, streams its frames into the pane and forwards your keys and mouse to the page. + +## Requirements + +- Linux or macOS, and a Chromium-family browser: Chrome, Chromium, Brave or Edge. Bilterm looks + for `chromium`, `chromium-browser`, `google-chrome-stable`, `google-chrome`, `brave-browser` + and `microsoft-edge-stable` on `PATH` (plus the standard application paths on macOS). Set + `BILTERM_CHROME_PATH=/path/to/chrome` to pick one. +- No browser found, or the launch fails: the pane shows a painted placeholder (the *synthetic* + engine) and the status line says why. On Windows the synthetic engine is always used. + +## Opening and closing + +| Action | Command | Key | Mouse | +|---|---|---|---| +| Open a URL | `:browser open ` or `:browser ` | | | +| Toggle the canvas | `:browser` | `Ctrl+Alt+B`, ` .` | | +| Close | `:browser close` | | `[✕]` on the nav bar | +| Back / forward | `:browser back`, `:browser forward` | | `[◄]` `[►]` | +| Reload / stop | `:browser reload`, `:browser stop` | | `[↻]` (shows `[■]` while loading) | +| Edit the URL | | `Ctrl+L` while the page has focus | click the URL | +| Zoom | `:browser zoom 25`, `:browser zoom -25` | | | +| Give the keyboard to the page | `:browser focus on` | `Ctrl+]` | click the page, or `[FOCUS:…]` | +| Take the keyboard back | `:browser focus off` | `Ctrl+]` | click the prompt, or `[FOCUS:…]` | + +`:bcanvas` is an alias of `:browser`. The same operations are available over RPC and MCP as +`browser.open`, `browser.control` and `browser.state` (`bilterm_browser`). + +## What the status line tells you + +The status line and `browser.state` name the engine that is really running: + +| Label | Meaning | +|---|---| +| `cdp (starting)` | Chrome was launched; the DevTools handshake has not finished. | +| `cdp` | Attached to the page. This is real Chrome. | +| `cdp (crashed)` | The page or Chrome died. The pane shows `… — press r to reload`. | +| `synthetic` | Placeholder engine, by request (`demo://`, `test://`, `about:` URLs or the environment switches below). | +| `synthetic (Chrome not found)` | No browser binary was found. | +| `synthetic (launch failed: …)` | Chrome was found but did not come up; the reason follows. A launch that gives no handshake within 5 seconds is reported this way. | + +The nav bar shows a lock only for `https://` pages, `◌` while the page is loading, the page +title after the URL, and `⚠` with the reason when a URL was refused or a navigation failed +(for example `net::ERR_NAME_NOT_RESOLVED`). + +## Keyboard + +Keys go to the floating prompt until the page has focus. Click the page, press `Ctrl+]` or run +`:browser focus on` to give it the keyboard. While it has focus: + +- Every key goes to the page, including `Tab`, `Esc`, arrows, `Enter`, function keys and + `Ctrl`/`Alt` chords. Pasted text is inserted at the page's caret. +- `Ctrl+]` returns the keyboard to bilterm. This always works. (Terminals without the kitty + keyboard protocol send it as `Ctrl+5`; both are accepted.) +- `Ctrl+L` opens the URL editor: type, `Enter` to go, `Esc` to cancel, `Ctrl+U` to clear. A bare + host such as `example.com` gets `https://`. +- The leader key (`Ctrl+B` / `Ctrl+X`), your own key bindings, `Ctrl+Q` and `Ctrl+Alt+A` still + belong to bilterm. So `Ctrl+X` (cut) and `Ctrl+B` are not sent to the page. +- After a crash, `r` relaunches the browser on the last URL (`:browser reload` does the same). + +Without focus the page takes no keys. In particular `Ctrl+L` reaches your shell as usual. + +## Mouse + +Clicks, drags, movement and the wheel on the page are forwarded to it. A click lands where you +point in the terminal and in `--gui`: the position is taken from the size of the frame and of +the pane, not from an assumed cell size. Double clicks are recognised. Clicking the floating +prompt gives the keyboard back to it. + +Links that ask for a new window open in the same pane (one page per pane). + +## Size and zoom + +The page is laid out for the pane: 10 by 20 CSS pixels per cell, between 320x200 and 3840x2160, +and it follows the pane when you resize or split it. `:browser zoom ` changes the zoom in +percent (25% to 500%): the page reflows larger or smaller, as with `Ctrl +` in a desktop browser. + +In the terminal the page is drawn with half-blocks, two pixels per cell row, so text on most +pages is not readable there. It is a preview; `bilterm --gui` draws the frame at full size. + +## What is allowed + +- URLs: `http://`, `https://` and `about:blank`. `javascript:`, `data:`, `chrome:`, + `view-source:` and anything starting with `-` are refused before they reach Chrome. +- `file://` is off. Start bilterm with `BILTERM_BROWSER_ALLOW_FILE=1` to allow it. +- Downloads are denied. +- Chrome's sandbox stays on. Bilterm never passes `--no-sandbox`; if your system cannot run the + sandbox (some containers, running as root), Chrome fails to start and you get + `synthetic (launch failed: …)`. +- Each launch gets its own empty profile in `$XDG_RUNTIME_DIR/bilterm/browser/` (mode `0700`). + Nothing is shared with your normal browser profile: no cookies, logins or extensions. The + profile is deleted when the canvas closes, and leftovers from a killed bilterm are removed the + next time a browser is opened. +- Chrome talks to bilterm over a private pipe. No debugging port is opened. + +## Environment switches + +| Variable | Effect | +|---|---| +| `BILTERM_CHROME_PATH` | Browser binary to launch. | +| `BILTERM_BROWSER_ALLOW_FILE=1` | Allow `file://` URLs. | +| `BILTERM_BROWSER_SYNTHETIC=1` | Never launch a browser; always use the placeholder engine. | +| `BILTERM_BROWSER_CDP=1` | For tests only: let a test binary launch a real browser. Tests use the placeholder engine otherwise. `BILTERM_BROWSER_CDP=1 cargo test --test browser_cdp_live` runs the live test. | + +## Known limits + +- One page per pane: no tabs, no find-in-page, no link hints. +- Zooming in lowers the frame resolution (Chrome captures frames at CSS-pixel size). +- No kitty or sixel image output yet, so the terminal view stays a half-block preview. +- Agents cannot yet read page text, take screenshots or click by selector over RPC/MCP. +- The clipboard is the page's own: `Ctrl+C` in the page does not reach the system clipboard. +- JavaScript `alert` dialogs are accepted automatically; `confirm` and `prompt` are dismissed. diff --git a/docs/user-guide/index.md b/docs/user-guide/index.md index 780fada2..028b5672 100644 --- a/docs/user-guide/index.md +++ b/docs/user-guide/index.md @@ -38,6 +38,8 @@ Welcome to the official User Guide for **Bilterm (`v3.2.0`)** — the Rust + Rat - The Find window (`Ctrl+Alt+F`, `:find agent|session|history`, View menu): fuzzy ranked search of agents, live and saved sessions, and command history; every row shows its source (`[live]`, `[saved:]`, `[log:]`); secrets are masked and counted; `:finder` verbs; `find.*` RPC and `bilterm_find` MCP; SSH and tmux coverage and known gaps G1 to G10. 13. **[Chapter 13: The Traffic Monitor](13-traffic-monitor.md)** - The TRAFFIC MONITOR window (`Ctrl+Alt+T`, `:traffic`, `:netmon`): bytes per connection, session, window, pane, origin, locality and direction, sparklines, pause, drill-down, `traffic.*` RPC and `bilterm_traffic` MCP. +14. **[Chapter 14: The Browser Canvas](14-browser-canvas.md)** + - A real web page in a pane (`:browser open `, `Ctrl+Alt+B`): headless Chrome over a private DevTools pipe, keyboard focus and the `Ctrl+]` escape hatch, mouse, resize and zoom, what the status line says about the running engine, allowed URLs, profile isolation, environment switches and known limits. --- From 2373040ab18f0ae640221d91113cbd2e35770472 Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 01:23:28 -0500 Subject: [PATCH 5/8] fix(browser): finish Chrome reaping before the process exits The reaper thread that waits for Chrome and deletes its profile dir was cut off when bilterm quit right after closing the canvas, leaving the profile behind until the next launch swept it. An atexit hook now gives pending reapers a bounded 2 s to finish. Co-Authored-By: Claude Fable 5.2 --- src/pane/browser_canvas/cdp_launch.rs | 53 +++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/src/pane/browser_canvas/cdp_launch.rs b/src/pane/browser_canvas/cdp_launch.rs index a72fc287..5ff67bc5 100644 --- a/src/pane/browser_canvas/cdp_launch.rs +++ b/src/pane/browser_canvas/cdp_launch.rs @@ -11,6 +11,9 @@ use std::os::fd::{AsRawFd, FromRawFd, OwnedFd, RawFd}; use std::os::unix::process::CommandExt; use std::path::{Path, PathBuf}; use std::process::{Child, Command, Stdio}; +use std::sync::{Mutex, Once}; +use std::thread::JoinHandle; +use std::time::{Duration, Instant}; use crate::pane::PaneId; use crate::secure_fs::{private_dir_in, private_runtime_dir}; @@ -202,12 +205,52 @@ impl Drop for ChromeProcess { std::thread::sleep(std::time::Duration::from_millis(20)); } }; - if let Err(e) = std::thread::Builder::new().name("bilterm-chrome-reaper".into()).spawn(reap) { - tracing::warn!("browser: could not spawn reaper thread: {e}"); + match std::thread::Builder::new().name("bilterm-chrome-reaper".into()).spawn(reap) { + Ok(handle) => track_reaper(handle), + Err(e) => tracing::warn!("browser: could not spawn reaper thread: {e}"), } } } +/// Reaper threads still running. Process exit would cut them off and leave a profile directory +/// behind, so an `atexit` hook gives them a short, bounded time to finish. +static REAPERS: Mutex>> = Mutex::new(Vec::new()); +const EXIT_GRACE: Duration = Duration::from_secs(2); + +fn track_reaper(handle: JoinHandle<()>) { + static HOOK: Once = Once::new(); + // SAFETY: registers a plain `extern "C"` function with no captured state. + HOOK.call_once(|| unsafe { + libc::atexit(finish_reapers_at_exit); + }); + let mut reapers = REAPERS.lock().unwrap_or_else(|e| e.into_inner()); + reapers.retain(|h| !h.is_finished()); + reapers.push(handle); +} + +/// Waits (bounded) for pending reaper threads. Returns `true` when all finished in time. +pub fn finish_reapers(grace: Duration) -> bool { + let deadline = Instant::now() + grace; + loop { + let pending = { + let mut reapers = REAPERS.lock().unwrap_or_else(|e| e.into_inner()); + reapers.retain(|h| !h.is_finished()); + reapers.len() + }; + if pending == 0 { + return true; + } + if Instant::now() >= deadline { + return false; + } + std::thread::sleep(Duration::from_millis(10)); + } +} + +extern "C" fn finish_reapers_at_exit() { + finish_reapers(EXIT_GRACE); +} + #[cfg(test)] mod tests { use super::*; @@ -271,11 +314,7 @@ mod tests { let (pgid, dir) = (proc_.pgid(), proc_.profile_dir().to_path_buf()); assert!(dir.exists()); drop(proc_); - let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5); - // SAFETY: signal 0 only probes the group. - while (unsafe { libc::kill(-pgid, 0) } == 0 || dir.exists()) && std::time::Instant::now() < deadline { - std::thread::sleep(std::time::Duration::from_millis(20)); - } + assert!(finish_reapers(Duration::from_secs(5)), "reaper finished"); assert!(!dir.exists(), "profile dir removed after drop"); // SAFETY: as above. assert_ne!(unsafe { libc::kill(-pgid, 0) }, 0, "process group is gone"); From 1209ae2814cd3237eb063a6e5cc4b2f36f0589b3 Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 01:25:14 -0500 Subject: [PATCH 6/8] fix(browser): report a closed DevTools pipe the same way from both threads The writer saw EPIPE and the reader saw EOF for the same event, so the failure reason depended on which thread noticed first. Co-Authored-By: Claude Fable 5.2 --- src/pane/browser_canvas/cdp_session.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/pane/browser_canvas/cdp_session.rs b/src/pane/browser_canvas/cdp_session.rs index b347eeb1..7fc9f985 100644 --- a/src/pane/browser_canvas/cdp_session.rs +++ b/src/pane/browser_canvas/cdp_session.rs @@ -566,7 +566,12 @@ fn writer_loop(mut writer: W, shared: Arc) { let bytes = cmd.encode(id, session); tracing::trace!(target: "bilterm::browser", "cdp -> {}", String::from_utf8_lossy(&bytes[..bytes.len().min(160)])); if let Err(e) = writer.write_all(&bytes).and_then(|()| writer.flush()) { - return shared.mark_dead(&format!("write to Chrome failed: {e}")); + // A closed pipe is the same fact the reader reports as EOF; whichever thread notices + // first must give the same reason. + return match e.kind() { + std::io::ErrorKind::BrokenPipe => shared.mark_dead("Chrome exited"), + _ => shared.mark_dead(&format!("write to Chrome failed: {e}")), + }; } } } From ac45e99883e3c6dcb8cddf3e3ff13b4157af21f8 Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 06:38:32 -0500 Subject: [PATCH 7/8] test(browser): wait for the orphan reap before asserting the process group is gone The group leader is reaped by the reaper thread, but its SIGKILLed children are reaped by init. Until that happens the zombie still counts as a group member, so kill(-pgid, 0) could still succeed (seen on macOS CI). Poll for up to 5 s. Co-Authored-By: Claude Fable 5.2 --- src/pane/browser_canvas/cdp_launch.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/pane/browser_canvas/cdp_launch.rs b/src/pane/browser_canvas/cdp_launch.rs index 5ff67bc5..d336fdc4 100644 --- a/src/pane/browser_canvas/cdp_launch.rs +++ b/src/pane/browser_canvas/cdp_launch.rs @@ -316,6 +316,13 @@ mod tests { drop(proc_); assert!(finish_reapers(Duration::from_secs(5)), "reaper finished"); assert!(!dir.exists(), "profile dir removed after drop"); + // The group leader is reaped above, but its SIGKILLed children are reaped by init, and a + // not-yet-reaped zombie still counts as a group member. Wait (bounded) for that reap. + let deadline = Instant::now() + Duration::from_secs(5); + // SAFETY: signal 0 only probes for existence; nothing is delivered. + while unsafe { libc::kill(-pgid, 0) } == 0 && Instant::now() < deadline { + std::thread::sleep(Duration::from_millis(10)); + } // SAFETY: as above. assert_ne!(unsafe { libc::kill(-pgid, 0) }, 0, "process group is gone"); } From 191af7d4e5e4f7a2b19703357d90fd6cdb3de60f Mon Sep 17 00:00:00 2001 From: Bill Sommers Date: Tue, 10 Nov 2026 06:41:48 -0500 Subject: [PATCH 8/8] fix(browser): re-signal the Chrome process group before reaping its leader A group member that is mid-fork when the first SIGKILL is delivered can miss it and outlive the leader; the macOS CI run still found the group alive 5 s after drop. The reaper thread now sends SIGKILL to the group three more times (20 ms apart) before wait(): until the leader is reaped its pid pins the group id, so the signal cannot reach an unrelated group. Co-Authored-By: Claude Fable 5.2 --- src/pane/browser_canvas/cdp_launch.rs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/pane/browser_canvas/cdp_launch.rs b/src/pane/browser_canvas/cdp_launch.rs index d336fdc4..1c48d191 100644 --- a/src/pane/browser_canvas/cdp_launch.rs +++ b/src/pane/browser_canvas/cdp_launch.rs @@ -194,9 +194,20 @@ impl Drop for ChromeProcess { self.kill_group(); let Some(mut child) = self.child.take() else { return }; let dir = std::mem::take(&mut self.profile_dir); + let pgid = self.pgid; // Reap and delete off the calling (UI) thread: wait() and remove_dir_all can take a while. - // The group is not signalled again after the reap: its id could have been reused by then. let reap = move || { + // A member that was in the middle of fork() when the first SIGKILL went out can miss + // it and outlive the group leader (seen on macOS). Signal again a few times BEFORE the + // reap: until `wait()` the leader's pid, and so the group id, cannot be reused. The + // group is never signalled after the reap. + for _ in 0..REKILL_ROUNDS { + std::thread::sleep(REKILL_PAUSE); + if pgid > 1 { + // SAFETY: see above; the unreaped leader pins the group id to our child. + unsafe { libc::kill(-pgid, libc::SIGKILL) }; + } + } let _ = child.wait(); for _ in 0..50 { if std::fs::remove_dir_all(&dir).is_ok() || !dir.exists() { @@ -216,6 +227,9 @@ impl Drop for ChromeProcess { /// behind, so an `atexit` hook gives them a short, bounded time to finish. static REAPERS: Mutex>> = Mutex::new(Vec::new()); const EXIT_GRACE: Duration = Duration::from_secs(2); +/// Extra SIGKILL rounds sent to the group before the leader is reaped (3 x 20 ms, off the UI thread). +const REKILL_ROUNDS: u32 = 3; +const REKILL_PAUSE: Duration = Duration::from_millis(20); fn track_reaper(handle: JoinHandle<()>) { static HOOK: Once = Once::new();