1 #include "CyteKit/UCPlatform.h"
3 #include "CyteKit/IndirectDelegate.h"
4 #include "CyteKit/Localize.h"
5 #include "CyteKit/MFMailComposeViewController-MailToURL.h"
6 #include "CyteKit/RegEx.hpp"
7 #include "CyteKit/WebThreadLocked.hpp"
8 #include "CyteKit/WebViewController.h"
10 #include "iPhonePrivate.h"
11 #include <Menes/ObjectHandle.h>
13 //#include <QuartzCore/CALayer.h>
14 // XXX: fix the minimum requirement
15 extern NSString * const kCAFilterNearest;
17 #include <WebCore/WebCoreThread.h>
20 #include <objc/runtime.h>
23 #define DefaultTimeout_ 120.0
25 #define ShowInternals 0
29 #define lprintf(args...) fprintf(stderr, args)
31 JSValueRef (*$JSObjectCallAsFunction)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *);
33 // XXX: centralize these special class things to some file or mechanism?
34 static Class $MFMailComposeViewController;
36 float CYScrollViewDecelerationRateNormal;
38 @interface WebFrame (Cydia)
39 - (void) cydia$updateHeight;
42 @implementation WebFrame (Cydia)
44 - (NSString *) description {
45 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[([self provisionalDataSource] ?: [self dataSource]) request] URL] absoluteString]];
48 - (void) cydia$updateHeight {
49 [[[self frameElement] style]
51 value:[NSString stringWithFormat:@"%dpx",
52 [[[self DOMDocument] body] scrollHeight]]
58 /* Indirect Delegate {{{ */
59 @implementation IndirectDelegate
65 - (void) setDelegate:(id)delegate {
69 - (id) initWithDelegate:(id)delegate {
74 - (IMP) methodForSelector:(SEL)sel {
75 if (IMP method = [super methodForSelector:sel])
77 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
81 - (BOOL) respondsToSelector:(SEL)sel {
82 if ([super respondsToSelector:sel])
85 // XXX: WebThreadCreateNSInvocation returns nil
88 fprintf(stderr, "[%s]R?%s\n", class_getName(object_getClass(self)), sel_getName(sel));
91 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
94 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
95 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
99 fprintf(stderr, "[%s]S?%s\n", class_getName(object_getClass(self)), sel_getName(sel));
102 if (delegate_ != nil)
103 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
106 // XXX: I fucking hate Apple so very very bad
107 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
110 - (void) forwardInvocation:(NSInvocation *)inv {
111 SEL sel = [inv selector];
112 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
113 [inv invokeWithTarget:delegate_];
119 @implementation CyteWebViewController {
120 _H<CyteWebView, 1> webview_;
121 _transient UIScrollView *scroller_;
123 _H<UIActivityIndicatorView> indicator_;
124 _H<IndirectDelegate, 1> indirect_;
125 _H<NSURLAuthenticationChallenge> challenge_;
128 _H<NSURLRequest> request_;
131 _transient NSNumber *sensitive_;
135 _H<NSMutableSet> loading_;
137 _H<NSMutableSet> registered_;
140 // XXX: NSString * or UIImage *
141 _H<NSObject> custom_;
144 _H<WebScriptObject> function_;
149 _H<UIBarButtonItem> reloaditem_;
150 _H<UIBarButtonItem> loadingitem_;
153 bool hidesNavigationBar_;
154 bool allowsNavigationAction_;
158 #include "CyteKit/UCInternal.h"
161 + (void) _initialize {
162 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
166 js = dlopen("/System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore", RTLD_GLOBAL | RTLD_LAZY);
168 js = dlopen("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore", RTLD_GLOBAL | RTLD_LAZY);
170 $JSObjectCallAsFunction = reinterpret_cast<JSValueRef (*)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(js, "JSObjectCallAsFunction"));
172 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
173 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
175 if (CGFloat *_UIScrollViewDecelerationRateNormal = reinterpret_cast<CGFloat *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
176 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
177 else // XXX: this actually might be fast on some older systems: we should look into this
178 CYScrollViewDecelerationRateNormal = 0.998;
181 - (bool) retainsNetworkActivityIndicator {
185 - (void) releaseNetworkActivityIndicator {
186 if ([loading_ count] != 0) {
187 [loading_ removeAllObjects];
189 if ([self retainsNetworkActivityIndicator])
190 [self.delegate releaseNetworkActivityIndicator];
196 NSLog(@"[CyteWebViewController dealloc]");
199 [self releaseNetworkActivityIndicator];
204 - (NSString *) description {
205 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]];
208 - (CyteWebView *) webView {
209 return (CyteWebView *) [self view];
212 - (CyteWebViewController *) indirect {
213 return (CyteWebViewController *) (IndirectDelegate *) indirect_;
216 - (NSURL *) URLWithURL:(NSURL *)url {
220 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy referrer:(NSString *)referrer {
221 NSMutableURLRequest *request([NSMutableURLRequest
222 requestWithURL:[self URLWithURL:url]
224 timeoutInterval:DefaultTimeout_
227 [request setValue:referrer forHTTPHeaderField:@"Referer"];
232 - (void) setRequest:(NSURLRequest *)request {
233 _assert(request_ == nil);
237 - (NSURLRequest *) request {
241 - (void) setURL:(NSURL *)url {
242 [self setURL:url withReferrer:nil];
245 - (void) setURL:(NSURL *)url withReferrer:(NSString *)referrer {
246 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy referrer:referrer]];
249 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
250 [self loadRequest:[self requestWithURL:url cachePolicy:policy referrer:nil]];
253 - (void) loadURL:(NSURL *)url {
254 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
257 - (void) loadRequest:(NSURLRequest *)request {
259 NSLog(@"loadRequest:%@", request);
265 WebThreadLocked lock;
266 [[self webView] loadRequest:request];
269 - (void) reloadURLWithCache:(BOOL)cache {
273 NSMutableURLRequest *request([request_ mutableCopy]);
274 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
278 if (cache || [request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
279 [self loadRequest:request_];
281 UIAlertView *alert = [[[UIAlertView alloc]
282 initWithTitle:UCLocalize("RESUBMIT_FORM")
285 cancelButtonTitle:UCLocalize("CANCEL")
287 UCLocalize("SUBMIT"),
291 [alert setContext:@"submit"];
296 - (void) reloadData {
300 [self dispatchEvent:@"CydiaReloadData"];
302 [self reloadURLWithCache:YES];
305 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
308 function_ = function;
310 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
313 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
316 function_ = function;
318 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
321 - (void) removeButton {
322 custom_ = [NSNull null];
323 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
326 - (void) scrollToBottomAnimated:(NSNumber *)animated {
327 CGSize size([scroller_ contentSize]);
328 CGPoint offset([scroller_ contentOffset]);
329 CGRect frame([scroller_ frame]);
331 if (size.height - offset.y < frame.size.height + 20.f) {
332 CGRect rect = {{0, size.height-1}, {size.width, 1}};
333 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
337 - (void) _setViewportWidth {
338 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
341 - (void) setViewportWidth:(float)width {
342 width_ = width != 0 ? width : [[self class] defaultWidth];
343 [self _setViewportWidth];
346 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
347 [self setViewportWidth:[width floatValue]];
350 - (void) setViewportWidthOnMainThread:(float)width {
351 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
354 - (void) webViewUpdateViewSettings:(UIWebView *)view {
355 [self _setViewportWidth];
358 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
359 [self dismissModalViewControllerAnimated:YES];
362 - (void) _setupMail:(MFMailComposeViewController *)controller {
365 - (void) _openMailToURL:(NSURL *)url {
366 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
367 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
368 [controller setMailComposeDelegate:self];
370 [controller setMailToURL:url];
372 [self _setupMail:controller];
374 [self presentModalViewController:controller animated:YES];
378 UIApplication *app([UIApplication sharedApplication]);
379 if ([app respondsToSelector:@selector(openURL:asPanel:)])
380 [app openURL:url asPanel:YES];
385 - (bool) _allowJavaScriptPanel {
389 - (bool) allowsNavigationAction {
390 return allowsNavigationAction_;
393 - (void) setAllowsNavigationAction:(bool)value {
394 allowsNavigationAction_ = value;
397 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
398 [self setAllowsNavigationAction:[value boolValue]];
401 - (void) popViewControllerWithNumber:(NSNumber *)value {
402 UINavigationController *navigation([self navigationController]);
403 if ([navigation topViewController] == self)
404 [navigation popViewControllerAnimated:[value boolValue]];
407 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
408 NSValue *object([NSValue valueWithNonretainedObject:frame]);
409 if (![loading_ containsObject:object])
411 [loading_ removeObject:object];
413 [self _didFinishLoading];
415 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
418 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
423 if ([frame parentFrame] == nil) {
424 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
425 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
426 [[error localizedDescription] stringByAddingPercentEscapes]
433 - (void) pushRequest:(NSURLRequest *)request forAction:(NSDictionary *)action asPop:(bool)pop {
434 WebFrame *frame(nil);
435 if (NSDictionary *WebActionElement = [action objectForKey:@"WebActionElementKey"])
436 frame = [WebActionElement objectForKey:@"WebElementFrame"];
438 frame = [[[[self webView] _documentView] webView] mainFrame];
440 WebDataSource *source([frame provisionalDataSource] ?: [frame dataSource]);
441 NSString *referrer([request valueForHTTPHeaderField:@"Referer"] ?: [[[source request] URL] absoluteString]);
443 NSURL *url([request URL]);
445 // XXX: filter to internal usage?
446 CyteViewController *page([self.delegate pageForURL:url forExternal:NO withReferrer:referrer]);
449 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
450 [browser setRequest:request];
454 [page setDelegate:self.delegate];
455 [page setPageColor:self.pageColor];
458 [[self navigationItem] setTitle:title_];
460 [[self navigationController] pushViewController:page animated:YES];
462 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
464 [navigation setDelegate:self.delegate];
466 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
467 initWithTitle:UCLocalize("CLOSE")
468 style:UIBarButtonItemStylePlain
470 action:@selector(close)
473 [[self navigationController] presentModalViewController:navigation animated:YES];
477 // CyteWebViewDelegate {{{
478 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
480 static RegEx irritating("(?"
481 ":" "The page at .* displayed insecure content from .*\\."
482 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
485 if (NSString *data = [message objectForKey:@"message"])
486 if (irritating(data))
489 NSLog(@"addMessageToConsole:%@", message);
493 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
495 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ %@ frame:%@", action, request, [request allHTTPHeaderFields], frame);
498 NSURL *url(request == nil ? nil : [request URL]);
499 NSString *scheme([[url scheme] lowercaseString]);
500 NSString *absolute([[url absoluteString] lowercaseString]);
503 [scheme isEqualToString:@"itms"] ||
504 [scheme isEqualToString:@"itmss"] ||
505 [scheme isEqualToString:@"itms-apps"] ||
506 [scheme isEqualToString:@"itms-appss"] ||
507 [absolute hasPrefix:@"http://itunes.apple.com/"] ||
508 [absolute hasPrefix:@"https://itunes.apple.com/"] ||
512 UIAlertView *alert = [[[UIAlertView alloc]
513 initWithTitle:UCLocalize("APP_STORE_REDIRECT")
516 cancelButtonTitle:UCLocalize("CANCEL")
522 [alert setContext:@"itmsappss"];
529 if ([frame parentFrame] == nil) {
531 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
533 [self pushRequest:request forAction:action asPop:NO];
540 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
542 NSLog(@"didDecidePolicy:%u forNavigationAction:%@ request:%@ %@ frame:%@", decision, action, request, [request allHTTPHeaderFields], frame);
545 if ([frame parentFrame] == nil) {
547 case CYWebPolicyDecisionIgnore:
548 if ([[request_ URL] isEqual:[request URL]])
552 case CYWebPolicyDecisionUse:
563 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
565 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ %@ newFrameName:%@", action, request, [request allHTTPHeaderFields], name);
568 NSURL *url([request URL]);
572 if ([name isEqualToString:@"_open"])
573 [self.delegate openURL:url];
575 NSString *scheme([[url scheme] lowercaseString]);
576 if ([scheme isEqualToString:@"mailto"])
577 [self _openMailToURL:url];
579 [self pushRequest:request forAction:action asPop:[name isEqualToString:@"_popup"]];
585 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
587 NSLog(@"didClearWindowObject:%@ forFrame:%@", window, frame);
591 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
593 NSLog(@"didCommitLoadForFrame:%@", frame);
596 if ([frame parentFrame] == nil) {
600 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
602 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
605 [self _didFailWithError:error forFrame:frame];
608 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
610 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
613 [self _didFailWithError:error forFrame:frame];
616 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
617 NSValue *object([NSValue valueWithNonretainedObject:frame]);
618 if (![loading_ containsObject:object])
620 [loading_ removeObject:object];
622 if ([frame parentFrame] == nil) {
623 if (DOMDocument *document = [frame DOMDocument])
624 if (DOMNodeList *bodies = [document getElementsByTagName:@"body"])
625 for (DOMHTMLBodyElement *body in (id) bodies) {
626 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
630 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
631 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
632 DOMRGBColor *rgb([color getRGBColorValue]);
634 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
635 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
636 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
637 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
641 colorWithRed:(red / 255)
649 [super setPageColor:uic];
650 [scroller_ setBackgroundColor:self.pageColor];
655 [self _didFinishLoading];
658 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
659 if ([frame parentFrame] != nil)
664 [[self navigationItem] setTitle:title_];
667 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
669 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
672 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
674 if ([frame parentFrame] == nil) {
680 [registered_ removeAllObjects];
683 allowsNavigationAction_ = true;
685 [self setHidesNavigationBar:NO];
686 [self setScrollAlwaysBounceVertical:true];
687 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
689 // XXX: do we still need to do this?
690 [[self navigationItem] setTitle:nil];
693 [self _didStartLoading];
696 - (void) webView:(WebView *)view resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
697 challenge_ = [challenge retain];
699 NSURLProtectionSpace *space([challenge protectionSpace]);
700 NSString *realm([space realm]);
704 UIAlertView *alert = [[[UIAlertView alloc]
708 cancelButtonTitle:UCLocalize("CANCEL")
709 otherButtonTitles:UCLocalize("LOGIN"), nil
712 [alert setContext:@"challenge"];
713 [alert setNumberOfRows:1];
715 [alert addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
716 [alert addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
718 UITextField *username([alert textFieldAtIndex:0]); {
719 NSObject<UITextInputTraits> *traits([username textInputTraits]);
720 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
721 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
722 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
723 [traits setReturnKeyType:UIReturnKeyNext];
726 UITextField *password([alert textFieldAtIndex:1]); {
727 NSObject<UITextInputTraits> *traits([password textInputTraits]);
728 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
729 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
730 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
731 // XXX: UIReturnKeyDone
732 [traits setReturnKeyType:UIReturnKeyNext];
733 [traits setSecureTextEntry:YES];
739 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
741 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
747 - (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
749 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
755 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
756 return [self _allowJavaScriptPanel];
759 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
760 return [self _allowJavaScriptPanel];
763 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
764 return [self _allowJavaScriptPanel];
767 - (void) webViewClose:(WebView *)view {
773 [[[self navigationController] parentOrPresentingViewController] dismissModalViewControllerAnimated:YES];
776 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
777 NSString *context([alert context]);
779 if ([context isEqualToString:@"sensitive"]) {
782 sensitive_ = [NSNumber numberWithBool:YES];
786 sensitive_ = [NSNumber numberWithBool:NO];
790 [alert dismissWithClickedButtonIndex:-1 animated:YES];
791 } else if ([context isEqualToString:@"challenge"]) {
792 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
794 if (button == [alert cancelButtonIndex])
795 [sender cancelAuthenticationChallenge:challenge_];
796 else if (button == [alert firstOtherButtonIndex]) {
797 NSString *username([[alert textFieldAtIndex:0] text]);
798 NSString *password([[alert textFieldAtIndex:1] text]);
800 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
802 [sender useCredential:credential forAuthenticationChallenge:challenge_];
807 [alert dismissWithClickedButtonIndex:-1 animated:YES];
808 } else if ([context isEqualToString:@"itmsappss"]) {
809 if (button == [alert cancelButtonIndex]) {
810 } else if (button == [alert firstOtherButtonIndex]) {
811 [self.delegate openURL:appstore_];
814 [alert dismissWithClickedButtonIndex:-1 animated:YES];
815 } else if ([context isEqualToString:@"submit"]) {
816 if (button == [alert cancelButtonIndex]) {
817 } else if (button == [alert firstOtherButtonIndex]) {
818 if (request_ != nil) {
819 WebThreadLocked lock;
820 [[self webView] loadRequest:request_];
824 [alert dismissWithClickedButtonIndex:-1 animated:YES];
828 - (UIBarButtonItemStyle) rightButtonStyle {
829 if (style_ == nil) normal:
830 return UIBarButtonItemStylePlain;
831 else if ([style_ isEqualToString:@"Normal"])
832 return UIBarButtonItemStylePlain;
833 else if ([style_ isEqualToString:@"Highlighted"])
834 return UIBarButtonItemStyleDone;
838 - (UIBarButtonItem *) customButton {
840 return [self rightButton];
841 else if ((/*clang:*/id) custom_ == [NSNull null])
844 return [[[UIBarButtonItem alloc]
845 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
846 style:[self rightButtonStyle]
848 action:@selector(customButtonClicked)
852 - (UIBarButtonItem *) leftButton {
853 UINavigationItem *item([self navigationItem]);
854 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
857 if (UINavigationController *navigation = [self navigationController])
858 if ([[navigation parentOrPresentingViewController] modalViewController] == navigation)
859 return [[[UIBarButtonItem alloc]
860 initWithTitle:UCLocalize("CLOSE")
861 style:UIBarButtonItemStylePlain
863 action:@selector(close)
869 - (void) applyLeftButton {
870 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
873 - (UIBarButtonItem *) rightButton {
877 - (void) applyLoadingTitle {
878 [[self navigationItem] setTitle:UCLocalize("LOADING")];
881 - (void) layoutRightButton {
882 [[loadingitem_ view] addSubview:indicator_];
883 [[loadingitem_ view] bringSubviewToFront:indicator_];
886 - (void) applyRightButton {
887 if ([self isLoading]) {
888 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
889 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
891 [indicator_ startAnimating];
892 [self applyLoadingTitle];
894 [indicator_ stopAnimating];
895 [[self navigationItem] setRightBarButtonItem:[self customButton] animated:YES];
899 - (void) didStartLoading {
900 // Overridden in subclasses.
903 - (void) _didStartLoading {
904 [self applyRightButton];
906 if ([loading_ count] != 1)
909 if ([self retainsNetworkActivityIndicator])
910 [self.delegate retainNetworkActivityIndicator];
912 [self didStartLoading];
915 - (void) didFinishLoading {
916 // Overridden in subclasses.
919 - (void) _didFinishLoading {
920 if ([loading_ count] != 0)
923 [self applyRightButton];
924 [[self navigationItem] setTitle:title_];
926 if ([self retainsNetworkActivityIndicator])
927 [self.delegate releaseNetworkActivityIndicator];
929 [self didFinishLoading];
933 return [loading_ count] != 0;
936 - (id) initWithWidth:(float)width ofClass:(Class)_class {
937 if ((self = [super init]) != nil) {
941 [super setPageColor:nil];
943 allowsNavigationAction_ = true;
945 loading_ = [NSMutableSet setWithCapacity:5];
946 registered_ = [NSMutableSet setWithCapacity:5];
947 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
949 reloaditem_ = [[[UIBarButtonItem alloc]
950 initWithTitle:UCLocalize("RELOAD")
951 style:[self rightButtonStyle]
953 action:@selector(reloadButtonClicked)
956 loadingitem_ = [[[UIBarButtonItem alloc]
957 initWithTitle:(kCFCoreFoundationVersionNumber >= 800 ? @" " : @" ")
958 style:UIBarButtonItemStylePlain
960 action:@selector(customButtonClicked)
963 UIActivityIndicatorViewStyle style;
965 if (kCFCoreFoundationVersionNumber >= 800) {
966 style = UIActivityIndicatorViewStyleGray;
969 style = UIActivityIndicatorViewStyleWhite;
973 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style] autorelease];
974 [indicator_ setFrame:CGRectMake(left, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
975 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
977 [self applyLeftButton];
978 [self applyRightButton];
982 - (NSString *) applicationNameForUserAgent {
987 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
989 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
990 [webview_ setDelegate:self];
991 [self setView:webview_];
993 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
994 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
996 [webview_ setDetectsPhoneNumbers:NO];
998 [webview_ setScalesPageToFit:YES];
1000 UIWebDocumentView *document([webview_ _documentView]);
1002 // XXX: I think this improves scrolling; the hardcoded-ness sucks
1003 [document setTileSize:CGSizeMake(320, 500)];
1005 WebView *webview([document webView]);
1006 WebPreferences *preferences([webview preferences]);
1008 // XXX: I have no clue if I actually /want/ this modification
1009 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1010 [webview _setLayoutInterval:0];
1011 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
1012 [preferences _setLayoutInterval:0];
1014 [preferences setCacheModel:WebCacheModelDocumentBrowser];
1015 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1017 if ([preferences respondsToSelector:@selector(setOfflineWebApplicationCacheEnabled:)])
1018 [preferences setOfflineWebApplicationCacheEnabled:YES];
1020 if (NSString *agent = [self applicationNameForUserAgent])
1021 [webview setApplicationNameForUserAgent:agent];
1023 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
1024 [webview setShouldUpdateWhileOffscreen:NO];
1027 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
1028 [document setAllowsMessaging:YES];
1029 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
1030 [webview _setAllowsMessaging:YES];
1033 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1034 scroller_ = [webview_ _scrollView];
1036 [scroller_ setDirectionalLockEnabled:YES];
1037 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
1038 [scroller_ setDelaysContentTouches:NO];
1040 [scroller_ setCanCancelContentTouches:YES];
1041 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1042 UIScroller *scroller([webview_ _scroller]);
1043 scroller_ = (UIScrollView *) scroller;
1045 [scroller setDirectionalScrolling:YES];
1046 // XXX: we might be better off /not/ setting this on older systems
1047 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
1048 [scroller setScrollHysteresis:0]; /* 8 */
1050 [scroller setThumbDetectionEnabled:NO];
1052 // use NO with UIApplicationUseLegacyEvents(YES)
1053 [scroller setEventMode:YES];
1055 // XXX: this is handled by setBounces, right?
1056 //[scroller setAllowsRubberBanding:YES];
1059 [webview_ setOpaque:NO];
1060 [webview_ setBackgroundColor:nil];
1062 [scroller_ setFixedBackgroundPattern:YES];
1063 [scroller_ setBackgroundColor:self.pageColor];
1064 [scroller_ setClipsSubviews:YES];
1066 [scroller_ setBounces:YES];
1067 [scroller_ setScrollingEnabled:YES];
1068 [scroller_ setShowBackgroundShadow:NO];
1070 [self setViewportWidth:width_];
1072 if ([[UIColor groupTableViewBackgroundColor] isEqual:[UIColor clearColor]]) {
1073 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
1074 [table setScrollsToTop:NO];
1075 [webview_ insertSubview:table atIndex:0];
1076 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1079 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1084 - (void) releaseSubviews {
1088 [self releaseNetworkActivityIndicator];
1090 [super releaseSubviews];
1093 - (id) initWithWidth:(float)width {
1094 return [self initWithWidth:width ofClass:[self class]];
1098 return [self initWithWidth:0];
1101 - (id) initWithURL:(NSURL *)url {
1102 if ((self = [self init]) != nil) {
1107 - (id) initWithRequest:(NSURLRequest *)request {
1108 if ((self = [self init]) != nil) {
1109 [self setRequest:request];
1113 + (void) _lockJavaScript:(WebPreferences *)preferences {
1114 WebThreadLocked lock;
1115 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1118 - (void) callFunction:(WebScriptObject *)function {
1119 WebThreadLocked lock;
1121 WebView *webview([[[self webView] _documentView] webView]);
1122 WebPreferences *preferences([webview preferences]);
1124 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
1125 if ([webview respondsToSelector:@selector(_preferencesChanged:)])
1126 [webview _preferencesChanged:preferences];
1128 [webview _preferencesChangedNotification:[NSNotification notificationWithName:@"" object:preferences]];
1130 WebFrame *frame([webview mainFrame]);
1131 JSGlobalContextRef context([frame globalContext]);
1133 JSObjectRef object([function JSObject]);
1134 if ($JSObjectCallAsFunction != NULL)
1135 ($JSObjectCallAsFunction)(context, object, NULL, 0, NULL, NULL);
1137 // XXX: the JavaScript code submits a form, which seems to happen asynchronously
1138 NSObject *target([CyteWebViewController class]);
1139 [NSObject cancelPreviousPerformRequestsWithTarget:target selector:@selector(_lockJavaScript:) object:preferences];
1140 [target performSelector:@selector(_lockJavaScript:) withObject:preferences afterDelay:1];
1143 - (void) reloadButtonClicked {
1144 [self reloadURLWithCache:NO];
1147 - (void) _customButtonClicked {
1148 [self reloadButtonClicked];
1151 - (void) customButtonClicked {
1153 if (function_ != nil)
1154 [self callFunction:function_];
1157 [self _customButtonClicked];
1160 + (float) defaultWidth {
1164 - (void) setNavigationBarStyle:(NSString *)name {
1166 if ([name isEqualToString:@"Black"])
1167 style = UIBarStyleBlack;
1169 style = UIBarStyleDefault;
1171 [[[self navigationController] navigationBar] setBarStyle:style];
1174 - (void) setNavigationBarTintColor:(UIColor *)color {
1175 [[[self navigationController] navigationBar] setTintColor:color];
1178 - (void) setBadgeValue:(id)value {
1179 [[[self navigationController] tabBarItem] setBadgeValue:value];
1182 - (void) setHidesBackButton:(bool)value {
1183 [[self navigationItem] setHidesBackButton:value];
1184 [self applyLeftButton];
1187 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
1188 [self setHidesBackButton:[value boolValue]];
1191 - (void) dispatchEvent:(NSString *)event {
1192 [[self webView] dispatchEvent:event];
1195 - (bool) hidesNavigationBar {
1196 return hidesNavigationBar_;
1199 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1201 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1204 - (void) setHidesNavigationBar:(bool)value {
1205 if (hidesNavigationBar_ != value) {
1206 hidesNavigationBar_ = value;
1207 [self _setHidesNavigationBar:YES animated:YES];
1211 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1212 [self setHidesNavigationBar:[value boolValue]];
1215 - (void) setScrollAlwaysBounceVertical:(bool)value {
1216 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1217 UIScrollView *scroller([webview_ _scrollView]);
1218 [scroller setAlwaysBounceVertical:value];
1219 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1220 //UIScroller *scroller([webview_ _scroller]);
1221 // XXX: I am sad here.
1225 - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1226 [self setScrollAlwaysBounceVertical:[value boolValue]];
1229 - (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style {
1230 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1231 UIScrollView *scroller([webview_ _scrollView]);
1232 [scroller setIndicatorStyle:style];
1233 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1234 UIScroller *scroller([webview_ _scroller]);
1235 [scroller setScrollerIndicatorStyle:style];
1239 - (void) setScrollIndicatorStyleWithName:(NSString *)style {
1240 UIScrollViewIndicatorStyle value;
1243 else if ([style isEqualToString:@"default"])
1244 value = UIScrollViewIndicatorStyleDefault;
1245 else if ([style isEqualToString:@"black"])
1246 value = UIScrollViewIndicatorStyleBlack;
1247 else if ([style isEqualToString:@"white"])
1248 value = UIScrollViewIndicatorStyleWhite;
1251 [self setScrollIndicatorStyle:value];
1254 - (void) viewWillAppear:(BOOL)animated {
1257 if ([self hidesNavigationBar])
1258 [self _setHidesNavigationBar:YES animated:animated];
1260 // XXX: why isn't this evern called automatically?
1261 [[self webView] setNeedsLayout];
1263 [self dispatchEvent:@"CydiaViewWillAppear"];
1264 [super viewWillAppear:animated];
1267 - (void) viewDidAppear:(BOOL)animated {
1268 [super viewDidAppear:animated];
1269 [self dispatchEvent:@"CydiaViewDidAppear"];
1272 - (void) viewWillDisappear:(BOOL)animated {
1273 [self dispatchEvent:@"CydiaViewWillDisappear"];
1274 [super viewWillDisappear:animated];
1276 if ([self hidesNavigationBar])
1277 [self _setHidesNavigationBar:NO animated:animated];
1282 - (void) viewDidDisappear:(BOOL)animated {
1283 [super viewDidDisappear:animated];
1284 [self dispatchEvent:@"CydiaViewDidDisappear"];
1287 - (void) updateHeights:(NSTimer *)timer {
1288 for (WebFrame *frame in (id) registered_)
1289 [frame cydia$updateHeight];
1292 - (void) registerFrame:(WebFrame *)frame {
1293 [registered_ addObject:frame];
1296 timer_ = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateHeights:) userInfo:nil repeats:YES];