diff options
author | 2021-08-13 00:45:01 +0530 | |
---|---|---|
committer | 2021-08-13 00:45:01 +0530 | |
commit | 2d27d5e66649d4f5baf127a53ee5ae524eae3a59 (patch) | |
tree | c592ea219ac8cd987fc367f57b54034c450ab2ab /src/electron/macOSPermissions.js | |
parent | Ferdi v5.6.0 (diff) | |
parent | 5.6.1-nightly.24 [skip ci] (diff) | |
download | ferdium-app-2d27d5e66649d4f5baf127a53ee5ae524eae3a59.tar.gz ferdium-app-2d27d5e66649d4f5baf127a53ee5ae524eae3a59.tar.zst ferdium-app-2d27d5e66649d4f5baf127a53ee5ae524eae3a59.zip |
chore: merge from nightly branch into release branch in prep for next beta
Diffstat (limited to 'src/electron/macOSPermissions.js')
-rw-r--r-- | src/electron/macOSPermissions.js | 67 |
1 files changed, 32 insertions, 35 deletions
diff --git a/src/electron/macOSPermissions.js b/src/electron/macOSPermissions.js index 940b16c6e..887af2903 100644 --- a/src/electron/macOSPermissions.js +++ b/src/electron/macOSPermissions.js | |||
@@ -1,83 +1,80 @@ | |||
1 | import { app, systemPreferences, dialog } from 'electron'; | 1 | import { systemPreferences, dialog } from 'electron'; |
2 | import fs from 'fs'; | 2 | import { pathExistsSync, mkdirSync, writeFileSync } from 'fs-extra'; |
3 | import macosVersion from 'macos-version'; | 3 | import macosVersion from 'macos-version'; |
4 | import path from 'path'; | 4 | import { dirname } from 'path'; |
5 | import { isMac } from '../environment'; | 5 | import { askForScreenCaptureAccess } from 'node-mac-permissions'; |
6 | 6 | import { userDataPath } from '../environment'; | |
7 | let askForScreenCaptureAccess; | ||
8 | if (isMac) { | ||
9 | // eslint-disable-next-line global-require | ||
10 | askForScreenCaptureAccess = require('node-mac-permissions').askForScreenCaptureAccess; | ||
11 | } | ||
12 | 7 | ||
13 | const debug = require('debug')('Ferdi:macOSPermissions'); | 8 | const debug = require('debug')('Ferdi:macOSPermissions'); |
14 | 9 | ||
15 | const permissionExists = macosVersion.isGreaterThanOrEqualTo('10.15'); | 10 | const isExplicitScreenCapturePermissionReqd = macosVersion.isGreaterThanOrEqualTo('10.15'); |
16 | const filePath = path.join(app.getPath('userData'), '.has-app-requested-screen-capture-permissions'); | 11 | debug(`Should check explicitly for screen-capture permissions: ${isExplicitScreenCapturePermissionReqd}`); |
17 | 12 | ||
18 | function hasPromptedForPermission() { | 13 | const filePath = userDataPath('.has-app-requested-screen-capture-permissions'); |
19 | if (!permissionExists) { | ||
20 | return false; | ||
21 | } | ||
22 | 14 | ||
23 | if (filePath && fs.existsSync(filePath)) { | 15 | function hasPromptedForScreenCapturePermission() { |
24 | return true; | 16 | if (!isExplicitScreenCapturePermissionReqd) { |
17 | return false; | ||
25 | } | 18 | } |
26 | 19 | ||
27 | return false; | 20 | debug('Checking if status file exists'); |
21 | return filePath && pathExistsSync(filePath); | ||
28 | } | 22 | } |
29 | 23 | ||
30 | function hasScreenCapturePermission() { | 24 | function hasScreenCapturePermissionAlreadyBeenGranted() { |
31 | if (!permissionExists) { | 25 | if (!isExplicitScreenCapturePermissionReqd) { |
32 | return true; | 26 | return true; |
33 | } | 27 | } |
34 | 28 | ||
35 | const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen'); | 29 | const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen'); |
30 | debug(`screen-capture permissions status: ${screenCaptureStatus}`); | ||
36 | return screenCaptureStatus === 'granted'; | 31 | return screenCaptureStatus === 'granted'; |
37 | } | 32 | } |
38 | 33 | ||
39 | function createStatusFile() { | 34 | function createStatusFile() { |
40 | try { | 35 | try { |
41 | fs.writeFileSync(filePath, ''); | 36 | writeFileSync(filePath, ''); |
42 | } catch (error) { | 37 | } catch (error) { |
43 | if (error.code === 'ENOENT') { | 38 | if (error.code === 'ENOENT') { |
44 | fs.mkdirSync(path.dirname(filePath)); | 39 | mkdirSync(dirname(filePath)); |
45 | fs.writeFileSync(filePath, ''); | 40 | writeFileSync(filePath, ''); |
46 | } | 41 | } |
47 | 42 | ||
48 | throw error; | 43 | throw error; |
49 | } | 44 | } |
50 | } | 45 | } |
51 | 46 | ||
52 | export default async function (mainWindow) { | 47 | export const askFormacOSPermissions = async mainWindow => { |
53 | debug('Checking camera & microphone permissions'); | 48 | debug('Checking camera & microphone permissions'); |
54 | systemPreferences.askForMediaAccess('camera'); | 49 | systemPreferences.askForMediaAccess('camera'); |
55 | systemPreferences.askForMediaAccess('microphone'); | 50 | systemPreferences.askForMediaAccess('microphone'); |
56 | 51 | ||
57 | if (!hasPromptedForPermission() && !hasScreenCapturePermission()) { | 52 | if (hasScreenCapturePermissionAlreadyBeenGranted()) { |
53 | debug('Already obtained screen-capture permissions - writing status file'); | ||
54 | createStatusFile(); | ||
55 | return; | ||
56 | } | ||
57 | |||
58 | if (!hasPromptedForScreenCapturePermission()) { | ||
58 | debug('Checking screen capture permissions'); | 59 | debug('Checking screen capture permissions'); |
59 | 60 | ||
60 | const { response } = await dialog.showMessageBox(mainWindow, { | 61 | const { response } = await dialog.showMessageBox(mainWindow, { |
61 | type: 'info', | 62 | type: 'info', |
62 | message: 'Enable Screen Sharing', | 63 | message: 'Enable Screen Sharing', |
63 | detail: 'To enable screen sharing for some services, Ferdi needs the permission to record your screen.', | 64 | detail: |
64 | buttons: [ | 65 | 'To enable screen sharing for some services, Ferdi needs the permission to record your screen.', |
65 | 'Allow screen sharing', | 66 | buttons: ['Allow screen sharing', 'No', 'Ask me later'], |
66 | 'No', | ||
67 | 'Ask me later', | ||
68 | ], | ||
69 | defaultId: 0, | 67 | defaultId: 0, |
70 | cancelId: 2, | 68 | cancelId: 2, |
71 | }); | 69 | }); |
72 | 70 | ||
73 | console.log('result', response); | ||
74 | if (response === 0) { | 71 | if (response === 0) { |
75 | debug('Asking for access'); | 72 | debug('Asking for access'); |
76 | askForScreenCaptureAccess(); | 73 | askForScreenCaptureAccess(); |
77 | createStatusFile(); | 74 | createStatusFile(); |
78 | } else if (response === 1) { | 75 | } else if (response === 1) { |
79 | debug('Don\'t ask again'); | 76 | debug("Don't ask again"); |
80 | createStatusFile(); | 77 | createStatusFile(); |
81 | } | 78 | } |
82 | } | 79 | } |
83 | } | 80 | }; |