This repository was archived by the owner on Jan 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHBLOHandlerController.x
More file actions
296 lines (231 loc) · 9.43 KB
/
HBLOHandlerController.x
File metadata and controls
296 lines (231 loc) · 9.43 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#import "HBLOHandlerController.h"
#import "HBLibOpener.h"
#import "HBLOHandler.h"
#import "HBLOOpenOperation.h"
#import "HBLOPreferences.h"
#import <Cephei/NSString+HBAdditions.h>
#import <MobileCoreServices/LSApplicationWorkspace.h>
#import <MobileCoreServices/LSApplicationProxy.h>
#import <SpringBoard/SpringBoard.h>
#import <SpringBoard/SBApplication.h>
#import <SpringBoardServices/SpringBoardServices.h>
#import <version.h>
#define HBLOAssertOpenerdOnly() \
if (![self.class isInOpenerd]) { \
[NSException raise:NSInternalInconsistencyException format:@"-[%@ %@] can only be called within openerd.", self.class, NSStringFromSelector(_cmd)]; \
}
@implementation HBLOHandlerController {
BOOL _hasLoadedHandlers;
}
#pragma mark - Helpers
+ (BOOL)isInOpenerd {
static BOOL isInOpenerd;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// we’re “in” openerd if we literally are in openerd (or Preferences) on iOS 9 or newer. on
// older iOS, we’re in-process so this is always YES
if (IS_IOS_OR_NEWER(iOS_9_0)) {
NSBundle *bundle = [NSBundle mainBundle];
isInOpenerd = [bundle.executablePath isEqualToString:@"/usr/libexec/openerd"] || [bundle.bundleIdentifier isEqualToString:@"com.apple.Preferences"];
} else {
isInOpenerd = YES;
}
});
return isInOpenerd;
}
+ (NSString *)foregroundBundleIdentifier {
NSString *sender = nil;
// get the frontmost app identifier via SBS if in openerd, or directly if in SpringBoard
if ([self.class isInOpenerd]) {
sender = SBSCopyFrontmostApplicationDisplayIdentifier();
} else if (IN_SPRINGBOARD) {
sender = ((SpringBoard *)[%c(SpringBoard) sharedApplication])._accessibilityFrontMostApplication.bundleIdentifier;
}
// if we didn’t get anything and aren’t in openerd, just try the current process’s bundle id
if (!sender && ![self.class isInOpenerd]) {
sender = [NSBundle mainBundle].bundleIdentifier;
}
// return what we got, or just use springboard as a last resort
return sender ?: @"com.apple.springboard";
}
#pragma mark - Object
+ (instancetype)sharedInstance {
static HBLOHandlerController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (instancetype)init {
self = [super init];
if (self) {
_handlers = [[NSMutableArray alloc] init];
}
return self;
}
#pragma mark - Registration/loading
- (BOOL)registerHandler:(HBLOHandler *)handler error:(NSError **)error {
HBLOAssertOpenerdOnly();
HBLOLogDebug(@"registering handler %@", handler.identifier);
for (HBLOHandler *handler2 in _handlers) {
if ([handler.identifier isEqualToString:handler2.identifier]) {
if (error) {
*error = [NSError errorWithDomain:HBLOErrorDomain code:1 userInfo:@{
NSLocalizedDescriptionKey: [NSString stringWithFormat:@"The handler “%@” is already registered.", handler.identifier]
}];
}
return NO;
}
}
[_handlers addObject:handler];
return YES;
}
- (void)loadHandlers {
HBLOAssertOpenerdOnly();
if (_hasLoadedHandlers) {
HBLOLogDebug(@"you only load handlers once (YOLHO)");
return;
}
HBLOLogDebug(@"loading handlers");
_hasLoadedHandlers = YES;
NSURL *handlersURL = [NSURL URLWithString:kHBLOHandlersURL].URLByResolvingSymlinksInPath;
NSError *error = nil;
NSArray <NSURL *> *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:handlersURL includingPropertiesForKeys:nil options:kNilOptions error:&error];
if (error) {
HBLogError(@"failed to access handler directory %@: %@", kHBLOHandlersURL, error.localizedDescription);
return;
}
for (NSURL *directory in contents) {
NSString *baseName = directory.pathComponents.lastObject;
HBLOLogDebug(@"loading %@", baseName);
NSBundle *bundle = [NSBundle bundleWithURL:directory];
if (!bundle) {
HBLogError(@"failed to load bundle for handler %@", baseName);
continue;
}
[bundle load];
if (!bundle.principalClass) {
HBLogError(@"no principal class for handler %@", baseName);
continue;
}
HBLOHandler *handler = [[bundle.principalClass alloc] init];
if (!handler) {
HBLogError(@"failed to initialise principal class for %@", baseName);
continue;
}
NSError *error = nil;
if (![self registerHandler:handler error:&error]) {
HBLogError(@"error registering handler %@: %@", baseName, error.localizedDescription);
continue;
}
}
}
#pragma mark - Open URL
- (nullable NSArray <HBLOOpenOperation *> *)getReplacementsForOpenOperation:(HBLOOpenOperation *)openOperation {
if ([self.class isInOpenerd]) {
// too easy
return [self _getReplacementsForOpenOperation:openOperation];
} else {
__block NSArray <HBLOOpenOperation *> *output = nil;
// wrap in a semaphore to ignore the operation if it takes longer than 1 sec. (kind of a hack…)
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
dispatch_queue_t queue = dispatch_queue_create([NSString stringWithFormat:@"ws.hbang.libopener.queue%f", [NSDate date].timeIntervalSince1970].UTF8String, DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
// send the message, and hopefully have it placed in the response buffer
NSData *input = [NSKeyedArchiver archivedDataWithRootObject:openOperation];
LMResponseBuffer buffer;
kern_return_t result = LMConnectionSendTwoWayData(&openerdService, 0, (__bridge CFDataRef)input, &buffer);
// if it failed, log and return nil
if (result != KERN_SUCCESS) {
HBLogError(@"could not contact openerd! error %i", result);
return;
}
// translate the message to NSData, then NSDictionary
CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8 *)LMMessageGetData(&buffer.message), LMMessageGetDataLength(&buffer.message), kCFAllocatorNull);
output = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)data];
LMResponseBufferFree(&buffer);
dispatch_semaphore_signal(semaphore);
});
dispatch_semaphore_wait(semaphore, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC));
// return what we got, or nothing if we got nothing
return output && output.count > 0 ? output : nil;
}
}
- (nullable NSArray <HBLOOpenOperation *> *)_getReplacementsForOpenOperation:(HBLOOpenOperation *)openOperation {
HBLOAssertOpenerdOnly();
NSURL *url = openOperation.URL;
NSString *sender = openOperation.application.applicationIdentifier;
// is it a googlechrome(s):// or googlechrome-x-callback:// url?
if ([url.scheme isEqualToString:@"googlechrome"] || [url.scheme isEqualToString:@"googlechromes"]) {
// extract the original url from the chrome-specific url
url = [NSURL URLWithString:[@"http" stringByAppendingString:[url.absoluteString substringWithRange:NSMakeRange(12, url.absoluteString.length - 12)]]];
} else if ([url.scheme isEqualToString:@"googlechrome-x-callback"]) {
// grab the url from the query arguments
NSDictionary <NSString *, NSString *> *query = url.query.hb_queryStringComponents;
if (query[@"url"]) {
url = [NSURL URLWithString:query[@"url"]];
}
}
// no sender given? just set it to our best guess of the foreground app
if (!sender) {
openOperation.application = [LSApplicationProxy applicationProxyForIdentifier:[self.class foregroundBundleIdentifier]];
}
// load the handlers if we haven't yet
if (!_hasLoadedHandlers) {
[self loadHandlers];
}
HBLOLogDebug(@"determining replacement for %@ (requested by %@)", url, openOperation.application.applicationIdentifier);
HBLOPreferences *preferences = [HBLOPreferences sharedInstance];
NSMutableArray <NSURL *> *results = [NSMutableArray array];
// loop over all available handlers
for (HBLOHandler *handler in _handlers) {
// not enabled? no worries, just skip over it
if (![preferences isHandlerEnabled:handler]) {
HBLOLogDebug(@" → %@ is disabled", handler.identifier);
continue;
}
// ask the handler for a replacement URL
id newURL = [handler openURL:url sender:openOperation.application.applicationIdentifier];
HBLOLogDebug(@" → %@ returned: %@", handler.identifier, newURL);
if (!newURL) {
// nothing returned? skip to the next handler
continue;
} else if ([newURL isKindOfClass:NSURL.class]) {
// it's an NSURL? add that to our results
[results addObject:newURL];
} else if ([newURL isKindOfClass:NSArray.class]) {
// it's an array, hopefully of NSURLs? add them to our results
[results addObjectsFromArray:newURL];
} else {
HBLogError(@"%@ returned invalid value of type %@: %@", handler.identifier, ((NSObject *)newURL).class, newURL);
}
}
NSMutableArray <HBLOOpenOperation *> *candidates = [NSMutableArray array];
// iterate over our results
for (NSURL *url_ in results) {
NSArray <LSApplicationProxy *> *apps = [[LSApplicationWorkspace defaultWorkspace] applicationsAvailableForHandlingURLScheme:url_.scheme];
// if nothing can open that url scheme, we don't want it (sorry)
if (apps.count == 0) {
continue;
}
// if the url can be opened by the same app, we should ignore it
for (LSApplicationProxy *app in apps) {
if ([app isEqual:openOperation.application]) {
HBLOLogDebug(@"url scheme %@: is supported by the sending app – ignoring", url_.scheme);
continue;
}
// add it to the candidates
[candidates addObject:[HBLOOpenOperation openOperationWithURL:url_ application:app]];
}
}
// if we don’t have anything, log and return nil. if we do, log that and return the array
if (candidates.count == 0) {
HBLOLogDebug(@"no candidates available");
return nil;
} else {
HBLOLogDebug(@"replacements: %@", candidates);
return candidates;
}
}
@end