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"
13 @interface NSString (UIKit)
14 - (NSString *) stringByAddingPercentEscapes;
17 /* Indirect Delegate {{{ */
18 @interface IndirectDelegate : NSObject {
19 _transient volatile id delegate_;
22 - (void) setDelegate:(id)delegate;
23 - (id) initWithDelegate:(id)delegate;
26 @implementation IndirectDelegate
28 - (void) setDelegate:(id)delegate {
32 - (id) initWithDelegate:(id)delegate {
37 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
39 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
42 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
44 return [delegate_ webView:sender didCommitLoadForFrame:frame];
47 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
49 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
52 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
54 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
57 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
59 return [delegate_ webView:sender didFinishLoadForFrame:frame];
62 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
64 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
67 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
69 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
72 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
74 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
77 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
79 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
83 - (IMP) methodForSelector:(SEL)sel {
84 if (IMP method = [super methodForSelector:sel])
86 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
90 - (BOOL) respondsToSelector:(SEL)sel {
91 if ([super respondsToSelector:sel])
93 // XXX: WebThreadCreateNSInvocation returns nil
94 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
95 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
98 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
99 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
101 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
102 if (delegate_ != nil)
103 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
105 // XXX: I fucking hate Apple so very very bad
106 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
109 - (void) forwardInvocation:(NSInvocation *)inv {
110 SEL sel = [inv selector];
111 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
112 [inv invokeWithTarget:delegate_];
118 @interface WebView (UICaboodle)
119 - (void) setScriptDebugDelegate:(id)delegate;
120 - (void) _setFormDelegate:(id)delegate;
121 - (void) _setUIKitDelegate:(id)delegate;
122 - (void) setWebMailDelegate:(id)delegate;
123 - (void) _setLayoutInterval:(float)interval;
126 @implementation WebScriptObject (UICaboodle)
129 id length([self valueForKey:@"length"]);
130 if ([length respondsToSelector:@selector(intValue)])
131 return [length intValue];
136 - (id) objectAtIndex:(unsigned)index {
137 return [self webScriptValueAtIndex:index];
143 /* Mail Composition {{{ */
144 @interface MailToView : PopUpView {
145 MailComposeController *controller_;
148 - (id) initWithView:(UIView *)view delegate:(id)delegate url:(NSURL *)url;
152 @implementation MailToView
155 [controller_ release];
159 - (void) mailComposeControllerWillAttemptToSend:(MailComposeController *)controller {
163 - (void) mailComposeControllerDidAttemptToSend:(MailComposeController *)controller mailDelivery:(id)delivery {
164 NSLog(@"did:%@", delivery);
165 // [UIApp setStatusBarShowsProgress:NO];
166 if ([controller error]){
167 NSArray *buttons = [NSArray arrayWithObjects:UCLocalize("OK"), nil];
168 UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize("ERROR") buttons:buttons defaultButtonIndex:0 delegate:self context:self];
169 [mailAlertSheet setBodyText:[controller error]];
170 [mailAlertSheet popupAlertAnimated:YES];
175 NSLog(@"%@", [controller_ error]);
176 NSArray *buttons = [NSArray arrayWithObjects:UCLocalize("OK"), nil];
177 UIActionSheet *mailAlertSheet = [[UIActionSheet alloc] initWithTitle:UCLocalize("ERROR") buttons:buttons defaultButtonIndex:0 delegate:self context:self];
178 [mailAlertSheet setBodyText:[controller_ error]];
179 [mailAlertSheet popupAlertAnimated:YES];
182 - (void) deliverMessage { _pooled
186 if (![controller_ deliverMessage])
187 [self performSelectorOnMainThread:@selector(showError) withObject:nil waitUntilDone:NO];
190 - (void) mailComposeControllerCompositionFinished:(MailComposeController *)controller {
191 if ([controller_ needsDelivery])
192 [NSThread detachNewThreadSelector:@selector(deliverMessage) toTarget:self withObject:nil];
197 - (id) initWithView:(UIView *)view delegate:(id)delegate url:(NSURL *)url {
198 if ((self = [super initWithView:view delegate:delegate]) != nil) {
199 controller_ = [[MailComposeController alloc] initForContentSize:[overlay_ bounds].size];
200 [controller_ setDelegate:self];
201 [controller_ initializeUI];
202 [controller_ setupForURL:url];
204 UIView *view([controller_ view]);
205 [overlay_ addSubview:view];
213 #define ShowInternals 0
216 #define lprintf(args...) fprintf(stderr, args)
218 @implementation BrowserView
221 #include "UICaboodle/UCInternal.h"
224 + (void) _initialize {
225 NSLog(@"INITIALIZING");
226 [WebView enableWebThread];
228 WebPreferences *preferences([WebPreferences standardPreferences]);
229 [preferences setCacheModel:WebCacheModelDocumentBrowser];
230 [preferences setOfflineWebApplicationCacheEnabled:YES];
232 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
237 NSLog(@"[BrowserView dealloc]");
240 if (challenge_ != nil)
241 [challenge_ release];
245 WebView *webview = [webview_ webView];
246 [webview setFrameLoadDelegate:nil];
247 [webview setResourceLoadDelegate:nil];
248 [webview setUIDelegate:nil];
249 [webview setScriptDebugDelegate:nil];
250 [webview setPolicyDelegate:nil];
252 /* XXX: these are set by UIWebDocumentView
253 [webview setDownloadDelegate:nil];
254 [webview _setFormDelegate:nil];
255 [webview _setUIKitDelegate:nil];
256 [webview setEditingDelegate:nil];*/
258 /* XXX: no one sets this, ever
259 [webview setWebMailDelegate:nil];*/
261 [webview_ setDelegate:nil];
262 [webview_ setGestureDelegate:nil];
263 [webview_ setFormEditingDelegate:nil];
264 [webview_ setInteractionDelegate:nil];
266 [indirect_ setDelegate:nil];
268 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
273 [webview_ removeFromSuperview];
274 [Documents_ addObject:[webview_ autorelease]];
283 [scroller_ setDelegate:nil];
289 if (function_ != nil)
299 [indicator_ release];
302 if (sensitive_ != nil)
303 [sensitive_ release];
309 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
310 [self loadRequest:[NSURLRequest
317 - (void) loadURL:(NSURL *)url {
318 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
321 - (void) loadRequest:(NSURLRequest *)request {
326 [webview_ loadRequest:request];
334 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
335 [self loadRequest:request_];
337 UIActionSheet *sheet = [[[UIActionSheet alloc]
338 initWithTitle:UCLocalize("RESUBMIT_FORM")
339 buttons:[NSArray arrayWithObjects:UCLocalize("CANCEL"), UCLocalize("SUBMIT"), nil]
345 [sheet setNumberOfRows:1];
346 [sheet popupAlertAnimated:YES];
350 - (WebView *) webView {
351 return [webview_ webView];
354 - (UIWebDocumentView *) documentView {
358 /* XXX: WebThreadLock? */
359 - (void) _fixScroller:(CGRect)bounds {
364 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
365 CGRect peripheral([assistant peripheralFrame]);
367 NSLog(@"per:%f", peripheral.size.height);
369 extra = peripheral.size.height;
372 CGRect subrect([scroller_ frame]);
373 subrect.size.height -= extra;
374 [scroller_ setScrollerIndicatorSubrect:subrect];
376 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
377 [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
380 size.height += extra;
381 [scroller_ setContentSize:size];
383 [scroller_ releaseRubberBandIfNecessary];
386 - (void) fixScroller {
387 CGRect bounds([webview_ documentBounds]);
389 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
391 [self _fixScroller:bounds];
394 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
397 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
399 [self _fixScroller:frame];
402 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
403 [self view:sender didSetFrame:frame];
406 - (void) pushPage:(RVPage *)page {
407 [page setDelegate:delegate_];
408 [self setBackButtonTitle:title_];
409 [book_ pushPage:page];
415 // WTR: [self autorelease];
417 [book_ pushPage:self];
420 - (void) swapPage:(RVPage *)page {
421 [page setDelegate:delegate_];
423 [book_ swapPage:page];
425 [book_ pushPage:page];
428 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
430 NSLog(@"getSpecial:%@", url);
433 if (RVPage *page = [delegate_ pageForURL:url hasTag:NULL]) {
435 [self swapPage:page];
437 [self pushPage:page];
444 - (void) formAssistant:(id)sender didBeginEditingFormNode:(id)node {
447 - (void) formAssistant:(id)sender didEndEditingFormNode:(id)node {
451 - (void) webViewShow:(WebView *)sender {
452 /* XXX: this is where I cry myself to sleep */
455 - (bool) _allowJavaScriptPanel {
459 - (bool) allowSensitiveRequests {
460 return [self _allowJavaScriptPanel];
463 - (void) _promptForSensitive:(NSMutableArray *)array {
464 NSString *name([array objectAtIndex:0]);
466 UIActionSheet *sheet = [[[UIActionSheet alloc]
468 buttons:[NSArray arrayWithObjects:UCLocalize("YES"), UCLocalize("NO"), nil]
474 NSString *host(@"XXX");
476 [sheet setNumberOfRows:1];
477 [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]];
478 [sheet popupAlertAnimated:YES];
480 NSRunLoop *loop([NSRunLoop currentRunLoop]);
481 NSDate *future([NSDate distantFuture]);
483 while (sensitive_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
485 NSNumber *sensitive([sensitive_ autorelease]);
489 [array replaceObjectAtIndex:0 withObject:sensitive];
492 - (bool) promptForSensitive:(NSString *)name {
493 if (![self allowSensitiveRequests])
496 NSMutableArray *array([NSMutableArray arrayWithCapacity:1]);
497 [array addObject:name];
499 [self performSelectorOnMainThread:@selector(_promptForSensitive:) withObject:array waitUntilDone:YES];
500 return [[array lastObject] boolValue];
503 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
504 if (![self _allowJavaScriptPanel])
508 UIActionSheet *sheet = [[[UIActionSheet alloc]
510 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), nil]
516 [sheet setBodyText:message];
517 [sheet popupAlertAnimated:YES];
520 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
521 if (![self _allowJavaScriptPanel])
525 UIActionSheet *sheet = [[[UIActionSheet alloc]
527 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), UCLocalize("CANCEL"), nil]
533 [sheet setNumberOfRows:1];
534 [sheet setBodyText:message];
535 [sheet popupAlertAnimated:YES];
537 NSRunLoop *loop([NSRunLoop currentRunLoop]);
538 NSDate *future([NSDate distantFuture]);
540 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
542 NSNumber *confirm([confirm_ autorelease]);
546 return [confirm boolValue];
549 - (void) setAutoPopup:(BOOL)popup {
553 - (void) setSpecial:(id)function {
555 [special_ autorelease];
556 special_ = function == nil ? nil : [function retain];
559 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
561 [button_ autorelease];
562 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
565 [style_ autorelease];
566 style_ = style == nil ? nil : [style retain];
568 if (function_ != nil)
569 [function_ autorelease];
570 function_ = function == nil ? nil : [function retain];
572 [self reloadButtons];
575 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
577 [button_ autorelease];
578 button_ = button == nil ? nil : [button retain];
581 [style_ autorelease];
582 style_ = style == nil ? nil : [style retain];
584 if (function_ != nil)
585 [function_ autorelease];
586 function_ = function == nil ? nil : [function retain];
588 [self reloadButtons];
591 - (void) setFinishHook:(id)function {
593 [finish_ autorelease];
594 finish_ = function == nil ? nil : [function retain];
597 - (void) setPopupHook:(id)function {
599 [closer_ autorelease];
600 closer_ = function == nil ? nil : [function retain];
603 - (void) _openMailToURL:(NSURL *)url {
604 // XXX: this makes me sad
606 [[[MailToView alloc] initWithView:underlay_ delegate:self url:url] autorelease];
608 [UIApp openURL:url];// asPanel:YES];
612 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
616 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
620 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
625 - (void) webViewClose:(WebView *)sender {
633 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
636 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
637 NSLog(@"err:%@", error);
640 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
642 NSLog(@"nwa:%@", name);
645 if (NSURL *url = [request URL]) {
646 if (name == nil) unknown: {
647 if (![self getSpecial:url swap:NO]) {
648 NSString *scheme([[url scheme] lowercaseString]);
649 if ([scheme isEqualToString:@"mailto"])
650 [self _openMailToURL:url];
653 } else if ([name isEqualToString:@"_open"])
654 [delegate_ openURL:url];
655 else if ([name isEqualToString:@"_popup"]) {
656 NSString *scheme([[url scheme] lowercaseString]);
657 if ([scheme isEqualToString:@"mailto"])
658 [self _openMailToURL:url];
660 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
661 [book setHook:indirect_];
663 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
665 /* XXX: call createWebViewWithRequest instead? */
667 [self setBackButtonTitle:title_];
669 BrowserView *browser([[[class_ alloc] initWithBook:book] autorelease]);
670 [browser loadURL:url];
674 [book setDelegate:delegate_];
675 [page setDelegate:delegate_];
678 [book_ pushBook:book];
687 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
688 if ([WebView canShowMIMEType:type])
691 // XXX: handle more mime types!
694 WebView *webview([webview_ webView]);
695 if (frame == [webview mainFrame])
696 [UIApp openURL:[request URL]];
700 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
701 if (request == nil) ignore: {
706 NSURL *url([request URL]);
708 if (url == nil) use: {
709 if (!error_ && [frame parentFrame] == nil) {
711 [request_ autorelease];
712 request_ = [request retain];
714 NSLog(@"dpn:%@", request_);
720 WebView *webview([webview_ webView]);
721 if (frame == [webview mainFrame])
726 else NSLog(@"nav:%@:%@", url, [action description]);
729 const NSArray *capability;
731 #if 0 // XXX:3:GSSystemCopyCapability
732 capability = reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability));
737 if (capability != nil && (
738 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
739 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
746 int store(_not(int));
747 if (NSURL *itms = [url itmsURL:&store]) {
749 NSLog(@"itms#%@#%u#%@", url, store, itms);
752 if (capability != nil && (
753 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
754 store == 2 && [capability containsObject:@"com.apple.AppStore"]
761 NSString *scheme([[url scheme] lowercaseString]);
763 if ([scheme isEqualToString:@"tel"]) {
768 if ([scheme isEqualToString:@"mailto"]) {
769 [self _openMailToURL:url];
773 if ([self getSpecial:url swap:YES])
775 else if ([WebView _canHandleRequest:request])
777 else if ([url isSpringboardHandledURL])
783 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
784 //lprintf("Status:%s\n", [text UTF8String]);
787 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
788 NSString *context([sheet context]);
790 if ([context isEqualToString:@"alert"]) {
793 } else if ([context isEqualToString:@"confirm"]) {
796 confirm_ = [NSNumber numberWithBool:YES];
800 confirm_ = [NSNumber numberWithBool:NO];
805 } else if ([context isEqualToString:@"sensitive"]) {
808 sensitive_ = [NSNumber numberWithBool:YES];
812 sensitive_ = [NSNumber numberWithBool:NO];
817 } else if ([context isEqualToString:@"challenge"]) {
818 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
822 NSString *username([[sheet textFieldAtIndex:0] text]);
823 NSString *password([[sheet textFieldAtIndex:1] text]);
825 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
827 [sender useCredential:credential forAuthenticationChallenge:challenge_];
831 [sender cancelAuthenticationChallenge:challenge_];
838 [challenge_ release];
842 } else if ([context isEqualToString:@"submit"]) {
848 if (request_ != nil) {
850 [webview_ loadRequest:request_];
863 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
864 challenge_ = [challenge retain];
866 NSURLProtectionSpace *space([challenge protectionSpace]);
867 NSString *realm([space realm]);
871 UIActionSheet *sheet = [[[UIActionSheet alloc]
873 buttons:[NSArray arrayWithObjects:UCLocalize("LOGIN"), UCLocalize("CANCEL"), nil]
879 [sheet setNumberOfRows:1];
881 [sheet addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
882 [sheet addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
884 UITextField *username([sheet textFieldAtIndex:0]); {
885 UITextInputTraits *traits([username textInputTraits]);
886 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
887 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
888 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
889 [traits setReturnKeyType:UIReturnKeyNext];
892 UITextField *password([sheet textFieldAtIndex:1]); {
893 UITextInputTraits *traits([password textInputTraits]);
894 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
895 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
896 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
897 // XXX: UIReturnKeyDone
898 [traits setReturnKeyType:UIReturnKeyNext];
899 [traits setSecureTextEntry:YES];
902 [sheet popupAlertAnimated:YES];
905 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
909 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
910 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
912 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
913 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
916 NSNumber *value([features objectForKey:@"width"]);
917 float width(value == nil ? 0 : [value floatValue]);
919 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
921 /* XXX: deal with cydia:// pages */
922 BrowserView *browser([[[class_ alloc] initWithBook:book forWidth:width] autorelease]);
924 if (features != nil && popup_) {
925 [book setDelegate:delegate_];
926 [book setHook:indirect_];
927 [browser setDelegate:delegate_];
929 [browser loadRequest:request];
931 [book setPage:browser];
932 [book_ pushBook:book];
933 } else if (request == nil) {
934 [self setBackButtonTitle:title_];
935 [browser setDelegate:delegate_];
938 [self pushPage:browser];
939 [browser loadRequest:request];
942 return [browser webView];
945 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
946 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
947 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
950 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
951 if ([frame parentFrame] != nil)
954 title_ = [title retain];
955 [book_ reloadTitleForPage:self];
958 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
959 if ([loading_ count] == 0)
961 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
963 if ([frame parentFrame] == nil) {
964 [webview_ resignFirstResponder];
973 if (button_ != nil) {
983 if (function_ != nil) {
988 if (finish_ != nil) {
993 if (closer_ != nil) {
998 if (special_ != nil) {
1003 [book_ reloadTitleForPage:self];
1005 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
1007 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
1008 [scroller_ setZoomScale:1 duration:0];
1009 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
1010 [scroller_ _setZoomScale:1 duration:0];
1011 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
1012 [scroller_ setZoomScale:1 animated:NO];*/
1014 CGRect webrect = [scroller_ bounds];
1015 webrect.size.height = 0;
1016 [webview_ setFrame:webrect];
1019 [self reloadButtons];
1022 - (void) _finishLoading {
1023 size_t count([loading_ count]);
1026 if (reloading_ || count != 0)
1029 [self callFunction:finish_];
1030 [self reloadButtons];
1033 - (bool) isLoading {
1034 return [loading_ count] != 0;
1037 - (void) reloadButtons {
1038 if ([self isLoading])
1039 [indicator_ startAnimation];
1041 [indicator_ stopAnimation];
1042 [super reloadButtons];
1045 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
1046 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
1049 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
1050 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
1053 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
1054 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
1057 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
1059 return [webview_ webView:sender didCommitLoadForFrame:frame];
1062 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
1063 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
1066 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
1067 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1068 [self _finishLoading];
1070 if ([frame parentFrame] == nil) {
1071 if (DOMDocument *document = [frame DOMDocument])
1072 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
1073 for (DOMHTMLBodyElement *body in bodies) {
1074 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
1076 bool colored(false);
1078 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
1079 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
1080 DOMRGBColor *rgb([color getRGBColorValue]);
1082 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
1083 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
1084 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
1085 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
1089 if (red == 0xc7 && green == 0xce && blue == 0xd5)
1090 uic = [UIColor pinStripeColor];
1091 else if (alpha != 0)
1093 colorWithRed:(red / 255)
1101 [scroller_ setBackgroundColor:uic];
1107 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1112 return [webview_ webView:sender didFinishLoadForFrame:frame];
1115 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
1116 if ([frame parentFrame] == nil)
1119 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1120 [self _finishLoading];
1125 if ([frame parentFrame] == nil) {
1126 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
1127 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
1128 [[error localizedDescription] stringByAddingPercentEscapes]
1135 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1136 [self _didFailWithError:error forFrame:frame];
1139 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1140 [self _didFailWithError:error forFrame:frame];
1143 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1144 #if LogBrowser || ForSaurik
1145 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1149 /* XXX: fix this stupid include file
1150 - (void) webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)database {
1151 [origin setQuota:0x500000];
1154 - (void) _setTileDrawingEnabled:(BOOL)enabled {
1155 //[webview_ setTileDrawingEnabled:enabled];
1158 - (void) setViewportWidth:(float)width {
1159 width_ = width ? width != 0 : [[self class] defaultWidth];
1160 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1163 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1164 [self _setTileDrawingEnabled:NO];
1167 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1168 [self _setTileDrawingEnabled:YES];
1169 [webview_ redrawScaledDocument];
1172 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
1173 [self _setTileDrawingEnabled:NO];
1176 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
1177 [self _setTileDrawingEnabled:YES];
1180 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
1181 [self _setTileDrawingEnabled:YES];
1184 - (id) initWithBook:(RVBook *)book forWidth:(float)width ofClass:(Class)_class {
1185 if ((self = [super initWithBook:book]) != nil) {
1187 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
1190 struct CGRect bounds = [self bounds];
1192 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
1193 [self addSubview:scroller_];
1195 [scroller_ setFixedBackgroundPattern:YES];
1196 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1198 [scroller_ setScrollingEnabled:YES];
1199 [scroller_ setClipsSubviews:YES];
1200 [scroller_ setAllowsRubberBanding:YES];
1202 [scroller_ setDelegate:self];
1203 [scroller_ setBounces:YES];
1204 [scroller_ setScrollHysteresis:8];
1205 [scroller_ setThumbDetectionEnabled:NO];
1206 [scroller_ setDirectionalScrolling:YES];
1207 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
1208 [scroller_ setEventMode:YES];
1209 [scroller_ setShowBackgroundShadow:NO]; /* YES */
1210 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
1211 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
1213 CGRect webrect = [scroller_ bounds];
1214 webrect.size.height = 0;
1221 webview_ = [Documents_ lastObject];
1222 if (webview_ != nil) {
1223 webview_ = [webview_ retain];
1224 webview = [webview_ webView];
1225 [Documents_ removeLastObject];
1226 [webview_ setFrame:webrect];
1231 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
1232 webview = [webview_ webView];
1234 // XXX: this is terribly (too?) expensive
1235 //[webview_ setDrawsBackground:NO];
1236 [webview setPreferencesIdentifier:@"Cydia"];
1238 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1240 if ([webview_ respondsToSelector:@selector(enableReachability)])
1241 [webview_ enableReachability];
1243 [webview_ setAllowsMessaging:YES];
1245 if ([webview_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
1246 [webview_ useSelectionAssistantWithMode:0];
1248 [webview_ setTilingEnabled:YES];
1249 [webview_ setDrawsGrid:NO];
1250 [webview_ setLogsTilingChanges:NO];
1251 [webview_ setTileMinificationFilter:kCAFilterNearest];
1253 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
1254 /* XXX: abstractify */
1255 [webview_ setDataDetectorTypes:0x80000000];
1257 [webview_ setDetectsPhoneNumbers:NO];
1259 [webview_ setAutoresizes:YES];
1261 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1262 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1263 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1264 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1266 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1268 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1269 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1270 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1272 [webview_ _setDocumentType:0x4];
1274 if ([webview_ respondsToSelector:@selector(UIWebDocumentView:)])
1275 [webview_ setZoomsFocusedFormControl:YES];
1276 [webview_ setContentsPosition:7];
1277 [webview_ setEnabledGestures:0xa];
1278 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1279 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1281 [webview_ setSmoothsFonts:YES];
1282 [webview_ setAllowsImageSheet:YES];
1283 [webview _setUsesLoaderCache:YES];
1285 [webview setGroupName:@"CydiaGroup"];
1287 WebPreferences *preferences([webview preferences]);
1289 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1290 [webview _setLayoutInterval:0];
1292 [preferences _setLayoutInterval:0];
1295 [self setViewportWidth:width];
1297 [webview_ setDelegate:self];
1298 [webview_ setGestureDelegate:self];
1299 [webview_ setFormEditingDelegate:self];
1300 [webview_ setInteractionDelegate:self];
1302 [scroller_ addSubview:webview_];
1304 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1306 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1308 [webview setFrameLoadDelegate:indirect_];
1309 [webview setPolicyDelegate:indirect_];
1310 [webview setResourceLoadDelegate:indirect_];
1311 [webview setUIDelegate:indirect_];
1313 /* XXX: do not turn this on under penalty of extreme pain */
1314 [webview setScriptDebugDelegate:nil];
1318 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1319 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1320 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1322 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1323 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1325 /*UIWebView *test([[[UIWebView alloc] initWithFrame:[self bounds]] autorelease]);
1326 [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
1327 [self addSubview:test];*/
1331 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
1332 return [self initWithBook:book forWidth:width ofClass:[self class]];
1335 - (id) initWithBook:(RVBook *)book {
1336 return [self initWithBook:book forWidth:0];
1339 - (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
1341 WebView *webview([webview_ webView]);
1342 NSString *string([webview stringByEvaluatingJavaScriptFromString:script]);
1347 - (void) callFunction:(WebScriptObject *)function {
1350 WebView *webview([webview_ webView]);
1351 WebFrame *frame([webview mainFrame]);
1353 id _private(MSHookIvar<id>(webview, "_private"));
1354 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1355 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1358 if (settings == NULL)
1361 no = settings->JavaScriptCanOpenWindowsAutomatically();
1362 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1365 if (UIWindow *window = [self window])
1366 if (UIResponder *responder = [window firstResponder])
1367 [responder resignFirstResponder];
1369 JSObjectRef object([function JSObject]);
1370 JSGlobalContextRef context([frame globalContext]);
1371 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1373 if (settings != NULL)
1374 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1379 - (void) didCloseBook:(RVBook *)book {
1381 [self callFunction:closer_];
1384 - (void) __rightButtonClicked {
1389 - (void) _rightButtonClicked {
1391 if (function_ != nil)
1392 [self callFunction:function_];
1395 [self __rightButtonClicked];
1398 - (id) _rightButtonTitle {
1399 return UCLocalize("RELOAD");
1402 - (id) rightButtonTitle {
1403 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
1406 - (UINavigationButtonStyle) rightButtonStyle {
1407 if (style_ == nil) normal:
1408 return UINavigationButtonStyleNormal;
1409 else if ([style_ isEqualToString:@"Normal"])
1410 return UINavigationButtonStyleNormal;
1411 else if ([style_ isEqualToString:@"Back"])
1412 return UINavigationButtonStyleBack;
1413 else if ([style_ isEqualToString:@"Highlighted"])
1414 return UINavigationButtonStyleHighlighted;
1415 else if ([style_ isEqualToString:@"Destructive"])
1416 return UINavigationButtonStyleDestructive;
1420 - (NSString *) title {
1421 return title_ == nil ? UCLocalize("LOADING") : title_;
1424 - (NSString *) backButtonTitle {
1425 return UCLocalize("BROWSER");
1428 - (void) setPageActive:(BOOL)active {
1430 [indicator_ removeFromSuperview];
1432 [[book_ navigationBar] addSubview:indicator_];
1435 - (void) resetViewAnimated:(BOOL)animated {
1438 - (void) setPushed:(bool)pushed {
1442 + (float) defaultWidth {