@@ -489,18 +489,17 @@ bool HasOpenSystemPreferencesDialog() {
489
489
Napi::Promise AskForContactsAccess (const Napi::CallbackInfo &info) {
490
490
Napi::Env env = info.Env ();
491
491
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
492
- Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
492
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New (
493
493
env, Napi::Function::New (env, NoOp), " contactsCallback" , 0 , 1 );
494
494
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
+
496
499
CNContactStore *store = [CNContactStore new ];
497
500
[store
498
501
requestAccessForEntityType: CNEntityTypeContacts
499
502
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
- };
504
503
tsfn.BlockingCall (granted ? " authorized" : " denied" , callback);
505
504
tsfn.Release ();
506
505
}];
@@ -512,41 +511,27 @@ bool HasOpenSystemPreferencesDialog() {
512
511
Napi::Promise AskForCalendarAccess (const Napi::CallbackInfo &info) {
513
512
Napi::Env env = info.Env ();
514
513
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
515
- Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
514
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New (
516
515
env, Napi::Function::New (env, NoOp), " calendarCallback" , 0 , 1 );
517
516
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 ];
519
528
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]);
537
533
} 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];
550
535
}
551
536
552
537
return deferred.Promise ();
@@ -556,26 +541,24 @@ bool HasOpenSystemPreferencesDialog() {
556
541
Napi::Promise AskForRemindersAccess (const Napi::CallbackInfo &info) {
557
542
Napi::Env env = info.Env ();
558
543
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
559
- Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
544
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New (
560
545
env, Napi::Function::New (env, NoOp), " remindersCallback" , 0 , 1 );
561
546
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
+ };
563
550
564
551
EKEventStoreRequestAccessCompletionHandler handler =
565
552
^(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
- };
570
553
tsfn.BlockingCall (granted ? " authorized" : " denied" , callback);
571
554
tsfn.Release ();
572
555
};
573
556
557
+ EKEventStore *store = [EKEventStore new ];
574
558
if (@available (macOS 14.0 , *)) {
575
- [[EKEventStore new ] requestFullAccessToRemindersWithCompletion: handler];
559
+ [store requestFullAccessToRemindersWithCompletion: handler];
576
560
} else {
577
- [[EKEventStore new ] requestAccessToEntityType: EKEntityTypeReminder
578
- completion: handler];
561
+ [store requestAccessToEntityType: EKEntityTypeReminder completion: handler];
579
562
}
580
563
581
564
return deferred.Promise ();
@@ -590,37 +573,33 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
590
573
Napi::Promise AskForCameraAccess (const Napi::CallbackInfo &info) {
591
574
Napi::Env env = info.Env ();
592
575
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
593
- Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
576
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New (
594
577
env, Napi::Function::New (env, NoOp), " cameraCallback" , 0 , 1 );
595
578
579
+ auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
580
+ deferred.Resolve (Napi::String::New (env, status));
581
+ };
582
+
596
583
if (@available (macOS 10.14 , *)) {
597
584
std::string auth_status = MediaAuthStatus (" camera" );
598
585
599
586
if (auth_status == kNotDetermined ) {
600
- __block Napi::ThreadSafeFunction tsfn = ts_fn;
601
587
[AVCaptureDevice
602
588
requestAccessForMediaType: AVMediaTypeVideo
603
589
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
-
609
590
tsfn.BlockingCall (granted ? " authorized" : " denied" ,
610
591
callback);
611
592
tsfn.Release ();
612
593
}];
613
- } else if (auth_status == kDenied ) {
614
- OpenPrefPane (" Privacy_Camera" );
615
-
616
- ts_fn.Release ();
617
- deferred.Resolve (Napi::String::New (env, kDenied ));
618
594
} else {
619
- ts_fn.Release ();
595
+ if (auth_status == kDenied )
596
+ OpenPrefPane (" Privacy_Camera" );
597
+
598
+ tsfn.Release ();
620
599
deferred.Resolve (Napi::String::New (env, auth_status));
621
600
}
622
601
} else {
623
- ts_fn .Release ();
602
+ tsfn .Release ();
624
603
deferred.Resolve (Napi::String::New (env, kAuthorized ));
625
604
}
626
605
@@ -631,35 +610,32 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
631
610
Napi::Promise AskForSpeechRecognitionAccess (const Napi::CallbackInfo &info) {
632
611
Napi::Env env = info.Env ();
633
612
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
634
- Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
613
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New (
635
614
env, Napi::Function::New (env, NoOp), " speechRecognitionCallback" , 0 , 1 );
636
615
616
+ auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
617
+ deferred.Resolve (Napi::String::New (env, status));
618
+ };
619
+
637
620
if (@available (macOS 10.15 , *)) {
638
621
std::string auth_status = SpeechRecognitionAuthStatus ();
639
622
640
623
if (auth_status == kNotDetermined ) {
641
- __block Napi::ThreadSafeFunction tsfn = ts_fn;
642
624
[SFSpeechRecognizer
643
625
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
- };
648
626
std::string auth_result = StringFromSpeechRecognitionStatus (status);
649
627
tsfn.BlockingCall (auth_result.c_str (), callback);
650
628
tsfn.Release ();
651
629
}];
652
- } else if (auth_status == kDenied ) {
653
- OpenPrefPane (" Privacy_SpeechRecognition" );
654
-
655
- ts_fn.Release ();
656
- deferred.Resolve (Napi::String::New (env, kDenied ));
657
630
} else {
658
- ts_fn.Release ();
631
+ if (auth_status == kDenied )
632
+ OpenPrefPane (" Privacy_SpeechRecognition" );
633
+
634
+ tsfn.Release ();
659
635
deferred.Resolve (Napi::String::New (env, auth_status));
660
636
}
661
637
} else {
662
- ts_fn .Release ();
638
+ tsfn .Release ();
663
639
deferred.Resolve (Napi::String::New (env, kAuthorized ));
664
640
}
665
641
@@ -670,25 +646,22 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
670
646
Napi::Promise AskForPhotosAccess (const Napi::CallbackInfo &info) {
671
647
Napi::Env env = info.Env ();
672
648
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
673
- Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
649
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New (
674
650
env, Napi::Function::New (env, NoOp), " photosCallback" , 0 , 1 );
675
651
676
652
std::string access_level = info[0 ].As <Napi::String>().Utf8Value ();
677
653
std::string auth_status = PhotosAuthStatus (access_level);
678
654
655
+ auto callback = [=](Napi::Env env, Napi::Function js_cb,
656
+ const char *granted) {
657
+ deferred.Resolve (Napi::String::New (env, granted));
658
+ };
659
+
679
660
if (auth_status == kNotDetermined ) {
680
- __block Napi::ThreadSafeFunction tsfn = ts_fn;
681
661
if (@available (macOS 10.16 , *)) {
682
662
[PHPhotoLibrary
683
663
requestAuthorizationForAccessLevel: GetPHAccessLevel (access_level)
684
664
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
- };
692
665
tsfn.BlockingCall (
693
666
StringFromPhotosStatus (status)
694
667
.c_str (),
@@ -697,61 +670,52 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
697
670
}];
698
671
} else {
699
672
[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
- };
704
673
tsfn.BlockingCall (StringFromPhotosStatus (status).c_str (), callback);
705
674
tsfn.Release ();
706
675
}];
707
676
}
708
- } else if (auth_status == kDenied ) {
709
- OpenPrefPane (" Privacy_Photos" );
710
-
711
- ts_fn.Release ();
712
- deferred.Resolve (Napi::String::New (env, kDenied ));
713
677
} else {
714
- ts_fn.Release ();
678
+ if (auth_status == kDenied )
679
+ OpenPrefPane (" Privacy_Photos" );
680
+
681
+ tsfn.Release ();
715
682
deferred.Resolve (Napi::String::New (env, auth_status));
716
683
}
684
+
717
685
return deferred.Promise ();
718
686
}
719
687
720
688
// Request Microphone access.
721
689
Napi::Promise AskForMicrophoneAccess (const Napi::CallbackInfo &info) {
722
690
Napi::Env env = info.Env ();
723
691
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
724
- Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
692
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New (
725
693
env, Napi::Function::New (env, NoOp), " microphoneCallback" , 0 , 1 );
726
694
695
+ auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
696
+ deferred.Resolve (Napi::String::New (env, status));
697
+ };
698
+
727
699
if (@available (macOS 10.14 , *)) {
728
700
std::string auth_status = MediaAuthStatus (" microphone" );
729
701
730
702
if (auth_status == kNotDetermined ) {
731
- __block Napi::ThreadSafeFunction tsfn = ts_fn;
732
703
[AVCaptureDevice
733
704
requestAccessForMediaType: AVMediaTypeAudio
734
705
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
-
740
706
tsfn.BlockingCall (granted ? " authorized" : " denied" ,
741
707
callback);
742
708
tsfn.Release ();
743
709
}];
744
- } else if (auth_status == kDenied ) {
745
- OpenPrefPane (" Privacy_Microphone" );
746
-
747
- ts_fn.Release ();
748
- deferred.Resolve (Napi::String::New (env, kDenied ));
749
710
} else {
750
- ts_fn.Release ();
711
+ if (auth_status == kDenied )
712
+ OpenPrefPane (" Privacy_Microphone" );
713
+
714
+ tsfn.Release ();
751
715
deferred.Resolve (Napi::String::New (env, auth_status));
752
716
}
753
717
} else {
754
- ts_fn .Release ();
718
+ tsfn .Release ();
755
719
deferred.Resolve (Napi::String::New (env, kAuthorized ));
756
720
}
757
721
@@ -769,12 +733,10 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
769
733
770
734
if (auth_status == kNotDetermined ) {
771
735
IOHIDRequestAccess (GetInputMonitoringAccessType (access_level));
772
- deferred.Resolve (Napi::String::New (env, kDenied ));
773
- } else if (auth_status == kDenied ) {
774
- OpenPrefPane (" Privacy_ListenEvent" );
775
-
776
736
deferred.Resolve (Napi::String::New (env, kDenied ));
777
737
} else {
738
+ if (auth_status == kDenied )
739
+ OpenPrefPane (" Privacy_ListenEvent" );
778
740
deferred.Resolve (Napi::String::New (env, auth_status));
779
741
}
780
742
} else {
@@ -788,35 +750,32 @@ void AskForFullDiskAccess(const Napi::CallbackInfo &info) {
788
750
Napi::Promise AskForMusicLibraryAccess (const Napi::CallbackInfo &info) {
789
751
Napi::Env env = info.Env ();
790
752
Napi::Promise::Deferred deferred = Napi::Promise::Deferred::New (env);
791
- Napi::ThreadSafeFunction ts_fn = Napi::ThreadSafeFunction::New (
753
+ Napi::ThreadSafeFunction tsfn = Napi::ThreadSafeFunction::New (
792
754
env, Napi::Function::New (env, NoOp), " musicLibraryCallback" , 0 , 1 );
793
755
756
+ auto callback = [=](Napi::Env env, Napi::Function js_cb, const char *status) {
757
+ deferred.Resolve (Napi::String::New (env, status));
758
+ };
759
+
794
760
if (@available (macOS 10.16 , *)) {
795
761
std::string auth_status = MusicLibraryAuthStatus ();
796
762
797
763
if (auth_status == kNotDetermined ) {
798
- __block Napi::ThreadSafeFunction tsfn = ts_fn;
799
764
[SKCloudServiceController
800
765
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
- };
805
766
tsfn.BlockingCall (StringFromMusicLibraryStatus (status).c_str (),
806
767
callback);
807
768
tsfn.Release ();
808
769
}];
809
- } else if (auth_status == kDenied ) {
810
- OpenPrefPane (" Privacy_Media" );
811
-
812
- ts_fn.Release ();
813
- deferred.Resolve (Napi::String::New (env, kDenied ));
814
770
} else {
815
- ts_fn.Release ();
771
+ if (auth_status == kDenied )
772
+ OpenPrefPane (" Privacy_Media" );
773
+
774
+ tsfn.Release ();
816
775
deferred.Resolve (Napi::String::New (env, auth_status));
817
776
}
818
777
} else {
819
- ts_fn .Release ();
778
+ tsfn .Release ();
820
779
deferred.Resolve (Napi::String::New (env, kAuthorized ));
821
780
}
822
781
0 commit comments