Files
wry/examples/permission_handler.rs
F0RLEandTony 9f9a237d78 feat: add permission handler API across webview backends (#1654)
* feat: add permission handler API - implement cross-platform permission handler API, fix combined camera and microphone logic on macOS, update documentation to clarify NFC and Bluetooth support, and simplify permission_handler example using permission.site

* fix(wkwebview): remove leftover PermissionObserver from UIDelegate signature

* Fix `permission_handler` example on linux

* Use type instead of string check

* Add more platform specific notes

* Move permissions to a file

* Add notice about prompt not supported on linux

* fix(webkitgtk): handle combined audio+video permissions and use is_for_display_device on v2_42

* fix: reorder module declarations for rustfmt

* fix(android): scope permission handler per webview

подтверждено

* fix(android): honor permission handler for geolocation

подтверждено

* fix(android): avoid overgranting mixed permission requests

* fix(android): scope permission callbacks by webview

* fix(android): pass existing webview id string

* Migrate `PACKAGE.get().unwrap()`

* Fix `web_chrome_client`

* small refactor on permissionLauncher

* Add `PermissionResponse::Default` docs for all platforms

* fix(android): simplify permission JNI response

* document allow is not supported on android

* refactor: simplify permission responses

* fix: update permission handler example

* fix(android): pass unknown permissions to handler

* fix(android): allow explicit unknown permissions

* onPermissionRequestNative will not return other values

* Send individual permissions to rust side
and support partial allow

---------

Co-authored-by: Tony <legendmastertony@gmail.com>
2026-06-11 23:09:00 +08:00

67 lines
1.8 KiB
Rust

// 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::Default,
_ => 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;
}
});
}