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 referrer:(NSString *)referrer {
170 NSMutableURLRequest *request([NSMutableURLRequest
171 requestWithURL:[self URLWithURL:url]
173 timeoutInterval:DefaultTimeout_
176 [request setValue:referrer forHTTPHeaderField:@"Referer"];
181 - (void) setRequest:(NSURLRequest *)request {
182 _assert(request_ == nil);
186 - (void) setURL:(NSURL *)url {
187 [self setURL:url withReferrer:nil];
190 - (void) setURL:(NSURL *)url withReferrer:(NSString *)referrer {
191 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy referrer:referrer]];
194 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
195 [self loadRequest:[self requestWithURL:url cachePolicy:policy referrer:nil]];
198 - (void) loadURL:(NSURL *)url {
199 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
202 - (void) loadRequest:(NSURLRequest *)request {
204 NSLog(@"loadRequest:%@", request);
210 WebThreadLocked lock;
211 [[self webView] loadRequest:request];
214 - (void) reloadURLWithCache:(BOOL)cache {
218 NSMutableURLRequest *request([request_ mutableCopy]);
219 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
223 if (cache || [request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
224 [self loadRequest:request_];
226 UIAlertView *alert = [[[UIAlertView alloc]
227 initWithTitle:UCLocalize("RESUBMIT_FORM")
230 cancelButtonTitle:UCLocalize("CANCEL")
232 UCLocalize("SUBMIT"),
236 [alert setContext:@"submit"];
241 - (void) reloadData {
245 [self dispatchEvent:@"CydiaReloadData"];
247 [self reloadURLWithCache:YES];
250 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
253 function_ = function;
255 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
258 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
261 function_ = function;
263 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
266 - (void) removeButton {
267 custom_ = [NSNull null];
268 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
271 - (void) scrollToBottomAnimated:(NSNumber *)animated {
272 CGSize size([scroller_ contentSize]);
273 CGPoint offset([scroller_ contentOffset]);
274 CGRect frame([scroller_ frame]);
276 if (size.height - offset.y < frame.size.height + 20.f) {
277 CGRect rect = {{0, size.height-1}, {size.width, 1}};
278 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
282 - (void) _setViewportWidth {
283 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
286 - (void) setViewportWidth:(float)width {
287 width_ = width != 0 ? width : [[self class] defaultWidth];
288 [self _setViewportWidth];
291 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
292 [self setViewportWidth:[width floatValue]];
295 - (void) setViewportWidthOnMainThread:(float)width {
296 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
299 - (void) webViewUpdateViewSettings:(UIWebView *)view {
300 [self _setViewportWidth];
303 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
304 [self dismissModalViewControllerAnimated:YES];
307 - (void) _setupMail:(MFMailComposeViewController *)controller {
310 - (void) _openMailToURL:(NSURL *)url {
311 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
312 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
313 [controller setMailComposeDelegate:self];
315 [controller setMailToURL:url];
317 [self _setupMail:controller];
319 [self presentModalViewController:controller animated:YES];
323 UIApplication *app([UIApplication sharedApplication]);
324 if ([app respondsToSelector:@selector(openURL:asPanel:)])
325 [app openURL:url asPanel:YES];
330 - (bool) _allowJavaScriptPanel {
334 - (bool) allowsNavigationAction {
335 return allowsNavigationAction_;
338 - (void) setAllowsNavigationAction:(bool)value {
339 allowsNavigationAction_ = value;
342 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
343 [self setAllowsNavigationAction:[value boolValue]];
346 - (void) popViewControllerWithNumber:(NSNumber *)value {
347 UINavigationController *navigation([self navigationController]);
348 if ([navigation topViewController] == self)
349 [navigation popViewControllerAnimated:[value boolValue]];
352 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
353 NSValue *object([NSValue valueWithNonretainedObject:frame]);
354 if (![loading_ containsObject:object])
356 [loading_ removeObject:object];
358 [self _didFinishLoading];
360 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
363 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
368 if ([frame parentFrame] == nil) {
369 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
370 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
371 [[error localizedDescription] stringByAddingPercentEscapes]
378 - (void) pushRequest:(NSURLRequest *)request forAction:(NSDictionary *)action asPop:(bool)pop {
379 WebFrame *frame(nil);
380 if (NSDictionary *WebActionElement = [action objectForKey:@"WebActionElementKey"])
381 frame = [WebActionElement objectForKey:@"WebElementFrame"];
383 frame = [[[[self webView] _documentView] webView] mainFrame];
385 WebDataSource *source([frame provisionalDataSource] ?: [frame dataSource]);
386 NSString *referrer([request valueForHTTPHeaderField:@"Referer"] ?: [[[source request] URL] absoluteString]);
388 NSURL *url([request URL]);
390 // XXX: filter to internal usage?
391 CyteViewController *page([delegate_ pageForURL:url forExternal:NO withReferrer:referrer]);
394 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
395 [browser setRequest:request];
399 [page setDelegate:delegate_];
402 [[self navigationItem] setTitle:title_];
404 [[self navigationController] pushViewController:page animated:YES];
406 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
408 [navigation setDelegate:delegate_];
410 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
411 initWithTitle:UCLocalize("CLOSE")
412 style:UIBarButtonItemStylePlain
414 action:@selector(close)
417 [[self navigationController] presentModalViewController:navigation animated:YES];
421 // CyteWebViewDelegate {{{
422 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
424 static Pcre irritating("^(?"
425 ":" "The page at .* displayed insecure content from .*\\."
426 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
429 if (NSString *data = [message objectForKey:@"message"])
430 if (irritating(data))
433 NSLog(@"addMessageToConsole:%@", message);
437 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
439 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ %@ frame:%@", action, request, [request allHTTPHeaderFields], frame);
442 if ([frame parentFrame] == nil) {
444 NSURL *url(request == nil ? nil : [request URL]);
446 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
448 [self pushRequest:request forAction:action asPop:NO];
455 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
457 NSLog(@"didDecidePolicy:%u forNavigationAction:%@ request:%@ frame:%@", decision, action, request, [request allHTTPHeaderFields], frame);
460 if ([frame parentFrame] == nil) {
462 case CYWebPolicyDecisionIgnore:
463 if ([[request_ URL] isEqual:[request URL]])
467 case CYWebPolicyDecisionUse:
478 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
480 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ %@ newFrameName:%@", action, request, [request allHTTPHeaderFields], name);
483 NSURL *url([request URL]);
487 if ([name isEqualToString:@"_open"])
488 [delegate_ openURL:url];
490 NSString *scheme([[url scheme] lowercaseString]);
491 if ([scheme isEqualToString:@"mailto"])
492 [self _openMailToURL:url];
494 [self pushRequest:request forAction:action asPop:[name isEqualToString:@"_popup"]];
500 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
503 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
505 NSLog(@"didCommitLoadForFrame:%@", frame);
508 if ([frame parentFrame] == nil) {
513 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
515 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
518 [self _didFailWithError:error forFrame:frame];
521 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
523 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
526 [self _didFailWithError:error forFrame:frame];
529 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
530 NSValue *object([NSValue valueWithNonretainedObject:frame]);
531 if (![loading_ containsObject:object])
533 [loading_ removeObject:object];
535 if ([frame parentFrame] == nil) {
536 if (DOMDocument *document = [frame DOMDocument])
537 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
538 for (DOMHTMLBodyElement *body in (id) bodies) {
539 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
543 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
544 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
545 DOMRGBColor *rgb([color getRGBColorValue]);
547 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
548 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
549 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
550 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
552 if (red == 0xc7 && green == 0xce && blue == 0xd5)
553 uic = [UIColor pinStripeColor];
556 colorWithRed:(red / 255)
564 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
569 [self _didFinishLoading];
572 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
573 if ([frame parentFrame] != nil)
578 [[self navigationItem] setTitle:title_];
581 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
583 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
586 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
588 if ([frame parentFrame] == nil) {
594 allowsNavigationAction_ = true;
596 [self setHidesNavigationBar:NO];
597 [self setScrollAlwaysBounceVertical:true];
598 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
600 // XXX: do we still need to do this?
601 [[self navigationItem] setTitle:nil];
604 [self _didStartLoading];
607 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
609 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
615 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
616 return [self _allowJavaScriptPanel];
619 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
620 return [self _allowJavaScriptPanel];
623 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
624 return [self _allowJavaScriptPanel];
627 - (void) webViewClose:(WebView *)view {
633 [[[self navigationController] parentViewController] dismissModalViewControllerAnimated:YES];
636 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
637 NSString *context([alert context]);
639 if ([context isEqualToString:@"sensitive"]) {
642 sensitive_ = [NSNumber numberWithBool:YES];
646 sensitive_ = [NSNumber numberWithBool:NO];
650 [alert dismissWithClickedButtonIndex:-1 animated:YES];
651 } else if ([context isEqualToString:@"challenge"]) {
652 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
656 NSString *username([[alert textFieldAtIndex:0] text]);
657 NSString *password([[alert textFieldAtIndex:1] text]);
659 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
661 [sender useCredential:credential forAuthenticationChallenge:challenge_];
665 [sender cancelAuthenticationChallenge:challenge_];
673 [alert dismissWithClickedButtonIndex:-1 animated:YES];
674 } else if ([context isEqualToString:@"submit"]) {
675 if (button == [alert cancelButtonIndex]) {
676 } else if (button == [alert firstOtherButtonIndex]) {
677 if (request_ != nil) {
678 WebThreadLocked lock;
679 [[self webView] loadRequest:request_];
683 [alert dismissWithClickedButtonIndex:-1 animated:YES];
687 - (UIBarButtonItemStyle) rightButtonStyle {
688 if (style_ == nil) normal:
689 return UIBarButtonItemStylePlain;
690 else if ([style_ isEqualToString:@"Normal"])
691 return UIBarButtonItemStylePlain;
692 else if ([style_ isEqualToString:@"Highlighted"])
693 return UIBarButtonItemStyleDone;
697 - (UIBarButtonItem *) customButton {
700 else if (custom_ == [NSNull null])
701 return (UIBarButtonItem *) [NSNull null];
703 return [[[UIBarButtonItem alloc]
704 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
705 style:[self rightButtonStyle]
707 action:@selector(customButtonClicked)
711 - (UIBarButtonItem *) leftButton {
712 UINavigationItem *item([self navigationItem]);
713 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
716 if (UINavigationController *navigation = [self navigationController])
717 if ([[navigation parentViewController] modalViewController] == navigation)
718 return [[[UIBarButtonItem alloc]
719 initWithTitle:UCLocalize("CLOSE")
720 style:UIBarButtonItemStylePlain
722 action:@selector(close)
728 - (void) applyLeftButton {
729 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
732 - (UIBarButtonItem *) rightButton {
736 - (void) applyLoadingTitle {
737 [[self navigationItem] setTitle:UCLocalize("LOADING")];
740 - (void) layoutRightButton {
741 [[loadingitem_ view] addSubview:indicator_];
742 [[loadingitem_ view] bringSubviewToFront:indicator_];
745 - (void) applyRightButton {
746 if ([self isLoading]) {
747 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
748 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
750 [indicator_ startAnimating];
751 [self applyLoadingTitle];
753 [indicator_ stopAnimating];
755 UIBarButtonItem *button([self customButton]);
757 button = [self rightButton];
758 else if (button == (UIBarButtonItem *) [NSNull null])
761 [[self navigationItem] setRightBarButtonItem:button animated:YES];
765 - (void) didStartLoading {
766 // Overridden in subclasses.
769 - (void) _didStartLoading {
770 [self applyRightButton];
772 if ([loading_ count] != 1)
775 if ([self retainsNetworkActivityIndicator])
776 [delegate_ retainNetworkActivityIndicator];
778 [self didStartLoading];
781 - (void) didFinishLoading {
782 // Overridden in subclasses.
785 - (void) _didFinishLoading {
786 if ([loading_ count] != 0)
789 [self applyRightButton];
790 [[self navigationItem] setTitle:title_];
792 if ([self retainsNetworkActivityIndicator])
793 [delegate_ releaseNetworkActivityIndicator];
795 [self didFinishLoading];
799 return [loading_ count] != 0;
802 - (id) initWithWidth:(float)width ofClass:(Class)_class {
803 if ((self = [super init]) != nil) {
807 allowsNavigationAction_ = true;
809 loading_ = [NSMutableSet setWithCapacity:5];
810 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
812 reloaditem_ = [[[UIBarButtonItem alloc]
813 initWithTitle:UCLocalize("RELOAD")
814 style:[self rightButtonStyle]
816 action:@selector(reloadButtonClicked)
819 loadingitem_ = [[[UIBarButtonItem alloc]
821 style:UIBarButtonItemStylePlain
823 action:@selector(reloadButtonClicked)
826 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
827 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
828 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
830 [self applyLeftButton];
831 [self applyRightButton];
835 - (NSString *) applicationNameForUserAgent {
840 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
842 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
843 [webview_ setDelegate:self];
844 [self setView:webview_];
846 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
847 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
849 [webview_ setDetectsPhoneNumbers:NO];
851 [webview_ setScalesPageToFit:YES];
853 UIWebDocumentView *document([webview_ _documentView]);
855 // XXX: I think this improves scrolling; the hardcoded-ness sucks
856 [document setTileSize:CGSizeMake(320, 500)];
858 [document setBackgroundColor:[UIColor clearColor]];
860 // XXX: this is terribly (too?) expensive
861 [document setDrawsBackground:NO];
863 WebView *webview([document webView]);
864 WebPreferences *preferences([webview preferences]);
866 // XXX: I have no clue if I actually /want/ this modification
867 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
868 [webview _setLayoutInterval:0];
869 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
870 [preferences _setLayoutInterval:0];
872 [preferences setCacheModel:WebCacheModelDocumentBrowser];
873 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
874 [preferences setOfflineWebApplicationCacheEnabled:YES];
876 if (NSString *agent = [self applicationNameForUserAgent])
877 [webview setApplicationNameForUserAgent:agent];
879 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
880 [webview setShouldUpdateWhileOffscreen:NO];
883 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
884 [document setAllowsMessaging:YES];
885 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
886 [webview _setAllowsMessaging:YES];
889 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
890 scroller_ = [webview_ _scrollView];
892 [scroller_ setDirectionalLockEnabled:YES];
893 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
894 [scroller_ setDelaysContentTouches:NO];
896 [scroller_ setCanCancelContentTouches:YES];
897 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
898 UIScroller *scroller([webview_ _scroller]);
899 scroller_ = (UIScrollView *) scroller;
901 [scroller setDirectionalScrolling:YES];
902 // XXX: we might be better off /not/ setting this on older systems
903 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
904 [scroller setScrollHysteresis:0]; /* 8 */
906 [scroller setThumbDetectionEnabled:NO];
908 // use NO with UIApplicationUseLegacyEvents(YES)
909 [scroller setEventMode:YES];
911 // XXX: this is handled by setBounces, right?
912 //[scroller setAllowsRubberBanding:YES];
915 [scroller_ setFixedBackgroundPattern:YES];
916 [scroller_ setBackgroundColor:[UIColor clearColor]];
917 [scroller_ setClipsSubviews:YES];
919 [scroller_ setBounces:YES];
920 [scroller_ setScrollingEnabled:YES];
921 [scroller_ setShowBackgroundShadow:NO];
923 [self setViewportWidth:width_];
925 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
926 [table setScrollsToTop:NO];
927 [webview_ insertSubview:table atIndex:0];
929 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
930 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
935 - (void) releaseSubviews {
939 [self releaseNetworkActivityIndicator];
941 [super releaseSubviews];
944 - (id) initWithWidth:(float)width {
945 return [self initWithWidth:width ofClass:[self class]];
949 return [self initWithWidth:0];
952 - (id) initWithURL:(NSURL *)url {
953 if ((self = [self init]) != nil) {
958 - (id) initWithRequest:(NSURLRequest *)request {
959 if ((self = [self init]) != nil) {
960 [self setRequest:request];
964 - (void) callFunction:(WebScriptObject *)function {
965 WebThreadLocked lock;
967 WebView *webview([[[self webView] _documentView] webView]);
968 WebFrame *frame([webview mainFrame]);
970 JSGlobalContextRef context([frame globalContext]);
971 JSObjectRef object([function JSObject]);
972 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
975 - (void) reloadButtonClicked {
976 [self reloadURLWithCache:NO];
979 - (void) _customButtonClicked {
980 [self reloadButtonClicked];
983 - (void) customButtonClicked {
985 if (function_ != nil)
986 [self callFunction:function_];
989 [self _customButtonClicked];
992 + (float) defaultWidth {
996 - (void) setNavigationBarStyle:(NSString *)name {
998 if ([name isEqualToString:@"Black"])
999 style = UIBarStyleBlack;
1001 style = UIBarStyleDefault;
1003 [[[self navigationController] navigationBar] setBarStyle:style];
1006 - (void) setNavigationBarTintColor:(UIColor *)color {
1007 [[[self navigationController] navigationBar] setTintColor:color];
1010 - (void) setBadgeValue:(id)value {
1011 [[[self navigationController] tabBarItem] setBadgeValue:value];
1014 - (void) setHidesBackButton:(bool)value {
1015 [[self navigationItem] setHidesBackButton:value];
1016 [self applyLeftButton];
1019 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
1020 [self setHidesBackButton:[value boolValue]];
1023 - (void) dispatchEvent:(NSString *)event {
1024 [[self webView] dispatchEvent:event];
1027 - (bool) hidesNavigationBar {
1028 return hidesNavigationBar_;
1031 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1033 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1036 - (void) setHidesNavigationBar:(bool)value {
1037 if (hidesNavigationBar_ != value) {
1038 hidesNavigationBar_ = value;
1039 [self _setHidesNavigationBar:YES animated:YES];
1043 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1044 [self setHidesNavigationBar:[value boolValue]];
1047 - (void) setScrollAlwaysBounceVertical:(bool)value {
1048 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1049 UIScrollView *scroller([webview_ _scrollView]);
1050 [scroller setAlwaysBounceVertical:value];
1051 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1052 //UIScroller *scroller([webview_ _scroller]);
1053 // XXX: I am sad here.
1057 - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1058 [self setScrollAlwaysBounceVertical:[value boolValue]];
1061 - (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style {
1062 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1063 UIScrollView *scroller([webview_ _scrollView]);
1064 [scroller setIndicatorStyle:style];
1065 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1066 UIScroller *scroller([webview_ _scroller]);
1067 [scroller setScrollerIndicatorStyle:style];
1071 - (void) setScrollIndicatorStyleWithName:(NSString *)style {
1072 UIScrollViewIndicatorStyle value;
1075 else if ([style isEqualToString:@"default"])
1076 value = UIScrollViewIndicatorStyleDefault;
1077 else if ([style isEqualToString:@"black"])
1078 value = UIScrollViewIndicatorStyleBlack;
1079 else if ([style isEqualToString:@"white"])
1080 value = UIScrollViewIndicatorStyleWhite;
1083 [self setScrollIndicatorStyle:value];
1086 - (void) viewWillAppear:(BOOL)animated {
1089 if ([self hidesNavigationBar])
1090 [self _setHidesNavigationBar:YES animated:animated];
1092 // XXX: why isn't this evern called automatically?
1093 [[self webView] setNeedsLayout];
1095 [self dispatchEvent:@"CydiaViewWillAppear"];
1096 [super viewWillAppear:animated];
1099 - (void) viewDidAppear:(BOOL)animated {
1100 [super viewDidAppear:animated];
1101 [self dispatchEvent:@"CydiaViewDidAppear"];
1104 - (void) viewWillDisappear:(BOOL)animated {
1105 [self dispatchEvent:@"CydiaViewWillDisappear"];
1106 [super viewWillDisappear:animated];
1108 if ([self hidesNavigationBar])
1109 [self _setHidesNavigationBar:NO animated:animated];
1114 - (void) viewDidDisappear:(BOOL)animated {
1115 [super viewDidDisappear:animated];
1116 [self dispatchEvent:@"CydiaViewDidDisappear"];