Separate logic from @media evaluator for reuse.
[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     [webview_ setDelegate:nil];
384     [webview_ release];
385     [super dealloc];
386 }
387
388 - (void) loadRequest:(NSURLRequest *)request {
389     [webview_ loadRequest:request];
390 }
391
392 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
393     [self loadRequest:[NSURLRequest
394         requestWithURL:url
395         cachePolicy:policy
396         timeoutInterval:30.0
397     ]];
398 }
399
400 - (void) loadURL:(NSURL *)url {
401     [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
402 }
403
404 - (id) initWithURL:(NSURL *)url {
405     CGRect frame = [[UIScreen mainScreen] bounds];
406     if (kCFCoreFoundationVersionNumber < 800)
407         frame.size.height -= 20; //[[[$SBStatusBarController sharedStatusBarController] statusBarView] frame].size.height;
408
409     if ((self = [super initWithFrame:frame]) != nil) {
410         CGRect bounds([self bounds]);
411         if (kCFCoreFoundationVersionNumber < 800)
412             bounds.size.height -= [TPBottomLockBar defaultHeight];
413
414         webview_ = [[CydgetWebView alloc] initWithFrame:bounds];
415         [webview_ setDelegate:self];
416         [self addSubview:webview_];
417
418         if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
419             [webview_ setDataDetectorTypes:0x80000000];
420         else
421             [webview_ setDetectsPhoneNumbers:NO];
422
423         [webview_ setScalesPageToFit:YES];
424
425         if (kCFCoreFoundationVersionNumber < 478.61)
426             if ([webview_ respondsToSelector:@selector(_setDrawInWebThread:)])
427                 [webview_ _setDrawInWebThread:NO];
428
429         UIWebDocumentView *document([webview_ _documentView]);
430         WebView *webview([document webView]);
431         WebPreferences *preferences([webview preferences]);
432
433         [document setTileSize:CGSizeMake(bounds.size.width, 500)];
434
435         [document setBackgroundColor:[UIColor clearColor]];
436         [document setDrawsBackground:NO];
437
438         [webview setPreferencesIdentifier:@"WebCycript"];
439
440         if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
441             [webview _setLayoutInterval:0];
442         else
443             [preferences _setLayoutInterval:0];
444
445         [preferences setCacheModel:WebCacheModelDocumentViewer];
446         [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
447         [preferences setOfflineWebApplicationCacheEnabled:YES];
448
449         if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
450             [webview setShouldUpdateWhileOffscreen:NO];
451
452         if ([document respondsToSelector:@selector(setAllowsMessaging:)])
453             [document setAllowsMessaging:YES];
454         if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
455             [webview _setAllowsMessaging:YES];
456
457         if ([webview_ respondsToSelector:@selector(_scrollView)]) {
458             scroller_ = [webview_ _scrollView];
459
460             [scroller_ setDirectionalLockEnabled:YES];
461             [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
462             [scroller_ setDelaysContentTouches:NO];
463
464             [scroller_ setCanCancelContentTouches:YES];
465
466             [scroller_ setAlwaysBounceVertical:NO];
467         } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
468             UIScroller *scroller([webview_ _scroller]);
469             scroller_ = (UIScrollView *) scroller;
470
471             [scroller setDirectionalScrolling:YES];
472             [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
473             [scroller setScrollHysteresis:0]; /* 8 */
474
475             [scroller setThumbDetectionEnabled:NO];
476         }
477
478         [webview_ setOpaque:NO];
479         [webview_ setBackgroundColor:[UIColor clearColor]];
480
481         [scroller_ setFixedBackgroundPattern:YES];
482         [scroller_ setBackgroundColor:[UIColor clearColor]];
483         [scroller_ setClipsSubviews:NO];
484
485         [scroller_ setBounces:YES];
486         [scroller_ setShowBackgroundShadow:NO]; /* YES */
487
488         [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
489         [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
490
491         NSDictionary *configuration([$CydgetController currentConfiguration]);
492         cycript_ = [configuration objectForKey:@"CycriptURLs"];
493
494         [scroller_ setScrollingEnabled:[[configuration objectForKey:@"Scrollable"] boolValue]];
495
496         [self loadURL:url];
497     } return self;
498 }
499
500 - (void) webView:(WebView *)webview didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
501     if (cycript_ != nil)
502         if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
503             if (RegEx([cycript_ UTF8String])(href))
504                 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
505                     if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
506                         WebFrame *frame([webview mainFrame]);
507                         JSGlobalContextRef context([frame globalContext]);
508                         @try {
509                             CYSetupContext(context);
510                         } @catch (NSException *e) {
511                             NSLog(@"*** CydgetSetupContext => %@", e);
512                         }
513                     }
514 }
515
516 - (void) updateStyles {
517     [webview_ updateStyles];
518 }
519
520 @end
521
522 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
523     NSDictionary *configuration_;
524     WebCydgetLockScreenView *background_;
525     WebCydgetLockScreenView *foreground_;
526 }
527
528 @end
529
530 #include <string>
531
532 struct State {
533     unsigned state;
534 };
535
536 namespace JSC {
537     class JSGlobalData;
538     class UString;
539 }
540
541 namespace WebCore {
542     class KURL;
543 }
544
545 namespace WebCore {
546 struct String {
547     void *impl_;
548 }; }
549
550 namespace JSC {
551 struct SourceCode {
552     void *provider_;
553     int start_;
554     int end_;
555     int line_;
556 }; }
557
558 namespace JSC {
559 union ScriptSourceCode {
560     struct {
561         JSC::SourceCode source_;
562     } Old;
563     struct {
564         void *provider_;
565         JSC::SourceCode source_;
566     } New;
567 }; }
568
569 // String Helpers {{{
570 static const UChar *(*_ZNK7WebCore6String10charactersEv)(const WebCore::String *);
571 static const UChar *(*_ZN7WebCore6String29charactersWithNullTerminationEv)(const WebCore::String *);
572 static unsigned (*_ZNK7WebCore6String6lengthEv)(const WebCore::String *);
573
574 static bool StringGet(const WebCore::String &string, const UChar *&data, size_t &length) {
575     bool terminated;
576
577     if (_ZNK7WebCore6String10charactersEv != NULL) {
578         data = (*_ZNK7WebCore6String10charactersEv)(&string);
579         terminated = false;
580     } else if (_ZN7WebCore6String29charactersWithNullTerminationEv != NULL) {
581         data = (*_ZN7WebCore6String29charactersWithNullTerminationEv)(&string);
582         terminated = true;
583     } else return false;
584
585     if (data == NULL)
586         return false;
587
588     if (_ZNK7WebCore6String6lengthEv != NULL)
589         length = (*_ZNK7WebCore6String6lengthEv)(&string);
590     else if (terminated)
591         for (length = 0; data[length] != 0; ++length);
592     else return false;
593
594     return true;
595 }
596
597 static bool StringEquals(const WebCore::String &string, const char *value) {
598     const UChar *data;
599     size_t size;
600     if (!StringGet(string, data, size))
601         return false;
602
603     size_t length(strlen(value));
604     if (size != length)
605         return false;
606
607     for (size_t index(0); index != length; ++index)
608         if (data[index] != value[index])
609             return false;
610
611     return true;
612 }
613 // }}}
614 // State Machine {{{
615 static bool cycript_;
616
617 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
618     if (!StringEquals(mime, "text/cycript")) {
619         cycript_ = false;
620         return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
621     }
622
623     static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
624     if (handle == NULL)
625         return false;
626
627     cycript_ = true;
628     return true;
629 }
630 // }}}
631 // Script Compiler {{{
632 static void Log(const WebCore::String &string) {
633 #if 0
634     const UChar *data;
635     size_t length;
636     if (!StringGet(string, data, length))
637         return;
638
639     UChar terminated[length + 1];
640     terminated[length] = 0;
641     memcpy(terminated, data, length * 2);
642     NSLog(@"wtf %p:%zu:%S:", &string, length, terminated);
643 #endif
644 }
645
646 static bool Cycriptify(const uint16_t *&data, size_t &size) {
647     cycript_ = false;
648
649     if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
650         if (void (*CydgetMemoryParse)(const uint16_t **, size_t *) = reinterpret_cast<void (*)(const uint16_t **, size_t *)>(dlsym(handle, "CydgetMemoryParse"))) @try {
651             CydgetMemoryParse(&data, &size);
652             return true;
653         } @catch (NSException *e) {
654             NSLog(@"*** CydgetMemoryParse => %@", e);
655         }
656     return false;
657 }
658
659 static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned);
660 static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned);
661
662 static void Cycriptify(const WebCore::String &source, int *psize = NULL) {
663     if (!cycript_)
664         return;
665     cycript_ = false;
666
667     const UChar *data;
668     size_t length;
669     if (!StringGet(source, data, length))
670         return;
671
672     size_t size(length);
673     if (!Cycriptify(data, size))
674         return;
675
676     WebCore::String &script(const_cast<WebCore::String &>(source));
677     _ZN7WebCore6String8truncateEj(&script, 0);
678     _ZN7WebCore6String6appendEPKtj(&script, data, size);
679
680     if (psize != NULL)
681         *psize = size;
682
683     free((void *) data);
684
685     Log(source);
686 }
687 // }}}
688
689 static WebCore::String *string;
690
691 // iOS 2.x
692 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, void *_this, const WebCore::String &string, State state, const WebCore::String &url, int line) {
693     Cycriptify(string);
694     return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i(_this, string, state, url, line);
695 }
696
697 // iOS 3.x
698 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
699     /*if (cycript_) {
700         JSC::SourceCode *source(_this[iOS32 ? 6 : 0]);
701         const uint16_t *data(source->data());
702         size_t size(source->length());
703
704         if (Cycriptify(data, size)) {
705             source->~SourceCode();
706             // XXX: I actually don't have the original URL here: pants
707             new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
708             free((void *) data);
709         }
710     }*/
711
712     return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
713 }
714
715 // iOS 3.x cdata
716 MSHook(const WebCore::String &, _ZNK7WebCore4Node11textContentEb, void *_this, bool convert) {
717     const WebCore::String &code(__ZNK7WebCore4Node11textContentEb(_this, convert));
718     string = const_cast<WebCore::String *>(&code);
719     Log(code);
720     Cycriptify(code);
721     return code;
722 }
723
724 // iOS 4.x cdata
725 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) {
726     Cycriptify(source);
727     return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line);
728 }
729
730 // iOS 4.x+5.0 @src=
731 MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) {
732     const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this));
733     string = const_cast<WebCore::String *>(&script);
734     Log(script);
735     return script;
736 }
737
738 // iOS 4.x @src=
739 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, void *_this, JSC::ScriptSourceCode &script, State state) {
740     if (string != NULL) {
741         JSC::SourceCode *source(iOS4 ? &script.New.source_ : &script.Old.source_);
742         Cycriptify(*string, &source->end_);
743         string = NULL;
744     }
745
746     return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, script, state);
747 }
748
749 // iOS 5.0 cdata
750 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
751     Cycriptify(source);
752     return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE(_this, source, url, position);
753 }
754
755 // iOS 5.0 @src=
756 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
757     string = NULL;
758     return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE(_this, position, legacy);
759 }
760
761 void (*$_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE)(void *_this, int legacy);
762
763 // iOS 5.0 @src=
764 MSHook(void, _ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, void *_this, JSC::ScriptSourceCode &script) {
765     if (string != NULL) {
766         JSC::SourceCode *source(&script.New.source_);
767         $_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE(_this, 0);
768         Cycriptify(*string, &source->end_);
769         string = NULL;
770     }
771
772     return __ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE(_this, script);
773 }
774
775 // iOS 6.0 cdata
776 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, void *_this, const WebCore::String &source, const WebCore::KURL &url, void *position) {
777     Cycriptify(source);
778     return __ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE(_this, source, url, position);
779 }
780
781 // iOS 6.0 @src=
782 MSHook(void, _ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, void *_this, void *position, int legacy) {
783     string = NULL;
784     return __ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE(_this, position, legacy);
785 }
786
787 /* Cydget:// Protocol {{{ */
788 @interface CydgetURLProtocol : NSURLProtocol {
789 }
790
791 @end
792
793 @implementation CydgetURLProtocol
794
795 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
796     NSURL *url([request URL]);
797     if (url == nil)
798         return NO;
799     NSString *scheme([[url scheme] lowercaseString]);
800     if (scheme == nil || ![scheme isEqualToString:@"cydget"])
801         return NO;
802     return YES;
803 }
804
805 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
806     return request;
807 }
808
809 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
810     id<NSURLProtocolClient> client([self client]);
811     if (icon == nil)
812         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
813     else {
814         NSData *data(UIImagePNGRepresentation(icon));
815
816         NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
817         [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
818         [client URLProtocol:self didLoadData:data];
819         [client URLProtocolDidFinishLoading:self];
820     }
821 }
822
823 - (void) startLoading {
824     id<NSURLProtocolClient> client([self client]);
825     NSURLRequest *request([self request]);
826
827     NSURL *url([request URL]);
828     NSString *href([url absoluteString]);
829
830     NSString *path([href substringFromIndex:9]);
831     NSRange slash([path rangeOfString:@"/"]);
832
833     NSString *command;
834     if (slash.location == NSNotFound) {
835         command = path;
836         path = nil;
837     } else {
838         command = [path substringToIndex:slash.location];
839         path = [path substringFromIndex:(slash.location + 1)];
840     }
841
842     if ([command isEqualToString:@"_UIImageWithName"]) {
843         if (path == nil)
844             goto fail;
845         path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
846         UIImage *icon(_UIImageWithName(path));
847         [self _returnPNGWithImage:icon forRequest:request];
848     } else fail: {
849         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
850     }
851 }
852
853 - (void) stopLoading {
854 }
855
856 @end
857 /* }}} */
858 /* Cydget-CGI:// Protocol {{{ */
859 @interface CydgetCGIURLProtocol : NSURLProtocol {
860     pid_t pid_;
861     CFHTTPMessageRef http_;
862     NSFileHandle *handle_;
863 }
864
865 @end
866
867 @implementation CydgetCGIURLProtocol
868
869 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
870     NSURL *url([request URL]);
871     if (url == nil)
872         return NO;
873     NSString *scheme([[url scheme] lowercaseString]);
874     if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"])
875         return NO;
876     return YES;
877 }
878
879 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
880     return request;
881 }
882
883 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
884     if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
885         pid_ = -1;
886     } return self;
887 }
888
889 - (void) startLoading {
890     id<NSURLProtocolClient> client([self client]);
891     NSURLRequest *request([self request]);
892     NSURL *url([request URL]);
893
894     NSString *path([url path]);
895     if (path == nil) {
896         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
897         return;
898     }
899
900     NSFileManager *manager([NSFileManager defaultManager]);
901     if (![manager fileExistsAtPath:path]) {
902         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
903         return;
904     }
905
906     int fds[2];
907     _assert(pipe(fds) != -1);
908
909     _assert(pid_ == -1);
910     pid_ = fork();
911     if (pid_ == -1) {
912         _assert(close(fds[0]) != -1);
913         _assert(close(fds[1]) != -1);
914         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
915         return;
916     }
917
918     if (pid_ == 0) {
919         const char *script([path UTF8String]);
920
921         setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
922         setenv("SCRIPT_FILENAME", script, true);
923         NSString *query([url query]);
924         if (query != nil)
925             setenv("QUERY_STRING", [query UTF8String], true);
926
927         _assert(dup2(fds[1], 1) != -1);
928         _assert(close(fds[0]) != -1);
929         _assert(close(fds[1]) != -1);
930
931         execl(script, script, NULL);
932         exit(1);
933         _assert(false);
934     }
935
936     _assert(close(fds[1]) != -1);
937
938     _assert(http_ == NULL);
939     http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
940     CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
941
942     _assert(handle_ == nil);
943     handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES];
944
945     [[NSNotificationCenter defaultCenter]
946         addObserver:self
947         selector:@selector(onRead:)
948         name:@"NSFileHandleReadCompletionNotification"
949         object:handle_
950     ];
951
952     [handle_ readInBackgroundAndNotify];
953 }
954
955 - (void) onRead:(NSNotification *)notification {
956     NSFileHandle *handle([notification object]);
957
958     NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
959
960     if (size_t length = [data length]) {
961         CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
962         [handle readInBackgroundAndNotify];
963     } else {
964         id<NSURLProtocolClient> client([self client]);
965
966         CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
967         if (mime == NULL)
968             [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
969         else {
970             NSURLRequest *request([self request]);
971
972             NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
973             CFRelease(mime);
974
975             [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
976
977             CFDataRef body(CFHTTPMessageCopyBody(http_));
978             [client URLProtocol:self didLoadData:(NSData *)body];
979             CFRelease(body);
980
981             [client URLProtocolDidFinishLoading:self];
982         }
983
984         CFRelease(http_);
985         http_ = NULL;
986     }
987 }
988
989     //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
990
991 - (void) stopLoading_ {
992     [[NSNotificationCenter defaultCenter] removeObserver:self];
993
994     if (handle_ != nil) {
995         [handle_ release];
996         handle_ = nil;
997     }
998
999     if (pid_ != -1) {
1000         kill(pid_, SIGTERM);
1001         int status;
1002         _syscall(waitpid(pid_, &status, 0));
1003         pid_ = -1;
1004     }
1005 }
1006
1007 - (void) stopLoading {
1008     [self
1009         performSelectorOnMainThread:@selector(stopLoading_)
1010         withObject:nil
1011         waitUntilDone:NO
1012     ];
1013 }
1014
1015 @end
1016 /* }}} */
1017
1018 namespace WebCore {
1019     class MediaQueryEvaluator;
1020     class CSSParserValueList;
1021 }
1022
1023 namespace WebCore {
1024 struct MediaQueryExp {
1025     String feature_;
1026     void *value_;
1027     bool valid_;
1028     String cache_;
1029 }; }
1030
1031 bool CYHaveMediaControls() {
1032     SBLockScreenView *view([[[$SBLockScreenManager sharedInstance] lockScreenViewController] lockScreenView]);
1033     return view != nil && ![view mediaControlsHidden];
1034     //return [[[$SBLockScreenManager sharedInstance] lockScreenViewController] isShowingMediaControls];
1035 }
1036
1037 bool CYHaveNotificationList() {
1038     SBLockScreenNotificationListController *controller([[[$SBLockScreenManager sharedInstance] lockScreenViewController] _notificationController]);
1039     return controller != nil && [controller hasAnyContent];
1040 }
1041
1042 MSHook(bool, _ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, WebCore::MediaQueryEvaluator *_this, WebCore::String &query) {
1043     Log(query); if (false);
1044     else if (kCFCoreFoundationVersionNumber >= 800 && StringEquals(query, "cydget-media-controls"))
1045         return CYHaveMediaControls();
1046     else if (kCFCoreFoundationVersionNumber >= 800 && StringEquals(query, "cydget-notification-list"))
1047         return CYHaveNotificationList();
1048     else
1049         return __ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE(_this, query);
1050 }
1051
1052 MSHook(void, _ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, WebCore::MediaQueryExp *_this, WebCore::String &query, WebCore::CSSParserValueList *values) {
1053     Log(query);
1054     __ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE(_this, query, values);
1055     if (!_this->valid_) if (
1056         StringEquals(query, "cydget-media-controls") ||
1057         StringEquals(query, "cydget-notification-list") ||
1058     false) _this->valid_ = true;
1059 }
1060
1061 template <typename Type_>
1062 static void dlset(Type_ &function, const char *name) {
1063     function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
1064 }
1065
1066 template <typename Type_>
1067 static void msset_(Type_ &function, const char *name, MSImageRef handle) {
1068     function = reinterpret_cast<Type_>(MSFindSymbol(handle, name));
1069 }
1070
1071 #define msset(function, handle) \
1072     msset_(function, "_" #function, handle)
1073
1074 @implementation WebCycriptLockScreenController
1075
1076 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
1077     UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
1078     if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
1079         [uiWebView webView:view addMessageToConsole:message];
1080 }
1081
1082 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
1083     UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
1084     if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
1085         [uiWebView webView:view didClearWindowObject:window forFrame:frame];
1086 }
1087
1088 + (void) initialize {
1089     if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
1090         class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
1091         class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
1092     }
1093
1094     if (CGFloat *_UIScrollViewDecelerationRateNormal = reinterpret_cast<CGFloat *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
1095         CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
1096     else // XXX: this actually might be fast on some older systems: we should look into this
1097         CYScrollViewDecelerationRateNormal = 0.998;
1098
1099     iOS4 = kCFCoreFoundationVersionNumber >= 550.32;
1100     iOS32 = !iOS4 && kCFCoreFoundationVersionNumber >= 478.61;
1101
1102     int maxproc;
1103     size_t size(sizeof(maxproc));
1104     if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
1105         NSLog(@"sysctlbyname(\"kern.maxproc\", ?)");
1106     else if (maxproc < 72) {
1107         maxproc = 72;
1108         if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
1109             NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
1110     }
1111
1112     [NSURLProtocol registerClass:[CydgetURLProtocol class]];
1113     [WebView registerURLSchemeAsLocal:@"cydget"];
1114
1115     [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]];
1116     [WebView registerURLSchemeAsLocal:@"cydget-cgi"];
1117
1118     MSImageRef JavaScriptCore(MSGetImageByName("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore"));
1119     MSImageRef WebCore(MSGetImageByName("/System/Library/PrivateFrameworks/WebCore.framework/WebCore"));
1120
1121     if (!iOS4) {
1122         void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
1123         dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
1124         if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
1125             MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
1126     }
1127
1128     bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &) = NULL;
1129     if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
1130         MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE", WebCore);
1131     if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE == NULL)
1132         MSHookSymbol(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKN3WTF6StringE", WebCore);
1133     if (_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE != NULL)
1134         MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
1135
1136     void (*_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)(void *, const WebCore::String &, const WebCore::KURL &, int) = NULL;
1137     if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi == NULL)
1138         MSHookSymbol(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, "__ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi", WebCore);
1139     if (_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi != NULL)
1140         MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi));
1141
1142     if (!iOS4) {
1143         const WebCore::String &(*_ZNK7WebCore4Node11textContentEb)(void *, bool) = NULL;
1144         if (_ZNK7WebCore4Node11textContentEb == NULL)
1145             MSHookSymbol(_ZNK7WebCore4Node11textContentEb, "__ZNK7WebCore4Node11textContentEb", WebCore);
1146         if (_ZNK7WebCore4Node11textContentEb != NULL)
1147             MSHookFunction(_ZNK7WebCore4Node11textContentEb, MSHake(_ZNK7WebCore4Node11textContentEb));
1148     }
1149
1150     const WebCore::String &(*_ZN7WebCore12CachedScript6scriptEv)(void *) = NULL;
1151     if (_ZN7WebCore12CachedScript6scriptEv == NULL)
1152         MSHookSymbol(_ZN7WebCore12CachedScript6scriptEv, "__ZN7WebCore12CachedScript6scriptEv", WebCore);
1153     if (_ZN7WebCore12CachedScript6scriptEv != NULL)
1154         MSHookFunction(_ZN7WebCore12CachedScript6scriptEv, MSHake(_ZN7WebCore12CachedScript6scriptEv));
1155
1156     State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i)(void *, const WebCore::String &, State, const WebCore::String &, int) = NULL;
1157     if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i == NULL)
1158         MSHookSymbol(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i", WebCore);
1159     if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i != NULL)
1160         MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_6StringENS0_5StateES3_i));
1161
1162     State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)(void *, JSC::ScriptSourceCode &, State) = NULL;
1163     if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE == NULL)
1164         MSHookSymbol(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE", WebCore);
1165     if (_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE != NULL)
1166         MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE));
1167
1168     void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
1169     msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, WebCore);
1170     if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE != NULL)
1171         MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionINS1_14OneBasedNumberEEE));
1172
1173     void (*_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE)(void *, const WebCore::String &, const WebCore::KURL &, void *);
1174     msset(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, WebCore);
1175     if (_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE != NULL)
1176         MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKN3WTF6StringERKNS_4KURLERKNS1_12TextPositionE));
1177
1178     void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE)(void *, void *, int);
1179     msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, WebCore);
1180     if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE != NULL)
1181         MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionINS1_14OneBasedNumberEEENS0_17LegacyTypeSupportE));
1182
1183     void (*_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE)(void *, void *, int);
1184     msset(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, WebCore);
1185     if (_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE != NULL)
1186         MSHookFunction(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE, MSHake(_ZN7WebCore13ScriptElement13prepareScriptERKN3WTF12TextPositionENS0_17LegacyTypeSupportE));
1187
1188     MSHookSymbol($_ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE, "__ZNK7WebCore13ScriptElement21isScriptTypeSupportedENS0_17LegacyTypeSupportE", WebCore);
1189
1190     void (*_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE)(void *, JSC::ScriptSourceCode &);
1191     msset(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, WebCore);
1192     if (_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE != NULL)
1193         MSHookFunction(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE, MSHake(_ZN7WebCore13ScriptElement13executeScriptERKNS_16ScriptSourceCodeE));
1194
1195     if (_ZN7WebCore6String6appendEPKtj == NULL)
1196         MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN7WebCore6String6appendEPKtj", WebCore);
1197     if (_ZN7WebCore6String6appendEPKtj == NULL)
1198         msset(_ZN7WebCore6String6appendEPKtj, JavaScriptCore);
1199     if (_ZN7WebCore6String6appendEPKtj == NULL)
1200         MSHookSymbol(_ZN7WebCore6String6appendEPKtj, "__ZN3WTF6String6appendEPKtj", JavaScriptCore);
1201
1202     if (_ZN7WebCore6String8truncateEj == NULL)
1203         MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN7WebCore6String8truncateEj", WebCore);
1204     if (_ZN7WebCore6String8truncateEj == NULL)
1205         msset(_ZN7WebCore6String8truncateEj, JavaScriptCore);
1206     if (_ZN7WebCore6String8truncateEj == NULL)
1207         MSHookSymbol(_ZN7WebCore6String8truncateEj, "__ZN3WTF6String8truncateEj", JavaScriptCore);
1208
1209     msset(_ZNK7WebCore6String10charactersEv, WebCore);
1210
1211     msset(_ZN7WebCore6String29charactersWithNullTerminationEv, JavaScriptCore);
1212     if (_ZN7WebCore6String29charactersWithNullTerminationEv == NULL)
1213         MSHookSymbol(_ZN7WebCore6String29charactersWithNullTerminationEv, "__ZN3WTF6String29charactersWithNullTerminationEv", JavaScriptCore);
1214
1215     msset(_ZNK7WebCore6String6lengthEv, WebCore);
1216
1217     bool (*_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE)(WebCore::MediaQueryEvaluator *, WebCore::String &);
1218     msset(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, WebCore);
1219     if (_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE != NULL)
1220         MSHookFunction(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE, MSHake(_ZNK7WebCore19MediaQueryEvaluator4evalEPKNS_13MediaQueryExpE));
1221
1222     void (*_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE)(WebCore::MediaQueryExp *, WebCore::String &, WebCore::CSSParserValueList *);
1223     msset(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, WebCore);
1224     if (_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE != NULL)
1225         MSHookFunction(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE, MSHake(_ZN7WebCore13MediaQueryExpC2ERKN3WTF12AtomicStringEPNS_18CSSParserValueListE));
1226 }
1227
1228 + (id) rootViewController {
1229     return [[[self alloc] init] autorelease];
1230 }
1231
1232 - (void) dealloc {
1233     [configuration_ release];
1234     [background_ release];
1235     [foreground_ release];
1236     [super dealloc];
1237 }
1238
1239 - (id) init {
1240     if ((self = [super init]) != nil) {
1241         configuration_ = [[$CydgetController currentConfiguration] retain];
1242     } return self;
1243 }
1244
1245 - (void) loadView {
1246     NSURL *base([NSURL fileURLWithPath:[$CydgetController currentPath]]);
1247
1248     if (NSString *background = [configuration_ objectForKey:@"Background"])
1249         background_ = [[WebCydgetLockScreenView alloc] initWithURL:[NSURL URLWithString:background relativeToURL:base]];
1250
1251     if (NSString *homepage = [configuration_ objectForKey:@"Homepage"]) {
1252         foreground_ = [[WebCydgetLockScreenView alloc] initWithURL:[NSURL URLWithString:homepage relativeToURL:base]];
1253         [self setView:foreground_];
1254     } else if (kCFCoreFoundationVersionNumber < 800 && background_ != nil) {
1255         [self setView:[background_ autorelease]];
1256         background_ = nil;
1257     }
1258 }
1259
1260 - (void) purgeView {
1261     [background_ removeFromSuperview];
1262     [background_ release];
1263     background_ = nil;
1264     [foreground_ removeFromSuperview];
1265     [foreground_ release];
1266     foreground_ = nil;
1267     [super purgeView];
1268 }
1269
1270 - (void) updateStyles {
1271     [foreground_ updateStyles];
1272     [background_ updateStyles];
1273 }
1274
1275 - (UIView *) backgroundView {
1276     return background_;
1277 }
1278
1279 - (BOOL) showAwayItems {
1280     return YES;
1281 }
1282
1283 - (BOOL) updateHidden {
1284     if (foreground_ == nil)
1285         return true;
1286     SBLockScreenViewController *controller([[$SBLockScreenManager sharedInstance] lockScreenViewController]);
1287     SBLockScreenView *view([controller lockScreenView]);
1288     bool media(view != nil && ![view mediaControlsHidden]);
1289     [foreground_ setHidden:media];
1290     return media;
1291 }
1292
1293 - (BOOL) showDateView {
1294     bool homepage([configuration_ objectForKey:@"Homepage"] != nil);
1295     if (kCFCoreFoundationVersionNumber < 800)
1296         return !homepage;
1297     else if (!homepage)
1298         return true;
1299     return [self updateHidden];
1300 }
1301
1302 - (BOOL) allowsLockScreenMediaControls {
1303     if (kCFCoreFoundationVersionNumber < 800)
1304         return true;
1305     if (background_ == nil)
1306         return false;
1307     [self updateHidden];
1308     return true;
1309 }
1310
1311 /*- (BOOL) showHeaderView {
1312     return YES;
1313 }*/
1314
1315 // 1-3
1316 - (NSUInteger) presentationStyle {
1317     return 1;
1318 }
1319
1320 // 1-5
1321 - (NSUInteger) overlayStyle {
1322     if ([configuration_ objectForKey:@"Background"] == nil)
1323         return 1;
1324     return 4;
1325 }
1326
1327 // 1-[2/3?]
1328 - (NSUInteger) notificationBehavior {
1329     return 1;
1330 }
1331
1332 - (BOOL) viewWantsFullscreenLayout {
1333     return kCFCoreFoundationVersionNumber >= 800;
1334 }
1335
1336 /*- (BOOL) viewWantsOverlayLayout {
1337     return kCFCoreFoundationVersionNumber >= 800;
1338 }*/
1339
1340 - (BOOL) shouldDisableOnUnlock {
1341     return YES;
1342 }
1343
1344 - (BOOL) canBeAlwaysFullscreen {
1345     return YES;
1346 }
1347
1348 /*- (BOOL) wantsSwipeGestureRecognizer {
1349     return YES;
1350 }
1351
1352 - (BOOL) handleGesture:(int)arg1 fingerCount:(NSUInteger)fingers {
1353     return NO;
1354     return YES;
1355 }*/
1356
1357 // - (void) lockScreenMediaControlsShown:(BOOL)shown;
1358
1359 - (BOOL) handleMenuButtonDoubleTap {
1360     if (kCFCoreFoundationVersionNumber >= 800) {
1361         SBLockScreenViewController *controller([[$SBLockScreenManager sharedInstance] lockScreenViewController]);
1362         [controller _setMediaControlsVisible:![controller isShowingMediaControls]];
1363         [self updateHidden];
1364     }
1365
1366     return [super handleMenuButtonDoubleTap];
1367 }
1368
1369 @end
1370
1371 MSClassHook(WebView)
1372 MSMetaClassHook(WebView)
1373
1374 MSClassMessageHook0(void, WebView, enableWebThread) {
1375     if (kCFCoreFoundationVersionNumber >= 478.61)
1376         return MSOldCall();
1377
1378     NSLog(@"-[WebView enableWebThread]");
1379 }