Skip to content

Commit cbe0944

Browse files
matt2eclaude
andcommitted
fix(media): save native menu media downloads and hide loopback links on macOS
Download Video, Audio and Image in WebKit's native context menu saved nothing. WebKit reports those downloads only to the private `_webView:contextMenuDidCreateDownload:` (WKNavigationDelegatePrivate, macOS 12+), which wry 0.57 does not implement, so each download had no delegate and was cancelled. For the main webview, the method is now added to wry's navigation delegate class. It attaches a WKDownloadDelegate to downloads of relay media, which saves them to ~/Downloads under the player Download's name, with `name (n).ext` if the name is taken. A non-200 response, a non-relay source, or a webview other than main keeps WebKit's current behaviour. `<video>` and `<audio>` play from the tokened 127.0.0.1 listener, and Copy Video Address, Open Video in New Window and Share… hand out that URL with the token in it. A `willOpenMenu:withEvent:` override on wry's webview class first calls the inherited method, then removes those three items, and the separators left behind, from main-webview media menus only. Play, Picture in Picture, Full Screen, controls and Download stay. Image and selection menus keep Share…. Both methods are added class-wide, so they check that the webview is the main one, which leaves Buzz Browser guests unaffected. If a class already has its own method, as after a wry upgrade, class_addMethod fails and that part logs and keeps WebKit's behaviour. The collision naming is extracted from `save_download` into `download_names` and shared. Verified by running: - cargo build, workspace clippy with --all-targets and -D warnings, and all 716 src-tauri tests pass. - New unit tests cover menu filtering for the video, image and selection menus, naming for loopback and buzz-media sources, and free names when an occupied name or a dangling symlink exists. - The app launched signed in with communities loaded and logged no hook-install failures. A native text-selection menu still showed Share…. Not verified in the running app: the live download and the media menu. The automated right-click on a video never opened its menu, and I stopped because the machine was in use. These parts are checked only by reading code: - the hooks fire and items are removed; - a file lands in ~/Downloads; - whether WebKit downloads buzz-media:// images; - what partial file is left if a download fails. Risks: this relies on private WebKit API, which Safari uses and which has existed since macOS 12. A wry upgrade that implements either method disables that part, with a log line. Re-check both items on every wry upgrade. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Matt Toohey <[email protected]>
1 parent 67e4049 commit cbe0944

5 files changed

Lines changed: 513 additions & 17 deletions

File tree

‎docs/identity.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ Both validate HTTPS and the `/media/<hash>` URL shape but do not enforce
584584
saved-community membership; the selected server receives a short-lived token
585585
scoped to its origin. The scheme signs a fresh 60-second `get` token per request
586586
and forwards only a bounded single `Range`; non-image/video/audio types (and SVG)
587-
are served with download disposition and `nosniff`. The main webview does not navigate to protected media for downloads: a narrowly scoped native command authenticates the bounded media GET, saves to the OS Downloads directory without replacing existing files, and rejects unsafe filenames. `relay_upload`
587+
are served with download disposition and `nosniff`. The main webview does not navigate to protected media for downloads: a narrowly scoped native command authenticates the bounded media GET, saves to the OS Downloads directory without replacing existing files, and rejects unsafe filenames. On macOS, the main webview's native media menu saves relay media to Downloads the same way, and omits the items that would copy, open or share the tokened loopback URL. `relay_upload`
588588
hashes, signs (`upload` + `x`) and sends the exact bytes JavaScript passes it;
589589
shared TypeScript (`hostUpload`) owns limits, error mapping and descriptor
590590
validation. JavaScript never signs kind 24242. Packaged HEIC and video

‎src-tauri/Cargo.toml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ tauri-plugin-mcp-bridge = { git = "https://github.com/hypothesi/mcp-server-tauri
9494
security-framework = "3.7"
9595
block2 = "=0.6.2"
9696
objc2 = "=0.6.4"
97-
objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["NSApplication", "NSResponder", "NSScreen", "NSWindow", "NSEvent", "NSWorkspace", "NSRunningApplication", "block2"] }
98-
objc2-foundation = { version = "=0.3.2", default-features = false, features = ["std", "NSObject", "NSThread", "NSBundle", "NSString", "NSURL", "NSError", "NSUserDefaults", "NSProcessInfo"] }
97+
objc2-app-kit = { version = "=0.3.2", default-features = false, features = ["NSApplication", "NSResponder", "NSScreen", "NSWindow", "NSEvent", "NSWorkspace", "NSRunningApplication", "NSMenu", "NSMenuItem", "NSUserInterfaceItemIdentification", "block2"] }
98+
objc2-foundation = { version = "=0.3.2", default-features = false, features = ["std", "NSObject", "NSThread", "NSBundle", "NSString", "NSURL", "NSError", "NSUserDefaults", "NSProcessInfo", "NSData", "NSURLRequest", "NSURLResponse"] }
9999
objc2-user-notifications = { version = "=0.3.2", default-features = false, features = ["UNUserNotificationCenter", "UNNotificationSettings", "UNNotification", "UNNotificationContent", "UNNotificationRequest", "UNNotificationResponse", "UNNotificationTrigger", "block2"] }
100100

101-
objc2-web-kit = { version = "=0.3.2", default-features = false, features = ["std", "objc2-app-kit", "block2", "WKWebView", "WKUIDelegate", "WKFrameInfo", "WKSecurityOrigin", "WKWebViewConfiguration", "WKWebsiteDataStore", "WKHTTPCookieStore"] }
101+
objc2-web-kit = { version = "=0.3.2", default-features = false, features = ["std", "objc2-app-kit", "block2", "WKWebView", "WKUIDelegate", "WKFrameInfo", "WKSecurityOrigin", "WKWebViewConfiguration", "WKWebsiteDataStore", "WKHTTPCookieStore", "WKDownload", "WKDownloadDelegate", "WKNavigationDelegate"] }
102102

103103
[target.'cfg(target_os = "windows")'.dependencies]
104104
webview2-com = "=0.39.1"

‎src-tauri/src/lib.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ mod keep_awake;
5858
#[cfg(target_os = "macos")]
5959
mod legacy_cli_shortcut;
6060
mod legacy_communities;
61+
#[cfg(target_os = "macos")]
62+
mod media_context_menu;
6163
#[cfg(any(not(target_os = "macos"), test))]
6264
mod microphone;
6365

@@ -925,6 +927,18 @@ pub fn run() {
925927
}
926928
Err(error) => eprintln!("Could not start the media listener: {error}"),
927929
}
930+
#[cfg(target_os = "macos")]
931+
if let Some(main) = app.get_webview_window("main") {
932+
let directory = app.path().download_dir().ok();
933+
let stream = app
934+
.try_state::<relay::MediaStream>()
935+
.map(|stream| stream.base().to_owned());
936+
if let Err(error) = main.with_webview(move |webview| {
937+
media_context_menu::install(&webview, directory, stream)
938+
}) {
939+
eprintln!("Could not prepare the media menu: {error}");
940+
}
941+
}
928942
// Only app-owned storage is created; a development build keeps its agents
929943
// in the installed app's (`legacy_identifier::agents`). Preview uses the
930944
// OS-resolved legacy parent, never a browser-supplied path or a

0 commit comments

Comments
 (0)