1 #include <BrowserView.h>
3 /* Indirect Delegate {{{ */
4 @interface IndirectDelegate : NSProxy {
5 _transient volatile id delegate_;
8 - (void) setDelegate:(id)delegate;
9 - (id) initWithDelegate:(id)delegate;
12 @implementation IndirectDelegate
14 - (void) setDelegate:(id)delegate {
18 - (id) initWithDelegate:(id)delegate {
23 - (BOOL) respondsToSelector:(SEL)sel {
24 return delegate_ == nil ? FALSE : [delegate_ respondsToSelector:sel];
27 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
29 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
31 // XXX: I fucking hate Apple so very very bad
32 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
35 - (void) forwardInvocation:(NSInvocation *)inv {
36 SEL sel = [inv selector];
37 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
38 [inv invokeWithTarget:delegate_];
44 @interface WebView (Cydia)
45 - (void) setScriptDebugDelegate:(id)delegate;
46 - (void) _setFormDelegate:(id)delegate;
47 - (void) _setUIKitDelegate:(id)delegate;
48 - (void) setWebMailDelegate:(id)delegate;
49 - (void) _setLayoutInterval:(float)interval;
52 /* Web Scripting {{{ */
53 @interface CydiaObject : NSObject {
57 - (id) initWithDelegate:(IndirectDelegate *)indirect;
60 @implementation CydiaObject
67 - (id) initWithDelegate:(IndirectDelegate *)indirect {
68 if ((self = [super init]) != nil) {
69 indirect_ = [indirect retain];
73 + (NSArray *) _attributeKeys {
74 return [NSArray arrayWithObjects:@"device", nil];
77 - (NSArray *) attributeKeys {
78 return [[self class] _attributeKeys];
81 + (BOOL) isKeyExcludedFromWebScript:(const char *)name {
82 return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
85 - (NSString *) device {
86 return [[UIDevice currentDevice] uniqueIdentifier];
89 + (NSString *) webScriptNameForSelector:(SEL)selector {
90 if (selector == @selector(close))
92 else if (selector == @selector(getPackageById:))
93 return @"getPackageById";
94 else if (selector == @selector(setAutoPopup:))
95 return @"setAutoPopup";
96 else if (selector == @selector(setButtonImage:withStyle:toFunction:))
97 return @"setButtonImage";
98 else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
99 return @"setButtonTitle";
100 else if (selector == @selector(setPopupHook:))
101 return @"setPopupHook";
102 else if (selector == @selector(setViewportWidth:))
103 return @"setViewportWidth";
104 else if (selector == @selector(supports:))
106 else if (selector == @selector(du:))
108 else if (selector == @selector(statfs:))
114 + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
115 return [self webScriptNameForSelector:selector] == nil;
118 - (BOOL) supports:(NSString *)feature {
119 return [feature isEqualToString:@"window.open"];
122 - (Package *) getPackageById:(NSString *)id {
123 return [[Database sharedInstance] packageWithName:id];
126 - (NSArray *) statfs:(NSString *)path {
129 if (path == nil || statfs([path UTF8String], &stat) == -1)
132 return [NSArray arrayWithObjects:
133 [NSNumber numberWithUnsignedLong:stat.f_bsize],
134 [NSNumber numberWithUnsignedLong:stat.f_blocks],
135 [NSNumber numberWithUnsignedLong:stat.f_bfree],
139 - (NSNumber *) du:(NSString *)path {
140 NSNumber *value(nil);
143 _assert(pipe(fds) != -1);
145 pid_t pid(ExecFork());
147 _assert(dup2(fds[1], 1) != -1);
148 _assert(close(fds[0]) != -1);
149 _assert(close(fds[1]) != -1);
150 execlp("du", "du", "-s", [path UTF8String], NULL);
155 _assert(close(fds[1]) != -1);
157 if (FILE *du = fdopen(fds[0], "r")) {
159 while (fgets(line, sizeof(line), du) != NULL) {
160 size_t length(strlen(line));
161 while (length != 0 && line[length - 1] == '\n')
162 line[--length] = '\0';
163 if (char *tab = strchr(line, '\t')) {
165 value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)];
170 } else _assert(close(fds[0]));
174 if (waitpid(pid, &status, 0) == -1)
186 - (void) setAutoPopup:(BOOL)popup {
187 [indirect_ setAutoPopup:popup];
190 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
191 [indirect_ setButtonImage:button withStyle:style toFunction:function];
194 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
195 [indirect_ setButtonTitle:button withStyle:style toFunction:function];
198 - (void) setPopupHook:(id)function {
199 [indirect_ setPopupHook:function];
202 - (void) setViewportWidth:(float)width {
203 [indirect_ setViewportWidth:width];
209 @implementation BrowserView
212 #include "Internals.h"
217 NSLog(@"[BrowserView dealloc]");
220 if (challenge_ != nil)
221 [challenge_ release];
223 WebView *webview = [webview_ webView];
224 [webview setFrameLoadDelegate:nil];
225 [webview setResourceLoadDelegate:nil];
226 [webview setUIDelegate:nil];
227 [webview setScriptDebugDelegate:nil];
228 [webview setPolicyDelegate:nil];
230 [webview setDownloadDelegate:nil];
232 [webview _setFormDelegate:nil];
233 [webview _setUIKitDelegate:nil];
234 [webview setWebMailDelegate:nil];
235 [webview setEditingDelegate:nil];
237 [webview_ setDelegate:nil];
238 [webview_ setGestureDelegate:nil];
239 [webview_ setFormEditingDelegate:nil];
240 [webview_ setInteractionDelegate:nil];
242 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
247 [webview_ removeFromSuperview];
248 [Documents_ addObject:[webview_ autorelease]];
253 [indirect_ setDelegate:nil];
258 [scroller_ setDelegate:nil];
264 if (function_ != nil)
270 [indicator_ release];
278 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
279 [self loadRequest:[NSURLRequest
286 - (void) loadURL:(NSURL *)url {
287 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
290 - (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
291 NSMutableURLRequest *copy = [request mutableCopy];
293 if (Machine_ != NULL)
294 [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
295 if (UniqueID_ != nil)
296 [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
299 [copy setValue:Role_ forHTTPHeaderField:@"X-Role"];
304 - (void) loadRequest:(NSURLRequest *)request {
307 [webview_ loadRequest:request];
314 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
315 [self loadRequest:request_];
317 UIActionSheet *sheet = [[[UIActionSheet alloc]
318 initWithTitle:@"Are you sure you want to submit this form again?"
319 buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil]
325 [sheet setNumberOfRows:1];
326 [sheet popupAlertAnimated:YES];
330 - (WebView *) webView {
331 return [webview_ webView];
334 - (UIWebDocumentView *) documentView {
338 - (void) _fixScroller {
339 CGRect bounds([webview_ documentBounds]);
341 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
348 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
349 CGRect peripheral([assistant peripheralFrame]);
351 NSLog(@"per:%f", peripheral.size.height);
353 extra = peripheral.size.height;
356 CGRect subrect([scroller_ frame]);
357 subrect.size.height -= extra;
358 [scroller_ setScrollerIndicatorSubrect:subrect];
360 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
361 [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
364 size.height += extra;
365 [scroller_ setContentSize:size];
367 [scroller_ releaseRubberBandIfNecessary];
370 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
373 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
378 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
379 [self view:sender didSetFrame:frame];
382 - (void) pushPage:(RVPage *)page {
383 [page setDelegate:delegate_];
384 [self setBackButtonTitle:title_];
385 [book_ pushPage:page];
393 [book_ pushPage:self];
396 - (void) swapPage:(RVPage *)page {
397 [page setDelegate:delegate_];
399 [book_ swapPage:page];
401 [book_ pushPage:page];
404 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
406 NSLog(@"getSpecial:%@", url);
409 NSString *href([url absoluteString]);
410 NSString *scheme([[url scheme] lowercaseString]);
414 if ([href hasPrefix:@"apptapp://package/"])
415 page = [delegate_ pageForPackage:[href substringFromIndex:18]];
416 else if ([scheme isEqualToString:@"cydia"]) {
417 page = [delegate_ pageForURL:url hasTag:NULL];
420 } else if (![scheme isEqualToString:@"apptapp"])
425 [self swapPage:page];
427 [self pushPage:page];
431 - (void) webViewShow:(WebView *)sender {
432 /* XXX: this is where I cry myself to sleep */
435 - (bool) _allowJavaScriptPanel {
439 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
440 if (![self _allowJavaScriptPanel])
445 UIActionSheet *sheet = [[[UIActionSheet alloc]
447 buttons:[NSArray arrayWithObjects:@"OK", nil]
453 [sheet setBodyText:message];
454 [sheet popupAlertAnimated:YES];
457 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
458 if (![self _allowJavaScriptPanel])
461 UIActionSheet *sheet = [[[UIActionSheet alloc]
463 buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
469 [sheet setNumberOfRows:1];
470 [sheet setBodyText:message];
471 [sheet popupAlertAnimated:YES];
473 NSRunLoop *loop([NSRunLoop currentRunLoop]);
474 NSDate *future([NSDate distantFuture]);
476 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
478 NSNumber *confirm([confirm_ autorelease]);
480 return [confirm boolValue];
483 - (void) setAutoPopup:(BOOL)popup {
487 - (void) setPopupHook:(id)function {
489 [closer_ autorelease];
490 closer_ = function == nil ? nil : [function retain];
493 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
495 [button_ autorelease];
496 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
499 [style_ autorelease];
500 style_ = style == nil ? nil : [style retain];
502 if (function_ != nil)
503 [function_ autorelease];
504 function_ = function == nil ? nil : [function retain];
506 [self reloadButtons];
509 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
511 [button_ autorelease];
512 button_ = button == nil ? nil : [button retain];
515 [style_ autorelease];
516 style_ = style == nil ? nil : [style retain];
518 if (function_ != nil)
519 [function_ autorelease];
520 function_ = function == nil ? nil : [function retain];
522 [self reloadButtons];
525 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
529 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
533 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
538 - (void) webViewClose:(WebView *)sender {
546 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
547 [window setValue:cydia_ forKey:@"cydia"];
550 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
551 NSLog(@"err:%@", error);
554 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
556 NSLog(@"nwa:%@", name);
559 if (NSURL *url = [request URL]) {
560 if (name == nil) unknown: {
561 if (![self getSpecial:url swap:NO]) {
562 NSString *scheme([[url scheme] lowercaseString]);
563 if ([scheme isEqualToString:@"mailto"])
564 [delegate_ openMailToURL:url];
567 } else if ([name isEqualToString:@"_open"])
568 [delegate_ openURL:url];
569 else if ([name isEqualToString:@"_popup"]) {
570 NSString *scheme([[url scheme] lowercaseString]);
571 if ([scheme isEqualToString:@"mailto"])
572 [delegate_ openMailToURL:url];
574 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
575 [book setHook:indirect_];
577 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
579 /* XXX: call createWebViewWithRequest instead? */
581 [self setBackButtonTitle:title_];
583 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
584 [browser loadURL:url];
588 [book setDelegate:delegate_];
589 [page setDelegate:delegate_];
592 [book_ pushBook:book];
601 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
602 if ([WebView canShowMIMEType:type])
605 // XXX: handle more mime types!
608 WebView *webview([webview_ webView]);
609 if (frame == [webview mainFrame])
610 [UIApp openURL:[request URL]];
614 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
615 if (request == nil) ignore: {
620 NSURL *url([request URL]);
622 if (url == nil) use: {
623 if (!error_ && [frame parentFrame] == nil) {
625 [request_ autorelease];
626 request_ = [request retain];
628 NSLog(@"dpn:%@", request_);
634 WebView *webview([webview_ webView]);
635 if (frame == [webview mainFrame])
640 else NSLog(@"nav:%@:%@", url, [action description]);
643 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
646 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
647 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
654 int store(_not(int));
655 if (NSURL *itms = [url itmsURL:&store]) {
657 NSLog(@"itms#%@#%u#%@", url, store, itms);
661 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
662 store == 2 && [capability containsObject:@"com.apple.AppStore"]
669 NSString *scheme([[url scheme] lowercaseString]);
671 if ([scheme isEqualToString:@"tel"]) {
676 if ([scheme isEqualToString:@"mailto"]) {
677 [delegate_ openMailToURL:url];
681 if ([self getSpecial:url swap:YES])
683 else if ([WebView _canHandleRequest:request])
685 else if ([url isSpringboardHandledURL])
691 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
692 //lprintf("Status:%s\n", [text UTF8String]);
695 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
696 NSString *context([sheet context]);
698 if ([context isEqualToString:@"alert"]) {
701 } else if ([context isEqualToString:@"confirm"]) {
704 confirm_ = [NSNumber numberWithBool:YES];
708 confirm_ = [NSNumber numberWithBool:NO];
713 } else if ([context isEqualToString:@"challenge"]) {
714 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
718 NSString *username([[sheet textFieldAtIndex:0] text]);
719 NSString *password([[sheet textFieldAtIndex:1] text]);
721 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
723 [sender useCredential:credential forAuthenticationChallenge:challenge_];
727 [sender cancelAuthenticationChallenge:challenge_];
734 [challenge_ release];
738 } else if ([context isEqualToString:@"submit"]) {
745 [webview_ loadRequest:request_];
756 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
757 challenge_ = [challenge retain];
759 NSURLProtectionSpace *space([challenge protectionSpace]);
760 NSString *realm([space realm]);
764 UIActionSheet *sheet = [[[UIActionSheet alloc]
766 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
772 [sheet setNumberOfRows:1];
774 [sheet addTextFieldWithValue:@"" label:@"username"];
775 [sheet addTextFieldWithValue:@"" label:@"password"];
777 UITextField *username([sheet textFieldAtIndex:0]); {
778 UITextInputTraits *traits([username textInputTraits]);
779 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
780 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
781 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
782 [traits setReturnKeyType:UIReturnKeyNext];
785 UITextField *password([sheet textFieldAtIndex:1]); {
786 UITextInputTraits *traits([password textInputTraits]);
787 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
788 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
789 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
790 // XXX: UIReturnKeyDone
791 [traits setReturnKeyType:UIReturnKeyNext];
792 [traits setSecureTextEntry:YES];
795 [sheet popupAlertAnimated:YES];
798 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
799 return [self _addHeadersToRequest:request];
802 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
803 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
805 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
806 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
809 NSNumber *value([features objectForKey:@"width"]);
810 float width(value == nil ? 0 : [value floatValue]);
812 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
814 /* XXX: deal with cydia:// pages */
815 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
817 if (features != nil && popup_) {
818 [book setDelegate:delegate_];
819 [book setHook:indirect_];
820 [browser setDelegate:delegate_];
822 [browser loadRequest:request];
824 [book setPage:browser];
825 [book_ pushBook:book];
826 } else if (request == nil) {
827 [self setBackButtonTitle:title_];
828 [browser setDelegate:delegate_];
831 [self pushPage:browser];
832 [browser loadRequest:request];
835 return [browser webView];
838 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
839 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
840 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
843 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
844 if ([frame parentFrame] != nil)
847 title_ = [title retain];
848 [book_ reloadTitleForPage:self];
851 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
852 if ([frame parentFrame] != nil)
855 [webview_ resignFirstResponder];
859 [self reloadButtons];
866 if (button_ != nil) {
876 if (function_ != nil) {
881 if (closer_ != nil) {
886 [book_ reloadTitleForPage:self];
888 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
889 [scroller_ setZoomScale:1 duration:0];
891 CGRect webrect = [scroller_ bounds];
892 webrect.size.height = 0;
893 [webview_ setFrame:webrect];
896 - (void) _finishLoading {
899 [self reloadButtons];
907 - (void) reloadButtons {
908 if ([self isLoading])
909 [indicator_ startAnimation];
911 [indicator_ stopAnimation];
912 [super reloadButtons];
915 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
916 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
919 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
920 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
923 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
924 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
927 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
929 return [webview_ webView:sender didCommitLoadForFrame:frame];
932 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
933 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
936 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
937 if ([frame parentFrame] == nil) {
938 [self _finishLoading];
940 if (DOMDocument *document = [frame DOMDocument])
941 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
942 for (DOMHTMLBodyElement *body in bodies) {
943 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
947 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
948 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
949 DOMRGBColor *rgb([color getRGBColorValue]);
951 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
952 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
953 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
954 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
958 if (red == 0xc7 && green == 0xce && blue == 0xd5)
959 uic = [UIColor pinStripeColor];
962 colorWithRed:(red / 255)
970 [scroller_ setBackgroundColor:uic];
976 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
981 return [webview_ webView:sender didFinishLoadForFrame:frame];
984 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
985 if ([frame parentFrame] != nil)
989 [self _finishLoading];
991 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
992 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
993 [[error localizedDescription] stringByAddingPercentEscapes]
999 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1000 #if LogBrowser || ForSaurik
1001 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1005 - (void) setViewportWidth:(float)width {
1006 width_ = width ? width != 0 : [[self class] defaultWidth];
1007 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1010 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
1011 if ((self = [super initWithBook:book]) != nil) {
1015 struct CGRect bounds = [self bounds];
1017 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
1018 [self addSubview:scroller_];
1020 [scroller_ setShowBackgroundShadow:NO];
1021 [scroller_ setFixedBackgroundPattern:YES];
1022 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1024 [scroller_ setScrollingEnabled:YES];
1025 [scroller_ setAdjustForContentSizeChange:YES];
1026 [scroller_ setClipsSubviews:YES];
1027 [scroller_ setAllowsRubberBanding:YES];
1028 [scroller_ setScrollDecelerationFactor:0.99];
1029 [scroller_ setDelegate:self];
1031 CGRect webrect = [scroller_ bounds];
1032 webrect.size.height = 0;
1037 webview_ = [Documents_ lastObject];
1038 if (webview_ != nil) {
1039 webview_ = [webview_ retain];
1040 webview = [webview_ webView];
1041 [Documents_ removeLastObject];
1042 [webview_ setFrame:webrect];
1047 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
1048 webview = [webview_ webView];
1050 // XXX: this is terribly (too?) expensive
1051 //[webview_ setDrawsBackground:NO];
1052 [webview setPreferencesIdentifier:@"Cydia"];
1054 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1056 [webview_ setAllowsMessaging:YES];
1058 [webview_ setTilingEnabled:YES];
1059 [webview_ setDrawsGrid:NO];
1060 [webview_ setLogsTilingChanges:NO];
1061 [webview_ setTileMinificationFilter:kCAFilterNearest];
1062 [webview_ setDetectsPhoneNumbers:NO];
1063 [webview_ setAutoresizes:YES];
1065 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1066 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1067 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1068 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1070 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1072 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1073 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1074 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1076 [webview_ _setDocumentType:0x4];
1078 [webview_ setZoomsFocusedFormControl:YES];
1079 [webview_ setContentsPosition:7];
1080 [webview_ setEnabledGestures:0xa];
1081 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1082 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1084 [webview_ setSmoothsFonts:YES];
1085 [webview_ setAllowsImageSheet:YES];
1086 [webview _setUsesLoaderCache:YES];
1088 [webview setGroupName:@"CydiaGroup"];
1089 [webview _setLayoutInterval:0];
1092 [self setViewportWidth:width];
1094 [webview_ setDelegate:self];
1095 [webview_ setGestureDelegate:self];
1096 [webview_ setFormEditingDelegate:self];
1097 [webview_ setInteractionDelegate:self];
1099 [scroller_ addSubview:webview_];
1101 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1103 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1104 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1105 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1107 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1108 NSString *application = package == nil ? @"Cydia" : [NSString
1109 stringWithFormat:@"Cydia/%@",
1113 if (Product_ != nil)
1114 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
1116 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1118 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
1120 /* XXX: lookup application directory? */
1121 /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
1122 if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
1123 application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
1125 [webview setApplicationNameForUserAgent:application];
1127 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1128 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
1130 [webview setFrameLoadDelegate:self];
1131 [webview setResourceLoadDelegate:indirect_];
1132 [webview setUIDelegate:self];
1133 [webview setScriptDebugDelegate:self];
1134 [webview setPolicyDelegate:self];
1136 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1137 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1141 - (id) initWithBook:(RVBook *)book {
1142 return [self initWithBook:book forWidth:0];
1145 - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1146 [webview_ redrawScaledDocument];
1149 - (void) callFunction:(WebScriptObject *)function {
1150 WebView *webview([webview_ webView]);
1151 WebFrame *frame([webview mainFrame]);
1153 id _private(MSHookIvar<id>(webview, "_private"));
1154 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1155 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1158 if (settings == NULL)
1161 no = settings->JavaScriptCanOpenWindowsAutomatically();
1162 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1165 [delegate_ clearFirstResponder];
1166 JSObjectRef object([function JSObject]);
1167 JSGlobalContextRef context([frame globalContext]);
1168 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1170 if (settings != NULL)
1171 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1174 - (void) didCloseBook:(RVBook *)book {
1176 [self callFunction:closer_];
1179 - (void) __rightButtonClicked {
1184 - (void) _rightButtonClicked {
1185 if (function_ == nil)
1186 [self __rightButtonClicked];
1188 [self callFunction:function_];
1191 - (id) _rightButtonTitle {
1195 - (id) rightButtonTitle {
1196 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
1199 - (UINavigationButtonStyle) rightButtonStyle {
1200 if (style_ == nil) normal:
1201 return UINavigationButtonStyleNormal;
1202 else if ([style_ isEqualToString:@"Normal"])
1203 return UINavigationButtonStyleNormal;
1204 else if ([style_ isEqualToString:@"Back"])
1205 return UINavigationButtonStyleBack;
1206 else if ([style_ isEqualToString:@"Highlighted"])
1207 return UINavigationButtonStyleHighlighted;
1208 else if ([style_ isEqualToString:@"Destructive"])
1209 return UINavigationButtonStyleDestructive;
1213 - (NSString *) title {
1214 return title_ == nil ? @"Loading" : title_;
1217 - (NSString *) backButtonTitle {
1221 - (void) setPageActive:(BOOL)active {
1223 [indicator_ removeFromSuperview];
1225 [[book_ navigationBar] addSubview:indicator_];
1228 - (void) resetViewAnimated:(BOOL)animated {
1231 - (void) setPushed:(bool)pushed {
1235 + (float) defaultWidth {