use std::collections::VecDeque;
use std::ffi::OsString;
use std::io::{BufRead, BufReader, Read, Write};
use std::net::{Ipv4Addr, TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use crate::browser::cdp::Cdp;
use crate::browser::{bidi, platform};
use crate::codec::json::Value;
use crate::error::{Error, Result};
pub const BROWSER_ENV: &str = "TVPIPE_BROWSER";
const START_TIMEOUT: Duration = Duration::from_secs(30);
const QUIT_TIMEOUT: Duration = Duration::from_secs(5);
const STDERR_TAIL: usize = 20;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Engine {
Chromium,
Firefox,
}
impl Engine {
pub fn of(bin: &Path) -> Engine {
let name = bin
.file_name()
.map(|n| n.to_string_lossy().to_ascii_lowercase())
.unwrap_or_default();
if name.contains("firefox") {
Engine::Firefox
} else {
Engine::Chromium
}
}
}
pub fn find_browser(explicit: Option<&Path>) -> Result<PathBuf> {
find_with(explicit, &|k| std::env::var_os(k), &|p| p.is_file())
}
fn find_with(
explicit: Option<&Path>,
env: &dyn Fn(&str) -> Option<OsString>,
exists: &dyn Fn(&Path) -> bool,
) -> Result<PathBuf> {
let path_dirs: Vec<PathBuf> = env("PATH")
.map(|p| std::env::split_paths(&p).collect())
.unwrap_or_default();
let on_path = |name: &Path| -> Option<PathBuf> {
path_dirs
.iter()
.map(|d| d.join(name))
.find(|p| exists(p))
.or_else(|| {
let suffix = std::env::consts::EXE_SUFFIX;
let mut with = name.as_os_str().to_owned();
with.push(suffix);
(!suffix.is_empty() && name.extension().is_none())
.then(|| path_dirs.iter().map(|d| d.join(&with)).find(|p| exists(p)))
.flatten()
})
};
let firefox = || {
platform::firefox_install_paths(env)
.into_iter()
.find(|p| exists(p))
.or_else(|| {
platform::firefox_names()
.iter()
.find_map(|n| on_path(Path::new(n)))
})
};
let given = |p: &Path, what: &str| -> Result<PathBuf> {
if exists(p) {
return Ok(p.to_path_buf());
}
if let Some(exe) = app_bundle_executable(p).into_iter().find(|e| exists(e)) {
return Ok(exe);
}
if p.components().count() == 1 {
if let Some(found) = on_path(p) {
return Ok(found);
}
if p.as_os_str().eq_ignore_ascii_case("firefox") {
if let Some(found) = firefox() {
return Ok(found);
}
}
}
Err(Error::not_found(format!(
"{what} {}: no such browser executable",
p.display()
)))
};
if let Some(p) = explicit {
return given(p, "--browser");
}
if let Some(p) = env(BROWSER_ENV).filter(|v| !v.is_empty()) {
return given(Path::new(&p), &format!("${BROWSER_ENV}"));
}
if let Some(p) = platform::browser_install_paths(env)
.into_iter()
.find(|p| exists(p))
{
return Ok(p);
}
if let Some(p) = platform::browser_names()
.iter()
.find_map(|n| on_path(Path::new(n)))
{
return Ok(p);
}
if let Some(p) = firefox() {
return Ok(p);
}
Err(Error::not_found(format!(
"no browser found (Chrome, Edge, Chromium, Brave or Firefox); \
install one or name it with --browser PATH or ${BROWSER_ENV}"
)))
}
fn app_bundle_executable(p: &Path) -> Vec<PathBuf> {
let Some(name) = p
.file_name()
.and_then(|n| n.to_str())
.and_then(|n| n.strip_suffix(".app"))
else {
return Vec::new();
};
let dir = p.join("Contents/MacOS");
vec![dir.join(name), dir.join("firefox")]
}
const TEMP_PREFIX: &str = "tvpipe-browser-";
const OWNER_FILE: &str = "tvpipe-owner";
const ORPHAN_AGE: Duration = Duration::from_secs(120);
#[derive(Debug)]
pub struct Profile {
dir: PathBuf,
temporary: bool,
beacon: Option<Beacon>,
}
impl Profile {
pub fn temporary() -> Result<Profile> {
Profile::temporary_in(&std::env::temp_dir())
}
fn temporary_in(root: &Path) -> Result<Profile> {
let nonce = crate::util::hex::encode(&crate::crypto::ring::random_array::<16>()?);
let dir = root.join(format!("{TEMP_PREFIX}{}", &nonce[..12]));
std::fs::create_dir(&dir).map_err(|e| Error::io(format!("create {}", dir.display()), e))?;
let mut p = Profile {
dir,
temporary: true,
beacon: None,
};
let beacon = Beacon::start(nonce.clone())?;
let owner = format!("{} {} {nonce}\n", std::process::id(), beacon.port);
p.beacon = Some(beacon);
std::fs::write(p.dir.join(OWNER_FILE), owner)
.map_err(|e| Error::io(format!("write {}", p.dir.display()), e))?;
Ok(p)
}
pub fn persistent(dir: &Path) -> Result<Profile> {
std::fs::create_dir_all(dir)
.map_err(|e| Error::io(format!("create {}", dir.display()), e))?;
let dir = std::fs::canonicalize(dir)
.map_err(|e| Error::io(format!("resolve {}", dir.display()), e))?;
let real = platform::default_profile_dirs(&|k| std::env::var_os(k));
check_not_real(&dir, &real)?;
Ok(Profile {
dir,
temporary: false,
beacon: None,
})
}
pub fn dir(&self) -> &Path {
&self.dir
}
}
fn check_not_real(dir: &Path, real: &[PathBuf]) -> Result<()> {
for r in real {
let r = std::fs::canonicalize(r).unwrap_or_else(|_| r.clone());
if dir.starts_with(&r) || r.starts_with(dir) {
return Err(Error::usage(format!(
"--profile {}: that is (or holds) a browser's own profile ({}); \
tvpipe uses a separate one: pick an empty directory",
dir.display(),
r.display()
)));
}
}
Ok(())
}
impl Drop for Profile {
fn drop(&mut self) {
self.beacon = None;
if !self.temporary {
return;
}
let deadline = Instant::now() + Duration::from_secs(5);
loop {
match std::fs::remove_dir_all(&self.dir) {
Ok(()) => return,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return,
Err(e) if Instant::now() >= deadline => {
warn!("cannot delete {}: {e}", self.dir.display());
return;
}
Err(_) => std::thread::sleep(Duration::from_millis(100)),
}
}
}
}
#[derive(Debug)]
struct Beacon {
port: u16,
stop: Arc<AtomicBool>,
thread: Option<std::thread::JoinHandle<()>>,
}
impl Beacon {
fn start(nonce: String) -> Result<Beacon> {
let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, 0))
.map_err(|e| Error::io("bind the profile beacon", e))?;
let port = listener
.local_addr()
.map_err(|e| Error::io("profile beacon address", e))?
.port();
let stop = Arc::new(AtomicBool::new(false));
let st = stop.clone();
let thread = std::thread::Builder::new()
.name("profile-beacon".into())
.spawn(move || {
for conn in listener.incoming() {
if st.load(Ordering::SeqCst) {
return;
}
if let Ok(mut c) = conn {
let _ = c.set_write_timeout(Some(Duration::from_secs(1)));
let _ = c.write_all(nonce.as_bytes());
}
}
})
.map_err(|e| Error::io("spawn the profile beacon", e))?;
Ok(Beacon {
port,
stop,
thread: Some(thread),
})
}
}
impl Drop for Beacon {
fn drop(&mut self) {
self.stop.store(true, Ordering::SeqCst);
let _ = TcpStream::connect_timeout(
&(Ipv4Addr::LOCALHOST, self.port).into(),
Duration::from_secs(1),
);
if let Some(t) = self.thread.take() {
crate::util::thread::join_until(t, Instant::now() + Duration::from_secs(2));
}
}
}
pub fn sweep_stale_profiles() -> usize {
sweep_in(&std::env::temp_dir(), ORPHAN_AGE)
}
fn sweep_in(root: &Path, orphan_age: Duration) -> usize {
let Ok(entries) = std::fs::read_dir(root) else {
return 0;
};
let mut removed = 0;
for e in entries.flatten() {
if !e.file_name().to_string_lossy().starts_with(TEMP_PREFIX)
|| !e.file_type().is_ok_and(|t| t.is_dir())
{
continue;
}
let dir = e.path();
let closed = match owner(&dir, orphan_age) {
Owner::Alive => continue,
Owner::Unknown if browser_answers(&dir) => continue,
Owner::Unknown => Ok(false),
Owner::Gone => close_stale_browser(&dir),
};
let closed = match closed {
Ok(closed) => closed,
Err(e) => {
warn!(
"browser: {} was left by an earlier run, but its browser {e}; leaving it",
dir.display()
);
continue;
}
};
let deadline = Instant::now() + Duration::from_secs(2);
let gone = loop {
match std::fs::remove_dir_all(&dir) {
Ok(()) => break true,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => break true,
Err(_) if Instant::now() < deadline => {
std::thread::sleep(Duration::from_millis(100))
}
Err(e) => {
debug!("browser: cannot delete {}: {e}", dir.display());
break false;
}
}
};
if gone {
removed += 1;
info!(
"browser: removed {} left by an earlier run{}",
dir.display(),
if closed {
" (and quit its browser)"
} else {
""
}
);
}
}
removed
}
#[derive(Debug, PartialEq, Eq)]
enum Owner {
Alive,
Gone,
Unknown,
}
fn owner(dir: &Path, orphan_age: Duration) -> Owner {
let Ok(owner) = std::fs::read_to_string(dir.join(OWNER_FILE)) else {
let age = std::fs::metadata(dir)
.and_then(|m| m.modified())
.ok()
.and_then(|t| t.elapsed().ok());
return if age.is_none_or(|a| a < orphan_age) {
Owner::Alive
} else {
Owner::Unknown
};
};
let mut f = owner.split_whitespace();
let (_pid, port, nonce) = (f.next(), f.next(), f.next());
match (port.and_then(|p| p.parse::<u16>().ok()), nonce) {
(Some(port), Some(nonce)) if beacon_answers(port, nonce) => Owner::Alive,
_ => Owner::Gone,
}
}
fn stale_endpoint(dir: &Path) -> Option<(Engine, u16, String)> {
if let Some((port, path)) = std::fs::read_to_string(dir.join("DevToolsActivePort"))
.ok()
.and_then(|s| parse_active_port(&s))
{
return Some((
Engine::Chromium,
port,
format!("ws://127.0.0.1:{port}{path}"),
));
}
let port = bidi::server_file(dir)?;
Some((Engine::Firefox, port, bidi::session_url(port)))
}
fn browser_answers(dir: &Path) -> bool {
stale_endpoint(dir)
.is_some_and(|(_, _, url)| Cdp::connect(&url, Duration::from_secs(2)).is_ok())
}
fn beacon_answers(port: u16, nonce: &str) -> bool {
let addr = (Ipv4Addr::LOCALHOST, port).into();
let Ok(mut c) = TcpStream::connect_timeout(&addr, Duration::from_millis(500)) else {
return false;
};
let _ = c.set_read_timeout(Some(Duration::from_secs(1)));
let mut got = Vec::new();
let _ = Read::by_ref(&mut c).take(64).read_to_end(&mut got);
got == nonce.as_bytes()
}
fn close_stale_browser(dir: &Path) -> Result<bool> {
let Some((engine, port, url)) = stale_endpoint(dir) else {
return Ok(false);
};
let Ok(cdp) = Cdp::connect(&url, Duration::from_secs(2)) else {
return Ok(false);
};
let marionette = (engine == Engine::Firefox)
.then(|| bidi::marionette_port(dir))
.flatten();
match marionette {
Some(m) => {
if let Err(e) = bidi::marionette_quit(m, Duration::from_secs(2)) {
debug!("browser: marionette: {e}");
}
}
None => ask_to_quit(&cdp, engine),
}
drop(cdp);
let deadline = Instant::now() + QUIT_TIMEOUT;
let addr = (Ipv4Addr::LOCALHOST, port).into();
while TcpStream::connect_timeout(&addr, Duration::from_millis(200)).is_ok() {
if Instant::now() >= deadline {
return Err(Error::timeout("does not quit"));
}
std::thread::sleep(Duration::from_millis(100));
}
std::thread::sleep(Duration::from_millis(300));
Ok(true)
}
fn ask_to_quit(rpc: &Cdp, engine: Engine) {
let wait = Duration::from_secs(2);
match engine {
Engine::Chromium => {
let _ = rpc.call_timeout(None, "Browser.close", Value::object(), wait);
}
Engine::Firefox => {
if rpc
.call_timeout(None, "browser.close", Value::object(), wait)
.is_err()
&& bidi::new_session(rpc).is_ok()
{
let _ = rpc.call_timeout(None, "browser.close", Value::object(), wait);
}
}
}
}
#[derive(Debug, Clone)]
pub struct LaunchOptions {
pub bin: PathBuf,
pub viewport: (u32, u32),
pub visible: bool,
pub screen: Option<ScreenLaunch>,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ScreenLaunch {
pub switches: Vec<String>,
pub background: bool,
}
impl LaunchOptions {
pub fn engine(&self) -> Engine {
Engine::of(&self.bin)
}
}
pub const FIREFOX_PREFS: &[(&str, &str)] = &[
("marionette.port", "0"),
("media.navigator.permission.disabled", "true"),
("media.getusermedia.browser.enabled", "true"),
("media.getusermedia.audio.capture.enabled", "true"),
("media.autoplay.default", "0"),
("media.autoplay.blocking_policy", "0"),
("media.volume_scale", "\"0.0\""),
("network.lna.enabled", "false"),
("network.lna.blocking", "false"),
("dom.disable_beforeunload", "true"),
("browser.newtab.preload", "false"),
("browser.sessionstore.resume_from_crash", "false"),
("toolkit.startup.max_resumed_crashes", "-1"),
("dom.timeout.enable_budget_timer_throttling", "false"),
("widget.windows.window_occlusion_tracking.enabled", "false"),
];
pub fn firefox_user_js() -> String {
let mut s = String::from("// Written by tvpipe for 'tvpipe share URL'.\n");
for (k, v) in FIREFOX_PREFS {
s.push_str(&format!("user_pref(\"{k}\", {v});\n"));
}
s
}
pub fn args(opts: &LaunchOptions, dir: &Path) -> Vec<OsString> {
if opts.engine() == Engine::Firefox {
return firefox_args(opts, dir);
}
if let Some(screen) = &opts.screen {
return screen_args(opts.viewport, screen, dir);
}
let (w, h) = opts.viewport;
let mut a: Vec<OsString> = Vec::new();
if !opts.visible {
a.push("--headless=new".into());
a.push(format!("--screen-info={{0,0 {w}x{h}}}").into());
}
let mut dir_arg = OsString::from("--user-data-dir=");
dir_arg.push(dir);
a.push(dir_arg);
for s in [
"--remote-debugging-port=0",
"--no-first-run",
"--no-default-browser-check",
"--use-mock-keychain",
"--password-store=basic",
"--auto-accept-this-tab-capture",
"--autoplay-policy=no-user-gesture-required",
"--disable-background-timer-throttling",
"--disable-renderer-backgrounding",
"--disable-backgrounding-occluded-windows",
"--force-device-scale-factor=1",
] {
a.push(s.into());
}
a.push(format!("--window-size={w},{h}").into());
a.push("about:blank".into());
a
}
fn firefox_args(opts: &LaunchOptions, dir: &Path) -> Vec<OsString> {
let (w, h) = opts.viewport;
let mut a: Vec<OsString> = Vec::new();
if !opts.visible {
a.push("--headless".into());
}
a.push("--profile".into());
a.push(dir.into());
for s in [
"--no-remote",
"--new-instance",
"--remote-debugging-port=0",
"--marionette",
] {
a.push(s.into());
}
a.push(format!("--window-size={w},{h}").into());
a.push("about:blank".into());
a
}
fn screen_args(window: (u32, u32), screen: &ScreenLaunch, dir: &Path) -> Vec<OsString> {
let mut dir_arg = OsString::from("--user-data-dir=");
dir_arg.push(dir);
let mut a: Vec<OsString> = vec![dir_arg];
for s in [
"--remote-debugging-port=0",
"--no-first-run",
"--no-default-browser-check",
"--use-mock-keychain",
"--password-store=basic",
"--disable-background-timer-throttling",
"--disable-renderer-backgrounding",
"--disable-backgrounding-occluded-windows",
"--system-audio-capture-default_checked",
"--enable-features=PulseaudioLoopbackForScreenShare",
"--lang=en-US",
] {
a.push(s.into());
}
a.extend(screen.switches.iter().map(OsString::from));
a.push(format!("--window-size={},{}", window.0, window.1).into());
a.push("about:blank".into());
a
}
pub struct Browser {
child: Option<Child>,
detached: Option<Detached>,
cdp: Cdp,
pub engine: Engine,
pub product: String,
pub user_agent: String,
stderr: Arc<Mutex<VecDeque<String>>>,
}
struct Detached {
pid: Option<u32>,
port: u16,
stderr: PathBuf,
}
impl Browser {
pub fn launch(opts: &LaunchOptions, profile: &Profile) -> Result<Browser> {
let engine = opts.engine();
let port_file = profile.dir().join(match engine {
Engine::Chromium => "DevToolsActivePort",
Engine::Firefox => bidi::SERVER_FILE,
});
let _ = std::fs::remove_file(&port_file);
if engine == Engine::Firefox {
let _ = std::fs::remove_file(profile.dir().join(bidi::MARIONETTE_FILE));
let user_js = profile.dir().join("user.js");
std::fs::write(&user_js, firefox_user_js())
.map_err(|e| Error::io(format!("write {}", user_js.display()), e))?;
}
debug!(
"browser: starting {} with profile {}",
opts.bin.display(),
profile.dir().display()
);
let argv = args(opts, profile.dir());
let stderr_file = profile.dir().join("tvpipe-stderr.log");
if let Some(mut open) = opts
.screen
.as_ref()
.and_then(|s| platform::own_app_command(&opts.bin, &argv, s.background, &stderr_file))
{
return Browser::launch_detached(&mut open, &port_file, stderr_file);
}
let mut child = Command::new(&opts.bin)
.args(argv)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.spawn()
.map_err(|e| Error::io(format!("start {}", opts.bin.display()), e))?;
let stderr: Arc<Mutex<VecDeque<String>>> = Arc::default();
if let Some(err) = child.stderr.take() {
let tail = stderr.clone();
let _ = std::thread::Builder::new()
.name("browser-stderr".into())
.spawn(move || {
for line in BufReader::new(err).lines() {
let Ok(line) = line else { return };
trace!("browser stderr: {line}");
let mut t = tail.lock().unwrap_or_else(|e| e.into_inner());
if t.len() >= STDERR_TAIL {
t.pop_front();
}
t.push_back(line);
}
});
}
let tail = |stderr: &Arc<Mutex<VecDeque<String>>>| {
let t = stderr.lock().unwrap_or_else(|e| e.into_inner());
t.iter()
.rev()
.find(|l| !l.trim().is_empty())
.map(|l| format!(": {}", l.trim()))
.unwrap_or_default()
};
let deadline = Instant::now() + START_TIMEOUT;
let (port, url) = loop {
let found = match engine {
Engine::Chromium => std::fs::read_to_string(&port_file)
.ok()
.and_then(|s| parse_active_port(&s))
.map(|(port, path)| (port, format!("ws://127.0.0.1:{port}{path}"))),
Engine::Firefox => {
bidi::server_file(profile.dir()).map(|port| (port, bidi::session_url(port)))
}
};
if let Some(p) = found {
break p;
}
if let Ok(Some(status)) = child.try_wait() {
std::thread::sleep(Duration::from_millis(100));
return Err(Error::process(format!(
"the browser exited at once ({status}){}; is another browser \
using the same --profile?",
tail(&stderr)
)));
}
if Instant::now() >= deadline {
kill(&mut child);
return Err(Error::timeout(format!(
"the browser did not open its remote debugging port within {} s{}",
START_TIMEOUT.as_secs(),
tail(&stderr)
)));
}
std::thread::sleep(Duration::from_millis(50));
};
let cdp = match Cdp::connect(&url, Duration::from_secs(10)) {
Ok(c) => c,
Err(e) => {
kill(&mut child);
return Err(e);
}
};
let mut b = Browser {
child: Some(child),
detached: None,
cdp,
engine,
product: String::new(),
user_agent: String::new(),
stderr,
};
b.identify(port)?;
Ok(b)
}
fn identify(&mut self, port: u16) -> Result<()> {
match self.engine {
Engine::Chromium => {
let v = self.cdp.call(None, "Browser.getVersion", Value::object())?;
let s = |k: &str| v.get(k).and_then(Value::as_str).unwrap_or("").to_owned();
self.product = s("product");
self.user_agent = s("userAgent");
debug!("browser: {} on DevTools port {port}", self.product);
}
Engine::Firefox => {
let info = bidi::new_session(&self.cdp)?;
self.product = info.product;
self.user_agent = info.user_agent;
debug!("browser: {} on WebDriver BiDi port {port}", self.product);
}
}
Ok(())
}
fn launch_detached(open: &mut Command, port_file: &Path, stderr: PathBuf) -> Result<Browser> {
debug!("browser: starting it as an app of its own: {open:?}");
let status = open
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::piped())
.output()
.map_err(|e| Error::io("run open", e))?;
if !status.status.success() {
return Err(Error::process(format!(
"open could not start the browser ({}): {}",
status.status,
String::from_utf8_lossy(&status.stderr).trim()
)));
}
let deadline = Instant::now() + START_TIMEOUT;
let (port, path) = loop {
if let Some(p) = std::fs::read_to_string(port_file)
.ok()
.and_then(|s| parse_active_port(&s))
{
break p;
}
if Instant::now() >= deadline {
return Err(Error::timeout(format!(
"the browser did not open its DevTools port within {} s{}",
START_TIMEOUT.as_secs(),
last_line_of(&stderr)
.map(|l| format!(": {l}"))
.unwrap_or_default()
)));
}
std::thread::sleep(Duration::from_millis(50));
};
let url = format!("ws://127.0.0.1:{port}{path}");
let cdp = Cdp::connect(&url, Duration::from_secs(10))?;
let mut b = Browser {
child: None,
detached: Some(Detached {
pid: None,
port,
stderr,
}),
cdp,
engine: Engine::Chromium,
product: String::new(),
user_agent: String::new(),
stderr: Arc::default(),
};
let pid = b
.cdp
.call(None, "SystemInfo.getProcessInfo", Value::object())
.ok()
.and_then(|v| {
v.get("processInfo")?
.as_array()?
.iter()
.find(|p| p.get("type").and_then(Value::as_str) == Some("browser"))?
.get("id")?
.as_u64()
})
.and_then(|p| u32::try_from(p).ok());
if let Some(d) = &mut b.detached {
d.pid = pid;
}
b.identify(port)?;
Ok(b)
}
pub fn cdp(&self) -> &Cdp {
&self.cdp
}
pub fn exited(&mut self) -> Option<std::process::ExitStatus> {
self.child
.as_mut()
.and_then(|c| c.try_wait().ok().flatten())
}
pub fn last_error_line(&self) -> Option<String> {
if let Some(d) = &self.detached {
return last_line_of(&d.stderr);
}
let t = self.stderr.lock().unwrap_or_else(|e| e.into_inner());
t.iter().rev().find(|l| !l.trim().is_empty()).cloned()
}
pub fn is_detached(&self) -> bool {
self.detached.is_some()
}
pub fn close(&mut self) {
if let Some(d) = self.detached.take() {
close_detached(&self.cdp, &d);
return;
}
let Some(mut child) = self.child.take() else {
return;
};
if matches!(child.try_wait(), Ok(None)) {
ask_to_quit(&self.cdp, self.engine);
}
let deadline = Instant::now() + QUIT_TIMEOUT;
loop {
match child.try_wait() {
Ok(Some(status)) => {
debug!("browser: exited ({status})");
return;
}
Ok(None) if Instant::now() < deadline => {
std::thread::sleep(Duration::from_millis(50))
}
_ => {
warn!("browser: did not quit; killing it");
kill(&mut child);
return;
}
}
}
}
}
impl Drop for Browser {
fn drop(&mut self) {
self.close();
}
}
fn kill(child: &mut Child) {
let _ = child.kill();
let _ = child.wait();
}
fn close_detached(cdp: &Cdp, d: &Detached) {
let addr = (Ipv4Addr::LOCALHOST, d.port).into();
let running = || TcpStream::connect_timeout(&addr, Duration::from_millis(200)).is_ok();
if !running() {
return;
}
let _ = cdp.call_timeout(
None,
"Browser.close",
Value::object(),
Duration::from_secs(2),
);
let deadline = Instant::now() + QUIT_TIMEOUT;
while running() {
if Instant::now() >= deadline {
warn!("browser: did not quit; killing it");
if let Some(pid) = d.pid {
platform::kill_pid(pid);
}
return;
}
std::thread::sleep(Duration::from_millis(100));
}
debug!("browser: quit");
std::thread::sleep(Duration::from_millis(300));
}
fn last_line_of(path: &Path) -> Option<String> {
let s = std::fs::read_to_string(path).ok()?;
s.lines()
.rev()
.find(|l| !l.trim().is_empty())
.map(|l| l.trim().to_owned())
}
fn parse_active_port(s: &str) -> Option<(u16, String)> {
let mut lines = s.lines();
let port: u16 = lines.next()?.trim().parse().ok().filter(|&p| p > 0)?;
let path = lines.next()?.trim();
path.starts_with("/devtools/browser/")
.then(|| (port, path.to_owned()))
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
fn env_of(vars: &[(&str, &str)]) -> impl Fn(&str) -> Option<OsString> {
let m: HashMap<String, OsString> = vars
.iter()
.map(|(k, v)| (k.to_string(), OsString::from(v)))
.collect();
move |k| m.get(k).cloned()
}
fn files(list: &[&str]) -> impl Fn(&Path) -> bool {
let set: Vec<PathBuf> = list.iter().map(PathBuf::from).collect();
move |p| set.iter().any(|f| f == p)
}
fn path_var(dirs: &[&str]) -> String {
std::env::join_paths(dirs).unwrap().into_string().unwrap()
}
#[test]
fn explicit_and_env_come_first() {
let path = path_var(&["/usr/bin"]);
let env = env_of(&[("PATH", &path), (BROWSER_ENV, "/opt/env/chrome")]);
let exists = files(&["/opt/mine/chrome", "/opt/env/chrome", "/usr/bin/chromium"]);
let mine = Path::new("/opt/mine/chrome");
assert_eq!(find_with(Some(mine), &env, &exists).unwrap(), mine);
assert_eq!(
find_with(None, &env, &exists).unwrap(),
Path::new("/opt/env/chrome")
);
let e = find_with(Some(Path::new("/nope/chrome")), &env, &exists).unwrap_err();
assert!(e.to_string().contains("--browser /nope/chrome"), "{e}");
let env2 = env_of(&[("PATH", &path), (BROWSER_ENV, "/nope")]);
assert!(find_with(None, &env2, &exists)
.unwrap_err()
.to_string()
.contains(BROWSER_ENV));
assert_eq!(
find_with(Some(Path::new("chromium")), &env, &exists).unwrap(),
Path::new("/usr/bin/chromium")
);
}
#[test]
fn engines() {
for (p, e) in [
(
"/Applications/Firefox.app/Contents/MacOS/firefox",
Engine::Firefox,
),
("/usr/lib/firefox-esr/firefox-esr", Engine::Firefox),
(
r"C:\Program Files\Mozilla Firefox\firefox.exe",
Engine::Firefox,
),
("FIREFOX", Engine::Firefox),
("/opt/google/chrome/chrome", Engine::Chromium),
(
"/Applications/Brave Browser.app/Contents/MacOS/Brave Browser",
Engine::Chromium,
),
("msedge.exe", Engine::Chromium),
] {
assert_eq!(Engine::of(Path::new(p)), e, "{p}");
}
}
#[test]
fn firefox_is_found() {
let home = if cfg!(windows) {
r"C:\Users\me"
} else {
"/home/me"
};
let vars = [
("HOME", home),
("ProgramFiles", r"C:\Program Files"),
("LOCALAPPDATA", r"C:\Users\me\AppData\Local"),
];
let installed = platform::firefox_install_paths(&env_of(&vars));
assert!(!installed.is_empty());
let ff = installed[0].to_string_lossy().into_owned();
let path = path_var(&["/usr/bin"]);
let mut with_path = vars.to_vec();
with_path.push(("PATH", &path));
let env = env_of(&with_path);
assert_eq!(find_with(None, &env, &files(&[&ff])).unwrap(), installed[0]);
let chrome = platform::browser_install_paths(&env)[0].clone();
let both = files(&[&ff, &chrome.to_string_lossy()]);
assert_eq!(find_with(None, &env, &both).unwrap(), chrome);
let name = platform::firefox_names()[0];
let on_path = Path::new("/usr/bin").join(name);
let on_path = on_path.to_string_lossy().into_owned();
assert_eq!(
find_with(Some(Path::new("firefox")), &env, &both).unwrap(),
installed[0]
);
assert_eq!(
find_with(
Some(Path::new("Firefox")),
&env,
&files(&[&ff, &on_path, &chrome.to_string_lossy()])
)
.unwrap(),
installed[0]
);
if !cfg!(windows) {
assert_eq!(
find_with(Some(Path::new("firefox")), &env, &files(&[&on_path])).unwrap(),
Path::new(&on_path)
);
}
let e = find_with(Some(Path::new("firefox")), &env, &files(&[])).unwrap_err();
assert!(e.to_string().contains("--browser firefox"), "{e}");
let e = find_with(None, &env, &files(&[])).unwrap_err();
assert!(e.to_string().contains("Firefox"), "{e}");
}
#[test]
fn app_bundles() {
let exists = files(&["/Apps/Brave Browser.app/Contents/MacOS/Brave Browser"]);
let env = env_of(&[]);
assert_eq!(
find_with(Some(Path::new("/Apps/Brave Browser.app")), &env, &exists).unwrap(),
Path::new("/Apps/Brave Browser.app/Contents/MacOS/Brave Browser")
);
let exists = files(&["/Apps/Firefox Nightly.app/Contents/MacOS/firefox"]);
let found = find_with(Some(Path::new("/Apps/Firefox Nightly.app")), &env, &exists).unwrap();
assert_eq!(
found,
Path::new("/Apps/Firefox Nightly.app/Contents/MacOS/firefox")
);
assert_eq!(Engine::of(&found), Engine::Firefox);
}
#[test]
fn install_paths_then_path() {
let home = if cfg!(windows) {
r"C:\Users\me"
} else {
"/home/me"
};
let env = env_of(&[
("HOME", home),
("ProgramFiles", r"C:\Program Files"),
("LOCALAPPDATA", r"C:\Users\me\AppData\Local"),
]);
let installed = platform::browser_install_paths(&env);
assert!(!installed.is_empty());
let second = installed[1].to_string_lossy().into_owned();
let last = installed.last().unwrap().to_string_lossy().into_owned();
let path = path_var(&["/usr/bin"]);
let name = platform::browser_names()[0];
let on_path = Path::new("/usr/bin").join(name);
let on_path = on_path.to_string_lossy().into_owned();
let env = env_of(&[
("HOME", home),
("ProgramFiles", r"C:\Program Files"),
("LOCALAPPDATA", r"C:\Users\me\AppData\Local"),
("PATH", &path),
]);
let exists = files(&[&last, &second, &on_path]);
assert_eq!(find_with(None, &env, &exists).unwrap(), installed[1]);
let exists = files(&[&on_path]);
assert_eq!(find_with(None, &env, &exists).unwrap(), Path::new(&on_path));
let e = find_with(None, &env, &files(&[])).unwrap_err();
assert!(e.to_string().contains("--browser PATH"), "{e}");
}
#[test]
fn known_install_paths() {
let env = env_of(&[
("HOME", "/Users/me"),
("ProgramFiles", r"C:\Program Files"),
("ProgramFiles(x86)", r"C:\Program Files (x86)"),
("LOCALAPPDATA", r"C:\Users\me\AppData\Local"),
]);
let all: Vec<String> = platform::browser_install_paths(&env)
.iter()
.map(|p| p.to_string_lossy().into_owned())
.collect();
let firefox: Vec<String> = platform::firefox_install_paths(&env)
.iter()
.map(|p| p.to_string_lossy().into_owned())
.collect();
if cfg!(target_os = "macos") {
assert_eq!(
firefox[0],
"/Applications/Firefox.app/Contents/MacOS/firefox"
);
assert!(firefox.contains(
&"/Users/me/Applications/Firefox Nightly.app/Contents/MacOS/firefox".into()
));
} else if cfg!(target_os = "windows") {
assert_eq!(firefox[0], r"C:\Program Files\Mozilla Firefox\firefox.exe");
assert!(
firefox.contains(&r"C:\Users\me\AppData\Local\Mozilla Firefox\firefox.exe".into())
);
} else {
assert_eq!(firefox[0], "/usr/lib/firefox/firefox");
}
if cfg!(target_os = "macos") {
assert_eq!(
all[0],
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
);
assert!(all.contains(
&"/Users/me/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge".into()
));
} else if cfg!(target_os = "windows") {
assert_eq!(
all[0],
r"C:\Program Files\Google\Chrome\Application\chrome.exe"
);
assert!(all
.iter()
.any(|p| p.ends_with(r"Edge\Application\msedge.exe")));
} else {
assert_eq!(all[0], "/opt/google/chrome/chrome");
}
}
#[test]
fn real_profiles_are_refused() {
let real = vec![PathBuf::from("/home/me/.config/google-chrome")];
assert!(check_not_real(Path::new("/home/me/.config/google-chrome"), &real).is_err());
assert!(
check_not_real(Path::new("/home/me/.config/google-chrome/Default"), &real).is_err()
);
assert!(check_not_real(Path::new("/home/me/.config"), &real).is_err());
assert!(check_not_real(Path::new("/home/me/tvpipe-profile"), &real).is_ok());
let env = env_of(&[
("HOME", "/Users/me"),
("LOCALAPPDATA", r"C:\L"),
("APPDATA", r"C:\R"),
]);
let dirs = platform::default_profile_dirs(&env);
assert!(!dirs.is_empty());
let firefox = if cfg!(target_os = "macos") {
"/Users/me/Library/Application Support/Firefox"
} else if cfg!(target_os = "windows") {
r"C:\R\Mozilla\Firefox"
} else {
"/Users/me/.mozilla/firefox"
};
assert!(dirs.contains(&PathBuf::from(firefox)), "{dirs:?}");
}
#[test]
fn temporary_profiles_are_deleted() {
let p = Profile::temporary().unwrap();
let dir = p.dir().to_path_buf();
assert!(dir.is_dir());
assert!(dir
.file_name()
.unwrap()
.to_string_lossy()
.starts_with("tvpipe-browser-"));
std::fs::write(dir.join("Cookies"), b"x").unwrap();
std::fs::create_dir(dir.join("Default")).unwrap();
drop(p);
assert!(!dir.exists());
let keep = std::env::temp_dir().join(format!("tvpipe-keep-{}", std::process::id()));
let p = Profile::persistent(&keep).unwrap();
drop(p);
assert!(keep.is_dir());
std::fs::remove_dir_all(&keep).unwrap();
}
#[test]
fn stale_profiles_are_swept() {
let root = std::env::temp_dir().join(format!("tvpipe-sweep-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir(&root).unwrap();
let mk = |name: &str, owner: Option<String>| {
let d = root.join(name);
std::fs::create_dir(&d).unwrap();
if let Some(o) = owner {
std::fs::write(d.join(OWNER_FILE), o).unwrap();
}
d
};
let live = Profile::temporary_in(&root).unwrap();
let owner = std::fs::read_to_string(live.dir().join(OWNER_FILE)).unwrap();
assert!(
owner.starts_with(&format!("{} ", std::process::id())),
"{owner}"
);
let mut dead = Profile::temporary_in(&root).unwrap();
dead.beacon = None;
dead.temporary = false;
let dead_dir = dead.dir().to_path_buf();
drop(dead);
let closed = TcpListener::bind("127.0.0.1:0").unwrap();
let gone_port = closed.local_addr().unwrap().port();
drop(closed);
std::fs::write(
dead_dir.join("DevToolsActivePort"),
format!("{gone_port}\n/devtools/browser/x\n"),
)
.unwrap();
let other = TcpListener::bind("127.0.0.1:0").unwrap();
let port = other.local_addr().unwrap().port();
let squatted = mk(
&format!("{TEMP_PREFIX}squatted"),
Some(format!("1 {port} 00ff")),
);
let garbage = mk(&format!("{TEMP_PREFIX}garbage"), Some("x".into()));
let fresh = mk(&format!("{TEMP_PREFIX}fresh"), None);
let unrelated = mk("tvpipe-session-1", Some("1 1 1".into()));
assert_eq!(sweep_in(&root, ORPHAN_AGE), 3);
for d in [&dead_dir, &squatted, &garbage] {
assert!(!d.exists(), "{} was kept", d.display());
}
for d in [live.dir(), &fresh, &unrelated] {
assert!(d.exists(), "{} was removed", d.display());
}
assert_eq!(sweep_in(&root, Duration::ZERO), 1);
assert!(!fresh.exists() && live.dir().exists());
drop(other);
let live_dir = live.dir().to_path_buf();
drop(live);
assert!(!live_dir.exists());
std::fs::remove_dir_all(&root).unwrap();
}
#[test]
#[ignore]
fn chrome_left_by_a_killed_tvpipe_is_swept() {
let Ok(bin) = std::env::var("TVPIPE_TEST_CHROME") else {
eprintln!("TVPIPE_TEST_CHROME not set");
return;
};
left_by_a_killed_tvpipe_is_swept(&bin);
}
#[test]
#[ignore]
fn firefox_left_by_a_killed_tvpipe_is_swept() {
let Ok(bin) = std::env::var("TVPIPE_TEST_FIREFOX") else {
eprintln!("TVPIPE_TEST_FIREFOX not set");
return;
};
left_by_a_killed_tvpipe_is_swept(&bin);
}
fn left_by_a_killed_tvpipe_is_swept(bin: &str) {
let root = std::env::temp_dir().join(format!(
"tvpipe-sweep-{}-{}",
if Engine::of(Path::new(bin)) == Engine::Firefox {
"firefox"
} else {
"chrome"
},
std::process::id()
));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir(&root).unwrap();
let mut profile = Profile::temporary_in(&root).unwrap();
let opts = LaunchOptions {
bin: bin.into(),
viewport: (640, 360),
visible: false,
screen: None,
};
let mut browser = Browser::launch(&opts, &profile).unwrap();
let mut child = browser.child.take().unwrap();
drop(browser);
profile.beacon = None;
profile.temporary = false;
let dir = profile.dir().to_path_buf();
drop(profile);
assert!(
child.try_wait().unwrap().is_none(),
"the browser should still run"
);
let owner = std::fs::read(dir.join(OWNER_FILE)).unwrap();
std::fs::remove_file(dir.join(OWNER_FILE)).unwrap();
assert_eq!(sweep_in(&root, Duration::ZERO), 0);
assert!(child.try_wait().unwrap().is_none());
std::fs::write(dir.join(OWNER_FILE), owner).unwrap();
assert_eq!(sweep_in(&root, ORPHAN_AGE), 1);
assert!(!dir.exists());
let status = child.try_wait().unwrap();
assert!(status.is_some(), "the browser still runs");
if !cfg!(windows) {
let out = Command::new("ps")
.args(["-A", "-o", "command="])
.output()
.unwrap();
let needle = dir.to_string_lossy().into_owned();
let left: Vec<String> = String::from_utf8_lossy(&out.stdout)
.lines()
.filter(|l| l.contains(&needle))
.map(str::to_owned)
.collect();
assert!(left.is_empty(), "browser processes left: {left:#?}");
}
std::fs::remove_dir_all(&root).unwrap();
}
#[test]
fn command_line() {
let o = LaunchOptions {
bin: "chrome".into(),
viewport: (1280, 720),
visible: false,
screen: None,
};
let a: Vec<String> = args(&o, Path::new("/tmp/p"))
.into_iter()
.map(|s| s.into_string().unwrap())
.collect();
for want in [
"--headless=new",
"--screen-info={0,0 1280x720}",
"--user-data-dir=/tmp/p",
"--remote-debugging-port=0",
"--auto-accept-this-tab-capture",
"--autoplay-policy=no-user-gesture-required",
"--window-size=1280,720",
"--force-device-scale-factor=1",
] {
assert!(a.iter().any(|x| x == want), "{want} missing: {a:?}");
}
assert_eq!(a.last().unwrap(), "about:blank");
let visible: Vec<OsString> = args(
&LaunchOptions {
visible: true,
..o.clone()
},
Path::new("/tmp/p"),
);
assert!(!visible
.iter()
.any(|x| x.to_string_lossy().starts_with("--headless")
|| x.to_string_lossy().starts_with("--screen-info")));
let ff = LaunchOptions {
bin: "/opt/firefox/firefox".into(),
..o
};
assert_eq!(ff.engine(), Engine::Firefox);
let a: Vec<String> = args(&ff, Path::new("/tmp/my p"))
.into_iter()
.map(|s| s.into_string().unwrap())
.collect();
assert_eq!(
a,
[
"--headless",
"--profile",
"/tmp/my p",
"--no-remote",
"--new-instance",
"--remote-debugging-port=0",
"--marionette",
"--window-size=1280,720",
"about:blank"
]
);
}
#[test]
fn firefox_preferences() {
let js = firefox_user_js();
for want in [
"user_pref(\"media.navigator.permission.disabled\", true);",
"user_pref(\"media.getusermedia.browser.enabled\", true);",
"user_pref(\"media.getusermedia.audio.capture.enabled\", true);",
"user_pref(\"media.autoplay.default\", 0);",
"user_pref(\"media.volume_scale\", \"0.0\");",
] {
assert!(js.lines().any(|l| l == want), "{want} missing:\n{js}");
}
for l in js.lines().skip(1) {
let inner = l
.strip_prefix("user_pref(")
.and_then(|r| r.strip_suffix(");"))
.unwrap_or_else(|| panic!("{l}"));
let (k, v) = inner.split_once(", ").unwrap();
assert!(Value::parse(k).unwrap().as_str().is_some(), "{l}");
Value::parse(v).unwrap_or_else(|e| panic!("{l}: {e}"));
}
}
#[test]
fn active_port_file() {
assert_eq!(
parse_active_port("53211\n/devtools/browser/0b5f-11\n"),
Some((53211, "/devtools/browser/0b5f-11".into()))
);
assert_eq!(parse_active_port("53211\n"), None);
assert_eq!(parse_active_port("0\n/devtools/browser/x"), None);
assert_eq!(parse_active_port("x\n/devtools/browser/x"), None);
assert_eq!(parse_active_port("1\n/etc/passwd"), None);
assert_eq!(parse_active_port(""), None);
}
}