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 #define DefaultTimeout_ 120.0
31 #define ShowInternals 0
35 #define lprintf(args...) fprintf(stderr, args)
37 // XXX: centralize these special class things to some file or mechanism?
38 static Class $MFMailComposeViewController;
40 float CYScrollViewDecelerationRateNormal;
42 @interface WebView (Apple)
43 - (void) _setLayoutInterval:(float)interval;
44 - (void) _setAllowsMessaging:(BOOL)allows;
47 @interface WebPreferences (Apple)
48 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
49 - (void) _setLayoutInterval:(NSInteger)interval;
50 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
53 /* Indirect Delegate {{{ */
54 @interface IndirectDelegate : NSObject {
55 _transient volatile id delegate_;
58 - (void) setDelegate:(id)delegate;
59 - (id) initWithDelegate:(id)delegate;
62 @implementation IndirectDelegate
64 - (void) setDelegate:(id)delegate {
68 - (id) initWithDelegate:(id)delegate {
73 - (IMP) methodForSelector:(SEL)sel {
74 if (IMP method = [super methodForSelector:sel])
76 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
80 - (BOOL) respondsToSelector:(SEL)sel {
81 if ([super respondsToSelector:sel])
84 // XXX: WebThreadCreateNSInvocation returns nil
87 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
90 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
93 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
94 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
98 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
101 if (delegate_ != nil)
102 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
105 // XXX: I fucking hate Apple so very very bad
106 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
109 - (void) forwardInvocation:(NSInvocation *)inv {
110 SEL sel = [inv selector];
111 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
112 [inv invokeWithTarget:delegate_];
118 @implementation CyteWebViewController
121 #include "CyteKit/UCInternal.h"
124 + (void) _initialize {
125 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
127 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
128 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
130 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
131 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
132 else // XXX: this actually might be fast on some older systems: we should look into this
133 CYScrollViewDecelerationRateNormal = 0.998;
138 NSLog(@"[CyteWebViewController dealloc]");
141 [webview_ setDelegate:nil];
142 [indirect_ setDelegate:nil];
144 if ([loading_ count] != 0)
145 [delegate_ releaseNetworkActivityIndicator];
150 - (NSURL *) URLWithURL:(NSURL *)url {
154 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
156 requestWithURL:[self URLWithURL:url]
158 timeoutInterval:DefaultTimeout_
162 - (void) setURL:(NSURL *)url {
163 _assert(request_ == nil);
164 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
167 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
168 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
171 - (void) loadURL:(NSURL *)url {
172 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
175 - (void) loadRequest:(NSURLRequest *)request {
177 NSLog(@"loadRequest:%@", request);
182 WebThreadLocked lock;
183 [webview_ loadRequest:request];
186 - (void) reloadURLWithCache:(BOOL)cache {
190 NSMutableURLRequest *request([request_ mutableCopy]);
191 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
195 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
196 [self loadRequest:request_];
198 UIAlertView *alert = [[[UIAlertView alloc]
199 initWithTitle:UCLocalize("RESUBMIT_FORM")
202 cancelButtonTitle:UCLocalize("CANCEL")
204 UCLocalize("SUBMIT"),
208 [alert setContext:@"submit"];
214 [self reloadURLWithCache:YES];
217 - (void) reloadData {
219 [self reloadURLWithCache:YES];
222 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
225 function_ = function;
227 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
230 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
233 function_ = function;
235 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
238 - (void) removeButton {
239 custom_ = [NSNull null];
240 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
243 - (void) scrollToBottomAnimated:(NSNumber *)animated {
244 CGSize size([scroller_ contentSize]);
245 CGPoint offset([scroller_ contentOffset]);
246 CGRect frame([scroller_ frame]);
248 if (size.height - offset.y < frame.size.height + 20.f) {
249 CGRect rect = {{0, size.height-1}, {size.width, 1}};
250 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
254 - (void) _setViewportWidth {
255 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
258 - (void) setViewportWidth:(float)width {
259 width_ = width != 0 ? width : [[self class] defaultWidth];
260 [self _setViewportWidth];
263 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
264 [self setViewportWidth:[width floatValue]];
267 - (void) setViewportWidthOnMainThread:(float)width {
268 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
271 - (void) webViewUpdateViewSettings:(UIWebView *)view {
272 [self _setViewportWidth];
275 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
276 [self dismissModalViewControllerAnimated:YES];
279 - (void) _openMailToURL:(NSURL *)url {
280 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
281 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
282 [controller setMailComposeDelegate:self];
284 [controller setMailToURL:url];
286 [self presentModalViewController:controller animated:YES];
290 UIApplication *app([UIApplication sharedApplication]);
291 if ([app respondsToSelector:@selector(openURL:asPanel:)])
292 [app openURL:url asPanel:YES];
297 - (bool) _allowJavaScriptPanel {
301 - (bool) allowsNavigationAction {
302 return allowsNavigationAction_;
305 - (void) setAllowsNavigationAction:(bool)value {
306 allowsNavigationAction_ = value;
309 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
310 [self setAllowsNavigationAction:[value boolValue]];
313 - (void) popViewControllerWithNumber:(NSNumber *)value {
314 UINavigationController *navigation([self navigationController]);
315 if ([navigation topViewController] == self)
316 [navigation popViewControllerAnimated:[value boolValue]];
319 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
320 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
321 [self _didFinishLoading];
323 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
326 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
327 request_ = (id) stage2_;
333 if ([frame parentFrame] == nil) {
334 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
335 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
336 [[error localizedDescription] stringByAddingPercentEscapes]
343 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
344 NSURL *url([request URL]);
346 // XXX: filter to internal usage?
347 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
350 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
351 [browser loadRequest:request];
355 [page setDelegate:delegate_];
358 [[self navigationItem] setTitle:title_];
360 [[self navigationController] pushViewController:page animated:YES];
362 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
364 [navigation setDelegate:delegate_];
366 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
367 initWithTitle:UCLocalize("CLOSE")
368 style:UIBarButtonItemStylePlain
370 action:@selector(close)
373 [delegate_ unloadData];
375 [[self navigationController] presentModalViewController:navigation animated:YES];
379 // CyteWebViewDelegate {{{
380 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
382 static Pcre irritating("^(?"
383 ":" "The page at .* displayed insecure content from .*\\."
384 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
387 if (NSString *data = [message objectForKey:@"message"])
388 if (irritating(data))
391 NSLog(@"addMessageToConsole:%@", message);
395 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
397 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
400 if ([frame parentFrame] == nil) {
402 NSURL *url(request == nil ? nil : [request URL]);
404 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
406 [self pushRequest:request asPop:NO];
413 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
414 if ([frame parentFrame] == nil)
415 if (decision == CYWebPolicyDecisionUse)
417 stage1_ = (id) request_;
422 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
424 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
427 NSURL *url([request URL]);
431 if ([frame isEqualToString:@"_open"])
432 [delegate_ openURL:url];
434 NSString *scheme([[url scheme] lowercaseString]);
435 if ([scheme isEqualToString:@"mailto"])
436 [self _openMailToURL:url];
438 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
444 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
447 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
449 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
452 [self _didFailWithError:error forFrame:frame];
455 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
457 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
460 [self _didFailWithError:error forFrame:frame];
463 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
464 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
466 if ([frame parentFrame] == nil) {
470 if (DOMDocument *document = [frame DOMDocument])
471 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
472 for (DOMHTMLBodyElement *body in (id) bodies) {
473 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
477 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
478 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
479 DOMRGBColor *rgb([color getRGBColorValue]);
481 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
482 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
483 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
484 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
486 if (red == 0xc7 && green == 0xce && blue == 0xd5)
487 uic = [UIColor pinStripeColor];
490 colorWithRed:(red / 255)
498 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
503 [self _didFinishLoading];
506 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
507 if ([frame parentFrame] != nil)
512 [[self navigationItem] setTitle:title_];
515 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
517 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
520 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
522 if ([frame parentFrame] == nil) {
528 stage2_ = (id) stage1_;
531 [self setHidesNavigationBar:NO];
533 // XXX: do we still need to do this?
534 [[self navigationItem] setTitle:nil];
537 [self _didStartLoading];
540 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
542 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
548 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
549 return [self _allowJavaScriptPanel];
552 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
553 return [self _allowJavaScriptPanel];
556 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
557 return [self _allowJavaScriptPanel];
560 - (void) webViewClose:(WebView *)view {
566 [[self navigationController] dismissModalViewControllerAnimated:YES];
569 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
570 NSString *context([alert context]);
572 if ([context isEqualToString:@"sensitive"]) {
575 sensitive_ = [NSNumber numberWithBool:YES];
579 sensitive_ = [NSNumber numberWithBool:NO];
583 [alert dismissWithClickedButtonIndex:-1 animated:YES];
584 } else if ([context isEqualToString:@"challenge"]) {
585 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
589 NSString *username([[alert textFieldAtIndex:0] text]);
590 NSString *password([[alert textFieldAtIndex:1] text]);
592 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
594 [sender useCredential:credential forAuthenticationChallenge:challenge_];
598 [sender cancelAuthenticationChallenge:challenge_];
606 [alert dismissWithClickedButtonIndex:-1 animated:YES];
607 } else if ([context isEqualToString:@"submit"]) {
608 if (button == [alert cancelButtonIndex]) {
609 } else if (button == [alert firstOtherButtonIndex]) {
610 if (request_ != nil) {
611 WebThreadLocked lock;
612 [webview_ loadRequest:request_];
616 [alert dismissWithClickedButtonIndex:-1 animated:YES];
620 - (UIBarButtonItemStyle) rightButtonStyle {
621 if (style_ == nil) normal:
622 return UIBarButtonItemStylePlain;
623 else if ([style_ isEqualToString:@"Normal"])
624 return UIBarButtonItemStylePlain;
625 else if ([style_ isEqualToString:@"Highlighted"])
626 return UIBarButtonItemStyleDone;
630 - (UIBarButtonItem *) customButton {
633 else if (custom_ == [NSNull null])
634 return (UIBarButtonItem *) [NSNull null];
636 return [[[UIBarButtonItem alloc]
637 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
638 style:[self rightButtonStyle]
640 action:@selector(customButtonClicked)
644 - (UIBarButtonItem *) leftButton {
645 UINavigationItem *item([self navigationItem]);
646 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
649 if (UINavigationController *navigation = [self navigationController])
650 if ([[navigation parentViewController] modalViewController] == navigation)
651 return [[[UIBarButtonItem alloc]
652 initWithTitle:UCLocalize("CLOSE")
653 style:UIBarButtonItemStylePlain
655 action:@selector(close)
661 - (void) applyLeftButton {
662 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
665 - (UIBarButtonItem *) rightButton {
669 - (void) applyLoadingTitle {
670 [[self navigationItem] setTitle:UCLocalize("LOADING")];
673 - (void) layoutRightButton {
674 [[loadingitem_ view] addSubview:indicator_];
675 [[loadingitem_ view] bringSubviewToFront:indicator_];
678 - (void) applyRightButton {
679 if ([self isLoading]) {
680 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
681 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
683 [indicator_ startAnimating];
684 [self applyLoadingTitle];
686 [indicator_ stopAnimating];
688 UIBarButtonItem *button([self customButton]);
690 button = [self rightButton];
691 else if (button == (UIBarButtonItem *) [NSNull null])
694 [[self navigationItem] setRightBarButtonItem:button];
698 - (void) didStartLoading {
699 // Overridden in subclasses.
702 - (void) _didStartLoading {
703 [self applyRightButton];
705 if ([loading_ count] != 1)
708 [delegate_ retainNetworkActivityIndicator];
709 [self didStartLoading];
712 - (void) didFinishLoading {
713 // Overridden in subclasses.
716 - (void) _didFinishLoading {
717 if ([loading_ count] != 0)
720 [self applyRightButton];
721 [[self navigationItem] setTitle:title_];
723 [delegate_ releaseNetworkActivityIndicator];
724 [self didFinishLoading];
728 return [loading_ count] != 0;
731 - (id) initWithWidth:(float)width ofClass:(Class)_class {
732 if ((self = [super init]) != nil) {
733 allowsNavigationAction_ = true;
736 loading_ = [NSMutableSet setWithCapacity:5];
738 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
740 CGRect bounds([[self view] bounds]);
742 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
743 [webview_ setDelegate:self];
744 [self setView:webview_];
746 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
747 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
749 [webview_ setDetectsPhoneNumbers:NO];
751 [webview_ setScalesPageToFit:YES];
753 UIWebDocumentView *document([webview_ _documentView]);
755 // XXX: I think this improves scrolling; the hardcoded-ness sucks
756 [document setTileSize:CGSizeMake(320, 500)];
758 [document setBackgroundColor:[UIColor clearColor]];
760 // XXX: this is terribly (too?) expensive
761 [document setDrawsBackground:NO];
763 WebView *webview([document webView]);
764 WebPreferences *preferences([webview preferences]);
766 // XXX: I have no clue if I actually /want/ this modification
767 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
768 [webview _setLayoutInterval:0];
769 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
770 [preferences _setLayoutInterval:0];
772 [preferences setCacheModel:WebCacheModelDocumentBrowser];
773 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
774 [preferences setOfflineWebApplicationCacheEnabled:YES];
777 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
778 [document setAllowsMessaging:YES];
779 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
780 [webview _setAllowsMessaging:YES];
783 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
784 scroller_ = [webview_ _scrollView];
786 [scroller_ setDirectionalLockEnabled:YES];
787 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
788 [scroller_ setDelaysContentTouches:NO];
790 [scroller_ setCanCancelContentTouches:YES];
791 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
792 UIScroller *scroller([webview_ _scroller]);
793 scroller_ = (UIScrollView *) scroller;
795 [scroller setDirectionalScrolling:YES];
796 // XXX: we might be better off /not/ setting this on older systems
797 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
798 [scroller setScrollHysteresis:0]; /* 8 */
800 [scroller setThumbDetectionEnabled:NO];
802 // use NO with UIApplicationUseLegacyEvents(YES)
803 [scroller setEventMode:YES];
805 // XXX: this is handled by setBounces, right?
806 //[scroller setAllowsRubberBanding:YES];
809 [scroller_ setFixedBackgroundPattern:YES];
810 [scroller_ setBackgroundColor:[UIColor clearColor]];
811 [scroller_ setClipsSubviews:YES];
813 [scroller_ setBounces:YES];
814 [scroller_ setScrollingEnabled:YES];
815 [scroller_ setShowBackgroundShadow:NO];
817 [self setViewportWidth:width];
819 reloaditem_ = [[[UIBarButtonItem alloc]
820 initWithTitle:UCLocalize("RELOAD")
821 style:[self rightButtonStyle]
823 action:@selector(reloadButtonClicked)
826 loadingitem_ = [[[UIBarButtonItem alloc]
828 style:UIBarButtonItemStylePlain
830 action:@selector(reloadButtonClicked)
833 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
834 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
836 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
837 [webview_ insertSubview:table atIndex:0];
839 [self applyLeftButton];
840 [self applyRightButton];
842 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
843 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
844 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
848 - (id) initWithWidth:(float)width {
849 return [self initWithWidth:width ofClass:[self class]];
853 return [self initWithWidth:0];
856 - (id) initWithURL:(NSURL *)url {
857 if ((self = [self init]) != nil) {
862 - (void) callFunction:(WebScriptObject *)function {
863 WebThreadLocked lock;
865 WebView *webview([[webview_ _documentView] webView]);
866 WebFrame *frame([webview mainFrame]);
868 JSGlobalContextRef context([frame globalContext]);
869 JSObjectRef object([function JSObject]);
870 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
873 - (void) reloadButtonClicked {
874 [self reloadURLWithCache:YES];
877 - (void) _customButtonClicked {
878 [self reloadButtonClicked];
881 - (void) customButtonClicked {
883 if (function_ != nil)
884 [self callFunction:function_];
887 [self _customButtonClicked];
890 + (float) defaultWidth {
894 - (void) setNavigationBarStyle:(NSString *)name {
896 if ([name isEqualToString:@"Black"])
897 style = UIBarStyleBlack;
899 style = UIBarStyleDefault;
901 [[[self navigationController] navigationBar] setBarStyle:style];
904 - (void) setNavigationBarTintColor:(UIColor *)color {
905 [[[self navigationController] navigationBar] setTintColor:color];
908 - (void) setBadgeValue:(id)value {
909 [[[self navigationController] tabBarItem] setBadgeValue:value];
912 - (void) setHidesBackButton:(bool)value {
913 [[self navigationItem] setHidesBackButton:value];
914 [self applyLeftButton];
917 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
918 [self setHidesBackButton:[value boolValue]];
921 - (void) dispatchEvent:(NSString *)event {
922 [webview_ dispatchEvent:event];
925 - (bool) hidesNavigationBar {
926 return hidesNavigationBar_;
929 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
931 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
934 - (void) setHidesNavigationBar:(bool)value {
935 if (hidesNavigationBar_ != value) {
936 hidesNavigationBar_ = value;
937 [self _setHidesNavigationBar:YES animated:YES];
941 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
942 [self setHidesNavigationBar:[value boolValue]];
945 - (void) viewWillAppear:(BOOL)animated {
948 if ([self hidesNavigationBar])
949 [self _setHidesNavigationBar:YES animated:animated];
951 [self dispatchEvent:@"CydiaViewWillAppear"];
952 [super viewWillAppear:animated];
955 - (void) viewDidAppear:(BOOL)animated {
956 [super viewDidAppear:animated];
957 [self dispatchEvent:@"CydiaViewDidAppear"];
960 - (void) viewWillDisappear:(BOOL)animated {
961 [self dispatchEvent:@"CydiaViewWillDisappear"];
962 [super viewWillDisappear:animated];
964 if ([self hidesNavigationBar])
965 [self _setHidesNavigationBar:NO animated:animated];
970 - (void) viewDidDisappear:(BOOL)animated {
971 [super viewDidDisappear:animated];
972 [self dispatchEvent:@"CydiaViewDidDisappear"];