euv_core/system/permissions/
impl.rs1use super::*;
2
3impl SystemPermission {
5 #[must_use]
16 pub const fn native_key(&self, platform: PlatformKind) -> &'static str {
17 match platform {
18 PlatformKind::MacOs | PlatformKind::Ios | PlatformKind::IpadOs => match *self {
19 Self::Camera => "NSCameraUsageDescription",
20 Self::Microphone => "NSMicrophoneUsageDescription",
21 Self::LocationWhenInUse => "NSLocationWhenInUseUsageDescription",
22 Self::LocationAlways => "NSLocationAlwaysAndWhenInUseUsageDescription",
23 Self::Notifications => "UNUserNotificationCenter.requestAuthorization",
24 Self::Photos => "NSPhotoLibraryUsageDescription",
25 Self::Contacts => "NSContactsUsageDescription",
26 Self::Calendar => "NSCalendarsUsageDescription",
27 Self::Bluetooth => "NSBluetoothAlwaysUsageDescription",
28 Self::Biometrics => "NSFaceIDUsageDescription",
29 Self::FileStorage => "NSDocumentsFolderUsageDescription",
30 Self::ClipboardRead => "UIPasteboard.general",
31 Self::MotionSensors => "NSMotionUsageDescription",
32 Self::LocalNetwork => "NSLocalNetworkUsageDescription",
33 },
34 PlatformKind::Android => match *self {
35 Self::Camera => "android.permission.CAMERA",
36 Self::Microphone => "android.permission.RECORD_AUDIO",
37 Self::LocationWhenInUse => "android.permission.ACCESS_FINE_LOCATION",
38 Self::LocationAlways => "android.permission.ACCESS_BACKGROUND_LOCATION",
39 Self::Notifications => "android.permission.POST_NOTIFICATIONS",
40 Self::Photos => "android.permission.READ_MEDIA_IMAGES",
41 Self::Contacts => "android.permission.READ_CONTACTS",
42 Self::Calendar => "android.permission.READ_CALENDAR",
43 Self::Bluetooth => "android.permission.BLUETOOTH_CONNECT",
44 Self::Biometrics => "android.permission.USE_BIOMETRIC",
45 Self::FileStorage => "android.permission.READ_EXTERNAL_STORAGE",
46 Self::ClipboardRead => "android.content.ClipboardManager",
47 Self::MotionSensors => "android.permission.BODY_SENSORS",
48 Self::LocalNetwork => "android.permission.NEARBY_WIFI_DEVICES",
49 },
50 PlatformKind::Windows => match *self {
51 Self::Camera => "winrt.capability.webcam",
52 Self::Microphone => "winrt.capability.microphone",
53 Self::LocationWhenInUse | Self::LocationAlways => "winrt.capability.location",
54 Self::Notifications => "winrt.capability.userNotificationListener",
55 Self::Photos => "winrt.capability.picturesLibrary",
56 Self::Contacts => "winrt.capability.contacts",
57 Self::Calendar => "winrt.capability.appointments",
58 Self::Bluetooth => "winrt.capability.bluetooth",
59 Self::Biometrics => "Windows.Security.Credentials.UI.UserConsentVerifier",
60 Self::FileStorage => "winrt.capability.broadFileSystemAccess",
61 Self::ClipboardRead => "Windows.ApplicationModel.DataTransfer.Clipboard",
62 Self::MotionSensors => "winrt.capability.activity",
63 Self::LocalNetwork => "winrt.capability.privateNetworkClientServer",
64 },
65 PlatformKind::Linux => match *self {
66 Self::Camera => "org.freedesktop.portal.Camera",
67 Self::Microphone => "org.freedesktop.portal.GlobalShortcuts",
68 Self::LocationWhenInUse | Self::LocationAlways => "org.freedesktop.portal.Location",
69 Self::Notifications => "org.freedesktop.portal.Notification",
70 Self::Photos | Self::FileStorage => "org.freedesktop.portal.FileChooser",
71 Self::Contacts | Self::Calendar => "org.freedesktop.portal.Secret",
72 Self::Bluetooth => "org.bluez.Adapter1",
73 Self::Biometrics => "net.reactivated.Fprint.Device",
74 Self::ClipboardRead => "org.freedesktop.portal.Clipboard",
75 Self::MotionSensors => "net.hadess.SensorProxy",
76 Self::LocalNetwork => "org.freedesktop.Avahi",
77 },
78 PlatformKind::Web | PlatformKind::Headless => match *self {
79 Self::Camera => "camera",
80 Self::Microphone => "microphone",
81 Self::LocationWhenInUse | Self::LocationAlways => "geolocation",
82 Self::Notifications => "notifications",
83 Self::Photos | Self::FileStorage => "persistent-storage",
84 Self::Contacts => "contacts",
85 Self::Calendar => "calendar",
86 Self::Bluetooth => "bluetooth",
87 Self::Biometrics => "publickey-credentials-get",
88 Self::ClipboardRead => "clipboard-read",
89 Self::MotionSensors => "accelerometer",
90 Self::LocalNetwork => "local-network-access",
91 },
92 }
93 }
94
95 #[must_use]
106 pub const fn is_supported_on(&self, platform: PlatformKind) -> bool {
107 match platform {
108 PlatformKind::Headless => !matches!(*self, Self::MotionSensors),
109 PlatformKind::Linux => !matches!(*self, Self::MotionSensors),
110 PlatformKind::Web => !matches!(*self, Self::LocationAlways | Self::Calendar),
111 PlatformKind::MacOs
112 | PlatformKind::Windows
113 | PlatformKind::Ios
114 | PlatformKind::IpadOs
115 | PlatformKind::Android => true,
116 }
117 }
118}
119
120impl PermissionManager {
122 #[must_use]
134 pub fn check(&self, permission: SystemPermission, platform: PlatformKind) -> PermissionStatus {
135 if !permission.is_supported_on(platform) {
136 return PermissionStatus::Unsupported;
137 }
138 self.get_statuses()
139 .get(&permission)
140 .copied()
141 .unwrap_or(PermissionStatus::NotDetermined)
142 }
143
144 pub fn set_status(&mut self, permission: SystemPermission, status: PermissionStatus) {
152 let _: Option<PermissionStatus> = self.get_mut_statuses().insert(permission, status);
153 }
154
155 pub fn request(
168 &mut self,
169 permission: SystemPermission,
170 rationale: String,
171 platform: PlatformKind,
172 ) -> PermissionDescriptor {
173 let native_key: String = permission.native_key(platform).to_string();
174 let _: bool = dispatch_native_action(NATIVE_ACTION_PERMISSION, &native_key, &rationale);
175 let _: Option<String> = self
176 .get_mut_rationales()
177 .insert(permission, rationale.clone());
178 let status: PermissionStatus = if !permission.is_supported_on(platform) {
179 PermissionStatus::Unsupported
180 } else {
181 let current: PermissionStatus = self.check(permission, platform);
182 if current == PermissionStatus::NotDetermined {
183 self.set_status(permission, PermissionStatus::Granted);
184 PermissionStatus::Granted
185 } else {
186 current
187 }
188 };
189 PermissionDescriptor::new(permission, platform, native_key, rationale, status)
190 }
191}