Skip to main content

tauri_runtime/
webview_permissions.rs

1// Copyright 2019-2024 Tauri Programme within The Commons Conservancy
2// SPDX-License-Identifier: Apache-2.0
3// SPDX-License-Identifier: MIT
4
5/// Permission types that can be requested by the webview.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7#[non_exhaustive]
8pub enum PermissionKind {
9  /// Microphone access permission.
10  Microphone,
11  /// Camera access permission.
12  Camera,
13  /// Geolocation access permission.
14  ///
15  /// ## Platform-specific
16  ///
17  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_GEOLOCATION`.
18  /// - **Linux**: Supported via `GeolocationPermissionRequest`.
19  /// - **Android**: Supported via `WebChromeClient.onGeolocationPermissionsShowPrompt`.
20  /// - **macOS / iOS**: Not yet supported by platform backends.
21  Geolocation,
22  /// Notifications permission.
23  ///
24  /// ## Platform-specific
25  ///
26  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_NOTIFICATIONS`.
27  /// - **Linux**: Supported via `NotificationPermissionRequest`.
28  /// - **macOS / Android / iOS**: Not yet supported by platform backends.
29  Notifications,
30  /// Clipboard read permission.
31  ///
32  /// ## Platform-specific
33  ///
34  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_CLIPBOARD_READ`.
35  /// - **macOS / Linux / Android / iOS**: Not yet supported by platform backends.
36  ClipboardRead,
37  /// Display capture permission (for getDisplayMedia).
38  DisplayCapture,
39  /// Midi access permission.
40  ///
41  /// ## Platform-specific
42  ///
43  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_MIDI_SYSTEM_EXCLUSIVE_MESSAGES`.
44  /// - **Android**: Supported via `android.webkit.resource.MIDI_SYSEX`.
45  /// - **macOS / Linux / iOS**: Not yet supported by platform backends.
46  Midi,
47  /// Sensors (accelerometer, gyroscope, etc.) access permission.
48  ///
49  /// ## Platform-specific
50  ///
51  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_OTHER_SENSORS`.
52  /// - **macOS / Linux / Android / iOS**: Not yet supported by platform backends.
53  Sensors,
54  /// Media key system access permission.
55  ///
56  /// ## Platform-specific
57  ///
58  /// - **Android**: Supported via `android.webkit.resource.PROTECTED_MEDIA_ID`.
59  /// - **Windows / macOS / Linux / iOS**: Not yet supported by platform backends.
60  MediaKeySystemAccess,
61  /// Local fonts access permission.
62  ///
63  /// ## Platform-specific
64  ///
65  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_LOCAL_FONTS`.
66  /// - **macOS / Linux / Android / iOS**: Not yet supported by platform backends.
67  LocalFonts,
68  /// Window management permission.
69  ///
70  /// ## Platform-specific
71  ///
72  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_WINDOW_MANAGEMENT`.
73  /// - **macOS / Linux / Android / iOS**: Not yet supported by platform backends.
74  WindowManagement,
75  /// Pointer lock permission.
76  ///
77  /// ## Platform-specific
78  ///
79  /// - **Linux**: Supported via `PointerLockPermissionRequest`.
80  /// - **Windows / macOS / Android / iOS**: Not yet supported by platform backends.
81  PointerLock,
82  /// Automatic downloads permission (multiple downloads without user interaction).
83  ///
84  /// ## Platform-specific
85  ///
86  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_MULTIPLE_AUTOMATIC_DOWNLOADS`.
87  /// - **macOS / Linux / Android / iOS**: Not yet supported by platform backends.
88  AutomaticDownloads,
89  /// File system access permission (read/write via File System Access API).
90  ///
91  /// ## Platform-specific
92  ///
93  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_FILE_READ_WRITE`.
94  /// - **macOS / Linux / Android / iOS**: Not yet supported by platform backends.
95  FileSystemAccess,
96  /// Media autoplay permission.
97  ///
98  /// ## Platform-specific
99  ///
100  /// - **Windows**: Supported via `COREWEBVIEW2_PERMISSION_KIND_AUTOPLAY`.
101  /// - **macOS / Linux / Android / iOS**: Not yet supported by platform backends.
102  Autoplay,
103  /// Other unrecognized permission type.
104  Other,
105}
106
107impl std::fmt::Display for PermissionKind {
108  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
109    match self {
110      Self::Microphone => write!(f, "microphone"),
111      Self::Camera => write!(f, "camera"),
112      Self::Geolocation => write!(f, "geolocation"),
113      Self::Notifications => write!(f, "notifications"),
114      Self::ClipboardRead => write!(f, "clipboard-read"),
115      Self::DisplayCapture => write!(f, "display-capture"),
116      Self::Midi => write!(f, "midi"),
117      Self::Sensors => write!(f, "sensors"),
118      Self::MediaKeySystemAccess => write!(f, "media-key-system-access"),
119      Self::LocalFonts => write!(f, "local-fonts"),
120      Self::WindowManagement => write!(f, "window-management"),
121      Self::PointerLock => write!(f, "pointer-lock"),
122      Self::AutomaticDownloads => write!(f, "automatic-downloads"),
123      Self::FileSystemAccess => write!(f, "file-system-access"),
124      Self::Autoplay => write!(f, "autoplay"),
125      Self::Other => write!(f, "other"),
126    }
127  }
128}
129
130/// Response for permission requests.
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
132pub enum PermissionResponse {
133  /// Grant the permission.
134  ///
135  /// ## Platform-specific
136  ///
137  /// - **Android**: Not supported for runtime permissions; the normal Android
138  ///   permission flow is used instead.
139  Allow,
140  /// Deny the permission.
141  Deny,
142  /// Use the platform or browser default behavior.
143  ///
144  /// ## Platform-specific
145  ///
146  /// - **Windows / macOS / Android**: The default behavior is to continue the
147  ///   platform or browser permission flow.
148  /// - **Linux**: The default behavior is [`Self::Deny`]
149  #[default]
150  Default,
151}
152
153impl std::fmt::Display for PermissionResponse {
154  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
155    match self {
156      Self::Allow => write!(f, "allow"),
157      Self::Deny => write!(f, "deny"),
158      Self::Default => write!(f, "default"),
159    }
160  }
161}