Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .changes/permission-handler.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"wry": minor
---

Add an expanded permission handling API for WebView2, WKWebView, WebKitGTK, and Android.
This includes:
- `PermissionKind` expansion: `DisplayCapture`, `Midi`, `Sensors`, `MediaKeySystemAccess`, `LocalFonts`, `WindowManagement`, `PointerLock`, `AutomaticDownloads`, `FileSystemAccess`, `Autoplay`.
- Support for `PermissionResponse::Prompt` to trigger native system dialogs.
- Android support via JNI bridge (`onPermissionRequest` in `RustWebChromeClient`).
- macOS: Split camera/microphone requests; `CameraAndMicrophone` resolved from individual responses.
- Linux: `DisplayCapture` detection for WebKitGTK < 2.42 (getDisplayMedia fix).
- Windows: Full coverage of all 12 `COREWEBVIEW2_PERMISSION_KIND` values.
66 changes: 66 additions & 0 deletions examples/permission_handler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2020-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

//! Example demonstrating the permission handler API.
//!
//! Run: cargo run --example permission_handler
//! Then click the buttons and watch the terminal output.

fn main() -> wry::Result<()> {
use tao::{
event::{Event, WindowEvent},
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
};
use wry::{PermissionKind, PermissionResponse, WebViewBuilder};

let event_loop = EventLoop::new();
let window = WindowBuilder::new()
.with_title("Permission Handler Example")
.with_inner_size(tao::dpi::LogicalSize::new(800, 600))
.build(&event_loop)
.unwrap();

let builder = WebViewBuilder::new()
.with_url("https://permission.site/")
.with_permission_handler(|kind| {
let response = match kind {
PermissionKind::Geolocation => PermissionResponse::Prompt,
_ => PermissionResponse::Allow,
};
println!("[permission] {kind} → {response}");
response
});

#[cfg(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "android"
))]
let _webview = builder.build(&window)?;
#[cfg(not(any(
target_os = "windows",
target_os = "macos",
target_os = "ios",
target_os = "android"
)))]
let _webview = {
use tao::platform::unix::WindowExtUnix;
use wry::WebViewBuilderExtUnix;
let vbox = window.default_vbox().unwrap();
builder.build_gtk(vbox)?
};

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Wait;
if let Event::WindowEvent {
event: WindowEvent::CloseRequested,
..
} = event
{
*control_flow = ControlFlow::Exit;
}
});
}
102 changes: 99 additions & 3 deletions src/android/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ use std::os::fd::{AsFd, AsRawFd};

use super::{
main_pipe::{MainPipe, MAIN_PIPE},
ASSET_LOADER_DOMAIN, EVAL_CALLBACKS, IPC, ON_LOAD_HANDLER, REQUEST_HANDLER, TITLE_CHANGE_HANDLER,
URL_LOADING_OVERRIDE, WITH_ASSET_LOADER,
ASSET_LOADER_DOMAIN, EVAL_CALLBACKS, IPC, ON_LOAD_HANDLER, PERMISSION_HANDLER, REQUEST_HANDLER,
TITLE_CHANGE_HANDLER, URL_LOADING_OVERRIDE, WITH_ASSET_LOADER,
};

use crate::PageLoadEvent;
use crate::{PageLoadEvent, PermissionKind, PermissionResponse};

#[macro_export]
macro_rules! android_binding {
Expand Down Expand Up @@ -87,6 +87,22 @@ macro_rules! android_binding {
handleReceivedTitle,
[JString, JString],
);
android_fn!(
$domain,
$package,
RustWebChromeClient,
onPermissionRequestNative,
[JString, jni::objects::JObjectArray],
jint
);
android_fn!(
$domain,
$package,
RustWebChromeClient,
onGeolocationPermissionRequestNative,
[JString, JString],
jint
);
}};
}

Expand Down Expand Up @@ -481,3 +497,83 @@ pub unsafe fn onPageLoaded(mut env: JNIEnv, _: JClass, webview_id: JString, url:
}
}
}

#[allow(non_snake_case)]
pub unsafe fn onPermissionRequestNative(
mut env: JNIEnv,
_: JClass,
webview_id: JString,
resources: jni::objects::JObjectArray,
) -> jint {
let mut allowed = false;
let mut denied = false;
let mut prompt = false;
let Ok(webview_id) = env.get_string(&webview_id) else {
return 2;
};
let webview_id = webview_id.to_str().ok().unwrap_or_default();
let permission_handlers = PERMISSION_HANDLER.lock().unwrap();
let Some(handler) = permission_handlers.get(webview_id) else {
return 2;
};

if let Ok(size) = env.get_array_length(&resources) {
for i in 0..size {
if let Ok(resource) = env.get_object_array_element(&resources, i) {
if let Ok(resource_str) = env.get_string(&resource.into()) {
let resource_str = resource_str.to_string_lossy();

let kind = match resource_str.as_ref() {
"android.webkit.resource.AUDIO_CAPTURE" => PermissionKind::Microphone,
"android.webkit.resource.VIDEO_CAPTURE" => PermissionKind::Camera,
"android.webkit.resource.PROTECTED_MEDIA_ID" => PermissionKind::MediaKeySystemAccess,
"android.webkit.resource.MIDI_SYSEX" => PermissionKind::Midi,
_ => PermissionKind::Other,
};

match (handler.handler)(kind) {
PermissionResponse::Allow => allowed = true,
PermissionResponse::Deny => denied = true,
PermissionResponse::Prompt => prompt = true,
PermissionResponse::Default => {}
}
}
}
}
}

// Consolidated decision logic
if denied {
1 // Deny
} else if allowed {
0 // Allow
} else if prompt {
3 // Prompt
} else {
2 // Default
}
}

#[allow(non_snake_case)]
pub unsafe fn onGeolocationPermissionRequestNative(
mut env: JNIEnv,
_: JClass,
webview_id: JString,
_origin: JString,
) -> jint {
let Ok(webview_id) = env.get_string(&webview_id) else {
return 2;
};
let webview_id = webview_id.to_str().ok().unwrap_or_default();
let permission_handlers = PERMISSION_HANDLER.lock().unwrap();
let Some(handler) = permission_handlers.get(webview_id) else {
return 2;
};

match (handler.handler)(PermissionKind::Geolocation) {
PermissionResponse::Allow => 0,
PermissionResponse::Deny => 1,
PermissionResponse::Default => 2,
PermissionResponse::Prompt => 3,
}
}
Loading