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];
489 if ([loading_ count] != 0)
490 [delegate_ releaseNetworkActivityIndicator];
493 [reloaditem_ release];
494 [loadingitem_ release];
496 [indicator_ release];
501 - (void) setURL:(NSURL *)url {
502 _assert(request_ == nil);
504 request_ = [NSURLRequest
506 cachePolicy:NSURLRequestUseProtocolCachePolicy
507 timeoutInterval:DefaultTimeout_
511 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
512 [self loadRequest:[NSURLRequest
515 timeoutInterval:DefaultTimeout_
519 - (void) loadURL:(NSURL *)url {
520 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
523 - (void) loadRequest:(NSURLRequest *)request {
525 NSLog(@"loadRequest:%@", request);
530 WebThreadLocked lock;
531 [webview_ loadRequest:request];
534 - (void) reloadURLWithCache:(BOOL)cache {
538 NSMutableURLRequest *request([request_ mutableCopy]);
539 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
543 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
544 [self loadRequest:request_];
546 UIAlertView *alert = [[[UIAlertView alloc]
547 initWithTitle:UCLocalize("RESUBMIT_FORM")
550 cancelButtonTitle:UCLocalize("CANCEL")
552 UCLocalize("SUBMIT"),
556 [alert setContext:@"submit"];
562 [self reloadURLWithCache:YES];
565 - (void) reloadData {
567 [self reloadURLWithCache:YES];
570 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
573 function_ = function;
575 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
578 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
581 function_ = function;
583 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
586 - (void) removeButton {
587 custom_ = [NSNull null];
588 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
591 - (void) setPopupHook:(id)function {
593 [closer_ autorelease];
597 closer_ = [function retain];
600 - (void) scrollToBottomAnimated:(NSNumber *)animated {
601 CGSize size([scroller_ contentSize]);
602 CGPoint offset([scroller_ contentOffset]);
603 CGRect frame([scroller_ frame]);
605 if (size.height - offset.y < frame.size.height + 20.f) {
606 CGRect rect = {{0, size.height-1}, {size.width, 1}};
607 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
611 - (void) _setViewportWidth {
612 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
615 - (void) setViewportWidth:(float)width {
616 width_ = width != 0 ? width : [[self class] defaultWidth];
617 [self _setViewportWidth];
620 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
621 [self setViewportWidth:[width floatValue]];
624 - (void) setViewportWidthOnMainThread:(float)width {
625 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
628 - (void) webViewUpdateViewSettings:(UIWebView *)view {
629 [self _setViewportWidth];
632 - (void) _openMailToURL:(NSURL *)url {
633 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
636 - (bool) _allowJavaScriptPanel {
640 - (bool) allowsNavigationAction {
641 return allowsNavigationAction_;
644 - (void) setAllowsNavigationAction:(bool)value {
645 allowsNavigationAction_ = value;
648 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
649 [self setAllowsNavigationAction:[value boolValue]];
652 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
653 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
654 [self _didFinishLoading];
656 if ([error code] == NSURLErrorCancelled)
659 if ([frame parentFrame] == nil) {
660 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
661 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
662 [[error localizedDescription] stringByAddingPercentEscapes]
669 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
670 NSURL *url([request URL]);
672 // XXX: filter to internal usage?
673 CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
676 BrowserController *browser([[[class_ alloc] init] autorelease]);
677 [browser loadRequest:request];
681 [page setDelegate:delegate_];
684 [[self navigationItem] setTitle:title_];
686 [[self navigationController] pushViewController:page animated:YES];
688 UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
690 [navigation setHook:indirect_];
691 [navigation setDelegate:delegate_];
693 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
694 initWithTitle:UCLocalize("CLOSE")
695 style:UIBarButtonItemStylePlain
697 action:@selector(close)
700 [[self navigationController] presentModalViewController:navigation animated:YES];
704 // CYWebViewDelegate {{{
705 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
707 NSLog(@"addMessageToConsole:%@", message);
711 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
713 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
716 if ([frame parentFrame] == nil) {
718 NSURL *url(request == nil ? nil : [request URL]);
720 if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
724 [self pushRequest:request asPop:NO];
731 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
733 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
736 NSURL *url([request URL]);
740 if ([frame isEqualToString:@"_open"])
741 [delegate_ openURL:url];
743 NSString *scheme([[url scheme] lowercaseString]);
744 if ([scheme isEqualToString:@"mailto"])
745 [self _openMailToURL:url];
747 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
753 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
756 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
758 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
761 [self _didFailWithError:error forFrame:frame];
764 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
766 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
769 [self _didFailWithError:error forFrame:frame];
772 // XXX: factor this out somewhere
773 - (UIColor *) groupTableViewBackgroundColor {
774 UIDevice *device([UIDevice currentDevice]);
775 bool iPad([device respondsToSelector:@selector(userInterfaceIdiom)] && [device userInterfaceIdiom] == UIUserInterfaceIdiomPad);
776 return iPad ? [UIColor colorWithRed:0.821 green:0.834 blue:0.860 alpha:1] : [UIColor groupTableViewBackgroundColor];
779 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
780 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
782 if ([frame parentFrame] == nil) {
783 if (DOMDocument *document = [frame DOMDocument])
784 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
785 for (DOMHTMLBodyElement *body in (id) bodies) {
786 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
788 UIColor *uic([self groupTableViewBackgroundColor]);
790 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
791 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
792 DOMRGBColor *rgb([color getRGBColorValue]);
794 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
795 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
796 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
797 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
799 if (red == 0xc7 && green == 0xce && blue == 0xd5)
800 uic = [UIColor pinStripeColor];
803 colorWithRed:(red / 255)
811 [scroller_ setBackgroundColor:uic];
816 [self _didFinishLoading];
819 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
820 if ([frame parentFrame] != nil)
824 [title_ autorelease];
825 title_ = [title retain];
827 [[self navigationItem] setTitle:title_];
830 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
831 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
833 if ([frame parentFrame] == nil) {
840 [self setHidesNavigationBar:NO];
842 // XXX: do we still need to do this?
843 [[self navigationItem] setTitle:nil];
846 [self _didStartLoading];
849 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
851 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
857 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
858 return [self _allowJavaScriptPanel];
861 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
862 return [self _allowJavaScriptPanel];
865 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
866 return [self _allowJavaScriptPanel];
869 - (void) webViewClose:(WebView *)view {
875 [[self navigationController] dismissModalViewControllerAnimated:YES];
878 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
879 NSString *context([alert context]);
881 if ([context isEqualToString:@"sensitive"]) {
884 sensitive_ = [NSNumber numberWithBool:YES];
888 sensitive_ = [NSNumber numberWithBool:NO];
892 [alert dismissWithClickedButtonIndex:-1 animated:YES];
893 } else if ([context isEqualToString:@"challenge"]) {
894 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
898 NSString *username([[alert textFieldAtIndex:0] text]);
899 NSString *password([[alert textFieldAtIndex:1] text]);
901 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
903 [sender useCredential:credential forAuthenticationChallenge:challenge_];
907 [sender cancelAuthenticationChallenge:challenge_];
913 [challenge_ release];
916 [alert dismissWithClickedButtonIndex:-1 animated:YES];
917 } else if ([context isEqualToString:@"submit"]) {
918 if (button == [alert cancelButtonIndex]) {
919 } else if (button == [alert firstOtherButtonIndex]) {
920 if (request_ != nil) {
921 WebThreadLocked lock;
922 [webview_ loadRequest:request_];
926 [alert dismissWithClickedButtonIndex:-1 animated:YES];
930 - (UIBarButtonItemStyle) rightButtonStyle {
931 if (style_ == nil) normal:
932 return UIBarButtonItemStylePlain;
933 else if ([style_ isEqualToString:@"Normal"])
934 return UIBarButtonItemStylePlain;
935 else if ([style_ isEqualToString:@"Highlighted"])
936 return UIBarButtonItemStyleDone;
940 - (UIBarButtonItem *) customButton {
941 return custom_ == [NSNull null] ? nil : [[[UIBarButtonItem alloc]
942 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
943 style:[self rightButtonStyle]
945 action:@selector(customButtonClicked)
949 - (UIBarButtonItem *) rightButton {
953 - (void) applyLoadingTitle {
954 [[self navigationItem] setTitle:UCLocalize("LOADING")];
957 - (void) layoutRightButton {
958 [[loadingitem_ view] addSubview:indicator_];
959 [[loadingitem_ view] bringSubviewToFront:indicator_];
962 - (void) applyRightButton {
963 if ([self isLoading]) {
964 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
965 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
967 [indicator_ startAnimating];
968 [self applyLoadingTitle];
970 [indicator_ stopAnimating];
972 [[self navigationItem] setRightBarButtonItem:(
973 custom_ != nil ? [self customButton] : [self rightButton]
978 - (void) didStartLoading {
979 // Overridden in subclasses.
982 - (void) _didStartLoading {
983 [self applyRightButton];
985 if ([loading_ count] != 1)
988 [delegate_ retainNetworkActivityIndicator];
989 [self didStartLoading];
992 - (void) didFinishLoading {
993 // Overridden in subclasses.
996 - (void) _didFinishLoading {
997 if ([loading_ count] != 0)
1000 [self applyRightButton];
1001 [[self navigationItem] setTitle:title_];
1003 [delegate_ releaseNetworkActivityIndicator];
1004 [self didFinishLoading];
1007 - (bool) isLoading {
1008 return [loading_ count] != 0;
1011 - (id) initWithWidth:(float)width ofClass:(Class)_class {
1012 if ((self = [super init]) != nil) {
1013 allowsNavigationAction_ = true;
1016 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
1018 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1020 webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
1021 [webview_ setDelegate:self];
1022 [self setView:webview_];
1024 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
1025 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
1027 [webview_ setDetectsPhoneNumbers:NO];
1029 [webview_ setScalesPageToFit:YES];
1031 UIWebDocumentView *document([webview_ _documentView]);
1033 // XXX: I think this improves scrolling; the hardcoded-ness sucks
1034 [document setTileSize:CGSizeMake(320, 500)];
1036 [document setBackgroundColor:[UIColor clearColor]];
1038 // XXX: this is terribly (too?) expensive
1039 [document setDrawsBackground:NO];
1041 WebView *webview([document webView]);
1042 WebPreferences *preferences([webview preferences]);
1044 // XXX: I have no clue if I actually /want/ this modification
1045 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1046 [webview _setLayoutInterval:0];
1047 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
1048 [preferences _setLayoutInterval:0];
1050 [preferences setCacheModel:WebCacheModelDocumentBrowser];
1051 [preferences setOfflineWebApplicationCacheEnabled:YES];
1054 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
1055 [document setAllowsMessaging:YES];
1056 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
1057 [webview _setAllowsMessaging:YES];
1060 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1061 scroller_ = [webview_ _scrollView];
1063 [scroller_ setDirectionalLockEnabled:YES];
1064 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
1065 [scroller_ setDelaysContentTouches:NO];
1067 [scroller_ setCanCancelContentTouches:YES];
1068 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1069 UIScroller *scroller([webview_ _scroller]);
1070 scroller_ = (UIScrollView *) scroller;
1072 [scroller setDirectionalScrolling:YES];
1073 // XXX: we might be better off /not/ setting this on older systems
1074 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
1075 [scroller setScrollHysteresis:0]; /* 8 */
1077 [scroller setThumbDetectionEnabled:NO];
1079 // use NO with UIApplicationUseLegacyEvents(YES)
1080 [scroller setEventMode:YES];
1082 // XXX: this is handled by setBounces, right?
1083 //[scroller setAllowsRubberBanding:YES];
1086 [scroller_ setFixedBackgroundPattern:YES];
1087 [scroller_ setBackgroundColor:[self groupTableViewBackgroundColor]];
1088 [scroller_ setClipsSubviews:YES];
1090 [scroller_ setBounces:YES];
1091 [scroller_ setScrollingEnabled:YES];
1092 [scroller_ setShowBackgroundShadow:NO];
1094 [self setViewportWidth:width];
1096 reloaditem_ = [[UIBarButtonItem alloc]
1097 initWithTitle:UCLocalize("RELOAD")
1098 style:[self rightButtonStyle]
1100 action:@selector(reloadButtonClicked)
1103 loadingitem_ = [[UIBarButtonItem alloc]
1105 style:UIBarButtonItemStylePlain
1107 action:@selector(reloadButtonClicked)
1110 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1111 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
1113 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1114 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1118 - (id) initWithWidth:(float)width {
1119 return [self initWithWidth:width ofClass:[self class]];
1123 return [self initWithWidth:0];
1126 - (id) initWithURL:(NSURL *)url {
1127 if ((self = [self init]) != nil) {
1132 - (void) didDismissModalViewController {
1134 [self callFunction:closer_];
1137 - (void) callFunction:(WebScriptObject *)function {
1138 WebThreadLocked lock;
1140 WebView *webview([[webview_ _documentView] webView]);
1141 WebFrame *frame([webview mainFrame]);
1142 WebPreferences *preferences([webview preferences]);
1144 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
1145 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1147 /*id _private(MSHookIvar<id>(webview, "_private"));
1148 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1149 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1152 if (settings == NULL)
1155 no = settings->JavaScriptCanOpenWindowsAutomatically();
1156 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1159 if (UIWindow *window = [[self view] window])
1160 if (UIResponder *responder = [window firstResponder])
1161 [responder resignFirstResponder];
1163 JSObjectRef object([function JSObject]);
1164 JSGlobalContextRef context([frame globalContext]);
1165 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1167 /*if (settings != NULL)
1168 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
1170 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
1173 - (void) reloadButtonClicked {
1174 [self reloadURLWithCache:YES];
1177 - (void) _customButtonClicked {
1178 [self reloadButtonClicked];
1181 - (void) customButtonClicked {
1183 if (function_ != nil)
1184 [self callFunction:function_];
1187 [self _customButtonClicked];
1190 + (float) defaultWidth {
1194 - (void) setNavigationBarStyle:(NSString *)name {
1196 if ([name isEqualToString:@"Black"])
1197 style = UIBarStyleBlack;
1199 style = UIBarStyleDefault;
1201 [[[self navigationController] navigationBar] setBarStyle:style];
1204 - (void) setNavigationBarTintColor:(UIColor *)color {
1205 [[[self navigationController] navigationBar] setTintColor:color];
1208 - (void) setHidesBackButton:(bool)value {
1209 [[self navigationItem] setHidesBackButton:value];
1212 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
1213 [self setHidesBackButton:[value boolValue]];
1216 - (void) dispatchEvent:(NSString *)event {
1217 WebThreadLocked lock;
1219 NSString *script([NSString stringWithFormat:@
1221 "var event = this.document.createEvent('Events');"
1222 "event.initEvent('%@', false, false);"
1223 "this.document.dispatchEvent(event);"
1227 NSMutableArray *frames([NSMutableArray arrayWithObjects:
1228 [[[webview_ _documentView] webView] mainFrame]
1231 while (WebFrame *frame = [frames lastObject]) {
1232 WebScriptObject *object([frame windowObject]);
1233 [object evaluateWebScript:script];
1234 [frames removeLastObject];
1235 [frames addObjectsFromArray:[frame childFrames]];
1239 - (bool) hidesNavigationBar {
1240 return hidesNavigationBar_;
1243 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1245 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1248 - (void) setHidesNavigationBar:(bool)value {
1249 if (hidesNavigationBar_ != value) {
1250 hidesNavigationBar_ = value;
1251 [self _setHidesNavigationBar:YES animated:YES];
1255 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1256 [self setHidesNavigationBar:[value boolValue]];
1259 - (void) viewWillAppear:(BOOL)animated {
1262 if ([self hidesNavigationBar])
1263 [self _setHidesNavigationBar:YES animated:animated];
1265 [self dispatchEvent:@"CydiaViewWillAppear"];
1266 [super viewWillAppear:animated];
1269 - (void) viewDidAppear:(BOOL)animated {
1270 [super viewDidAppear:animated];
1271 [self dispatchEvent:@"CydiaViewDidAppear"];
1274 - (void) viewWillDisappear:(BOOL)animated {
1275 [self dispatchEvent:@"CydiaViewWillDisappear"];
1276 [super viewWillDisappear:animated];
1278 if ([self hidesNavigationBar])
1279 [self _setHidesNavigationBar:NO animated:animated];
1284 - (void) viewDidDisappear:(BOOL)animated {
1285 [super viewDidDisappear:animated];
1286 [self dispatchEvent:@"CydiaViewDidDisappear"];