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 #define _assert(test) do \
99 fprintf(stderr, "_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
104 #define _syscall(expr) \
105 do if ((long) (expr) != -1) \
107 else switch (errno) { \
114 @protocol CydgetController
115 - (NSDictionary *) currentConfiguration;
118 static Class $CydgetController(objc_getClass("CydgetController"));
120 @interface NSString (UIKit)
121 - (NSString *) stringByAddingPercentEscapes;
124 @implementation UIWebDocumentView (WebCycript)
126 - (void) _setScrollerOffset:(CGPoint)offset {
127 UIScroller *scroller([self _scroller]);
129 CGSize size([scroller contentSize]);
130 CGSize bounds([scroller bounds].size);
133 max.x = size.width - bounds.width;
134 max.y = size.height - bounds.height;
142 offset.x = offset.x < 0 ? 0 : offset.x > max.x ? max.x : offset.x;
143 offset.y = offset.y < 0 ? 0 : offset.y > max.y ? max.y : offset.y;
145 [scroller setOffset:offset];
150 /* Perl-Compatible RegEx {{{ */
160 Pcre(const char *regex, int options = 0) :
165 code_ = pcre_compile(regex, options, &error, &offset, NULL);
168 @throw [NSException exceptionWithName:NSInvalidArgumentException reason:[NSString stringWithFormat:@"*** Pcre(,): [%u] %s", offset, error] userInfo:nil];
170 pcre_fullinfo(code_, study_, PCRE_INFO_CAPTURECOUNT, &capture_);
171 matches_ = new int[(capture_ + 1) * 3];
179 NSString *operator [](size_t match) {
180 return [[[NSString alloc] initWithBytes:(data_ + matches_[match * 2]) length:(matches_[match * 2 + 1] - matches_[match * 2]) encoding:NSUTF8StringEncoding] autorelease];
183 bool operator ()(NSString *data) {
184 // XXX: length is for characters, not for bytes
185 return operator ()([data UTF8String], [data length]);
188 bool operator ()(const char *data, size_t size) {
190 return pcre_exec(code_, study_, data, size, 0, 0, matches_, (capture_ + 1) * 3) >= 0;
194 /* WebCycript Delegate {{{ */
195 @interface WebCycriptDelegate : NSObject {
196 _transient volatile id delegate_;
199 - (void) setDelegate:(id)delegate;
200 - (id) initWithDelegate:(id)delegate;
203 @implementation WebCycriptDelegate
205 - (void) setDelegate:(id)delegate {
206 delegate_ = delegate;
209 - (id) initWithDelegate:(id)delegate {
210 delegate_ = delegate;
214 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
215 if (delegate_ != nil)
216 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
219 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
220 if (delegate_ != nil)
221 return [delegate_ webView:sender didCommitLoadForFrame:frame];
224 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
225 if (delegate_ != nil)
226 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
229 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
230 if (delegate_ != nil)
231 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
234 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
235 if (delegate_ != nil)
236 return [delegate_ webView:sender didFinishLoadForFrame:frame];
239 /*- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
240 if (delegate_ != nil)
241 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
244 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
245 if (delegate_ != nil)
246 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
249 /*- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
250 if (delegate_ != nil)
251 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
254 /*- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
255 if (delegate_ != nil)
256 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
260 - (IMP) methodForSelector:(SEL)sel {
261 if (IMP method = [super methodForSelector:sel])
263 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
267 - (BOOL) respondsToSelector:(SEL)sel {
268 if ([super respondsToSelector:sel])
270 // XXX: WebThreadCreateNSInvocation returns nil
271 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
272 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
275 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
276 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
278 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
279 if (delegate_ != nil)
280 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
282 // XXX: I fucking hate Apple so very very bad
283 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
286 - (void) forwardInvocation:(NSInvocation *)inv {
287 SEL sel = [inv selector];
288 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
289 [inv invokeWithTarget:delegate_];
295 @interface WebCydgetLockScreenView : UIView {
296 WebCycriptDelegate *indirect_;
297 UIProgressIndicator *indicator_;
298 UIScroller *scroller_;
299 UIWebDocumentView *document_;
310 NSMutableSet *loading_;
317 @implementation WebCydgetLockScreenView
319 //#include "UICaboodle/UCInternal.h"
324 WebView *webview([document_ webView]);
325 [webview setFrameLoadDelegate:nil];
326 [webview setResourceLoadDelegate:nil];
327 [webview setUIDelegate:nil];
328 [webview setScriptDebugDelegate:nil];
329 [webview setPolicyDelegate:nil];
331 /* XXX: these are set by UIWebDocumentView
332 [webview setDownloadDelegate:nil];
333 [webview _setFormDelegate:nil];
334 [webview _setUIKitDelegate:nil];
335 [webview setEditingDelegate:nil];*/
337 /* XXX: no one sets this, ever
338 [webview setWebMailDelegate:nil];*/
340 [document_ setDelegate:nil];
341 [document_ setGestureDelegate:nil];
342 [document_ setFormEditingDelegate:nil];
343 [document_ setInteractionDelegate:nil];
345 [indirect_ setDelegate:nil];
354 [scroller_ setDelegate:nil];
360 [indicator_ release];
365 + (float) defaultWidth {
369 - (void) _setTileDrawingEnabled:(BOOL)enabled {
370 //[document_ setTileDrawingEnabled:enabled];
373 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
374 [self _setTileDrawingEnabled:NO];
377 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
378 [self _setTileDrawingEnabled:YES];
379 [document_ redrawScaledDocument];
382 - (void) setViewportWidth:(float)width {
383 width_ = width != 0 ? width : [[self class] defaultWidth];
384 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
387 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
388 [self _setTileDrawingEnabled:NO];
391 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
392 [self _setTileDrawingEnabled:YES];
395 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
396 [self _setTileDrawingEnabled:YES];
399 - (void) loadRequest:(NSURLRequest *)request {
403 [document_ loadRequest:request];
407 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
408 [self loadRequest:[NSURLRequest
415 - (void) loadURL:(NSURL *)url {
416 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
420 CGRect frame = {{0, 0}, {320, 480}};
421 frame.size.height -= GSDefaultStatusBarHeight();
423 if ((self = [super initWithFrame:frame]) != nil) {
424 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
426 struct CGRect bounds([self bounds]);
428 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
429 [self addSubview:scroller_];
431 [scroller_ setFixedBackgroundPattern:YES];
432 [scroller_ setBackgroundColor:[UIColor blackColor]];
434 [scroller_ setScrollingEnabled:YES];
435 [scroller_ setClipsSubviews:YES];
436 [scroller_ setAllowsRubberBanding:YES];
438 [scroller_ setDelegate:self];
439 [scroller_ setBounces:YES];
440 [scroller_ setScrollHysteresis:8];
441 [scroller_ setThumbDetectionEnabled:NO];
442 [scroller_ setDirectionalScrolling:YES];
443 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
444 [scroller_ setEventMode:YES];
445 [scroller_ setShowBackgroundShadow:NO]; /* YES */
446 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
447 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
449 CGRect rect([scroller_ bounds]);
450 //rect.size.height = 0;
454 document_ = [[UIWebDocumentView alloc] initWithFrame:rect];
455 WebView *webview([document_ webView]);
457 [document_ setBackgroundColor:[UIColor blackColor]];
458 if ([document_ respondsToSelector:@selector(setDrawsBackground:)])
459 [document_ setDrawsBackground:NO];
460 [webview setDrawsBackground:NO];
462 [webview setPreferencesIdentifier:@"WebCycript"];
464 [document_ setTileSize:CGSizeMake(rect.size.width, 500)];
466 if ([document_ respondsToSelector:@selector(enableReachability)])
467 [document_ enableReachability];
469 [document_ setAllowsMessaging:YES];
471 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
472 [document_ useSelectionAssistantWithMode:0];
474 [document_ setTilingEnabled:YES];
475 [document_ setDrawsGrid:NO];
476 [document_ setLogsTilingChanges:NO];
477 [document_ setTileMinificationFilter:kCAFilterNearest];
479 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
480 /* XXX: abstractify */
481 [document_ setDataDetectorTypes:0x80000000];
483 [document_ setDetectsPhoneNumbers:NO];
485 [document_ setAutoresizes:YES];
487 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
488 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
489 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
490 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
492 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
494 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
495 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
496 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
498 [document_ _setDocumentType:0x4];
500 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
501 [document_ setZoomsFocusedFormControl:YES];
502 [document_ setContentsPosition:7];
503 [document_ setEnabledGestures:0xa];
504 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
505 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
507 [document_ setSmoothsFonts:YES];
508 [document_ setAllowsImageSheet:YES];
509 [webview _setUsesLoaderCache:YES];
511 [webview setGroupName:@"CydgetGroup"];
513 WebPreferences *preferences([webview preferences]);
515 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
516 [webview _setLayoutInterval:0];
518 [preferences _setLayoutInterval:0];
520 [self setViewportWidth:0];
522 [document_ setDelegate:self];
523 [document_ setGestureDelegate:self];
524 [document_ setFormEditingDelegate:self];
525 [document_ setInteractionDelegate:self];
527 [scroller_ addSubview:document_];
529 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
531 indirect_ = [[WebCycriptDelegate alloc] initWithDelegate:self];
533 [webview setFrameLoadDelegate:indirect_];
534 [webview setPolicyDelegate:indirect_];
535 [webview setResourceLoadDelegate:indirect_];
536 [webview setUIDelegate:indirect_];
538 /* XXX: do not turn this on under penalty of extreme pain */
539 [webview setScriptDebugDelegate:nil];
543 CGSize indsize([UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]);
544 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
545 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
547 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
548 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
550 NSDictionary *configuration([$CydgetController currentConfiguration]);
552 cycript_ = [configuration objectForKey:@"CycriptURLs"];
554 scrollable_ = [[configuration objectForKey:@"Scrollable"] boolValue];
555 [scroller_ setScrollingEnabled:scrollable_];
557 NSString *homepage([configuration objectForKey:@"Homepage"]);
558 [self loadURL:[NSURL URLWithString:homepage]];
562 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
565 UIActionSheet *sheet = [[[UIActionSheet alloc]
567 buttons:[NSArray arrayWithObjects:@"OK", nil]
573 [sheet setBodyText:message];
574 [sheet popupAlertAnimated:YES];
577 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
580 UIActionSheet *sheet = [[[UIActionSheet alloc]
582 buttons:[NSArray arrayWithObjects:@"OK", @"CANCEL", nil]
588 [sheet setNumberOfRows:1];
589 [sheet setBodyText:message];
590 [sheet popupAlertAnimated:YES];
592 NSRunLoop *loop([NSRunLoop currentRunLoop]);
593 NSDate *future([NSDate distantFuture]);
595 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
597 NSNumber *confirm([confirm_ autorelease]);
601 return [confirm boolValue];
604 /* XXX: WebThreadLock? */
605 - (void) _fixScroller:(CGRect)bounds {
610 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
611 CGRect peripheral([assistant peripheralFrame]);
612 extra = peripheral.size.height;
615 CGRect subrect([scroller_ frame]);
616 subrect.size.height -= [TPBottomLockBar defaultHeight];
617 subrect.size.height -= extra;
618 [scroller_ setScrollerIndicatorSubrect:subrect];
621 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
622 [document_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
625 size.height += extra;
626 size.height += [TPBottomLockBar defaultHeight];
627 [scroller_ setContentSize:size];
629 [scroller_ releaseRubberBandIfNecessary];
632 - (void) fixScroller {
633 CGRect bounds([document_ documentBounds]);
634 [self _fixScroller:bounds];
637 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
639 [self _fixScroller:frame];
642 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
643 [self view:sender didSetFrame:frame];
646 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
650 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
654 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
659 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
661 if (NSString *href = [[[[frame dataSource] request] URL] absoluteString])
662 if (Pcre([cycript_ UTF8String], 0 /*XXX:PCRE_UTF8*/)(href))
663 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
664 if (void (*CYSetupContext)(JSGlobalContextRef) = reinterpret_cast<void (*)(JSGlobalContextRef)>(dlsym(handle, "CydgetSetupContext"))) {
665 WebView *webview([document_ webView]);
666 WebFrame *frame([webview mainFrame]);
667 JSGlobalContextRef context([frame globalContext]);
668 CYSetupContext(context);
673 return [loading_ count] != 0;
676 - (void) reloadButtons {
677 if ([self isLoading]) {
678 [UIApp setNetworkActivityIndicatorVisible:YES];
679 [indicator_ startAnimation];
681 [UIApp setNetworkActivityIndicatorVisible:NO];
682 [indicator_ stopAnimation];
686 - (void) _finishLoading {
687 size_t count([loading_ count]);
689 [self autorelease];*/
690 if (reloading_ || count != 0)
692 [self reloadButtons];
695 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
696 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
699 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
700 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
703 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
704 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
707 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
708 return [document_ webView:sender didCommitLoadForFrame:frame];
711 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
712 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
715 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
716 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
717 [self _finishLoading];
718 return [document_ webView:sender didFinishLoadForFrame:frame];
721 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
722 /*if ([loading_ count] == 0)
724 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
726 if ([frame parentFrame] == nil) {
727 [document_ resignFirstResponder];
731 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
733 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
734 [scroller_ setZoomScale:1 duration:0];
735 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
736 [scroller_ _setZoomScale:1 duration:0];
737 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
738 [scroller_ setZoomScale:1 animated:NO];*/
740 CGRect rect([scroller_ bounds]);
741 //rect.size.height = 0;
742 [document_ setFrame:rect];
745 [self reloadButtons];
748 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
749 /*if ([frame parentFrame] == nil)
750 [self autorelease];*/
752 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
753 [self _finishLoading];
758 if ([frame parentFrame] == nil) {
759 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
760 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
761 [[error localizedDescription] stringByAddingPercentEscapes]
768 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
769 [self _didFailWithError:error forFrame:frame];
772 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
773 [self _didFailWithError:error forFrame:frame];
776 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
777 fprintf(stderr, "Console:%s\n", [[dictionary description] UTF8String]);
782 @interface WebCycriptLockScreenController : SBAwayViewPluginController {
789 static bool cycript_;
790 static bool jscript_;
792 static void SetParser(bool cycript, bool jscript) {
797 static bool GetParser0() {
801 static bool GetParser1() {
805 static void Cycriptify(apr_pool_t *pool, const uint16_t *&data, size_t &size) {
806 if (void *handle = dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL))
807 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")))
808 CYParseUChar(pool, &data, &size);
811 extern "C" void *_ZN3JSC7UString3Rep14nullBaseStringE __attribute__((__weak_import__));
812 extern "C" void *_ZN3JSC7UString3Rep7destroyEv __attribute__((__weak_import__));
813 extern "C" void *_ZN3JSC7UStringC1EPKti __attribute__((__weak_import__));
814 extern "C" void *_ZN3JSC7UStringC1EPKc __attribute__((__weak_import__));
815 extern "C" void *_ZNK3JSC7UString6substrEii __attribute__((__weak_import__));
816 extern "C" void *_ZN3WTF10fastMallocEm __attribute__((__weak_import__));
817 extern "C" void WTFReportAssertionFailure(const char *, int, const char *, const char *) __attribute__((__weak_import__));
818 extern "C" void *_ZN3WTF8fastFreeEPv __attribute__((__weak_import__));
822 &_ZN3JSC7UString3Rep14nullBaseStringE == NULL ||
823 &_ZN3JSC7UString3Rep7destroyEv == NULL ||
824 &_ZN3JSC7UStringC1EPKti == NULL ||
825 &_ZN3JSC7UStringC1EPKc == NULL ||
826 &_ZNK3JSC7UString6substrEii == NULL ||
827 &_ZN3WTF10fastMallocEm == NULL ||
828 &WTFReportAssertionFailure == NULL ||
829 &_ZN3WTF8fastFreeEPv == NULL ||
833 MSHook(void, _ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, JSC::SourceCode **_this, JSC::JSGlobalData *global, int *line, JSC::UString *message) {
835 return __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
837 SetParser(false, true);
839 JSC::SourceCode *source(*_this);
840 const uint16_t *data(source->data());
841 size_t size(source->length());
844 apr_pool_create(&pool, NULL);
846 Cycriptify(pool, data, size);
847 source->~SourceCode();
848 new (source) JSC::SourceCode(JSC::UStringSourceProvider::create(JSC::UString(data, size), "cycript://"), 1);
850 apr_pool_destroy(pool);
852 __ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE(_this, global, line, message);
856 MSHook(void, _ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, void *_this, int start, const UChar *code, unsigned length, int *source, int *line, JSC::UString *message) {
858 return __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, code, length, source, line, message);
860 const uint16_t *data(code);
864 apr_pool_create(&pool, NULL);
866 Cycriptify(pool, data, size);
867 __ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE(_this, start, data, size, source, line, message);
869 apr_pool_destroy(pool);
877 MSHook(State, _ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, State state) {
878 SetParser(false, true);
879 state = __ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE(state);
880 SetParser(false, false);
884 MSHook(void, _ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, void *resource) {
885 SetParser(false, true);
886 __ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE(resource);
887 SetParser(false, false);
890 MSHook(bool, _ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, const WebCore::String &mime) {
891 if (!GetParser1() || mime != "text/cycript")
892 return __ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE(mime);
894 static void *handle(dlopen("/usr/lib/libcycript.dylib", RTLD_LAZY | RTLD_GLOBAL));
898 SetParser(true, true);
902 /* Cydget:// Protocol {{{ */
903 @interface CydgetURLProtocol : NSURLProtocol {
908 @implementation CydgetURLProtocol
910 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
911 NSURL *url([request URL]);
914 NSString *scheme([[url scheme] lowercaseString]);
915 if (scheme == nil || ![scheme isEqualToString:@"cydget"])
920 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
924 - (void) _returnPNGWithImage:(UIImage *)icon forRequest:(NSURLRequest *)request {
925 id<NSURLProtocolClient> client([self client]);
927 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
929 NSData *data(UIImagePNGRepresentation(icon));
931 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:@"image/png" expectedContentLength:-1 textEncodingName:nil] autorelease]);
932 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
933 [client URLProtocol:self didLoadData:data];
934 [client URLProtocolDidFinishLoading:self];
938 - (void) startLoading {
939 id<NSURLProtocolClient> client([self client]);
940 NSURLRequest *request([self request]);
942 NSURL *url([request URL]);
943 NSString *href([url absoluteString]);
945 NSString *path([href substringFromIndex:9]);
946 NSRange slash([path rangeOfString:@"/"]);
949 if (slash.location == NSNotFound) {
953 command = [path substringToIndex:slash.location];
954 path = [path substringFromIndex:(slash.location + 1)];
957 if ([command isEqualToString:@"_UIImageWithName"]) {
960 path = [path stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
961 UIImage *icon(_UIImageWithName(path));
962 [self _returnPNGWithImage:icon forRequest:request];
964 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
968 - (void) stopLoading {
973 /* Cydget-PHP:// Protocol {{{ */
974 @interface CydgetPHPURLProtocol : NSURLProtocol {
979 @implementation CydgetPHPURLProtocol
981 + (BOOL) canInitWithRequest:(NSURLRequest *)request {
982 NSURL *url([request URL]);
985 NSString *scheme([[url scheme] lowercaseString]);
986 if (scheme == nil || ![scheme isEqualToString:@"cydget-php"])
991 + (NSURLRequest *) canonicalRequestForRequest:(NSURLRequest *)request {
995 - (void) startLoading {
996 id<NSURLProtocolClient> client([self client]);
997 NSURLRequest *request([self request]);
998 NSURL *url([request URL]);
1000 NSString *path([url path]);
1001 if (path == nil || ![path hasSuffix:@".php"]) {
1002 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorResourceUnavailable userInfo:nil]];
1006 NSFileManager *manager([NSFileManager defaultManager]);
1007 if (![manager fileExistsAtPath:path]) {
1008 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]];
1012 NSLog(@"%@::%@", path, [url query]);
1015 _assert(pipe(fds) != -1);
1019 setenv("GATEWAY_INTERFACE", "CGI/1.1", true);
1020 setenv("SCRIPT_FILENAME", [path UTF8String], true);
1021 NSString *query([url query]);
1023 setenv("QUERY_STRING", [query UTF8String], true);
1025 _assert(dup2(fds[1], 1) != -1);
1026 _assert(close(fds[0]) != -1);
1027 _assert(close(fds[1]) != -1);
1029 execl("/usr/bin/php-cgi", "php-cgi", NULL);
1034 _assert(close(fds[1]) != -1);
1036 CFHTTPMessageRef http(CFHTTPMessageCreateEmpty(kCFAllocatorDefault, FALSE));
1038 CFHTTPMessageAppendBytes(http, (const uint8_t *) "HTTP/1.1 200 OK\r\n", 17);
1040 if (FILE *file = fdopen(fds[0], "r")) {
1041 uint8_t buffer[16*1024];
1043 size_t count(fread(buffer, 1, sizeof(buffer), file));
1045 fwrite(buffer, 1, count, stderr);
1046 CFHTTPMessageAppendBytes(http, buffer, count);
1048 if (count == sizeof(buffer))
1051 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorNetworkConnectionLost userInfo:nil]];
1056 } else _assert(close(fds[0]));
1059 CFStringRef mime(CFHTTPMessageCopyHeaderFieldValue(http, CFSTR("Content-type")));
1061 [client URLProtocol:self didFailWithError:[NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:nil]];
1065 NSURLResponse *response([[[NSURLResponse alloc] initWithURL:[request URL] MIMEType:(NSString *)mime expectedContentLength:-1 textEncodingName:nil] autorelease]);
1068 [client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
1070 CFDataRef data(CFHTTPMessageCopyBody(http));
1071 [client URLProtocol:self didLoadData:(NSData *)data];
1074 [client URLProtocolDidFinishLoading:self];
1081 _syscall(waitpid(pid, &status, 0));
1084 - (void) stopLoading {
1090 template <typename Type_>
1091 static void nlset(Type_ &function, struct nlist *nl, size_t index) {
1092 struct nlist &name(nl[index]);
1093 uintptr_t value(name.n_value);
1094 if ((name.n_desc & N_ARM_THUMB_DEF) != 0)
1095 value |= 0x00000001;
1096 function = reinterpret_cast<Type_>(value);
1099 template <typename Type_>
1100 static void dlset(Type_ &function, const char *name) {
1101 function = reinterpret_cast<Type_>(dlsym(RTLD_DEFAULT, name));
1104 @implementation WebCycriptLockScreenController
1106 + (void) initialize {
1109 [NSURLProtocol registerClass:[CydgetURLProtocol class]];
1110 [NSURLProtocol registerClass:[CydgetPHPURLProtocol class]];
1112 void (*_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE)(JSC::SourceCode **, JSC::JSGlobalData *, int *, JSC::UString *);
1113 dlset(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, "_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE");
1114 if (_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE != NULL)
1115 MSHookFunction(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE, MSHake(_ZN3JSC6Parser5parseEPNS_12JSGlobalDataEPiPNS_7UStringE));
1117 void (*_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE)(void *, int, const UChar *, unsigned, int *, int *, JSC::UString *);
1118 dlset(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, "_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE");
1119 if (_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE != NULL)
1120 MSHookFunction(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE, MSHake(_ZN3KJS6Parser5parseEiPKNS_5UCharEjPiS4_PNS_7UStringE));
1123 memset(nl, 0, sizeof(nl));
1124 nl[0].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE";
1125 nl[1].n_un.n_name = (char *) "__ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE";
1126 nl[2].n_un.n_name = (char *) "__ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE";
1127 nlist("/System/Library/PrivateFrameworks/WebCore.framework/WebCore", nl);
1129 State (*_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE)(State);
1130 nlset(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, nl, 0);
1131 MSHookFunction(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE, MSHake(_ZN7WebCore13HTMLTokenizer13scriptHandlerENS0_5StateE));
1133 void (*_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE)(void *);
1134 nlset(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, nl, 1);
1135 MSHookFunction(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE, MSHake(_ZN7WebCore13HTMLTokenizer14notifyFinishedEPNS_14CachedResourceE));
1137 bool (*_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE)(const WebCore::String &);
1138 nlset(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, nl, 2);
1139 MSHookFunction(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE, MSHake(_ZN7WebCore16MIMETypeRegistry29isSupportedJavaScriptMIMETypeERKNS_6StringE));
1142 + (id) rootViewController {
1143 return [[[self alloc] init] autorelease];
1147 [self setView:[[[WebCydgetLockScreenView alloc] init] autorelease]];