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