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(setViewportWidth:))
83 return @"setViewportWidth";
84 else if (selector == @selector(supports:))
86 else if (selector == @selector(du:))
88 else if (selector == @selector(statfs:))
94 + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
95 return [self webScriptNameForSelector:selector] == nil;
98 - (BOOL) supports:(NSString *)feature {
99 return [feature isEqualToString:@"window.open"];
102 - (Package *) getPackageById:(NSString *)id {
103 return [[Database sharedInstance] packageWithName:id];
106 - (NSArray *) statfs:(NSString *)path {
109 if (path == nil || statfs([path UTF8String], &stat) == -1)
112 return [NSArray arrayWithObjects:
113 [NSNumber numberWithUnsignedLong:stat.f_bsize],
114 [NSNumber numberWithUnsignedLong:stat.f_blocks],
115 [NSNumber numberWithUnsignedLong:stat.f_bfree],
119 - (NSNumber *) du:(NSString *)path {
120 NSNumber *value(nil);
123 _assert(pipe(fds) != -1);
125 pid_t pid(ExecFork());
127 _assert(dup2(fds[1], 1) != -1);
128 _assert(close(fds[0]) != -1);
129 _assert(close(fds[1]) != -1);
130 execlp("du", "du", "-s", [path UTF8String], NULL);
135 _assert(close(fds[1]) != -1);
137 if (FILE *du = fdopen(fds[0], "r")) {
139 while (fgets(line, sizeof(line), du) != NULL) {
140 size_t length(strlen(line));
141 while (length != 0 && line[length - 1] == '\n')
142 line[--length] = '\0';
143 if (char *tab = strchr(line, '\t')) {
145 value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)];
150 } else _assert(close(fds[0]));
154 if (waitpid(pid, &status, 0) == -1)
162 - (void) setAutoPopup:(BOOL)popup {
163 [indirect_ setAutoPopup:popup];
166 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
167 [indirect_ setButtonImage:button withStyle:style toFunction:function];
170 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
171 [indirect_ setButtonTitle:button withStyle:style toFunction:function];
174 - (void) setViewportWidth:(float)width {
175 [indirect_ setViewportWidth:width];
181 @implementation BrowserView
184 #include "Internals.h"
189 NSLog(@"[BrowserView dealloc]");
192 if (challenge_ != nil)
193 [challenge_ release];
195 WebView *webview = [webview_ webView];
196 [webview setFrameLoadDelegate:nil];
197 [webview setResourceLoadDelegate:nil];
198 [webview setUIDelegate:nil];
199 [webview setScriptDebugDelegate:nil];
200 [webview setPolicyDelegate:nil];
202 [webview setDownloadDelegate:nil];
204 [webview _setFormDelegate:nil];
205 [webview _setUIKitDelegate:nil];
206 [webview setWebMailDelegate:nil];
207 [webview setEditingDelegate:nil];
209 [webview_ setDelegate:nil];
210 [webview_ setGestureDelegate:nil];
211 [webview_ setFormEditingDelegate:nil];
212 [webview_ setInteractionDelegate:nil];
214 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
219 [webview_ removeFromSuperview];
220 [Documents_ addObject:[webview_ autorelease]];
225 [indirect_ setDelegate:nil];
230 [scroller_ setDelegate:nil];
236 if (function_ != nil)
240 [indicator_ release];
248 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
249 [self loadRequest:[NSURLRequest
256 - (void) loadURL:(NSURL *)url {
257 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
260 - (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
261 NSMutableURLRequest *copy = [request mutableCopy];
263 if (Machine_ != NULL)
264 [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
265 if (UniqueID_ != nil)
266 [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
269 [copy setValue:Role_ forHTTPHeaderField:@"X-Role"];
274 - (void) loadRequest:(NSURLRequest *)request {
277 [webview_ loadRequest:request];
284 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
285 [self loadRequest:request_];
287 UIActionSheet *sheet = [[[UIActionSheet alloc]
288 initWithTitle:@"Are you sure you want to submit this form again?"
289 buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil]
295 [sheet setNumberOfRows:1];
296 [sheet popupAlertAnimated:YES];
300 - (WebView *) webView {
301 return [webview_ webView];
304 - (UIWebDocumentView *) documentView {
308 - (void) _fixScroller {
309 CGRect bounds([webview_ documentBounds]);
311 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
318 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
319 CGRect peripheral([assistant peripheralFrame]);
321 NSLog(@"per:%f", peripheral.size.height);
323 extra = peripheral.size.height;
326 CGRect subrect([scroller_ frame]);
327 subrect.size.height -= extra;
328 [scroller_ setScrollerIndicatorSubrect:subrect];
330 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
331 [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
334 size.height += extra;
335 [scroller_ setContentSize:size];
337 [scroller_ releaseRubberBandIfNecessary];
340 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
343 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
348 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
349 [self view:sender didSetFrame:frame];
352 - (void) pushPage:(RVPage *)page {
353 [page setDelegate:delegate_];
354 [self setBackButtonTitle:title_];
355 [book_ pushPage:page];
363 [book_ pushPage:self];
366 - (void) swapPage:(RVPage *)page {
367 [page setDelegate:delegate_];
369 [book_ swapPage:page];
371 [book_ pushPage:page];
374 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
376 NSLog(@"getSpecial:%@", url);
379 NSString *href([url absoluteString]);
380 NSString *scheme([[url scheme] lowercaseString]);
384 if ([href hasPrefix:@"apptapp://package/"])
385 page = [delegate_ pageForPackage:[href substringFromIndex:18]];
386 else if ([scheme isEqualToString:@"cydia"]) {
387 page = [delegate_ pageForURL:url hasTag:NULL];
390 } else if (![scheme isEqualToString:@"apptapp"])
395 [self swapPage:page];
397 [self pushPage:page];
401 - (void) webViewShow:(WebView *)sender {
402 /* XXX: this is where I cry myself to sleep */
405 - (bool) _allowJavaScriptPanel {
409 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
410 if (![self _allowJavaScriptPanel])
415 UIActionSheet *sheet = [[[UIActionSheet alloc]
417 buttons:[NSArray arrayWithObjects:@"OK", nil]
423 [sheet setBodyText:message];
424 [sheet popupAlertAnimated:YES];
427 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
428 if (![self _allowJavaScriptPanel])
431 UIActionSheet *sheet = [[[UIActionSheet alloc]
433 buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
439 [sheet setNumberOfRows:1];
440 [sheet setBodyText:message];
441 [sheet popupAlertAnimated:YES];
443 NSRunLoop *loop([NSRunLoop currentRunLoop]);
444 NSDate *future([NSDate distantFuture]);
446 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
448 NSNumber *confirm([confirm_ autorelease]);
450 return [confirm boolValue];
453 - (void) setAutoPopup:(BOOL)popup {
457 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
459 [button_ autorelease];
460 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
463 [style_ autorelease];
464 style_ = style == nil ? nil : [style retain];
466 if (function_ != nil)
467 [function_ autorelease];
468 function_ = function == nil ? nil : [function retain];
471 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
473 [button_ autorelease];
474 button_ = button == nil ? nil : [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];
485 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
489 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
493 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
498 - (void) webViewClose:(WebView *)sender {
502 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
503 [window setValue:cydia_ forKey:@"cydia"];
506 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
507 NSLog(@"err:%@", error);
510 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
512 NSLog(@"nwa:%@", name);
515 if (NSURL *url = [request URL]) {
516 if (name == nil) unknown: {
517 if (![self getSpecial:url swap:NO]) {
518 NSString *scheme([[url scheme] lowercaseString]);
519 if ([scheme isEqualToString:@"mailto"])
520 [delegate_ openMailToURL:url];
523 } else if ([name isEqualToString:@"_open"])
524 [delegate_ openURL:url];
525 else if ([name isEqualToString:@"_popup"]) {
526 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
528 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
530 /* XXX: call createWebViewWithRequest instead? */
532 [self setBackButtonTitle:title_];
534 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
535 [browser loadURL:url];
539 [book setDelegate:delegate_];
540 [page setDelegate:delegate_];
543 [book_ pushBook:book];
551 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
552 if ([WebView canShowMIMEType:type])
555 // XXX: handle more mime types!
558 WebView *webview([webview_ webView]);
559 if (frame == [webview mainFrame])
560 [UIApp openURL:[request URL]];
564 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
565 if (request == nil) ignore: {
570 NSURL *url([request URL]);
572 if (url == nil) use: {
573 if (!error_ && [frame parentFrame] == nil) {
575 [request_ autorelease];
576 request_ = [request retain];
578 NSLog(@"dpn:%@", request_);
584 WebView *webview([webview_ webView]);
585 if (frame == [webview mainFrame])
590 else NSLog(@"nav:%@:%@", url, [action description]);
593 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
596 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
597 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
604 int store(_not(int));
605 if (NSURL *itms = [url itmsURL:&store]) {
607 NSLog(@"itms#%@#%u#%@", url, store, itms);
611 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
612 store == 2 && [capability containsObject:@"com.apple.AppStore"]
619 NSString *scheme([[url scheme] lowercaseString]);
621 if ([scheme isEqualToString:@"tel"]) {
626 if ([scheme isEqualToString:@"mailto"]) {
627 [delegate_ openMailToURL:url];
631 if ([self getSpecial:url swap:YES])
633 else if ([WebView _canHandleRequest:request])
635 else if ([url isSpringboardHandledURL])
641 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
642 //lprintf("Status:%s\n", [text UTF8String]);
645 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
646 NSString *context([sheet context]);
648 if ([context isEqualToString:@"alert"]) {
651 } else if ([context isEqualToString:@"confirm"]) {
654 confirm_ = [NSNumber numberWithBool:YES];
658 confirm_ = [NSNumber numberWithBool:NO];
663 } else if ([context isEqualToString:@"challenge"]) {
664 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
668 NSString *username([[sheet textFieldAtIndex:0] text]);
669 NSString *password([[sheet textFieldAtIndex:1] text]);
671 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
673 [sender useCredential:credential forAuthenticationChallenge:challenge_];
677 [sender cancelAuthenticationChallenge:challenge_];
684 [challenge_ release];
688 } else if ([context isEqualToString:@"submit"]) {
695 [webview_ loadRequest:request_];
706 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
707 challenge_ = [challenge retain];
709 NSURLProtectionSpace *space([challenge protectionSpace]);
710 NSString *realm([space realm]);
714 UIActionSheet *sheet = [[[UIActionSheet alloc]
716 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
722 [sheet setNumberOfRows:1];
724 [sheet addTextFieldWithValue:@"" label:@"username"];
725 [sheet addTextFieldWithValue:@"" label:@"password"];
727 UITextField *username([sheet textFieldAtIndex:0]); {
728 UITextInputTraits *traits([username textInputTraits]);
729 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
730 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
731 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
732 [traits setReturnKeyType:UIReturnKeyNext];
735 UITextField *password([sheet textFieldAtIndex:1]); {
736 UITextInputTraits *traits([password textInputTraits]);
737 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
738 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
739 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
740 // XXX: UIReturnKeyDone
741 [traits setReturnKeyType:UIReturnKeyNext];
742 [traits setSecureTextEntry:YES];
745 [sheet popupAlertAnimated:YES];
748 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
749 return [self _addHeadersToRequest:request];
752 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
753 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
755 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
756 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
759 NSNumber *value([features objectForKey:@"width"]);
760 float width(value == nil ? 0 : [value floatValue]);
762 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
764 /* XXX: deal with cydia:// pages */
765 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
767 if (features != nil && popup_) {
768 [book setDelegate:delegate_];
769 [browser setDelegate:delegate_];
771 [browser loadRequest:request];
773 [book setPage:browser];
774 [book_ pushBook:book];
775 } else if (request == nil) {
776 [self setBackButtonTitle:title_];
777 [browser setDelegate:delegate_];
780 [self pushPage:browser];
781 [browser loadRequest:request];
784 return [browser webView];
787 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
788 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
789 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
792 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
793 if ([frame parentFrame] != nil)
796 title_ = [title retain];
797 [book_ reloadTitleForPage:self];
800 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
801 if ([frame parentFrame] != nil)
804 [webview_ resignFirstResponder];
808 [self reloadButtons];
815 if (button_ != nil) {
825 if (function_ != nil) {
830 [book_ reloadTitleForPage:self];
832 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
833 [scroller_ setZoomScale:1 duration:0];
835 CGRect webrect = [scroller_ bounds];
836 webrect.size.height = 0;
837 [webview_ setFrame:webrect];
840 - (void) _finishLoading {
843 [self reloadButtons];
851 - (void) reloadButtons {
853 [indicator_ startAnimation];
855 [indicator_ stopAnimation];
856 [super reloadButtons];
859 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
860 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
863 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
864 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
867 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
868 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
871 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
873 return [webview_ webView:sender didCommitLoadForFrame:frame];
876 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
877 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
880 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
881 if ([frame parentFrame] == nil) {
882 [self _finishLoading];
884 if (DOMDocument *document = [frame DOMDocument])
885 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
886 for (DOMHTMLBodyElement *body in bodies) {
887 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
891 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
892 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
893 DOMRGBColor *rgb([color getRGBColorValue]);
895 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
896 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
897 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
898 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
902 if (red == 0xc7 && green == 0xce && blue == 0xd5)
903 uic = [UIColor pinStripeColor];
906 colorWithRed:(red / 255)
914 [scroller_ setBackgroundColor:uic];
920 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
925 return [webview_ webView:sender didFinishLoadForFrame:frame];
928 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
929 if ([frame parentFrame] != nil)
933 [self _finishLoading];
935 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
936 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
937 [[error localizedDescription] stringByAddingPercentEscapes]
943 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
945 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
949 - (void) setViewportWidth:(float)width {
950 width_ = width ? width != 0 : [[self class] defaultWidth];
951 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
954 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
955 if ((self = [super initWithBook:book]) != nil) {
959 struct CGRect bounds = [self bounds];
961 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
962 [self addSubview:scroller_];
964 [scroller_ setShowBackgroundShadow:NO];
965 [scroller_ setFixedBackgroundPattern:YES];
966 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
968 [scroller_ setScrollingEnabled:YES];
969 [scroller_ setAdjustForContentSizeChange:YES];
970 [scroller_ setClipsSubviews:YES];
971 [scroller_ setAllowsRubberBanding:YES];
972 [scroller_ setScrollDecelerationFactor:0.99];
973 [scroller_ setDelegate:self];
975 CGRect webrect = [scroller_ bounds];
976 webrect.size.height = 0;
981 webview_ = [Documents_ lastObject];
982 if (webview_ != nil) {
983 webview_ = [webview_ retain];
984 webview = [webview_ webView];
985 [Documents_ removeLastObject];
986 [webview_ setFrame:webrect];
991 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
992 webview = [webview_ webView];
994 // XXX: this is terribly (too?) expensive
995 //[webview_ setDrawsBackground:NO];
996 [webview setPreferencesIdentifier:@"Cydia"];
998 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1000 [webview_ setAllowsMessaging:YES];
1002 [webview_ setTilingEnabled:YES];
1003 [webview_ setDrawsGrid:NO];
1004 [webview_ setLogsTilingChanges:NO];
1005 [webview_ setTileMinificationFilter:kCAFilterNearest];
1006 [webview_ setDetectsPhoneNumbers:NO];
1007 [webview_ setAutoresizes:YES];
1009 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1010 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1011 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1012 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1014 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1016 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1017 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1018 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1020 [webview_ _setDocumentType:0x4];
1022 [webview_ setZoomsFocusedFormControl:YES];
1023 [webview_ setContentsPosition:7];
1024 [webview_ setEnabledGestures:0xa];
1025 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1026 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1028 [webview_ setSmoothsFonts:YES];
1029 [webview_ setAllowsImageSheet:YES];
1030 [webview _setUsesLoaderCache:YES];
1032 [webview setGroupName:@"CydiaGroup"];
1033 [webview _setLayoutInterval:0];
1036 [self setViewportWidth:width];
1038 [webview_ setDelegate:self];
1039 [webview_ setGestureDelegate:self];
1040 [webview_ setFormEditingDelegate:self];
1041 [webview_ setInteractionDelegate:self];
1043 [scroller_ addSubview:webview_];
1045 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1047 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1048 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1049 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1051 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1052 NSString *application = package == nil ? @"Cydia" : [NSString
1053 stringWithFormat:@"Cydia/%@",
1057 if (Product_ != nil)
1058 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
1060 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1062 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
1064 /* XXX: lookup application directory? */
1065 /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
1066 if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
1067 application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
1069 [webview setApplicationNameForUserAgent:application];
1071 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1072 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
1074 [webview setFrameLoadDelegate:self];
1075 [webview setResourceLoadDelegate:indirect_];
1076 [webview setUIDelegate:self];
1077 [webview setScriptDebugDelegate:self];
1078 [webview setPolicyDelegate:self];
1080 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1081 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1085 - (id) initWithBook:(RVBook *)book {
1086 return [self initWithBook:book forWidth:0];
1089 - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1090 [webview_ redrawScaledDocument];
1093 - (void) _rightButtonClicked {
1094 if (function_ == nil) {
1098 WebView *webview([webview_ webView]);
1099 WebFrame *frame([webview mainFrame]);
1101 id _private(MSHookIvar<id>(webview, "_private"));
1102 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1103 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1106 if (settings == NULL)
1109 no = settings->JavaScriptCanOpenWindowsAutomatically();
1110 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1113 [delegate_ clearFirstResponder];
1114 JSObjectRef function([function_ JSObject]);
1115 JSGlobalContextRef context([frame globalContext]);
1116 JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
1118 if (settings != NULL)
1119 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1123 - (id) _rightButtonTitle {
1124 return button_ != nil ? button_ : @"Reload";
1127 - (id) rightButtonTitle {
1128 return [self _loading] ? @"" : [self _rightButtonTitle];
1131 - (UINavigationButtonStyle) rightButtonStyle {
1132 if (style_ == nil) normal:
1133 return UINavigationButtonStyleNormal;
1134 else if ([style_ isEqualToString:@"Normal"])
1135 return UINavigationButtonStyleNormal;
1136 else if ([style_ isEqualToString:@"Back"])
1137 return UINavigationButtonStyleBack;
1138 else if ([style_ isEqualToString:@"Highlighted"])
1139 return UINavigationButtonStyleHighlighted;
1140 else if ([style_ isEqualToString:@"Destructive"])
1141 return UINavigationButtonStyleDestructive;
1145 - (NSString *) title {
1146 return title_ == nil ? @"Loading" : title_;
1149 - (NSString *) backButtonTitle {
1153 - (void) setPageActive:(BOOL)active {
1155 [indicator_ removeFromSuperview];
1157 [[book_ navigationBar] addSubview:indicator_];
1160 - (void) resetViewAnimated:(BOOL)animated {
1163 - (void) setPushed:(bool)pushed {
1167 + (float) defaultWidth {