1 /* CydgetScript - open-source IntelliDial replacement
2 * Copyright (C) 2009 Jay Freeman (saurik)
6 * Redistribution and use in source and binary
7 * forms, with or without modification, are permitted
8 * provided that the following conditions are met:
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
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.
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.
38 #include <substrate.h>
39 #include <sys/sysctl.h>
41 #import <GraphicsServices/GraphicsServices.h>
42 #import <UIKit/UIKit.h>
43 #import <AddressBook/AddressBook.h>
45 #import <SpringBoardUI/SBAwayViewPluginController.h>
46 #import <TelephonyUI/TPBottomLockBar.h>
48 #import <QuartzCore/CALayer.h>
49 // XXX: fix the minimum requirement
50 extern NSString * const kCAFilterNearest;
52 #include <WebKit/DOMCSSPrimitiveValue.h>
53 #include <WebKit/DOMCSSStyleDeclaration.h>
54 #include <WebKit/DOMDocument.h>
55 #include <WebKit/DOMHTMLBodyElement.h>
56 #include <WebKit/DOMNodeList.h>
57 #include <WebKit/DOMRGBColor.h>
59 #include <WebKit/WebFrame.h>
60 #include <WebKit/WebPolicyDelegate.h>
61 #include <WebKit/WebPreferences.h>
62 #include <WebKit/WebScriptObject.h>
64 #import <WebKit/WebView.h>
65 #import <WebKit/WebView-WebPrivate.h>
67 #include <WebCore/Page.h>
68 #include <WebCore/Settings.h>
70 #include <WebCore/WebCoreThread.h>
71 #include <WebKit/WebPreferences-WebPrivate.h>
73 #include "JSGlobalData.h"
74 #include "SourceCode.h"
76 #include <apr-1/apr_pools.h>
79 @interface WebView (UICaboodle)
80 - (void) setScriptDebugDelegate:(id)delegate;
81 - (void) _setFormDelegate:(id)delegate;
82 - (void) _setUIKitDelegate:(id)delegate;
83 - (void) setWebMailDelegate:(id)delegate;
84 - (void) _setLayoutInterval:(float)interval;
88 #define _forever for (;;)
90 _disused static unsigned trace_;
92 #define _trace() do { \
93 NSLog(@"_trace(%u)@%s:%u[%s]\n", \
94 trace_++, __FILE__, __LINE__, __FUNCTION__\
98 #define _assert(test) do \
100 fprintf(stderr, "_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
105 #define _syscall(expr) \
106 do if ((long) (expr) != -1) \
108 else switch (errno) { \
115 @protocol CydgetController
116 - (NSDictionary *) currentConfiguration;
119 static Class $CydgetController(objc_getClass("CydgetController"));
121 @interface NSString (UIKit)
122 - (NSString *) stringByAddingPercentEscapes;
125 @implementation UIWebDocumentView (WebCycript)
127 - (void) _setScrollerOffset:(CGPoint)offset {
128 UIScroller *scroller([self _scroller]);
130 CGSize size([scroller contentSize]);
131 CGSize bounds([scroller bounds].size);
134 max.x = size.width - bounds.width;
135 max.y = size.height - bounds.height;
143 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
144 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
146 [scroller setOffset:offset];
151 /* Perl-Compatible RegEx {{{ */
161 Pcre(const char *regex, int options = 0) :
166 code_ = pcre_compile(regex, options, &error, &offset, NULL);
169 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** Pcre(,): [%u] %s", offset, error] userInfo:nil];
171 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
172 matches_ = new int[(capture_ + 1) * 3];
180 NSString *operator [](size_t match) {
181 return [[[NSString alloc] initWithBytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2]) encoding:NSUTF8StringEncoding] autorelease];
184 bool operator ()(NSString *data) {
185 // XXX: length is for characters, not for bytes
186 return operator ()([data UTF8String], [data length]);
189 bool operator ()(const char *data, size_t size) {
191 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
195 /* WebCycript Delegate {{{ */
196 @interface WebCycriptDelegate : NSObject {
197 _transient volatile id delegate_;
200 - (void) setDelegate:(id)delegate;
201 - (id) initWithDelegate:(id)delegate;
204 @implementation WebCycriptDelegate
206 - (void) setDelegate:(id)delegate {
207 delegate_ = delegate;
210 - (id) initWithDelegate:(id)delegate {
211 delegate_ = delegate;
215 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
216 if (delegate_ != nil)
217 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
220 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
221 if (delegate_ != nil)
222 return [delegate_ webView:sender didCommitLoadForFrame:frame];
225 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
226 if (delegate_ != nil)
227 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
230 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
231 if (delegate_ != nil)
232 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
235 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
236 if (delegate_ != nil)
237 return [delegate_ webView:sender didFinishLoadForFrame:frame];
240 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
241 if (delegate_ != nil)
242 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
245 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
246 if (delegate_ != nil)
247 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
250 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
251 if (delegate_ != nil)
252 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
255 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
256 if (delegate_ != nil)
257 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
261 - (IMP) methodForSelector:(SEL)sel {
262 if (IMP method = [super methodForSelector:sel])
264 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
268 - (BOOL) respondsToSelector:(SEL)sel {
269 if ([super respondsToSelector:sel])
271 // XXX: WebThreadCreateNSInvocation returns nil
272 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
273 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
276 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
277 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
279 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
280 if (delegate_ != nil)
281 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
283 // XXX: I fucking hate Apple so very very bad
284 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
287 - (void) forwardInvocation:(NSInvocation *)inv {
288 SEL sel = [inv selector];
289 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
290 [inv invokeWithTarget:delegate_];
296 @interface WebCydgetLockScreenView : UIView {
297 WebCycriptDelegate *indirect_;
298 UIProgressIndicator *indicator_;
299 UIScroller *scroller_;
300 UIWebDocumentView *document_;
311 NSMutableSet *loading_;
315 //UIKeyboard *keyboard_;
320 @implementation WebCydgetLockScreenView
322 //#include "UICaboodle/UCInternal.h"
327 WebView *webview([document_ webView]);
328 [webview setFrameLoadDelegate:nil];
329 [webview setResourceLoadDelegate:nil];
330 [webview setUIDelegate:nil];
331 [webview setScriptDebugDelegate:nil];
332 [webview setPolicyDelegate:nil];
334 /* XXX: these are set by UIWebDocumentView
335 [webview setDownloadDelegate:nil];
336 [webview _setFormDelegate:nil];
337 [webview _setUIKitDelegate:nil];
338 [webview setEditingDelegate:nil];*/
340 /* XXX: no one sets this, ever
341 [webview setWebMailDelegate:nil];*/
343 [document_ setDelegate:nil];
344 [document_ setGestureDelegate:nil];
345 [document_ setFormEditingDelegate:nil];
346 [document_ setInteractionDelegate:nil];
348 [indirect_ setDelegate:nil];
357 [scroller_ setDelegate:nil];
362 //[keyboard_ release];
365 [indicator_ release];
370 + (float) defaultWidth {
374 - (void) _setTileDrawingEnabled:(BOOL)enabled {
375 //[document_ setTileDrawingEnabled:enabled];
378 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
379 [self _setTileDrawingEnabled:NO];
382 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
383 [self _setTileDrawingEnabled:YES];
384 [document_ redrawScaledDocument];
387 - (void) setViewportWidth:(float)width {
388 width_ = width != 0 ? width : [[self class] defaultWidth];
389 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
392 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
393 [self _setTileDrawingEnabled:NO];
396 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
397 [self _setTileDrawingEnabled:YES];
400 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
401 [self _setTileDrawingEnabled:YES];
404 - (void) loadRequest:(NSURLRequest *)request {
408 [document_ loadRequest:request];
412 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
413 [self loadRequest:[NSURLRequest
420 - (void) loadURL:(NSURL *)url {
421 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
425 CGRect frame = {{0, 0}, {320, 480}};
426 frame.size.height -= GSDefaultStatusBarHeight();
428 if ((self = [super initWithFrame:frame]) != nil) {
429 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
431 struct CGRect bounds([self bounds]);
433 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
434 [self addSubview:scroller_];
436 [scroller_ setFixedBackgroundPattern:YES];
437 [scroller_ setBackgroundColor:[UIColor blackColor]];
439 [scroller_ setScrollingEnabled:YES];
440 [scroller_ setClipsSubviews:YES];
441 [scroller_ setAllowsRubberBanding:YES];
443 [scroller_ setDelegate:self];
444 [scroller_ setBounces:YES];
445 [scroller_ setScrollHysteresis:8];
446 [scroller_ setThumbDetectionEnabled:NO];
447 [scroller_ setDirectionalScrolling:YES];
448 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
449 [scroller_ setEventMode:YES];
450 [scroller_ setShowBackgroundShadow:NO]; /* YES */
451 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
452 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
454 CGRect rect([scroller_ bounds]);
455 //rect.size.height = 0;
459 document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
460 WebView *webview([document_ webView]);
462 [document_ setBackgroundColor:[UIColor blackColor]];
463 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
464 [document_ setDrawsBackground:NO];
465 [webview setDrawsBackground:NO];
467 [webview setPreferencesIdentifier:@"WebCycript"];
469 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
471 if ([document_ respondsToSelector:@selector(enableReachability)])
472 [document_ enableReachability];
474 [document_ setAllowsMessaging:YES];
476 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
477 [document_ useSelectionAssistantWithMode:0];
479 [document_ setTilingEnabled:YES];
480 [document_ setDrawsGrid:NO];
481 [document_ setLogsTilingChanges:NO];
482 [document_ setTileMinificationFilter:kCAFilterNearest];
484 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
485 /* XXX: abstractify */
486 [document_ setDataDetectorTypes:0x80000000];
488 [document_ setDetectsPhoneNumbers:NO];
490 [document_ setAutoresizes:YES];
492 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
493 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
494 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
495 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
497 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
499 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
500 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
501 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
503 [document_ _setDocumentType:0x4];
505 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
506 [document_ setZoomsFocusedFormControl:YES];
507 [document_ setContentsPosition:7];
508 [document_ setEnabledGestures:0xa];
509 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
510 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
512 [document_ setSmoothsFonts:YES];
513 [document_ setAllowsImageSheet:YES];
514 [webview _setUsesLoaderCache:YES];
516 [webview setGroupName:@"CydgetGroup"];
518 WebPreferences *preferences([webview preferences]);
520 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
521 [webview _setLayoutInterval:0];
523 [preferences _setLayoutInterval:0];
525 [self setViewportWidth:0];
527 [document_ setDelegate:self];
528 [document_ setGestureDelegate:self];
529 [document_ setFormEditingDelegate:self];
530 [document_ setInteractionDelegate:self];
532 [scroller_ addSubview:document_];
534 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
536 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
538 [webview setFrameLoadDelegate:indirect_];
539 [webview setPolicyDelegate:indirect_];
540 [webview setResourceLoadDelegate:indirect_];
541 [webview setUIDelegate:indirect_];
543 /* XXX: do not turn this on under penalty of extreme pain */
544 [webview setScriptDebugDelegate:nil];
548 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
549 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
550 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
552 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
553 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
555 NSDictionary *configuration([$CydgetController currentConfiguration]);
557 cycript_ = [configuration objectForKey:@"CycriptURLs"];
559 scrollable_ = [[configuration objectForKey:@"Scrollable"] boolValue];
560 [scroller_ setScrollingEnabled:scrollable_];
562 NSString *homepage([configuration objectForKey:@"Homepage"]);
563 [self loadURL:[NSURL URLWithString:homepage]];
565 /*[UIKeyboard initImplementationNow];
566 CGSize keysize = [UIKeyboard defaultSize];
567 CGRect keyrect = {{0, [self bounds].size.height - 100}, keysize};
568 keyboard_ = [[UIKeyboard alloc] initWithFrame:keyrect];
569 [self addSubview:keyboard_];
571 [self addSubview:[[UITextView alloc] initWithFrame:CGRectMake(200, 0, 100, 100)]];
572 [self addSubview:[[UITextView alloc] initWithFrame:CGRectMake(200, 150, 100, 100)]];
573 [self addSubview:[[UITextView alloc] initWithFrame:CGRectMake(200, 300, 100, 100)]];*/
577 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
580 UIActionSheet *sheet = [[[UIActionSheet alloc]
582 buttons:[NSArray arrayWithObjects:@"OK", nil]
588 [sheet setBodyText:message];
589 [sheet popupAlertAnimated:YES];
592 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
595 UIActionSheet *sheet = [[[UIActionSheet alloc]
597 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
603 [sheet setNumberOfRows:1];
604 [sheet setBodyText:message];
605 [sheet popupAlertAnimated:YES];
607 NSRunLoop *loop([NSRunLoop currentRunLoop]);
608 NSDate *future([NSDate distantFuture]);
610 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
612 NSNumber *confirm([confirm_ autorelease]);
616 return [confirm boolValue];
619 /* XXX: WebThreadLock? */
620 - (void) _fixScroller:(CGRect)bounds {
625 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
626 CGRect peripheral([assistant peripheralFrame]);
627 extra = peripheral.size.height;
630 CGRect subrect([scroller_ frame]);
631 subrect.size.height -= [TPBottomLockBar defaultHeight];
632 subrect.size.height -= extra;
633 [scroller_ setScrollerIndicatorSubrect:subrect];
636 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
637 [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
640 size.height += extra;
641 size.height += [TPBottomLockBar defaultHeight];
642 [scroller_ setContentSize:size];
644 [scroller_ releaseRubberBandIfNecessary];
647 - (void) fixScroller {
648 CGRect bounds([document_ documentBounds]);
649 [self _fixScroller:bounds];
652 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
654 [self _fixScroller:frame];
657 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
658 [self view:sender didSetFrame:frame];
661 - (void) formAssistant:(id)sender didBeginEditingFormNode:(id)node {
664 - (void) formAssistant:(id)sender didEndEditingFormNode:(id)node {
668 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
672 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
676 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
681 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
683 if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
684 if (Pcre([cycript_ UTF8String], 0 /*XXX:PCRE_UTF8*/)(href))
685 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
686 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
687 WebView *webview([document_ webView]);
688 WebFrame *frame([webview mainFrame]);
689 JSGlobalContextRef context([frame globalContext]);
690 CYSetupContext(context);
695 return [loading_ count] != 0;
698 - (void) reloadButtons {
699 if ([self isLoading]) {
700 [UIApp setNetworkActivityIndicatorVisible:YES];
701 [indicator_ startAnimation];
703 [UIApp setNetworkActivityIndicatorVisible:NO];
704 [indicator_ stopAnimation];
708 - (void) _finishLoading {
709 size_t count([loading_ count]);
711 [self autorelease];*/
712 if (reloading_ || count != 0)
714 [self reloadButtons];
717 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
718 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
721 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
722 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
725 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
726 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
729 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
730 return [document_ webView:sender didCommitLoadForFrame:frame];
733 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
734 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
737 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
738 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
739 [self _finishLoading];
740 return [document_ webView:sender didFinishLoadForFrame:frame];
743 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
744 /*if ([loading_ count] == 0)
746 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
748 if ([frame parentFrame] == nil) {
749 [document_ resignFirstResponder];
753 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
755 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
756 [scroller_ setZoomScale:1 duration:0];
757 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
758 [scroller_ _setZoomScale:1 duration:0];
759 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
760 [scroller_ setZoomScale:1 animated:NO];*/
762 CGRect rect([scroller_ bounds]);
763 //rect.size.height = 0;
764 [document_ setFrame:rect];
767 [self reloadButtons];
770 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
771 /*if ([frame parentFrame] == nil)
772 [self autorelease];*/
774 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
775 [self _finishLoading];
780 if ([frame parentFrame] == nil) {
781 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
782 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
783 [[error localizedDescription] stringByAddingPercentEscapes]
790 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
791 [self _didFailWithError:error forFrame:frame];
794 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
795 [self _didFailWithError:error forFrame:frame];
798 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
799 fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
804 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
811 static bool cycript_;
812 static bool jscript_;
814 static void SetParser(bool cycript, bool jscript) {
819 static bool GetParser0() {
823 static bool GetParser1() {
827 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
828 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
829 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")))
830 CYParseUChar(pool, &data, &size);
833 extern "C" void *_ZN3JSC7UString3Rep14nullBaseStringE __attribute__((__weak_import__));
834 extern "C" void *_ZN3JSC7UString3Rep7destroyEv __attribute__((__weak_import__));
835 extern "C" void *_ZN3JSC7UStringC1EPKti __attribute__((__weak_import__));
836 extern "C" void *_ZN3JSC7UStringC1EPKc __attribute__((__weak_import__));
837 extern "C" void *_ZNK3JSC7UString6substrEii __attribute__((__weak_import__));
838 extern "C" void *_ZN3WTF10fastMallocEm __attribute__((__weak_import__));
839 extern "C" void WTFReportAssertionFailure(const char *, int, const char *, const char *) __attribute__((__weak_import__));
840 extern "C" void *_ZN3WTF8fastFreeEPv __attribute__((__weak_import__));
844 &_ZN3JSC7UString3Rep14nullBaseStringE == NULL ||
845 &_ZN3JSC7UString3Rep7destroyEv == NULL ||
846 &_ZN3JSC7UStringC1EPKti == NULL ||
847 &_ZN3JSC7UStringC1EPKc == NULL ||
848 &_ZNK3JSC7UString6substrEii == NULL ||
849 &_ZN3WTF10fastMallocEm == NULL ||
850 &WTFReportAssertionFailure == NULL ||
851 &_ZN3WTF8fastFreeEPv == NULL ||
855 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
857 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
859 SetParser(false, true);
861 JSC::SourceCode *source(*_this);
862 const uint16_t *data(source->data());
863 size_t size(source->length());
866 apr_pool_create(&pool, NULL);
868 Cycriptify(pool, data, size);
869 source->~SourceCode();
870 new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
872 apr_pool_destroy(pool);
874 __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
878 MSHook(void, _ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, void *_this, int start, const UChar *code, unsigned length, int *source, int *line, JSC::UString *message) {
880 return __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, code, length, source, line, message);
882 const uint16_t *data(code);
886 apr_pool_create(&pool, NULL);
888 Cycriptify(pool, data, size);
889 __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, data, size, source, line, message);
891 apr_pool_destroy(pool);
899 MSHook(State, _ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, State state) {
900 SetParser(false, true);
901 state = __ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE(state);
902 SetParser(false, false);
906 MSHook(void, _ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, void *resource) {
907 SetParser(false, true);
908 __ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE(resource);
909 SetParser(false, false);
912 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
913 if (!GetParser1() || mime != "text/cycript")
914 return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
916 static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
920 SetParser(true, true);
924 /* Cydget:// Protocol {{{ */
925 @interface CydgetURLProtocol : NSURLProtocol {
930 @implementation CydgetURLProtocol
932 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
933 NSURL *url([request URL]);
936 NSString *scheme([[url scheme] lowercaseString]);
937 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
942 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
946 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
947 id<NSURLProtocolClient> client([self client]);
949 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
951 NSData *data(UIImagePNGRepresentation(icon));
953 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
954 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
955 [client URLProtocol:self didLoadData:data];
956 [client URLProtocolDidFinishLoading:self];
960 - (void) startLoading {
961 id<NSURLProtocolClient> client([self client]);
962 NSURLRequest *request([self request]);
964 NSURL *url([request URL]);
965 NSString *href([url absoluteString]);
967 NSString *path([href substringFromIndex:9]);
968 NSRange slash([path rangeOfString:@"/"]);
971 if (slash.location == NSNotFound) {
975 command = [path substringToIndex:slash.location];
976 path = [path substringFromIndex:(slash.location + 1)];
979 if ([command isEqualToString:@"_UIImageWithName"]) {
982 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
983 UIImage *icon(_UIImageWithName(path));
984 [self _returnPNGWithImage:icon forRequest:request];
986 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
990 - (void) stopLoading {
995 /* Cydget-PHP:// Protocol {{{ */
996 @interface CydgetPHPURLProtocol : NSURLProtocol {
998 CFHTTPMessageRef http_;
1003 @implementation CydgetPHPURLProtocol
1005 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
1006 NSURL *url([request URL]);
1009 NSString *scheme([[url scheme] lowercaseString]);
1010 if (scheme == nil || ![scheme isEqualToString:@"cydget-php"])
1015 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
1019 - (id) initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)response client:(id<NSURLProtocolClient>)client {
1020 if ((self = [super initWithRequest:request cachedResponse:response client:client]) != nil) {
1025 - (void) startLoading {
1026 id<NSURLProtocolClient> client([self client]);
1027 NSURLRequest *request([self request]);
1028 NSURL *url([request URL]);
1030 NSString *path([url path]);
1031 if (path == nil || ![path hasSuffix:@".php"]) {
1032 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
1036 NSFileManager *manager([NSFileManager defaultManager]);
1037 if (![manager fileExistsAtPath:path]) {
1038 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
1043 _assert(pipe(fds) != -1);
1045 _assert(pid_ == -1);
1048 _assert(close(fds[0]) != -1);
1049 _assert(close(fds[1]) != -1);
1050 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
1055 setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
1056 setenv("SCRIPT_FILENAME", [path UTF8String], true);
1057 NSString *query([url query]);
1059 setenv("QUERY_STRING", [query UTF8String], true);
1061 _assert(dup2(fds[1], 1) != -1);
1062 _assert(close(fds[0]) != -1);
1063 _assert(close(fds[1]) != -1);
1065 execl("/usr/bin/php-cgi", "php-cgi", NULL);
1070 _assert(close(fds[1]) != -1);
1072 _assert(http_ == NULL);
1073 http_ = CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE);
1074 CFHTTPMessageAppendBytes(http_, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
1076 NSFileHandle *handle([[NSFileHandle alloc] initWithFileDescriptor:fds[0] closeOnDealloc:YES]);
1078 [[NSNotificationCenter defaultCenter]
1080 selector:@selector(onRead:)
1081 name:@"NSFileHandleReadCompletionNotification"
1085 [handle readInBackgroundAndNotify];
1090 - (void) onRead:(NSNotification *)notification {
1091 NSFileHandle *handle([notification object]);
1093 NSData *data([[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]);
1095 if (size_t length = [data length]) {
1096 CFHTTPMessageAppendBytes(http_, reinterpret_cast<const UInt8 *>([data bytes]), length);
1097 [handle readInBackgroundAndNotify];
1099 id<NSURLProtocolClient> client([self client]);
1101 CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http_, CFSTR("Content-type")));
1103 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
1105 NSURLRequest *request([self request]);
1107 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
1110 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
1112 CFDataRef body(CFHTTPMessageCopyBody(http_));
1113 [client URLProtocol:self didLoadData:(NSData *)body];
1116 [client URLProtocolDidFinishLoading:self];
1124 //[client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
1126 - (void) stopLoading {
1128 kill(pid_, SIGTERM);
1130 _syscall(waitpid(pid_, &status, 0));
1138 template <typename Type_>
1139 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
1140 struct nlist &name(nl[index]);
1141 uintptr_t value(name.n_value);
1142 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
1143 value |= 0x00000001;
1144 function = reinterpret_cast<Type_>(value);
1147 template <typename Type_>
1148 static void dlset(Type_ &function, const char *name) {
1149 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
1152 @implementation WebCycriptLockScreenController
1154 + (void) initialize {
1156 size_t size(sizeof(maxproc));
1157 if (sysctlbyname("kern.maxproc", &maxproc, &size, NULL, 0) == -1)
1158 NSLog(@"sysctlbyname(\"kern.maxproc\", ?)");
1159 else if (maxproc < 72) {
1161 if (sysctlbyname("kern.maxproc", NULL, NULL, &maxproc, sizeof(maxproc)) == -1)
1162 NSLog(@"sysctlbyname(\"kern.maxproc\", #)");
1167 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
1168 [NSURLProtocol registerClass:[CydgetPHPURLProtocol class]];
1170 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
1171 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
1172 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
1173 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
1175 void (*_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE)(void *, int, const UChar *, unsigned, int *, int *, JSC::UString *);
1176 dlset(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, "_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE");
1177 if (_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE != NULL)
1178 MSHookFunction(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, MSHake(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE));
1181 memset(nl, 0, sizeof(nl));
1182 nl[0].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE";
1183 nl[1].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE";
1184 nl[2].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
1185 nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
1187 State (*_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE)(State);
1188 nlset(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, nl, 0);
1189 MSHookFunction(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE));
1191 void (*_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE)(void *);
1192 nlset(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, nl, 1);
1193 MSHookFunction(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, MSHake(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE));
1195 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
1196 nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 2);
1197 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
1200 + (id) rootViewController {
1201 return [[[self alloc] init] autorelease];
1205 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];