From f18356ec215e000b7b6323ef485e36ad3aef4c93 Mon Sep 17 00:00:00 2001 From: Arun Mani J Date: Wed, 18 Dec 2024 14:47:35 +0530 Subject: [PATCH] backend: Pass handle token to method calls By having a handle token, an implementer can now associate further calls on the `Request` with its backend logic. For example, suppose an app A calls `Account.GetUserInformation()` followed by an app B. The implementer shows a dialog for each of them that lets the user give their response. Now, if any of these apps call `Request.close()`, then the implementer does not know which dialog to close, as it did not have any way to associate the `Request` to its backend logic in the first place. This commit solves this issue. --- backend-demo/src/account.rs | 3 ++- backend-demo/src/screenshot.rs | 4 +++- backend-demo/src/secret.rs | 2 ++ backend-demo/src/wallpaper.rs | 2 ++ src/backend/access.rs | 7 ++++-- src/backend/account.rs | 14 ++++++++---- src/backend/app_chooser.rs | 15 +++++++++---- src/backend/background.rs | 17 ++++++++++----- src/backend/email.rs | 14 ++++++++---- src/backend/file_chooser.rs | 40 ++++++++++++++++++++++++++-------- src/backend/print.rs | 10 +++++++-- src/backend/screenshot.rs | 28 ++++++++++++++++++------ src/backend/secret.rs | 14 +++++++++--- src/backend/wallpaper.rs | 15 +++++++++---- 14 files changed, 139 insertions(+), 46 deletions(-) diff --git a/backend-demo/src/account.rs b/backend-demo/src/account.rs index 5e7543ee7..7ca9ed0cc 100644 --- a/backend-demo/src/account.rs +++ b/backend-demo/src/account.rs @@ -4,7 +4,7 @@ use ashpd::{ request::RequestImpl, Result, }, - desktop::account::UserInformation, + desktop::{account::UserInformation, HandleToken}, AppID, WindowIdentifierType, }; use async_trait::async_trait; @@ -39,6 +39,7 @@ mod fdo_account { impl AccountImpl for Account { async fn get_user_information( &self, + _token: HandleToken, _app_id: Option, _window_identifier: Option, _options: UserInformationOptions, diff --git a/backend-demo/src/screenshot.rs b/backend-demo/src/screenshot.rs index 84ccacf7c..fc07a4904 100644 --- a/backend-demo/src/screenshot.rs +++ b/backend-demo/src/screenshot.rs @@ -4,7 +4,7 @@ use ashpd::{ screenshot::{ColorOptions, ScreenshotImpl, ScreenshotOptions}, Result, }, - desktop::{screenshot::Screenshot as ScreenshotResponse, Color}, + desktop::{screenshot::Screenshot as ScreenshotResponse, Color, HandleToken}, AppID, WindowIdentifierType, }; use async_trait::async_trait; @@ -23,6 +23,7 @@ impl RequestImpl for Screenshot { impl ScreenshotImpl for Screenshot { async fn screenshot( &self, + _token: HandleToken, _app_id: Option, _window_identifier: Option, _options: ScreenshotOptions, @@ -34,6 +35,7 @@ impl ScreenshotImpl for Screenshot { async fn pick_color( &self, + _token: HandleToken, _app_id: Option, _window_identifier: Option, _options: ColorOptions, diff --git a/backend-demo/src/secret.rs b/backend-demo/src/secret.rs index 797a6b94b..0470b632e 100644 --- a/backend-demo/src/secret.rs +++ b/backend-demo/src/secret.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use ashpd::{ backend::{request::RequestImpl, secret::SecretImpl, Result}, + desktop::HandleToken, zbus::zvariant::OwnedValue, AppID, }; @@ -21,6 +22,7 @@ impl RequestImpl for Secret { impl SecretImpl for Secret { async fn retrieve( &self, + _token: HandleToken, _app_id: AppID, _fd: std::os::fd::OwnedFd, ) -> Result> { diff --git a/backend-demo/src/wallpaper.rs b/backend-demo/src/wallpaper.rs index 0d9e904cd..28fe237f5 100644 --- a/backend-demo/src/wallpaper.rs +++ b/backend-demo/src/wallpaper.rs @@ -4,6 +4,7 @@ use ashpd::{ wallpaper::{WallpaperImpl, WallpaperOptions}, Result, }, + desktop::HandleToken, AppID, WindowIdentifierType, }; use async_trait::async_trait; @@ -22,6 +23,7 @@ impl RequestImpl for Wallpaper { impl WallpaperImpl for Wallpaper { async fn with_uri( &self, + _token: HandleToken, _app_id: Option, _window_identifier: Option, _uri: url::Url, diff --git a/src/backend/access.rs b/src/backend/access.rs index 26416a2b4..09550dd41 100644 --- a/src/backend/access.rs +++ b/src/backend/access.rs @@ -7,7 +7,7 @@ use crate::{ request::{Request, RequestImpl}, MaybeAppID, MaybeWindowIdentifier, Result, }, - desktop::{file_chooser::Choice, request::Response, Icon}, + desktop::{file_chooser::Choice, request::Response, HandleToken, Icon}, zvariant::{self, DeserializeDict, OwnedObjectPath, SerializeDict}, AppID, WindowIdentifierType, }; @@ -63,8 +63,10 @@ impl AccessResponse { #[async_trait] pub trait AccessImpl: RequestImpl { + #[allow(clippy::too_many_arguments)] async fn access_dialog( &self, + token: HandleToken, app_id: Option, window_identifier: Option, title: String, @@ -109,10 +111,11 @@ impl AccessInterface { Request::spawn( "Access::AccessDialog", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { imp.access_dialog( + HandleToken::try_from(&handle).unwrap(), app_id.inner(), window_identifier.inner(), title, diff --git a/src/backend/account.rs b/src/backend/account.rs index fac8ddfdf..77d15dd88 100644 --- a/src/backend/account.rs +++ b/src/backend/account.rs @@ -7,7 +7,7 @@ use crate::{ request::{Request, RequestImpl}, MaybeAppID, MaybeWindowIdentifier, Result, }, - desktop::{account::UserInformation, request::Response}, + desktop::{account::UserInformation, request::Response, HandleToken}, zvariant::{DeserializeDict, OwnedObjectPath, Type}, AppID, WindowIdentifierType, }; @@ -28,6 +28,7 @@ impl UserInformationOptions { pub trait AccountImpl: RequestImpl { async fn get_user_information( &self, + token: HandleToken, app_id: Option, window_identifier: Option, options: UserInformationOptions, @@ -66,11 +67,16 @@ impl AccountInterface { Request::spawn( "Account::GetUserInformation", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.get_user_information(app_id.inner(), window_identifier.inner(), options) - .await + imp.get_user_information( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + window_identifier.inner(), + options, + ) + .await }, ) .await diff --git a/src/backend/app_chooser.rs b/src/backend/app_chooser.rs index fc62ff4ff..6acd0c412 100644 --- a/src/backend/app_chooser.rs +++ b/src/backend/app_chooser.rs @@ -7,7 +7,7 @@ use crate::{ request::{Request, RequestImpl}, MaybeAppID, MaybeWindowIdentifier, }, - desktop::Response, + desktop::{HandleToken, Response}, zbus::object_server::{InterfaceRef, ObjectServer}, zvariant::{DeserializeDict, OwnedObjectPath, SerializeDict, Type}, ActivationToken, AppID, PortalError, WindowIdentifierType, @@ -79,6 +79,7 @@ impl Choice { pub trait AppChooserImpl: RequestImpl { async fn choose_application( &self, + token: HandleToken, app_id: Option, parent_window: Option, choices: Vec, @@ -124,11 +125,17 @@ impl AppChooserInterface { Request::spawn( "AppChooser::ChooseApplication", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.choose_application(app_id.inner(), parent_window.inner(), choices, options) - .await + imp.choose_application( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + parent_window.inner(), + choices, + options, + ) + .await }, ) .await diff --git a/src/backend/background.rs b/src/backend/background.rs index d371395ff..a0482b6e6 100644 --- a/src/backend/background.rs +++ b/src/backend/background.rs @@ -6,7 +6,7 @@ use serde_repr::{Deserialize_repr, Serialize_repr}; use crate::{ backend::request::{Request, RequestImpl}, - desktop::Response, + desktop::{HandleToken, Response}, zbus::object_server::SignalEmitter, zvariant::{OwnedObjectPath, SerializeDict, Type}, AppID, PortalError, @@ -56,8 +56,12 @@ pub trait BackgroundSignalEmitter: Send + Sync { pub trait BackgroundImpl: RequestImpl { async fn get_app_state(&self) -> Result, PortalError>; - async fn notify_background(&self, app_id: AppID, name: &str) - -> Result; + async fn notify_background( + &self, + token: HandleToken, + app_id: AppID, + name: &str, + ) -> Result; async fn enable_autostart( &self, @@ -128,9 +132,12 @@ impl BackgroundInterface { Request::spawn( "Background::NotifyBackground", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), - async move { imp.notify_background(app_id, &name).await }, + async move { + imp.notify_background(HandleToken::try_from(&handle).unwrap(), app_id, &name) + .await + }, ) .await } diff --git a/src/backend/email.rs b/src/backend/email.rs index 2cb768c1c..8a3fbecd2 100644 --- a/src/backend/email.rs +++ b/src/backend/email.rs @@ -7,7 +7,7 @@ use crate::{ request::{Request, RequestImpl}, MaybeAppID, MaybeWindowIdentifier, Result, }, - desktop::request::Response, + desktop::{request::Response, HandleToken}, zvariant::{self, DeserializeDict, OwnedObjectPath}, ActivationToken, AppID, WindowIdentifierType, }; @@ -63,6 +63,7 @@ impl Options { pub trait EmailImpl: RequestImpl { async fn compose( &self, + token: HandleToken, app_id: Option, window_identifier: Option, options: Options, @@ -100,11 +101,16 @@ impl EmailInterface { Request::spawn( "Email::ComposeEmail", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.compose(app_id.inner(), window_identifier.inner(), options) - .await + imp.compose( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + window_identifier.inner(), + options, + ) + .await }, ) .await diff --git a/src/backend/file_chooser.rs b/src/backend/file_chooser.rs index 92334080d..4a55676f0 100644 --- a/src/backend/file_chooser.rs +++ b/src/backend/file_chooser.rs @@ -10,6 +10,7 @@ use crate::{ desktop::{ file_chooser::{Choice, FileFilter}, request::Response, + HandleToken, }, zvariant::{DeserializeDict, OwnedObjectPath, SerializeDict, Type}, AppID, FilePath, WindowIdentifierType, @@ -188,6 +189,7 @@ impl SaveFilesOptions { pub trait FileChooserImpl: RequestImpl { async fn open_file( &self, + token: HandleToken, app_id: Option, window_identifier: Option, title: &str, @@ -196,6 +198,7 @@ pub trait FileChooserImpl: RequestImpl { async fn save_file( &self, + token: HandleToken, app_id: Option, window_identifier: Option, title: &str, @@ -204,6 +207,7 @@ pub trait FileChooserImpl: RequestImpl { async fn save_files( &self, + token: HandleToken, app_id: Option, window_identifier: Option, title: &str, @@ -243,11 +247,17 @@ impl FileChooserInterface { Request::spawn( "FileChooser::OpenFile", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.open_file(app_id.inner(), window_identifier.inner(), &title, options) - .await + imp.open_file( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + window_identifier.inner(), + &title, + options, + ) + .await }, ) .await @@ -267,11 +277,17 @@ impl FileChooserInterface { Request::spawn( "FileChooser::SaveFile", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.save_file(app_id.inner(), window_identifier.inner(), &title, options) - .await + imp.save_file( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + window_identifier.inner(), + &title, + options, + ) + .await }, ) .await @@ -291,11 +307,17 @@ impl FileChooserInterface { Request::spawn( "FileChooser::SaveFiles", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.save_files(app_id.inner(), window_identifier.inner(), &title, options) - .await + imp.save_files( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + window_identifier.inner(), + &title, + options, + ) + .await }, ) .await diff --git a/src/backend/print.rs b/src/backend/print.rs index 6bea6913b..764dc50f2 100644 --- a/src/backend/print.rs +++ b/src/backend/print.rs @@ -10,6 +10,7 @@ use crate::{ desktop::{ print::{PageSetup, PreparePrint, Settings}, request::Response, + HandleToken, }, zvariant::{self, DeserializeDict, OwnedObjectPath}, AppID, WindowIdentifierType, @@ -51,8 +52,10 @@ impl PrintOptions { #[async_trait] pub trait PrintImpl: RequestImpl { + #[allow(clippy::too_many_arguments)] async fn prepare_print( &self, + token: HandleToken, app_id: Option, parent_window: Option, title: String, @@ -63,6 +66,7 @@ pub trait PrintImpl: RequestImpl { async fn print( &self, + token: HandleToken, app_id: Option, parent_window: Option, title: String, @@ -106,10 +110,11 @@ impl PrintInterface { Request::spawn( "Print::PreparePrint", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { imp.prepare_print( + HandleToken::try_from(&handle).unwrap(), app_id.inner(), window_identifier.inner(), title, @@ -139,10 +144,11 @@ impl PrintInterface { Request::spawn( "Print::Print", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { imp.print( + HandleToken::try_from(&handle).unwrap(), app_id.inner(), window_identifier.inner(), title, diff --git a/src/backend/screenshot.rs b/src/backend/screenshot.rs index 1ae926bf9..c275b22ff 100644 --- a/src/backend/screenshot.rs +++ b/src/backend/screenshot.rs @@ -7,7 +7,9 @@ use crate::{ request::{Request, RequestImpl}, MaybeAppID, MaybeWindowIdentifier, Result, }, - desktop::{request::Response, screenshot::Screenshot as ScreenshotResponse, Color}, + desktop::{ + request::Response, screenshot::Screenshot as ScreenshotResponse, Color, HandleToken, + }, zvariant::{DeserializeDict, OwnedObjectPath, Type}, AppID, WindowIdentifierType, }; @@ -42,6 +44,7 @@ pub struct ColorOptions; pub trait ScreenshotImpl: RequestImpl { async fn screenshot( &self, + token: HandleToken, app_id: Option, window_identifier: Option, options: ScreenshotOptions, @@ -49,6 +52,7 @@ pub trait ScreenshotImpl: RequestImpl { async fn pick_color( &self, + token: HandleToken, app_id: Option, window_identifier: Option, options: ColorOptions, @@ -87,11 +91,16 @@ impl ScreenshotInterface { Request::spawn( "Screenshot::Screenshot", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.screenshot(app_id.inner(), window_identifier.inner(), options) - .await + imp.screenshot( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + window_identifier.inner(), + options, + ) + .await }, ) .await @@ -111,11 +120,16 @@ impl ScreenshotInterface { Request::spawn( "Screenshot::PickColor", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.pick_color(app_id.inner(), window_identifier.inner(), options) - .await + imp.pick_color( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + window_identifier.inner(), + options, + ) + .await }, ) .await diff --git a/src/backend/secret.rs b/src/backend/secret.rs index b05cc5892..e8d89c87b 100644 --- a/src/backend/secret.rs +++ b/src/backend/secret.rs @@ -8,7 +8,7 @@ use crate::{ request::{Request, RequestImpl}, Result, }, - desktop::Response, + desktop::{HandleToken, Response}, AppID, }; @@ -16,6 +16,7 @@ use crate::{ pub trait SecretImpl: RequestImpl { async fn retrieve( &self, + token: HandleToken, app_id: AppID, fd: std::os::fd::OwnedFd, ) -> Result>; @@ -52,9 +53,16 @@ impl SecretInterface { Request::spawn( "Secret::RetrieveSecret", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), - async move { imp.retrieve(app_id, std::os::fd::OwnedFd::from(fd)).await }, + async move { + imp.retrieve( + HandleToken::try_from(&handle).unwrap(), + app_id, + std::os::fd::OwnedFd::from(fd), + ) + .await + }, ) .await } diff --git a/src/backend/wallpaper.rs b/src/backend/wallpaper.rs index aaf178882..354500ff2 100644 --- a/src/backend/wallpaper.rs +++ b/src/backend/wallpaper.rs @@ -7,7 +7,7 @@ use crate::{ request::{Request, RequestImpl}, MaybeAppID, MaybeWindowIdentifier, Result, }, - desktop::{request::ResponseType, wallpaper::SetOn}, + desktop::{request::ResponseType, wallpaper::SetOn, HandleToken}, zvariant::{DeserializeDict, OwnedObjectPath, Type}, AppID, WindowIdentifierType, }; @@ -35,6 +35,7 @@ impl WallpaperOptions { pub trait WallpaperImpl: RequestImpl { async fn with_uri( &self, + token: HandleToken, app_id: Option, window_identifier: Option, uri: url::Url, @@ -75,11 +76,17 @@ impl WallpaperInterface { Request::spawn( "Wallpaper::SetWallpaperURI", &self.cnx, - handle, + handle.clone(), Arc::clone(&self.imp), async move { - imp.with_uri(app_id.inner(), window_identifier.inner(), uri, options) - .await + imp.with_uri( + HandleToken::try_from(&handle).unwrap(), + app_id.inner(), + window_identifier.inner(), + uri, + options, + ) + .await }, ) .await