Permissions
How to check if the application has access to the microphone, camera, or accessibility and request it from the system.
Overview
There are two types of permissions your application can deal with:
- The permissions granted to the application by operating system.
- The permissions required by a website.
System permissions
Your application might need to access the microphone, camera, or accessibility to perform some actions on the end user’s device.
Different operating systems have different ways to manage permissions. On macOS, users should grant the permission to the application in the System Settings. On Windows, your application has access to the microphone and camera by default, but users can deny it in Settings > Privacy & Security > Microphone any moment.
MōBrowser provides a unified API to check the permission status and request it from the system.
Permission status
You can check if the application has access to the microphone, camera, or accessibility. See PermissionName for the list of supported permissions.
The following example shows how to check if the application has access to the microphone:
import { app } from '@mobrowser/api'
const status = app.permissions.getStatus('microphone')
console.log(status)
Status is a PermissionStatus object with the following possible values:
| Status | Description |
|---|---|
'granted' | The application has access to the requested permission. |
'denied' | The application does not have access to the requested permission. |
'notDetermined' | The user has not made a choice yet; requesting may show a system prompt. |
'restricted' | The application is restricted from accessing the requested permission. |
'unsupported' | The platform, permission, or app configuration does not support this API. |
Requesting a permission
If the permission is not determined or denied, you can request it from the system.
import { app } from '@mobrowser/api'
const status = await app.permissions.request('accessibility')
console.log(status)
On macOS, this method may show a system prompt to the user asking them to grant the permission. On Windows and Linux, there’s no such functionality, so this method resolves to 'unsupported'.
Opening the system settings
You can open the system settings from your application to ask the user to grant the required permission for your application manually.
import { app } from '@mobrowser/api'
app.permissions.openSystemSettings('camera')
Website permissions
If you load an external website, the web page on that site may request a permission to access the microphone, camera, geolocation, or ability to show desktop notifications. By default, access to the permissions is denied for external websites for security reasons. But you can handle the permission request and grant or deny it.
To handle the permission request, you can implement the requestPermissions handler. The following example shows how to grant the geolocation permission:
import { BrowserWindow, RequestPermissionsParams } from '@mobrowser/api';
const win = new BrowserWindow()
win.browser.handle('requestPermissions', (params: RequestPermissionsParams) => {
if (params.permissionType === 'geolocation') {
return 'grant'
}
return 'deny'
})
win.browser.loadUrl('https://example.com')
When the handler returns 'grant', the decision for the requesting origin is remembered. If the user opens the same website again later within the application session, the permission is read and this handler is not invoked for that permission on that origin.
After application restart, the permission is reset and the handler is invoked again.