Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.
Python agentagent-osaiai-agentsai-partnerchatopsclaudeclaude-codecodexdevtoolsdiscord-botlark-botllmlocal-firstlong-term-memoryopencodeslack-botvibe-codingvibeywechat
fix(desktop): open external new-context links in the system browser on macOS (#2229)
Max82a65c55ac73
diff --git a/desktop/README.md b/desktop/README.mdindex 5d080ce35..a6416fca2 100644--- a/desktop/README.md+++ b/desktop/README.md@@ -242,6 +242,17 @@ files do). A page that does not, or cannot answer, gets the standard title barwith the window title, the strip hidden, and the inset reset to `0px`. Windowsand Linux keep the native title bar and a zero inset.+Links that ask for a new browsing context (`target="_blank"` or+`window.open`) open in the system browser when they are `http(s)`; anything+else is dropped, and the page never gets a second webview. WKWebView first+offers a `target="_blank"` click to the navigation policy, which only lets the+window itself stay on the shell and Runtime origins, so on macOS the shell+answers that step natively (`src/macos_new_context.rs`): a request whose+WebKit `targetFrame` is nil and whose destination is `http(s)` is allowed on to+the `on_new_window` handler. The decision reads WebKit's own navigation action,+so it holds for links in frames and under page handlers, injects nothing into+the page, and leaves every in-window navigation to the unchanged policy.+For manual acceptance, use a packaged test install with isolated app config anda fake loopback Runtime: set a non-default frame, quit/relaunch, hide/Open, anddeliver a link both before and after readiness. Check the destination and frame,diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.tomlindex 620d1f1a5..3ef84cba9 100644--- a/desktop/src-tauri/Cargo.toml+++ b/desktop/src-tauri/Cargo.toml@@ -54,6 +54,8 @@ objc2-foundation = { version = "0.3.2", default-features = false, features = ["NSError","NSGeometry","NSString",+ "NSURL",+ "NSURLRequest","NSUserDefaults",] }objc2-app-kit = { version = "0.3.2", default-features = false, features = [@@ -64,4 +66,12 @@ objc2-app-kit = { version = "0.3.2", default-features = false, features = ["NSView","NSWindow",] }-objc2-web-kit = { version = "0.3.2", default-features = false, features = ["std", "block2", "objc2-app-kit", "WKWebView"] }+objc2-web-kit = { version = "0.3.2", default-features = false, features = [+ "std",+ "block2",+ "objc2-app-kit",+ "WKFrameInfo",+ "WKNavigationAction",+ "WKNavigationDelegate",+ "WKWebView",+] }diff --git a/desktop/src-tauri/src/lib.rs b/desktop/src-tauri/src/lib.rsindex 793fbc352..8f230a0fd 100644--- a/desktop/src-tauri/src/lib.rs+++ b/desktop/src-tauri/src/lib.rs@@ -26,6 +26,8 @@ mod updater;#[cfg(target_os = "macos")]mod macos_deep_link;#[cfg(target_os = "macos")]+mod macos_new_context;+#[cfg(target_os = "macos")]mod macos_title_bar;use avibe_runtime_host::deep_link::{DeepLinkNavigation, DeepLinks};@@ -1208,7 +1210,10 @@ fn ensure_main_window(app: &AppHandle) -> Option<WebviewWindow> {let builder = builder.initialization_script(macos_title_bar::inset_script());let window = builder.build().ok()?;#[cfg(target_os = "macos")]- macos_title_bar::install(&window);+ {+ macos_title_bar::install(&window);+ macos_new_context::install(&window);+ }Some(window)}diff --git a/desktop/src-tauri/src/macos_new_context.rs b/desktop/src-tauri/src/macos_new_context.rsnew file mode 100644index 000000000..fd3611690--- /dev/null+++ b/desktop/src-tauri/src/macos_new_context.rs@@ -0,0 +1,149 @@+//! Lets WKWebView ask the shell for a new browsing context.+//!+//! WKWebView decides a `target="_blank"` link in two steps. It first asks the+//! navigation delegate whether the navigation may happen, marking a request for+//! a new context with a nil `targetFrame`, and only when that answer is Allow+//! does it ask the UI delegate for a webview — the step Tauri exposes as+//! `on_new_window`. wry hands the shell's navigation handler the URL alone, so+//! that handler cannot tell a new context from a navigation of this webview and+//! cancels every destination outside the Workbench origin. The link then does+//! nothing, although `on_new_window` would have sent it to the system browser.+//! `window.open` skips the first step, which is why it already worked.+//!+//! The shell therefore answers the first step itself for exactly one case: a+//! request for a new context whose destination `on_new_window` would hand to+//! the system browser. It allows that request, so WebKit carries it to+//! `on_new_window`, which opens the browser and never returns a webview.+//! Every other navigation, downloads included, still goes to wry unchanged,+//! so the in-window navigation rules stay where they are.+//!+//! The decision is made on WebKit's own navigation action, after the page has+//! finished handling the click: page scripts, frames, and modifier keys cannot+//! hide a link from it, and nothing is injected into the page.++use std::sync::OnceLock;++use objc2::rc::Retained;+use objc2::runtime::{AnyObject, Imp, Sel};+use objc2::sel;+use objc2_foundation::NSObjectProtocol;+use objc2_web_kit::{WKNavigationAction, WKNavigationActionPolicy, WKWebView};+use tauri::WebviewWindow;+use url::Url;++use crate::{new_window_decision, NewWindowDecision};++type DecisionHandler = block2::DynBlock<dyn Fn(WKNavigationActionPolicy)>;+type PolicyImp = unsafe extern "C-unwind" fn(&AnyObject, Sel, &WKWebView, &WKNavigationAction, &DecisionHandler);++/// wry's own answer to the first step, which the shell defers to for+/// everything it does not answer itself.+static WRY_POLICY: OnceLock<PolicyImp> = OnceLock::new();++/// Whether the shell allows a navigation action instead of asking wry.+fn allows_new_context(opens_new_context: bool, should_download: bool, url: Option<&Url>) -> bool {+ opens_new_context+ && !should_download+ && url.is_some_and(|url| new_window_decision(url) == NewWindowDecision::OpenInSystemBrowser)+}++/// Routes the webview's new-context requests to `on_new_window`. wry installs+/// one navigation delegate class for every webview it builds; the shell builds+/// only the main window, and the first call adopts that class for the process.+pub fn install(window: &WebviewWindow) {+ let _ = window.with_webview(|webview| {+ if WRY_POLICY.get().is_some() {+ return;+ }+ // SAFETY: Tauri hands out the live WKWebView, on the main thread, for+ // the duration of this closure.+ let Some(webview) = (unsafe { Retained::retain(webview.inner().cast::<WKWebView>()) }) else {+ return;+ };+ let Some(delegate) = (unsafe { webview.navigationDelegate() }) else {+ return;+ };+ let delegate: &AnyObject = delegate.as_ref();+ let selector = sel!(webView:decidePolicyForNavigationAction:decisionHandler:);+ // WebKit calls the preferences variant instead when a delegate has one,+ // and would then never reach this answer.+ let preferences = sel!(webView:decidePolicyForNavigationAction:preferences:decisionHandler:);+ if delegate.class().instance_method(preferences).is_some() {+ eprintln!("wry's navigation delegate changed shape; new-context links stay in wry's hands");+ return;+ }+ let Some(method) = delegate.class().instance_method(selector) else {+ return;+ };+ // SAFETY: The selector fixes the signature of both implementations.+ let wry_policy: PolicyImp = unsafe { std::mem::transmute::<Imp, PolicyImp>(method.implementation()) };+ if WRY_POLICY.set(wry_policy).is_err() {+ return;+ }+ // SAFETY: `decide_policy` has the signature the selector declares.+ unsafe {+ method.set_implementation(std::mem::transmute::<PolicyImp, Imp>(decide_policy));+ }+ });+}++unsafe extern "C-unwind" fn decide_policy(+ delegate: &AnyObject,+ selector: Sel,+ webview: &WKWebView,+ action: &WKNavigationAction,+ handler: &DecisionHandler,+) {+ // SAFETY: WebKit calls this on the main thread with a live action.+ let (opens_new_context, should_download, url) = unsafe {+ let should_download = action.respondsToSelector(sel!(shouldPerformDownload)) && action.shouldPerformDownload();+ let url = action+ .request()+ .URL()+ .and_then(|url| url.absoluteString())+ .and_then(|url| Url::parse(&url.to_string()).ok());+ (action.targetFrame().is_none(), should_download, url)+ };+ if allows_new_context(opens_new_context, should_download, url.as_ref()) {+ handler.call((WKNavigationActionPolicy::Allow,));+ return;+ }+ let wry_policy = WRY_POLICY.get().expect("installed before it can be called");+ // SAFETY: The arguments are the ones WebKit passed for this selector.+ unsafe { wry_policy(delegate, selector, webview, action, handler) }+}++#[cfg(test)]+mod tests {+ use super::*;++ fn url(raw: &str) -> Url {+ Url::parse(raw).expect("test URL")+ }++ #[test]+ fn only_new_web_contexts_skip_the_in_window_rules() {+ let web = url("https://github.com/avibe-bot/avibe/releases/tag/gh-v3.1.1rc23");+ assert!(allows_new_context(true, false, Some(&web)));+ assert!(allows_new_context(+ true,+ false,+ Some(&url("http://127.0.0.1:5123/show/page"))+ ));++ // A navigation of this webview keeps the shell's in-window rules.+ assert!(!allows_new_context(false, false, Some(&web)));+ // A download keeps wry's download handling.+ assert!(!allows_new_context(true, true, Some(&web)));+ // No destination the system browser can take.+ assert!(!allows_new_context(true, false, None));+ for raw in [+ "about:blank",+ "javascript:alert(1)",+ "blob:http://127.0.0.1:5123/7f1c",+ "file:///etc/hosts",+ ] {+ assert!(!allows_new_context(true, false, Some(&url(raw))), "{raw}");+ }+ }+}