1 #include "CyteKit/UCPlatform.h"
2 #include "CyteKit/WebViewController.h"
4 #include "CyteKit/MFMailComposeViewController-MailToURL.h"
6 #include "iPhonePrivate.h"
8 #include "CyteKit/IndirectDelegate.h"
9 #include "CyteKit/Localize.h"
10 #include "CyteKit/WebViewController.h"
11 #include "CyteKit/PerlCompatibleRegEx.hpp"
12 #include "CyteKit/WebThreadLocked.hpp"
14 //#include <QuartzCore/CALayer.h>
15 // XXX: fix the minimum requirement
16 extern NSString * const kCAFilterNearest;
18 #include <WebCore/WebCoreThread.h>
20 #import <WebKit/WebKitErrors.h>
21 #import <WebKit/WebPreferences.h>
23 #include <WebKit/DOMCSSPrimitiveValue.h>
24 #include <WebKit/DOMCSSStyleDeclaration.h>
25 #include <WebKit/DOMDocument.h>
26 #include <WebKit/DOMHTMLBodyElement.h>
27 #include <WebKit/DOMRGBColor.h>
30 #include <objc/runtime.h>
33 #define DefaultTimeout_ 120.0
35 #define ShowInternals 0
39 #define lprintf(args...) fprintf(stderr, args)
41 // XXX: centralize these special class things to some file or mechanism?
42 static Class $MFMailComposeViewController;
44 float CYScrollViewDecelerationRateNormal;
46 @interface WebView (Apple)
47 - (void) _setLayoutInterval:(float)interval;
48 - (void) _setAllowsMessaging:(BOOL)allows;
51 @implementation WebFrame (Cydia)
53 - (NSString *) description {
54 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[([self provisionalDataSource] ?: [self dataSource]) request] URL] absoluteString]];
59 /* Indirect Delegate {{{ */
60 @implementation IndirectDelegate
62 - (void) setDelegate:(id)delegate {
66 - (id) initWithDelegate:(id)delegate {
71 - (IMP) methodForSelector:(SEL)sel {
72 if (IMP method = [super methodForSelector:sel])
74 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
78 - (BOOL) respondsToSelector:(SEL)sel {
79 if ([super respondsToSelector:sel])
82 // XXX: WebThreadCreateNSInvocation returns nil
85 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
88 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
91 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
92 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
96 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
100 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
103 // XXX: I fucking hate Apple so very very bad
104 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
107 - (void) forwardInvocation:(NSInvocation *)inv {
108 SEL sel = [inv selector];
109 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
110 [inv invokeWithTarget:delegate_];
116 @implementation CyteWebViewController
119 #include "CyteKit/UCInternal.h"
122 + (void) _initialize {
123 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
125 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
126 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
128 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
129 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
130 else // XXX: this actually might be fast on some older systems: we should look into this
131 CYScrollViewDecelerationRateNormal = 0.998;
134 - (bool) retainsNetworkActivityIndicator {
138 - (void) releaseNetworkActivityIndicator {
139 if ([loading_ count] != 0) {
140 [loading_ removeAllObjects];
142 if ([self retainsNetworkActivityIndicator])
143 [delegate_ releaseNetworkActivityIndicator];
149 NSLog(@"[CyteWebViewController dealloc]");
152 [self releaseNetworkActivityIndicator];
157 - (NSString *) description {
158 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]];
161 - (CyteWebView *) webView {
162 return (CyteWebView *) [self view];
165 - (NSURL *) URLWithURL:(NSURL *)url {
169 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
171 requestWithURL:[self URLWithURL:url]
173 timeoutInterval:DefaultTimeout_
177 - (void) setRequest:(NSURLRequest *)request {
178 _assert(request_ == nil);
182 - (void) setURL:(NSURL *)url {
183 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]];
186 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
187 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
190 - (void) loadURL:(NSURL *)url {
191 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
194 - (void) loadRequest:(NSURLRequest *)request {
196 NSLog(@"loadRequest:%@", request);
202 WebThreadLocked lock;
203 [[self webView] loadRequest:request];
206 - (void) reloadURLWithCache:(BOOL)cache {
210 NSMutableURLRequest *request([request_ mutableCopy]);
211 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
215 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
216 [self loadRequest:request_];
218 UIAlertView *alert = [[[UIAlertView alloc]
219 initWithTitle:UCLocalize("RESUBMIT_FORM")
222 cancelButtonTitle:UCLocalize("CANCEL")
224 UCLocalize("SUBMIT"),
228 [alert setContext:@"submit"];
234 [self reloadURLWithCache:YES];
237 - (void) reloadData {
241 [self dispatchEvent:@"CydiaReloadData"];
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 [[[self 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) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
300 [self dismissModalViewControllerAnimated:YES];
303 - (void) _setupMail:(MFMailComposeViewController *)controller {
306 - (void) _openMailToURL:(NSURL *)url {
307 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
308 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
309 [controller setMailComposeDelegate:self];
311 [controller setMailToURL:url];
313 [self _setupMail:controller];
315 [self presentModalViewController:controller animated:YES];
319 UIApplication *app([UIApplication sharedApplication]);
320 if ([app respondsToSelector:@selector(openURL:asPanel:)])
321 [app openURL:url asPanel:YES];
326 - (bool) _allowJavaScriptPanel {
330 - (bool) allowsNavigationAction {
331 return allowsNavigationAction_;
334 - (void) setAllowsNavigationAction:(bool)value {
335 allowsNavigationAction_ = value;
338 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
339 [self setAllowsNavigationAction:[value boolValue]];
342 - (void) popViewControllerWithNumber:(NSNumber *)value {
343 UINavigationController *navigation([self navigationController]);
344 if ([navigation topViewController] == self)
345 [navigation popViewControllerAnimated:[value boolValue]];
348 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
349 NSValue *object([NSValue valueWithNonretainedObject:frame]);
350 if (![loading_ containsObject:object])
352 [loading_ removeObject:object];
354 [self _didFinishLoading];
356 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
359 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
364 if ([frame parentFrame] == nil) {
365 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
366 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
367 [[error localizedDescription] stringByAddingPercentEscapes]
374 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
375 NSURL *url([request URL]);
377 // XXX: filter to internal usage?
378 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
381 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
382 [browser setRequest:request];
386 [page setDelegate:delegate_];
389 [[self navigationItem] setTitle:title_];
391 [[self navigationController] pushViewController:page animated:YES];
393 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
395 [navigation setDelegate:delegate_];
397 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
398 initWithTitle:UCLocalize("CLOSE")
399 style:UIBarButtonItemStylePlain
401 action:@selector(close)
404 [[self navigationController] presentModalViewController:navigation animated:YES];
408 // CyteWebViewDelegate {{{
409 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
411 static Pcre irritating("^(?"
412 ":" "The page at .* displayed insecure content from .*\\."
413 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
416 if (NSString *data = [message objectForKey:@"message"])
417 if (irritating(data))
420 NSLog(@"addMessageToConsole:%@", message);
424 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
426 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
429 if ([frame parentFrame] == nil) {
431 NSURL *url(request == nil ? nil : [request URL]);
433 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
435 [self pushRequest:request asPop:NO];
442 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
443 if ([frame parentFrame] == nil)
444 if (decision == CYWebPolicyDecisionUse)
449 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
451 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
454 NSURL *url([request URL]);
458 if ([frame isEqualToString:@"_open"])
459 [delegate_ openURL:url];
461 NSString *scheme([[url scheme] lowercaseString]);
462 if ([scheme isEqualToString:@"mailto"])
463 [self _openMailToURL:url];
465 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
471 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
474 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
476 NSLog(@"didCommitLoadForFrame:%@", frame);
479 if ([frame parentFrame] == nil) {
484 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
486 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
489 [self _didFailWithError:error forFrame:frame];
492 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
494 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
497 [self _didFailWithError:error forFrame:frame];
500 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
501 NSValue *object([NSValue valueWithNonretainedObject:frame]);
502 if (![loading_ containsObject:object])
504 [loading_ removeObject:object];
506 if ([frame parentFrame] == nil) {
507 if (DOMDocument *document = [frame DOMDocument])
508 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
509 for (DOMHTMLBodyElement *body in (id) bodies) {
510 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
514 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
515 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
516 DOMRGBColor *rgb([color getRGBColorValue]);
518 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
519 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
520 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
521 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
523 if (red == 0xc7 && green == 0xce && blue == 0xd5)
524 uic = [UIColor pinStripeColor];
527 colorWithRed:(red / 255)
535 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
540 [self _didFinishLoading];
543 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
544 if ([frame parentFrame] != nil)
549 [[self navigationItem] setTitle:title_];
552 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
554 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
557 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
559 if ([frame parentFrame] == nil) {
565 allowsNavigationAction_ = true;
567 [self setHidesNavigationBar:NO];
568 [self setScrollAlwaysBounceVertical:true];
569 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
571 // XXX: do we still need to do this?
572 [[self navigationItem] setTitle:nil];
575 [self _didStartLoading];
578 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
580 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
586 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
587 return [self _allowJavaScriptPanel];
590 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
591 return [self _allowJavaScriptPanel];
594 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
595 return [self _allowJavaScriptPanel];
598 - (void) webViewClose:(WebView *)view {
604 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
607 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
608 NSString *context([alert context]);
610 if ([context isEqualToString:@"sensitive"]) {
613 sensitive_ = [NSNumber numberWithBool:YES];
617 sensitive_ = [NSNumber numberWithBool:NO];
621 [alert dismissWithClickedButtonIndex:-1 animated:YES];
622 } else if ([context isEqualToString:@"challenge"]) {
623 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
627 NSString *username([[alert textFieldAtIndex:0] text]);
628 NSString *password([[alert textFieldAtIndex:1] text]);
630 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
632 [sender useCredential:credential forAuthenticationChallenge:challenge_];
636 [sender cancelAuthenticationChallenge:challenge_];
644 [alert dismissWithClickedButtonIndex:-1 animated:YES];
645 } else if ([context isEqualToString:@"submit"]) {
646 if (button == [alert cancelButtonIndex]) {
647 } else if (button == [alert firstOtherButtonIndex]) {
648 if (request_ != nil) {
649 WebThreadLocked lock;
650 [[self webView] loadRequest:request_];
654 [alert dismissWithClickedButtonIndex:-1 animated:YES];
658 - (UIBarButtonItemStyle) rightButtonStyle {
659 if (style_ == nil) normal:
660 return UIBarButtonItemStylePlain;
661 else if ([style_ isEqualToString:@"Normal"])
662 return UIBarButtonItemStylePlain;
663 else if ([style_ isEqualToString:@"Highlighted"])
664 return UIBarButtonItemStyleDone;
668 - (UIBarButtonItem *) customButton {
671 else if (custom_ == [NSNull null])
672 return (UIBarButtonItem *) [NSNull null];
674 return [[[UIBarButtonItem alloc]
675 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
676 style:[self rightButtonStyle]
678 action:@selector(customButtonClicked)
682 - (UIBarButtonItem *) leftButton {
683 UINavigationItem *item([self navigationItem]);
684 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
687 if (UINavigationController *navigation = [self navigationController])
688 if ([[navigation parentViewController] modalViewController] == navigation)
689 return [[[UIBarButtonItem alloc]
690 initWithTitle:UCLocalize("CLOSE")
691 style:UIBarButtonItemStylePlain
693 action:@selector(close)
699 - (void) applyLeftButton {
700 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
703 - (UIBarButtonItem *) rightButton {
707 - (void) applyLoadingTitle {
708 [[self navigationItem] setTitle:UCLocalize("LOADING")];
711 - (void) layoutRightButton {
712 [[loadingitem_ view] addSubview:indicator_];
713 [[loadingitem_ view] bringSubviewToFront:indicator_];
716 - (void) applyRightButton {
717 if ([self isLoading]) {
718 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
719 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
721 [indicator_ startAnimating];
722 [self applyLoadingTitle];
724 [indicator_ stopAnimating];
726 UIBarButtonItem *button([self customButton]);
728 button = [self rightButton];
729 else if (button == (UIBarButtonItem *) [NSNull null])
732 [[self navigationItem] setRightBarButtonItem:button animated:YES];
736 - (void) didStartLoading {
737 // Overridden in subclasses.
740 - (void) _didStartLoading {
741 [self applyRightButton];
743 if ([loading_ count] != 1)
746 if ([self retainsNetworkActivityIndicator])
747 [delegate_ retainNetworkActivityIndicator];
749 [self didStartLoading];
752 - (void) didFinishLoading {
753 // Overridden in subclasses.
756 - (void) _didFinishLoading {
757 if ([loading_ count] != 0)
760 [self applyRightButton];
761 [[self navigationItem] setTitle:title_];
763 if ([self retainsNetworkActivityIndicator])
764 [delegate_ releaseNetworkActivityIndicator];
766 [self didFinishLoading];
770 return [loading_ count] != 0;
773 - (id) initWithWidth:(float)width ofClass:(Class)_class {
774 if ((self = [super init]) != nil) {
778 allowsNavigationAction_ = true;
780 loading_ = [NSMutableSet setWithCapacity:5];
781 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
783 reloaditem_ = [[[UIBarButtonItem alloc]
784 initWithTitle:UCLocalize("RELOAD")
785 style:[self rightButtonStyle]
787 action:@selector(reloadButtonClicked)
790 loadingitem_ = [[[UIBarButtonItem alloc]
792 style:UIBarButtonItemStylePlain
794 action:@selector(reloadButtonClicked)
797 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
798 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
799 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
801 [self applyLeftButton];
802 [self applyRightButton];
806 - (NSString *) applicationNameForUserAgent {
811 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
813 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
814 [webview_ setDelegate:self];
815 [self setView:webview_];
817 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
818 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
820 [webview_ setDetectsPhoneNumbers:NO];
822 [webview_ setScalesPageToFit:YES];
824 UIWebDocumentView *document([webview_ _documentView]);
826 // XXX: I think this improves scrolling; the hardcoded-ness sucks
827 [document setTileSize:CGSizeMake(320, 500)];
829 [document setBackgroundColor:[UIColor clearColor]];
831 // XXX: this is terribly (too?) expensive
832 [document setDrawsBackground:NO];
834 WebView *webview([document webView]);
835 WebPreferences *preferences([webview preferences]);
837 // XXX: I have no clue if I actually /want/ this modification
838 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
839 [webview _setLayoutInterval:0];
840 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
841 [preferences _setLayoutInterval:0];
843 [preferences setCacheModel:WebCacheModelDocumentBrowser];
844 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
845 [preferences setOfflineWebApplicationCacheEnabled:YES];
847 if (NSString *agent = [self applicationNameForUserAgent])
848 [webview setApplicationNameForUserAgent:agent];
850 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
851 [webview setShouldUpdateWhileOffscreen:NO];
854 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
855 [document setAllowsMessaging:YES];
856 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
857 [webview _setAllowsMessaging:YES];
860 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
861 scroller_ = [webview_ _scrollView];
863 [scroller_ setDirectionalLockEnabled:YES];
864 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
865 [scroller_ setDelaysContentTouches:NO];
867 [scroller_ setCanCancelContentTouches:YES];
868 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
869 UIScroller *scroller([webview_ _scroller]);
870 scroller_ = (UIScrollView *) scroller;
872 [scroller setDirectionalScrolling:YES];
873 // XXX: we might be better off /not/ setting this on older systems
874 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
875 [scroller setScrollHysteresis:0]; /* 8 */
877 [scroller setThumbDetectionEnabled:NO];
879 // use NO with UIApplicationUseLegacyEvents(YES)
880 [scroller setEventMode:YES];
882 // XXX: this is handled by setBounces, right?
883 //[scroller setAllowsRubberBanding:YES];
886 [scroller_ setFixedBackgroundPattern:YES];
887 [scroller_ setBackgroundColor:[UIColor clearColor]];
888 [scroller_ setClipsSubviews:YES];
890 [scroller_ setBounces:YES];
891 [scroller_ setScrollingEnabled:YES];
892 [scroller_ setShowBackgroundShadow:NO];
894 [self setViewportWidth:width_];
896 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
897 [table setScrollsToTop:NO];
898 [webview_ insertSubview:table atIndex:0];
900 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
901 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
906 - (void) releaseSubviews {
910 [self releaseNetworkActivityIndicator];
912 [super releaseSubviews];
915 - (id) initWithWidth:(float)width {
916 return [self initWithWidth:width ofClass:[self class]];
920 return [self initWithWidth:0];
923 - (id) initWithURL:(NSURL *)url {
924 if ((self = [self init]) != nil) {
929 - (id) initWithRequest:(NSURLRequest *)request {
930 if ((self = [self init]) != nil) {
931 [self setRequest:request];
935 - (void) callFunction:(WebScriptObject *)function {
936 WebThreadLocked lock;
938 WebView *webview([[[self webView] _documentView] webView]);
939 WebFrame *frame([webview mainFrame]);
941 JSGlobalContextRef context([frame globalContext]);
942 JSObjectRef object([function JSObject]);
943 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
946 - (void) reloadButtonClicked {
947 [self reloadURLWithCache:YES];
950 - (void) _customButtonClicked {
951 [self reloadButtonClicked];
954 - (void) customButtonClicked {
956 if (function_ != nil)
957 [self callFunction:function_];
960 [self _customButtonClicked];
963 + (float) defaultWidth {
967 - (void) setNavigationBarStyle:(NSString *)name {
969 if ([name isEqualToString:@"Black"])
970 style = UIBarStyleBlack;
972 style = UIBarStyleDefault;
974 [[[self navigationController] navigationBar] setBarStyle:style];
977 - (void) setNavigationBarTintColor:(UIColor *)color {
978 [[[self navigationController] navigationBar] setTintColor:color];
981 - (void) setBadgeValue:(id)value {
982 [[[self navigationController] tabBarItem] setBadgeValue:value];
985 - (void) setHidesBackButton:(bool)value {
986 [[self navigationItem] setHidesBackButton:value];
987 [self applyLeftButton];
990 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
991 [self setHidesBackButton:[value boolValue]];
994 - (void) dispatchEvent:(NSString *)event {
995 [[self webView] dispatchEvent:event];
998 - (bool) hidesNavigationBar {
999 return hidesNavigationBar_;
1002 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1004 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1007 - (void) setHidesNavigationBar:(bool)value {
1008 if (hidesNavigationBar_ != value) {
1009 hidesNavigationBar_ = value;
1010 [self _setHidesNavigationBar:YES animated:YES];
1014 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1015 [self setHidesNavigationBar:[value boolValue]];
1018 - (void) setScrollAlwaysBounceVertical:(bool)value {
1019 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1020 UIScrollView *scroller([webview_ _scrollView]);
1021 [scroller setAlwaysBounceVertical:value];
1022 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1023 //UIScroller *scroller([webview_ _scroller]);
1024 // XXX: I am sad here.
1028 - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1029 [self setScrollAlwaysBounceVertical:[value boolValue]];
1032 - (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style {
1033 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1034 UIScrollView *scroller([webview_ _scrollView]);
1035 [scroller setIndicatorStyle:style];
1036 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1037 UIScroller *scroller([webview_ _scroller]);
1038 [scroller setScrollerIndicatorStyle:style];
1042 - (void) setScrollIndicatorStyleWithName:(NSString *)style {
1043 UIScrollViewIndicatorStyle value;
1046 else if ([style isEqualToString:@"default"])
1047 value = UIScrollViewIndicatorStyleDefault;
1048 else if ([style isEqualToString:@"black"])
1049 value = UIScrollViewIndicatorStyleBlack;
1050 else if ([style isEqualToString:@"white"])
1051 value = UIScrollViewIndicatorStyleWhite;
1054 [self setScrollIndicatorStyle:value];
1057 - (void) viewWillAppear:(BOOL)animated {
1060 if ([self hidesNavigationBar])
1061 [self _setHidesNavigationBar:YES animated:animated];
1063 [self dispatchEvent:@"CydiaViewWillAppear"];
1064 [super viewWillAppear:animated];
1067 - (void) viewDidAppear:(BOOL)animated {
1068 [super viewDidAppear:animated];
1069 [self dispatchEvent:@"CydiaViewDidAppear"];
1072 - (void) viewWillDisappear:(BOOL)animated {
1073 [self dispatchEvent:@"CydiaViewWillDisappear"];
1074 [super viewWillDisappear:animated];
1076 if ([self hidesNavigationBar])
1077 [self _setHidesNavigationBar:NO animated:animated];
1082 - (void) viewDidDisappear:(BOOL)animated {
1083 [super viewDidDisappear:animated];
1084 [self dispatchEvent:@"CydiaViewDidDisappear"];