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