]> git.saurik.com Git - cydget.git/blob - WebCycript.mm
Update Depends: firmware to allow installs on 8.3.
[cydget.git] / WebCycript.mm
1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2015 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cydget is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydget is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydget. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include <CydiaSubstrate/CydiaSubstrate.h>
23 #include <UIKit/UIKit.h>
24
25 #include <sys/sysctl.h>
26 #include <pthread.h>
27
28 #include <WebKit/WebFrame.h>
29 #include <WebKit/WebView.h>
30
31 #include <map>
32
33 #include "yieldToSelector.h"
34
35 _disused static unsigned trace_;
36
37 #define _trace() do { \
38 NSLog(@"_trace(%u)@%s:%u[%s](%p)\n", \
39 trace_++, __FILE__, __LINE__, __FUNCTION__, pthread_self() \
40 ); \
41 } while (false)
42
43 #define _assert(test) do \
44 if (!(test)) { \
45 NSLog(@"_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
46 exit(-1); \
47 } \
48 while (false)
49
50 #define _syscall(expr) \
51 do if ((long) (expr) != -1) \
52 break; \
53 else switch (errno) { \
54 case EINTR: \
55 continue; \
56 default: \
57 _assert(false); \
58 } while (true)
59
60 extern "C" UIImage *_UIImageWithName(NSString *name);
61
62 typedef uint16_t UChar;
63
64 @interface UIApplication (Apple)
65 - (void) applicationOpenURL:(NSURL *)url;
66 @end
67
68 @interface UIScroller : UIView
69 - (CGSize) contentSize;
70 - (void) setOffset:(CGPoint)offset;
71 @end
72
73 @interface UIWebDocumentView : UIView
74 - (WebView *) webView;
75 @end
76
77 @interface UIView (Apple)
78 - (UIScroller *) _scroller;
79 @end
80
81 static bool iOS32, iOS4;
82
83 @interface NSString (UIKit)
84 - (NSString *) stringByAddingPercentEscapes;
85 @end
86
87 @implementation UIWebDocumentView (WebCycript)
88
89 - (void) _setScrollerOffset:(CGPoint)offset {
90 UIScroller *scroller([self _scroller]);
91
92 CGSize size([scroller contentSize]);
93 CGSize bounds([scroller bounds].size);
94
95 CGPoint max;
96 max.x = size.width - bounds.width;
97 max.y = size.height - bounds.height;
98
99 // wtf Apple?!
100 if (max.x < 0)
101 max.x = 0;
102 if (max.y < 0)
103 max.y = 0;
104
105 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
106 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
107
108 [scroller setOffset:offset];
109 }
110
111 @end
112
113 @interface DOMCSSStyleSheet : NSObject
114 - (int) addRule:(NSString *)rule style:(NSString *)style index:(unsigned)index;
115 - (void) deleteRule:(unsigned)index;
116 @end
117
118 @interface DOMStyleSheetList : NSObject
119 - (DOMCSSStyleSheet *) item:(unsigned)index;
120 @end
121
122 @interface DOMDocument : NSObject
123 - (DOMStyleSheetList *) styleSheets;
124 @end
125
126 @interface NSURL (Apple)
127 - (BOOL) isSpringboardHandledURL;
128 @end
129
130 @interface UIWebView (Apple)
131 - (UIWebDocumentView *) _documentView;
132 - (void) setDataDetectorTypes:(NSInteger)types;
133 - (void) _setDrawInWebThread:(BOOL)draw;
134 - (UIScrollView *) _scrollView;
135 - (UIScroller *) _scroller;
136 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message;
137 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
138 @end
139
140 @protocol CydgetWebViewDelegate <UIWebViewDelegate>
141 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
142 @end
143
144 MSClassHook(UIApplication)
145
146 MSInstanceMessageHook1(void, UIApplication, openURL, NSURL *, url) {
147 [self applicationOpenURL:url];
148 }
149
150 @implementation NSURL (Cydget)
151
152 - (NSNumber *) cydget$isSpringboardHandledURL {
153 return [NSNumber numberWithBool:[self isSpringboardHandledURL]];
154 }
155
156 @end
157
158 MSClassHook(NSURL)
159
160 MSInstanceMessageHook0(BOOL, NSURL, isSpringboardHandledURL) {
161 if (![NSThread isMainThread])
162 return MSOldCall();
163
164 return [[self cydget$yieldToSelector:@selector(cydget$isSpringboardHandledURL)] boolValue];
165 }
166
167 @implementation UIWebView (WebCycript)
168
169 - (void) updateStyles {
170 DOMCSSStyleSheet *sheet([[[[[[self _documentView] webView] mainFrame] DOMDocument] styleSheets] item:0]);
171 [sheet addRule:@"cydget" style:@"color: black" index:0];
172 [sheet deleteRule:0];
173 }
174
175 @end
176
177 extern "C" void WebCycriptSetupView(WebView *webview) {
178 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
179 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
180 WebFrame *frame([webview mainFrame]);
181 JSGlobalContextRef context([frame globalContext]);
182 @try {
183 CYSetupContext(context);
184 } @catch (NSException *e) {
185 NSLog(@"*** CydgetSetupContext => %@", e);
186 }
187 }
188 }
189
190 #include <string>
191
192 struct State {
193 unsigned state;
194 };
195
196 namespace JSC {
197 class JSGlobalData;
198 class UString;
199 }
200
201 namespace WebCore {
202 class KURL;
203 }
204
205 namespace WebCore {
206 struct String {
207 void *impl_;
208 }; }
209
210 namespace JSC {
211 struct SourceCode {
212 void *provider_;
213 int start_;
214 int end_;
215 int line_;
216 }; }
217
218 namespace JSC {
219 union ScriptSourceCode {
220 struct {
221 JSC::SourceCode source_;
222 } Old;
223 struct {
224 void *provider_;
225 JSC::SourceCode source_;
226 } New;
227 }; }
228
229 class CFStringStruct {
230 private:
231 CFStringRef value_;
232
233 public:
234 CFStringStruct() :
235 value_(NULL)
236 {
237 }
238
239 CFStringStruct(const CFStringStruct &value) :
240 value_((CFStringRef) CFRetain(value.value_))
241 {
242 }
243
244 ~CFStringStruct() {
245 [*this autorelease];
246 }
247
248 operator CFStringRef() const {
249 return value_;
250 }
251
252 operator NSString *() const {
253 return (NSString *) value_;
254 }
255 };
256
257 // String Helpers {{{
258 static const UChar *(*_ZNK7WebCore6String10charactersEv)(const WebCore::String *);
259 static const UChar *(*_ZN7WebCore6String29charactersWithNullTerminationEv)(const WebCore::String *);
260 static CFStringStruct (*_ZNK3WTF6String14createCFStringEv)(const WebCore::String *);
261 static unsigned (*_ZNK7WebCore6String6lengthEv)(const WebCore::String *);
262
263 static bool StringGet(const WebCore::String &string, const UChar *&data, size_t &length) {
264 bool terminated;
265
266 if (false) {
267 } else if (_ZNK7WebCore6String10charactersEv != NULL) {
268 data = (*_ZNK7WebCore6String10charactersEv)(&string);
269 terminated = false;
270 } else if (_ZN7WebCore6String29charactersWithNullTerminationEv != NULL) {
271 data = (*_ZN7WebCore6String29charactersWithNullTerminationEv)(&string);
272 terminated = true;
273 } else if (_ZNK3WTF6String14createCFStringEv != NULL) {
274 CFStringStruct cf((*_ZNK3WTF6String14createCFStringEv)(&string));
275 data = (const UChar *) [cf cStringUsingEncoding:NSUTF16StringEncoding];
276 length = CFStringGetLength(cf);
277 return true;
278 } else return false;
279
280 if (data == NULL)
281 return false;
282
283 if (_ZNK7WebCore6String6lengthEv != NULL)
284 length = (*_ZNK7WebCore6String6lengthEv)(&string);
285 else if (terminated)
286 for (length = 0; data[length] != 0; ++length);
287 else return false;
288
289 return true;
290 }
291
292 static bool StringEquals(const WebCore::String &string, const char *value) {
293 const UChar *data;
294 size_t size;
295 if (!StringGet(string, data, size))
296 return false;
297
298 size_t length(strlen(value));
299 if (size != length)
300 return false;
301
302 for (size_t index(0); index != length; ++index)
303 if (data[index] != value[index])
304 return false;
305
306 return true;
307 }
308 // }}}
309 // State Machine {{{
310 static bool cycript_;
311
312 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
313 if (!StringEquals(mime, "text/cycript")) {
314 cycript_ = false;
315 return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
316 }
317
318 static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
319 if (handle == NULL)
320 return false;
321
322 cycript_ = true;
323 return true;
324 }
325 // }}}
326 // Script Compiler {{{
327 static void Log(const WebCore::String &string) {
328 #if 0
329 const UChar *data;
330 size_t length;
331 if (!StringGet(string, data, length))
332 return;
333
334 UChar terminated[length + 1];
335 terminated[length] = 0;
336 memcpy(terminated, data, length * 2);
337 NSLog(@"wtf %p:%zu:%S:", &string, length, terminated);
338 #endif
339 }
340
341 static bool Cycriptify(const uint16_t *&data, size_t &size) {
342 cycript_ = false;
343
344 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
345 if (void (*CydgetMemoryParse)(const uint16_t **, size_t *) = reinterpret_cast<void (*)(const uint16_t **, size_t *)>(dlsym(handle, "CydgetMemoryParse"))) @try {
346 CydgetMemoryParse(&data, &size);
347 return true;
348 } @catch (NSException *e) {
349 NSLog(@"*** CydgetMemoryParse => %@", e);
350 }
351 return false;
352 }
353
354 static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned);
355 static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned);
356
357 static void Cycriptify(const WebCore::String &source, int *psize = NULL) {
358 if (!cycript_)
359 return;
360 cycript_ = false;
361
362 const UChar *data;
363 size_t length;
364 if (!StringGet(source, data, length))
365 return;
366
367 size_t size(length);
368 if (!Cycriptify(data, size))
369 return;
370
371 WebCore::String &script(const_cast<WebCore::String &>(source));
372 _ZN7WebCore6String8truncateEj(&script, 0);
373 _ZN7WebCore6String6appendEPKtj(&script, data, size);
374
375 if (psize != NULL)
376 *psize = size;
377
378 free((void *) data);
379
380 Log(source);
381 }
382 // }}}
383
384 static WebCore::String *string;
385
386 // iOS 2.x
387 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, void *_this, const WebCore::String &string, State state, const WebCore::String &url, int line) {
388 Cycriptify(string);
389 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i(_this, string, state, url, line);
390 }
391
392 // iOS 3.x
393 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
394 /*if (cycript_) {
395 JSC::SourceCode *source(_this[iOS32 ? 6 : 0]);
396 const uint16_t *data(source->data());
397 size_t size(source->length());
398
399 if (Cycriptify(data, size)) {
400 source->~SourceCode();
401 // XXX: I actually don't have the original URL here: pants
402 new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
403 free((void *) data);
404 }
405 }*/
406
407 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
408 }
409
410 // iOS 3.x cdata
411 MSHook(const WebCore::String &, _ZNK7WebCore4Node11textContentEb, void *_this, bool convert) {
412 const WebCore::String &code(__ZNK7WebCore4Node11textContentEb(_this, convert));
413 string = const_cast<WebCore::String *>(&code);
414 Cycriptify(code);
415 return code;
416 }
417
418 // iOS 4.x cdata
419 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) {
420 Cycriptify(source);
421 return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line);
422 }
423
424 // iOS 4.x+5.0 @src=
425 MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) {
426 const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this));
427 string = const_cast<WebCore::String *>(&script);
428 return script;
429 }
430
431 // iOS 4.x @src=
432 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, void *_this, JSC::ScriptSourceCode &script, State state) {
433 if (string != NULL) {
434 JSC::SourceCode *source(iOS4 ? &script.New.source_ : &script.Old.source_);
435 Cycriptify(*string, &source->end_);
436 string = NULL;
437 }
438
439 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, script, state);
440 }
441
442 // iOS 5.0 cdata
443 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
444 Cycriptify(source);
445 return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE(_this, source, url, position);
446 }
447
448 // iOS 5.0 @src=
449 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
450 string = NULL;
451 return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE(_this, position, legacy);
452 }
453
454 void (*$_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE)(void *_this, int legacy);
455
456 // iOS 5.0 @src=
457 MSHook(void, _ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, void *_this, JSC::ScriptSourceCode &script) {
458 if (string != NULL) {
459 JSC::SourceCode *source(&script.New.source_);
460 $_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE(_this, 0);
461 Cycriptify(*string, &source->end_);
462 string = NULL;
463 }
464
465 return __ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE(_this, script);
466 }
467
468 // iOS 6.0 cdata
469 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
470 Cycriptify(source);
471 return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE(_this, source, url, position);
472 }
473
474 // iOS 6.0 @src=
475 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
476 string = NULL;
477 return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE(_this, position, legacy);
478 }
479
480 /* Cydget:// Protocol {{{ */
481 @interface CydgetURLProtocol : NSURLProtocol {
482 }
483
484 @end
485
486 @implementation CydgetURLProtocol
487
488 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
489 NSURL *url([request URL]);
490 if (url == nil)
491 return NO;
492 NSString *scheme([[url scheme] lowercaseString]);
493 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
494 return NO;
495 return YES;
496 }
497
498 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
499 return request;
500 }
501
502 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
503 id<NSURLProtocolClient> client([self client]);
504 if (icon == nil)
505 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
506 else {
507 NSData *data(UIImagePNGRepresentation(icon));
508
509 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
510 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
511 [client URLProtocol:self didLoadData:data];
512 [client URLProtocolDidFinishLoading:self];
513 }
514 }
515
516 - (void) startLoading {
517 id<NSURLProtocolClient> client([self client]);
518 NSURLRequest *request([self request]);
519
520 NSURL *url([request URL]);
521 NSString *href([url absoluteString]);
522
523 NSString *path([href substringFromIndex:9]);
524 NSRange slash([path rangeOfString:@"/"]);
525
526 NSString *command;
527 if (slash.location == NSNotFound) {
528 command = path;
529 path = nil;
530 } else {
531 command = [path substringToIndex:slash.location];
532 path = [path substringFromIndex:(slash.location + 1)];
533 }
534
535 if ([command isEqualToString:@"_UIImageWithName"]) {
536 if (path == nil)
537 goto fail;
538 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
539 UIImage *icon(_UIImageWithName(path));
540 [self _returnPNGWithImage:icon forRequest:request];
541 } else fail: {
542 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
543 }
544 }
545
546 - (void) stopLoading {
547 }
548
549 @end
550 /* }}} */
551 /* Cydget-CGI:// Protocol {{{ */
552 @interface CydgetCGIURLProtocol : NSURLProtocol {
553 pid_t pid_;
554 CFHTTPMessageRef http_;
555 NSFileHandle *handle_;
556 }
557
558 @end
559
560 @implementation CydgetCGIURLProtocol
561
562 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
563 NSURL *url([request URL]);
564 if (url == nil)
565 return NO;
566 NSString *scheme([[url scheme] lowercaseString]);
567 if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"])
568 return NO;
569 return YES;
570 }
571
572 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
573 return request;
574 }
575
576 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
577 if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
578 pid_ = -1;
579 } return self;
580 }
581
582 - (void) startLoading {
583 id<NSURLProtocolClient> client([self client]);
584 NSURLRequest *request([self request]);
585 NSURL *url([request URL]);
586
587 NSString *path([url path]);
588 if (path == nil) {
589 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
590 return;
591 }
592
593 NSFileManager *manager([NSFileManager defaultManager]);
594 if (![manager fileExistsAtPath:path]) {
595 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
596 return;
597 }
598
599 int fds[2];
600 _assert(pipe(fds) != -1);
601
602 _assert(pid_ == -1);
603 pid_ = fork();
604 if (pid_ == -1) {
605 _assert(close(fds[0]) != -1);
606 _assert(close(fds[1]) != -1);
607 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
608 return;
609 }
610
611 if (pid_ == 0) {
612 const char *script([path UTF8String]);
613
614 setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
615 setenv("SCRIPT_FILENAME", script, true);
616 NSString *query([url query]);
617 if (query != nil)
618 setenv("QUERY_STRING", [query UTF8String], true);
619
620 _assert(dup2(fds[1], 1) != -1);
621 _assert(close(fds[0]) != -1);
622 _assert(close(fds[1]) != -1);
623
624 execl(script, script, NULL);
625 exit(1);
626 _assert(false);
627 }
628
629 _assert(close(fds[1]) != -1);
630
631 _assert(http_ == NULL);
632 http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
633 CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
634
635 _assert(handle_ == nil);
636 handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES];
637
638 [[NSNotificationCenter defaultCenter]
639 addObserver:self
640 selector:@selector(onRead:)
641 name:@"NSFileHandleReadCompletionNotification"
642 object:handle_
643 ];
644
645 [handle_ readInBackgroundAndNotify];
646 }
647
648 - (void) onRead:(NSNotification *)notification {
649 NSFileHandle *handle([notification object]);
650
651 NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
652
653 if (size_t length = [data length]) {
654 CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
655 [handle readInBackgroundAndNotify];
656 } else {
657 id<NSURLProtocolClient> client([self client]);
658
659 CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
660 if (mime == NULL)
661 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
662 else {
663 NSURLRequest *request([self request]);
664
665 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
666 CFRelease(mime);
667
668 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
669
670 CFDataRef body(CFHTTPMessageCopyBody(http_));
671 [client URLProtocol:self didLoadData:(NSData *)body];
672 CFRelease(body);
673
674 [client URLProtocolDidFinishLoading:self];
675 }
676
677 CFRelease(http_);
678 http_ = NULL;
679 }
680 }
681
682 //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
683
684 - (void) stopLoading_ {
685 [[NSNotificationCenter defaultCenter] removeObserver:self];
686
687 if (handle_ != nil) {
688 [handle_ release];
689 handle_ = nil;
690 }
691
692 if (pid_ != -1) {
693 kill(pid_, SIGTERM);
694 int status;
695 _syscall(waitpid(pid_, &status, 0));
696 pid_ = -1;
697 }
698 }
699
700 - (void) stopLoading {
701 [self
702 performSelectorOnMainThread:@selector(stopLoading_)
703 withObject:nil
704 waitUntilDone:NO
705 ];
706 }
707
708 @end
709 /* }}} */
710
711 namespace WebCore {
712 class MediaQueryEvaluator;
713 class CSSParserValueList;
714 }
715
716 namespace WebCore {
717 struct MediaQueryExp {
718 String feature_;
719 void *value_;
720 bool valid_;
721 String cache_;
722 }; }
723
724 typedef std::map<std::string, BOOL (*)()> StyleMap_;
725 static StyleMap_ styles_;
726
727 extern "C" void WebCycriptRegisterStyle(const char *name, BOOL (*code)()) {
728 styles_.insert(std::make_pair(name, code));
729 }
730
731 MSHook(bool, _ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, WebCore::MediaQueryEvaluator *_this, WebCore::String &query) {
732 Log(query);
733 for (StyleMap_::const_iterator style(styles_.begin()); style != styles_.end(); ++style)
734 if (StringEquals(query, style->first.c_str()))
735 return style->second();
736 return __ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE(_this, query);
737 }
738
739 MSHook(void *, _ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, WebCore::MediaQueryExp *_this, WebCore::String &query, WebCore::CSSParserValueList *values) {
740 Log(query);
741 void *value(__ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE(_this, query, values));
742 if (!_this->valid_)
743 for (StyleMap_::const_iterator style(styles_.begin()); style != styles_.end(); ++style)
744 if (StringEquals(query, style->first.c_str()))
745 _this->valid_ = true;
746 return value;
747 }
748
749 template <typename Type_>
750 static void dlset(Type_ &function, const char *name) {
751 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
752 }
753
754 #define msset(function, handle) \
755 MSHookSymbol(function, "_" #function, handle)
756
757 MSInitialize {
758 iOS4 = kCFCoreFoundationVersionNumber >= 550.32;
759 iOS32 = !iOS4 && kCFCoreFoundationVersionNumber >= 478.61;
760
761 int maxproc;
762 size_t size(sizeof(maxproc));
763 if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
764 NSLog(@"sysctlbyname(\"kern.maxproc\", ?)");
765 else if (maxproc < 72) {
766 maxproc = 72;
767 if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
768 NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
769 }
770
771 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
772 [WebView registerURLSchemeAsLocal:@"cydget"];
773
774 [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]];
775 [WebView registerURLSchemeAsLocal:@"cydget-cgi"];
776
777 MSImageRef JavaScriptCore(NULL);
778 if (JavaScriptCore == NULL)
779 JavaScriptCore = MSGetImageByName("/System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore");
780 if (JavaScriptCore == NULL)
781 JavaScriptCore = MSGetImageByName("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore");
782
783 MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
784
785 if (!iOS4) {
786 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
787 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
788 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
789 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
790 }
791
792 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &) = NULL;
793 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
794 MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE", WebCore);
795 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
796 MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKN3WTF6StringE", WebCore);
797 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE != NULL)
798 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
799
800 void (*_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)(void *, const WebCore::String &, const WebCore::KURL &, int) = NULL;
801 if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi == NULL)
802 MSHookSymbol(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, "__ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi", WebCore);
803 if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi != NULL)
804 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi));
805
806 if (!iOS4) {
807 const WebCore::String &(*_ZNK7WebCore4Node11textContentEb)(void *, bool) = NULL;
808 if (_ZNK7WebCore4Node11textContentEb == NULL)
809 MSHookSymbol(_ZNK7WebCore4Node11textContentEb, "__ZNK7WebCore4Node11textContentEb", WebCore);
810 if (_ZNK7WebCore4Node11textContentEb != NULL)
811 MSHookFunction(_ZNK7WebCore4Node11textContentEb, MSHake(_ZNK7WebCore4Node11textContentEb));
812 }
813
814 const WebCore::String &(*_ZN7WebCore12CachedScript6scriptEv)(void *) = NULL;
815 if (_ZN7WebCore12CachedScript6scriptEv == NULL)
816 MSHookSymbol(_ZN7WebCore12CachedScript6scriptEv, "__ZN7WebCore12CachedScript6scriptEv", WebCore);
817 if (_ZN7WebCore12CachedScript6scriptEv != NULL)
818 MSHookFunction(_ZN7WebCore12CachedScript6scriptEv, MSHake(_ZN7WebCore12CachedScript6scriptEv));
819
820 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i)(void *, const WebCore::String &, State, const WebCore::String &, int) = NULL;
821 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i == NULL)
822 MSHookSymbol(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i", WebCore);
823 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i != NULL)
824 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i));
825
826 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)(void *, JSC::ScriptSourceCode &, State) = NULL;
827 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE == NULL)
828 MSHookSymbol(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE", WebCore);
829 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE != NULL)
830 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE));
831
832 void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
833 msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, WebCore);
834 if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE != NULL)
835 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE));
836
837 void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
838 msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, WebCore);
839 if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE == NULL)
840 MSHookSymbol(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, "__ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_3URLERKNS1_12TextPositionE", WebCore);
841 if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE != NULL)
842 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE));
843
844 void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE)(void *, void *, int);
845 msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, WebCore);
846 if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE != NULL)
847 MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE));
848
849 void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE)(void *, void *, int);
850 msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, WebCore);
851 if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE != NULL)
852 MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE));
853
854 MSHookSymbol($_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE, "__ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE", WebCore);
855
856 void (*_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE)(void *, JSC::ScriptSourceCode &);
857 msset(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, WebCore);
858 if (_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE != NULL)
859 MSHookFunction(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, MSHake(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE));
860
861 if (_ZN7WebCore6String6appendEPKtj == NULL)
862 MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN7WebCore6String6appendEPKtj", WebCore);
863 if (_ZN7WebCore6String6appendEPKtj == NULL)
864 msset(_ZN7WebCore6String6appendEPKtj, JavaScriptCore);
865 if (_ZN7WebCore6String6appendEPKtj == NULL)
866 MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN3WTF6String6appendEPKtj", JavaScriptCore);
867
868 if (_ZN7WebCore6String8truncateEj == NULL)
869 MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN7WebCore6String8truncateEj", WebCore);
870 if (_ZN7WebCore6String8truncateEj == NULL)
871 msset(_ZN7WebCore6String8truncateEj, JavaScriptCore);
872 if (_ZN7WebCore6String8truncateEj == NULL)
873 MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN3WTF6String8truncateEj", JavaScriptCore);
874
875 msset(_ZNK7WebCore6String10charactersEv, WebCore);
876
877 msset(_ZN7WebCore6String29charactersWithNullTerminationEv, JavaScriptCore);
878 if (_ZN7WebCore6String29charactersWithNullTerminationEv == NULL)
879 MSHookSymbol(_ZN7WebCore6String29charactersWithNullTerminationEv, "__ZN3WTF6String29charactersWithNullTerminationEv", JavaScriptCore);
880
881 msset(_ZNK3WTF6String14createCFStringEv, JavaScriptCore);
882
883 msset(_ZNK7WebCore6String6lengthEv, WebCore);
884
885 bool (*_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE)(WebCore::MediaQueryEvaluator *, WebCore::String &);
886 msset(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, WebCore);
887 if (_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE != NULL)
888 MSHookFunction(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, MSHake(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE));
889
890 void *(*_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE)(WebCore::MediaQueryExp *, WebCore::String &, WebCore::CSSParserValueList *);
891 msset(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, WebCore);
892 if (_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE != NULL)
893 MSHookFunction(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, MSHake(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE));
894 }
895
896 MSClassHook(WebView)
897 MSMetaClassHook(WebView)
898
899 MSClassMessageHook0(void, WebView, enableWebThread) {
900 if (kCFCoreFoundationVersionNumber >= 478.61)
901 return MSOldCall();
902
903 NSLog(@"-[WebView enableWebThread]");
904 }