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 sharedApplication] openURL:url];// asPanel:YES];
292 - (bool) _allowJavaScriptPanel {
296 - (bool) allowsNavigationAction {
297 return allowsNavigationAction_;
300 - (void) setAllowsNavigationAction:(bool)value {
301 allowsNavigationAction_ = value;
304 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
305 [self setAllowsNavigationAction:[value boolValue]];
308 - (void) popViewControllerWithNumber:(NSNumber *)value {
309 UINavigationController *navigation([self navigationController]);
310 if ([navigation topViewController] == self)
311 [navigation popViewControllerAnimated:[value boolValue]];
314 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
315 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
316 [self _didFinishLoading];
318 if ([error code] == NSURLErrorCancelled)
321 if ([frame parentFrame] == nil) {
322 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
323 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
324 [[error localizedDescription] stringByAddingPercentEscapes]
331 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
332 NSURL *url([request URL]);
334 // XXX: filter to internal usage?
335 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
338 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
339 [browser loadRequest:request];
343 [page setDelegate:delegate_];
346 [[self navigationItem] setTitle:title_];
348 [[self navigationController] pushViewController:page animated:YES];
350 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
352 [navigation setDelegate:delegate_];
354 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
355 initWithTitle:UCLocalize("CLOSE")
356 style:UIBarButtonItemStylePlain
358 action:@selector(close)
361 [[self navigationController] presentModalViewController:navigation animated:YES];
363 [delegate_ unloadData];
367 // CyteWebViewDelegate {{{
368 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
370 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$");
371 if (NSString *data = [message objectForKey:@"message"])
372 if (irritating(data))
375 NSLog(@"addMessageToConsole:%@", message);
379 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
381 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
384 if ([frame parentFrame] == nil) {
386 NSURL *url(request == nil ? nil : [request URL]);
388 if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
392 [self pushRequest:request asPop:NO];
399 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
401 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
404 NSURL *url([request URL]);
408 if ([frame isEqualToString:@"_open"])
409 [delegate_ openURL:url];
411 NSString *scheme([[url scheme] lowercaseString]);
412 if ([scheme isEqualToString:@"mailto"])
413 [self _openMailToURL:url];
415 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
421 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
424 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
426 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
429 [self _didFailWithError:error forFrame:frame];
432 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
434 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
437 [self _didFailWithError:error forFrame:frame];
440 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
441 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
443 if ([frame parentFrame] == nil) {
444 if (DOMDocument *document = [frame DOMDocument])
445 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
446 for (DOMHTMLBodyElement *body in (id) bodies) {
447 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
451 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
452 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
453 DOMRGBColor *rgb([color getRGBColorValue]);
455 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
456 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
457 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
458 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
460 if (red == 0xc7 && green == 0xce && blue == 0xd5)
461 uic = [UIColor pinStripeColor];
464 colorWithRed:(red / 255)
472 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
477 [self _didFinishLoading];
480 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
481 if ([frame parentFrame] != nil)
485 [title_ autorelease];
486 title_ = [title retain];
488 [[self navigationItem] setTitle:title_];
491 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
492 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
494 if ([frame parentFrame] == nil) {
500 [self setHidesNavigationBar:NO];
502 // XXX: do we still need to do this?
503 [[self navigationItem] setTitle:nil];
506 [self _didStartLoading];
509 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
511 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
517 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
518 return [self _allowJavaScriptPanel];
521 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
522 return [self _allowJavaScriptPanel];
525 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
526 return [self _allowJavaScriptPanel];
529 - (void) webViewClose:(WebView *)view {
535 [[self navigationController] dismissModalViewControllerAnimated:YES];
538 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
539 NSString *context([alert context]);
541 if ([context isEqualToString:@"sensitive"]) {
544 sensitive_ = [NSNumber numberWithBool:YES];
548 sensitive_ = [NSNumber numberWithBool:NO];
552 [alert dismissWithClickedButtonIndex:-1 animated:YES];
553 } else if ([context isEqualToString:@"challenge"]) {
554 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
558 NSString *username([[alert textFieldAtIndex:0] text]);
559 NSString *password([[alert textFieldAtIndex:1] text]);
561 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
563 [sender useCredential:credential forAuthenticationChallenge:challenge_];
567 [sender cancelAuthenticationChallenge:challenge_];
573 [challenge_ release];
576 [alert dismissWithClickedButtonIndex:-1 animated:YES];
577 } else if ([context isEqualToString:@"submit"]) {
578 if (button == [alert cancelButtonIndex]) {
579 } else if (button == [alert firstOtherButtonIndex]) {
580 if (request_ != nil) {
581 WebThreadLocked lock;
582 [webview_ loadRequest:request_];
586 [alert dismissWithClickedButtonIndex:-1 animated:YES];
590 - (UIBarButtonItemStyle) rightButtonStyle {
591 if (style_ == nil) normal:
592 return UIBarButtonItemStylePlain;
593 else if ([style_ isEqualToString:@"Normal"])
594 return UIBarButtonItemStylePlain;
595 else if ([style_ isEqualToString:@"Highlighted"])
596 return UIBarButtonItemStyleDone;
600 - (UIBarButtonItem *) customButton {
603 else if (custom_ == [NSNull null])
604 return (UIBarButtonItem *) [NSNull null];
606 return [[[UIBarButtonItem alloc]
607 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
608 style:[self rightButtonStyle]
610 action:@selector(customButtonClicked)
614 - (UIBarButtonItem *) leftButton {
615 UINavigationItem *item([self navigationItem]);
616 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
619 if (UINavigationController *navigation = [self navigationController])
620 if ([[navigation parentViewController] modalViewController] == navigation)
621 return [[[UIBarButtonItem alloc]
622 initWithTitle:UCLocalize("CLOSE")
623 style:UIBarButtonItemStylePlain
625 action:@selector(close)
631 - (void) applyLeftButton {
632 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
635 - (UIBarButtonItem *) rightButton {
639 - (void) applyLoadingTitle {
640 [[self navigationItem] setTitle:UCLocalize("LOADING")];
643 - (void) layoutRightButton {
644 [[loadingitem_ view] addSubview:indicator_];
645 [[loadingitem_ view] bringSubviewToFront:indicator_];
648 - (void) applyRightButton {
649 if ([self isLoading]) {
650 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
651 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
653 [indicator_ startAnimating];
654 [self applyLoadingTitle];
656 [indicator_ stopAnimating];
658 UIBarButtonItem *button([self customButton]);
660 button = [self rightButton];
661 else if (button == (UIBarButtonItem *) [NSNull null])
664 [[self navigationItem] setRightBarButtonItem:button];
668 - (void) didStartLoading {
669 // Overridden in subclasses.
672 - (void) _didStartLoading {
673 [self applyRightButton];
675 if ([loading_ count] != 1)
678 [delegate_ retainNetworkActivityIndicator];
679 [self didStartLoading];
682 - (void) didFinishLoading {
683 // Overridden in subclasses.
686 - (void) _didFinishLoading {
687 if ([loading_ count] != 0)
690 [self applyRightButton];
691 [[self navigationItem] setTitle:title_];
693 [delegate_ releaseNetworkActivityIndicator];
694 [self didFinishLoading];
698 return [loading_ count] != 0;
701 - (id) initWithWidth:(float)width ofClass:(Class)_class {
702 if ((self = [super init]) != nil) {
703 allowsNavigationAction_ = true;
706 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
708 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
710 CGRect bounds([[self view] bounds]);
712 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
713 [webview_ setDelegate:self];
714 [self setView:webview_];
716 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
717 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
719 [webview_ setDetectsPhoneNumbers:NO];
721 [webview_ setScalesPageToFit:YES];
723 UIWebDocumentView *document([webview_ _documentView]);
725 // XXX: I think this improves scrolling; the hardcoded-ness sucks
726 [document setTileSize:CGSizeMake(320, 500)];
728 [document setBackgroundColor:[UIColor clearColor]];
730 // XXX: this is terribly (too?) expensive
731 [document setDrawsBackground:NO];
733 WebView *webview([document webView]);
734 WebPreferences *preferences([webview preferences]);
736 // XXX: I have no clue if I actually /want/ this modification
737 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
738 [webview _setLayoutInterval:0];
739 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
740 [preferences _setLayoutInterval:0];
742 [preferences setCacheModel:WebCacheModelDocumentBrowser];
743 [preferences setOfflineWebApplicationCacheEnabled:YES];
746 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
747 [document setAllowsMessaging:YES];
748 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
749 [webview _setAllowsMessaging:YES];
752 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
753 scroller_ = [webview_ _scrollView];
755 [scroller_ setDirectionalLockEnabled:YES];
756 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
757 [scroller_ setDelaysContentTouches:NO];
759 [scroller_ setCanCancelContentTouches:YES];
760 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
761 UIScroller *scroller([webview_ _scroller]);
762 scroller_ = (UIScrollView *) scroller;
764 [scroller setDirectionalScrolling:YES];
765 // XXX: we might be better off /not/ setting this on older systems
766 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
767 [scroller setScrollHysteresis:0]; /* 8 */
769 [scroller setThumbDetectionEnabled:NO];
771 // use NO with UIApplicationUseLegacyEvents(YES)
772 [scroller setEventMode:YES];
774 // XXX: this is handled by setBounces, right?
775 //[scroller setAllowsRubberBanding:YES];
778 [scroller_ setFixedBackgroundPattern:YES];
779 [scroller_ setBackgroundColor:[UIColor clearColor]];
780 [scroller_ setClipsSubviews:YES];
782 [scroller_ setBounces:YES];
783 [scroller_ setScrollingEnabled:YES];
784 [scroller_ setShowBackgroundShadow:NO];
786 [self setViewportWidth:width];
788 reloaditem_ = [[UIBarButtonItem alloc]
789 initWithTitle:UCLocalize("RELOAD")
790 style:[self rightButtonStyle]
792 action:@selector(reloadButtonClicked)
795 loadingitem_ = [[UIBarButtonItem alloc]
797 style:UIBarButtonItemStylePlain
799 action:@selector(reloadButtonClicked)
802 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
803 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
805 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
806 [webview_ insertSubview:table atIndex:0];
808 [self applyLeftButton];
810 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
811 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
812 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
816 - (id) initWithWidth:(float)width {
817 return [self initWithWidth:width ofClass:[self class]];
821 return [self initWithWidth:0];
824 - (id) initWithURL:(NSURL *)url {
825 if ((self = [self init]) != nil) {
830 - (void) callFunction:(WebScriptObject *)function {
831 WebThreadLocked lock;
833 WebView *webview([[webview_ _documentView] webView]);
834 WebFrame *frame([webview mainFrame]);
835 WebPreferences *preferences([webview preferences]);
837 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
838 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
840 /*id _private(MSHookIvar<id>(webview, "_private"));
841 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
842 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
845 if (settings == NULL)
848 no = settings->JavaScriptCanOpenWindowsAutomatically();
849 settings->setJavaScriptCanOpenWindowsAutomatically(true);
852 if (UIWindow *window = [[self view] window])
853 if (UIResponder *responder = [window firstResponder])
854 [responder resignFirstResponder];
856 JSObjectRef object([function JSObject]);
857 JSGlobalContextRef context([frame globalContext]);
858 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
860 /*if (settings != NULL)
861 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
863 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
866 - (void) reloadButtonClicked {
867 [self reloadURLWithCache:YES];
870 - (void) _customButtonClicked {
871 [self reloadButtonClicked];
874 - (void) customButtonClicked {
876 if (function_ != nil)
877 [self callFunction:function_];
880 [self _customButtonClicked];
883 + (float) defaultWidth {
887 - (void) setNavigationBarStyle:(NSString *)name {
889 if ([name isEqualToString:@"Black"])
890 style = UIBarStyleBlack;
892 style = UIBarStyleDefault;
894 [[[self navigationController] navigationBar] setBarStyle:style];
897 - (void) setNavigationBarTintColor:(UIColor *)color {
898 [[[self navigationController] navigationBar] setTintColor:color];
901 - (void) setBadgeValue:(id)value {
902 [[[self navigationController] tabBarItem] setBadgeValue:value];
905 - (void) setHidesBackButton:(bool)value {
906 [[self navigationItem] setHidesBackButton:value];
907 [self applyLeftButton];
910 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
911 [self setHidesBackButton:[value boolValue]];
914 - (void) dispatchEvent:(NSString *)event {
915 [webview_ dispatchEvent:event];
918 - (bool) hidesNavigationBar {
919 return hidesNavigationBar_;
922 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
924 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
927 - (void) setHidesNavigationBar:(bool)value {
928 if (hidesNavigationBar_ != value) {
929 hidesNavigationBar_ = value;
930 [self _setHidesNavigationBar:YES animated:YES];
934 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
935 [self setHidesNavigationBar:[value boolValue]];
938 - (void) viewWillAppear:(BOOL)animated {
941 if ([self hidesNavigationBar])
942 [self _setHidesNavigationBar:YES animated:animated];
944 [self dispatchEvent:@"CydiaViewWillAppear"];
945 [super viewWillAppear:animated];
948 - (void) viewDidAppear:(BOOL)animated {
949 [super viewDidAppear:animated];
950 [self dispatchEvent:@"CydiaViewDidAppear"];
953 - (void) viewWillDisappear:(BOOL)animated {
954 [self dispatchEvent:@"CydiaViewWillDisappear"];
955 [super viewWillDisappear:animated];
957 if ([self hidesNavigationBar])
958 [self _setHidesNavigationBar:NO animated:animated];
963 - (void) viewDidDisappear:(BOOL)animated {
964 [super viewDidDisappear:animated];
965 [self dispatchEvent:@"CydiaViewDidDisappear"];