rightkit_browser/
visible.rs1use crate::config::{
9 chrome_for_testing_path, reject_real_browser_profile, validate_extra_args, validate_profile_dir,
10};
11use crate::error::{BrowserError, Result};
12use rightkit_child_process::OwnedCommand;
13use std::path::PathBuf;
14use std::process::Stdio;
15
16#[derive(Clone, Debug)]
18pub struct VisibleLaunch {
19 pub url: String,
21 pub profile_dir: PathBuf,
24 pub executable: Option<PathBuf>,
26 pub args: Vec<String>,
28}
29
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
32pub struct VisibleExit {
33 pub success: bool,
35 pub code: Option<i32>,
37}
38
39pub fn run_visible_until_exit(launch: &VisibleLaunch) -> Result<VisibleExit> {
46 validate_profile_dir(&launch.profile_dir)?;
47 reject_real_browser_profile(&launch.profile_dir)?;
48 validate_extra_args(&launch.args)?;
49 let url = url::Url::parse(&launch.url)
50 .map_err(|e| BrowserError::Invalid(format!("visible browser URL: {e}")))?;
51 if !matches!(url.scheme(), "http" | "https") {
52 return Err(BrowserError::Invalid(
53 "visible browser URL must be http or https".into(),
54 ));
55 }
56 let executable = match &launch.executable {
57 Some(path) => path.clone(),
58 None => chrome_for_testing_path(None)?,
59 };
60 let mut cmd = OwnedCommand::new(&executable);
61 cmd.command_mut()
62 .arg(format!("--user-data-dir={}", launch.profile_dir.display()))
63 .arg("--no-first-run")
64 .arg("--no-default-browser-check")
65 .args(&launch.args)
66 .arg(url.as_str())
67 .stdin(Stdio::null())
68 .stdout(Stdio::null())
69 .stderr(Stdio::null());
70 let mut child = cmd
71 .spawn()
72 .map_err(|e| BrowserError::Launch(e.to_string()))?;
73 let status = child
74 .wait()
75 .map_err(|e| BrowserError::Launch(format!("waiting for the visible browser: {e}")))?;
76 Ok(VisibleExit {
77 success: status.success(),
78 code: status.code(),
79 })
80}