use log::info;
use crate::i18n::{tr_log, tr_log_args};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::RwLock;
#[derive(Debug, Clone, PartialEq)]
pub enum DownloadStatus {
NotDownloaded,
Downloading,
Downloaded,
Failed(String),
}
#[derive(Error, Debug)]
pub enum BrowserDownloadError {
#[error("Download failed: {0}")]
DownloadFailed(String),
#[error("Browser directory creation failed: {0}")]
DirectoryCreationFailed(String),
#[error("Browser executable not found: {0}")]
ExecutableNotFound(PathBuf),
#[error("Permission denied: {0}")]
PermissionDenied(String),
}
#[derive(Clone, Debug)]
pub struct BrowserDownloadConfig {
pub download_dir: PathBuf,
pub timeout_seconds: u64,
pub auto_download: bool,
}
impl Default for BrowserDownloadConfig {
fn default() -> Self {
Self {
download_dir: get_default_browser_path(),
timeout_seconds: 300, auto_download: true,
}
}
}
fn get_default_browser_path() -> PathBuf {
let mut path = dirs::home_dir().unwrap_or_else(|| PathBuf::from("."));
path.push(".cache");
path.push("crawlrs");
path.push("chromium");
path
}
pub fn get_browser_executable_path(cache_dir: &Path) -> PathBuf {
let mut path = cache_dir.to_path_buf();
#[cfg(target_os = "windows")]
{
path.push("chrome-win");
path.push("chrome.exe");
}
#[cfg(target_os = "macos")]
{
path.push("chrome-mac");
path.push("Chromium.app");
path.push("Contents");
path.push("MacOS");
path.push("Chromium");
}
#[cfg(target_os = "linux")]
{
path.push("chrome-linux");
path.push("chrome");
}
path
}
#[derive(Clone, Debug)]
pub struct BrowserDownloadManager {
config: Arc<BrowserDownloadConfig>,
status: Arc<RwLock<DownloadStatus>>,
}
impl BrowserDownloadManager {
pub fn new(config: BrowserDownloadConfig) -> Self {
Self {
config: Arc::new(config),
status: Arc::new(RwLock::new(DownloadStatus::NotDownloaded)),
}
}
pub fn with_defaults() -> Self {
Self::new(BrowserDownloadConfig::default())
}
pub async fn get_status(&self) -> DownloadStatus {
let status = self.status.read().await;
status.clone()
}
pub async fn is_browser_downloaded(&self) -> bool {
let executable_path = get_browser_executable_path(&self.config.download_dir);
executable_path.exists() && executable_path.is_file()
}
pub async fn download_browser(&self) -> Result<PathBuf, BrowserDownloadError> {
info!("{}", tr_log("browser-check-started"));
{
let mut status = self.status.write().await;
*status = DownloadStatus::Downloading;
}
if let Some(path) = find_system_browser().await {
let mut status = self.status.write().await;
*status = DownloadStatus::Downloaded;
return Ok(path);
}
if self.is_browser_downloaded().await {
let path = get_browser_executable_path(&self.config.download_dir);
let mut status = self.status.write().await;
*status = DownloadStatus::Downloaded;
return Ok(path);
}
if let Err(e) = tokio::fs::create_dir_all(&self.config.download_dir).await {
let err_msg = format!("Failed to create download directory: {}", e);
let mut status = self.status.write().await;
*status = DownloadStatus::Failed(err_msg.clone());
return Err(BrowserDownloadError::DirectoryCreationFailed(err_msg));
}
if let Ok(path) = self.try_fetcher_download().await {
let mut status = self.status.write().await;
*status = DownloadStatus::Downloaded;
return Ok(path);
}
let err_msg = String::from(
"Chrome/Chromium browser not found. Install one of the following and retry:\n\
- Google Chrome (https: // www.google.com/chrome)\n\
- Chromium (https: // www.chromium.org/getting-involved/download-chromium)\n\
or make sure the browser is available on PATH.",
);
let mut status = self.status.write().await;
*status = DownloadStatus::Failed(err_msg.clone());
Err(BrowserDownloadError::DownloadFailed(err_msg))
}
async fn try_fetcher_download(&self) -> Result<PathBuf, BrowserDownloadError> {
#[cfg(feature = "engine-playwright")]
{
match self.do_fetcher_download().await {
Ok(path) => return Ok(path),
Err(e) => {
log::warn!(
"{}",
tr_log_args(
"browser-fetcher-download-failed",
&[("error", e.to_string())],
)
);
}
}
}
Err(BrowserDownloadError::DownloadFailed(
"automatic browser download is unavailable in this build (chromiumoxide_fetcher was removed for RUSTSEC advisories); install Chrome/Chromium or set CRAWLRS__ENGINES__PLAYWRIGHT__BROWSER_PATH"
.to_string(),
))
}
#[cfg(feature = "engine-playwright")]
async fn do_fetcher_download(&self) -> Result<PathBuf, BrowserDownloadError> {
Err(BrowserDownloadError::DownloadFailed(
"Browser download feature requires full implementation".to_string(),
))
}
pub async fn get_or_download_browser(&self) -> Result<PathBuf, BrowserDownloadError> {
self.download_browser().await
}
pub async fn cleanup(&self) -> Result<(), BrowserDownloadError> {
if self.config.download_dir.exists() {
tokio::fs::remove_dir_all(&self.config.download_dir)
.await
.map_err(|e| {
BrowserDownloadError::DownloadFailed(format!("Cleanup failed: {}", e))
})?;
let mut status = self.status.write().await;
*status = DownloadStatus::NotDownloaded;
info!("{}", tr_log("browser-cleaned-up"));
}
Ok(())
}
pub fn get_cache_dir(&self) -> &PathBuf {
&self.config.download_dir
}
}
pub const BROWSER_PATH_ENV: &str = "CRAWLRS__ENGINES__PLAYWRIGHT__BROWSER_PATH";
pub fn browser_path_from_env() -> Option<PathBuf> {
let raw = std::env::var(BROWSER_PATH_ENV).ok()?;
let trimmed = raw.trim();
if trimmed.is_empty() {
return None;
}
let path = PathBuf::from(trimmed);
if path.is_file() {
log::info!("{BROWSER_PATH_ENV} is set and points to an existing file");
Some(path)
} else {
log::warn!("{BROWSER_PATH_ENV} is set but the path does not exist; ignoring");
None
}
}
pub async fn find_system_browser() -> Option<PathBuf> {
let common_paths = [
PathBuf::from("/usr/bin/google-chrome"),
PathBuf::from("/usr/bin/chromium"),
PathBuf::from("/usr/bin/chromium-browser"),
PathBuf::from("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"),
PathBuf::from("/Applications/Chromium.app/Contents/MacOS/Chromium"),
PathBuf::from(r"C:\Program Files\Google\Chrome\Application\chrome.exe"),
PathBuf::from(r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"),
];
for path in &common_paths {
if path.is_file() {
log::info!(
"{}",
tr_log_args("browser-system-found", &[("path", format!("{:?}", path))],)
);
return Some(path.clone());
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn test_download_status_not_downloaded() {
let status = DownloadStatus::NotDownloaded;
assert_eq!(status, DownloadStatus::NotDownloaded);
assert_ne!(status, DownloadStatus::Downloading);
}
#[test]
fn test_download_status_downloading() {
let status = DownloadStatus::Downloading;
assert_eq!(status, DownloadStatus::Downloading);
assert_ne!(status, DownloadStatus::Downloaded);
}
#[test]
fn test_download_status_downloaded() {
let status = DownloadStatus::Downloaded;
assert_eq!(status, DownloadStatus::Downloaded);
assert_ne!(status, DownloadStatus::NotDownloaded);
}
#[test]
fn test_download_status_failed_with_message() {
let status = DownloadStatus::Failed("network error".to_string());
match status {
DownloadStatus::Failed(msg) => assert_eq!(msg, "network error"),
other => panic!("Expected Failed, got {:?}", other),
}
}
#[test]
fn test_download_status_clone() {
let status = DownloadStatus::Failed("error".to_string());
let cloned = status.clone();
assert_eq!(status, cloned);
}
#[test]
fn test_download_status_debug() {
let status = DownloadStatus::Downloaded;
let debug_str = format!("{:?}", status);
assert_eq!(debug_str, "Downloaded");
}
#[test]
fn test_browser_download_error_download_failed() {
let err = BrowserDownloadError::DownloadFailed("timeout".to_string());
assert_eq!(err.to_string(), "Download failed: timeout");
}
#[test]
fn test_browser_download_error_directory_creation_failed() {
let err = BrowserDownloadError::DirectoryCreationFailed("permission denied".to_string());
assert_eq!(
err.to_string(),
"Browser directory creation failed: permission denied"
);
}
#[test]
fn test_browser_download_error_executable_not_found() {
let path = PathBuf::from("/usr/bin/chrome");
let err = BrowserDownloadError::ExecutableNotFound(path.clone());
assert_eq!(
err.to_string(),
format!("Browser executable not found: {}", path.display())
);
}
#[test]
fn test_browser_download_error_permission_denied() {
let err = BrowserDownloadError::PermissionDenied("/root/.cache".to_string());
assert_eq!(err.to_string(), "Permission denied: /root/.cache");
}
#[test]
fn test_browser_download_config_default() {
let config = BrowserDownloadConfig::default();
assert_eq!(config.timeout_seconds, 300);
assert!(config.auto_download);
assert!(config.download_dir.ends_with("chromium"));
}
#[test]
fn test_browser_download_config_custom() {
let config = BrowserDownloadConfig {
download_dir: PathBuf::from("/tmp/custom-browser"),
timeout_seconds: 600,
auto_download: false,
};
assert_eq!(config.download_dir, PathBuf::from("/tmp/custom-browser"));
assert_eq!(config.timeout_seconds, 600);
assert!(!config.auto_download);
}
#[test]
fn test_browser_download_config_clone() {
let config = BrowserDownloadConfig {
download_dir: PathBuf::from("/tmp/test"),
timeout_seconds: 100,
auto_download: true,
};
let cloned = config.clone();
assert_eq!(config.download_dir, cloned.download_dir);
assert_eq!(config.timeout_seconds, cloned.timeout_seconds);
assert_eq!(config.auto_download, cloned.auto_download);
}
#[test]
fn test_get_browser_executable_path_returns_path_under_cache() {
let cache_dir = PathBuf::from("/tmp/some-cache");
let exe_path = get_browser_executable_path(&cache_dir);
assert!(exe_path.starts_with(&cache_dir));
#[cfg(target_os = "linux")]
{
assert!(exe_path.ends_with("chrome"));
assert!(exe_path.to_string_lossy().contains("chrome-linux"));
}
#[cfg(target_os = "windows")]
{
assert!(exe_path.ends_with("chrome.exe"));
assert!(exe_path.to_string_lossy().contains("chrome-win"));
}
#[cfg(target_os = "macos")]
{
assert!(exe_path.ends_with("Chromium"));
assert!(exe_path.to_string_lossy().contains("chrome-mac"));
}
}
#[test]
fn test_get_browser_executable_path_with_empty_dir() {
let cache_dir = PathBuf::new();
let exe_path = get_browser_executable_path(&cache_dir);
#[cfg(target_os = "linux")]
assert!(exe_path.ends_with("chrome"));
#[cfg(not(target_os = "linux"))]
let _ = &exe_path;
}
#[test]
fn test_browser_download_manager_new() {
let config = BrowserDownloadConfig {
download_dir: PathBuf::from("/tmp/test-browser"),
timeout_seconds: 60,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
assert_eq!(manager.get_cache_dir(), &PathBuf::from("/tmp/test-browser"));
}
#[test]
fn test_browser_download_manager_with_defaults() {
let manager = BrowserDownloadManager::with_defaults();
let cache_dir = manager.get_cache_dir();
assert!(cache_dir.ends_with("chromium"));
}
#[tokio::test]
async fn test_get_status_initial_not_downloaded() {
let manager = BrowserDownloadManager::with_defaults();
let status = manager.get_status().await;
assert_eq!(status, DownloadStatus::NotDownloaded);
}
#[tokio::test]
async fn test_get_status_initial_after_custom_config() {
let config = BrowserDownloadConfig {
download_dir: PathBuf::from("/nonexistent/path/test"),
timeout_seconds: 60,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let status = manager.get_status().await;
assert_eq!(status, DownloadStatus::NotDownloaded);
}
#[tokio::test]
async fn test_is_browser_downloaded_nonexistent_path() {
let config = BrowserDownloadConfig {
download_dir: PathBuf::from("/nonexistent/path/that/does/not/exist"),
timeout_seconds: 60,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
assert!(!manager.is_browser_downloaded().await);
}
#[tokio::test]
async fn test_is_browser_downloaded_temp_empty_dir() {
let temp_dir = tempfile::tempdir().unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 60,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
assert!(!manager.is_browser_downloaded().await);
}
#[test]
fn test_get_cache_dir_custom() {
let custom_path = PathBuf::from("/custom/cache/dir");
let config = BrowserDownloadConfig {
download_dir: custom_path.clone(),
timeout_seconds: 60,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
assert_eq!(manager.get_cache_dir(), &custom_path);
}
#[tokio::test]
async fn test_cleanup_nonexistent_dir() {
let config = BrowserDownloadConfig {
download_dir: PathBuf::from("/nonexistent/path/for/cleanup/test"),
timeout_seconds: 60,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let result = manager.cleanup().await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_cleanup_existing_temp_dir() {
let temp_dir = tempfile::tempdir().unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 60,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let test_file = temp_dir.path().join("test_file.txt");
tokio::fs::write(&test_file, "test").await.unwrap();
assert!(test_file.exists());
let result = manager.cleanup().await;
assert!(result.is_ok());
assert!(!temp_dir.path().exists());
let status = manager.get_status().await;
assert_eq!(status, DownloadStatus::NotDownloaded);
}
#[tokio::test]
async fn test_cleanup_then_status_is_not_downloaded() {
let temp_dir = tempfile::tempdir().unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 60,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
manager.cleanup().await.unwrap();
let status = manager.get_status().await;
assert_eq!(status, DownloadStatus::NotDownloaded);
}
#[tokio::test]
async fn test_find_system_browser_returns_option() {
let result = find_system_browser().await;
let _ = result;
}
#[tokio::test]
async fn test_download_browser_no_system_browser_returns_error_or_path() {
let temp_dir = tempfile::tempdir().unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let result = manager.download_browser().await;
match result {
Ok(path) => {
assert!(path.exists());
let status = manager.get_status().await;
assert_eq!(status, DownloadStatus::Downloaded);
}
Err(e) => {
let status = manager.get_status().await;
match status {
DownloadStatus::Failed(msg) => {
assert!(!msg.is_empty());
}
_ => panic!("Expected Failed status, got {:?}", status),
}
assert!(matches!(e, BrowserDownloadError::DownloadFailed(_)));
}
}
}
#[tokio::test]
async fn test_download_browser_sets_status_downloading() {
let temp_dir = tempfile::tempdir().unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let _ = manager.download_browser().await;
let status = manager.get_status().await;
assert_ne!(status, DownloadStatus::NotDownloaded);
assert_ne!(status, DownloadStatus::Downloading);
}
#[tokio::test]
async fn test_get_or_download_browser_calls_download() {
let temp_dir = tempfile::tempdir().unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let result = manager.get_or_download_browser().await;
if let Ok(path) = result {
assert!(path.exists());
}
}
#[test]
fn test_browser_download_manager_clone() {
let config = BrowserDownloadConfig {
download_dir: PathBuf::from("/tmp/clone-test"),
timeout_seconds: 120,
auto_download: true,
};
let manager = BrowserDownloadManager::new(config);
let cloned = manager.clone();
assert_eq!(manager.get_cache_dir(), cloned.get_cache_dir());
}
#[test]
fn test_browser_download_manager_debug() {
let manager = BrowserDownloadManager::with_defaults();
let debug_str = format!("{:?}", manager);
assert!(debug_str.contains("BrowserDownloadManager"));
}
#[tokio::test]
async fn test_download_browser_returns_cached_executable() {
let temp_dir = tempfile::tempdir().unwrap();
let exe_path = get_browser_executable_path(temp_dir.path());
if let Some(parent) = exe_path.parent() {
tokio::fs::create_dir_all(parent).await.unwrap();
}
tokio::fs::write(&exe_path, "fake browser binary")
.await
.unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let result = manager.download_browser().await;
assert!(result.is_ok());
let status = manager.get_status().await;
assert_eq!(status, DownloadStatus::Downloaded);
}
#[tokio::test]
async fn test_is_browser_downloaded_true_when_executable_exists() {
let temp_dir = tempfile::tempdir().unwrap();
let exe_path = get_browser_executable_path(temp_dir.path());
if let Some(parent) = exe_path.parent() {
tokio::fs::create_dir_all(parent).await.unwrap();
}
tokio::fs::write(&exe_path, "binary").await.unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
assert!(manager.is_browser_downloaded().await);
}
#[tokio::test]
async fn test_download_browser_create_dir_failure() {
let temp_dir = tempfile::tempdir().unwrap();
let blocking_file = temp_dir.path().join("blocking_file");
tokio::fs::write(&blocking_file, "block").await.unwrap();
let impossible_dir = blocking_file.join("subdir");
let config = BrowserDownloadConfig {
download_dir: impossible_dir,
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let result = manager.download_browser().await;
match result {
Ok(_) => {
let status = manager.get_status().await;
assert_eq!(status, DownloadStatus::Downloaded);
}
Err(BrowserDownloadError::DirectoryCreationFailed(msg)) => {
assert!(msg.contains("Failed to create download directory"));
let status = manager.get_status().await;
match status {
DownloadStatus::Failed(s) => assert!(!s.is_empty()),
other => panic!("Expected Failed status, got {:?}", other),
}
}
Err(other) => panic!("Expected DirectoryCreationFailed or Ok, got {:?}", other),
}
}
#[tokio::test]
async fn test_download_browser_sets_downloading_then_terminal_status() {
let temp_dir = tempfile::tempdir().unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let _ = manager.download_browser().await;
let status = manager.get_status().await;
assert_ne!(status, DownloadStatus::NotDownloaded);
assert_ne!(status, DownloadStatus::Downloading);
}
#[tokio::test]
async fn test_download_browser_fetcher_unavailable_falls_through_to_error() {
let temp_dir = tempfile::tempdir().unwrap();
let config = BrowserDownloadConfig {
download_dir: temp_dir.path().to_path_buf(),
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let result = manager.download_browser().await;
match result {
Ok(path) => {
assert!(path.exists());
let status = manager.get_status().await;
assert_eq!(status, DownloadStatus::Downloaded);
}
Err(BrowserDownloadError::DownloadFailed(msg)) => {
assert!(msg.contains("Chrome") || msg.contains("Chromium"));
let status = manager.get_status().await;
match status {
DownloadStatus::Failed(s) => assert!(!s.is_empty()),
other => panic!("Expected Failed status, got {:?}", other),
}
}
Err(other) => panic!("Expected DownloadFailed or Ok, got {:?}", other),
}
}
#[tokio::test]
async fn test_cleanup_error_on_non_directory_path() {
let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("not_a_dir");
tokio::fs::write(&file_path, "file").await.unwrap();
let config = BrowserDownloadConfig {
download_dir: file_path,
timeout_seconds: 5,
auto_download: false,
};
let manager = BrowserDownloadManager::new(config);
let result = manager.cleanup().await;
assert!(result.is_err());
match result.unwrap_err() {
BrowserDownloadError::DownloadFailed(msg) => {
assert!(msg.contains("Cleanup failed"));
}
other => panic!("Expected DownloadFailed, got {:?}", other),
}
}
}