mirror of
https://github.com/zhom/donutbrowser.git
synced 2026-06-07 07:23:56 +02:00
refactor: browser auto-update
This commit is contained in:
@@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use tauri::Emitter;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct UpdateNotification {
|
||||
@@ -120,6 +121,53 @@ impl AutoUpdater {
|
||||
|
||||
Ok(notifications)
|
||||
}
|
||||
|
||||
pub async fn check_for_updates_with_progress(
|
||||
&self,
|
||||
app_handle: &tauri::AppHandle,
|
||||
) {
|
||||
// Check for browser updates and trigger auto-downloads
|
||||
match self.check_for_updates().await {
|
||||
Ok(update_notifications) => {
|
||||
if !update_notifications.is_empty() {
|
||||
println!(
|
||||
"Found {} browser updates to auto-download",
|
||||
update_notifications.len()
|
||||
);
|
||||
|
||||
// Trigger automatic downloads for each update
|
||||
for notification in update_notifications {
|
||||
println!(
|
||||
"Auto-downloading {} version {}",
|
||||
notification.browser, notification.new_version
|
||||
);
|
||||
|
||||
// Emit a custom event to trigger auto-download
|
||||
let auto_update_event = serde_json::json!({
|
||||
"browser": notification.browser,
|
||||
"new_version": notification.new_version,
|
||||
"notification_id": notification.id,
|
||||
"affected_profiles": notification.affected_profiles
|
||||
});
|
||||
|
||||
if let Err(e) = app_handle.emit("browser-auto-update-available", &auto_update_event) {
|
||||
eprintln!(
|
||||
"Failed to emit auto-update event for {}: {e}",
|
||||
notification.browser
|
||||
);
|
||||
} else {
|
||||
println!("Emitted auto-update event for {}", notification.browser);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("No browser updates needed");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Failed to check for browser updates: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a specific profile has an available update
|
||||
fn check_profile_update(
|
||||
@@ -426,6 +474,14 @@ pub async fn complete_browser_update_with_auto_update(
|
||||
.map_err(|e| format!("Failed to complete browser update: {e}"))
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn check_for_updates_with_progress(
|
||||
app_handle: tauri::AppHandle,
|
||||
) {
|
||||
let updater = AutoUpdater::new();
|
||||
updater.check_for_updates_with_progress(&app_handle).await;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -1913,7 +1913,10 @@ impl BrowserRunner {
|
||||
if let Ok(settings) = settings_manager.load_settings() {
|
||||
if settings.auto_delete_unused_binaries {
|
||||
// Perform cleanup in the background after profile deletion
|
||||
let _ = self.cleanup_unused_binaries_internal();
|
||||
// Ignore errors since this is not critical for profile deletion
|
||||
if let Err(e) = self.cleanup_unused_binaries_internal() {
|
||||
println!("Warning: Failed to cleanup unused binaries: {e}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -297,6 +297,13 @@ pub fn run() {
|
||||
version_updater::VersionUpdater::run_background_task().await;
|
||||
});
|
||||
|
||||
let app_handle_auto_updater = app.handle().clone();
|
||||
|
||||
// Start the auto-update check task separately
|
||||
tauri::async_runtime::spawn(async move {
|
||||
auto_updater::check_for_updates_with_progress(app_handle_auto_updater).await;
|
||||
});
|
||||
|
||||
let app_handle_update = app.handle().clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
println!("Starting app update check at startup...");
|
||||
|
||||
@@ -9,6 +9,7 @@ use tauri::Emitter;
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::time::interval;
|
||||
|
||||
use crate::auto_updater::AutoUpdater;
|
||||
use crate::browser_version_service::BrowserVersionService;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
@@ -47,6 +48,7 @@ impl Default for BackgroundUpdateState {
|
||||
|
||||
pub struct VersionUpdater {
|
||||
version_service: BrowserVersionService,
|
||||
auto_updater: AutoUpdater,
|
||||
app_handle: Option<tauri::AppHandle>,
|
||||
}
|
||||
|
||||
@@ -54,6 +56,7 @@ impl VersionUpdater {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
version_service: BrowserVersionService::new(),
|
||||
auto_updater: AutoUpdater::new(),
|
||||
app_handle: None,
|
||||
}
|
||||
}
|
||||
@@ -379,9 +382,11 @@ impl VersionUpdater {
|
||||
}
|
||||
|
||||
// Small delay between browsers to avoid overwhelming APIs
|
||||
tokio::time::sleep(Duration::from_millis(500)).await;
|
||||
tokio::time::sleep(Duration::from_millis(200)).await;
|
||||
}
|
||||
|
||||
self.auto_updater.check_for_updates_with_progress(app_handle).await;
|
||||
|
||||
// Emit completion event
|
||||
let progress = VersionUpdateProgress {
|
||||
current_browser: "".to_string(),
|
||||
|
||||
Reference in New Issue
Block a user