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 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
571 [book setHook:indirect_];
573 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
575 /* XXX: call createWebViewWithRequest instead? */
577 [self setBackButtonTitle:title_];
579 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
580 [browser loadURL:url];
584 [book setDelegate:delegate_];
585 [page setDelegate:delegate_];
588 [book_ pushBook:book];
596 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
597 if ([WebView canShowMIMEType:type])
600 // XXX: handle more mime types!
603 WebView *webview([webview_ webView]);
604 if (frame == [webview mainFrame])
605 [UIApp openURL:[request URL]];
609 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
610 if (request == nil) ignore: {
615 NSURL *url([request URL]);
617 if (url == nil) use: {
618 if (!error_ && [frame parentFrame] == nil) {
620 [request_ autorelease];
621 request_ = [request retain];
623 NSLog(@"dpn:%@", request_);
629 WebView *webview([webview_ webView]);
630 if (frame == [webview mainFrame])
635 else NSLog(@"nav:%@:%@", url, [action description]);
638 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
641 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
642 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
649 int store(_not(int));
650 if (NSURL *itms = [url itmsURL:&store]) {
652 NSLog(@"itms#%@#%u#%@", url, store, itms);
656 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
657 store == 2 && [capability containsObject:@"com.apple.AppStore"]
664 NSString *scheme([[url scheme] lowercaseString]);
666 if ([scheme isEqualToString:@"tel"]) {
671 if ([scheme isEqualToString:@"mailto"]) {
672 [delegate_ openMailToURL:url];
676 if ([self getSpecial:url swap:YES])
678 else if ([WebView _canHandleRequest:request])
680 else if ([url isSpringboardHandledURL])
686 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
687 //lprintf("Status:%s\n", [text UTF8String]);
690 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
691 NSString *context([sheet context]);
693 if ([context isEqualToString:@"alert"]) {
696 } else if ([context isEqualToString:@"confirm"]) {
699 confirm_ = [NSNumber numberWithBool:YES];
703 confirm_ = [NSNumber numberWithBool:NO];
708 } else if ([context isEqualToString:@"challenge"]) {
709 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
713 NSString *username([[sheet textFieldAtIndex:0] text]);
714 NSString *password([[sheet textFieldAtIndex:1] text]);
716 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
718 [sender useCredential:credential forAuthenticationChallenge:challenge_];
722 [sender cancelAuthenticationChallenge:challenge_];
729 [challenge_ release];
733 } else if ([context isEqualToString:@"submit"]) {
740 [webview_ loadRequest:request_];
751 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
752 challenge_ = [challenge retain];
754 NSURLProtectionSpace *space([challenge protectionSpace]);
755 NSString *realm([space realm]);
759 UIActionSheet *sheet = [[[UIActionSheet alloc]
761 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
767 [sheet setNumberOfRows:1];
769 [sheet addTextFieldWithValue:@"" label:@"username"];
770 [sheet addTextFieldWithValue:@"" label:@"password"];
772 UITextField *username([sheet textFieldAtIndex:0]); {
773 UITextInputTraits *traits([username textInputTraits]);
774 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
775 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
776 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
777 [traits setReturnKeyType:UIReturnKeyNext];
780 UITextField *password([sheet textFieldAtIndex:1]); {
781 UITextInputTraits *traits([password textInputTraits]);
782 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
783 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
784 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
785 // XXX: UIReturnKeyDone
786 [traits setReturnKeyType:UIReturnKeyNext];
787 [traits setSecureTextEntry:YES];
790 [sheet popupAlertAnimated:YES];
793 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
794 return [self _addHeadersToRequest:request];
797 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
798 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
800 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
801 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
804 NSNumber *value([features objectForKey:@"width"]);
805 float width(value == nil ? 0 : [value floatValue]);
807 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
809 /* XXX: deal with cydia:// pages */
810 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
812 if (features != nil && popup_) {
813 [book setDelegate:delegate_];
814 [book setHook:indirect_];
815 [browser setDelegate:delegate_];
817 [browser loadRequest:request];
819 [book setPage:browser];
820 [book_ pushBook:book];
821 } else if (request == nil) {
822 [self setBackButtonTitle:title_];
823 [browser setDelegate:delegate_];
826 [self pushPage:browser];
827 [browser loadRequest:request];
830 return [browser webView];
833 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
834 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
835 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
838 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
839 if ([frame parentFrame] != nil)
842 title_ = [title retain];
843 [book_ reloadTitleForPage:self];
846 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
847 if ([frame parentFrame] != nil)
850 [webview_ resignFirstResponder];
854 [self reloadButtons];
861 if (button_ != nil) {
871 if (function_ != nil) {
876 if (closer_ != nil) {
881 [book_ reloadTitleForPage:self];
883 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
884 [scroller_ setZoomScale:1 duration:0];
886 CGRect webrect = [scroller_ bounds];
887 webrect.size.height = 0;
888 [webview_ setFrame:webrect];
891 - (void) _finishLoading {
894 [self reloadButtons];
902 - (void) reloadButtons {
903 if ([self isLoading])
904 [indicator_ startAnimation];
906 [indicator_ stopAnimation];
907 [super reloadButtons];
910 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
911 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
914 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
915 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
918 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
919 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
922 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
924 return [webview_ webView:sender didCommitLoadForFrame:frame];
927 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
928 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
931 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
932 if ([frame parentFrame] == nil) {
933 [self _finishLoading];
935 if (DOMDocument *document = [frame DOMDocument])
936 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
937 for (DOMHTMLBodyElement *body in bodies) {
938 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
942 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
943 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
944 DOMRGBColor *rgb([color getRGBColorValue]);
946 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
947 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
948 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
949 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
953 if (red == 0xc7 && green == 0xce && blue == 0xd5)
954 uic = [UIColor pinStripeColor];
957 colorWithRed:(red / 255)
965 [scroller_ setBackgroundColor:uic];
971 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
976 return [webview_ webView:sender didFinishLoadForFrame:frame];
979 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
980 if ([frame parentFrame] != nil)
984 [self _finishLoading];
986 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
987 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
988 [[error localizedDescription] stringByAddingPercentEscapes]
994 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
995 #if LogBrowser || ForSaurik
996 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1000 - (void) setViewportWidth:(float)width {
1001 width_ = width ? width != 0 : [[self class] defaultWidth];
1002 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1005 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
1006 if ((self = [super initWithBook:book]) != nil) {
1010 struct CGRect bounds = [self bounds];
1012 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
1013 [self addSubview:scroller_];
1015 [scroller_ setShowBackgroundShadow:NO];
1016 [scroller_ setFixedBackgroundPattern:YES];
1017 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1019 [scroller_ setScrollingEnabled:YES];
1020 [scroller_ setAdjustForContentSizeChange:YES];
1021 [scroller_ setClipsSubviews:YES];
1022 [scroller_ setAllowsRubberBanding:YES];
1023 [scroller_ setScrollDecelerationFactor:0.99];
1024 [scroller_ setDelegate:self];
1026 CGRect webrect = [scroller_ bounds];
1027 webrect.size.height = 0;
1032 webview_ = [Documents_ lastObject];
1033 if (webview_ != nil) {
1034 webview_ = [webview_ retain];
1035 webview = [webview_ webView];
1036 [Documents_ removeLastObject];
1037 [webview_ setFrame:webrect];
1042 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
1043 webview = [webview_ webView];
1045 // XXX: this is terribly (too?) expensive
1046 //[webview_ setDrawsBackground:NO];
1047 [webview setPreferencesIdentifier:@"Cydia"];
1049 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1051 [webview_ setAllowsMessaging:YES];
1053 [webview_ setTilingEnabled:YES];
1054 [webview_ setDrawsGrid:NO];
1055 [webview_ setLogsTilingChanges:NO];
1056 [webview_ setTileMinificationFilter:kCAFilterNearest];
1057 [webview_ setDetectsPhoneNumbers:NO];
1058 [webview_ setAutoresizes:YES];
1060 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1061 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1062 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1063 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1065 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1067 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1068 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1069 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1071 [webview_ _setDocumentType:0x4];
1073 [webview_ setZoomsFocusedFormControl:YES];
1074 [webview_ setContentsPosition:7];
1075 [webview_ setEnabledGestures:0xa];
1076 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1077 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1079 [webview_ setSmoothsFonts:YES];
1080 [webview_ setAllowsImageSheet:YES];
1081 [webview _setUsesLoaderCache:YES];
1083 [webview setGroupName:@"CydiaGroup"];
1084 [webview _setLayoutInterval:0];
1087 [self setViewportWidth:width];
1089 [webview_ setDelegate:self];
1090 [webview_ setGestureDelegate:self];
1091 [webview_ setFormEditingDelegate:self];
1092 [webview_ setInteractionDelegate:self];
1094 [scroller_ addSubview:webview_];
1096 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1098 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1099 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1100 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1102 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1103 NSString *application = package == nil ? @"Cydia" : [NSString
1104 stringWithFormat:@"Cydia/%@",
1108 if (Product_ != nil)
1109 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
1111 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1113 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
1115 /* XXX: lookup application directory? */
1116 /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
1117 if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
1118 application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
1120 [webview setApplicationNameForUserAgent:application];
1122 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1123 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
1125 [webview setFrameLoadDelegate:self];
1126 [webview setResourceLoadDelegate:indirect_];
1127 [webview setUIDelegate:self];
1128 [webview setScriptDebugDelegate:self];
1129 [webview setPolicyDelegate:self];
1131 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1132 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1136 - (id) initWithBook:(RVBook *)book {
1137 return [self initWithBook:book forWidth:0];
1140 - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1141 [webview_ redrawScaledDocument];
1144 - (void) callFunction:(WebScriptObject *)function {
1145 WebView *webview([webview_ webView]);
1146 WebFrame *frame([webview mainFrame]);
1148 id _private(MSHookIvar<id>(webview, "_private"));
1149 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1150 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1153 if (settings == NULL)
1156 no = settings->JavaScriptCanOpenWindowsAutomatically();
1157 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1160 [delegate_ clearFirstResponder];
1161 JSObjectRef object([function JSObject]);
1162 JSGlobalContextRef context([frame globalContext]);
1163 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1165 if (settings != NULL)
1166 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1169 - (void) didCloseBook:(RVBook *)book {
1171 [self callFunction:closer_];
1174 - (void) __rightButtonClicked {
1179 - (void) _rightButtonClicked {
1180 if (function_ == nil)
1181 [self __rightButtonClicked];
1183 [self callFunction:function_];
1186 - (id) _rightButtonTitle {
1190 - (id) rightButtonTitle {
1191 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
1194 - (UINavigationButtonStyle) rightButtonStyle {
1195 if (style_ == nil) normal:
1196 return UINavigationButtonStyleNormal;
1197 else if ([style_ isEqualToString:@"Normal"])
1198 return UINavigationButtonStyleNormal;
1199 else if ([style_ isEqualToString:@"Back"])
1200 return UINavigationButtonStyleBack;
1201 else if ([style_ isEqualToString:@"Highlighted"])
1202 return UINavigationButtonStyleHighlighted;
1203 else if ([style_ isEqualToString:@"Destructive"])
1204 return UINavigationButtonStyleDestructive;
1208 - (NSString *) title {
1209 return title_ == nil ? @"Loading" : title_;
1212 - (NSString *) backButtonTitle {
1216 - (void) setPageActive:(BOOL)active {
1218 [indicator_ removeFromSuperview];
1220 [[book_ navigationBar] addSubview:indicator_];
1223 - (void) resetViewAnimated:(BOOL)animated {
1226 - (void) setPushed:(bool)pushed {
1230 + (float) defaultWidth {