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 + (NSString *) webScriptNameForSelector:(SEL)selector {
74 if (selector == @selector(getPackageById:))
75 return @"getPackageById";
76 else if (selector == @selector(setAutoPopup:))
77 return @"setAutoPopup";
78 else if (selector == @selector(setButtonImage:withStyle:toFunction:))
79 return @"setButtonImage";
80 else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
81 return @"setButtonTitle";
82 else if (selector == @selector(setPopupHook:))
83 return @"setPopupHook";
84 else if (selector == @selector(setViewportWidth:))
85 return @"setViewportWidth";
86 else if (selector == @selector(supports:))
88 else if (selector == @selector(du:))
90 else if (selector == @selector(statfs:))
96 + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
97 return [self webScriptNameForSelector:selector] == nil;
100 - (BOOL) supports:(NSString *)feature {
101 return [feature isEqualToString:@"window.open"];
104 - (Package *) getPackageById:(NSString *)id {
105 return [[Database sharedInstance] packageWithName:id];
108 - (NSArray *) statfs:(NSString *)path {
111 if (path == nil || statfs([path UTF8String], &stat) == -1)
114 return [NSArray arrayWithObjects:
115 [NSNumber numberWithUnsignedLong:stat.f_bsize],
116 [NSNumber numberWithUnsignedLong:stat.f_blocks],
117 [NSNumber numberWithUnsignedLong:stat.f_bfree],
121 - (NSNumber *) du:(NSString *)path {
122 NSNumber *value(nil);
125 _assert(pipe(fds) != -1);
127 pid_t pid(ExecFork());
129 _assert(dup2(fds[1], 1) != -1);
130 _assert(close(fds[0]) != -1);
131 _assert(close(fds[1]) != -1);
132 execlp("du", "du", "-s", [path UTF8String], NULL);
137 _assert(close(fds[1]) != -1);
139 if (FILE *du = fdopen(fds[0], "r")) {
141 while (fgets(line, sizeof(line), du) != NULL) {
142 size_t length(strlen(line));
143 while (length != 0 && line[length - 1] == '\n')
144 line[--length] = '\0';
145 if (char *tab = strchr(line, '\t')) {
147 value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)];
152 } else _assert(close(fds[0]));
156 if (waitpid(pid, &status, 0) == -1)
164 - (void) setAutoPopup:(BOOL)popup {
165 [indirect_ setAutoPopup:popup];
168 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
169 [indirect_ setButtonImage:button withStyle:style toFunction:function];
172 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
173 [indirect_ setButtonTitle:button withStyle:style toFunction:function];
176 - (void) setPopupHook:(id)function {
177 [indirect_ setPopupHook:function];
180 - (void) setViewportWidth:(float)width {
181 [indirect_ setViewportWidth:width];
187 @implementation BrowserView
190 #include "Internals.h"
195 NSLog(@"[BrowserView dealloc]");
198 if (challenge_ != nil)
199 [challenge_ release];
201 WebView *webview = [webview_ webView];
202 [webview setFrameLoadDelegate:nil];
203 [webview setResourceLoadDelegate:nil];
204 [webview setUIDelegate:nil];
205 [webview setScriptDebugDelegate:nil];
206 [webview setPolicyDelegate:nil];
208 [webview setDownloadDelegate:nil];
210 [webview _setFormDelegate:nil];
211 [webview _setUIKitDelegate:nil];
212 [webview setWebMailDelegate:nil];
213 [webview setEditingDelegate:nil];
215 [webview_ setDelegate:nil];
216 [webview_ setGestureDelegate:nil];
217 [webview_ setFormEditingDelegate:nil];
218 [webview_ setInteractionDelegate:nil];
220 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
225 [webview_ removeFromSuperview];
226 [Documents_ addObject:[webview_ autorelease]];
231 [indirect_ setDelegate:nil];
236 [scroller_ setDelegate:nil];
242 if (function_ != nil)
248 [indicator_ release];
256 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
257 [self loadRequest:[NSURLRequest
264 - (void) loadURL:(NSURL *)url {
265 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
268 - (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
269 NSMutableURLRequest *copy = [request mutableCopy];
271 if (Machine_ != NULL)
272 [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
273 if (UniqueID_ != nil)
274 [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
277 [copy setValue:Role_ forHTTPHeaderField:@"X-Role"];
282 - (void) loadRequest:(NSURLRequest *)request {
285 [webview_ loadRequest:request];
292 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
293 [self loadRequest:request_];
295 UIActionSheet *sheet = [[[UIActionSheet alloc]
296 initWithTitle:@"Are you sure you want to submit this form again?"
297 buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil]
303 [sheet setNumberOfRows:1];
304 [sheet popupAlertAnimated:YES];
308 - (WebView *) webView {
309 return [webview_ webView];
312 - (UIWebDocumentView *) documentView {
316 - (void) _fixScroller {
317 CGRect bounds([webview_ documentBounds]);
319 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
326 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
327 CGRect peripheral([assistant peripheralFrame]);
329 NSLog(@"per:%f", peripheral.size.height);
331 extra = peripheral.size.height;
334 CGRect subrect([scroller_ frame]);
335 subrect.size.height -= extra;
336 [scroller_ setScrollerIndicatorSubrect:subrect];
338 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
339 [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
342 size.height += extra;
343 [scroller_ setContentSize:size];
345 [scroller_ releaseRubberBandIfNecessary];
348 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
351 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
356 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
357 [self view:sender didSetFrame:frame];
360 - (void) pushPage:(RVPage *)page {
361 [page setDelegate:delegate_];
362 [self setBackButtonTitle:title_];
363 [book_ pushPage:page];
371 [book_ pushPage:self];
374 - (void) swapPage:(RVPage *)page {
375 [page setDelegate:delegate_];
377 [book_ swapPage:page];
379 [book_ pushPage:page];
382 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
384 NSLog(@"getSpecial:%@", url);
387 NSString *href([url absoluteString]);
388 NSString *scheme([[url scheme] lowercaseString]);
392 if ([href hasPrefix:@"apptapp://package/"])
393 page = [delegate_ pageForPackage:[href substringFromIndex:18]];
394 else if ([scheme isEqualToString:@"cydia"]) {
395 page = [delegate_ pageForURL:url hasTag:NULL];
398 } else if (![scheme isEqualToString:@"apptapp"])
403 [self swapPage:page];
405 [self pushPage:page];
409 - (void) webViewShow:(WebView *)sender {
410 /* XXX: this is where I cry myself to sleep */
413 - (bool) _allowJavaScriptPanel {
417 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
418 if (![self _allowJavaScriptPanel])
423 UIActionSheet *sheet = [[[UIActionSheet alloc]
425 buttons:[NSArray arrayWithObjects:@"OK", nil]
431 [sheet setBodyText:message];
432 [sheet popupAlertAnimated:YES];
435 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
436 if (![self _allowJavaScriptPanel])
439 UIActionSheet *sheet = [[[UIActionSheet alloc]
441 buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
447 [sheet setNumberOfRows:1];
448 [sheet setBodyText:message];
449 [sheet popupAlertAnimated:YES];
451 NSRunLoop *loop([NSRunLoop currentRunLoop]);
452 NSDate *future([NSDate distantFuture]);
454 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
456 NSNumber *confirm([confirm_ autorelease]);
458 return [confirm boolValue];
461 - (void) setAutoPopup:(BOOL)popup {
465 - (void) setPopupHook:(id)function {
467 [closer_ autorelease];
468 closer_ = function == nil ? nil : [function retain];
471 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
473 [button_ autorelease];
474 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
477 [style_ autorelease];
478 style_ = style == nil ? nil : [style retain];
480 if (function_ != nil)
481 [function_ autorelease];
482 function_ = function == nil ? nil : [function retain];
484 [self reloadButtons];
487 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
489 [button_ autorelease];
490 button_ = button == nil ? nil : [button retain];
493 [style_ autorelease];
494 style_ = style == nil ? nil : [style retain];
496 if (function_ != nil)
497 [function_ autorelease];
498 function_ = function == nil ? nil : [function retain];
500 [self reloadButtons];
503 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
507 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
511 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
516 - (void) webViewClose:(WebView *)sender {
520 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
521 [window setValue:cydia_ forKey:@"cydia"];
524 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
525 NSLog(@"err:%@", error);
528 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
530 NSLog(@"nwa:%@", name);
533 if (NSURL *url = [request URL]) {
534 if (name == nil) unknown: {
535 if (![self getSpecial:url swap:NO]) {
536 NSString *scheme([[url scheme] lowercaseString]);
537 if ([scheme isEqualToString:@"mailto"])
538 [delegate_ openMailToURL:url];
541 } else if ([name isEqualToString:@"_open"])
542 [delegate_ openURL:url];
543 else if ([name isEqualToString:@"_popup"]) {
544 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
545 [book setHook:indirect_];
547 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
549 /* XXX: call createWebViewWithRequest instead? */
551 [self setBackButtonTitle:title_];
553 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
554 [browser loadURL:url];
558 [book setDelegate:delegate_];
559 [page setDelegate:delegate_];
562 [book_ pushBook:book];
570 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
571 if ([WebView canShowMIMEType:type])
574 // XXX: handle more mime types!
577 WebView *webview([webview_ webView]);
578 if (frame == [webview mainFrame])
579 [UIApp openURL:[request URL]];
583 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
584 if (request == nil) ignore: {
589 NSURL *url([request URL]);
591 if (url == nil) use: {
592 if (!error_ && [frame parentFrame] == nil) {
594 [request_ autorelease];
595 request_ = [request retain];
597 NSLog(@"dpn:%@", request_);
603 WebView *webview([webview_ webView]);
604 if (frame == [webview mainFrame])
609 else NSLog(@"nav:%@:%@", url, [action description]);
612 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
615 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
616 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
623 int store(_not(int));
624 if (NSURL *itms = [url itmsURL:&store]) {
626 NSLog(@"itms#%@#%u#%@", url, store, itms);
630 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
631 store == 2 && [capability containsObject:@"com.apple.AppStore"]
638 NSString *scheme([[url scheme] lowercaseString]);
640 if ([scheme isEqualToString:@"tel"]) {
645 if ([scheme isEqualToString:@"mailto"]) {
646 [delegate_ openMailToURL:url];
650 if ([self getSpecial:url swap:YES])
652 else if ([WebView _canHandleRequest:request])
654 else if ([url isSpringboardHandledURL])
660 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
661 //lprintf("Status:%s\n", [text UTF8String]);
664 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
665 NSString *context([sheet context]);
667 if ([context isEqualToString:@"alert"]) {
670 } else if ([context isEqualToString:@"confirm"]) {
673 confirm_ = [NSNumber numberWithBool:YES];
677 confirm_ = [NSNumber numberWithBool:NO];
682 } else if ([context isEqualToString:@"challenge"]) {
683 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
687 NSString *username([[sheet textFieldAtIndex:0] text]);
688 NSString *password([[sheet textFieldAtIndex:1] text]);
690 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
692 [sender useCredential:credential forAuthenticationChallenge:challenge_];
696 [sender cancelAuthenticationChallenge:challenge_];
703 [challenge_ release];
707 } else if ([context isEqualToString:@"submit"]) {
714 [webview_ loadRequest:request_];
725 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
726 challenge_ = [challenge retain];
728 NSURLProtectionSpace *space([challenge protectionSpace]);
729 NSString *realm([space realm]);
733 UIActionSheet *sheet = [[[UIActionSheet alloc]
735 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
741 [sheet setNumberOfRows:1];
743 [sheet addTextFieldWithValue:@"" label:@"username"];
744 [sheet addTextFieldWithValue:@"" label:@"password"];
746 UITextField *username([sheet textFieldAtIndex:0]); {
747 UITextInputTraits *traits([username textInputTraits]);
748 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
749 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
750 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
751 [traits setReturnKeyType:UIReturnKeyNext];
754 UITextField *password([sheet textFieldAtIndex:1]); {
755 UITextInputTraits *traits([password textInputTraits]);
756 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
757 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
758 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
759 // XXX: UIReturnKeyDone
760 [traits setReturnKeyType:UIReturnKeyNext];
761 [traits setSecureTextEntry:YES];
764 [sheet popupAlertAnimated:YES];
767 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
768 return [self _addHeadersToRequest:request];
771 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
772 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
774 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
775 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
778 NSNumber *value([features objectForKey:@"width"]);
779 float width(value == nil ? 0 : [value floatValue]);
781 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
783 /* XXX: deal with cydia:// pages */
784 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
786 if (features != nil && popup_) {
787 [book setDelegate:delegate_];
788 [book setHook:indirect_];
789 [browser setDelegate:delegate_];
791 [browser loadRequest:request];
793 [book setPage:browser];
794 [book_ pushBook:book];
795 } else if (request == nil) {
796 [self setBackButtonTitle:title_];
797 [browser setDelegate:delegate_];
800 [self pushPage:browser];
801 [browser loadRequest:request];
804 return [browser webView];
807 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
808 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
809 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
812 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
813 if ([frame parentFrame] != nil)
816 title_ = [title retain];
817 [book_ reloadTitleForPage:self];
820 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
821 if ([frame parentFrame] != nil)
824 [webview_ resignFirstResponder];
828 [self reloadButtons];
835 if (button_ != nil) {
845 if (function_ != nil) {
850 if (closer_ != nil) {
855 [book_ reloadTitleForPage:self];
857 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
858 [scroller_ setZoomScale:1 duration:0];
860 CGRect webrect = [scroller_ bounds];
861 webrect.size.height = 0;
862 [webview_ setFrame:webrect];
865 - (void) _finishLoading {
868 [self reloadButtons];
876 - (void) reloadButtons {
877 if ([self isLoading])
878 [indicator_ startAnimation];
880 [indicator_ stopAnimation];
881 [super reloadButtons];
884 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
885 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
888 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
889 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
892 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
893 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
896 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
898 return [webview_ webView:sender didCommitLoadForFrame:frame];
901 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
902 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
905 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
906 if ([frame parentFrame] == nil) {
907 [self _finishLoading];
909 if (DOMDocument *document = [frame DOMDocument])
910 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
911 for (DOMHTMLBodyElement *body in bodies) {
912 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
916 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
917 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
918 DOMRGBColor *rgb([color getRGBColorValue]);
920 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
921 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
922 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
923 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
927 if (red == 0xc7 && green == 0xce && blue == 0xd5)
928 uic = [UIColor pinStripeColor];
931 colorWithRed:(red / 255)
939 [scroller_ setBackgroundColor:uic];
945 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
950 return [webview_ webView:sender didFinishLoadForFrame:frame];
953 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
954 if ([frame parentFrame] != nil)
958 [self _finishLoading];
960 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
961 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
962 [[error localizedDescription] stringByAddingPercentEscapes]
968 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
969 #if LogBrowser || ForSaurik
970 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
974 - (void) setViewportWidth:(float)width {
975 width_ = width ? width != 0 : [[self class] defaultWidth];
976 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
979 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
980 if ((self = [super initWithBook:book]) != nil) {
984 struct CGRect bounds = [self bounds];
986 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
987 [self addSubview:scroller_];
989 [scroller_ setShowBackgroundShadow:NO];
990 [scroller_ setFixedBackgroundPattern:YES];
991 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
993 [scroller_ setScrollingEnabled:YES];
994 [scroller_ setAdjustForContentSizeChange:YES];
995 [scroller_ setClipsSubviews:YES];
996 [scroller_ setAllowsRubberBanding:YES];
997 [scroller_ setScrollDecelerationFactor:0.99];
998 [scroller_ setDelegate:self];
1000 CGRect webrect = [scroller_ bounds];
1001 webrect.size.height = 0;
1006 webview_ = [Documents_ lastObject];
1007 if (webview_ != nil) {
1008 webview_ = [webview_ retain];
1009 webview = [webview_ webView];
1010 [Documents_ removeLastObject];
1011 [webview_ setFrame:webrect];
1016 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
1017 webview = [webview_ webView];
1019 // XXX: this is terribly (too?) expensive
1020 //[webview_ setDrawsBackground:NO];
1021 [webview setPreferencesIdentifier:@"Cydia"];
1023 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1025 [webview_ setAllowsMessaging:YES];
1027 [webview_ setTilingEnabled:YES];
1028 [webview_ setDrawsGrid:NO];
1029 [webview_ setLogsTilingChanges:NO];
1030 [webview_ setTileMinificationFilter:kCAFilterNearest];
1031 [webview_ setDetectsPhoneNumbers:NO];
1032 [webview_ setAutoresizes:YES];
1034 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1035 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1036 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1037 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1039 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1041 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1042 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1043 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1045 [webview_ _setDocumentType:0x4];
1047 [webview_ setZoomsFocusedFormControl:YES];
1048 [webview_ setContentsPosition:7];
1049 [webview_ setEnabledGestures:0xa];
1050 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1051 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1053 [webview_ setSmoothsFonts:YES];
1054 [webview_ setAllowsImageSheet:YES];
1055 [webview _setUsesLoaderCache:YES];
1057 [webview setGroupName:@"CydiaGroup"];
1058 [webview _setLayoutInterval:0];
1061 [self setViewportWidth:width];
1063 [webview_ setDelegate:self];
1064 [webview_ setGestureDelegate:self];
1065 [webview_ setFormEditingDelegate:self];
1066 [webview_ setInteractionDelegate:self];
1068 [scroller_ addSubview:webview_];
1070 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1072 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1073 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1074 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1076 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1077 NSString *application = package == nil ? @"Cydia" : [NSString
1078 stringWithFormat:@"Cydia/%@",
1082 if (Product_ != nil)
1083 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
1085 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1087 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
1089 /* XXX: lookup application directory? */
1090 /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
1091 if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
1092 application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
1094 [webview setApplicationNameForUserAgent:application];
1096 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1097 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
1099 [webview setFrameLoadDelegate:self];
1100 [webview setResourceLoadDelegate:indirect_];
1101 [webview setUIDelegate:self];
1102 [webview setScriptDebugDelegate:self];
1103 [webview setPolicyDelegate:self];
1105 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1106 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1110 - (id) initWithBook:(RVBook *)book {
1111 return [self initWithBook:book forWidth:0];
1114 - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1115 [webview_ redrawScaledDocument];
1118 - (void) callFunction:(WebScriptObject *)function {
1119 WebView *webview([webview_ webView]);
1120 WebFrame *frame([webview mainFrame]);
1122 id _private(MSHookIvar<id>(webview, "_private"));
1123 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1124 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1127 if (settings == NULL)
1130 no = settings->JavaScriptCanOpenWindowsAutomatically();
1131 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1134 [delegate_ clearFirstResponder];
1135 JSObjectRef object([function JSObject]);
1136 JSGlobalContextRef context([frame globalContext]);
1137 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1139 if (settings != NULL)
1140 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1143 - (void) didCloseBook:(RVBook *)book {
1145 [self callFunction:closer_];
1148 - (void) __rightButtonClicked {
1153 - (void) _rightButtonClicked {
1154 if (function_ == nil)
1155 [self __rightButtonClicked];
1157 [self callFunction:function_];
1160 - (id) _rightButtonTitle {
1164 - (id) rightButtonTitle {
1165 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
1168 - (UINavigationButtonStyle) rightButtonStyle {
1169 if (style_ == nil) normal:
1170 return UINavigationButtonStyleNormal;
1171 else if ([style_ isEqualToString:@"Normal"])
1172 return UINavigationButtonStyleNormal;
1173 else if ([style_ isEqualToString:@"Back"])
1174 return UINavigationButtonStyleBack;
1175 else if ([style_ isEqualToString:@"Highlighted"])
1176 return UINavigationButtonStyleHighlighted;
1177 else if ([style_ isEqualToString:@"Destructive"])
1178 return UINavigationButtonStyleDestructive;
1182 - (NSString *) title {
1183 return title_ == nil ? @"Loading" : title_;
1186 - (NSString *) backButtonTitle {
1190 - (void) setPageActive:(BOOL)active {
1192 [indicator_ removeFromSuperview];
1194 [[book_ navigationBar] addSubview:indicator_];
1197 - (void) resetViewAnimated:(BOOL)animated {
1200 - (void) setPushed:(bool)pushed {
1204 + (float) defaultWidth {