Skip to content

Commit

Permalink
backend: Pass handle token to method calls
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
arun-mani-j authored and bilelmoussaoui committed Dec 24, 2024
1 parent 9dc39fa commit f18356e
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 46 deletions.
3 changes: 2 additions & 1 deletion backend-demo/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use ashpd::{
request::RequestImpl,
Result,
},
desktop::account::UserInformation,
desktop::{account::UserInformation, HandleToken},
AppID, WindowIdentifierType,
};
use async_trait::async_trait;
Expand Down Expand Up @@ -39,6 +39,7 @@ mod fdo_account {
impl AccountImpl for Account {
async fn get_user_information(
&self,
_token: HandleToken,
_app_id: Option<AppID>,
_window_identifier: Option<WindowIdentifierType>,
_options: UserInformationOptions,
Expand Down
4 changes: 3 additions & 1 deletion backend-demo/src/screenshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +23,7 @@ impl RequestImpl for Screenshot {
impl ScreenshotImpl for Screenshot {
async fn screenshot(
&self,
_token: HandleToken,
_app_id: Option<AppID>,
_window_identifier: Option<WindowIdentifierType>,
_options: ScreenshotOptions,
Expand All @@ -34,6 +35,7 @@ impl ScreenshotImpl for Screenshot {

async fn pick_color(
&self,
_token: HandleToken,
_app_id: Option<AppID>,
_window_identifier: Option<WindowIdentifierType>,
_options: ColorOptions,
Expand Down
2 changes: 2 additions & 0 deletions backend-demo/src/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;

use ashpd::{
backend::{request::RequestImpl, secret::SecretImpl, Result},
desktop::HandleToken,
zbus::zvariant::OwnedValue,
AppID,
};
Expand All @@ -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<HashMap<String, OwnedValue>> {
Expand Down
2 changes: 2 additions & 0 deletions backend-demo/src/wallpaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ashpd::{
wallpaper::{WallpaperImpl, WallpaperOptions},
Result,
},
desktop::HandleToken,
AppID, WindowIdentifierType,
};
use async_trait::async_trait;
Expand All @@ -22,6 +23,7 @@ impl RequestImpl for Wallpaper {
impl WallpaperImpl for Wallpaper {
async fn with_uri(
&self,
_token: HandleToken,
_app_id: Option<AppID>,
_window_identifier: Option<WindowIdentifierType>,
_uri: url::Url,
Expand Down
7 changes: 5 additions & 2 deletions src/backend/access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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<AppID>,
window_identifier: Option<WindowIdentifierType>,
title: String,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 10 additions & 4 deletions src/backend/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -28,6 +28,7 @@ impl UserInformationOptions {
pub trait AccountImpl: RequestImpl {
async fn get_user_information(
&self,
token: HandleToken,
app_id: Option<AppID>,
window_identifier: Option<WindowIdentifierType>,
options: UserInformationOptions,
Expand Down Expand Up @@ -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
Expand Down
15 changes: 11 additions & 4 deletions src/backend/app_chooser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -79,6 +79,7 @@ impl Choice {
pub trait AppChooserImpl: RequestImpl {
async fn choose_application(
&self,
token: HandleToken,
app_id: Option<AppID>,
parent_window: Option<WindowIdentifierType>,
choices: Vec<AppID>,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 12 additions & 5 deletions src/backend/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,8 +56,12 @@ pub trait BackgroundSignalEmitter: Send + Sync {
pub trait BackgroundImpl: RequestImpl {
async fn get_app_state(&self) -> Result<HashMap<AppID, AppState>, PortalError>;

async fn notify_background(&self, app_id: AppID, name: &str)
-> Result<Background, PortalError>;
async fn notify_background(
&self,
token: HandleToken,
app_id: AppID,
name: &str,
) -> Result<Background, PortalError>;

async fn enable_autostart(
&self,
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions src/backend/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -63,6 +63,7 @@ impl Options {
pub trait EmailImpl: RequestImpl {
async fn compose(
&self,
token: HandleToken,
app_id: Option<AppID>,
window_identifier: Option<WindowIdentifierType>,
options: Options,
Expand Down Expand Up @@ -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
Expand Down
40 changes: 31 additions & 9 deletions src/backend/file_chooser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
desktop::{
file_chooser::{Choice, FileFilter},
request::Response,
HandleToken,
},
zvariant::{DeserializeDict, OwnedObjectPath, SerializeDict, Type},
AppID, FilePath, WindowIdentifierType,
Expand Down Expand Up @@ -188,6 +189,7 @@ impl SaveFilesOptions {
pub trait FileChooserImpl: RequestImpl {
async fn open_file(
&self,
token: HandleToken,
app_id: Option<AppID>,
window_identifier: Option<WindowIdentifierType>,
title: &str,
Expand All @@ -196,6 +198,7 @@ pub trait FileChooserImpl: RequestImpl {

async fn save_file(
&self,
token: HandleToken,
app_id: Option<AppID>,
window_identifier: Option<WindowIdentifierType>,
title: &str,
Expand All @@ -204,6 +207,7 @@ pub trait FileChooserImpl: RequestImpl {

async fn save_files(
&self,
token: HandleToken,
app_id: Option<AppID>,
window_identifier: Option<WindowIdentifierType>,
title: &str,
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit f18356e

Please sign in to comment.