Skip to content

Commit

Permalink
refactor: extract OpenPrefPane helper
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed May 21, 2022
1 parent 45a21e4 commit 1b2e2f0
Showing 1 changed file with 19 additions and 43 deletions.
62 changes: 19 additions & 43 deletions permissions.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
error:nil];
}

// Open a specific pane in System Preferences Security and Privacy.
void OpenPrefPane(const std::string &pane_string) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string = [NSString
stringWithFormat:
@"x-apple.systempreferences:com.apple.preference.security?%s",
pane_string.c_str()];
[workspace openURL:[NSURL URLWithString:pref_string]];
}

// Dummy value to pass into function parameter for ThreadSafeFunction.
Napi::Value NoOp(const Napi::CallbackInfo &info) {
return info.Env().Undefined();
Expand Down Expand Up @@ -491,10 +501,7 @@ bool HasOpenSystemPreferencesDialog() {

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

// Request Camera access.
Expand Down Expand Up @@ -522,11 +529,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
tsfn.Release();
}];
} else if (auth_status == kDenied) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
@"security?Privacy_Camera";

[workspace openURL:[NSURL URLWithString:pref_string]];
OpenPrefPane("Privacy_Camera");

ts_fn.Release();
deferred.Resolve(Napi::String::New(env, kDenied));
Expand Down Expand Up @@ -568,11 +571,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
tsfn.Release();
}];
} else if (auth_status == kDenied) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
@"security?Privacy_SpeechRecognition";

[workspace openURL:[NSURL URLWithString:pref_string]];
OpenPrefPane("Privacy_SpeechRecognition");

ts_fn.Release();
deferred.Resolve(Napi::String::New(env, kDenied));
Expand Down Expand Up @@ -611,11 +610,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
tsfn.Release();
}];
} else if (auth_status == kDenied) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
@"security?Privacy_Photos";

[workspace openURL:[NSURL URLWithString:pref_string]];
OpenPrefPane("Privacy_Photos");

ts_fn.Release();
deferred.Resolve(Napi::String::New(env, kDenied));
Expand Down Expand Up @@ -656,11 +651,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
tsfn.Release();
}];
} else if (auth_status == kDenied) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
@"security?Privacy_Microphone";

[workspace openURL:[NSURL URLWithString:pref_string]];
OpenPrefPane("Privacy_Microphone");

ts_fn.Release();
deferred.Resolve(Napi::String::New(env, kDenied));
Expand Down Expand Up @@ -688,11 +679,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
IOHIDRequestAccess(kIOHIDRequestTypeListenEvent);
deferred.Resolve(Napi::String::New(env, kDenied));
} else if (auth_status == kDenied) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
@"security?Privacy_ListenEvent";

[workspace openURL:[NSURL URLWithString:pref_string]];
OpenPrefPane("Privacy_ListenEvent");

deferred.Resolve(Napi::String::New(env, kDenied));
} else {
Expand Down Expand Up @@ -730,11 +717,7 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
tsfn.Release();
}];
} else if (auth_status == kDenied) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
@"security?Privacy_Media";

[workspace openURL:[NSURL URLWithString:pref_string]];
OpenPrefPane("Privacy_Media");

ts_fn.Release();
deferred.Resolve(Napi::String::New(env, kDenied));
Expand Down Expand Up @@ -768,11 +751,7 @@ void AskForScreenCaptureAccess(const Napi::CallbackInfo &info) {
CFRelease(stream);
} else {
if (!HasOpenSystemPreferencesDialog()) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string =
@"x-apple.systempreferences:com.apple.preference."
@"security?Privacy_ScreenCapture";
[workspace openURL:[NSURL URLWithString:pref_string]];
OpenPrefPane("Privacy_ScreenCapture");
}
}
}
Expand All @@ -784,10 +763,7 @@ void AskForAccessibilityAccess(const Napi::CallbackInfo &info) {
bool trusted = AXIsProcessTrustedWithOptions((CFDictionaryRef)options);

if (!trusted) {
NSWorkspace *workspace = [[NSWorkspace alloc] init];
NSString *pref_string = @"x-apple.systempreferences:com.apple.preference."
@"security?Privacy_Accessibility";
[workspace openURL:[NSURL URLWithString:pref_string]];
OpenPrefPane("Privacy_Accessibility");
}
}

Expand Down

1 comment on commit 1b2e2f0

@KishanBagaria
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

19 additions and 43 deletions

nice

Please sign in to comment.