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"
15 static CFArrayRef (*$GSSystemCopyCapability)(CFStringRef);
16 static CFArrayRef (*$GSSystemGetCapability)(CFStringRef);
17 static Class $UIFormAssistant;
18 static Class $UIWebBrowserView;
20 @interface NSString (UIKit)
21 - (NSString *) stringByAddingPercentEscapes;
24 /* Indirect Delegate {{{ */
25 @interface IndirectDelegate : NSObject {
26 _transient volatile id delegate_;
29 - (void) setDelegate:(id)delegate;
30 - (id) initWithDelegate:(id)delegate;
33 @implementation IndirectDelegate
35 - (void) setDelegate:(id)delegate {
39 - (id) initWithDelegate:(id)delegate {
44 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
46 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
49 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
51 return [delegate_ webView:sender didCommitLoadForFrame:frame];
54 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
56 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
59 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
61 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
64 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
66 return [delegate_ webView:sender didFinishLoadForFrame:frame];
69 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
71 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
74 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
76 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
79 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
81 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
84 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
86 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
90 - (IMP) methodForSelector:(SEL)sel {
91 if (IMP method = [super methodForSelector:sel])
93 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
97 - (BOOL) respondsToSelector:(SEL)sel {
98 if ([super respondsToSelector:sel])
100 // XXX: WebThreadCreateNSInvocation returns nil
101 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
102 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
105 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
106 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
108 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
109 if (delegate_ != nil)
110 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
112 // XXX: I fucking hate Apple so very very bad
113 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
116 - (void) forwardInvocation:(NSInvocation *)inv {
117 SEL sel = [inv selector];
118 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
119 [inv invokeWithTarget:delegate_];
125 @interface WebView (UICaboodle)
126 - (void) setScriptDebugDelegate:(id)delegate;
127 - (void) _setFormDelegate:(id)delegate;
128 - (void) _setUIKitDelegate:(id)delegate;
129 - (void) setWebMailDelegate:(id)delegate;
130 - (void) _setLayoutInterval:(float)interval;
133 @implementation WebScriptObject (UICaboodle)
136 id length([self valueForKey:@"length"]);
137 if ([length respondsToSelector:@selector(intValue)])
138 return [length intValue];
143 - (id) objectAtIndex:(unsigned)index {
144 return [self webScriptValueAtIndex:index];
149 #define ShowInternals 1
152 #define lprintf(args...) fprintf(stderr, args)
154 @implementation BrowserView
157 #include "UICaboodle/UCInternal.h"
160 + (void) _initialize {
161 [WebView enableWebThread];
163 WebPreferences *preferences([WebPreferences standardPreferences]);
164 [preferences setCacheModel:WebCacheModelDocumentBrowser];
165 [preferences setOfflineWebApplicationCacheEnabled:YES];
167 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
169 $GSSystemCopyCapability = reinterpret_cast<CFArrayRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemCopyCapability"));
170 $GSSystemGetCapability = reinterpret_cast<CFArrayRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemGetCapability"));
171 $UIFormAssistant = objc_getClass("UIFormAssistant");
173 $UIWebBrowserView = objc_getClass("UIWebBrowserView");
174 if ($UIWebBrowserView == nil)
175 $UIWebBrowserView = objc_getClass("UIWebDocumentView");
180 NSLog(@"[BrowserView dealloc]");
183 if (challenge_ != nil)
184 [challenge_ release];
188 WebView *webview = [webview_ webView];
189 [webview setFrameLoadDelegate:nil];
190 [webview setResourceLoadDelegate:nil];
191 [webview setUIDelegate:nil];
192 [webview setScriptDebugDelegate:nil];
193 [webview setPolicyDelegate:nil];
195 /* XXX: these are set by UIWebDocumentView
196 [webview setDownloadDelegate:nil];
197 [webview _setFormDelegate:nil];
198 [webview _setUIKitDelegate:nil];
199 [webview setEditingDelegate:nil];*/
201 /* XXX: no one sets this, ever
202 [webview setWebMailDelegate:nil];*/
204 [webview_ setDelegate:nil];
205 [webview_ setGestureDelegate:nil];
207 if ([webview_ respondsToSelector:@selector(setFormEditingDelegate:)])
208 [webview_ setFormEditingDelegate:nil];
210 [webview_ setInteractionDelegate:nil];
212 [indirect_ setDelegate:nil];
214 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
219 [webview_ removeFromSuperview];
220 [Documents_ addObject:[webview_ autorelease]];
229 [scroller_ setDelegate:nil];
235 if (function_ != nil)
245 [indicator_ release];
248 if (sensitive_ != nil)
249 [sensitive_ release];
255 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
256 [self loadRequest:[NSURLRequest
263 - (void) loadURL:(NSURL *)url {
264 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
267 - (void) loadRequest:(NSURLRequest *)request {
272 [webview_ loadRequest:request];
280 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
281 [self loadRequest:request_];
283 UIActionSheet *sheet = [[[UIActionSheet alloc]
284 initWithTitle:UCLocalize("RESUBMIT_FORM")
285 buttons:[NSArray arrayWithObjects:UCLocalize("CANCEL"), UCLocalize("SUBMIT"), nil]
291 [sheet setNumberOfRows:1];
292 [sheet popupAlertAnimated:YES];
296 - (WebView *) webView {
297 return [webview_ webView];
300 - (UIWebDocumentView *) documentView {
304 /* XXX: WebThreadLock? */
305 - (void) _fixScroller:(CGRect)bounds {
308 if (!editing_ || $UIFormAssistant == nil)
311 UIFormAssistant *assistant([$UIFormAssistant sharedFormAssistant]);
312 CGRect peripheral([assistant peripheralFrame]);
314 NSLog(@"per:%f", peripheral.size.height);
316 extra = peripheral.size.height;
319 CGRect subrect([scroller_ frame]);
320 subrect.size.height -= extra;
322 if ([scroller_ respondsToSelector:@selector(setScrollerIndicatorSubrect:)])
323 [scroller_ setScrollerIndicatorSubrect:subrect];
325 [webview_ setValue:[NSValue valueWithSize:NSMakeSize(subrect.size.width, subrect.size.height)] forGestureAttribute:UIGestureAttributeVisibleSize];
328 size.height += extra;
329 [scroller_ setContentSize:size];
331 if ([scroller_ respondsToSelector:@selector(releaseRubberBandIfNecessary)])
332 [scroller_ releaseRubberBandIfNecessary];
335 - (void) fixScroller {
336 CGRect bounds([webview_ documentBounds]);
338 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
340 [self _fixScroller:bounds];
343 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
346 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
348 [self _fixScroller:frame];
351 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
352 [self view:sender didSetFrame:frame];
355 - (void) pushPage:(RVPage *)page {
356 [page setDelegate:delegate_];
357 [self setBackButtonTitle:title_];
358 [book_ pushPage:page];
364 // WTR: [self autorelease];
366 [book_ pushPage:self];
369 - (void) swapPage:(RVPage *)page {
370 [page setDelegate:delegate_];
372 [book_ swapPage:page];
374 [book_ pushPage:page];
377 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
379 NSLog(@"getSpecial:%@", url);
382 if (RVPage *page = [delegate_ pageForURL:url hasTag:NULL]) {
384 [self swapPage:page];
386 [self pushPage:page];
393 - (void) formAssistant:(id)sender didBeginEditingFormNode:(id)node {
396 - (void) formAssistant:(id)sender didEndEditingFormNode:(id)node {
400 - (void) webViewShow:(WebView *)sender {
401 /* XXX: this is where I cry myself to sleep */
404 - (bool) _allowJavaScriptPanel {
408 - (bool) allowSensitiveRequests {
409 return [self _allowJavaScriptPanel];
412 - (void) _promptForSensitive:(NSMutableArray *)array {
413 NSString *name([array objectAtIndex:0]);
415 UIActionSheet *sheet = [[[UIActionSheet alloc]
417 buttons:[NSArray arrayWithObjects:UCLocalize("YES"), UCLocalize("NO"), nil]
423 NSString *host(@"XXX");
425 [sheet setNumberOfRows:1];
426 [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]];
427 [sheet popupAlertAnimated:YES];
429 NSRunLoop *loop([NSRunLoop currentRunLoop]);
430 NSDate *future([NSDate distantFuture]);
432 while (sensitive_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
434 NSNumber *sensitive([sensitive_ autorelease]);
438 [array replaceObjectAtIndex:0 withObject:sensitive];
441 - (bool) promptForSensitive:(NSString *)name {
442 if (![self allowSensitiveRequests])
445 NSMutableArray *array([NSMutableArray arrayWithCapacity:1]);
446 [array addObject:name];
448 [self performSelectorOnMainThread:@selector(_promptForSensitive:) withObject:array waitUntilDone:YES];
449 return [[array lastObject] boolValue];
452 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
453 if (![self _allowJavaScriptPanel])
457 UIActionSheet *sheet = [[[UIActionSheet alloc]
459 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), nil]
465 [sheet setBodyText:message];
466 [sheet popupAlertAnimated:YES];
469 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
470 if (![self _allowJavaScriptPanel])
474 UIActionSheet *sheet = [[[UIActionSheet alloc]
476 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), UCLocalize("CANCEL"), nil]
482 [sheet setNumberOfRows:1];
483 [sheet setBodyText:message];
484 [sheet popupAlertAnimated:YES];
486 NSRunLoop *loop([NSRunLoop currentRunLoop]);
487 NSDate *future([NSDate distantFuture]);
489 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
491 NSNumber *confirm([confirm_ autorelease]);
495 return [confirm boolValue];
498 - (void) setAutoPopup:(BOOL)popup {
502 - (void) setSpecial:(id)function {
504 [special_ autorelease];
505 special_ = function == nil ? nil : [function retain];
508 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
510 [button_ autorelease];
511 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
514 [style_ autorelease];
515 style_ = style == nil ? nil : [style retain];
517 if (function_ != nil)
518 [function_ autorelease];
519 function_ = function == nil ? nil : [function retain];
521 [self reloadButtons];
524 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
526 [button_ autorelease];
527 button_ = button == nil ? nil : [button retain];
530 [style_ autorelease];
531 style_ = style == nil ? nil : [style retain];
533 if (function_ != nil)
534 [function_ autorelease];
535 function_ = function == nil ? nil : [function retain];
537 [self reloadButtons];
540 - (void) setFinishHook:(id)function {
542 [finish_ autorelease];
543 finish_ = function == nil ? nil : [function retain];
546 - (void) setPopupHook:(id)function {
548 [closer_ autorelease];
549 closer_ = function == nil ? nil : [function retain];
552 - (void) _openMailToURL:(NSURL *)url {
553 [UIApp openURL:url];// asPanel:YES];
556 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
560 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
564 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
569 - (void) webViewClose:(WebView *)sender {
577 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
580 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
581 NSLog(@"err:%@", error);
584 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
586 NSLog(@"nwa:%@", name);
589 if (NSURL *url = [request URL]) {
590 if (name == nil) unknown: {
591 if (![self getSpecial:url swap:NO]) {
592 NSString *scheme([[url scheme] lowercaseString]);
593 if ([scheme isEqualToString:@"mailto"])
594 [self _openMailToURL:url];
597 } else if ([name isEqualToString:@"_open"])
598 [delegate_ openURL:url];
599 else if ([name isEqualToString:@"_popup"]) {
600 NSString *scheme([[url scheme] lowercaseString]);
601 if ([scheme isEqualToString:@"mailto"])
602 [self _openMailToURL:url];
604 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
605 [book setHook:indirect_];
607 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
609 /* XXX: call createWebViewWithRequest instead? */
611 [self setBackButtonTitle:title_];
613 BrowserView *browser([[[class_ alloc] initWithBook:book] autorelease]);
614 [browser loadURL:url];
618 [book setDelegate:delegate_];
619 [page setDelegate:delegate_];
622 [book_ pushBook:book];
631 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
632 if ([WebView canShowMIMEType:type])
635 // XXX: handle more mime types!
638 WebView *webview([webview_ webView]);
639 if (frame == [webview mainFrame])
640 [UIApp openURL:[request URL]];
644 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
645 if (request == nil) ignore: {
650 NSURL *url([request URL]);
651 NSString *host([url host]);
653 if (url == nil) use: {
654 if (!error_ && [frame parentFrame] == nil) {
656 [request_ autorelease];
657 request_ = [request retain];
659 NSLog(@"dpn:%@", request_);
665 WebView *webview([webview_ webView]);
666 if (frame == [webview mainFrame])
671 else NSLog(@"nav:%@:%@", url, [action description]);
674 const NSArray *capability;
676 if ($GSSystemCopyCapability != NULL) {
677 capability = reinterpret_cast<const NSArray *>((*$GSSystemCopyCapability)(kGSDisplayIdentifiersCapability));
678 capability = [capability autorelease];
679 } else if ($GSSystemGetCapability != NULL) {
680 capability = reinterpret_cast<const NSArray *>((*$GSSystemGetCapability)(kGSDisplayIdentifiersCapability));
686 if (capability != nil && (
687 [url isGoogleMapsURL] && [capability containsObject:@"com.apple.Maps"] && (open = [url mapsURL]) != nil||
688 [host hasSuffix:@"youtube.com"] && [capability containsObject:@"com.apple.youtube"] && (open = [url youTubeURL]) != nil ||
689 [url respondsToSelector:@selector(phobosURL)] && (open = [url phobosURL]) != nil
697 int store(_not(int));
698 if (NSURL *itms = [url itmsURL:&store]) {
700 NSLog(@"itms#%@#%u#%@", url, store, itms);
703 if (capability != nil && (
704 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
705 store == 2 && [capability containsObject:@"com.apple.AppStore"]
712 NSString *scheme([[url scheme] lowercaseString]);
714 if ([scheme isEqualToString:@"tel"]) {
719 if ([scheme isEqualToString:@"mailto"]) {
720 [self _openMailToURL:url];
724 if ([self getSpecial:url swap:YES])
726 else if ([WebView _canHandleRequest:request])
728 else if ([url isSpringboardHandledURL])
734 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
735 //lprintf("Status:%s\n", [text UTF8String]);
738 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
739 NSString *context([sheet context]);
741 if ([context isEqualToString:@"alert"]) {
744 } else if ([context isEqualToString:@"confirm"]) {
747 confirm_ = [NSNumber numberWithBool:YES];
751 confirm_ = [NSNumber numberWithBool:NO];
756 } else if ([context isEqualToString:@"sensitive"]) {
759 sensitive_ = [NSNumber numberWithBool:YES];
763 sensitive_ = [NSNumber numberWithBool:NO];
768 } else if ([context isEqualToString:@"challenge"]) {
769 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
773 NSString *username([[sheet textFieldAtIndex:0] text]);
774 NSString *password([[sheet textFieldAtIndex:1] text]);
776 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
778 [sender useCredential:credential forAuthenticationChallenge:challenge_];
782 [sender cancelAuthenticationChallenge:challenge_];
788 [challenge_ release];
792 } else if ([context isEqualToString:@"submit"]) {
798 if (request_ != nil) {
800 [webview_ loadRequest:request_];
812 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
813 challenge_ = [challenge retain];
815 NSURLProtectionSpace *space([challenge protectionSpace]);
816 NSString *realm([space realm]);
820 UIActionSheet *sheet = [[[UIActionSheet alloc]
822 buttons:[NSArray arrayWithObjects:UCLocalize("LOGIN"), UCLocalize("CANCEL"), nil]
828 [sheet setNumberOfRows:1];
830 [sheet addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
831 [sheet addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
833 UITextField *username([sheet textFieldAtIndex:0]); {
834 UITextInputTraits *traits([username textInputTraits]);
835 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
836 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
837 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
838 [traits setReturnKeyType:UIReturnKeyNext];
841 UITextField *password([sheet textFieldAtIndex:1]); {
842 UITextInputTraits *traits([password textInputTraits]);
843 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
844 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
845 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
846 // XXX: UIReturnKeyDone
847 [traits setReturnKeyType:UIReturnKeyNext];
848 [traits setSecureTextEntry:YES];
851 [sheet popupAlertAnimated:YES];
854 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
858 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
859 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
861 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
862 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
865 NSNumber *value([features objectForKey:@"width"]);
866 float width(value == nil ? 0 : [value floatValue]);
868 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
870 /* XXX: deal with cydia:// pages */
871 BrowserView *browser([[[class_ alloc] initWithBook:book forWidth:width] autorelease]);
873 if (features != nil && popup_) {
874 [book setDelegate:delegate_];
875 [book setHook:indirect_];
876 [browser setDelegate:delegate_];
878 [browser loadRequest:request];
880 [book setPage:browser];
881 [book_ pushBook:book];
882 } else if (request == nil) {
883 [self setBackButtonTitle:title_];
884 [browser setDelegate:delegate_];
887 [self pushPage:browser];
888 [browser loadRequest:request];
891 return [browser webView];
894 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
895 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
896 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
899 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
900 if ([frame parentFrame] != nil)
903 title_ = [title retain];
904 [book_ reloadTitleForPage:self];
907 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
908 /*if ([loading_ count] == 0)
910 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
912 if ([frame parentFrame] == nil) {
913 [webview_ resignFirstResponder];
922 if (button_ != nil) {
932 if (function_ != nil) {
937 if (finish_ != nil) {
942 if (closer_ != nil) {
947 if (special_ != nil) {
952 [book_ reloadTitleForPage:self];
954 CGRect webrect = [scroller_ bounds];
955 webrect.size.height = 1;
956 [webview_ setFrame:webrect];
958 if ([scroller_ respondsToSelector:@selector(scrollPointVisibleAtTopLeft:)])
959 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
961 [scroller_ scrollRectToVisible:CGRectZero animated:NO];
963 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
964 [scroller_ setZoomScale:1 duration:0];
965 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
966 [scroller_ _setZoomScale:1 duration:0];
967 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
968 [scroller_ setZoomScale:1 animated:NO];*/
971 [self reloadButtons];
974 - (void) _finishLoading {
975 size_t count([loading_ count]);
977 [self autorelease];*/
978 if (reloading_ || count != 0)
981 [self callFunction:finish_];
982 [self reloadButtons];
986 return [loading_ count] != 0;
989 - (void) reloadButtons {
990 if ([self isLoading])
991 [indicator_ startAnimation];
993 [indicator_ stopAnimation];
994 [super reloadButtons];
997 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
998 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
1001 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
1002 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
1005 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
1006 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
1009 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
1011 return [webview_ webView:sender didCommitLoadForFrame:frame];
1014 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
1015 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
1018 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
1019 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1020 [self _finishLoading];
1022 if ([frame parentFrame] == nil) {
1023 if (DOMDocument *document = [frame DOMDocument])
1024 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
1025 for (DOMHTMLBodyElement *body in bodies) {
1026 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
1028 bool colored(false);
1030 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
1031 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
1032 DOMRGBColor *rgb([color getRGBColorValue]);
1034 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
1035 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
1036 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
1037 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
1041 if (red == 0xc7 && green == 0xce && blue == 0xd5)
1042 uic = [UIColor pinStripeColor];
1043 else if (alpha != 0)
1045 colorWithRed:(red / 255)
1053 [scroller_ setBackgroundColor:uic];
1059 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1064 return [webview_ webView:sender didFinishLoadForFrame:frame];
1067 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
1069 /*if ([frame parentFrame] == nil)
1070 [self autorelease];*/
1072 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1073 [self _finishLoading];
1078 if ([frame parentFrame] == nil) {
1079 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
1080 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
1081 [[error localizedDescription] stringByAddingPercentEscapes]
1088 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1089 [self _didFailWithError:error forFrame:frame];
1090 if ([webview_ respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
1091 [webview_ webView:sender didFailLoadWithError:error forFrame:frame];
1094 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1095 [self _didFailWithError:error forFrame:frame];
1098 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1099 #if LogBrowser || ForSaurik
1100 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1104 - (void) webView:(WebView *)sender didReceiveMessage:(NSDictionary *)dictionary {
1105 #if LogBrowser || ForSaurik
1106 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1108 if ([webview_ respondsToSelector:@selector(webView:didReceiveMessage:)])
1109 [webview_ webView:sender didReceiveMessage:dictionary];
1112 - (void) webView:(id)sender willCloseFrame:(id)frame {
1113 if ([webview_ respondsToSelector:@selector(webView:willCloseFrame:)])
1114 [webview_ webView:sender willCloseFrame:frame];
1117 - (void) webView:(id)sender didFinishDocumentLoadForFrame:(id)frame {
1118 if ([webview_ respondsToSelector:@selector(webView:didFinishDocumentLoadForFrame:)])
1119 [webview_ webView:sender didFinishDocumentLoadForFrame:frame];
1122 - (void) webView:(id)sender didFirstLayoutInFrame:(id)frame {
1123 if ([webview_ respondsToSelector:@selector(webView:didFirstLayoutInFrame:)])
1124 [webview_ webView:sender didFirstLayoutInFrame:frame];
1127 - (void) webViewFormEditedStatusHasChanged:(id)changed {
1128 if ([webview_ respondsToSelector:@selector(webViewFormEditedStatusHasChanged:)])
1129 [webview_ webViewFormEditedStatusHasChanged:changed];
1132 - (void) webView:(id)sender formStateDidFocusNode:(id)formState {
1133 if ([webview_ respondsToSelector:@selector(webView:formStateDidFocusNode:)])
1134 [webview_ webView:sender formStateDidFocusNode:formState];
1137 - (void) webView:(id)sender formStateDidBlurNode:(id)formState {
1138 if ([webview_ respondsToSelector:@selector(webView:formStateDidBlurNode:)])
1139 [webview_ webView:sender formStateDidBlurNode:formState];
1142 /* XXX: fix this stupid include file
1143 - (void) webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)database {
1144 [origin setQuota:0x500000];
1147 - (void) webViewDidLayout:(id)sender {
1148 [webview_ webViewDidLayout:sender];
1151 - (void) webView:(id)sender didFirstVisuallyNonEmptyLayoutInFrame:(id)frame {
1152 [webview_ webView:sender didFirstVisuallyNonEmptyLayoutInFrame:frame];
1155 - (void) webView:(id)sender saveStateToHistoryItem:(id)item forFrame:(id)frame {
1156 [webview_ webView:sender saveStateToHistoryItem:item forFrame:frame];
1159 - (void) webView:(id)sender restoreStateFromHistoryItem:(id)item forFrame:(id)frame force:(BOOL)force {
1160 [webview_ webView:sender restoreStateFromHistoryItem:item forFrame:frame force:force];
1163 - (void) webView:(id)sender attachRootLayer:(id)layer {
1164 [webview_ webView:sender attachRootLayer:layer];
1167 - (id) webView:(id)sender plugInViewWithArguments:(id)arguments fromPlugInPackage:(id)package {
1168 return [webview_ webView:sender plugInViewWithArguments:arguments fromPlugInPackage:package];
1171 - (void) webView:(id)sender willShowFullScreenForPlugInView:(id)view {
1172 [webview_ webView:sender willShowFullScreenForPlugInView:view];
1175 - (void) webView:(id)sender didHideFullScreenForPlugInView:(id)view {
1176 [webview_ webView:sender didHideFullScreenForPlugInView:view];
1179 - (void) webView:(id)sender willAddPlugInView:(id)view {
1180 [webview_ webView:sender willAddPlugInView:view];
1183 - (void) webView:(id)sender didObserveDeferredContentChange:(int)change forFrame:(id)frame {
1184 [webview_ webView:sender didObserveDeferredContentChange:change forFrame:frame];
1187 - (void) webViewDidPreventDefaultForEvent:(id)sender {
1188 [webview_ webViewDidPreventDefaultForEvent:sender];
1191 - (void) _setTileDrawingEnabled:(BOOL)enabled {
1192 //[webview_ setTileDrawingEnabled:enabled];
1195 - (void) setViewportWidth:(float)width {
1196 width_ = width != 0 ? width : [[self class] defaultWidth];
1197 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1200 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1201 [self _setTileDrawingEnabled:NO];
1204 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1205 [self _setTileDrawingEnabled:YES];
1206 [webview_ redrawScaledDocument];
1209 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
1210 [self _setTileDrawingEnabled:NO];
1213 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
1214 [self _setTileDrawingEnabled:YES];
1217 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
1218 [self _setTileDrawingEnabled:YES];
1221 - (id) initWithBook:(RVBook *)book forWidth:(float)width ofClass:(Class)_class {
1222 if ((self = [super initWithBook:book]) != nil) {
1224 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
1227 struct CGRect bounds = [self bounds];
1229 scroller_ = [[UIScrollView alloc] initWithFrame:bounds];
1230 [self addSubview:scroller_];
1232 [scroller_ setFixedBackgroundPattern:YES];
1233 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1235 [scroller_ setScrollingEnabled:YES];
1236 [scroller_ setClipsSubviews:YES];
1239 [scroller_ setAllowsRubberBanding:YES];
1241 [scroller_ setDelegate:self];
1242 [scroller_ setBounces:YES];
1245 [scroller_ setScrollHysteresis:8];
1246 [scroller_ setThumbDetectionEnabled:NO];
1247 [scroller_ setDirectionalScrolling:YES];
1248 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
1249 [scroller_ setEventMode:YES];
1252 [scroller_ setShowBackgroundShadow:NO]; /* YES */
1253 //[scroller_ setAllowsRubberBanding:YES]; /* Vertical */
1256 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
1258 CGRect webrect = [scroller_ bounds];
1259 webrect.size.height = 0;
1266 webview_ = [Documents_ lastObject];
1267 if (webview_ != nil) {
1268 webview_ = [webview_ retain];
1269 webview = [webview_ webView];
1270 [Documents_ removeLastObject];
1271 [webview_ setFrame:webrect];
1276 webview_ = [[$UIWebBrowserView alloc] initWithFrame:webrect];
1277 webview = [webview_ webView];
1279 // XXX: this is terribly (too?) expensive
1280 //[webview_ setDrawsBackground:NO];
1281 [webview setPreferencesIdentifier:@"Cydia"];
1283 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1285 if ([webview_ respondsToSelector:@selector(enableReachability)])
1286 [webview_ enableReachability];
1287 if ([webview_ respondsToSelector:@selector(setAllowsMessaging:)])
1288 [webview_ setAllowsMessaging:YES];
1289 if ([webview_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
1290 [webview_ useSelectionAssistantWithMode:0];
1292 [webview_ setTilingEnabled:YES];
1293 [webview_ setDrawsGrid:NO];
1294 [webview_ setLogsTilingChanges:NO];
1295 [webview_ setTileMinificationFilter:kCAFilterNearest];
1297 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
1298 /* XXX: abstractify */
1299 [webview_ setDataDetectorTypes:0x80000000];
1301 [webview_ setDetectsPhoneNumbers:NO];
1303 [webview_ setAutoresizes:YES];
1305 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1306 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1307 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1308 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1310 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1312 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1313 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1314 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1316 [webview_ _setDocumentType:0x4];
1318 if ([webview_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
1319 [webview_ setZoomsFocusedFormControl:YES];
1320 [webview_ setContentsPosition:7];
1321 [webview_ setEnabledGestures:0xa];
1322 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1323 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1325 [webview_ setSmoothsFonts:YES];
1326 [webview_ setAllowsImageSheet:YES];
1327 [webview _setUsesLoaderCache:YES];
1329 [webview setGroupName:@"CydiaGroup"];
1331 WebPreferences *preferences([webview preferences]);
1333 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1334 [webview _setLayoutInterval:0];
1336 [preferences _setLayoutInterval:0];
1339 [self setViewportWidth:width];
1341 [webview_ setDelegate:self];
1342 [webview_ setGestureDelegate:self];
1344 if ([webview_ respondsToSelector:@selector(setFormEditingDelegate:)])
1345 [webview_ setFormEditingDelegate:self];
1347 [webview_ setInteractionDelegate:self];
1349 [scroller_ addSubview:webview_];
1351 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1353 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1355 [webview setFrameLoadDelegate:indirect_];
1356 [webview setPolicyDelegate:indirect_];
1357 [webview setResourceLoadDelegate:indirect_];
1358 [webview setUIDelegate:indirect_];
1360 /* XXX: do not turn this on under penalty of extreme pain */
1361 [webview setScriptDebugDelegate:nil];
1365 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1366 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(bounds.size.width - 39, 12, indsize.width, indsize.height)];
1367 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1369 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1370 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1372 /*UIWebView *test([[[UIWebView alloc] initWithFrame:[self bounds]] autorelease]);
1373 [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
1374 [self addSubview:test];*/
1378 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
1379 return [self initWithBook:book forWidth:width ofClass:[self class]];
1382 - (id) initWithBook:(RVBook *)book {
1383 return [self initWithBook:book forWidth:0];
1386 - (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
1388 WebView *webview([webview_ webView]);
1389 NSString *string([webview stringByEvaluatingJavaScriptFromString:script]);
1394 - (void) callFunction:(WebScriptObject *)function {
1397 WebView *webview([webview_ webView]);
1398 WebFrame *frame([webview mainFrame]);
1400 id _private(MSHookIvar<id>(webview, "_private"));
1401 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1402 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1405 if (settings == NULL)
1408 no = settings->JavaScriptCanOpenWindowsAutomatically();
1409 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1412 if (UIWindow *window = [self window])
1413 if (UIResponder *responder = [window firstResponder])
1414 [responder resignFirstResponder];
1416 JSObjectRef object([function JSObject]);
1417 JSGlobalContextRef context([frame globalContext]);
1418 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1420 if (settings != NULL)
1421 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1426 - (void) didCloseBook:(RVBook *)book {
1428 [self callFunction:closer_];
1431 - (void) __rightButtonClicked {
1436 - (void) _rightButtonClicked {
1438 if (function_ != nil)
1439 [self callFunction:function_];
1442 [self __rightButtonClicked];
1445 - (id) _rightButtonTitle {
1446 return UCLocalize("RELOAD");
1449 - (id) rightButtonTitle {
1450 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
1453 - (UINavigationButtonStyle) rightButtonStyle {
1454 if (style_ == nil) normal:
1455 return UINavigationButtonStyleNormal;
1456 else if ([style_ isEqualToString:@"Normal"])
1457 return UINavigationButtonStyleNormal;
1458 else if ([style_ isEqualToString:@"Back"])
1459 return UINavigationButtonStyleBack;
1460 else if ([style_ isEqualToString:@"Highlighted"])
1461 return UINavigationButtonStyleHighlighted;
1462 else if ([style_ isEqualToString:@"Destructive"])
1463 return UINavigationButtonStyleDestructive;
1467 - (NSString *) title {
1468 return title_ == nil ? UCLocalize("LOADING") : title_;
1471 - (NSString *) backButtonTitle {
1472 return UCLocalize("BROWSER");
1475 - (void) setPageActive:(BOOL)active {
1477 [indicator_ removeFromSuperview];
1479 [[book_ navigationBar] addSubview:indicator_];
1482 - (void) resetViewAnimated:(BOOL)animated {
1485 - (void) setPushed:(bool)pushed {
1489 + (float) defaultWidth {