feat: add permission handler API for WebView2#1654
feat: add permission handler API for WebView2#1654F0RLE wants to merge 7 commits intotauri-apps:devfrom
Conversation
This adds a new with_permission_handler method to WebViewBuilder that allows
applications to intercept and handle permission requests from the webview.
New types:
- PermissionKind - enum for permission types (Microphone, Camera, Geolocation, etc.)
- PermissionResponse - enum for responses (Allow, Deny, Default)
Platform support:
- Windows: Fully implemented via WebView2's PermissionRequested event
- Other platforms: Not yet implemented (handler is ignored)
Example usage:
`
ust
WebViewBuilder::new()
.with_permission_handler(|kind| {
match kind {
PermissionKind::Microphone => PermissionResponse::Allow,
PermissionKind::Camera => PermissionResponse::Allow,
_ => PermissionResponse::Default,
}
})
`
Closes: https://github.com/tauri-apps/wry/issues/XXX
This extends the permission handler API to support: - macOS/iOS: Via WKUIDelegate's requestMediaCapturePermissionForOrigin - Linux: Via WebKitGTK's permission-request signal with UserMediaPermissionRequest Changes: - wkwebview/class/wry_web_view_ui_delegate.rs: Added permission_handler to ivars and updated request_media_capture_permission method - wkwebview/mod.rs: Pass permission_handler to WryWebViewUIDelegate - webkitgtk/mod.rs: Added connect_permission_request handler in attach_handlers - lib.rs: Updated platform-specific documentation
Package Changes Through 4fa8e94There are 1 changes which include wry with minor Planned Package VersionsThe following package releases are the planned based on the context of changes in this pull request.
Add another change file through the GitHub UI by following this link. Read about change files or the docs at github.com/jbolda/covector |
56cc0d1 to
755877e
Compare
This adds DisplayCapture to PermissionKind enum and updates the WebKitGTK permission handler to detect screen capture requests via is_for_display_device(). Changes: - lib.rs: Added PermissionKind::DisplayCapture variant - webkitgtk/mod.rs: Added is_for_display_device() check before audio/video checks This enables apps to programmatically handle getDisplayMedia() permission requests on Linux.
pewsheen
left a comment
There was a problem hiding this comment.
I currently only have a MacBook, so I'll share some thoughts about the macOS implementation here. For WebView2, maybe @Legend-Master can give some advice?
Personally, I tend not to loosen permission controls, to avoid them being used for malicious purposes.
| let decision = if let Some(handler) = &self.ivars().permission_handler { | ||
| match handler(permission_kind) { | ||
| PermissionResponse::Allow => WKPermissionDecision::Grant, | ||
| PermissionResponse::Deny => WKPermissionDecision::Deny, | ||
| PermissionResponse::Default => WKPermissionDecision::Grant, // Default to grant for backwards compatibility | ||
| } | ||
| } else { | ||
| // No handler set, default to grant (backwards compatible behavior) | ||
| WKPermissionDecision::Grant | ||
| }; |
There was a problem hiding this comment.
I think we should align the default action with the OS, giving WKPermissionDecision::Prompt. Granting by default looks dangerous to me.
There was a problem hiding this comment.
It seems to match the old behavior here? I'm not really aware of the historical context here though
There was a problem hiding this comment.
Hmm, you're right. Let's leave it granted then. But I think we might need a discussion for this, maybe in a new issue.
There was a problem hiding this comment.
I've added a PermissionResponse::Prompt variant. This keeps the default behavior as Grant (for backward compatibility) but allows apps to explicitly opt-in to the system prompt if they want. I think this covers both use cases.
| let permission_kind = match capture_type { | ||
| WKMediaCaptureType::Camera => PermissionKind::Camera, | ||
| WKMediaCaptureType::Microphone => PermissionKind::Microphone, | ||
| WKMediaCaptureType::CameraAndMicrophone => PermissionKind::Microphone, // Treat as microphone for now |
There was a problem hiding this comment.
Collapsing CameraAndMicrophone to Microphone also looks dangerous to me.
Maybe we can add some platform-specific type and document it well, or split it into something like vec![Microphone, Camera] and granted if and only if Microphone and Camera are both granted.
| /// - **Windows**: Fully supported via WebView2's PermissionRequested event. | ||
| /// - **macOS / iOS / Linux / Android**: Not yet implemented, handler is ignored. |
There was a problem hiding this comment.
Looks like you have implemented most of them!
Legend-Master
left a comment
There was a problem hiding this comment.
Looks good for the webview2 implementation at least at a glance, awesome work!
Could you also add a change file?
https://github.com/tauri-apps/wry/blob/dev/.changes/readme.md
This adds a new with_permission_handler method to WebViewBuilder that allows applications to intercept and handle permission requests from the webview.
New types:
PermissionKind- enum for permission types (Microphone, Camera, Geolocation, etc.)PermissionResponse- enum for responses (Allow, Deny, Default)Platform support:
Example usage: