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"
31 template <typename Type_>
32 static inline void CYRelease(Type_ &value) {
39 float CYScrollViewDecelerationRateNormal;
41 @interface WebView (Apple)
42 - (void) _setLayoutInterval:(float)interval;
45 @interface WebPreferences (Apple)
46 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
47 - (void) _setLayoutInterval:(NSInteger)interval;
48 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
51 /* Indirect Delegate {{{ */
52 @interface IndirectDelegate : NSObject <
55 _transient volatile id delegate_;
58 - (void) setDelegate:(id)delegate;
59 - (id) initWithDelegate:(id)delegate;
62 @implementation IndirectDelegate
64 - (void) setDelegate:(id)delegate {
68 - (id) initWithDelegate:(id)delegate {
73 - (void) didDismissModalViewController {
75 return [delegate_ didDismissModalViewController];
78 - (IMP) methodForSelector:(SEL)sel {
79 if (IMP method = [super methodForSelector:sel])
81 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
85 - (BOOL) respondsToSelector:(SEL)sel {
86 if ([super respondsToSelector:sel])
88 // XXX: WebThreadCreateNSInvocation returns nil
89 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
90 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
93 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
94 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
96 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
98 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
100 // XXX: I fucking hate Apple so very very bad
101 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
104 - (void) forwardInvocation:(NSInvocation *)inv {
105 SEL sel = [inv selector];
106 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
107 [inv invokeWithTarget:delegate_];
113 @implementation WebScriptObject (UICaboodle)
115 - (NSUInteger) count {
116 id length([self valueForKey:@"length"]);
117 if ([length respondsToSelector:@selector(intValue)])
118 return [length intValue];
123 - (id) objectAtIndex:(unsigned)index {
124 return [self webScriptValueAtIndex:index];
129 // CYWebPolicyDecision* {{{
130 enum CYWebPolicyDecision {
131 CYWebPolicyDecisionUnknown,
132 CYWebPolicyDecisionDownload,
133 CYWebPolicyDecisionIgnore,
134 CYWebPolicyDecisionUse,
137 @interface CYWebPolicyDecisionMediator : NSObject <
138 WebPolicyDecisionListener
140 id<WebPolicyDecisionListener> listener_;
141 CYWebPolicyDecision decision_;
144 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
146 - (CYWebPolicyDecision) decision;
152 @implementation CYWebPolicyDecisionMediator
154 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
155 if ((self = [super init]) != nil) {
156 listener_ = listener;
160 - (CYWebPolicyDecision) decision {
165 return decision_ != CYWebPolicyDecisionUnknown;
170 case CYWebPolicyDecisionUnknown:
172 NSLog(@"CYWebPolicyDecisionUnknown");
175 case CYWebPolicyDecisionDownload: [listener_ download]; break;
176 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
177 case CYWebPolicyDecisionUse: [listener_ use]; break;
184 decision_ = CYWebPolicyDecisionDownload;
188 decision_ = CYWebPolicyDecisionIgnore;
192 decision_ = CYWebPolicyDecisionUse;
198 @implementation CYWebView : UIWebView
200 - (id) initWithFrame:(CGRect)frame {
201 if ((self = [super initWithFrame:frame]) != nil) {
209 - (id<CYWebViewDelegate>) delegate {
210 return (id<CYWebViewDelegate>) [super delegate];
213 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
214 id<CYWebViewDelegate> delegate([self delegate]);
215 WebView *created(nil);
216 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
217 created = [delegate webView:view createWebViewWithRequest:request];
218 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
219 created = [super webView:view createWebViewWithRequest:request];
223 // webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
224 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
225 id<CYWebViewDelegate> delegate([self delegate]);
226 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
227 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
228 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
229 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
230 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
234 // webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
235 static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
236 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
237 if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
238 [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
241 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
242 id<CYWebViewDelegate> delegate([self delegate]);
243 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
244 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
245 [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
246 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
247 [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
251 // webView:didClearWindowObject:forFrame: (3.2+) {{{
252 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
253 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
254 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
255 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
258 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
259 id<CYWebViewDelegate> delegate([self delegate]);
260 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
261 [delegate webView:view didClearWindowObject:window forFrame:frame];
262 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
263 [super webView:view didClearWindowObject:window forFrame:frame];
266 // webView:didFailLoadWithError:forFrame: (2.0+) {{{
267 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
268 id<CYWebViewDelegate> delegate([self delegate]);
269 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
270 [delegate webView:view didFailLoadWithError:error forFrame:frame];
271 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
272 [super webView:view didFailLoadWithError:error forFrame:frame];
275 // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
276 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
277 id<CYWebViewDelegate> delegate([self delegate]);
278 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
279 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
280 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
281 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
284 // webView:didFinishLoadForFrame: (2.0+) {{{
285 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
286 id<CYWebViewDelegate> delegate([self delegate]);
287 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
288 [delegate webView:view didFinishLoadForFrame:frame];
289 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
290 [super webView:view didFinishLoadForFrame:frame];
293 // webView:didReceiveTitle:forFrame: (3.2+) {{{
294 static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
295 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
296 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
297 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
300 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
301 id<CYWebViewDelegate> delegate([self delegate]);
302 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
303 [delegate webView:view didReceiveTitle:title forFrame:frame];
304 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
305 [super webView:view didReceiveTitle:title forFrame:frame];
308 // webView:didStartProvisionalLoadForFrame: (2.0+) {{{
309 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
310 id<CYWebViewDelegate> delegate([self delegate]);
311 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
312 [delegate webView:view didStartProvisionalLoadForFrame:frame];
313 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
314 [super webView:view didStartProvisionalLoadForFrame:frame];
317 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
318 static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
319 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
320 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
321 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
325 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
326 id<CYWebViewDelegate> delegate([self delegate]);
327 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
328 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
329 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
330 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
334 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
335 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
336 id<CYWebViewDelegate> delegate([self delegate]);
337 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
339 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
340 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
342 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
345 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
346 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
347 id<CYWebViewDelegate> delegate([self delegate]);
348 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
350 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
351 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
353 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
357 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
358 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
359 id<CYWebViewDelegate> delegate([self delegate]);
360 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
362 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
363 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
365 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
369 // webViewClose: (3.2+) {{{
370 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
371 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
372 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
373 [uiWebView webViewClose:view];
376 - (void) webViewClose:(WebView *)view {
377 id<CYWebViewDelegate> delegate([self delegate]);
378 if ([delegate respondsToSelector:@selector(webViewClose:)])
379 [delegate webViewClose:view];
380 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
381 [super webViewClose:view];
385 - (void) _updateViewSettings {
386 [super _updateViewSettings];
388 id<CYWebViewDelegate> delegate([self delegate]);
389 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
390 [delegate webViewUpdateViewSettings:self];
393 + (void) initialize {
394 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
395 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
396 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
397 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
398 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
399 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
405 #define ShowInternals 0
408 #define lprintf(args...) fprintf(stderr, args)
410 @implementation BrowserController
413 #include "UICaboodle/UCInternal.h"
416 + (void) _initialize {
417 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
419 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
420 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
421 else // XXX: this actually might be fast on some older systems: we should look into this
422 CYScrollViewDecelerationRateNormal = 0.998;
427 NSLog(@"[BrowserController dealloc]");
430 [webview_ setDelegate:nil];
432 [indirect_ setDelegate:nil];
435 if (challenge_ != nil)
436 [challenge_ release];
446 if (function_ != nil)
454 if ([loading_ count] != 0)
455 [delegate_ releaseNetworkActivityIndicator];
458 [reloaditem_ release];
459 [loadingitem_ release];
461 [indicator_ release];
466 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
467 [self loadRequest:[NSURLRequest
470 timeoutInterval:120.0
474 - (void) loadURL:(NSURL *)url {
475 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
478 - (void) loadRequest:(NSURLRequest *)request {
480 NSLog(@"loadRequest:%@", request);
486 [webview_ loadRequest:request];
494 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
495 [self loadRequest:request_];
497 UIAlertView *alert = [[[UIAlertView alloc]
498 initWithTitle:UCLocalize("RESUBMIT_FORM")
501 cancelButtonTitle:UCLocalize("CANCEL")
502 otherButtonTitles:UCLocalize("SUBMIT"), nil
505 [alert setContext:@"submit"];
510 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
512 [custom_ autorelease];
516 custom_ = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
519 [style_ autorelease];
523 style_ = [style retain];
525 if (function_ != nil)
526 [function_ autorelease];
530 function_ = [function retain];
532 [self applyRightButton];
535 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
537 [custom_ autorelease];
541 custom_ = [button retain];
544 [style_ autorelease];
548 style_ = [style retain];
550 if (function_ != nil)
551 [function_ autorelease];
555 function_ = [function retain];
557 [self applyRightButton];
560 - (void) setPopupHook:(id)function {
562 [closer_ autorelease];
566 closer_ = [function retain];
569 - (void) _setViewportWidth {
570 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
573 - (void) setViewportWidth:(float)width {
574 width_ = width != 0 ? width : [[self class] defaultWidth];
575 [self _setViewportWidth];
578 - (void) webViewUpdateViewSettings:(UIWebView *)view {
579 [self _setViewportWidth];
582 - (void) _openMailToURL:(NSURL *)url {
583 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
586 - (bool) _allowJavaScriptPanel {
590 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
591 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
592 [self _didFinishLoading];
594 if ([error code] == NSURLErrorCancelled)
597 if ([frame parentFrame] == nil) {
598 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
599 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
600 [[error localizedDescription] stringByAddingPercentEscapes]
607 // CYWebViewDelegate {{{
608 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
610 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
613 if ([frame parentFrame] == nil) {
616 [request_ autorelease];
620 request_ = [request retain];
625 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
627 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
630 NSURL *url([request URL]);
634 if ([frame isEqualToString:@"_open"])
635 [delegate_ openURL:url];
637 NSString *scheme([[url scheme] lowercaseString]);
638 if ([scheme isEqualToString:@"mailto"])
639 [self _openMailToURL:url];
641 CYViewController *page([delegate_ pageForURL:url]);
644 BrowserController *browser([[[class_ alloc] init] autorelease]);
645 [browser loadRequest:request];
649 [page setDelegate:delegate_];
651 if (![frame isEqualToString:@"_popup"]) {
652 [[self navigationItem] setTitle:title_];
654 [[self navigationController] pushViewController:page animated:YES];
656 UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
658 [navigation setHook:indirect_];
659 [navigation setDelegate:delegate_];
661 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
662 initWithTitle:UCLocalize("CLOSE")
663 style:UIBarButtonItemStylePlain
665 action:@selector(close)
668 [[self navigationController] presentModalViewController:navigation animated:YES];
674 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
677 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
679 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
682 [self _didFailWithError:error forFrame:frame];
685 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
687 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
690 [self _didFailWithError:error forFrame:frame];
693 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
694 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
696 if ([frame parentFrame] == nil) {
697 if (DOMDocument *document = [frame DOMDocument])
698 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
699 for (DOMHTMLBodyElement *body in (id) bodies) {
700 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
704 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
705 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
706 DOMRGBColor *rgb([color getRGBColorValue]);
708 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
709 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
710 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
711 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
715 if (red == 0xc7 && green == 0xce && blue == 0xd5)
716 uic = [UIColor groupTableViewBackgroundColor];
719 colorWithRed:(red / 255)
727 [scroller_ setBackgroundColor:uic];
733 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
738 [self _didFinishLoading];
741 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
742 if ([frame parentFrame] != nil)
746 [title_ autorelease];
747 title_ = [title retain];
749 [[self navigationItem] setTitle:title_];
752 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
753 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
755 if ([frame parentFrame] == nil) {
759 CYRelease(function_);
762 // XXX: do we still need to do this?
763 [[self navigationItem] setTitle:nil];
766 [self _didStartLoading];
769 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
771 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
777 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
778 return [self _allowJavaScriptPanel];
781 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
782 return [self _allowJavaScriptPanel];
785 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
786 return [self _allowJavaScriptPanel];
789 - (void) webViewClose:(WebView *)view {
795 [[self navigationController] dismissModalViewControllerAnimated:YES];
798 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
799 NSString *context([alert context]);
801 if ([context isEqualToString:@"sensitive"]) {
804 sensitive_ = [NSNumber numberWithBool:YES];
808 sensitive_ = [NSNumber numberWithBool:NO];
812 [alert dismissWithClickedButtonIndex:-1 animated:YES];
813 } else if ([context isEqualToString:@"challenge"]) {
814 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
818 NSString *username([[alert textFieldAtIndex:0] text]);
819 NSString *password([[alert textFieldAtIndex:1] text]);
821 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
823 [sender useCredential:credential forAuthenticationChallenge:challenge_];
827 [sender cancelAuthenticationChallenge:challenge_];
833 [challenge_ release];
836 [alert dismissWithClickedButtonIndex:-1 animated:YES];
837 } else if ([context isEqualToString:@"submit"]) {
838 if (button == [alert cancelButtonIndex]) {
839 } else if (button == [alert firstOtherButtonIndex]) {
840 if (request_ != nil) {
842 [webview_ loadRequest:request_];
847 [alert dismissWithClickedButtonIndex:-1 animated:YES];
851 - (UIBarButtonItemStyle) rightButtonStyle {
852 if (style_ == nil) normal:
853 return UIBarButtonItemStylePlain;
854 else if ([style_ isEqualToString:@"Normal"])
855 return UIBarButtonItemStylePlain;
856 else if ([style_ isEqualToString:@"Highlighted"])
857 return UIBarButtonItemStyleDone;
861 - (UIBarButtonItem *) customButton {
862 return [[[UIBarButtonItem alloc]
863 initWithTitle:custom_
864 style:[self rightButtonStyle]
866 action:@selector(customButtonClicked)
870 - (UIBarButtonItem *) rightButton {
874 - (void) applyLoadingTitle {
875 [[self navigationItem] setTitle:UCLocalize("LOADING")];
878 - (void) layoutRightButton {
879 [[loadingitem_ view] addSubview:indicator_];
880 [[loadingitem_ view] bringSubviewToFront:indicator_];
883 - (void) applyRightButton {
884 if ([self isLoading]) {
885 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
886 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
888 [indicator_ startAnimating];
889 [self applyLoadingTitle];
891 [indicator_ stopAnimating];
893 [[self navigationItem] setRightBarButtonItem:(
894 custom_ != nil ? [self customButton] : [self rightButton]
899 - (void) didStartLoading {
900 // Overridden in subclasses.
903 - (void) _didStartLoading {
904 [self applyRightButton];
906 if ([loading_ count] != 1)
909 [delegate_ retainNetworkActivityIndicator];
910 [self didStartLoading];
913 - (void) didFinishLoading {
914 // Overridden in subclasses.
917 - (void) _didFinishLoading {
918 if ([loading_ count] != 0)
921 [self applyRightButton];
922 [[self navigationItem] setTitle:title_];
924 [delegate_ releaseNetworkActivityIndicator];
925 [self didFinishLoading];
929 return [loading_ count] != 0;
932 - (id) initWithWidth:(float)width ofClass:(Class)_class {
933 if ((self = [super init]) != nil) {
935 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
937 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
939 webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
940 [webview_ setDelegate:self];
941 [self setView:webview_];
943 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
944 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
946 [webview_ setDetectsPhoneNumbers:NO];
948 [webview_ setScalesPageToFit:YES];
950 UIWebDocumentView *document([webview_ _documentView]);
952 // XXX: I think this improves scrolling; the hardcoded-ness sucks
953 [document setTileSize:CGSizeMake(320, 500)];
955 [document setBackgroundColor:[UIColor clearColor]];
957 // XXX: this is terribly (too?) expensive
958 [document setDrawsBackground:NO];
960 WebView *webview([document webView]);
961 WebPreferences *preferences([webview preferences]);
963 // XXX: I have no clue if I actually /want/ this modification
964 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
965 [webview _setLayoutInterval:0];
966 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
967 [preferences _setLayoutInterval:0];
969 [preferences setCacheModel:WebCacheModelDocumentBrowser];
970 [preferences setOfflineWebApplicationCacheEnabled:YES];
972 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
973 scroller_ = [webview_ _scrollView];
975 [scroller_ setDirectionalLockEnabled:YES];
976 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
977 [scroller_ setDelaysContentTouches:NO];
979 [scroller_ setCanCancelContentTouches:YES];
980 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
981 UIScroller *scroller([webview_ _scroller]);
982 scroller_ = (UIScrollView *) scroller;
984 [scroller setDirectionalScrolling:YES];
985 // XXX: we might be better off /not/ setting this on older systems
986 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
987 [scroller setScrollHysteresis:0]; /* 8 */
989 [scroller setThumbDetectionEnabled:NO];
991 // use NO with UIApplicationUseLegacyEvents(YES)
992 [scroller setEventMode:YES];
994 // XXX: this is handled by setBounces, right?
995 //[scroller setAllowsRubberBanding:YES];
998 [scroller_ setFixedBackgroundPattern:YES];
999 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
1000 [scroller_ setClipsSubviews:YES];
1002 [scroller_ setBounces:YES];
1003 [scroller_ setScrollingEnabled:YES];
1004 [scroller_ setShowBackgroundShadow:NO];
1006 [self setViewportWidth:width];
1008 reloaditem_ = [[UIBarButtonItem alloc]
1009 initWithTitle:UCLocalize("RELOAD")
1010 style:[self rightButtonStyle]
1012 action:@selector(reloadButtonClicked)
1015 loadingitem_ = [[UIBarButtonItem alloc]
1017 style:UIBarButtonItemStylePlain
1019 action:@selector(reloadButtonClicked)
1022 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1023 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
1025 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1026 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1030 - (id) initWithWidth:(float)width {
1031 return [self initWithWidth:width ofClass:[self class]];
1035 return [self initWithWidth:0];
1038 - (void) didDismissModalViewController {
1040 [self callFunction:closer_];
1043 - (void) callFunction:(WebScriptObject *)function {
1046 WebView *webview([[webview_ _documentView] webView]);
1047 WebFrame *frame([webview mainFrame]);
1048 WebPreferences *preferences([webview preferences]);
1050 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
1051 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1053 /*id _private(MSHookIvar<id>(webview, "_private"));
1054 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1055 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1058 if (settings == NULL)
1061 no = settings->JavaScriptCanOpenWindowsAutomatically();
1062 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1065 if (UIWindow *window = [[self view] window])
1066 if (UIResponder *responder = [window firstResponder])
1067 [responder resignFirstResponder];
1069 JSObjectRef object([function JSObject]);
1070 JSGlobalContextRef context([frame globalContext]);
1071 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1073 /*if (settings != NULL)
1074 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
1076 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
1081 - (void) reloadButtonClicked {
1085 - (void) _customButtonClicked {
1086 [self reloadButtonClicked];
1089 - (void) customButtonClicked {
1091 if (function_ != nil)
1092 [self callFunction:function_];
1095 [self _customButtonClicked];
1098 + (float) defaultWidth {