diff options
author | 2021-05-12 21:53:51 +0530 | |
---|---|---|
committer | 2021-05-13 18:04:20 +0530 | |
commit | 36a93851f7acea6e8d902a1276e8bd9a21a18069 (patch) | |
tree | 71eabfdafe65aef8b4ed900aded23da2c1f11cdf /src/electron | |
parent | Specify node, npm and node-gyp versions. (diff) | |
download | ferdium-app-36a93851f7acea6e8d902a1276e8bd9a21a18069.tar.gz ferdium-app-36a93851f7acea6e8d902a1276e8bd9a21a18069.tar.zst ferdium-app-36a93851f7acea6e8d902a1276e8bd9a21a18069.zip |
[Franz catchup] Use a newer maintained module to trigger and capture permissions on macos.
Diffstat (limited to 'src/electron')
-rw-r--r-- | src/electron/macOSPermissions.js | 86 |
1 files changed, 78 insertions, 8 deletions
diff --git a/src/electron/macOSPermissions.js b/src/electron/macOSPermissions.js index 4ba6a7619..682a46a41 100644 --- a/src/electron/macOSPermissions.js +++ b/src/electron/macOSPermissions.js | |||
@@ -1,14 +1,84 @@ | |||
1 | import { systemPreferences } from 'electron'; | 1 | import { app, systemPreferences, dialog } from 'electron'; |
2 | import { | 2 | import fs from 'fs'; |
3 | hasScreenCapturePermission, | 3 | import macosVersion from 'macos-version'; |
4 | hasPromptedForPermission, | 4 | import path from 'path'; |
5 | } from 'mac-screen-capture-permissions'; | 5 | import { isMac } from '../environment'; |
6 | 6 | ||
7 | export default function () { | 7 | let askForScreenCaptureAccess; |
8 | if (isMac) { | ||
9 | // eslint-disable-next-line global-require | ||
10 | askForScreenCaptureAccess = require('node-mac-permissions').askForScreenCaptureAccess; | ||
11 | } | ||
12 | |||
13 | const debug = require('debug')('Franz:macOSPermissions'); | ||
14 | |||
15 | const permissionExists = macosVersion.isGreaterThanOrEqualTo('10.15'); | ||
16 | const filePath = path.join(app.getPath('userData'), '.has-app-requested-screen-capture-permissions'); | ||
17 | |||
18 | function hasPromptedForPermission() { | ||
19 | if (!permissionExists) { | ||
20 | return false; | ||
21 | } | ||
22 | |||
23 | if (filePath && fs.existsSync(filePath)) { | ||
24 | return true; | ||
25 | } | ||
26 | |||
27 | return false; | ||
28 | } | ||
29 | |||
30 | function hasScreenCapturePermission() { | ||
31 | if (!permissionExists) { | ||
32 | return true; | ||
33 | } | ||
34 | |||
35 | const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen'); | ||
36 | return screenCaptureStatus === 'granted'; | ||
37 | } | ||
38 | |||
39 | function createStatusFile() { | ||
40 | try { | ||
41 | fs.writeFileSync(filePath, ''); | ||
42 | } catch (error) { | ||
43 | if (error.code === 'ENOENT') { | ||
44 | fs.mkdirSync(path.dirname(filePath)); | ||
45 | fs.writeFileSync(filePath, ''); | ||
46 | } | ||
47 | |||
48 | throw error; | ||
49 | } | ||
50 | } | ||
51 | |||
52 | |||
53 | export default async function (mainWindow) { | ||
54 | debug('Checking camera & microphone permissions'); | ||
8 | systemPreferences.askForMediaAccess('camera'); | 55 | systemPreferences.askForMediaAccess('camera'); |
9 | systemPreferences.askForMediaAccess('microphone'); | 56 | systemPreferences.askForMediaAccess('microphone'); |
10 | 57 | ||
11 | if (!hasPromptedForPermission()) { | 58 | if (!hasPromptedForPermission() && !hasScreenCapturePermission()) { |
12 | hasScreenCapturePermission(); | 59 | debug('Checking screen capture permissions'); |
60 | |||
61 | const { response } = await dialog.showMessageBox(mainWindow, { | ||
62 | type: 'info', | ||
63 | message: 'Enable Screen Sharing', | ||
64 | detail: 'To enable screen sharing for some services, Franz needs the permission to record your screen.', | ||
65 | buttons: [ | ||
66 | 'Allow screen sharing', | ||
67 | 'No', | ||
68 | 'Ask me later', | ||
69 | ], | ||
70 | defaultId: 0, | ||
71 | cancelId: 2, | ||
72 | }); | ||
73 | |||
74 | console.log('result', response); | ||
75 | if (response === 0) { | ||
76 | debug('Asking for access'); | ||
77 | askForScreenCaptureAccess(); | ||
78 | createStatusFile(); | ||
79 | } else if (response === 1) { | ||
80 | debug('Don\'t ask again'); | ||
81 | createStatusFile(); | ||
82 | } | ||
13 | } | 83 | } |
14 | } | 84 | } |