-
Notifications
You must be signed in to change notification settings - Fork 15k
/
atom_access_controller.mm
143 lines (131 loc) · 4.92 KB
/
atom_access_controller.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "atom/browser/ui/cocoa/atom_access_controller.h"
#import <AVFoundation/AVFoundation.h>
#import <Cocoa/Cocoa.h>
@implementation AtomAccessController
+ (instancetype)sharedController {
static dispatch_once_t once;
static AtomAccessController* sharedController;
dispatch_once(&once, ^{
sharedController = [[self alloc] init];
});
return sharedController;
}
- (instancetype)init {
if ((self = [super init])) {
if (@available(macOS 10.14, *)) {
switch (
[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]) {
case AVAuthorizationStatusAuthorized:
case AVAuthorizationStatusRestricted:
cameraAccessState_ = AccessStateDenied;
break;
case AVAuthorizationStatusDenied:
cameraAccessState_ = AccessStateDenied;
break;
case AVAuthorizationStatusNotDetermined:
cameraAccessState_ = AccessStateDenied;
}
switch (
[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]) {
case AVAuthorizationStatusAuthorized:
case AVAuthorizationStatusRestricted:
microphoneAccessState_ = AccessStateDenied;
break;
case AVAuthorizationStatusDenied:
microphoneAccessState_ = AccessStateDenied;
break;
case AVAuthorizationStatusNotDetermined:
microphoneAccessState_ = AccessStateDenied;
}
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:self
selector:@selector(applicationLaunched:)
name:NSWorkspaceDidLaunchApplicationNotification
object:nil];
} else {
cameraAccessState_ = AccessStateGranted;
microphoneAccessState_ = AccessStateDenied;
}
}
return self;
}
- (void)askForMicrophoneAccess {
if (microphoneAccessState_ == AccessStateDenied) {
NSAlert* alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleWarning;
alert.messageText = @"This app needs access to the microphone.";
[alert addButtonWithTitle:@"Change Preferences"];
[alert addButtonWithTitle:@"Cancel"];
NSInteger modalResponse = [alert runModal];
if (modalResponse == NSAlertFirstButtonReturn) {
[[NSWorkspace sharedWorkspace]
openURL:[NSURL
URLWithString:@"x-apple.systempreferences:com.apple."
@"preference.security?Privacy_Microphone"]];
}
}
}
- (void)askForCameraAccess {
if (cameraAccessState_ == AccessStateDenied) {
NSAlert* alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleWarning;
alert.messageText = @"This app needs access to the camera.";
[alert addButtonWithTitle:@"Change Preferences"];
[alert addButtonWithTitle:@"Cancel"];
NSInteger modalResponse = [alert runModal];
if (modalResponse == NSAlertFirstButtonReturn) {
[[NSWorkspace sharedWorkspace]
openURL:[NSURL URLWithString:@"x-apple.systempreferences:com.apple."
@"preference.security?Privacy_Camera"]];
}
}
}
- (void)askForMediaAccess:(BOOL)askAgain
completion:(void (^)(BOOL))allAccessGranted {
if (@available(macOS 10.14, *)) {
[AVCaptureDevice
requestAccessForMediaType:AVMediaTypeAudio
completionHandler:^(BOOL granted) {
microphoneAccessState_ =
(granted) ? AccessStateGranted : AccessStateDenied;
[AVCaptureDevice
requestAccessForMediaType:AVMediaTypeVideo
completionHandler:^(BOOL granted) {
cameraAccessState_ = (granted)
? AccessStateGranted
: AccessStateDenied;
if (askAgain) {
dispatch_async(dispatch_get_main_queue(), ^{
[self askForMicrophoneAccess];
[self askForCameraAccess];
});
}
dispatch_async(dispatch_get_main_queue(), ^{
allAccessGranted(self.hasFullMediaAccess);
});
}];
}];
} else {
allAccessGranted(self.hasFullMediaAccess);
}
}
- (BOOL)mediaConsentStatusKnown {
return ((microphoneAccessState_ != AccessStateUnknown) &&
(cameraAccessState_ != AccessStateUnknown));
}
- (BOOL)hasCameraAccess {
if (@available(macOS 10.14, *)) {
return (cameraAccessState_ == AccessStateGranted);
}
return YES;
}
- (BOOL)hasMicrophoneAccess {
if (@available(macOS 10.14, *)) {
return (microphoneAccessState_ == AccessStateGranted);
}
return YES;
}
- (BOOL)hasFullMediaAccess {
return (self.hasCameraAccess && self.hasMicrophoneAccess);
}
@end