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) {
361 if ([frame parentFrame] == nil) {
362 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
363 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
364 [[error localizedDescription] stringByAddingPercentEscapes]
371 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
372 NSURL *url([request URL]);
374 // XXX: filter to internal usage?
375 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
378 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
379 [browser setRequest:request];
383 [page setDelegate:delegate_];
386 [[self navigationItem] setTitle:title_];
388 [[self navigationController] pushViewController:page animated:YES];
390 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
392 [navigation setDelegate:delegate_];
394 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
395 initWithTitle:UCLocalize("CLOSE")
396 style:UIBarButtonItemStylePlain
398 action:@selector(close)
401 [[self navigationController] presentModalViewController:navigation animated:YES];
405 // CyteWebViewDelegate {{{
406 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
408 static Pcre irritating("^(?"
409 ":" "The page at .* displayed insecure content from .*\\."
410 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
413 if (NSString *data = [message objectForKey:@"message"])
414 if (irritating(data))
417 NSLog(@"addMessageToConsole:%@", message);
421 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
423 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
426 if ([frame parentFrame] == nil) {
428 NSURL *url(request == nil ? nil : [request URL]);
430 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
432 [self pushRequest:request asPop:NO];
439 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
440 if ([frame parentFrame] == nil)
441 if (decision == CYWebPolicyDecisionUse)
446 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
448 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
451 NSURL *url([request URL]);
455 if ([frame isEqualToString:@"_open"])
456 [delegate_ openURL:url];
458 NSString *scheme([[url scheme] lowercaseString]);
459 if ([scheme isEqualToString:@"mailto"])
460 [self _openMailToURL:url];
462 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
468 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
471 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
473 NSLog(@"didCommitLoadForFrame:%@", frame);
476 if ([frame parentFrame] == nil) {
481 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
483 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
486 [self _didFailWithError:error forFrame:frame];
489 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
491 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
494 [self _didFailWithError:error forFrame:frame];
497 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
498 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
500 if ([frame parentFrame] == nil) {
501 if (DOMDocument *document = [frame DOMDocument])
502 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
503 for (DOMHTMLBodyElement *body in (id) bodies) {
504 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
508 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
509 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
510 DOMRGBColor *rgb([color getRGBColorValue]);
512 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
513 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
514 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
515 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
517 if (red == 0xc7 && green == 0xce && blue == 0xd5)
518 uic = [UIColor pinStripeColor];
521 colorWithRed:(red / 255)
529 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
534 [self _didFinishLoading];
537 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
538 if ([frame parentFrame] != nil)
543 [[self navigationItem] setTitle:title_];
546 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
548 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
551 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
553 if ([frame parentFrame] == nil) {
559 allowsNavigationAction_ = true;
561 [self setHidesNavigationBar:NO];
562 [self setScrollAlwaysBounceVertical:true];
563 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
565 // XXX: do we still need to do this?
566 [[self navigationItem] setTitle:nil];
569 [self _didStartLoading];
572 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
574 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
580 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
581 return [self _allowJavaScriptPanel];
584 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
585 return [self _allowJavaScriptPanel];
588 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
589 return [self _allowJavaScriptPanel];
592 - (void) webViewClose:(WebView *)view {
598 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
601 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
602 NSString *context([alert context]);
604 if ([context isEqualToString:@"sensitive"]) {
607 sensitive_ = [NSNumber numberWithBool:YES];
611 sensitive_ = [NSNumber numberWithBool:NO];
615 [alert dismissWithClickedButtonIndex:-1 animated:YES];
616 } else if ([context isEqualToString:@"challenge"]) {
617 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
621 NSString *username([[alert textFieldAtIndex:0] text]);
622 NSString *password([[alert textFieldAtIndex:1] text]);
624 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
626 [sender useCredential:credential forAuthenticationChallenge:challenge_];
630 [sender cancelAuthenticationChallenge:challenge_];
638 [alert dismissWithClickedButtonIndex:-1 animated:YES];
639 } else if ([context isEqualToString:@"submit"]) {
640 if (button == [alert cancelButtonIndex]) {
641 } else if (button == [alert firstOtherButtonIndex]) {
642 if (request_ != nil) {
643 WebThreadLocked lock;
644 [[self webView] loadRequest:request_];
648 [alert dismissWithClickedButtonIndex:-1 animated:YES];
652 - (UIBarButtonItemStyle) rightButtonStyle {
653 if (style_ == nil) normal:
654 return UIBarButtonItemStylePlain;
655 else if ([style_ isEqualToString:@"Normal"])
656 return UIBarButtonItemStylePlain;
657 else if ([style_ isEqualToString:@"Highlighted"])
658 return UIBarButtonItemStyleDone;
662 - (UIBarButtonItem *) customButton {
665 else if (custom_ == [NSNull null])
666 return (UIBarButtonItem *) [NSNull null];
668 return [[[UIBarButtonItem alloc]
669 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
670 style:[self rightButtonStyle]
672 action:@selector(customButtonClicked)
676 - (UIBarButtonItem *) leftButton {
677 UINavigationItem *item([self navigationItem]);
678 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
681 if (UINavigationController *navigation = [self navigationController])
682 if ([[navigation parentViewController] modalViewController] == navigation)
683 return [[[UIBarButtonItem alloc]
684 initWithTitle:UCLocalize("CLOSE")
685 style:UIBarButtonItemStylePlain
687 action:@selector(close)
693 - (void) applyLeftButton {
694 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
697 - (UIBarButtonItem *) rightButton {
701 - (void) applyLoadingTitle {
702 [[self navigationItem] setTitle:UCLocalize("LOADING")];
705 - (void) layoutRightButton {
706 [[loadingitem_ view] addSubview:indicator_];
707 [[loadingitem_ view] bringSubviewToFront:indicator_];
710 - (void) applyRightButton {
711 if ([self isLoading]) {
712 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
713 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
715 [indicator_ startAnimating];
716 [self applyLoadingTitle];
718 [indicator_ stopAnimating];
720 UIBarButtonItem *button([self customButton]);
722 button = [self rightButton];
723 else if (button == (UIBarButtonItem *) [NSNull null])
726 [[self navigationItem] setRightBarButtonItem:button animated:YES];
730 - (void) didStartLoading {
731 // Overridden in subclasses.
734 - (void) _didStartLoading {
735 [self applyRightButton];
737 if ([loading_ count] != 1)
740 [delegate_ retainNetworkActivityIndicator];
741 [self didStartLoading];
743 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0)
747 - (void) didFinishLoading {
748 // Overridden in subclasses.
751 - (void) _didFinishLoading {
752 if ([loading_ count] != 0)
755 [self applyRightButton];
756 [[self navigationItem] setTitle:title_];
758 [delegate_ releaseNetworkActivityIndicator];
759 [self didFinishLoading];
761 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0)
766 return [loading_ count] != 0;
769 - (id) initWithWidth:(float)width ofClass:(Class)_class {
770 if ((self = [super init]) != nil) {
774 allowsNavigationAction_ = true;
776 loading_ = [NSMutableSet setWithCapacity:5];
777 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
779 reloaditem_ = [[[UIBarButtonItem alloc]
780 initWithTitle:UCLocalize("RELOAD")
781 style:[self rightButtonStyle]
783 action:@selector(reloadButtonClicked)
786 loadingitem_ = [[[UIBarButtonItem alloc]
788 style:UIBarButtonItemStylePlain
790 action:@selector(reloadButtonClicked)
793 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
794 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
795 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
797 [self applyLeftButton];
798 [self applyRightButton];
802 - (NSString *) applicationNameForUserAgent {
807 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
809 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
810 [webview_ setDelegate:self];
811 [self setView:webview_];
813 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
814 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
816 [webview_ setDetectsPhoneNumbers:NO];
818 [webview_ setScalesPageToFit:YES];
820 UIWebDocumentView *document([webview_ _documentView]);
822 // XXX: I think this improves scrolling; the hardcoded-ness sucks
823 [document setTileSize:CGSizeMake(320, 500)];
825 [document setBackgroundColor:[UIColor clearColor]];
827 // XXX: this is terribly (too?) expensive
828 [document setDrawsBackground:NO];
830 WebView *webview([document webView]);
831 WebPreferences *preferences([webview preferences]);
833 // XXX: I have no clue if I actually /want/ this modification
834 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
835 [webview _setLayoutInterval:0];
836 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
837 [preferences _setLayoutInterval:0];
839 [preferences setCacheModel:WebCacheModelDocumentBrowser];
840 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
841 [preferences setOfflineWebApplicationCacheEnabled:YES];
843 if (NSString *agent = [self applicationNameForUserAgent])
844 [webview setApplicationNameForUserAgent:agent];
846 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
847 [webview setShouldUpdateWhileOffscreen:NO];
850 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
851 [document setAllowsMessaging:YES];
852 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
853 [webview _setAllowsMessaging:YES];
856 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
857 scroller_ = [webview_ _scrollView];
859 [scroller_ setDirectionalLockEnabled:YES];
860 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
861 [scroller_ setDelaysContentTouches:NO];
863 [scroller_ setCanCancelContentTouches:YES];
864 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
865 UIScroller *scroller([webview_ _scroller]);
866 scroller_ = (UIScrollView *) scroller;
868 [scroller setDirectionalScrolling:YES];
869 // XXX: we might be better off /not/ setting this on older systems
870 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
871 [scroller setScrollHysteresis:0]; /* 8 */
873 [scroller setThumbDetectionEnabled:NO];
875 // use NO with UIApplicationUseLegacyEvents(YES)
876 [scroller setEventMode:YES];
878 // XXX: this is handled by setBounces, right?
879 //[scroller setAllowsRubberBanding:YES];
882 [scroller_ setFixedBackgroundPattern:YES];
883 [scroller_ setBackgroundColor:[UIColor clearColor]];
884 [scroller_ setClipsSubviews:YES];
886 [scroller_ setBounces:YES];
887 [scroller_ setScrollingEnabled:YES];
888 [scroller_ setShowBackgroundShadow:NO];
890 [self setViewportWidth:width_];
892 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
893 [table setScrollsToTop:NO];
894 [webview_ insertSubview:table atIndex:0];
896 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
897 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
902 - (void) releaseSubviews {
906 [super releaseSubviews];
909 - (id) initWithWidth:(float)width {
910 return [self initWithWidth:width ofClass:[self class]];
914 return [self initWithWidth:0];
917 - (id) initWithURL:(NSURL *)url {
918 if ((self = [self init]) != nil) {
923 - (void) callFunction:(WebScriptObject *)function {
924 WebThreadLocked lock;
926 WebView *webview([[[self webView] _documentView] webView]);
927 WebFrame *frame([webview mainFrame]);
929 JSGlobalContextRef context([frame globalContext]);
930 JSObjectRef object([function JSObject]);
931 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
934 - (void) reloadButtonClicked {
935 [self reloadURLWithCache:YES];
938 - (void) _customButtonClicked {
939 [self reloadButtonClicked];
942 - (void) customButtonClicked {
944 if (function_ != nil)
945 [self callFunction:function_];
948 [self _customButtonClicked];
951 + (float) defaultWidth {
955 - (void) setNavigationBarStyle:(NSString *)name {
957 if ([name isEqualToString:@"Black"])
958 style = UIBarStyleBlack;
960 style = UIBarStyleDefault;
962 [[[self navigationController] navigationBar] setBarStyle:style];
965 - (void) setNavigationBarTintColor:(UIColor *)color {
966 [[[self navigationController] navigationBar] setTintColor:color];
969 - (void) setBadgeValue:(id)value {
970 [[[self navigationController] tabBarItem] setBadgeValue:value];
973 - (void) setHidesBackButton:(bool)value {
974 [[self navigationItem] setHidesBackButton:value];
975 [self applyLeftButton];
978 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
979 [self setHidesBackButton:[value boolValue]];
982 - (void) dispatchEvent:(NSString *)event {
983 [[self webView] dispatchEvent:event];
986 - (bool) hidesNavigationBar {
987 return hidesNavigationBar_;
990 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
992 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
995 - (void) setHidesNavigationBar:(bool)value {
996 if (hidesNavigationBar_ != value) {
997 hidesNavigationBar_ = value;
998 [self _setHidesNavigationBar:YES animated:YES];
1002 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1003 [self setHidesNavigationBar:[value boolValue]];
1006 - (void) setScrollAlwaysBounceVertical:(bool)value {
1007 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1008 UIScrollView *scroller([webview_ _scrollView]);
1009 [scroller setAlwaysBounceVertical:value];
1010 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1011 //UIScroller *scroller([webview_ _scroller]);
1012 // XXX: I am sad here.
1016 - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1017 [self setScrollAlwaysBounceVertical:[value boolValue]];
1020 - (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style {
1021 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1022 UIScrollView *scroller([webview_ _scrollView]);
1023 [scroller setIndicatorStyle:style];
1024 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1025 UIScroller *scroller([webview_ _scroller]);
1026 [scroller setScrollerIndicatorStyle:style];
1030 - (void) setScrollIndicatorStyleWithName:(NSString *)style {
1031 UIScrollViewIndicatorStyle value;
1034 else if ([style isEqualToString:@"default"])
1035 value = UIScrollViewIndicatorStyleDefault;
1036 else if ([style isEqualToString:@"black"])
1037 value = UIScrollViewIndicatorStyleBlack;
1038 else if ([style isEqualToString:@"white"])
1039 value = UIScrollViewIndicatorStyleWhite;
1042 [self setScrollIndicatorStyle:value];
1045 - (void) viewWillAppear:(BOOL)animated {
1048 if ([self hidesNavigationBar])
1049 [self _setHidesNavigationBar:YES animated:animated];
1051 [self dispatchEvent:@"CydiaViewWillAppear"];
1052 [super viewWillAppear:animated];
1055 - (void) viewDidAppear:(BOOL)animated {
1056 [super viewDidAppear:animated];
1057 [self dispatchEvent:@"CydiaViewDidAppear"];
1060 - (void) viewWillDisappear:(BOOL)animated {
1061 [self dispatchEvent:@"CydiaViewWillDisappear"];
1062 [super viewWillDisappear:animated];
1064 if ([self hidesNavigationBar])
1065 [self _setHidesNavigationBar:NO animated:animated];
1070 - (void) viewDidDisappear:(BOOL)animated {
1071 [super viewDidDisappear:animated];
1072 [self dispatchEvent:@"CydiaViewDidDisappear"];