Debian Codegraph @

src/backend/background.rs

raw
1 use std::{collections::HashMap, sync::Arc};
2
3 use async_trait::async_trait;
4 use enumflags2::{BitFlags, bitflags};
5 use serde::Serialize;
6 use serde_repr::{Deserialize_repr, Serialize_repr};
7
8 use crate::{
9 MaybeAppID, PortalError,
10 backend::request::{Request, RequestImpl},
11 desktop::{HandleToken, Response},
12 zbus::object_server::SignalEmitter,
13 zvariant::{OwnedObjectPath, Type, as_value},
14 };
15
16 #[derive(Serialize_repr, Deserialize_repr, Copy, Clone, PartialEq, Eq, Debug, Type)]
17 #[repr(u32)]
18 pub enum Activity {
19 Forbid = 0,
20 Allow = 1,
21 AllowInstance = 2,
22 }
23
24 #[derive(Debug, Serialize, Type)]
25 #[zvariant(signature = "dict")]
26 pub struct Background {
27 #[serde(with = "as_value")]
28 result: Activity,
29 }
30
31 impl Background {
32 pub fn new(activity: Activity) -> Self {
33 Self { result: activity }
34 }
35 }
36
37 #[derive(Serialize_repr, Copy, Clone, PartialEq, Eq, Debug, Type)]
38 #[repr(u32)]
39 pub enum AppState {
40 Background = 0,
41 Running = 1,
42 Active = 2,
43 }
44
45 #[bitflags]
46 #[derive(Deserialize_repr, PartialEq, Eq, Copy, Clone, Debug, Type)]
47 #[repr(u32)]
48 pub enum AutoStartFlags {
49 DBusActivation = 1,
50 }
51
52 #[async_trait]
53 pub trait BackgroundSignalEmitter: Send + Sync {
54 #[doc(alias = "RunningApplicationsChanged")]
55 async fn emit_changed(&self) -> zbus::Result<()>;
56 }
57
58 #[async_trait]
59 pub trait BackgroundImpl: RequestImpl {
60 #[doc(alias = "GetAppState")]
61 async fn get_app_state(&self) -> Result<HashMap<MaybeAppID, AppState>, PortalError>;
62
63 #[doc(alias = "NotifyBackground")]
64 async fn notify_background(
65 &self,
66 token: HandleToken,
67 app_id: MaybeAppID,
68 name: &str,
69 ) -> Result<Background, PortalError>;
70
71 #[doc(alias = "EnableAutostart")]
72 async fn enable_autostart(
73 &self,
74 app_id: MaybeAppID,
75 enable: bool,
76 commandline: Vec<String>,
77 flags: BitFlags<AutoStartFlags>,
78 ) -> Result<bool, PortalError>;
79
80 // Set the signal emitter, allowing to notify of changes.
81 fn set_signal_emitter(&mut self, signal_emitter: Arc<dyn BackgroundSignalEmitter>);
82 }
83
84 pub(crate) struct BackgroundInterface {
85 imp: Arc<dyn BackgroundImpl>,
86 spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>,
87 cnx: zbus::Connection,
88 }
89
90 impl BackgroundInterface {
91 pub fn new(
92 imp: Arc<dyn BackgroundImpl>,
93 cnx: zbus::Connection,
94 spawn: Arc<dyn futures_util::task::Spawn + Send + Sync>,
95 ) -> Self {
96 Self { imp, cnx, spawn }
97 }
98
99 pub async fn changed(&self) -> zbus::Result<()> {
100 let object_server = self.cnx.object_server();
101 let iface_ref = object_server
102 .interface::<_, Self>(crate::proxy::DESKTOP_PATH)
103 .await?;
104 Self::running_applications_changed(iface_ref.signal_emitter()).await
105 }
106 }
107
108 #[async_trait]
109 impl BackgroundSignalEmitter for BackgroundInterface {
110 async fn emit_changed(&self) -> zbus::Result<()> {
111 self.changed().await
112 }
113 }
114
115 #[zbus::interface(name = "org.freedesktop.impl.portal.Background")]
116 impl BackgroundInterface {
117 #[zbus(property(emits_changed_signal = "const"), name = "version")]
118 fn version(&self) -> u32 {
119 2
120 }
121
122 #[zbus(out_args("apps"))]
123 async fn get_app_state(&self) -> Result<HashMap<MaybeAppID, AppState>, PortalError> {
124 #[cfg(feature = "tracing")]
125 tracing::debug!("Background::GetAppState");
126
127 let response = self.imp.get_app_state().await;
128
129 #[cfg(feature = "tracing")]
130 tracing::debug!("Background::GetAppState returned {:#?}", response);
131 response
132 }
133
134 #[zbus(out_args("response", "results"))]
135 async fn notify_background(
136 &self,
137 handle: OwnedObjectPath,
138 app_id: MaybeAppID,
139 name: String,
140 ) -> Result<Response<Background>, PortalError> {
141 let imp = Arc::clone(&self.imp);
142
143 Request::spawn(
144 "Background::NotifyBackground",
145 &self.cnx,
146 handle.clone(),
147 Arc::clone(&self.imp),
148 Arc::clone(&self.spawn),
149 async move {
150 imp.notify_background(HandleToken::try_from(&handle).unwrap(), app_id, &name)
151 .await
152 },
153 )
154 .await
155 }
156
157 #[zbus(out_args("result"))]
158 async fn enable_autostart(
159 &self,
160 app_id: MaybeAppID,
161 enable: bool,
162 commandline: Vec<String>,
163 flags: BitFlags<AutoStartFlags>,
164 ) -> Result<bool, PortalError> {
165 #[cfg(feature = "tracing")]
166 tracing::debug!("Background::EnableAutostart");
167
168 let response = self
169 .imp
170 .enable_autostart(app_id, enable, commandline, flags)
171 .await;
172
173 #[cfg(feature = "tracing")]
174 tracing::debug!("Background::EnableAutostart returned {:#?}", response);
175 response
176 }
177
178 #[zbus(signal)]
179 async fn running_applications_changed(signal_ctxt: &SignalEmitter<'_>) -> zbus::Result<()>;
180 }