hypersig_desktop/
device.rs1use wry::{PermissionKind, PermissionResponse};
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9#[non_exhaustive]
10pub enum Device {
11 Microphone,
13 Camera,
15 Screen,
23}
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27pub(crate) struct Devices {
28 microphone: bool,
29 camera: bool,
30 screen: bool,
31}
32
33impl Devices {
34 pub(crate) const NONE: Self = Self {
36 microphone: false,
37 camera: false,
38 screen: false,
39 };
40
41 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 #[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 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}