| 1 |
use std::{collections::HashMap, sync::Arc}; |
| 2 |
|
| 3 |
use async_trait::async_trait; |
| 4 |
use zbus::zvariant::{self, OwnedValue}; |
| 5 |
|
| 6 |
use crate::{ |
| 7 |
MaybeAppID, |
| 8 |
backend::{ |
| 9 |
Result, |
| 10 |
request::{Request, RequestImpl}, |
| 11 |
}, |
| 12 |
desktop::{HandleToken, Response}, |
| 13 |
}; |
| 14 |
|
| 15 |
#[async_trait] |
| 16 |
pub trait SecretImpl: RequestImpl { |
| 17 |
#[doc(alias = "RetrieveSecret")] |
| 18 |
async fn retrieve( |
| 19 |
&self, |
| 20 |
token: HandleToken, |
| 21 |
app_id: MaybeAppID, |
| 22 |
fd: std::os::fd::OwnedFd, |
| 23 |
) -> Result<HashMap<String, OwnedValue>>; |
| 24 |
} |
| 25 |
|
| 26 |
pub(crate) struct SecretInterface { |
| 27 |
imp: Arc<dyn SecretImpl>, |
| 28 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 29 |
cnx: zbus::Connection, |
| 30 |
} |
| 31 |
|
| 32 |
impl SecretInterface { |
| 33 |
pub fn new( |
| 34 |
imp: Arc<dyn SecretImpl>, |
| 35 |
cnx: zbus::Connection, |
| 36 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 37 |
) -> Self { |
| 38 |
Self { imp, cnx, spawn } |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
#[zbus::interface(name = "org.freedesktop.impl.portal.Secret")] |
| 43 |
impl SecretInterface { |
| 44 |
#[zbus(property(emits_changed_signal = "const"), name = "version")] |
| 45 |
fn version(&self) -> u32 { |
| 46 |
1 |
| 47 |
} |
| 48 |
|
| 49 |
#[zbus(out_args("response", "results"))] |
| 50 |
async fn retrieve_secret( |
| 51 |
&self, |
| 52 |
handle: zvariant::OwnedObjectPath, |
| 53 |
app_id: MaybeAppID, |
| 54 |
fd: zvariant::OwnedFd, |
| 55 |
_options: HashMap<String, OwnedValue>, |
| 56 |
) -> Result<Response<HashMap<String, OwnedValue>>> { |
| 57 |
let imp = Arc::clone(&self.imp); |
| 58 |
|
| 59 |
Request::spawn( |
| 60 |
"Secret::RetrieveSecret", |
| 61 |
&self.cnx, |
| 62 |
handle.clone(), |
| 63 |
Arc::clone(&self.imp), |
| 64 |
Arc::clone(&self.spawn), |
| 65 |
async move { |
| 66 |
imp.retrieve( |
| 67 |
HandleToken::try_from(&handle).unwrap(), |
| 68 |
app_id, |
| 69 |
std::os::fd::OwnedFd::from(fd), |
| 70 |
) |
| 71 |
.await |
| 72 |
}, |
| 73 |
) |
| 74 |
.await |
| 75 |
} |
| 76 |
} |