Skip to content

Commit 1b2e2f0

Browse files
committed
refactor: extract OpenPrefPane helper
1 parent 45a21e4 commit 1b2e2f0

File tree

1 file changed

+19
-43
lines changed

1 file changed

+19
-43
lines changed

permissions.mm

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

34+
// Open a specific pane in System Preferences Security and Privacy.
35+
void OpenPrefPane(const std::string &pane_string) {
36+
NSWorkspace *workspace = [[NSWorkspace alloc] init];
37+
NSString *pref_string = [NSString
38+
stringWithFormat:
39+
@"x-apple.systempreferences:com.apple.preference.security?%s",
40+
pane_string.c_str()];
41+
[workspace openURL:[NSURL URLWithString:pref_string]];
42+
}
43+
3444
// Dummy value to pass into function parameter for ThreadSafeFunction.
3545
Napi::Value NoOp(const Napi::CallbackInfo &info) {
3646
return info.Env().Undefined();
@@ -491,10 +501,7 @@ bool HasOpenSystemPreferencesDialog() {
491501

492502
// Request Full Disk Access.
493503
void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
494-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
495-
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
496-
@"security?Privacy_AllFiles";
497-
[workspace openURL:[NSURL URLWithString:pref_string]];
504+
OpenPrefPane("Privacy_AllFiles");
498505
}
499506

500507
// Request Camera access.
@@ -522,11 +529,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
522529
tsfn.Release();
523530
}];
524531
} else if (auth_status == kDenied) {
525-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
526-
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
527-
@"security?Privacy_Camera";
528-
529-
[workspace openURL:[NSURL URLWithString:pref_string]];
532+
OpenPrefPane("Privacy_Camera");
530533

531534
ts_fn.Release();
532535
deferred.Resolve(Napi::String::New(env, kDenied));
@@ -568,11 +571,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
568571
tsfn.Release();
569572
}];
570573
} else if (auth_status == kDenied) {
571-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
572-
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
573-
@"security?Privacy_SpeechRecognition";
574-
575-
[workspace openURL:[NSURL URLWithString:pref_string]];
574+
OpenPrefPane("Privacy_SpeechRecognition");
576575

577576
ts_fn.Release();
578577
deferred.Resolve(Napi::String::New(env, kDenied));
@@ -611,11 +610,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
611610
tsfn.Release();
612611
}];
613612
} else if (auth_status == kDenied) {
614-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
615-
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
616-
@"security?Privacy_Photos";
617-
618-
[workspace openURL:[NSURL URLWithString:pref_string]];
613+
OpenPrefPane("Privacy_Photos");
619614

620615
ts_fn.Release();
621616
deferred.Resolve(Napi::String::New(env, kDenied));
@@ -656,11 +651,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
656651
tsfn.Release();
657652
}];
658653
} else if (auth_status == kDenied) {
659-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
660-
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
661-
@"security?Privacy_Microphone";
662-
663-
[workspace openURL:[NSURL URLWithString:pref_string]];
654+
OpenPrefPane("Privacy_Microphone");
664655

665656
ts_fn.Release();
666657
deferred.Resolve(Napi::String::New(env, kDenied));
@@ -688,11 +679,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
688679
IOHIDRequestAccess(kIOHIDRequestTypeListenEvent);
689680
deferred.Resolve(Napi::String::New(env, kDenied));
690681
} else if (auth_status == kDenied) {
691-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
692-
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
693-
@"security?Privacy_ListenEvent";
694-
695-
[workspace openURL:[NSURL URLWithString:pref_string]];
682+
OpenPrefPane("Privacy_ListenEvent");
696683

697684
deferred.Resolve(Napi::String::New(env, kDenied));
698685
} else {
@@ -730,11 +717,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
730717
tsfn.Release();
731718
}];
732719
} else if (auth_status == kDenied) {
733-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
734-
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
735-
@"security?Privacy_Media";
736-
737-
[workspace openURL:[NSURL URLWithString:pref_string]];
720+
OpenPrefPane("Privacy_Media");
738721

739722
ts_fn.Release();
740723
deferred.Resolve(Napi::String::New(env, kDenied));
@@ -768,11 +751,7 @@ void AskForScreenCaptureAccess(const Napi::CallbackInfo &info) {
768751
CFRelease(stream);
769752
} else {
770753
if (!HasOpenSystemPreferencesDialog()) {
771-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
772-
NSString *pref_string =
773-
@"x-apple.systempreferences:com.apple.preference."
774-
@"security?Privacy_ScreenCapture";
775-
[workspace openURL:[NSURL URLWithString:pref_string]];
754+
OpenPrefPane("Privacy_ScreenCapture");
776755
}
777756
}
778757
}
@@ -784,10 +763,7 @@ void AskForAccessibilityAccess(const Napi::CallbackInfo &info) {
784763
bool trusted = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);
785764

786765
if (!trusted) {
787-
NSWorkspace *workspace = [[NSWorkspace alloc] init];
788-
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
789-
@"security?Privacy_Accessibility";
790-
[workspace openURL:[NSURL URLWithString:pref_string]];
766+
OpenPrefPane("Privacy_Accessibility");
791767
}
792768
}
793769

0 commit comments

Comments
 (0)