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