-
Notifications
You must be signed in to change notification settings - Fork 0
/
DASDaemonInterface.x
74 lines (63 loc) · 3.08 KB
/
DASDaemonInterface.x
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
#import <objc/runtime.h>
#import "_DASDaemon.h"
#import "_DASDaemonClient.h"
#import "NSXPCConnection+Internal.h"
#import "XPCActivityRunError.h"
%hook _DASDaemonInterface
+ (void)startDASDaemon {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bundleDidLoadNotification:) name:NSBundleDidLoadNotification object:nil];
%orig;
}
%new
+ (void)bundleDidLoadNotification:(NSNotification*)notification {
NSArray* loadedClasses = [notification.userInfo objectForKey:NSLoadedClasses];
if ([loadedClasses containsObject:@"_DASDaemonClient"]) {
Class targetClass = NSClassFromString(@"_DASDaemonClient");
SEL newSelector = @selector(forceRunActivities:completion:);
Method newMethod = class_getInstanceMethod(self, newSelector);
class_addMethod(targetClass,
newSelector,
method_getImplementation(newMethod),
method_getTypeEncoding(newMethod));
}
}
%new
- (void)forceRunActivities:(NSArray *)activities completion:(void (^)(NSError *))completion {
// The expectation is that this method is actually called on an instance
// of `_DASDaemonClient` (enabled via the call to `class_addMethod`
// in +[_DASDaemonInterface bundleDidLoadNotification:]
if (![(_DASDaemonClient *)self isKindOfClass:NSClassFromString(@"_DASDaemonClient")]) {
%log(@"`forceRunActivities:completion:` called on a non-DASDaemonClient class");
NSError *error = [NSError errorWithDomain:XPCActivityRunErrorDomain
code:XPCActivityRunErrorCodeInternalTypeError
userInfo:nil];
completion(error);
return;
}
_DASDaemonClient *daemonClient = (_DASDaemonClient *)self;
// Check calling process' entitlements
if (![[[daemonClient connection] valueForEntitlement:@"com.apple.duet.activityscheduler.allow"] boolValue]) {
%log(@"`forceRunActivities:completion:` called by process without \"com.apple.duet.activityscheduler.allow\" entitlement");
NSError *error = [NSError errorWithDomain:XPCActivityRunErrorDomain
code:XPCActivityRunErrorCodeMissingEntitlements
userInfo:nil];
completion(error);
return;
}
// Check all given activies to make sure they exist
for (NSString *activityName in activities) {
if ([daemonClient.daemon getActivityWithName:activityName] == nil) {
NSString *logMessage = [NSString stringWithFormat:@"`forceRunActivities:completion:` encountered unknown activity with name \"%@\"", activityName];
%log(logMessage);
NSError *error = [NSError errorWithDomain:XPCActivityRunErrorDomain
code:XPCActivityRunErrorCodeNoActivityWithName
userInfo:@{@"activityName": activityName}];
completion(error);
return;
}
}
// Run all activities
[daemonClient forceRunActivities:activities];
completion(nil);
}
%end