# One instance per profile

> Handle a second launch in the running app, run separate copies and choose where the profile lives.

Chromium runs one instance of your app per profile. Starting the app again while it runs brings its windows to the front instead of opening another copy. The new launch hands its arguments to the running app and exits with status 0.

## Open what a second launch names

A launch often carries something to open. The user double-clicks a file, chooses your app from **Open With**, clicks a `myapp://` link or runs:

```text
myapp notes.txt
```

[`App::on_second_instance`](https://kurogane-rs.org/docs/api/app/app.md#method.on_second_instance) receives that launch in the running app. Open the file in the existing window:

```rust
use kurogane::App;

fn main() {
    App::new("content")
        .on_second_instance(|launch, app| {
            for arg in launch.args() {
                // Relative to where the new launch started
                let path = match launch.working_dir() {
                    Some(dir) => dir.join(arg),
                    None => arg.into(),
                };
                app.broadcast_json("open-file", &path);
            }
        })
        .run_or_exit();
}
```

```javascript
window.kurogane.on("open-file", (json) => openFile(JSON.parse(json)));
```

[`SecondInstance`](https://kurogane-rs.org/docs/api/app/secondinstance.md) describes the launch:

* `args()` lists its arguments that aren't switches, in order and without the program name.
* `working_dir()` is the directory it started in. Relative paths among its arguments are relative to it.
* `switch("new-window")` gives the value of `--new-window`. It's an empty string for a switch without a value and `None` when the switch is absent.

Without a hook a second launch only brings the windows to the front.

The first launch reads its own arguments with `std::env::args` as any Rust program does.

## Open a window for a second launch

The page can open an application window of your own origin with `window.open`. Ask it to from the hook:

```rust
use kurogane::App;

fn main() {
    App::new("content")
        .on_second_instance(|launch, app| {
            if launch.switch("new-window").is_some() {
                app.broadcast_json("new-window", &true);
            }
        })
        .run_or_exit();
}
```

```javascript
window.kurogane.on("new-window", () => window.open("/index.html"));
```

## Second launches on macOS

Opening the app's bundle while it runs activates it without starting another process. The hook runs only for launches that start a process, such as running the executable from a terminal.

## Run separate copies

Two copies that must run at the same time need different profiles. [`App::profile_id`](https://kurogane-rs.org/docs/api/app/app.md#method.profile_id) names one:

```rust
use kurogane::App;

fn main() {
    let profile = std::env::var("APP_PROFILE").unwrap_or_else(|_| "default".into());
    App::new("content").profile_id(profile).run_or_exit();
}
```

Debug builds use a profile of their own. A development run doesn't hand its launch to an installed copy that runs at the same time.

## Where the profile lives

The profile holds your app's cookies, storage, permissions and caches. Kurogane keeps it in `kurogane/profiles/<id>` in the local data directory. Cache cleaners leave it alone there:

| Platform | Profile |
|---|---|
| Linux | `~/.local/share/kurogane/profiles/<id>` |
| macOS | `~/Library/Application Support/kurogane/profiles/<id>` |
| Windows | `%LOCALAPPDATA%\kurogane\profiles\<id>` |

`<id>` is the `profile_id` or the executable's name. A debug build adds `-dev`. `kurogane list profiles` lists them. `kurogane clean all` removes them.

### Keep the profile next to the app

[`App::profile_dir`](https://kurogane-rs.org/docs/api/app/app.md#method.profile_dir) puts the profile in a directory of your choosing. A portable app keeps it next to its executable:

```rust
use kurogane::App;

fn main() {
    let exe = std::env::current_exe().expect("the executable's path");
    let profile = exe.parent().expect("a folder").join("profile");
    App::new("content").profile_dir(profile).run_or_exit();
}
```

* The directory is created when missing.
* It takes an absolute path. A relative one fails at startup.
* It's used as given in debug builds too. A development run then shares it with an installed copy and hands its launch over when that copy runs.
* It can't be combined with `profile_id`.

## What lasts from one run to the next

Cookies with an expiry, local storage and everything else the profile holds last between runs.

Session cookies end with each run as in a browser. They're the cookies a server sets without `Expires` or `Max-Age`. Every run starts with your app's windows alone, not the last session's.

A login meant to outlast a run uses a cookie with an expiry:

```http
Set-Cookie: session=...; Max-Age=2592000; Path=/; Secure; HttpOnly; SameSite=Lax
```

A page can keep its token in local storage instead.

## Rules for the hook

* It runs on the UI thread in the running app.
* A later call replaces an earlier hook.
