]> git.saurik.com Git - cydget.git/blob - LockScreen.mm
I don't even like this project anymore.
[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 #include <sys/sysctl.h>
40
41 #import <GraphicsServices/GraphicsServices.h>
42 #import <UIKit/UIKit.h>
43 #import <AddressBook/AddressBook.h>
44
45 #import <SpringBoard/SBStatusBarController.h>
46 #import <SpringBoardUI/SBAwayViewPluginController.h>
47 #import <TelephonyUI/TPBottomLockBar.h>
48
49 #import <QuartzCore/CALayer.h>
50 // XXX: fix the minimum requirement
51 extern NSString * const kCAFilterNearest;
52
53 #include <WebKit/DOMCSSPrimitiveValue.h>
54 #include <WebKit/DOMCSSStyleDeclaration.h>
55 #include <WebKit/DOMDocument.h>
56 #include <WebKit/DOMHTMLBodyElement.h>
57 #include <WebKit/DOMNodeList.h>
58 #include <WebKit/DOMRGBColor.h>
59
60 #include <WebKit/WebFrame.h>
61 #include <WebKit/WebPolicyDelegate.h>
62 #include <WebKit/WebPreferences.h>
63 #include <WebKit/WebScriptObject.h>
64
65 #import <WebKit/WebView.h>
66 #import <WebKit/WebView-WebPrivate.h>
67
68 #include <WebCore/Page.h>
69 #include <WebCore/Settings.h>
70
71 #include <WebCore/WebCoreThread.h>
72 #include <WebKit/WebPreferences-WebPrivate.h>
73
74 #include "JSGlobalData.h"
75
76 #include "SourceCode.h"
77
78 #include <apr-1/apr_pools.h>
79 #include <pcre.h>
80
81 @interface WebView (UICaboodle)
82 - (void) setScriptDebugDelegate:(id)delegate;
83 - (void) _setFormDelegate:(id)delegate;
84 - (void) _setUIKitDelegate:(id)delegate;
85 - (void) setWebMailDelegate:(id)delegate;
86 - (void) _setLayoutInterval:(float)interval;
87 @end
88
89 #define _transient
90 #define _forever for (;;)
91
92 _disused static unsigned trace_;
93
94 #define _trace() do { \
95 NSLog(@"_trace(%u)@%s:%u[%s]\n", \
96 trace_++, __FILE__, __LINE__, __FUNCTION__\
97 ); \
98 } while (false)
99
100 #define _assert(test) do \
101 if (!(test)) { \
102 fprintf(stderr, "_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
103 exit(-1); \
104 } \
105 while (false)
106
107 #define _syscall(expr) \
108 do if ((long) (expr) != -1) \
109 break; \
110 else switch (errno) { \
111 case EINTR: \
112 continue; \
113 default: \
114 _assert(false); \
115 } while (true)
116
117 @protocol CydgetController
118 - (NSDictionary *) currentConfiguration;
119 @end
120
121 static Class $CydgetController(objc_getClass("CydgetController"));
122 static Class $UIFormAssistant(objc_getClass("UIFormAssistant"));
123 //static Class $SBStatusBarController(objc_getClass("SBStatusBarController"));
124
125 static Class $UIWebBrowserView;
126 static bool Wildcat_, iOS4;
127
128 @interface NSString (UIKit)
129 - (NSString *) stringByAddingPercentEscapes;
130 @end
131
132 @implementation UIWebDocumentView (WebCycript)
133
134 - (void) _setScrollerOffset:(CGPoint)offset {
135 UIScroller *scroller([self _scroller]);
136
137 CGSize size([scroller contentSize]);
138 CGSize bounds([scroller bounds].size);
139
140 CGPoint max;
141 max.x = size.width - bounds.width;
142 max.y = size.height - bounds.height;
143
144 // wtf Apple?!
145 if (max.x < 0)
146 max.x = 0;
147 if (max.y < 0)
148 max.y = 0;
149
150 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
151 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
152
153 [scroller setOffset:offset];
154 }
155
156 @end
157
158 /* Perl-Compatible RegEx {{{ */
159 class Pcre {
160 private:
161 pcre *code_;
162 pcre_extra *study_;
163 int capture_;
164 int *matches_;
165 const char *data_;
166
167 public:
168 Pcre(const char *regex, int options = 0) :
169 study_(NULL)
170 {
171 const char *error;
172 int offset;
173 code_ = pcre_compile(regex, options, &error, &offset, NULL);
174
175 if (code_ == NULL)
176 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** Pcre(,): [%u] %s", offset, error] userInfo:nil];
177
178 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
179 matches_ = new int[(capture_ + 1) * 3];
180 }
181
182 ~Pcre() {
183 pcre_free(code_);
184 delete matches_;
185 }
186
187 NSString *operator [](size_t match) {
188 return [[[NSString alloc] initWithBytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2]) encoding:NSUTF8StringEncoding] autorelease];
189 }
190
191 bool operator ()(NSString *data) {
192 // XXX: length is for characters, not for bytes
193 return operator ()([data UTF8String], [data length]);
194 }
195
196 bool operator ()(const char *data, size_t size) {
197 data_ = data;
198 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
199 }
200 };
201 /* }}} */
202 /* WebCycript Delegate {{{ */
203 @interface WebCycriptDelegate : NSObject {
204 _transient volatile id delegate_;
205 }
206
207 - (void) setDelegate:(id)delegate;
208 - (id) initWithDelegate:(id)delegate;
209 @end
210
211 @implementation WebCycriptDelegate
212
213 - (void) setDelegate:(id)delegate {
214 delegate_ = delegate;
215 }
216
217 - (id) initWithDelegate:(id)delegate {
218 delegate_ = delegate;
219 return self;
220 }
221
222 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
223 if (delegate_ != nil)
224 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
225 }
226
227 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
228 if (delegate_ != nil)
229 return [delegate_ webView:sender didCommitLoadForFrame:frame];
230 }
231
232 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
233 if (delegate_ != nil)
234 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
235 }
236
237 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
238 if (delegate_ != nil)
239 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
240 }
241
242 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
243 if (delegate_ != nil)
244 return [delegate_ webView:sender didFinishLoadForFrame:frame];
245 }
246
247 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
248 if (delegate_ != nil)
249 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
250 }*/
251
252 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
253 if (delegate_ != nil)
254 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
255 }
256
257 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
258 if (delegate_ != nil)
259 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
260 }*/
261
262 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
263 if (delegate_ != nil)
264 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
265 return nil;
266 }*/
267
268 - (IMP) methodForSelector:(SEL)sel {
269 if (IMP method = [super methodForSelector:sel])
270 return method;
271 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
272 return NULL;
273 }
274
275 - (BOOL) respondsToSelector:(SEL)sel {
276 if ([super respondsToSelector:sel])
277 return YES;
278 // XXX: WebThreadCreateNSInvocation returns nil
279 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
280 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
281 }
282
283 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
284 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
285 return method;
286 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
287 if (delegate_ != nil)
288 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
289 return sig;
290 // XXX: I fucking hate Apple so very very bad
291 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
292 }
293
294 - (void) forwardInvocation:(NSInvocation *)inv {
295 SEL sel = [inv selector];
296 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
297 [inv invokeWithTarget:delegate_];
298 }
299
300 @end
301 /* }}} */
302
303 @interface WebCydgetLockScreenView : UIView {
304 WebCycriptDelegate *indirect_;
305 UIProgressIndicator *indicator_;
306 UIScroller *scroller_;
307 UIWebDocumentView *document_;
308
309 NSString *cycript_;
310 bool scrollable_;
311
312 float width_;
313 CGSize size_;
314 bool editing_;
315
316 NSNumber *confirm_;
317
318 NSMutableSet *loading_;
319 bool error_;
320 bool reloading_;
321
322 //UIKeyboard *keyboard_;
323 }
324
325 @end
326
327 @implementation WebCydgetLockScreenView
328
329 //#include "UICaboodle/UCInternal.h"
330
331 - (void) dealloc {
332 WebThreadLock();
333
334 WebView *webview([document_ webView]);
335 [webview setFrameLoadDelegate:nil];
336 [webview setResourceLoadDelegate:nil];
337 [webview setUIDelegate:nil];
338 [webview setScriptDebugDelegate:nil];
339 [webview setPolicyDelegate:nil];
340
341 /* XXX: these are set by UIWebDocumentView
342 [webview setDownloadDelegate:nil];
343 [webview _setFormDelegate:nil];
344 [webview _setUIKitDelegate:nil];
345 [webview setEditingDelegate:nil];*/
346
347 /* XXX: no one sets this, ever
348 [webview setWebMailDelegate:nil];*/
349
350 [document_ setDelegate:nil];
351 [document_ setGestureDelegate:nil];
352
353 if ([document_ respondsToSelector:@selector(setFormEditingDelegate:)])
354 [document_ setFormEditingDelegate:nil];
355
356 [document_ setInteractionDelegate:nil];
357
358 [indirect_ setDelegate:nil];
359
360 [webview close];
361 [document_ release];
362
363 [indirect_ release];
364
365 WebThreadUnlock();
366
367 [scroller_ setDelegate:nil];
368
369 if (confirm_ != nil)
370 [confirm_ release];
371
372 //[keyboard_ release];
373
374 [scroller_ release];
375 [indicator_ release];
376 [loading_ release];
377 [super dealloc];
378 }
379
380 + (float) defaultWidth {
381 return 980;
382 }
383
384 - (void) webView:(WebView *)sender didReceiveMessage:(NSDictionary *)dictionary {
385 #if LogBrowser || ForSaurik
386 NSLog(@"Console:%@\n", [dictionary description]);
387 #endif
388 if ([document_ respondsToSelector:@selector(webView:didReceiveMessage:)])
389 [document_ webView:sender didReceiveMessage:dictionary];
390 }
391
392 - (void) webView:(id)sender willCloseFrame:(id)frame {
393 if ([document_ respondsToSelector:@selector(webView:willCloseFrame:)])
394 [document_ webView:sender willCloseFrame:frame];
395 }
396
397 - (void) webView:(id)sender didFinishDocumentLoadForFrame:(id)frame {
398 if ([document_ respondsToSelector:@selector(webView:didFinishDocumentLoadForFrame:)])
399 [document_ webView:sender didFinishDocumentLoadForFrame:frame];
400 }
401
402 - (void) webView:(id)sender didFirstLayoutInFrame:(id)frame {
403 if ([document_ respondsToSelector:@selector(webView:didFirstLayoutInFrame:)])
404 [document_ webView:sender didFirstLayoutInFrame:frame];
405 }
406
407 - (void) webViewFormEditedStatusHasChanged:(id)changed {
408 if ([document_ respondsToSelector:@selector(webViewFormEditedStatusHasChanged:)])
409 [document_ webViewFormEditedStatusHasChanged:changed];
410 }
411
412 - (void) webView:(id)sender formStateDidFocusNode:(id)formState {
413 if ([document_ respondsToSelector:@selector(webView:formStateDidFocusNode:)])
414 [document_ webView:sender formStateDidFocusNode:formState];
415 }
416
417 - (void) webView:(id)sender formStateDidBlurNode:(id)formState {
418 if ([document_ respondsToSelector:@selector(webView:formStateDidBlurNode:)])
419 [document_ webView:sender formStateDidBlurNode:formState];
420 }
421
422 - (void) webViewDidLayout:(id)sender {
423 [document_ webViewDidLayout:sender];
424 }
425
426 - (void) webView:(id)sender didFirstVisuallyNonEmptyLayoutInFrame:(id)frame {
427 [document_ webView:sender didFirstVisuallyNonEmptyLayoutInFrame:frame];
428 }
429
430 - (void) webView:(id)sender saveStateToHistoryItem:(id)item forFrame:(id)frame {
431 [document_ webView:sender saveStateToHistoryItem:item forFrame:frame];
432 }
433
434 - (void) webView:(id)sender restoreStateFromHistoryItem:(id)item forFrame:(id)frame force:(BOOL)force {
435 [document_ webView:sender restoreStateFromHistoryItem:item forFrame:frame force:force];
436 }
437
438 - (void) webView:(id)sender attachRootLayer:(id)layer {
439 [document_ webView:sender attachRootLayer:layer];
440 }
441
442 - (id) webView:(id)sender plugInViewWithArguments:(id)arguments fromPlugInPackage:(id)package {
443 return [document_ webView:sender plugInViewWithArguments:arguments fromPlugInPackage:package];
444 }
445
446 - (void) webView:(id)sender willShowFullScreenForPlugInView:(id)view {
447 [document_ webView:sender willShowFullScreenForPlugInView:view];
448 }
449
450 - (void) webView:(id)sender didHideFullScreenForPlugInView:(id)view {
451 [document_ webView:sender didHideFullScreenForPlugInView:view];
452 }
453
454 - (void) webView:(id)sender willAddPlugInView:(id)view {
455 [document_ webView:sender willAddPlugInView:view];
456 }
457
458 - (void) webView:(id)sender didObserveDeferredContentChange:(int)change forFrame:(id)frame {
459 [document_ webView:sender didObserveDeferredContentChange:change forFrame:frame];
460 }
461
462 - (void) webViewDidPreventDefaultForEvent:(id)sender {
463 [document_ webViewDidPreventDefaultForEvent:sender];
464 }
465
466 - (void) _setTileDrawingEnabled:(BOOL)enabled {
467 //[document_ setTileDrawingEnabled:enabled];
468 }
469
470 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
471 [self _setTileDrawingEnabled:NO];
472 }
473
474 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
475 [self _setTileDrawingEnabled:YES];
476 [document_ redrawScaledDocument];
477 }
478
479 - (void) setViewportWidth:(float)width {
480 width_ = width != 0 ? width : [[self class] defaultWidth];
481 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
482 }
483
484 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
485 [self _setTileDrawingEnabled:NO];
486 }
487
488 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
489 [self _setTileDrawingEnabled:YES];
490 }
491
492 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
493 [self _setTileDrawingEnabled:YES];
494 }
495
496 - (void) loadRequest:(NSURLRequest *)request {
497 error_ = false;
498
499 WebThreadLock();
500 [document_ loadRequest:request];
501 WebThreadUnlock();
502 }
503
504 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
505 [self loadRequest:[NSURLRequest
506 requestWithURL:url
507 cachePolicy:policy
508 timeoutInterval:30.0
509 ]];
510 }
511
512 - (void) loadURL:(NSURL *)url {
513 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
514 }
515
516 - (id) init {
517 CGRect frame = {{0, 0}, {320, 480}};
518 frame.size.height -= 20; //[[[$SBStatusBarController sharedStatusBarController] statusBarView] frame].size.height;
519
520 if ((self = [super initWithFrame:frame]) != nil) {
521 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
522
523 struct CGRect bounds([self bounds]);
524
525 scroller_ = [[objc_getClass(Wildcat_ ? "UIScrollView" : "UIScroller") alloc] initWithFrame:bounds];
526 [self addSubview:scroller_];
527
528 [scroller_ setFixedBackgroundPattern:YES];
529 [scroller_ setBackgroundColor:[UIColor blackColor]];
530
531 [scroller_ setScrollingEnabled:YES];
532 [scroller_ setClipsSubviews:YES];
533
534 if (!Wildcat_)
535 [scroller_ setAllowsRubberBanding:YES];
536
537 [scroller_ setDelegate:self];
538 [scroller_ setBounces:YES];
539
540 if (!Wildcat_) {
541 [scroller_ setScrollHysteresis:8];
542 [scroller_ setThumbDetectionEnabled:NO];
543 [scroller_ setDirectionalScrolling:YES];
544 //[scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
545 [scroller_ setEventMode:YES];
546 }
547
548 if (Wildcat_) {
549 UIScrollView *scroller((UIScrollView *)scroller_);
550 //[scroller setDirectionalLockEnabled:NO];
551 [scroller setDelaysContentTouches:NO];
552 //[scroller setScrollsToTop:NO];
553 //[scroller setCanCancelContentTouches:NO];
554 }
555
556 [scroller_ setShowBackgroundShadow:NO]; /* YES */
557 //[scroller_ setAllowsRubberBanding:YES]; /* Vertical */
558
559 if (!Wildcat_)
560 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
561
562 CGRect rect([scroller_ bounds]);
563 //rect.size.height = 0;
564
565 WebThreadLock();
566
567 document_ = [[$UIWebBrowserView alloc] initWithFrame:rect];
568 WebView *webview([document_ webView]);
569
570 [document_ setBackgroundColor:[UIColor blackColor]];
571 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
572 [document_ setDrawsBackground:NO];
573 [webview setDrawsBackground:NO];
574
575 [webview setPreferencesIdentifier:@"WebCycript"];
576
577 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
578
579 if ([document_ respondsToSelector:@selector(enableReachability)])
580 [document_ enableReachability];
581 if ([document_ respondsToSelector:@selector(setAllowsMessaging:)])
582 [document_ setAllowsMessaging:YES];
583 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
584 [document_ useSelectionAssistantWithMode:0];
585
586 [document_ setTilingEnabled:YES];
587 [document_ setDrawsGrid:NO];
588 [document_ setLogsTilingChanges:NO];
589 [document_ setTileMinificationFilter:kCAFilterNearest];
590
591 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
592 /* XXX: abstractify */
593 [document_ setDataDetectorTypes:0x80000000];
594 else
595 [document_ setDetectsPhoneNumbers:NO];
596
597 [document_ setAutoresizes:YES];
598
599 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
600 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
601 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
602 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
603
604 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
605
606 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
607 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
608 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
609
610 [document_ _setDocumentType:0x4];
611
612 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
613 [document_ setZoomsFocusedFormControl:YES];
614 [document_ setContentsPosition:7];
615 [document_ setEnabledGestures:0xa];
616 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
617 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
618
619 [document_ setSmoothsFonts:YES];
620 [document_ setAllowsImageSheet:YES];
621 [webview _setUsesLoaderCache:YES];
622
623 [webview setGroupName:@"CydgetGroup"];
624
625 WebPreferences *preferences([webview preferences]);
626
627 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
628 [webview _setLayoutInterval:0];
629 else
630 [preferences _setLayoutInterval:0];
631
632 [self setViewportWidth:0];
633
634 [document_ setDelegate:self];
635 [document_ setGestureDelegate:self];
636
637 if ([document_ respondsToSelector:@selector(setFormEditingDelegate:)])
638 [document_ setFormEditingDelegate:self];
639 [document_ setInteractionDelegate:self];
640
641 [scroller_ addSubview:document_];
642
643 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
644
645 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
646
647 [webview setFrameLoadDelegate:indirect_];
648 [webview setPolicyDelegate:indirect_];
649 [webview setResourceLoadDelegate:indirect_];
650 [webview setUIDelegate:indirect_];
651
652 /* XXX: do not turn this on under penalty of extreme pain */
653 [webview setScriptDebugDelegate:nil];
654
655 WebThreadUnlock();
656
657 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
658 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
659 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
660
661 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
662 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
663
664 NSDictionary *configuration([$CydgetController currentConfiguration]);
665
666 cycript_ = [configuration objectForKey:@"CycriptURLs"];
667
668 scrollable_ = [[configuration objectForKey:@"Scrollable"] boolValue];
669 [scroller_ setScrollingEnabled:scrollable_];
670
671 NSString *homepage([configuration objectForKey:@"Homepage"]);
672 [self loadURL:[NSURL URLWithString:homepage]];
673
674 /*[UIKeyboard initImplementationNow];
675 CGSize keysize = [UIKeyboard defaultSize];
676 CGRect keyrect = {{0, [self bounds].size.height - 100}, keysize};
677 keyboard_ = [[UIKeyboard alloc] initWithFrame:keyrect];
678 [self addSubview:keyboard_];
679
680 [self addSubview:[[UITextView alloc] initWithFrame:CGRectMake(200, 0, 100, 100)]];
681 [self addSubview:[[UITextView alloc] initWithFrame:CGRectMake(200, 150, 100, 100)]];
682 [self addSubview:[[UITextView alloc] initWithFrame:CGRectMake(200, 300, 100, 100)]];*/
683 } return self;
684 }
685
686 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
687 [self retain];
688
689 UIActionSheet *sheet = [[[UIActionSheet alloc]
690 initWithTitle:nil
691 buttons:[NSArray arrayWithObjects:@"OK", nil]
692 defaultButtonIndex:0
693 delegate:self
694 context:@"alert"
695 ] autorelease];
696
697 [sheet setBodyText:message];
698 [sheet popupAlertAnimated:YES];
699 }
700
701 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
702 [self retain];
703
704 UIActionSheet *sheet = [[[UIActionSheet alloc]
705 initWithTitle:nil
706 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
707 defaultButtonIndex:0
708 delegate:indirect_
709 context:@"confirm"
710 ] autorelease];
711
712 [sheet setNumberOfRows:1];
713 [sheet setBodyText:message];
714 [sheet popupAlertAnimated:YES];
715
716 NSRunLoop *loop([NSRunLoop currentRunLoop]);
717 NSDate *future([NSDate distantFuture]);
718
719 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
720
721 NSNumber *confirm([confirm_ autorelease]);
722 confirm_ = nil;
723
724 [self autorelease];
725 return [confirm boolValue];
726 }
727
728 /* XXX: WebThreadLock? */
729 - (void) _fixScroller:(CGRect)bounds {
730 float extra;
731 if (!editing_ || $UIFormAssistant == nil)
732 extra = 0;
733 else {
734 UIFormAssistant *assistant([$UIFormAssistant sharedFormAssistant]);
735 CGRect peripheral([assistant peripheralFrame]);
736 extra = peripheral.size.height;
737 }
738
739 CGRect subrect([scroller_ frame]);
740 subrect.size.height -= [TPBottomLockBar defaultHeight];
741 subrect.size.height -= extra;
742
743 if ([scroller_ respondsToSelector:@selector(setScrollerIndicatorSubrect:)])
744 [scroller_ setScrollerIndicatorSubrect:subrect];
745
746 [document_ setValue:[NSValue valueWithSize:NSMakeSize(subrect.size.width, subrect.size.height)] forGestureAttribute:UIGestureAttributeVisibleSize];
747
748 CGSize size(size_);
749 size.height += extra;
750 size.height += [TPBottomLockBar defaultHeight];
751 [scroller_ setContentSize:size];
752
753 if ([scroller_ respondsToSelector:@selector(releaseRubberBandIfNecessary)])
754 [scroller_ releaseRubberBandIfNecessary];
755 }
756
757 - (void) fixScroller {
758 CGRect bounds([document_ documentBounds]);
759 [self _fixScroller:bounds];
760 }
761
762 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
763 size_ = frame.size;
764 [self _fixScroller:frame];
765 }
766
767 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
768 [self view:sender didSetFrame:frame];
769 }
770
771 - (void) formAssistant:(id)sender didBeginEditingFormNode:(id)node {
772 }
773
774 - (void) formAssistant:(id)sender didEndEditingFormNode:(id)node {
775 [self fixScroller];
776 }
777
778 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
779 editing_ = true;
780 }
781
782 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
783 [self fixScroller];
784 }
785
786 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
787 editing_ = false;
788 [self fixScroller];
789 }
790
791 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
792 if (cycript_ != nil)
793 if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
794 if (Pcre([cycript_ UTF8String], 0 /*XXX:PCRE_UTF8*/)(href))
795 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
796 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
797 WebView *webview([document_ webView]);
798 WebFrame *frame([webview mainFrame]);
799 JSGlobalContextRef context([frame globalContext]);
800 CYSetupContext(context);
801 }
802 }
803
804 - (bool) isLoading {
805 return [loading_ count] != 0;
806 }
807
808 - (void) reloadButtons {
809 if ([self isLoading]) {
810 [UIApp setNetworkActivityIndicatorVisible:YES];
811 [indicator_ startAnimation];
812 } else {
813 [UIApp setNetworkActivityIndicatorVisible:NO];
814 [indicator_ stopAnimation];
815 }
816 }
817
818 - (void) _finishLoading {
819 size_t count([loading_ count]);
820 /*if (count == 0)
821 [self autorelease];*/
822 if (reloading_ || count != 0)
823 return;
824 [self reloadButtons];
825 }
826
827 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
828 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
829 }
830
831 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
832 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
833 }
834
835 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
836 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
837 }
838
839 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
840 return [document_ webView:sender didCommitLoadForFrame:frame];
841 }
842
843 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
844 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
845 }
846
847 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
848 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
849 [self _finishLoading];
850 return [document_ webView:sender didFinishLoadForFrame:frame];
851 }
852
853 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
854 /*if ([loading_ count] == 0)
855 [self retain];*/
856 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
857
858 if ([frame parentFrame] == nil) {
859 [document_ resignFirstResponder];
860
861 reloading_ = false;
862
863 if (Wildcat_) {
864 CGRect webrect = [scroller_ bounds];
865 webrect.size.height = 1;
866 [document_ setFrame:webrect];
867 }
868
869 if ([scroller_ respondsToSelector:@selector(scrollPointVisibleAtTopLeft:)])
870 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
871 else
872 [scroller_ scrollRectToVisible:CGRectZero animated:NO];
873
874 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
875 [scroller_ setZoomScale:1 duration:0];
876 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
877 [scroller_ _setZoomScale:1 duration:0];
878 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
879 [scroller_ setZoomScale:1 animated:NO];*/
880
881 if (!Wildcat_) {
882 CGRect webrect = [scroller_ bounds];
883 webrect.size.height = 1;
884 [document_ setFrame:webrect];
885 }
886 }
887
888 [self reloadButtons];
889 }
890
891 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
892 /*if ([frame parentFrame] == nil)
893 [self autorelease];*/
894
895 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
896 [self _finishLoading];
897
898 if (reloading_)
899 return;
900
901 if ([frame parentFrame] == nil) {
902 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
903 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
904 [[error localizedDescription] stringByAddingPercentEscapes]
905 ]]];
906
907 error_ = true;
908 }
909 }
910
911 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
912 [self _didFailWithError:error forFrame:frame];
913 if ([document_ respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
914 [document_ webView:sender didFailLoadWithError:error forFrame:frame];
915 }
916
917 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
918 [self _didFailWithError:error forFrame:frame];
919 }
920
921 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
922 NSLog(@"Console:%@\n", [dictionary description]);
923 }
924
925 @end
926
927 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
928 }
929
930 @end
931
932 #include <string>
933
934 struct State {
935 unsigned state;
936 };
937
938 // State Machine {{{
939 static bool cycript_;
940 static bool jscript_;
941
942 struct ParserSet {
943 bool backup_;
944
945 ParserSet() {
946 backup_ = jscript_;
947 jscript_ = true;
948 cycript_ = false;
949 }
950
951 ~ParserSet() {
952 jscript_ = backup_;
953 }
954 };
955
956 MSHook(State, _ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, void *_this, State state) {
957 ParserSet set;
958 return __ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE(_this, state);
959 }
960
961 MSHook(void, _ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, void *_this, void *resource) {
962 ParserSet set;
963 return __ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE(_this, resource);
964 }
965
966 MSHook(void, _ZN7WebCore13HTMLTokenizer29executeExternalScriptsIfReadyEv, void *_this) {
967 ParserSet set;
968 return __ZN7WebCore13HTMLTokenizer29executeExternalScriptsIfReadyEv(_this);
969 }
970
971 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
972 if (!jscript_ || mime != "text/cycript")
973 return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
974
975 static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
976 if (handle == NULL)
977 return false;
978
979 cycript_ = true;
980 return true;
981 }
982 // }}}
983 // Script Compiler {{{
984 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
985 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
986 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")))
987 CYParseUChar(pool, &data, &size);
988 }
989
990 static void (*_ZN7WebCore6String6appendEPKtj)(WebCore::String *, const UChar *, unsigned);
991 static void (*_ZN7WebCore6String8truncateEj)(WebCore::String *, unsigned);
992
993 static void Cycriptify(const WebCore::String &source, int *psize = NULL) {
994 const uint16_t *data(source.characters());
995 size_t length(source.length()), size(length);
996
997 apr_pool_t *pool;
998 apr_pool_create(&pool, NULL);
999
1000 Cycriptify(pool, data, size);
1001
1002 WebCore::String &script(const_cast<WebCore::String &>(source));
1003
1004 _ZN7WebCore6String8truncateEj(&script, 0);
1005 _ZN7WebCore6String6appendEPKtj(&script, data, size);
1006
1007 if (psize != NULL)
1008 *psize = size;
1009
1010 apr_pool_destroy(pool);
1011 }
1012 // }}}
1013
1014 extern "C" void *_ZN3JSC7UString3Rep14nullBaseStringE __attribute__((__weak_import__));
1015 extern "C" void *_ZN3JSC7UString3Rep7destroyEv __attribute__((__weak_import__));
1016 extern "C" void *_ZN3JSC7UStringC1EPKti __attribute__((__weak_import__));
1017 extern "C" void *_ZN3JSC7UStringC1EPKc __attribute__((__weak_import__));
1018 extern "C" void *_ZNK3JSC7UString6substrEii __attribute__((__weak_import__));
1019 extern "C" void *_ZN3WTF10fastMallocEm __attribute__((__weak_import__));
1020 extern "C" void WTFReportAssertionFailure(const char *, int, const char *, const char *) __attribute__((__weak_import__));
1021 extern "C" void *_ZN3WTF8fastFreeEPv __attribute__((__weak_import__));
1022
1023 bool CYWeakHell() {
1024 return
1025 &_ZN3JSC7UString3Rep14nullBaseStringE == NULL ||
1026 &_ZN3JSC7UString3Rep7destroyEv == NULL ||
1027 &_ZN3JSC7UStringC1EPKti == NULL ||
1028 &_ZN3JSC7UStringC1EPKc == NULL ||
1029 &_ZNK3JSC7UString6substrEii == NULL ||
1030 &_ZN3WTF10fastMallocEm == NULL ||
1031 &WTFReportAssertionFailure == NULL ||
1032 &_ZN3WTF8fastFreeEPv == NULL ||
1033 false;
1034 }
1035
1036 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
1037 if (!cycript_)
1038 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
1039 else {
1040 cycript_ = false;
1041
1042 JSC::SourceCode *source(*_this);
1043 const uint16_t *data(source->data());
1044 size_t size(source->length());
1045
1046 apr_pool_t *pool;
1047 apr_pool_create(&pool, NULL);
1048
1049 Cycriptify(pool, data, size);
1050 source->~SourceCode();
1051 new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
1052
1053 apr_pool_destroy(pool);
1054
1055 __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
1056 }
1057 }
1058
1059 MSHook(void, _ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, void *_this, int start, const UChar *code, unsigned length, int *source, int *line, JSC::UString *message) {
1060 if (!cycript_)
1061 return __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, code, length, source, line, message);
1062 else {
1063 const uint16_t *data(code);
1064 size_t size(length);
1065
1066 apr_pool_t *pool;
1067 apr_pool_create(&pool, NULL);
1068
1069 Cycriptify(pool, data, size);
1070 __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, data, size, source, line, message);
1071
1072 apr_pool_destroy(pool);
1073 }
1074 }
1075
1076 static WebCore::String *string;
1077
1078 MSHook(void, _ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, void *_this, const WebCore::String &source, const WebCore::KURL &url, int line) {
1079 if (cycript_)
1080 Cycriptify(source);
1081 return __ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi(_this, source, url, line);
1082 }
1083
1084 /*void Log(const WebCore::String &string) {
1085 size_t length(string.length());
1086 UChar data[length + 1];
1087 data[length] = 0;
1088 memcpy(data, string.characters(), length * 2);
1089 NSLog(@"wtf :%S:", data);
1090 }*/
1091
1092 MSHook(const WebCore::String &, _ZN7WebCore12CachedScript6scriptEv, void *_this) {
1093 const WebCore::String &script(__ZN7WebCore12CachedScript6scriptEv(_this));
1094
1095 //NSLog(@"wtf evil %s %s -- %p %p", jscript_ ? "true" : "false", cycript_ ? "true" : "false", _this, &script);
1096 //Log(script);
1097
1098 string = const_cast<WebCore::String *>(&script);
1099 return script;
1100 }
1101
1102 MSHook(State, _ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, void *_this, void *source, State state) {
1103 //NSLog(@"wtf tree %s %s %p", jscript_ ? "true" : "false", cycript_ ? "true" : "false", string);
1104
1105 if (cycript_ && string != NULL)
1106 Cycriptify(*string, reinterpret_cast<int *>(source) + 3);
1107
1108 cycript_ = false;
1109 string = NULL;
1110
1111 return __ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE(_this, source, state);
1112 }
1113
1114 /* Cydget:// Protocol {{{ */
1115 @interface CydgetURLProtocol : NSURLProtocol {
1116 }
1117
1118 @end
1119
1120 @implementation CydgetURLProtocol
1121
1122 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
1123 NSURL *url([request URL]);
1124 if (url == nil)
1125 return NO;
1126 NSString *scheme([[url scheme] lowercaseString]);
1127 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
1128 return NO;
1129 return YES;
1130 }
1131
1132 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
1133 return request;
1134 }
1135
1136 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
1137 id<NSURLProtocolClient> client([self client]);
1138 if (icon == nil)
1139 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
1140 else {
1141 NSData *data(UIImagePNGRepresentation(icon));
1142
1143 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
1144 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
1145 [client URLProtocol:self didLoadData:data];
1146 [client URLProtocolDidFinishLoading:self];
1147 }
1148 }
1149
1150 - (void) startLoading {
1151 id<NSURLProtocolClient> client([self client]);
1152 NSURLRequest *request([self request]);
1153
1154 NSURL *url([request URL]);
1155 NSString *href([url absoluteString]);
1156
1157 NSString *path([href substringFromIndex:9]);
1158 NSRange slash([path rangeOfString:@"/"]);
1159
1160 NSString *command;
1161 if (slash.location == NSNotFound) {
1162 command = path;
1163 path = nil;
1164 } else {
1165 command = [path substringToIndex:slash.location];
1166 path = [path substringFromIndex:(slash.location + 1)];
1167 }
1168
1169 if ([command isEqualToString:@"_UIImageWithName"]) {
1170 if (path == nil)
1171 goto fail;
1172 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
1173 UIImage *icon(_UIImageWithName(path));
1174 [self _returnPNGWithImage:icon forRequest:request];
1175 } else fail: {
1176 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
1177 }
1178 }
1179
1180 - (void) stopLoading {
1181 }
1182
1183 @end
1184 /* }}} */
1185 /* Cydget-CGI:// Protocol {{{ */
1186 @interface CydgetCGIURLProtocol : NSURLProtocol {
1187 pid_t pid_;
1188 CFHTTPMessageRef http_;
1189 NSFileHandle *handle_;
1190 }
1191
1192 @end
1193
1194 @implementation CydgetCGIURLProtocol
1195
1196 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
1197 NSURL *url([request URL]);
1198 if (url == nil)
1199 return NO;
1200 NSString *scheme([[url scheme] lowercaseString]);
1201 if (scheme == nil || ![scheme isEqualToString:@"cydget-cgi"])
1202 return NO;
1203 return YES;
1204 }
1205
1206 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
1207 return request;
1208 }
1209
1210 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
1211 if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
1212 pid_ = -1;
1213 } return self;
1214 }
1215
1216 - (void) startLoading {
1217 id<NSURLProtocolClient> client([self client]);
1218 NSURLRequest *request([self request]);
1219 NSURL *url([request URL]);
1220
1221 NSString *path([url path]);
1222 if (path == nil) {
1223 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
1224 return;
1225 }
1226
1227 NSFileManager *manager([NSFileManager defaultManager]);
1228 if (![manager fileExistsAtPath:path]) {
1229 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
1230 return;
1231 }
1232
1233 int fds[2];
1234 _assert(pipe(fds) != -1);
1235
1236 _assert(pid_ == -1);
1237 pid_ = fork();
1238 if (pid_ == -1) {
1239 _assert(close(fds[0]) != -1);
1240 _assert(close(fds[1]) != -1);
1241 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
1242 return;
1243 }
1244
1245 if (pid_ == 0) {
1246 const char *script([path UTF8String]);
1247
1248 setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
1249 setenv("SCRIPT_FILENAME", script, true);
1250 NSString *query([url query]);
1251 if (query != nil)
1252 setenv("QUERY_STRING", [query UTF8String], true);
1253
1254 _assert(dup2(fds[1], 1) != -1);
1255 _assert(close(fds[0]) != -1);
1256 _assert(close(fds[1]) != -1);
1257
1258 execl(script, script, NULL);
1259 exit(1);
1260 _assert(false);
1261 }
1262
1263 _assert(close(fds[1]) != -1);
1264
1265 _assert(http_ == NULL);
1266 http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
1267 CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
1268
1269 _assert(handle_ == nil);
1270 handle_ = [[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES];
1271
1272 [[NSNotificationCenter defaultCenter]
1273 addObserver:self
1274 selector:@selector(onRead:)
1275 name:@"NSFileHandleReadCompletionNotification"
1276 object:handle_
1277 ];
1278
1279 [handle_ readInBackgroundAndNotify];
1280 }
1281
1282 - (void) onRead:(NSNotification *)notification {
1283 NSFileHandle *handle([notification object]);
1284
1285 NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
1286
1287 if (size_t length = [data length]) {
1288 CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
1289 [handle readInBackgroundAndNotify];
1290 } else {
1291 id<NSURLProtocolClient> client([self client]);
1292
1293 CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
1294 if (mime == NULL)
1295 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
1296 else {
1297 NSURLRequest *request([self request]);
1298
1299 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
1300 CFRelease(mime);
1301
1302 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
1303
1304 CFDataRef body(CFHTTPMessageCopyBody(http_));
1305 [client URLProtocol:self didLoadData:(NSData *)body];
1306 CFRelease(body);
1307
1308 [client URLProtocolDidFinishLoading:self];
1309 }
1310
1311 CFRelease(http_);
1312 http_ = NULL;
1313 }
1314 }
1315
1316 //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
1317
1318 - (void) stopLoading_ {
1319 [[NSNotificationCenter defaultCenter] removeObserver:self];
1320
1321 if (handle_ != nil) {
1322 [handle_ release];
1323 handle_ = nil;
1324 }
1325
1326 if (pid_ != -1) {
1327 kill(pid_, SIGTERM);
1328 int status;
1329 _syscall(waitpid(pid_, &status, 0));
1330 pid_ = -1;
1331 }
1332 }
1333
1334 - (void) stopLoading {
1335 [self
1336 performSelectorOnMainThread:@selector(stopLoading_)
1337 withObject:nil
1338 waitUntilDone:NO
1339 ];
1340 }
1341
1342 @end
1343 /* }}} */
1344
1345 template <typename Type_>
1346 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
1347 struct nlist &name(nl[index]);
1348 uintptr_t value(name.n_value);
1349 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
1350 value |= 0x00000001;
1351 function = reinterpret_cast<Type_>(value);
1352 }
1353
1354 template <typename Type_>
1355 static void dlset(Type_ &function, const char *name) {
1356 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
1357 }
1358
1359 @implementation WebCycriptLockScreenController
1360
1361 + (void) initialize {
1362 iOS4 = kCFCoreFoundationVersionNumber >= 550.32;
1363
1364 $UIWebBrowserView = objc_getClass("UIWebBrowserView");
1365 if ($UIWebBrowserView == nil) {
1366 Wildcat_ = false;
1367 $UIWebBrowserView = objc_getClass("UIWebDocumentView");
1368 } else {
1369 Wildcat_ = true;
1370 }
1371
1372 int maxproc;
1373 size_t size(sizeof(maxproc));
1374 if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
1375 NSLog(@"sysctlbyname(\"kern.maxproc\", ?)");
1376 else if (maxproc < 72) {
1377 maxproc = 72;
1378 if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
1379 NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
1380 }
1381
1382 apr_initialize();
1383
1384 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
1385 [NSURLProtocol registerClass:[CydgetCGIURLProtocol class]];
1386
1387 if (!iOS4) {
1388 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
1389 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
1390 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
1391 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
1392 }
1393
1394 void (*_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE)(void *, int, const UChar *, unsigned, int *, int *, JSC::UString *);
1395 dlset(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, "_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE");
1396 if (_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE != NULL)
1397 MSHookFunction(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, MSHake(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE));
1398
1399 struct nlist nl[10];
1400 memset(nl, 0, sizeof(nl));
1401
1402 nl[0].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE";
1403 nl[1].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE";
1404 nl[2].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer29executeExternalScriptsIfReadyEv";
1405 nl[3].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
1406
1407 nl[4].n_un.n_name = (char *) "__ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi";
1408 nl[5].n_un.n_name = (char *) "__ZN7WebCore12CachedScript6scriptEv";
1409 nl[6].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE";
1410
1411 nl[7].n_un.n_name = (char *) "__ZN7WebCore6String6appendEPKtj";
1412 nl[8].n_un.n_name = (char *) "__ZN7WebCore6String8truncateEj";
1413
1414 nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
1415
1416 State (*_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE)(void *, State);
1417 nlset(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, nl, 0);
1418 MSHookFunction(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE));
1419
1420 void (*_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE)(void *, void *);
1421 nlset(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, nl, 1);
1422 if (_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE != NULL)
1423 MSHookFunction(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, MSHake(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE));
1424
1425 void (*_ZN7WebCore13HTMLTokenizer29executeExternalScriptsIfReadyEv)(void *);
1426 nlset(_ZN7WebCore13HTMLTokenizer29executeExternalScriptsIfReadyEv, nl, 2);
1427 if (_ZN7WebCore13HTMLTokenizer29executeExternalScriptsIfReadyEv != NULL)
1428 MSHookFunction(_ZN7WebCore13HTMLTokenizer29executeExternalScriptsIfReadyEv, MSHake(_ZN7WebCore13HTMLTokenizer29executeExternalScriptsIfReadyEv));
1429
1430 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
1431 nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 3);
1432 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
1433
1434 if (iOS4) {
1435 void (*_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi)(void *, const WebCore::String &, const WebCore::KURL &, int);
1436 nlset(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, nl, 4);
1437 MSHookFunction(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi, MSHake(_ZN7WebCore16ScriptSourceCodeC2ERKNS_6StringERKNS_4KURLEi));
1438
1439 const WebCore::String &(*_ZN7WebCore12CachedScript6scriptEv)(void *);
1440 nlset(_ZN7WebCore12CachedScript6scriptEv, nl, 5);
1441 MSHookFunction(_ZN7WebCore12CachedScript6scriptEv, MSHake(_ZN7WebCore12CachedScript6scriptEv));
1442
1443 State (*_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE)(void *, void *, State);
1444 nlset(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, nl, 6);
1445 MSHookFunction(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer15scriptExecutionERKNS_16ScriptSourceCodeENS0_5StateE));
1446 }
1447
1448 nlset(_ZN7WebCore6String6appendEPKtj, nl, 7);
1449 nlset(_ZN7WebCore6String8truncateEj, nl, 8);
1450 }
1451
1452 + (id) rootViewController {
1453 return [[[self alloc] init] autorelease];
1454 }
1455
1456 - (void) loadView {
1457 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];
1458 }
1459
1460 @end