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 WebScriptObject (UICaboodle)
133 - (NSUInteger) count {
134 id length([self valueForKey:@"length"]);
135 if ([length respondsToSelector:@selector(intValue)])
136 return [length intValue];
141 - (id) objectAtIndex:(unsigned)index {
142 return [self webScriptValueAtIndex:index];
147 @implementation BrowserController
150 #include "CyteKit/UCInternal.h"
153 + (void) _initialize {
154 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
156 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
157 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
158 else // XXX: this actually might be fast on some older systems: we should look into this
159 CYScrollViewDecelerationRateNormal = 0.998;
164 NSLog(@"[BrowserController dealloc]");
167 [webview_ setDelegate:nil];
169 [indirect_ setDelegate:nil];
172 if (challenge_ != nil)
173 [challenge_ release];
178 if ([loading_ count] != 0)
179 [delegate_ releaseNetworkActivityIndicator];
182 [reloaditem_ release];
183 [loadingitem_ release];
185 [indicator_ release];
190 - (NSURL *) URLWithURL:(NSURL *)url {
194 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
196 requestWithURL:[self URLWithURL:url]
198 timeoutInterval:DefaultTimeout_
202 - (void) setURL:(NSURL *)url {
203 _assert(request_ == nil);
204 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
207 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
208 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
211 - (void) loadURL:(NSURL *)url {
212 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
215 - (void) loadRequest:(NSURLRequest *)request {
217 NSLog(@"loadRequest:%@", request);
222 WebThreadLocked lock;
223 [webview_ loadRequest:request];
226 - (void) reloadURLWithCache:(BOOL)cache {
230 NSMutableURLRequest *request([request_ mutableCopy]);
231 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
235 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
236 [self loadRequest:request_];
238 UIAlertView *alert = [[[UIAlertView alloc]
239 initWithTitle:UCLocalize("RESUBMIT_FORM")
242 cancelButtonTitle:UCLocalize("CANCEL")
244 UCLocalize("SUBMIT"),
248 [alert setContext:@"submit"];
254 [self reloadURLWithCache:YES];
257 - (void) reloadData {
259 [self reloadURLWithCache:YES];
262 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
265 function_ = function;
267 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
270 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
273 function_ = function;
275 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
278 - (void) removeButton {
279 custom_ = [NSNull null];
280 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
283 - (void) scrollToBottomAnimated:(NSNumber *)animated {
284 CGSize size([scroller_ contentSize]);
285 CGPoint offset([scroller_ contentOffset]);
286 CGRect frame([scroller_ frame]);
288 if (size.height - offset.y < frame.size.height + 20.f) {
289 CGRect rect = {{0, size.height-1}, {size.width, 1}};
290 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
294 - (void) _setViewportWidth {
295 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
298 - (void) setViewportWidth:(float)width {
299 width_ = width != 0 ? width : [[self class] defaultWidth];
300 [self _setViewportWidth];
303 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
304 [self setViewportWidth:[width floatValue]];
307 - (void) setViewportWidthOnMainThread:(float)width {
308 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
311 - (void) webViewUpdateViewSettings:(UIWebView *)view {
312 [self _setViewportWidth];
315 - (void) _openMailToURL:(NSURL *)url {
316 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
319 - (bool) _allowJavaScriptPanel {
323 - (bool) allowsNavigationAction {
324 return allowsNavigationAction_;
327 - (void) setAllowsNavigationAction:(bool)value {
328 allowsNavigationAction_ = value;
331 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
332 [self setAllowsNavigationAction:[value boolValue]];
335 - (void) popViewControllerWithNumber:(NSNumber *)value {
336 UINavigationController *navigation([self navigationController]);
337 if ([navigation topViewController] == self)
338 [navigation popViewControllerAnimated:[value boolValue]];
341 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
342 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
343 [self _didFinishLoading];
345 if ([error code] == NSURLErrorCancelled)
348 if ([frame parentFrame] == nil) {
349 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
350 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
351 [[error localizedDescription] stringByAddingPercentEscapes]
358 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
359 NSURL *url([request URL]);
361 // XXX: filter to internal usage?
362 CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
365 BrowserController *browser([[[class_ alloc] init] autorelease]);
366 [browser loadRequest:request];
370 [page setDelegate:delegate_];
373 [[self navigationItem] setTitle:title_];
375 [[self navigationController] pushViewController:page animated:YES];
377 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
379 [navigation setDelegate:delegate_];
381 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
382 initWithTitle:UCLocalize("CLOSE")
383 style:UIBarButtonItemStylePlain
385 action:@selector(close)
388 [[self navigationController] presentModalViewController:navigation animated:YES];
390 [delegate_ unloadData];
394 // CYWebViewDelegate {{{
395 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
397 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$");
398 if (NSString *data = [message objectForKey:@"message"])
399 if (irritating(data))
402 NSLog(@"addMessageToConsole:%@", message);
406 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
408 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
411 if ([frame parentFrame] == nil) {
413 NSURL *url(request == nil ? nil : [request URL]);
415 if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
419 [self pushRequest:request asPop:NO];
426 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
428 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
431 NSURL *url([request URL]);
435 if ([frame isEqualToString:@"_open"])
436 [delegate_ openURL:url];
438 NSString *scheme([[url scheme] lowercaseString]);
439 if ([scheme isEqualToString:@"mailto"])
440 [self _openMailToURL:url];
442 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
448 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
451 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
453 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
456 [self _didFailWithError:error forFrame:frame];
459 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
461 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
464 [self _didFailWithError:error forFrame:frame];
467 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
468 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
470 if ([frame parentFrame] == nil) {
471 if (DOMDocument *document = [frame DOMDocument])
472 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
473 for (DOMHTMLBodyElement *body in (id) bodies) {
474 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
478 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
479 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
480 DOMRGBColor *rgb([color getRGBColorValue]);
482 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
483 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
484 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
485 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
487 if (red == 0xc7 && green == 0xce && blue == 0xd5)
488 uic = [UIColor pinStripeColor];
491 colorWithRed:(red / 255)
499 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
504 [self _didFinishLoading];
507 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
508 if ([frame parentFrame] != nil)
512 [title_ autorelease];
513 title_ = [title retain];
515 [[self navigationItem] setTitle:title_];
518 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
519 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
521 if ([frame parentFrame] == nil) {
527 [self setHidesNavigationBar:NO];
529 // XXX: do we still need to do this?
530 [[self navigationItem] setTitle:nil];
533 [self _didStartLoading];
536 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
538 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
544 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
545 return [self _allowJavaScriptPanel];
548 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
549 return [self _allowJavaScriptPanel];
552 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
553 return [self _allowJavaScriptPanel];
556 - (void) webViewClose:(WebView *)view {
562 [[self navigationController] dismissModalViewControllerAnimated:YES];
565 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
566 NSString *context([alert context]);
568 if ([context isEqualToString:@"sensitive"]) {
571 sensitive_ = [NSNumber numberWithBool:YES];
575 sensitive_ = [NSNumber numberWithBool:NO];
579 [alert dismissWithClickedButtonIndex:-1 animated:YES];
580 } else if ([context isEqualToString:@"challenge"]) {
581 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
585 NSString *username([[alert textFieldAtIndex:0] text]);
586 NSString *password([[alert textFieldAtIndex:1] text]);
588 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
590 [sender useCredential:credential forAuthenticationChallenge:challenge_];
594 [sender cancelAuthenticationChallenge:challenge_];
600 [challenge_ release];
603 [alert dismissWithClickedButtonIndex:-1 animated:YES];
604 } else if ([context isEqualToString:@"submit"]) {
605 if (button == [alert cancelButtonIndex]) {
606 } else if (button == [alert firstOtherButtonIndex]) {
607 if (request_ != nil) {
608 WebThreadLocked lock;
609 [webview_ loadRequest:request_];
613 [alert dismissWithClickedButtonIndex:-1 animated:YES];
617 - (UIBarButtonItemStyle) rightButtonStyle {
618 if (style_ == nil) normal:
619 return UIBarButtonItemStylePlain;
620 else if ([style_ isEqualToString:@"Normal"])
621 return UIBarButtonItemStylePlain;
622 else if ([style_ isEqualToString:@"Highlighted"])
623 return UIBarButtonItemStyleDone;
627 - (UIBarButtonItem *) customButton {
628 return custom_ == [NSNull null] ? nil : [[[UIBarButtonItem alloc]
629 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
630 style:[self rightButtonStyle]
632 action:@selector(customButtonClicked)
636 - (UIBarButtonItem *) rightButton {
640 - (void) applyLoadingTitle {
641 [[self navigationItem] setTitle:UCLocalize("LOADING")];
644 - (void) layoutRightButton {
645 [[loadingitem_ view] addSubview:indicator_];
646 [[loadingitem_ view] bringSubviewToFront:indicator_];
649 - (void) applyRightButton {
650 if ([self isLoading]) {
651 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
652 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
654 [indicator_ startAnimating];
655 [self applyLoadingTitle];
657 [indicator_ stopAnimating];
659 [[self navigationItem] setRightBarButtonItem:(
660 custom_ != nil ? [self customButton] : [self rightButton]
665 - (void) didStartLoading {
666 // Overridden in subclasses.
669 - (void) _didStartLoading {
670 [self applyRightButton];
672 if ([loading_ count] != 1)
675 [delegate_ retainNetworkActivityIndicator];
676 [self didStartLoading];
679 - (void) didFinishLoading {
680 // Overridden in subclasses.
683 - (void) _didFinishLoading {
684 if ([loading_ count] != 0)
687 [self applyRightButton];
688 [[self navigationItem] setTitle:title_];
690 [delegate_ releaseNetworkActivityIndicator];
691 [self didFinishLoading];
695 return [loading_ count] != 0;
698 - (id) initWithWidth:(float)width ofClass:(Class)_class {
699 if ((self = [super init]) != nil) {
700 allowsNavigationAction_ = true;
703 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
705 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
707 CGRect bounds([[self view] bounds]);
709 webview_ = [[[CYWebView alloc] initWithFrame:bounds] autorelease];
710 [webview_ setDelegate:self];
711 [self setView:webview_];
713 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
714 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
716 [webview_ setDetectsPhoneNumbers:NO];
718 [webview_ setScalesPageToFit:YES];
720 UIWebDocumentView *document([webview_ _documentView]);
722 // XXX: I think this improves scrolling; the hardcoded-ness sucks
723 [document setTileSize:CGSizeMake(320, 500)];
725 [document setBackgroundColor:[UIColor clearColor]];
727 // XXX: this is terribly (too?) expensive
728 [document setDrawsBackground:NO];
730 WebView *webview([document webView]);
731 WebPreferences *preferences([webview preferences]);
733 // XXX: I have no clue if I actually /want/ this modification
734 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
735 [webview _setLayoutInterval:0];
736 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
737 [preferences _setLayoutInterval:0];
739 [preferences setCacheModel:WebCacheModelDocumentBrowser];
740 [preferences setOfflineWebApplicationCacheEnabled:YES];
743 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
744 [document setAllowsMessaging:YES];
745 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
746 [webview _setAllowsMessaging:YES];
749 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
750 scroller_ = [webview_ _scrollView];
752 [scroller_ setDirectionalLockEnabled:YES];
753 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
754 [scroller_ setDelaysContentTouches:NO];
756 [scroller_ setCanCancelContentTouches:YES];
757 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
758 UIScroller *scroller([webview_ _scroller]);
759 scroller_ = (UIScrollView *) scroller;
761 [scroller setDirectionalScrolling:YES];
762 // XXX: we might be better off /not/ setting this on older systems
763 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
764 [scroller setScrollHysteresis:0]; /* 8 */
766 [scroller setThumbDetectionEnabled:NO];
768 // use NO with UIApplicationUseLegacyEvents(YES)
769 [scroller setEventMode:YES];
771 // XXX: this is handled by setBounces, right?
772 //[scroller setAllowsRubberBanding:YES];
775 [scroller_ setFixedBackgroundPattern:YES];
776 [scroller_ setBackgroundColor:[UIColor clearColor]];
777 [scroller_ setClipsSubviews:YES];
779 [scroller_ setBounces:YES];
780 [scroller_ setScrollingEnabled:YES];
781 [scroller_ setShowBackgroundShadow:NO];
783 [self setViewportWidth:width];
785 reloaditem_ = [[UIBarButtonItem alloc]
786 initWithTitle:UCLocalize("RELOAD")
787 style:[self rightButtonStyle]
789 action:@selector(reloadButtonClicked)
792 loadingitem_ = [[UIBarButtonItem alloc]
794 style:UIBarButtonItemStylePlain
796 action:@selector(reloadButtonClicked)
799 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
800 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
802 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
803 [webview_ insertSubview:table atIndex:0];
805 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
806 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
807 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
811 - (id) initWithWidth:(float)width {
812 return [self initWithWidth:width ofClass:[self class]];
816 return [self initWithWidth:0];
819 - (id) initWithURL:(NSURL *)url {
820 if ((self = [self init]) != nil) {
825 - (void) callFunction:(WebScriptObject *)function {
826 WebThreadLocked lock;
828 WebView *webview([[webview_ _documentView] webView]);
829 WebFrame *frame([webview mainFrame]);
830 WebPreferences *preferences([webview preferences]);
832 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
833 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
835 /*id _private(MSHookIvar<id>(webview, "_private"));
836 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
837 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
840 if (settings == NULL)
843 no = settings->JavaScriptCanOpenWindowsAutomatically();
844 settings->setJavaScriptCanOpenWindowsAutomatically(true);
847 if (UIWindow *window = [[self view] window])
848 if (UIResponder *responder = [window firstResponder])
849 [responder resignFirstResponder];
851 JSObjectRef object([function JSObject]);
852 JSGlobalContextRef context([frame globalContext]);
853 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
855 /*if (settings != NULL)
856 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
858 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
861 - (void) reloadButtonClicked {
862 [self reloadURLWithCache:YES];
865 - (void) _customButtonClicked {
866 [self reloadButtonClicked];
869 - (void) customButtonClicked {
871 if (function_ != nil)
872 [self callFunction:function_];
875 [self _customButtonClicked];
878 + (float) defaultWidth {
882 - (void) setNavigationBarStyle:(NSString *)name {
884 if ([name isEqualToString:@"Black"])
885 style = UIBarStyleBlack;
887 style = UIBarStyleDefault;
889 [[[self navigationController] navigationBar] setBarStyle:style];
892 - (void) setNavigationBarTintColor:(UIColor *)color {
893 [[[self navigationController] navigationBar] setTintColor:color];
896 - (void) setBadgeValue:(id)value {
897 [[[self navigationController] tabBarItem] setBadgeValue:value];
900 - (void) setHidesBackButton:(bool)value {
901 [[self navigationItem] setHidesBackButton:value];
904 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
905 [self setHidesBackButton:[value boolValue]];
908 - (void) dispatchEvent:(NSString *)event {
909 WebThreadLocked lock;
911 NSString *script([NSString stringWithFormat:@
913 "var event = this.document.createEvent('Events');"
914 "event.initEvent('%@', false, false);"
915 "this.document.dispatchEvent(event);"
919 NSMutableArray *frames([NSMutableArray arrayWithObjects:
920 [[[webview_ _documentView] webView] mainFrame]
923 while (WebFrame *frame = [frames lastObject]) {
924 WebScriptObject *object([frame windowObject]);
925 [object evaluateWebScript:script];
926 [frames removeLastObject];
927 [frames addObjectsFromArray:[frame childFrames]];
931 - (bool) hidesNavigationBar {
932 return hidesNavigationBar_;
935 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
937 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
940 - (void) setHidesNavigationBar:(bool)value {
941 if (hidesNavigationBar_ != value) {
942 hidesNavigationBar_ = value;
943 [self _setHidesNavigationBar:YES animated:YES];
947 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
948 [self setHidesNavigationBar:[value boolValue]];
951 - (void) viewWillAppear:(BOOL)animated {
954 if ([self hidesNavigationBar])
955 [self _setHidesNavigationBar:YES animated:animated];
957 [self dispatchEvent:@"CydiaViewWillAppear"];
958 [super viewWillAppear:animated];
961 - (void) viewDidAppear:(BOOL)animated {
962 [super viewDidAppear:animated];
963 [self dispatchEvent:@"CydiaViewDidAppear"];
966 - (void) viewWillDisappear:(BOOL)animated {
967 [self dispatchEvent:@"CydiaViewWillDisappear"];
968 [super viewWillDisappear:animated];
970 if ([self hidesNavigationBar])
971 [self _setHidesNavigationBar:NO animated:animated];
976 - (void) viewDidDisappear:(BOOL)animated {
977 [super viewDidDisappear:animated];
978 [self dispatchEvent:@"CydiaViewDidDisappear"];