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