# Struct `kurogane::AppHandle`

```rust
pub struct AppHandle { /* private fields */ }
```

Shared lifecycle handle for a running Kurogane application.

Obtain one with [`AppInstance::handle`]; clones share the application.

## Threads

Usable from any thread. Queries copy what is open under a short lock.
A broadcast sends to its subscribers on the calling thread, which CEF
allows for frames in the browser process. Closing and ending run on CEF's
UI thread. They run at once when called there and are posted there
otherwise.

Once [`AppInstance::shutdown`] has begun, calls that would reach CEF do
nothing, since CEF takes no call after it has shut down.

[`AppInstance::handle`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.handle
[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown

## Methods

### `shutdown`

```rust
pub fn shutdown(&self)
```

Ends the application by closing all browsers.

The application ends after the last browser closes. Calling this from
another thread posts the close to the UI thread. The call does not wait
for the browsers to close. Does nothing once [`AppInstance::shutdown`]
has begun.

This end cannot be cancelled and no browser may open after it begins.
Popups are refused, [`AppInstance::create_window`] and
[`AppInstance::create_child_browser`] return [`RuntimeError::ShuttingDown`]
and any browser CEF is already creating is closed as it appears. No
context menu opens either, not even the one whose
[`App::on_context_menu`](https://kurogane-rs.org/docs/api/app/app.md#method.on_context_menu) hook began the end.

[`AppInstance::create_window`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.create_window
[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown
[crate::App::on_context_menu]: https://kurogane-rs.org/docs/api/app/app.md#method.on_context_menu
[`RuntimeError::ShuttingDown`]: https://kurogane-rs.org/docs/api/app/runtimeerror.md#variant.ShuttingDown
[`AppInstance::create_child_browser`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.create_child_browser

### `should_shutdown`

```rust
pub fn should_shutdown(&self) -> bool
```

Returns whether the application has ended.

Becomes true once the last browser has closed, whatever closed it (its
window, [`AppHandle::shutdown`], Ctrl+C or macOS asking the
application to quit). Any of those three that finds nothing open ends
it at once. Also true once [`AppInstance::shutdown`] has begun. An
application that pumps CEF itself keeps pumping until then.

[`AppHandle::shutdown`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.shutdown
[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown

### `broadcast`

```rust
pub fn broadcast(&self, event: &str, data: &[u8])
```

Broadcast bytes to all renderers subscribed to event.

A page's `kurogane.on` callback receives them as an `ArrayBuffer`. The
event is delivered asynchronously to every active subscription for the
given event name. This method is thread-safe and returns immediately after
queuing the event for delivery. Does nothing once
[`AppInstance::shutdown`] has begun.

[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown

### `broadcast_json`

```rust
pub fn broadcast_json<T: Serialize>(&self, event: &str, value: &T)
```

Broadcast a JSON-serializable event to all renderers subscribed to event.

The value is serialized to JSON. A page's `kurogane.on` callback receives
the JSON text as a string. This is the preferred way to emit structured
events.

### `browser_count`

```rust
pub fn browser_count(&self) -> usize
```

Number of currently live browser instances.

### `window_count`

```rust
pub fn window_count(&self) -> usize
```

Number of currently open windows.

### `window_ids`

```rust
pub fn window_ids(&self) -> Vec<WindowId>
```

IDs of all open windows.

### `close_all_windows`

```rust
pub fn close_all_windows(&self)
```

Closes all open windows.

Safe to call from any thread. CEF's windows close only on the UI
thread; a call from elsewhere is posted there. Does nothing once
[`AppInstance::shutdown`] has begun.

[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown

### `close_all_browsers`

```rust
pub fn close_all_browsers(&self, force: bool)
```

Closes all live browser instances.

The call does not wait. A browser counts in [`AppHandle::browser_count`]
until CEF has finished closing it. An application that pumps CEF keeps
pumping until the count is 0. A browser embedded in the
application's own window closes without asking that window to close.
A call from another thread is posted to the UI thread. Does nothing
once [`AppInstance::shutdown`] has begun.

With `force` no page can cancel the close. A call that finds a browser
open then ends the application as [`AppHandle::shutdown`] does and
nothing opens after it. Without it a page's `beforeunload` may keep its
browser and the application running.

[`AppHandle::browser_count`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.browser_count
[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown
[`AppHandle::shutdown`]: https://kurogane-rs.org/docs/api/app/apphandle.md#method.shutdown

### `find_window_by_browser`

```rust
pub fn find_window_by_browser(&self, browser_id: BrowserId) -> Option<WindowId>
```

Looks up the window that hosts a given browser.

None once the browser has closed, even while CEF is still closing
its window.

### `find_window_by_name`

```rust
pub fn find_window_by_name(&self, name: &str) -> Option<WindowId>
```

Looks up the open window named `name`
([`WindowOptions::name`](https://kurogane-rs.org/docs/api/windows/windowoptions.md#method.name)).

None once its close has been reported
([`App::on_window_closing`](https://kurogane-rs.org/docs/api/app/app.md#method.on_window_closing)), even
while CEF is still closing it. The name is then free for a new window.

[crate::App::on_window_closing]: https://kurogane-rs.org/docs/api/app/app.md#method.on_window_closing
[crate::WindowOptions::name]: https://kurogane-rs.org/docs/api/windows/windowoptions.md#method.name

### `browsers`

```rust
pub fn browsers(&self) -> Vec<(BrowserId, BrowserMetadata)>
```

Metadata for all live browsers.

### `windows`

```rust
pub fn windows(&self) -> Vec<(WindowId, WindowMetadata)>
```

Metadata for all open windows.

### `browser_parent`

```rust
pub fn browser_parent(&self, id: BrowserId) -> Option<BrowserId>
```

Parent of a given browser.

### `browser_opener`

```rust
pub fn browser_opener(&self, id: BrowserId) -> Option<BrowserId>
```

Opener of a given browser.

### `children_of`

```rust
pub fn children_of(&self, id: BrowserId) -> Vec<BrowserId>
```

All children of the given parent browser.

### `browser_for_window`

```rust
pub fn browser_for_window(&self, id: WindowId) -> Option<BrowserId>
```

Browser hosted in the given window.

None until the window's browser has been created and once it has
closed.

### `forget_permissions`

```rust
pub fn forget_permissions(&self, origin: &Origin)
```

Forgets the permission answers Chromium remembers for `origin`. The
site's next request then reaches
[`App::on_permission`](https://kurogane-rs.org/docs/api/app/app.md#method.on_permission) again. Use it after
the application's policy changed or the user took a permission back.

Chromium remembers its answers to web sites (http, https), granted
or denied, in the profile. It forgets them nowhere else. Forgetting
also takes back a grant the site holds now; its notifications stop.
Applies to the profile of every open browser, an embedded browser's
own profile included. A call from another thread is posted to the UI
thread. Does nothing for the opaque origin, or once
[`AppInstance::shutdown`] has begun.

[crate::App::on_permission]: https://kurogane-rs.org/docs/api/app/app.md#method.on_permission
[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown

### `get_browser_handle`

```rust
pub fn get_browser_handle(&self, id: BrowserId) -> Option<BrowserHandle>
```

Creates a BrowserHandle for a registered browser, if it exists.

Returns None if no browser with the given BrowserId is registered, or
once [`AppInstance::shutdown`] has begun.

[`AppInstance::shutdown`]: https://kurogane-rs.org/docs/api/app/appinstance.md#method.shutdown

## Trait implementations

- `impl Clone for AppHandle`

Source: https://github.com/0x48piraj/kurogane/blob/584c586118ee53cc04604007ee038557bd859092/kurogane/src/runtime.rs#L512
