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 @interface BrowserViewActualView : UIView {
153 UIWebDocumentView *documentView;
155 @property (nonatomic, retain) UIWebDocumentView *documentView;
158 @implementation BrowserViewActualView
160 @synthesize documentView;
163 [documentView release];
167 - (void)layoutSubviews {
168 [super layoutSubviews];
169 if ([documentView respondsToSelector:@selector(setMinimumSize:)])
170 [documentView setMinimumSize:documentView.bounds.size];
175 #define ShowInternals 0
178 #define lprintf(args...) fprintf(stderr, args)
180 @implementation BrowserView
183 #include "UICaboodle/UCInternal.h"
186 + (void) _initialize {
187 //[WebView enableWebThread];
189 WebPreferences *preferences([WebPreferences standardPreferences]);
190 [preferences setCacheModel:WebCacheModelDocumentBrowser];
191 [preferences setOfflineWebApplicationCacheEnabled:YES];
193 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
195 $GSSystemCopyCapability = reinterpret_cast<CFArrayRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemCopyCapability"));
196 $GSSystemGetCapability = reinterpret_cast<CFArrayRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemGetCapability"));
197 $UIFormAssistant = objc_getClass("UIFormAssistant");
199 $UIWebBrowserView = objc_getClass("UIWebBrowserView");
200 if ($UIWebBrowserView == nil) {
202 $UIWebBrowserView = objc_getClass("UIWebDocumentView");
210 NSLog(@"[BrowserView dealloc]");
213 if (challenge_ != nil)
214 [challenge_ release];
218 WebView *webview = [document_ webView];
219 [webview setFrameLoadDelegate:nil];
220 [webview setResourceLoadDelegate:nil];
221 [webview setUIDelegate:nil];
222 [webview setScriptDebugDelegate:nil];
223 [webview setPolicyDelegate:nil];
225 /* XXX: these are set by UIWebDocumentView
226 [webview setDownloadDelegate:nil];
227 [webview _setFormDelegate:nil];
228 [webview _setUIKitDelegate:nil];
229 [webview setEditingDelegate:nil];*/
231 /* XXX: no one sets this, ever
232 [webview setWebMailDelegate:nil];*/
234 [document_ setDelegate:nil];
235 [document_ setGestureDelegate:nil];
237 if ([document_ respondsToSelector:@selector(setFormEditingDelegate:)])
238 [document_ setFormEditingDelegate:nil];
240 [document_ setInteractionDelegate:nil];
242 [indirect_ setDelegate:nil];
244 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
249 [document_ removeFromSuperview];
250 [Documents_ addObject:[document_ autorelease]];
259 [scroller_ setDelegate:nil];
265 if (function_ != nil)
275 [indicator_ release];
278 if (sensitive_ != nil)
279 [sensitive_ release];
285 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
286 [self loadRequest:[NSURLRequest
289 timeoutInterval:120.0
293 - (void) loadURL:(NSURL *)url {
294 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
297 - (void) loadRequest:(NSURLRequest *)request {
302 [document_ loadRequest:request];
310 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
311 [self loadRequest:request_];
313 UIActionSheet *sheet = [[[UIActionSheet alloc]
314 initWithTitle:UCLocalize("RESUBMIT_FORM")
315 buttons:[NSArray arrayWithObjects:UCLocalize("CANCEL"), UCLocalize("SUBMIT"), nil]
321 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
323 [sheet setNumberOfRows:1];
324 [sheet popupAlertAnimated:YES];
328 - (WebView *) webView {
329 return [document_ webView];
332 - (UIWebDocumentView *) documentView {
336 /* XXX: WebThreadLock? */
337 - (void) _fixScroller:(CGRect)bounds {
340 if (!editing_ || $UIFormAssistant == nil)
343 UIFormAssistant *assistant([$UIFormAssistant sharedFormAssistant]);
344 CGRect peripheral([assistant peripheralFrame]);
346 NSLog(@"per:%f", peripheral.size.height);
348 extra = peripheral.size.height;
351 CGRect subrect([scroller_ frame]);
352 subrect.size.height -= extra;
354 if ([scroller_ respondsToSelector:@selector(setScrollerIndicatorSubrect:)])
355 [scroller_ setScrollerIndicatorSubrect:subrect];
357 [document_ setValue:[NSValue valueWithSize:NSMakeSize(subrect.size.width, subrect.size.height)] forGestureAttribute:UIGestureAttributeVisibleSize];
360 size.height += extra;
361 [scroller_ setContentSize:size];
363 if ([scroller_ respondsToSelector:@selector(releaseRubberBandIfNecessary)])
364 [scroller_ releaseRubberBandIfNecessary];
367 - (void) fixScroller {
368 CGRect bounds([document_ documentBounds]);
370 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
372 [self _fixScroller:bounds];
375 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
378 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
380 [self _fixScroller:frame];
383 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
384 [self view:sender didSetFrame:frame];
387 - (void) pushPage:(UCViewController *)page {
388 [page setDelegate:delegate_];
389 [[self navigationItem] setTitle:title_];
390 [[self navigationController] pushViewController:page animated:YES];
396 // WTR: [self autorelease];
398 [[self navigationController] pushViewController:self animated:YES];
401 - (void) swapPage:(UCViewController *)page {
402 [page setDelegate:delegate_];
403 if (pushed_) [[self navigationController] popViewControllerAnimated:NO];
405 [[self navigationController] pushViewController:page animated:NO];
408 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
410 NSLog(@"getSpecial:%@", url);
413 if (UCViewController *page = [delegate_ pageForURL:url hasTag:NULL]) {
415 [self swapPage:page];
417 [self pushPage:page];
424 - (void) formAssistant:(id)sender didBeginEditingFormNode:(id)node {
427 - (void) formAssistant:(id)sender didEndEditingFormNode:(id)node {
431 - (void) webViewShow:(WebView *)sender {
432 /* XXX: this is where I cry myself to sleep */
435 - (bool) _allowJavaScriptPanel {
439 - (bool) allowSensitiveRequests {
440 return [self _allowJavaScriptPanel];
443 - (void) _promptForSensitive:(NSMutableArray *)array {
444 NSString *name([array objectAtIndex:0]);
446 UIActionSheet *sheet = [[[UIActionSheet alloc]
448 buttons:[NSArray arrayWithObjects:UCLocalize("YES"), UCLocalize("NO"), nil]
454 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
456 NSString *host(@"XXX");
458 [sheet setNumberOfRows:1];
459 [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]];
460 [sheet popupAlertAnimated:YES];
462 NSRunLoop *loop([NSRunLoop currentRunLoop]);
463 NSDate *future([NSDate distantFuture]);
465 while (sensitive_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
467 NSNumber *sensitive([sensitive_ autorelease]);
471 [array replaceObjectAtIndex:0 withObject:sensitive];
474 - (bool) promptForSensitive:(NSString *)name {
475 if (![self allowSensitiveRequests])
478 NSMutableArray *array([NSMutableArray arrayWithCapacity:1]);
479 [array addObject:name];
481 [self performSelectorOnMainThread:@selector(_promptForSensitive:) withObject:array waitUntilDone:YES];
482 return [[array lastObject] boolValue];
485 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
486 if (![self _allowJavaScriptPanel])
490 UIActionSheet *sheet = [[[UIActionSheet alloc]
492 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), nil]
498 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
500 [sheet setBodyText:message];
501 [sheet popupAlertAnimated:YES];
504 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
505 if (![self _allowJavaScriptPanel])
509 UIActionSheet *sheet = [[[UIActionSheet alloc]
511 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), UCLocalize("CANCEL"), nil]
517 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
519 [sheet setNumberOfRows:1];
520 [sheet setBodyText:message];
521 [sheet popupAlertAnimated:YES];
523 NSRunLoop *loop([NSRunLoop currentRunLoop]);
524 NSDate *future([NSDate distantFuture]);
526 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
528 NSNumber *confirm([confirm_ autorelease]);
532 return [confirm boolValue];
535 - (void) setAutoPopup:(BOOL)popup {
539 - (void) setSpecial:(id)function {
541 [special_ autorelease];
542 special_ = function == nil ? nil : [function retain];
545 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
547 [button_ autorelease];
548 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
551 [style_ autorelease];
552 style_ = style == nil ? nil : [style retain];
554 if (function_ != nil)
555 [function_ autorelease];
556 function_ = function == nil ? nil : [function retain];
558 [self reloadButtons];
561 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
563 [button_ autorelease];
564 button_ = button == nil ? nil : [button retain];
567 [style_ autorelease];
568 style_ = style == nil ? nil : [style retain];
570 if (function_ != nil)
571 [function_ autorelease];
572 function_ = function == nil ? nil : [function retain];
574 [self reloadButtons];
577 - (void) setFinishHook:(id)function {
579 [finish_ autorelease];
580 finish_ = function == nil ? nil : [function retain];
583 - (void) setPopupHook:(id)function {
585 [closer_ autorelease];
586 closer_ = function == nil ? nil : [function retain];
589 - (void) _openMailToURL:(NSURL *)url {
590 [UIApp openURL:url];// asPanel:YES];
593 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
597 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
601 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
606 - (void) webViewClose:(WebView *)sender {
611 [[self navigationController] dismissModalViewControllerAnimated:YES];
614 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
617 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
618 NSLog(@"err:%@", error);
621 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
623 NSLog(@"nwa:%@", name);
626 if (NSURL *url = [request URL]) {
627 if (name == nil) unknown: {
628 if (![self getSpecial:url swap:NO]) {
629 NSString *scheme([[url scheme] lowercaseString]);
630 if ([scheme isEqualToString:@"mailto"])
631 [self _openMailToURL:url];
634 } else if ([name isEqualToString:@"_open"])
635 [delegate_ openURL:url];
636 else if ([name isEqualToString:@"_popup"]) {
637 NSString *scheme([[url scheme] lowercaseString]);
638 if ([scheme isEqualToString:@"mailto"])
639 [self _openMailToURL:url];
641 UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]);
642 [navigation setHook:indirect_];
644 UCViewController *page([delegate_ pageForURL:url hasTag:NULL]);
646 /* XXX: call createWebViewWithRequest instead? */
648 BrowserView *browser([[[class_ alloc] init] autorelease]);
649 [browser loadURL:url];
653 [navigation setDelegate:delegate_];
654 [page setDelegate:delegate_];
656 [navigation setViewControllers:[NSArray arrayWithObject:page]];
657 UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]
658 initWithTitle:UCLocalize("CLOSE")
659 style:UIBarButtonItemStylePlain
661 action:@selector(close)
663 [[page navigationItem] setLeftBarButtonItem:closeItem];
666 [[self navigationController] presentModalViewController:navigation animated:YES];
675 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
676 if ([WebView canShowMIMEType:type])
679 // XXX: handle more mime types!
682 WebView *webview([document_ webView]);
683 if (frame == [webview mainFrame])
684 [UIApp openURL:[request URL]];
688 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
689 if (request == nil) ignore: {
694 NSURL *url([request URL]);
695 NSString *host([url host]);
697 if (url == nil) use: {
698 if (!error_ && [frame parentFrame] == nil) {
700 [request_ autorelease];
701 request_ = [request retain];
703 NSLog(@"dpn:%@", request_);
709 WebView *webview([document_ webView]);
710 if (frame == [webview mainFrame])
715 else NSLog(@"nav:%@:%@", url, [action description]);
718 const NSArray *capability;
720 if ($GSSystemCopyCapability != NULL) {
721 capability = reinterpret_cast<const NSArray *>((*$GSSystemCopyCapability)(kGSDisplayIdentifiersCapability));
722 capability = [capability autorelease];
723 } else if ($GSSystemGetCapability != NULL) {
724 capability = reinterpret_cast<const NSArray *>((*$GSSystemGetCapability)(kGSDisplayIdentifiersCapability));
730 if (capability != nil && (
731 [url isGoogleMapsURL] && [capability containsObject:@"com.apple.Maps"] && (open = [url mapsURL]) != nil||
732 [host hasSuffix:@"youtube.com"] && [capability containsObject:@"com.apple.youtube"] && (open = [url youTubeURL]) != nil ||
733 [url respondsToSelector:@selector(phobosURL)] && (open = [url phobosURL]) != nil
741 int store(_not(int));
742 if (NSURL *itms = [url itmsURL:&store]) {
744 NSLog(@"itms#%@#%u#%@", url, store, itms);
747 if (capability != nil && (
748 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
749 store == 2 && [capability containsObject:@"com.apple.AppStore"]
756 NSString *scheme([[url scheme] lowercaseString]);
758 if ([scheme isEqualToString:@"tel"]) {
763 if ([scheme isEqualToString:@"mailto"]) {
764 [self _openMailToURL:url];
768 if ([self getSpecial:url swap:YES])
770 else if ([WebView _canHandleRequest:request])
772 else if ([url isSpringboardHandledURL])
778 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
779 //lprintf("Status:%s\n", [text UTF8String]);
782 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
783 NSString *context([sheet context]);
785 if ([context isEqualToString:@"alert"]) {
788 } else if ([context isEqualToString:@"confirm"]) {
791 confirm_ = [NSNumber numberWithBool:YES];
795 confirm_ = [NSNumber numberWithBool:NO];
800 } else if ([context isEqualToString:@"sensitive"]) {
803 sensitive_ = [NSNumber numberWithBool:YES];
807 sensitive_ = [NSNumber numberWithBool:NO];
812 } else if ([context isEqualToString:@"challenge"]) {
813 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
817 NSString *username([[sheet textFieldAtIndex:0] text]);
818 NSString *password([[sheet textFieldAtIndex:1] text]);
820 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
822 [sender useCredential:credential forAuthenticationChallenge:challenge_];
826 [sender cancelAuthenticationChallenge:challenge_];
832 [challenge_ release];
836 } else if ([context isEqualToString:@"submit"]) {
842 if (request_ != nil) {
844 [document_ loadRequest:request_];
856 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
857 challenge_ = [challenge retain];
859 NSURLProtectionSpace *space([challenge protectionSpace]);
860 NSString *realm([space realm]);
864 UIActionSheet *sheet = [[[UIActionSheet alloc]
866 buttons:[NSArray arrayWithObjects:UCLocalize("LOGIN"), UCLocalize("CANCEL"), nil]
872 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
874 [sheet setNumberOfRows:1];
876 [sheet addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
877 [sheet addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
879 UITextField *username([sheet textFieldAtIndex:0]); {
880 UITextInputTraits *traits([username textInputTraits]);
881 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
882 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
883 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
884 [traits setReturnKeyType:UIReturnKeyNext];
887 UITextField *password([sheet textFieldAtIndex:1]); {
888 UITextInputTraits *traits([password textInputTraits]);
889 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
890 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
891 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
892 // XXX: UIReturnKeyDone
893 [traits setReturnKeyType:UIReturnKeyNext];
894 [traits setSecureTextEntry:YES];
897 [sheet popupAlertAnimated:YES];
900 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
904 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
905 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
907 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
908 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
911 NSNumber *value([features objectForKey:@"width"]);
912 float width(value == nil ? 0 : [value floatValue]);
914 UCNavigationController *navigation(!popup_ ? [self navigationController] : [[[UCNavigationController alloc] init] autorelease]);
916 /* XXX: deal with cydia:// pages */
917 BrowserView *browser([[[class_ alloc] initWithWidth:width] autorelease]);
919 if (features != nil && popup_) {
920 [navigation setDelegate:delegate_];
921 [navigation setHook:indirect_];
922 [browser setDelegate:delegate_];
924 [browser loadRequest:request];
926 [navigation setViewControllers:[NSArray arrayWithObject:browser]];
927 UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]
928 initWithTitle:UCLocalize("CLOSE")
929 style:UIBarButtonItemStylePlain
931 action:@selector(close)
933 [[browser navigationItem] setLeftBarButtonItem:closeItem];
936 [[self navigationController] presentModalViewController:navigation animated:YES];
937 } /*else if (request == nil) {
938 [[self navigationItem] setTitle:title_];
939 [browser setDelegate:delegate_];
942 [self pushPage:browser];
943 [browser loadRequest:request];
946 return [browser webView];
949 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
950 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
951 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
954 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
955 if ([frame parentFrame] != nil)
958 title_ = [title retain];
959 [[self navigationItem] setTitle:title_];
962 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
963 /*if ([loading_ count] == 0)
965 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
967 if ([frame parentFrame] == nil) {
968 [document_ resignFirstResponder];
977 if (button_ != nil) {
987 if (function_ != nil) {
992 if (finish_ != nil) {
997 if (closer_ != nil) {
1002 if (special_ != nil) {
1007 [[self navigationItem] setTitle:title_];
1010 CGRect webrect = [scroller_ bounds];
1011 webrect.size.height = 1;
1012 [document_ setFrame:webrect];
1015 if ([scroller_ respondsToSelector:@selector(scrollPointVisibleAtTopLeft:)])
1016 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
1018 [scroller_ scrollRectToVisible:CGRectZero animated:NO];
1020 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
1021 [scroller_ setZoomScale:1 duration:0];
1022 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
1023 [scroller_ _setZoomScale:1 duration:0];
1024 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
1025 [scroller_ setZoomScale:1 animated:NO];*/
1028 CGRect webrect = [scroller_ bounds];
1029 webrect.size.height = 0;
1030 [document_ setFrame:webrect];
1034 [self reloadButtons];
1037 - (void) didFinishLoading { }
1039 - (void) _finishLoading {
1040 size_t count([loading_ count]);
1042 [self autorelease];*/
1043 if (reloading_ || count != 0)
1046 [self callFunction:finish_];
1048 [self reloadButtons];
1051 - (bool) isLoading {
1052 return [loading_ count] != 0;
1055 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
1056 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
1059 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
1060 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
1063 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
1064 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
1067 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
1069 return [document_ webView:sender didCommitLoadForFrame:frame];
1072 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
1073 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
1076 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
1077 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1078 [self _finishLoading];
1080 if ([frame parentFrame] == nil) {
1081 if (DOMDocument *document = [frame DOMDocument])
1082 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
1083 for (DOMHTMLBodyElement *body in bodies) {
1084 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
1086 bool colored(false);
1088 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
1089 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
1090 DOMRGBColor *rgb([color getRGBColorValue]);
1092 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
1093 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
1094 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
1095 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
1099 if (red == 0xc7 && green == 0xce && blue == 0xd5)
1100 uic = [UIColor pinStripeColor];
1101 else if (alpha != 0)
1103 colorWithRed:(red / 255)
1111 [scroller_ setBackgroundColor:uic];
1117 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1122 return [document_ webView:sender didFinishLoadForFrame:frame];
1125 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
1127 /*if ([frame parentFrame] == nil)
1128 [self autorelease];*/
1130 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1131 [self _finishLoading];
1136 if ([frame parentFrame] == nil) {
1137 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
1138 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
1139 [[error localizedDescription] stringByAddingPercentEscapes]
1146 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1147 [self _didFailWithError:error forFrame:frame];
1148 if ([document_ respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
1149 [document_ webView:sender didFailLoadWithError:error forFrame:frame];
1152 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1153 [self _didFailWithError:error forFrame:frame];
1156 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1157 #if LogBrowser || ForSaurik
1158 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1162 - (void) webView:(WebView *)sender didReceiveMessage:(NSDictionary *)dictionary {
1163 #if LogBrowser || ForSaurik
1164 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1166 if ([document_ respondsToSelector:@selector(webView:didReceiveMessage:)])
1167 [document_ webView:sender didReceiveMessage:dictionary];
1170 - (void) webView:(id)sender willCloseFrame:(id)frame {
1171 if ([document_ respondsToSelector:@selector(webView:willCloseFrame:)])
1172 [document_ webView:sender willCloseFrame:frame];
1175 - (void) webView:(id)sender didFinishDocumentLoadForFrame:(id)frame {
1176 if ([document_ respondsToSelector:@selector(webView:didFinishDocumentLoadForFrame:)])
1177 [document_ webView:sender didFinishDocumentLoadForFrame:frame];
1180 - (void) webView:(id)sender didFirstLayoutInFrame:(id)frame {
1181 if ([document_ respondsToSelector:@selector(webView:didFirstLayoutInFrame:)])
1182 [document_ webView:sender didFirstLayoutInFrame:frame];
1185 - (void) webViewFormEditedStatusHasChanged:(id)changed {
1186 if ([document_ respondsToSelector:@selector(webViewFormEditedStatusHasChanged:)])
1187 [document_ webViewFormEditedStatusHasChanged:changed];
1190 - (void) webView:(id)sender formStateDidFocusNode:(id)formState {
1191 if ([document_ respondsToSelector:@selector(webView:formStateDidFocusNode:)])
1192 [document_ webView:sender formStateDidFocusNode:formState];
1195 - (void) webView:(id)sender formStateDidBlurNode:(id)formState {
1196 if ([document_ respondsToSelector:@selector(webView:formStateDidBlurNode:)])
1197 [document_ webView:sender formStateDidBlurNode:formState];
1200 /* XXX: fix this stupid include file
1201 - (void) webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)database {
1202 [origin setQuota:0x500000];
1205 - (void) webViewDidLayout:(id)sender {
1206 [document_ webViewDidLayout:sender];
1209 - (void) webView:(id)sender didFirstVisuallyNonEmptyLayoutInFrame:(id)frame {
1210 [document_ webView:sender didFirstVisuallyNonEmptyLayoutInFrame:frame];
1213 - (void) webView:(id)sender saveStateToHistoryItem:(id)item forFrame:(id)frame {
1214 [document_ webView:sender saveStateToHistoryItem:item forFrame:frame];
1217 - (void) webView:(id)sender restoreStateFromHistoryItem:(id)item forFrame:(id)frame force:(BOOL)force {
1218 [document_ webView:sender restoreStateFromHistoryItem:item forFrame:frame force:force];
1221 - (void) webView:(id)sender attachRootLayer:(id)layer {
1222 [document_ webView:sender attachRootLayer:layer];
1225 - (id) webView:(id)sender plugInViewWithArguments:(id)arguments fromPlugInPackage:(id)package {
1226 return [document_ webView:sender plugInViewWithArguments:arguments fromPlugInPackage:package];
1229 - (void) webView:(id)sender willShowFullScreenForPlugInView:(id)view {
1230 [document_ webView:sender willShowFullScreenForPlugInView:view];
1233 - (void) webView:(id)sender didHideFullScreenForPlugInView:(id)view {
1234 [document_ webView:sender didHideFullScreenForPlugInView:view];
1237 - (void) webView:(id)sender willAddPlugInView:(id)view {
1238 [document_ webView:sender willAddPlugInView:view];
1241 - (void) webView:(id)sender didObserveDeferredContentChange:(int)change forFrame:(id)frame {
1242 [document_ webView:sender didObserveDeferredContentChange:change forFrame:frame];
1245 - (void) webViewDidPreventDefaultForEvent:(id)sender {
1246 [document_ webViewDidPreventDefaultForEvent:sender];
1249 - (void) _setTileDrawingEnabled:(BOOL)enabled {
1250 //[document_ setTileDrawingEnabled:enabled];
1253 - (void) setViewportWidth:(float)width {
1254 width_ = width != 0 ? width : [[self class] defaultWidth];
1255 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1258 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1259 [self _setTileDrawingEnabled:NO];
1262 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1263 [self _setTileDrawingEnabled:YES];
1264 [document_ redrawScaledDocument];
1267 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
1268 [self _setTileDrawingEnabled:NO];
1271 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
1272 [self _setTileDrawingEnabled:YES];
1275 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
1276 [self _setTileDrawingEnabled:YES];
1279 - (id) initWithWidth:(float)width ofClass:(Class)_class {
1280 if ((self = [super init]) != nil) {
1282 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
1285 BrowserViewActualView *actualView = [[BrowserViewActualView alloc] initWithFrame:CGRectZero];
1286 [self setView:actualView];
1288 struct CGRect bounds = [[self view] bounds];
1290 scroller_ = [[objc_getClass(Wildcat_ ? "UIScrollView" : "UIScroller") alloc] initWithFrame:bounds];
1291 [[self view] addSubview:scroller_];
1293 [scroller_ setFixedBackgroundPattern:YES];
1294 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1296 [scroller_ setScrollingEnabled:YES];
1297 [scroller_ setClipsSubviews:YES];
1300 [scroller_ setAllowsRubberBanding:YES];
1302 [scroller_ setDelegate:self];
1303 [scroller_ setBounces:YES];
1306 [scroller_ setScrollHysteresis:8];
1307 [scroller_ setThumbDetectionEnabled:NO];
1308 [scroller_ setDirectionalScrolling:YES];
1309 //[scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
1310 [scroller_ setEventMode:YES];
1314 UIScrollView *scroller((UIScrollView *)scroller_);
1315 //[scroller setDirectionalLockEnabled:NO];
1316 [scroller setDelaysContentTouches:NO];
1317 //[scroller setScrollsToTop:NO];
1318 //[scroller setCanCancelContentTouches:NO];
1321 [scroller_ setShowBackgroundShadow:NO]; /* YES */
1322 //[scroller_ setAllowsRubberBanding:YES]; /* Vertical */
1325 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
1327 CGRect webrect = [scroller_ bounds];
1328 webrect.size.height = 0;
1335 document_ = [Documents_ lastObject];
1336 if (document_ != nil) {
1337 document_ = [document_ retain];
1338 webview = [document_ webView];
1339 [Documents_ removeLastObject];
1340 [document_ setFrame:webrect];
1345 document_ = [[$UIWebBrowserView alloc] initWithFrame:webrect];
1346 webview = [document_ webView];
1348 // XXX: this is terribly (too?) expensive
1349 //[document_ setDrawsBackground:NO];
1350 [webview setPreferencesIdentifier:@"Cydia"];
1352 [document_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1354 if ([document_ respondsToSelector:@selector(enableReachability)])
1355 [document_ enableReachability];
1356 if ([document_ respondsToSelector:@selector(setAllowsMessaging:)])
1357 [document_ setAllowsMessaging:YES];
1358 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
1359 [document_ useSelectionAssistantWithMode:0];
1361 [document_ setTilingEnabled:YES];
1362 [document_ setDrawsGrid:NO];
1363 [document_ setLogsTilingChanges:NO];
1364 [document_ setTileMinificationFilter:kCAFilterNearest];
1366 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
1367 /* XXX: abstractify */
1368 [document_ setDataDetectorTypes:0x80000000];
1370 [document_ setDetectsPhoneNumbers:NO];
1372 [document_ setAutoresizes:YES];
1374 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
1375 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
1376 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1377 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1379 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1381 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
1382 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1383 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1385 [document_ _setDocumentType:0x4];
1387 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
1388 [document_ setZoomsFocusedFormControl:YES];
1389 [document_ setContentsPosition:7];
1390 [document_ setEnabledGestures:0xa];
1391 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1392 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1394 [document_ setSmoothsFonts:YES];
1395 [document_ setAllowsImageSheet:YES];
1396 [webview _setUsesLoaderCache:YES];
1398 [webview setGroupName:@"CydiaGroup"];
1400 WebPreferences *preferences([webview preferences]);
1402 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1403 [webview _setLayoutInterval:0];
1405 [preferences _setLayoutInterval:0];
1408 actualView.documentView = document_;
1409 [actualView release];
1411 [self setViewportWidth:width];
1413 [document_ setDelegate:self];
1414 [document_ setGestureDelegate:self];
1416 if ([document_ respondsToSelector:@selector(setFormEditingDelegate:)])
1417 [document_ setFormEditingDelegate:self];
1419 [document_ setInteractionDelegate:self];
1421 [scroller_ addSubview:document_];
1423 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1425 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1427 [webview setFrameLoadDelegate:indirect_];
1428 [webview setPolicyDelegate:indirect_];
1429 [webview setResourceLoadDelegate:indirect_];
1430 [webview setUIDelegate:indirect_];
1432 /* XXX: do not turn this on under penalty of extreme pain */
1433 [webview setScriptDebugDelegate:nil];
1437 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1438 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(15, 5, indsize.width, indsize.height)];
1439 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1440 [indicator_ startAnimation];
1442 [scroller_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1443 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1444 [document_ setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
1446 /*UIWebView *test([[[UIWebView alloc] initWithFrame:[[self view] bounds]] autorelease]);
1447 [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
1448 [[self view] addSubview:test];*/
1452 - (id) initWithWidth:(float)width {
1453 return [self initWithWidth:width ofClass:[self class]];
1457 return [self initWithWidth:0];
1460 - (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
1462 WebView *webview([document_ webView]);
1463 NSString *string([webview stringByEvaluatingJavaScriptFromString:script]);
1468 - (void) callFunction:(WebScriptObject *)function {
1471 WebView *webview([document_ webView]);
1472 WebFrame *frame([webview mainFrame]);
1474 id _private(MSHookIvar<id>(webview, "_private"));
1475 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1476 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1479 if (settings == NULL)
1482 no = settings->JavaScriptCanOpenWindowsAutomatically();
1483 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1486 if (UIWindow *window = [[self view] window])
1487 if (UIResponder *responder = [window firstResponder])
1488 [responder resignFirstResponder];
1490 JSObjectRef object([function JSObject]);
1491 JSGlobalContextRef context([frame globalContext]);
1492 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1494 if (settings != NULL)
1495 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1500 - (void) didDismissModalViewController {
1502 [self callFunction:closer_];
1505 - (void) __rightButtonClicked {
1510 - (void) _rightButtonClicked {
1512 if (function_ != nil)
1513 [self callFunction:function_];
1516 [self __rightButtonClicked];
1519 - (UINavigationButtonStyle) rightButtonStyle {
1520 if (style_ == nil) normal:
1521 return UINavigationButtonStyleNormal;
1522 else if ([style_ isEqualToString:@"Normal"])
1523 return UINavigationButtonStyleNormal;
1524 else if ([style_ isEqualToString:@"Back"])
1525 return UINavigationButtonStyleBack;
1526 else if ([style_ isEqualToString:@"Highlighted"])
1527 return UINavigationButtonStyleHighlighted;
1528 else if ([style_ isEqualToString:@"Destructive"])
1529 return UINavigationButtonStyleDestructive;
1533 - (void) reloadButtons {
1534 if ([self isLoading]) {
1535 UIBarButtonItem *reloadItem = [[UIBarButtonItem alloc]
1537 style:UIBarButtonItemStylePlain
1539 action:@selector(_rightButtonClicked)
1541 [[self navigationItem] setRightBarButtonItem:reloadItem];
1542 [[reloadItem view] addSubview:indicator_];
1543 [[self navigationItem] setTitle:UCLocalize("LOADING")];
1544 [reloadItem release];
1546 UIBarButtonItem *reloadItem = [[UIBarButtonItem alloc]
1547 initWithTitle:button_ ?: UCLocalize("RELOAD")
1548 style:[self rightButtonStyle]
1550 action:@selector(_rightButtonClicked)
1552 [[self navigationItem] setRightBarButtonItem:reloadItem animated:YES];
1553 [[self navigationItem] setTitle:title_];
1554 [reloadItem release];
1556 if (function_ == nil) [self didFinishLoading];
1560 - (void) setPageActive:(BOOL)active {
1562 [indicator_ removeFromSuperview];
1564 [[[[self navigationItem] rightBarButtonItem] view] addSubview:indicator_];
1567 - (void) resetViewAnimated:(BOOL)animated {
1570 - (void) setPushed:(bool)pushed {
1574 + (float) defaultWidth {