Despite using SleepToLock, I hate SleepToLock :(.
[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 @interface SBLockScreenView : UIView
131 - (BOOL) mediaControlsHidden;
132 @end
133
134 @interface SBLockScreenNotificationListController : NSObject
135 - (BOOL) hasAnyContent;
136 @end
137
138 @interface SBLockScreenViewController : UIViewController
139 - (SBLockScreenView *) lockScreenView;
140 - (BOOL) isShowingMediaControls;
141 - (void) _setMediaControlsVisible:(BOOL)visible;
142 - (SBLockScreenNotificationListController *) _notificationController;
143 @end
144
145 @interface SBLockScreenManager : NSObject
146 + (SBLockScreenManager *) sharedInstance;
147 - (SBLockScreenViewController *) lockScreenViewController;
148 @end
149
150 @protocol CydgetController
151 - (NSDictionary *) currentConfiguration;
152 - (NSString *) currentPath;
153 @end
154
155 static Class $CydgetController(objc_getClass("CydgetController"));
156
157 static bool iOS32, iOS4;
158
159 @interface NSString (UIKit)
160 - (NSString *) stringByAddingPercentEscapes;
161 @end
162
163 @implementation UIWebDocumentView (WebCycript)
164
165 - (void) _setScrollerOffset:(CGPoint)offset {
166     UIScroller *scroller([self _scroller]);
167
168     CGSize size([scroller contentSize]);
169     CGSize bounds([scroller bounds].size);
170
171     CGPoint max;
172     max.x = size.width - bounds.width;
173     max.y = size.height - bounds.height;
174
175     // wtf Apple?!
176     if (max.x < 0)
177         max.x = 0;
178     if (max.y < 0)
179         max.y = 0;
180
181     offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
182     offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
183
184     [scroller setOffset:offset];
185 }
186
187 @end
188
189 #ifdef USE_ICU_REGEX
190 /* ICU Regular Expression {{{ */
191 class RegEx {
192   private:
193     URegularExpression *regex_;
194
195   public:
196     RegEx(const char *regex) {
197         UParseError error;
198         UErrorCode status(U_ZERO_ERROR);
199         regex_ = uregex_openC(regex, 0, &error, &status);
200         if (U_FAILURE(status))
201             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** RegEx(): [%u] %s", error.offset, u_errorName(status)] userInfo:nil];
202     }
203
204     ~RegEx() {
205         uregex_close(regex_);
206     }
207
208     bool operator ()(NSString *string) {
209         return operator ()(reinterpret_cast<const uint16_t *>([string cStringUsingEncoding:NSUTF16StringEncoding]), [string length]);
210     }
211
212     bool operator ()(const UChar *data, size_t size) {
213         UErrorCode status(U_ZERO_ERROR);
214         uregex_setText(regex_, data, size, &status);
215         _assert(U_SUCCESS(status));
216         bool matches(uregex_matches(regex_, 0, &status));
217         _assert(U_SUCCESS(status));
218         return matches;
219     }
220 };
221 /* }}} */
222 #else
223 /* Perl-Compatible RegEx {{{ */
224 class RegEx {
225   private:
226     pcre *code_;
227     pcre_extra *study_;
228     int capture_;
229     int *matches_;
230     const char *data_;
231
232   public:
233     RegEx(const char *regex) :
234         study_(NULL)
235     {
236         const char *error;
237         int offset;
238         code_ = pcre_compile(regex, 0, &error, &offset, NULL);
239
240         if (code_ == NULL)
241             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** RegEx(): [%u] %s", offset, error] userInfo:nil];
242
243         pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
244         matches_ = new int[(capture_ + 1) * 3];
245     }
246
247     ~RegEx() {
248         pcre_free(code_);
249         delete matches_;
250     }
251
252     bool operator ()(NSString *data) {
253         // XXX: length is for characters, not for bytes
254         return operator ()([data UTF8String], [data length]);
255     }
256
257     bool operator ()(const char *data, size_t size) {
258         data_ = data;
259         return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
260     }
261 };
262 /* }}} */
263 #endif
264
265 @interface DOMCSSStyleSheet : NSObject
266 - (int) addRule:(NSString *)rule style:(NSString *)style index:(unsigned)index;
267 - (void) deleteRule:(unsigned)index;
268 @end
269
270 @interface DOMStyleSheetList : NSObject
271 - (DOMCSSStyleSheet *) item:(unsigned)index;
272 @end
273
274 @interface DOMDocument : NSObject
275 - (DOMStyleSheetList *) styleSheets;
276 @end
277
278 static float CYScrollViewDecelerationRateNormal;
279
280 @interface NSURL (Apple)
281 - (BOOL) isSpringboardHandledURL;
282 @end
283
284 @interface UIScrollView (Apple)
285 - (void) setDecelerationRate:(CGFloat)value;
286 - (void) setScrollingEnabled:(BOOL)enabled;
287 - (void) setShowBackgroundShadow:(BOOL)show;
288 @end
289
290 @interface UIWebView (Apple)
291 - (UIWebDocumentView *) _documentView;
292 - (void) setDataDetectorTypes:(NSInteger)types;
293 - (void) _setDrawInWebThread:(BOOL)draw;
294 - (UIScrollView *) _scrollView;
295 - (UIScroller *) _scroller;
296 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message;
297 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
298 @end
299
300 @interface WebView (Apple)
301 - (void) _setLayoutInterval:(float)interval;
302 - (void) _setAllowsMessaging:(BOOL)allows;
303 - (void) setShouldUpdateWhileOffscreen:(BOOL)update;
304 @end
305
306 @protocol CydgetWebViewDelegate <UIWebViewDelegate>
307 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame;
308 @end
309
310 @class UIWebViewWebViewDelegate;
311
312 @interface CydgetWebView : UIWebView {
313 }
314
315 - (void) updateStyles;
316
317 @end
318
319 MSClassHook(UIApplication)
320 MSClassHook(SBLockScreenManager)
321
322 MSInstanceMessageHook1(void, UIApplication, openURL, NSURL *, url) {
323     [self applicationOpenURL:url];
324 }
325
326 @implementation NSURL (Cydget)
327
328 - (NSNumber *) cydget$isSpringboardHandledURL {
329     return [NSNumber numberWithBool:[self isSpringboardHandledURL]];
330 }
331
332 @end
333
334 MSClassHook(NSURL)
335
336 MSInstanceMessageHook0(BOOL, NSURL, isSpringboardHandledURL) {
337     if (![NSThread isMainThread])
338         return MSOldCall();
339
340     return [[self cydget$yieldToSelector:@selector(cydget$isSpringboardHandledURL)] boolValue];
341 }
342
343 @implementation CydgetWebView
344
345 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
346     NSObject<CydgetWebViewDelegate> *delegate((NSObject<CydgetWebViewDelegate> *) [self delegate]);
347     if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
348         [delegate webView:view didClearWindowObject:window forFrame:frame];
349     if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
350         [super webView:view didClearWindowObject:window forFrame:frame];
351 }
352
353 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
354     NSLog(@"addMessageToConsole:%@", message);
355
356     if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
357         [super webView:view addMessageToConsole:message];
358 }
359
360 - (void) updateStyles {
361     DOMCSSStyleSheet *sheet([[[[[[self _documentView] webView] mainFrame] DOMDocument] styleSheets] item:0]);
362     [sheet addRule:@"cydget" style:@"color: black" index:0];
363     [sheet deleteRule:0];
364 }
365
366 @end
367
368 @interface WebCydgetLockScreenView : UIView <UIWebViewDelegate> {
369     CydgetWebView *webview_;
370     UIScrollView *scroller_;
371     NSString *cycript_;
372 }
373
374 - (void) updateStyles;
375
376 @end
377
378 @implementation WebCydgetLockScreenView
379
380 //#include "UICaboodle/UCInternal.h"
381
382 - (void) dealloc {
383     [[NSNotificationCenter defaultCenter] removeObserver:self];
384     [webview_ setDelegate:nil];
385     [webview_ release];
386     [super dealloc];
387 }
388
389 - (void) loadRequest:(NSURLRequest *)request {
390     [webview_ loadRequest:request];
391 }
392
393 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
394     [self loadRequest:[NSURLRequest
395         requestWithURL:url
396         cachePolicy:policy
397         timeoutInterval:30.0
398     ]];
399 }
400
401 - (void) loadURL:(NSURL *)url {
402     [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
403 }
404
405 - (id) initWithURL:(NSURL *)url {
406     CGRect frame = [[UIScreen mainScreen] bounds];
407     if (kCFCoreFoundationVersionNumber < 800)
408         frame.size.height -= 20; //[[[$SBStatusBarController sharedStatusBarController] statusBarView] frame].size.height;
409
410     if ((self = [super initWithFrame:frame]) != nil) {
411         CGRect bounds([self bounds]);
412         if (kCFCoreFoundationVersionNumber < 800)
413             bounds.size.height -= [TPBottomLockBar defaultHeight];
414
415         webview_ = [[CydgetWebView alloc] initWithFrame:bounds];
416         [webview_ setDelegate:self];
417         [self addSubview:webview_];
418
419         if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
420             [webview_ setDataDetectorTypes:0x80000000];
421         else
422             [webview_ setDetectsPhoneNumbers:NO];
423
424         [webview_ setScalesPageToFit:YES];
425
426         if (kCFCoreFoundationVersionNumber < 478.61)
427             if ([webview_ respondsToSelector:@selector(_setDrawInWebThread:)])
428                 [webview_ _setDrawInWebThread:NO];
429
430         UIWebDocumentView *document([webview_ _documentView]);
431         WebView *webview([document webView]);
432         WebPreferences *preferences([webview preferences]);
433
434         [document setTileSize:CGSizeMake(bounds.size.width, 500)];
435
436         [document setBackgroundColor:[UIColor clearColor]];
437         [document setDrawsBackground:NO];
438
439         [webview setPreferencesIdentifier:@"WebCycript"];
440
441         if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
442             [webview _setLayoutInterval:0];
443         else
444             [preferences _setLayoutInterval:0];
445
446         [preferences setCacheModel:WebCacheModelDocumentViewer];
447         [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
448         [preferences setOfflineWebApplicationCacheEnabled:YES];
449
450         if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
451             [webview setShouldUpdateWhileOffscreen:NO];
452
453         if ([document respondsToSelector:@selector(setAllowsMessaging:)])
454             [document setAllowsMessaging:YES];
455         if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
456             [webview _setAllowsMessaging:YES];
457
458         if ([webview_ respondsToSelector:@selector(_scrollView)]) {
459             scroller_ = [webview_ _scrollView];
460
461             [scroller_ setDirectionalLockEnabled:YES];
462             [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
463             [scroller_ setDelaysContentTouches:NO];
464
465             [scroller_ setCanCancelContentTouches:YES];
466
467             [scroller_ setAlwaysBounceVertical:NO];
468         } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
469             UIScroller *scroller([webview_ _scroller]);
470             scroller_ = (UIScrollView *) scroller;
471
472             [scroller setDirectionalScrolling:YES];
473             [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
474             [scroller setScrollHysteresis:0]; /* 8 */
475
476             [scroller setThumbDetectionEnabled:NO];
477         }
478
479         [webview_ setOpaque:NO];
480         [webview_ setBackgroundColor:[UIColor clearColor]];
481
482         [scroller_ setFixedBackgroundPattern:YES];
483         [scroller_ setBackgroundColor:[UIColor clearColor]];
484         [scroller_ setClipsSubviews:NO];
485
486         [scroller_ setBounces:YES];
487         [scroller_ setShowBackgroundShadow:NO]; /* YES */
488
489         [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
490         [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
491
492         NSDictionary *configuration([$CydgetController currentConfiguration]);
493         cycript_ = [configuration objectForKey:@"CycriptURLs"];
494
495         [scroller_ setScrollingEnabled:[[configuration objectForKey:@"Scrollable"] boolValue]];
496
497         [self loadURL:url];
498
499         [[NSNotificationCenter defaultCenter]
500             addObserver:self
501             selector:@selector(mediaControlsDidSomething:)
502             name:@"SBLockScreenViewControllerMediaControlsDidShow"
503             object:nil
504         ];
505
506         [[NSNotificationCenter defaultCenter]
507             addObserver:self
508             selector:@selector(mediaControlsDidSomething:)
509             name:@"SBLockScreenViewControllerMediaControlsDidHide"
510             object:nil
511         ];
512     } return self;
513 }
514
515 - (void) mediaControlsDidSomething:(NSNotification *)notification {
516     [self updateStyles];
517 }
518
519 - (void) webView:(WebView *)webview didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
520     if (cycript_ != nil)
521         if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
522             if (RegEx([cycript_ UTF8String])(href))
523                 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
524                     if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
525                         WebFrame *frame([webview mainFrame]);
526                         JSGlobalContextRef context([frame globalContext]);
527                         @try {
528                             CYSetupContext(context);
529                         } @catch (NSException *e) {
530                             NSLog(@"*** CydgetSetupContext => %@", e);
531                         }
532                     }
533 }
534
535 - (void) updateStyles {
536     [webview_ updateStyles];
537 }
538
539 @end
540
541 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
542     NSDictionary *configuration_;
543     bool legacy_;
544     bool media_;
545     bool items_;
546     WebCydgetLockScreenView *background_;
547     WebCydgetLockScreenView *foreground_;
548 }
549
550 @end
551
552 #include <string>
553
554 struct State {
555     unsigned state;
556 };
557
558 namespace JSC {
559     class JSGlobalData;
560     class UString;
561 }
562
563 namespace WebCore {
564     class KURL;
565 }
566
567 namespace WebCore {
568 struct String {
569     void *impl_;
570 }; }
571
572 namespace JSC {
573 struct SourceCode {
574     void *provider_;
575     int start_;
576     int end_;
577     int line_;
578 }; }
579
580 namespace JSC {
581 union ScriptSourceCode {
582     struct {
583         JSC::SourceCode source_;
584     } Old;
585     struct {
586         void *provider_;
587         JSC::SourceCode source_;
588     } New;
589 }; }
590
591 // String Helpers {{{
592 static const UChar *(*_ZNK7WebCore6String10charactersEv)(const WebCore::String *);
593 static const UChar *(*_ZN7WebCore6String29charactersWithNullTerminationEv)(const WebCore::String *);
594 static unsigned (*_ZNK7WebCore6String6lengthEv)(const WebCore::String *);
595
596 static bool StringGet(const WebCore::String &string, const UChar *&data, size_t &length) {
597     bool terminated;
598
599     if (_ZNK7WebCore6String10charactersEv != NULL) {
600         data = (*_ZNK7WebCore6String10charactersEv)(&string);
601         terminated = false;
602     } else if (_ZN7WebCore6String29charactersWithNullTerminationEv != NULL) {
603         data = (*_ZN7WebCore6String29charactersWithNullTerminationEv)(&string);
604         terminated = true;
605     } else return false;
606
607     if (data == NULL)
608         return false;
609
610     if (_ZNK7WebCore6String6lengthEv != NULL)
611         length = (*_ZNK7WebCore6String6lengthEv)(&string);
612     else if (terminated)
613         for (length = 0; data[length] != 0; ++length);
614     else return false;
615
616     return true;
617 }
618
619 static bool StringEquals(const WebCore::String &string, const char *value) {
620     const UChar *data;
621     size_t size;
622     if (!StringGet(string, data, size))
623         return false;
624
625     size_t length(strlen(value));
626     if (size != length)
627         return false;
628
629     for (size_t index(0); index != length; ++index)
630         if (data[index] != value[index])
631             return false;
632
633     return true;
634 }
635 // }}}
636 // State Machine {{{
637 static bool cycript_;
638
639 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
640     if (!StringEquals(mime, "text/cycript")) {
641         cycript_ = false;
642         return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
643     }
644
645     static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
646     if (handle == NULL)
647         return false;
648
649     cycript_ = true;
650     return true;
651 }
652 // }}}
653 // Script Compiler {{{
654 static void Log(const WebCore::String &string) {
655 #if 0
656     const UChar *data;
657     size_t length;
658     if (!StringGet(string, data, length))
659         return;
660
661     UChar terminated[length + 1];
662     terminated[length] = 0;
663     memcpy(terminated, data, length * 2);
664     NSLog(@"wtf %p:%zu:%S:", &string, length, terminated);
665 #endif
666 }
667
668 static bool Cycriptify(const uint16_t *&data, size_t &size) {
669     cycript_ = false;
670
671     if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
672         if (void (*CydgetMemoryParse)(const uint16_t **, size_t *) = reinterpret_cast<void (*)(const uint16_t **, size_t *)>(dlsym(handle, "CydgetMemoryParse"))) @try {
673             CydgetMemoryParse(&data, &size);
674             return true;
675         } @catch (NSException *e) {
676             NSLog(@"*** CydgetMemoryParse => %@", e);
677         }
678     return false;
679 }
680
681 static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned);
682 static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned);
683
684 static void Cycriptify(const WebCore::String &source, int *psize = NULL) {
685     if (!cycript_)
686         return;
687     cycript_ = false;
688
689     const UChar *data;
690     size_t length;
691     if (!StringGet(source, data, length))
692         return;
693
694     size_t size(length);
695     if (!Cycriptify(data, size))
696         return;
697
698     WebCore::String &script(const_cast<WebCore::String &>(source));
699     _ZN7WebCore6String8truncateEj(&script, 0);
700     _ZN7WebCore6String6appendEPKtj(&script, data, size);
701
702     if (psize != NULL)
703         *psize = size;
704
705     free((void *) data);
706
707     Log(source);
708 }
709 // }}}
710
711 static WebCore::String *string;
712
713 // iOS 2.x
714 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, void *_this, const WebCore::String &string, State state, const WebCore::String &url, int line) {
715     Cycriptify(string);
716     return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i(_this, string, state, url, line);
717 }
718
719 // iOS 3.x
720 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
721     /*if (cycript_) {
722         JSC::SourceCode *source(_this[iOS32 ? 6 : 0]);
723         const uint16_t *data(source->data());
724         size_t size(source->length());
725
726         if (Cycriptify(data, size)) {
727             source->~SourceCode();
728             // XXX: I actually don't have the original URL here: pants
729             new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
730             free((void *) data);
731         }
732     }*/
733
734     return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
735 }
736
737 // iOS 3.x cdata
738 MSHook(const WebCore::String &, _ZNK7WebCore4Node11textContentEb, void *_this, bool convert) {
739     const WebCore::String &code(__ZNK7WebCore4Node11textContentEb(_this, convert));
740     string = const_cast<WebCore::String *>(&code);
741     Log(code);
742     Cycriptify(code);
743     return code;
744 }
745
746 // iOS 4.x cdata
747 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) {
748     Cycriptify(source);
749     return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line);
750 }
751
752 // iOS 4.x+5.0 @src=
753 MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) {
754     const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this));
755     string = const_cast<WebCore::String *>(&script);
756     Log(script);
757     return script;
758 }
759
760 // iOS 4.x @src=
761 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, void *_this, JSC::ScriptSourceCode &script, State state) {
762     if (string != NULL) {
763         JSC::SourceCode *source(iOS4 ? &script.New.source_ : &script.Old.source_);
764         Cycriptify(*string, &source->end_);
765         string = NULL;
766     }
767
768     return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, script, state);
769 }
770
771 // iOS 5.0 cdata
772 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
773     Cycriptify(source);
774     return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE(_this, source, url, position);
775 }
776
777 // iOS 5.0 @src=
778 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
779     string = NULL;
780     return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE(_this, position, legacy);
781 }
782
783 void (*$_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE)(void *_this, int legacy);
784
785 // iOS 5.0 @src=
786 MSHook(void, _ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, void *_this, JSC::ScriptSourceCode &script) {
787     if (string != NULL) {
788         JSC::SourceCode *source(&script.New.source_);
789         $_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE(_this, 0);
790         Cycriptify(*string, &source->end_);
791         string = NULL;
792     }
793
794     return __ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE(_this, script);
795 }
796
797 // iOS 6.0 cdata
798 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
799     Cycriptify(source);
800     return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE(_this, source, url, position);
801 }
802
803 // iOS 6.0 @src=
804 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
805     string = NULL;
806     return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE(_this, position, legacy);
807 }
808
809 /* Cydget:// Protocol {{{ */
810 @interface CydgetURLProtocol : NSURLProtocol {
811 }
812
813 @end
814
815 @implementation CydgetURLProtocol
816
817 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
818     NSURL *url([request URL]);
819     if (url == nil)
820         return NO;
821     NSString *scheme([[url scheme] lowercaseString]);
822     if (scheme == nil || ![scheme isEqualToString:@"cydget"])
823         return NO;
824     return YES;
825 }
826
827 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
828     return request;
829 }
830
831 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
832     id<NSURLProtocolClient> client([self client]);
833     if (icon == nil)
834         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
835     else {
836         NSData *data(UIImagePNGRepresentation(icon));
837
838         NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
839         [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
840         [client URLProtocol:self didLoadData:data];
841         [client URLProtocolDidFinishLoading:self];
842     }
843 }
844
845 - (void) startLoading {
846     id<NSURLProtocolClient> client([self client]);
847     NSURLRequest *request([self request]);
848
849     NSURL *url([request URL]);
850     NSString *href([url absoluteString]);
851
852     NSString *path([href substringFromIndex:9]);
853     NSRange slash([path rangeOfString:@"/"]);
854
855     NSString *command;
856     if (slash.location == NSNotFound) {
857         command = path;
858         path = nil;
859     } else {
860         command = [path substringToIndex:slash.location];
861         path = [path substringFromIndex:(slash.location + 1)];
862     }
863
864     if ([command isEqualToString:@"_UIImageWithName"]) {
865         if (path == nil)
866             goto fail;
867         path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
868         UIImage *icon(_UIImageWithName(path));
869         [self _returnPNGWithImage:icon forRequest:request];
870     } else fail: {
871         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
872     }
873 }
874
875 - (void) stopLoading {
876 }
877
878 @end
879 /* }}} */
880 /* Cydget-CGI:// Protocol {{{ */
881 @interface CydgetCGIURLProtocol : NSURLProtocol {
882     pid_t pid_;
883     CFHTTPMessageRef http_;
884     NSFileHandle *handle_;
885 }
886
887 @end
888
889 @implementation CydgetCGIURLProtocol
890
891 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
892     NSURL *url([request URL]);
893     if (url == nil)
894         return NO;
895     NSString *scheme([[url scheme] lowercaseString]);
896     if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"])
897         return NO;
898     return YES;
899 }
900
901 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
902     return request;
903 }
904
905 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
906     if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
907         pid_ = -1;
908     } return self;
909 }
910
911 - (void) startLoading {
912     id<NSURLProtocolClient> client([self client]);
913     NSURLRequest *request([self request]);
914     NSURL *url([request URL]);
915
916     NSString *path([url path]);
917     if (path == nil) {
918         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
919         return;
920     }
921
922     NSFileManager *manager([NSFileManager defaultManager]);
923     if (![manager fileExistsAtPath:path]) {
924         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
925         return;
926     }
927
928     int fds[2];
929     _assert(pipe(fds) != -1);
930
931     _assert(pid_ == -1);
932     pid_ = fork();
933     if (pid_ == -1) {
934         _assert(close(fds[0]) != -1);
935         _assert(close(fds[1]) != -1);
936         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
937         return;
938     }
939
940     if (pid_ == 0) {
941         const char *script([path UTF8String]);
942
943         setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
944         setenv("SCRIPT_FILENAME", script, true);
945         NSString *query([url query]);
946         if (query != nil)
947             setenv("QUERY_STRING", [query UTF8String], true);
948
949         _assert(dup2(fds[1], 1) != -1);
950         _assert(close(fds[0]) != -1);
951         _assert(close(fds[1]) != -1);
952
953         execl(script, script, NULL);
954         exit(1);
955         _assert(false);
956     }
957
958     _assert(close(fds[1]) != -1);
959
960     _assert(http_ == NULL);
961     http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
962     CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
963
964     _assert(handle_ == nil);
965     handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES];
966
967     [[NSNotificationCenter defaultCenter]
968         addObserver:self
969         selector:@selector(onRead:)
970         name:@"NSFileHandleReadCompletionNotification"
971         object:handle_
972     ];
973
974     [handle_ readInBackgroundAndNotify];
975 }
976
977 - (void) onRead:(NSNotification *)notification {
978     NSFileHandle *handle([notification object]);
979
980     NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
981
982     if (size_t length = [data length]) {
983         CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
984         [handle readInBackgroundAndNotify];
985     } else {
986         id<NSURLProtocolClient> client([self client]);
987
988         CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
989         if (mime == NULL)
990             [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
991         else {
992             NSURLRequest *request([self request]);
993
994             NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
995             CFRelease(mime);
996
997             [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
998
999             CFDataRef body(CFHTTPMessageCopyBody(http_));
1000             [client URLProtocol:self didLoadData:(NSData *)body];
1001             CFRelease(body);
1002
1003             [client URLProtocolDidFinishLoading:self];
1004         }
1005
1006         CFRelease(http_);
1007         http_ = NULL;
1008     }
1009 }
1010
1011     //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
1012
1013 - (void) stopLoading_ {
1014     [[NSNotificationCenter defaultCenter] removeObserver:self];
1015
1016     if (handle_ != nil) {
1017         [handle_ release];
1018         handle_ = nil;
1019     }
1020
1021     if (pid_ != -1) {
1022         kill(pid_, SIGTERM);
1023         int status;
1024         _syscall(waitpid(pid_, &status, 0));
1025         pid_ = -1;
1026     }
1027 }
1028
1029 - (void) stopLoading {
1030     [self
1031         performSelectorOnMainThread:@selector(stopLoading_)
1032         withObject:nil
1033         waitUntilDone:NO
1034     ];
1035 }
1036
1037 @end
1038 /* }}} */
1039
1040 namespace WebCore {
1041     class MediaQueryEvaluator;
1042     class CSSParserValueList;
1043 }
1044
1045 namespace WebCore {
1046 struct MediaQueryExp {
1047     String feature_;
1048     void *value_;
1049     bool valid_;
1050     String cache_;
1051 }; }
1052
1053 bool CYHaveMediaControls() {
1054     SBLockScreenView *view([[[$SBLockScreenManager sharedInstance] lockScreenViewController] lockScreenView]);
1055     return view != nil && ![view mediaControlsHidden];
1056     //return [[[$SBLockScreenManager sharedInstance] lockScreenViewController] isShowingMediaControls];
1057 }
1058
1059 bool CYHaveNotificationList() {
1060     SBLockScreenNotificationListController *controller([[[$SBLockScreenManager sharedInstance] lockScreenViewController] _notificationController]);
1061     return controller != nil && [controller hasAnyContent];
1062 }
1063
1064 MSHook(bool, _ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, WebCore::MediaQueryEvaluator *_this, WebCore::String &query) {
1065     Log(query); if (false);
1066     else if (kCFCoreFoundationVersionNumber >= 800 && StringEquals(query, "-cydget-media-controls"))
1067         return CYHaveMediaControls();
1068     else if (kCFCoreFoundationVersionNumber >= 800 && StringEquals(query, "-cydget-notification-list"))
1069         return CYHaveNotificationList();
1070     else
1071         return __ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE(_this, query);
1072 }
1073
1074 MSHook(void, _ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, WebCore::MediaQueryExp *_this, WebCore::String &query, WebCore::CSSParserValueList *values) {
1075     Log(query);
1076     __ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE(_this, query, values);
1077     if (!_this->valid_) if (
1078         StringEquals(query, "-cydget-media-controls") ||
1079         StringEquals(query, "-cydget-notification-list") ||
1080     false) _this->valid_ = true;
1081 }
1082
1083 template <typename Type_>
1084 static void dlset(Type_ &function, const char *name) {
1085     function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
1086 }
1087
1088 template <typename Type_>
1089 static void msset_(Type_ &function, const char *name, MSImageRef handle) {
1090     function = reinterpret_cast<Type_>(MSFindSymbol(handle, name));
1091 }
1092
1093 #define msset(function, handle) \
1094     msset_(function, "_" #function, handle)
1095
1096 @implementation WebCycriptLockScreenController
1097
1098 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
1099     UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
1100     if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
1101         [uiWebView webView:view addMessageToConsole:message];
1102 }
1103
1104 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
1105     UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
1106     if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
1107         [uiWebView webView:view didClearWindowObject:window forFrame:frame];
1108 }
1109
1110 + (void) initialize {
1111     if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
1112         class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
1113         class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
1114     }
1115
1116     if (CGFloat *_UIScrollViewDecelerationRateNormal = reinterpret_cast<CGFloat *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
1117         CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
1118     else // XXX: this actually might be fast on some older systems: we should look into this
1119         CYScrollViewDecelerationRateNormal = 0.998;
1120
1121     iOS4 = kCFCoreFoundationVersionNumber >= 550.32;
1122     iOS32 = !iOS4 && kCFCoreFoundationVersionNumber >= 478.61;
1123
1124     int maxproc;
1125     size_t size(sizeof(maxproc));
1126     if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
1127         NSLog(@"sysctlbyname(\"kern.maxproc\", ?)");
1128     else if (maxproc < 72) {
1129         maxproc = 72;
1130         if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
1131             NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
1132     }
1133
1134     [NSURLProtocol registerClass:[CydgetURLProtocol class]];
1135     [WebView registerURLSchemeAsLocal:@"cydget"];
1136
1137     [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]];
1138     [WebView registerURLSchemeAsLocal:@"cydget-cgi"];
1139
1140     MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"));
1141     MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
1142
1143     if (!iOS4) {
1144         void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
1145         dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
1146         if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
1147             MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
1148     }
1149
1150     bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &) = NULL;
1151     if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
1152         MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE", WebCore);
1153     if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
1154         MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKN3WTF6StringE", WebCore);
1155     if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE != NULL)
1156         MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
1157
1158     void (*_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)(void *, const WebCore::String &, const WebCore::KURL &, int) = NULL;
1159     if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi == NULL)
1160         MSHookSymbol(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, "__ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi", WebCore);
1161     if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi != NULL)
1162         MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi));
1163
1164     if (!iOS4) {
1165         const WebCore::String &(*_ZNK7WebCore4Node11textContentEb)(void *, bool) = NULL;
1166         if (_ZNK7WebCore4Node11textContentEb == NULL)
1167             MSHookSymbol(_ZNK7WebCore4Node11textContentEb, "__ZNK7WebCore4Node11textContentEb", WebCore);
1168         if (_ZNK7WebCore4Node11textContentEb != NULL)
1169             MSHookFunction(_ZNK7WebCore4Node11textContentEb, MSHake(_ZNK7WebCore4Node11textContentEb));
1170     }
1171
1172     const WebCore::String &(*_ZN7WebCore12CachedScript6scriptEv)(void *) = NULL;
1173     if (_ZN7WebCore12CachedScript6scriptEv == NULL)
1174         MSHookSymbol(_ZN7WebCore12CachedScript6scriptEv, "__ZN7WebCore12CachedScript6scriptEv", WebCore);
1175     if (_ZN7WebCore12CachedScript6scriptEv != NULL)
1176         MSHookFunction(_ZN7WebCore12CachedScript6scriptEv, MSHake(_ZN7WebCore12CachedScript6scriptEv));
1177
1178     State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i)(void *, const WebCore::String &, State, const WebCore::String &, int) = NULL;
1179     if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i == NULL)
1180         MSHookSymbol(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i", WebCore);
1181     if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i != NULL)
1182         MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i));
1183
1184     State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)(void *, JSC::ScriptSourceCode &, State) = NULL;
1185     if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE == NULL)
1186         MSHookSymbol(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE", WebCore);
1187     if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE != NULL)
1188         MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE));
1189
1190     void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
1191     msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, WebCore);
1192     if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE != NULL)
1193         MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE));
1194
1195     void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
1196     msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, WebCore);
1197     if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE != NULL)
1198         MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE));
1199
1200     void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE)(void *, void *, int);
1201     msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, WebCore);
1202     if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE != NULL)
1203         MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE));
1204
1205     void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE)(void *, void *, int);
1206     msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, WebCore);
1207     if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE != NULL)
1208         MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE));
1209
1210     MSHookSymbol($_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE, "__ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE", WebCore);
1211
1212     void (*_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE)(void *, JSC::ScriptSourceCode &);
1213     msset(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, WebCore);
1214     if (_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE != NULL)
1215         MSHookFunction(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, MSHake(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE));
1216
1217     if (_ZN7WebCore6String6appendEPKtj == NULL)
1218         MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN7WebCore6String6appendEPKtj", WebCore);
1219     if (_ZN7WebCore6String6appendEPKtj == NULL)
1220         msset(_ZN7WebCore6String6appendEPKtj, JavaScriptCore);
1221     if (_ZN7WebCore6String6appendEPKtj == NULL)
1222         MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN3WTF6String6appendEPKtj", JavaScriptCore);
1223
1224     if (_ZN7WebCore6String8truncateEj == NULL)
1225         MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN7WebCore6String8truncateEj", WebCore);
1226     if (_ZN7WebCore6String8truncateEj == NULL)
1227         msset(_ZN7WebCore6String8truncateEj, JavaScriptCore);
1228     if (_ZN7WebCore6String8truncateEj == NULL)
1229         MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN3WTF6String8truncateEj", JavaScriptCore);
1230
1231     msset(_ZNK7WebCore6String10charactersEv, WebCore);
1232
1233     msset(_ZN7WebCore6String29charactersWithNullTerminationEv, JavaScriptCore);
1234     if (_ZN7WebCore6String29charactersWithNullTerminationEv == NULL)
1235         MSHookSymbol(_ZN7WebCore6String29charactersWithNullTerminationEv, "__ZN3WTF6String29charactersWithNullTerminationEv", JavaScriptCore);
1236
1237     msset(_ZNK7WebCore6String6lengthEv, WebCore);
1238
1239     bool (*_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE)(WebCore::MediaQueryEvaluator *, WebCore::String &);
1240     msset(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, WebCore);
1241     if (_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE != NULL)
1242         MSHookFunction(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, MSHake(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE));
1243
1244     void (*_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE)(WebCore::MediaQueryExp *, WebCore::String &, WebCore::CSSParserValueList *);
1245     msset(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, WebCore);
1246     if (_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE != NULL)
1247         MSHookFunction(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, MSHake(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE));
1248 }
1249
1250 + (id) rootViewController {
1251     return [[[self alloc] init] autorelease];
1252 }
1253
1254 - (void) dealloc {
1255     [configuration_ release];
1256     [background_ release];
1257     [foreground_ release];
1258     [super dealloc];
1259 }
1260
1261 - (id) init {
1262     if ((self = [super init]) != nil) {
1263         configuration_ = [[$CydgetController currentConfiguration] retain];
1264         legacy_ = [configuration_ objectForKey:@"Background"] == nil;
1265         media_ = [[configuration_ objectForKey:@"MediaControls"] boolValue];
1266         items_ = [[configuration_ objectForKey:@"NotificationList"] boolValue];
1267     } return self;
1268 }
1269
1270 - (void) loadView {
1271     NSURL *base([NSURL fileURLWithPath:[$CydgetController currentPath]]);
1272
1273     if (NSString *background = [configuration_ objectForKey:@"Background"])
1274         background_ = [[WebCydgetLockScreenView alloc] initWithURL:[NSURL URLWithString:background relativeToURL:base]];
1275
1276     if (NSString *homepage = [configuration_ objectForKey:@"Homepage"]) {
1277         foreground_ = [[WebCydgetLockScreenView alloc] initWithURL:[NSURL URLWithString:homepage relativeToURL:base]];
1278         [self setView:foreground_];
1279     }
1280 }
1281
1282 - (void) purgeView {
1283     [background_ removeFromSuperview];
1284     [background_ release];
1285     background_ = nil;
1286     [foreground_ removeFromSuperview];
1287     [foreground_ release];
1288     foreground_ = nil;
1289     [super purgeView];
1290 }
1291
1292 - (void) updateStyles {
1293     [foreground_ updateStyles];
1294     [background_ updateStyles];
1295 }
1296
1297 - (UIView *) backgroundView {
1298     return background_;
1299 }
1300
1301 - (BOOL) showAwayItems {
1302     return YES;
1303 }
1304
1305 - (BOOL) updateHidden {
1306     if (foreground_ == nil)
1307         return true;
1308     bool media(CYHaveMediaControls());
1309     [foreground_ setHidden:media];
1310     return media;
1311 }
1312
1313 - (BOOL) showDateView {
1314     if (kCFCoreFoundationVersionNumber < 800)
1315         return false;
1316     [self updateStyles];
1317     if (!legacy_ && foreground_ == nil)
1318         return true;
1319     if (!items_ && CYHaveNotificationList())
1320         return true;
1321     return false;
1322 }
1323
1324 - (BOOL) allowsLockScreenMediaControls {
1325     if (kCFCoreFoundationVersionNumber < 800)
1326         return true;
1327     return media_;
1328 }
1329
1330 /*- (BOOL) showHeaderView {
1331     return YES;
1332 }*/
1333
1334 // 0: view is rendered above head
1335 // 1: view moves as one with head
1336 // 2: view moves up and down only
1337 // 3: view simply never does move
1338 - (NSUInteger) presentationStyle {
1339     return 1;
1340 }
1341
1342 // 1: light blur
1343 // 2: heavy blur
1344 // 3: just black
1345 // 4: legibility
1346 // 5: no overlay
1347 - (NSUInteger) overlayStyle {
1348     return legacy_ ? 1 : 4;
1349 }
1350
1351 // 1: blur -> view -> list
1352 // 2: view -> blur -> list
1353 // 3: view. unblur below?!
1354 // 4: blur -> list -> view
1355 - (NSUInteger) notificationBehavior {
1356     return items_ ? 1 : 2;
1357 }
1358
1359 - (BOOL) viewWantsFullscreenLayout {
1360     return kCFCoreFoundationVersionNumber >= 800;
1361 }
1362
1363 /*- (BOOL) viewWantsOverlayLayout {
1364     return kCFCoreFoundationVersionNumber >= 800;
1365 }*/
1366
1367 - (BOOL) shouldDisableOnUnlock {
1368     return YES;
1369 }
1370
1371 - (BOOL) canBeAlwaysFullscreen {
1372     return YES;
1373 }
1374
1375 /*- (BOOL) wantsSwipeGestureRecognizer {
1376     return YES;
1377 }
1378
1379 - (BOOL) handleGesture:(int)arg1 fingerCount:(NSUInteger)fingers {
1380     return NO;
1381     return YES;
1382 }*/
1383
1384 // - (void) lockScreenMediaControlsShown:(BOOL)shown;
1385
1386 - (BOOL) handleMenuButtonDoubleTap {
1387     if (kCFCoreFoundationVersionNumber >= 800) {
1388         SBLockScreenViewController *controller([[$SBLockScreenManager sharedInstance] lockScreenViewController]);
1389         [controller _setMediaControlsVisible:![controller isShowingMediaControls]];
1390     }
1391
1392     return [super handleMenuButtonDoubleTap];
1393 }
1394
1395 @end
1396
1397 MSClassHook(WebView)
1398 MSMetaClassHook(WebView)
1399
1400 MSClassMessageHook0(void, WebView, enableWebThread) {
1401     if (kCFCoreFoundationVersionNumber >= 478.61)
1402         return MSOldCall();
1403
1404     NSLog(@"-[WebView enableWebThread]");
1405 }