Skip to content

Commit 03d0b0d

Browse files
committed
refactor: pull out string converters
1 parent f9cc660 commit 03d0b0d

File tree

2 files changed

+60
-44
lines changed

2 files changed

+60
-44
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ askForCalendarAccess().then(status => {
136136

137137
### `permissions.askForSpeechRecognitionAccess()`
138138

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

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

143143
* `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`.
144144
* `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`.
145+
* `restricted` - The Promise is resolved as `restricted`.
145146

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

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

304305
### `permissions.askForMusicLibraryAccess()`
305306

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

308309
* `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`.
309310
* `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`.

permissions.mm

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,49 @@
3131
error:nil];
3232
}
3333

34+
std::string StringFromPhotosStatus(PHAuthorizationStatus status) {
35+
switch (status) {
36+
case PHAuthorizationStatusAuthorized:
37+
return kAuthorized;
38+
case PHAuthorizationStatusDenied:
39+
return kDenied;
40+
case PHAuthorizationStatusRestricted:
41+
return kRestricted;
42+
default:
43+
return kNotDetermined;
44+
}
45+
}
46+
47+
std::string
48+
StringFromMusicLibraryStatus(SKCloudServiceAuthorizationStatus status)
49+
API_AVAILABLE(macosx(11)) {
50+
switch (status) {
51+
case SKCloudServiceAuthorizationStatusAuthorized:
52+
return kAuthorized;
53+
case SKCloudServiceAuthorizationStatusDenied:
54+
return kDenied;
55+
case SKCloudServiceAuthorizationStatusRestricted:
56+
return kRestricted;
57+
default:
58+
return kNotDetermined;
59+
}
60+
}
61+
62+
std::string
63+
StringFromSpeechRecognitionStatus(SFSpeechRecognizerAuthorizationStatus status)
64+
API_AVAILABLE(macosx(10.15)) {
65+
switch (status) {
66+
case SFSpeechRecognizerAuthorizationStatusAuthorized:
67+
return kAuthorized;
68+
case SFSpeechRecognizerAuthorizationStatusDenied:
69+
return kDenied;
70+
case SFSpeechRecognizerAuthorizationStatusRestricted:
71+
return kRestricted;
72+
default:
73+
return kNotDetermined;
74+
}
75+
}
76+
3477
// Open a specific pane in System Preferences Security and Privacy.
3578
void OpenPrefPane(const std::string &pane_string) {
3679
NSWorkspace *workspace = [[NSWorkspace alloc] init];
@@ -168,16 +211,9 @@ bool HasOpenSystemPreferencesDialog() {
168211
// Library access.
169212
std::string MusicLibraryAuthStatus() {
170213
if (@available(macOS 11, *)) {
171-
switch ([SKCloudServiceController authorizationStatus]) {
172-
case SKCloudServiceAuthorizationStatusAuthorized:
173-
return kAuthorized;
174-
case SKCloudServiceAuthorizationStatusDenied:
175-
return kDenied;
176-
case SKCloudServiceAuthorizationStatusRestricted:
177-
return kRestricted;
178-
default:
179-
return kNotDetermined;
180-
}
214+
SKCloudServiceAuthorizationStatus status =
215+
[SKCloudServiceController authorizationStatus];
216+
return StringFromMusicLibraryStatus(status);
181217
}
182218

183219
return kAuthorized;
@@ -306,16 +342,9 @@ bool HasOpenSystemPreferencesDialog() {
306342
// recognition access.
307343
std::string SpeechRecognitionAuthStatus() {
308344
if (@available(macOS 10.15, *)) {
309-
switch ([SFSpeechRecognizer authorizationStatus]) {
310-
case SFSpeechRecognizerAuthorizationStatusAuthorized:
311-
return kAuthorized;
312-
case SFSpeechRecognizerAuthorizationStatusDenied:
313-
return kDenied;
314-
case SFSpeechRecognizerAuthorizationStatusRestricted:
315-
return kRestricted;
316-
default:
317-
return kNotDetermined;
318-
}
345+
SFSpeechRecognizerAuthorizationStatus status =
346+
[SFSpeechRecognizer authorizationStatus];
347+
return StringFromSpeechRecognitionStatus(status);
319348
}
320349

321350
return kAuthorized;
@@ -339,16 +368,8 @@ bool HasOpenSystemPreferencesDialog() {
339368
// Returns a status indicating whether or not the user has authorized Photos
340369
// access.
341370
std::string PhotosAuthStatus() {
342-
switch ([PHPhotoLibrary authorizationStatus]) {
343-
case PHAuthorizationStatusAuthorized:
344-
return kAuthorized;
345-
case PHAuthorizationStatusDenied:
346-
return kDenied;
347-
case PHAuthorizationStatusRestricted:
348-
return kRestricted;
349-
default:
350-
return kNotDetermined;
351-
}
371+
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
372+
return StringFromPhotosStatus(status);
352373
}
353374

354375
/***** EXPORTED FUNCTIONS *****/
@@ -554,11 +575,8 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
554575
const char *granted) {
555576
deferred.Resolve(Napi::String::New(env, granted));
556577
};
557-
tsfn.BlockingCall(
558-
status == SFSpeechRecognizerAuthorizationStatusAuthorized
559-
? "authorized"
560-
: "denied",
561-
callback);
578+
std::string auth_result = StringFromSpeechRecognitionStatus(status);
579+
tsfn.BlockingCall(auth_result.c_str(), callback);
562580
tsfn.Release();
563581
}];
564582
} else if (auth_status == kDenied) {
@@ -593,9 +611,8 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
593611
const char *granted) {
594612
deferred.Resolve(Napi::String::New(env, granted));
595613
};
596-
tsfn.BlockingCall(status == PHAuthorizationStatusAuthorized ? "authorized"
597-
: "denied",
598-
callback);
614+
std::string auth_result = StringFromPhotosStatus(status);
615+
tsfn.BlockingCall(auth_result.c_str(), callback);
599616
tsfn.Release();
600617
}];
601618
} else if (auth_status == kDenied) {
@@ -695,10 +712,8 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
695712
const char *granted) {
696713
deferred.Resolve(Napi::String::New(env, granted));
697714
};
698-
699-
bool granted =
700-
status == SKCloudServiceAuthorizationStatusAuthorized;
701-
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
715+
std::string auth_result = StringFromMusicLibraryStatus(status);
716+
tsfn.BlockingCall(auth_result.c_str(), callback);
702717
tsfn.Release();
703718
}];
704719
} else if (auth_status == kDenied) {

0 commit comments

Comments
 (0)