1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2012 Jay Freeman (saurik)
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the
11 * above copyright notice, this list of conditions
12 * and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the
14 * above copyright notice, this list of conditions
15 * and the following disclaimer in the documentation
16 * and/or other materials provided with the
18 * 3. The name of the author may not be used to endorse
19 * or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <substrate.h>
39 #include <sys/sysctl.h>
41 #import <GraphicsServices/GraphicsServices.h>
42 #import <UIKit/UIKit.h>
43 #import <AddressBook/AddressBook.h>
45 #import <SpringBoard/SBStatusBarController.h>
46 #import <SpringBoardUI/SBAwayViewPluginController.h>
47 #import <TelephonyUI/TPBottomLockBar.h>
49 #import <QuartzCore/CALayer.h>
50 // XXX: fix the minimum requirement
51 extern NSString * const kCAFilterNearest;
53 #include <WebKit/DOMCSSPrimitiveValue.h>
54 #include <WebKit/DOMCSSStyleDeclaration.h>
55 #include <WebKit/DOMDocument.h>
56 #include <WebKit/DOMHTMLBodyElement.h>
57 #include <WebKit/DOMNodeList.h>
58 #include <WebKit/DOMRGBColor.h>
60 #include <WebKit/WebFrame.h>
61 #include <WebKit/WebPolicyDelegate.h>
62 #include <WebKit/WebPreferences.h>
63 #include <WebKit/WebScriptObject.h>
65 #import <WebKit/WebView.h>
66 #import <WebKit/WebView-WebPrivate.h>
68 #include <WebCore/Page.h>
69 #include <WebCore/Settings.h>
71 #include <WebCore/WebCoreThread.h>
72 #include <WebKit/WebPreferences-WebPrivate.h>
74 #include "JSGlobalData.h"
76 #include "SourceCode.h"
78 #include "yieldToSelector.h"
80 #include <apr-1/apr_pools.h>
84 #define _forever for (;;)
86 _disused static unsigned trace_;
88 #define _trace() do { \
89 NSLog(@"_trace(%u)@%s:%u[%s](%p)\n", \
90 trace_++, __FILE__, __LINE__, __FUNCTION__, pthread_self() \
94 #define _assert(test) do \
96 fprintf(stderr, "_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
101 #define _syscall(expr) \
102 do if ((long) (expr) != -1) \
104 else switch (errno) { \
111 @protocol CydgetController
112 - (NSDictionary *) currentConfiguration;
115 static Class $CydgetController(objc_getClass("CydgetController"));
117 static bool iOS32, iOS4;
119 @interface NSString (UIKit)
120 - (NSString *) stringByAddingPercentEscapes;
123 @implementation UIWebDocumentView (WebCycript)
125 - (void) _setScrollerOffset:(CGPoint)offset {
126 UIScroller *scroller([self _scroller]);
128 CGSize size([scroller contentSize]);
129 CGSize bounds([scroller bounds].size);
132 max.x = size.width - bounds.width;
133 max.y = size.height - bounds.height;
141 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
142 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
144 [scroller setOffset:offset];
149 /* Perl-Compatible RegEx {{{ */
159 Pcre(const char *regex, int options = 0) :
164 code_ = pcre_compile(regex, options, &error, &offset, NULL);
167 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** Pcre(,): [%u] %s", offset, error] userInfo:nil];
169 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
170 matches_ = new int[(capture_ + 1) * 3];
178 NSString *operator [](size_t match) {
179 return [[[NSString alloc] initWithBytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2]) encoding:NSUTF8StringEncoding] autorelease];
182 bool operator ()(NSString *data) {
183 // XXX: length is for characters, not for bytes
184 return operator ()([data UTF8String], [data length]);
187 bool operator ()(const char *data, size_t size) {
189 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
194 static float CYScrollViewDecelerationRateNormal;
196 @interface NSURL (Apple)
197 - (BOOL) isSpringboardHandledURL;
200 @interface UIScrollView (Apple)
201 - (void) setDecelerationRate:(float)value;
202 - (void) setScrollingEnabled:(BOOL)enabled;
205 @interface UIWebView (Apple)
206 - (void) setDataDetectorTypes:(int)types;
207 - (void) _setDrawInWebThread:(BOOL)draw;
208 - (UIScrollView *) _scrollView;
209 - (UIScroller *) _scroller;
210 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message;
211 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
214 @interface WebView (Apple)
215 - (void) _setLayoutInterval:(float)interval;
216 - (void) _setAllowsMessaging:(BOOL)allows;
217 - (void) setShouldUpdateWhileOffscreen:(BOOL)update;
220 @protocol CydgetWebViewDelegate //<UIWebViewDelegate>
221 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
224 @class UIWebViewWebViewDelegate;
226 @interface CydgetWebView : UIWebView {
231 MSClassHook(UIApplication)
233 MSInstanceMessageHook1(void, UIApplication, openURL, NSURL *, url) {
234 [self applicationOpenURL:url];
237 @implementation NSURL (Cydget)
239 - (NSNumber *) cydget$isSpringboardHandledURL {
240 return [NSNumber numberWithBool:[self isSpringboardHandledURL]];
247 MSInstanceMessageHook0(BOOL, NSURL, isSpringboardHandledURL) {
248 if (![NSThread isMainThread])
251 return [[self cydget$yieldToSelector:@selector(cydget$isSpringboardHandledURL)] boolValue];
254 @implementation CydgetWebView
256 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
257 NSObject<CydgetWebViewDelegate> *delegate([self delegate]);
258 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
259 [delegate webView:view didClearWindowObject:window forFrame:frame];
260 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
261 [super webView:view didClearWindowObject:window forFrame:frame];
264 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
265 NSLog(@"addMessageToConsole:%@", message);
267 if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
268 [super webView:view addMessageToConsole:message];
273 @interface WebCydgetLockScreenView : UIView {
274 CydgetWebView *webview_;
275 UIScrollView *scroller_;
281 @implementation WebCydgetLockScreenView
283 //#include "UICaboodle/UCInternal.h"
286 [webview_ setDelegate:nil];
291 - (void) loadRequest:(NSURLRequest *)request {
292 [webview_ loadRequest:request];
295 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
296 [self loadRequest:[NSURLRequest
303 - (void) loadURL:(NSURL *)url {
304 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
308 CGRect frame = {{0, 0}, {320, 480}};
309 frame.size.height -= 20; //[[[$SBStatusBarController sharedStatusBarController] statusBarView] frame].size.height;
311 if ((self = [super initWithFrame:frame]) != nil) {
312 CGRect bounds([self bounds]);
313 bounds.size.height -= [TPBottomLockBar defaultHeight];
315 webview_ = [[CydgetWebView alloc] initWithFrame:bounds];
316 [webview_ setDelegate:self];
317 [self addSubview:webview_];
319 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
320 [webview_ setDataDetectorTypes:0x80000000];
322 [webview_ setDetectsPhoneNumbers:NO];
324 [webview_ setScalesPageToFit:YES];
326 if (kCFCoreFoundationVersionNumber < 478.61)
327 if ([webview_ respondsToSelector:@selector(_setDrawInWebThread:)])
328 [webview_ _setDrawInWebThread:NO];
330 UIWebDocumentView *document([webview_ _documentView]);
331 WebView *webview([document webView]);
332 WebPreferences *preferences([webview preferences]);
334 [document setTileSize:CGSizeMake(bounds.size.width, 500)];
336 [document setBackgroundColor:[UIColor blackColor]];
337 [document setDrawsBackground:NO];
339 [webview setPreferencesIdentifier:@"WebCycript"];
341 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
342 [webview _setLayoutInterval:0];
344 [preferences _setLayoutInterval:0];
346 [preferences setCacheModel:WebCacheModelDocumentViewer];
347 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
348 [preferences setOfflineWebApplicationCacheEnabled:YES];
350 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
351 [webview setShouldUpdateWhileOffscreen:NO];
353 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
354 [document setAllowsMessaging:YES];
355 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
356 [webview _setAllowsMessaging:YES];
358 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
359 scroller_ = [webview_ _scrollView];
361 [scroller_ setDirectionalLockEnabled:YES];
362 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
363 [scroller_ setDelaysContentTouches:NO];
365 [scroller_ setCanCancelContentTouches:YES];
367 [scroller_ setAlwaysBounceVertical:NO];
368 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
369 UIScroller *scroller([webview_ _scroller]);
370 scroller_ = (UIScrollView *) scroller;
372 [scroller setDirectionalScrolling:YES];
373 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
374 [scroller setScrollHysteresis:0]; /* 8 */
376 [scroller setThumbDetectionEnabled:NO];
379 [scroller_ setFixedBackgroundPattern:YES];
380 [scroller_ setBackgroundColor:[UIColor blackColor]];
381 [scroller_ setClipsSubviews:NO];
383 [scroller_ setBounces:YES];
384 [scroller_ setShowBackgroundShadow:NO]; /* YES */
386 [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
387 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
389 NSDictionary *configuration([$CydgetController currentConfiguration]);
391 cycript_ = [configuration objectForKey:@"CycriptURLs"];
393 [scroller_ setScrollingEnabled:[[configuration objectForKey:@"Scrollable"] boolValue]];
395 NSString *homepage([configuration objectForKey:@"Homepage"]);
396 [self loadURL:[NSURL URLWithString:homepage]];
400 - (void) webView:(WebView *)webview didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
402 if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
403 if (Pcre([cycript_ UTF8String], 0 /*XXX:PCRE_UTF8*/)(href))
404 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
405 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
406 WebFrame *frame([webview mainFrame]);
407 JSGlobalContextRef context([frame globalContext]);
408 CYSetupContext(context);
414 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
425 // String Helpers {{{
426 static const UChar *(*_ZNK7WebCore6String10charactersEv)(const WebCore::String *);
427 static const UChar *(*_ZN7WebCore6String29charactersWithNullTerminationEv)(const WebCore::String *);
428 static unsigned (*_ZNK7WebCore6String6lengthEv)(const WebCore::String *);
430 static bool StringGet(const WebCore::String &string, const UChar *&data, size_t &length) {
433 if (_ZNK7WebCore6String10charactersEv != NULL) {
434 data = (*_ZNK7WebCore6String10charactersEv)(&string);
436 } else if (_ZN7WebCore6String29charactersWithNullTerminationEv != NULL) {
437 data = (*_ZN7WebCore6String29charactersWithNullTerminationEv)(&string);
444 if (_ZNK7WebCore6String6lengthEv != NULL)
445 length = (*_ZNK7WebCore6String6lengthEv)(&string);
447 for (length = 0; data[length] != 0; ++length);
453 static bool StringEquals(const WebCore::String &string, const char *value) {
456 if (!StringGet(string, data, size))
459 size_t length(strlen(value));
463 for (size_t index(0); index != length; ++index)
464 if (data[index] != value[index])
471 static bool cycript_;
473 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
474 if (!StringEquals(mime, "text/cycript")) {
476 return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
479 static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
487 // Script Compiler {{{
488 static void Log(const WebCore::String &string) {
492 if (!StringGet(string, data, length))
495 UChar terminated[length + 1];
496 terminated[length] = 0;
497 memcpy(terminated, data, length * 2);
498 NSLog(@"wtf %p:%zu:%S:", &string, length, terminated);
502 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
505 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
506 if (void (*CydgetPoolParse)(apr_pool_t *, const uint16_t **, size_t *) = reinterpret_cast<void (*)(apr_pool_t *, const uint16_t **, size_t *)>(dlsym(handle, "CydgetPoolParse")))
507 CydgetPoolParse(pool, &data, &size);
510 static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned);
511 static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned);
513 static void Cycriptify(const WebCore::String &source, int *psize = NULL) {
521 if (!StringGet(source, data, length)) {
528 apr_pool_create(&pool, NULL);
530 Cycriptify(pool, data, size);
532 WebCore::String &script(const_cast<WebCore::String &>(source));
534 _ZN7WebCore6String8truncateEj(&script, 0);
535 _ZN7WebCore6String6appendEPKtj(&script, data, size);
540 apr_pool_destroy(pool);
546 static WebCore::String *string;
549 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, void *_this, const WebCore::String &string, State state, const WebCore::String &url, int line) {
551 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i(_this, string, state, url, line);
555 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
557 JSC::SourceCode *source(_this[iOS32 ? 6 : 0]);
558 const uint16_t *data(source->data());
559 size_t size(source->length());
562 apr_pool_create(&pool, NULL);
564 Cycriptify(pool, data, size);
565 source->~SourceCode();
566 // XXX: I actually don't have the original URL here: pants
567 new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
569 apr_pool_destroy(pool);
573 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
577 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) {
579 return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line);
583 MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) {
584 const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this));
585 string = const_cast<WebCore::String *>(&script);
591 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, void *_this, void *source, State state) {
592 if (string != NULL) {
594 Cycriptify(*string, reinterpret_cast<int *>(source) + 3);
599 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, source, state);
603 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
605 return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE(_this, source, url, position);
609 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
611 return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE(_this, position, legacy);
614 void (*_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE)(void *_this, int legacy);
617 MSHook(void, _ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, void *_this, void *source) {
618 if (string != NULL) {
619 _ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE(_this, 0);
620 Cycriptify(*string, reinterpret_cast<int *>(source) + 3);
624 return __ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE(_this, source);
628 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
630 return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE(_this, source, url, position);
634 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
636 return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE(_this, position, legacy);
639 /* Cydget:// Protocol {{{ */
640 @interface CydgetURLProtocol : NSURLProtocol {
645 @implementation CydgetURLProtocol
647 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
648 NSURL *url([request URL]);
651 NSString *scheme([[url scheme] lowercaseString]);
652 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
657 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
661 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
662 id<NSURLProtocolClient> client([self client]);
664 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
666 NSData *data(UIImagePNGRepresentation(icon));
668 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
669 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
670 [client URLProtocol:self didLoadData:data];
671 [client URLProtocolDidFinishLoading:self];
675 - (void) startLoading {
676 id<NSURLProtocolClient> client([self client]);
677 NSURLRequest *request([self request]);
679 NSURL *url([request URL]);
680 NSString *href([url absoluteString]);
682 NSString *path([href substringFromIndex:9]);
683 NSRange slash([path rangeOfString:@"/"]);
686 if (slash.location == NSNotFound) {
690 command = [path substringToIndex:slash.location];
691 path = [path substringFromIndex:(slash.location + 1)];
694 if ([command isEqualToString:@"_UIImageWithName"]) {
697 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
698 UIImage *icon(_UIImageWithName(path));
699 [self _returnPNGWithImage:icon forRequest:request];
701 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
705 - (void) stopLoading {
710 /* Cydget-CGI:// Protocol {{{ */
711 @interface CydgetCGIURLProtocol : NSURLProtocol {
713 CFHTTPMessageRef http_;
714 NSFileHandle *handle_;
719 @implementation CydgetCGIURLProtocol
721 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
722 NSURL *url([request URL]);
725 NSString *scheme([[url scheme] lowercaseString]);
726 if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"])
731 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
735 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
736 if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
741 - (void) startLoading {
742 id<NSURLProtocolClient> client([self client]);
743 NSURLRequest *request([self request]);
744 NSURL *url([request URL]);
746 NSString *path([url path]);
748 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
752 NSFileManager *manager([NSFileManager defaultManager]);
753 if (![manager fileExistsAtPath:path]) {
754 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
759 _assert(pipe(fds) != -1);
764 _assert(close(fds[0]) != -1);
765 _assert(close(fds[1]) != -1);
766 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
771 const char *script([path UTF8String]);
773 setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
774 setenv("SCRIPT_FILENAME", script, true);
775 NSString *query([url query]);
777 setenv("QUERY_STRING", [query UTF8String], true);
779 _assert(dup2(fds[1], 1) != -1);
780 _assert(close(fds[0]) != -1);
781 _assert(close(fds[1]) != -1);
783 execl(script, script, NULL);
788 _assert(close(fds[1]) != -1);
790 _assert(http_ == NULL);
791 http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
792 CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
794 _assert(handle_ == nil);
795 handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES];
797 [[NSNotificationCenter defaultCenter]
799 selector:@selector(onRead:)
800 name:@"NSFileHandleReadCompletionNotification"
804 [handle_ readInBackgroundAndNotify];
807 - (void) onRead:(NSNotification *)notification {
808 NSFileHandle *handle([notification object]);
810 NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
812 if (size_t length = [data length]) {
813 CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
814 [handle readInBackgroundAndNotify];
816 id<NSURLProtocolClient> client([self client]);
818 CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
820 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
822 NSURLRequest *request([self request]);
824 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
827 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
829 CFDataRef body(CFHTTPMessageCopyBody(http_));
830 [client URLProtocol:self didLoadData:(NSData *)body];
833 [client URLProtocolDidFinishLoading:self];
841 //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
843 - (void) stopLoading_ {
844 [[NSNotificationCenter defaultCenter] removeObserver:self];
846 if (handle_ != nil) {
854 _syscall(waitpid(pid_, &status, 0));
859 - (void) stopLoading {
861 performSelectorOnMainThread:@selector(stopLoading_)
870 template <typename Type_>
871 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
872 struct nlist &name(nl[index]);
873 uintptr_t value(name.n_value);
874 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
876 function = reinterpret_cast<Type_>(value);
879 template <typename Type_>
880 static void dlset(Type_ &function, const char *name) {
881 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
884 template <typename Type_>
885 static void msset_(Type_ &function, const char *name, MSImageRef handle) {
886 function = reinterpret_cast<Type_>(MSFindSymbol(handle, name));
889 #define msset(function, handle) \
890 msset_(function, "_" #function, handle)
892 @implementation WebCycriptLockScreenController
894 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
895 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
896 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
897 [uiWebView webView:view addMessageToConsole:message];
900 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
901 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
902 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
903 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
906 + (void) initialize {
907 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
908 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
909 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
912 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
913 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
914 else // XXX: this actually might be fast on some older systems: we should look into this
915 CYScrollViewDecelerationRateNormal = 0.998;
917 iOS4 = kCFCoreFoundationVersionNumber >= 550.32;
918 iOS32 = !iOS4 && kCFCoreFoundationVersionNumber >= 478.61;
921 size_t size(sizeof(maxproc));
922 if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
923 NSLog(@"sysctlbyname(\"kern.maxproc\", ?)");
924 else if (maxproc < 72) {
926 if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
927 NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
932 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
933 [WebView registerURLSchemeAsLocal:@"cydget"];
935 [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]];
936 [WebView registerURLSchemeAsLocal:@"cydget-cgi"];
938 MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"));
939 MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
942 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
943 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
944 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
945 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
949 memset(nl, 0, sizeof(nl));
951 nl[0].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
953 nl[1].n_un.n_name = (char *) "__ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi";
955 nl[2].n_un.n_name = (char *) "__ZN7WebCore12CachedScript6scriptEv";
956 nl[3].n_un.n_name = (char *) "__ZNK7WebCore20StringSourceProvider6sourceEv";
958 nl[4].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i";
959 nl[5].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE";
961 nl[6].n_un.n_name = (char *) "__ZN7WebCore6String6appendEPKtj";
962 nl[7].n_un.n_name = (char *) "__ZN7WebCore6String8truncateEj";
964 nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
966 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
967 nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 0);
968 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
969 MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKN3WTF6StringE", WebCore);
970 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE != NULL)
971 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
973 void (*_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)(void *, const WebCore::String &, const WebCore::KURL &, int);
974 nlset(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, nl, 1);
975 if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi != NULL)
976 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi));
979 const WebCore::String &(*_ZN7WebCore12CachedScript6scriptEv)(void *);
980 nlset(_ZN7WebCore12CachedScript6scriptEv, nl, 2);
981 if (_ZN7WebCore12CachedScript6scriptEv != NULL)
982 MSHookFunction(_ZN7WebCore12CachedScript6scriptEv, MSHake(_ZN7WebCore12CachedScript6scriptEv));
985 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i)(void *, const WebCore::String &, State, const WebCore::String &, int);
986 nlset(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, nl, 4);
987 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i != NULL)
988 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i));
991 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)(void *, void *, State);
992 nlset(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, nl, 5);
993 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE != NULL)
994 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE));
997 nlset(_ZN7WebCore6String6appendEPKtj, nl, 6);
998 nlset(_ZN7WebCore6String8truncateEj, nl, 7);
1000 void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
1001 msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, WebCore);
1002 if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE != NULL)
1003 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE));
1005 void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
1006 msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, WebCore);
1007 if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE != NULL)
1008 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE));
1010 void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE)(void *, void *, int);
1011 msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, WebCore);
1012 if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE != NULL)
1013 MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE));
1015 void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE)(void *, void *, int);
1016 msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, WebCore);
1017 if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE != NULL)
1018 MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE));
1020 msset(_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE, WebCore);
1022 void (*_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE)(void *, void *);
1023 msset(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, WebCore);
1024 if (_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE != NULL)
1025 MSHookFunction(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, MSHake(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE));
1027 if (_ZN7WebCore6String6appendEPKtj == NULL)
1028 msset(_ZN7WebCore6String6appendEPKtj, JavaScriptCore);
1029 if (_ZN7WebCore6String6appendEPKtj == NULL)
1030 MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN3WTF6String6appendEPKtj", JavaScriptCore);
1032 if (_ZN7WebCore6String8truncateEj == NULL)
1033 msset(_ZN7WebCore6String8truncateEj, JavaScriptCore);
1034 if (_ZN7WebCore6String8truncateEj == NULL)
1035 MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN3WTF6String8truncateEj", JavaScriptCore);
1037 msset(_ZNK7WebCore6String10charactersEv, WebCore);
1039 msset(_ZN7WebCore6String29charactersWithNullTerminationEv, JavaScriptCore);
1040 if (_ZN7WebCore6String29charactersWithNullTerminationEv == NULL)
1041 MSHookSymbol(_ZN7WebCore6String29charactersWithNullTerminationEv, "__ZN3WTF6String29charactersWithNullTerminationEv", JavaScriptCore);
1043 msset(_ZNK7WebCore6String6lengthEv, WebCore);
1046 + (id) rootViewController {
1047 return [[[self alloc] init] autorelease];
1051 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];
1054 - (BOOL) showAwayItems {
1060 MSClassHook(WebView)
1061 MSMetaClassHook(WebView)
1063 MSClassMessageHook0(void, WebView, enableWebThread) {
1064 if (kCFCoreFoundationVersionNumber >= 478.61)
1067 NSLog(@"-[WebView enableWebThread]");