-
Notifications
You must be signed in to change notification settings - Fork 139
/
PermissionsManager.m
194 lines (169 loc) · 5.88 KB
/
PermissionsManager.m
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//
// PermissionsManager.m
// Scroll Reverser
//
// Created by Nicholas Moore on 21/11/2019.
//
#import "PermissionsManager.h"
#import <IOKit/hidsystem/IOHIDLib.h>
NSString *const PermissionsManagerKeyAccessibilityEnabled=@"accessibilityEnabled";
NSString *const PermissionsManagerKeyInputMonitoringEnabled=@"inputMonitoringEnabled";
NSString *const PermissionsManagerKeyHasAllRequiredPermissions=@"hasAllRequiredPermissions";
static NSString *const PrefsHasRequestedInputMonitoringPermission=@"HasRequestedInputMonitoringPermission";
static NSString *const PrefsHasRequestedAccessibilityPermission=@"HasRequestedAccessibilityPermission";
@interface PermissionsManager ()
@property (getter=isAccessibilityEnabled) BOOL accessibilityEnabled;
@property (getter=isInputMonitoringEnabled) BOOL inputMonitoringEnabled;
@property NSTimer *refreshTimer;
@property NSDate *refreshStarted;
@end
@implementation PermissionsManager
- (instancetype)init
{
self = [super init];
if (self) {
[self checkState];
// refresh on user activity
const NSEventMask mask=NSEventMaskLeftMouseDown;
[NSEvent addGlobalMonitorForEventsMatchingMask:mask handler:^(NSEvent *event) {
[self refresh];
}];
[NSEvent addLocalMonitorForEventsMatchingMask:mask handler:^NSEvent *(NSEvent *event) {
[self refresh];
return event;
}];
}
return self;
}
# pragma mark Private Methods
- (BOOL)checkAccessibilityWithPrompt:(BOOL)prompt
{
// this is a 10.9 API but is only needed on 10.14.
// with no prompt, this check is very fast. otherwise it blocks.
NSDictionary *const options=@{(__bridge NSString *)kAXTrustedCheckOptionPrompt: @(prompt)};
return AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
}
- (BOOL)checkInputMonitoringWithPrompt:(BOOL)prompt
{
if (@available(macOS 10.15, *)) {
static const IOHIDRequestType accessType=kIOHIDRequestTypeListenEvent;
if (prompt) {
// this will block
dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
IOHIDRequestAccess(accessType);
});
return NO;
}
else {
// this check is very fast
return kIOHIDAccessTypeGranted==IOHIDCheckAccess(accessType);
}
}
else {
return YES;
}
}
+ (NSSet *)keyPathsForValuesAffectingHasAllRequiredPermissions
{
return [NSSet setWithArray:@[PermissionsManagerKeyAccessibilityEnabled, PermissionsManagerKeyInputMonitoringEnabled]];
}
- (void)checkState
{
BOOL axState=[self checkAccessibilityWithPrompt:NO];
if(axState!=self.accessibilityEnabled) {
self.accessibilityEnabled=axState;
}
BOOL imState=[self checkInputMonitoringWithPrompt:NO];
if(imState!=self.inputMonitoringEnabled) {
self.inputMonitoringEnabled=imState;
}
NSLog(@"permissions: ax %@, im %@", @(axState), @(imState));
}
#pragma mark Public Interface
/* Every time this is called, a repeat timer will repeatedly poll. */
- (void)refresh
{
// always do timer stuff on main thread
dispatch_async(dispatch_get_main_queue(), ^{
self.refreshStarted=[NSDate date];
if (!self.refreshTimer.valid) {
self.refreshTimer=[NSTimer scheduledTimerWithTimeInterval:0.333 repeats:YES block:^(NSTimer * _Nonnull timer) {
[self checkState];
if ([[NSDate date] timeIntervalSinceDate:self.refreshStarted]>2.5) {
[self.refreshTimer invalidate];
self.refreshTimer=nil;
}
}];
}
});
}
- (BOOL)hasAllRequiredPermissions
{
BOOL result=YES;
if (self.accessibilityRequired && !self.accessibilityEnabled) {
NSLog(@"missing required accessibility permission");
result=NO;
}
if (self.inputMonitoringRequired && !self.inputMonitoringEnabled) {
NSLog(@"missing required input monitoring permission");
result=NO;
}
return result;
}
+ (NSURL *)securitySettingsUrlForKey:(NSString *)key
{
return [NSURL URLWithString:[NSString stringWithFormat:@"x-apple.systempreferences:com.apple.preference.security?%@", key]];
}
- (void)requestAccessibilityPermission
{
[self willChangeValueForKey:@"accessibilityRequested"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:PrefsHasRequestedAccessibilityPermission];
[self didChangeValueForKey:@"accessibilityRequested"];
[self checkAccessibilityWithPrompt:YES];
}
- (void)requestInputMonitoringPermission
{
[self willChangeValueForKey:@"inputMonitoringRequested"];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:PrefsHasRequestedInputMonitoringPermission];
[self didChangeValueForKey:@"inputMonitoringRequested"];
[self checkInputMonitoringWithPrompt:YES];
}
- (void)openAccessibilityPrefs
{
[[NSWorkspace sharedWorkspace] openURL:[[self class] securitySettingsUrlForKey:@"Privacy_Accessibility"]];
}
- (void)openInputMonitoringPrefs
{
[[NSWorkspace sharedWorkspace] openURL:[[self class] securitySettingsUrlForKey:@"Privacy_ListenEvent"]];
}
// Accessibility permission is needed on Mojave and above
- (BOOL)isAccessibilityRequired
{
if (@available(macOS 10.14, *)) {
return YES;
}
else {
return NO;
}
}
// Input Monitoring permission is needed on Catalina and above
- (BOOL)isInputMonitoringRequired
{
if (@available(macOS 10.15, *)) {
return YES;
}
else {
return NO;
}
}
- (BOOL)isAccessibilityRequested
{
return [[NSUserDefaults standardUserDefaults] boolForKey:PrefsHasRequestedAccessibilityPermission];
}
// macos only lets an app request this ONCE EVER. so we remember if we requested it, and
// thereafter can offer to open the prefpane instead
- (BOOL)isInputMonitoringRequested
{
return [[NSUserDefaults standardUserDefaults] boolForKey:PrefsHasRequestedInputMonitoringPermission];
}
@end