Port Cydget to iOS 7, ARM64, Xcode 5, and cycc :/.
[cydget.git] / LockScreen.mm
1 /* Cydget - open-source AwayView plugin multiplexer
2  * Copyright (C) 2009-2014  Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
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.
11  *
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.
16  *
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/>.
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 <SpringBoardUI/SBAwayViewPluginController.h>
29
30 #include <WebKit/WebFrame.h>
31 #include <WebKit/WebView.h>
32 #include <WebKit/WebPreferences-WebPrivate.h>
33
34 #include "yieldToSelector.h"
35
36 #ifdef USE_ICU_REGEX
37 #include <unicode/uregex.h>
38 #else
39 #include <pcre.h>
40 #endif
41
42 #define _transient
43 #define _forever for (;;)
44
45 _disused static unsigned trace_;
46
47 #define _trace() do { \
48     NSLog(@"_trace(%u)@%s:%u[%s](%p)\n", \
49         trace_++, __FILE__, __LINE__, __FUNCTION__, pthread_self() \
50     ); \
51 } while (false)
52
53 #define _assert(test) do \
54     if (!(test)) { \
55         NSLog(@"_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
56         exit(-1); \
57     } \
58 while (false)
59
60 #define _syscall(expr) \
61     do if ((long) (expr) != -1) \
62         break; \
63     else switch (errno) { \
64         case EINTR: \
65             continue; \
66         default: \
67             _assert(false); \
68     } while (true)
69
70 extern "C" UIImage *_UIImageWithName(NSString *name);
71
72 typedef uint16_t UChar;
73
74 @interface TPBottomLockBar
75 - (CGFloat) defaultHeight;
76 @end
77
78 @interface UIApplication (Apple)
79 - (void) applicationOpenURL:(NSURL *)url;
80 @end
81
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;
89 @end
90
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;
116 @end
117
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;
128 @end
129
130 @protocol CydgetController
131 - (NSDictionary *) currentConfiguration;
132 @end
133
134 static Class $CydgetController(objc_getClass("CydgetController"));
135
136 static bool iOS32, iOS4;
137
138 @interface NSString (UIKit)
139 - (NSString *) stringByAddingPercentEscapes;
140 @end
141
142 @implementation UIWebDocumentView (WebCycript)
143
144 - (void) _setScrollerOffset:(CGPoint)offset {
145     UIScroller *scroller([self _scroller]);
146
147     CGSize size([scroller contentSize]);
148     CGSize bounds([scroller bounds].size);
149
150     CGPoint max;
151     max.x = size.width - bounds.width;
152     max.y = size.height - bounds.height;
153
154     // wtf Apple?!
155     if (max.x < 0)
156         max.x = 0;
157     if (max.y < 0)
158         max.y = 0;
159
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;
162
163     [scroller setOffset:offset];
164 }
165
166 @end
167
168 #ifdef USE_ICU_REGEX
169 /* ICU Regular Expression {{{ */
170 class RegEx {
171   private:
172     URegularExpression *regex_;
173
174   public:
175     RegEx(const char *regex) {
176         UParseError error;
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];
181     }
182
183     ~RegEx() {
184         uregex_close(regex_);
185     }
186
187     bool operator ()(NSString *string) {
188         return operator ()(reinterpret_cast<const uint16_t *>([string cStringUsingEncoding:NSUTF16StringEncoding]), [string length]);
189     }
190
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));
197         return matches;
198     }
199 };
200 /* }}} */
201 #else
202 /* Perl-Compatible RegEx {{{ */
203 class RegEx {
204   private:
205     pcre *code_;
206     pcre_extra *study_;
207     int capture_;
208     int *matches_;
209     const char *data_;
210
211   public:
212     RegEx(const char *regex) :
213         study_(NULL)
214     {
215         const char *error;
216         int offset;
217         code_ = pcre_compile(regex, 0, &error, &offset, NULL);
218
219         if (code_ == NULL)
220             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** RegEx(): [%u] %s", offset, error] userInfo:nil];
221
222         pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
223         matches_ = new int[(capture_ + 1) * 3];
224     }
225
226     ~RegEx() {
227         pcre_free(code_);
228         delete matches_;
229     }
230
231     bool operator ()(NSString *data) {
232         // XXX: length is for characters, not for bytes
233         return operator ()([data UTF8String], [data length]);
234     }
235
236     bool operator ()(const char *data, size_t size) {
237         data_ = data;
238         return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
239     }
240 };
241 /* }}} */
242 #endif
243
244 static float CYScrollViewDecelerationRateNormal;
245
246 @interface NSURL (Apple)
247 - (BOOL) isSpringboardHandledURL;
248 @end
249
250 @interface UIScrollView (Apple)
251 - (void) setDecelerationRate:(CGFloat)value;
252 - (void) setScrollingEnabled:(BOOL)enabled;
253 - (void) setShowBackgroundShadow:(BOOL)show;
254 @end
255
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;
264 @end
265
266 @interface WebView (Apple)
267 - (void) _setLayoutInterval:(float)interval;
268 - (void) _setAllowsMessaging:(BOOL)allows;
269 - (void) setShouldUpdateWhileOffscreen:(BOOL)update;
270 @end
271
272 @protocol CydgetWebViewDelegate <UIWebViewDelegate>
273 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
274 @end
275
276 @class UIWebViewWebViewDelegate;
277
278 @interface CydgetWebView : UIWebView {
279 }
280
281 @end
282
283 MSClassHook(UIApplication)
284
285 MSInstanceMessageHook1(void, UIApplication, openURL, NSURL *, url) {
286     [self applicationOpenURL:url];
287 }
288
289 @implementation NSURL (Cydget)
290
291 - (NSNumber *) cydget$isSpringboardHandledURL {
292     return [NSNumber numberWithBool:[self isSpringboardHandledURL]];
293 }
294
295 @end
296
297 MSClassHook(NSURL)
298
299 MSInstanceMessageHook0(BOOL, NSURL, isSpringboardHandledURL) {
300     if (![NSThread isMainThread])
301         return MSOldCall();
302
303     return [[self cydget$yieldToSelector:@selector(cydget$isSpringboardHandledURL)] boolValue];
304 }
305
306 @implementation CydgetWebView
307
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];
314 }
315
316 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
317     NSLog(@"addMessageToConsole:%@", message);
318
319     if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
320         [super webView:view addMessageToConsole:message];
321 }
322
323 @end
324
325 @interface WebCydgetLockScreenView : UIView <UIWebViewDelegate> {
326     CydgetWebView *webview_;
327     UIScrollView *scroller_;
328     NSString *cycript_;
329 }
330
331 @end
332
333 @implementation WebCydgetLockScreenView
334
335 //#include "UICaboodle/UCInternal.h"
336
337 - (void) dealloc {
338     [webview_ setDelegate:nil];
339     [webview_ release];
340     [super dealloc];
341 }
342
343 - (void) loadRequest:(NSURLRequest *)request {
344     [webview_ loadRequest:request];
345 }
346
347 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
348     [self loadRequest:[NSURLRequest
349         requestWithURL:url
350         cachePolicy:policy
351         timeoutInterval:30.0
352     ]];
353 }
354
355 - (void) loadURL:(NSURL *)url {
356     [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
357 }
358
359 - (id) init {
360     CGRect frame = [[UIScreen mainScreen] bounds];
361     if (kCFCoreFoundationVersionNumber < 800)
362         frame.size.height -= 20; //[[[$SBStatusBarController sharedStatusBarController] statusBarView] frame].size.height;
363
364     if ((self = [super initWithFrame:frame]) != nil) {
365         CGRect bounds([self bounds]);
366         if (kCFCoreFoundationVersionNumber < 800)
367             bounds.size.height -= [TPBottomLockBar defaultHeight];
368
369         webview_ = [[CydgetWebView alloc] initWithFrame:bounds];
370         [webview_ setDelegate:self];
371         [self addSubview:webview_];
372
373         if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
374             [webview_ setDataDetectorTypes:0x80000000];
375         else
376             [webview_ setDetectsPhoneNumbers:NO];
377
378         [webview_ setScalesPageToFit:YES];
379
380         if (kCFCoreFoundationVersionNumber < 478.61)
381             if ([webview_ respondsToSelector:@selector(_setDrawInWebThread:)])
382                 [webview_ _setDrawInWebThread:NO];
383
384         UIWebDocumentView *document([webview_ _documentView]);
385         WebView *webview([document webView]);
386         WebPreferences *preferences([webview preferences]);
387
388         [document setTileSize:CGSizeMake(bounds.size.width, 500)];
389
390         [document setBackgroundColor:[UIColor clearColor]];
391         [document setDrawsBackground:NO];
392
393         [webview setPreferencesIdentifier:@"WebCycript"];
394
395         if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
396             [webview _setLayoutInterval:0];
397         else
398             [preferences _setLayoutInterval:0];
399
400         [preferences setCacheModel:WebCacheModelDocumentViewer];
401         [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
402         [preferences setOfflineWebApplicationCacheEnabled:YES];
403
404         if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
405             [webview setShouldUpdateWhileOffscreen:NO];
406
407         if ([document respondsToSelector:@selector(setAllowsMessaging:)])
408             [document setAllowsMessaging:YES];
409         if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
410             [webview _setAllowsMessaging:YES];
411
412         if ([webview_ respondsToSelector:@selector(_scrollView)]) {
413             scroller_ = [webview_ _scrollView];
414
415             [scroller_ setDirectionalLockEnabled:YES];
416             [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
417             [scroller_ setDelaysContentTouches:NO];
418
419             [scroller_ setCanCancelContentTouches:YES];
420
421             [scroller_ setAlwaysBounceVertical:NO];
422         } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
423             UIScroller *scroller([webview_ _scroller]);
424             scroller_ = (UIScrollView *) scroller;
425
426             [scroller setDirectionalScrolling:YES];
427             [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
428             [scroller setScrollHysteresis:0]; /* 8 */
429
430             [scroller setThumbDetectionEnabled:NO];
431         }
432
433         [webview_ setOpaque:NO];
434         [webview_ setBackgroundColor:[UIColor clearColor]];
435
436         [scroller_ setFixedBackgroundPattern:YES];
437         [scroller_ setBackgroundColor:[UIColor clearColor]];
438         [scroller_ setClipsSubviews:NO];
439
440         [scroller_ setBounces:YES];
441         [scroller_ setShowBackgroundShadow:NO]; /* YES */
442
443         [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
444         [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
445
446         NSDictionary *configuration([$CydgetController currentConfiguration]);
447
448         cycript_ = [configuration objectForKey:@"CycriptURLs"];
449
450         [scroller_ setScrollingEnabled:[[configuration objectForKey:@"Scrollable"] boolValue]];
451
452         NSString *homepage([configuration objectForKey:@"Homepage"]);
453         [self loadURL:[NSURL URLWithString:homepage]];
454     } return self;
455 }
456
457 - (void) webView:(WebView *)webview didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
458     if (cycript_ != nil)
459         if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
460             if (RegEx([cycript_ UTF8String])(href))
461                 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
462                     if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
463                         WebFrame *frame([webview mainFrame]);
464                         JSGlobalContextRef context([frame globalContext]);
465                         @try {
466                             CYSetupContext(context);
467                         } @catch (NSException *e) {
468                             NSLog(@"*** CydgetSetupContext => %@", e);
469                         }
470                     }
471 }
472
473 @end
474
475 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
476 }
477
478 @end
479
480 #include <string>
481
482 struct State {
483     unsigned state;
484 };
485
486 namespace JSC {
487     class JSGlobalData;
488     class UString;
489 }
490
491 namespace WebCore {
492     class KURL;
493     class String;
494 }
495
496 namespace JSC {
497 struct SourceCode {
498     void *provider_;
499     int start_;
500     int end_;
501     int line_;
502 }; }
503
504 namespace JSC {
505 union ScriptSourceCode {
506     struct {
507         JSC::SourceCode source_;
508     } Old;
509     struct {
510         void *provider_;
511         JSC::SourceCode source_;
512     } New;
513 }; }
514
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 *);
519
520 static bool StringGet(const WebCore::String &string, const UChar *&data, size_t &length) {
521     bool terminated;
522
523     if (_ZNK7WebCore6String10charactersEv != NULL) {
524         data = (*_ZNK7WebCore6String10charactersEv)(&string);
525         terminated = false;
526     } else if (_ZN7WebCore6String29charactersWithNullTerminationEv != NULL) {
527         data = (*_ZN7WebCore6String29charactersWithNullTerminationEv)(&string);
528         terminated = true;
529     } else return false;
530
531     if (data == NULL)
532         return false;
533
534     if (_ZNK7WebCore6String6lengthEv != NULL)
535         length = (*_ZNK7WebCore6String6lengthEv)(&string);
536     else if (terminated)
537         for (length = 0; data[length] != 0; ++length);
538     else return false;
539
540     return true;
541 }
542
543 static bool StringEquals(const WebCore::String &string, const char *value) {
544     const UChar *data;
545     size_t size;
546     if (!StringGet(string, data, size))
547         return false;
548
549     size_t length(strlen(value));
550     if (size != length)
551         return false;
552
553     for (size_t index(0); index != length; ++index)
554         if (data[index] != value[index])
555             return false;
556
557     return true;
558 }
559 // }}}
560 // State Machine {{{
561 static bool cycript_;
562
563 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
564     if (!StringEquals(mime, "text/cycript")) {
565         cycript_ = false;
566         return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
567     }
568
569     static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
570     if (handle == NULL)
571         return false;
572
573     cycript_ = true;
574     return true;
575 }
576 // }}}
577 // Script Compiler {{{
578 static void Log(const WebCore::String &string) {
579 #if 0
580     const UChar *data;
581     size_t length;
582     if (!StringGet(string, data, length))
583         return;
584
585     UChar terminated[length + 1];
586     terminated[length] = 0;
587     memcpy(terminated, data, length * 2);
588     NSLog(@"wtf %p:%zu:%S:", &string, length, terminated);
589 #endif
590 }
591
592 static bool Cycriptify(const uint16_t *&data, size_t &size) {
593     cycript_ = false;
594
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);
598             return true;
599         } @catch (NSException *e) {
600             NSLog(@"*** CydgetMemoryParse => %@", e);
601         }
602     return false;
603 }
604
605 static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned);
606 static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned);
607
608 static void Cycriptify(const WebCore::String &source, int *psize = NULL) {
609     if (!cycript_)
610         return;
611     cycript_ = false;
612
613     const UChar *data;
614     size_t length;
615     if (!StringGet(source, data, length))
616         return;
617
618     size_t size(length);
619     if (!Cycriptify(data, size))
620         return;
621
622     WebCore::String &script(const_cast<WebCore::String &>(source));
623     _ZN7WebCore6String8truncateEj(&script, 0);
624     _ZN7WebCore6String6appendEPKtj(&script, data, size);
625
626     if (psize != NULL)
627         *psize = size;
628
629     free((void *) data);
630
631     Log(source);
632 }
633 // }}}
634
635 static WebCore::String *string;
636
637 // iOS 2.x
638 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, void *_this, const WebCore::String &string, State state, const WebCore::String &url, int line) {
639     Cycriptify(string);
640     return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i(_this, string, state, url, line);
641 }
642
643 // iOS 3.x
644 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
645     /*if (cycript_) {
646         JSC::SourceCode *source(_this[iOS32 ? 6 : 0]);
647         const uint16_t *data(source->data());
648         size_t size(source->length());
649
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);
654             free((void *) data);
655         }
656     }*/
657
658     return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
659 }
660
661 // iOS 3.x cdata
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);
665     Log(code);
666     Cycriptify(code);
667     return code;
668 }
669
670 // iOS 4.x cdata
671 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) {
672     Cycriptify(source);
673     return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line);
674 }
675
676 // iOS 4.x+5.0 @src=
677 MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) {
678     const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this));
679     string = const_cast<WebCore::String *>(&script);
680     Log(script);
681     return script;
682 }
683
684 // iOS 4.x @src=
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_);
689         string = NULL;
690     }
691
692     return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, script, state);
693 }
694
695 // iOS 5.0 cdata
696 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
697     Cycriptify(source);
698     return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE(_this, source, url, position);
699 }
700
701 // iOS 5.0 @src=
702 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
703     string = NULL;
704     return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE(_this, position, legacy);
705 }
706
707 void (*$_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE)(void *_this, int legacy);
708
709 // iOS 5.0 @src=
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_);
715         string = NULL;
716     }
717
718     return __ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE(_this, script);
719 }
720
721 // iOS 6.0 cdata
722 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
723     Cycriptify(source);
724     return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE(_this, source, url, position);
725 }
726
727 // iOS 6.0 @src=
728 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
729     string = NULL;
730     return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE(_this, position, legacy);
731 }
732
733 /* Cydget:// Protocol {{{ */
734 @interface CydgetURLProtocol : NSURLProtocol {
735 }
736
737 @end
738
739 @implementation CydgetURLProtocol
740
741 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
742     NSURL *url([request URL]);
743     if (url == nil)
744         return NO;
745     NSString *scheme([[url scheme] lowercaseString]);
746     if (scheme == nil || ![scheme isEqualToString:@"cydget"])
747         return NO;
748     return YES;
749 }
750
751 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
752     return request;
753 }
754
755 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
756     id<NSURLProtocolClient> client([self client]);
757     if (icon == nil)
758         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
759     else {
760         NSData *data(UIImagePNGRepresentation(icon));
761
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];
766     }
767 }
768
769 - (void) startLoading {
770     id<NSURLProtocolClient> client([self client]);
771     NSURLRequest *request([self request]);
772
773     NSURL *url([request URL]);
774     NSString *href([url absoluteString]);
775
776     NSString *path([href substringFromIndex:9]);
777     NSRange slash([path rangeOfString:@"/"]);
778
779     NSString *command;
780     if (slash.location == NSNotFound) {
781         command = path;
782         path = nil;
783     } else {
784         command = [path substringToIndex:slash.location];
785         path = [path substringFromIndex:(slash.location + 1)];
786     }
787
788     if ([command isEqualToString:@"_UIImageWithName"]) {
789         if (path == nil)
790             goto fail;
791         path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
792         UIImage *icon(_UIImageWithName(path));
793         [self _returnPNGWithImage:icon forRequest:request];
794     } else fail: {
795         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
796     }
797 }
798
799 - (void) stopLoading {
800 }
801
802 @end
803 /* }}} */
804 /* Cydget-CGI:// Protocol {{{ */
805 @interface CydgetCGIURLProtocol : NSURLProtocol {
806     pid_t pid_;
807     CFHTTPMessageRef http_;
808     NSFileHandle *handle_;
809 }
810
811 @end
812
813 @implementation CydgetCGIURLProtocol
814
815 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
816     NSURL *url([request URL]);
817     if (url == nil)
818         return NO;
819     NSString *scheme([[url scheme] lowercaseString]);
820     if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"])
821         return NO;
822     return YES;
823 }
824
825 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
826     return request;
827 }
828
829 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
830     if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
831         pid_ = -1;
832     } return self;
833 }
834
835 - (void) startLoading {
836     id<NSURLProtocolClient> client([self client]);
837     NSURLRequest *request([self request]);
838     NSURL *url([request URL]);
839
840     NSString *path([url path]);
841     if (path == nil) {
842         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
843         return;
844     }
845
846     NSFileManager *manager([NSFileManager defaultManager]);
847     if (![manager fileExistsAtPath:path]) {
848         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
849         return;
850     }
851
852     int fds[2];
853     _assert(pipe(fds) != -1);
854
855     _assert(pid_ == -1);
856     pid_ = fork();
857     if (pid_ == -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]];
861         return;
862     }
863
864     if (pid_ == 0) {
865         const char *script([path UTF8String]);
866
867         setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
868         setenv("SCRIPT_FILENAME", script, true);
869         NSString *query([url query]);
870         if (query != nil)
871             setenv("QUERY_STRING", [query UTF8String], true);
872
873         _assert(dup2(fds[1], 1) != -1);
874         _assert(close(fds[0]) != -1);
875         _assert(close(fds[1]) != -1);
876
877         execl(script, script, NULL);
878         exit(1);
879         _assert(false);
880     }
881
882     _assert(close(fds[1]) != -1);
883
884     _assert(http_ == NULL);
885     http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
886     CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
887
888     _assert(handle_ == nil);
889     handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES];
890
891     [[NSNotificationCenter defaultCenter]
892         addObserver:self
893         selector:@selector(onRead:)
894         name:@"NSFileHandleReadCompletionNotification"
895         object:handle_
896     ];
897
898     [handle_ readInBackgroundAndNotify];
899 }
900
901 - (void) onRead:(NSNotification *)notification {
902     NSFileHandle *handle([notification object]);
903
904     NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
905
906     if (size_t length = [data length]) {
907         CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
908         [handle readInBackgroundAndNotify];
909     } else {
910         id<NSURLProtocolClient> client([self client]);
911
912         CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
913         if (mime == NULL)
914             [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
915         else {
916             NSURLRequest *request([self request]);
917
918             NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
919             CFRelease(mime);
920
921             [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
922
923             CFDataRef body(CFHTTPMessageCopyBody(http_));
924             [client URLProtocol:self didLoadData:(NSData *)body];
925             CFRelease(body);
926
927             [client URLProtocolDidFinishLoading:self];
928         }
929
930         CFRelease(http_);
931         http_ = NULL;
932     }
933 }
934
935     //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
936
937 - (void) stopLoading_ {
938     [[NSNotificationCenter defaultCenter] removeObserver:self];
939
940     if (handle_ != nil) {
941         [handle_ release];
942         handle_ = nil;
943     }
944
945     if (pid_ != -1) {
946         kill(pid_, SIGTERM);
947         int status;
948         _syscall(waitpid(pid_, &status, 0));
949         pid_ = -1;
950     }
951 }
952
953 - (void) stopLoading {
954     [self
955         performSelectorOnMainThread:@selector(stopLoading_)
956         withObject:nil
957         waitUntilDone:NO
958     ];
959 }
960
961 @end
962 /* }}} */
963
964 template <typename Type_>
965 static void dlset(Type_ &function, const char *name) {
966     function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
967 }
968
969 template <typename Type_>
970 static void msset_(Type_ &function, const char *name, MSImageRef handle) {
971     function = reinterpret_cast<Type_>(MSFindSymbol(handle, name));
972 }
973
974 #define msset(function, handle) \
975     msset_(function, "_" #function, handle)
976
977 @implementation WebCycriptLockScreenController
978
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];
983 }
984
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];
989 }
990
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");
995     }
996
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;
1001
1002     iOS4 = kCFCoreFoundationVersionNumber >= 550.32;
1003     iOS32 = !iOS4 && kCFCoreFoundationVersionNumber >= 478.61;
1004
1005     int maxproc;
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) {
1010         maxproc = 72;
1011         if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
1012             NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
1013     }
1014
1015     [NSURLProtocol registerClass:[CydgetURLProtocol class]];
1016     [WebView registerURLSchemeAsLocal:@"cydget"];
1017
1018     [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]];
1019     [WebView registerURLSchemeAsLocal:@"cydget-cgi"];
1020
1021     MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"));
1022     MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
1023
1024     if (!iOS4) {
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));
1029     }
1030
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));
1038
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));
1044
1045     if (!iOS4) {
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));
1051     }
1052
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));
1058
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));
1064
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));
1070
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));
1075
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));
1080
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));
1085
1086     void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE)(void *, void *, int);
1087     msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, WebCore);
1088     if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE != NULL)
1089         MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE));
1090
1091     MSHookSymbol($_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE, "__ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE", WebCore);
1092
1093     void (*_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE)(void *, JSC::ScriptSourceCode &);
1094     msset(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, WebCore);
1095     if (_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE != NULL)
1096         MSHookFunction(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, MSHake(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE));
1097
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);
1104
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);
1111
1112     msset(_ZNK7WebCore6String10charactersEv, WebCore);
1113
1114     msset(_ZN7WebCore6String29charactersWithNullTerminationEv, JavaScriptCore);
1115     if (_ZN7WebCore6String29charactersWithNullTerminationEv == NULL)
1116         MSHookSymbol(_ZN7WebCore6String29charactersWithNullTerminationEv, "__ZN3WTF6String29charactersWithNullTerminationEv", JavaScriptCore);
1117
1118     msset(_ZNK7WebCore6String6lengthEv, WebCore);
1119 }
1120
1121 + (id) rootViewController {
1122     return [[[self alloc] init] autorelease];
1123 }
1124
1125 - (void) loadView {
1126     [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];
1127 }
1128
1129 - (BOOL) showAwayItems {
1130     return YES;
1131 }
1132
1133 - (BOOL) viewWantsFullscreenLayout {
1134     return kCFCoreFoundationVersionNumber >= 800;
1135 }
1136
1137 - (BOOL) shouldDisableOnUnlock {
1138     return YES;
1139 }
1140
1141 - (BOOL) canBeAlwaysFullscreen {
1142     return YES;
1143 }
1144
1145 @end
1146
1147 MSClassHook(WebView)
1148 MSMetaClassHook(WebView)
1149
1150 MSClassMessageHook0(void, WebView, enableWebThread) {
1151     if (kCFCoreFoundationVersionNumber >= 478.61)
1152         return MSOldCall();
1153
1154     NSLog(@"-[WebView enableWebThread]");
1155 }