# Lifecycle and shutdown

> How a browser closes, when your app ends and how Chromium shuts down.

Closing in Chromium is asynchronous. A close request starts a sequence. The browser is gone only once that sequence finishes. Your app ends when its last browser is gone.

## How a browser closes

1. **Something asks it to close.** The user closes its window, the page calls `window.close()` or your app calls [`BrowserHandle::close`](https://kurogane-rs.org/docs/api/app/browserhandle.md#method.close).
2. **The page's unload handlers run.** A page's `beforeunload` may ask the user to stay. A forced close skips that question.
3. **Chromium destroys the browser.** Its window goes with it. A browser embedded in your own window takes only its child window.
4. **Kurogane lets go of it.** The browser leaves [`AppHandle::browsers`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.browsers) and [`AppHandle::browser_count`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.browser_count) drops.

No close call waits for that sequence. A browser counts as open until the last step.

## When your app ends

Your app ends once its last browser has closed, whatever closed it:

* The user closes the last window.
* Your app calls [`AppHandle::shutdown`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.shutdown).
* The user presses Ctrl+C in the terminal that started the app.
* macOS asks the app to quit.

Any open browser keeps the app running, a hidden window's browser and a popup included. A browser Chromium opened on its own (`ChromeUi`) closes with the app instead. [`AppHandle::should_shutdown`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.should_shutdown) turns true once the app has ended.

Ctrl+C closes the browsers as closing their windows by hand would. Their unload handlers run. Kurogane installs that handler only when it owns the windows. An embedded app handles Ctrl+C itself.

## Ending on purpose

[`AppHandle::shutdown`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.shutdown) ends the app from any thread:

* No page can cancel it. A page's `beforeunload` leaves without asking.
* Nothing opens after it begins. Popups are refused. `create_window` and `create_child_browser` fail with [`RuntimeError::ShuttingDown`](https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.ShuttingDown).
* The call doesn't wait for the browsers to close.

[`AppHandle::close_all_browsers`](https://kurogane-rs.org/docs/api/app/apphandle.md#method.close_all_browsers) takes a `force` flag. With `force` it ends the app as `shutdown` does. Without it a page's `beforeunload` may keep its browser and the app running. [`BrowserHandle::close`](https://kurogane-rs.org/docs/api/app/browserhandle.md#method.close) takes the same flag for one browser.

## Hearing windows close

[`App::on_window_closing`](https://kurogane-rs.org/docs/api/app/app.md#method.on_window_closing) hears each application window close, however it closes. The user, the page, your app and Ctrl+C all count. It reports the window's name and its [placement](https://kurogane-rs.org/docs/api/windows/windowplacement.md) to save for the next run.

The hook runs on the UI thread before the last window ends the app. Popups, DevTools and embedded browsers aren't reported. Saving a small file there is fine. Anything slow isn't. A hook that panics is logged and the window closes as it would.

## Shutting Chromium down

[`App::run`](https://kurogane-rs.org/docs/api/app/app.md#method.run) shuts Chromium down by itself once the app has ended.

An app that runs its own loop keeps pumping until `should_shutdown` turns true. Then it calls [`AppInstance::shutdown`](https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown). Dropping the `AppInstance` does the same.

Once Chromium is shut down, `AppHandle` and `BrowserHandle` calls that would reach it do nothing. Stop using handles on other threads before you shut down.

## Embedded apps

An embedded app owns its windows. Closing a browser never closes your window. Your window decides when to close and your loop decides when to end:

* On a close request, ask the browsers to close with `close_all_browsers(true)`. Don't leave the loop yet.
* Keep pumping. The close sequence runs inside `pump`.
* Once `should_shutdown` turns true, release your window, call `AppInstance::shutdown` and leave the loop.

With winit:

```rust
fn window_event(&mut self, _: &ActiveEventLoop, _: WindowId, event: WindowEvent) {
    if let WindowEvent::CloseRequested = event {
        // Ask the browsers to close and keep pumping
        self.runtime.handle().close_all_browsers(true);
    }
}

fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) {
    // Pump at the deadline Chromium asked for

    if self.runtime.should_shutdown() {
        self.window = None;
        self.runtime.shutdown();
        event_loop.exit();
    }
}
```

> [!WARNING]
> Leaving the loop or dropping your window on the close request stops the pump. The close sequence never finishes, the browser count never reaches zero and `should_shutdown` never turns true.

End the loop on `should_shutdown` and not on a flag of your own. Your window isn't the only way the browsers end. On macOS Quit closes them all without a close request to your window.
