Skip to main content

hypersig_desktop/
device.rs

1//! The capture devices a page may use through `getUserMedia` and
2//! `getDisplayMedia`, which the
3//! application names with [`App::allow`](crate::App::allow).
4
5use wry::{PermissionKind, PermissionResponse};
6
7/// A capture device the pages may use, for [`App::allow`](crate::App::allow).
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10pub enum Device {
11    /// The microphone, for `getUserMedia({ audio: true })`.
12    Microphone,
13    /// The camera, for `getUserMedia({ video: true })`.
14    Camera,
15    /// The screen, for `getDisplayMedia`, as sharing it in a call does.
16    ///
17    /// Only `WebKitGTK`, on Linux, asks the application about it, and shares
18    /// the screen from a click of the person's alone. `WebView2`, on Windows,
19    /// answers `getDisplayMedia` on its own, whether or not the application
20    /// names the screen. `WKWebView`, on macOS, does too: a click there shows
21    /// the system's picker of what to share.
22    Screen,
23}
24
25/// The devices an application allows, none by default.
26#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub(crate) struct Devices {
28    microphone: bool,
29    camera: bool,
30    screen: bool,
31}
32
33impl Devices {
34    /// No device.
35    pub(crate) const NONE: Self = Self {
36        microphone: false,
37        camera: false,
38        screen: false,
39    };
40
41    /// These devices and `device`.
42    pub(crate) const fn with(mut self, device: Device) -> Self {
43        match device {
44            Device::Microphone => self.microphone = true,
45            Device::Camera => self.camera = true,
46            Device::Screen => self.screen = true,
47        }
48        self
49    }
50
51    /// Whether any device is allowed.
52    #[cfg_attr(
53        all(target_os = "macos", not(test)),
54        expect(
55            dead_code,
56            reason = "only WebKitGTK and a smoke run's WebView2 read it"
57        )
58    )]
59    pub(crate) const fn any(self) -> bool {
60        self.microphone || self.camera || self.screen
61    }
62
63    /// The web view's answer to a page asking for `kind`. A device the
64    /// application did not allow is refused without asking the person, and
65    /// a request for anything else gets the web view's own answer.
66    pub(crate) fn answer(self, kind: PermissionKind) -> PermissionResponse {
67        let allowed = match kind {
68            PermissionKind::Microphone => self.microphone,
69            PermissionKind::Camera => self.camera,
70            PermissionKind::DisplayCapture => self.screen,
71            _ => return PermissionResponse::Default,
72        };
73        if allowed {
74            PermissionResponse::Allow
75        } else {
76            PermissionResponse::Deny
77        }
78    }
79}
80
81#[cfg(test)]
82mod tests {
83    use wry::{PermissionKind, PermissionResponse};
84
85    use super::{Device, Devices};
86
87    #[test]
88    fn only_the_devices_allowed_are_granted() {
89        let devices = Devices::NONE.with(Device::Microphone);
90        assert!(devices.any());
91        assert!(!Devices::NONE.any());
92        assert_eq!(
93            devices.answer(PermissionKind::Microphone),
94            PermissionResponse::Allow
95        );
96        assert_eq!(
97            devices.answer(PermissionKind::Camera),
98            PermissionResponse::Deny
99        );
100        assert_eq!(
101            devices.answer(PermissionKind::DisplayCapture),
102            PermissionResponse::Deny
103        );
104        assert_eq!(
105            Devices::NONE
106                .with(Device::Screen)
107                .answer(PermissionKind::DisplayCapture),
108            PermissionResponse::Allow
109        );
110        assert_eq!(
111            devices.answer(PermissionKind::Geolocation),
112            PermissionResponse::Default
113        );
114    }
115}