1 #include "CyteKit/UCPlatform.h"
3 #include <UIKit/UIKit.h>
4 #include "iPhonePrivate.h"
6 #include "CyteKit/Localize.h"
7 #include "CyteKit/WebViewController.h"
8 #include "CyteKit/PerlCompatibleRegEx.hpp"
9 #include "CyteKit/WebThreadLocked.hpp"
11 //#include <QuartzCore/CALayer.h>
12 // XXX: fix the minimum requirement
13 extern NSString * const kCAFilterNearest;
15 #include <WebCore/WebCoreThread.h>
17 #include <WebKit/WebPreferences.h>
19 #include <WebKit/DOMCSSPrimitiveValue.h>
20 #include <WebKit/DOMCSSStyleDeclaration.h>
21 #include <WebKit/DOMDocument.h>
22 #include <WebKit/DOMHTMLBodyElement.h>
23 #include <WebKit/DOMRGBColor.h>
26 #define DefaultTimeout_ 120.0
28 #define ShowInternals 0
32 #define lprintf(args...) fprintf(stderr, args)
34 template <typename Type_>
35 static inline void CYRelease(Type_ &value) {
42 float CYScrollViewDecelerationRateNormal;
44 @interface WebView (Apple)
45 - (void) _setLayoutInterval:(float)interval;
46 - (void) _setAllowsMessaging:(BOOL)allows;
49 @interface WebPreferences (Apple)
50 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
51 - (void) _setLayoutInterval:(NSInteger)interval;
52 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
55 /* Indirect Delegate {{{ */
56 @interface IndirectDelegate : NSObject {
57 _transient volatile id delegate_;
60 - (void) setDelegate:(id)delegate;
61 - (id) initWithDelegate:(id)delegate;
64 @implementation IndirectDelegate
66 - (void) setDelegate:(id)delegate {
70 - (id) initWithDelegate:(id)delegate {
75 - (IMP) methodForSelector:(SEL)sel {
76 if (IMP method = [super methodForSelector:sel])
78 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
82 - (BOOL) respondsToSelector:(SEL)sel {
83 if ([super respondsToSelector:sel])
86 // XXX: WebThreadCreateNSInvocation returns nil
89 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
92 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
95 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
96 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
100 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
103 if (delegate_ != nil)
104 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
107 // XXX: I fucking hate Apple so very very bad
108 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
111 - (void) forwardInvocation:(NSInvocation *)inv {
112 SEL sel = [inv selector];
113 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
114 [inv invokeWithTarget:delegate_];
120 @implementation CyteWebViewController
123 #include "CyteKit/UCInternal.h"
126 + (void) _initialize {
127 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
129 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
130 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
131 else // XXX: this actually might be fast on some older systems: we should look into this
132 CYScrollViewDecelerationRateNormal = 0.998;
137 NSLog(@"[CyteWebViewController dealloc]");
140 [webview_ setDelegate:nil];
142 [indirect_ setDelegate:nil];
145 if (challenge_ != nil)
146 [challenge_ release];
151 if ([loading_ count] != 0)
152 [delegate_ releaseNetworkActivityIndicator];
155 [reloaditem_ release];
156 [loadingitem_ release];
158 [indicator_ release];
163 - (NSURL *) URLWithURL:(NSURL *)url {
167 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
169 requestWithURL:[self URLWithURL:url]
171 timeoutInterval:DefaultTimeout_
175 - (void) setURL:(NSURL *)url {
176 _assert(request_ == nil);
177 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
180 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
181 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
184 - (void) loadURL:(NSURL *)url {
185 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
188 - (void) loadRequest:(NSURLRequest *)request {
190 NSLog(@"loadRequest:%@", request);
195 WebThreadLocked lock;
196 [webview_ loadRequest:request];
199 - (void) reloadURLWithCache:(BOOL)cache {
203 NSMutableURLRequest *request([request_ mutableCopy]);
204 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
208 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
209 [self loadRequest:request_];
211 UIAlertView *alert = [[[UIAlertView alloc]
212 initWithTitle:UCLocalize("RESUBMIT_FORM")
215 cancelButtonTitle:UCLocalize("CANCEL")
217 UCLocalize("SUBMIT"),
221 [alert setContext:@"submit"];
227 [self reloadURLWithCache:YES];
230 - (void) reloadData {
232 [self reloadURLWithCache:YES];
235 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
238 function_ = function;
240 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
243 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
246 function_ = function;
248 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
251 - (void) removeButton {
252 custom_ = [NSNull null];
253 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
256 - (void) scrollToBottomAnimated:(NSNumber *)animated {
257 CGSize size([scroller_ contentSize]);
258 CGPoint offset([scroller_ contentOffset]);
259 CGRect frame([scroller_ frame]);
261 if (size.height - offset.y < frame.size.height + 20.f) {
262 CGRect rect = {{0, size.height-1}, {size.width, 1}};
263 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
267 - (void) _setViewportWidth {
268 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
271 - (void) setViewportWidth:(float)width {
272 width_ = width != 0 ? width : [[self class] defaultWidth];
273 [self _setViewportWidth];
276 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
277 [self setViewportWidth:[width floatValue]];
280 - (void) setViewportWidthOnMainThread:(float)width {
281 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
284 - (void) webViewUpdateViewSettings:(UIWebView *)view {
285 [self _setViewportWidth];
288 - (void) _openMailToURL:(NSURL *)url {
289 UIApplication *app([UIApplication sharedApplication]);
290 if ([app respondsToSelector:@selector(openURL:asPanel:)])
291 [app openURL:url asPanel:YES];
296 - (bool) _allowJavaScriptPanel {
300 - (bool) allowsNavigationAction {
301 return allowsNavigationAction_;
304 - (void) setAllowsNavigationAction:(bool)value {
305 allowsNavigationAction_ = value;
308 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
309 [self setAllowsNavigationAction:[value boolValue]];
312 - (void) popViewControllerWithNumber:(NSNumber *)value {
313 UINavigationController *navigation([self navigationController]);
314 if ([navigation topViewController] == self)
315 [navigation popViewControllerAnimated:[value boolValue]];
318 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
319 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
320 [self _didFinishLoading];
322 if ([error code] == NSURLErrorCancelled)
325 if ([frame parentFrame] == nil) {
326 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
327 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
328 [[error localizedDescription] stringByAddingPercentEscapes]
335 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
336 NSURL *url([request URL]);
338 // XXX: filter to internal usage?
339 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
342 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
343 [browser loadRequest:request];
347 [page setDelegate:delegate_];
350 [[self navigationItem] setTitle:title_];
352 [[self navigationController] pushViewController:page animated:YES];
354 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
356 [navigation setDelegate:delegate_];
358 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
359 initWithTitle:UCLocalize("CLOSE")
360 style:UIBarButtonItemStylePlain
362 action:@selector(close)
365 [[self navigationController] presentModalViewController:navigation animated:YES];
367 [delegate_ unloadData];
371 // CyteWebViewDelegate {{{
372 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
374 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$");
375 if (NSString *data = [message objectForKey:@"message"])
376 if (irritating(data))
379 NSLog(@"addMessageToConsole:%@", message);
383 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
385 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
388 if ([frame parentFrame] == nil) {
390 NSURL *url(request == nil ? nil : [request URL]);
392 if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
396 [self pushRequest:request asPop:NO];
403 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
405 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
408 NSURL *url([request URL]);
412 if ([frame isEqualToString:@"_open"])
413 [delegate_ openURL:url];
415 NSString *scheme([[url scheme] lowercaseString]);
416 if ([scheme isEqualToString:@"mailto"])
417 [self _openMailToURL:url];
419 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
425 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
428 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
430 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
433 [self _didFailWithError:error forFrame:frame];
436 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
438 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
441 [self _didFailWithError:error forFrame:frame];
444 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
445 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
447 if ([frame parentFrame] == nil) {
448 if (DOMDocument *document = [frame DOMDocument])
449 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
450 for (DOMHTMLBodyElement *body in (id) bodies) {
451 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
455 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
456 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
457 DOMRGBColor *rgb([color getRGBColorValue]);
459 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
460 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
461 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
462 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
464 if (red == 0xc7 && green == 0xce && blue == 0xd5)
465 uic = [UIColor pinStripeColor];
468 colorWithRed:(red / 255)
476 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
481 [self _didFinishLoading];
484 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
485 if ([frame parentFrame] != nil)
489 [title_ autorelease];
490 title_ = [title retain];
492 [[self navigationItem] setTitle:title_];
495 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
496 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
498 if ([frame parentFrame] == nil) {
504 [self setHidesNavigationBar:NO];
506 // XXX: do we still need to do this?
507 [[self navigationItem] setTitle:nil];
510 [self _didStartLoading];
513 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
515 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
521 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
522 return [self _allowJavaScriptPanel];
525 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
526 return [self _allowJavaScriptPanel];
529 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
530 return [self _allowJavaScriptPanel];
533 - (void) webViewClose:(WebView *)view {
539 [[self navigationController] dismissModalViewControllerAnimated:YES];
542 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
543 NSString *context([alert context]);
545 if ([context isEqualToString:@"sensitive"]) {
548 sensitive_ = [NSNumber numberWithBool:YES];
552 sensitive_ = [NSNumber numberWithBool:NO];
556 [alert dismissWithClickedButtonIndex:-1 animated:YES];
557 } else if ([context isEqualToString:@"challenge"]) {
558 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
562 NSString *username([[alert textFieldAtIndex:0] text]);
563 NSString *password([[alert textFieldAtIndex:1] text]);
565 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
567 [sender useCredential:credential forAuthenticationChallenge:challenge_];
571 [sender cancelAuthenticationChallenge:challenge_];
577 [challenge_ release];
580 [alert dismissWithClickedButtonIndex:-1 animated:YES];
581 } else if ([context isEqualToString:@"submit"]) {
582 if (button == [alert cancelButtonIndex]) {
583 } else if (button == [alert firstOtherButtonIndex]) {
584 if (request_ != nil) {
585 WebThreadLocked lock;
586 [webview_ loadRequest:request_];
590 [alert dismissWithClickedButtonIndex:-1 animated:YES];
594 - (UIBarButtonItemStyle) rightButtonStyle {
595 if (style_ == nil) normal:
596 return UIBarButtonItemStylePlain;
597 else if ([style_ isEqualToString:@"Normal"])
598 return UIBarButtonItemStylePlain;
599 else if ([style_ isEqualToString:@"Highlighted"])
600 return UIBarButtonItemStyleDone;
604 - (UIBarButtonItem *) customButton {
607 else if (custom_ == [NSNull null])
608 return (UIBarButtonItem *) [NSNull null];
610 return [[[UIBarButtonItem alloc]
611 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
612 style:[self rightButtonStyle]
614 action:@selector(customButtonClicked)
618 - (UIBarButtonItem *) leftButton {
619 UINavigationItem *item([self navigationItem]);
620 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
623 if (UINavigationController *navigation = [self navigationController])
624 if ([[navigation parentViewController] modalViewController] == navigation)
625 return [[[UIBarButtonItem alloc]
626 initWithTitle:UCLocalize("CLOSE")
627 style:UIBarButtonItemStylePlain
629 action:@selector(close)
635 - (void) applyLeftButton {
636 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
639 - (UIBarButtonItem *) rightButton {
643 - (void) applyLoadingTitle {
644 [[self navigationItem] setTitle:UCLocalize("LOADING")];
647 - (void) layoutRightButton {
648 [[loadingitem_ view] addSubview:indicator_];
649 [[loadingitem_ view] bringSubviewToFront:indicator_];
652 - (void) applyRightButton {
653 if ([self isLoading]) {
654 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
655 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
657 [indicator_ startAnimating];
658 [self applyLoadingTitle];
660 [indicator_ stopAnimating];
662 UIBarButtonItem *button([self customButton]);
664 button = [self rightButton];
665 else if (button == (UIBarButtonItem *) [NSNull null])
668 [[self navigationItem] setRightBarButtonItem:button];
672 - (void) didStartLoading {
673 // Overridden in subclasses.
676 - (void) _didStartLoading {
677 [self applyRightButton];
679 if ([loading_ count] != 1)
682 [delegate_ retainNetworkActivityIndicator];
683 [self didStartLoading];
686 - (void) didFinishLoading {
687 // Overridden in subclasses.
690 - (void) _didFinishLoading {
691 if ([loading_ count] != 0)
694 [self applyRightButton];
695 [[self navigationItem] setTitle:title_];
697 [delegate_ releaseNetworkActivityIndicator];
698 [self didFinishLoading];
702 return [loading_ count] != 0;
705 - (id) initWithWidth:(float)width ofClass:(Class)_class {
706 if ((self = [super init]) != nil) {
707 allowsNavigationAction_ = true;
710 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
712 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
714 CGRect bounds([[self view] bounds]);
716 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
717 [webview_ setDelegate:self];
718 [self setView:webview_];
720 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
721 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
723 [webview_ setDetectsPhoneNumbers:NO];
725 [webview_ setScalesPageToFit:YES];
727 UIWebDocumentView *document([webview_ _documentView]);
729 // XXX: I think this improves scrolling; the hardcoded-ness sucks
730 [document setTileSize:CGSizeMake(320, 500)];
732 [document setBackgroundColor:[UIColor clearColor]];
734 // XXX: this is terribly (too?) expensive
735 [document setDrawsBackground:NO];
737 WebView *webview([document webView]);
738 WebPreferences *preferences([webview preferences]);
740 // XXX: I have no clue if I actually /want/ this modification
741 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
742 [webview _setLayoutInterval:0];
743 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
744 [preferences _setLayoutInterval:0];
746 [preferences setCacheModel:WebCacheModelDocumentBrowser];
747 [preferences setOfflineWebApplicationCacheEnabled:YES];
750 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
751 [document setAllowsMessaging:YES];
752 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
753 [webview _setAllowsMessaging:YES];
756 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
757 scroller_ = [webview_ _scrollView];
759 [scroller_ setDirectionalLockEnabled:YES];
760 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
761 [scroller_ setDelaysContentTouches:NO];
763 [scroller_ setCanCancelContentTouches:YES];
764 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
765 UIScroller *scroller([webview_ _scroller]);
766 scroller_ = (UIScrollView *) scroller;
768 [scroller setDirectionalScrolling:YES];
769 // XXX: we might be better off /not/ setting this on older systems
770 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
771 [scroller setScrollHysteresis:0]; /* 8 */
773 [scroller setThumbDetectionEnabled:NO];
775 // use NO with UIApplicationUseLegacyEvents(YES)
776 [scroller setEventMode:YES];
778 // XXX: this is handled by setBounces, right?
779 //[scroller setAllowsRubberBanding:YES];
782 [scroller_ setFixedBackgroundPattern:YES];
783 [scroller_ setBackgroundColor:[UIColor clearColor]];
784 [scroller_ setClipsSubviews:YES];
786 [scroller_ setBounces:YES];
787 [scroller_ setScrollingEnabled:YES];
788 [scroller_ setShowBackgroundShadow:NO];
790 [self setViewportWidth:width];
792 reloaditem_ = [[UIBarButtonItem alloc]
793 initWithTitle:UCLocalize("RELOAD")
794 style:[self rightButtonStyle]
796 action:@selector(reloadButtonClicked)
799 loadingitem_ = [[UIBarButtonItem alloc]
801 style:UIBarButtonItemStylePlain
803 action:@selector(reloadButtonClicked)
806 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
807 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
809 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
810 [webview_ insertSubview:table atIndex:0];
812 [self applyLeftButton];
813 [self applyRightButton];
815 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
816 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
817 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
821 - (id) initWithWidth:(float)width {
822 return [self initWithWidth:width ofClass:[self class]];
826 return [self initWithWidth:0];
829 - (id) initWithURL:(NSURL *)url {
830 if ((self = [self init]) != nil) {
835 - (void) callFunction:(WebScriptObject *)function {
836 WebThreadLocked lock;
838 WebView *webview([[webview_ _documentView] webView]);
839 WebFrame *frame([webview mainFrame]);
840 WebPreferences *preferences([webview preferences]);
842 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
843 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
845 /*id _private(MSHookIvar<id>(webview, "_private"));
846 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
847 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
850 if (settings == NULL)
853 no = settings->JavaScriptCanOpenWindowsAutomatically();
854 settings->setJavaScriptCanOpenWindowsAutomatically(true);
857 if (UIWindow *window = [[self view] window])
858 if (UIResponder *responder = [window firstResponder])
859 [responder resignFirstResponder];
861 JSObjectRef object([function JSObject]);
862 JSGlobalContextRef context([frame globalContext]);
863 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
865 /*if (settings != NULL)
866 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
868 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
871 - (void) reloadButtonClicked {
872 [self reloadURLWithCache:YES];
875 - (void) _customButtonClicked {
876 [self reloadButtonClicked];
879 - (void) customButtonClicked {
881 if (function_ != nil)
882 [self callFunction:function_];
885 [self _customButtonClicked];
888 + (float) defaultWidth {
892 - (void) setNavigationBarStyle:(NSString *)name {
894 if ([name isEqualToString:@"Black"])
895 style = UIBarStyleBlack;
897 style = UIBarStyleDefault;
899 [[[self navigationController] navigationBar] setBarStyle:style];
902 - (void) setNavigationBarTintColor:(UIColor *)color {
903 [[[self navigationController] navigationBar] setTintColor:color];
906 - (void) setBadgeValue:(id)value {
907 [[[self navigationController] tabBarItem] setBadgeValue:value];
910 - (void) setHidesBackButton:(bool)value {
911 [[self navigationItem] setHidesBackButton:value];
912 [self applyLeftButton];
915 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
916 [self setHidesBackButton:[value boolValue]];
919 - (void) dispatchEvent:(NSString *)event {
920 [webview_ dispatchEvent:event];
923 - (bool) hidesNavigationBar {
924 return hidesNavigationBar_;
927 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
929 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
932 - (void) setHidesNavigationBar:(bool)value {
933 if (hidesNavigationBar_ != value) {
934 hidesNavigationBar_ = value;
935 [self _setHidesNavigationBar:YES animated:YES];
939 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
940 [self setHidesNavigationBar:[value boolValue]];
943 - (void) viewWillAppear:(BOOL)animated {
946 if ([self hidesNavigationBar])
947 [self _setHidesNavigationBar:YES animated:animated];
949 [self dispatchEvent:@"CydiaViewWillAppear"];
950 [super viewWillAppear:animated];
953 - (void) viewDidAppear:(BOOL)animated {
954 [super viewDidAppear:animated];
955 [self dispatchEvent:@"CydiaViewDidAppear"];
958 - (void) viewWillDisappear:(BOOL)animated {
959 [self dispatchEvent:@"CydiaViewWillDisappear"];
960 [super viewWillDisappear:animated];
962 if ([self hidesNavigationBar])
963 [self _setHidesNavigationBar:NO animated:animated];
968 - (void) viewDidDisappear:(BOOL)animated {
969 [super viewDidDisappear:animated];
970 [self dispatchEvent:@"CydiaViewDidDisappear"];