use crate::config::{
chrome_for_testing_path, reject_real_browser_profile, validate_extra_args, validate_profile_dir,
};
use crate::error::{BrowserError, Result};
use rightkit_child_process::OwnedCommand;
use std::path::PathBuf;
use std::process::Stdio;
#[derive(Clone, Debug)]
pub struct VisibleLaunch {
pub url: String,
pub profile_dir: PathBuf,
pub executable: Option<PathBuf>,
pub args: Vec<String>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct VisibleExit {
pub success: bool,
pub code: Option<i32>,
}
pub fn run_visible_until_exit(launch: &VisibleLaunch) -> Result<VisibleExit> {
validate_profile_dir(&launch.profile_dir)?;
reject_real_browser_profile(&launch.profile_dir)?;
validate_extra_args(&launch.args)?;
let url = url::Url::parse(&launch.url)
.map_err(|e| BrowserError::Invalid(format!("visible browser URL: {e}")))?;
if !matches!(url.scheme(), "http" | "https") {
return Err(BrowserError::Invalid(
"visible browser URL must be http or https".into(),
));
}
let executable = match &launch.executable {
Some(path) => path.clone(),
None => chrome_for_testing_path(None)?,
};
let mut cmd = OwnedCommand::new(&executable);
cmd.command_mut()
.arg(format!("--user-data-dir={}", launch.profile_dir.display()))
.arg("--no-first-run")
.arg("--no-default-browser-check")
.args(&launch.args)
.arg(url.as_str())
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null());
let mut child = cmd
.spawn()
.map_err(|e| BrowserError::Launch(e.to_string()))?;
let status = child
.wait()
.map_err(|e| BrowserError::Launch(format!("waiting for the visible browser: {e}")))?;
Ok(VisibleExit {
success: status.success(),
code: status.code(),
})
}