1 #include <UICaboodle/BrowserView.h>
2 #include <UICaboodle/UCLocalize.h>
4 #import <QuartzCore/CALayer.h>
5 // XXX: fix the minimum requirement
6 extern NSString * const kCAFilterNearest;
8 #include <WebCore/WebCoreThread.h>
9 #include <WebKit/WebPreferences-WebPrivate.h>
11 #include "substrate.h"
17 static CFArrayRef (*$GSSystemCopyCapability)(CFStringRef);
18 static CFArrayRef (*$GSSystemGetCapability)(CFStringRef);
19 static Class $UIFormAssistant;
20 static Class $UIWebBrowserView;
22 @interface NSString (UIKit)
23 - (NSString *) stringByAddingPercentEscapes;
26 /* Indirect Delegate {{{ */
27 @interface IndirectDelegate : NSObject {
28 _transient volatile id delegate_;
31 - (void) setDelegate:(id)delegate;
32 - (id) initWithDelegate:(id)delegate;
35 @implementation IndirectDelegate
37 - (void) setDelegate:(id)delegate {
41 - (id) initWithDelegate:(id)delegate {
46 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
48 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
51 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
53 return [delegate_ webView:sender didCommitLoadForFrame:frame];
56 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
58 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
61 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
63 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
66 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
68 return [delegate_ webView:sender didFinishLoadForFrame:frame];
71 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
73 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
76 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
78 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
81 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
83 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
86 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
88 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
92 - (IMP) methodForSelector:(SEL)sel {
93 if (IMP method = [super methodForSelector:sel])
95 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
99 - (BOOL) respondsToSelector:(SEL)sel {
100 if ([super respondsToSelector:sel])
102 // XXX: WebThreadCreateNSInvocation returns nil
103 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
104 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
107 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
108 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
110 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
111 if (delegate_ != nil)
112 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
114 // XXX: I fucking hate Apple so very very bad
115 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
118 - (void) forwardInvocation:(NSInvocation *)inv {
119 SEL sel = [inv selector];
120 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
121 [inv invokeWithTarget:delegate_];
127 @interface WebView (UICaboodle)
128 - (void) setScriptDebugDelegate:(id)delegate;
129 - (void) _setFormDelegate:(id)delegate;
130 - (void) _setUIKitDelegate:(id)delegate;
131 - (void) setWebMailDelegate:(id)delegate;
132 - (void) _setLayoutInterval:(float)interval;
135 @implementation WebScriptObject (UICaboodle)
138 id length([self valueForKey:@"length"]);
139 if ([length respondsToSelector:@selector(intValue)])
140 return [length intValue];
145 - (id) objectAtIndex:(unsigned)index {
146 return [self webScriptValueAtIndex:index];
151 #define ShowInternals 0
154 #define lprintf(args...) fprintf(stderr, args)
156 @implementation BrowserView
159 #include "UICaboodle/UCInternal.h"
162 + (void) _initialize {
163 [WebView enableWebThread];
165 WebPreferences *preferences([WebPreferences standardPreferences]);
166 [preferences setCacheModel:WebCacheModelDocumentBrowser];
167 [preferences setOfflineWebApplicationCacheEnabled:YES];
169 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
171 $GSSystemCopyCapability = reinterpret_cast<CFArrayRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemCopyCapability"));
172 $GSSystemGetCapability = reinterpret_cast<CFArrayRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemGetCapability"));
173 $UIFormAssistant = objc_getClass("UIFormAssistant");
175 $UIWebBrowserView = objc_getClass("UIWebBrowserView");
176 if ($UIWebBrowserView == nil) {
178 $UIWebBrowserView = objc_getClass("UIWebDocumentView");
186 NSLog(@"[BrowserView dealloc]");
189 if (challenge_ != nil)
190 [challenge_ release];
194 WebView *webview = [document_ webView];
195 [webview setFrameLoadDelegate:nil];
196 [webview setResourceLoadDelegate:nil];
197 [webview setUIDelegate:nil];
198 [webview setScriptDebugDelegate:nil];
199 [webview setPolicyDelegate:nil];
201 /* XXX: these are set by UIWebDocumentView
202 [webview setDownloadDelegate:nil];
203 [webview _setFormDelegate:nil];
204 [webview _setUIKitDelegate:nil];
205 [webview setEditingDelegate:nil];*/
207 /* XXX: no one sets this, ever
208 [webview setWebMailDelegate:nil];*/
210 [document_ setDelegate:nil];
211 [document_ setGestureDelegate:nil];
213 if ([document_ respondsToSelector:@selector(setFormEditingDelegate:)])
214 [document_ setFormEditingDelegate:nil];
216 [document_ setInteractionDelegate:nil];
218 [indirect_ setDelegate:nil];
220 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
225 [document_ removeFromSuperview];
226 [Documents_ addObject:[document_ autorelease]];
235 [scroller_ setDelegate:nil];
241 if (function_ != nil)
251 [indicator_ release];
254 if (sensitive_ != nil)
255 [sensitive_ release];
261 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
262 [self loadRequest:[NSURLRequest
265 timeoutInterval:120.0
269 - (void) loadURL:(NSURL *)url {
270 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
273 - (void) loadRequest:(NSURLRequest *)request {
278 [document_ loadRequest:request];
286 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
287 [self loadRequest:request_];
289 UIActionSheet *sheet = [[[UIActionSheet alloc]
290 initWithTitle:UCLocalize("RESUBMIT_FORM")
291 buttons:[NSArray arrayWithObjects:UCLocalize("CANCEL"), UCLocalize("SUBMIT"), nil]
297 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
299 [sheet setNumberOfRows:1];
300 [sheet popupAlertAnimated:YES];
304 - (WebView *) webView {
305 return [document_ webView];
308 - (UIWebDocumentView *) documentView {
312 /* XXX: WebThreadLock? */
313 - (void) _fixScroller:(CGRect)bounds {
316 if (!editing_ || $UIFormAssistant == nil)
319 UIFormAssistant *assistant([$UIFormAssistant sharedFormAssistant]);
320 CGRect peripheral([assistant peripheralFrame]);
322 NSLog(@"per:%f", peripheral.size.height);
324 extra = peripheral.size.height;
327 CGRect subrect([scroller_ frame]);
328 subrect.size.height -= extra;
330 if ([scroller_ respondsToSelector:@selector(setScrollerIndicatorSubrect:)])
331 [scroller_ setScrollerIndicatorSubrect:subrect];
333 [document_ setValue:[NSValue valueWithSize:NSMakeSize(subrect.size.width, subrect.size.height)] forGestureAttribute:UIGestureAttributeVisibleSize];
336 size.height += extra;
337 [scroller_ setContentSize:size];
339 if ([scroller_ respondsToSelector:@selector(releaseRubberBandIfNecessary)])
340 [scroller_ releaseRubberBandIfNecessary];
343 - (void) fixScroller {
344 CGRect bounds([document_ documentBounds]);
346 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
348 [self _fixScroller:bounds];
351 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
354 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
356 [self _fixScroller:frame];
359 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
360 [self view:sender didSetFrame:frame];
363 - (void) pushPage:(RVPage *)page {
364 [page setDelegate:delegate_];
365 [self setBackButtonTitle:title_];
366 [book_ pushPage:page];
372 // WTR: [self autorelease];
374 [book_ pushPage:self];
377 - (void) swapPage:(RVPage *)page {
378 [page setDelegate:delegate_];
380 [book_ swapPage:page];
382 [book_ pushPage:page];
385 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
387 NSLog(@"getSpecial:%@", url);
390 if (RVPage *page = [delegate_ pageForURL:url hasTag:NULL]) {
392 [self swapPage:page];
394 [self pushPage:page];
401 - (void) formAssistant:(id)sender didBeginEditingFormNode:(id)node {
404 - (void) formAssistant:(id)sender didEndEditingFormNode:(id)node {
408 - (void) webViewShow:(WebView *)sender {
409 /* XXX: this is where I cry myself to sleep */
412 - (bool) _allowJavaScriptPanel {
416 - (bool) allowSensitiveRequests {
417 return [self _allowJavaScriptPanel];
420 - (void) _promptForSensitive:(NSMutableArray *)array {
421 NSString *name([array objectAtIndex:0]);
423 UIActionSheet *sheet = [[[UIActionSheet alloc]
425 buttons:[NSArray arrayWithObjects:UCLocalize("YES"), UCLocalize("NO"), nil]
431 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
433 NSString *host(@"XXX");
435 [sheet setNumberOfRows:1];
436 [sheet setBodyText:[NSString stringWithFormat:@"The website at %@ is requesting your phone's %@. This is almost certainly for product licensing purposes. Will you allow this?", host, name]];
437 [sheet popupAlertAnimated:YES];
439 NSRunLoop *loop([NSRunLoop currentRunLoop]);
440 NSDate *future([NSDate distantFuture]);
442 while (sensitive_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
444 NSNumber *sensitive([sensitive_ autorelease]);
448 [array replaceObjectAtIndex:0 withObject:sensitive];
451 - (bool) promptForSensitive:(NSString *)name {
452 if (![self allowSensitiveRequests])
455 NSMutableArray *array([NSMutableArray arrayWithCapacity:1]);
456 [array addObject:name];
458 [self performSelectorOnMainThread:@selector(_promptForSensitive:) withObject:array waitUntilDone:YES];
459 return [[array lastObject] boolValue];
462 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
463 if (![self _allowJavaScriptPanel])
467 UIActionSheet *sheet = [[[UIActionSheet alloc]
469 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), nil]
475 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
477 [sheet setBodyText:message];
478 [sheet popupAlertAnimated:YES];
481 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
482 if (![self _allowJavaScriptPanel])
486 UIActionSheet *sheet = [[[UIActionSheet alloc]
488 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), UCLocalize("CANCEL"), nil]
494 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
496 [sheet setNumberOfRows:1];
497 [sheet setBodyText:message];
498 [sheet popupAlertAnimated:YES];
500 NSRunLoop *loop([NSRunLoop currentRunLoop]);
501 NSDate *future([NSDate distantFuture]);
503 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
505 NSNumber *confirm([confirm_ autorelease]);
509 return [confirm boolValue];
512 - (void) setAutoPopup:(BOOL)popup {
516 - (void) setSpecial:(id)function {
518 [special_ autorelease];
519 special_ = function == nil ? nil : [function retain];
522 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
524 [button_ autorelease];
525 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
528 [style_ autorelease];
529 style_ = style == nil ? nil : [style retain];
531 if (function_ != nil)
532 [function_ autorelease];
533 function_ = function == nil ? nil : [function retain];
535 [self reloadButtons];
538 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
540 [button_ autorelease];
541 button_ = button == nil ? nil : [button retain];
544 [style_ autorelease];
545 style_ = style == nil ? nil : [style retain];
547 if (function_ != nil)
548 [function_ autorelease];
549 function_ = function == nil ? nil : [function retain];
551 [self reloadButtons];
554 - (void) setFinishHook:(id)function {
556 [finish_ autorelease];
557 finish_ = function == nil ? nil : [function retain];
560 - (void) setPopupHook:(id)function {
562 [closer_ autorelease];
563 closer_ = function == nil ? nil : [function retain];
566 - (void) _openMailToURL:(NSURL *)url {
567 [UIApp openURL:url];// asPanel:YES];
570 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
574 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
578 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
583 - (void) webViewClose:(WebView *)sender {
591 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
594 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
595 NSLog(@"err:%@", error);
598 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
600 NSLog(@"nwa:%@", name);
603 if (NSURL *url = [request URL]) {
604 if (name == nil) unknown: {
605 if (![self getSpecial:url swap:NO]) {
606 NSString *scheme([[url scheme] lowercaseString]);
607 if ([scheme isEqualToString:@"mailto"])
608 [self _openMailToURL:url];
611 } else if ([name isEqualToString:@"_open"])
612 [delegate_ openURL:url];
613 else if ([name isEqualToString:@"_popup"]) {
614 NSString *scheme([[url scheme] lowercaseString]);
615 if ([scheme isEqualToString:@"mailto"])
616 [self _openMailToURL:url];
618 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
619 [book setHook:indirect_];
621 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
623 /* XXX: call createWebViewWithRequest instead? */
625 [self setBackButtonTitle:title_];
627 BrowserView *browser([[[class_ alloc] initWithBook:book] autorelease]);
628 [browser loadURL:url];
632 [book setDelegate:delegate_];
633 [page setDelegate:delegate_];
636 [book_ pushBook:book];
645 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
646 if ([WebView canShowMIMEType:type])
649 // XXX: handle more mime types!
652 WebView *webview([document_ webView]);
653 if (frame == [webview mainFrame])
654 [UIApp openURL:[request URL]];
658 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
659 if (request == nil) ignore: {
664 NSURL *url([request URL]);
665 NSString *host([url host]);
667 if (url == nil) use: {
668 if (!error_ && [frame parentFrame] == nil) {
670 [request_ autorelease];
671 request_ = [request retain];
673 NSLog(@"dpn:%@", request_);
679 WebView *webview([document_ webView]);
680 if (frame == [webview mainFrame])
685 else NSLog(@"nav:%@:%@", url, [action description]);
688 const NSArray *capability;
690 if ($GSSystemCopyCapability != NULL) {
691 capability = reinterpret_cast<const NSArray *>((*$GSSystemCopyCapability)(kGSDisplayIdentifiersCapability));
692 capability = [capability autorelease];
693 } else if ($GSSystemGetCapability != NULL) {
694 capability = reinterpret_cast<const NSArray *>((*$GSSystemGetCapability)(kGSDisplayIdentifiersCapability));
700 if (capability != nil && (
701 [url isGoogleMapsURL] && [capability containsObject:@"com.apple.Maps"] && (open = [url mapsURL]) != nil||
702 [host hasSuffix:@"youtube.com"] && [capability containsObject:@"com.apple.youtube"] && (open = [url youTubeURL]) != nil ||
703 [url respondsToSelector:@selector(phobosURL)] && (open = [url phobosURL]) != nil
711 int store(_not(int));
712 if (NSURL *itms = [url itmsURL:&store]) {
714 NSLog(@"itms#%@#%u#%@", url, store, itms);
717 if (capability != nil && (
718 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
719 store == 2 && [capability containsObject:@"com.apple.AppStore"]
726 NSString *scheme([[url scheme] lowercaseString]);
728 if ([scheme isEqualToString:@"tel"]) {
733 if ([scheme isEqualToString:@"mailto"]) {
734 [self _openMailToURL:url];
738 if ([self getSpecial:url swap:YES])
740 else if ([WebView _canHandleRequest:request])
742 else if ([url isSpringboardHandledURL])
748 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
749 //lprintf("Status:%s\n", [text UTF8String]);
752 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
753 NSString *context([sheet context]);
755 if ([context isEqualToString:@"alert"]) {
758 } else if ([context isEqualToString:@"confirm"]) {
761 confirm_ = [NSNumber numberWithBool:YES];
765 confirm_ = [NSNumber numberWithBool:NO];
770 } else if ([context isEqualToString:@"sensitive"]) {
773 sensitive_ = [NSNumber numberWithBool:YES];
777 sensitive_ = [NSNumber numberWithBool:NO];
782 } else if ([context isEqualToString:@"challenge"]) {
783 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
787 NSString *username([[sheet textFieldAtIndex:0] text]);
788 NSString *password([[sheet textFieldAtIndex:1] text]);
790 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
792 [sender useCredential:credential forAuthenticationChallenge:challenge_];
796 [sender cancelAuthenticationChallenge:challenge_];
802 [challenge_ release];
806 } else if ([context isEqualToString:@"submit"]) {
812 if (request_ != nil) {
814 [document_ loadRequest:request_];
826 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
827 challenge_ = [challenge retain];
829 NSURLProtectionSpace *space([challenge protectionSpace]);
830 NSString *realm([space realm]);
834 UIActionSheet *sheet = [[[UIActionSheet alloc]
836 buttons:[NSArray arrayWithObjects:UCLocalize("LOGIN"), UCLocalize("CANCEL"), nil]
842 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
844 [sheet setNumberOfRows:1];
846 [sheet addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
847 [sheet addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
849 UITextField *username([sheet textFieldAtIndex:0]); {
850 UITextInputTraits *traits([username textInputTraits]);
851 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
852 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
853 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
854 [traits setReturnKeyType:UIReturnKeyNext];
857 UITextField *password([sheet textFieldAtIndex:1]); {
858 UITextInputTraits *traits([password textInputTraits]);
859 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
860 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
861 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
862 // XXX: UIReturnKeyDone
863 [traits setReturnKeyType:UIReturnKeyNext];
864 [traits setSecureTextEntry:YES];
867 [sheet popupAlertAnimated:YES];
870 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
874 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
875 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
877 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
878 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
881 NSNumber *value([features objectForKey:@"width"]);
882 float width(value == nil ? 0 : [value floatValue]);
884 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
886 /* XXX: deal with cydia:// pages */
887 BrowserView *browser([[[class_ alloc] initWithBook:book forWidth:width] autorelease]);
889 if (features != nil && popup_) {
890 [book setDelegate:delegate_];
891 [book setHook:indirect_];
892 [browser setDelegate:delegate_];
894 [browser loadRequest:request];
896 [book setPage:browser];
897 [book_ pushBook:book];
898 } else if (request == nil) {
899 [self setBackButtonTitle:title_];
900 [browser setDelegate:delegate_];
903 [self pushPage:browser];
904 [browser loadRequest:request];
907 return [browser webView];
910 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
911 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
912 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
915 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
916 if ([frame parentFrame] != nil)
919 title_ = [title retain];
920 [book_ reloadTitleForPage:self];
923 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
924 /*if ([loading_ count] == 0)
926 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
928 if ([frame parentFrame] == nil) {
929 [document_ resignFirstResponder];
938 if (button_ != nil) {
948 if (function_ != nil) {
953 if (finish_ != nil) {
958 if (closer_ != nil) {
963 if (special_ != nil) {
968 [book_ reloadTitleForPage:self];
971 CGRect webrect = [scroller_ bounds];
972 webrect.size.height = 1;
973 [document_ setFrame:webrect];
976 if ([scroller_ respondsToSelector:@selector(scrollPointVisibleAtTopLeft:)])
977 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
979 [scroller_ scrollRectToVisible:CGRectZero animated:NO];
981 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
982 [scroller_ setZoomScale:1 duration:0];
983 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
984 [scroller_ _setZoomScale:1 duration:0];
985 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
986 [scroller_ setZoomScale:1 animated:NO];*/
989 CGRect webrect = [scroller_ bounds];
990 webrect.size.height = 0;
991 [document_ setFrame:webrect];
995 [self reloadButtons];
998 - (void) _finishLoading {
999 size_t count([loading_ count]);
1001 [self autorelease];*/
1002 if (reloading_ || count != 0)
1005 [self callFunction:finish_];
1006 [self reloadButtons];
1009 - (bool) isLoading {
1010 return [loading_ count] != 0;
1013 - (void) reloadButtons {
1014 if ([self isLoading])
1015 [indicator_ startAnimation];
1017 [indicator_ stopAnimation];
1018 [super reloadButtons];
1021 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
1022 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
1025 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
1026 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
1029 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
1030 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
1033 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
1035 return [document_ webView:sender didCommitLoadForFrame:frame];
1038 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
1039 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
1042 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
1043 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1044 [self _finishLoading];
1046 if ([frame parentFrame] == nil) {
1047 if (DOMDocument *document = [frame DOMDocument])
1048 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
1049 for (DOMHTMLBodyElement *body in bodies) {
1050 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
1052 bool colored(false);
1054 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
1055 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
1056 DOMRGBColor *rgb([color getRGBColorValue]);
1058 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
1059 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
1060 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
1061 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
1065 if (red == 0xc7 && green == 0xce && blue == 0xd5)
1066 uic = [UIColor pinStripeColor];
1067 else if (alpha != 0)
1069 colorWithRed:(red / 255)
1077 [scroller_ setBackgroundColor:uic];
1083 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1088 return [document_ webView:sender didFinishLoadForFrame:frame];
1091 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
1093 /*if ([frame parentFrame] == nil)
1094 [self autorelease];*/
1096 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1097 [self _finishLoading];
1102 if ([frame parentFrame] == nil) {
1103 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
1104 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
1105 [[error localizedDescription] stringByAddingPercentEscapes]
1112 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1113 [self _didFailWithError:error forFrame:frame];
1114 if ([document_ respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
1115 [document_ webView:sender didFailLoadWithError:error forFrame:frame];
1118 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1119 [self _didFailWithError:error forFrame:frame];
1122 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1123 #if LogBrowser || ForSaurik
1124 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1128 - (void) webView:(WebView *)sender didReceiveMessage:(NSDictionary *)dictionary {
1129 #if LogBrowser || ForSaurik
1130 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1132 if ([document_ respondsToSelector:@selector(webView:didReceiveMessage:)])
1133 [document_ webView:sender didReceiveMessage:dictionary];
1136 - (void) webView:(id)sender willCloseFrame:(id)frame {
1137 if ([document_ respondsToSelector:@selector(webView:willCloseFrame:)])
1138 [document_ webView:sender willCloseFrame:frame];
1141 - (void) webView:(id)sender didFinishDocumentLoadForFrame:(id)frame {
1142 if ([document_ respondsToSelector:@selector(webView:didFinishDocumentLoadForFrame:)])
1143 [document_ webView:sender didFinishDocumentLoadForFrame:frame];
1146 - (void) webView:(id)sender didFirstLayoutInFrame:(id)frame {
1147 if ([document_ respondsToSelector:@selector(webView:didFirstLayoutInFrame:)])
1148 [document_ webView:sender didFirstLayoutInFrame:frame];
1151 - (void) webViewFormEditedStatusHasChanged:(id)changed {
1152 if ([document_ respondsToSelector:@selector(webViewFormEditedStatusHasChanged:)])
1153 [document_ webViewFormEditedStatusHasChanged:changed];
1156 - (void) webView:(id)sender formStateDidFocusNode:(id)formState {
1157 if ([document_ respondsToSelector:@selector(webView:formStateDidFocusNode:)])
1158 [document_ webView:sender formStateDidFocusNode:formState];
1161 - (void) webView:(id)sender formStateDidBlurNode:(id)formState {
1162 if ([document_ respondsToSelector:@selector(webView:formStateDidBlurNode:)])
1163 [document_ webView:sender formStateDidBlurNode:formState];
1166 /* XXX: fix this stupid include file
1167 - (void) webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)database {
1168 [origin setQuota:0x500000];
1171 - (void) webViewDidLayout:(id)sender {
1172 [document_ webViewDidLayout:sender];
1175 - (void) webView:(id)sender didFirstVisuallyNonEmptyLayoutInFrame:(id)frame {
1176 [document_ webView:sender didFirstVisuallyNonEmptyLayoutInFrame:frame];
1179 - (void) webView:(id)sender saveStateToHistoryItem:(id)item forFrame:(id)frame {
1180 [document_ webView:sender saveStateToHistoryItem:item forFrame:frame];
1183 - (void) webView:(id)sender restoreStateFromHistoryItem:(id)item forFrame:(id)frame force:(BOOL)force {
1184 [document_ webView:sender restoreStateFromHistoryItem:item forFrame:frame force:force];
1187 - (void) webView:(id)sender attachRootLayer:(id)layer {
1188 [document_ webView:sender attachRootLayer:layer];
1191 - (id) webView:(id)sender plugInViewWithArguments:(id)arguments fromPlugInPackage:(id)package {
1192 return [document_ webView:sender plugInViewWithArguments:arguments fromPlugInPackage:package];
1195 - (void) webView:(id)sender willShowFullScreenForPlugInView:(id)view {
1196 [document_ webView:sender willShowFullScreenForPlugInView:view];
1199 - (void) webView:(id)sender didHideFullScreenForPlugInView:(id)view {
1200 [document_ webView:sender didHideFullScreenForPlugInView:view];
1203 - (void) webView:(id)sender willAddPlugInView:(id)view {
1204 [document_ webView:sender willAddPlugInView:view];
1207 - (void) webView:(id)sender didObserveDeferredContentChange:(int)change forFrame:(id)frame {
1208 [document_ webView:sender didObserveDeferredContentChange:change forFrame:frame];
1211 - (void) webViewDidPreventDefaultForEvent:(id)sender {
1212 [document_ webViewDidPreventDefaultForEvent:sender];
1215 - (void) _setTileDrawingEnabled:(BOOL)enabled {
1216 //[document_ setTileDrawingEnabled:enabled];
1219 - (void) setViewportWidth:(float)width {
1220 width_ = width != 0 ? width : [[self class] defaultWidth];
1221 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1224 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1225 [self _setTileDrawingEnabled:NO];
1228 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1229 [self _setTileDrawingEnabled:YES];
1230 [document_ redrawScaledDocument];
1233 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
1234 [self _setTileDrawingEnabled:NO];
1237 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
1238 [self _setTileDrawingEnabled:YES];
1241 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
1242 [self _setTileDrawingEnabled:YES];
1245 - (id) initWithBook:(RVBook *)book forWidth:(float)width ofClass:(Class)_class {
1246 if ((self = [super initWithBook:book]) != nil) {
1248 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
1251 struct CGRect bounds = [self bounds];
1253 scroller_ = [[objc_getClass(Wildcat_ ? "UIScrollView" : "UIScroller") alloc] initWithFrame:bounds];
1254 [self addSubview:scroller_];
1256 [scroller_ setFixedBackgroundPattern:YES];
1257 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1259 [scroller_ setScrollingEnabled:YES];
1260 [scroller_ setClipsSubviews:YES];
1263 [scroller_ setAllowsRubberBanding:YES];
1265 [scroller_ setDelegate:self];
1266 [scroller_ setBounces:YES];
1269 [scroller_ setScrollHysteresis:8];
1270 [scroller_ setThumbDetectionEnabled:NO];
1271 [scroller_ setDirectionalScrolling:YES];
1272 //[scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
1273 [scroller_ setEventMode:YES];
1277 UIScrollView *scroller((UIScrollView *)scroller_);
1278 //[scroller setDirectionalLockEnabled:NO];
1279 [scroller setDelaysContentTouches:NO];
1280 //[scroller setScrollsToTop:NO];
1281 //[scroller setCanCancelContentTouches:NO];
1284 [scroller_ setShowBackgroundShadow:NO]; /* YES */
1285 //[scroller_ setAllowsRubberBanding:YES]; /* Vertical */
1288 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
1290 CGRect webrect = [scroller_ bounds];
1291 webrect.size.height = 0;
1298 document_ = [Documents_ lastObject];
1299 if (document_ != nil) {
1300 document_ = [document_ retain];
1301 webview = [document_ webView];
1302 [Documents_ removeLastObject];
1303 [document_ setFrame:webrect];
1308 document_ = [[$UIWebBrowserView alloc] initWithFrame:webrect];
1309 webview = [document_ webView];
1311 // XXX: this is terribly (too?) expensive
1312 //[document_ setDrawsBackground:NO];
1313 [webview setPreferencesIdentifier:@"Cydia"];
1315 [document_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1317 if ([document_ respondsToSelector:@selector(enableReachability)])
1318 [document_ enableReachability];
1319 if ([document_ respondsToSelector:@selector(setAllowsMessaging:)])
1320 [document_ setAllowsMessaging:YES];
1321 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
1322 [document_ useSelectionAssistantWithMode:0];
1324 [document_ setTilingEnabled:YES];
1325 [document_ setDrawsGrid:NO];
1326 [document_ setLogsTilingChanges:NO];
1327 [document_ setTileMinificationFilter:kCAFilterNearest];
1329 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
1330 /* XXX: abstractify */
1331 [document_ setDataDetectorTypes:0x80000000];
1333 [document_ setDetectsPhoneNumbers:NO];
1335 [document_ setAutoresizes:YES];
1337 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
1338 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
1339 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1340 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1342 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1344 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
1345 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1346 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1348 [document_ _setDocumentType:0x4];
1350 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
1351 [document_ setZoomsFocusedFormControl:YES];
1352 [document_ setContentsPosition:7];
1353 [document_ setEnabledGestures:0xa];
1354 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1355 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1357 [document_ setSmoothsFonts:YES];
1358 [document_ setAllowsImageSheet:YES];
1359 [webview _setUsesLoaderCache:YES];
1361 [webview setGroupName:@"CydiaGroup"];
1363 WebPreferences *preferences([webview preferences]);
1365 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1366 [webview _setLayoutInterval:0];
1368 [preferences _setLayoutInterval:0];
1371 [self setViewportWidth:width];
1373 [document_ setDelegate:self];
1374 [document_ setGestureDelegate:self];
1376 if ([document_ respondsToSelector:@selector(setFormEditingDelegate:)])
1377 [document_ setFormEditingDelegate:self];
1379 [document_ setInteractionDelegate:self];
1381 [scroller_ addSubview:document_];
1383 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1385 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1387 [webview setFrameLoadDelegate:indirect_];
1388 [webview setPolicyDelegate:indirect_];
1389 [webview setResourceLoadDelegate:indirect_];
1390 [webview setUIDelegate:indirect_];
1392 /* XXX: do not turn this on under penalty of extreme pain */
1393 [webview setScriptDebugDelegate:nil];
1397 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1398 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(bounds.size.width - 39, 12, indsize.width, indsize.height)];
1399 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1401 [self setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1402 [scroller_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1403 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1404 [document_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1406 /*UIWebView *test([[[UIWebView alloc] initWithFrame:[self bounds]] autorelease]);
1407 [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
1408 [self addSubview:test];*/
1412 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
1413 return [self initWithBook:book forWidth:width ofClass:[self class]];
1416 - (id) initWithBook:(RVBook *)book {
1417 return [self initWithBook:book forWidth:0];
1420 - (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
1422 WebView *webview([document_ webView]);
1423 NSString *string([webview stringByEvaluatingJavaScriptFromString:script]);
1428 - (void) callFunction:(WebScriptObject *)function {
1431 WebView *webview([document_ webView]);
1432 WebFrame *frame([webview mainFrame]);
1434 id _private(MSHookIvar<id>(webview, "_private"));
1435 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1436 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1439 if (settings == NULL)
1442 no = settings->JavaScriptCanOpenWindowsAutomatically();
1443 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1446 if (UIWindow *window = [self window])
1447 if (UIResponder *responder = [window firstResponder])
1448 [responder resignFirstResponder];
1450 JSObjectRef object([function JSObject]);
1451 JSGlobalContextRef context([frame globalContext]);
1452 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1454 if (settings != NULL)
1455 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1460 - (void) didCloseBook:(RVBook *)book {
1462 [self callFunction:closer_];
1465 - (void) __rightButtonClicked {
1470 - (void) _rightButtonClicked {
1472 if (function_ != nil)
1473 [self callFunction:function_];
1476 [self __rightButtonClicked];
1479 - (id) _rightButtonTitle {
1480 return UCLocalize("RELOAD");
1483 - (id) rightButtonTitle {
1484 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
1487 - (UINavigationButtonStyle) rightButtonStyle {
1488 if (style_ == nil) normal:
1489 return UINavigationButtonStyleNormal;
1490 else if ([style_ isEqualToString:@"Normal"])
1491 return UINavigationButtonStyleNormal;
1492 else if ([style_ isEqualToString:@"Back"])
1493 return UINavigationButtonStyleBack;
1494 else if ([style_ isEqualToString:@"Highlighted"])
1495 return UINavigationButtonStyleHighlighted;
1496 else if ([style_ isEqualToString:@"Destructive"])
1497 return UINavigationButtonStyleDestructive;
1501 - (NSString *) title {
1502 return title_ == nil ? UCLocalize("LOADING") : title_;
1505 - (NSString *) backButtonTitle {
1506 return UCLocalize("BROWSER");
1509 - (void) setPageActive:(BOOL)active {
1511 [indicator_ removeFromSuperview];
1513 [[book_ navigationBar] addSubview:indicator_];
1516 - (void) resetViewAnimated:(BOOL)animated {
1519 - (void) setPushed:(bool)pushed {
1523 + (float) defaultWidth {