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) {
484 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
486 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
489 [self _didFailWithError:error forFrame:frame];
492 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
494 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
497 [self _didFailWithError:error forFrame:frame];
500 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
501 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
503 if ([frame parentFrame] == nil) {
507 if (DOMDocument *document = [frame DOMDocument])
508 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
509 for (DOMHTMLBodyElement *body in (id) bodies) {
510 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
514 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
515 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
516 DOMRGBColor *rgb([color getRGBColorValue]);
518 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
519 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
520 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
521 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
523 if (red == 0xc7 && green == 0xce && blue == 0xd5)
524 uic = [UIColor pinStripeColor];
527 colorWithRed:(red / 255)
535 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
540 [self _didFinishLoading];
543 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
544 if ([frame parentFrame] != nil)
549 [[self navigationItem] setTitle:title_];
552 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
554 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
557 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
559 if ([frame parentFrame] == nil) {
565 allowsNavigationAction_ = true;
570 [self setHidesNavigationBar:NO];
572 // XXX: do we still need to do this?
573 [[self navigationItem] setTitle:nil];
576 [self _didStartLoading];
579 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
581 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
587 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
588 return [self _allowJavaScriptPanel];
591 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
592 return [self _allowJavaScriptPanel];
595 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
596 return [self _allowJavaScriptPanel];
599 - (void) webViewClose:(WebView *)view {
605 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
608 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
609 NSString *context([alert context]);
611 if ([context isEqualToString:@"sensitive"]) {
614 sensitive_ = [NSNumber numberWithBool:YES];
618 sensitive_ = [NSNumber numberWithBool:NO];
622 [alert dismissWithClickedButtonIndex:-1 animated:YES];
623 } else if ([context isEqualToString:@"challenge"]) {
624 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
628 NSString *username([[alert textFieldAtIndex:0] text]);
629 NSString *password([[alert textFieldAtIndex:1] text]);
631 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
633 [sender useCredential:credential forAuthenticationChallenge:challenge_];
637 [sender cancelAuthenticationChallenge:challenge_];
645 [alert dismissWithClickedButtonIndex:-1 animated:YES];
646 } else if ([context isEqualToString:@"submit"]) {
647 if (button == [alert cancelButtonIndex]) {
648 } else if (button == [alert firstOtherButtonIndex]) {
649 if (request_ != nil) {
650 WebThreadLocked lock;
651 [[self webView] loadRequest:request_];
655 [alert dismissWithClickedButtonIndex:-1 animated:YES];
659 - (UIBarButtonItemStyle) rightButtonStyle {
660 if (style_ == nil) normal:
661 return UIBarButtonItemStylePlain;
662 else if ([style_ isEqualToString:@"Normal"])
663 return UIBarButtonItemStylePlain;
664 else if ([style_ isEqualToString:@"Highlighted"])
665 return UIBarButtonItemStyleDone;
669 - (UIBarButtonItem *) customButton {
672 else if (custom_ == [NSNull null])
673 return (UIBarButtonItem *) [NSNull null];
675 return [[[UIBarButtonItem alloc]
676 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
677 style:[self rightButtonStyle]
679 action:@selector(customButtonClicked)
683 - (UIBarButtonItem *) leftButton {
684 UINavigationItem *item([self navigationItem]);
685 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
688 if (UINavigationController *navigation = [self navigationController])
689 if ([[navigation parentViewController] modalViewController] == navigation)
690 return [[[UIBarButtonItem alloc]
691 initWithTitle:UCLocalize("CLOSE")
692 style:UIBarButtonItemStylePlain
694 action:@selector(close)
700 - (void) applyLeftButton {
701 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
704 - (UIBarButtonItem *) rightButton {
708 - (void) applyLoadingTitle {
709 [[self navigationItem] setTitle:UCLocalize("LOADING")];
712 - (void) layoutRightButton {
713 [[loadingitem_ view] addSubview:indicator_];
714 [[loadingitem_ view] bringSubviewToFront:indicator_];
717 - (void) applyRightButton {
718 if ([self isLoading]) {
719 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
720 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
722 [indicator_ startAnimating];
723 [self applyLoadingTitle];
725 [indicator_ stopAnimating];
727 UIBarButtonItem *button([self customButton]);
729 button = [self rightButton];
730 else if (button == (UIBarButtonItem *) [NSNull null])
733 [[self navigationItem] setRightBarButtonItem:button animated:YES];
737 - (void) didStartLoading {
738 // Overridden in subclasses.
741 - (void) _didStartLoading {
742 [self applyRightButton];
744 if ([loading_ count] != 1)
747 [delegate_ retainNetworkActivityIndicator];
748 [self didStartLoading];
751 - (void) didFinishLoading {
752 // Overridden in subclasses.
755 - (void) _didFinishLoading {
756 if ([loading_ count] != 0)
759 [self applyRightButton];
760 [[self navigationItem] setTitle:title_];
762 [delegate_ releaseNetworkActivityIndicator];
763 [self didFinishLoading];
767 return [loading_ count] != 0;
770 - (id) initWithWidth:(float)width ofClass:(Class)_class {
771 if ((self = [super init]) != nil) {
775 allowsNavigationAction_ = true;
777 loading_ = [NSMutableSet setWithCapacity:5];
778 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
780 reloaditem_ = [[[UIBarButtonItem alloc]
781 initWithTitle:UCLocalize("RELOAD")
782 style:[self rightButtonStyle]
784 action:@selector(reloadButtonClicked)
787 loadingitem_ = [[[UIBarButtonItem alloc]
789 style:UIBarButtonItemStylePlain
791 action:@selector(reloadButtonClicked)
794 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
795 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
796 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
798 [self applyLeftButton];
799 [self applyRightButton];
803 - (NSString *) applicationNameForUserAgent {
808 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
810 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
811 [webview_ setDelegate:self];
812 [self setView:webview_];
814 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
815 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
817 [webview_ setDetectsPhoneNumbers:NO];
819 [webview_ setScalesPageToFit:YES];
821 UIWebDocumentView *document([webview_ _documentView]);
823 // XXX: I think this improves scrolling; the hardcoded-ness sucks
824 [document setTileSize:CGSizeMake(320, 500)];
826 [document setBackgroundColor:[UIColor clearColor]];
828 // XXX: this is terribly (too?) expensive
829 [document setDrawsBackground:NO];
831 WebView *webview([document webView]);
832 WebPreferences *preferences([webview preferences]);
834 // XXX: I have no clue if I actually /want/ this modification
835 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
836 [webview _setLayoutInterval:0];
837 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
838 [preferences _setLayoutInterval:0];
840 [preferences setCacheModel:WebCacheModelDocumentBrowser];
841 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
842 [preferences setOfflineWebApplicationCacheEnabled:YES];
844 if (NSString *agent = [self applicationNameForUserAgent])
845 [webview setApplicationNameForUserAgent:agent];
847 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
848 [webview setShouldUpdateWhileOffscreen:NO];
851 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
852 [document setAllowsMessaging:YES];
853 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
854 [webview _setAllowsMessaging:YES];
857 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
858 scroller_ = [webview_ _scrollView];
860 [scroller_ setDirectionalLockEnabled:YES];
861 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
862 [scroller_ setDelaysContentTouches:NO];
864 [scroller_ setCanCancelContentTouches:YES];
865 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
866 UIScroller *scroller([webview_ _scroller]);
867 scroller_ = (UIScrollView *) scroller;
869 [scroller setDirectionalScrolling:YES];
870 // XXX: we might be better off /not/ setting this on older systems
871 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
872 [scroller setScrollHysteresis:0]; /* 8 */
874 [scroller setThumbDetectionEnabled:NO];
876 // use NO with UIApplicationUseLegacyEvents(YES)
877 [scroller setEventMode:YES];
879 // XXX: this is handled by setBounces, right?
880 //[scroller setAllowsRubberBanding:YES];
883 [scroller_ setFixedBackgroundPattern:YES];
884 [scroller_ setBackgroundColor:[UIColor clearColor]];
885 [scroller_ setClipsSubviews:YES];
887 [scroller_ setBounces:YES];
888 [scroller_ setScrollingEnabled:YES];
889 [scroller_ setShowBackgroundShadow:NO];
891 [self setViewportWidth:width_];
893 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
894 [table setScrollsToTop:NO];
895 [webview_ insertSubview:table atIndex:0];
897 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
898 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
903 - (void) releaseSubviews {
907 [super releaseSubviews];
910 - (id) initWithWidth:(float)width {
911 return [self initWithWidth:width ofClass:[self class]];
915 return [self initWithWidth:0];
918 - (id) initWithURL:(NSURL *)url {
919 if ((self = [self init]) != nil) {
924 - (void) callFunction:(WebScriptObject *)function {
925 WebThreadLocked lock;
927 WebView *webview([[[self webView] _documentView] webView]);
928 WebFrame *frame([webview mainFrame]);
930 JSGlobalContextRef context([frame globalContext]);
931 JSObjectRef object([function JSObject]);
932 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
935 - (void) reloadButtonClicked {
936 [self reloadURLWithCache:YES];
939 - (void) _customButtonClicked {
940 [self reloadButtonClicked];
943 - (void) customButtonClicked {
945 if (function_ != nil)
946 [self callFunction:function_];
949 [self _customButtonClicked];
952 + (float) defaultWidth {
956 - (void) setNavigationBarStyle:(NSString *)name {
958 if ([name isEqualToString:@"Black"])
959 style = UIBarStyleBlack;
961 style = UIBarStyleDefault;
963 [[[self navigationController] navigationBar] setBarStyle:style];
966 - (void) setNavigationBarTintColor:(UIColor *)color {
967 [[[self navigationController] navigationBar] setTintColor:color];
970 - (void) setBadgeValue:(id)value {
971 [[[self navigationController] tabBarItem] setBadgeValue:value];
974 - (void) setHidesBackButton:(bool)value {
975 [[self navigationItem] setHidesBackButton:value];
976 [self applyLeftButton];
979 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
980 [self setHidesBackButton:[value boolValue]];
983 - (void) dispatchEvent:(NSString *)event {
984 [[self webView] dispatchEvent:event];
987 - (bool) hidesNavigationBar {
988 return hidesNavigationBar_;
991 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
993 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
996 - (void) setHidesNavigationBar:(bool)value {
997 if (hidesNavigationBar_ != value) {
998 hidesNavigationBar_ = value;
999 [self _setHidesNavigationBar:YES animated:YES];
1003 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1004 [self setHidesNavigationBar:[value boolValue]];
1007 - (void) viewWillAppear:(BOOL)animated {
1010 if ([self hidesNavigationBar])
1011 [self _setHidesNavigationBar:YES animated:animated];
1013 [self dispatchEvent:@"CydiaViewWillAppear"];
1014 [super viewWillAppear:animated];
1017 - (void) viewDidAppear:(BOOL)animated {
1018 [super viewDidAppear:animated];
1019 [self dispatchEvent:@"CydiaViewDidAppear"];
1022 - (void) viewWillDisappear:(BOOL)animated {
1023 [self dispatchEvent:@"CydiaViewWillDisappear"];
1024 [super viewWillDisappear:animated];
1026 if ([self hidesNavigationBar])
1027 [self _setHidesNavigationBar:NO animated:animated];
1032 - (void) viewDidDisappear:(BOOL)animated {
1033 [super viewDidDisappear:animated];
1034 [self dispatchEvent:@"CydiaViewDidDisappear"];