# Process model

> Which processes a Kurogane app runs and which of your code runs in each.

A Kurogane app runs as several processes, the way Chromium does. Every one of them is your binary.

## The processes

| Process | What it does | Your code in it |
|---|---|---|
| Browser | creates windows, navigates, dispatches IPC | `main`, command and stream handlers, every `App` callback, browser delegates |
| Renderer | runs your page's HTML, CSS and JavaScript in V8 | your page, renderer delegates |
| GPU | composites and rasterizes | none |
| Utility | runs networking, media and other services | none |

The browser process is the one you start. Chromium starts the others as it needs them. It decides how many renderers run.

## One binary

Chromium starts each helper process by running your binary again with a `--type=` argument (`--type=renderer`, `--type=gpu-process` and others).

A helper runs `main` from the top until the call that starts the runtime. That's [`App::run`](https://kurogane-rs.org/docs/api/app/app.md#method.run), `run_or_exit`, `start`, `build` or `start_embedded`. In a helper that call becomes the helper and never returns. Everything before it runs once in every process:

* **Building the `App` is fine.** It's configuration. The helpers need the same configuration for custom schemes and renderer delegates.
* **Side effects aren't.** Files, output, sockets and spawned processes happen again in every helper at the same time as in the browser process. A helper that recreates a directory pulls it out from under a handle the browser process holds.

Guard one-time work with [`is_browser_process`](https://kurogane-rs.org/docs/api/app/is_browser_process.md):

```rust
fn main() {
    if kurogane::is_browser_process() {
        prepare_data_dir(); // once, not once per helper
    }
    kurogane::App::new("content").run_or_exit();
}
```

Work that needs Chromium running belongs in [`ClientAppBrowserDelegate::on_context_initialized`](https://kurogane-rs.org/docs/api/app/clientappbrowserdelegate.md). Only the browser process calls it.

## The boundary

Your page can't reach native APIs. It runs in a renderer and talks to the browser process through [IPC](https://kurogane-rs.org/docs/concepts/ipc.md). The browser process checks every request against the page's origin before your handler runs. The renderer has no say in that check.

[Renderer delegates](https://kurogane-rs.org/docs/api/app/clientapprendererdelegate.md) run your Rust code inside the renderer itself. They see V8 contexts, JavaScript exceptions and process messages.

## Panics

Kurogane catches a panic in the code it runs to answer a page:

* A command handler's panic rejects the page's call.
* A stream factory's or stream handler's panic fails the stream.
* A panic in [`SchemeHandler::create`](https://kurogane-rs.org/docs/api/app/schemehandler.md) fails the request.

A panic in any other code Kurogane calls aborts the process it happens in. In the browser process that ends your app. In a renderer it ends that renderer. That covers renderer delegates too.

This holds with Rust's default `panic = "unwind"`. An app built with `panic = "abort"` ends at any panic.

## The sandbox

Helper processes run unsandboxed by default. [`SandboxMode::Chromium`](https://kurogane-rs.org/docs/api/app/sandboxmode.md) runs them in the operating system's sandbox. On Windows CEF's sandbox bootstrap then becomes the process you start and loads your app as a library. See [Sandbox](https://kurogane-rs.org/docs/concepts/sandbox.md) for what each platform needs.

## Helpers on macOS

On macOS each helper runs from its own bundle in the app's `Contents/Frameworks`. `kurogane bundle` makes them. Each is a copy of your binary with its own bundle identifier:

| Bundle | Identifier |
|---|---|
| `MyApp Helper.app` | `<id>.helper` |
| `MyApp Helper (GPU).app` | `<id>.helper.gpu` |
| `MyApp Helper (Plugin).app` | `<id>.helper.plugin` |
| `MyApp Helper (Renderer).app` | `<id>.helper.renderer` |
| `MyApp Helper (Alerts).app` | `<id>.helper.alerts` |

The helpers set `LSUIElement`. They stay out of the Dock and the app switcher. They carry neither resources nor the framework and find both through the app that owns them.
