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 - (CyteWebView *) webView {
151 return (CyteWebView *) [self view];
154 - (NSURL *) URLWithURL:(NSURL *)url {
158 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
160 requestWithURL:[self URLWithURL:url]
162 timeoutInterval:DefaultTimeout_
166 - (void) setURL:(NSURL *)url {
167 _assert(request_ == nil);
168 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
171 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
172 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
175 - (void) loadURL:(NSURL *)url {
176 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
179 - (void) loadRequest:(NSURLRequest *)request {
181 NSLog(@"loadRequest:%@", request);
187 WebThreadLocked lock;
188 [[self webView] loadRequest:request];
191 - (void) reloadURLWithCache:(BOOL)cache {
195 NSMutableURLRequest *request([request_ mutableCopy]);
196 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
200 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
201 [self loadRequest:request_];
203 UIAlertView *alert = [[[UIAlertView alloc]
204 initWithTitle:UCLocalize("RESUBMIT_FORM")
207 cancelButtonTitle:UCLocalize("CANCEL")
209 UCLocalize("SUBMIT"),
213 [alert setContext:@"submit"];
219 [self reloadURLWithCache:YES];
222 - (void) reloadData {
226 [self dispatchEvent:@"CydiaReloadData"];
228 [self reloadURLWithCache:YES];
231 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
234 function_ = function;
236 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
239 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
242 function_ = function;
244 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
247 - (void) removeButton {
248 custom_ = [NSNull null];
249 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
252 - (void) scrollToBottomAnimated:(NSNumber *)animated {
253 CGSize size([scroller_ contentSize]);
254 CGPoint offset([scroller_ contentOffset]);
255 CGRect frame([scroller_ frame]);
257 if (size.height - offset.y < frame.size.height + 20.f) {
258 CGRect rect = {{0, size.height-1}, {size.width, 1}};
259 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
263 - (void) _setViewportWidth {
264 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
267 - (void) setViewportWidth:(float)width {
268 width_ = width != 0 ? width : [[self class] defaultWidth];
269 [self _setViewportWidth];
272 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
273 [self setViewportWidth:[width floatValue]];
276 - (void) setViewportWidthOnMainThread:(float)width {
277 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
280 - (void) webViewUpdateViewSettings:(UIWebView *)view {
281 [self _setViewportWidth];
284 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
285 [self dismissModalViewControllerAnimated:YES];
288 - (void) _setupMail:(MFMailComposeViewController *)controller {
291 - (void) _openMailToURL:(NSURL *)url {
292 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
293 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
294 [controller setMailComposeDelegate:self];
296 [controller setMailToURL:url];
298 [self _setupMail:controller];
300 [self presentModalViewController:controller animated:YES];
304 UIApplication *app([UIApplication sharedApplication]);
305 if ([app respondsToSelector:@selector(openURL:asPanel:)])
306 [app openURL:url asPanel:YES];
311 - (bool) _allowJavaScriptPanel {
315 - (bool) allowsNavigationAction {
316 return allowsNavigationAction_;
319 - (void) setAllowsNavigationAction:(bool)value {
320 allowsNavigationAction_ = value;
323 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
324 [self setAllowsNavigationAction:[value boolValue]];
327 - (void) popViewControllerWithNumber:(NSNumber *)value {
328 UINavigationController *navigation([self navigationController]);
329 if ([navigation topViewController] == self)
330 [navigation popViewControllerAnimated:[value boolValue]];
333 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
334 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
335 [self _didFinishLoading];
337 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
340 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
347 if ([frame parentFrame] == nil) {
348 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
349 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
350 [[error localizedDescription] stringByAddingPercentEscapes]
357 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
358 NSURL *url([request URL]);
360 // XXX: filter to internal usage?
361 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
364 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
365 [browser loadRequest:request];
369 [page setDelegate:delegate_];
372 [[self navigationItem] setTitle:title_];
374 [[self navigationController] pushViewController:page animated:YES];
376 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
378 [navigation setDelegate:delegate_];
380 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
381 initWithTitle:UCLocalize("CLOSE")
382 style:UIBarButtonItemStylePlain
384 action:@selector(close)
387 [[self navigationController] presentModalViewController:navigation animated:YES];
391 // CyteWebViewDelegate {{{
392 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
394 static Pcre irritating("^(?"
395 ":" "The page at .* displayed insecure content from .*\\."
396 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
399 if (NSString *data = [message objectForKey:@"message"])
400 if (irritating(data))
403 NSLog(@"addMessageToConsole:%@", message);
407 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
409 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
412 if ([frame parentFrame] == nil) {
414 NSURL *url(request == nil ? nil : [request URL]);
416 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
418 [self pushRequest:request asPop:NO];
425 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
426 if ([frame parentFrame] == nil)
427 if (decision == CYWebPolicyDecisionUse)
434 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
436 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
439 NSURL *url([request URL]);
443 if ([frame isEqualToString:@"_open"])
444 [delegate_ openURL:url];
446 NSString *scheme([[url scheme] lowercaseString]);
447 if ([scheme isEqualToString:@"mailto"])
448 [self _openMailToURL:url];
450 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
456 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
459 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
461 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
464 [self _didFailWithError:error forFrame:frame];
467 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
469 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
472 [self _didFailWithError:error forFrame:frame];
475 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
476 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
478 if ([frame parentFrame] == nil) {
482 if (DOMDocument *document = [frame DOMDocument])
483 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
484 for (DOMHTMLBodyElement *body in (id) bodies) {
485 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
489 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
490 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
491 DOMRGBColor *rgb([color getRGBColorValue]);
493 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
494 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
495 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
496 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
498 if (red == 0xc7 && green == 0xce && blue == 0xd5)
499 uic = [UIColor pinStripeColor];
502 colorWithRed:(red / 255)
510 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
515 [self _didFinishLoading];
518 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
519 if ([frame parentFrame] != nil)
524 [[self navigationItem] setTitle:title_];
527 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
529 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
532 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
534 if ([frame parentFrame] == nil) {
540 allowsNavigationAction_ = true;
545 [self setHidesNavigationBar:NO];
547 // XXX: do we still need to do this?
548 [[self navigationItem] setTitle:nil];
551 [self _didStartLoading];
554 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
556 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
562 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
563 return [self _allowJavaScriptPanel];
566 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
567 return [self _allowJavaScriptPanel];
570 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
571 return [self _allowJavaScriptPanel];
574 - (void) webViewClose:(WebView *)view {
580 [[self navigationController] dismissModalViewControllerAnimated:YES];
583 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
584 NSString *context([alert context]);
586 if ([context isEqualToString:@"sensitive"]) {
589 sensitive_ = [NSNumber numberWithBool:YES];
593 sensitive_ = [NSNumber numberWithBool:NO];
597 [alert dismissWithClickedButtonIndex:-1 animated:YES];
598 } else if ([context isEqualToString:@"challenge"]) {
599 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
603 NSString *username([[alert textFieldAtIndex:0] text]);
604 NSString *password([[alert textFieldAtIndex:1] text]);
606 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
608 [sender useCredential:credential forAuthenticationChallenge:challenge_];
612 [sender cancelAuthenticationChallenge:challenge_];
620 [alert dismissWithClickedButtonIndex:-1 animated:YES];
621 } else if ([context isEqualToString:@"submit"]) {
622 if (button == [alert cancelButtonIndex]) {
623 } else if (button == [alert firstOtherButtonIndex]) {
624 if (request_ != nil) {
625 WebThreadLocked lock;
626 [[self webView] loadRequest:request_];
630 [alert dismissWithClickedButtonIndex:-1 animated:YES];
634 - (UIBarButtonItemStyle) rightButtonStyle {
635 if (style_ == nil) normal:
636 return UIBarButtonItemStylePlain;
637 else if ([style_ isEqualToString:@"Normal"])
638 return UIBarButtonItemStylePlain;
639 else if ([style_ isEqualToString:@"Highlighted"])
640 return UIBarButtonItemStyleDone;
644 - (UIBarButtonItem *) customButton {
647 else if (custom_ == [NSNull null])
648 return (UIBarButtonItem *) [NSNull null];
650 return [[[UIBarButtonItem alloc]
651 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
652 style:[self rightButtonStyle]
654 action:@selector(customButtonClicked)
658 - (UIBarButtonItem *) leftButton {
659 UINavigationItem *item([self navigationItem]);
660 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
663 if (UINavigationController *navigation = [self navigationController])
664 if ([[navigation parentViewController] modalViewController] == navigation)
665 return [[[UIBarButtonItem alloc]
666 initWithTitle:UCLocalize("CLOSE")
667 style:UIBarButtonItemStylePlain
669 action:@selector(close)
675 - (void) applyLeftButton {
676 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
679 - (UIBarButtonItem *) rightButton {
683 - (void) applyLoadingTitle {
684 [[self navigationItem] setTitle:UCLocalize("LOADING")];
687 - (void) layoutRightButton {
688 [[loadingitem_ view] addSubview:indicator_];
689 [[loadingitem_ view] bringSubviewToFront:indicator_];
692 - (void) applyRightButton {
693 if ([self isLoading]) {
694 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
695 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
697 [indicator_ startAnimating];
698 [self applyLoadingTitle];
700 [indicator_ stopAnimating];
702 UIBarButtonItem *button([self customButton]);
704 button = [self rightButton];
705 else if (button == (UIBarButtonItem *) [NSNull null])
708 [[self navigationItem] setRightBarButtonItem:button animated:YES];
712 - (void) didStartLoading {
713 // Overridden in subclasses.
716 - (void) _didStartLoading {
717 [self applyRightButton];
719 if ([loading_ count] != 1)
722 [delegate_ retainNetworkActivityIndicator];
723 [self didStartLoading];
726 - (void) didFinishLoading {
727 // Overridden in subclasses.
730 - (void) _didFinishLoading {
731 if ([loading_ count] != 0)
734 [self applyRightButton];
735 [[self navigationItem] setTitle:title_];
737 [delegate_ releaseNetworkActivityIndicator];
738 [self didFinishLoading];
742 return [loading_ count] != 0;
745 - (id) initWithWidth:(float)width ofClass:(Class)_class {
746 if ((self = [super init]) != nil) {
750 allowsNavigationAction_ = true;
752 loading_ = [NSMutableSet setWithCapacity:5];
753 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
755 reloaditem_ = [[[UIBarButtonItem alloc]
756 initWithTitle:UCLocalize("RELOAD")
757 style:[self rightButtonStyle]
759 action:@selector(reloadButtonClicked)
762 loadingitem_ = [[[UIBarButtonItem alloc]
764 style:UIBarButtonItemStylePlain
766 action:@selector(reloadButtonClicked)
769 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
770 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
771 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
773 [self applyLeftButton];
774 [self applyRightButton];
778 - (NSString *) applicationNameForUserAgent {
783 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
785 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
786 [webview_ setDelegate:self];
787 [self setView:webview_];
789 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
790 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
792 [webview_ setDetectsPhoneNumbers:NO];
794 [webview_ setScalesPageToFit:YES];
796 UIWebDocumentView *document([webview_ _documentView]);
798 // XXX: I think this improves scrolling; the hardcoded-ness sucks
799 [document setTileSize:CGSizeMake(320, 500)];
801 [document setBackgroundColor:[UIColor clearColor]];
803 // XXX: this is terribly (too?) expensive
804 [document setDrawsBackground:NO];
806 WebView *webview([document webView]);
807 WebPreferences *preferences([webview preferences]);
809 // XXX: I have no clue if I actually /want/ this modification
810 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
811 [webview _setLayoutInterval:0];
812 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
813 [preferences _setLayoutInterval:0];
815 [preferences setCacheModel:WebCacheModelDocumentBrowser];
816 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
817 [preferences setOfflineWebApplicationCacheEnabled:YES];
819 if (NSString *agent = [self applicationNameForUserAgent])
820 [webview setApplicationNameForUserAgent:agent];
822 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
823 [webview setShouldUpdateWhileOffscreen:NO];
826 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
827 [document setAllowsMessaging:YES];
828 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
829 [webview _setAllowsMessaging:YES];
832 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
833 scroller_ = [webview_ _scrollView];
835 [scroller_ setDirectionalLockEnabled:YES];
836 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
837 [scroller_ setDelaysContentTouches:NO];
839 [scroller_ setCanCancelContentTouches:YES];
840 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
841 UIScroller *scroller([webview_ _scroller]);
842 scroller_ = (UIScrollView *) scroller;
844 [scroller setDirectionalScrolling:YES];
845 // XXX: we might be better off /not/ setting this on older systems
846 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
847 [scroller setScrollHysteresis:0]; /* 8 */
849 [scroller setThumbDetectionEnabled:NO];
851 // use NO with UIApplicationUseLegacyEvents(YES)
852 [scroller setEventMode:YES];
854 // XXX: this is handled by setBounces, right?
855 //[scroller setAllowsRubberBanding:YES];
858 [scroller_ setFixedBackgroundPattern:YES];
859 [scroller_ setBackgroundColor:[UIColor clearColor]];
860 [scroller_ setClipsSubviews:YES];
862 [scroller_ setBounces:YES];
863 [scroller_ setScrollingEnabled:YES];
864 [scroller_ setShowBackgroundShadow:NO];
866 [self setViewportWidth:width_];
868 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
869 [webview_ insertSubview:table atIndex:0];
871 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
872 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
877 - (void) releaseSubviews {
881 [super releaseSubviews];
884 - (id) initWithWidth:(float)width {
885 return [self initWithWidth:width ofClass:[self class]];
889 return [self initWithWidth:0];
892 - (id) initWithURL:(NSURL *)url {
893 if ((self = [self init]) != nil) {
898 - (void) callFunction:(WebScriptObject *)function {
899 WebThreadLocked lock;
901 WebView *webview([[[self webView] _documentView] webView]);
902 WebFrame *frame([webview mainFrame]);
904 JSGlobalContextRef context([frame globalContext]);
905 JSObjectRef object([function JSObject]);
906 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
909 - (void) reloadButtonClicked {
910 [self reloadURLWithCache:YES];
913 - (void) _customButtonClicked {
914 [self reloadButtonClicked];
917 - (void) customButtonClicked {
919 if (function_ != nil)
920 [self callFunction:function_];
923 [self _customButtonClicked];
926 + (float) defaultWidth {
930 - (void) setNavigationBarStyle:(NSString *)name {
932 if ([name isEqualToString:@"Black"])
933 style = UIBarStyleBlack;
935 style = UIBarStyleDefault;
937 [[[self navigationController] navigationBar] setBarStyle:style];
940 - (void) setNavigationBarTintColor:(UIColor *)color {
941 [[[self navigationController] navigationBar] setTintColor:color];
944 - (void) setBadgeValue:(id)value {
945 [[[self navigationController] tabBarItem] setBadgeValue:value];
948 - (void) setHidesBackButton:(bool)value {
949 [[self navigationItem] setHidesBackButton:value];
950 [self applyLeftButton];
953 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
954 [self setHidesBackButton:[value boolValue]];
957 - (void) dispatchEvent:(NSString *)event {
958 [[self webView] dispatchEvent:event];
961 - (bool) hidesNavigationBar {
962 return hidesNavigationBar_;
965 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
967 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
970 - (void) setHidesNavigationBar:(bool)value {
971 if (hidesNavigationBar_ != value) {
972 hidesNavigationBar_ = value;
973 [self _setHidesNavigationBar:YES animated:YES];
977 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
978 [self setHidesNavigationBar:[value boolValue]];
981 - (void) viewWillAppear:(BOOL)animated {
984 if ([self hidesNavigationBar])
985 [self _setHidesNavigationBar:YES animated:animated];
987 [self dispatchEvent:@"CydiaViewWillAppear"];
988 [super viewWillAppear:animated];
991 - (void) viewDidAppear:(BOOL)animated {
992 [super viewDidAppear:animated];
993 [self dispatchEvent:@"CydiaViewDidAppear"];
996 - (void) viewWillDisappear:(BOOL)animated {
997 [self dispatchEvent:@"CydiaViewWillDisappear"];
998 [super viewWillDisappear:animated];
1000 if ([self hidesNavigationBar])
1001 [self _setHidesNavigationBar:NO animated:animated];
1006 - (void) viewDidDisappear:(BOOL)animated {
1007 [super viewDidDisappear:animated];
1008 [self dispatchEvent:@"CydiaViewDidDisappear"];