use super::config::BrowserConfig;
use rho_browser::diagnose::Problem;
use rho_browser::install::Install;
use rho_browser::manager::{Executable, ManagerError, ManagerOptions};
use rho_browser::Sandbox;
use std::path::Path;
use std::time::Duration;
pub(crate) const UNLOAD_IDLE_AFTER: Duration = Duration::from_secs(5 * 60);
const DOWNLOAD_STALL: Duration = Duration::from_secs(60);
pub(crate) fn download_client() -> reqwest::Result<reqwest::Client> {
crate::reqwest_client_builder()
.connect_timeout(DOWNLOAD_STALL)
.read_timeout(DOWNLOAD_STALL)
.build()
}
pub(crate) fn executable(
config: &BrowserConfig,
rho_home: &Path,
client: reqwest::Client,
) -> Executable {
match &config.executable {
Some(path) => Executable::Path(path.clone()),
None => Executable::Pinned {
install: Install::new(rho_home.join("browser")),
client,
},
}
}
pub(crate) fn manager_options(
config: &BrowserConfig,
runtime_dir: &Path,
executable: Executable,
) -> Result<ManagerOptions, String> {
let sandbox = match config.sandbox {
Some(false) => Sandbox::Disabled,
Some(true) | None => Sandbox::Enabled,
};
Ok(ManagerOptions {
executable,
profiles_dir: runtime_dir.join("browser"),
sandbox,
unload_idle_after: config.unload_idle_after.unwrap_or(UNLOAD_IDLE_AFTER),
navigation: super::navigation::check(),
connect: super::connect::connect()?,
})
}
pub(crate) fn failure_text(error: &ManagerError) -> String {
let mut text = error.to_string();
match error {
ManagerError::UnsupportedPlatform => {
text.push_str("; set `executable` under `[browser]` in config.toml to a Chrome");
}
ManagerError::Launch {
executable,
problems,
..
} => {
for problem in problems {
let fix = match problem {
Problem::UserNamespacesRestricted => format!(
"allow user namespaces for {} with an AppArmor profile \
({DOCS}#browser), or set `sandbox = false` under `[browser]` \
in config.toml to run it without its sandbox",
executable.display()
),
Problem::MissingLibraries(_) => {
let deb_deps = executable.with_file_name("deb.deps");
if deb_deps.is_file() {
format!(
"install them; on Debian and Ubuntu, {} lists the packages",
deb_deps.display()
)
} else {
"install them".to_owned()
}
}
};
text.push_str(&format!("\n{problem}\nfix: {fix}"));
}
}
ManagerError::Install(_)
| ManagerError::Proxy(_)
| ManagerError::Profile { .. }
| ManagerError::Context(_)
| ManagerError::Guard(_)
| ManagerError::Hung { .. } => {}
}
text
}
const DOCS: &str = "https://matthewyjiang.github.io/rho/configuration";