1 #include <UIKit/UIKit.h>
2 #include "iPhonePrivate.h"
4 #include "UCPlatform.h"
6 #include <UICaboodle/BrowserView.h>
7 #include <UICaboodle/UCLocalize.h>
9 //#include <QuartzCore/CALayer.h>
10 // XXX: fix the minimum requirement
11 extern NSString * const kCAFilterNearest;
13 #include <WebCore/WebCoreThread.h>
15 #include <WebKit/WebPolicyDelegate.h>
16 #include <WebKit/WebPreferences.h>
18 #include <WebKit/DOMCSSPrimitiveValue.h>
19 #include <WebKit/DOMCSSStyleDeclaration.h>
20 #include <WebKit/DOMDocument.h>
21 #include <WebKit/DOMHTMLBodyElement.h>
22 #include <WebKit/DOMRGBColor.h>
24 //#include <WebCore/Page.h>
25 //#include <WebCore/Settings.h>
27 #include "substrate.h"
30 #define DefaultTimeout_ 120.0
32 template <typename Type_>
33 static inline void CYRelease(Type_ &value) {
40 float CYScrollViewDecelerationRateNormal;
42 @interface WebView (Apple)
43 - (void) _setLayoutInterval:(float)interval;
46 @interface WebPreferences (Apple)
47 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
48 - (void) _setLayoutInterval:(NSInteger)interval;
49 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
52 /* Indirect Delegate {{{ */
53 @interface IndirectDelegate : NSObject <
56 _transient volatile id delegate_;
59 - (void) setDelegate:(id)delegate;
60 - (id) initWithDelegate:(id)delegate;
63 @implementation IndirectDelegate
65 - (void) setDelegate:(id)delegate {
69 - (id) initWithDelegate:(id)delegate {
74 - (void) didDismissModalViewController {
76 return [delegate_ didDismissModalViewController];
79 - (IMP) methodForSelector:(SEL)sel {
80 if (IMP method = [super methodForSelector:sel])
82 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
86 - (BOOL) respondsToSelector:(SEL)sel {
87 if ([super respondsToSelector:sel])
89 // XXX: WebThreadCreateNSInvocation returns nil
90 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
91 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
94 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
95 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
97 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
99 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
101 // XXX: I fucking hate Apple so very very bad
102 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
105 - (void) forwardInvocation:(NSInvocation *)inv {
106 SEL sel = [inv selector];
107 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
108 [inv invokeWithTarget:delegate_];
114 @implementation WebScriptObject (UICaboodle)
116 - (NSUInteger) count {
117 id length([self valueForKey:@"length"]);
118 if ([length respondsToSelector:@selector(intValue)])
119 return [length intValue];
124 - (id) objectAtIndex:(unsigned)index {
125 return [self webScriptValueAtIndex:index];
130 // CYWebPolicyDecision* {{{
131 enum CYWebPolicyDecision {
132 CYWebPolicyDecisionUnknown,
133 CYWebPolicyDecisionDownload,
134 CYWebPolicyDecisionIgnore,
135 CYWebPolicyDecisionUse,
138 @interface CYWebPolicyDecisionMediator : NSObject <
139 WebPolicyDecisionListener
141 id<WebPolicyDecisionListener> listener_;
142 CYWebPolicyDecision decision_;
145 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
147 - (CYWebPolicyDecision) decision;
153 @implementation CYWebPolicyDecisionMediator
155 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
156 if ((self = [super init]) != nil) {
157 listener_ = listener;
161 - (CYWebPolicyDecision) decision {
166 return decision_ != CYWebPolicyDecisionUnknown;
171 case CYWebPolicyDecisionUnknown:
173 NSLog(@"CYWebPolicyDecisionUnknown");
176 case CYWebPolicyDecisionDownload: [listener_ download]; break;
177 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
178 case CYWebPolicyDecisionUse: [listener_ use]; break;
185 decision_ = CYWebPolicyDecisionDownload;
189 decision_ = CYWebPolicyDecisionIgnore;
193 decision_ = CYWebPolicyDecisionUse;
199 @implementation CYWebView : UIWebView
201 - (id) initWithFrame:(CGRect)frame {
202 if ((self = [super initWithFrame:frame]) != nil) {
210 - (id<CYWebViewDelegate>) delegate {
211 return (id<CYWebViewDelegate>) [super delegate];
214 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
215 id<CYWebViewDelegate> delegate([self delegate]);
216 WebView *created(nil);
217 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
218 created = [delegate webView:view createWebViewWithRequest:request];
219 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
220 created = [super webView:view createWebViewWithRequest:request];
224 // webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
225 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
226 id<CYWebViewDelegate> delegate([self delegate]);
227 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
228 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
229 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
230 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
231 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
235 // webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
236 static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
237 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
238 if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
239 [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
242 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
243 id<CYWebViewDelegate> delegate([self delegate]);
244 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
245 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
246 [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
247 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
248 [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
252 // webView:didClearWindowObject:forFrame: (3.2+) {{{
253 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
254 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
255 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
256 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
259 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
260 id<CYWebViewDelegate> delegate([self delegate]);
261 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
262 [delegate webView:view didClearWindowObject:window forFrame:frame];
263 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
264 [super webView:view didClearWindowObject:window forFrame:frame];
267 // webView:didFailLoadWithError:forFrame: (2.0+) {{{
268 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
269 id<CYWebViewDelegate> delegate([self delegate]);
270 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
271 [delegate webView:view didFailLoadWithError:error forFrame:frame];
272 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
273 [super webView:view didFailLoadWithError:error forFrame:frame];
276 // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
277 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
278 id<CYWebViewDelegate> delegate([self delegate]);
279 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
280 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
281 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
282 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
285 // webView:didFinishLoadForFrame: (2.0+) {{{
286 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
287 id<CYWebViewDelegate> delegate([self delegate]);
288 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
289 [delegate webView:view didFinishLoadForFrame:frame];
290 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
291 [super webView:view didFinishLoadForFrame:frame];
294 // webView:didReceiveTitle:forFrame: (3.2+) {{{
295 static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
296 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
297 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
298 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
301 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
302 id<CYWebViewDelegate> delegate([self delegate]);
303 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
304 [delegate webView:view didReceiveTitle:title forFrame:frame];
305 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
306 [super webView:view didReceiveTitle:title forFrame:frame];
309 // webView:didStartProvisionalLoadForFrame: (2.0+) {{{
310 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
311 id<CYWebViewDelegate> delegate([self delegate]);
312 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
313 [delegate webView:view didStartProvisionalLoadForFrame:frame];
314 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
315 [super webView:view didStartProvisionalLoadForFrame:frame];
318 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
319 static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
320 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
321 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
322 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
326 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
327 id<CYWebViewDelegate> delegate([self delegate]);
328 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
329 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
330 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
331 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
335 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
336 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
337 id<CYWebViewDelegate> delegate([self delegate]);
338 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
340 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
341 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
343 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
346 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
347 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
348 id<CYWebViewDelegate> delegate([self delegate]);
349 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
351 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
352 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
354 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
358 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
359 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
360 id<CYWebViewDelegate> delegate([self delegate]);
361 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
363 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
364 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
366 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
370 // webViewClose: (3.2+) {{{
371 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
372 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
373 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
374 [uiWebView webViewClose:view];
377 - (void) webViewClose:(WebView *)view {
378 id<CYWebViewDelegate> delegate([self delegate]);
379 if ([delegate respondsToSelector:@selector(webViewClose:)])
380 [delegate webViewClose:view];
381 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
382 [super webViewClose:view];
386 - (void) _updateViewSettings {
387 [super _updateViewSettings];
389 id<CYWebViewDelegate> delegate([self delegate]);
390 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
391 [delegate webViewUpdateViewSettings:self];
394 + (void) initialize {
395 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
396 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
397 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
398 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
399 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
400 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
406 #define ShowInternals 0
409 #define lprintf(args...) fprintf(stderr, args)
411 @implementation BrowserController
414 #include "UICaboodle/UCInternal.h"
417 + (void) _initialize {
418 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
420 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
421 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
422 else // XXX: this actually might be fast on some older systems: we should look into this
423 CYScrollViewDecelerationRateNormal = 0.998;
428 NSLog(@"[BrowserController dealloc]");
431 [webview_ setDelegate:nil];
433 [indirect_ setDelegate:nil];
436 if (challenge_ != nil)
437 [challenge_ release];
447 if (function_ != nil)
455 if ([loading_ count] != 0)
456 [delegate_ releaseNetworkActivityIndicator];
459 [reloaditem_ release];
460 [loadingitem_ release];
462 [indicator_ release];
467 - (void) setURL:(NSURL *)url {
468 _assert(request_ == nil);
470 request_ = [[NSURLRequest
472 cachePolicy:NSURLRequestUseProtocolCachePolicy
473 timeoutInterval:DefaultTimeout_
477 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
478 [self loadRequest:[NSURLRequest
481 timeoutInterval:DefaultTimeout_
485 - (void) loadURL:(NSURL *)url {
486 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
489 - (void) loadRequest:(NSURLRequest *)request {
491 NSLog(@"loadRequest:%@", request);
497 [webview_ loadRequest:request];
505 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
506 [self loadRequest:request_];
508 UIAlertView *alert = [[[UIAlertView alloc]
509 initWithTitle:UCLocalize("RESUBMIT_FORM")
512 cancelButtonTitle:UCLocalize("CANCEL")
513 otherButtonTitles:UCLocalize("SUBMIT"), nil
516 [alert setContext:@"submit"];
521 - (void) reloadData {
526 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
528 [custom_ autorelease];
532 custom_ = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
535 [style_ autorelease];
539 style_ = [style retain];
541 if (function_ != nil)
542 [function_ autorelease];
546 function_ = [function retain];
548 [self applyRightButton];
551 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
553 [custom_ autorelease];
557 custom_ = [button retain];
560 [style_ autorelease];
564 style_ = [style retain];
566 if (function_ != nil)
567 [function_ autorelease];
571 function_ = [function retain];
573 [self applyRightButton];
576 - (void) setPopupHook:(id)function {
578 [closer_ autorelease];
582 closer_ = [function retain];
585 - (void) _setViewportWidth {
586 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
589 - (void) setViewportWidth:(float)width {
590 width_ = width != 0 ? width : [[self class] defaultWidth];
591 [self _setViewportWidth];
594 - (void) webViewUpdateViewSettings:(UIWebView *)view {
595 [self _setViewportWidth];
598 - (void) _openMailToURL:(NSURL *)url {
599 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
602 - (bool) _allowJavaScriptPanel {
606 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
607 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
608 [self _didFinishLoading];
610 if ([error code] == NSURLErrorCancelled)
613 if ([frame parentFrame] == nil) {
614 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
615 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
616 [[error localizedDescription] stringByAddingPercentEscapes]
623 // CYWebViewDelegate {{{
624 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
626 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
629 if ([frame parentFrame] == nil) {
632 [request_ autorelease];
636 request_ = [request retain];
641 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
643 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
646 NSURL *url([request URL]);
650 if ([frame isEqualToString:@"_open"])
651 [delegate_ openURL:url];
653 NSString *scheme([[url scheme] lowercaseString]);
654 if ([scheme isEqualToString:@"mailto"])
655 [self _openMailToURL:url];
657 CYViewController *page([delegate_ pageForURL:url]);
660 BrowserController *browser([[[class_ alloc] init] autorelease]);
661 [browser loadRequest:request];
665 [page setDelegate:delegate_];
667 if (![frame isEqualToString:@"_popup"]) {
668 [[self navigationItem] setTitle:title_];
670 [[self navigationController] pushViewController:page animated:YES];
672 UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
674 [navigation setHook:indirect_];
675 [navigation setDelegate:delegate_];
677 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
678 initWithTitle:UCLocalize("CLOSE")
679 style:UIBarButtonItemStylePlain
681 action:@selector(close)
684 [[self navigationController] presentModalViewController:navigation animated:YES];
690 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
693 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
695 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
698 [self _didFailWithError:error forFrame:frame];
701 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
703 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
706 [self _didFailWithError:error forFrame:frame];
709 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
710 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
712 if ([frame parentFrame] == nil) {
713 if (DOMDocument *document = [frame DOMDocument])
714 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
715 for (DOMHTMLBodyElement *body in (id) bodies) {
716 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
720 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
721 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
722 DOMRGBColor *rgb([color getRGBColorValue]);
724 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
725 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
726 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
727 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
731 if (red == 0xc7 && green == 0xce && blue == 0xd5)
732 uic = [UIColor groupTableViewBackgroundColor];
735 colorWithRed:(red / 255)
743 [scroller_ setBackgroundColor:uic];
749 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
754 [self _didFinishLoading];
757 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
758 if ([frame parentFrame] != nil)
762 [title_ autorelease];
763 title_ = [title retain];
765 [[self navigationItem] setTitle:title_];
768 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
769 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
771 if ([frame parentFrame] == nil) {
775 CYRelease(function_);
778 // XXX: do we still need to do this?
779 [[self navigationItem] setTitle:nil];
782 [self _didStartLoading];
785 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
787 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
793 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
794 return [self _allowJavaScriptPanel];
797 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
798 return [self _allowJavaScriptPanel];
801 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
802 return [self _allowJavaScriptPanel];
805 - (void) webViewClose:(WebView *)view {
811 [[self navigationController] dismissModalViewControllerAnimated:YES];
814 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
815 NSString *context([alert context]);
817 if ([context isEqualToString:@"sensitive"]) {
820 sensitive_ = [NSNumber numberWithBool:YES];
824 sensitive_ = [NSNumber numberWithBool:NO];
828 [alert dismissWithClickedButtonIndex:-1 animated:YES];
829 } else if ([context isEqualToString:@"challenge"]) {
830 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
834 NSString *username([[alert textFieldAtIndex:0] text]);
835 NSString *password([[alert textFieldAtIndex:1] text]);
837 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
839 [sender useCredential:credential forAuthenticationChallenge:challenge_];
843 [sender cancelAuthenticationChallenge:challenge_];
849 [challenge_ release];
852 [alert dismissWithClickedButtonIndex:-1 animated:YES];
853 } else if ([context isEqualToString:@"submit"]) {
854 if (button == [alert cancelButtonIndex]) {
855 } else if (button == [alert firstOtherButtonIndex]) {
856 if (request_ != nil) {
858 [webview_ loadRequest:request_];
863 [alert dismissWithClickedButtonIndex:-1 animated:YES];
867 - (UIBarButtonItemStyle) rightButtonStyle {
868 if (style_ == nil) normal:
869 return UIBarButtonItemStylePlain;
870 else if ([style_ isEqualToString:@"Normal"])
871 return UIBarButtonItemStylePlain;
872 else if ([style_ isEqualToString:@"Highlighted"])
873 return UIBarButtonItemStyleDone;
877 - (UIBarButtonItem *) customButton {
878 return [[[UIBarButtonItem alloc]
879 initWithTitle:custom_
880 style:[self rightButtonStyle]
882 action:@selector(customButtonClicked)
886 - (UIBarButtonItem *) rightButton {
890 - (void) applyLoadingTitle {
891 [[self navigationItem] setTitle:UCLocalize("LOADING")];
894 - (void) layoutRightButton {
895 [[loadingitem_ view] addSubview:indicator_];
896 [[loadingitem_ view] bringSubviewToFront:indicator_];
899 - (void) applyRightButton {
900 if ([self isLoading]) {
901 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
902 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
904 [indicator_ startAnimating];
905 [self applyLoadingTitle];
907 [indicator_ stopAnimating];
909 [[self navigationItem] setRightBarButtonItem:(
910 custom_ != nil ? [self customButton] : [self rightButton]
915 - (void) didStartLoading {
916 // Overridden in subclasses.
919 - (void) _didStartLoading {
920 [self applyRightButton];
922 if ([loading_ count] != 1)
925 [delegate_ retainNetworkActivityIndicator];
926 [self didStartLoading];
929 - (void) didFinishLoading {
930 // Overridden in subclasses.
933 - (void) _didFinishLoading {
934 if ([loading_ count] != 0)
937 [self applyRightButton];
938 [[self navigationItem] setTitle:title_];
940 [delegate_ releaseNetworkActivityIndicator];
941 [self didFinishLoading];
945 return [loading_ count] != 0;
948 - (id) initWithWidth:(float)width ofClass:(Class)_class {
949 if ((self = [super init]) != nil) {
951 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
953 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
955 webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
956 [webview_ setDelegate:self];
957 [self setView:webview_];
959 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
960 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
962 [webview_ setDetectsPhoneNumbers:NO];
964 [webview_ setScalesPageToFit:YES];
966 UIWebDocumentView *document([webview_ _documentView]);
968 // XXX: I think this improves scrolling; the hardcoded-ness sucks
969 [document setTileSize:CGSizeMake(320, 500)];
971 [document setBackgroundColor:[UIColor clearColor]];
973 // XXX: this is terribly (too?) expensive
974 [document setDrawsBackground:NO];
976 WebView *webview([document webView]);
977 WebPreferences *preferences([webview preferences]);
979 // XXX: I have no clue if I actually /want/ this modification
980 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
981 [webview _setLayoutInterval:0];
982 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
983 [preferences _setLayoutInterval:0];
985 [preferences setCacheModel:WebCacheModelDocumentBrowser];
986 [preferences setOfflineWebApplicationCacheEnabled:YES];
988 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
989 scroller_ = [webview_ _scrollView];
991 [scroller_ setDirectionalLockEnabled:YES];
992 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
993 [scroller_ setDelaysContentTouches:NO];
995 [scroller_ setCanCancelContentTouches:YES];
996 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
997 UIScroller *scroller([webview_ _scroller]);
998 scroller_ = (UIScrollView *) scroller;
1000 [scroller setDirectionalScrolling:YES];
1001 // XXX: we might be better off /not/ setting this on older systems
1002 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
1003 [scroller setScrollHysteresis:0]; /* 8 */
1005 [scroller setThumbDetectionEnabled:NO];
1007 // use NO with UIApplicationUseLegacyEvents(YES)
1008 [scroller setEventMode:YES];
1010 // XXX: this is handled by setBounces, right?
1011 //[scroller setAllowsRubberBanding:YES];
1014 [scroller_ setFixedBackgroundPattern:YES];
1015 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
1016 [scroller_ setClipsSubviews:YES];
1018 [scroller_ setBounces:YES];
1019 [scroller_ setScrollingEnabled:YES];
1020 [scroller_ setShowBackgroundShadow:NO];
1022 [self setViewportWidth:width];
1024 reloaditem_ = [[UIBarButtonItem alloc]
1025 initWithTitle:UCLocalize("RELOAD")
1026 style:[self rightButtonStyle]
1028 action:@selector(reloadButtonClicked)
1031 loadingitem_ = [[UIBarButtonItem alloc]
1033 style:UIBarButtonItemStylePlain
1035 action:@selector(reloadButtonClicked)
1038 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1039 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
1041 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1042 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1046 - (id) initWithWidth:(float)width {
1047 return [self initWithWidth:width ofClass:[self class]];
1051 return [self initWithWidth:0];
1054 - (id) initWithURL:(NSURL *)url {
1055 if ((self = [self init]) != nil) {
1060 - (void) didDismissModalViewController {
1062 [self callFunction:closer_];
1065 - (void) callFunction:(WebScriptObject *)function {
1068 WebView *webview([[webview_ _documentView] webView]);
1069 WebFrame *frame([webview mainFrame]);
1070 WebPreferences *preferences([webview preferences]);
1072 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
1073 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1075 /*id _private(MSHookIvar<id>(webview, "_private"));
1076 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1077 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1080 if (settings == NULL)
1083 no = settings->JavaScriptCanOpenWindowsAutomatically();
1084 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1087 if (UIWindow *window = [[self view] window])
1088 if (UIResponder *responder = [window firstResponder])
1089 [responder resignFirstResponder];
1091 JSObjectRef object([function JSObject]);
1092 JSGlobalContextRef context([frame globalContext]);
1093 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1095 /*if (settings != NULL)
1096 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
1098 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
1103 - (void) reloadButtonClicked {
1107 - (void) _customButtonClicked {
1108 [self reloadButtonClicked];
1111 - (void) customButtonClicked {
1113 if (function_ != nil)
1114 [self callFunction:function_];
1117 [self _customButtonClicked];
1120 + (float) defaultWidth {