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:)])
338 // XXX: check delegate
339 if ([delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame])
340 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
343 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
344 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
345 id<CYWebViewDelegate> delegate([self delegate]);
346 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
347 // XXX: check delegate
348 if ([delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame])
349 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
353 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
354 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
355 id<CYWebViewDelegate> delegate([self delegate]);
356 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
357 // XXX: check delegate
358 if ([delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame])
359 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
363 // webViewClose: (3.2+) {{{
364 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
365 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
366 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
367 [uiWebView webViewClose:view];
370 - (void) webViewClose:(WebView *)view {
371 id<CYWebViewDelegate> delegate([self delegate]);
372 if ([delegate respondsToSelector:@selector(webViewClose:)])
373 [delegate webViewClose:view];
374 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
375 [super webViewClose:view];
379 - (void) _updateViewSettings {
380 [super _updateViewSettings];
382 id<CYWebViewDelegate> delegate([self delegate]);
383 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
384 [delegate webViewUpdateViewSettings:self];
387 + (void) initialize {
388 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
389 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
390 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
391 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
392 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
393 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
399 #define ShowInternals 0
402 #define lprintf(args...) fprintf(stderr, args)
404 @implementation BrowserController
407 #include "UICaboodle/UCInternal.h"
410 + (void) _initialize {
411 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
413 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
414 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
415 else // XXX: this actually might be fast on some older systems: we should look into this
416 CYScrollViewDecelerationRateNormal = 0.998;
421 NSLog(@"[BrowserController dealloc]");
424 [webview_ setDelegate:nil];
426 [indirect_ setDelegate:nil];
429 if (challenge_ != nil)
430 [challenge_ release];
435 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
442 if (function_ != nil)
452 [reloaditem_ release];
453 [loadingitem_ release];
455 [indicator_ release];
460 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
461 [self loadRequest:[NSURLRequest
464 timeoutInterval:120.0
468 - (void) loadURL:(NSURL *)url {
469 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
472 - (void) loadRequest:(NSURLRequest *)request {
474 NSLog(@"loadRequest:%@", request);
480 [webview_ loadRequest:request];
488 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
489 [self loadRequest:request_];
491 UIAlertView *alert = [[[UIAlertView alloc]
492 initWithTitle:UCLocalize("RESUBMIT_FORM")
495 cancelButtonTitle:UCLocalize("CANCEL")
496 otherButtonTitles:UCLocalize("SUBMIT"), nil
499 [alert setContext:@"submit"];
504 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
506 [custom_ autorelease];
510 custom_ = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
513 [style_ autorelease];
517 style_ = [style retain];
519 if (function_ != nil)
520 [function_ autorelease];
524 function_ = [function retain];
526 [self applyRightButton];
529 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
531 [custom_ autorelease];
535 custom_ = [button retain];
538 [style_ autorelease];
542 style_ = [style retain];
544 if (function_ != nil)
545 [function_ autorelease];
549 function_ = [function retain];
551 [self applyRightButton];
554 - (void) setPopupHook:(id)function {
556 [closer_ autorelease];
560 closer_ = [function retain];
563 - (void) _setViewportWidth {
564 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
567 - (void) setViewportWidth:(float)width {
568 width_ = width != 0 ? width : [[self class] defaultWidth];
569 [self _setViewportWidth];
572 - (void) webViewUpdateViewSettings:(UIWebView *)view {
573 [self _setViewportWidth];
576 - (void) _openMailToURL:(NSURL *)url {
577 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
580 - (bool) _allowJavaScriptPanel {
584 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
585 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
586 [self _didFinishLoading];
588 if ([error code] == NSURLErrorCancelled)
591 if ([frame parentFrame] == nil) {
592 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
593 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
594 [[error localizedDescription] stringByAddingPercentEscapes]
601 // CYWebViewDelegate {{{
602 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
604 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
607 if ([frame parentFrame] == nil) {
610 [request_ autorelease];
614 request_ = [request retain];
619 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
621 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
624 NSURL *url([request URL]);
628 if ([frame isEqualToString:@"_open"])
629 [delegate_ openURL:url];
631 NSString *scheme([[url scheme] lowercaseString]);
632 if ([scheme isEqualToString:@"mailto"])
633 [self _openMailToURL:url];
635 CYViewController *page([delegate_ pageForURL:url hasTag:NULL]);
638 BrowserController *browser([[[class_ alloc] init] autorelease]);
639 [browser loadRequest:request];
643 [page setDelegate:delegate_];
645 if (![frame isEqualToString:@"_popup"]) {
646 [[self navigationItem] setTitle:title_];
648 [[self navigationController] pushViewController:page animated:YES];
650 UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]);
652 [navigation setHook:indirect_];
653 [navigation setDelegate:delegate_];
655 [navigation setViewControllers:[NSArray arrayWithObject:page]];
657 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
658 initWithTitle:UCLocalize("CLOSE")
659 style:UIBarButtonItemStylePlain
661 action:@selector(close)
664 [[self navigationController] presentModalViewController:navigation animated:YES];
670 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
673 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
675 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
678 [self _didFailWithError:error forFrame:frame];
681 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
683 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
686 [self _didFailWithError:error forFrame:frame];
689 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
690 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
692 if ([frame parentFrame] == nil) {
693 if (DOMDocument *document = [frame DOMDocument])
694 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
695 for (DOMHTMLBodyElement *body in (id) bodies) {
696 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
700 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
701 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
702 DOMRGBColor *rgb([color getRGBColorValue]);
704 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
705 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
706 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
707 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
711 if (red == 0xc7 && green == 0xce && blue == 0xd5)
712 uic = [UIColor groupTableViewBackgroundColor];
715 colorWithRed:(red / 255)
723 [scroller_ setBackgroundColor:uic];
729 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
734 [self _didFinishLoading];
737 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
738 if ([frame parentFrame] != nil)
742 [title_ autorelease];
743 title_ = [title retain];
745 [[self navigationItem] setTitle:title_];
748 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
749 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
751 if ([frame parentFrame] == nil) {
755 CYRelease(function_);
758 // XXX: do we still need to do this?
759 [[self navigationItem] setTitle:nil];
762 [self _didStartLoading];
765 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
767 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
773 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
774 return [self _allowJavaScriptPanel];
777 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
778 return [self _allowJavaScriptPanel];
781 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
782 return [self _allowJavaScriptPanel];
785 - (void) webViewClose:(WebView *)view {
791 [[self navigationController] dismissModalViewControllerAnimated:YES];
794 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
795 NSString *context([alert context]);
797 if ([context isEqualToString:@"sensitive"]) {
800 sensitive_ = [NSNumber numberWithBool:YES];
804 sensitive_ = [NSNumber numberWithBool:NO];
808 [alert dismissWithClickedButtonIndex:-1 animated:YES];
809 } else if ([context isEqualToString:@"challenge"]) {
810 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
814 NSString *username([[alert textFieldAtIndex:0] text]);
815 NSString *password([[alert textFieldAtIndex:1] text]);
817 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
819 [sender useCredential:credential forAuthenticationChallenge:challenge_];
823 [sender cancelAuthenticationChallenge:challenge_];
829 [challenge_ release];
832 [alert dismissWithClickedButtonIndex:-1 animated:YES];
833 } else if ([context isEqualToString:@"submit"]) {
839 if (request_ != nil) {
841 [webview_ loadRequest:request_];
849 [alert dismissWithClickedButtonIndex:-1 animated:YES];
853 - (UIBarButtonItemStyle) rightButtonStyle {
854 if (style_ == nil) normal:
855 return UIBarButtonItemStylePlain;
856 else if ([style_ isEqualToString:@"Normal"])
857 return UIBarButtonItemStylePlain;
858 else if ([style_ isEqualToString:@"Highlighted"])
859 return UIBarButtonItemStyleDone;
863 - (UIBarButtonItem *) customButton {
864 return [[[UIBarButtonItem alloc]
865 initWithTitle:custom_
866 style:[self rightButtonStyle]
868 action:@selector(customButtonClicked)
872 - (UIBarButtonItem *) rightButton {
876 - (void) applyLoadingTitle {
877 [[self navigationItem] setTitle:UCLocalize("LOADING")];
880 - (void) applyRightButton {
881 if ([self isLoading]) {
882 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
883 // XXX: why do we do this again here? (if we don't, just remove indicator_)
884 [[loadingitem_ view] addSubview:indicator_];
885 [self applyLoadingTitle];
886 } else if (custom_ != nil) {
887 [[self navigationItem] setRightBarButtonItem:[self customButton] animated:YES];
889 [[self navigationItem] setRightBarButtonItem:[self rightButton] animated:YES];
893 - (void) _didStartLoading {
894 [self applyRightButton];
897 - (void) _didFinishLoading {
898 if ([loading_ count] != 0)
901 [self applyRightButton];
904 if (![self isLoading])
905 [[self navigationItem] setTitle:title_];
909 return [loading_ count] != 0;
912 - (id) initWithWidth:(float)width ofClass:(Class)_class {
913 if ((self = [super init]) != nil) {
915 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
917 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
919 webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
920 [webview_ setDelegate:self];
921 [self setView:webview_];
923 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
924 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
926 [webview_ setDetectsPhoneNumbers:NO];
928 [webview_ setScalesPageToFit:YES];
930 UIWebDocumentView *document([webview_ _documentView]);
932 // XXX: I think this improves scrolling; the hardcoded-ness sucks
933 [document setTileSize:CGSizeMake(320, 500)];
935 [document setBackgroundColor:[UIColor clearColor]];
936 [document setDrawsBackground:NO];
938 WebView *webview([document webView]);
939 WebPreferences *preferences([webview preferences]);
941 // XXX: I have no clue if I actually /want/ this modification
942 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
943 [webview _setLayoutInterval:0];
944 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
945 [preferences _setLayoutInterval:0];
947 [preferences setCacheModel:WebCacheModelDocumentBrowser];
948 [preferences setOfflineWebApplicationCacheEnabled:YES];
950 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
951 scroller_ = [webview_ _scrollView];
953 [scroller_ setDirectionalLockEnabled:YES];
954 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
955 [scroller_ setDelaysContentTouches:NO];
957 [scroller_ setCanCancelContentTouches:YES];
958 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
959 UIScroller *scroller([webview_ _scroller]);
960 scroller_ = (UIScrollView *) scroller;
962 [scroller setDirectionalScrolling:YES];
963 // XXX: we might be better off /not/ setting this on older systems
964 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
965 [scroller setScrollHysteresis:0]; /* 8 */
967 [scroller setThumbDetectionEnabled:NO];
969 // use NO with UIApplicationUseLegacyEvents(YES)
970 [scroller setEventMode:YES];
972 // XXX: this is handled by setBounces, right?
973 //[scroller setAllowsRubberBanding:YES];
976 [scroller_ setFixedBackgroundPattern:YES];
977 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
978 [scroller_ setClipsSubviews:YES];
980 [scroller_ setBounces:YES];
981 [scroller_ setScrollingEnabled:YES];
982 [scroller_ setShowBackgroundShadow:NO];
984 [self setViewportWidth:width];
986 reloaditem_ = [[UIBarButtonItem alloc]
987 initWithTitle:UCLocalize("RELOAD")
988 style:[self rightButtonStyle]
990 action:@selector(reloadButtonClicked)
993 loadingitem_ = [[UIBarButtonItem alloc]
995 style:UIBarButtonItemStylePlain
997 action:@selector(reloadButtonClicked)
1000 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1001 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(15, 5, indsize.width, indsize.height)];
1002 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1003 [indicator_ startAnimation];
1004 [[loadingitem_ view] addSubview:indicator_];
1006 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1007 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1011 - (id) initWithWidth:(float)width {
1012 return [self initWithWidth:width ofClass:[self class]];
1016 return [self initWithWidth:0];
1019 - (void) didDismissModalViewController {
1021 [self callFunction:closer_];
1024 - (void) callFunction:(WebScriptObject *)function {
1027 WebView *webview([[webview_ _documentView] webView]);
1028 WebFrame *frame([webview mainFrame]);
1029 WebPreferences *preferences([webview preferences]);
1031 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
1032 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1034 /*id _private(MSHookIvar<id>(webview, "_private"));
1035 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1036 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1039 if (settings == NULL)
1042 no = settings->JavaScriptCanOpenWindowsAutomatically();
1043 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1046 if (UIWindow *window = [[self view] window])
1047 if (UIResponder *responder = [window firstResponder])
1048 [responder resignFirstResponder];
1050 JSObjectRef object([function JSObject]);
1051 JSGlobalContextRef context([frame globalContext]);
1052 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1054 /*if (settings != NULL)
1055 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
1057 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
1062 - (void) reloadButtonClicked {
1066 - (void) _customButtonClicked {
1067 [self reloadButtonClicked];
1070 - (void) customButtonClicked {
1072 if (function_ != nil)
1073 [self callFunction:function_];
1076 [self _customButtonClicked];
1079 + (float) defaultWidth {