1 #include "CyteKit/UCPlatform.h"
2 #include "CyteKit/WebViewController.h"
4 #include "CyteKit/MFMailComposeViewController-MailToURL.h"
6 #include "iPhonePrivate.h"
8 #include "CyteKit/Localize.h"
9 #include "CyteKit/WebViewController.h"
10 #include "CyteKit/PerlCompatibleRegEx.hpp"
11 #include "CyteKit/WebThreadLocked.hpp"
13 //#include <QuartzCore/CALayer.h>
14 // XXX: fix the minimum requirement
15 extern NSString * const kCAFilterNearest;
17 #include <WebCore/WebCoreThread.h>
19 #include <WebKit/WebKitErrors.h>
20 #include <WebKit/WebPreferences.h>
22 #include <WebKit/DOMCSSPrimitiveValue.h>
23 #include <WebKit/DOMCSSStyleDeclaration.h>
24 #include <WebKit/DOMDocument.h>
25 #include <WebKit/DOMHTMLBodyElement.h>
26 #include <WebKit/DOMRGBColor.h>
29 #include <objc/runtime.h>
32 #define DefaultTimeout_ 120.0
34 #define ShowInternals 0
38 #define lprintf(args...) fprintf(stderr, args)
40 // XXX: centralize these special class things to some file or mechanism?
41 static Class $MFMailComposeViewController;
43 float CYScrollViewDecelerationRateNormal;
45 @interface WebView (Apple)
46 - (void) _setLayoutInterval:(float)interval;
47 - (void) _setAllowsMessaging:(BOOL)allows;
50 @interface WebPreferences (Apple)
51 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
52 - (void) _setLayoutInterval:(NSInteger)interval;
53 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
56 /* Indirect Delegate {{{ */
57 @interface IndirectDelegate : NSObject {
58 _transient volatile id delegate_;
61 - (void) setDelegate:(id)delegate;
62 - (id) initWithDelegate:(id)delegate;
65 @implementation IndirectDelegate
67 - (void) setDelegate:(id)delegate {
71 - (id) initWithDelegate:(id)delegate {
76 - (IMP) methodForSelector:(SEL)sel {
77 if (IMP method = [super methodForSelector:sel])
79 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
83 - (BOOL) respondsToSelector:(SEL)sel {
84 if ([super respondsToSelector:sel])
87 // XXX: WebThreadCreateNSInvocation returns nil
90 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
93 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
96 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
97 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
101 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
104 if (delegate_ != nil)
105 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
108 // XXX: I fucking hate Apple so very very bad
109 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
112 - (void) forwardInvocation:(NSInvocation *)inv {
113 SEL sel = [inv selector];
114 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
115 [inv invokeWithTarget:delegate_];
121 @implementation CyteWebViewController
124 #include "CyteKit/UCInternal.h"
127 + (void) _initialize {
128 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
130 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
131 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
133 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
134 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
135 else // XXX: this actually might be fast on some older systems: we should look into this
136 CYScrollViewDecelerationRateNormal = 0.998;
141 NSLog(@"[CyteWebViewController dealloc]");
144 if ([loading_ count] != 0)
145 [delegate_ releaseNetworkActivityIndicator];
150 - (NSString *) description {
151 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]];
154 - (CyteWebView *) webView {
155 return (CyteWebView *) [self view];
158 - (NSURL *) URLWithURL:(NSURL *)url {
162 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
164 requestWithURL:[self URLWithURL:url]
166 timeoutInterval:DefaultTimeout_
170 - (void) setRequest:(NSURLRequest *)request {
171 _assert(request_ == nil);
175 - (void) setURL:(NSURL *)url {
176 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]];
179 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
180 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
183 - (void) loadURL:(NSURL *)url {
184 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
187 - (void) loadRequest:(NSURLRequest *)request {
189 NSLog(@"loadRequest:%@", request);
195 WebThreadLocked lock;
196 [[self webView] loadRequest:request];
199 - (void) reloadURLWithCache:(BOOL)cache {
203 NSMutableURLRequest *request([request_ mutableCopy]);
204 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
208 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
209 [self loadRequest:request_];
211 UIAlertView *alert = [[[UIAlertView alloc]
212 initWithTitle:UCLocalize("RESUBMIT_FORM")
215 cancelButtonTitle:UCLocalize("CANCEL")
217 UCLocalize("SUBMIT"),
221 [alert setContext:@"submit"];
227 [self reloadURLWithCache:YES];
230 - (void) reloadData {
234 [self dispatchEvent:@"CydiaReloadData"];
236 [self reloadURLWithCache:YES];
239 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
242 function_ = function;
244 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
247 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
250 function_ = function;
252 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
255 - (void) removeButton {
256 custom_ = [NSNull null];
257 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
260 - (void) scrollToBottomAnimated:(NSNumber *)animated {
261 CGSize size([scroller_ contentSize]);
262 CGPoint offset([scroller_ contentOffset]);
263 CGRect frame([scroller_ frame]);
265 if (size.height - offset.y < frame.size.height + 20.f) {
266 CGRect rect = {{0, size.height-1}, {size.width, 1}};
267 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
271 - (void) _setViewportWidth {
272 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
275 - (void) setViewportWidth:(float)width {
276 width_ = width != 0 ? width : [[self class] defaultWidth];
277 [self _setViewportWidth];
280 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
281 [self setViewportWidth:[width floatValue]];
284 - (void) setViewportWidthOnMainThread:(float)width {
285 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
288 - (void) webViewUpdateViewSettings:(UIWebView *)view {
289 [self _setViewportWidth];
292 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
293 [self dismissModalViewControllerAnimated:YES];
296 - (void) _setupMail:(MFMailComposeViewController *)controller {
299 - (void) _openMailToURL:(NSURL *)url {
300 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
301 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
302 [controller setMailComposeDelegate:self];
304 [controller setMailToURL:url];
306 [self _setupMail:controller];
308 [self presentModalViewController:controller animated:YES];
312 UIApplication *app([UIApplication sharedApplication]);
313 if ([app respondsToSelector:@selector(openURL:asPanel:)])
314 [app openURL:url asPanel:YES];
319 - (bool) _allowJavaScriptPanel {
323 - (bool) allowsNavigationAction {
324 return allowsNavigationAction_;
327 - (void) setAllowsNavigationAction:(bool)value {
328 allowsNavigationAction_ = value;
331 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
332 [self setAllowsNavigationAction:[value boolValue]];
335 - (void) popViewControllerWithNumber:(NSNumber *)value {
336 UINavigationController *navigation([self navigationController]);
337 if ([navigation topViewController] == self)
338 [navigation popViewControllerAnimated:[value boolValue]];
341 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
342 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
343 [self _didFinishLoading];
345 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
348 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
355 if ([frame parentFrame] == nil) {
356 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
357 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
358 [[error localizedDescription] stringByAddingPercentEscapes]
365 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
366 NSURL *url([request URL]);
368 // XXX: filter to internal usage?
369 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
372 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
373 [browser setRequest:request];
377 [page setDelegate:delegate_];
380 [[self navigationItem] setTitle:title_];
382 [[self navigationController] pushViewController:page animated:YES];
384 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
386 [navigation setDelegate:delegate_];
388 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
389 initWithTitle:UCLocalize("CLOSE")
390 style:UIBarButtonItemStylePlain
392 action:@selector(close)
395 [[self navigationController] presentModalViewController:navigation animated:YES];
399 // CyteWebViewDelegate {{{
400 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
402 static Pcre irritating("^(?"
403 ":" "The page at .* displayed insecure content from .*\\."
404 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
407 if (NSString *data = [message objectForKey:@"message"])
408 if (irritating(data))
411 NSLog(@"addMessageToConsole:%@", message);
415 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
417 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
420 if ([frame parentFrame] == nil) {
422 NSURL *url(request == nil ? nil : [request URL]);
424 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
426 [self pushRequest:request asPop:NO];
433 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
434 if ([frame parentFrame] == nil)
435 if (decision == CYWebPolicyDecisionUse)
442 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
444 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
447 NSURL *url([request URL]);
451 if ([frame isEqualToString:@"_open"])
452 [delegate_ openURL:url];
454 NSString *scheme([[url scheme] lowercaseString]);
455 if ([scheme isEqualToString:@"mailto"])
456 [self _openMailToURL:url];
458 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
464 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
467 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
469 NSLog(@"didCommitLoadForFrame:%@", frame);
472 if ([frame parentFrame] == nil) {
476 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
478 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
481 [self _didFailWithError:error forFrame:frame];
484 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
486 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
489 [self _didFailWithError:error forFrame:frame];
492 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
493 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
495 if ([frame parentFrame] == nil) {
499 if (DOMDocument *document = [frame DOMDocument])
500 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
501 for (DOMHTMLBodyElement *body in (id) bodies) {
502 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
506 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
507 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
508 DOMRGBColor *rgb([color getRGBColorValue]);
510 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
511 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
512 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
513 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
515 if (red == 0xc7 && green == 0xce && blue == 0xd5)
516 uic = [UIColor pinStripeColor];
519 colorWithRed:(red / 255)
527 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
532 [self _didFinishLoading];
535 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
536 if ([frame parentFrame] != nil)
541 [[self navigationItem] setTitle:title_];
544 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
546 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
549 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
551 if ([frame parentFrame] == nil) {
557 allowsNavigationAction_ = true;
562 [self setHidesNavigationBar:NO];
564 // XXX: do we still need to do this?
565 [[self navigationItem] setTitle:nil];
568 [self _didStartLoading];
571 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
573 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
579 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
580 return [self _allowJavaScriptPanel];
583 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
584 return [self _allowJavaScriptPanel];
587 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
588 return [self _allowJavaScriptPanel];
591 - (void) webViewClose:(WebView *)view {
597 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
600 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
601 NSString *context([alert context]);
603 if ([context isEqualToString:@"sensitive"]) {
606 sensitive_ = [NSNumber numberWithBool:YES];
610 sensitive_ = [NSNumber numberWithBool:NO];
614 [alert dismissWithClickedButtonIndex:-1 animated:YES];
615 } else if ([context isEqualToString:@"challenge"]) {
616 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
620 NSString *username([[alert textFieldAtIndex:0] text]);
621 NSString *password([[alert textFieldAtIndex:1] text]);
623 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
625 [sender useCredential:credential forAuthenticationChallenge:challenge_];
629 [sender cancelAuthenticationChallenge:challenge_];
637 [alert dismissWithClickedButtonIndex:-1 animated:YES];
638 } else if ([context isEqualToString:@"submit"]) {
639 if (button == [alert cancelButtonIndex]) {
640 } else if (button == [alert firstOtherButtonIndex]) {
641 if (request_ != nil) {
642 WebThreadLocked lock;
643 [[self webView] loadRequest:request_];
647 [alert dismissWithClickedButtonIndex:-1 animated:YES];
651 - (UIBarButtonItemStyle) rightButtonStyle {
652 if (style_ == nil) normal:
653 return UIBarButtonItemStylePlain;
654 else if ([style_ isEqualToString:@"Normal"])
655 return UIBarButtonItemStylePlain;
656 else if ([style_ isEqualToString:@"Highlighted"])
657 return UIBarButtonItemStyleDone;
661 - (UIBarButtonItem *) customButton {
664 else if (custom_ == [NSNull null])
665 return (UIBarButtonItem *) [NSNull null];
667 return [[[UIBarButtonItem alloc]
668 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
669 style:[self rightButtonStyle]
671 action:@selector(customButtonClicked)
675 - (UIBarButtonItem *) leftButton {
676 UINavigationItem *item([self navigationItem]);
677 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
680 if (UINavigationController *navigation = [self navigationController])
681 if ([[navigation parentViewController] modalViewController] == navigation)
682 return [[[UIBarButtonItem alloc]
683 initWithTitle:UCLocalize("CLOSE")
684 style:UIBarButtonItemStylePlain
686 action:@selector(close)
692 - (void) applyLeftButton {
693 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
696 - (UIBarButtonItem *) rightButton {
700 - (void) applyLoadingTitle {
701 [[self navigationItem] setTitle:UCLocalize("LOADING")];
704 - (void) layoutRightButton {
705 [[loadingitem_ view] addSubview:indicator_];
706 [[loadingitem_ view] bringSubviewToFront:indicator_];
709 - (void) applyRightButton {
710 if ([self isLoading]) {
711 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
712 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
714 [indicator_ startAnimating];
715 [self applyLoadingTitle];
717 [indicator_ stopAnimating];
719 UIBarButtonItem *button([self customButton]);
721 button = [self rightButton];
722 else if (button == (UIBarButtonItem *) [NSNull null])
725 [[self navigationItem] setRightBarButtonItem:button animated:YES];
729 - (void) didStartLoading {
730 // Overridden in subclasses.
733 - (void) _didStartLoading {
734 [self applyRightButton];
736 if ([loading_ count] != 1)
739 [delegate_ retainNetworkActivityIndicator];
740 [self didStartLoading];
743 - (void) didFinishLoading {
744 // Overridden in subclasses.
747 - (void) _didFinishLoading {
748 if ([loading_ count] != 0)
751 [self applyRightButton];
752 [[self navigationItem] setTitle:title_];
754 [delegate_ releaseNetworkActivityIndicator];
755 [self didFinishLoading];
759 return [loading_ count] != 0;
762 - (id) initWithWidth:(float)width ofClass:(Class)_class {
763 if ((self = [super init]) != nil) {
767 allowsNavigationAction_ = true;
769 loading_ = [NSMutableSet setWithCapacity:5];
770 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
772 reloaditem_ = [[[UIBarButtonItem alloc]
773 initWithTitle:UCLocalize("RELOAD")
774 style:[self rightButtonStyle]
776 action:@selector(reloadButtonClicked)
779 loadingitem_ = [[[UIBarButtonItem alloc]
781 style:UIBarButtonItemStylePlain
783 action:@selector(reloadButtonClicked)
786 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
787 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
788 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
790 [self applyLeftButton];
791 [self applyRightButton];
795 - (NSString *) applicationNameForUserAgent {
800 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
802 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
803 [webview_ setDelegate:self];
804 [self setView:webview_];
806 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
807 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
809 [webview_ setDetectsPhoneNumbers:NO];
811 [webview_ setScalesPageToFit:YES];
813 UIWebDocumentView *document([webview_ _documentView]);
815 // XXX: I think this improves scrolling; the hardcoded-ness sucks
816 [document setTileSize:CGSizeMake(320, 500)];
818 [document setBackgroundColor:[UIColor clearColor]];
820 // XXX: this is terribly (too?) expensive
821 [document setDrawsBackground:NO];
823 WebView *webview([document webView]);
824 WebPreferences *preferences([webview preferences]);
826 // XXX: I have no clue if I actually /want/ this modification
827 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
828 [webview _setLayoutInterval:0];
829 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
830 [preferences _setLayoutInterval:0];
832 [preferences setCacheModel:WebCacheModelDocumentBrowser];
833 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
834 [preferences setOfflineWebApplicationCacheEnabled:YES];
836 if (NSString *agent = [self applicationNameForUserAgent])
837 [webview setApplicationNameForUserAgent:agent];
839 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
840 [webview setShouldUpdateWhileOffscreen:NO];
843 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
844 [document setAllowsMessaging:YES];
845 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
846 [webview _setAllowsMessaging:YES];
849 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
850 scroller_ = [webview_ _scrollView];
852 [scroller_ setDirectionalLockEnabled:YES];
853 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
854 [scroller_ setDelaysContentTouches:NO];
856 [scroller_ setCanCancelContentTouches:YES];
857 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
858 UIScroller *scroller([webview_ _scroller]);
859 scroller_ = (UIScrollView *) scroller;
861 [scroller setDirectionalScrolling:YES];
862 // XXX: we might be better off /not/ setting this on older systems
863 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
864 [scroller setScrollHysteresis:0]; /* 8 */
866 [scroller setThumbDetectionEnabled:NO];
868 // use NO with UIApplicationUseLegacyEvents(YES)
869 [scroller setEventMode:YES];
871 // XXX: this is handled by setBounces, right?
872 //[scroller setAllowsRubberBanding:YES];
875 [scroller_ setFixedBackgroundPattern:YES];
876 [scroller_ setBackgroundColor:[UIColor clearColor]];
877 [scroller_ setClipsSubviews:YES];
879 [scroller_ setBounces:YES];
880 [scroller_ setScrollingEnabled:YES];
881 [scroller_ setShowBackgroundShadow:NO];
883 [self setViewportWidth:width_];
885 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
886 [table setScrollsToTop:NO];
887 [webview_ insertSubview:table atIndex:0];
889 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
890 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
895 - (void) releaseSubviews {
899 [super releaseSubviews];
902 - (id) initWithWidth:(float)width {
903 return [self initWithWidth:width ofClass:[self class]];
907 return [self initWithWidth:0];
910 - (id) initWithURL:(NSURL *)url {
911 if ((self = [self init]) != nil) {
916 - (void) callFunction:(WebScriptObject *)function {
917 WebThreadLocked lock;
919 WebView *webview([[[self webView] _documentView] webView]);
920 WebFrame *frame([webview mainFrame]);
922 JSGlobalContextRef context([frame globalContext]);
923 JSObjectRef object([function JSObject]);
924 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
927 - (void) reloadButtonClicked {
928 [self reloadURLWithCache:YES];
931 - (void) _customButtonClicked {
932 [self reloadButtonClicked];
935 - (void) customButtonClicked {
937 if (function_ != nil)
938 [self callFunction:function_];
941 [self _customButtonClicked];
944 + (float) defaultWidth {
948 - (void) setNavigationBarStyle:(NSString *)name {
950 if ([name isEqualToString:@"Black"])
951 style = UIBarStyleBlack;
953 style = UIBarStyleDefault;
955 [[[self navigationController] navigationBar] setBarStyle:style];
958 - (void) setNavigationBarTintColor:(UIColor *)color {
959 [[[self navigationController] navigationBar] setTintColor:color];
962 - (void) setBadgeValue:(id)value {
963 [[[self navigationController] tabBarItem] setBadgeValue:value];
966 - (void) setHidesBackButton:(bool)value {
967 [[self navigationItem] setHidesBackButton:value];
968 [self applyLeftButton];
971 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
972 [self setHidesBackButton:[value boolValue]];
975 - (void) dispatchEvent:(NSString *)event {
976 [[self webView] dispatchEvent:event];
979 - (bool) hidesNavigationBar {
980 return hidesNavigationBar_;
983 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
985 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
988 - (void) setHidesNavigationBar:(bool)value {
989 if (hidesNavigationBar_ != value) {
990 hidesNavigationBar_ = value;
991 [self _setHidesNavigationBar:YES animated:YES];
995 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
996 [self setHidesNavigationBar:[value boolValue]];
999 - (void) viewWillAppear:(BOOL)animated {
1002 if ([self hidesNavigationBar])
1003 [self _setHidesNavigationBar:YES animated:animated];
1005 [self dispatchEvent:@"CydiaViewWillAppear"];
1006 [super viewWillAppear:animated];
1009 - (void) viewDidAppear:(BOOL)animated {
1010 [super viewDidAppear:animated];
1011 [self dispatchEvent:@"CydiaViewDidAppear"];
1014 - (void) viewWillDisappear:(BOOL)animated {
1015 [self dispatchEvent:@"CydiaViewWillDisappear"];
1016 [super viewWillDisappear:animated];
1018 if ([self hidesNavigationBar])
1019 [self _setHidesNavigationBar:NO animated:animated];
1024 - (void) viewDidDisappear:(BOOL)animated {
1025 [super viewDidDisappear:animated];
1026 [self dispatchEvent:@"CydiaViewDidDisappear"];