2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2018 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-05-24 19:13:16 +00:00
|
|
|
// The list of permissions is here:
|
|
|
|
// https://electronjs.org/docs/api/session#sessetpermissionrequesthandlerhandler
|
|
|
|
|
2023-04-20 15:59:17 +00:00
|
|
|
import type { session as ElectronSession, Session } from 'electron';
|
2021-06-18 17:04:27 +00:00
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
import type { ConfigType } from './base_config';
|
2021-06-18 17:04:27 +00:00
|
|
|
|
|
|
|
const PERMISSIONS: Record<string, boolean> = {
|
2018-05-24 19:13:16 +00:00
|
|
|
// Allowed
|
|
|
|
fullscreen: true, // required to show videos in full-screen
|
|
|
|
notifications: true, // required to show OS notifications for new messages
|
2022-12-19 18:23:53 +00:00
|
|
|
'clipboard-sanitized-write': true, // required to copy text into clipboard
|
2018-05-24 19:13:16 +00:00
|
|
|
|
2018-07-03 22:33:50 +00:00
|
|
|
// Off by default, can be enabled by user
|
2020-06-04 18:16:19 +00:00
|
|
|
media: false, // required for access to microphone and camera, used for voice notes and calling
|
2018-07-03 22:33:50 +00:00
|
|
|
|
2018-05-24 19:13:16 +00:00
|
|
|
// Not allowed
|
|
|
|
geolocation: false,
|
|
|
|
midiSysex: false,
|
|
|
|
openExternal: false, // we don't need this; we open links via 'will-navigate' event
|
|
|
|
pointerLock: false,
|
|
|
|
};
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
function _createPermissionHandler(
|
2021-08-18 20:08:14 +00:00
|
|
|
userConfig: Pick<ConfigType, 'get'>
|
2021-06-18 17:04:27 +00:00
|
|
|
): Parameters<typeof ElectronSession.prototype.setPermissionRequestHandler>[0] {
|
|
|
|
return (_webContents, permission, callback, details): void => {
|
2020-06-04 18:16:19 +00:00
|
|
|
// We default 'media' permission to false, but the user can override that for
|
|
|
|
// the microphone and camera.
|
2021-05-20 21:54:03 +00:00
|
|
|
if (permission === 'media') {
|
2024-05-15 16:48:54 +00:00
|
|
|
// Pacifying typescript because it is always there for 'media' permission
|
|
|
|
if (!('mediaTypes' in details)) {
|
|
|
|
callback(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-20 21:54:03 +00:00
|
|
|
if (
|
2021-06-18 17:04:27 +00:00
|
|
|
details.mediaTypes?.includes('audio') ||
|
|
|
|
details.mediaTypes?.includes('video')
|
2021-05-20 21:54:03 +00:00
|
|
|
) {
|
|
|
|
if (
|
2021-06-18 17:04:27 +00:00
|
|
|
details.mediaTypes?.includes('audio') &&
|
2021-05-20 21:54:03 +00:00
|
|
|
userConfig.get('mediaPermissions')
|
|
|
|
) {
|
2021-06-18 17:04:27 +00:00
|
|
|
callback(true);
|
|
|
|
return;
|
2021-05-20 21:54:03 +00:00
|
|
|
}
|
|
|
|
if (
|
2021-06-18 17:04:27 +00:00
|
|
|
details.mediaTypes?.includes('video') &&
|
2021-05-20 21:54:03 +00:00
|
|
|
userConfig.get('mediaCameraPermissions')
|
|
|
|
) {
|
2021-06-18 17:04:27 +00:00
|
|
|
callback(true);
|
|
|
|
return;
|
2021-05-20 21:54:03 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
callback(false);
|
|
|
|
return;
|
2021-05-20 21:54:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If it doesn't have 'video' or 'audio', it's probably screenshare.
|
|
|
|
// TODO: DESKTOP-1611
|
2021-06-18 17:04:27 +00:00
|
|
|
callback(true);
|
|
|
|
return;
|
2018-07-03 22:33:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (PERMISSIONS[permission]) {
|
|
|
|
console.log(`Approving request for permission '${permission}'`);
|
2021-06-18 17:04:27 +00:00
|
|
|
callback(true);
|
|
|
|
return;
|
2018-07-03 22:33:50 +00:00
|
|
|
}
|
2018-05-24 19:13:16 +00:00
|
|
|
|
2018-07-03 22:33:50 +00:00
|
|
|
console.log(`Denying request for permission '${permission}'`);
|
2021-06-18 17:04:27 +00:00
|
|
|
callback(false);
|
2018-07-03 22:33:50 +00:00
|
|
|
};
|
2018-05-24 19:13:16 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 17:04:27 +00:00
|
|
|
export function installPermissionsHandler({
|
|
|
|
session,
|
|
|
|
userConfig,
|
|
|
|
}: {
|
2023-04-20 15:59:17 +00:00
|
|
|
session: Session;
|
2021-08-18 20:08:14 +00:00
|
|
|
userConfig: Pick<ConfigType, 'get'>;
|
2021-06-18 17:04:27 +00:00
|
|
|
}): void {
|
2018-07-03 22:33:50 +00:00
|
|
|
// Setting the permission request handler to null first forces any permissions to be
|
|
|
|
// requested again. Without this, revoked permissions might still be available if
|
|
|
|
// they've already been used successfully.
|
2023-04-20 15:59:17 +00:00
|
|
|
session.setPermissionRequestHandler(null);
|
2018-07-03 22:33:50 +00:00
|
|
|
|
2023-04-20 15:59:17 +00:00
|
|
|
session.setPermissionRequestHandler(_createPermissionHandler(userConfig));
|
2018-05-24 19:13:16 +00:00
|
|
|
}
|