Menu

Guides

Browser and renderer delegates

Take part in Chromium's browser and renderer process lifecycles without replacing Kurogane's own.

Delegates are the lower-level way into Chromium. A browser delegate runs in the browser process. A renderer delegate runs inside each renderer, next to your page. Kurogane’s own handling runs either way. Delegates add to it.

Reach for a delegate when no App setting or hook covers what you need. Their callbacks take CEF types which Kurogane re-exports as kurogane::tanso.

Run work once Chromium is ready

ClientAppBrowserDelegate gets the browser process’s lifecycle. App::delegate registers one:

use kurogane::{App, ClientAppBrowserDelegate};

struct Startup;

impl ClientAppBrowserDelegate for Startup {
    fn on_context_initialized(&self) {
        // Runs once, in the browser process only
        println!("Chromium is ready");
    }
}

fn main() {
    App::new("content").delegate(Startup).run_or_exit();
}
Method When it runs
on_before_command_line_processing before Chromium reads its command line
on_context_initialized once the browser process has started; windows may open from here on

on_context_initialized runs only in the browser process. It’s the place for one-time work that needs Chromium running. See Process model.

on_before_command_line_processing is an escape hatch. App::chromium_flag covers switches in almost every case.

Watch the page from inside the renderer

ClientAppRendererDelegate gets each renderer’s lifecycle. App::renderer_delegate registers one:

use kurogane::tanso::{Browser, Frame, V8Context, V8Exception, V8StackTrace};
use kurogane::{App, ClientAppRendererDelegate};

struct Watch;

impl ClientAppRendererDelegate for Watch {
    fn on_context_created(&self, _: Option<&Browser>, _: Option<&Frame>, _: Option<&V8Context>) {
        println!("a page's JavaScript context is ready");
    }

    fn on_uncaught_exception(
        &self,
        _: Option<&Browser>,
        _: Option<&Frame>,
        _: Option<&V8Context>,
        _: Option<&V8Exception>,
        _: Option<&V8StackTrace>,
    ) {
        println!("a page threw an exception it didn't catch");
    }
}

fn main() {
    App::new("content").renderer_delegate(Watch).run_or_exit();
}
Method When it runs
on_web_kit_initialized once per renderer; register V8 extensions and renderer-wide state
on_browser_created, on_browser_destroyed as a browser appears in and leaves the renderer
on_context_created, on_context_released as a page’s JavaScript context starts and ends; window.kurogane is already there
on_uncaught_exception when a page throws an exception it doesn’t catch
on_focused_node_changed when the focused element changes
on_process_message_received for a process message from another process; Kurogane’s own IPC never reaches it
load_handler supplies a load handler for the renderer

Every method has a default that does nothing. Implement the ones you need.

Where delegates run

  • Register delegates on the App as usual. Chromium runs your binary again for each helper and builds the same App there. That’s how a renderer gets your renderer delegates.
  • A renderer delegate’s output and log come from the renderer process. Install your tracing subscriber before the app starts so helpers log too. See Logging.
  • Under SandboxMode::Chromium renderers run sandboxed. A renderer delegate then can’t reach files, the network or other processes on its own.
  • For your page to reach Rust use commands. Delegates don’t replace IPC.

Rules for delegates

  • Several delegates of a kind run in registration order.
  • For load_handler the first delegate that returns a handler wins.
  • A non-zero return from on_process_message_received marks the message handled and keeps it from later delegates.
  • Kurogane may do its own work before or after a delegate’s callback. Don’t rely on an order unless a method’s docs promise one.
  • A panic in a delegate aborts the process it runs in. In the browser process that ends your app. In a renderer it ends that renderer.