]> git.saurik.com Git - cydia.git/blob - CyteKit/WebViewController.mm
20838bf99ea099d5e0aaddac68dbaa0b881f313e
[cydia.git] / CyteKit / WebViewController.mm
1 #include "CyteKit/UCPlatform.h"
2
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"
9
10 #include "iPhonePrivate.h"
11 #include <Menes/ObjectHandle.h>
12
13 //#include <QuartzCore/CALayer.h>
14 // XXX: fix the minimum requirement
15 extern NSString * const kCAFilterNearest;
16
17 #include <WebCore/WebCoreThread.h>
18
19 #include <dlfcn.h>
20 #include <objc/runtime.h>
21
22 #define ForSaurik 0
23 #define DefaultTimeout_ 120.0
24
25 #define ShowInternals 0
26 #define LogBrowser 0
27 #define LogMessages 0
28
29 #define lprintf(args...) fprintf(stderr, args)
30
31 JSValueRef (*$JSObjectCallAsFunction)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *);
32
33 // XXX: centralize these special class things to some file or mechanism?
34 static Class $MFMailComposeViewController;
35
36 float CYScrollViewDecelerationRateNormal;
37
38 @interface WebFrame (Cydia)
39 - (void) cydia$updateHeight;
40 @end
41
42 @implementation WebFrame (Cydia)
43
44 - (NSString *) description {
45 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[([self provisionalDataSource] ?: [self dataSource]) request] URL] absoluteString]];
46 }
47
48 - (void) cydia$updateHeight {
49 [[[self frameElement] style]
50 setProperty:@"height"
51 value:[NSString stringWithFormat:@"%dpx",
52 [[[self DOMDocument] body] scrollHeight]]
53 priority:nil];
54 }
55
56 @end
57
58 /* Indirect Delegate {{{ */
59 @implementation IndirectDelegate
60
61 - (id) delegate {
62 return delegate_;
63 }
64
65 - (void) setDelegate:(id)delegate {
66 delegate_ = delegate;
67 }
68
69 - (id) initWithDelegate:(id)delegate {
70 delegate_ = delegate;
71 return self;
72 }
73
74 - (IMP) methodForSelector:(SEL)sel {
75 if (IMP method = [super methodForSelector:sel])
76 return method;
77 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
78 return NULL;
79 }
80
81 - (BOOL) respondsToSelector:(SEL)sel {
82 if ([super respondsToSelector:sel])
83 return YES;
84
85 // XXX: WebThreadCreateNSInvocation returns nil
86
87 #if ShowInternals
88 fprintf(stderr, "[%s]R?%s\n", class_getName(object_getClass(self)), sel_getName(sel));
89 #endif
90
91 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
92 }
93
94 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
95 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
96 return method;
97
98 #if ShowInternals
99 fprintf(stderr, "[%s]S?%s\n", class_getName(object_getClass(self)), sel_getName(sel));
100 #endif
101
102 if (delegate_ != nil)
103 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
104 return sig;
105
106 // XXX: I fucking hate Apple so very very bad
107 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
108 }
109
110 - (void) forwardInvocation:(NSInvocation *)inv {
111 SEL sel = [inv selector];
112 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
113 [inv invokeWithTarget:delegate_];
114 }
115
116 @end
117 /* }}} */
118
119 @implementation CyteWebViewController {
120 _H<CyteWebView, 1> webview_;
121 _transient UIScrollView *scroller_;
122
123 _H<UIActivityIndicatorView> indicator_;
124 _H<IndirectDelegate, 1> indirect_;
125 _H<NSURLAuthenticationChallenge> challenge_;
126
127 bool error_;
128 _H<NSURLRequest> request_;
129 bool ready_;
130
131 _transient NSNumber *sensitive_;
132 _H<NSURL> appstore_;
133
134 _H<NSString> title_;
135 _H<NSMutableSet> loading_;
136
137 _H<NSMutableSet> registered_;
138 _H<NSTimer> timer_;
139
140 // XXX: NSString * or UIImage *
141 _H<NSObject> custom_;
142 _H<NSString> style_;
143
144 _H<WebScriptObject> function_;
145
146 float width_;
147 Class class_;
148
149 _H<UIBarButtonItem> reloaditem_;
150 _H<UIBarButtonItem> loadingitem_;
151
152 bool visible_;
153 bool hidesNavigationBar_;
154 bool allowsNavigationAction_;
155 }
156
157 #if ShowInternals
158 #include "CyteKit/UCInternal.h"
159 #endif
160
161 + (void) _initialize {
162 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
163
164 void *js(NULL);
165 if (js == NULL)
166 js = dlopen("/System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore", RTLD_GLOBAL | RTLD_LAZY);
167 if (js == NULL)
168 js = dlopen("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore", RTLD_GLOBAL | RTLD_LAZY);
169 if (js != NULL)
170 $JSObjectCallAsFunction = reinterpret_cast<JSValueRef (*)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(js, "JSObjectCallAsFunction"));
171
172 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
173 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
174
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;
179 }
180
181 - (bool) retainsNetworkActivityIndicator {
182 return true;
183 }
184
185 - (void) releaseNetworkActivityIndicator {
186 if ([loading_ count] != 0) {
187 [loading_ removeAllObjects];
188
189 if ([self retainsNetworkActivityIndicator])
190 [self.delegate releaseNetworkActivityIndicator];
191 }
192 }
193
194 - (void) dealloc {
195 #if LogBrowser
196 NSLog(@"[CyteWebViewController dealloc]");
197 #endif
198
199 [self releaseNetworkActivityIndicator];
200
201 [super dealloc];
202 }
203
204 - (NSString *) description {
205 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]];
206 }
207
208 - (CyteWebView *) webView {
209 return (CyteWebView *) [self view];
210 }
211
212 - (CyteWebViewController *) indirect {
213 return (CyteWebViewController *) (IndirectDelegate *) indirect_;
214 }
215
216 - (NSURL *) URLWithURL:(NSURL *)url {
217 return url;
218 }
219
220 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy referrer:(NSString *)referrer {
221 NSMutableURLRequest *request([NSMutableURLRequest
222 requestWithURL:[self URLWithURL:url]
223 cachePolicy:policy
224 timeoutInterval:DefaultTimeout_
225 ]);
226
227 [request setValue:referrer forHTTPHeaderField:@"Referer"];
228
229 return request;
230 }
231
232 - (void) setRequest:(NSURLRequest *)request {
233 _assert(request_ == nil);
234 request_ = request;
235 }
236
237 - (NSURLRequest *) request {
238 return request_;
239 }
240
241 - (void) setURL:(NSURL *)url {
242 [self setURL:url withReferrer:nil];
243 }
244
245 - (void) setURL:(NSURL *)url withReferrer:(NSString *)referrer {
246 [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy referrer:referrer]];
247 }
248
249 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
250 [self loadRequest:[self requestWithURL:url cachePolicy:policy referrer:nil]];
251 }
252
253 - (void) loadURL:(NSURL *)url {
254 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
255 }
256
257 - (void) loadRequest:(NSURLRequest *)request {
258 #if LogBrowser
259 NSLog(@"loadRequest:%@", request);
260 #endif
261
262 error_ = false;
263 ready_ = true;
264
265 WebThreadLocked lock;
266 [[self webView] loadRequest:request];
267 }
268
269 - (void) reloadURLWithCache:(BOOL)cache {
270 if (request_ == nil)
271 return;
272
273 NSMutableURLRequest *request([request_ mutableCopy]);
274 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
275
276 request_ = request;
277
278 if (cache || [request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
279 [self loadRequest:request_];
280 else {
281 UIAlertView *alert = [[[UIAlertView alloc]
282 initWithTitle:UCLocalize("RESUBMIT_FORM")
283 message:nil
284 delegate:self
285 cancelButtonTitle:UCLocalize("CANCEL")
286 otherButtonTitles:
287 UCLocalize("SUBMIT"),
288 nil
289 ] autorelease];
290
291 [alert setContext:@"submit"];
292 [alert show];
293 }
294 }
295
296 - (void) reloadData {
297 [super reloadData];
298
299 if (ready_)
300 [self dispatchEvent:@"CydiaReloadData"];
301 else
302 [self reloadURLWithCache:YES];
303 }
304
305 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
306 custom_ = button;
307 style_ = style;
308 function_ = function;
309
310 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
311 }
312
313 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
314 custom_ = button;
315 style_ = style;
316 function_ = function;
317
318 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
319 }
320
321 - (void) removeButton {
322 custom_ = [NSNull null];
323 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
324 }
325
326 - (void) scrollToBottomAnimated:(NSNumber *)animated {
327 CGSize size([scroller_ contentSize]);
328 CGPoint offset([scroller_ contentOffset]);
329 CGRect frame([scroller_ frame]);
330
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]];
334 }
335 }
336
337 - (void) _setViewportWidth {
338 [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
339 }
340
341 - (void) setViewportWidth:(float)width {
342 width_ = width != 0 ? width : [[self class] defaultWidth];
343 [self _setViewportWidth];
344 }
345
346 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
347 [self setViewportWidth:[width floatValue]];
348 }
349
350 - (void) setViewportWidthOnMainThread:(float)width {
351 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
352 }
353
354 - (void) webViewUpdateViewSettings:(UIWebView *)view {
355 [self _setViewportWidth];
356 }
357
358 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
359 [self dismissModalViewControllerAnimated:YES];
360 }
361
362 - (void) _setupMail:(MFMailComposeViewController *)controller {
363 }
364
365 - (void) _openMailToURL:(NSURL *)url {
366 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
367 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
368 [controller setMailComposeDelegate:self];
369
370 [controller setMailToURL:url];
371
372 [self _setupMail:controller];
373
374 [self presentModalViewController:controller animated:YES];
375 return;
376 }
377
378 UIApplication *app([UIApplication sharedApplication]);
379 if ([app respondsToSelector:@selector(openURL:asPanel:)])
380 [app openURL:url asPanel:YES];
381 else
382 [app openURL:url];
383 }
384
385 - (bool) _allowJavaScriptPanel {
386 return true;
387 }
388
389 - (bool) allowsNavigationAction {
390 return allowsNavigationAction_;
391 }
392
393 - (void) setAllowsNavigationAction:(bool)value {
394 allowsNavigationAction_ = value;
395 }
396
397 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
398 [self setAllowsNavigationAction:[value boolValue]];
399 }
400
401 - (void) popViewControllerWithNumber:(NSNumber *)value {
402 UINavigationController *navigation([self navigationController]);
403 if ([navigation topViewController] == self)
404 [navigation popViewControllerAnimated:[value boolValue]];
405 }
406
407 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
408 NSValue *object([NSValue valueWithNonretainedObject:frame]);
409 if (![loading_ containsObject:object])
410 return;
411 [loading_ removeObject:object];
412
413 [self _didFinishLoading];
414
415 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
416 return;
417
418 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
419 request_ = nil;
420 return;
421 }
422
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]
427 ]]];
428
429 error_ = true;
430 }
431 }
432
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"];
437 if (frame == nil)
438 frame = [[[[self webView] _documentView] webView] mainFrame];
439
440 WebDataSource *source([frame provisionalDataSource] ?: [frame dataSource]);
441 NSString *referrer([request valueForHTTPHeaderField:@"Referer"] ?: [[[source request] URL] absoluteString]);
442
443 NSURL *url([request URL]);
444
445 // XXX: filter to internal usage?
446 CyteViewController *page([self.delegate pageForURL:url forExternal:NO withReferrer:referrer]);
447
448 if (page == nil) {
449 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
450 [browser setRequest:request];
451 page = browser;
452 }
453
454 [page setDelegate:self.delegate];
455 [page setPageColor:self.pageColor];
456
457 if (!pop) {
458 [[self navigationItem] setTitle:title_];
459
460 [[self navigationController] pushViewController:page animated:YES];
461 } else {
462 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
463
464 [navigation setDelegate:self.delegate];
465
466 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
467 initWithTitle:UCLocalize("CLOSE")
468 style:UIBarButtonItemStylePlain
469 target:page
470 action:@selector(close)
471 ] autorelease]];
472
473 [[self navigationController] presentModalViewController:navigation animated:YES];
474 }
475 }
476
477 // CyteWebViewDelegate {{{
478 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
479 #if LogMessages
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\\."
483 ")\\n");
484
485 if (NSString *data = [message objectForKey:@"message"])
486 if (irritating(data))
487 return;
488
489 NSLog(@"addMessageToConsole:%@", message);
490 #endif
491 }
492
493 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
494 #if LogBrowser
495 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ %@ frame:%@", action, request, [request allHTTPHeaderFields], frame);
496 #endif
497
498 NSURL *url(request == nil ? nil : [request URL]);
499 NSString *scheme([[url scheme] lowercaseString]);
500 NSString *absolute([[url absoluteString] lowercaseString]);
501
502 if (
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/"] ||
509 false) {
510 appstore_ = url;
511
512 UIAlertView *alert = [[[UIAlertView alloc]
513 initWithTitle:UCLocalize("APP_STORE_REDIRECT")
514 message:nil
515 delegate:self
516 cancelButtonTitle:UCLocalize("CANCEL")
517 otherButtonTitles:
518 UCLocalize("ALLOW"),
519 nil
520 ] autorelease];
521
522 [alert setContext:@"itmsappss"];
523 [alert show];
524
525 [listener ignore];
526 return;
527 }
528
529 if ([frame parentFrame] == nil) {
530 if (!error_) {
531 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
532 if (url != nil)
533 [self pushRequest:request forAction:action asPop:NO];
534 [listener ignore];
535 }
536 }
537 }
538 }
539
540 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
541 #if LogBrowser
542 NSLog(@"didDecidePolicy:%u forNavigationAction:%@ request:%@ %@ frame:%@", decision, action, request, [request allHTTPHeaderFields], frame);
543 #endif
544
545 if ([frame parentFrame] == nil) {
546 switch (decision) {
547 case CYWebPolicyDecisionIgnore:
548 if ([[request_ URL] isEqual:[request URL]])
549 request_ = nil;
550 break;
551
552 case CYWebPolicyDecisionUse:
553 if (!error_)
554 request_ = request;
555 break;
556
557 default:
558 break;
559 }
560 }
561 }
562
563 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
564 #if LogBrowser
565 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ %@ newFrameName:%@", action, request, [request allHTTPHeaderFields], name);
566 #endif
567
568 NSURL *url([request URL]);
569 if (url == nil)
570 return;
571
572 if ([name isEqualToString:@"_open"])
573 [self.delegate openURL:url];
574 else {
575 NSString *scheme([[url scheme] lowercaseString]);
576 if ([scheme isEqualToString:@"mailto"])
577 [self _openMailToURL:url];
578 else
579 [self pushRequest:request forAction:action asPop:[name isEqualToString:@"_popup"]];
580 }
581
582 [listener ignore];
583 }
584
585 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
586 #if LogBrowser
587 NSLog(@"didClearWindowObject:%@ forFrame:%@", window, frame);
588 #endif
589 }
590
591 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
592 #if LogBrowser
593 NSLog(@"didCommitLoadForFrame:%@", frame);
594 #endif
595
596 if ([frame parentFrame] == nil) {
597 }
598 }
599
600 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
601 #if LogBrowser
602 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
603 #endif
604
605 [self _didFailWithError:error forFrame:frame];
606 }
607
608 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
609 #if LogBrowser
610 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
611 #endif
612
613 [self _didFailWithError:error forFrame:frame];
614 }
615
616 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
617 NSValue *object([NSValue valueWithNonretainedObject:frame]);
618 if (![loading_ containsObject:object])
619 return;
620 [loading_ removeObject:object];
621
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]);
627
628 UIColor *uic(nil);
629
630 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
631 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
632 DOMRGBColor *rgb([color getRGBColorValue]);
633
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]);
638
639 if (alpha == 1)
640 uic = [UIColor
641 colorWithRed:(red / 255)
642 green:(green / 255)
643 blue:(blue / 255)
644 alpha:alpha
645 ];
646 }
647 }
648
649 [super setPageColor:uic];
650 [scroller_ setBackgroundColor:self.pageColor];
651 break;
652 }
653 }
654
655 [self _didFinishLoading];
656 }
657
658 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
659 if ([frame parentFrame] != nil)
660 return;
661
662 title_ = title;
663
664 [[self navigationItem] setTitle:title_];
665 }
666
667 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
668 #if LogBrowser
669 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
670 #endif
671
672 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
673
674 if ([frame parentFrame] == nil) {
675 title_ = nil;
676 custom_ = nil;
677 style_ = nil;
678 function_ = nil;
679
680 [registered_ removeAllObjects];
681 timer_ = nil;
682
683 allowsNavigationAction_ = true;
684
685 [self setHidesNavigationBar:NO];
686 [self setScrollAlwaysBounceVertical:true];
687 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
688
689 // XXX: do we still need to do this?
690 [[self navigationItem] setTitle:nil];
691 }
692
693 [self _didStartLoading];
694 }
695
696 - (void) webView:(WebView *)view resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
697 challenge_ = [challenge retain];
698
699 NSURLProtectionSpace *space([challenge protectionSpace]);
700 NSString *realm([space realm]);
701 if (realm == nil)
702 realm = @"";
703
704 UIAlertView *alert = [[[UIAlertView alloc]
705 initWithTitle:realm
706 message:nil
707 delegate:self
708 cancelButtonTitle:UCLocalize("CANCEL")
709 otherButtonTitles:UCLocalize("LOGIN"), nil
710 ] autorelease];
711
712 [alert setContext:@"challenge"];
713 [alert setNumberOfRows:1];
714
715 [alert addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
716 [alert addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
717
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];
724 }
725
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];
734 }
735
736 [alert show];
737 }
738
739 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
740 #if LogBrowser
741 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
742 #endif
743
744 return request;
745 }
746
747 - (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
748 #if LogBrowser
749 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
750 #endif
751
752 return request;
753 }
754
755 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
756 return [self _allowJavaScriptPanel];
757 }
758
759 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
760 return [self _allowJavaScriptPanel];
761 }
762
763 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
764 return [self _allowJavaScriptPanel];
765 }
766
767 - (void) webViewClose:(WebView *)view {
768 [self close];
769 }
770 // }}}
771
772 - (void) close {
773 [[[self navigationController] parentOrPresentingViewController] dismissModalViewControllerAnimated:YES];
774 }
775
776 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
777 NSString *context([alert context]);
778
779 if ([context isEqualToString:@"sensitive"]) {
780 switch (button) {
781 case 1:
782 sensitive_ = [NSNumber numberWithBool:YES];
783 break;
784
785 case 2:
786 sensitive_ = [NSNumber numberWithBool:NO];
787 break;
788 }
789
790 [alert dismissWithClickedButtonIndex:-1 animated:YES];
791 } else if ([context isEqualToString:@"challenge"]) {
792 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
793
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]);
799
800 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
801
802 [sender useCredential:credential forAuthenticationChallenge:challenge_];
803 }
804
805 challenge_ = nil;
806
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_];
812 }
813
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_];
821 }
822 }
823
824 [alert dismissWithClickedButtonIndex:-1 animated:YES];
825 }
826 }
827
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;
835 else goto normal;
836 }
837
838 - (UIBarButtonItem *) customButton {
839 if (custom_ == nil)
840 return [self rightButton];
841 else if ((/*clang:*/id) custom_ == [NSNull null])
842 return nil;
843
844 return [[[UIBarButtonItem alloc]
845 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
846 style:[self rightButtonStyle]
847 target:self
848 action:@selector(customButtonClicked)
849 ] autorelease];
850 }
851
852 - (UIBarButtonItem *) leftButton {
853 UINavigationItem *item([self navigationItem]);
854 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
855 return nil;
856
857 if (UINavigationController *navigation = [self navigationController])
858 if ([[navigation parentOrPresentingViewController] modalViewController] == navigation)
859 return [[[UIBarButtonItem alloc]
860 initWithTitle:UCLocalize("CLOSE")
861 style:UIBarButtonItemStylePlain
862 target:self
863 action:@selector(close)
864 ] autorelease];
865
866 return nil;
867 }
868
869 - (void) applyLeftButton {
870 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
871 }
872
873 - (UIBarButtonItem *) rightButton {
874 return reloaditem_;
875 }
876
877 - (void) applyLoadingTitle {
878 [[self navigationItem] setTitle:UCLocalize("LOADING")];
879 }
880
881 - (void) layoutRightButton {
882 [[loadingitem_ view] addSubview:indicator_];
883 [[loadingitem_ view] bringSubviewToFront:indicator_];
884 }
885
886 - (void) applyRightButton {
887 if ([self isLoading]) {
888 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
889 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
890
891 [indicator_ startAnimating];
892 [self applyLoadingTitle];
893 } else {
894 [indicator_ stopAnimating];
895 [[self navigationItem] setRightBarButtonItem:[self customButton] animated:YES];
896 }
897 }
898
899 - (void) didStartLoading {
900 // Overridden in subclasses.
901 }
902
903 - (void) _didStartLoading {
904 [self applyRightButton];
905
906 if ([loading_ count] != 1)
907 return;
908
909 if ([self retainsNetworkActivityIndicator])
910 [self.delegate retainNetworkActivityIndicator];
911
912 [self didStartLoading];
913 }
914
915 - (void) didFinishLoading {
916 // Overridden in subclasses.
917 }
918
919 - (void) _didFinishLoading {
920 if ([loading_ count] != 0)
921 return;
922
923 [self applyRightButton];
924 [[self navigationItem] setTitle:title_];
925
926 if ([self retainsNetworkActivityIndicator])
927 [self.delegate releaseNetworkActivityIndicator];
928
929 [self didFinishLoading];
930 }
931
932 - (bool) isLoading {
933 return [loading_ count] != 0;
934 }
935
936 - (id) initWithWidth:(float)width ofClass:(Class)_class {
937 if ((self = [super init]) != nil) {
938 width_ = width;
939 class_ = _class;
940
941 [super setPageColor:nil];
942
943 allowsNavigationAction_ = true;
944
945 loading_ = [NSMutableSet setWithCapacity:5];
946 registered_ = [NSMutableSet setWithCapacity:5];
947 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
948
949 reloaditem_ = [[[UIBarButtonItem alloc]
950 initWithTitle:UCLocalize("RELOAD")
951 style:[self rightButtonStyle]
952 target:self
953 action:@selector(reloadButtonClicked)
954 ] autorelease];
955
956 loadingitem_ = [[[UIBarButtonItem alloc]
957 initWithTitle:(kCFCoreFoundationVersionNumber >= 800 ? @" " : @" ")
958 style:UIBarButtonItemStylePlain
959 target:self
960 action:@selector(customButtonClicked)
961 ] autorelease];
962
963 UIActivityIndicatorViewStyle style;
964 float left;
965 if (kCFCoreFoundationVersionNumber >= 800) {
966 style = UIActivityIndicatorViewStyleGray;
967 left = 7;
968 } else {
969 style = UIActivityIndicatorViewStyleWhite;
970 left = 15;
971 }
972
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];
976
977 [self applyLeftButton];
978 [self applyRightButton];
979 } return self;
980 }
981
982 - (NSString *) applicationNameForUserAgent {
983 return nil;
984 }
985
986 - (void) loadView {
987 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
988
989 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
990 [webview_ setDelegate:self];
991 [self setView:webview_];
992
993 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
994 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
995 else
996 [webview_ setDetectsPhoneNumbers:NO];
997
998 [webview_ setScalesPageToFit:YES];
999
1000 UIWebDocumentView *document([webview_ _documentView]);
1001
1002 // XXX: I think this improves scrolling; the hardcoded-ness sucks
1003 [document setTileSize:CGSizeMake(320, 500)];
1004
1005 WebView *webview([document webView]);
1006 WebPreferences *preferences([webview preferences]);
1007
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];
1013
1014 [preferences setCacheModel:WebCacheModelDocumentBrowser];
1015 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1016
1017 if ([preferences respondsToSelector:@selector(setOfflineWebApplicationCacheEnabled:)])
1018 [preferences setOfflineWebApplicationCacheEnabled:YES];
1019
1020 if (NSString *agent = [self applicationNameForUserAgent])
1021 [webview setApplicationNameForUserAgent:agent];
1022
1023 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
1024 [webview setShouldUpdateWhileOffscreen:NO];
1025
1026 #if LogMessages
1027 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
1028 [document setAllowsMessaging:YES];
1029 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
1030 [webview _setAllowsMessaging:YES];
1031 #endif
1032
1033 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1034 scroller_ = [webview_ _scrollView];
1035
1036 [scroller_ setDirectionalLockEnabled:YES];
1037 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
1038 [scroller_ setDelaysContentTouches:NO];
1039
1040 [scroller_ setCanCancelContentTouches:YES];
1041 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1042 UIScroller *scroller([webview_ _scroller]);
1043 scroller_ = (UIScrollView *) scroller;
1044
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 */
1049
1050 [scroller setThumbDetectionEnabled:NO];
1051
1052 // use NO with UIApplicationUseLegacyEvents(YES)
1053 [scroller setEventMode:YES];
1054
1055 // XXX: this is handled by setBounces, right?
1056 //[scroller setAllowsRubberBanding:YES];
1057 }
1058
1059 [webview_ setOpaque:NO];
1060 [webview_ setBackgroundColor:nil];
1061
1062 [scroller_ setFixedBackgroundPattern:YES];
1063 [scroller_ setBackgroundColor:self.pageColor];
1064 [scroller_ setClipsSubviews:YES];
1065
1066 [scroller_ setBounces:YES];
1067 [scroller_ setScrollingEnabled:YES];
1068 [scroller_ setShowBackgroundShadow:NO];
1069
1070 [self setViewportWidth:width_];
1071
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)];
1077 }
1078
1079 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1080
1081 ready_ = false;
1082 }
1083
1084 - (void) releaseSubviews {
1085 webview_ = nil;
1086 scroller_ = nil;
1087
1088 [self releaseNetworkActivityIndicator];
1089
1090 [super releaseSubviews];
1091 }
1092
1093 - (id) initWithWidth:(float)width {
1094 return [self initWithWidth:width ofClass:[self class]];
1095 }
1096
1097 - (id) init {
1098 return [self initWithWidth:0];
1099 }
1100
1101 - (id) initWithURL:(NSURL *)url {
1102 if ((self = [self init]) != nil) {
1103 [self setURL:url];
1104 } return self;
1105 }
1106
1107 - (id) initWithRequest:(NSURLRequest *)request {
1108 if ((self = [self init]) != nil) {
1109 [self setRequest:request];
1110 } return self;
1111 }
1112
1113 + (void) _lockJavaScript:(WebPreferences *)preferences {
1114 WebThreadLocked lock;
1115 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1116 }
1117
1118 - (void) callFunction:(WebScriptObject *)function {
1119 WebThreadLocked lock;
1120
1121 WebView *webview([[[self webView] _documentView] webView]);
1122 WebPreferences *preferences([webview preferences]);
1123
1124 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
1125 if ([webview respondsToSelector:@selector(_preferencesChanged:)])
1126 [webview _preferencesChanged:preferences];
1127 else
1128 [webview _preferencesChangedNotification:[NSNotification notificationWithName:@"" object:preferences]];
1129
1130 WebFrame *frame([webview mainFrame]);
1131 JSGlobalContextRef context([frame globalContext]);
1132
1133 JSObjectRef object([function JSObject]);
1134 if ($JSObjectCallAsFunction != NULL)
1135 ($JSObjectCallAsFunction)(context, object, NULL, 0, NULL, NULL);
1136
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];
1141 }
1142
1143 - (void) reloadButtonClicked {
1144 [self reloadURLWithCache:NO];
1145 }
1146
1147 - (void) _customButtonClicked {
1148 [self reloadButtonClicked];
1149 }
1150
1151 - (void) customButtonClicked {
1152 #if !AlwaysReload
1153 if (function_ != nil)
1154 [self callFunction:function_];
1155 else
1156 #endif
1157 [self _customButtonClicked];
1158 }
1159
1160 + (float) defaultWidth {
1161 return 980;
1162 }
1163
1164 - (void) setNavigationBarStyle:(NSString *)name {
1165 UIBarStyle style;
1166 if ([name isEqualToString:@"Black"])
1167 style = UIBarStyleBlack;
1168 else
1169 style = UIBarStyleDefault;
1170
1171 [[[self navigationController] navigationBar] setBarStyle:style];
1172 }
1173
1174 - (void) setNavigationBarTintColor:(UIColor *)color {
1175 [[[self navigationController] navigationBar] setTintColor:color];
1176 }
1177
1178 - (void) setBadgeValue:(id)value {
1179 [[[self navigationController] tabBarItem] setBadgeValue:value];
1180 }
1181
1182 - (void) setHidesBackButton:(bool)value {
1183 [[self navigationItem] setHidesBackButton:value];
1184 [self applyLeftButton];
1185 }
1186
1187 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
1188 [self setHidesBackButton:[value boolValue]];
1189 }
1190
1191 - (void) dispatchEvent:(NSString *)event {
1192 [[self webView] dispatchEvent:event];
1193 }
1194
1195 - (bool) hidesNavigationBar {
1196 return hidesNavigationBar_;
1197 }
1198
1199 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1200 if (visible_)
1201 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1202 }
1203
1204 - (void) setHidesNavigationBar:(bool)value {
1205 if (hidesNavigationBar_ != value) {
1206 hidesNavigationBar_ = value;
1207 [self _setHidesNavigationBar:YES animated:YES];
1208 }
1209 }
1210
1211 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1212 [self setHidesNavigationBar:[value boolValue]];
1213 }
1214
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.
1222 } else return;
1223 }
1224
1225 - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1226 [self setScrollAlwaysBounceVertical:[value boolValue]];
1227 }
1228
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];
1236 } else return;
1237 }
1238
1239 - (void) setScrollIndicatorStyleWithName:(NSString *)style {
1240 UIScrollViewIndicatorStyle value;
1241
1242 if (false);
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;
1249 else return;
1250
1251 [self setScrollIndicatorStyle:value];
1252 }
1253
1254 - (void) viewWillAppear:(BOOL)animated {
1255 visible_ = true;
1256
1257 if ([self hidesNavigationBar])
1258 [self _setHidesNavigationBar:YES animated:animated];
1259
1260 // XXX: why isn't this evern called automatically?
1261 [[self webView] setNeedsLayout];
1262
1263 [self dispatchEvent:@"CydiaViewWillAppear"];
1264 [super viewWillAppear:animated];
1265 }
1266
1267 - (void) viewDidAppear:(BOOL)animated {
1268 [super viewDidAppear:animated];
1269 [self dispatchEvent:@"CydiaViewDidAppear"];
1270 }
1271
1272 - (void) viewWillDisappear:(BOOL)animated {
1273 [self dispatchEvent:@"CydiaViewWillDisappear"];
1274 [super viewWillDisappear:animated];
1275
1276 if ([self hidesNavigationBar])
1277 [self _setHidesNavigationBar:NO animated:animated];
1278
1279 visible_ = false;
1280 }
1281
1282 - (void) viewDidDisappear:(BOOL)animated {
1283 [super viewDidDisappear:animated];
1284 [self dispatchEvent:@"CydiaViewDidDisappear"];
1285 }
1286
1287 - (void) updateHeights:(NSTimer *)timer {
1288 for (WebFrame *frame in (id) registered_)
1289 [frame cydia$updateHeight];
1290 }
1291
1292 - (void) registerFrame:(WebFrame *)frame {
1293 [registered_ addObject:frame];
1294
1295 if (timer_ == nil)
1296 timer_ = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateHeights:) userInfo:nil repeats:YES];
1297 }
1298
1299 @end