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;
147 - (bool) retainsNetworkActivityIndicator {
153 NSLog(@"[CyteWebViewController dealloc]");
156 if ([loading_ count] != 0)
157 if ([self retainsNetworkActivityIndicator])
158 [delegate_ releaseNetworkActivityIndicator];
163 - (NSString *) description {
164 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]];
167 - (CyteWebView *) webView {
168 return (CyteWebView *) [self view];
171 - (NSURL *) URLWithURL:(NSURL *)url {
175 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
177 requestWithURL:[self URLWithURL:url]
179 timeoutInterval:DefaultTimeout_
183 - (void) setRequest:(NSURLRequest *)request {
184 _assert(request_ == nil);
188 - (void) setURL:(NSURL *)url {
189 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]];
192 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
193 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
196 - (void) loadURL:(NSURL *)url {
197 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
200 - (void) loadRequest:(NSURLRequest *)request {
202 NSLog(@"loadRequest:%@", request);
208 WebThreadLocked lock;
209 [[self webView] loadRequest:request];
212 - (void) reloadURLWithCache:(BOOL)cache {
216 NSMutableURLRequest *request([request_ mutableCopy]);
217 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
221 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
222 [self loadRequest:request_];
224 UIAlertView *alert = [[[UIAlertView alloc]
225 initWithTitle:UCLocalize("RESUBMIT_FORM")
228 cancelButtonTitle:UCLocalize("CANCEL")
230 UCLocalize("SUBMIT"),
234 [alert setContext:@"submit"];
240 [self reloadURLWithCache:YES];
243 - (void) reloadData {
247 [self dispatchEvent:@"CydiaReloadData"];
249 [self reloadURLWithCache:YES];
252 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
255 function_ = function;
257 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
260 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
263 function_ = function;
265 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
268 - (void) removeButton {
269 custom_ = [NSNull null];
270 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
273 - (void) scrollToBottomAnimated:(NSNumber *)animated {
274 CGSize size([scroller_ contentSize]);
275 CGPoint offset([scroller_ contentOffset]);
276 CGRect frame([scroller_ frame]);
278 if (size.height - offset.y < frame.size.height + 20.f) {
279 CGRect rect = {{0, size.height-1}, {size.width, 1}};
280 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
284 - (void) _setViewportWidth {
285 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
288 - (void) setViewportWidth:(float)width {
289 width_ = width != 0 ? width : [[self class] defaultWidth];
290 [self _setViewportWidth];
293 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
294 [self setViewportWidth:[width floatValue]];
297 - (void) setViewportWidthOnMainThread:(float)width {
298 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
301 - (void) webViewUpdateViewSettings:(UIWebView *)view {
302 [self _setViewportWidth];
305 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
306 [self dismissModalViewControllerAnimated:YES];
309 - (void) _setupMail:(MFMailComposeViewController *)controller {
312 - (void) _openMailToURL:(NSURL *)url {
313 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
314 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
315 [controller setMailComposeDelegate:self];
317 [controller setMailToURL:url];
319 [self _setupMail:controller];
321 [self presentModalViewController:controller animated:YES];
325 UIApplication *app([UIApplication sharedApplication]);
326 if ([app respondsToSelector:@selector(openURL:asPanel:)])
327 [app openURL:url asPanel:YES];
332 - (bool) _allowJavaScriptPanel {
336 - (bool) allowsNavigationAction {
337 return allowsNavigationAction_;
340 - (void) setAllowsNavigationAction:(bool)value {
341 allowsNavigationAction_ = value;
344 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
345 [self setAllowsNavigationAction:[value boolValue]];
348 - (void) popViewControllerWithNumber:(NSNumber *)value {
349 UINavigationController *navigation([self navigationController]);
350 if ([navigation topViewController] == self)
351 [navigation popViewControllerAnimated:[value boolValue]];
354 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
355 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
356 [self _didFinishLoading];
358 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
361 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
366 if ([frame parentFrame] == nil) {
367 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
368 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
369 [[error localizedDescription] stringByAddingPercentEscapes]
376 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
377 NSURL *url([request URL]);
379 // XXX: filter to internal usage?
380 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
383 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
384 [browser setRequest:request];
388 [page setDelegate:delegate_];
391 [[self navigationItem] setTitle:title_];
393 [[self navigationController] pushViewController:page animated:YES];
395 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
397 [navigation setDelegate:delegate_];
399 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
400 initWithTitle:UCLocalize("CLOSE")
401 style:UIBarButtonItemStylePlain
403 action:@selector(close)
406 [[self navigationController] presentModalViewController:navigation animated:YES];
410 // CyteWebViewDelegate {{{
411 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
413 static Pcre irritating("^(?"
414 ":" "The page at .* displayed insecure content from .*\\."
415 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
418 if (NSString *data = [message objectForKey:@"message"])
419 if (irritating(data))
422 NSLog(@"addMessageToConsole:%@", message);
426 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
428 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
431 if ([frame parentFrame] == nil) {
433 NSURL *url(request == nil ? nil : [request URL]);
435 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
437 [self pushRequest:request asPop:NO];
444 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
445 if ([frame parentFrame] == nil)
446 if (decision == CYWebPolicyDecisionUse)
451 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
453 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
456 NSURL *url([request URL]);
460 if ([frame isEqualToString:@"_open"])
461 [delegate_ openURL:url];
463 NSString *scheme([[url scheme] lowercaseString]);
464 if ([scheme isEqualToString:@"mailto"])
465 [self _openMailToURL:url];
467 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
473 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
476 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
478 NSLog(@"didCommitLoadForFrame:%@", frame);
481 if ([frame parentFrame] == nil) {
486 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
488 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
491 [self _didFailWithError:error forFrame:frame];
494 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
496 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
499 [self _didFailWithError:error forFrame:frame];
502 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
503 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
505 if ([frame parentFrame] == nil) {
506 if (DOMDocument *document = [frame DOMDocument])
507 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
508 for (DOMHTMLBodyElement *body in (id) bodies) {
509 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
513 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
514 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
515 DOMRGBColor *rgb([color getRGBColorValue]);
517 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
518 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
519 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
520 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
522 if (red == 0xc7 && green == 0xce && blue == 0xd5)
523 uic = [UIColor pinStripeColor];
526 colorWithRed:(red / 255)
534 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
539 [self _didFinishLoading];
542 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
543 if ([frame parentFrame] != nil)
548 [[self navigationItem] setTitle:title_];
551 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
553 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
556 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
558 if ([frame parentFrame] == nil) {
564 allowsNavigationAction_ = true;
566 [self setHidesNavigationBar:NO];
567 [self setScrollAlwaysBounceVertical:true];
568 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
570 // XXX: do we still need to do this?
571 [[self navigationItem] setTitle:nil];
574 [self _didStartLoading];
577 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
579 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
585 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
586 return [self _allowJavaScriptPanel];
589 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
590 return [self _allowJavaScriptPanel];
593 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
594 return [self _allowJavaScriptPanel];
597 - (void) webViewClose:(WebView *)view {
603 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
606 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
607 NSString *context([alert context]);
609 if ([context isEqualToString:@"sensitive"]) {
612 sensitive_ = [NSNumber numberWithBool:YES];
616 sensitive_ = [NSNumber numberWithBool:NO];
620 [alert dismissWithClickedButtonIndex:-1 animated:YES];
621 } else if ([context isEqualToString:@"challenge"]) {
622 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
626 NSString *username([[alert textFieldAtIndex:0] text]);
627 NSString *password([[alert textFieldAtIndex:1] text]);
629 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
631 [sender useCredential:credential forAuthenticationChallenge:challenge_];
635 [sender cancelAuthenticationChallenge:challenge_];
643 [alert dismissWithClickedButtonIndex:-1 animated:YES];
644 } else if ([context isEqualToString:@"submit"]) {
645 if (button == [alert cancelButtonIndex]) {
646 } else if (button == [alert firstOtherButtonIndex]) {
647 if (request_ != nil) {
648 WebThreadLocked lock;
649 [[self webView] loadRequest:request_];
653 [alert dismissWithClickedButtonIndex:-1 animated:YES];
657 - (UIBarButtonItemStyle) rightButtonStyle {
658 if (style_ == nil) normal:
659 return UIBarButtonItemStylePlain;
660 else if ([style_ isEqualToString:@"Normal"])
661 return UIBarButtonItemStylePlain;
662 else if ([style_ isEqualToString:@"Highlighted"])
663 return UIBarButtonItemStyleDone;
667 - (UIBarButtonItem *) customButton {
670 else if (custom_ == [NSNull null])
671 return (UIBarButtonItem *) [NSNull null];
673 return [[[UIBarButtonItem alloc]
674 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
675 style:[self rightButtonStyle]
677 action:@selector(customButtonClicked)
681 - (UIBarButtonItem *) leftButton {
682 UINavigationItem *item([self navigationItem]);
683 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
686 if (UINavigationController *navigation = [self navigationController])
687 if ([[navigation parentViewController] modalViewController] == navigation)
688 return [[[UIBarButtonItem alloc]
689 initWithTitle:UCLocalize("CLOSE")
690 style:UIBarButtonItemStylePlain
692 action:@selector(close)
698 - (void) applyLeftButton {
699 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
702 - (UIBarButtonItem *) rightButton {
706 - (void) applyLoadingTitle {
707 [[self navigationItem] setTitle:UCLocalize("LOADING")];
710 - (void) layoutRightButton {
711 [[loadingitem_ view] addSubview:indicator_];
712 [[loadingitem_ view] bringSubviewToFront:indicator_];
715 - (void) applyRightButton {
716 if ([self isLoading]) {
717 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
718 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
720 [indicator_ startAnimating];
721 [self applyLoadingTitle];
723 [indicator_ stopAnimating];
725 UIBarButtonItem *button([self customButton]);
727 button = [self rightButton];
728 else if (button == (UIBarButtonItem *) [NSNull null])
731 [[self navigationItem] setRightBarButtonItem:button animated:YES];
735 - (void) didStartLoading {
736 // Overridden in subclasses.
739 - (void) _didStartLoading {
740 [self applyRightButton];
742 if ([loading_ count] != 1)
745 if ([self retainsNetworkActivityIndicator])
746 [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 if ([self retainsNetworkActivityIndicator])
763 [delegate_ releaseNetworkActivityIndicator];
765 [self didFinishLoading];
769 return [loading_ count] != 0;
772 - (id) initWithWidth:(float)width ofClass:(Class)_class {
773 if ((self = [super init]) != nil) {
777 allowsNavigationAction_ = true;
779 loading_ = [NSMutableSet setWithCapacity:5];
780 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
782 reloaditem_ = [[[UIBarButtonItem alloc]
783 initWithTitle:UCLocalize("RELOAD")
784 style:[self rightButtonStyle]
786 action:@selector(reloadButtonClicked)
789 loadingitem_ = [[[UIBarButtonItem alloc]
791 style:UIBarButtonItemStylePlain
793 action:@selector(reloadButtonClicked)
796 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
797 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
798 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
800 [self applyLeftButton];
801 [self applyRightButton];
805 - (NSString *) applicationNameForUserAgent {
810 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
812 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
813 [webview_ setDelegate:self];
814 [self setView:webview_];
816 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
817 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
819 [webview_ setDetectsPhoneNumbers:NO];
821 [webview_ setScalesPageToFit:YES];
823 UIWebDocumentView *document([webview_ _documentView]);
825 // XXX: I think this improves scrolling; the hardcoded-ness sucks
826 [document setTileSize:CGSizeMake(320, 500)];
828 [document setBackgroundColor:[UIColor clearColor]];
830 // XXX: this is terribly (too?) expensive
831 [document setDrawsBackground:NO];
833 WebView *webview([document webView]);
834 WebPreferences *preferences([webview preferences]);
836 // XXX: I have no clue if I actually /want/ this modification
837 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
838 [webview _setLayoutInterval:0];
839 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
840 [preferences _setLayoutInterval:0];
842 [preferences setCacheModel:WebCacheModelDocumentBrowser];
843 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
844 [preferences setOfflineWebApplicationCacheEnabled:YES];
846 if (NSString *agent = [self applicationNameForUserAgent])
847 [webview setApplicationNameForUserAgent:agent];
849 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
850 [webview setShouldUpdateWhileOffscreen:NO];
853 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
854 [document setAllowsMessaging:YES];
855 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
856 [webview _setAllowsMessaging:YES];
859 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
860 scroller_ = [webview_ _scrollView];
862 [scroller_ setDirectionalLockEnabled:YES];
863 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
864 [scroller_ setDelaysContentTouches:NO];
866 [scroller_ setCanCancelContentTouches:YES];
867 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
868 UIScroller *scroller([webview_ _scroller]);
869 scroller_ = (UIScrollView *) scroller;
871 [scroller setDirectionalScrolling:YES];
872 // XXX: we might be better off /not/ setting this on older systems
873 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
874 [scroller setScrollHysteresis:0]; /* 8 */
876 [scroller setThumbDetectionEnabled:NO];
878 // use NO with UIApplicationUseLegacyEvents(YES)
879 [scroller setEventMode:YES];
881 // XXX: this is handled by setBounces, right?
882 //[scroller setAllowsRubberBanding:YES];
885 [scroller_ setFixedBackgroundPattern:YES];
886 [scroller_ setBackgroundColor:[UIColor clearColor]];
887 [scroller_ setClipsSubviews:YES];
889 [scroller_ setBounces:YES];
890 [scroller_ setScrollingEnabled:YES];
891 [scroller_ setShowBackgroundShadow:NO];
893 [self setViewportWidth:width_];
895 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
896 [table setScrollsToTop:NO];
897 [webview_ insertSubview:table atIndex:0];
899 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
900 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
905 - (void) releaseSubviews {
909 [super releaseSubviews];
912 - (id) initWithWidth:(float)width {
913 return [self initWithWidth:width ofClass:[self class]];
917 return [self initWithWidth:0];
920 - (id) initWithURL:(NSURL *)url {
921 if ((self = [self init]) != nil) {
926 - (void) callFunction:(WebScriptObject *)function {
927 WebThreadLocked lock;
929 WebView *webview([[[self webView] _documentView] webView]);
930 WebFrame *frame([webview mainFrame]);
932 JSGlobalContextRef context([frame globalContext]);
933 JSObjectRef object([function JSObject]);
934 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
937 - (void) reloadButtonClicked {
938 [self reloadURLWithCache:YES];
941 - (void) _customButtonClicked {
942 [self reloadButtonClicked];
945 - (void) customButtonClicked {
947 if (function_ != nil)
948 [self callFunction:function_];
951 [self _customButtonClicked];
954 + (float) defaultWidth {
958 - (void) setNavigationBarStyle:(NSString *)name {
960 if ([name isEqualToString:@"Black"])
961 style = UIBarStyleBlack;
963 style = UIBarStyleDefault;
965 [[[self navigationController] navigationBar] setBarStyle:style];
968 - (void) setNavigationBarTintColor:(UIColor *)color {
969 [[[self navigationController] navigationBar] setTintColor:color];
972 - (void) setBadgeValue:(id)value {
973 [[[self navigationController] tabBarItem] setBadgeValue:value];
976 - (void) setHidesBackButton:(bool)value {
977 [[self navigationItem] setHidesBackButton:value];
978 [self applyLeftButton];
981 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
982 [self setHidesBackButton:[value boolValue]];
985 - (void) dispatchEvent:(NSString *)event {
986 [[self webView] dispatchEvent:event];
989 - (bool) hidesNavigationBar {
990 return hidesNavigationBar_;
993 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
995 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
998 - (void) setHidesNavigationBar:(bool)value {
999 if (hidesNavigationBar_ != value) {
1000 hidesNavigationBar_ = value;
1001 [self _setHidesNavigationBar:YES animated:YES];
1005 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1006 [self setHidesNavigationBar:[value boolValue]];
1009 - (void) setScrollAlwaysBounceVertical:(bool)value {
1010 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1011 UIScrollView *scroller([webview_ _scrollView]);
1012 [scroller setAlwaysBounceVertical:value];
1013 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1014 //UIScroller *scroller([webview_ _scroller]);
1015 // XXX: I am sad here.
1019 - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1020 [self setScrollAlwaysBounceVertical:[value boolValue]];
1023 - (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style {
1024 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1025 UIScrollView *scroller([webview_ _scrollView]);
1026 [scroller setIndicatorStyle:style];
1027 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1028 UIScroller *scroller([webview_ _scroller]);
1029 [scroller setScrollerIndicatorStyle:style];
1033 - (void) setScrollIndicatorStyleWithName:(NSString *)style {
1034 UIScrollViewIndicatorStyle value;
1037 else if ([style isEqualToString:@"default"])
1038 value = UIScrollViewIndicatorStyleDefault;
1039 else if ([style isEqualToString:@"black"])
1040 value = UIScrollViewIndicatorStyleBlack;
1041 else if ([style isEqualToString:@"white"])
1042 value = UIScrollViewIndicatorStyleWhite;
1045 [self setScrollIndicatorStyle:value];
1048 - (void) viewWillAppear:(BOOL)animated {
1051 if ([self hidesNavigationBar])
1052 [self _setHidesNavigationBar:YES animated:animated];
1054 [self dispatchEvent:@"CydiaViewWillAppear"];
1055 [super viewWillAppear:animated];
1058 - (void) viewDidAppear:(BOOL)animated {
1059 [super viewDidAppear:animated];
1060 [self dispatchEvent:@"CydiaViewDidAppear"];
1063 - (void) viewWillDisappear:(BOOL)animated {
1064 [self dispatchEvent:@"CydiaViewWillDisappear"];
1065 [super viewWillDisappear:animated];
1067 if ([self hidesNavigationBar])
1068 [self _setHidesNavigationBar:NO animated:animated];
1073 - (void) viewDidDisappear:(BOOL)animated {
1074 [super viewDidDisappear:animated];
1075 [self dispatchEvent:@"CydiaViewDidDisappear"];