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 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 || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
418 [self pushRequest:request asPop:NO];
425 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
427 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
430 NSURL *url([request URL]);
434 if ([frame isEqualToString:@"_open"])
435 [delegate_ openURL:url];
437 NSString *scheme([[url scheme] lowercaseString]);
438 if ([scheme isEqualToString:@"mailto"])
439 [self _openMailToURL:url];
441 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
447 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
450 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
452 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
455 [self _didFailWithError:error forFrame:frame];
458 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
460 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
463 [self _didFailWithError:error forFrame:frame];
466 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
467 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
469 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)
511 [title_ autorelease];
512 title_ = [title retain];
514 [[self navigationItem] setTitle:title_];
517 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
518 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
520 if ([frame parentFrame] == nil) {
526 [self setHidesNavigationBar:NO];
528 // XXX: do we still need to do this?
529 [[self navigationItem] setTitle:nil];
532 [self _didStartLoading];
535 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
537 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
543 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
544 return [self _allowJavaScriptPanel];
547 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
548 return [self _allowJavaScriptPanel];
551 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
552 return [self _allowJavaScriptPanel];
555 - (void) webViewClose:(WebView *)view {
561 [[self navigationController] dismissModalViewControllerAnimated:YES];
564 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
565 NSString *context([alert context]);
567 if ([context isEqualToString:@"sensitive"]) {
570 sensitive_ = [NSNumber numberWithBool:YES];
574 sensitive_ = [NSNumber numberWithBool:NO];
578 [alert dismissWithClickedButtonIndex:-1 animated:YES];
579 } else if ([context isEqualToString:@"challenge"]) {
580 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
584 NSString *username([[alert textFieldAtIndex:0] text]);
585 NSString *password([[alert textFieldAtIndex:1] text]);
587 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
589 [sender useCredential:credential forAuthenticationChallenge:challenge_];
593 [sender cancelAuthenticationChallenge:challenge_];
599 [challenge_ release];
602 [alert dismissWithClickedButtonIndex:-1 animated:YES];
603 } else if ([context isEqualToString:@"submit"]) {
604 if (button == [alert cancelButtonIndex]) {
605 } else if (button == [alert firstOtherButtonIndex]) {
606 if (request_ != nil) {
607 WebThreadLocked lock;
608 [webview_ loadRequest:request_];
612 [alert dismissWithClickedButtonIndex:-1 animated:YES];
616 - (UIBarButtonItemStyle) rightButtonStyle {
617 if (style_ == nil) normal:
618 return UIBarButtonItemStylePlain;
619 else if ([style_ isEqualToString:@"Normal"])
620 return UIBarButtonItemStylePlain;
621 else if ([style_ isEqualToString:@"Highlighted"])
622 return UIBarButtonItemStyleDone;
626 - (UIBarButtonItem *) customButton {
629 else if (custom_ == [NSNull null])
630 return (UIBarButtonItem *) [NSNull null];
632 return [[[UIBarButtonItem alloc]
633 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
634 style:[self rightButtonStyle]
636 action:@selector(customButtonClicked)
640 - (UIBarButtonItem *) leftButton {
641 UINavigationItem *item([self navigationItem]);
642 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
645 if (UINavigationController *navigation = [self navigationController])
646 if ([[navigation parentViewController] modalViewController] == navigation)
647 return [[[UIBarButtonItem alloc]
648 initWithTitle:UCLocalize("CLOSE")
649 style:UIBarButtonItemStylePlain
651 action:@selector(close)
657 - (void) applyLeftButton {
658 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
661 - (UIBarButtonItem *) rightButton {
665 - (void) applyLoadingTitle {
666 [[self navigationItem] setTitle:UCLocalize("LOADING")];
669 - (void) layoutRightButton {
670 [[loadingitem_ view] addSubview:indicator_];
671 [[loadingitem_ view] bringSubviewToFront:indicator_];
674 - (void) applyRightButton {
675 if ([self isLoading]) {
676 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
677 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
679 [indicator_ startAnimating];
680 [self applyLoadingTitle];
682 [indicator_ stopAnimating];
684 UIBarButtonItem *button([self customButton]);
686 button = [self rightButton];
687 else if (button == (UIBarButtonItem *) [NSNull null])
690 [[self navigationItem] setRightBarButtonItem:button];
694 - (void) didStartLoading {
695 // Overridden in subclasses.
698 - (void) _didStartLoading {
699 [self applyRightButton];
701 if ([loading_ count] != 1)
704 [delegate_ retainNetworkActivityIndicator];
705 [self didStartLoading];
708 - (void) didFinishLoading {
709 // Overridden in subclasses.
712 - (void) _didFinishLoading {
713 if ([loading_ count] != 0)
716 [self applyRightButton];
717 [[self navigationItem] setTitle:title_];
719 [delegate_ releaseNetworkActivityIndicator];
720 [self didFinishLoading];
724 return [loading_ count] != 0;
727 - (id) initWithWidth:(float)width ofClass:(Class)_class {
728 if ((self = [super init]) != nil) {
729 allowsNavigationAction_ = true;
732 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
734 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
736 CGRect bounds([[self view] bounds]);
738 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
739 [webview_ setDelegate:self];
740 [self setView:webview_];
742 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
743 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
745 [webview_ setDetectsPhoneNumbers:NO];
747 [webview_ setScalesPageToFit:YES];
749 UIWebDocumentView *document([webview_ _documentView]);
751 // XXX: I think this improves scrolling; the hardcoded-ness sucks
752 [document setTileSize:CGSizeMake(320, 500)];
754 [document setBackgroundColor:[UIColor clearColor]];
756 // XXX: this is terribly (too?) expensive
757 [document setDrawsBackground:NO];
759 WebView *webview([document webView]);
760 WebPreferences *preferences([webview preferences]);
762 // XXX: I have no clue if I actually /want/ this modification
763 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
764 [webview _setLayoutInterval:0];
765 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
766 [preferences _setLayoutInterval:0];
768 [preferences setCacheModel:WebCacheModelDocumentBrowser];
769 [preferences setOfflineWebApplicationCacheEnabled:YES];
772 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
773 [document setAllowsMessaging:YES];
774 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
775 [webview _setAllowsMessaging:YES];
778 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
779 scroller_ = [webview_ _scrollView];
781 [scroller_ setDirectionalLockEnabled:YES];
782 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
783 [scroller_ setDelaysContentTouches:NO];
785 [scroller_ setCanCancelContentTouches:YES];
786 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
787 UIScroller *scroller([webview_ _scroller]);
788 scroller_ = (UIScrollView *) scroller;
790 [scroller setDirectionalScrolling:YES];
791 // XXX: we might be better off /not/ setting this on older systems
792 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
793 [scroller setScrollHysteresis:0]; /* 8 */
795 [scroller setThumbDetectionEnabled:NO];
797 // use NO with UIApplicationUseLegacyEvents(YES)
798 [scroller setEventMode:YES];
800 // XXX: this is handled by setBounces, right?
801 //[scroller setAllowsRubberBanding:YES];
804 [scroller_ setFixedBackgroundPattern:YES];
805 [scroller_ setBackgroundColor:[UIColor clearColor]];
806 [scroller_ setClipsSubviews:YES];
808 [scroller_ setBounces:YES];
809 [scroller_ setScrollingEnabled:YES];
810 [scroller_ setShowBackgroundShadow:NO];
812 [self setViewportWidth:width];
814 reloaditem_ = [[UIBarButtonItem alloc]
815 initWithTitle:UCLocalize("RELOAD")
816 style:[self rightButtonStyle]
818 action:@selector(reloadButtonClicked)
821 loadingitem_ = [[UIBarButtonItem alloc]
823 style:UIBarButtonItemStylePlain
825 action:@selector(reloadButtonClicked)
828 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
829 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
831 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
832 [webview_ insertSubview:table atIndex:0];
834 [self applyLeftButton];
835 [self applyRightButton];
837 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
838 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
839 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
843 - (id) initWithWidth:(float)width {
844 return [self initWithWidth:width ofClass:[self class]];
848 return [self initWithWidth:0];
851 - (id) initWithURL:(NSURL *)url {
852 if ((self = [self init]) != nil) {
857 - (void) callFunction:(WebScriptObject *)function {
858 WebThreadLocked lock;
860 WebView *webview([[webview_ _documentView] webView]);
861 WebFrame *frame([webview mainFrame]);
862 WebPreferences *preferences([webview preferences]);
864 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
865 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
867 /*id _private(MSHookIvar<id>(webview, "_private"));
868 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
869 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
872 if (settings == NULL)
875 no = settings->JavaScriptCanOpenWindowsAutomatically();
876 settings->setJavaScriptCanOpenWindowsAutomatically(true);
879 if (UIWindow *window = [[self view] window])
880 if (UIResponder *responder = [window firstResponder])
881 [responder resignFirstResponder];
883 JSObjectRef object([function JSObject]);
884 JSGlobalContextRef context([frame globalContext]);
885 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
887 /*if (settings != NULL)
888 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
890 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
893 - (void) reloadButtonClicked {
894 [self reloadURLWithCache:YES];
897 - (void) _customButtonClicked {
898 [self reloadButtonClicked];
901 - (void) customButtonClicked {
903 if (function_ != nil)
904 [self callFunction:function_];
907 [self _customButtonClicked];
910 + (float) defaultWidth {
914 - (void) setNavigationBarStyle:(NSString *)name {
916 if ([name isEqualToString:@"Black"])
917 style = UIBarStyleBlack;
919 style = UIBarStyleDefault;
921 [[[self navigationController] navigationBar] setBarStyle:style];
924 - (void) setNavigationBarTintColor:(UIColor *)color {
925 [[[self navigationController] navigationBar] setTintColor:color];
928 - (void) setBadgeValue:(id)value {
929 [[[self navigationController] tabBarItem] setBadgeValue:value];
932 - (void) setHidesBackButton:(bool)value {
933 [[self navigationItem] setHidesBackButton:value];
934 [self applyLeftButton];
937 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
938 [self setHidesBackButton:[value boolValue]];
941 - (void) dispatchEvent:(NSString *)event {
942 [webview_ dispatchEvent:event];
945 - (bool) hidesNavigationBar {
946 return hidesNavigationBar_;
949 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
951 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
954 - (void) setHidesNavigationBar:(bool)value {
955 if (hidesNavigationBar_ != value) {
956 hidesNavigationBar_ = value;
957 [self _setHidesNavigationBar:YES animated:YES];
961 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
962 [self setHidesNavigationBar:[value boolValue]];
965 - (void) viewWillAppear:(BOOL)animated {
968 if ([self hidesNavigationBar])
969 [self _setHidesNavigationBar:YES animated:animated];
971 [self dispatchEvent:@"CydiaViewWillAppear"];
972 [super viewWillAppear:animated];
975 - (void) viewDidAppear:(BOOL)animated {
976 [super viewDidAppear:animated];
977 [self dispatchEvent:@"CydiaViewDidAppear"];
980 - (void) viewWillDisappear:(BOOL)animated {
981 [self dispatchEvent:@"CydiaViewWillDisappear"];
982 [super viewWillDisappear:animated];
984 if ([self hidesNavigationBar])
985 [self _setHidesNavigationBar:NO animated:animated];
990 - (void) viewDidDisappear:(BOOL)animated {
991 [super viewDidDisappear:animated];
992 [self dispatchEvent:@"CydiaViewDidDisappear"];