Fixed a crash bug reported by ZodTTD in SourceURL string handling.
[cydget.git] / LockScreen.mm
1 /* CydgetScript - open-source IntelliDial replacement
2  * Copyright (C) 2009  Jay Freeman (saurik)
3 */
4
5 /*
6  *        Redistribution and use in source and binary
7  * forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the
11  *    above copyright notice, this list of conditions
12  *    and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the
14  *    above copyright notice, this list of conditions
15  *    and the following disclaimer in the documentation
16  *    and/or other materials provided with the
17  *    distribution.
18  * 3. The name of the author may not be used to endorse
19  *    or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
23  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
30  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <substrate.h>
39
40 #import <GraphicsServices/GraphicsServices.h>
41 #import <UIKit/UIKit.h>
42 #import <AddressBook/AddressBook.h>
43
44 #import <SpringBoardUI/SBAwayViewPluginController.h>
45 #import <TelephonyUI/TPBottomLockBar.h>
46
47 #import <QuartzCore/CALayer.h>
48 // XXX: fix the minimum requirement
49 extern NSString * const kCAFilterNearest;
50
51 #include <WebKit/DOMCSSPrimitiveValue.h>
52 #include <WebKit/DOMCSSStyleDeclaration.h>
53 #include <WebKit/DOMDocument.h>
54 #include <WebKit/DOMHTMLBodyElement.h>
55 #include <WebKit/DOMNodeList.h>
56 #include <WebKit/DOMRGBColor.h>
57
58 #include <WebKit/WebFrame.h>
59 #include <WebKit/WebPolicyDelegate.h>
60 #include <WebKit/WebPreferences.h>
61 #include <WebKit/WebScriptObject.h>
62
63 #import <WebKit/WebView.h>
64 #import <WebKit/WebView-WebPrivate.h>
65
66 #include <WebCore/Page.h>
67 #include <WebCore/Settings.h>
68
69 #include <WebCore/WebCoreThread.h>
70 #include <WebKit/WebPreferences-WebPrivate.h>
71
72 #include "JSGlobalData.h"
73 #include "SourceCode.h"
74
75 #include <apr-1/apr_pools.h>
76 #include <pcre.h>
77
78 @interface WebView (UICaboodle)
79 - (void) setScriptDebugDelegate:(id)delegate;
80 - (void) _setFormDelegate:(id)delegate;
81 - (void) _setUIKitDelegate:(id)delegate;
82 - (void) setWebMailDelegate:(id)delegate;
83 - (void) _setLayoutInterval:(float)interval;
84 @end
85
86 #define _transient
87 #define _forever for (;;)
88
89 _disused static unsigned trace_;
90
91 #define _trace() do { \
92     NSLog(@"_trace(%u)@%s:%u[%s]\n", \
93         trace_++, __FILE__, __LINE__, __FUNCTION__\
94     ); \
95 } while (false)
96
97 @protocol CydgetController
98 - (NSDictionary *) currentConfiguration;
99 @end
100
101 static Class $CydgetController(objc_getClass("CydgetController"));
102
103 @interface NSString (UIKit)
104 - (NSString *) stringByAddingPercentEscapes;
105 @end
106
107 @implementation UIWebDocumentView (WebCycript)
108
109 - (void) _setScrollerOffset:(CGPoint)offset {
110     UIScroller *scroller([self _scroller]);
111
112     CGSize size([scroller contentSize]);
113     CGSize bounds([scroller bounds].size);
114
115     CGPoint max;
116     max.x = size.width - bounds.width;
117     max.y = size.height - bounds.height;
118
119     // wtf Apple?!
120     if (max.x < 0)
121         max.x = 0;
122     if (max.y < 0)
123         max.y = 0;
124
125     offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
126     offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
127
128     [scroller setOffset:offset];
129 }
130
131 @end
132
133 /* Perl-Compatible RegEx {{{ */
134 class Pcre {
135   private:
136     pcre *code_;
137     pcre_extra *study_;
138     int capture_;
139     int *matches_;
140     const char *data_;
141
142   public:
143     Pcre(const char *regex, int options = 0) :
144         study_(NULL)
145     {
146         const char *error;
147         int offset;
148         code_ = pcre_compile(regex, options, &error, &offset, NULL);
149
150         if (code_ == NULL)
151             @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** Pcre(,): [%u] %s", offset, error] userInfo:nil];
152
153         pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
154         matches_ = new int[(capture_ + 1) * 3];
155     }
156
157     ~Pcre() {
158         pcre_free(code_);
159         delete matches_;
160     }
161
162     NSString *operator [](size_t match) {
163         return [[[NSString alloc] initWithBytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2]) encoding:NSUTF8StringEncoding] autorelease];
164     }
165
166     bool operator ()(NSString *data) {
167         // XXX: length is for characters, not for bytes
168         return operator ()([data UTF8String], [data length]);
169     }
170
171     bool operator ()(const char *data, size_t size) {
172         data_ = data;
173         return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
174     }
175 };
176 /* }}} */
177 /* WebCycript Delegate {{{ */
178 @interface WebCycriptDelegate : NSObject {
179     _transient volatile id delegate_;
180 }
181
182 - (void) setDelegate:(id)delegate;
183 - (id) initWithDelegate:(id)delegate;
184 @end
185
186 @implementation WebCycriptDelegate
187
188 - (void) setDelegate:(id)delegate {
189     delegate_ = delegate;
190 }
191
192 - (id) initWithDelegate:(id)delegate {
193     delegate_ = delegate;
194     return self;
195 }
196
197 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
198     if (delegate_ != nil)
199         return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
200 }
201
202 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
203     if (delegate_ != nil)
204         return [delegate_ webView:sender didCommitLoadForFrame:frame];
205 }
206
207 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
208     if (delegate_ != nil)
209         return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
210 }
211
212 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
213     if (delegate_ != nil)
214         return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
215 }
216
217 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
218     if (delegate_ != nil)
219         return [delegate_ webView:sender didFinishLoadForFrame:frame];
220 }
221
222 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
223     if (delegate_ != nil)
224         return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
225 }*/
226
227 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
228     if (delegate_ != nil)
229         return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
230 }
231
232 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
233     if (delegate_ != nil)
234         return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
235 }*/
236
237 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
238     if (delegate_ != nil)
239         return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
240     return nil;
241 }*/
242
243 - (IMP) methodForSelector:(SEL)sel {
244     if (IMP method = [super methodForSelector:sel])
245         return method;
246     fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
247     return NULL;
248 }
249
250 - (BOOL) respondsToSelector:(SEL)sel {
251     if ([super respondsToSelector:sel])
252         return YES;
253     // XXX: WebThreadCreateNSInvocation returns nil
254     //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
255     return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
256 }
257
258 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
259     if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
260         return method;
261     //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
262     if (delegate_ != nil)
263         if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
264             return sig;
265     // XXX: I fucking hate Apple so very very bad
266     return [NSMethodSignature signatureWithObjCTypes:"v@:"];
267 }
268
269 - (void) forwardInvocation:(NSInvocation *)inv {
270     SEL sel = [inv selector];
271     if (delegate_ != nil && [delegate_ respondsToSelector:sel])
272         [inv invokeWithTarget:delegate_];
273 }
274
275 @end
276 /* }}} */
277
278 @interface WebCydgetLockScreenView : UIView {
279     WebCycriptDelegate *indirect_;
280     UIProgressIndicator *indicator_;
281     UIScroller *scroller_;
282     UIWebDocumentView *document_;
283
284     NSString *cycript_;
285     bool scrollable_;
286
287     float width_;
288     CGSize size_;
289     bool editing_;
290
291     NSNumber *confirm_;
292
293     NSMutableSet *loading_;
294     bool error_;
295     bool reloading_;
296 }
297
298 @end
299
300 @implementation WebCydgetLockScreenView
301
302 //#include "UICaboodle/UCInternal.h"
303
304 - (void) dealloc {
305     WebThreadLock();
306
307     WebView *webview([document_ webView]);
308     [webview setFrameLoadDelegate:nil];
309     [webview setResourceLoadDelegate:nil];
310     [webview setUIDelegate:nil];
311     [webview setScriptDebugDelegate:nil];
312     [webview setPolicyDelegate:nil];
313
314     /* XXX: these are set by UIWebDocumentView
315     [webview setDownloadDelegate:nil];
316     [webview _setFormDelegate:nil];
317     [webview _setUIKitDelegate:nil];
318     [webview setEditingDelegate:nil];*/
319
320     /* XXX: no one sets this, ever
321     [webview setWebMailDelegate:nil];*/
322
323     [document_ setDelegate:nil];
324     [document_ setGestureDelegate:nil];
325     [document_ setFormEditingDelegate:nil];
326     [document_ setInteractionDelegate:nil];
327
328     [indirect_ setDelegate:nil];
329
330     [webview close];
331     [document_ release];
332
333     [indirect_ release];
334
335     WebThreadUnlock();
336
337     [scroller_ setDelegate:nil];
338
339     if (confirm_ != nil)
340         [confirm_ release];
341
342     [scroller_ release];
343     [indicator_ release];
344     [loading_ release];
345     [super dealloc];
346 }
347
348 + (float) defaultWidth {
349     return 980;
350 }
351
352 - (void) _setTileDrawingEnabled:(BOOL)enabled {
353     //[document_ setTileDrawingEnabled:enabled];
354 }
355
356 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
357     [self _setTileDrawingEnabled:NO];
358 }
359
360 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
361     [self _setTileDrawingEnabled:YES];
362     [document_ redrawScaledDocument];
363 }
364
365 - (void) setViewportWidth:(float)width {
366     width_ = width != 0 ? width : [[self class] defaultWidth];
367     [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
368 }
369
370 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
371     [self _setTileDrawingEnabled:NO];
372 }
373
374 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
375     [self _setTileDrawingEnabled:YES];
376 }
377
378 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
379     [self _setTileDrawingEnabled:YES];
380 }
381
382 - (void) loadRequest:(NSURLRequest *)request {
383     error_ = false;
384
385     WebThreadLock();
386     [document_ loadRequest:request];
387     WebThreadUnlock();
388 }
389
390 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
391     [self loadRequest:[NSURLRequest
392         requestWithURL:url
393         cachePolicy:policy
394         timeoutInterval:30.0
395     ]];
396 }
397
398 - (void) loadURL:(NSURL *)url {
399     [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
400 }
401
402 - (id) init {
403     CGRect frame = {{0, 0}, {320, 480}};
404     frame.size.height -= GSDefaultStatusBarHeight();
405
406     if ((self = [super initWithFrame:frame]) != nil) {
407         loading_ = [[NSMutableSet alloc] initWithCapacity:3];
408
409         struct CGRect bounds([self bounds]);
410
411         scroller_ = [[UIScroller alloc] initWithFrame:bounds];
412         [self addSubview:scroller_];
413
414         [scroller_ setFixedBackgroundPattern:YES];
415         [scroller_ setBackgroundColor:[UIColor blackColor]];
416
417         [scroller_ setScrollingEnabled:YES];
418         [scroller_ setClipsSubviews:YES];
419         [scroller_ setAllowsRubberBanding:YES];
420
421         [scroller_ setDelegate:self];
422         [scroller_ setBounces:YES];
423         [scroller_ setScrollHysteresis:8];
424         [scroller_ setThumbDetectionEnabled:NO];
425         [scroller_ setDirectionalScrolling:YES];
426         [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
427         [scroller_ setEventMode:YES];
428         [scroller_ setShowBackgroundShadow:NO]; /* YES */
429         [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
430         [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
431
432         CGRect rect([scroller_ bounds]);
433         //rect.size.height = 0;
434
435         WebThreadLock();
436
437         document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
438         WebView *webview([document_ webView]);
439
440         [document_ setBackgroundColor:[UIColor blackColor]];
441         if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
442             [document_ setDrawsBackground:NO];
443         [webview setDrawsBackground:NO];
444
445         [webview setPreferencesIdentifier:@"WebCycript"];
446
447         [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
448
449         if ([document_ respondsToSelector:@selector(enableReachability)])
450             [document_ enableReachability];
451
452         [document_ setAllowsMessaging:YES];
453
454         if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
455             [document_ useSelectionAssistantWithMode:0];
456
457         [document_ setTilingEnabled:YES];
458         [document_ setDrawsGrid:NO];
459         [document_ setLogsTilingChanges:NO];
460         [document_ setTileMinificationFilter:kCAFilterNearest];
461
462         if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
463             /* XXX: abstractify */
464             [document_ setDataDetectorTypes:0x80000000];
465         else
466             [document_ setDetectsPhoneNumbers:NO];
467
468         [document_ setAutoresizes:YES];
469
470         [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
471         [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
472         [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
473         //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
474
475         [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
476
477         [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
478         [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
479         [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
480
481         [document_ _setDocumentType:0x4];
482
483         if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
484             [document_ setZoomsFocusedFormControl:YES];
485         [document_ setContentsPosition:7];
486         [document_ setEnabledGestures:0xa];
487         [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
488         [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
489
490         [document_ setSmoothsFonts:YES];
491         [document_ setAllowsImageSheet:YES];
492         [webview _setUsesLoaderCache:YES];
493
494         [webview setGroupName:@"CydgetGroup"];
495
496         WebPreferences *preferences([webview preferences]);
497
498         if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
499             [webview _setLayoutInterval:0];
500         else
501             [preferences _setLayoutInterval:0];
502
503         [self setViewportWidth:0];
504
505         [document_ setDelegate:self];
506         [document_ setGestureDelegate:self];
507         [document_ setFormEditingDelegate:self];
508         [document_ setInteractionDelegate:self];
509
510         [scroller_ addSubview:document_];
511
512         //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
513
514         indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
515
516         [webview setFrameLoadDelegate:indirect_];
517         [webview setPolicyDelegate:indirect_];
518         [webview setResourceLoadDelegate:indirect_];
519         [webview setUIDelegate:indirect_];
520
521         /* XXX: do not turn this on under penalty of extreme pain */
522         [webview setScriptDebugDelegate:nil];
523
524         WebThreadUnlock();
525
526         CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
527         indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
528         [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
529
530         [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
531         [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
532
533         NSDictionary *configuration([$CydgetController currentConfiguration]);
534
535         cycript_ = [configuration objectForKey:@"Cycript"];
536
537         scrollable_ = [[configuration objectForKey:@"Scrollable"] boolValue];
538         [scroller_ setScrollingEnabled:scrollable_];
539
540         NSString *homepage([configuration objectForKey:@"Homepage"]);
541         [self loadURL:[NSURL URLWithString:homepage]];
542     } return self;
543 }
544
545 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
546     [self retain];
547
548     UIActionSheet *sheet = [[[UIActionSheet alloc]
549         initWithTitle:nil
550         buttons:[NSArray arrayWithObjects:@"OK", nil]
551         defaultButtonIndex:0
552         delegate:self
553         context:@"alert"
554     ] autorelease];
555
556     [sheet setBodyText:message];
557     [sheet popupAlertAnimated:YES];
558 }
559
560 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
561     [self retain];
562
563     UIActionSheet *sheet = [[[UIActionSheet alloc]
564         initWithTitle:nil
565         buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
566         defaultButtonIndex:0
567         delegate:indirect_
568         context:@"confirm"
569     ] autorelease];
570
571     [sheet setNumberOfRows:1];
572     [sheet setBodyText:message];
573     [sheet popupAlertAnimated:YES];
574
575     NSRunLoop *loop([NSRunLoop currentRunLoop]);
576     NSDate *future([NSDate distantFuture]);
577
578     while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
579
580     NSNumber *confirm([confirm_ autorelease]);
581     confirm_ = nil;
582
583     [self autorelease];
584     return [confirm boolValue];
585 }
586
587 /* XXX: WebThreadLock? */
588 - (void) _fixScroller:(CGRect)bounds {
589     float extra;
590     if (!editing_)
591         extra = 0;
592     else {
593         UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
594         CGRect peripheral([assistant peripheralFrame]);
595         extra = peripheral.size.height;
596     }
597
598     CGRect subrect([scroller_ frame]);
599     subrect.size.height -= [TPBottomLockBar defaultHeight];
600     subrect.size.height -= extra;
601     [scroller_ setScrollerIndicatorSubrect:subrect];
602
603 #undef NSSize
604     NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
605     [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
606
607     CGSize size(size_);
608     size.height += extra;
609     size.height += [TPBottomLockBar defaultHeight];
610     [scroller_ setContentSize:size];
611
612     [scroller_ releaseRubberBandIfNecessary];
613 }
614
615 - (void) fixScroller {
616     CGRect bounds([document_ documentBounds]);
617     [self _fixScroller:bounds];
618 }
619
620 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
621     size_ = frame.size;
622     [self _fixScroller:frame];
623 }
624
625 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
626     [self view:sender didSetFrame:frame];
627 }
628
629 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
630     editing_ = true;
631 }
632
633 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
634     [self fixScroller];
635 }
636
637 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
638     editing_ = false;
639     [self fixScroller];
640 }
641
642 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
643     if (cycript_ != nil)
644         if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
645             if (Pcre([cycript_ UTF8String], 0 /*XXX:PCRE_UTF8*/)(href))
646                 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
647                     if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
648                         WebView *webview([document_ webView]);
649                         WebFrame *frame([webview mainFrame]);
650                         JSGlobalContextRef context([frame globalContext]);
651                         CYSetupContext(context);
652                     }
653 }
654
655 - (bool) isLoading {
656     return [loading_ count] != 0;
657 }
658
659 - (void) reloadButtons {
660     if ([self isLoading]) {
661         [UIApp setNetworkActivityIndicatorVisible:YES];
662         [indicator_ startAnimation];
663     } else {
664         [UIApp setNetworkActivityIndicatorVisible:NO];
665         [indicator_ stopAnimation];
666     }
667 }
668
669 - (void) _finishLoading {
670     size_t count([loading_ count]);
671     /*if (count == 0)
672         [self autorelease];*/
673     if (reloading_ || count != 0)
674         return;
675     [self reloadButtons];
676 }
677
678 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
679     return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
680 }
681
682 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
683     return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
684 }
685
686 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
687     return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
688 }
689
690 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
691     return [document_ webView:sender didCommitLoadForFrame:frame];
692 }
693
694 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
695     return [document_ webView:sender didReceiveDocTypeForFrame:frame];
696 }
697
698 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
699     [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
700     [self _finishLoading];
701     return [document_ webView:sender didFinishLoadForFrame:frame];
702 }
703
704 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
705     /*if ([loading_ count] == 0)
706         [self retain];*/
707     [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
708
709     if ([frame parentFrame] == nil) {
710         [document_ resignFirstResponder];
711
712         reloading_ = false;
713
714         [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
715
716         if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
717             [scroller_ setZoomScale:1 duration:0];
718         else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
719             [scroller_ _setZoomScale:1 duration:0];
720         /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
721             [scroller_ setZoomScale:1 animated:NO];*/
722
723         CGRect rect([scroller_ bounds]);
724         //rect.size.height = 0;
725         [document_ setFrame:rect];
726     }
727
728     [self reloadButtons];
729 }
730
731 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
732     /*if ([frame parentFrame] == nil)
733         [self autorelease];*/
734
735     [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
736     [self _finishLoading];
737
738     if (reloading_)
739         return;
740
741     if ([frame parentFrame] == nil) {
742         [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
743             [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
744             [[error localizedDescription] stringByAddingPercentEscapes]
745         ]]];
746
747         error_ = true;
748     }
749 }
750
751 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
752     [self _didFailWithError:error forFrame:frame];
753 }
754
755 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
756     [self _didFailWithError:error forFrame:frame];
757 }
758
759 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
760     fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
761 }
762
763 @end
764
765 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
766 }
767
768 @end
769
770 #include <string>
771
772 static bool cycript_;
773 static bool jscript_;
774
775 static void SetParser(bool cycript, bool jscript) {
776     cycript_ = cycript;
777     jscript_ = jscript;
778 }
779
780 static bool GetParser0() {
781     return cycript_;
782 }
783
784 static bool GetParser1() {
785     return jscript_;
786 }
787
788 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
789     if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
790         if (void (*CYParseUChar)(apr_pool_t *, const uint16_t **, size_t *) = reinterpret_cast<void (*)(apr_pool_t *, const uint16_t **, size_t *)>(dlsym(handle, "CydgetPoolParse")))
791             CYParseUChar(pool, &data, &size);
792 }
793
794 extern "C" void *_ZN3JSC7UString3Rep14nullBaseStringE __attribute__((__weak_import__));
795 extern "C" void *_ZN3JSC7UString3Rep7destroyEv __attribute__((__weak_import__));
796 extern "C" void *_ZN3JSC7UStringC1EPKti __attribute__((__weak_import__));
797 extern "C" void *_ZN3JSC7UStringC1EPKc __attribute__((__weak_import__));
798 extern "C" void *_ZNK3JSC7UString6substrEii __attribute__((__weak_import__));
799 extern "C" void *_ZN3WTF10fastMallocEm __attribute__((__weak_import__));
800 extern "C" void WTFReportAssertionFailure(const char *, int, const char *, const char *) __attribute__((__weak_import__));
801 extern "C" void *_ZN3WTF8fastFreeEPv __attribute__((__weak_import__));
802
803 bool CYWeakHell() {
804     return
805         &_ZN3JSC7UString3Rep14nullBaseStringE == NULL ||
806         &_ZN3JSC7UString3Rep7destroyEv == NULL ||
807         &_ZN3JSC7UStringC1EPKti == NULL ||
808         &_ZN3JSC7UStringC1EPKc == NULL ||
809         &_ZNK3JSC7UString6substrEii == NULL ||
810         &_ZN3WTF10fastMallocEm == NULL ||
811         &WTFReportAssertionFailure == NULL ||
812         &_ZN3WTF8fastFreeEPv == NULL ||
813     false;
814 }
815
816 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
817     if (!GetParser0())
818         return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
819     else {
820         SetParser(false, true);
821
822         JSC::SourceCode *source(*_this);
823         const uint16_t *data(source->data());
824         size_t size(source->length());
825
826         apr_pool_t *pool;
827         apr_pool_create(&pool, NULL);
828
829         Cycriptify(pool, data, size);
830         source->~SourceCode();
831         new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
832
833         apr_pool_destroy(pool);
834
835         __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
836     }
837 }
838
839 MSHook(void, _ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, void *_this, int start, const UChar *code, unsigned length, int *source, int *line, JSC::UString *message) {
840     if (!GetParser0())
841         return __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, code, length, source, line, message);
842     else {
843         const uint16_t *data(code);
844         size_t size(length);
845
846         apr_pool_t *pool;
847         apr_pool_create(&pool, NULL);
848
849         Cycriptify(pool, data, size);
850         __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, data, size, source, line, message);
851
852         apr_pool_destroy(pool);
853     }
854 }
855
856 struct State {
857     unsigned state;
858 };
859
860 MSHook(State, _ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, State state) {
861     SetParser(false, true);
862     state = __ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE(state);
863     SetParser(false, false);
864     return state;
865 }
866
867 MSHook(void, _ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, void *resource) {
868     SetParser(false, true);
869     __ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE(resource);
870     SetParser(false, false);
871 }
872
873 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
874     if (!GetParser1() || mime != "text/cycript")
875         return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
876
877     static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
878     if (handle == NULL)
879         return false;
880
881     SetParser(true, true);
882     return true;
883 }
884
885 /* Cydget:// Protocol {{{ */
886 @interface CydgetURLProtocol : NSURLProtocol {
887 }
888
889 @end
890
891 @implementation CydgetURLProtocol
892
893 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
894     NSURL *url([request URL]);
895     if (url == nil)
896         return NO;
897     NSString *scheme([[url scheme] lowercaseString]);
898     if (scheme == nil || ![scheme isEqualToString:@"cydget"])
899         return NO;
900     return YES;
901 }
902
903 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
904     return request;
905 }
906
907 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
908     id<NSURLProtocolClient> client([self client]);
909     if (icon == nil)
910         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
911     else {
912         NSData *data(UIImagePNGRepresentation(icon));
913
914         NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
915         [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
916         [client URLProtocol:self didLoadData:data];
917         [client URLProtocolDidFinishLoading:self];
918     }
919 }
920
921 - (void) startLoading {
922     id<NSURLProtocolClient> client([self client]);
923     NSURLRequest *request([self request]);
924
925     NSURL *url([request URL]);
926     NSString *href([url absoluteString]);
927
928     NSString *path([href substringFromIndex:9]);
929     NSRange slash([path rangeOfString:@"/"]);
930
931     NSString *command;
932     if (slash.location == NSNotFound) {
933         command = path;
934         path = nil;
935     } else {
936         command = [path substringToIndex:slash.location];
937         path = [path substringFromIndex:(slash.location + 1)];
938     }
939
940     if ([command isEqualToString:@"_UIImageWithName"]) {
941         if (path == nil)
942             goto fail;
943         path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
944         UIImage *icon(_UIImageWithName(path));
945         [self _returnPNGWithImage:icon forRequest:request];
946     } else fail: {
947         [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
948     }
949 }
950
951 - (void) stopLoading {
952 }
953
954 @end
955 /* }}} */
956
957 template <typename Type_>
958 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
959     struct nlist &name(nl[index]);
960     uintptr_t value(name.n_value);
961     if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
962         value |= 0x00000001;
963     function = reinterpret_cast<Type_>(value);
964 }
965
966 template <typename Type_>
967 static void dlset(Type_ &function, const char *name) {
968     function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
969 }
970
971 @implementation WebCycriptLockScreenController
972
973 + (void) initialize {
974     apr_initialize();
975
976     [NSURLProtocol registerClass:[CydgetURLProtocol class]];
977
978     void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
979     dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
980     if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
981         MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
982
983     void (*_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE)(void *, int, const UChar *, unsigned, int *, int *, JSC::UString *);
984     dlset(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, "_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE");
985     if (_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE != NULL)
986         MSHookFunction(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, MSHake(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE));
987
988     struct nlist nl[4];
989     memset(nl, 0, sizeof(nl));
990     nl[0].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE";
991     nl[1].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE";
992     nl[2].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
993     nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
994
995     State (*_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE)(State);
996     nlset(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, nl, 0);
997     MSHookFunction(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE));
998
999     void (*_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE)(void *);
1000     nlset(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, nl, 1);
1001     MSHookFunction(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, MSHake(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE));
1002
1003     bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
1004     nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 2);
1005     MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
1006 }
1007
1008 + (id) rootViewController {
1009     return [[[self alloc] init] autorelease];
1010 }
1011
1012 - (void) loadView {
1013     [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];
1014 }
1015
1016 @end