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 | PlatformKind::Wearable => 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::HarmonyOs => match *self {
51 Self::Camera => PERM_OHOS_CAMERA,
52 Self::Microphone => PERM_OHOS_MICROPHONE,
53 Self::LocationWhenInUse => PERM_OHOS_LOCATION,
54 Self::LocationAlways => PERM_OHOS_LOCATION_BG,
55 Self::Notifications => PERM_OHOS_NOTIFICATIONS,
56 Self::Photos => PERM_OHOS_PHOTOS,
57 Self::Contacts => PERM_OHOS_CONTACTS,
58 Self::Calendar => PERM_OHOS_CALENDAR,
59 Self::Bluetooth => PERM_OHOS_BLUETOOTH,
60 Self::Biometrics => PERM_OHOS_BIOMETRICS,
61 Self::FileStorage => PERM_OHOS_STORAGE,
62 Self::ClipboardRead => PERM_OHOS_CLIPBOARD,
63 Self::MotionSensors => PERM_OHOS_SENSORS,
64 Self::LocalNetwork => PERM_OHOS_LOCAL_NET,
65 },
66 PlatformKind::MiniApp => match *self {
67 Self::Camera => PERM_MINI_CAMERA,
68 Self::Microphone => PERM_MINI_RECORD,
69 Self::LocationWhenInUse => PERM_MINI_LOCATION,
70 Self::LocationAlways => PERM_MINI_LOCATION_BG,
71 Self::Photos => PERM_MINI_ALBUM,
72 Self::Bluetooth => PERM_MINI_BLUETOOTH,
73 _ => PERM_MINI_SANDBOX,
74 },
75 PlatformKind::QuickApp => PERM_QUICKAPP_FEATURE,
76 PlatformKind::Terminal | PlatformKind::TerminalCli => PERM_TERMINAL_NONE,
77 PlatformKind::Windows => match *self {
78 Self::Camera => "winrt.capability.webcam",
79 Self::Microphone => "winrt.capability.microphone",
80 Self::LocationWhenInUse | Self::LocationAlways => "winrt.capability.location",
81 Self::Notifications => "winrt.capability.userNotificationListener",
82 Self::Photos => "winrt.capability.picturesLibrary",
83 Self::Contacts => "winrt.capability.contacts",
84 Self::Calendar => "winrt.capability.appointments",
85 Self::Bluetooth => "winrt.capability.bluetooth",
86 Self::Biometrics => "Windows.Security.Credentials.UI.UserConsentVerifier",
87 Self::FileStorage => "winrt.capability.broadFileSystemAccess",
88 Self::ClipboardRead => "Windows.ApplicationModel.DataTransfer.Clipboard",
89 Self::MotionSensors => "winrt.capability.activity",
90 Self::LocalNetwork => "winrt.capability.privateNetworkClientServer",
91 },
92 PlatformKind::Linux => match *self {
93 Self::Camera => "org.freedesktop.portal.Camera",
94 Self::Microphone => "org.freedesktop.portal.GlobalShortcuts",
95 Self::LocationWhenInUse | Self::LocationAlways => "org.freedesktop.portal.Location",
96 Self::Notifications => "org.freedesktop.portal.Notification",
97 Self::Photos | Self::FileStorage => "org.freedesktop.portal.FileChooser",
98 Self::Contacts | Self::Calendar => "org.freedesktop.portal.Secret",
99 Self::Bluetooth => "org.bluez.Adapter1",
100 Self::Biometrics => "net.reactivated.Fprint.Device",
101 Self::ClipboardRead => "org.freedesktop.portal.Clipboard",
102 Self::MotionSensors => "net.hadess.SensorProxy",
103 Self::LocalNetwork => "org.freedesktop.Avahi",
104 },
105 PlatformKind::Web | PlatformKind::Headless => match *self {
106 Self::Camera => "camera",
107 Self::Microphone => "microphone",
108 Self::LocationWhenInUse | Self::LocationAlways => "geolocation",
109 Self::Notifications => "notifications",
110 Self::Photos | Self::FileStorage => "persistent-storage",
111 Self::Contacts => "contacts",
112 Self::Calendar => "calendar",
113 Self::Bluetooth => "bluetooth",
114 Self::Biometrics => "publickey-credentials-get",
115 Self::ClipboardRead => "clipboard-read",
116 Self::MotionSensors => "accelerometer",
117 Self::LocalNetwork => "local-network-access",
118 },
119 }
120 }
121
122 #[must_use]
133 pub const fn is_supported_on(&self, platform: PlatformKind) -> bool {
134 match platform {
135 PlatformKind::Headless => !matches!(*self, Self::MotionSensors),
136 PlatformKind::Linux => !matches!(*self, Self::MotionSensors),
137 PlatformKind::Web => !matches!(*self, Self::LocationAlways | Self::Calendar),
138 PlatformKind::Terminal | PlatformKind::TerminalCli => {
139 matches!(
140 *self,
141 Self::FileStorage | Self::ClipboardRead | Self::LocalNetwork
142 )
143 }
144 PlatformKind::MiniApp | PlatformKind::QuickApp => !matches!(
145 *self,
146 Self::Contacts | Self::Calendar | Self::Biometrics | Self::LocalNetwork
147 ),
148 PlatformKind::Wearable => {
149 !matches!(*self, Self::Camera | Self::Photos | Self::FileStorage)
150 }
151 PlatformKind::MacOs
152 | PlatformKind::Windows
153 | PlatformKind::Ios
154 | PlatformKind::IpadOs
155 | PlatformKind::Android
156 | PlatformKind::HarmonyOs => true,
157 }
158 }
159}
160
161impl PermissionManager {
163 #[must_use]
175 pub fn check(&self, permission: SystemPermission, platform: PlatformKind) -> PermissionStatus {
176 if !permission.is_supported_on(platform) {
177 return PermissionStatus::Unsupported;
178 }
179 self.get_statuses()
180 .get(&permission)
181 .copied()
182 .unwrap_or(PermissionStatus::NotDetermined)
183 }
184
185 pub fn set_status(&mut self, permission: SystemPermission, status: PermissionStatus) {
193 let _: Option<PermissionStatus> = self.get_mut_statuses().insert(permission, status);
194 }
195
196 pub fn request(
209 &mut self,
210 permission: SystemPermission,
211 rationale: String,
212 platform: PlatformKind,
213 ) -> PermissionDescriptor {
214 let native_key: String = permission.native_key(platform).to_string();
215 let _: bool = dispatch_native_action(NATIVE_ACTION_PERMISSION, &native_key, &rationale);
216 let _: Option<String> = self
217 .get_mut_rationales()
218 .insert(permission, rationale.clone());
219 let status: PermissionStatus = if !permission.is_supported_on(platform) {
220 PermissionStatus::Unsupported
221 } else {
222 let current: PermissionStatus = self.check(permission, platform);
223 if current == PermissionStatus::NotDetermined {
224 self.set_status(permission, PermissionStatus::Granted);
225 PermissionStatus::Granted
226 } else {
227 current
228 }
229 };
230 PermissionDescriptor::new(permission, platform, native_key, rationale, status)
231 }
232}