1 /* Cydget - open-source AwayView plugin multiplexer
2 * Copyright (C) 2009-2014 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydia 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.
12 * Cydia 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.
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
22 #include <CydiaSubstrate/CydiaSubstrate.h>
23 #include <UIKit/UIKit.h>
25 #include <sys/sysctl.h>
28 #include <SpringBoardUI/SBAwayViewPluginController.h>
30 #include <WebKit/WebFrame.h>
31 #include <WebKit/WebView.h>
32 #include <WebKit/WebPreferences-WebPrivate.h>
34 #include "yieldToSelector.h"
37 #include <unicode/uregex.h>
43 #define _forever for (;;)
45 _disused static unsigned trace_;
47 #define _trace() do { \
48 NSLog(@"_trace(%u)@%s:%u[%s](%p)\n", \
49 trace_++, __FILE__, __LINE__, __FUNCTION__, pthread_self() \
53 #define _assert(test) do \
55 NSLog(@"_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
60 #define _syscall(expr) \
61 do if ((long) (expr) != -1) \
63 else switch (errno) { \
70 extern "C" UIImage *_UIImageWithName(NSString *name);
72 typedef uint16_t UChar;
74 @interface TPBottomLockBar
75 - (CGFloat) defaultHeight;
78 @interface UIApplication (Apple)
79 - (void) applicationOpenURL:(NSURL *)url;
82 @interface UIScroller : UIView
83 - (CGSize) contentSize;
84 - (void) setDirectionalScrolling:(BOOL)directional;
85 - (void) setOffset:(CGPoint)offset;
86 - (void) setScrollDecelerationFactor:(CGFloat)factor;
87 - (void) setScrollHysteresis:(CGFloat)hysteresis;
88 - (void) setThumbDetectionEnabled:(BOOL)enabled;
91 @interface UIWebDocumentView : UIView
92 - (CGRect) documentBounds;
93 - (void) enableReachability;
94 - (void) loadRequest:(NSURLRequest *)request;
95 - (void) redrawScaledDocument;
96 - (void) setAllowsImageSheet:(BOOL)allows;
97 - (void) setAllowsMessaging:(BOOL)allows;
98 - (void) setAutoresizes:(BOOL)autoresizes;
99 - (void) setContentsPosition:(NSInteger)position;
100 - (void) setDrawsBackground:(BOOL)draws;
101 - (void) _setDocumentType:(NSInteger)type;
102 - (void) setDrawsGrid:(BOOL)draws;
103 - (void) setInitialScale:(float)scale forDocumentTypes:(NSInteger)types;
104 - (void) setLogsTilingChanges:(BOOL)logs;
105 - (void) setMinimumScale:(float)scale forDocumentTypes:(NSInteger)types;
106 - (void) setMinimumSize:(CGSize)size;
107 - (void) setMaximumScale:(float)scale forDocumentTypes:(NSInteger)tpyes;
108 - (void) setSmoothsFonts:(BOOL)smooths;
109 - (void) setTileMinificationFilter:(NSString *)filter;
110 - (void) setTileSize:(CGSize)size;
111 - (void) setTilingEnabled:(BOOL)enabled;
112 - (void) setViewportSize:(CGSize)size forDocumentTypes:(NSInteger)types;
113 - (void) setZoomsFocusedFormControl:(BOOL)zooms;
114 - (void) useSelectionAssistantWithMode:(NSInteger)mode;
115 - (WebView *) webView;
118 @interface UIView (Apple)
119 - (UIScroller *) _scroller;
120 - (void) setClipsSubviews:(BOOL)clips;
121 - (void) setEnabledGestures:(NSInteger)gestures;
122 - (void) setFixedBackgroundPattern:(BOOL)fixed;
123 - (void) setGestureDelegate:(id)delegate;
124 - (void) setNeedsDisplayOnBoundsChange:(BOOL)needs;
125 - (void) setValue:(NSValue *)value forGestureAttribute:(NSInteger)attribute;
126 - (void) setZoomScale:(float)scale duration:(double)duration;
127 - (void) _setZoomScale:(float)scale duration:(double)duration;
130 @protocol CydgetController
131 - (NSDictionary *) currentConfiguration;
134 static Class $CydgetController(objc_getClass("CydgetController"));
136 static bool iOS32, iOS4;
138 @interface NSString (UIKit)
139 - (NSString *) stringByAddingPercentEscapes;
142 @implementation UIWebDocumentView (WebCycript)
144 - (void) _setScrollerOffset:(CGPoint)offset {
145 UIScroller *scroller([self _scroller]);
147 CGSize size([scroller contentSize]);
148 CGSize bounds([scroller bounds].size);
151 max.x = size.width - bounds.width;
152 max.y = size.height - bounds.height;
160 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
161 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
163 [scroller setOffset:offset];
169 /* ICU Regular Expression {{{ */
172 URegularExpression *regex_;
175 RegEx(const char *regex) {
177 UErrorCode status(U_ZERO_ERROR);
178 regex_ = uregex_openC(regex, 0, &error, &status);
179 if (U_FAILURE(status))
180 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** RegEx(): [%u] %s", error.offset, u_errorName(status)] userInfo:nil];
184 uregex_close(regex_);
187 bool operator ()(NSString *string) {
188 return operator ()(reinterpret_cast<const uint16_t *>([string cStringUsingEncoding:NSUTF16StringEncoding]), [string length]);
191 bool operator ()(const UChar *data, size_t size) {
192 UErrorCode status(U_ZERO_ERROR);
193 uregex_setText(regex_, data, size, &status);
194 _assert(U_SUCCESS(status));
195 bool matches(uregex_matches(regex_, 0, &status));
196 _assert(U_SUCCESS(status));
202 /* Perl-Compatible RegEx {{{ */
212 RegEx(const char *regex) :
217 code_ = pcre_compile(regex, 0, &error, &offset, NULL);
220 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** RegEx(): [%u] %s", offset, error] userInfo:nil];
222 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
223 matches_ = new int[(capture_ + 1) * 3];
231 bool operator ()(NSString *data) {
232 // XXX: length is for characters, not for bytes
233 return operator ()([data UTF8String], [data length]);
236 bool operator ()(const char *data, size_t size) {
238 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
244 static float CYScrollViewDecelerationRateNormal;
246 @interface NSURL (Apple)
247 - (BOOL) isSpringboardHandledURL;
250 @interface UIScrollView (Apple)
251 - (void) setDecelerationRate:(CGFloat)value;
252 - (void) setScrollingEnabled:(BOOL)enabled;
253 - (void) setShowBackgroundShadow:(BOOL)show;
256 @interface UIWebView (Apple)
257 - (UIWebDocumentView *) _documentView;
258 - (void) setDataDetectorTypes:(NSInteger)types;
259 - (void) _setDrawInWebThread:(BOOL)draw;
260 - (UIScrollView *) _scrollView;
261 - (UIScroller *) _scroller;
262 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message;
263 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
266 @interface WebView (Apple)
267 - (void) _setLayoutInterval:(float)interval;
268 - (void) _setAllowsMessaging:(BOOL)allows;
269 - (void) setShouldUpdateWhileOffscreen:(BOOL)update;
272 @protocol CydgetWebViewDelegate <UIWebViewDelegate>
273 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
276 @class UIWebViewWebViewDelegate;
278 @interface CydgetWebView : UIWebView {
283 MSClassHook(UIApplication)
285 MSInstanceMessageHook1(void, UIApplication, openURL, NSURL *, url) {
286 [self applicationOpenURL:url];
289 @implementation NSURL (Cydget)
291 - (NSNumber *) cydget$isSpringboardHandledURL {
292 return [NSNumber numberWithBool:[self isSpringboardHandledURL]];
299 MSInstanceMessageHook0(BOOL, NSURL, isSpringboardHandledURL) {
300 if (![NSThread isMainThread])
303 return [[self cydget$yieldToSelector:@selector(cydget$isSpringboardHandledURL)] boolValue];
306 @implementation CydgetWebView
308 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
309 NSObject<CydgetWebViewDelegate> *delegate((NSObject<CydgetWebViewDelegate> *) [self delegate]);
310 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
311 [delegate webView:view didClearWindowObject:window forFrame:frame];
312 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
313 [super webView:view didClearWindowObject:window forFrame:frame];
316 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
317 NSLog(@"addMessageToConsole:%@", message);
319 if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
320 [super webView:view addMessageToConsole:message];
325 @interface WebCydgetLockScreenView : UIView <UIWebViewDelegate> {
326 CydgetWebView *webview_;
327 UIScrollView *scroller_;
333 @implementation WebCydgetLockScreenView
335 //#include "UICaboodle/UCInternal.h"
338 [webview_ setDelegate:nil];
343 - (void) loadRequest:(NSURLRequest *)request {
344 [webview_ loadRequest:request];
347 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
348 [self loadRequest:[NSURLRequest
355 - (void) loadURL:(NSURL *)url {
356 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
359 - (id) initWithURL:(NSURL *)url {
360 CGRect frame = [[UIScreen mainScreen] bounds];
361 if (kCFCoreFoundationVersionNumber < 800)
362 frame.size.height -= 20; //[[[$SBStatusBarController sharedStatusBarController] statusBarView] frame].size.height;
364 if ((self = [super initWithFrame:frame]) != nil) {
365 CGRect bounds([self bounds]);
366 if (kCFCoreFoundationVersionNumber < 800)
367 bounds.size.height -= [TPBottomLockBar defaultHeight];
369 webview_ = [[CydgetWebView alloc] initWithFrame:bounds];
370 [webview_ setDelegate:self];
371 [self addSubview:webview_];
373 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
374 [webview_ setDataDetectorTypes:0x80000000];
376 [webview_ setDetectsPhoneNumbers:NO];
378 [webview_ setScalesPageToFit:YES];
380 if (kCFCoreFoundationVersionNumber < 478.61)
381 if ([webview_ respondsToSelector:@selector(_setDrawInWebThread:)])
382 [webview_ _setDrawInWebThread:NO];
384 UIWebDocumentView *document([webview_ _documentView]);
385 WebView *webview([document webView]);
386 WebPreferences *preferences([webview preferences]);
388 [document setTileSize:CGSizeMake(bounds.size.width, 500)];
390 [document setBackgroundColor:[UIColor clearColor]];
391 [document setDrawsBackground:NO];
393 [webview setPreferencesIdentifier:@"WebCycript"];
395 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
396 [webview _setLayoutInterval:0];
398 [preferences _setLayoutInterval:0];
400 [preferences setCacheModel:WebCacheModelDocumentViewer];
401 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
402 [preferences setOfflineWebApplicationCacheEnabled:YES];
404 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
405 [webview setShouldUpdateWhileOffscreen:NO];
407 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
408 [document setAllowsMessaging:YES];
409 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
410 [webview _setAllowsMessaging:YES];
412 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
413 scroller_ = [webview_ _scrollView];
415 [scroller_ setDirectionalLockEnabled:YES];
416 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
417 [scroller_ setDelaysContentTouches:NO];
419 [scroller_ setCanCancelContentTouches:YES];
421 [scroller_ setAlwaysBounceVertical:NO];
422 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
423 UIScroller *scroller([webview_ _scroller]);
424 scroller_ = (UIScrollView *) scroller;
426 [scroller setDirectionalScrolling:YES];
427 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
428 [scroller setScrollHysteresis:0]; /* 8 */
430 [scroller setThumbDetectionEnabled:NO];
433 [webview_ setOpaque:NO];
434 [webview_ setBackgroundColor:[UIColor clearColor]];
436 [scroller_ setFixedBackgroundPattern:YES];
437 [scroller_ setBackgroundColor:[UIColor clearColor]];
438 [scroller_ setClipsSubviews:NO];
440 [scroller_ setBounces:YES];
441 [scroller_ setShowBackgroundShadow:NO]; /* YES */
443 [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
444 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
446 NSDictionary *configuration([$CydgetController currentConfiguration]);
447 cycript_ = [configuration objectForKey:@"CycriptURLs"];
449 [scroller_ setScrollingEnabled:[[configuration objectForKey:@"Scrollable"] boolValue]];
455 - (void) webView:(WebView *)webview didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
457 if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
458 if (RegEx([cycript_ UTF8String])(href))
459 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
460 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
461 WebFrame *frame([webview mainFrame]);
462 JSGlobalContextRef context([frame globalContext]);
464 CYSetupContext(context);
465 } @catch (NSException *e) {
466 NSLog(@"*** CydgetSetupContext => %@", e);
473 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
474 NSDictionary *configuration_;
475 WebCydgetLockScreenView *background_;
505 union ScriptSourceCode {
507 JSC::SourceCode source_;
511 JSC::SourceCode source_;
515 // String Helpers {{{
516 static const UChar *(*_ZNK7WebCore6String10charactersEv)(const WebCore::String *);
517 static const UChar *(*_ZN7WebCore6String29charactersWithNullTerminationEv)(const WebCore::String *);
518 static unsigned (*_ZNK7WebCore6String6lengthEv)(const WebCore::String *);
520 static bool StringGet(const WebCore::String &string, const UChar *&data, size_t &length) {
523 if (_ZNK7WebCore6String10charactersEv != NULL) {
524 data = (*_ZNK7WebCore6String10charactersEv)(&string);
526 } else if (_ZN7WebCore6String29charactersWithNullTerminationEv != NULL) {
527 data = (*_ZN7WebCore6String29charactersWithNullTerminationEv)(&string);
534 if (_ZNK7WebCore6String6lengthEv != NULL)
535 length = (*_ZNK7WebCore6String6lengthEv)(&string);
537 for (length = 0; data[length] != 0; ++length);
543 static bool StringEquals(const WebCore::String &string, const char *value) {
546 if (!StringGet(string, data, size))
549 size_t length(strlen(value));
553 for (size_t index(0); index != length; ++index)
554 if (data[index] != value[index])
561 static bool cycript_;
563 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
564 if (!StringEquals(mime, "text/cycript")) {
566 return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
569 static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
577 // Script Compiler {{{
578 static void Log(const WebCore::String &string) {
582 if (!StringGet(string, data, length))
585 UChar terminated[length + 1];
586 terminated[length] = 0;
587 memcpy(terminated, data, length * 2);
588 NSLog(@"wtf %p:%zu:%S:", &string, length, terminated);
592 static bool Cycriptify(const uint16_t *&data, size_t &size) {
595 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
596 if (void (*CydgetMemoryParse)(const uint16_t **, size_t *) = reinterpret_cast<void (*)(const uint16_t **, size_t *)>(dlsym(handle, "CydgetMemoryParse"))) @try {
597 CydgetMemoryParse(&data, &size);
599 } @catch (NSException *e) {
600 NSLog(@"*** CydgetMemoryParse => %@", e);
605 static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned);
606 static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned);
608 static void Cycriptify(const WebCore::String &source, int *psize = NULL) {
615 if (!StringGet(source, data, length))
619 if (!Cycriptify(data, size))
622 WebCore::String &script(const_cast<WebCore::String &>(source));
623 _ZN7WebCore6String8truncateEj(&script, 0);
624 _ZN7WebCore6String6appendEPKtj(&script, data, size);
635 static WebCore::String *string;
638 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, void *_this, const WebCore::String &string, State state, const WebCore::String &url, int line) {
640 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i(_this, string, state, url, line);
644 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
646 JSC::SourceCode *source(_this[iOS32 ? 6 : 0]);
647 const uint16_t *data(source->data());
648 size_t size(source->length());
650 if (Cycriptify(data, size)) {
651 source->~SourceCode();
652 // XXX: I actually don't have the original URL here: pants
653 new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
658 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
662 MSHook(const WebCore::String &, _ZNK7WebCore4Node11textContentEb, void *_this, bool convert) {
663 const WebCore::String &code(__ZNK7WebCore4Node11textContentEb(_this, convert));
664 string = const_cast<WebCore::String *>(&code);
671 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) {
673 return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line);
677 MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) {
678 const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this));
679 string = const_cast<WebCore::String *>(&script);
685 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, void *_this, JSC::ScriptSourceCode &script, State state) {
686 if (string != NULL) {
687 JSC::SourceCode *source(iOS4 ? &script.New.source_ : &script.Old.source_);
688 Cycriptify(*string, &source->end_);
692 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, script, state);
696 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
698 return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE(_this, source, url, position);
702 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
704 return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE(_this, position, legacy);
707 void (*$_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE)(void *_this, int legacy);
710 MSHook(void, _ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, void *_this, JSC::ScriptSourceCode &script) {
711 if (string != NULL) {
712 JSC::SourceCode *source(&script.New.source_);
713 $_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE(_this, 0);
714 Cycriptify(*string, &source->end_);
718 return __ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE(_this, script);
722 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
724 return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE(_this, source, url, position);
728 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
730 return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE(_this, position, legacy);
733 /* Cydget:// Protocol {{{ */
734 @interface CydgetURLProtocol : NSURLProtocol {
739 @implementation CydgetURLProtocol
741 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
742 NSURL *url([request URL]);
745 NSString *scheme([[url scheme] lowercaseString]);
746 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
751 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
755 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
756 id<NSURLProtocolClient> client([self client]);
758 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
760 NSData *data(UIImagePNGRepresentation(icon));
762 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
763 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
764 [client URLProtocol:self didLoadData:data];
765 [client URLProtocolDidFinishLoading:self];
769 - (void) startLoading {
770 id<NSURLProtocolClient> client([self client]);
771 NSURLRequest *request([self request]);
773 NSURL *url([request URL]);
774 NSString *href([url absoluteString]);
776 NSString *path([href substringFromIndex:9]);
777 NSRange slash([path rangeOfString:@"/"]);
780 if (slash.location == NSNotFound) {
784 command = [path substringToIndex:slash.location];
785 path = [path substringFromIndex:(slash.location + 1)];
788 if ([command isEqualToString:@"_UIImageWithName"]) {
791 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
792 UIImage *icon(_UIImageWithName(path));
793 [self _returnPNGWithImage:icon forRequest:request];
795 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
799 - (void) stopLoading {
804 /* Cydget-CGI:// Protocol {{{ */
805 @interface CydgetCGIURLProtocol : NSURLProtocol {
807 CFHTTPMessageRef http_;
808 NSFileHandle *handle_;
813 @implementation CydgetCGIURLProtocol
815 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
816 NSURL *url([request URL]);
819 NSString *scheme([[url scheme] lowercaseString]);
820 if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"])
825 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
829 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
830 if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
835 - (void) startLoading {
836 id<NSURLProtocolClient> client([self client]);
837 NSURLRequest *request([self request]);
838 NSURL *url([request URL]);
840 NSString *path([url path]);
842 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
846 NSFileManager *manager([NSFileManager defaultManager]);
847 if (![manager fileExistsAtPath:path]) {
848 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
853 _assert(pipe(fds) != -1);
858 _assert(close(fds[0]) != -1);
859 _assert(close(fds[1]) != -1);
860 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
865 const char *script([path UTF8String]);
867 setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
868 setenv("SCRIPT_FILENAME", script, true);
869 NSString *query([url query]);
871 setenv("QUERY_STRING", [query UTF8String], true);
873 _assert(dup2(fds[1], 1) != -1);
874 _assert(close(fds[0]) != -1);
875 _assert(close(fds[1]) != -1);
877 execl(script, script, NULL);
882 _assert(close(fds[1]) != -1);
884 _assert(http_ == NULL);
885 http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
886 CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
888 _assert(handle_ == nil);
889 handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES];
891 [[NSNotificationCenter defaultCenter]
893 selector:@selector(onRead:)
894 name:@"NSFileHandleReadCompletionNotification"
898 [handle_ readInBackgroundAndNotify];
901 - (void) onRead:(NSNotification *)notification {
902 NSFileHandle *handle([notification object]);
904 NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
906 if (size_t length = [data length]) {
907 CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
908 [handle readInBackgroundAndNotify];
910 id<NSURLProtocolClient> client([self client]);
912 CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
914 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
916 NSURLRequest *request([self request]);
918 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
921 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
923 CFDataRef body(CFHTTPMessageCopyBody(http_));
924 [client URLProtocol:self didLoadData:(NSData *)body];
927 [client URLProtocolDidFinishLoading:self];
935 //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
937 - (void) stopLoading_ {
938 [[NSNotificationCenter defaultCenter] removeObserver:self];
940 if (handle_ != nil) {
948 _syscall(waitpid(pid_, &status, 0));
953 - (void) stopLoading {
955 performSelectorOnMainThread:@selector(stopLoading_)
964 template <typename Type_>
965 static void dlset(Type_ &function, const char *name) {
966 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
969 template <typename Type_>
970 static void msset_(Type_ &function, const char *name, MSImageRef handle) {
971 function = reinterpret_cast<Type_>(MSFindSymbol(handle, name));
974 #define msset(function, handle) \
975 msset_(function, "_" #function, handle)
977 @implementation WebCycriptLockScreenController
979 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
980 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
981 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
982 [uiWebView webView:view addMessageToConsole:message];
985 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
986 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
987 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
988 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
991 + (void) initialize {
992 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
993 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
994 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
997 if (CGFloat *_UIScrollViewDecelerationRateNormal = reinterpret_cast<CGFloat *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
998 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
999 else // XXX: this actually might be fast on some older systems: we should look into this
1000 CYScrollViewDecelerationRateNormal = 0.998;
1002 iOS4 = kCFCoreFoundationVersionNumber >= 550.32;
1003 iOS32 = !iOS4 && kCFCoreFoundationVersionNumber >= 478.61;
1006 size_t size(sizeof(maxproc));
1007 if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
1008 NSLog(@"sysctlbyname(\"kern.maxproc\", ?)");
1009 else if (maxproc < 72) {
1011 if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
1012 NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
1015 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
1016 [WebView registerURLSchemeAsLocal:@"cydget"];
1018 [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]];
1019 [WebView registerURLSchemeAsLocal:@"cydget-cgi"];
1021 MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"));
1022 MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
1025 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
1026 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
1027 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
1028 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
1031 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &) = NULL;
1032 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
1033 MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE", WebCore);
1034 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
1035 MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKN3WTF6StringE", WebCore);
1036 if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE != NULL)
1037 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
1039 void (*_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)(void *, const WebCore::String &, const WebCore::KURL &, int) = NULL;
1040 if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi == NULL)
1041 MSHookSymbol(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, "__ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi", WebCore);
1042 if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi != NULL)
1043 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi));
1046 const WebCore::String &(*_ZNK7WebCore4Node11textContentEb)(void *, bool) = NULL;
1047 if (_ZNK7WebCore4Node11textContentEb == NULL)
1048 MSHookSymbol(_ZNK7WebCore4Node11textContentEb, "__ZNK7WebCore4Node11textContentEb", WebCore);
1049 if (_ZNK7WebCore4Node11textContentEb != NULL)
1050 MSHookFunction(_ZNK7WebCore4Node11textContentEb, MSHake(_ZNK7WebCore4Node11textContentEb));
1053 const WebCore::String &(*_ZN7WebCore12CachedScript6scriptEv)(void *) = NULL;
1054 if (_ZN7WebCore12CachedScript6scriptEv == NULL)
1055 MSHookSymbol(_ZN7WebCore12CachedScript6scriptEv, "__ZN7WebCore12CachedScript6scriptEv", WebCore);
1056 if (_ZN7WebCore12CachedScript6scriptEv != NULL)
1057 MSHookFunction(_ZN7WebCore12CachedScript6scriptEv, MSHake(_ZN7WebCore12CachedScript6scriptEv));
1059 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i)(void *, const WebCore::String &, State, const WebCore::String &, int) = NULL;
1060 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i == NULL)
1061 MSHookSymbol(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i", WebCore);
1062 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i != NULL)
1063 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i));
1065 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)(void *, JSC::ScriptSourceCode &, State) = NULL;
1066 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE == NULL)
1067 MSHookSymbol(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE", WebCore);
1068 if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE != NULL)
1069 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE));
1071 void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
1072 msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, WebCore);
1073 if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE != NULL)
1074 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE));
1076 void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
1077 msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, WebCore);
1078 if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE != NULL)
1079 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE));
1081 void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE)(void *, void *, int);
1082 msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, WebCore);
1083 if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE != NULL)
1084 MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE));
1086 void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE)(void *, void *, int);
1087 msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, WebCore);
1088 if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE != NULL)
1089 MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE));
1091 MSHookSymbol($_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE, "__ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE", WebCore);
1093 void (*_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE)(void *, JSC::ScriptSourceCode &);
1094 msset(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, WebCore);
1095 if (_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE != NULL)
1096 MSHookFunction(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, MSHake(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE));
1098 if (_ZN7WebCore6String6appendEPKtj == NULL)
1099 MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN7WebCore6String6appendEPKtj", WebCore);
1100 if (_ZN7WebCore6String6appendEPKtj == NULL)
1101 msset(_ZN7WebCore6String6appendEPKtj, JavaScriptCore);
1102 if (_ZN7WebCore6String6appendEPKtj == NULL)
1103 MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN3WTF6String6appendEPKtj", JavaScriptCore);
1105 if (_ZN7WebCore6String8truncateEj == NULL)
1106 MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN7WebCore6String8truncateEj", WebCore);
1107 if (_ZN7WebCore6String8truncateEj == NULL)
1108 msset(_ZN7WebCore6String8truncateEj, JavaScriptCore);
1109 if (_ZN7WebCore6String8truncateEj == NULL)
1110 MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN3WTF6String8truncateEj", JavaScriptCore);
1112 msset(_ZNK7WebCore6String10charactersEv, WebCore);
1114 msset(_ZN7WebCore6String29charactersWithNullTerminationEv, JavaScriptCore);
1115 if (_ZN7WebCore6String29charactersWithNullTerminationEv == NULL)
1116 MSHookSymbol(_ZN7WebCore6String29charactersWithNullTerminationEv, "__ZN3WTF6String29charactersWithNullTerminationEv", JavaScriptCore);
1118 msset(_ZNK7WebCore6String6lengthEv, WebCore);
1121 + (id) rootViewController {
1122 return [[[self alloc] init] autorelease];
1126 [configuration_ release];
1127 [background_ release];
1132 if ((self = [super init]) != nil) {
1133 configuration_ = [[$CydgetController currentConfiguration] retain];
1138 if (NSString *homepage = [configuration_ objectForKey:@"Homepage"])
1139 [self setView:[[[WebCydgetLockScreenView alloc] initWithURL:[NSURL URLWithString:homepage]] autorelease]];
1141 if (NSString *background = [configuration_ objectForKey:@"Background"])
1142 background_ = [[WebCydgetLockScreenView alloc] initWithURL:[NSURL URLWithString:background]];
1145 - (void) purgeView {
1146 [background_ removeFromSuperview];
1147 [background_ release];
1152 - (UIView *) backgroundView {
1156 - (BOOL) showAwayItems {
1160 - (BOOL) showDateView {
1161 return [configuration_ objectForKey:@"Homepage"] == nil;
1164 /*- (BOOL) showHeaderView {
1168 - (NSUInteger) presentationStyle {
1172 - (NSUInteger) overlayStyle {
1173 if ([configuration_ objectForKey:@"Background"] == nil)
1178 - (BOOL) viewWantsFullscreenLayout {
1179 return kCFCoreFoundationVersionNumber >= 800;
1182 /*- (BOOL) viewWantsOverlayLayout {
1183 return kCFCoreFoundationVersionNumber >= 800;
1186 - (BOOL) shouldDisableOnUnlock {
1190 - (BOOL) canBeAlwaysFullscreen {
1194 /*- (BOOL) wantsSwipeGestureRecognizer {
1198 - (BOOL) handleGesture:(int)arg1 fingerCount:(NSUInteger)fingers {
1205 MSClassHook(WebView)
1206 MSMetaClassHook(WebView)
1208 MSClassMessageHook0(void, WebView, enableWebThread) {
1209 if (kCFCoreFoundationVersionNumber >= 478.61)
1212 NSLog(@"-[WebView enableWebThread]");