| 1 |
use std::{collections::HashMap, sync::Arc}; |
| 2 |
|
| 3 |
use async_trait::async_trait; |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
MaybeAppID, PortalError, |
| 7 |
documents::{DocumentID, Permission}, |
| 8 |
zbus::object_server::SignalEmitter, |
| 9 |
zvariant::{OwnedValue, Value}, |
| 10 |
}; |
| 11 |
|
| 12 |
#[async_trait] |
| 13 |
pub trait PermissionStoreSignalEmitter: Send + Sync { |
| 14 |
async fn emit_document_changed( |
| 15 |
&self, |
| 16 |
table: &str, |
| 17 |
id: DocumentID, |
| 18 |
deleted: bool, |
| 19 |
data: Value<'_>, |
| 20 |
permissions: HashMap<MaybeAppID, Vec<Permission>>, |
| 21 |
) -> zbus::Result<()>; |
| 22 |
} |
| 23 |
|
| 24 |
#[async_trait] |
| 25 |
pub trait PermissionStoreImpl: Send + Sync { |
| 26 |
#[doc(alias = "Lookup")] |
| 27 |
async fn lookup( |
| 28 |
&self, |
| 29 |
table: &str, |
| 30 |
id: DocumentID, |
| 31 |
) -> Result<(HashMap<MaybeAppID, Vec<Permission>>, OwnedValue), PortalError>; |
| 32 |
|
| 33 |
#[doc(alias = "Set")] |
| 34 |
async fn set( |
| 35 |
&self, |
| 36 |
table: &str, |
| 37 |
create: bool, |
| 38 |
id: DocumentID, |
| 39 |
app_permissions: HashMap<MaybeAppID, Vec<Permission>>, |
| 40 |
data: Value<'_>, |
| 41 |
) -> Result<(), PortalError>; |
| 42 |
|
| 43 |
#[doc(alias = "Delete")] |
| 44 |
async fn delete(&self, table: &str, id: DocumentID) -> Result<(), PortalError>; |
| 45 |
|
| 46 |
#[doc(alias = "SetValue")] |
| 47 |
async fn set_value( |
| 48 |
&self, |
| 49 |
table: &str, |
| 50 |
create: bool, |
| 51 |
id: DocumentID, |
| 52 |
data: Value<'_>, |
| 53 |
) -> Result<(), PortalError>; |
| 54 |
|
| 55 |
#[doc(alias = "List")] |
| 56 |
async fn list(&self, table: &str) -> Result<Vec<DocumentID>, PortalError>; |
| 57 |
|
| 58 |
#[doc(alias = "GetPermission")] |
| 59 |
async fn get_permission( |
| 60 |
&self, |
| 61 |
table: &str, |
| 62 |
id: DocumentID, |
| 63 |
app: MaybeAppID, |
| 64 |
) -> Result<Vec<Permission>, PortalError>; |
| 65 |
|
| 66 |
#[doc(alias = "SetPermission")] |
| 67 |
async fn set_permission( |
| 68 |
&self, |
| 69 |
table: &str, |
| 70 |
create: bool, |
| 71 |
id: DocumentID, |
| 72 |
app: MaybeAppID, |
| 73 |
permissions: Vec<Permission>, |
| 74 |
) -> Result<(), PortalError>; |
| 75 |
|
| 76 |
#[doc(alias = "DeletePermission")] |
| 77 |
async fn delete_permission( |
| 78 |
&self, |
| 79 |
table: &str, |
| 80 |
id: DocumentID, |
| 81 |
app: MaybeAppID, |
| 82 |
) -> Result<(), PortalError>; |
| 83 |
|
| 84 |
// Set the signal emitter, allowing to notify of changes. |
| 85 |
fn set_signal_emitter(&mut self, signal_emitter: Arc<dyn PermissionStoreSignalEmitter>); |
| 86 |
} |
| 87 |
|
| 88 |
pub(crate) struct PermissionStoreInterface { |
| 89 |
imp: Arc<dyn PermissionStoreImpl>, |
| 90 |
#[allow(dead_code)] |
| 91 |
cnx: zbus::Connection, |
| 92 |
#[allow(dead_code)] |
| 93 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 94 |
} |
| 95 |
|
| 96 |
impl PermissionStoreInterface { |
| 97 |
pub fn new( |
| 98 |
imp: Arc<dyn PermissionStoreImpl>, |
| 99 |
cnx: zbus::Connection, |
| 100 |
spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>, |
| 101 |
) -> Self { |
| 102 |
Self { imp, cnx, spawn } |
| 103 |
} |
| 104 |
|
| 105 |
pub async fn document_changed( |
| 106 |
&self, |
| 107 |
table: &str, |
| 108 |
id: DocumentID, |
| 109 |
deleted: bool, |
| 110 |
data: Value<'_>, |
| 111 |
permissions: HashMap<MaybeAppID, Vec<Permission>>, |
| 112 |
) -> zbus::Result<()> { |
| 113 |
let object_server = self.cnx.object_server(); |
| 114 |
let iface_ref = object_server |
| 115 |
.interface::<_, Self>(crate::proxy::DESKTOP_PATH) |
| 116 |
.await?; |
| 117 |
Self::changed( |
| 118 |
iface_ref.signal_emitter(), |
| 119 |
table, |
| 120 |
id, |
| 121 |
deleted, |
| 122 |
data, |
| 123 |
permissions, |
| 124 |
) |
| 125 |
.await |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#[async_trait] |
| 130 |
impl PermissionStoreSignalEmitter for PermissionStoreInterface { |
| 131 |
#[doc(alias = "Changed")] |
| 132 |
async fn emit_document_changed( |
| 133 |
&self, |
| 134 |
table: &str, |
| 135 |
id: DocumentID, |
| 136 |
deleted: bool, |
| 137 |
data: Value<'_>, |
| 138 |
permissions: HashMap<MaybeAppID, Vec<Permission>>, |
| 139 |
) -> zbus::Result<()> { |
| 140 |
self.document_changed(table, id, deleted, data, permissions) |
| 141 |
.await |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
#[zbus::interface(name = "org.freedesktop.impl.portal.PermissionStore")] |
| 146 |
impl PermissionStoreInterface { |
| 147 |
#[zbus(property(emits_changed_signal = "const"), name = "version")] |
| 148 |
fn version(&self) -> u32 { |
| 149 |
2 |
| 150 |
} |
| 151 |
|
| 152 |
#[zbus(out_args("permissions", "data"))] |
| 153 |
async fn lookup( |
| 154 |
&self, |
| 155 |
table: &str, |
| 156 |
id: DocumentID, |
| 157 |
) -> Result<(HashMap<MaybeAppID, Vec<Permission>>, OwnedValue), PortalError> { |
| 158 |
#[cfg(feature = "tracing")] |
| 159 |
tracing::debug!("PermissionStore::Lookup"); |
| 160 |
|
| 161 |
let response = self.imp.lookup(table, id).await; |
| 162 |
|
| 163 |
#[cfg(feature = "tracing")] |
| 164 |
tracing::debug!("PermissionStore::Lookup returned {:#?}", response); |
| 165 |
response |
| 166 |
} |
| 167 |
|
| 168 |
async fn set( |
| 169 |
&self, |
| 170 |
table: &str, |
| 171 |
create: bool, |
| 172 |
id: DocumentID, |
| 173 |
app_permissions: HashMap<MaybeAppID, Vec<Permission>>, |
| 174 |
data: Value<'_>, |
| 175 |
) -> Result<(), PortalError> { |
| 176 |
#[cfg(feature = "tracing")] |
| 177 |
tracing::debug!("PermissionStore::Set"); |
| 178 |
|
| 179 |
let response = self.imp.set(table, create, id, app_permissions, data).await; |
| 180 |
|
| 181 |
#[cfg(feature = "tracing")] |
| 182 |
tracing::debug!("PermissionStore::Set returned {:#?}", response); |
| 183 |
response |
| 184 |
} |
| 185 |
|
| 186 |
async fn set_value( |
| 187 |
&self, |
| 188 |
table: &str, |
| 189 |
create: bool, |
| 190 |
id: DocumentID, |
| 191 |
data: Value<'_>, |
| 192 |
) -> Result<(), PortalError> { |
| 193 |
#[cfg(feature = "tracing")] |
| 194 |
tracing::debug!("PermissionStore::SetValue"); |
| 195 |
|
| 196 |
let response = self.imp.set_value(table, create, id, data).await; |
| 197 |
|
| 198 |
#[cfg(feature = "tracing")] |
| 199 |
tracing::debug!("PermissionStore::SetValue returned {:#?}", response); |
| 200 |
response |
| 201 |
} |
| 202 |
|
| 203 |
#[zbus(out_args("ids"))] |
| 204 |
async fn list(&self, table: &str) -> Result<Vec<DocumentID>, PortalError> { |
| 205 |
#[cfg(feature = "tracing")] |
| 206 |
tracing::debug!("PermissionStore::List"); |
| 207 |
|
| 208 |
let response = self.imp.list(table).await; |
| 209 |
|
| 210 |
#[cfg(feature = "tracing")] |
| 211 |
tracing::debug!("PermissionStore::List returned {:#?}", response); |
| 212 |
response |
| 213 |
} |
| 214 |
|
| 215 |
#[zbus(out_args("permissions"))] |
| 216 |
async fn get_permission( |
| 217 |
&self, |
| 218 |
table: &str, |
| 219 |
id: DocumentID, |
| 220 |
app: MaybeAppID, |
| 221 |
) -> Result<Vec<Permission>, PortalError> { |
| 222 |
#[cfg(feature = "tracing")] |
| 223 |
tracing::debug!("PermissionStore::GetPermission"); |
| 224 |
|
| 225 |
let response = self.imp.get_permission(table, id, app).await; |
| 226 |
|
| 227 |
#[cfg(feature = "tracing")] |
| 228 |
tracing::debug!("PermissionStore::GetPermission returned {:#?}", response); |
| 229 |
response |
| 230 |
} |
| 231 |
|
| 232 |
async fn set_permission( |
| 233 |
&self, |
| 234 |
table: &str, |
| 235 |
create: bool, |
| 236 |
id: DocumentID, |
| 237 |
app: MaybeAppID, |
| 238 |
permissions: Vec<Permission>, |
| 239 |
) -> Result<(), PortalError> { |
| 240 |
#[cfg(feature = "tracing")] |
| 241 |
tracing::debug!("PermissionStore::SetPermission"); |
| 242 |
|
| 243 |
let response = self |
| 244 |
.imp |
| 245 |
.set_permission(table, create, id, app, permissions) |
| 246 |
.await; |
| 247 |
|
| 248 |
#[cfg(feature = "tracing")] |
| 249 |
tracing::debug!("PermissionStore::SetPermission returned {:#?}", response); |
| 250 |
response |
| 251 |
} |
| 252 |
|
| 253 |
async fn delete_permission( |
| 254 |
&self, |
| 255 |
table: &str, |
| 256 |
id: DocumentID, |
| 257 |
app: MaybeAppID, |
| 258 |
) -> Result<(), PortalError> { |
| 259 |
#[cfg(feature = "tracing")] |
| 260 |
tracing::debug!("PermissionStore::DeletePermission"); |
| 261 |
|
| 262 |
let response = self.imp.delete_permission(table, id, app).await; |
| 263 |
|
| 264 |
#[cfg(feature = "tracing")] |
| 265 |
tracing::debug!("PermissionStore::DeletePermission returned {:#?}", response); |
| 266 |
response |
| 267 |
} |
| 268 |
|
| 269 |
async fn delete(&self, table: &str, id: DocumentID) -> Result<(), PortalError> { |
| 270 |
#[cfg(feature = "tracing")] |
| 271 |
tracing::debug!("PermissionStore::Delete"); |
| 272 |
|
| 273 |
let response = self.imp.delete(table, id).await; |
| 274 |
|
| 275 |
#[cfg(feature = "tracing")] |
| 276 |
tracing::debug!("PermissionStore::Delete returned {:#?}", response); |
| 277 |
response |
| 278 |
} |
| 279 |
|
| 280 |
#[zbus(signal)] |
| 281 |
async fn changed( |
| 282 |
signal_ctxt: &SignalEmitter<'_>, |
| 283 |
table: &str, |
| 284 |
id: DocumentID, |
| 285 |
deleted: bool, |
| 286 |
data: Value<'_>, |
| 287 |
permissions: HashMap<MaybeAppID, Vec<Permission>>, |
| 288 |
) -> zbus::Result<()>; |
| 289 |
} |