diff options
author | 2021-08-08 00:01:37 +0000 | |
---|---|---|
committer | 2021-08-08 05:31:37 +0530 | |
commit | 97d51a7763b14c92ee71ff9a012311dd9498d803 (patch) | |
tree | bd36005031ecb1148f27aa541e7a92a5e3aa4c0c /src/electron/macOSPermissions.js | |
parent | 5.6.1-nightly.17 [skip ci] (diff) | |
download | ferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.tar.gz ferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.tar.zst ferdium-app-97d51a7763b14c92ee71ff9a012311dd9498d803.zip |
refactor: path-references refactoring and using 'import' instead of 'require' (#1752)
* refactor references to 'userData' and 'appData' directories to move hardcoding into single location
* convert to es6 for lower memory usage as per https://codesource.io/the-difference-between-import-and-require-in-javascript/
Diffstat (limited to 'src/electron/macOSPermissions.js')
-rw-r--r-- | src/electron/macOSPermissions.js | 46 |
1 files changed, 25 insertions, 21 deletions
diff --git a/src/electron/macOSPermissions.js b/src/electron/macOSPermissions.js index c114f4843..887af2903 100644 --- a/src/electron/macOSPermissions.js +++ b/src/electron/macOSPermissions.js | |||
@@ -1,45 +1,43 @@ | |||
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 { askForScreenCaptureAccess } from 'node-mac-permissions'; | 5 | import { askForScreenCaptureAccess } from 'node-mac-permissions'; |
6 | import { userDataPath } from '../environment'; | ||
6 | 7 | ||
7 | const debug = require('debug')('Ferdi:macOSPermissions'); | 8 | const debug = require('debug')('Ferdi:macOSPermissions'); |
8 | 9 | ||
9 | const permissionExists = macosVersion.isGreaterThanOrEqualTo('10.15'); | 10 | const isExplicitScreenCapturePermissionReqd = macosVersion.isGreaterThanOrEqualTo('10.15'); |
10 | const filePath = path.join( | 11 | debug(`Should check explicitly for screen-capture permissions: ${isExplicitScreenCapturePermissionReqd}`); |
11 | app.getPath('userData'), | ||
12 | '.has-app-requested-screen-capture-permissions', | ||
13 | ); | ||
14 | 12 | ||
15 | function hasPromptedForPermission() { | 13 | const filePath = userDataPath('.has-app-requested-screen-capture-permissions'); |
16 | if (!permissionExists) { | ||
17 | return false; | ||
18 | } | ||
19 | 14 | ||
20 | if (filePath && fs.existsSync(filePath)) { | 15 | function hasPromptedForScreenCapturePermission() { |
21 | return true; | 16 | if (!isExplicitScreenCapturePermissionReqd) { |
17 | return false; | ||
22 | } | 18 | } |
23 | 19 | ||
24 | return false; | 20 | debug('Checking if status file exists'); |
21 | return filePath && pathExistsSync(filePath); | ||
25 | } | 22 | } |
26 | 23 | ||
27 | function hasScreenCapturePermission() { | 24 | function hasScreenCapturePermissionAlreadyBeenGranted() { |
28 | if (!permissionExists) { | 25 | if (!isExplicitScreenCapturePermissionReqd) { |
29 | return true; | 26 | return true; |
30 | } | 27 | } |
31 | 28 | ||
32 | const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen'); | 29 | const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen'); |
30 | debug(`screen-capture permissions status: ${screenCaptureStatus}`); | ||
33 | return screenCaptureStatus === 'granted'; | 31 | return screenCaptureStatus === 'granted'; |
34 | } | 32 | } |
35 | 33 | ||
36 | function createStatusFile() { | 34 | function createStatusFile() { |
37 | try { | 35 | try { |
38 | fs.writeFileSync(filePath, ''); | 36 | writeFileSync(filePath, ''); |
39 | } catch (error) { | 37 | } catch (error) { |
40 | if (error.code === 'ENOENT') { | 38 | if (error.code === 'ENOENT') { |
41 | fs.mkdirSync(path.dirname(filePath)); | 39 | mkdirSync(dirname(filePath)); |
42 | fs.writeFileSync(filePath, ''); | 40 | writeFileSync(filePath, ''); |
43 | } | 41 | } |
44 | 42 | ||
45 | throw error; | 43 | throw error; |
@@ -51,7 +49,13 @@ export const askFormacOSPermissions = async mainWindow => { | |||
51 | systemPreferences.askForMediaAccess('camera'); | 49 | systemPreferences.askForMediaAccess('camera'); |
52 | systemPreferences.askForMediaAccess('microphone'); | 50 | systemPreferences.askForMediaAccess('microphone'); |
53 | 51 | ||
54 | 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()) { | ||
55 | debug('Checking screen capture permissions'); | 59 | debug('Checking screen capture permissions'); |
56 | 60 | ||
57 | const { response } = await dialog.showMessageBox(mainWindow, { | 61 | const { response } = await dialog.showMessageBox(mainWindow, { |