Menu

Guides

Permissions

Allow a camera, a microphone, the location and Chromium's other permissions, or ask the user first.

A page asks before it uses a camera, a microphone, the screen, the location, notifications or another Permission. Without a hook every request is denied. See Capabilities and permissions for the rules behind each answer.

Allow a camera and a microphone for your own pages

App::on_permission answers each request:

use kurogane::{App, Origin, Permission, PermissionDecision};

fn main() {
    let own = Origin::parse("app://app").unwrap();

    App::new("content")
        .on_permission(move |request, _app| {
            let calls = request
                .permissions()
                .iter()
                .all(|kind| matches!(kind, Permission::Camera | Permission::Microphone));
            if request.origin() == &own && calls {
                PermissionDecision::Allow
            } else {
                PermissionDecision::Deny
            }
        })
        .run_or_exit();
}

A request is granted or refused whole. Check every permission it asks for, as all does here. A page asking for a camera and a microphone together gets both or neither.

request.origin() is the origin that asks. It’s the page’s, or a frame’s inside the page when the frame asks.

Ask the user first

Return PermissionDecision::Later and keep the request’s PermissionResponder. The page waits until the responder answers from any thread:

use kurogane::{App, PermissionDecision};

fn ask_the_user(question: &str) -> bool {
    // Show your own dialog here
    println!("{question}");
    true
}

fn main() {
    App::new("content")
        .on_permission(|request, _app| {
            let question = format!("Allow {:?} for {}?", request.permissions(), request.origin());
            let responder = request.responder();
            std::thread::spawn(move || {
                if ask_the_user(&question) {
                    responder.allow();
                } else {
                    responder.deny();
                }
            });
            PermissionDecision::Later
        })
        .run_or_exit();
}
  • A request is answered once. The first answer counts.
  • Dropping the responder without an answer denies the request.
  • An answer that comes after the page went away grants nothing.
  • Later without a responder kept denies at once.

The hook itself runs on the UI thread and must not wait for the user. Ask from another thread as here.

Take a permission back

Chromium remembers its answers to http and https sites in the profile. A site’s later requests get the remembered answer without reaching the hook. AppHandle::forget_permissions forgets them for one origin:

use kurogane::{App, AppHandle, IpcError, Origin};

fn main() {
    App::new("content")
        .command("revoke", |origin: String, app: &AppHandle| {
            let origin = Origin::parse(&origin).map_err(|e| IpcError::from(e.to_string()))?;
            app.forget_permissions(&origin);
            Ok(())
        })
        .run_or_exit();
}

The site’s next request reaches on_permission again. Forgetting also takes back a grant the site holds now. Its notifications stop.

A camera, a microphone and the screen are never remembered. Nothing is remembered for your app’s own pages either.

Rules for the hook

  • Default gives Kurogane’s answer. That’s Deny.
  • The hook runs on the UI thread and must not block. A hook that panics denies.
  • Requests from DevTools never reach it and are denied.
  • Your own pages are told yes for notifications and the location but can’t show a notification or read the location. Those grants have to outlast the request. They don’t for your app’s own origin.