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 {
151 - (void) releaseNetworkActivityIndicator {
152 if ([loading_ count] != 0) {
153 [loading_ removeAllObjects];
155 if ([self retainsNetworkActivityIndicator])
156 [delegate_ releaseNetworkActivityIndicator];
162 NSLog(@"[CyteWebViewController dealloc]");
165 [self releaseNetworkActivityIndicator];
170 - (NSString *) description {
171 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]];
174 - (CyteWebView *) webView {
175 return (CyteWebView *) [self view];
178 - (NSURL *) URLWithURL:(NSURL *)url {
182 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
184 requestWithURL:[self URLWithURL:url]
186 timeoutInterval:DefaultTimeout_
190 - (void) setRequest:(NSURLRequest *)request {
191 _assert(request_ == nil);
195 - (void) setURL:(NSURL *)url {
196 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]];
199 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
200 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
203 - (void) loadURL:(NSURL *)url {
204 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
207 - (void) loadRequest:(NSURLRequest *)request {
209 NSLog(@"loadRequest:%@", request);
215 WebThreadLocked lock;
216 [[self webView] loadRequest:request];
219 - (void) reloadURLWithCache:(BOOL)cache {
223 NSMutableURLRequest *request([request_ mutableCopy]);
224 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
228 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
229 [self loadRequest:request_];
231 UIAlertView *alert = [[[UIAlertView alloc]
232 initWithTitle:UCLocalize("RESUBMIT_FORM")
235 cancelButtonTitle:UCLocalize("CANCEL")
237 UCLocalize("SUBMIT"),
241 [alert setContext:@"submit"];
247 [self reloadURLWithCache:YES];
250 - (void) reloadData {
254 [self dispatchEvent:@"CydiaReloadData"];
256 [self reloadURLWithCache:YES];
259 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
262 function_ = function;
264 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
267 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
270 function_ = function;
272 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
275 - (void) removeButton {
276 custom_ = [NSNull null];
277 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
280 - (void) scrollToBottomAnimated:(NSNumber *)animated {
281 CGSize size([scroller_ contentSize]);
282 CGPoint offset([scroller_ contentOffset]);
283 CGRect frame([scroller_ frame]);
285 if (size.height - offset.y < frame.size.height + 20.f) {
286 CGRect rect = {{0, size.height-1}, {size.width, 1}};
287 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
291 - (void) _setViewportWidth {
292 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
295 - (void) setViewportWidth:(float)width {
296 width_ = width != 0 ? width : [[self class] defaultWidth];
297 [self _setViewportWidth];
300 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
301 [self setViewportWidth:[width floatValue]];
304 - (void) setViewportWidthOnMainThread:(float)width {
305 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
308 - (void) webViewUpdateViewSettings:(UIWebView *)view {
309 [self _setViewportWidth];
312 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
313 [self dismissModalViewControllerAnimated:YES];
316 - (void) _setupMail:(MFMailComposeViewController *)controller {
319 - (void) _openMailToURL:(NSURL *)url {
320 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
321 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
322 [controller setMailComposeDelegate:self];
324 [controller setMailToURL:url];
326 [self _setupMail:controller];
328 [self presentModalViewController:controller animated:YES];
332 UIApplication *app([UIApplication sharedApplication]);
333 if ([app respondsToSelector:@selector(openURL:asPanel:)])
334 [app openURL:url asPanel:YES];
339 - (bool) _allowJavaScriptPanel {
343 - (bool) allowsNavigationAction {
344 return allowsNavigationAction_;
347 - (void) setAllowsNavigationAction:(bool)value {
348 allowsNavigationAction_ = value;
351 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
352 [self setAllowsNavigationAction:[value boolValue]];
355 - (void) popViewControllerWithNumber:(NSNumber *)value {
356 UINavigationController *navigation([self navigationController]);
357 if ([navigation topViewController] == self)
358 [navigation popViewControllerAnimated:[value boolValue]];
361 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
362 NSValue *object([NSValue valueWithNonretainedObject:frame]);
363 if (![loading_ containsObject:object])
365 [loading_ removeObject:object];
367 [self _didFinishLoading];
369 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
372 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
377 if ([frame parentFrame] == nil) {
378 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
379 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
380 [[error localizedDescription] stringByAddingPercentEscapes]
387 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
388 NSURL *url([request URL]);
390 // XXX: filter to internal usage?
391 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
394 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
395 [browser setRequest:request];
399 [page setDelegate:delegate_];
402 [[self navigationItem] setTitle:title_];
404 [[self navigationController] pushViewController:page animated:YES];
406 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
408 [navigation setDelegate:delegate_];
410 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
411 initWithTitle:UCLocalize("CLOSE")
412 style:UIBarButtonItemStylePlain
414 action:@selector(close)
417 [[self navigationController] presentModalViewController:navigation animated:YES];
421 // CyteWebViewDelegate {{{
422 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
424 static Pcre irritating("^(?"
425 ":" "The page at .* displayed insecure content from .*\\."
426 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
429 if (NSString *data = [message objectForKey:@"message"])
430 if (irritating(data))
433 NSLog(@"addMessageToConsole:%@", message);
437 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
439 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
442 if ([frame parentFrame] == nil) {
444 NSURL *url(request == nil ? nil : [request URL]);
446 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
448 [self pushRequest:request asPop:NO];
455 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
456 if ([frame parentFrame] == nil)
457 if (decision == CYWebPolicyDecisionUse)
462 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
464 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
467 NSURL *url([request URL]);
471 if ([frame isEqualToString:@"_open"])
472 [delegate_ openURL:url];
474 NSString *scheme([[url scheme] lowercaseString]);
475 if ([scheme isEqualToString:@"mailto"])
476 [self _openMailToURL:url];
478 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
484 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
487 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
489 NSLog(@"didCommitLoadForFrame:%@", frame);
492 if ([frame parentFrame] == nil) {
497 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
499 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
502 [self _didFailWithError:error forFrame:frame];
505 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
507 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
510 [self _didFailWithError:error forFrame:frame];
513 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
514 NSValue *object([NSValue valueWithNonretainedObject:frame]);
515 if (![loading_ containsObject:object])
517 [loading_ removeObject:object];
519 if ([frame parentFrame] == nil) {
520 if (DOMDocument *document = [frame DOMDocument])
521 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
522 for (DOMHTMLBodyElement *body in (id) bodies) {
523 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
527 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
528 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
529 DOMRGBColor *rgb([color getRGBColorValue]);
531 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
532 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
533 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
534 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
536 if (red == 0xc7 && green == 0xce && blue == 0xd5)
537 uic = [UIColor pinStripeColor];
540 colorWithRed:(red / 255)
548 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
553 [self _didFinishLoading];
556 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
557 if ([frame parentFrame] != nil)
562 [[self navigationItem] setTitle:title_];
565 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
567 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
570 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
572 if ([frame parentFrame] == nil) {
578 allowsNavigationAction_ = true;
580 [self setHidesNavigationBar:NO];
581 [self setScrollAlwaysBounceVertical:true];
582 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
584 // XXX: do we still need to do this?
585 [[self navigationItem] setTitle:nil];
588 [self _didStartLoading];
591 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
593 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
599 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
600 return [self _allowJavaScriptPanel];
603 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
604 return [self _allowJavaScriptPanel];
607 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
608 return [self _allowJavaScriptPanel];
611 - (void) webViewClose:(WebView *)view {
617 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
620 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
621 NSString *context([alert context]);
623 if ([context isEqualToString:@"sensitive"]) {
626 sensitive_ = [NSNumber numberWithBool:YES];
630 sensitive_ = [NSNumber numberWithBool:NO];
634 [alert dismissWithClickedButtonIndex:-1 animated:YES];
635 } else if ([context isEqualToString:@"challenge"]) {
636 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
640 NSString *username([[alert textFieldAtIndex:0] text]);
641 NSString *password([[alert textFieldAtIndex:1] text]);
643 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
645 [sender useCredential:credential forAuthenticationChallenge:challenge_];
649 [sender cancelAuthenticationChallenge:challenge_];
657 [alert dismissWithClickedButtonIndex:-1 animated:YES];
658 } else if ([context isEqualToString:@"submit"]) {
659 if (button == [alert cancelButtonIndex]) {
660 } else if (button == [alert firstOtherButtonIndex]) {
661 if (request_ != nil) {
662 WebThreadLocked lock;
663 [[self webView] loadRequest:request_];
667 [alert dismissWithClickedButtonIndex:-1 animated:YES];
671 - (UIBarButtonItemStyle) rightButtonStyle {
672 if (style_ == nil) normal:
673 return UIBarButtonItemStylePlain;
674 else if ([style_ isEqualToString:@"Normal"])
675 return UIBarButtonItemStylePlain;
676 else if ([style_ isEqualToString:@"Highlighted"])
677 return UIBarButtonItemStyleDone;
681 - (UIBarButtonItem *) customButton {
684 else if (custom_ == [NSNull null])
685 return (UIBarButtonItem *) [NSNull null];
687 return [[[UIBarButtonItem alloc]
688 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
689 style:[self rightButtonStyle]
691 action:@selector(customButtonClicked)
695 - (UIBarButtonItem *) leftButton {
696 UINavigationItem *item([self navigationItem]);
697 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
700 if (UINavigationController *navigation = [self navigationController])
701 if ([[navigation parentViewController] modalViewController] == navigation)
702 return [[[UIBarButtonItem alloc]
703 initWithTitle:UCLocalize("CLOSE")
704 style:UIBarButtonItemStylePlain
706 action:@selector(close)
712 - (void) applyLeftButton {
713 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
716 - (UIBarButtonItem *) rightButton {
720 - (void) applyLoadingTitle {
721 [[self navigationItem] setTitle:UCLocalize("LOADING")];
724 - (void) layoutRightButton {
725 [[loadingitem_ view] addSubview:indicator_];
726 [[loadingitem_ view] bringSubviewToFront:indicator_];
729 - (void) applyRightButton {
730 if ([self isLoading]) {
731 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
732 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
734 [indicator_ startAnimating];
735 [self applyLoadingTitle];
737 [indicator_ stopAnimating];
739 UIBarButtonItem *button([self customButton]);
741 button = [self rightButton];
742 else if (button == (UIBarButtonItem *) [NSNull null])
745 [[self navigationItem] setRightBarButtonItem:button animated:YES];
749 - (void) didStartLoading {
750 // Overridden in subclasses.
753 - (void) _didStartLoading {
754 [self applyRightButton];
756 if ([loading_ count] != 1)
759 if ([self retainsNetworkActivityIndicator])
760 [delegate_ retainNetworkActivityIndicator];
762 [self didStartLoading];
765 - (void) didFinishLoading {
766 // Overridden in subclasses.
769 - (void) _didFinishLoading {
770 if ([loading_ count] != 0)
773 [self applyRightButton];
774 [[self navigationItem] setTitle:title_];
776 if ([self retainsNetworkActivityIndicator])
777 [delegate_ releaseNetworkActivityIndicator];
779 [self didFinishLoading];
783 return [loading_ count] != 0;
786 - (id) initWithWidth:(float)width ofClass:(Class)_class {
787 if ((self = [super init]) != nil) {
791 allowsNavigationAction_ = true;
793 loading_ = [NSMutableSet setWithCapacity:5];
794 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
796 reloaditem_ = [[[UIBarButtonItem alloc]
797 initWithTitle:UCLocalize("RELOAD")
798 style:[self rightButtonStyle]
800 action:@selector(reloadButtonClicked)
803 loadingitem_ = [[[UIBarButtonItem alloc]
805 style:UIBarButtonItemStylePlain
807 action:@selector(reloadButtonClicked)
810 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
811 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
812 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
814 [self applyLeftButton];
815 [self applyRightButton];
819 - (NSString *) applicationNameForUserAgent {
824 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
826 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
827 [webview_ setDelegate:self];
828 [self setView:webview_];
830 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
831 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
833 [webview_ setDetectsPhoneNumbers:NO];
835 [webview_ setScalesPageToFit:YES];
837 UIWebDocumentView *document([webview_ _documentView]);
839 // XXX: I think this improves scrolling; the hardcoded-ness sucks
840 [document setTileSize:CGSizeMake(320, 500)];
842 [document setBackgroundColor:[UIColor clearColor]];
844 // XXX: this is terribly (too?) expensive
845 [document setDrawsBackground:NO];
847 WebView *webview([document webView]);
848 WebPreferences *preferences([webview preferences]);
850 // XXX: I have no clue if I actually /want/ this modification
851 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
852 [webview _setLayoutInterval:0];
853 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
854 [preferences _setLayoutInterval:0];
856 [preferences setCacheModel:WebCacheModelDocumentBrowser];
857 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
858 [preferences setOfflineWebApplicationCacheEnabled:YES];
860 if (NSString *agent = [self applicationNameForUserAgent])
861 [webview setApplicationNameForUserAgent:agent];
863 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
864 [webview setShouldUpdateWhileOffscreen:NO];
867 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
868 [document setAllowsMessaging:YES];
869 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
870 [webview _setAllowsMessaging:YES];
873 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
874 scroller_ = [webview_ _scrollView];
876 [scroller_ setDirectionalLockEnabled:YES];
877 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
878 [scroller_ setDelaysContentTouches:NO];
880 [scroller_ setCanCancelContentTouches:YES];
881 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
882 UIScroller *scroller([webview_ _scroller]);
883 scroller_ = (UIScrollView *) scroller;
885 [scroller setDirectionalScrolling:YES];
886 // XXX: we might be better off /not/ setting this on older systems
887 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
888 [scroller setScrollHysteresis:0]; /* 8 */
890 [scroller setThumbDetectionEnabled:NO];
892 // use NO with UIApplicationUseLegacyEvents(YES)
893 [scroller setEventMode:YES];
895 // XXX: this is handled by setBounces, right?
896 //[scroller setAllowsRubberBanding:YES];
899 [scroller_ setFixedBackgroundPattern:YES];
900 [scroller_ setBackgroundColor:[UIColor clearColor]];
901 [scroller_ setClipsSubviews:YES];
903 [scroller_ setBounces:YES];
904 [scroller_ setScrollingEnabled:YES];
905 [scroller_ setShowBackgroundShadow:NO];
907 [self setViewportWidth:width_];
909 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
910 [table setScrollsToTop:NO];
911 [webview_ insertSubview:table atIndex:0];
913 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
914 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
919 - (void) releaseSubviews {
923 [self releaseNetworkActivityIndicator];
925 [super releaseSubviews];
928 - (id) initWithWidth:(float)width {
929 return [self initWithWidth:width ofClass:[self class]];
933 return [self initWithWidth:0];
936 - (id) initWithURL:(NSURL *)url {
937 if ((self = [self init]) != nil) {
942 - (id) initWithRequest:(NSURLRequest *)request {
943 if ((self = [self init]) != nil) {
944 [self setRequest:request];
948 - (void) callFunction:(WebScriptObject *)function {
949 WebThreadLocked lock;
951 WebView *webview([[[self webView] _documentView] webView]);
952 WebFrame *frame([webview mainFrame]);
954 JSGlobalContextRef context([frame globalContext]);
955 JSObjectRef object([function JSObject]);
956 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
959 - (void) reloadButtonClicked {
960 [self reloadURLWithCache:YES];
963 - (void) _customButtonClicked {
964 [self reloadButtonClicked];
967 - (void) customButtonClicked {
969 if (function_ != nil)
970 [self callFunction:function_];
973 [self _customButtonClicked];
976 + (float) defaultWidth {
980 - (void) setNavigationBarStyle:(NSString *)name {
982 if ([name isEqualToString:@"Black"])
983 style = UIBarStyleBlack;
985 style = UIBarStyleDefault;
987 [[[self navigationController] navigationBar] setBarStyle:style];
990 - (void) setNavigationBarTintColor:(UIColor *)color {
991 [[[self navigationController] navigationBar] setTintColor:color];
994 - (void) setBadgeValue:(id)value {
995 [[[self navigationController] tabBarItem] setBadgeValue:value];
998 - (void) setHidesBackButton:(bool)value {
999 [[self navigationItem] setHidesBackButton:value];
1000 [self applyLeftButton];
1003 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
1004 [self setHidesBackButton:[value boolValue]];
1007 - (void) dispatchEvent:(NSString *)event {
1008 [[self webView] dispatchEvent:event];
1011 - (bool) hidesNavigationBar {
1012 return hidesNavigationBar_;
1015 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1017 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1020 - (void) setHidesNavigationBar:(bool)value {
1021 if (hidesNavigationBar_ != value) {
1022 hidesNavigationBar_ = value;
1023 [self _setHidesNavigationBar:YES animated:YES];
1027 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1028 [self setHidesNavigationBar:[value boolValue]];
1031 - (void) setScrollAlwaysBounceVertical:(bool)value {
1032 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1033 UIScrollView *scroller([webview_ _scrollView]);
1034 [scroller setAlwaysBounceVertical:value];
1035 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1036 //UIScroller *scroller([webview_ _scroller]);
1037 // XXX: I am sad here.
1041 - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1042 [self setScrollAlwaysBounceVertical:[value boolValue]];
1045 - (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style {
1046 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1047 UIScrollView *scroller([webview_ _scrollView]);
1048 [scroller setIndicatorStyle:style];
1049 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1050 UIScroller *scroller([webview_ _scroller]);
1051 [scroller setScrollerIndicatorStyle:style];
1055 - (void) setScrollIndicatorStyleWithName:(NSString *)style {
1056 UIScrollViewIndicatorStyle value;
1059 else if ([style isEqualToString:@"default"])
1060 value = UIScrollViewIndicatorStyleDefault;
1061 else if ([style isEqualToString:@"black"])
1062 value = UIScrollViewIndicatorStyleBlack;
1063 else if ([style isEqualToString:@"white"])
1064 value = UIScrollViewIndicatorStyleWhite;
1067 [self setScrollIndicatorStyle:value];
1070 - (void) viewWillAppear:(BOOL)animated {
1073 if ([self hidesNavigationBar])
1074 [self _setHidesNavigationBar:YES animated:animated];
1076 [self dispatchEvent:@"CydiaViewWillAppear"];
1077 [super viewWillAppear:animated];
1080 - (void) viewDidAppear:(BOOL)animated {
1081 [super viewDidAppear:animated];
1082 [self dispatchEvent:@"CydiaViewDidAppear"];
1085 - (void) viewWillDisappear:(BOOL)animated {
1086 [self dispatchEvent:@"CydiaViewWillDisappear"];
1087 [super viewWillDisappear:animated];
1089 if ([self hidesNavigationBar])
1090 [self _setHidesNavigationBar:NO animated:animated];
1095 - (void) viewDidDisappear:(BOOL)animated {
1096 [super viewDidDisappear:animated];
1097 [self dispatchEvent:@"CydiaViewDidDisappear"];