diff options
| -rw-r--r-- | installer-downloader/src/cacao_impl/delegate.rs | 5 | ||||
| -rw-r--r-- | installer-downloader/src/cacao_impl/mod.rs | 9 | ||||
| -rw-r--r-- | installer-downloader/src/cacao_impl/ui.rs | 13 | ||||
| -rw-r--r-- | installer-downloader/src/controller.rs | 113 | ||||
| -rw-r--r-- | installer-downloader/src/winapi_impl/ui.rs | 2 | ||||
| -rw-r--r-- | installer-downloader/tests/controller.rs | 4 | ||||
| -rw-r--r-- | mullvad-update/src/bin/mullvad-version-metadata.rs | 2 | ||||
| -rw-r--r-- | mullvad-update/src/format/deserializer.rs | 4 | ||||
| -rw-r--r-- | mullvad-update/src/format/key.rs | 10 | ||||
| -rw-r--r-- | mullvad-update/src/version.rs | 4 |
10 files changed, 84 insertions, 82 deletions
diff --git a/installer-downloader/src/cacao_impl/delegate.rs b/installer-downloader/src/cacao_impl/delegate.rs index 0a09b3324d..84a80a5f20 100644 --- a/installer-downloader/src/cacao_impl/delegate.rs +++ b/installer-downloader/src/cacao_impl/delegate.rs @@ -168,7 +168,7 @@ impl AppWindow { fn sync_callback( callback: impl Fn() + Send + 'static, ) -> Arc<Mutex<Box<dyn Fn() + Send + 'static>>> { - Arc::new(Mutex::new(Box::new(move || callback()))) + Arc::new(Mutex::new(Box::new(callback))) } } @@ -178,8 +178,7 @@ pub struct Queue {} impl AppDelegateQueue<AppWindow> for Queue { fn queue_main<F: FnOnce(&mut AppWindow) + 'static + Send>(&self, callback: F) { // NOTE: We need this horrible lock because Dispatcher demands Sync - let cb: Mutex<Option<Box<dyn FnOnce(&mut AppWindow) + Send>>> = - Mutex::new(Some(Box::new(callback))); + let cb: Mutex<Option<super::ui::MainThreadCallback>> = Mutex::new(Some(Box::new(callback))); cacao::appkit::App::<super::ui::AppImpl, _>::dispatch_main(Action::QueueMain(cb)); } } diff --git a/installer-downloader/src/cacao_impl/mod.rs b/installer-downloader/src/cacao_impl/mod.rs index 89cd3b1402..e2974d370e 100644 --- a/installer-downloader/src/cacao_impl/mod.rs +++ b/installer-downloader/src/cacao_impl/mod.rs @@ -1,7 +1,7 @@ use std::sync::Mutex; use cacao::appkit::App; -use ui::{Action, AppImpl, AppWindow}; +use ui::{Action, AppImpl}; mod delegate; mod ui; @@ -9,10 +9,9 @@ mod ui; pub fn main() { let app = App::new("net.mullvad.downloader", AppImpl::default()); - let cb: Mutex<Option<Box<dyn FnOnce(&mut AppWindow) + Send + 'static>>> = - Mutex::new(Some(Box::new(|self_| { - crate::controller::initialize_controller(self_); - }))); + let cb: Mutex<Option<ui::MainThreadCallback>> = Mutex::new(Some(Box::new(|self_| { + crate::controller::initialize_controller(self_); + }))); cacao::appkit::App::<ui::AppImpl, _>::dispatch_main(Action::QueueMain(cb)); app.run(); diff --git a/installer-downloader/src/cacao_impl/ui.rs b/installer-downloader/src/cacao_impl/ui.rs index 5981b9465e..b38dbd02d6 100644 --- a/installer-downloader/src/cacao_impl/ui.rs +++ b/installer-downloader/src/cacao_impl/ui.rs @@ -83,7 +83,7 @@ pub enum Action { /// User clicked the cancel button CancelClick(Arc<Mutex<Box<dyn Fn() + Send>>>), /// Run callback on main thread - QueueMain(Mutex<Option<Box<dyn for<'a> FnOnce(&'a mut AppWindow) + Send>>>), + QueueMain(Mutex<Option<MainThreadCallback>>), /// User clicked the retry button in the error view ErrorRetry(Arc<Mutex<Box<dyn Fn() + Send>>>), /// User clicked the cancel button in the error view @@ -92,6 +92,9 @@ pub enum Action { Quit, } +/// Callback used for `QueueMain` +pub type MainThreadCallback = Box<dyn for<'a> FnOnce(&'a mut AppWindow) + Send>; + impl Dispatcher for AppImpl { type Message = Action; @@ -155,8 +158,8 @@ pub struct AppWindow { pub status_text: Label, pub error_view: Option<ErrorView>, - pub error_retry_callback: Option<Arc<Mutex<Box<dyn Fn() + Send>>>>, - pub error_cancel_callback: Option<Arc<Mutex<Box<dyn Fn() + Send>>>>, + pub error_retry_callback: Option<Arc<Mutex<ErrorViewClickCallback>>>, + pub error_cancel_callback: Option<Arc<Mutex<ErrorViewClickCallback>>>, pub download_text: Label, @@ -172,6 +175,8 @@ pub struct ErrorView { pub cancel_button: Button, } +pub type ErrorViewClickCallback = Box<dyn Fn() + Send>; + pub struct DownloadButton { pub button: Button, } @@ -286,7 +291,7 @@ impl AppWindow { self.beta_link_preface.set_text(BETA_PREFACE_DESC); self.main_view.add_subview(&self.beta_link_preface); - let mut attr_text = AttributedString::new(&BETA_LINK_TEXT); + let mut attr_text = AttributedString::new(BETA_LINK_TEXT); attr_text.set_text_color(Color::Link, 0..BETA_LINK_TEXT.len() as isize); self.beta_link.set_attributed_text(attr_text); diff --git a/installer-downloader/src/controller.rs b/installer-downloader/src/controller.rs index c4383dff7d..5bd42709e2 100644 --- a/installer-downloader/src/controller.rs +++ b/installer-downloader/src/controller.rs @@ -123,78 +123,75 @@ impl AppController { } /// Background task that fetches app version data. -fn fetch_app_version_info<Delegate, VersionProvider>( +async fn fetch_app_version_info<Delegate, VersionProvider>( queue: Delegate::Queue, download_tx: mpsc::Sender<TaskMessage>, version_provider: VersionProvider, -) -> impl Future<Output = ()> -where +) where Delegate: AppDelegate + 'static, VersionProvider: VersionInfoProvider + Send, { - async move { - loop { - let version_params = VersionParameters { - // TODO: detect current architecture - architecture: VersionArchitecture::X86, - // For the downloader, the rollout version is always preferred - rollout: 1., - // The downloader allows any version - lowest_metadata_version: 0, - }; + loop { + let version_params = VersionParameters { + // TODO: detect current architecture + architecture: VersionArchitecture::X86, + // For the downloader, the rollout version is always preferred + rollout: 1., + // The downloader allows any version + lowest_metadata_version: 0, + }; - let err = match version_provider.get_version_info(version_params).await { - Ok(version_info) => { - let _ = download_tx.try_send(TaskMessage::SetVersionInfo(version_info)); - return; - } - Err(err) => err, - }; + let err = match version_provider.get_version_info(version_params).await { + Ok(version_info) => { + let _ = download_tx.try_send(TaskMessage::SetVersionInfo(version_info)); + return; + } + Err(err) => err, + }; - eprintln!("Failed to get version info: {err}"); + eprintln!("Failed to get version info: {err}"); - enum Action { - Retry, - Cancel, - } + enum Action { + Retry, + Cancel, + } - let (action_tx, mut action_rx) = mpsc::channel(1); + let (action_tx, mut action_rx) = mpsc::channel(1); - // show error message (needs to happen on the UI thread) - // send Action when user presses a button to contin - queue.queue_main(move |self_| { - self_.hide_download_button(); + // show error message (needs to happen on the UI thread) + // send Action when user presses a button to continue + queue.queue_main(move |self_| { + self_.hide_download_button(); - let (retry_tx, cancel_tx) = (action_tx.clone(), action_tx); + let (retry_tx, cancel_tx) = (action_tx.clone(), action_tx); - self_.set_status_text(""); - self_.on_error_message_retry(move || { - let _ = retry_tx.try_send(Action::Retry); - }); - self_.on_error_message_cancel(move || { - let _ = cancel_tx.try_send(Action::Cancel); - }); - self_.show_error_message(crate::delegate::ErrorMessage { - status_text: resource::FETCH_VERSION_ERROR_DESC.to_owned(), - cancel_button_text: resource::FETCH_VERSION_ERROR_CANCEL_BUTTON_TEXT.to_owned(), - retry_button_text: resource::FETCH_VERSION_ERROR_RETRY_BUTTON_TEXT.to_owned(), - }); + self_.set_status_text(""); + self_.on_error_message_retry(move || { + let _ = retry_tx.try_send(Action::Retry); + }); + self_.on_error_message_cancel(move || { + let _ = cancel_tx.try_send(Action::Cancel); }); + self_.show_error_message(crate::delegate::ErrorMessage { + status_text: resource::FETCH_VERSION_ERROR_DESC.to_owned(), + cancel_button_text: resource::FETCH_VERSION_ERROR_CANCEL_BUTTON_TEXT.to_owned(), + retry_button_text: resource::FETCH_VERSION_ERROR_RETRY_BUTTON_TEXT.to_owned(), + }); + }); - // wait for user to press either button - let Some(action) = action_rx.recv().await else { - panic!("channel was dropped? argh") - }; + // wait for user to press either button + let Some(action) = action_rx.recv().await else { + panic!("channel was dropped? argh") + }; - match action { - Action::Retry => { - continue; - } - Action::Cancel => { - queue.queue_main(|self_| { - self_.quit(); - }); - } + match action { + Action::Retry => { + continue; + } + Action::Cancel => { + queue.queue_main(|self_| { + self_.quit(); + }); } } } @@ -269,7 +266,7 @@ async fn handle_action_messages<D, A, DirProvider>( }); } TaskMessage::BeginDownload => { - if let Some(_) = active_download.take() { + if active_download.take().is_some() { println!("Interrupting ongoing download"); } let Some(version_info) = version_info.clone() else { @@ -372,7 +369,7 @@ async fn handle_action_messages<D, A, DirProvider>( } }; - let version_label = format_latest_version(&selected_version); + let version_label = format_latest_version(selected_version); let has_beta = version_info.beta.is_some(); queue.queue_main(move |self_| { diff --git a/installer-downloader/src/winapi_impl/ui.rs b/installer-downloader/src/winapi_impl/ui.rs index 820972a914..a24874a55a 100644 --- a/installer-downloader/src/winapi_impl/ui.rs +++ b/installer-downloader/src/winapi_impl/ui.rs @@ -406,7 +406,7 @@ fn handle_link_messages( handler_id: usize, ) -> Result<nwg::RawEventHandler, nwg::NwgError> { let link_hwnd = link.handle.hwnd().map(|hwnd| hwnd as isize); - nwg::bind_raw_event_handler(&parent, handler_id, move |_hwnd, msg, w, p| { + nwg::bind_raw_event_handler(parent, handler_id, move |_hwnd, msg, w, p| { /// This is the RGB() macro except it takes in a slice representing RGB values pub fn rgb(color: [u8; 3]) -> COLORREF { color[0] as COLORREF | ((color[1] as COLORREF) << 8) | ((color[2] as COLORREF) << 16) diff --git a/installer-downloader/tests/controller.rs b/installer-downloader/tests/controller.rs index b1a05a0578..704835e5a7 100644 --- a/installer-downloader/tests/controller.rs +++ b/installer-downloader/tests/controller.rs @@ -123,9 +123,11 @@ impl<const EXE_SUCCEED: bool, const VERIFY_SUCCEED: bool, const LAUNCH_SUCCEED: /// A fake queue that stores callbacks so that tests can run them later. #[derive(Clone, Default)] pub struct FakeQueue { - callbacks: Arc<Mutex<Vec<Box<dyn FnOnce(&mut FakeAppDelegate) + Send>>>>, + callbacks: Arc<Mutex<Vec<MainThreadCallback>>>, } +pub type MainThreadCallback = Box<dyn FnOnce(&mut FakeAppDelegate) + Send>; + impl FakeQueue { /// Run all queued callbacks on the given delegate. fn run_callbacks(&self, delegate: &mut FakeAppDelegate) { diff --git a/mullvad-update/src/bin/mullvad-version-metadata.rs b/mullvad-update/src/bin/mullvad-version-metadata.rs index fbfc0ed51b..c531fbe9dd 100644 --- a/mullvad-update/src/bin/mullvad-version-metadata.rs +++ b/mullvad-update/src/bin/mullvad-version-metadata.rs @@ -35,7 +35,7 @@ async fn main() -> anyhow::Result<()> { match opt { Opt::GenerateKey => { - println!("{}", key::SecretKey::generate().to_string()); + println!("{}", key::SecretKey::generate()); Ok(()) } Opt::Sign { file, secret } => sign(file, secret).await, diff --git a/mullvad-update/src/format/deserializer.rs b/mullvad-update/src/format/deserializer.rs index 28291dde00..f79aab6537 100644 --- a/mullvad-update/src/format/deserializer.rs +++ b/mullvad-update/src/format/deserializer.rs @@ -40,7 +40,7 @@ impl SignedResponse { min_metadata_version: usize, ) -> Result<Self, anyhow::Error> { // Deserialize and verify signature - let partial_data = deserialize_and_verify(&key, bytes)?; + let partial_data = deserialize_and_verify(key, bytes)?; // Deserialize the canonical JSON to structured representation let signed_response: Response = serde_json::from_value(partial_data.signed) @@ -182,7 +182,7 @@ mod test { .expect("deserialization failed"); let expected_key = VerifyingKey::from_hex(pubkey).unwrap(); - let expected_sig = Signature::from_hex(&fakesig).unwrap(); + let expected_sig = Signature::from_hex(fakesig).unwrap(); // Ed25519 key assert!( diff --git a/mullvad-update/src/format/key.rs b/mullvad-update/src/format/key.rs index a951aff709..01cd51a6ea 100644 --- a/mullvad-update/src/format/key.rs +++ b/mullvad-update/src/format/key.rs @@ -1,6 +1,6 @@ //! Key and signature types for API version response format -use std::str::FromStr; +use std::{fmt, str::FromStr}; use anyhow::{bail, Context}; use ed25519_dalek::ed25519::signature::SignerMut; @@ -34,9 +34,9 @@ impl SecretKey { } } -impl ToString for SecretKey { - fn to_string(&self) -> String { - hex::encode(self.0) +impl fmt::Display for SecretKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", hex::encode(self.0)) } } @@ -56,7 +56,7 @@ impl FromStr for SecretKey { type Err = anyhow::Error; fn from_str(s: &str) -> Result<Self, Self::Err> { - let bytes = bytes_from_hex::<{ ed25519_dalek::SECRET_KEY_LENGTH }>(&s)?; + let bytes = bytes_from_hex::<{ ed25519_dalek::SECRET_KEY_LENGTH }>(s)?; Ok(SecretKey(bytes)) } } diff --git a/mullvad-update/src/version.rs b/mullvad-update/src/version.rs index 5317cf8d49..80c28c7846 100644 --- a/mullvad-update/src/version.rs +++ b/mullvad-update/src/version.rs @@ -121,7 +121,7 @@ impl VersionInfo { Ok(Self { stable: Version::try_from(stable)?, - beta: beta.map(|beta| Version::try_from(beta)).transpose()?, + beta: beta.map(Version::try_from).transpose()?, }) } @@ -131,7 +131,7 @@ impl VersionInfo { fn find_duplicate_version(releases: &[format::Release]) -> Option<&mullvad_version::Version> { releases .windows(2) - .find(|pair| &pair[0].version == &pair[1].version) + .find(|pair| pair[0].version == pair[1].version) .map(|pair| &pair[0].version) } } |
