From 4601c0f1bb803c76ca543b821d582d4401b23e0f Mon Sep 17 00:00:00 2001 From: Thomas Zhou Date: Tue, 16 Aug 2022 00:09:11 -0700 Subject: [PATCH 1/2] #1095 Use different library for screen capture permission --- main/common/system-permissions.ts | 15 ++++++--------- package.json | 2 +- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/main/common/system-permissions.ts b/main/common/system-permissions.ts index c60f5971..f8ae7cfa 100644 --- a/main/common/system-permissions.ts +++ b/main/common/system-permissions.ts @@ -1,5 +1,5 @@ import {systemPreferences, shell, dialog, app} from 'electron'; -const {hasScreenCapturePermission, hasPromptedForPermission} = require('mac-screen-capture-permissions'); +import {getAuthStatus, askForScreenCaptureAccess} from 'node-mac-permissions'; const {ensureDockIsShowing} = require('../utils/dock'); let isDialogShowing = false; @@ -73,17 +73,14 @@ const screenCaptureFallback = promptSystemPreferences({ }); export const ensureScreenCapturePermissions = (fallback = screenCaptureFallback) => { - const hadAsked = hasPromptedForPermission(); - - const hasAccess = hasScreenCapturePermission(); - - if (hasAccess) { + const status = getAuthStatus('screen'); + if (status === 'authorized') { return true; } - fallback({hasAsked: !hadAsked}); + askForScreenCaptureAccess(); + fallback(); return false; }; -export const hasScreenCaptureAccess = () => hasScreenCapturePermission(); - +export const hasScreenCaptureAccess = () => getAuthStatus('screen') === 'authorized'; diff --git a/package.json b/package.json index 8ee0fe54..73464fb7 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,6 @@ "is-online": "^8.4.0", "lodash": "^4.17.21", "mac-open-with": "^1.2.3", - "mac-screen-capture-permissions": "^1.1.0", "mac-windows": "^1.0.0", "macos-audio-devices": "^1.4.0", "macos-version": "^5.2.1", @@ -69,6 +68,7 @@ "move-file": "^2.0.0", "nearest-normal-aspect-ratio": "^1.2.1", "node-mac-app-icon": "^1.4.0", + "node-mac-permissions": "^2.2.1", "object-hash": "^2.1.1", "p-cancelable": "^2.1.0", "p-event": "^4.2.0", From 34ad95b67208b777013ecc595e9f8d88f855cf5f Mon Sep 17 00:00:00 2001 From: Thomas Zhou Date: Sat, 27 Aug 2022 21:28:45 -0700 Subject: [PATCH 2/2] #1095 Show fallback prompt if already asked for screen cap permissions --- main/common/system-permissions.ts | 13 ++++++++++- yarn.lock | 36 ++++++++++++++++++++++--------- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/main/common/system-permissions.ts b/main/common/system-permissions.ts index f8ae7cfa..b41f83de 100644 --- a/main/common/system-permissions.ts +++ b/main/common/system-permissions.ts @@ -1,7 +1,10 @@ +import {join} from 'path'; +import {existsSync, openSync} from 'fs'; import {systemPreferences, shell, dialog, app} from 'electron'; import {getAuthStatus, askForScreenCaptureAccess} from 'node-mac-permissions'; const {ensureDockIsShowing} = require('../utils/dock'); +const hasAskedForScreenCapturePermissionsPath = join(app.getPath('userData'), '.has-asked-for-screen-capture-permissions'); let isDialogShowing = false; const promptSystemPreferences = (options: {message: string; detail: string; systemPreferencesPath: string}) => async ({hasAsked}: {hasAsked?: boolean} = {}) => { @@ -73,12 +76,20 @@ const screenCaptureFallback = promptSystemPreferences({ }); export const ensureScreenCapturePermissions = (fallback = screenCaptureFallback) => { + // Check for screen capture permissions const status = getAuthStatus('screen'); if (status === 'authorized') { return true; } - askForScreenCaptureAccess(); + // If not authorized for screen capture and we haven't already asked, then ask for permissions now + if (!existsSync(hasAskedForScreenCapturePermissionsPath)) { + askForScreenCaptureAccess(); + openSync(hasAskedForScreenCapturePermissionsPath, 'w'); + return false; + } + + // If we've already asked, then prompt user again to give permission fallback(); return false; }; diff --git a/yarn.lock b/yarn.lock index 430f0ce2..81bd010a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1680,6 +1680,13 @@ binary-extensions@^2.0.0: resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d" integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA== +bindings@^1.5.0: + version "1.5.0" + resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df" + integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ== + dependencies: + file-uri-to-path "1.0.0" + bl@^1.0.0: version "1.2.3" resolved "https://registry.yarnpkg.com/bl/-/bl-1.2.3.tgz#1e8dd80142eac80d7158c9dccc047fb620e035e7" @@ -3302,7 +3309,7 @@ electron-util@^0.12.1: electron-is-dev "^1.1.0" new-github-issue-url "^0.2.1" -electron-util@^0.13.0, electron-util@^0.13.1: +electron-util@^0.13.1: version "0.13.1" resolved "https://registry.yarnpkg.com/electron-util/-/electron-util-0.13.1.tgz#ba3b9cb7e5fdb6a51970a01e9070877cf7855ef8" integrity sha512-CvOuAyQPaPtnDp7SspwnT1yTb1yynw6yp4LrZCfEJ7TG/kJFiZW9RqMHlCEFWMn3QNoMkNhGVeCvWJV5NsYyuQ== @@ -4243,6 +4250,11 @@ file-type@^8.1.0: resolved "https://registry.yarnpkg.com/file-type/-/file-type-8.1.0.tgz#244f3b7ef641bbe0cca196c7276e4b332399f68c" integrity sha512-qyQ0pzAy78gVoJsmYeNgl8uH8yKhr1lVhW7JbzJmnlRi0I4R2eEDEJZVKG8agpDnLpacwNbDhLNG/LMdxHD2YQ== +file-uri-to-path@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd" + integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw== + file-url@^3.0.0: version "3.0.0" resolved "https://registry.yarnpkg.com/file-url/-/file-url-3.0.0.tgz#247a586a746ce9f7a8ed05560290968afc262a77" @@ -6203,15 +6215,6 @@ mac-open-with@^1.2.3: execa "^2.0.4" macos-version "^5.2.0" -mac-screen-capture-permissions@^1.1.0: - version "1.1.0" - resolved "https://registry.yarnpkg.com/mac-screen-capture-permissions/-/mac-screen-capture-permissions-1.1.0.tgz#a60ac7f06240d5037b0e86d409723ec36d077c8c" - integrity sha512-jMRumlB3FScui/7yW+5FqqbuO7CQ0XOJVT5oTsb7W9eRQDhCIpJpIF0XxLVXwq2DIOp0fYsz1LFiBjnyDYULyQ== - dependencies: - electron-util "^0.13.0" - execa "^2.0.4" - macos-version "^5.2.0" - mac-windows@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/mac-windows/-/mac-windows-1.0.0.tgz#d5c40f548f94ea075005af7ca766811324638326" @@ -6689,6 +6692,11 @@ node-addon-api@^1.6.3: resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-1.7.2.tgz#3df30b95720b53c24e59948b49532b662444f54d" integrity sha512-ibPK3iA+vaY1eEjESkQkM0BbCqFOaZMiXRTtdB0u7b4djtY6JnsjvPdUHVMg6xQt3B8fpTTWHI9A+ADjM9frzg== +node-addon-api@^3.0.2: + version "3.2.1" + resolved "https://registry.yarnpkg.com/node-addon-api/-/node-addon-api-3.2.1.tgz#81325e0a2117789c0128dab65e7e38f07ceba161" + integrity sha512-mmcei9JghVNDYydghQmeDX8KoAm0FAiYyIcUt/N4nhyAipB17pllZQDOJD2fotxABnt4Mdz+dKTO7eftLg4d0A== + node-fetch@2.6.1: version "2.6.1" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" @@ -6738,6 +6746,14 @@ node-mac-app-icon@^1.4.0: electron-util "^0.4.1" execa "^0.8.0" +node-mac-permissions@^2.2.1: + version "2.2.1" + resolved "https://registry.yarnpkg.com/node-mac-permissions/-/node-mac-permissions-2.2.1.tgz#69497a08a47ebed336d794a005eb3b8949793f87" + integrity sha512-lmSuexF/XcAvtanSReDDrr61Bz4FveSmVl1wHY0Be6RV0+QpCK1RMpjWxtKG0ALqX4p+k2cJ/u7GBV8dkUeLeA== + dependencies: + bindings "^1.5.0" + node-addon-api "^3.0.2" + node-releases@^1.1.71, node-releases@^1.1.73: version "1.1.74" resolved "https://registry.yarnpkg.com/node-releases/-/node-releases-1.1.74.tgz#e5866488080ebaa70a93b91144ccde06f3c3463e"