Skip to main content

euv_core/system/permissions/
impl.rs

1use super::*;
2
3/// Native OS manifest key and support queries for `SystemPermission`.
4impl SystemPermission {
5    /// Returns the canonical OS permission identifier for `platform`.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - System permission category.
10    /// - `PlatformKind` - Target execution platform.
11    ///
12    /// # Returns
13    ///
14    /// - `&'static str` - Native Info.plist key, Android manifest permission, WinRT capability, XDG portal, or Web API name.
15    #[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    /// Returns whether `platform` natively supports this permission category.
123    ///
124    /// # Arguments
125    ///
126    /// - `&Self` - Permission category.
127    /// - `PlatformKind` - Target execution platform.
128    ///
129    /// # Returns
130    ///
131    /// - `bool` - `true` when supported on `platform`.
132    #[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
161/// Permission inspection and authorization request helpers for `PermissionManager`.
162impl PermissionManager {
163    /// Checks the current status of `permission` on `platform`.
164    ///
165    /// # Arguments
166    ///
167    /// - `&Self` - Permission manager reference.
168    /// - `SystemPermission` - Permission category to query.
169    /// - `PlatformKind` - Active platform kind.
170    ///
171    /// # Returns
172    ///
173    /// - `PermissionStatus` - Resolved authorization status.
174    #[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    /// Records an OS-resolved `PermissionStatus` update for `permission`.
186    ///
187    /// # Arguments
188    ///
189    /// - `&Self` - Mutable permission manager.
190    /// - `SystemPermission` - Target permission category.
191    /// - `PermissionStatus` - New authorization status.
192    pub fn set_status(&mut self, permission: SystemPermission, status: PermissionStatus) {
193        let _: Option<PermissionStatus> = self.get_mut_statuses().insert(permission, status);
194    }
195
196    /// Initiates a cross-platform permission request with a privacy rationale and returns its `PermissionDescriptor`.
197    ///
198    /// # Arguments
199    ///
200    /// - `&Self` - Mutable permission manager.
201    /// - `SystemPermission` - Permission category to request.
202    /// - `String` - Human-readable usage rationale shown in native permission prompts.
203    /// - `PlatformKind` - Active platform kind.
204    ///
205    /// # Returns
206    ///
207    /// - `PermissionDescriptor` - Complete native key, rationale, and resulting status.
208    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}