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>
40 #import <GraphicsServices/GraphicsServices.h>
41 #import <UIKit/UIKit.h>
42 #import <AddressBook/AddressBook.h>
44 #import <SpringBoardUI/SBAwayViewPluginController.h>
45 #import <TelephonyUI/TPBottomLockBar.h>
47 #import <QuartzCore/CALayer.h>
48 // XXX: fix the minimum requirement
49 extern NSString * const kCAFilterNearest;
51 #include <WebKit/DOMCSSPrimitiveValue.h>
52 #include <WebKit/DOMCSSStyleDeclaration.h>
53 #include <WebKit/DOMDocument.h>
54 #include <WebKit/DOMHTMLBodyElement.h>
55 #include <WebKit/DOMNodeList.h>
56 #include <WebKit/DOMRGBColor.h>
58 #include <WebKit/WebFrame.h>
59 #include <WebKit/WebPolicyDelegate.h>
60 #include <WebKit/WebPreferences.h>
61 #include <WebKit/WebScriptObject.h>
63 #import <WebKit/WebView.h>
64 #import <WebKit/WebView-WebPrivate.h>
66 #include <WebCore/Page.h>
67 #include <WebCore/Settings.h>
69 #include <WebCore/WebCoreThread.h>
70 #include <WebKit/WebPreferences-WebPrivate.h>
72 #include "JSGlobalData.h"
73 #include "SourceCode.h"
75 #include <apr-1/apr_pools.h>
78 @interface WebView (UICaboodle)
79 - (void) setScriptDebugDelegate:(id)delegate;
80 - (void) _setFormDelegate:(id)delegate;
81 - (void) _setUIKitDelegate:(id)delegate;
82 - (void) setWebMailDelegate:(id)delegate;
83 - (void) _setLayoutInterval:(float)interval;
87 #define _forever for (;;)
89 _disused static unsigned trace_;
91 #define _trace() do { \
92 NSLog(@"_trace(%u)@%s:%u[%s]\n", \
93 trace_++, __FILE__, __LINE__, __FUNCTION__\
97 @protocol CydgetController
98 - (NSDictionary *) currentConfiguration;
101 static Class $CydgetController(objc_getClass("CydgetController"));
103 @interface NSString (UIKit)
104 - (NSString *) stringByAddingPercentEscapes;
107 @implementation UIWebDocumentView (WebCycript)
109 - (void) _setScrollerOffset:(CGPoint)offset {
110 UIScroller *scroller([self _scroller]);
112 CGSize size([scroller contentSize]);
113 CGSize bounds([scroller bounds].size);
116 max.x = size.width - bounds.width;
117 max.y = size.height - bounds.height;
125 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
126 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
128 [scroller setOffset:offset];
133 /* Perl-Compatible RegEx {{{ */
143 Pcre(const char *regex, int options = 0) :
148 code_ = pcre_compile(regex, options, &error, &offset, NULL);
151 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** Pcre(,): [%u] %s", offset, error] userInfo:nil];
153 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
154 matches_ = new int[(capture_ + 1) * 3];
162 NSString *operator [](size_t match) {
163 return [[[NSString alloc] initWithBytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2]) encoding:NSUTF8StringEncoding] autorelease];
166 bool operator ()(NSString *data) {
167 // XXX: length is for characters, not for bytes
168 return operator ()([data UTF8String], [data length]);
171 bool operator ()(const char *data, size_t size) {
173 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
177 /* WebCycript Delegate {{{ */
178 @interface WebCycriptDelegate : NSObject {
179 _transient volatile id delegate_;
182 - (void) setDelegate:(id)delegate;
183 - (id) initWithDelegate:(id)delegate;
186 @implementation WebCycriptDelegate
188 - (void) setDelegate:(id)delegate {
189 delegate_ = delegate;
192 - (id) initWithDelegate:(id)delegate {
193 delegate_ = delegate;
197 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
198 if (delegate_ != nil)
199 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
202 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
203 if (delegate_ != nil)
204 return [delegate_ webView:sender didCommitLoadForFrame:frame];
207 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
208 if (delegate_ != nil)
209 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
212 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
213 if (delegate_ != nil)
214 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
217 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
218 if (delegate_ != nil)
219 return [delegate_ webView:sender didFinishLoadForFrame:frame];
222 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
223 if (delegate_ != nil)
224 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
227 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
228 if (delegate_ != nil)
229 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
232 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
233 if (delegate_ != nil)
234 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
237 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
238 if (delegate_ != nil)
239 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
243 - (IMP) methodForSelector:(SEL)sel {
244 if (IMP method = [super methodForSelector:sel])
246 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
250 - (BOOL) respondsToSelector:(SEL)sel {
251 if ([super respondsToSelector:sel])
253 // XXX: WebThreadCreateNSInvocation returns nil
254 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
255 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
258 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
259 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
261 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
262 if (delegate_ != nil)
263 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
265 // XXX: I fucking hate Apple so very very bad
266 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
269 - (void) forwardInvocation:(NSInvocation *)inv {
270 SEL sel = [inv selector];
271 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
272 [inv invokeWithTarget:delegate_];
278 @interface WebCydgetLockScreenView : UIView {
279 WebCycriptDelegate *indirect_;
280 UIProgressIndicator *indicator_;
281 UIScroller *scroller_;
282 UIWebDocumentView *document_;
293 NSMutableSet *loading_;
300 @implementation WebCydgetLockScreenView
302 //#include "UICaboodle/UCInternal.h"
307 WebView *webview([document_ webView]);
308 [webview setFrameLoadDelegate:nil];
309 [webview setResourceLoadDelegate:nil];
310 [webview setUIDelegate:nil];
311 [webview setScriptDebugDelegate:nil];
312 [webview setPolicyDelegate:nil];
314 /* XXX: these are set by UIWebDocumentView
315 [webview setDownloadDelegate:nil];
316 [webview _setFormDelegate:nil];
317 [webview _setUIKitDelegate:nil];
318 [webview setEditingDelegate:nil];*/
320 /* XXX: no one sets this, ever
321 [webview setWebMailDelegate:nil];*/
323 [document_ setDelegate:nil];
324 [document_ setGestureDelegate:nil];
325 [document_ setFormEditingDelegate:nil];
326 [document_ setInteractionDelegate:nil];
328 [indirect_ setDelegate:nil];
337 [scroller_ setDelegate:nil];
343 [indicator_ release];
348 + (float) defaultWidth {
352 - (void) _setTileDrawingEnabled:(BOOL)enabled {
353 //[document_ setTileDrawingEnabled:enabled];
356 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
357 [self _setTileDrawingEnabled:NO];
360 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
361 [self _setTileDrawingEnabled:YES];
362 [document_ redrawScaledDocument];
365 - (void) setViewportWidth:(float)width {
366 width_ = width != 0 ? width : [[self class] defaultWidth];
367 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
370 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
371 [self _setTileDrawingEnabled:NO];
374 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
375 [self _setTileDrawingEnabled:YES];
378 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
379 [self _setTileDrawingEnabled:YES];
382 - (void) loadRequest:(NSURLRequest *)request {
386 [document_ loadRequest:request];
390 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
391 [self loadRequest:[NSURLRequest
398 - (void) loadURL:(NSURL *)url {
399 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
403 CGRect frame = {{0, 0}, {320, 480}};
404 frame.size.height -= GSDefaultStatusBarHeight();
406 if ((self = [super initWithFrame:frame]) != nil) {
407 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
409 struct CGRect bounds([self bounds]);
411 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
412 [self addSubview:scroller_];
414 [scroller_ setFixedBackgroundPattern:YES];
415 [scroller_ setBackgroundColor:[UIColor blackColor]];
417 [scroller_ setScrollingEnabled:YES];
418 [scroller_ setClipsSubviews:YES];
419 [scroller_ setAllowsRubberBanding:YES];
421 [scroller_ setDelegate:self];
422 [scroller_ setBounces:YES];
423 [scroller_ setScrollHysteresis:8];
424 [scroller_ setThumbDetectionEnabled:NO];
425 [scroller_ setDirectionalScrolling:YES];
426 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
427 [scroller_ setEventMode:YES];
428 [scroller_ setShowBackgroundShadow:NO]; /* YES */
429 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
430 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
432 CGRect rect([scroller_ bounds]);
433 //rect.size.height = 0;
437 document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
438 WebView *webview([document_ webView]);
440 [document_ setBackgroundColor:[UIColor blackColor]];
441 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
442 [document_ setDrawsBackground:NO];
443 [webview setDrawsBackground:NO];
445 [webview setPreferencesIdentifier:@"WebCycript"];
447 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
449 if ([document_ respondsToSelector:@selector(enableReachability)])
450 [document_ enableReachability];
452 [document_ setAllowsMessaging:YES];
454 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
455 [document_ useSelectionAssistantWithMode:0];
457 [document_ setTilingEnabled:YES];
458 [document_ setDrawsGrid:NO];
459 [document_ setLogsTilingChanges:NO];
460 [document_ setTileMinificationFilter:kCAFilterNearest];
462 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
463 /* XXX: abstractify */
464 [document_ setDataDetectorTypes:0x80000000];
466 [document_ setDetectsPhoneNumbers:NO];
468 [document_ setAutoresizes:YES];
470 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
471 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
472 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
473 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
475 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
477 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
478 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
479 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
481 [document_ _setDocumentType:0x4];
483 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
484 [document_ setZoomsFocusedFormControl:YES];
485 [document_ setContentsPosition:7];
486 [document_ setEnabledGestures:0xa];
487 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
488 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
490 [document_ setSmoothsFonts:YES];
491 [document_ setAllowsImageSheet:YES];
492 [webview _setUsesLoaderCache:YES];
494 [webview setGroupName:@"CydgetGroup"];
496 WebPreferences *preferences([webview preferences]);
498 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
499 [webview _setLayoutInterval:0];
501 [preferences _setLayoutInterval:0];
503 [self setViewportWidth:0];
505 [document_ setDelegate:self];
506 [document_ setGestureDelegate:self];
507 [document_ setFormEditingDelegate:self];
508 [document_ setInteractionDelegate:self];
510 [scroller_ addSubview:document_];
512 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
514 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
516 [webview setFrameLoadDelegate:indirect_];
517 [webview setPolicyDelegate:indirect_];
518 [webview setResourceLoadDelegate:indirect_];
519 [webview setUIDelegate:indirect_];
521 /* XXX: do not turn this on under penalty of extreme pain */
522 [webview setScriptDebugDelegate:nil];
526 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
527 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
528 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
530 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
531 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
533 NSDictionary *configuration([$CydgetController currentConfiguration]);
535 cycript_ = [configuration objectForKey:@"Cycript"];
537 scrollable_ = [[configuration objectForKey:@"Scrollable"] boolValue];
538 [scroller_ setScrollingEnabled:scrollable_];
540 NSString *homepage([configuration objectForKey:@"Homepage"]);
541 [self loadURL:[NSURL URLWithString:homepage]];
545 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
548 UIActionSheet *sheet = [[[UIActionSheet alloc]
550 buttons:[NSArray arrayWithObjects:@"OK", nil]
556 [sheet setBodyText:message];
557 [sheet popupAlertAnimated:YES];
560 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
563 UIActionSheet *sheet = [[[UIActionSheet alloc]
565 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
571 [sheet setNumberOfRows:1];
572 [sheet setBodyText:message];
573 [sheet popupAlertAnimated:YES];
575 NSRunLoop *loop([NSRunLoop currentRunLoop]);
576 NSDate *future([NSDate distantFuture]);
578 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
580 NSNumber *confirm([confirm_ autorelease]);
584 return [confirm boolValue];
587 /* XXX: WebThreadLock? */
588 - (void) _fixScroller:(CGRect)bounds {
593 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
594 CGRect peripheral([assistant peripheralFrame]);
595 extra = peripheral.size.height;
598 CGRect subrect([scroller_ frame]);
599 subrect.size.height -= [TPBottomLockBar defaultHeight];
600 subrect.size.height -= extra;
601 [scroller_ setScrollerIndicatorSubrect:subrect];
604 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
605 [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
608 size.height += extra;
609 size.height += [TPBottomLockBar defaultHeight];
610 [scroller_ setContentSize:size];
612 [scroller_ releaseRubberBandIfNecessary];
615 - (void) fixScroller {
616 CGRect bounds([document_ documentBounds]);
617 [self _fixScroller:bounds];
620 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
622 [self _fixScroller:frame];
625 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
626 [self view:sender didSetFrame:frame];
629 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
633 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
637 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
642 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
644 if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
645 if (Pcre([cycript_ UTF8String], 0 /*XXX:PCRE_UTF8*/)(href))
646 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
647 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
648 WebView *webview([document_ webView]);
649 WebFrame *frame([webview mainFrame]);
650 JSGlobalContextRef context([frame globalContext]);
651 CYSetupContext(context);
656 return [loading_ count] != 0;
659 - (void) reloadButtons {
660 if ([self isLoading]) {
661 [UIApp setNetworkActivityIndicatorVisible:YES];
662 [indicator_ startAnimation];
664 [UIApp setNetworkActivityIndicatorVisible:NO];
665 [indicator_ stopAnimation];
669 - (void) _finishLoading {
670 size_t count([loading_ count]);
672 [self autorelease];*/
673 if (reloading_ || count != 0)
675 [self reloadButtons];
678 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
679 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
682 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
683 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
686 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
687 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
690 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
691 return [document_ webView:sender didCommitLoadForFrame:frame];
694 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
695 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
698 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
699 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
700 [self _finishLoading];
701 return [document_ webView:sender didFinishLoadForFrame:frame];
704 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
705 /*if ([loading_ count] == 0)
707 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
709 if ([frame parentFrame] == nil) {
710 [document_ resignFirstResponder];
714 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
716 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
717 [scroller_ setZoomScale:1 duration:0];
718 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
719 [scroller_ _setZoomScale:1 duration:0];
720 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
721 [scroller_ setZoomScale:1 animated:NO];*/
723 CGRect rect([scroller_ bounds]);
724 //rect.size.height = 0;
725 [document_ setFrame:rect];
728 [self reloadButtons];
731 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
732 /*if ([frame parentFrame] == nil)
733 [self autorelease];*/
735 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
736 [self _finishLoading];
741 if ([frame parentFrame] == nil) {
742 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
743 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
744 [[error localizedDescription] stringByAddingPercentEscapes]
751 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
752 [self _didFailWithError:error forFrame:frame];
755 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
756 [self _didFailWithError:error forFrame:frame];
759 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
760 fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
765 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
772 static bool cycript_;
773 static bool jscript_;
775 static void SetParser(bool cycript, bool jscript) {
780 static bool GetParser0() {
784 static bool GetParser1() {
788 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
789 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
790 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")))
791 CYParseUChar(pool, &data, &size);
794 extern "C" void *_ZN3JSC7UString3Rep14nullBaseStringE __attribute__((__weak_import__));
795 extern "C" void *_ZN3JSC7UString3Rep7destroyEv __attribute__((__weak_import__));
796 extern "C" void *_ZN3JSC7UStringC1EPKti __attribute__((__weak_import__));
797 extern "C" void *_ZN3JSC7UStringC1EPKc __attribute__((__weak_import__));
798 extern "C" void *_ZNK3JSC7UString6substrEii __attribute__((__weak_import__));
799 extern "C" void *_ZN3WTF10fastMallocEm __attribute__((__weak_import__));
800 extern "C" void WTFReportAssertionFailure(const char *, int, const char *, const char *) __attribute__((__weak_import__));
801 extern "C" void *_ZN3WTF8fastFreeEPv __attribute__((__weak_import__));
805 &_ZN3JSC7UString3Rep14nullBaseStringE == NULL ||
806 &_ZN3JSC7UString3Rep7destroyEv == NULL ||
807 &_ZN3JSC7UStringC1EPKti == NULL ||
808 &_ZN3JSC7UStringC1EPKc == NULL ||
809 &_ZNK3JSC7UString6substrEii == NULL ||
810 &_ZN3WTF10fastMallocEm == NULL ||
811 &WTFReportAssertionFailure == NULL ||
812 &_ZN3WTF8fastFreeEPv == NULL ||
816 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
818 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
820 SetParser(false, true);
822 JSC::SourceCode *source(*_this);
823 const uint16_t *data(source->data());
824 size_t size(source->length());
827 apr_pool_create(&pool, NULL);
829 Cycriptify(pool, data, size);
830 source->~SourceCode();
831 new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
833 apr_pool_destroy(pool);
835 __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
839 MSHook(void, _ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, void *_this, int start, const UChar *code, unsigned length, int *source, int *line, JSC::UString *message) {
841 return __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, code, length, source, line, message);
843 const uint16_t *data(code);
847 apr_pool_create(&pool, NULL);
849 Cycriptify(pool, data, size);
850 __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, data, size, source, line, message);
852 apr_pool_destroy(pool);
860 MSHook(State, _ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, State state) {
861 SetParser(false, true);
862 state = __ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE(state);
863 SetParser(false, false);
867 MSHook(void, _ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, void *resource) {
868 SetParser(false, true);
869 __ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE(resource);
870 SetParser(false, false);
873 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
874 if (!GetParser1() || mime != "text/cycript")
875 return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
877 static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
881 SetParser(true, true);
885 /* Cydget:// Protocol {{{ */
886 @interface CydgetURLProtocol : NSURLProtocol {
891 @implementation CydgetURLProtocol
893 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
894 NSURL *url([request URL]);
897 NSString *scheme([[url scheme] lowercaseString]);
898 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
903 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
907 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
908 id<NSURLProtocolClient> client([self client]);
910 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
912 NSData *data(UIImagePNGRepresentation(icon));
914 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
915 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
916 [client URLProtocol:self didLoadData:data];
917 [client URLProtocolDidFinishLoading:self];
921 - (void) startLoading {
922 id<NSURLProtocolClient> client([self client]);
923 NSURLRequest *request([self request]);
925 NSURL *url([request URL]);
926 NSString *href([url absoluteString]);
928 NSString *path([href substringFromIndex:9]);
929 NSRange slash([path rangeOfString:@"/"]);
932 if (slash.location == NSNotFound) {
936 command = [path substringToIndex:slash.location];
937 path = [path substringFromIndex:(slash.location + 1)];
940 if ([command isEqualToString:@"_UIImageWithName"]) {
943 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
944 UIImage *icon(_UIImageWithName(path));
945 [self _returnPNGWithImage:icon forRequest:request];
947 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
951 - (void) stopLoading {
957 template <typename Type_>
958 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
959 struct nlist &name(nl[index]);
960 uintptr_t value(name.n_value);
961 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
963 function = reinterpret_cast<Type_>(value);
966 template <typename Type_>
967 static void dlset(Type_ &function, const char *name) {
968 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
971 @implementation WebCycriptLockScreenController
973 + (void) initialize {
976 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
978 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
979 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
980 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
981 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
983 void (*_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE)(void *, int, const UChar *, unsigned, int *, int *, JSC::UString *);
984 dlset(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, "_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE");
985 if (_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE != NULL)
986 MSHookFunction(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, MSHake(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE));
989 memset(nl, 0, sizeof(nl));
990 nl[0].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE";
991 nl[1].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE";
992 nl[2].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
993 nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
995 State (*_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE)(State);
996 nlset(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, nl, 0);
997 MSHookFunction(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE));
999 void (*_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE)(void *);
1000 nlset(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, nl, 1);
1001 MSHookFunction(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, MSHake(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE));
1003 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
1004 nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 2);
1005 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
1008 + (id) rootViewController {
1009 return [[[self alloc] init] autorelease];
1013 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];