Skip to content

Commit 9bc6648

Browse files
committed
refactor: clean things up a bit
1 parent 49ccc2d commit 9bc6648

File tree

1 file changed

+82
-123
lines changed

1 file changed

+82
-123
lines changed

permissions.mm

Lines changed: 82 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -489,18 +489,17 @@ bool HasOpenSystemPreferencesDialog() {
489489
Napi::Promise AskForContactsAccess(const Napi::CallbackInfo &info) {
490490
Napi::Env env = info.Env();
491491
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
492-
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
492+
Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
493493
env, Napi::Function::New(env, NoOp), "contactsCallback", 0, 1);
494494

495-
__block Napi::ThreadSafeFunction tsfn = ts_fn;
495+
auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
496+
deferred.Resolve(Napi::String::New(env, status));
497+
};
498+
496499
CNContactStore *store = [CNContactStore new];
497500
[store
498501
requestAccessForEntityType:CNEntityTypeContacts
499502
completionHandler:^(BOOL granted, NSError *error) {
500-
auto callback = [=](Napi::Env env, Napi::Function js_cb,
501-
const char *granted) {
502-
deferred.Resolve(Napi::String::New(env, granted));
503-
};
504503
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
505504
tsfn.Release();
506505
}];
@@ -512,41 +511,27 @@ bool HasOpenSystemPreferencesDialog() {
512511
Napi::Promise AskForCalendarAccess(const Napi::CallbackInfo &info) {
513512
Napi::Env env = info.Env();
514513
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
515-
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
514+
Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
516515
env, Napi::Function::New(env, NoOp), "calendarCallback", 0, 1);
517516

518-
__block Napi::ThreadSafeFunction tsfn = ts_fn;
517+
auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
518+
deferred.Resolve(Napi::String::New(env, status));
519+
};
520+
521+
EKEventStoreRequestAccessCompletionHandler handler =
522+
^(BOOL granted, NSError *error) {
523+
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
524+
tsfn.Release();
525+
};
526+
527+
EKEventStore *store = [EKEventStore new];
519528
if (@available(macOS 14.0, *)) {
520-
const std::string access_level = info[0].As<Napi::String>().Utf8Value();
521-
522-
EKEventStoreRequestAccessCompletionHandler handler =
523-
^(BOOL granted, NSError *error) {
524-
auto callback = [=](Napi::Env env, Napi::Function js_cb,
525-
const char *granted) {
526-
deferred.Resolve(Napi::String::New(env, granted));
527-
};
528-
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
529-
tsfn.Release();
530-
};
531-
532-
if (access_level == "full") {
533-
[[EKEventStore new] requestFullAccessToEventsWithCompletion:handler];
534-
} else {
535-
[[EKEventStore new] requestWriteOnlyAccessToEventsWithCompletion:handler];
536-
}
529+
const std::string level = info[0].As<Napi::String>().Utf8Value();
530+
(level == "full"
531+
? [store requestFullAccessToEventsWithCompletion:handler]
532+
: [store requestWriteOnlyAccessToEventsWithCompletion:handler]);
537533
} else {
538-
[[EKEventStore new]
539-
requestAccessToEntityType:EKEntityTypeEvent
540-
completion:^(BOOL granted, NSError *error) {
541-
auto callback = [=](Napi::Env env,
542-
Napi::Function js_cb,
543-
const char *granted) {
544-
deferred.Resolve(Napi::String::New(env, granted));
545-
};
546-
tsfn.BlockingCall(granted ? "authorized" : "denied",
547-
callback);
548-
tsfn.Release();
549-
}];
534+
[store requestAccessToEntityType:EKEntityTypeEvent completion:handler];
550535
}
551536

552537
return deferred.Promise();
@@ -556,26 +541,24 @@ bool HasOpenSystemPreferencesDialog() {
556541
Napi::Promise AskForRemindersAccess(const Napi::CallbackInfo &info) {
557542
Napi::Env env = info.Env();
558543
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
559-
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
544+
Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
560545
env, Napi::Function::New(env, NoOp), "remindersCallback", 0, 1);
561546

562-
__block Napi::ThreadSafeFunction tsfn = ts_fn;
547+
auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
548+
deferred.Resolve(Napi::String::New(env, status));
549+
};
563550

564551
EKEventStoreRequestAccessCompletionHandler handler =
565552
^(BOOL granted, NSError *error) {
566-
auto callback = [=](Napi::Env env, Napi::Function prom_cb,
567-
const char *granted) {
568-
deferred.Resolve(Napi::String::New(env, granted));
569-
};
570553
tsfn.BlockingCall(granted ? "authorized" : "denied", callback);
571554
tsfn.Release();
572555
};
573556

557+
EKEventStore *store = [EKEventStore new];
574558
if (@available(macOS 14.0, *)) {
575-
[[EKEventStore new] requestFullAccessToRemindersWithCompletion:handler];
559+
[store requestFullAccessToRemindersWithCompletion:handler];
576560
} else {
577-
[[EKEventStore new] requestAccessToEntityType:EKEntityTypeReminder
578-
completion:handler];
561+
[store requestAccessToEntityType:EKEntityTypeReminder completion:handler];
579562
}
580563

581564
return deferred.Promise();
@@ -590,37 +573,33 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
590573
Napi::Promise AskForCameraAccess(const Napi::CallbackInfo &info) {
591574
Napi::Env env = info.Env();
592575
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
593-
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
576+
Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
594577
env, Napi::Function::New(env, NoOp), "cameraCallback", 0, 1);
595578

579+
auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
580+
deferred.Resolve(Napi::String::New(env, status));
581+
};
582+
596583
if (@available(macOS 10.14, *)) {
597584
std::string auth_status = MediaAuthStatus("camera");
598585

599586
if (auth_status == kNotDetermined) {
600-
__block Napi::ThreadSafeFunction tsfn = ts_fn;
601587
[AVCaptureDevice
602588
requestAccessForMediaType:AVMediaTypeVideo
603589
completionHandler:^(BOOL granted) {
604-
auto callback = [=](Napi::Env env, Napi::Function js_cb,
605-
const char *granted) {
606-
deferred.Resolve(Napi::String::New(env, granted));
607-
};
608-
609590
tsfn.BlockingCall(granted ? "authorized" : "denied",
610591
callback);
611592
tsfn.Release();
612593
}];
613-
} else if (auth_status == kDenied) {
614-
OpenPrefPane("Privacy_Camera");
615-
616-
ts_fn.Release();
617-
deferred.Resolve(Napi::String::New(env, kDenied));
618594
} else {
619-
ts_fn.Release();
595+
if (auth_status == kDenied)
596+
OpenPrefPane("Privacy_Camera");
597+
598+
tsfn.Release();
620599
deferred.Resolve(Napi::String::New(env, auth_status));
621600
}
622601
} else {
623-
ts_fn.Release();
602+
tsfn.Release();
624603
deferred.Resolve(Napi::String::New(env, kAuthorized));
625604
}
626605

@@ -631,35 +610,32 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
631610
Napi::Promise AskForSpeechRecognitionAccess(const Napi::CallbackInfo &info) {
632611
Napi::Env env = info.Env();
633612
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
634-
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
613+
Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
635614
env, Napi::Function::New(env, NoOp), "speechRecognitionCallback", 0, 1);
636615

616+
auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
617+
deferred.Resolve(Napi::String::New(env, status));
618+
};
619+
637620
if (@available(macOS 10.15, *)) {
638621
std::string auth_status = SpeechRecognitionAuthStatus();
639622

640623
if (auth_status == kNotDetermined) {
641-
__block Napi::ThreadSafeFunction tsfn = ts_fn;
642624
[SFSpeechRecognizer
643625
requestAuthorization:^(SFSpeechRecognizerAuthorizationStatus status) {
644-
auto callback = [=](Napi::Env env, Napi::Function js_cb,
645-
const char *granted) {
646-
deferred.Resolve(Napi::String::New(env, granted));
647-
};
648626
std::string auth_result = StringFromSpeechRecognitionStatus(status);
649627
tsfn.BlockingCall(auth_result.c_str(), callback);
650628
tsfn.Release();
651629
}];
652-
} else if (auth_status == kDenied) {
653-
OpenPrefPane("Privacy_SpeechRecognition");
654-
655-
ts_fn.Release();
656-
deferred.Resolve(Napi::String::New(env, kDenied));
657630
} else {
658-
ts_fn.Release();
631+
if (auth_status == kDenied)
632+
OpenPrefPane("Privacy_SpeechRecognition");
633+
634+
tsfn.Release();
659635
deferred.Resolve(Napi::String::New(env, auth_status));
660636
}
661637
} else {
662-
ts_fn.Release();
638+
tsfn.Release();
663639
deferred.Resolve(Napi::String::New(env, kAuthorized));
664640
}
665641

@@ -670,25 +646,22 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
670646
Napi::Promise AskForPhotosAccess(const Napi::CallbackInfo &info) {
671647
Napi::Env env = info.Env();
672648
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
673-
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
649+
Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
674650
env, Napi::Function::New(env, NoOp), "photosCallback", 0, 1);
675651

676652
std::string access_level = info[0].As<Napi::String>().Utf8Value();
677653
std::string auth_status = PhotosAuthStatus(access_level);
678654

655+
auto callback = [=](Napi::Env env, Napi::Function js_cb,
656+
const char *granted) {
657+
deferred.Resolve(Napi::String::New(env, granted));
658+
};
659+
679660
if (auth_status == kNotDetermined) {
680-
__block Napi::ThreadSafeFunction tsfn = ts_fn;
681661
if (@available(macOS 10.16, *)) {
682662
[PHPhotoLibrary
683663
requestAuthorizationForAccessLevel:GetPHAccessLevel(access_level)
684664
handler:^(PHAuthorizationStatus status) {
685-
auto callback =
686-
[=](Napi::Env env,
687-
Napi::Function js_cb,
688-
const char *granted) {
689-
deferred.Resolve(Napi::String::New(
690-
env, granted));
691-
};
692665
tsfn.BlockingCall(
693666
StringFromPhotosStatus(status)
694667
.c_str(),
@@ -697,61 +670,52 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
697670
}];
698671
} else {
699672
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
700-
auto callback = [=](Napi::Env env, Napi::Function js_cb,
701-
const char *granted) {
702-
deferred.Resolve(Napi::String::New(env, granted));
703-
};
704673
tsfn.BlockingCall(StringFromPhotosStatus(status).c_str(), callback);
705674
tsfn.Release();
706675
}];
707676
}
708-
} else if (auth_status == kDenied) {
709-
OpenPrefPane("Privacy_Photos");
710-
711-
ts_fn.Release();
712-
deferred.Resolve(Napi::String::New(env, kDenied));
713677
} else {
714-
ts_fn.Release();
678+
if (auth_status == kDenied)
679+
OpenPrefPane("Privacy_Photos");
680+
681+
tsfn.Release();
715682
deferred.Resolve(Napi::String::New(env, auth_status));
716683
}
684+
717685
return deferred.Promise();
718686
}
719687

720688
// Request Microphone access.
721689
Napi::Promise AskForMicrophoneAccess(const Napi::CallbackInfo &info) {
722690
Napi::Env env = info.Env();
723691
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
724-
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
692+
Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
725693
env, Napi::Function::New(env, NoOp), "microphoneCallback", 0, 1);
726694

695+
auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
696+
deferred.Resolve(Napi::String::New(env, status));
697+
};
698+
727699
if (@available(macOS 10.14, *)) {
728700
std::string auth_status = MediaAuthStatus("microphone");
729701

730702
if (auth_status == kNotDetermined) {
731-
__block Napi::ThreadSafeFunction tsfn = ts_fn;
732703
[AVCaptureDevice
733704
requestAccessForMediaType:AVMediaTypeAudio
734705
completionHandler:^(BOOL granted) {
735-
auto callback = [=](Napi::Env env, Napi::Function js_cb,
736-
const char *granted) {
737-
deferred.Resolve(Napi::String::New(env, granted));
738-
};
739-
740706
tsfn.BlockingCall(granted ? "authorized" : "denied",
741707
callback);
742708
tsfn.Release();
743709
}];
744-
} else if (auth_status == kDenied) {
745-
OpenPrefPane("Privacy_Microphone");
746-
747-
ts_fn.Release();
748-
deferred.Resolve(Napi::String::New(env, kDenied));
749710
} else {
750-
ts_fn.Release();
711+
if (auth_status == kDenied)
712+
OpenPrefPane("Privacy_Microphone");
713+
714+
tsfn.Release();
751715
deferred.Resolve(Napi::String::New(env, auth_status));
752716
}
753717
} else {
754-
ts_fn.Release();
718+
tsfn.Release();
755719
deferred.Resolve(Napi::String::New(env, kAuthorized));
756720
}
757721

@@ -769,12 +733,10 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
769733

770734
if (auth_status == kNotDetermined) {
771735
IOHIDRequestAccess(GetInputMonitoringAccessType(access_level));
772-
deferred.Resolve(Napi::String::New(env, kDenied));
773-
} else if (auth_status == kDenied) {
774-
OpenPrefPane("Privacy_ListenEvent");
775-
776736
deferred.Resolve(Napi::String::New(env, kDenied));
777737
} else {
738+
if (auth_status == kDenied)
739+
OpenPrefPane("Privacy_ListenEvent");
778740
deferred.Resolve(Napi::String::New(env, auth_status));
779741
}
780742
} else {
@@ -788,35 +750,32 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
788750
Napi::Promise AskForMusicLibraryAccess(const Napi::CallbackInfo &info) {
789751
Napi::Env env = info.Env();
790752
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New(env);
791-
Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New(
753+
Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New(
792754
env, Napi::Function::New(env, NoOp), "musicLibraryCallback", 0, 1);
793755

756+
auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
757+
deferred.Resolve(Napi::String::New(env, status));
758+
};
759+
794760
if (@available(macOS 10.16, *)) {
795761
std::string auth_status = MusicLibraryAuthStatus();
796762

797763
if (auth_status == kNotDetermined) {
798-
__block Napi::ThreadSafeFunction tsfn = ts_fn;
799764
[SKCloudServiceController
800765
requestAuthorization:^(SKCloudServiceAuthorizationStatus status) {
801-
auto callback = [=](Napi::Env env, Napi::Function js_cb,
802-
const char *granted) {
803-
deferred.Resolve(Napi::String::New(env, granted));
804-
};
805766
tsfn.BlockingCall(StringFromMusicLibraryStatus(status).c_str(),
806767
callback);
807768
tsfn.Release();
808769
}];
809-
} else if (auth_status == kDenied) {
810-
OpenPrefPane("Privacy_Media");
811-
812-
ts_fn.Release();
813-
deferred.Resolve(Napi::String::New(env, kDenied));
814770
} else {
815-
ts_fn.Release();
771+
if (auth_status == kDenied)
772+
OpenPrefPane("Privacy_Media");
773+
774+
tsfn.Release();
816775
deferred.Resolve(Napi::String::New(env, auth_status));
817776
}
818777
} else {
819-
ts_fn.Release();
778+
tsfn.Release();
820779
deferred.Resolve(Napi::String::New(env, kAuthorized));
821780
}
822781

0 commit comments

Comments
 (0)