From 36a93851f7acea6e8d902a1276e8bd9a21a18069 Mon Sep 17 00:00:00 2001 From: Vijay A Date: Wed, 12 May 2021 21:53:51 +0530 Subject: [Franz catchup] Use a newer maintained module to trigger and capture permissions on macos. --- src/electron/macOSPermissions.js | 86 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 78 insertions(+), 8 deletions(-) (limited to 'src/electron') 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 @@ -import { systemPreferences } from 'electron'; -import { - hasScreenCapturePermission, - hasPromptedForPermission, -} from 'mac-screen-capture-permissions'; +import { app, systemPreferences, dialog } from 'electron'; +import fs from 'fs'; +import macosVersion from 'macos-version'; +import path from 'path'; +import { isMac } from '../environment'; -export default function () { +let askForScreenCaptureAccess; +if (isMac) { + // eslint-disable-next-line global-require + askForScreenCaptureAccess = require('node-mac-permissions').askForScreenCaptureAccess; +} + +const debug = require('debug')('Franz:macOSPermissions'); + +const permissionExists = macosVersion.isGreaterThanOrEqualTo('10.15'); +const filePath = path.join(app.getPath('userData'), '.has-app-requested-screen-capture-permissions'); + +function hasPromptedForPermission() { + if (!permissionExists) { + return false; + } + + if (filePath && fs.existsSync(filePath)) { + return true; + } + + return false; +} + +function hasScreenCapturePermission() { + if (!permissionExists) { + return true; + } + + const screenCaptureStatus = systemPreferences.getMediaAccessStatus('screen'); + return screenCaptureStatus === 'granted'; +} + +function createStatusFile() { + try { + fs.writeFileSync(filePath, ''); + } catch (error) { + if (error.code === 'ENOENT') { + fs.mkdirSync(path.dirname(filePath)); + fs.writeFileSync(filePath, ''); + } + + throw error; + } +} + + +export default async function (mainWindow) { + debug('Checking camera & microphone permissions'); systemPreferences.askForMediaAccess('camera'); systemPreferences.askForMediaAccess('microphone'); - if (!hasPromptedForPermission()) { - hasScreenCapturePermission(); + if (!hasPromptedForPermission() && !hasScreenCapturePermission()) { + debug('Checking screen capture permissions'); + + const { response } = await dialog.showMessageBox(mainWindow, { + type: 'info', + message: 'Enable Screen Sharing', + detail: 'To enable screen sharing for some services, Franz needs the permission to record your screen.', + buttons: [ + 'Allow screen sharing', + 'No', + 'Ask me later', + ], + defaultId: 0, + cancelId: 2, + }); + + console.log('result', response); + if (response === 0) { + debug('Asking for access'); + askForScreenCaptureAccess(); + createStatusFile(); + } else if (response === 1) { + debug('Don\'t ask again'); + createStatusFile(); + } } } -- cgit v1.2.3-70-g09d2