# Control links, popups and navigation

> Decide which windows a page may open and where it may take its window.

A page can't give itself a window or take its window anywhere it likes. Kurogane decides first and asks your app's hooks where you give them.

## What happens by default

A page asks for a window with `window.open`, a `target="_blank"` link, a form that targets a new window or a link clicked with Ctrl (Cmd on macOS), the middle button or Shift:

* A page of your app's own origin opens in an application window. A window the page fills in itself (`about:blank`) does too.
* An `http` or `https` link the user clicked opens in the system's default browser.
* Anything else is refused. That includes a script opening a website on its own, `mailto:`, `file:` and custom schemes.

A page can also take its own window somewhere else with a link, `location = …`, a form or a redirect. A window shows only the origins let into it:

* Your app's own origin
* Every origin your app loaded there itself (the start URL, `create_window` and `BrowserHandle::navigate`) with the redirects they lead to
* The origin `on_new_window` opened a popup to
* Every origin `on_navigation` let in before

Within those a page navigates freely. A navigation anywhere else is answered as a new window is. A clicked `http` or `https` link opens in the system browser. Anything else is refused and the window stays on its page.

## Keep a sign-in popup inside the app

A sign-in provider often reports back to the page that opened it. It has to run inside your app. [`App::on_new_window`](https://kurogane-rs.org/docs/api/app/app.md#method.on_new_window) lets its origin open in an application window:

```rust
use kurogane::{App, NewWindowDecision, Origin};

fn main() {
    let sign_in = Origin::parse("https://accounts.example.com").unwrap();

    App::new("content")
        .on_new_window(move |request, _app| {
            if request.origin() == &sign_in {
                NewWindowDecision::Allow
            } else {
                NewWindowDecision::Default
            }
        })
        .run_or_exit();
}
```

[`NewWindowDecision`](https://kurogane-rs.org/docs/api/requests/newwindowdecision.md) has four answers:

| Decision | What happens |
|---|---|
| `Allow` | opens the page in an application window whatever its origin |
| `Deny` | opens no window |
| `OpenExternal` | opens the URL in the system browser; only a clicked `http` or `https` link goes there |
| `Default` | Kurogane's policy decides |

An allowed page reaches only the commands and events that [`App::permit`](https://kurogane-rs.org/docs/api/app/app.md#method.permit) grants its origin.

## Let a provider into the window

A page may send the user to a sign-in provider in the same window. [`App::on_navigation`](https://kurogane-rs.org/docs/api/app/app.md#method.on_navigation) lets its origin in:

```rust
use kurogane::{App, NavigationDecision, Origin};

fn main() {
    let sign_in = Origin::parse("https://accounts.example.com").unwrap();

    App::url("https://app.example.com")
        .on_navigation(move |navigation, _app| {
            if navigation.origin() == &sign_in {
                NavigationDecision::Allow
            } else {
                NavigationDecision::Default
            }
        })
        .run_or_exit();
}
```

`Allow` loads the page and lets its origin into that window from then on. The provider sends the user back to `app.example.com`. The window may always show it. `Deny`, `OpenExternal` and `Default` answer as they do for new windows.

Your app's own loads, back and forward navigation and frames inside a page never reach the hook. Frames aren't guarded. `App::permit` decides what a frame of another origin reaches.

## Refuse popups a script opens

[`NewWindowRequest::kind`](https://kurogane-rs.org/docs/api/requests/newwindowrequest.md#method.kind) says how the page asked. [`NewWindowRequest::user_gesture`](https://kurogane-rs.org/docs/api/requests/newwindowrequest.md#method.user_gesture) says whether the user clicked or a script asked on its own:

```rust
use kurogane::{App, NewWindowDecision, NewWindowKind};

fn main() {
    App::new("content")
        .on_new_window(|request, _app| {
            if request.kind() == NewWindowKind::Popup && !request.user_gesture() {
                NewWindowDecision::Deny
            } else {
                NewWindowDecision::Default
            }
        })
        .run_or_exit();
}
```

| [`NewWindowKind`](https://kurogane-rs.org/docs/api/requests/newwindowkind.md) | The page asked with |
|---|---|
| `Tab` | a `target="_blank"` link, `window.open` without features or Ctrl or Cmd |
| `BackgroundTab` | the middle button or Ctrl and Shift |
| `Popup` | `window.open` with a size or position |
| `Window` | Shift |
| `PictureInPicture` | `documentPictureInPicture` |

Kurogane opens every kind as an application window. The kind lets a policy treat each differently.

## Compare origins, not URLs

Compare [`Origin`](https://kurogane-rs.org/docs/api/ipc/origin.md) values and never URL strings. `https://accounts.example.com.evil.net` starts with `https://accounts.example.com`.

A request gives every origin it involves:

| Request | Methods |
|---|---|
| [`NewWindowRequest`](https://kurogane-rs.org/docs/api/requests/newwindowrequest.md) | `url`, `origin`, `opener_origin`, `kind`, `user_gesture` |
| [`NavigationRequest`](https://kurogane-rs.org/docs/api/requests/navigationrequest.md) | `url`, `origin`, `from_origin`, `user_gesture`, `is_redirect` |

A window the page fills in itself has the page's origin. A `blob:` URL has the origin of the page that made it.

## Test a policy

Write the policy as a function and make requests the way a page sends them:

```rust
use kurogane::{NewWindowDecision, NewWindowKind, NewWindowRequest};

fn policy(request: &NewWindowRequest) -> NewWindowDecision {
    if request.kind() == NewWindowKind::Popup && !request.user_gesture() {
        NewWindowDecision::Deny
    } else {
        NewWindowDecision::Default
    }
}

#[test]
fn scripted_popups_are_refused() {
    let scripted = NewWindowRequest::new("https://ads.example/", "app://app/index.html")
        .with_kind(NewWindowKind::Popup);
    assert_eq!(policy(&scripted), NewWindowDecision::Deny);
}
```

Hand the function to the hook with `.on_new_window(|request, _app| policy(request))`.

## Rules for the hooks

* Both hooks run on the UI thread before the window or the navigation starts. They must not block.
* A hook that panics refuses the window or the navigation.
* A later call replaces an earlier hook.
* Chromium's own tabbed browser window never opens. Its new window and new tab shortcuts do nothing.
