Repository: AmbientRun/Ambient
File: campfire/src/web/browser.rs (Rust)

Code before:
        ])
        .arg(format!("--ignore-certificate-errors-spki-list={spki}"))
        .spawn()
        .context("Failed to open Google Chrome")?
        .wait()
        .context("Failed to wait for launch command to exit")?;

    if !status.success() {
        anyhow::bail!("Failed to launch browser. Process exited with {status:?}");
    }

    Ok(())
}

#[cfg(target_os = "linux")]
fn spawn(spki: &str, url: &str) -> anyhow::Result<()> {
    const CANDIDATES: &[&str] = &["google-chrome", "google-chrome-stable", "chromium"];

    let status = try_until_success(CANDIDATES, |cmd| {
        detach_process(
            Command::new(cmd)
                .arg(url)
                // Feeding a url to chrome here makes `--args spki` not be fed to chrome
                .arg(format!("--ignore-certificate-errors-spki-list={spki}")),
        )
        .spawn()
        .map_err(anyhow::Error::from)
    })
    .context("Failed to open Google Chrome; no installs found")?
    .wait()
    .context("Failed to wait for launch command to exit")?;

    if !status.success() {
        anyhow::bail!("Failed to launch browser. Process exited with {status:?}");
    }

    Ok(())
}

#[cfg(target_os = "linux")]
fn try_until_success<T>(
    inputs: &[&str],
    f: impl Fn(&str) -> anyhow::Result<T>,
) -> anyhow::Result<T> {
    let mut errors = vec![];

    for input in inputs {
        match f(input) {
            Ok(success) => return Ok(success),
            Err(error) => errors.push((input, error.to_string())),
        }

Review comment (bug):
👍 Good strategy to disable fallbacks if something explicit is passed, and not the other way around.

Have fallen victim of "you think you specified an override, but it was incorrect so a fallback was silently used for 3 months" *many* times 😅 

Code after:
    let status = Command::new("open")
        .args([
            "-a",
            options.app.as_deref().unwrap_or("Google Chrome"),
            "--args",
            "--origin-to-force-quic-on=127.0.0.1:4433",
        ])
        .arg(format!("--ignore-certificate-errors-spki-list={spki}"))
        .arg(url)
        .spawn()
        .context("Failed to open Google Chrome")?
        .wait()
        .context("Failed to wait for launch command to exit")?;

    if !status.success() {
        anyhow::bail!("Failed to launch browser. Process exited with {status:?}");
    }

    Ok(())
}

#[cfg(target_os = "linux")]
fn spawn(spki: &str, url: &str, options: BrowserOptions) -> anyhow::Result<()> {
    const CANDIDATES: &[&str] = &["google-chrome", "google-chrome-stable", "chromium"];
    let provided_candidate = &[options.app.as_deref().unwrap_or("google-chrome")];
    let candidates = if options.app.is_some() {
        provided_candidate
    } else {
        CANDIDATES
    };

    let status = try_until_success(candidates, |cmd| {
        detach_process(
            Command::new(cmd)
                .arg(url)
                // Feeding a url to chrome here makes `--args spki` not be fed to chrome
                .arg(format!("--ignore-certificate-errors-spki-list={spki}")),
        )
        .spawn()
        .map_err(anyhow::Error::from)
    })
    .context("Failed to open Google Chrome; no installs found")?
    .wait()
    .context("Failed to wait for launch command to exit")?;

    if !status.success() {
        anyhow::bail!("Failed to launch browser. Process exited with {status:?}");
    }

    Ok(())
}