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) setURL:(NSURL *)url {
171 _assert(request_ == nil);
172 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
175 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
176 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
179 - (void) loadURL:(NSURL *)url {
180 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
183 - (void) loadRequest:(NSURLRequest *)request {
185 NSLog(@"loadRequest:%@", request);
191 WebThreadLocked lock;
192 [[self webView] loadRequest:request];
195 - (void) reloadURLWithCache:(BOOL)cache {
199 NSMutableURLRequest *request([request_ mutableCopy]);
200 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
204 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
205 [self loadRequest:request_];
207 UIAlertView *alert = [[[UIAlertView alloc]
208 initWithTitle:UCLocalize("RESUBMIT_FORM")
211 cancelButtonTitle:UCLocalize("CANCEL")
213 UCLocalize("SUBMIT"),
217 [alert setContext:@"submit"];
223 [self reloadURLWithCache:YES];
226 - (void) reloadData {
230 [self dispatchEvent:@"CydiaReloadData"];
232 [self reloadURLWithCache:YES];
235 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
238 function_ = function;
240 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
243 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
246 function_ = function;
248 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
251 - (void) removeButton {
252 custom_ = [NSNull null];
253 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
256 - (void) scrollToBottomAnimated:(NSNumber *)animated {
257 CGSize size([scroller_ contentSize]);
258 CGPoint offset([scroller_ contentOffset]);
259 CGRect frame([scroller_ frame]);
261 if (size.height - offset.y < frame.size.height + 20.f) {
262 CGRect rect = {{0, size.height-1}, {size.width, 1}};
263 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
267 - (void) _setViewportWidth {
268 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
271 - (void) setViewportWidth:(float)width {
272 width_ = width != 0 ? width : [[self class] defaultWidth];
273 [self _setViewportWidth];
276 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
277 [self setViewportWidth:[width floatValue]];
280 - (void) setViewportWidthOnMainThread:(float)width {
281 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
284 - (void) webViewUpdateViewSettings:(UIWebView *)view {
285 [self _setViewportWidth];
288 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
289 [self dismissModalViewControllerAnimated:YES];
292 - (void) _setupMail:(MFMailComposeViewController *)controller {
295 - (void) _openMailToURL:(NSURL *)url {
296 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
297 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
298 [controller setMailComposeDelegate:self];
300 [controller setMailToURL:url];
302 [self _setupMail:controller];
304 [self presentModalViewController:controller animated:YES];
308 UIApplication *app([UIApplication sharedApplication]);
309 if ([app respondsToSelector:@selector(openURL:asPanel:)])
310 [app openURL:url asPanel:YES];
315 - (bool) _allowJavaScriptPanel {
319 - (bool) allowsNavigationAction {
320 return allowsNavigationAction_;
323 - (void) setAllowsNavigationAction:(bool)value {
324 allowsNavigationAction_ = value;
327 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
328 [self setAllowsNavigationAction:[value boolValue]];
331 - (void) popViewControllerWithNumber:(NSNumber *)value {
332 UINavigationController *navigation([self navigationController]);
333 if ([navigation topViewController] == self)
334 [navigation popViewControllerAnimated:[value boolValue]];
337 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
338 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
339 [self _didFinishLoading];
341 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
344 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
351 if ([frame parentFrame] == nil) {
352 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
353 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
354 [[error localizedDescription] stringByAddingPercentEscapes]
361 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
362 NSURL *url([request URL]);
364 // XXX: filter to internal usage?
365 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
368 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
369 [browser loadRequest:request];
373 [page setDelegate:delegate_];
376 [[self navigationItem] setTitle:title_];
378 [[self navigationController] pushViewController:page animated:YES];
380 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
382 [navigation setDelegate:delegate_];
384 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
385 initWithTitle:UCLocalize("CLOSE")
386 style:UIBarButtonItemStylePlain
388 action:@selector(close)
391 [[self navigationController] presentModalViewController:navigation animated:YES];
395 // CyteWebViewDelegate {{{
396 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
398 static Pcre irritating("^(?"
399 ":" "The page at .* displayed insecure content from .*\\."
400 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
403 if (NSString *data = [message objectForKey:@"message"])
404 if (irritating(data))
407 NSLog(@"addMessageToConsole:%@", message);
411 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
413 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
416 if ([frame parentFrame] == nil) {
418 NSURL *url(request == nil ? nil : [request URL]);
420 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
422 [self pushRequest:request asPop:NO];
429 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
430 if ([frame parentFrame] == nil)
431 if (decision == CYWebPolicyDecisionUse)
438 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
440 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
443 NSURL *url([request URL]);
447 if ([frame isEqualToString:@"_open"])
448 [delegate_ openURL:url];
450 NSString *scheme([[url scheme] lowercaseString]);
451 if ([scheme isEqualToString:@"mailto"])
452 [self _openMailToURL:url];
454 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
460 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
463 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
465 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
468 [self _didFailWithError:error forFrame:frame];
471 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
473 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
476 [self _didFailWithError:error forFrame:frame];
479 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
480 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
482 if ([frame parentFrame] == nil) {
486 if (DOMDocument *document = [frame DOMDocument])
487 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
488 for (DOMHTMLBodyElement *body in (id) bodies) {
489 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
493 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
494 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
495 DOMRGBColor *rgb([color getRGBColorValue]);
497 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
498 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
499 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
500 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
502 if (red == 0xc7 && green == 0xce && blue == 0xd5)
503 uic = [UIColor pinStripeColor];
506 colorWithRed:(red / 255)
514 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
519 [self _didFinishLoading];
522 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
523 if ([frame parentFrame] != nil)
528 [[self navigationItem] setTitle:title_];
531 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
533 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
536 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
538 if ([frame parentFrame] == nil) {
544 allowsNavigationAction_ = true;
549 [self setHidesNavigationBar:NO];
551 // XXX: do we still need to do this?
552 [[self navigationItem] setTitle:nil];
555 [self _didStartLoading];
558 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
560 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
566 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
567 return [self _allowJavaScriptPanel];
570 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
571 return [self _allowJavaScriptPanel];
574 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
575 return [self _allowJavaScriptPanel];
578 - (void) webViewClose:(WebView *)view {
584 [[self navigationController] dismissModalViewControllerAnimated:YES];
587 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
588 NSString *context([alert context]);
590 if ([context isEqualToString:@"sensitive"]) {
593 sensitive_ = [NSNumber numberWithBool:YES];
597 sensitive_ = [NSNumber numberWithBool:NO];
601 [alert dismissWithClickedButtonIndex:-1 animated:YES];
602 } else if ([context isEqualToString:@"challenge"]) {
603 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
607 NSString *username([[alert textFieldAtIndex:0] text]);
608 NSString *password([[alert textFieldAtIndex:1] text]);
610 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
612 [sender useCredential:credential forAuthenticationChallenge:challenge_];
616 [sender cancelAuthenticationChallenge:challenge_];
624 [alert dismissWithClickedButtonIndex:-1 animated:YES];
625 } else if ([context isEqualToString:@"submit"]) {
626 if (button == [alert cancelButtonIndex]) {
627 } else if (button == [alert firstOtherButtonIndex]) {
628 if (request_ != nil) {
629 WebThreadLocked lock;
630 [[self webView] loadRequest:request_];
634 [alert dismissWithClickedButtonIndex:-1 animated:YES];
638 - (UIBarButtonItemStyle) rightButtonStyle {
639 if (style_ == nil) normal:
640 return UIBarButtonItemStylePlain;
641 else if ([style_ isEqualToString:@"Normal"])
642 return UIBarButtonItemStylePlain;
643 else if ([style_ isEqualToString:@"Highlighted"])
644 return UIBarButtonItemStyleDone;
648 - (UIBarButtonItem *) customButton {
651 else if (custom_ == [NSNull null])
652 return (UIBarButtonItem *) [NSNull null];
654 return [[[UIBarButtonItem alloc]
655 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
656 style:[self rightButtonStyle]
658 action:@selector(customButtonClicked)
662 - (UIBarButtonItem *) leftButton {
663 UINavigationItem *item([self navigationItem]);
664 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
667 if (UINavigationController *navigation = [self navigationController])
668 if ([[navigation parentViewController] modalViewController] == navigation)
669 return [[[UIBarButtonItem alloc]
670 initWithTitle:UCLocalize("CLOSE")
671 style:UIBarButtonItemStylePlain
673 action:@selector(close)
679 - (void) applyLeftButton {
680 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
683 - (UIBarButtonItem *) rightButton {
687 - (void) applyLoadingTitle {
688 [[self navigationItem] setTitle:UCLocalize("LOADING")];
691 - (void) layoutRightButton {
692 [[loadingitem_ view] addSubview:indicator_];
693 [[loadingitem_ view] bringSubviewToFront:indicator_];
696 - (void) applyRightButton {
697 if ([self isLoading]) {
698 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
699 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
701 [indicator_ startAnimating];
702 [self applyLoadingTitle];
704 [indicator_ stopAnimating];
706 UIBarButtonItem *button([self customButton]);
708 button = [self rightButton];
709 else if (button == (UIBarButtonItem *) [NSNull null])
712 [[self navigationItem] setRightBarButtonItem:button animated:YES];
716 - (void) didStartLoading {
717 // Overridden in subclasses.
720 - (void) _didStartLoading {
721 [self applyRightButton];
723 if ([loading_ count] != 1)
726 [delegate_ retainNetworkActivityIndicator];
727 [self didStartLoading];
730 - (void) didFinishLoading {
731 // Overridden in subclasses.
734 - (void) _didFinishLoading {
735 if ([loading_ count] != 0)
738 [self applyRightButton];
739 [[self navigationItem] setTitle:title_];
741 [delegate_ releaseNetworkActivityIndicator];
742 [self didFinishLoading];
746 return [loading_ count] != 0;
749 - (id) initWithWidth:(float)width ofClass:(Class)_class {
750 if ((self = [super init]) != nil) {
754 allowsNavigationAction_ = true;
756 loading_ = [NSMutableSet setWithCapacity:5];
757 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
759 reloaditem_ = [[[UIBarButtonItem alloc]
760 initWithTitle:UCLocalize("RELOAD")
761 style:[self rightButtonStyle]
763 action:@selector(reloadButtonClicked)
766 loadingitem_ = [[[UIBarButtonItem alloc]
768 style:UIBarButtonItemStylePlain
770 action:@selector(reloadButtonClicked)
773 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
774 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
775 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
777 [self applyLeftButton];
778 [self applyRightButton];
782 - (NSString *) applicationNameForUserAgent {
787 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
789 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
790 [webview_ setDelegate:self];
791 [self setView:webview_];
793 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
794 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
796 [webview_ setDetectsPhoneNumbers:NO];
798 [webview_ setScalesPageToFit:YES];
800 UIWebDocumentView *document([webview_ _documentView]);
802 // XXX: I think this improves scrolling; the hardcoded-ness sucks
803 [document setTileSize:CGSizeMake(320, 500)];
805 [document setBackgroundColor:[UIColor clearColor]];
807 // XXX: this is terribly (too?) expensive
808 [document setDrawsBackground:NO];
810 WebView *webview([document webView]);
811 WebPreferences *preferences([webview preferences]);
813 // XXX: I have no clue if I actually /want/ this modification
814 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
815 [webview _setLayoutInterval:0];
816 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
817 [preferences _setLayoutInterval:0];
819 [preferences setCacheModel:WebCacheModelDocumentBrowser];
820 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
821 [preferences setOfflineWebApplicationCacheEnabled:YES];
823 if (NSString *agent = [self applicationNameForUserAgent])
824 [webview setApplicationNameForUserAgent:agent];
826 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
827 [webview setShouldUpdateWhileOffscreen:NO];
830 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
831 [document setAllowsMessaging:YES];
832 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
833 [webview _setAllowsMessaging:YES];
836 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
837 scroller_ = [webview_ _scrollView];
839 [scroller_ setDirectionalLockEnabled:YES];
840 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
841 [scroller_ setDelaysContentTouches:NO];
843 [scroller_ setCanCancelContentTouches:YES];
844 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
845 UIScroller *scroller([webview_ _scroller]);
846 scroller_ = (UIScrollView *) scroller;
848 [scroller setDirectionalScrolling:YES];
849 // XXX: we might be better off /not/ setting this on older systems
850 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
851 [scroller setScrollHysteresis:0]; /* 8 */
853 [scroller setThumbDetectionEnabled:NO];
855 // use NO with UIApplicationUseLegacyEvents(YES)
856 [scroller setEventMode:YES];
858 // XXX: this is handled by setBounces, right?
859 //[scroller setAllowsRubberBanding:YES];
862 [scroller_ setFixedBackgroundPattern:YES];
863 [scroller_ setBackgroundColor:[UIColor clearColor]];
864 [scroller_ setClipsSubviews:YES];
866 [scroller_ setBounces:YES];
867 [scroller_ setScrollingEnabled:YES];
868 [scroller_ setShowBackgroundShadow:NO];
870 [self setViewportWidth:width_];
872 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
873 [webview_ insertSubview:table atIndex:0];
875 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
876 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
881 - (void) releaseSubviews {
885 [super releaseSubviews];
888 - (id) initWithWidth:(float)width {
889 return [self initWithWidth:width ofClass:[self class]];
893 return [self initWithWidth:0];
896 - (id) initWithURL:(NSURL *)url {
897 if ((self = [self init]) != nil) {
902 - (void) callFunction:(WebScriptObject *)function {
903 WebThreadLocked lock;
905 WebView *webview([[[self webView] _documentView] webView]);
906 WebFrame *frame([webview mainFrame]);
908 JSGlobalContextRef context([frame globalContext]);
909 JSObjectRef object([function JSObject]);
910 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
913 - (void) reloadButtonClicked {
914 [self reloadURLWithCache:YES];
917 - (void) _customButtonClicked {
918 [self reloadButtonClicked];
921 - (void) customButtonClicked {
923 if (function_ != nil)
924 [self callFunction:function_];
927 [self _customButtonClicked];
930 + (float) defaultWidth {
934 - (void) setNavigationBarStyle:(NSString *)name {
936 if ([name isEqualToString:@"Black"])
937 style = UIBarStyleBlack;
939 style = UIBarStyleDefault;
941 [[[self navigationController] navigationBar] setBarStyle:style];
944 - (void) setNavigationBarTintColor:(UIColor *)color {
945 [[[self navigationController] navigationBar] setTintColor:color];
948 - (void) setBadgeValue:(id)value {
949 [[[self navigationController] tabBarItem] setBadgeValue:value];
952 - (void) setHidesBackButton:(bool)value {
953 [[self navigationItem] setHidesBackButton:value];
954 [self applyLeftButton];
957 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
958 [self setHidesBackButton:[value boolValue]];
961 - (void) dispatchEvent:(NSString *)event {
962 [[self webView] dispatchEvent:event];
965 - (bool) hidesNavigationBar {
966 return hidesNavigationBar_;
969 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
971 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
974 - (void) setHidesNavigationBar:(bool)value {
975 if (hidesNavigationBar_ != value) {
976 hidesNavigationBar_ = value;
977 [self _setHidesNavigationBar:YES animated:YES];
981 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
982 [self setHidesNavigationBar:[value boolValue]];
985 - (void) viewWillAppear:(BOOL)animated {
988 if ([self hidesNavigationBar])
989 [self _setHidesNavigationBar:YES animated:animated];
991 [self dispatchEvent:@"CydiaViewWillAppear"];
992 [super viewWillAppear:animated];
995 - (void) viewDidAppear:(BOOL)animated {
996 [super viewDidAppear:animated];
997 [self dispatchEvent:@"CydiaViewDidAppear"];
1000 - (void) viewWillDisappear:(BOOL)animated {
1001 [self dispatchEvent:@"CydiaViewWillDisappear"];
1002 [super viewWillDisappear:animated];
1004 if ([self hidesNavigationBar])
1005 [self _setHidesNavigationBar:NO animated:animated];
1010 - (void) viewDidDisappear:(BOOL)animated {
1011 [super viewDidDisappear:animated];
1012 [self dispatchEvent:@"CydiaViewDidDisappear"];