aboutsummaryrefslogtreecommitdiffstats
path: root/src/electron/macOSPermissions.ts
diff options
context:
space:
mode:
authorLibravatar Markus Hatvan <markus_hatvan@aon.at>2021-09-14 11:03:28 +0200
committerLibravatar GitHub <noreply@github.com>2021-09-14 11:03:28 +0200
commit087113d8a1214ba4c7df03bfe66747d8d944280c (patch)
tree4d853a03057138dfa845cd6a7d91ccf63565a1a6 /src/electron/macOSPermissions.ts
parentchore: codebase improvements (#1930) (diff)
downloadferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.tar.gz
ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.tar.zst
ferdium-app-087113d8a1214ba4c7df03bfe66747d8d944280c.zip
chore: convert JS to TS (#1934)
Diffstat (limited to 'src/electron/macOSPermissions.ts')
-rw-r--r--src/electron/macOSPermissions.ts83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/electron/macOSPermissions.ts b/src/electron/macOSPermissions.ts
new file mode 100644
index 000000000..f5a8c7cc4
--- /dev/null
+++ b/src/electron/macOSPermissions.ts
@@ -0,0 +1,83 @@
1import { systemPreferences, BrowserWindow, dialog } from 'electron';
2import { pathExistsSync, mkdirSync, writeFileSync } from 'fs-extra';
3import macosVersion from 'macos-version';
4import { dirname } from 'path';
5import { askForScreenCaptureAccess } from 'node-mac-permissions';
6import { userDataPath } from '../environment';
7
8const debug = require('debug')('Ferdi:macOSPermissions');
9
10const isExplicitScreenCapturePermissionReqd =
11 macosVersion.isGreaterThanOrEqualTo('10.15');
12debug(
13 `Should check explicitly for screen-capture permissions: ${isExplicitScreenCapturePermissionReqd}`,
14);
15
16const filePath = userDataPath('.has-app-requested-screen-capture-permissions');
17
18function hasPromptedForScreenCapturePermission(): boolean {
19 if (!isExplicitScreenCapturePermissionReqd) {
20 return false;
21 }
22
23 debug('Checking if status file exists');
24 return filePath && pathExistsSync(filePath);
25}
26
27function hasScreenCapturePermissionAlreadyBeenGranted(): boolean {
28 if (!isExplicitScreenCapturePermissionReqd) {
29 return true;
30 }
31
32 const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen');
33 debug(`screen-capture permissions status: ${screenCaptureStatus}`);
34 return screenCaptureStatus === 'granted';
35}
36
37function createStatusFile() {
38 try {
39 writeFileSync(filePath, '');
40 } catch (error) {
41 if ((error as any).code === 'ENOENT') {
42 mkdirSync(dirname(filePath));
43 writeFileSync(filePath, '');
44 }
45
46 throw error;
47 }
48}
49
50export const askFormacOSPermissions = async (mainWindow: BrowserWindow) => {
51 debug('Checking camera & microphone permissions');
52 systemPreferences.askForMediaAccess('camera');
53 systemPreferences.askForMediaAccess('microphone');
54
55 if (hasScreenCapturePermissionAlreadyBeenGranted()) {
56 debug('Already obtained screen-capture permissions - writing status file');
57 createStatusFile();
58 return;
59 }
60
61 if (!hasPromptedForScreenCapturePermission()) {
62 debug('Checking screen capture permissions');
63
64 const { response } = await dialog.showMessageBox(mainWindow, {
65 type: 'info',
66 message: 'Enable Screen Sharing',
67 detail:
68 'To enable screen sharing for some services, Ferdi needs the permission to record your screen.',
69 buttons: ['Allow screen sharing', 'No', 'Ask me later'],
70 defaultId: 0,
71 cancelId: 2,
72 });
73
74 if (response === 0) {
75 debug('Asking for access');
76 askForScreenCaptureAccess();
77 createStatusFile();
78 } else if (response === 1) {
79 debug("Don't ask again");
80 createStatusFile();
81 }
82 }
83};