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>
77 @interface WebView (UICaboodle)
78 - (void) setScriptDebugDelegate:(id)delegate;
79 - (void) _setFormDelegate:(id)delegate;
80 - (void) _setUIKitDelegate:(id)delegate;
81 - (void) setWebMailDelegate:(id)delegate;
82 - (void) _setLayoutInterval:(float)interval;
86 #define _forever for (;;)
88 _disused static unsigned trace_;
90 #define _trace() do { \
91 NSLog(@"_trace(%u)@%s:%u[%s]\n", \
92 trace_++, __FILE__, __LINE__, __FUNCTION__\
96 @protocol CydgetController
97 - (NSDictionary *) currentConfiguration;
100 static Class $CydgetController(objc_getClass("CydgetController"));
102 @interface NSString (UIKit)
103 - (NSString *) stringByAddingPercentEscapes;
106 @implementation UIWebDocumentView (WebCycript)
108 - (void) _setScrollerOffset:(CGPoint)offset {
109 UIScroller *scroller([self _scroller]);
111 CGSize size([scroller contentSize]);
112 CGSize bounds([scroller bounds].size);
115 max.x = size.width - bounds.width;
116 max.y = size.height - bounds.height;
124 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
125 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
127 [scroller setOffset:offset];
132 /* WebCycript Delegate {{{ */
133 @interface WebCycriptDelegate : NSObject {
134 _transient volatile id delegate_;
137 - (void) setDelegate:(id)delegate;
138 - (id) initWithDelegate:(id)delegate;
141 @implementation WebCycriptDelegate
143 - (void) setDelegate:(id)delegate {
144 delegate_ = delegate;
147 - (id) initWithDelegate:(id)delegate {
148 delegate_ = delegate;
152 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
153 if (delegate_ != nil)
154 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
157 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
158 if (delegate_ != nil)
159 return [delegate_ webView:sender didCommitLoadForFrame:frame];
162 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
163 if (delegate_ != nil)
164 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
167 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
168 if (delegate_ != nil)
169 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
172 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
173 if (delegate_ != nil)
174 return [delegate_ webView:sender didFinishLoadForFrame:frame];
177 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
178 if (delegate_ != nil)
179 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
182 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
183 if (delegate_ != nil)
184 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
187 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
188 if (delegate_ != nil)
189 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
192 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
193 if (delegate_ != nil)
194 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
198 - (IMP) methodForSelector:(SEL)sel {
199 if (IMP method = [super methodForSelector:sel])
201 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
205 - (BOOL) respondsToSelector:(SEL)sel {
206 if ([super respondsToSelector:sel])
208 // XXX: WebThreadCreateNSInvocation returns nil
209 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
210 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
213 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
214 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
216 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
217 if (delegate_ != nil)
218 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
220 // XXX: I fucking hate Apple so very very bad
221 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
224 - (void) forwardInvocation:(NSInvocation *)inv {
225 SEL sel = [inv selector];
226 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
227 [inv invokeWithTarget:delegate_];
233 @interface WebCydgetLockScreenView : UIView {
234 WebCycriptDelegate *indirect_;
235 UIProgressIndicator *indicator_;
236 UIScroller *scroller_;
237 UIWebDocumentView *document_;
248 NSMutableSet *loading_;
255 @implementation WebCydgetLockScreenView
257 //#include "UICaboodle/UCInternal.h"
262 WebView *webview([document_ webView]);
263 [webview setFrameLoadDelegate:nil];
264 [webview setResourceLoadDelegate:nil];
265 [webview setUIDelegate:nil];
266 [webview setScriptDebugDelegate:nil];
267 [webview setPolicyDelegate:nil];
269 /* XXX: these are set by UIWebDocumentView
270 [webview setDownloadDelegate:nil];
271 [webview _setFormDelegate:nil];
272 [webview _setUIKitDelegate:nil];
273 [webview setEditingDelegate:nil];*/
275 /* XXX: no one sets this, ever
276 [webview setWebMailDelegate:nil];*/
278 [document_ setDelegate:nil];
279 [document_ setGestureDelegate:nil];
280 [document_ setFormEditingDelegate:nil];
281 [document_ setInteractionDelegate:nil];
283 [indirect_ setDelegate:nil];
292 [scroller_ setDelegate:nil];
298 [indicator_ release];
303 + (float) defaultWidth {
307 - (void) _setTileDrawingEnabled:(BOOL)enabled {
308 //[document_ setTileDrawingEnabled:enabled];
311 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
312 [self _setTileDrawingEnabled:NO];
315 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
316 [self _setTileDrawingEnabled:YES];
317 [document_ redrawScaledDocument];
320 - (void) setViewportWidth:(float)width {
321 width_ = width != 0 ? width : [[self class] defaultWidth];
322 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
325 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
326 [self _setTileDrawingEnabled:NO];
329 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
330 [self _setTileDrawingEnabled:YES];
333 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
334 [self _setTileDrawingEnabled:YES];
337 - (void) loadRequest:(NSURLRequest *)request {
341 [document_ loadRequest:request];
345 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
346 [self loadRequest:[NSURLRequest
353 - (void) loadURL:(NSURL *)url {
354 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
358 CGRect frame = {{0, 0}, {320, 480}};
359 frame.size.height -= GSDefaultStatusBarHeight();
361 if ((self = [super initWithFrame:frame]) != nil) {
362 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
364 struct CGRect bounds([self bounds]);
366 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
367 [self addSubview:scroller_];
369 [scroller_ setFixedBackgroundPattern:YES];
370 [scroller_ setBackgroundColor:[UIColor blackColor]];
372 [scroller_ setScrollingEnabled:YES];
373 [scroller_ setClipsSubviews:YES];
374 [scroller_ setAllowsRubberBanding:YES];
376 [scroller_ setDelegate:self];
377 [scroller_ setBounces:YES];
378 [scroller_ setScrollHysteresis:8];
379 [scroller_ setThumbDetectionEnabled:NO];
380 [scroller_ setDirectionalScrolling:YES];
381 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
382 [scroller_ setEventMode:YES];
383 [scroller_ setShowBackgroundShadow:NO]; /* YES */
384 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
385 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
387 CGRect rect([scroller_ bounds]);
388 //rect.size.height = 0;
392 document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
393 WebView *webview([document_ webView]);
395 [document_ setBackgroundColor:[UIColor blackColor]];
396 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
397 [document_ setDrawsBackground:NO];
398 [webview setDrawsBackground:NO];
400 [webview setPreferencesIdentifier:@"WebCycript"];
402 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
404 if ([document_ respondsToSelector:@selector(enableReachability)])
405 [document_ enableReachability];
407 [document_ setAllowsMessaging:YES];
409 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
410 [document_ useSelectionAssistantWithMode:0];
412 [document_ setTilingEnabled:YES];
413 [document_ setDrawsGrid:NO];
414 [document_ setLogsTilingChanges:NO];
415 [document_ setTileMinificationFilter:kCAFilterNearest];
417 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
418 /* XXX: abstractify */
419 [document_ setDataDetectorTypes:0x80000000];
421 [document_ setDetectsPhoneNumbers:NO];
423 [document_ setAutoresizes:YES];
425 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
426 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
427 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
428 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
430 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
432 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
433 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
434 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
436 [document_ _setDocumentType:0x4];
438 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
439 [document_ setZoomsFocusedFormControl:YES];
440 [document_ setContentsPosition:7];
441 [document_ setEnabledGestures:0xa];
442 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
443 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
445 [document_ setSmoothsFonts:YES];
446 [document_ setAllowsImageSheet:YES];
447 [webview _setUsesLoaderCache:YES];
449 [webview setGroupName:@"CydgetGroup"];
451 WebPreferences *preferences([webview preferences]);
453 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
454 [webview _setLayoutInterval:0];
456 [preferences _setLayoutInterval:0];
458 [self setViewportWidth:0];
460 [document_ setDelegate:self];
461 [document_ setGestureDelegate:self];
462 [document_ setFormEditingDelegate:self];
463 [document_ setInteractionDelegate:self];
465 [scroller_ addSubview:document_];
467 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
469 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
471 [webview setFrameLoadDelegate:indirect_];
472 [webview setPolicyDelegate:indirect_];
473 [webview setResourceLoadDelegate:indirect_];
474 [webview setUIDelegate:indirect_];
476 /* XXX: do not turn this on under penalty of extreme pain */
477 [webview setScriptDebugDelegate:nil];
481 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
482 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
483 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
485 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
486 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
488 NSDictionary *configuration([$CydgetController currentConfiguration]);
490 cycript_ = [[configuration objectForKey:@"Cycript"] boolValue];
492 scrollable_ = [[configuration objectForKey:@"Scrollable"] boolValue];
493 [scroller_ setScrollingEnabled:scrollable_];
495 NSString *homepage([configuration objectForKey:@"Homepage"]);
496 [self loadURL:[NSURL URLWithString:homepage]];
500 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
503 UIActionSheet *sheet = [[[UIActionSheet alloc]
505 buttons:[NSArray arrayWithObjects:@"OK", nil]
511 [sheet setBodyText:message];
512 [sheet popupAlertAnimated:YES];
515 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
518 UIActionSheet *sheet = [[[UIActionSheet alloc]
520 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
526 [sheet setNumberOfRows:1];
527 [sheet setBodyText:message];
528 [sheet popupAlertAnimated:YES];
530 NSRunLoop *loop([NSRunLoop currentRunLoop]);
531 NSDate *future([NSDate distantFuture]);
533 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
535 NSNumber *confirm([confirm_ autorelease]);
539 return [confirm boolValue];
542 /* XXX: WebThreadLock? */
543 - (void) _fixScroller:(CGRect)bounds {
548 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
549 CGRect peripheral([assistant peripheralFrame]);
550 extra = peripheral.size.height;
553 CGRect subrect([scroller_ frame]);
554 subrect.size.height -= [TPBottomLockBar defaultHeight];
555 subrect.size.height -= extra;
556 [scroller_ setScrollerIndicatorSubrect:subrect];
559 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
560 [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
563 size.height += extra;
564 size.height += [TPBottomLockBar defaultHeight];
565 [scroller_ setContentSize:size];
567 [scroller_ releaseRubberBandIfNecessary];
570 - (void) fixScroller {
571 CGRect bounds([document_ documentBounds]);
572 [self _fixScroller:bounds];
575 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
577 [self _fixScroller:frame];
580 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
581 [self view:sender didSetFrame:frame];
584 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
588 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
592 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
597 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
599 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
600 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
601 WebView *webview([document_ webView]);
602 WebFrame *frame([webview mainFrame]);
603 JSGlobalContextRef context([frame globalContext]);
604 CYSetupContext(context);
609 return [loading_ count] != 0;
612 - (void) reloadButtons {
613 if ([self isLoading]) {
614 [UIApp setNetworkActivityIndicatorVisible:YES];
615 [indicator_ startAnimation];
617 [UIApp setNetworkActivityIndicatorVisible:NO];
618 [indicator_ stopAnimation];
622 - (void) _finishLoading {
623 size_t count([loading_ count]);
625 [self autorelease];*/
626 if (reloading_ || count != 0)
628 [self reloadButtons];
631 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
632 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
635 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
636 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
639 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
640 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
643 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
644 return [document_ webView:sender didCommitLoadForFrame:frame];
647 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
648 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
651 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
652 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
653 [self _finishLoading];
654 return [document_ webView:sender didFinishLoadForFrame:frame];
657 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
658 /*if ([loading_ count] == 0)
660 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
662 if ([frame parentFrame] == nil) {
663 [document_ resignFirstResponder];
667 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
669 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
670 [scroller_ setZoomScale:1 duration:0];
671 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
672 [scroller_ _setZoomScale:1 duration:0];
673 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
674 [scroller_ setZoomScale:1 animated:NO];*/
676 CGRect rect([scroller_ bounds]);
677 //rect.size.height = 0;
678 [document_ setFrame:rect];
681 [self reloadButtons];
684 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
685 /*if ([frame parentFrame] == nil)
686 [self autorelease];*/
688 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
689 [self _finishLoading];
694 if ([frame parentFrame] == nil) {
695 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
696 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
697 [[error localizedDescription] stringByAddingPercentEscapes]
704 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
705 [self _didFailWithError:error forFrame:frame];
708 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
709 [self _didFailWithError:error forFrame:frame];
712 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
713 fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
718 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
725 static bool cycript_;
726 static bool jscript_;
728 static void SetParser(bool cycript, bool jscript) {
733 static bool GetParser0() {
737 static bool GetParser1() {
741 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
742 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
743 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")))
744 CYParseUChar(pool, &data, &size);
747 extern "C" void *_ZN3JSC7UString3Rep14nullBaseStringE __attribute__((__weak_import__));
748 extern "C" void *_ZN3JSC7UString3Rep7destroyEv __attribute__((__weak_import__));
749 extern "C" void *_ZN3JSC7UStringC1EPKti __attribute__((__weak_import__));
750 extern "C" void *_ZNK3JSC7UString6substrEii __attribute__((__weak_import__));
751 extern "C" void *_ZN3WTF10fastMallocEm __attribute__((__weak_import__));
752 extern "C" void WTFReportAssertionFailure(const char *, int, const char *, const char *) __attribute__((__weak_import__));
753 extern "C" void *_ZN3WTF8fastFreeEPv __attribute__((__weak_import__));
757 &_ZN3JSC7UString3Rep14nullBaseStringE == NULL ||
758 &_ZN3JSC7UString3Rep7destroyEv == NULL ||
759 &_ZN3JSC7UStringC1EPKti == NULL ||
760 &_ZNK3JSC7UString6substrEii == NULL ||
761 &_ZN3WTF10fastMallocEm == NULL ||
762 &WTFReportAssertionFailure == NULL ||
763 &_ZN3WTF8fastFreeEPv == NULL ||
767 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
769 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
771 SetParser(false, true);
772 JSC::SourceCode *source(*_this);
773 const uint16_t *data(source->data());
774 size_t size(source->length());
776 apr_pool_create(&pool, NULL);
777 Cycriptify(pool, data, size);
778 JSC::SourceCode code(JSC::makeSource(JSC::UString(data, size)));
780 __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
781 apr_pool_destroy(pool);
786 MSHook(void, _ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, void *_this, int start, const UChar *code, unsigned length, int *source, int *line, JSC::UString *message) {
788 return __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, code, length, source, line, message);
790 const uint16_t *data(code);
793 apr_pool_create(&pool, NULL);
794 Cycriptify(pool, data, size);
795 __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, data, size, source, line, message);
796 apr_pool_destroy(pool);
804 MSHook(State, _ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, State state) {
805 SetParser(false, true);
806 state = __ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE(state);
807 SetParser(false, false);
811 MSHook(void, _ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, void *resource) {
812 SetParser(false, true);
813 __ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE(resource);
814 SetParser(false, false);
817 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
820 if (!GetParser1()) through:
821 jscript = __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
822 else if (mime == "text/cycript") {
823 SetParser(true, true);
831 /* Cydget:// Protocol {{{ */
832 @interface CydgetURLProtocol : NSURLProtocol {
837 @implementation CydgetURLProtocol
839 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
840 NSURL *url([request URL]);
843 NSString *scheme([[url scheme] lowercaseString]);
844 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
849 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
853 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
854 id<NSURLProtocolClient> client([self client]);
856 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
858 NSData *data(UIImagePNGRepresentation(icon));
860 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
861 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
862 [client URLProtocol:self didLoadData:data];
863 [client URLProtocolDidFinishLoading:self];
867 - (void) startLoading {
868 id<NSURLProtocolClient> client([self client]);
869 NSURLRequest *request([self request]);
871 NSURL *url([request URL]);
872 NSString *href([url absoluteString]);
874 NSString *path([href substringFromIndex:9]);
875 NSRange slash([path rangeOfString:@"/"]);
878 if (slash.location == NSNotFound) {
882 command = [path substringToIndex:slash.location];
883 path = [path substringFromIndex:(slash.location + 1)];
886 if ([command isEqualToString:@"_UIImageWithName"]) {
889 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
890 UIImage *icon(_UIImageWithName(path));
891 [self _returnPNGWithImage:icon forRequest:request];
893 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
897 - (void) stopLoading {
903 template <typename Type_>
904 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
905 struct nlist &name(nl[index]);
906 uintptr_t value(name.n_value);
907 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
909 function = reinterpret_cast<Type_>(value);
912 template <typename Type_>
913 static void dlset(Type_ &function, const char *name) {
914 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
917 @implementation WebCycriptLockScreenController
919 + (void) initialize {
922 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
924 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
925 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
926 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
927 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
929 void (*_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE)(void *, int, const UChar *, unsigned, int *, int *, JSC::UString *);
930 dlset(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, "_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE");
931 if (_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE != NULL)
932 MSHookFunction(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, MSHake(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE));
935 memset(nl, 0, sizeof(nl));
936 nl[0].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE";
937 nl[1].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE";
938 nl[2].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
939 nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
941 State (*_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE)(State);
942 nlset(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, nl, 0);
943 MSHookFunction(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE));
945 void (*_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE)(void *);
946 nlset(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, nl, 1);
947 MSHookFunction(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, MSHake(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE));
949 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
950 nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 2);
951 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
954 + (id) rootViewController {
955 return [[[self alloc] init] autorelease];
959 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];