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 {
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"])
394 [self swapPage:page];
398 - (void) webViewShow:(WebView *)sender {
399 /* XXX: this is where I cry myself to sleep */
402 - (bool) _allowJavaScriptPanel {
406 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
407 if (![self _allowJavaScriptPanel])
412 UIActionSheet *sheet = [[[UIActionSheet alloc]
414 buttons:[NSArray arrayWithObjects:@"OK", nil]
420 [sheet setBodyText:message];
421 [sheet popupAlertAnimated:YES];
424 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
425 if (![self _allowJavaScriptPanel])
428 UIActionSheet *sheet = [[[UIActionSheet alloc]
430 buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
436 [sheet setNumberOfRows:1];
437 [sheet setBodyText:message];
438 [sheet popupAlertAnimated:YES];
440 NSRunLoop *loop([NSRunLoop currentRunLoop]);
441 NSDate *future([NSDate distantFuture]);
443 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
445 NSNumber *confirm([confirm_ autorelease]);
447 return [confirm boolValue];
450 - (void) setAutoPopup:(BOOL)popup {
454 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
456 [button_ autorelease];
457 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
460 [style_ autorelease];
461 style_ = style == nil ? nil : [style retain];
463 if (function_ != nil)
464 [function_ autorelease];
465 function_ = function == nil ? nil : [function retain];
468 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
470 [button_ autorelease];
471 button_ = button == nil ? nil : [button retain];
474 [style_ autorelease];
475 style_ = style == nil ? nil : [style retain];
477 if (function_ != nil)
478 [function_ autorelease];
479 function_ = function == nil ? nil : [function retain];
482 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
486 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
490 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
495 - (void) webViewClose:(WebView *)sender {
499 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
500 [window setValue:cydia_ forKey:@"cydia"];
503 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
504 NSLog(@"err:%@", error);
507 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
509 NSLog(@"nwa:%@", name);
512 if (NSURL *url = [request URL]) {
513 if (name == nil) unknown: {
514 if (![self getSpecial:url]) {
515 NSString *scheme([[url scheme] lowercaseString]);
516 if ([scheme isEqualToString:@"mailto"])
517 [delegate_ openMailToURL:url];
520 } else if ([name isEqualToString:@"_open"])
521 [delegate_ openURL:url];
522 else if ([name isEqualToString:@"_popup"]) {
523 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
525 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
527 /* XXX: call createWebViewWithRequest instead? */
529 [self setBackButtonTitle:title_];
531 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
532 [browser loadURL:url];
536 [book setDelegate:delegate_];
537 [page setDelegate:delegate_];
540 [book_ pushBook:book];
548 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
549 if ([WebView canShowMIMEType:type])
552 // XXX: handle more mime types!
555 WebView *webview([webview_ webView]);
556 if (frame == [webview mainFrame])
557 [UIApp openURL:[request URL]];
561 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
562 if (request == nil) ignore: {
567 NSURL *url([request URL]);
569 if (url == nil) use: {
570 if (!error_ && [frame parentFrame] == nil) {
572 [request_ autorelease];
573 request_ = [request retain];
575 NSLog(@"dpn:%@", request_);
581 WebView *webview([webview_ webView]);
582 if (frame == [webview mainFrame])
587 else NSLog(@"nav:%@:%@", url, [action description]);
590 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
593 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
594 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
601 int store(_not(int));
602 if (NSURL *itms = [url itmsURL:&store]) {
604 NSLog(@"itms#%@#%u#%@", url, store, itms);
608 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
609 store == 2 && [capability containsObject:@"com.apple.AppStore"]
616 NSString *scheme([[url scheme] lowercaseString]);
618 if ([scheme isEqualToString:@"tel"]) {
623 if ([scheme isEqualToString:@"mailto"]) {
624 [delegate_ openMailToURL:url];
628 if ([self getSpecial:url])
630 else if ([WebView _canHandleRequest:request])
632 else if ([url isSpringboardHandledURL])
638 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
639 //lprintf("Status:%s\n", [text UTF8String]);
642 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
643 NSString *context([sheet context]);
645 if ([context isEqualToString:@"alert"]) {
648 } else if ([context isEqualToString:@"confirm"]) {
651 confirm_ = [NSNumber numberWithBool:YES];
655 confirm_ = [NSNumber numberWithBool:NO];
660 } else if ([context isEqualToString:@"challenge"]) {
661 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
665 NSString *username([[sheet textFieldAtIndex:0] text]);
666 NSString *password([[sheet textFieldAtIndex:1] text]);
668 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
670 [sender useCredential:credential forAuthenticationChallenge:challenge_];
674 [sender cancelAuthenticationChallenge:challenge_];
681 [challenge_ release];
685 } else if ([context isEqualToString:@"submit"]) {
692 [webview_ loadRequest:request_];
703 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
704 challenge_ = [challenge retain];
706 NSURLProtectionSpace *space([challenge protectionSpace]);
707 NSString *realm([space realm]);
711 UIActionSheet *sheet = [[[UIActionSheet alloc]
713 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
719 [sheet setNumberOfRows:1];
721 [sheet addTextFieldWithValue:@"" label:@"username"];
722 [sheet addTextFieldWithValue:@"" label:@"password"];
724 UITextField *username([sheet textFieldAtIndex:0]); {
725 UITextInputTraits *traits([username textInputTraits]);
726 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
727 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
728 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
729 [traits setReturnKeyType:UIReturnKeyNext];
732 UITextField *password([sheet textFieldAtIndex:1]); {
733 UITextInputTraits *traits([password textInputTraits]);
734 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
735 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
736 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
737 // XXX: UIReturnKeyDone
738 [traits setReturnKeyType:UIReturnKeyNext];
739 [traits setSecureTextEntry:YES];
742 [sheet popupAlertAnimated:YES];
745 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
746 return [self _addHeadersToRequest:request];
749 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
750 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
752 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
753 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
756 NSNumber *value([features objectForKey:@"width"]);
757 float width(value == nil ? 0 : [value floatValue]);
759 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
761 /* XXX: deal with cydia:// pages */
762 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
764 if (features != nil && popup_) {
765 [book setDelegate:delegate_];
766 [browser setDelegate:delegate_];
768 [browser loadRequest:request];
770 [book setPage:browser];
771 [book_ pushBook:book];
772 } else if (request == nil) {
773 [self setBackButtonTitle:title_];
774 [browser setDelegate:delegate_];
777 [self pushPage:browser];
778 [browser loadRequest:request];
781 return [browser webView];
784 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
785 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
786 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
789 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
790 if ([frame parentFrame] != nil)
793 title_ = [title retain];
794 [book_ reloadTitleForPage:self];
797 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
798 if ([frame parentFrame] != nil)
801 [webview_ resignFirstResponder];
805 [self reloadButtons];
812 if (button_ != nil) {
822 if (function_ != nil) {
827 [book_ reloadTitleForPage:self];
829 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
830 [scroller_ setZoomScale:1 duration:0];
832 CGRect webrect = [scroller_ bounds];
833 webrect.size.height = 0;
834 [webview_ setFrame:webrect];
837 - (void) _finishLoading {
840 [self reloadButtons];
848 - (void) reloadButtons {
850 [indicator_ startAnimation];
852 [indicator_ stopAnimation];
853 [super reloadButtons];
856 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
857 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
860 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
861 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
864 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
865 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
868 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
870 return [webview_ webView:sender didCommitLoadForFrame:frame];
873 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
874 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
877 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
878 if ([frame parentFrame] == nil) {
879 [self _finishLoading];
881 if (DOMDocument *document = [frame DOMDocument])
882 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
883 for (DOMHTMLBodyElement *body in bodies) {
884 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
888 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
889 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
890 DOMRGBColor *rgb([color getRGBColorValue]);
892 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
893 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
894 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
895 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
899 if (red == 0xc7 && green == 0xce && blue == 0xd5)
900 uic = [UIColor pinStripeColor];
903 colorWithRed:(red / 255)
911 [scroller_ setBackgroundColor:uic];
917 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
922 return [webview_ webView:sender didFinishLoadForFrame:frame];
925 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
926 if ([frame parentFrame] != nil)
930 [self _finishLoading];
932 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
933 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
934 [[error localizedDescription] stringByAddingPercentEscapes]
940 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
942 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
946 - (void) setViewportWidth:(float)width {
947 width_ = width ? width != 0 : [[self class] defaultWidth];
948 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
951 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
952 if ((self = [super initWithBook:book]) != nil) {
956 struct CGRect bounds = [self bounds];
958 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
959 [self addSubview:scroller_];
961 [scroller_ setShowBackgroundShadow:NO];
962 [scroller_ setFixedBackgroundPattern:YES];
963 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
965 [scroller_ setScrollingEnabled:YES];
966 [scroller_ setAdjustForContentSizeChange:YES];
967 [scroller_ setClipsSubviews:YES];
968 [scroller_ setAllowsRubberBanding:YES];
969 [scroller_ setScrollDecelerationFactor:0.99];
970 [scroller_ setDelegate:self];
972 CGRect webrect = [scroller_ bounds];
973 webrect.size.height = 0;
978 webview_ = [Documents_ lastObject];
979 if (webview_ != nil) {
980 webview_ = [webview_ retain];
981 webview = [webview_ webView];
982 [Documents_ removeLastObject];
983 [webview_ setFrame:webrect];
988 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
989 webview = [webview_ webView];
991 // XXX: this is terribly (too?) expensive
992 //[webview_ setDrawsBackground:NO];
993 [webview setPreferencesIdentifier:@"Cydia"];
995 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
997 [webview_ setAllowsMessaging:YES];
999 [webview_ setTilingEnabled:YES];
1000 [webview_ setDrawsGrid:NO];
1001 [webview_ setLogsTilingChanges:NO];
1002 [webview_ setTileMinificationFilter:kCAFilterNearest];
1003 [webview_ setDetectsPhoneNumbers:NO];
1004 [webview_ setAutoresizes:YES];
1006 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1007 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1008 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1009 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1011 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1013 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1014 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1015 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1017 [webview_ _setDocumentType:0x4];
1019 [webview_ setZoomsFocusedFormControl:YES];
1020 [webview_ setContentsPosition:7];
1021 [webview_ setEnabledGestures:0xa];
1022 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1023 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1025 [webview_ setSmoothsFonts:YES];
1026 [webview_ setAllowsImageSheet:YES];
1027 [webview _setUsesLoaderCache:YES];
1029 [webview setGroupName:@"CydiaGroup"];
1030 [webview _setLayoutInterval:0];
1033 [self setViewportWidth:width];
1035 [webview_ setDelegate:self];
1036 [webview_ setGestureDelegate:self];
1037 [webview_ setFormEditingDelegate:self];
1038 [webview_ setInteractionDelegate:self];
1040 [scroller_ addSubview:webview_];
1042 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1044 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1045 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1046 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1048 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1049 NSString *application = package == nil ? @"Cydia" : [NSString
1050 stringWithFormat:@"Cydia/%@",
1054 if (Product_ != nil)
1055 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
1057 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1059 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
1061 /* XXX: lookup application directory? */
1062 /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
1063 if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
1064 application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
1066 [webview setApplicationNameForUserAgent:application];
1068 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1069 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
1071 [webview setFrameLoadDelegate:self];
1072 [webview setResourceLoadDelegate:indirect_];
1073 [webview setUIDelegate:self];
1074 [webview setScriptDebugDelegate:self];
1075 [webview setPolicyDelegate:self];
1077 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1078 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1082 - (id) initWithBook:(RVBook *)book {
1083 return [self initWithBook:book forWidth:0];
1086 - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1087 [webview_ redrawScaledDocument];
1090 - (void) _rightButtonClicked {
1091 if (function_ == nil) {
1095 WebView *webview([webview_ webView]);
1096 WebFrame *frame([webview mainFrame]);
1098 id _private(MSHookIvar<id>(webview, "_private"));
1099 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1100 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1103 if (settings == NULL)
1106 no = settings->JavaScriptCanOpenWindowsAutomatically();
1107 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1110 [delegate_ clearFirstResponder];
1111 JSObjectRef function([function_ JSObject]);
1112 JSGlobalContextRef context([frame globalContext]);
1113 JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
1115 if (settings != NULL)
1116 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1120 - (id) _rightButtonTitle {
1121 return button_ != nil ? button_ : @"Reload";
1124 - (id) rightButtonTitle {
1125 return [self _loading] ? @"" : [self _rightButtonTitle];
1128 - (UINavigationButtonStyle) rightButtonStyle {
1129 if (style_ == nil) normal:
1130 return UINavigationButtonStyleNormal;
1131 else if ([style_ isEqualToString:@"Normal"])
1132 return UINavigationButtonStyleNormal;
1133 else if ([style_ isEqualToString:@"Back"])
1134 return UINavigationButtonStyleBack;
1135 else if ([style_ isEqualToString:@"Highlighted"])
1136 return UINavigationButtonStyleHighlighted;
1137 else if ([style_ isEqualToString:@"Destructive"])
1138 return UINavigationButtonStyleDestructive;
1142 - (NSString *) title {
1143 return title_ == nil ? @"Loading" : title_;
1146 - (NSString *) backButtonTitle {
1150 - (void) setPageActive:(BOOL)active {
1152 [indicator_ removeFromSuperview];
1154 [[book_ navigationBar] addSubview:indicator_];
1157 - (void) resetViewAnimated:(BOOL)animated {
1160 - (void) setPushed:(bool)pushed {
1164 + (float) defaultWidth {