Skip to content

Commit 7e9618c

Browse files
committed
refactor: remove pre-macOS 11.0 fallback code paths
With the deployment target bumped to 11.0, remove all @available guards for macOS 10.16/11.0 and their else branches: - ScreenAuthStatus: use CGPreflightScreenCaptureAccess directly - AskForScreenCaptureAccess: remove CGDisplayStreamCreate fallback - PhotosAuthStatus: always use authorizationStatusForAccessLevel: - AskForPhotosAccess: always use requestAuthorizationForAccessLevel: - MusicLibraryAuthStatus: always use SKCloudServiceController - AskForMusicLibraryAccess: remove pre-11.0 authorized fallback - Remove API_AVAILABLE annotations that are now always satisfied Also remove outdated README notes about pre-11.0 behavior.
1 parent b1ea9d8 commit 7e9618c

2 files changed

Lines changed: 32 additions & 144 deletions

File tree

README.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,6 @@ Return Value Descriptions:
7575
- `limited` - The application is authorized for limited access to `type` data. Currently only applicable to the `photos` type.
7676
- 'provisional' - The application is provisionally authorized to access `type` data. Currently only applicable to the `notifications` type.
7777

78-
**Notes:**
79-
80-
- Access to `bluetooth` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
81-
- Access to `camera` and `microphone` will always return a status of `authorized` prior to macOS 10.14, as the underlying API was not introduced until that version.
82-
- Access to `input-monitoring` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
83-
- Access to `music-library` will always return a status of `authorized` prior to macOS 11.0, as the underlying API was not introduced until that version.
84-
- Access to `screen` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
85-
- Access to `speech-recognition` will always return a status of `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
86-
8778
Example:
8879

8980
```js
@@ -224,8 +215,6 @@ askForSpeechRecognitionAccess().then(status => {
224215
})
225216
```
226217

227-
**Note:** `status` will be resolved back as `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
228-
229218
### `permissions.askForRemindersAccess()`
230219

231220
Returns `Promise<String>` - Whether or not the request succeeded or failed; can be `authorized` or `denied`.
@@ -315,10 +304,6 @@ Your app must provide an explanation for its use of capture devices using the `N
315304
<string>Your reason for wanting to access the Camera</string>
316305
```
317306

318-
**Note:**
319-
320-
- `status` will be resolved back as `authorized` prior to macOS 10.14, as the underlying API was not introduced until that version.
321-
322307
Example:
323308

324309
```js
@@ -358,10 +343,6 @@ Checks the authorization status for input monitoring access. If the status check
358343
- `not determined` - A dialog will be displayed directing the user to the `Security & Privacy` System Preferences window , where the user can approve your app to monitor keyboard events in the background. The Promise is resolved as `denied`.
359344
- `denied` - The `Security & Privacy` System Preferences window is opened with the Input Monitoring privacy key highlighted. On open of the `Security & Privacy` window, the Promise is resolved as `denied`.
360345

361-
**Note:**
362-
363-
- `status` will be resolved back as `authorized` prior to macOS 10.15, as the underlying API was not introduced until that version.
364-
365346
Example:
366347

367348
```js
@@ -389,10 +370,6 @@ Your app must provide an explanation for its use of capture devices using the `N
389370
<string>Your reason for wanting to access the Microphone</string>
390371
```
391372

392-
**Note:**
393-
394-
- `status` will be resolved back as `authorized` prior to macOS 10.14, as the underlying API was not introduced until that version.
395-
396373
Example:
397374

398375
```js
@@ -418,10 +395,6 @@ Your app must provide an explanation for its use of the music library using the
418395
<string>Your reason for wanting to access the user’s media library.</string>
419396
```
420397

421-
**Note:**
422-
423-
- `status` will be resolved back as `authorized` prior to macOS 11.0, as the underlying API was not introduced until that version.
424-
425398
Example:
426399

427400
```js

permissions.mm

Lines changed: 32 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ bool IsValidBundleID(NSString *bundleID) {
5757
return kNotDetermined;
5858
}
5959

60-
PHAccessLevel GetPHAccessLevel(const std::string &type)
61-
API_AVAILABLE(macosx(10.16)) {
60+
PHAccessLevel GetPHAccessLevel(const std::string &type) {
6261
return type == "read-write" ? PHAccessLevelReadWrite : PHAccessLevelAddOnly;
6362
}
6463

@@ -92,8 +91,7 @@ IOHIDRequestType GetInputMonitoringAccessType(const std::string &type) {
9291
}
9392

9493
const std::string &
95-
StringFromMusicLibraryStatus(SKCloudServiceAuthorizationStatus status)
96-
API_AVAILABLE(macosx(10.16)) {
94+
StringFromMusicLibraryStatus(SKCloudServiceAuthorizationStatus status) {
9795
switch (status) {
9896
case SKCloudServiceAuthorizationStatusAuthorized:
9997
return kAuthorized;
@@ -261,13 +259,9 @@ bool HasOpenSystemPreferencesDialog() {
261259
// Returns a status indicating whether the user has authorized Apple Music
262260
// Library access.
263261
std::string MusicLibraryAuthStatus() {
264-
if (@available(macOS 10.16, *)) {
265-
SKCloudServiceAuthorizationStatus status =
266-
[SKCloudServiceController authorizationStatus];
267-
return StringFromMusicLibraryStatus(status);
268-
}
269-
270-
return kAuthorized;
262+
SKCloudServiceAuthorizationStatus status =
263+
[SKCloudServiceController authorizationStatus];
264+
return StringFromMusicLibraryStatus(status);
271265
}
272266

273267
// Returns a status indicating whether the user has authorized
@@ -321,48 +315,7 @@ bool HasOpenSystemPreferencesDialog() {
321315
// Returns a status indicating whether the user has authorized
322316
// Screen Capture access.
323317
std::string ScreenAuthStatus() {
324-
std::string auth_status = kNotDetermined;
325-
if (@available(macOS 11.0, *)) {
326-
auth_status = CGPreflightScreenCaptureAccess() ? kAuthorized : kDenied;
327-
} else {
328-
auth_status = kDenied;
329-
NSRunningApplication *runningApplication =
330-
NSRunningApplication.currentApplication;
331-
NSNumber *ourProcessIdentifier =
332-
[NSNumber numberWithInteger:runningApplication.processIdentifier];
333-
334-
CFArrayRef windowList =
335-
CGWindowListCopyWindowInfo(kCGWindowListOptionAll, kCGNullWindowID);
336-
int numberOfWindows = CFArrayGetCount(windowList);
337-
for (int index = 0; index < numberOfWindows; index++) {
338-
// Get information for each window.
339-
NSDictionary *windowInfo =
340-
(NSDictionary *)CFArrayGetValueAtIndex(windowList, index);
341-
NSString *windowName = windowInfo[(id)kCGWindowName];
342-
NSNumber *processIdentifier = windowInfo[(id)kCGWindowOwnerPID];
343-
344-
// Don't check windows owned by the current process.
345-
if (![processIdentifier isEqual:ourProcessIdentifier]) {
346-
// Get process information for each window.
347-
pid_t pid = processIdentifier.intValue;
348-
NSRunningApplication *windowRunningApplication =
349-
[NSRunningApplication runningApplicationWithProcessIdentifier:pid];
350-
if (windowRunningApplication) {
351-
NSString *windowExecutableName =
352-
windowRunningApplication.executableURL.lastPathComponent;
353-
if (windowName) {
354-
if (![windowExecutableName isEqual:@"Dock"]) {
355-
auth_status = kAuthorized;
356-
break;
357-
}
358-
}
359-
}
360-
}
361-
}
362-
CFRelease(windowList);
363-
}
364-
365-
return auth_status;
318+
return CGPreflightScreenCaptureAccess() ? kAuthorized : kDenied;
366319
}
367320

368321
// Returns a status indicating whether the user has authorized
@@ -433,15 +386,9 @@ bool HasOpenSystemPreferencesDialog() {
433386
// Returns a status indicating whether or not the user has authorized Photos
434387
// access.
435388
std::string PhotosAuthStatus(const std::string &access_level) {
436-
PHAuthorizationStatus status = PHAuthorizationStatusNotDetermined;
437-
438-
if (@available(macOS 10.16, *)) {
439-
PHAccessLevel level = GetPHAccessLevel(access_level);
440-
status = [PHPhotoLibrary authorizationStatusForAccessLevel:level];
441-
} else {
442-
status = [PHPhotoLibrary authorizationStatus];
443-
}
444-
389+
PHAccessLevel level = GetPHAccessLevel(access_level);
390+
PHAuthorizationStatus status =
391+
[PHPhotoLibrary authorizationStatusForAccessLevel:level];
445392
return StringFromPhotosStatus(status);
446393
}
447394

@@ -720,22 +667,14 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
720667
};
721668

722669
if (auth_status == kNotDetermined) {
723-
if (@available(macOS 10.16, *)) {
724-
[PHPhotoLibrary
725-
requestAuthorizationForAccessLevel:GetPHAccessLevel(access_level)
726-
handler:^(PHAuthorizationStatus status) {
727-
tsfn.BlockingCall(
728-
StringFromPhotosStatus(status)
729-
.c_str(),
730-
callback);
731-
tsfn.Release();
732-
}];
733-
} else {
734-
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
735-
tsfn.BlockingCall(StringFromPhotosStatus(status).c_str(), callback);
736-
tsfn.Release();
737-
}];
738-
}
670+
[PHPhotoLibrary
671+
requestAuthorizationForAccessLevel:GetPHAccessLevel(access_level)
672+
handler:^(PHAuthorizationStatus status) {
673+
tsfn.BlockingCall(
674+
StringFromPhotosStatus(status).c_str(),
675+
callback);
676+
tsfn.Release();
677+
}];
739678
} else {
740679
if (auth_status == kDenied)
741680
OpenPrefPane("Privacy_Photos");
@@ -809,57 +748,33 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
809748
deferred.Resolve(Napi::String::New(env, status));
810749
};
811750

812-
if (@available(macOS 10.16, *)) {
813-
std::string auth_status = MusicLibraryAuthStatus();
814-
815-
if (auth_status == kNotDetermined) {
816-
[SKCloudServiceController
817-
requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
818-
tsfn.BlockingCall(StringFromMusicLibraryStatus(status).c_str(),
819-
callback);
820-
tsfn.Release();
821-
}];
822-
} else {
823-
if (auth_status == kDenied)
824-
OpenPrefPane("Privacy_Media");
751+
std::string auth_status = MusicLibraryAuthStatus();
825752

753+
if (auth_status == kNotDetermined) {
754+
[SKCloudServiceController requestAuthorization:^(
755+
SKCloudServiceAuthorizationStatus status) {
756+
tsfn.BlockingCall(StringFromMusicLibraryStatus(status).c_str(), callback);
826757
tsfn.Release();
827-
deferred.Resolve(Napi::String::New(env, auth_status));
828-
}
758+
}];
829759
} else {
760+
if (auth_status == kDenied)
761+
OpenPrefPane("Privacy_Media");
762+
830763
tsfn.Release();
831-
deferred.Resolve(Napi::String::New(env, kAuthorized));
764+
deferred.Resolve(Napi::String::New(env, auth_status));
832765
}
833766

834767
return deferred.Promise();
835768
}
836769

837770
// Request Screen Capture Access.
838771
void AskForScreenCaptureAccess(const Napi::CallbackInfo &info) {
839-
if (@available(macOS 11.0, *)) {
840-
if (CGPreflightScreenCaptureAccess() || CGRequestScreenCaptureAccess())
841-
return;
772+
if (CGPreflightScreenCaptureAccess() || CGRequestScreenCaptureAccess())
773+
return;
842774

843-
bool should_force_prefs = info[0].As<Napi::Boolean>().Value();
844-
if (should_force_prefs && !HasOpenSystemPreferencesDialog()) {
845-
OpenPrefPane("Privacy_ScreenCapture");
846-
}
847-
} else {
848-
// Tries to create a capture stream. This is necessary to add the app back
849-
// to the list in sysprefs if the user previously denied.
850-
// https://stackoverflow.com/questions/56597221/detecting-screen-recording-settings-on-macos-catalina
851-
CGDisplayStreamRef stream = CGDisplayStreamCreate(
852-
CGMainDisplayID(), 1, 1, kCVPixelFormatType_32BGRA, NULL,
853-
^(CGDisplayStreamFrameStatus status, uint64_t displayTime,
854-
IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef){
855-
});
856-
857-
if (stream) {
858-
CFRelease(stream);
859-
} else {
860-
if (!HasOpenSystemPreferencesDialog())
861-
OpenPrefPane("Privacy_ScreenCapture");
862-
}
775+
bool should_force_prefs = info[0].As<Napi::Boolean>().Value();
776+
if (should_force_prefs && !HasOpenSystemPreferencesDialog()) {
777+
OpenPrefPane("Privacy_ScreenCapture");
863778
}
864779
}
865780

0 commit comments

Comments
 (0)