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/WebPreferences.h>
21 #include <WebKit/DOMCSSPrimitiveValue.h>
22 #include <WebKit/DOMCSSStyleDeclaration.h>
23 #include <WebKit/DOMDocument.h>
24 #include <WebKit/DOMHTMLBodyElement.h>
25 #include <WebKit/DOMRGBColor.h>
28 #define DefaultTimeout_ 120.0
30 #define ShowInternals 0
34 #define lprintf(args...) fprintf(stderr, args)
36 // XXX: centralize these special class things to some file or mechanism?
37 static Class $MFMailComposeViewController;
39 template <typename Type_>
40 static inline void CYRelease(Type_ &value) {
47 float CYScrollViewDecelerationRateNormal;
49 @interface WebView (Apple)
50 - (void) _setLayoutInterval:(float)interval;
51 - (void) _setAllowsMessaging:(BOOL)allows;
54 @interface WebPreferences (Apple)
55 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
56 - (void) _setLayoutInterval:(NSInteger)interval;
57 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
60 /* Indirect Delegate {{{ */
61 @interface IndirectDelegate : NSObject {
62 _transient volatile id delegate_;
65 - (void) setDelegate:(id)delegate;
66 - (id) initWithDelegate:(id)delegate;
69 @implementation IndirectDelegate
71 - (void) setDelegate:(id)delegate {
75 - (id) initWithDelegate:(id)delegate {
80 - (IMP) methodForSelector:(SEL)sel {
81 if (IMP method = [super methodForSelector:sel])
83 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
87 - (BOOL) respondsToSelector:(SEL)sel {
88 if ([super respondsToSelector:sel])
91 // XXX: WebThreadCreateNSInvocation returns nil
94 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
97 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
100 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
101 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
105 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
108 if (delegate_ != nil)
109 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
112 // XXX: I fucking hate Apple so very very bad
113 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
116 - (void) forwardInvocation:(NSInvocation *)inv {
117 SEL sel = [inv selector];
118 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
119 [inv invokeWithTarget:delegate_];
125 @implementation CyteWebViewController
128 #include "CyteKit/UCInternal.h"
131 + (void) _initialize {
132 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
134 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
135 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
137 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
138 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
139 else // XXX: this actually might be fast on some older systems: we should look into this
140 CYScrollViewDecelerationRateNormal = 0.998;
145 NSLog(@"[CyteWebViewController dealloc]");
148 [webview_ setDelegate:nil];
150 [indirect_ setDelegate:nil];
153 if (challenge_ != nil)
154 [challenge_ release];
159 if ([loading_ count] != 0)
160 [delegate_ releaseNetworkActivityIndicator];
163 [reloaditem_ release];
164 [loadingitem_ release];
166 [indicator_ release];
171 - (NSURL *) URLWithURL:(NSURL *)url {
175 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
177 requestWithURL:[self URLWithURL:url]
179 timeoutInterval:DefaultTimeout_
183 - (void) setURL:(NSURL *)url {
184 _assert(request_ == nil);
185 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
188 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
189 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
192 - (void) loadURL:(NSURL *)url {
193 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
196 - (void) loadRequest:(NSURLRequest *)request {
198 NSLog(@"loadRequest:%@", request);
203 WebThreadLocked lock;
204 [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 {
240 [self reloadURLWithCache:YES];
243 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
246 function_ = function;
248 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
251 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
254 function_ = function;
256 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
259 - (void) removeButton {
260 custom_ = [NSNull null];
261 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
264 - (void) scrollToBottomAnimated:(NSNumber *)animated {
265 CGSize size([scroller_ contentSize]);
266 CGPoint offset([scroller_ contentOffset]);
267 CGRect frame([scroller_ frame]);
269 if (size.height - offset.y < frame.size.height + 20.f) {
270 CGRect rect = {{0, size.height-1}, {size.width, 1}};
271 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
275 - (void) _setViewportWidth {
276 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
279 - (void) setViewportWidth:(float)width {
280 width_ = width != 0 ? width : [[self class] defaultWidth];
281 [self _setViewportWidth];
284 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
285 [self setViewportWidth:[width floatValue]];
288 - (void) setViewportWidthOnMainThread:(float)width {
289 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
292 - (void) webViewUpdateViewSettings:(UIWebView *)view {
293 [self _setViewportWidth];
296 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
297 [self dismissModalViewControllerAnimated:YES];
300 - (void) _openMailToURL:(NSURL *)url {
301 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
302 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
303 [controller setMailComposeDelegate:self];
305 [controller setMailToURL:url];
307 [self presentModalViewController:controller animated:YES];
311 UIApplication *app([UIApplication sharedApplication]);
312 if ([app respondsToSelector:@selector(openURL:asPanel:)])
313 [app openURL:url asPanel:YES];
318 - (bool) _allowJavaScriptPanel {
322 - (bool) allowsNavigationAction {
323 return allowsNavigationAction_;
326 - (void) setAllowsNavigationAction:(bool)value {
327 allowsNavigationAction_ = value;
330 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
331 [self setAllowsNavigationAction:[value boolValue]];
334 - (void) popViewControllerWithNumber:(NSNumber *)value {
335 UINavigationController *navigation([self navigationController]);
336 if ([navigation topViewController] == self)
337 [navigation popViewControllerAnimated:[value boolValue]];
340 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
341 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
342 [self _didFinishLoading];
344 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
347 if ([frame parentFrame] == nil) {
348 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
349 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
350 [[error localizedDescription] stringByAddingPercentEscapes]
357 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
358 NSURL *url([request URL]);
360 // XXX: filter to internal usage?
361 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
364 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
365 [browser loadRequest:request];
369 [page setDelegate:delegate_];
372 [[self navigationItem] setTitle:title_];
374 [[self navigationController] pushViewController:page animated:YES];
376 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
378 [navigation setDelegate:delegate_];
380 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
381 initWithTitle:UCLocalize("CLOSE")
382 style:UIBarButtonItemStylePlain
384 action:@selector(close)
387 [delegate_ unloadData];
389 [[self navigationController] presentModalViewController:navigation animated:YES];
393 // CyteWebViewDelegate {{{
394 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
396 static Pcre irritating("^(?:The page at .* displayed insecure content from .*\\.|Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\.)\\n$");
397 if (NSString *data = [message objectForKey:@"message"])
398 if (irritating(data))
401 NSLog(@"addMessageToConsole:%@", message);
405 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
407 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
410 if ([frame parentFrame] == nil) {
412 NSURL *url(request == nil ? nil : [request URL]);
414 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
416 [self pushRequest:request asPop:NO];
423 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
424 if (decision == CYWebPolicyDecisionUse && !error_)
428 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
430 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
433 NSURL *url([request URL]);
437 if ([frame isEqualToString:@"_open"])
438 [delegate_ openURL:url];
440 NSString *scheme([[url scheme] lowercaseString]);
441 if ([scheme isEqualToString:@"mailto"])
442 [self _openMailToURL:url];
444 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
450 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
453 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
455 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
458 [self _didFailWithError:error forFrame:frame];
461 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
463 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
466 [self _didFailWithError:error forFrame:frame];
469 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
470 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
472 if ([frame parentFrame] == nil) {
473 if (DOMDocument *document = [frame DOMDocument])
474 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
475 for (DOMHTMLBodyElement *body in (id) bodies) {
476 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
480 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
481 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
482 DOMRGBColor *rgb([color getRGBColorValue]);
484 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
485 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
486 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
487 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
489 if (red == 0xc7 && green == 0xce && blue == 0xd5)
490 uic = [UIColor pinStripeColor];
493 colorWithRed:(red / 255)
501 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
506 [self _didFinishLoading];
509 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
510 if ([frame parentFrame] != nil)
514 [title_ autorelease];
515 title_ = [title retain];
517 [[self navigationItem] setTitle:title_];
520 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
521 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
523 if ([frame parentFrame] == nil) {
529 [self setHidesNavigationBar:NO];
531 // XXX: do we still need to do this?
532 [[self navigationItem] setTitle:nil];
535 [self _didStartLoading];
538 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
540 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
546 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
547 return [self _allowJavaScriptPanel];
550 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
551 return [self _allowJavaScriptPanel];
554 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
555 return [self _allowJavaScriptPanel];
558 - (void) webViewClose:(WebView *)view {
564 [[self navigationController] dismissModalViewControllerAnimated:YES];
567 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
568 NSString *context([alert context]);
570 if ([context isEqualToString:@"sensitive"]) {
573 sensitive_ = [NSNumber numberWithBool:YES];
577 sensitive_ = [NSNumber numberWithBool:NO];
581 [alert dismissWithClickedButtonIndex:-1 animated:YES];
582 } else if ([context isEqualToString:@"challenge"]) {
583 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
587 NSString *username([[alert textFieldAtIndex:0] text]);
588 NSString *password([[alert textFieldAtIndex:1] text]);
590 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
592 [sender useCredential:credential forAuthenticationChallenge:challenge_];
596 [sender cancelAuthenticationChallenge:challenge_];
602 [challenge_ release];
605 [alert dismissWithClickedButtonIndex:-1 animated:YES];
606 } else if ([context isEqualToString:@"submit"]) {
607 if (button == [alert cancelButtonIndex]) {
608 } else if (button == [alert firstOtherButtonIndex]) {
609 if (request_ != nil) {
610 WebThreadLocked lock;
611 [webview_ loadRequest:request_];
615 [alert dismissWithClickedButtonIndex:-1 animated:YES];
619 - (UIBarButtonItemStyle) rightButtonStyle {
620 if (style_ == nil) normal:
621 return UIBarButtonItemStylePlain;
622 else if ([style_ isEqualToString:@"Normal"])
623 return UIBarButtonItemStylePlain;
624 else if ([style_ isEqualToString:@"Highlighted"])
625 return UIBarButtonItemStyleDone;
629 - (UIBarButtonItem *) customButton {
632 else if (custom_ == [NSNull null])
633 return (UIBarButtonItem *) [NSNull null];
635 return [[[UIBarButtonItem alloc]
636 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
637 style:[self rightButtonStyle]
639 action:@selector(customButtonClicked)
643 - (UIBarButtonItem *) leftButton {
644 UINavigationItem *item([self navigationItem]);
645 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
648 if (UINavigationController *navigation = [self navigationController])
649 if ([[navigation parentViewController] modalViewController] == navigation)
650 return [[[UIBarButtonItem alloc]
651 initWithTitle:UCLocalize("CLOSE")
652 style:UIBarButtonItemStylePlain
654 action:@selector(close)
660 - (void) applyLeftButton {
661 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
664 - (UIBarButtonItem *) rightButton {
668 - (void) applyLoadingTitle {
669 [[self navigationItem] setTitle:UCLocalize("LOADING")];
672 - (void) layoutRightButton {
673 [[loadingitem_ view] addSubview:indicator_];
674 [[loadingitem_ view] bringSubviewToFront:indicator_];
677 - (void) applyRightButton {
678 if ([self isLoading]) {
679 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
680 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
682 [indicator_ startAnimating];
683 [self applyLoadingTitle];
685 [indicator_ stopAnimating];
687 UIBarButtonItem *button([self customButton]);
689 button = [self rightButton];
690 else if (button == (UIBarButtonItem *) [NSNull null])
693 [[self navigationItem] setRightBarButtonItem:button];
697 - (void) didStartLoading {
698 // Overridden in subclasses.
701 - (void) _didStartLoading {
702 [self applyRightButton];
704 if ([loading_ count] != 1)
707 [delegate_ retainNetworkActivityIndicator];
708 [self didStartLoading];
711 - (void) didFinishLoading {
712 // Overridden in subclasses.
715 - (void) _didFinishLoading {
716 if ([loading_ count] != 0)
719 [self applyRightButton];
720 [[self navigationItem] setTitle:title_];
722 [delegate_ releaseNetworkActivityIndicator];
723 [self didFinishLoading];
727 return [loading_ count] != 0;
730 - (id) initWithWidth:(float)width ofClass:(Class)_class {
731 if ((self = [super init]) != nil) {
732 allowsNavigationAction_ = true;
735 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
737 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
739 CGRect bounds([[self view] bounds]);
741 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
742 [webview_ setDelegate:self];
743 [self setView:webview_];
745 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
746 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
748 [webview_ setDetectsPhoneNumbers:NO];
750 [webview_ setScalesPageToFit:YES];
752 UIWebDocumentView *document([webview_ _documentView]);
754 // XXX: I think this improves scrolling; the hardcoded-ness sucks
755 [document setTileSize:CGSizeMake(320, 500)];
757 [document setBackgroundColor:[UIColor clearColor]];
759 // XXX: this is terribly (too?) expensive
760 [document setDrawsBackground:NO];
762 WebView *webview([document webView]);
763 WebPreferences *preferences([webview preferences]);
765 // XXX: I have no clue if I actually /want/ this modification
766 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
767 [webview _setLayoutInterval:0];
768 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
769 [preferences _setLayoutInterval:0];
771 [preferences setCacheModel:WebCacheModelDocumentBrowser];
772 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
773 [preferences setOfflineWebApplicationCacheEnabled:YES];
776 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
777 [document setAllowsMessaging:YES];
778 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
779 [webview _setAllowsMessaging:YES];
782 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
783 scroller_ = [webview_ _scrollView];
785 [scroller_ setDirectionalLockEnabled:YES];
786 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
787 [scroller_ setDelaysContentTouches:NO];
789 [scroller_ setCanCancelContentTouches:YES];
790 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
791 UIScroller *scroller([webview_ _scroller]);
792 scroller_ = (UIScrollView *) scroller;
794 [scroller setDirectionalScrolling:YES];
795 // XXX: we might be better off /not/ setting this on older systems
796 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
797 [scroller setScrollHysteresis:0]; /* 8 */
799 [scroller setThumbDetectionEnabled:NO];
801 // use NO with UIApplicationUseLegacyEvents(YES)
802 [scroller setEventMode:YES];
804 // XXX: this is handled by setBounces, right?
805 //[scroller setAllowsRubberBanding:YES];
808 [scroller_ setFixedBackgroundPattern:YES];
809 [scroller_ setBackgroundColor:[UIColor clearColor]];
810 [scroller_ setClipsSubviews:YES];
812 [scroller_ setBounces:YES];
813 [scroller_ setScrollingEnabled:YES];
814 [scroller_ setShowBackgroundShadow:NO];
816 [self setViewportWidth:width];
818 reloaditem_ = [[UIBarButtonItem alloc]
819 initWithTitle:UCLocalize("RELOAD")
820 style:[self rightButtonStyle]
822 action:@selector(reloadButtonClicked)
825 loadingitem_ = [[UIBarButtonItem alloc]
827 style:UIBarButtonItemStylePlain
829 action:@selector(reloadButtonClicked)
832 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
833 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
835 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
836 [webview_ insertSubview:table atIndex:0];
838 [self applyLeftButton];
839 [self applyRightButton];
841 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
842 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
843 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
847 - (id) initWithWidth:(float)width {
848 return [self initWithWidth:width ofClass:[self class]];
852 return [self initWithWidth:0];
855 - (id) initWithURL:(NSURL *)url {
856 if ((self = [self init]) != nil) {
861 - (void) callFunction:(WebScriptObject *)function {
862 WebThreadLocked lock;
864 WebView *webview([[webview_ _documentView] webView]);
865 WebFrame *frame([webview mainFrame]);
867 JSGlobalContextRef context([frame globalContext]);
868 JSObjectRef object([function JSObject]);
869 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
872 - (void) reloadButtonClicked {
873 [self reloadURLWithCache:YES];
876 - (void) _customButtonClicked {
877 [self reloadButtonClicked];
880 - (void) customButtonClicked {
882 if (function_ != nil)
883 [self callFunction:function_];
886 [self _customButtonClicked];
889 + (float) defaultWidth {
893 - (void) setNavigationBarStyle:(NSString *)name {
895 if ([name isEqualToString:@"Black"])
896 style = UIBarStyleBlack;
898 style = UIBarStyleDefault;
900 [[[self navigationController] navigationBar] setBarStyle:style];
903 - (void) setNavigationBarTintColor:(UIColor *)color {
904 [[[self navigationController] navigationBar] setTintColor:color];
907 - (void) setBadgeValue:(id)value {
908 [[[self navigationController] tabBarItem] setBadgeValue:value];
911 - (void) setHidesBackButton:(bool)value {
912 [[self navigationItem] setHidesBackButton:value];
913 [self applyLeftButton];
916 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
917 [self setHidesBackButton:[value boolValue]];
920 - (void) dispatchEvent:(NSString *)event {
921 [webview_ dispatchEvent:event];
924 - (bool) hidesNavigationBar {
925 return hidesNavigationBar_;
928 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
930 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
933 - (void) setHidesNavigationBar:(bool)value {
934 if (hidesNavigationBar_ != value) {
935 hidesNavigationBar_ = value;
936 [self _setHidesNavigationBar:YES animated:YES];
940 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
941 [self setHidesNavigationBar:[value boolValue]];
944 - (void) viewWillAppear:(BOOL)animated {
947 if ([self hidesNavigationBar])
948 [self _setHidesNavigationBar:YES animated:animated];
950 [self dispatchEvent:@"CydiaViewWillAppear"];
951 [super viewWillAppear:animated];
954 - (void) viewDidAppear:(BOOL)animated {
955 [super viewDidAppear:animated];
956 [self dispatchEvent:@"CydiaViewDidAppear"];
959 - (void) viewWillDisappear:(BOOL)animated {
960 [self dispatchEvent:@"CydiaViewWillDisappear"];
961 [super viewWillDisappear:animated];
963 if ([self hidesNavigationBar])
964 [self _setHidesNavigationBar:NO animated:animated];
969 - (void) viewDidDisappear:(BOOL)animated {
970 [super viewDidDisappear:animated];
971 [self dispatchEvent:@"CydiaViewDidDisappear"];