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 @implementation WebFrame (Cydia)
58 - (NSString *) description {
59 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[([self provisionalDataSource] ?: [self dataSource]) request] URL] absoluteString]];
64 /* Indirect Delegate {{{ */
65 @interface IndirectDelegate : NSObject {
66 _transient volatile id delegate_;
69 - (void) setDelegate:(id)delegate;
70 - (id) initWithDelegate:(id)delegate;
73 @implementation IndirectDelegate
75 - (void) setDelegate:(id)delegate {
79 - (id) initWithDelegate:(id)delegate {
84 - (IMP) methodForSelector:(SEL)sel {
85 if (IMP method = [super methodForSelector:sel])
87 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
91 - (BOOL) respondsToSelector:(SEL)sel {
92 if ([super respondsToSelector:sel])
95 // XXX: WebThreadCreateNSInvocation returns nil
98 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
101 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
104 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
105 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
109 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
112 if (delegate_ != nil)
113 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
116 // XXX: I fucking hate Apple so very very bad
117 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
120 - (void) forwardInvocation:(NSInvocation *)inv {
121 SEL sel = [inv selector];
122 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
123 [inv invokeWithTarget:delegate_];
129 @implementation CyteWebViewController
132 #include "CyteKit/UCInternal.h"
135 + (void) _initialize {
136 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
138 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
139 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
141 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
142 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
143 else // XXX: this actually might be fast on some older systems: we should look into this
144 CYScrollViewDecelerationRateNormal = 0.998;
149 NSLog(@"[CyteWebViewController dealloc]");
152 if ([loading_ count] != 0)
153 [delegate_ releaseNetworkActivityIndicator];
158 - (NSString *) description {
159 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]];
162 - (CyteWebView *) webView {
163 return (CyteWebView *) [self view];
166 - (NSURL *) URLWithURL:(NSURL *)url {
170 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
172 requestWithURL:[self URLWithURL:url]
174 timeoutInterval:DefaultTimeout_
178 - (void) setRequest:(NSURLRequest *)request {
179 _assert(request_ == nil);
183 - (void) setURL:(NSURL *)url {
184 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]];
187 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
188 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
191 - (void) loadURL:(NSURL *)url {
192 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
195 - (void) loadRequest:(NSURLRequest *)request {
197 NSLog(@"loadRequest:%@", request);
203 WebThreadLocked lock;
204 [[self webView] loadRequest:request];
207 - (void) reloadURLWithCache:(BOOL)cache {
211 NSMutableURLRequest *request([request_ mutableCopy]);
212 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
216 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
217 [self loadRequest:request_];
219 UIAlertView *alert = [[[UIAlertView alloc]
220 initWithTitle:UCLocalize("RESUBMIT_FORM")
223 cancelButtonTitle:UCLocalize("CANCEL")
225 UCLocalize("SUBMIT"),
229 [alert setContext:@"submit"];
235 [self reloadURLWithCache:YES];
238 - (void) reloadData {
242 [self dispatchEvent:@"CydiaReloadData"];
244 [self reloadURLWithCache:YES];
247 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
250 function_ = function;
252 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
255 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
258 function_ = function;
260 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
263 - (void) removeButton {
264 custom_ = [NSNull null];
265 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
268 - (void) scrollToBottomAnimated:(NSNumber *)animated {
269 CGSize size([scroller_ contentSize]);
270 CGPoint offset([scroller_ contentOffset]);
271 CGRect frame([scroller_ frame]);
273 if (size.height - offset.y < frame.size.height + 20.f) {
274 CGRect rect = {{0, size.height-1}, {size.width, 1}};
275 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
279 - (void) _setViewportWidth {
280 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
283 - (void) setViewportWidth:(float)width {
284 width_ = width != 0 ? width : [[self class] defaultWidth];
285 [self _setViewportWidth];
288 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
289 [self setViewportWidth:[width floatValue]];
292 - (void) setViewportWidthOnMainThread:(float)width {
293 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
296 - (void) webViewUpdateViewSettings:(UIWebView *)view {
297 [self _setViewportWidth];
300 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
301 [self dismissModalViewControllerAnimated:YES];
304 - (void) _setupMail:(MFMailComposeViewController *)controller {
307 - (void) _openMailToURL:(NSURL *)url {
308 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
309 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
310 [controller setMailComposeDelegate:self];
312 [controller setMailToURL:url];
314 [self _setupMail:controller];
316 [self presentModalViewController:controller animated:YES];
320 UIApplication *app([UIApplication sharedApplication]);
321 if ([app respondsToSelector:@selector(openURL:asPanel:)])
322 [app openURL:url asPanel:YES];
327 - (bool) _allowJavaScriptPanel {
331 - (bool) allowsNavigationAction {
332 return allowsNavigationAction_;
335 - (void) setAllowsNavigationAction:(bool)value {
336 allowsNavigationAction_ = value;
339 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
340 [self setAllowsNavigationAction:[value boolValue]];
343 - (void) popViewControllerWithNumber:(NSNumber *)value {
344 UINavigationController *navigation([self navigationController]);
345 if ([navigation topViewController] == self)
346 [navigation popViewControllerAnimated:[value boolValue]];
349 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
350 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
351 [self _didFinishLoading];
353 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
356 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
363 if ([frame parentFrame] == nil) {
364 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
365 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
366 [[error localizedDescription] stringByAddingPercentEscapes]
373 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
374 NSURL *url([request URL]);
376 // XXX: filter to internal usage?
377 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
380 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
381 [browser setRequest:request];
385 [page setDelegate:delegate_];
388 [[self navigationItem] setTitle:title_];
390 [[self navigationController] pushViewController:page animated:YES];
392 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
394 [navigation setDelegate:delegate_];
396 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
397 initWithTitle:UCLocalize("CLOSE")
398 style:UIBarButtonItemStylePlain
400 action:@selector(close)
403 [[self navigationController] presentModalViewController:navigation animated:YES];
407 // CyteWebViewDelegate {{{
408 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
410 static Pcre irritating("^(?"
411 ":" "The page at .* displayed insecure content from .*\\."
412 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
415 if (NSString *data = [message objectForKey:@"message"])
416 if (irritating(data))
419 NSLog(@"addMessageToConsole:%@", message);
423 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
425 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
428 if ([frame parentFrame] == nil) {
430 NSURL *url(request == nil ? nil : [request URL]);
432 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
434 [self pushRequest:request asPop:NO];
441 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
442 if ([frame parentFrame] == nil)
443 if (decision == CYWebPolicyDecisionUse)
450 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
452 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
455 NSURL *url([request URL]);
459 if ([frame isEqualToString:@"_open"])
460 [delegate_ openURL:url];
462 NSString *scheme([[url scheme] lowercaseString]);
463 if ([scheme isEqualToString:@"mailto"])
464 [self _openMailToURL:url];
466 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
472 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
475 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
477 NSLog(@"didCommitLoadForFrame:%@", frame);
480 if ([frame parentFrame] == nil) {
485 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
487 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
490 [self _didFailWithError:error forFrame:frame];
493 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
495 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
498 [self _didFailWithError:error forFrame:frame];
501 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
502 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
504 if ([frame parentFrame] == nil) {
508 if (DOMDocument *document = [frame DOMDocument])
509 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
510 for (DOMHTMLBodyElement *body in (id) bodies) {
511 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
515 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
516 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
517 DOMRGBColor *rgb([color getRGBColorValue]);
519 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
520 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
521 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
522 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
524 if (red == 0xc7 && green == 0xce && blue == 0xd5)
525 uic = [UIColor pinStripeColor];
528 colorWithRed:(red / 255)
536 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
541 [self _didFinishLoading];
544 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
545 if ([frame parentFrame] != nil)
550 [[self navigationItem] setTitle:title_];
553 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
555 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
558 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
560 if ([frame parentFrame] == nil) {
566 allowsNavigationAction_ = true;
571 [self setHidesNavigationBar:NO];
573 // XXX: do we still need to do this?
574 [[self navigationItem] setTitle:nil];
577 [self _didStartLoading];
580 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
582 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
588 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
589 return [self _allowJavaScriptPanel];
592 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
593 return [self _allowJavaScriptPanel];
596 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
597 return [self _allowJavaScriptPanel];
600 - (void) webViewClose:(WebView *)view {
606 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
609 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
610 NSString *context([alert context]);
612 if ([context isEqualToString:@"sensitive"]) {
615 sensitive_ = [NSNumber numberWithBool:YES];
619 sensitive_ = [NSNumber numberWithBool:NO];
623 [alert dismissWithClickedButtonIndex:-1 animated:YES];
624 } else if ([context isEqualToString:@"challenge"]) {
625 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
629 NSString *username([[alert textFieldAtIndex:0] text]);
630 NSString *password([[alert textFieldAtIndex:1] text]);
632 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
634 [sender useCredential:credential forAuthenticationChallenge:challenge_];
638 [sender cancelAuthenticationChallenge:challenge_];
646 [alert dismissWithClickedButtonIndex:-1 animated:YES];
647 } else if ([context isEqualToString:@"submit"]) {
648 if (button == [alert cancelButtonIndex]) {
649 } else if (button == [alert firstOtherButtonIndex]) {
650 if (request_ != nil) {
651 WebThreadLocked lock;
652 [[self webView] loadRequest:request_];
656 [alert dismissWithClickedButtonIndex:-1 animated:YES];
660 - (UIBarButtonItemStyle) rightButtonStyle {
661 if (style_ == nil) normal:
662 return UIBarButtonItemStylePlain;
663 else if ([style_ isEqualToString:@"Normal"])
664 return UIBarButtonItemStylePlain;
665 else if ([style_ isEqualToString:@"Highlighted"])
666 return UIBarButtonItemStyleDone;
670 - (UIBarButtonItem *) customButton {
673 else if (custom_ == [NSNull null])
674 return (UIBarButtonItem *) [NSNull null];
676 return [[[UIBarButtonItem alloc]
677 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
678 style:[self rightButtonStyle]
680 action:@selector(customButtonClicked)
684 - (UIBarButtonItem *) leftButton {
685 UINavigationItem *item([self navigationItem]);
686 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
689 if (UINavigationController *navigation = [self navigationController])
690 if ([[navigation parentViewController] modalViewController] == navigation)
691 return [[[UIBarButtonItem alloc]
692 initWithTitle:UCLocalize("CLOSE")
693 style:UIBarButtonItemStylePlain
695 action:@selector(close)
701 - (void) applyLeftButton {
702 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
705 - (UIBarButtonItem *) rightButton {
709 - (void) applyLoadingTitle {
710 [[self navigationItem] setTitle:UCLocalize("LOADING")];
713 - (void) layoutRightButton {
714 [[loadingitem_ view] addSubview:indicator_];
715 [[loadingitem_ view] bringSubviewToFront:indicator_];
718 - (void) applyRightButton {
719 if ([self isLoading]) {
720 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
721 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
723 [indicator_ startAnimating];
724 [self applyLoadingTitle];
726 [indicator_ stopAnimating];
728 UIBarButtonItem *button([self customButton]);
730 button = [self rightButton];
731 else if (button == (UIBarButtonItem *) [NSNull null])
734 [[self navigationItem] setRightBarButtonItem:button animated:YES];
738 - (void) didStartLoading {
739 // Overridden in subclasses.
742 - (void) _didStartLoading {
743 [self applyRightButton];
745 if ([loading_ count] != 1)
748 [delegate_ retainNetworkActivityIndicator];
749 [self didStartLoading];
752 - (void) didFinishLoading {
753 // Overridden in subclasses.
756 - (void) _didFinishLoading {
757 if ([loading_ count] != 0)
760 [self applyRightButton];
761 [[self navigationItem] setTitle:title_];
763 [delegate_ releaseNetworkActivityIndicator];
764 [self didFinishLoading];
768 return [loading_ count] != 0;
771 - (id) initWithWidth:(float)width ofClass:(Class)_class {
772 if ((self = [super init]) != nil) {
776 allowsNavigationAction_ = true;
778 loading_ = [NSMutableSet setWithCapacity:5];
779 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
781 reloaditem_ = [[[UIBarButtonItem alloc]
782 initWithTitle:UCLocalize("RELOAD")
783 style:[self rightButtonStyle]
785 action:@selector(reloadButtonClicked)
788 loadingitem_ = [[[UIBarButtonItem alloc]
790 style:UIBarButtonItemStylePlain
792 action:@selector(reloadButtonClicked)
795 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
796 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
797 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
799 [self applyLeftButton];
800 [self applyRightButton];
804 - (NSString *) applicationNameForUserAgent {
809 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
811 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
812 [webview_ setDelegate:self];
813 [self setView:webview_];
815 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
816 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
818 [webview_ setDetectsPhoneNumbers:NO];
820 [webview_ setScalesPageToFit:YES];
822 UIWebDocumentView *document([webview_ _documentView]);
824 // XXX: I think this improves scrolling; the hardcoded-ness sucks
825 [document setTileSize:CGSizeMake(320, 500)];
827 [document setBackgroundColor:[UIColor clearColor]];
829 // XXX: this is terribly (too?) expensive
830 [document setDrawsBackground:NO];
832 WebView *webview([document webView]);
833 WebPreferences *preferences([webview preferences]);
835 // XXX: I have no clue if I actually /want/ this modification
836 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
837 [webview _setLayoutInterval:0];
838 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
839 [preferences _setLayoutInterval:0];
841 [preferences setCacheModel:WebCacheModelDocumentBrowser];
842 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
843 [preferences setOfflineWebApplicationCacheEnabled:YES];
845 if (NSString *agent = [self applicationNameForUserAgent])
846 [webview setApplicationNameForUserAgent:agent];
848 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
849 [webview setShouldUpdateWhileOffscreen:NO];
852 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
853 [document setAllowsMessaging:YES];
854 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
855 [webview _setAllowsMessaging:YES];
858 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
859 scroller_ = [webview_ _scrollView];
861 [scroller_ setDirectionalLockEnabled:YES];
862 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
863 [scroller_ setDelaysContentTouches:NO];
865 [scroller_ setCanCancelContentTouches:YES];
866 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
867 UIScroller *scroller([webview_ _scroller]);
868 scroller_ = (UIScrollView *) scroller;
870 [scroller setDirectionalScrolling:YES];
871 // XXX: we might be better off /not/ setting this on older systems
872 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
873 [scroller setScrollHysteresis:0]; /* 8 */
875 [scroller setThumbDetectionEnabled:NO];
877 // use NO with UIApplicationUseLegacyEvents(YES)
878 [scroller setEventMode:YES];
880 // XXX: this is handled by setBounces, right?
881 //[scroller setAllowsRubberBanding:YES];
884 [scroller_ setFixedBackgroundPattern:YES];
885 [scroller_ setBackgroundColor:[UIColor clearColor]];
886 [scroller_ setClipsSubviews:YES];
888 [scroller_ setBounces:YES];
889 [scroller_ setScrollingEnabled:YES];
890 [scroller_ setShowBackgroundShadow:NO];
892 [self setViewportWidth:width_];
894 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
895 [table setScrollsToTop:NO];
896 [webview_ insertSubview:table atIndex:0];
898 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
899 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
904 - (void) releaseSubviews {
908 [super releaseSubviews];
911 - (id) initWithWidth:(float)width {
912 return [self initWithWidth:width ofClass:[self class]];
916 return [self initWithWidth:0];
919 - (id) initWithURL:(NSURL *)url {
920 if ((self = [self init]) != nil) {
925 - (void) callFunction:(WebScriptObject *)function {
926 WebThreadLocked lock;
928 WebView *webview([[[self webView] _documentView] webView]);
929 WebFrame *frame([webview mainFrame]);
931 JSGlobalContextRef context([frame globalContext]);
932 JSObjectRef object([function JSObject]);
933 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
936 - (void) reloadButtonClicked {
937 [self reloadURLWithCache:YES];
940 - (void) _customButtonClicked {
941 [self reloadButtonClicked];
944 - (void) customButtonClicked {
946 if (function_ != nil)
947 [self callFunction:function_];
950 [self _customButtonClicked];
953 + (float) defaultWidth {
957 - (void) setNavigationBarStyle:(NSString *)name {
959 if ([name isEqualToString:@"Black"])
960 style = UIBarStyleBlack;
962 style = UIBarStyleDefault;
964 [[[self navigationController] navigationBar] setBarStyle:style];
967 - (void) setNavigationBarTintColor:(UIColor *)color {
968 [[[self navigationController] navigationBar] setTintColor:color];
971 - (void) setBadgeValue:(id)value {
972 [[[self navigationController] tabBarItem] setBadgeValue:value];
975 - (void) setHidesBackButton:(bool)value {
976 [[self navigationItem] setHidesBackButton:value];
977 [self applyLeftButton];
980 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
981 [self setHidesBackButton:[value boolValue]];
984 - (void) dispatchEvent:(NSString *)event {
985 [[self webView] dispatchEvent:event];
988 - (bool) hidesNavigationBar {
989 return hidesNavigationBar_;
992 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
994 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
997 - (void) setHidesNavigationBar:(bool)value {
998 if (hidesNavigationBar_ != value) {
999 hidesNavigationBar_ = value;
1000 [self _setHidesNavigationBar:YES animated:YES];
1004 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1005 [self setHidesNavigationBar:[value boolValue]];
1008 - (void) viewWillAppear:(BOOL)animated {
1011 if ([self hidesNavigationBar])
1012 [self _setHidesNavigationBar:YES animated:animated];
1014 [self dispatchEvent:@"CydiaViewWillAppear"];
1015 [super viewWillAppear:animated];
1018 - (void) viewDidAppear:(BOOL)animated {
1019 [super viewDidAppear:animated];
1020 [self dispatchEvent:@"CydiaViewDidAppear"];
1023 - (void) viewWillDisappear:(BOOL)animated {
1024 [self dispatchEvent:@"CydiaViewWillDisappear"];
1025 [super viewWillDisappear:animated];
1027 if ([self hidesNavigationBar])
1028 [self _setHidesNavigationBar:NO animated:animated];
1033 - (void) viewDidDisappear:(BOOL)animated {
1034 [super viewDidDisappear:animated];
1035 [self dispatchEvent:@"CydiaViewDidDisappear"];