Skip to content

Commit

Permalink
refactor: pull out string converters
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed May 21, 2022
1 parent f9cc660 commit 03d0b0d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ askForCalendarAccess().then(status => {

### `permissions.askForSpeechRecognitionAccess()`

Returns `Promise<String>` - Whether or not the request succeeded or failed; can be `authorized` or `denied`.
Returns `Promise<String>` - Whether or not the request succeeded or failed; can be `authorized`, `denied`, or `restricted`.

Checks the authorization status for Speech Recognition access. If the status check returns:

* `not determined` - The Speech Recognition access authorization will prompt the user to authorize or deny. The Promise is resolved after the user selection with either `authorized` or `denied`.
* `denied` - The `Security & Privacy` System Preferences window is opened with the Speech Recognition privacy key highlighted. On open of the `Security & Privacy` window, the Promise is resolved as `denied`.
* `restricted` - The Promise is resolved as `restricted`.

Your app must provide an explanation for its use of Speech Recognition using the `NSSpeechRecognitionUsageDescription` `Info.plist` key;

Expand Down Expand Up @@ -303,7 +304,7 @@ askForMicrophoneAccess().then(status => {

### `permissions.askForMusicLibraryAccess()`

Returns `Promise<String>` - Whether or not the request succeeded or failed; can be `authorized` or `denied`.
Returns `Promise<String>` - Whether or not the request succeeded or failed; can be `authorized`, `denied`, or `restricted`.

* `not determined` - The Music Library access authorization will prompt the user to authorize or deny. The Promise is resolved after the user selection with either `authorized` or `denied`.
* `denied` - The `Security & Privacy` System Preferences window is opened with the Music Library privacy key highlighted. On open of the `Security & Privacy` window, the Promise is resolved as `denied`.
Expand Down
99 changes: 57 additions & 42 deletions permissions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@
error:nil];
}

std::string StringFromPhotosStatus(PHAuthorizationStatus status) {
switch (status) {
case PHAuthorizationStatusAuthorized:
return kAuthorized;
case PHAuthorizationStatusDenied:
return kDenied;
case PHAuthorizationStatusRestricted:
return kRestricted;
default:
return kNotDetermined;
}
}

std::string
StringFromMusicLibraryStatus(SKCloudServiceAuthorizationStatus status)
API_AVAILABLE(macosx(11)) {
switch (status) {
case SKCloudServiceAuthorizationStatusAuthorized:
return kAuthorized;
case SKCloudServiceAuthorizationStatusDenied:
return kDenied;
case SKCloudServiceAuthorizationStatusRestricted:
return kRestricted;
default:
return kNotDetermined;
}
}

std::string
StringFromSpeechRecognitionStatus(SFSpeechRecognizerAuthorizationStatus status)
API_AVAILABLE(macosx(10.15)) {
switch (status) {
case SFSpeechRecognizerAuthorizationStatusAuthorized:
return kAuthorized;
case SFSpeechRecognizerAuthorizationStatusDenied:
return kDenied;
case SFSpeechRecognizerAuthorizationStatusRestricted:
return kRestricted;
default:
return kNotDetermined;
}
}

// Open a specific pane in System Preferences Security and Privacy.
void OpenPrefPane(const std::string &pane_string) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
Expand Down Expand Up @@ -168,16 +211,9 @@ bool HasOpenSystemPreferencesDialog() {
// Library access.
std::string MusicLibraryAuthStatus() {
if (@available(macOS 11, *)) {
switch ([SKCloudServiceController authorizationStatus]) {
case SKCloudServiceAuthorizationStatusAuthorized:
return kAuthorized;
case SKCloudServiceAuthorizationStatusDenied:
return kDenied;
case SKCloudServiceAuthorizationStatusRestricted:
return kRestricted;
default:
return kNotDetermined;
}
SKCloudServiceAuthorizationStatus status =
[SKCloudServiceController authorizationStatus];
return StringFromMusicLibraryStatus(status);
}

return kAuthorized;
Expand Down Expand Up @@ -306,16 +342,9 @@ bool HasOpenSystemPreferencesDialog() {
// recognition access.
std::string SpeechRecognitionAuthStatus() {
if (@available(macOS 10.15, *)) {
switch ([SFSpeechRecognizer authorizationStatus]) {
case SFSpeechRecognizerAuthorizationStatusAuthorized:
return kAuthorized;
case SFSpeechRecognizerAuthorizationStatusDenied:
return kDenied;
case SFSpeechRecognizerAuthorizationStatusRestricted:
return kRestricted;
default:
return kNotDetermined;
}
SFSpeechRecognizerAuthorizationStatus status =
[SFSpeechRecognizer authorizationStatus];
return StringFromSpeechRecognitionStatus(status);
}

return kAuthorized;
Expand All @@ -339,16 +368,8 @@ bool HasOpenSystemPreferencesDialog() {
// Returns a status indicating whether or not the user has authorized Photos
// access.
std::string PhotosAuthStatus() {
switch ([PHPhotoLibrary authorizationStatus]) {
case PHAuthorizationStatusAuthorized:
return kAuthorized;
case PHAuthorizationStatusDenied:
return kDenied;
case PHAuthorizationStatusRestricted:
return kRestricted;
default:
return kNotDetermined;
}
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
return StringFromPhotosStatus(status);
}

/***** EXPORTED FUNCTIONS *****/
Expand Down Expand Up @@ -554,11 +575,8 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
const char *granted) {
deferred.Resolve(Napi::String::New(env, granted));
};
tsfn.BlockingCall(
status == SFSpeechRecognizerAuthorizationStatusAuthorized
? "authorized"
: "denied",
callback);
std::string auth_result = StringFromSpeechRecognitionStatus(status);
tsfn.BlockingCall(auth_result.c_str(), callback);
tsfn.Release();
}];
} else if (auth_status == kDenied) {
Expand Down Expand Up @@ -593,9 +611,8 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
const char *granted) {
deferred.Resolve(Napi::String::New(env, granted));
};
tsfn.BlockingCall(status == PHAuthorizationStatusAuthorized ? "authorized"
: "denied",
callback);
std::string auth_result = StringFromPhotosStatus(status);
tsfn.BlockingCall(auth_result.c_str(), callback);
tsfn.Release();
}];
} else if (auth_status == kDenied) {
Expand Down Expand Up @@ -695,10 +712,8 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
const char *granted) {
deferred.Resolve(Napi::String::New(env, granted));
};

bool granted =
status == SKCloudServiceAuthorizationStatusAuthorized;
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
std::string auth_result = StringFromMusicLibraryStatus(status);
tsfn.BlockingCall(auth_result.c_str(), callback);
tsfn.Release();
}];
} else if (auth_status == kDenied) {
Expand Down

0 comments on commit 03d0b0d

Please sign in to comment.