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 #define ShowInternals 0
36 #define lprintf(args...) fprintf(stderr, args)
38 // WebThreadLocked {{{
39 struct WebThreadLocked {
40 _finline WebThreadLocked() {
44 _finline ~WebThreadLocked() {
50 template <typename Type_>
51 static inline void CYRelease(Type_ &value) {
58 float CYScrollViewDecelerationRateNormal;
60 @interface WebView (Apple)
61 - (void) _setLayoutInterval:(float)interval;
62 - (void) _setAllowsMessaging:(BOOL)allows;
65 @interface WebPreferences (Apple)
66 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
67 - (void) _setLayoutInterval:(NSInteger)interval;
68 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
71 /* Indirect Delegate {{{ */
72 @interface IndirectDelegate : NSObject <
75 _transient volatile id delegate_;
78 - (void) setDelegate:(id)delegate;
79 - (id) initWithDelegate:(id)delegate;
82 @implementation IndirectDelegate
84 - (void) setDelegate:(id)delegate {
88 - (id) initWithDelegate:(id)delegate {
93 - (void) didDismissModalViewController {
95 return [delegate_ didDismissModalViewController];
98 - (IMP) methodForSelector:(SEL)sel {
99 if (IMP method = [super methodForSelector:sel])
101 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
105 - (BOOL) respondsToSelector:(SEL)sel {
106 if ([super respondsToSelector:sel])
109 // XXX: WebThreadCreateNSInvocation returns nil
112 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
115 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
118 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
119 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
123 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
126 if (delegate_ != nil)
127 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
130 // XXX: I fucking hate Apple so very very bad
131 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
134 - (void) forwardInvocation:(NSInvocation *)inv {
135 SEL sel = [inv selector];
136 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
137 [inv invokeWithTarget:delegate_];
143 @implementation WebScriptObject (UICaboodle)
145 - (NSUInteger) count {
146 id length([self valueForKey:@"length"]);
147 if ([length respondsToSelector:@selector(intValue)])
148 return [length intValue];
153 - (id) objectAtIndex:(unsigned)index {
154 return [self webScriptValueAtIndex:index];
159 // CYWebPolicyDecision* {{{
160 enum CYWebPolicyDecision {
161 CYWebPolicyDecisionUnknown,
162 CYWebPolicyDecisionDownload,
163 CYWebPolicyDecisionIgnore,
164 CYWebPolicyDecisionUse,
167 @interface CYWebPolicyDecisionMediator : NSObject <
168 WebPolicyDecisionListener
170 id<WebPolicyDecisionListener> listener_;
171 CYWebPolicyDecision decision_;
174 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
176 - (CYWebPolicyDecision) decision;
182 @implementation CYWebPolicyDecisionMediator
184 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
185 if ((self = [super init]) != nil) {
186 listener_ = listener;
190 - (CYWebPolicyDecision) decision {
195 return decision_ != CYWebPolicyDecisionUnknown;
200 case CYWebPolicyDecisionUnknown:
202 NSLog(@"CYWebPolicyDecisionUnknown");
205 case CYWebPolicyDecisionDownload: [listener_ download]; break;
206 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
207 case CYWebPolicyDecisionUse: [listener_ use]; break;
214 decision_ = CYWebPolicyDecisionDownload;
218 decision_ = CYWebPolicyDecisionIgnore;
222 decision_ = CYWebPolicyDecisionUse;
228 @implementation CYWebView : UIWebView
231 #include "UICaboodle/UCInternal.h"
234 - (id) initWithFrame:(CGRect)frame {
235 if ((self = [super initWithFrame:frame]) != nil) {
243 - (id<CYWebViewDelegate>) delegate {
244 return (id<CYWebViewDelegate>) [super delegate];
247 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
248 id<CYWebViewDelegate> delegate([self delegate]);
249 WebView *created(nil);
250 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
251 created = [delegate webView:view createWebViewWithRequest:request];
252 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
253 created = [super webView:view createWebViewWithRequest:request];
257 // webView:addMessageToConsole: (X.Xx) {{{
258 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
259 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
260 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
261 [uiWebView webView:view addMessageToConsole:message];
264 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
265 id<CYWebViewDelegate> delegate([self delegate]);
266 if ([delegate respondsToSelector:@selector(webView:addMessageToConsole:)])
267 [delegate webView:view addMessageToConsole:message];
268 if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
269 [super webView:view addMessageToConsole:message];
272 // webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
273 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
274 id<CYWebViewDelegate> delegate([self delegate]);
275 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
276 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
277 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
278 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
279 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
283 // webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
284 static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
285 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
286 if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
287 [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
290 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
291 id<CYWebViewDelegate> delegate([self delegate]);
292 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
293 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
294 [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
295 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
296 [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
300 // webView:didClearWindowObject:forFrame: (3.2+) {{{
301 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
302 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
303 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
304 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
307 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
308 id<CYWebViewDelegate> delegate([self delegate]);
309 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
310 [delegate webView:view didClearWindowObject:window forFrame:frame];
311 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
312 [super webView:view didClearWindowObject:window forFrame:frame];
315 // webView:didFailLoadWithError:forFrame: (2.0+) {{{
316 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
317 id<CYWebViewDelegate> delegate([self delegate]);
318 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
319 [delegate webView:view didFailLoadWithError:error forFrame:frame];
320 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
321 [super webView:view didFailLoadWithError:error forFrame:frame];
324 // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
325 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
326 id<CYWebViewDelegate> delegate([self delegate]);
327 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
328 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
329 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
330 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
333 // webView:didFinishLoadForFrame: (2.0+) {{{
334 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
335 id<CYWebViewDelegate> delegate([self delegate]);
336 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
337 [delegate webView:view didFinishLoadForFrame:frame];
338 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
339 [super webView:view didFinishLoadForFrame:frame];
342 // webView:didReceiveTitle:forFrame: (3.2+) {{{
343 static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
344 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
345 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
346 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
349 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
350 id<CYWebViewDelegate> delegate([self delegate]);
351 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
352 [delegate webView:view didReceiveTitle:title forFrame:frame];
353 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
354 [super webView:view didReceiveTitle:title forFrame:frame];
357 // webView:didStartProvisionalLoadForFrame: (2.0+) {{{
358 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
359 id<CYWebViewDelegate> delegate([self delegate]);
360 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
361 [delegate webView:view didStartProvisionalLoadForFrame:frame];
362 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
363 [super webView:view didStartProvisionalLoadForFrame:frame];
366 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
367 static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
368 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
369 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
370 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
374 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
375 id<CYWebViewDelegate> delegate([self delegate]);
376 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
377 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
378 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
379 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
383 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
384 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
385 id<CYWebViewDelegate> delegate([self delegate]);
386 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
388 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
389 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
391 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
394 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
395 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
396 id<CYWebViewDelegate> delegate([self delegate]);
397 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
399 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
400 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
402 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
406 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
407 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
408 id<CYWebViewDelegate> delegate([self delegate]);
409 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
411 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
412 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
414 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
418 // webViewClose: (3.2+) {{{
419 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
420 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
421 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
422 [uiWebView webViewClose:view];
425 - (void) webViewClose:(WebView *)view {
426 id<CYWebViewDelegate> delegate([self delegate]);
427 if ([delegate respondsToSelector:@selector(webViewClose:)])
428 [delegate webViewClose:view];
429 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
430 [super webViewClose:view];
434 - (void) _updateViewSettings {
435 [super _updateViewSettings];
437 id<CYWebViewDelegate> delegate([self delegate]);
438 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
439 [delegate webViewUpdateViewSettings:self];
442 + (void) initialize {
443 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
444 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
445 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
446 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
447 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
448 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
449 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
455 @implementation BrowserController
458 #include "UICaboodle/UCInternal.h"
461 + (void) _initialize {
462 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
464 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
465 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
466 else // XXX: this actually might be fast on some older systems: we should look into this
467 CYScrollViewDecelerationRateNormal = 0.998;
472 NSLog(@"[BrowserController dealloc]");
475 [webview_ setDelegate:nil];
477 [indirect_ setDelegate:nil];
480 if (challenge_ != nil)
481 [challenge_ release];
491 if (function_ != nil)
499 if ([loading_ count] != 0)
500 [delegate_ releaseNetworkActivityIndicator];
503 [reloaditem_ release];
504 [loadingitem_ release];
506 [indicator_ release];
511 - (void) setURL:(NSURL *)url {
512 _assert(request_ == nil);
514 request_ = [[NSURLRequest
516 cachePolicy:NSURLRequestUseProtocolCachePolicy
517 timeoutInterval:DefaultTimeout_
521 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
522 [self loadRequest:[NSURLRequest
525 timeoutInterval:DefaultTimeout_
529 - (void) loadURL:(NSURL *)url {
530 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
533 - (void) loadRequest:(NSURLRequest *)request {
535 NSLog(@"loadRequest:%@", request);
540 WebThreadLocked lock;
541 [webview_ loadRequest:request];
548 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
549 [self loadRequest:request_];
551 UIAlertView *alert = [[[UIAlertView alloc]
552 initWithTitle:UCLocalize("RESUBMIT_FORM")
555 cancelButtonTitle:UCLocalize("CANCEL")
557 UCLocalize("SUBMIT"),
561 [alert setContext:@"submit"];
566 - (void) reloadData {
571 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
573 [custom_ autorelease];
577 custom_ = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
580 [style_ autorelease];
584 style_ = [style retain];
586 if (function_ != nil)
587 [function_ autorelease];
591 function_ = [function retain];
593 [self applyRightButton];
596 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
598 [custom_ autorelease];
602 custom_ = [button retain];
605 [style_ autorelease];
609 style_ = [style retain];
611 if (function_ != nil)
612 [function_ autorelease];
616 function_ = [function retain];
618 [self applyRightButton];
621 - (void) setPopupHook:(id)function {
623 [closer_ autorelease];
627 closer_ = [function retain];
630 - (void) _setViewportWidth {
631 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
634 - (void) setViewportWidth:(float)width {
635 width_ = width != 0 ? width : [[self class] defaultWidth];
636 [self _setViewportWidth];
639 - (void) webViewUpdateViewSettings:(UIWebView *)view {
640 [self _setViewportWidth];
643 - (void) _openMailToURL:(NSURL *)url {
644 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
647 - (bool) _allowJavaScriptPanel {
651 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
652 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
653 [self _didFinishLoading];
655 if ([error code] == NSURLErrorCancelled)
658 if ([frame parentFrame] == nil) {
659 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
660 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
661 [[error localizedDescription] stringByAddingPercentEscapes]
668 // CYWebViewDelegate {{{
669 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
671 NSLog(@"addMessageToConsole:%@", message);
675 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
677 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
680 if ([frame parentFrame] == nil) {
683 [request_ autorelease];
687 request_ = [request retain];
692 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
694 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
697 NSURL *url([request URL]);
701 if ([frame isEqualToString:@"_open"])
702 [delegate_ openURL:url];
704 NSString *scheme([[url scheme] lowercaseString]);
705 if ([scheme isEqualToString:@"mailto"])
706 [self _openMailToURL:url];
708 CYViewController *page([delegate_ pageForURL:url]);
711 BrowserController *browser([[[class_ alloc] init] autorelease]);
712 [browser loadRequest:request];
716 [page setDelegate:delegate_];
718 if (![frame isEqualToString:@"_popup"]) {
719 [[self navigationItem] setTitle:title_];
721 [[self navigationController] pushViewController:page animated:YES];
723 UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
725 [navigation setHook:indirect_];
726 [navigation setDelegate:delegate_];
728 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
729 initWithTitle:UCLocalize("CLOSE")
730 style:UIBarButtonItemStylePlain
732 action:@selector(close)
735 [[self navigationController] presentModalViewController:navigation animated:YES];
741 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
744 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
746 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
749 [self _didFailWithError:error forFrame:frame];
752 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
754 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
757 [self _didFailWithError:error forFrame:frame];
760 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
761 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
763 if ([frame parentFrame] == nil) {
764 if (DOMDocument *document = [frame DOMDocument])
765 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
766 for (DOMHTMLBodyElement *body in (id) bodies) {
767 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
771 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
772 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
773 DOMRGBColor *rgb([color getRGBColorValue]);
775 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
776 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
777 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
778 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
782 if (red == 0xc7 && green == 0xce && blue == 0xd5)
783 uic = [UIColor groupTableViewBackgroundColor];
786 colorWithRed:(red / 255)
794 [scroller_ setBackgroundColor:uic];
800 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
805 [self _didFinishLoading];
808 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
809 if ([frame parentFrame] != nil)
813 [title_ autorelease];
814 title_ = [title retain];
816 [[self navigationItem] setTitle:title_];
819 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
820 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
822 if ([frame parentFrame] == nil) {
826 CYRelease(function_);
829 // XXX: do we still need to do this?
830 [[self navigationItem] setTitle:nil];
833 [self _didStartLoading];
836 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
838 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
844 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
845 return [self _allowJavaScriptPanel];
848 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
849 return [self _allowJavaScriptPanel];
852 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
853 return [self _allowJavaScriptPanel];
856 - (void) webViewClose:(WebView *)view {
862 [[self navigationController] dismissModalViewControllerAnimated:YES];
865 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
866 NSString *context([alert context]);
868 if ([context isEqualToString:@"sensitive"]) {
871 sensitive_ = [NSNumber numberWithBool:YES];
875 sensitive_ = [NSNumber numberWithBool:NO];
879 [alert dismissWithClickedButtonIndex:-1 animated:YES];
880 } else if ([context isEqualToString:@"challenge"]) {
881 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
885 NSString *username([[alert textFieldAtIndex:0] text]);
886 NSString *password([[alert textFieldAtIndex:1] text]);
888 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
890 [sender useCredential:credential forAuthenticationChallenge:challenge_];
894 [sender cancelAuthenticationChallenge:challenge_];
900 [challenge_ release];
903 [alert dismissWithClickedButtonIndex:-1 animated:YES];
904 } else if ([context isEqualToString:@"submit"]) {
905 if (button == [alert cancelButtonIndex]) {
906 } else if (button == [alert firstOtherButtonIndex]) {
907 if (request_ != nil) {
908 WebThreadLocked lock;
909 [webview_ loadRequest:request_];
913 [alert dismissWithClickedButtonIndex:-1 animated:YES];
917 - (UIBarButtonItemStyle) rightButtonStyle {
918 if (style_ == nil) normal:
919 return UIBarButtonItemStylePlain;
920 else if ([style_ isEqualToString:@"Normal"])
921 return UIBarButtonItemStylePlain;
922 else if ([style_ isEqualToString:@"Highlighted"])
923 return UIBarButtonItemStyleDone;
927 - (UIBarButtonItem *) customButton {
928 return [[[UIBarButtonItem alloc]
929 initWithTitle:custom_
930 style:[self rightButtonStyle]
932 action:@selector(customButtonClicked)
936 - (UIBarButtonItem *) rightButton {
940 - (void) applyLoadingTitle {
941 [[self navigationItem] setTitle:UCLocalize("LOADING")];
944 - (void) layoutRightButton {
945 [[loadingitem_ view] addSubview:indicator_];
946 [[loadingitem_ view] bringSubviewToFront:indicator_];
949 - (void) applyRightButton {
950 if ([self isLoading]) {
951 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
952 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
954 [indicator_ startAnimating];
955 [self applyLoadingTitle];
957 [indicator_ stopAnimating];
959 [[self navigationItem] setRightBarButtonItem:(
960 custom_ != nil ? [self customButton] : [self rightButton]
965 - (void) didStartLoading {
966 // Overridden in subclasses.
969 - (void) _didStartLoading {
970 [self applyRightButton];
972 if ([loading_ count] != 1)
975 [delegate_ retainNetworkActivityIndicator];
976 [self didStartLoading];
979 - (void) didFinishLoading {
980 // Overridden in subclasses.
983 - (void) _didFinishLoading {
984 if ([loading_ count] != 0)
987 [self applyRightButton];
988 [[self navigationItem] setTitle:title_];
990 [delegate_ releaseNetworkActivityIndicator];
991 [self didFinishLoading];
995 return [loading_ count] != 0;
998 - (id) initWithWidth:(float)width ofClass:(Class)_class {
999 if ((self = [super init]) != nil) {
1001 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
1003 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1005 webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
1006 [webview_ setDelegate:self];
1007 [self setView:webview_];
1009 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
1010 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
1012 [webview_ setDetectsPhoneNumbers:NO];
1014 [webview_ setScalesPageToFit:YES];
1016 UIWebDocumentView *document([webview_ _documentView]);
1018 // XXX: I think this improves scrolling; the hardcoded-ness sucks
1019 [document setTileSize:CGSizeMake(320, 500)];
1021 [document setBackgroundColor:[UIColor clearColor]];
1023 // XXX: this is terribly (too?) expensive
1024 [document setDrawsBackground:NO];
1026 WebView *webview([document webView]);
1027 WebPreferences *preferences([webview preferences]);
1029 // XXX: I have no clue if I actually /want/ this modification
1030 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1031 [webview _setLayoutInterval:0];
1032 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
1033 [preferences _setLayoutInterval:0];
1035 [preferences setCacheModel:WebCacheModelDocumentBrowser];
1036 [preferences setOfflineWebApplicationCacheEnabled:YES];
1039 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
1040 [document setAllowsMessaging:YES];
1041 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
1042 [webview _setAllowsMessaging:YES];
1045 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1046 scroller_ = [webview_ _scrollView];
1048 [scroller_ setDirectionalLockEnabled:YES];
1049 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
1050 [scroller_ setDelaysContentTouches:NO];
1052 [scroller_ setCanCancelContentTouches:YES];
1053 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1054 UIScroller *scroller([webview_ _scroller]);
1055 scroller_ = (UIScrollView *) scroller;
1057 [scroller setDirectionalScrolling:YES];
1058 // XXX: we might be better off /not/ setting this on older systems
1059 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
1060 [scroller setScrollHysteresis:0]; /* 8 */
1062 [scroller setThumbDetectionEnabled:NO];
1064 // use NO with UIApplicationUseLegacyEvents(YES)
1065 [scroller setEventMode:YES];
1067 // XXX: this is handled by setBounces, right?
1068 //[scroller setAllowsRubberBanding:YES];
1071 [scroller_ setFixedBackgroundPattern:YES];
1072 [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
1073 [scroller_ setClipsSubviews:YES];
1075 [scroller_ setBounces:YES];
1076 [scroller_ setScrollingEnabled:YES];
1077 [scroller_ setShowBackgroundShadow:NO];
1079 [self setViewportWidth:width];
1081 reloaditem_ = [[UIBarButtonItem alloc]
1082 initWithTitle:UCLocalize("RELOAD")
1083 style:[self rightButtonStyle]
1085 action:@selector(reloadButtonClicked)
1088 loadingitem_ = [[UIBarButtonItem alloc]
1090 style:UIBarButtonItemStylePlain
1092 action:@selector(reloadButtonClicked)
1095 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1096 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
1098 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1099 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1103 - (id) initWithWidth:(float)width {
1104 return [self initWithWidth:width ofClass:[self class]];
1108 return [self initWithWidth:0];
1111 - (id) initWithURL:(NSURL *)url {
1112 if ((self = [self init]) != nil) {
1117 - (void) didDismissModalViewController {
1119 [self callFunction:closer_];
1122 - (void) callFunction:(WebScriptObject *)function {
1123 WebThreadLocked lock;
1125 WebView *webview([[webview_ _documentView] webView]);
1126 WebFrame *frame([webview mainFrame]);
1127 WebPreferences *preferences([webview preferences]);
1129 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
1130 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1132 /*id _private(MSHookIvar<id>(webview, "_private"));
1133 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1134 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1137 if (settings == NULL)
1140 no = settings->JavaScriptCanOpenWindowsAutomatically();
1141 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1144 if (UIWindow *window = [[self view] window])
1145 if (UIResponder *responder = [window firstResponder])
1146 [responder resignFirstResponder];
1148 JSObjectRef object([function JSObject]);
1149 JSGlobalContextRef context([frame globalContext]);
1150 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1152 /*if (settings != NULL)
1153 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
1155 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
1158 - (void) reloadButtonClicked {
1162 - (void) _customButtonClicked {
1163 [self reloadButtonClicked];
1166 - (void) customButtonClicked {
1168 if (function_ != nil)
1169 [self callFunction:function_];
1172 [self _customButtonClicked];
1175 + (float) defaultWidth {
1179 - (void) dispatchEvent:(NSString *)event {
1180 WebThreadLocked lock;
1182 NSString *script([NSString stringWithFormat:@
1184 "var event = this.document.createEvent('Events');"
1185 "event.initEvent('%@', false, false);"
1186 "this.document.dispatchEvent(event);"
1190 NSMutableArray *frames([NSMutableArray arrayWithObjects:
1191 [[[webview_ _documentView] webView] mainFrame]
1194 while (WebFrame *frame = [frames lastObject]) {
1195 WebScriptObject *object([frame windowObject]);
1196 [object evaluateWebScript:script];
1197 [frames removeLastObject];
1198 [frames addObjectsFromArray:[frame childFrames]];
1202 - (void) viewWillAppear:(BOOL)animated {
1203 [self dispatchEvent:@"CydiaViewWillAppear"];
1204 [super viewWillAppear:animated];
1207 - (void) viewDidAppear:(BOOL)animated {
1208 [self dispatchEvent:@"CydiaViewDidAppear"];
1209 [super viewDidAppear:animated];
1212 - (void) viewWillDisappear:(BOOL)animated {
1213 [self dispatchEvent:@"CydiaViewWillDisappear"];
1214 [super viewWillDisappear:animated];
1217 - (void) viewDidDisappear:(BOOL)animated {
1218 [self dispatchEvent:@"CydiaViewDidDisappear"];
1219 [super viewDidDisappear:animated];