ashpd 0.13.0-alpha

XDG portals wrapper in Rust using zbus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(rustdoc::broken_intra_doc_links)]
#![deny(missing_docs)]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/bilelmoussaoui/ashpd/main/ashpd-demo/data/icons/com.belmoussaoui.ashpd.demo.svg",
    html_favicon_url = "https://raw.githubusercontent.com/bilelmoussaoui/ashpd/main/ashpd-demo/data/icons/com.belmoussaoui.ashpd.demo-symbolic.svg"
)]
#![doc = include_str!("../README.md")]
#[cfg(all(all(feature = "tokio", feature = "async-io"), not(doc)))]
compile_error!("You can't enable both async-io & tokio features at once");
#[cfg(all(not(feature = "tokio"), not(feature = "async-io"), not(doc)))]
compile_error!("Either the `async-io` or the `tokio` feature has to be enabled");

/// Alias for a [`Result`] with the error type `ashpd::Error`.
pub type Result<T> = std::result::Result<T, Error>;

static IS_SANDBOXED: OnceLock<bool> = OnceLock::new();

mod activation_token;
/// Interact with the user's desktop such as taking a screenshot, setting a
/// background or querying the user's location.
pub mod desktop;
/// Interact with the documents store or transfer files across apps.
#[cfg(feature = "documents")]
#[cfg_attr(docsrs, doc(cfg(feature = "documents")))]
pub mod documents;
mod error;
mod window_identifier;

pub use self::{activation_token::ActivationToken, window_identifier::WindowIdentifier};
mod app_id;
mod registry;
pub use self::{
    app_id::AppID,
    registry::{register_host_app, register_host_app_with_connection},
};
mod file_path;
pub use self::file_path::FilePath;

mod proxy;

#[cfg(any(
    feature = "backend_access",
    feature = "backend_account",
    feature = "backend_app_chooser",
    feature = "backend_background",
    feature = "backend_email",
    feature = "backend_file_chooser",
    feature = "backend_lockdown",
    feature = "backend_permission_store",
    feature = "backend_print",
    feature = "backend_screencast",
    feature = "backend_screenshot",
    feature = "backend_secret",
    feature = "backend_settings",
    feature = "backend_usb",
    feature = "backend_wallpaper"
))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(
        feature = "backend_access",
        feature = "backend_account",
        feature = "backend_app_chooser",
        feature = "backend_background",
        feature = "backend_email",
        feature = "backend_file_chooser",
        feature = "backend_lockdown",
        feature = "backend_permission_store",
        feature = "backend_print",
        feature = "backend_screencast",
        feature = "backend_screenshot",
        feature = "backend_secret",
        feature = "backend_settings",
        feature = "backend_usb",
        feature = "backend_wallpaper"
    )))
)]
pub use self::window_identifier::WindowIdentifierType;
#[cfg(any(
    feature = "backend_access",
    feature = "backend_account",
    feature = "backend_app_chooser",
    feature = "backend_background",
    feature = "backend_email",
    feature = "backend_file_chooser",
    feature = "backend_lockdown",
    feature = "backend_permission_store",
    feature = "backend_print",
    feature = "backend_screencast",
    feature = "backend_screenshot",
    feature = "backend_secret",
    feature = "backend_settings",
    feature = "backend_usb",
    feature = "backend_wallpaper"
))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(
        feature = "backend_access",
        feature = "backend_account",
        feature = "backend_app_chooser",
        feature = "backend_background",
        feature = "backend_email",
        feature = "backend_file_chooser",
        feature = "backend_lockdown",
        feature = "backend_permission_store",
        feature = "backend_print",
        feature = "backend_screencast",
        feature = "backend_screenshot",
        feature = "backend_secret",
        feature = "backend_settings",
        feature = "backend_usb",
        feature = "backend_wallpaper"
    )))
)]
#[allow(missing_docs)]
/// Build your custom portals backend.
pub mod backend;
/// Spawn commands outside the sandbox or monitor if the running application has
/// received an update & install it.
#[cfg(feature = "flatpak")]
#[cfg_attr(docsrs, doc(cfg(feature = "flatpak")))]
pub mod flatpak;
mod helpers;
use std::sync::OnceLock;

#[cfg(any(
    feature = "backend_access",
    feature = "backend_account",
    feature = "backend_app_chooser",
    feature = "backend_background",
    feature = "backend_email",
    feature = "backend_file_chooser",
    feature = "backend_lockdown",
    feature = "backend_permission_store",
    feature = "backend_print",
    feature = "backend_screencast",
    feature = "backend_screenshot",
    feature = "backend_secret",
    feature = "backend_settings",
    feature = "backend_usb",
    feature = "backend_wallpaper"
))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(
        feature = "backend_access",
        feature = "backend_account",
        feature = "backend_app_chooser",
        feature = "backend_background",
        feature = "backend_email",
        feature = "backend_file_chooser",
        feature = "backend_lockdown",
        feature = "backend_permission_store",
        feature = "backend_print",
        feature = "backend_screencast",
        feature = "backend_screenshot",
        feature = "backend_secret",
        feature = "backend_settings",
        feature = "backend_usb",
        feature = "backend_wallpaper"
    )))
)]
pub use async_trait;
pub use enumflags2;
pub use zbus::{self, zvariant};

/// Check whether the application is running inside a sandbox.
///
/// The function checks whether the file `/.flatpak-info` exists, or if the app
/// is running as a snap, or if the environment variable `GTK_USE_PORTAL` is set
/// to `1`. As the return value of this function will not change during the
/// runtime of a program; it is cached for future calls.
pub fn is_sandboxed() -> bool {
    if let Some(cached_value) = IS_SANDBOXED.get() {
        return *cached_value;
    }
    let new_value = crate::helpers::is_flatpak() || crate::helpers::is_snap();

    *IS_SANDBOXED.get_or_init(|| new_value)
}

pub use self::error::{Error, PortalError};

mod sealed {
    /// Use as a supertrait for public traits that users should not be able to
    /// implement
    pub trait Sealed {}
}

pub(crate) use sealed::Sealed;

/// Process ID.
///
/// Matches the type used in std.
pub type Pid = u32;

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, fs, path::PathBuf};

    use quick_xml::{Reader, events::Event};

    // Helper to convert PascalCase to snake_case
    fn pascal_to_snake_case(s: &str) -> String {
        let mut result = String::new();
        for (i, c) in s.chars().enumerate() {
            if c.is_ascii_uppercase() {
                if i > 0 {
                    result.push('_');
                }
                result.push(c.to_ascii_lowercase());
            } else {
                result.push(c);
            }
        }
        result
    }

    fn extract_names_from_xml(xml_content: &str) -> HashMap<String, Vec<String>> {
        let mut interfaces = HashMap::new();
        let mut reader = Reader::from_str(xml_content);
        reader.config_mut().trim_text(true);
        let mut buf = Vec::new();
        let mut current_interface_name = String::new();
        let mut current_names = Vec::new();

        loop {
            match reader.read_event_into(&mut buf) {
                Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
                Ok(Event::Eof) => break,
                Ok(Event::Start(e)) | Ok(Event::Empty(e)) => {
                    // Handle both Start and Empty events
                    if let Some(Ok(attr)) = e
                        .attributes()
                        .find(|a| a.as_ref().map_or(false, |a| a.key.as_ref() == b"name"))
                    {
                        if let Ok(value) = attr.decode_and_unescape_value(reader.decoder()) {
                            match e.name().as_ref() {
                                b"interface" => {
                                    current_interface_name = value.to_string();
                                    current_names.clear();
                                }
                                b"method" | b"property" | b"signal" => {
                                    if value != "version" {
                                        current_names.push(value.to_string());
                                    }
                                }
                                _ => (),
                            }
                        }
                    }
                }
                Ok(Event::End(e)) => {
                    if e.name().as_ref() == b"interface" {
                        interfaces.insert(current_interface_name.clone(), current_names.clone());
                    }
                }
                _ => (),
            }
            buf.clear();
        }
        interfaces
    }

    struct TestConfig {
        interfaces_dir: PathBuf,
        rust_src_prefix: &'static str,
        rust_file_mappings: HashMap<&'static str, &'static str>,
        ignored_interfaces: &'static [&'static str],
        interface_prefix: &'static str,
    }

    fn check_doc_aliases_for_config(config: TestConfig) {
        assert!(
            config.interfaces_dir.exists(),
            "Interfaces directory not found at {}",
            config.interfaces_dir.display()
        );

        let entries =
            fs::read_dir(&config.interfaces_dir).expect("Failed to read interfaces directory");

        for entry in entries.filter_map(|e| e.ok()) {
            let path = entry.path();
            if path.is_file() && path.extension().map_or(false, |ext| ext == "xml") {
                println!("Checking XML file: {}", path.display());

                let xml_content = fs::read_to_string(&path).unwrap();
                let interfaces = extract_names_from_xml(&xml_content);

                for (interface_name, names_to_check) in interfaces {
                    // Map the D-Bus interface name to the corresponding Rust file path
                    let interface_name_suffix = interface_name
                        .strip_prefix(config.interface_prefix)
                        .expect("Interface name does not have the expected prefix.");

                    if config.ignored_interfaces.contains(&interface_name_suffix) {
                        continue;
                    }

                    let rust_path = if let Some(mapped_path) =
                        config.rust_file_mappings.get(interface_name.as_str())
                    {
                        PathBuf::from(mapped_path)
                    } else {
                        let rust_file_name_snake = pascal_to_snake_case(interface_name_suffix);
                        PathBuf::from(format!(
                            "{}{}.rs",
                            config.rust_src_prefix, rust_file_name_snake
                        ))
                    };

                    // Check if the Rust file exists
                    assert!(
                        rust_path.exists(),
                        "Corresponding Rust file not found for interface '{}' at {}",
                        interface_name,
                        rust_path.display()
                    );

                    // Read the Rust file content
                    let rust_content = fs::read_to_string(&rust_path).unwrap();

                    // Assert that each name has a corresponding doc alias
                    for name in &names_to_check {
                        let alias_str = format!("#[doc(alias = \"{}\")]", name);
                        assert!(
                            rust_content.contains(&alias_str),
                            "Missing doc alias '{}' for interface '{}' in file {}",
                            alias_str,
                            interface_name,
                            rust_path.display()
                        );
                    }
                }
            }
        }
    }

    #[cfg(feature = "backend")]
    #[test]
    fn all_interfaces_have_backend_implementations() {
        let rust_file_mappings: HashMap<&str, &str> = HashMap::from([(
            "org.freedesktop.impl.portal.ScreenCast",
            "src/backend/screencast.rs",
        )]);

        const IGNORED_BACKEND_PORTALS: &[&str; 7] = &[
            "Clipboard",
            "DynamicLauncher",
            "GlobalShortcuts",
            "Inhibit",
            "InputCapture",
            "Notification",
            "RemoteDesktop",
        ];

        let config = TestConfig {
            interfaces_dir: PathBuf::from("./../interfaces/backend"),
            rust_src_prefix: "src/backend/",
            rust_file_mappings,
            ignored_interfaces: IGNORED_BACKEND_PORTALS,
            interface_prefix: "org.freedesktop.impl.portal.",
        };

        check_doc_aliases_for_config(config);
    }

    #[test]
    fn all_interfaces_have_implementations() {
        let rust_file_mappings: HashMap<&str, &str> = HashMap::from([
            (
                "org.freedesktop.portal.ScreenCast",
                "src/desktop/screencast.rs",
            ),
            ("org.freedesktop.portal.OpenURI", "src/desktop/open_uri.rs"),
            (
                "org.freedesktop.portal.FileTransfer",
                "src/documents/file_transfer.rs",
            ),
            ("org.freedesktop.portal.Documents", "src/documents/mod.rs"),
            ("org.freedesktop.portal.Flatpak", "src/flatpak/mod.rs"),
            (
                "org.freedesktop.portal.Flatpak.UpdateMonitor",
                "src/flatpak/update_monitor.rs",
            ),
        ]);

        const NO_IGNORED_INTERFACES: &[&str; 0] = &[];

        let config = TestConfig {
            interfaces_dir: PathBuf::from("./../interfaces"),
            rust_src_prefix: "src/desktop/",
            rust_file_mappings,
            ignored_interfaces: NO_IGNORED_INTERFACES,
            interface_prefix: "org.freedesktop.portal.",
        };

        check_doc_aliases_for_config(config);
    }
}