1 #include "CyteKit/UCPlatform.h"
3 #include <UIKit/UIKit.h>
4 #include "iPhonePrivate.h"
6 #include "CyteKit/CyteLocalize.h"
7 #include "CyteKit/CyteWebViewController.h"
8 #include "CyteKit/PerlCompatibleRegEx.hpp"
10 //#include <QuartzCore/CALayer.h>
11 // XXX: fix the minimum requirement
12 extern NSString * const kCAFilterNearest;
14 #include <WebCore/WebCoreThread.h>
16 #include <WebKit/WebPreferences.h>
18 #include <WebKit/DOMCSSPrimitiveValue.h>
19 #include <WebKit/DOMCSSStyleDeclaration.h>
20 #include <WebKit/DOMDocument.h>
21 #include <WebKit/DOMHTMLBodyElement.h>
22 #include <WebKit/DOMRGBColor.h>
25 #define DefaultTimeout_ 120.0
27 #define ShowInternals 0
31 #define lprintf(args...) fprintf(stderr, args)
33 // WebThreadLocked {{{
34 struct WebThreadLocked {
35 _finline WebThreadLocked() {
39 _finline ~WebThreadLocked() {
45 template <typename Type_>
46 static inline void CYRelease(Type_ &value) {
53 float CYScrollViewDecelerationRateNormal;
55 @interface WebView (Apple)
56 - (void) _setLayoutInterval:(float)interval;
57 - (void) _setAllowsMessaging:(BOOL)allows;
60 @interface WebPreferences (Apple)
61 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
62 - (void) _setLayoutInterval:(NSInteger)interval;
63 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
66 /* Indirect Delegate {{{ */
67 @interface IndirectDelegate : NSObject {
68 _transient volatile id delegate_;
71 - (void) setDelegate:(id)delegate;
72 - (id) initWithDelegate:(id)delegate;
75 @implementation IndirectDelegate
77 - (void) setDelegate:(id)delegate {
81 - (id) initWithDelegate:(id)delegate {
86 - (IMP) methodForSelector:(SEL)sel {
87 if (IMP method = [super methodForSelector:sel])
89 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
93 - (BOOL) respondsToSelector:(SEL)sel {
94 if ([super respondsToSelector:sel])
97 // XXX: WebThreadCreateNSInvocation returns nil
100 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
103 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
106 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
107 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
111 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
114 if (delegate_ != nil)
115 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
118 // XXX: I fucking hate Apple so very very bad
119 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
122 - (void) forwardInvocation:(NSInvocation *)inv {
123 SEL sel = [inv selector];
124 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
125 [inv invokeWithTarget:delegate_];
131 @implementation BrowserController
134 #include "CyteKit/UCInternal.h"
137 + (void) _initialize {
138 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
140 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
141 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
142 else // XXX: this actually might be fast on some older systems: we should look into this
143 CYScrollViewDecelerationRateNormal = 0.998;
148 NSLog(@"[BrowserController dealloc]");
151 [webview_ setDelegate:nil];
153 [indirect_ setDelegate:nil];
156 if (challenge_ != nil)
157 [challenge_ release];
162 if ([loading_ count] != 0)
163 [delegate_ releaseNetworkActivityIndicator];
166 [reloaditem_ release];
167 [loadingitem_ release];
169 [indicator_ release];
174 - (NSURL *) URLWithURL:(NSURL *)url {
178 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
180 requestWithURL:[self URLWithURL:url]
182 timeoutInterval:DefaultTimeout_
186 - (void) setURL:(NSURL *)url {
187 _assert(request_ == nil);
188 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
191 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
192 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
195 - (void) loadURL:(NSURL *)url {
196 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
199 - (void) loadRequest:(NSURLRequest *)request {
201 NSLog(@"loadRequest:%@", request);
206 WebThreadLocked lock;
207 [webview_ loadRequest:request];
210 - (void) reloadURLWithCache:(BOOL)cache {
214 NSMutableURLRequest *request([request_ mutableCopy]);
215 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
219 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
220 [self loadRequest:request_];
222 UIAlertView *alert = [[[UIAlertView alloc]
223 initWithTitle:UCLocalize("RESUBMIT_FORM")
226 cancelButtonTitle:UCLocalize("CANCEL")
228 UCLocalize("SUBMIT"),
232 [alert setContext:@"submit"];
238 [self reloadURLWithCache:YES];
241 - (void) reloadData {
243 [self reloadURLWithCache:YES];
246 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
249 function_ = function;
251 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
254 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
257 function_ = function;
259 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
262 - (void) removeButton {
263 custom_ = [NSNull null];
264 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
267 - (void) scrollToBottomAnimated:(NSNumber *)animated {
268 CGSize size([scroller_ contentSize]);
269 CGPoint offset([scroller_ contentOffset]);
270 CGRect frame([scroller_ frame]);
272 if (size.height - offset.y < frame.size.height + 20.f) {
273 CGRect rect = {{0, size.height-1}, {size.width, 1}};
274 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
278 - (void) _setViewportWidth {
279 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
282 - (void) setViewportWidth:(float)width {
283 width_ = width != 0 ? width : [[self class] defaultWidth];
284 [self _setViewportWidth];
287 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
288 [self setViewportWidth:[width floatValue]];
291 - (void) setViewportWidthOnMainThread:(float)width {
292 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
295 - (void) webViewUpdateViewSettings:(UIWebView *)view {
296 [self _setViewportWidth];
299 - (void) _openMailToURL:(NSURL *)url {
300 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
303 - (bool) _allowJavaScriptPanel {
307 - (bool) allowsNavigationAction {
308 return allowsNavigationAction_;
311 - (void) setAllowsNavigationAction:(bool)value {
312 allowsNavigationAction_ = value;
315 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
316 [self setAllowsNavigationAction:[value boolValue]];
319 - (void) popViewControllerWithNumber:(NSNumber *)value {
320 UINavigationController *navigation([self navigationController]);
321 if ([navigation topViewController] == self)
322 [navigation popViewControllerAnimated:[value boolValue]];
325 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
326 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
327 [self _didFinishLoading];
329 if ([error code] == NSURLErrorCancelled)
332 if ([frame parentFrame] == nil) {
333 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
334 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
335 [[error localizedDescription] stringByAddingPercentEscapes]
342 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
343 NSURL *url([request URL]);
345 // XXX: filter to internal usage?
346 CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
349 BrowserController *browser([[[class_ alloc] init] autorelease]);
350 [browser loadRequest:request];
354 [page setDelegate:delegate_];
357 [[self navigationItem] setTitle:title_];
359 [[self navigationController] pushViewController:page animated:YES];
361 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
363 [navigation setDelegate:delegate_];
365 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
366 initWithTitle:UCLocalize("CLOSE")
367 style:UIBarButtonItemStylePlain
369 action:@selector(close)
372 [[self navigationController] presentModalViewController:navigation animated:YES];
374 [delegate_ unloadData];
378 // CYWebViewDelegate {{{
379 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
381 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$");
382 if (NSString *data = [message objectForKey:@"message"])
383 if (irritating(data))
386 NSLog(@"addMessageToConsole:%@", message);
390 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
392 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
395 if ([frame parentFrame] == nil) {
397 NSURL *url(request == nil ? nil : [request URL]);
399 if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
403 [self pushRequest:request asPop:NO];
410 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
412 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
415 NSURL *url([request URL]);
419 if ([frame isEqualToString:@"_open"])
420 [delegate_ openURL:url];
422 NSString *scheme([[url scheme] lowercaseString]);
423 if ([scheme isEqualToString:@"mailto"])
424 [self _openMailToURL:url];
426 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
432 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
435 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
437 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
440 [self _didFailWithError:error forFrame:frame];
443 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
445 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
448 [self _didFailWithError:error forFrame:frame];
451 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
452 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
454 if ([frame parentFrame] == nil) {
455 if (DOMDocument *document = [frame DOMDocument])
456 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
457 for (DOMHTMLBodyElement *body in (id) bodies) {
458 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
462 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
463 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
464 DOMRGBColor *rgb([color getRGBColorValue]);
466 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
467 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
468 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
469 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
471 if (red == 0xc7 && green == 0xce && blue == 0xd5)
472 uic = [UIColor pinStripeColor];
475 colorWithRed:(red / 255)
483 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
488 [self _didFinishLoading];
491 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
492 if ([frame parentFrame] != nil)
496 [title_ autorelease];
497 title_ = [title retain];
499 [[self navigationItem] setTitle:title_];
502 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
503 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
505 if ([frame parentFrame] == nil) {
511 [self setHidesNavigationBar:NO];
513 // XXX: do we still need to do this?
514 [[self navigationItem] setTitle:nil];
517 [self _didStartLoading];
520 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
522 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
528 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
529 return [self _allowJavaScriptPanel];
532 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
533 return [self _allowJavaScriptPanel];
536 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
537 return [self _allowJavaScriptPanel];
540 - (void) webViewClose:(WebView *)view {
546 [[self navigationController] dismissModalViewControllerAnimated:YES];
549 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
550 NSString *context([alert context]);
552 if ([context isEqualToString:@"sensitive"]) {
555 sensitive_ = [NSNumber numberWithBool:YES];
559 sensitive_ = [NSNumber numberWithBool:NO];
563 [alert dismissWithClickedButtonIndex:-1 animated:YES];
564 } else if ([context isEqualToString:@"challenge"]) {
565 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
569 NSString *username([[alert textFieldAtIndex:0] text]);
570 NSString *password([[alert textFieldAtIndex:1] text]);
572 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
574 [sender useCredential:credential forAuthenticationChallenge:challenge_];
578 [sender cancelAuthenticationChallenge:challenge_];
584 [challenge_ release];
587 [alert dismissWithClickedButtonIndex:-1 animated:YES];
588 } else if ([context isEqualToString:@"submit"]) {
589 if (button == [alert cancelButtonIndex]) {
590 } else if (button == [alert firstOtherButtonIndex]) {
591 if (request_ != nil) {
592 WebThreadLocked lock;
593 [webview_ loadRequest:request_];
597 [alert dismissWithClickedButtonIndex:-1 animated:YES];
601 - (UIBarButtonItemStyle) rightButtonStyle {
602 if (style_ == nil) normal:
603 return UIBarButtonItemStylePlain;
604 else if ([style_ isEqualToString:@"Normal"])
605 return UIBarButtonItemStylePlain;
606 else if ([style_ isEqualToString:@"Highlighted"])
607 return UIBarButtonItemStyleDone;
611 - (UIBarButtonItem *) customButton {
612 return custom_ == [NSNull null] ? nil : [[[UIBarButtonItem alloc]
613 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
614 style:[self rightButtonStyle]
616 action:@selector(customButtonClicked)
620 - (UIBarButtonItem *) rightButton {
624 - (void) applyLoadingTitle {
625 [[self navigationItem] setTitle:UCLocalize("LOADING")];
628 - (void) layoutRightButton {
629 [[loadingitem_ view] addSubview:indicator_];
630 [[loadingitem_ view] bringSubviewToFront:indicator_];
633 - (void) applyRightButton {
634 if ([self isLoading]) {
635 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
636 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
638 [indicator_ startAnimating];
639 [self applyLoadingTitle];
641 [indicator_ stopAnimating];
643 [[self navigationItem] setRightBarButtonItem:(
644 custom_ != nil ? [self customButton] : [self rightButton]
649 - (void) didStartLoading {
650 // Overridden in subclasses.
653 - (void) _didStartLoading {
654 [self applyRightButton];
656 if ([loading_ count] != 1)
659 [delegate_ retainNetworkActivityIndicator];
660 [self didStartLoading];
663 - (void) didFinishLoading {
664 // Overridden in subclasses.
667 - (void) _didFinishLoading {
668 if ([loading_ count] != 0)
671 [self applyRightButton];
672 [[self navigationItem] setTitle:title_];
674 [delegate_ releaseNetworkActivityIndicator];
675 [self didFinishLoading];
679 return [loading_ count] != 0;
682 - (id) initWithWidth:(float)width ofClass:(Class)_class {
683 if ((self = [super init]) != nil) {
684 allowsNavigationAction_ = true;
687 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
689 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
691 CGRect bounds([[self view] bounds]);
693 webview_ = [[[CYWebView alloc] initWithFrame:bounds] autorelease];
694 [webview_ setDelegate:self];
695 [self setView:webview_];
697 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
698 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
700 [webview_ setDetectsPhoneNumbers:NO];
702 [webview_ setScalesPageToFit:YES];
704 UIWebDocumentView *document([webview_ _documentView]);
706 // XXX: I think this improves scrolling; the hardcoded-ness sucks
707 [document setTileSize:CGSizeMake(320, 500)];
709 [document setBackgroundColor:[UIColor clearColor]];
711 // XXX: this is terribly (too?) expensive
712 [document setDrawsBackground:NO];
714 WebView *webview([document webView]);
715 WebPreferences *preferences([webview preferences]);
717 // XXX: I have no clue if I actually /want/ this modification
718 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
719 [webview _setLayoutInterval:0];
720 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
721 [preferences _setLayoutInterval:0];
723 [preferences setCacheModel:WebCacheModelDocumentBrowser];
724 [preferences setOfflineWebApplicationCacheEnabled:YES];
727 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
728 [document setAllowsMessaging:YES];
729 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
730 [webview _setAllowsMessaging:YES];
733 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
734 scroller_ = [webview_ _scrollView];
736 [scroller_ setDirectionalLockEnabled:YES];
737 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
738 [scroller_ setDelaysContentTouches:NO];
740 [scroller_ setCanCancelContentTouches:YES];
741 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
742 UIScroller *scroller([webview_ _scroller]);
743 scroller_ = (UIScrollView *) scroller;
745 [scroller setDirectionalScrolling:YES];
746 // XXX: we might be better off /not/ setting this on older systems
747 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
748 [scroller setScrollHysteresis:0]; /* 8 */
750 [scroller setThumbDetectionEnabled:NO];
752 // use NO with UIApplicationUseLegacyEvents(YES)
753 [scroller setEventMode:YES];
755 // XXX: this is handled by setBounces, right?
756 //[scroller setAllowsRubberBanding:YES];
759 [scroller_ setFixedBackgroundPattern:YES];
760 [scroller_ setBackgroundColor:[UIColor clearColor]];
761 [scroller_ setClipsSubviews:YES];
763 [scroller_ setBounces:YES];
764 [scroller_ setScrollingEnabled:YES];
765 [scroller_ setShowBackgroundShadow:NO];
767 [self setViewportWidth:width];
769 reloaditem_ = [[UIBarButtonItem alloc]
770 initWithTitle:UCLocalize("RELOAD")
771 style:[self rightButtonStyle]
773 action:@selector(reloadButtonClicked)
776 loadingitem_ = [[UIBarButtonItem alloc]
778 style:UIBarButtonItemStylePlain
780 action:@selector(reloadButtonClicked)
783 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
784 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
786 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
787 [webview_ insertSubview:table atIndex:0];
789 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
790 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
791 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
795 - (id) initWithWidth:(float)width {
796 return [self initWithWidth:width ofClass:[self class]];
800 return [self initWithWidth:0];
803 - (id) initWithURL:(NSURL *)url {
804 if ((self = [self init]) != nil) {
809 - (void) callFunction:(WebScriptObject *)function {
810 WebThreadLocked lock;
812 WebView *webview([[webview_ _documentView] webView]);
813 WebFrame *frame([webview mainFrame]);
814 WebPreferences *preferences([webview preferences]);
816 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
817 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
819 /*id _private(MSHookIvar<id>(webview, "_private"));
820 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
821 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
824 if (settings == NULL)
827 no = settings->JavaScriptCanOpenWindowsAutomatically();
828 settings->setJavaScriptCanOpenWindowsAutomatically(true);
831 if (UIWindow *window = [[self view] window])
832 if (UIResponder *responder = [window firstResponder])
833 [responder resignFirstResponder];
835 JSObjectRef object([function JSObject]);
836 JSGlobalContextRef context([frame globalContext]);
837 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
839 /*if (settings != NULL)
840 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
842 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
845 - (void) reloadButtonClicked {
846 [self reloadURLWithCache:YES];
849 - (void) _customButtonClicked {
850 [self reloadButtonClicked];
853 - (void) customButtonClicked {
855 if (function_ != nil)
856 [self callFunction:function_];
859 [self _customButtonClicked];
862 + (float) defaultWidth {
866 - (void) setNavigationBarStyle:(NSString *)name {
868 if ([name isEqualToString:@"Black"])
869 style = UIBarStyleBlack;
871 style = UIBarStyleDefault;
873 [[[self navigationController] navigationBar] setBarStyle:style];
876 - (void) setNavigationBarTintColor:(UIColor *)color {
877 [[[self navigationController] navigationBar] setTintColor:color];
880 - (void) setBadgeValue:(id)value {
881 [[[self navigationController] tabBarItem] setBadgeValue:value];
884 - (void) setHidesBackButton:(bool)value {
885 [[self navigationItem] setHidesBackButton:value];
888 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
889 [self setHidesBackButton:[value boolValue]];
892 - (void) dispatchEvent:(NSString *)event {
893 WebThreadLocked lock;
895 NSString *script([NSString stringWithFormat:@
897 "var event = this.document.createEvent('Events');"
898 "event.initEvent('%@', false, false);"
899 "this.document.dispatchEvent(event);"
903 NSMutableArray *frames([NSMutableArray arrayWithObjects:
904 [[[webview_ _documentView] webView] mainFrame]
907 while (WebFrame *frame = [frames lastObject]) {
908 WebScriptObject *object([frame windowObject]);
909 [object evaluateWebScript:script];
910 [frames removeLastObject];
911 [frames addObjectsFromArray:[frame childFrames]];
915 - (bool) hidesNavigationBar {
916 return hidesNavigationBar_;
919 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
921 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
924 - (void) setHidesNavigationBar:(bool)value {
925 if (hidesNavigationBar_ != value) {
926 hidesNavigationBar_ = value;
927 [self _setHidesNavigationBar:YES animated:YES];
931 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
932 [self setHidesNavigationBar:[value boolValue]];
935 - (void) viewWillAppear:(BOOL)animated {
938 if ([self hidesNavigationBar])
939 [self _setHidesNavigationBar:YES animated:animated];
941 [self dispatchEvent:@"CydiaViewWillAppear"];
942 [super viewWillAppear:animated];
945 - (void) viewDidAppear:(BOOL)animated {
946 [super viewDidAppear:animated];
947 [self dispatchEvent:@"CydiaViewDidAppear"];
950 - (void) viewWillDisappear:(BOOL)animated {
951 [self dispatchEvent:@"CydiaViewWillDisappear"];
952 [super viewWillDisappear:animated];
954 if ([self hidesNavigationBar])
955 [self _setHidesNavigationBar:NO animated:animated];
960 - (void) viewDidDisappear:(BOOL)animated {
961 [super viewDidDisappear:animated];
962 [self dispatchEvent:@"CydiaViewDidDisappear"];