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 #import <WebKit/WebKitErrors.h>
20 #import <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 @implementation WebFrame (Cydia)
52 - (NSString *) description {
53 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[([self provisionalDataSource] ?: [self dataSource]) request] URL] absoluteString]];
58 /* Indirect Delegate {{{ */
59 @interface IndirectDelegate : NSObject {
60 _transient volatile id delegate_;
63 - (void) setDelegate:(id)delegate;
64 - (id) initWithDelegate:(id)delegate;
67 @implementation IndirectDelegate
69 - (void) setDelegate:(id)delegate {
73 - (id) initWithDelegate:(id)delegate {
78 - (IMP) methodForSelector:(SEL)sel {
79 if (IMP method = [super methodForSelector:sel])
81 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
85 - (BOOL) respondsToSelector:(SEL)sel {
86 if ([super respondsToSelector:sel])
89 // XXX: WebThreadCreateNSInvocation returns nil
92 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
95 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
98 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
99 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
103 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
106 if (delegate_ != nil)
107 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
110 // XXX: I fucking hate Apple so very very bad
111 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
114 - (void) forwardInvocation:(NSInvocation *)inv {
115 SEL sel = [inv selector];
116 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
117 [inv invokeWithTarget:delegate_];
123 @implementation CyteWebViewController
126 #include "CyteKit/UCInternal.h"
129 + (void) _initialize {
130 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
132 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
133 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
135 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
136 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
137 else // XXX: this actually might be fast on some older systems: we should look into this
138 CYScrollViewDecelerationRateNormal = 0.998;
141 - (bool) retainsNetworkActivityIndicator {
145 - (void) releaseNetworkActivityIndicator {
146 if ([loading_ count] != 0) {
147 [loading_ removeAllObjects];
149 if ([self retainsNetworkActivityIndicator])
150 [delegate_ releaseNetworkActivityIndicator];
156 NSLog(@"[CyteWebViewController dealloc]");
159 [self releaseNetworkActivityIndicator];
164 - (NSString *) description {
165 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]];
168 - (CyteWebView *) webView {
169 return (CyteWebView *) [self view];
172 - (NSURL *) URLWithURL:(NSURL *)url {
176 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
178 requestWithURL:[self URLWithURL:url]
180 timeoutInterval:DefaultTimeout_
184 - (void) setRequest:(NSURLRequest *)request {
185 _assert(request_ == nil);
189 - (void) setURL:(NSURL *)url {
190 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]];
193 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
194 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
197 - (void) loadURL:(NSURL *)url {
198 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
201 - (void) loadRequest:(NSURLRequest *)request {
203 NSLog(@"loadRequest:%@", request);
209 WebThreadLocked lock;
210 [[self webView] loadRequest:request];
213 - (void) reloadURLWithCache:(BOOL)cache {
217 NSMutableURLRequest *request([request_ mutableCopy]);
218 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
222 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
223 [self loadRequest:request_];
225 UIAlertView *alert = [[[UIAlertView alloc]
226 initWithTitle:UCLocalize("RESUBMIT_FORM")
229 cancelButtonTitle:UCLocalize("CANCEL")
231 UCLocalize("SUBMIT"),
235 [alert setContext:@"submit"];
241 [self reloadURLWithCache:YES];
244 - (void) reloadData {
248 [self dispatchEvent:@"CydiaReloadData"];
250 [self reloadURLWithCache:YES];
253 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
256 function_ = function;
258 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
261 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
264 function_ = function;
266 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
269 - (void) removeButton {
270 custom_ = [NSNull null];
271 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
274 - (void) scrollToBottomAnimated:(NSNumber *)animated {
275 CGSize size([scroller_ contentSize]);
276 CGPoint offset([scroller_ contentOffset]);
277 CGRect frame([scroller_ frame]);
279 if (size.height - offset.y < frame.size.height + 20.f) {
280 CGRect rect = {{0, size.height-1}, {size.width, 1}};
281 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
285 - (void) _setViewportWidth {
286 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
289 - (void) setViewportWidth:(float)width {
290 width_ = width != 0 ? width : [[self class] defaultWidth];
291 [self _setViewportWidth];
294 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
295 [self setViewportWidth:[width floatValue]];
298 - (void) setViewportWidthOnMainThread:(float)width {
299 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
302 - (void) webViewUpdateViewSettings:(UIWebView *)view {
303 [self _setViewportWidth];
306 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
307 [self dismissModalViewControllerAnimated:YES];
310 - (void) _setupMail:(MFMailComposeViewController *)controller {
313 - (void) _openMailToURL:(NSURL *)url {
314 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
315 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
316 [controller setMailComposeDelegate:self];
318 [controller setMailToURL:url];
320 [self _setupMail:controller];
322 [self presentModalViewController:controller animated:YES];
326 UIApplication *app([UIApplication sharedApplication]);
327 if ([app respondsToSelector:@selector(openURL:asPanel:)])
328 [app openURL:url asPanel:YES];
333 - (bool) _allowJavaScriptPanel {
337 - (bool) allowsNavigationAction {
338 return allowsNavigationAction_;
341 - (void) setAllowsNavigationAction:(bool)value {
342 allowsNavigationAction_ = value;
345 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
346 [self setAllowsNavigationAction:[value boolValue]];
349 - (void) popViewControllerWithNumber:(NSNumber *)value {
350 UINavigationController *navigation([self navigationController]);
351 if ([navigation topViewController] == self)
352 [navigation popViewControllerAnimated:[value boolValue]];
355 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
356 NSValue *object([NSValue valueWithNonretainedObject:frame]);
357 if (![loading_ containsObject:object])
359 [loading_ removeObject:object];
361 [self _didFinishLoading];
363 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
366 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
371 if ([frame parentFrame] == nil) {
372 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
373 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
374 [[error localizedDescription] stringByAddingPercentEscapes]
381 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
382 NSURL *url([request URL]);
384 // XXX: filter to internal usage?
385 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
388 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
389 [browser setRequest:request];
393 [page setDelegate:delegate_];
396 [[self navigationItem] setTitle:title_];
398 [[self navigationController] pushViewController:page animated:YES];
400 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
402 [navigation setDelegate:delegate_];
404 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
405 initWithTitle:UCLocalize("CLOSE")
406 style:UIBarButtonItemStylePlain
408 action:@selector(close)
411 [[self navigationController] presentModalViewController:navigation animated:YES];
415 // CyteWebViewDelegate {{{
416 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
418 static Pcre irritating("^(?"
419 ":" "The page at .* displayed insecure content from .*\\."
420 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
423 if (NSString *data = [message objectForKey:@"message"])
424 if (irritating(data))
427 NSLog(@"addMessageToConsole:%@", message);
431 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
433 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
436 if ([frame parentFrame] == nil) {
438 NSURL *url(request == nil ? nil : [request URL]);
440 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
442 [self pushRequest:request asPop:NO];
449 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
450 if ([frame parentFrame] == nil)
451 if (decision == CYWebPolicyDecisionUse)
456 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
458 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
461 NSURL *url([request URL]);
465 if ([frame isEqualToString:@"_open"])
466 [delegate_ openURL:url];
468 NSString *scheme([[url scheme] lowercaseString]);
469 if ([scheme isEqualToString:@"mailto"])
470 [self _openMailToURL:url];
472 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
478 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
481 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
483 NSLog(@"didCommitLoadForFrame:%@", frame);
486 if ([frame parentFrame] == nil) {
491 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
493 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
496 [self _didFailWithError:error forFrame:frame];
499 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
501 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
504 [self _didFailWithError:error forFrame:frame];
507 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
508 NSValue *object([NSValue valueWithNonretainedObject:frame]);
509 if (![loading_ containsObject:object])
511 [loading_ removeObject:object];
513 if ([frame parentFrame] == nil) {
514 if (DOMDocument *document = [frame DOMDocument])
515 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
516 for (DOMHTMLBodyElement *body in (id) bodies) {
517 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
521 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
522 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
523 DOMRGBColor *rgb([color getRGBColorValue]);
525 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
526 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
527 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
528 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
530 if (red == 0xc7 && green == 0xce && blue == 0xd5)
531 uic = [UIColor pinStripeColor];
534 colorWithRed:(red / 255)
542 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
547 [self _didFinishLoading];
550 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
551 if ([frame parentFrame] != nil)
556 [[self navigationItem] setTitle:title_];
559 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
561 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
564 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
566 if ([frame parentFrame] == nil) {
572 allowsNavigationAction_ = true;
574 [self setHidesNavigationBar:NO];
575 [self setScrollAlwaysBounceVertical:true];
576 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
578 // XXX: do we still need to do this?
579 [[self navigationItem] setTitle:nil];
582 [self _didStartLoading];
585 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
587 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
593 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
594 return [self _allowJavaScriptPanel];
597 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
598 return [self _allowJavaScriptPanel];
601 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
602 return [self _allowJavaScriptPanel];
605 - (void) webViewClose:(WebView *)view {
611 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
614 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
615 NSString *context([alert context]);
617 if ([context isEqualToString:@"sensitive"]) {
620 sensitive_ = [NSNumber numberWithBool:YES];
624 sensitive_ = [NSNumber numberWithBool:NO];
628 [alert dismissWithClickedButtonIndex:-1 animated:YES];
629 } else if ([context isEqualToString:@"challenge"]) {
630 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
634 NSString *username([[alert textFieldAtIndex:0] text]);
635 NSString *password([[alert textFieldAtIndex:1] text]);
637 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
639 [sender useCredential:credential forAuthenticationChallenge:challenge_];
643 [sender cancelAuthenticationChallenge:challenge_];
651 [alert dismissWithClickedButtonIndex:-1 animated:YES];
652 } else if ([context isEqualToString:@"submit"]) {
653 if (button == [alert cancelButtonIndex]) {
654 } else if (button == [alert firstOtherButtonIndex]) {
655 if (request_ != nil) {
656 WebThreadLocked lock;
657 [[self webView] loadRequest:request_];
661 [alert dismissWithClickedButtonIndex:-1 animated:YES];
665 - (UIBarButtonItemStyle) rightButtonStyle {
666 if (style_ == nil) normal:
667 return UIBarButtonItemStylePlain;
668 else if ([style_ isEqualToString:@"Normal"])
669 return UIBarButtonItemStylePlain;
670 else if ([style_ isEqualToString:@"Highlighted"])
671 return UIBarButtonItemStyleDone;
675 - (UIBarButtonItem *) customButton {
678 else if (custom_ == [NSNull null])
679 return (UIBarButtonItem *) [NSNull null];
681 return [[[UIBarButtonItem alloc]
682 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
683 style:[self rightButtonStyle]
685 action:@selector(customButtonClicked)
689 - (UIBarButtonItem *) leftButton {
690 UINavigationItem *item([self navigationItem]);
691 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
694 if (UINavigationController *navigation = [self navigationController])
695 if ([[navigation parentViewController] modalViewController] == navigation)
696 return [[[UIBarButtonItem alloc]
697 initWithTitle:UCLocalize("CLOSE")
698 style:UIBarButtonItemStylePlain
700 action:@selector(close)
706 - (void) applyLeftButton {
707 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
710 - (UIBarButtonItem *) rightButton {
714 - (void) applyLoadingTitle {
715 [[self navigationItem] setTitle:UCLocalize("LOADING")];
718 - (void) layoutRightButton {
719 [[loadingitem_ view] addSubview:indicator_];
720 [[loadingitem_ view] bringSubviewToFront:indicator_];
723 - (void) applyRightButton {
724 if ([self isLoading]) {
725 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
726 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
728 [indicator_ startAnimating];
729 [self applyLoadingTitle];
731 [indicator_ stopAnimating];
733 UIBarButtonItem *button([self customButton]);
735 button = [self rightButton];
736 else if (button == (UIBarButtonItem *) [NSNull null])
739 [[self navigationItem] setRightBarButtonItem:button animated:YES];
743 - (void) didStartLoading {
744 // Overridden in subclasses.
747 - (void) _didStartLoading {
748 [self applyRightButton];
750 if ([loading_ count] != 1)
753 if ([self retainsNetworkActivityIndicator])
754 [delegate_ retainNetworkActivityIndicator];
756 [self didStartLoading];
759 - (void) didFinishLoading {
760 // Overridden in subclasses.
763 - (void) _didFinishLoading {
764 if ([loading_ count] != 0)
767 [self applyRightButton];
768 [[self navigationItem] setTitle:title_];
770 if ([self retainsNetworkActivityIndicator])
771 [delegate_ releaseNetworkActivityIndicator];
773 [self didFinishLoading];
777 return [loading_ count] != 0;
780 - (id) initWithWidth:(float)width ofClass:(Class)_class {
781 if ((self = [super init]) != nil) {
785 allowsNavigationAction_ = true;
787 loading_ = [NSMutableSet setWithCapacity:5];
788 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
790 reloaditem_ = [[[UIBarButtonItem alloc]
791 initWithTitle:UCLocalize("RELOAD")
792 style:[self rightButtonStyle]
794 action:@selector(reloadButtonClicked)
797 loadingitem_ = [[[UIBarButtonItem alloc]
799 style:UIBarButtonItemStylePlain
801 action:@selector(reloadButtonClicked)
804 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
805 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
806 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
808 [self applyLeftButton];
809 [self applyRightButton];
813 - (NSString *) applicationNameForUserAgent {
818 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
820 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
821 [webview_ setDelegate:self];
822 [self setView:webview_];
824 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
825 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
827 [webview_ setDetectsPhoneNumbers:NO];
829 [webview_ setScalesPageToFit:YES];
831 UIWebDocumentView *document([webview_ _documentView]);
833 // XXX: I think this improves scrolling; the hardcoded-ness sucks
834 [document setTileSize:CGSizeMake(320, 500)];
836 [document setBackgroundColor:[UIColor clearColor]];
838 // XXX: this is terribly (too?) expensive
839 [document setDrawsBackground:NO];
841 WebView *webview([document webView]);
842 WebPreferences *preferences([webview preferences]);
844 // XXX: I have no clue if I actually /want/ this modification
845 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
846 [webview _setLayoutInterval:0];
847 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
848 [preferences _setLayoutInterval:0];
850 [preferences setCacheModel:WebCacheModelDocumentBrowser];
851 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
852 [preferences setOfflineWebApplicationCacheEnabled:YES];
854 if (NSString *agent = [self applicationNameForUserAgent])
855 [webview setApplicationNameForUserAgent:agent];
857 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
858 [webview setShouldUpdateWhileOffscreen:NO];
861 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
862 [document setAllowsMessaging:YES];
863 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
864 [webview _setAllowsMessaging:YES];
867 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
868 scroller_ = [webview_ _scrollView];
870 [scroller_ setDirectionalLockEnabled:YES];
871 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
872 [scroller_ setDelaysContentTouches:NO];
874 [scroller_ setCanCancelContentTouches:YES];
875 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
876 UIScroller *scroller([webview_ _scroller]);
877 scroller_ = (UIScrollView *) scroller;
879 [scroller setDirectionalScrolling:YES];
880 // XXX: we might be better off /not/ setting this on older systems
881 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
882 [scroller setScrollHysteresis:0]; /* 8 */
884 [scroller setThumbDetectionEnabled:NO];
886 // use NO with UIApplicationUseLegacyEvents(YES)
887 [scroller setEventMode:YES];
889 // XXX: this is handled by setBounces, right?
890 //[scroller setAllowsRubberBanding:YES];
893 [scroller_ setFixedBackgroundPattern:YES];
894 [scroller_ setBackgroundColor:[UIColor clearColor]];
895 [scroller_ setClipsSubviews:YES];
897 [scroller_ setBounces:YES];
898 [scroller_ setScrollingEnabled:YES];
899 [scroller_ setShowBackgroundShadow:NO];
901 [self setViewportWidth:width_];
903 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
904 [table setScrollsToTop:NO];
905 [webview_ insertSubview:table atIndex:0];
907 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
908 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
913 - (void) releaseSubviews {
917 [self releaseNetworkActivityIndicator];
919 [super releaseSubviews];
922 - (id) initWithWidth:(float)width {
923 return [self initWithWidth:width ofClass:[self class]];
927 return [self initWithWidth:0];
930 - (id) initWithURL:(NSURL *)url {
931 if ((self = [self init]) != nil) {
936 - (id) initWithRequest:(NSURLRequest *)request {
937 if ((self = [self init]) != nil) {
938 [self setRequest:request];
942 - (void) callFunction:(WebScriptObject *)function {
943 WebThreadLocked lock;
945 WebView *webview([[[self webView] _documentView] webView]);
946 WebFrame *frame([webview mainFrame]);
948 JSGlobalContextRef context([frame globalContext]);
949 JSObjectRef object([function JSObject]);
950 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
953 - (void) reloadButtonClicked {
954 [self reloadURLWithCache:YES];
957 - (void) _customButtonClicked {
958 [self reloadButtonClicked];
961 - (void) customButtonClicked {
963 if (function_ != nil)
964 [self callFunction:function_];
967 [self _customButtonClicked];
970 + (float) defaultWidth {
974 - (void) setNavigationBarStyle:(NSString *)name {
976 if ([name isEqualToString:@"Black"])
977 style = UIBarStyleBlack;
979 style = UIBarStyleDefault;
981 [[[self navigationController] navigationBar] setBarStyle:style];
984 - (void) setNavigationBarTintColor:(UIColor *)color {
985 [[[self navigationController] navigationBar] setTintColor:color];
988 - (void) setBadgeValue:(id)value {
989 [[[self navigationController] tabBarItem] setBadgeValue:value];
992 - (void) setHidesBackButton:(bool)value {
993 [[self navigationItem] setHidesBackButton:value];
994 [self applyLeftButton];
997 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
998 [self setHidesBackButton:[value boolValue]];
1001 - (void) dispatchEvent:(NSString *)event {
1002 [[self webView] dispatchEvent:event];
1005 - (bool) hidesNavigationBar {
1006 return hidesNavigationBar_;
1009 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1011 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1014 - (void) setHidesNavigationBar:(bool)value {
1015 if (hidesNavigationBar_ != value) {
1016 hidesNavigationBar_ = value;
1017 [self _setHidesNavigationBar:YES animated:YES];
1021 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1022 [self setHidesNavigationBar:[value boolValue]];
1025 - (void) setScrollAlwaysBounceVertical:(bool)value {
1026 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1027 UIScrollView *scroller([webview_ _scrollView]);
1028 [scroller setAlwaysBounceVertical:value];
1029 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1030 //UIScroller *scroller([webview_ _scroller]);
1031 // XXX: I am sad here.
1035 - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1036 [self setScrollAlwaysBounceVertical:[value boolValue]];
1039 - (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style {
1040 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1041 UIScrollView *scroller([webview_ _scrollView]);
1042 [scroller setIndicatorStyle:style];
1043 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1044 UIScroller *scroller([webview_ _scroller]);
1045 [scroller setScrollerIndicatorStyle:style];
1049 - (void) setScrollIndicatorStyleWithName:(NSString *)style {
1050 UIScrollViewIndicatorStyle value;
1053 else if ([style isEqualToString:@"default"])
1054 value = UIScrollViewIndicatorStyleDefault;
1055 else if ([style isEqualToString:@"black"])
1056 value = UIScrollViewIndicatorStyleBlack;
1057 else if ([style isEqualToString:@"white"])
1058 value = UIScrollViewIndicatorStyleWhite;
1061 [self setScrollIndicatorStyle:value];
1064 - (void) viewWillAppear:(BOOL)animated {
1067 if ([self hidesNavigationBar])
1068 [self _setHidesNavigationBar:YES animated:animated];
1070 [self dispatchEvent:@"CydiaViewWillAppear"];
1071 [super viewWillAppear:animated];
1074 - (void) viewDidAppear:(BOOL)animated {
1075 [super viewDidAppear:animated];
1076 [self dispatchEvent:@"CydiaViewDidAppear"];
1079 - (void) viewWillDisappear:(BOOL)animated {
1080 [self dispatchEvent:@"CydiaViewWillDisappear"];
1081 [super viewWillDisappear:animated];
1083 if ([self hidesNavigationBar])
1084 [self _setHidesNavigationBar:NO animated:animated];
1089 - (void) viewDidDisappear:(BOOL)animated {
1090 [super viewDidDisappear:animated];
1091 [self dispatchEvent:@"CydiaViewDidDisappear"];