| 1 |
use std::sync::{Arc, Mutex}; |
| 2 |
|
| 3 |
use enumflags2::BitFlags; |
| 4 |
use futures_util::{StreamExt, task::SpawnExt}; |
| 5 |
use zbus::names::{OwnedWellKnownName, WellKnownName}; |
| 6 |
|
| 7 |
use crate::backend::{Result, session::SessionManager}; |
| 8 |
|
| 9 |
pub struct Builder { |
| 10 |
name: OwnedWellKnownName, |
| 11 |
flags: BitFlags<zbus::fdo::RequestNameFlags>, |
| 12 |
#[cfg(feature = "account")] |
| 13 |
account_impl: Option<Arc<dyn crate::backend::account::AccountImpl>>, |
| 14 |
access_impl: Option<Arc<dyn crate::backend::access::AccessImpl>>, |
| 15 |
app_chooser_impl: Option<Arc<dyn crate::backend::app_chooser::AppChooserImpl>>, |
| 16 |
#[cfg(feature = "background")] |
| 17 |
background_impl: Option<Arc<dyn crate::backend::background::BackgroundImpl>>, |
| 18 |
#[cfg(feature = "email")] |
| 19 |
email_impl: Option<Arc<dyn crate::backend::email::EmailImpl>>, |
| 20 |
#[cfg(feature = "file_chooser")] |
| 21 |
file_chooser_impl: Option<Arc<dyn crate::backend::file_chooser::FileChooserImpl>>, |
| 22 |
lockdown_impl: Option<Arc<dyn crate::backend::lockdown::LockdownImpl>>, |
| 23 |
#[cfg(feature = "documents")] |
| 24 |
permission_store_impl: Option<Arc<dyn crate::backend::permission_store::PermissionStoreImpl>>, |
| 25 |
#[cfg(feature = "print")] |
| 26 |
print_impl: Option<Arc<dyn crate::backend::print::PrintImpl>>, |
| 27 |
#[cfg(feature = "screencast")] |
| 28 |
screencast_impl: Option<Arc<dyn crate::backend::screencast::ScreencastImpl>>, |
| 29 |
#[cfg(feature = "screenshot")] |
| 30 |
screenshot_impl: Option<Arc<dyn crate::backend::screenshot::ScreenshotImpl>>, |
| 31 |
#[cfg(feature = "secret")] |
| 32 |
secret_impl: Option<Arc<dyn crate::backend::secret::SecretImpl>>, |
| 33 |
#[cfg(feature = "settings")] |
| 34 |
settings_impl: Option<Arc<dyn crate::backend::settings::SettingsImpl>>, |
| 35 |
#[cfg(feature = "wallpaper")] |
| 36 |
wallpaper_impl: Option<Arc<dyn crate::backend::wallpaper::WallpaperImpl>>, |
| 37 |
#[cfg(feature = "usb")] |
| 38 |
usb_impl: Option<Arc<dyn crate::backend::usb::UsbImpl>>, |
| 39 |
spawn: Option<Arc<dyn futures_util::task::Spawn + Send + Sync + 'static>>, |
| 40 |
name_lost: Option<Arc<dyn Fn() + Send + Sync + 'static>>, |
| 41 |
sessions: Arc<Mutex<SessionManager>>, |
| 42 |
} |
| 43 |
|
| 44 |
impl Builder { |
| 45 |
pub fn new<'a, W>(well_known_name: W) -> zbus::Result<Self> |
| 46 |
where |
| 47 |
W: TryInto<WellKnownName<'a>>, |
| 48 |
<W as TryInto<WellKnownName<'a>>>::Error: Into<zbus::Error>, |
| 49 |
{ |
| 50 |
let well_known_name = well_known_name.try_into().map_err(Into::into)?; |
| 51 |
Ok(Self { |
| 52 |
name: well_known_name.into(), |
| 53 |
// same flags as zbus::Connection::request_name |
| 54 |
flags: zbus::fdo::RequestNameFlags::ReplaceExisting |
| 55 |
| zbus::fdo::RequestNameFlags::DoNotQueue, |
| 56 |
#[cfg(feature = "account")] |
| 57 |
account_impl: None, |
| 58 |
access_impl: None, |
| 59 |
app_chooser_impl: None, |
| 60 |
#[cfg(feature = "background")] |
| 61 |
background_impl: None, |
| 62 |
#[cfg(feature = "email")] |
| 63 |
email_impl: None, |
| 64 |
#[cfg(feature = "file_chooser")] |
| 65 |
file_chooser_impl: None, |
| 66 |
lockdown_impl: None, |
| 67 |
|
| 68 |
#[cfg(feature = "documents")] |
| 69 |
permission_store_impl: None, |
| 70 |
#[cfg(feature = "print")] |
| 71 |
print_impl: None, |
| 72 |
#[cfg(feature = "screencast")] |
| 73 |
screencast_impl: None, |
| 74 |
#[cfg(feature = "screenshot")] |
| 75 |
screenshot_impl: None, |
| 76 |
#[cfg(feature = "secret")] |
| 77 |
secret_impl: None, |
| 78 |
#[cfg(feature = "settings")] |
| 79 |
settings_impl: None, |
| 80 |
#[cfg(feature = "wallpaper")] |
| 81 |
wallpaper_impl: None, |
| 82 |
#[cfg(feature = "usb")] |
| 83 |
usb_impl: None, |
| 84 |
spawn: None, |
| 85 |
name_lost: None, |
| 86 |
sessions: Arc::new(Mutex::new(SessionManager::default())), |
| 87 |
}) |
| 88 |
} |
| 89 |
|
| 90 |
pub fn with_flags(mut self, flags: BitFlags<zbus::fdo::RequestNameFlags>) -> Self { |
| 91 |
self.flags = flags; |
| 92 |
self |
| 93 |
} |
| 94 |
|
| 95 |
#[cfg(not(any(feature = "tokio")))] |
| 96 |
pub fn with_spawn( |
| 97 |
mut self, |
| 98 |
spawn: impl futures_util::task::Spawn + Send + Sync + 'static, |
| 99 |
) -> Self { |
| 100 |
self.spawn = Some(Arc::new(spawn)); |
| 101 |
self |
| 102 |
} |
| 103 |
|
| 104 |
pub fn with_name_lost(mut self, name_lost: impl Fn() + Send + Sync + 'static) -> Self { |
| 105 |
self.name_lost = Some(Arc::new(name_lost)); |
| 106 |
self |
| 107 |
} |
| 108 |
|
| 109 |
#[cfg(feature = "account")] |
| 110 |
pub fn account(mut self, imp: impl crate::backend::account::AccountImpl + 'static) -> Self { |
| 111 |
self.account_impl = Some(Arc::new(imp)); |
| 112 |
self |
| 113 |
} |
| 114 |
|
| 115 |
pub fn access(mut self, imp: impl crate::backend::access::AccessImpl + 'static) -> Self { |
| 116 |
self.access_impl = Some(Arc::new(imp)); |
| 117 |
self |
| 118 |
} |
| 119 |
|
| 120 |
pub fn app_chooser( |
| 121 |
mut self, |
| 122 |
imp: impl crate::backend::app_chooser::AppChooserImpl + 'static, |
| 123 |
) -> Self { |
| 124 |
self.app_chooser_impl = Some(Arc::new(imp)); |
| 125 |
self |
| 126 |
} |
| 127 |
|
| 128 |
#[cfg(feature = "background")] |
| 129 |
pub fn background( |
| 130 |
mut self, |
| 131 |
imp: impl crate::backend::background::BackgroundImpl + 'static, |
| 132 |
) -> Self { |
| 133 |
self.background_impl = Some(Arc::new(imp)); |
| 134 |
self |
| 135 |
} |
| 136 |
|
| 137 |
#[cfg(feature = "email")] |
| 138 |
pub fn email(mut self, imp: impl crate::backend::email::EmailImpl + 'static) -> Self { |
| 139 |
self.email_impl = Some(Arc::new(imp)); |
| 140 |
self |
| 141 |
} |
| 142 |
|
| 143 |
#[cfg(feature = "file_chooser")] |
| 144 |
pub fn file_chooser( |
| 145 |
mut self, |
| 146 |
imp: impl crate::backend::file_chooser::FileChooserImpl + 'static, |
| 147 |
) -> Self { |
| 148 |
self.file_chooser_impl = Some(Arc::new(imp)); |
| 149 |
self |
| 150 |
} |
| 151 |
|
| 152 |
pub fn lockdown(mut self, imp: impl crate::backend::lockdown::LockdownImpl + 'static) -> Self { |
| 153 |
self.lockdown_impl = Some(Arc::new(imp)); |
| 154 |
self |
| 155 |
} |
| 156 |
|
| 157 |
#[cfg(feature = "documents")] |
| 158 |
pub fn permission_store( |
| 159 |
mut self, |
| 160 |
imp: impl crate::backend::permission_store::PermissionStoreImpl + 'static, |
| 161 |
) -> Self { |
| 162 |
self.permission_store_impl = Some(Arc::new(imp)); |
| 163 |
self |
| 164 |
} |
| 165 |
|
| 166 |
#[cfg(feature = "print")] |
| 167 |
pub fn print(mut self, imp: impl crate::backend::print::PrintImpl + 'static) -> Self { |
| 168 |
self.print_impl = Some(Arc::new(imp)); |
| 169 |
self |
| 170 |
} |
| 171 |
|
| 172 |
#[cfg(feature = "screencast")] |
| 173 |
pub fn screencast( |
| 174 |
mut self, |
| 175 |
imp: impl crate::backend::screencast::ScreencastImpl + 'static, |
| 176 |
) -> Self { |
| 177 |
self.screencast_impl = Some(Arc::new(imp)); |
| 178 |
self |
| 179 |
} |
| 180 |
|
| 181 |
#[cfg(feature = "screenshot")] |
| 182 |
pub fn screenshot( |
| 183 |
mut self, |
| 184 |
imp: impl crate::backend::screenshot::ScreenshotImpl + 'static, |
| 185 |
) -> Self { |
| 186 |
self.screenshot_impl = Some(Arc::new(imp)); |
| 187 |
self |
| 188 |
} |
| 189 |
|
| 190 |
#[cfg(feature = "secret")] |
| 191 |
pub fn secret(mut self, imp: impl crate::backend::secret::SecretImpl + 'static) -> Self { |
| 192 |
self.secret_impl = Some(Arc::new(imp)); |
| 193 |
self |
| 194 |
} |
| 195 |
|
| 196 |
#[cfg(feature = "settings")] |
| 197 |
pub fn settings(mut self, imp: impl crate::backend::settings::SettingsImpl + 'static) -> Self { |
| 198 |
self.settings_impl = Some(Arc::new(imp)); |
| 199 |
self |
| 200 |
} |
| 201 |
|
| 202 |
#[cfg(feature = "wallpaper")] |
| 203 |
pub fn wallpaper( |
| 204 |
mut self, |
| 205 |
imp: impl crate::backend::wallpaper::WallpaperImpl + 'static, |
| 206 |
) -> Self { |
| 207 |
self.wallpaper_impl = Some(Arc::new(imp)); |
| 208 |
self |
| 209 |
} |
| 210 |
|
| 211 |
#[cfg(feature = "usb")] |
| 212 |
pub fn usb(mut self, imp: impl crate::backend::usb::UsbImpl + 'static) -> Self { |
| 213 |
self.usb_impl = Some(Arc::new(imp)); |
| 214 |
self |
| 215 |
} |
| 216 |
|
| 217 |
pub async fn build(self) -> Result<()> { |
| 218 |
let connection = crate::proxy::Proxy::connection().await?; |
| 219 |
self.build_with_connection(connection).await |
| 220 |
} |
| 221 |
|
| 222 |
pub async fn build_with_connection(self, connection: zbus::Connection) -> Result<()> { |
| 223 |
#[cfg(feature = "tokio")] |
| 224 |
let spawn = self.spawn.unwrap_or(Arc::new(super::spawn::TokioSpawner)); |
| 225 |
|
| 226 |
#[cfg(not(feature = "tokio"))] |
| 227 |
let spawn = self |
| 228 |
.spawn |
| 229 |
.expect("Must provide a spawner when not using tokio"); |
| 230 |
|
| 231 |
if let Some(name_lost) = self.name_lost { |
| 232 |
let proxy = zbus::fdo::DBusProxy::new(&connection).await?; |
| 233 |
let mut name_lost_stream = proxy.receive_name_lost().await?; |
| 234 |
if let Err(error) = spawn.spawn(async move { |
| 235 |
while (name_lost_stream.next().await).is_some() { |
| 236 |
name_lost(); |
| 237 |
} |
| 238 |
}) { |
| 239 |
return Err(crate::PortalError::Failed(error.to_string())); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
let object_server = connection.object_server(); |
| 244 |
#[cfg(feature = "account")] |
| 245 |
if let Some(imp) = self.account_impl { |
| 246 |
let portal = crate::backend::account::AccountInterface::new( |
| 247 |
imp, |
| 248 |
connection.clone(), |
| 249 |
Arc::clone(&spawn), |
| 250 |
); |
| 251 |
#[cfg(feature = "tracing")] |
| 252 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Account`"); |
| 253 |
object_server |
| 254 |
.at("/org/freedesktop/portal/desktop", portal) |
| 255 |
.await?; |
| 256 |
} |
| 257 |
|
| 258 |
if let Some(imp) = self.access_impl { |
| 259 |
let portal = crate::backend::access::AccessInterface::new( |
| 260 |
imp, |
| 261 |
connection.clone(), |
| 262 |
Arc::clone(&spawn), |
| 263 |
); |
| 264 |
#[cfg(feature = "tracing")] |
| 265 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Access`"); |
| 266 |
object_server |
| 267 |
.at("/org/freedesktop/portal/desktop", portal) |
| 268 |
.await?; |
| 269 |
} |
| 270 |
|
| 271 |
if let Some(imp) = self.app_chooser_impl { |
| 272 |
let portal = crate::backend::app_chooser::AppChooserInterface::new( |
| 273 |
imp, |
| 274 |
connection.clone(), |
| 275 |
Arc::clone(&spawn), |
| 276 |
); |
| 277 |
#[cfg(feature = "tracing")] |
| 278 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.AppChooser`"); |
| 279 |
object_server |
| 280 |
.at("/org/freedesktop/portal/desktop", portal) |
| 281 |
.await?; |
| 282 |
} |
| 283 |
|
| 284 |
#[cfg(feature = "background")] |
| 285 |
if let Some(imp) = self.background_impl { |
| 286 |
let portal = crate::backend::background::BackgroundInterface::new( |
| 287 |
imp, |
| 288 |
connection.clone(), |
| 289 |
Arc::clone(&spawn), |
| 290 |
); |
| 291 |
#[cfg(feature = "tracing")] |
| 292 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Background`"); |
| 293 |
object_server |
| 294 |
.at("/org/freedesktop/portal/desktop", portal) |
| 295 |
.await?; |
| 296 |
} |
| 297 |
|
| 298 |
#[cfg(feature = "email")] |
| 299 |
if let Some(imp) = self.email_impl { |
| 300 |
let portal = crate::backend::email::EmailInterface::new( |
| 301 |
imp, |
| 302 |
connection.clone(), |
| 303 |
Arc::clone(&spawn), |
| 304 |
); |
| 305 |
#[cfg(feature = "tracing")] |
| 306 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Email`"); |
| 307 |
object_server |
| 308 |
.at("/org/freedesktop/portal/desktop", portal) |
| 309 |
.await?; |
| 310 |
} |
| 311 |
|
| 312 |
#[cfg(feature = "file_chooser")] |
| 313 |
if let Some(imp) = self.file_chooser_impl { |
| 314 |
let portal = crate::backend::file_chooser::FileChooserInterface::new( |
| 315 |
imp, |
| 316 |
connection.clone(), |
| 317 |
Arc::clone(&spawn), |
| 318 |
); |
| 319 |
#[cfg(feature = "tracing")] |
| 320 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.FileChooser`"); |
| 321 |
object_server |
| 322 |
.at("/org/freedesktop/portal/desktop", portal) |
| 323 |
.await?; |
| 324 |
} |
| 325 |
|
| 326 |
if let Some(imp) = self.lockdown_impl { |
| 327 |
let portal = crate::backend::lockdown::LockdownInterface::new( |
| 328 |
imp, |
| 329 |
connection.clone(), |
| 330 |
Arc::clone(&spawn), |
| 331 |
); |
| 332 |
#[cfg(feature = "tracing")] |
| 333 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Lockdown`"); |
| 334 |
object_server |
| 335 |
.at("/org/freedesktop/portal/desktop", portal) |
| 336 |
.await?; |
| 337 |
} |
| 338 |
|
| 339 |
#[cfg(feature = "documents")] |
| 340 |
if let Some(imp) = self.permission_store_impl { |
| 341 |
let portal = crate::backend::permission_store::PermissionStoreInterface::new( |
| 342 |
imp, |
| 343 |
connection.clone(), |
| 344 |
Arc::clone(&spawn), |
| 345 |
); |
| 346 |
#[cfg(feature = "tracing")] |
| 347 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.PermissionStore`"); |
| 348 |
object_server |
| 349 |
.at("/org/freedesktop/portal/desktop", portal) |
| 350 |
.await?; |
| 351 |
} |
| 352 |
|
| 353 |
#[cfg(feature = "print")] |
| 354 |
if let Some(imp) = self.print_impl { |
| 355 |
let portal = crate::backend::print::PrintInterface::new( |
| 356 |
imp, |
| 357 |
connection.clone(), |
| 358 |
Arc::clone(&spawn), |
| 359 |
); |
| 360 |
#[cfg(feature = "tracing")] |
| 361 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Print`"); |
| 362 |
object_server |
| 363 |
.at("/org/freedesktop/portal/desktop", portal) |
| 364 |
.await?; |
| 365 |
} |
| 366 |
|
| 367 |
#[cfg(feature = "screencast")] |
| 368 |
if let Some(imp) = self.screencast_impl { |
| 369 |
let portal = crate::backend::screencast::ScreencastInterface::new( |
| 370 |
imp, |
| 371 |
connection.clone(), |
| 372 |
Arc::clone(&spawn), |
| 373 |
Arc::clone(&self.sessions), |
| 374 |
); |
| 375 |
#[cfg(feature = "tracing")] |
| 376 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.ScreenCast`"); |
| 377 |
object_server |
| 378 |
.at("/org/freedesktop/portal/desktop", portal) |
| 379 |
.await?; |
| 380 |
} |
| 381 |
|
| 382 |
#[cfg(feature = "screenshot")] |
| 383 |
if let Some(imp) = self.screenshot_impl { |
| 384 |
let portal = crate::backend::screenshot::ScreenshotInterface::new( |
| 385 |
imp, |
| 386 |
connection.clone(), |
| 387 |
Arc::clone(&spawn), |
| 388 |
); |
| 389 |
#[cfg(feature = "tracing")] |
| 390 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Screenshot`"); |
| 391 |
object_server |
| 392 |
.at("/org/freedesktop/portal/desktop", portal) |
| 393 |
.await?; |
| 394 |
} |
| 395 |
|
| 396 |
#[cfg(feature = "secret")] |
| 397 |
if let Some(imp) = self.secret_impl { |
| 398 |
let portal = crate::backend::secret::SecretInterface::new( |
| 399 |
imp, |
| 400 |
connection.clone(), |
| 401 |
Arc::clone(&spawn), |
| 402 |
); |
| 403 |
#[cfg(feature = "tracing")] |
| 404 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Secret`"); |
| 405 |
object_server |
| 406 |
.at("/org/freedesktop/portal/desktop", portal) |
| 407 |
.await?; |
| 408 |
} |
| 409 |
|
| 410 |
#[cfg(feature = "settings")] |
| 411 |
if let Some(imp) = self.settings_impl { |
| 412 |
let portal = crate::backend::settings::SettingsInterface::new( |
| 413 |
imp, |
| 414 |
connection.clone(), |
| 415 |
Arc::clone(&spawn), |
| 416 |
); |
| 417 |
#[cfg(feature = "tracing")] |
| 418 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Settings`"); |
| 419 |
object_server |
| 420 |
.at("/org/freedesktop/portal/desktop", portal) |
| 421 |
.await?; |
| 422 |
} |
| 423 |
|
| 424 |
#[cfg(feature = "wallpaper")] |
| 425 |
if let Some(imp) = self.wallpaper_impl { |
| 426 |
let portal = crate::backend::wallpaper::WallpaperInterface::new( |
| 427 |
imp, |
| 428 |
connection.clone(), |
| 429 |
Arc::clone(&spawn), |
| 430 |
); |
| 431 |
#[cfg(feature = "tracing")] |
| 432 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Wallpaper`"); |
| 433 |
object_server |
| 434 |
.at("/org/freedesktop/portal/desktop", portal) |
| 435 |
.await?; |
| 436 |
} |
| 437 |
|
| 438 |
#[cfg(feature = "usb")] |
| 439 |
if let Some(imp) = self.usb_impl { |
| 440 |
let portal = |
| 441 |
crate::backend::usb::UsbInterface::new(imp, connection.clone(), Arc::clone(&spawn)); |
| 442 |
#[cfg(feature = "tracing")] |
| 443 |
tracing::debug!("Serving interface `org.freedesktop.impl.portal.Usb`"); |
| 444 |
object_server |
| 445 |
.at("/org/freedesktop/portal/desktop", portal) |
| 446 |
.await?; |
| 447 |
} |
| 448 |
|
| 449 |
connection |
| 450 |
.request_name_with_flags(self.name, self.flags) |
| 451 |
.await?; |
| 452 |
|
| 453 |
connection.closed().await; |
| 454 |
|
| 455 |
Ok(()) |
| 456 |
} |
| 457 |
} |