]> git.saurik.com Git - cydia.git/blob - CyteKit/WebViewController.mm
4bd05b950cc4ad9e3620400b402db915ea7b15b9
[cydia.git] / CyteKit / WebViewController.mm
1 #include "CyteKit/UCPlatform.h"
2 #include "CyteKit/WebViewController.h"
3
4 #include "CyteKit/MFMailComposeViewController-MailToURL.h"
5
6 #include "iPhonePrivate.h"
7
8 #include "CyteKit/Localize.h"
9 #include "CyteKit/WebViewController.h"
10 #include "CyteKit/PerlCompatibleRegEx.hpp"
11 #include "CyteKit/WebThreadLocked.hpp"
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 <WebKit/WebKitErrors.h>
20 #include <WebKit/WebPreferences.h>
21
22 #include <WebKit/DOMCSSPrimitiveValue.h>
23 #include <WebKit/DOMCSSStyleDeclaration.h>
24 #include <WebKit/DOMDocument.h>
25 #include <WebKit/DOMHTMLBodyElement.h>
26 #include <WebKit/DOMRGBColor.h>
27
28 #include <dlfcn.h>
29 #include <objc/runtime.h>
30
31 #define ForSaurik 0
32 #define DefaultTimeout_ 120.0
33
34 #define ShowInternals 0
35 #define LogBrowser 0
36 #define LogMessages 0
37
38 #define lprintf(args...) fprintf(stderr, args)
39
40 // XXX: centralize these special class things to some file or mechanism?
41 static Class $MFMailComposeViewController;
42
43 float CYScrollViewDecelerationRateNormal;
44
45 @interface WebView (Apple)
46 - (void) _setLayoutInterval:(float)interval;
47 - (void) _setAllowsMessaging:(BOOL)allows;
48 @end
49
50 @interface WebPreferences (Apple)
51 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
52 - (void) _setLayoutInterval:(NSInteger)interval;
53 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
54 @end
55
56 /* Indirect Delegate {{{ */
57 @interface IndirectDelegate : NSObject {
58 _transient volatile id delegate_;
59 }
60
61 - (void) setDelegate:(id)delegate;
62 - (id) initWithDelegate:(id)delegate;
63 @end
64
65 @implementation IndirectDelegate
66
67 - (void) setDelegate:(id)delegate {
68 delegate_ = delegate;
69 }
70
71 - (id) initWithDelegate:(id)delegate {
72 delegate_ = delegate;
73 return self;
74 }
75
76 - (IMP) methodForSelector:(SEL)sel {
77 if (IMP method = [super methodForSelector:sel])
78 return method;
79 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
80 return NULL;
81 }
82
83 - (BOOL) respondsToSelector:(SEL)sel {
84 if ([super respondsToSelector:sel])
85 return YES;
86
87 // XXX: WebThreadCreateNSInvocation returns nil
88
89 #if ShowInternals
90 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
91 #endif
92
93 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
94 }
95
96 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
97 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
98 return method;
99
100 #if ShowInternals
101 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
102 #endif
103
104 if (delegate_ != nil)
105 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
106 return sig;
107
108 // XXX: I fucking hate Apple so very very bad
109 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
110 }
111
112 - (void) forwardInvocation:(NSInvocation *)inv {
113 SEL sel = [inv selector];
114 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
115 [inv invokeWithTarget:delegate_];
116 }
117
118 @end
119 /* }}} */
120
121 @implementation CyteWebViewController
122
123 #if ShowInternals
124 #include "CyteKit/UCInternal.h"
125 #endif
126
127 + (void) _initialize {
128 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
129
130 dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY);
131 $MFMailComposeViewController = objc_getClass("MFMailComposeViewController");
132
133 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
134 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
135 else // XXX: this actually might be fast on some older systems: we should look into this
136 CYScrollViewDecelerationRateNormal = 0.998;
137 }
138
139 - (void) dealloc {
140 #if LogBrowser
141 NSLog(@"[CyteWebViewController dealloc]");
142 #endif
143
144 if ([loading_ count] != 0)
145 [delegate_ releaseNetworkActivityIndicator];
146
147 [super dealloc];
148 }
149
150 - (NSURL *) URLWithURL:(NSURL *)url {
151 return url;
152 }
153
154 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
155 return [NSURLRequest
156 requestWithURL:[self URLWithURL:url]
157 cachePolicy:policy
158 timeoutInterval:DefaultTimeout_
159 ];
160 }
161
162 - (void) setURL:(NSURL *)url {
163 _assert(request_ == nil);
164 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
165 }
166
167 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
168 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
169 }
170
171 - (void) loadURL:(NSURL *)url {
172 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
173 }
174
175 - (void) loadRequest:(NSURLRequest *)request {
176 #if LogBrowser
177 NSLog(@"loadRequest:%@", request);
178 #endif
179
180 error_ = false;
181 ready_ = true;
182
183 WebThreadLocked lock;
184 [webview_ loadRequest:request];
185 }
186
187 - (void) reloadURLWithCache:(BOOL)cache {
188 if (request_ == nil)
189 return;
190
191 NSMutableURLRequest *request([request_ mutableCopy]);
192 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
193
194 request_ = request;
195
196 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
197 [self loadRequest:request_];
198 else {
199 UIAlertView *alert = [[[UIAlertView alloc]
200 initWithTitle:UCLocalize("RESUBMIT_FORM")
201 message:nil
202 delegate:self
203 cancelButtonTitle:UCLocalize("CANCEL")
204 otherButtonTitles:
205 UCLocalize("SUBMIT"),
206 nil
207 ] autorelease];
208
209 [alert setContext:@"submit"];
210 [alert show];
211 }
212 }
213
214 - (void) reloadURL {
215 [self reloadURLWithCache:YES];
216 }
217
218 - (void) reloadData {
219 [super reloadData];
220
221 if (ready_)
222 [self dispatchEvent:@"CydiaReloadData"];
223 else
224 [self reloadURLWithCache:YES];
225 }
226
227 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
228 custom_ = button;
229 style_ = style;
230 function_ = function;
231
232 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
233 }
234
235 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
236 custom_ = button;
237 style_ = style;
238 function_ = function;
239
240 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
241 }
242
243 - (void) removeButton {
244 custom_ = [NSNull null];
245 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
246 }
247
248 - (void) scrollToBottomAnimated:(NSNumber *)animated {
249 CGSize size([scroller_ contentSize]);
250 CGPoint offset([scroller_ contentOffset]);
251 CGRect frame([scroller_ frame]);
252
253 if (size.height - offset.y < frame.size.height + 20.f) {
254 CGRect rect = {{0, size.height-1}, {size.width, 1}};
255 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
256 }
257 }
258
259 - (void) _setViewportWidth {
260 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
261 }
262
263 - (void) setViewportWidth:(float)width {
264 width_ = width != 0 ? width : [[self class] defaultWidth];
265 [self _setViewportWidth];
266 }
267
268 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
269 [self setViewportWidth:[width floatValue]];
270 }
271
272 - (void) setViewportWidthOnMainThread:(float)width {
273 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
274 }
275
276 - (void) webViewUpdateViewSettings:(UIWebView *)view {
277 [self _setViewportWidth];
278 }
279
280 - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
281 [self dismissModalViewControllerAnimated:YES];
282 }
283
284 - (void) _openMailToURL:(NSURL *)url {
285 if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) {
286 MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]);
287 [controller setMailComposeDelegate:self];
288
289 [controller setMailToURL:url];
290
291 [self presentModalViewController:controller animated:YES];
292 return;
293 }
294
295 UIApplication *app([UIApplication sharedApplication]);
296 if ([app respondsToSelector:@selector(openURL:asPanel:)])
297 [app openURL:url asPanel:YES];
298 else
299 [app openURL:url];
300 }
301
302 - (bool) _allowJavaScriptPanel {
303 return true;
304 }
305
306 - (bool) allowsNavigationAction {
307 return allowsNavigationAction_;
308 }
309
310 - (void) setAllowsNavigationAction:(bool)value {
311 allowsNavigationAction_ = value;
312 }
313
314 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
315 [self setAllowsNavigationAction:[value boolValue]];
316 }
317
318 - (void) popViewControllerWithNumber:(NSNumber *)value {
319 UINavigationController *navigation([self navigationController]);
320 if ([navigation topViewController] == self)
321 [navigation popViewControllerAnimated:[value boolValue]];
322 }
323
324 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
325 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
326 [self _didFinishLoading];
327
328 if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
329 return;
330
331 if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) {
332 request_ = stage2_;
333 stage1_ = nil;
334 stage2_ = nil;
335 return;
336 }
337
338 if ([frame parentFrame] == nil) {
339 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
340 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
341 [[error localizedDescription] stringByAddingPercentEscapes]
342 ]]];
343
344 error_ = true;
345 }
346 }
347
348 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
349 NSURL *url([request URL]);
350
351 // XXX: filter to internal usage?
352 CyteViewController *page([delegate_ pageForURL:url forExternal:NO]);
353
354 if (page == nil) {
355 CyteWebViewController *browser([[[class_ alloc] init] autorelease]);
356 [browser loadRequest:request];
357 page = browser;
358 }
359
360 [page setDelegate:delegate_];
361
362 if (!pop) {
363 [[self navigationItem] setTitle:title_];
364
365 [[self navigationController] pushViewController:page animated:YES];
366 } else {
367 UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]);
368
369 [navigation setDelegate:delegate_];
370
371 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
372 initWithTitle:UCLocalize("CLOSE")
373 style:UIBarButtonItemStylePlain
374 target:page
375 action:@selector(close)
376 ] autorelease]];
377
378 [delegate_ unloadData];
379
380 [[self navigationController] presentModalViewController:navigation animated:YES];
381 }
382 }
383
384 // CyteWebViewDelegate {{{
385 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
386 #if LogMessages
387 static Pcre irritating("^(?"
388 ":" "The page at .* displayed insecure content from .*\\."
389 "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\."
390 ")\\n$");
391
392 if (NSString *data = [message objectForKey:@"message"])
393 if (irritating(data))
394 return;
395
396 NSLog(@"addMessageToConsole:%@", message);
397 #endif
398 }
399
400 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
401 #if LogBrowser
402 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
403 #endif
404
405 if ([frame parentFrame] == nil) {
406 if (!error_) {
407 NSURL *url(request == nil ? nil : [request URL]);
408
409 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
410 if (url != nil)
411 [self pushRequest:request asPop:NO];
412 [listener ignore];
413 }
414 }
415 }
416 }
417
418 - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
419 if ([frame parentFrame] == nil)
420 if (decision == CYWebPolicyDecisionUse)
421 if (!error_) {
422 stage1_ = request_;
423 request_ = request;
424 }
425 }
426
427 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
428 #if LogBrowser
429 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
430 #endif
431
432 NSURL *url([request URL]);
433 if (url == nil)
434 return;
435
436 if ([frame isEqualToString:@"_open"])
437 [delegate_ openURL:url];
438 else {
439 NSString *scheme([[url scheme] lowercaseString]);
440 if ([scheme isEqualToString:@"mailto"])
441 [self _openMailToURL:url];
442 else
443 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
444 }
445
446 [listener ignore];
447 }
448
449 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
450 }
451
452 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
453 #if LogBrowser
454 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
455 #endif
456
457 [self _didFailWithError:error forFrame:frame];
458 }
459
460 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
461 #if LogBrowser
462 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
463 #endif
464
465 [self _didFailWithError:error forFrame:frame];
466 }
467
468 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
469 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
470
471 if ([frame parentFrame] == nil) {
472 stage1_ = nil;
473 stage2_ = nil;
474
475 if (DOMDocument *document = [frame DOMDocument])
476 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
477 for (DOMHTMLBodyElement *body in (id) bodies) {
478 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
479
480 UIColor *uic(nil);
481
482 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
483 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
484 DOMRGBColor *rgb([color getRGBColorValue]);
485
486 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
487 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
488 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
489 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
490
491 if (red == 0xc7 && green == 0xce && blue == 0xd5)
492 uic = [UIColor pinStripeColor];
493 else if (alpha != 0)
494 uic = [UIColor
495 colorWithRed:(red / 255)
496 green:(green / 255)
497 blue:(blue / 255)
498 alpha:alpha
499 ];
500 }
501 }
502
503 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
504 break;
505 }
506 }
507
508 [self _didFinishLoading];
509 }
510
511 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
512 if ([frame parentFrame] != nil)
513 return;
514
515 title_ = title;
516
517 [[self navigationItem] setTitle:title_];
518 }
519
520 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
521 #if LogBrowser
522 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
523 #endif
524
525 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
526
527 if ([frame parentFrame] == nil) {
528 title_ = nil;
529 custom_ = nil;
530 style_ = nil;
531 function_ = nil;
532
533 allowsNavigationAction_ = true;
534
535 stage2_ = stage1_;
536 stage1_ = nil;
537
538 [self setHidesNavigationBar:NO];
539
540 // XXX: do we still need to do this?
541 [[self navigationItem] setTitle:nil];
542 }
543
544 [self _didStartLoading];
545 }
546
547 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
548 #if LogBrowser
549 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
550 #endif
551
552 return request;
553 }
554
555 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
556 return [self _allowJavaScriptPanel];
557 }
558
559 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
560 return [self _allowJavaScriptPanel];
561 }
562
563 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
564 return [self _allowJavaScriptPanel];
565 }
566
567 - (void) webViewClose:(WebView *)view {
568 [self close];
569 }
570 // }}}
571
572 - (void) close {
573 [[self navigationController] dismissModalViewControllerAnimated:YES];
574 }
575
576 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
577 NSString *context([alert context]);
578
579 if ([context isEqualToString:@"sensitive"]) {
580 switch (button) {
581 case 1:
582 sensitive_ = [NSNumber numberWithBool:YES];
583 break;
584
585 case 2:
586 sensitive_ = [NSNumber numberWithBool:NO];
587 break;
588 }
589
590 [alert dismissWithClickedButtonIndex:-1 animated:YES];
591 } else if ([context isEqualToString:@"challenge"]) {
592 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
593
594 switch (button) {
595 case 1: {
596 NSString *username([[alert textFieldAtIndex:0] text]);
597 NSString *password([[alert textFieldAtIndex:1] text]);
598
599 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
600
601 [sender useCredential:credential forAuthenticationChallenge:challenge_];
602 } break;
603
604 case 2:
605 [sender cancelAuthenticationChallenge:challenge_];
606 break;
607
608 _nodefault
609 }
610
611 challenge_ = nil;
612
613 [alert dismissWithClickedButtonIndex:-1 animated:YES];
614 } else if ([context isEqualToString:@"submit"]) {
615 if (button == [alert cancelButtonIndex]) {
616 } else if (button == [alert firstOtherButtonIndex]) {
617 if (request_ != nil) {
618 WebThreadLocked lock;
619 [webview_ loadRequest:request_];
620 }
621 }
622
623 [alert dismissWithClickedButtonIndex:-1 animated:YES];
624 }
625 }
626
627 - (UIBarButtonItemStyle) rightButtonStyle {
628 if (style_ == nil) normal:
629 return UIBarButtonItemStylePlain;
630 else if ([style_ isEqualToString:@"Normal"])
631 return UIBarButtonItemStylePlain;
632 else if ([style_ isEqualToString:@"Highlighted"])
633 return UIBarButtonItemStyleDone;
634 else goto normal;
635 }
636
637 - (UIBarButtonItem *) customButton {
638 if (custom_ == nil)
639 return nil;
640 else if (custom_ == [NSNull null])
641 return (UIBarButtonItem *) [NSNull null];
642
643 return [[[UIBarButtonItem alloc]
644 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
645 style:[self rightButtonStyle]
646 target:self
647 action:@selector(customButtonClicked)
648 ] autorelease];
649 }
650
651 - (UIBarButtonItem *) leftButton {
652 UINavigationItem *item([self navigationItem]);
653 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
654 return nil;
655
656 if (UINavigationController *navigation = [self navigationController])
657 if ([[navigation parentViewController] modalViewController] == navigation)
658 return [[[UIBarButtonItem alloc]
659 initWithTitle:UCLocalize("CLOSE")
660 style:UIBarButtonItemStylePlain
661 target:self
662 action:@selector(close)
663 ] autorelease];
664
665 return nil;
666 }
667
668 - (void) applyLeftButton {
669 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
670 }
671
672 - (UIBarButtonItem *) rightButton {
673 return reloaditem_;
674 }
675
676 - (void) applyLoadingTitle {
677 [[self navigationItem] setTitle:UCLocalize("LOADING")];
678 }
679
680 - (void) layoutRightButton {
681 [[loadingitem_ view] addSubview:indicator_];
682 [[loadingitem_ view] bringSubviewToFront:indicator_];
683 }
684
685 - (void) applyRightButton {
686 if ([self isLoading]) {
687 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
688 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
689
690 [indicator_ startAnimating];
691 [self applyLoadingTitle];
692 } else {
693 [indicator_ stopAnimating];
694
695 UIBarButtonItem *button([self customButton]);
696 if (button == nil)
697 button = [self rightButton];
698 else if (button == (UIBarButtonItem *) [NSNull null])
699 button = nil;
700
701 [[self navigationItem] setRightBarButtonItem:button];
702 }
703 }
704
705 - (void) didStartLoading {
706 // Overridden in subclasses.
707 }
708
709 - (void) _didStartLoading {
710 [self applyRightButton];
711
712 if ([loading_ count] != 1)
713 return;
714
715 [delegate_ retainNetworkActivityIndicator];
716 [self didStartLoading];
717 }
718
719 - (void) didFinishLoading {
720 // Overridden in subclasses.
721 }
722
723 - (void) _didFinishLoading {
724 if ([loading_ count] != 0)
725 return;
726
727 [self applyRightButton];
728 [[self navigationItem] setTitle:title_];
729
730 [delegate_ releaseNetworkActivityIndicator];
731 [self didFinishLoading];
732 }
733
734 - (bool) isLoading {
735 return [loading_ count] != 0;
736 }
737
738 - (id) initWithWidth:(float)width ofClass:(Class)_class {
739 if ((self = [super init]) != nil) {
740 allowsNavigationAction_ = true;
741
742 class_ = _class;
743 loading_ = [NSMutableSet setWithCapacity:5];
744
745 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
746
747 CGRect bounds([[self view] bounds]);
748
749 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
750 [webview_ setDelegate:self];
751 [self setView:webview_];
752
753 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
754 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
755 else
756 [webview_ setDetectsPhoneNumbers:NO];
757
758 [webview_ setScalesPageToFit:YES];
759
760 UIWebDocumentView *document([webview_ _documentView]);
761
762 // XXX: I think this improves scrolling; the hardcoded-ness sucks
763 [document setTileSize:CGSizeMake(320, 500)];
764
765 [document setBackgroundColor:[UIColor clearColor]];
766
767 // XXX: this is terribly (too?) expensive
768 [document setDrawsBackground:NO];
769
770 WebView *webview([document webView]);
771 WebPreferences *preferences([webview preferences]);
772
773 // XXX: I have no clue if I actually /want/ this modification
774 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
775 [webview _setLayoutInterval:0];
776 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
777 [preferences _setLayoutInterval:0];
778
779 [preferences setCacheModel:WebCacheModelDocumentBrowser];
780 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
781 [preferences setOfflineWebApplicationCacheEnabled:YES];
782
783 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
784 [webview setShouldUpdateWhileOffscreen:NO];
785
786 #if LogMessages
787 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
788 [document setAllowsMessaging:YES];
789 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
790 [webview _setAllowsMessaging:YES];
791 #endif
792
793 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
794 scroller_ = [webview_ _scrollView];
795
796 [scroller_ setDirectionalLockEnabled:YES];
797 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
798 [scroller_ setDelaysContentTouches:NO];
799
800 [scroller_ setCanCancelContentTouches:YES];
801 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
802 UIScroller *scroller([webview_ _scroller]);
803 scroller_ = (UIScrollView *) scroller;
804
805 [scroller setDirectionalScrolling:YES];
806 // XXX: we might be better off /not/ setting this on older systems
807 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
808 [scroller setScrollHysteresis:0]; /* 8 */
809
810 [scroller setThumbDetectionEnabled:NO];
811
812 // use NO with UIApplicationUseLegacyEvents(YES)
813 [scroller setEventMode:YES];
814
815 // XXX: this is handled by setBounces, right?
816 //[scroller setAllowsRubberBanding:YES];
817 }
818
819 [scroller_ setFixedBackgroundPattern:YES];
820 [scroller_ setBackgroundColor:[UIColor clearColor]];
821 [scroller_ setClipsSubviews:YES];
822
823 [scroller_ setBounces:YES];
824 [scroller_ setScrollingEnabled:YES];
825 [scroller_ setShowBackgroundShadow:NO];
826
827 [self setViewportWidth:width];
828
829 reloaditem_ = [[[UIBarButtonItem alloc]
830 initWithTitle:UCLocalize("RELOAD")
831 style:[self rightButtonStyle]
832 target:self
833 action:@selector(reloadButtonClicked)
834 ] autorelease];
835
836 loadingitem_ = [[[UIBarButtonItem alloc]
837 initWithTitle:@" "
838 style:UIBarButtonItemStylePlain
839 target:self
840 action:@selector(reloadButtonClicked)
841 ] autorelease];
842
843 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite] autorelease];
844 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
845
846 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
847 [webview_ insertSubview:table atIndex:0];
848
849 [self applyLeftButton];
850 [self applyRightButton];
851
852 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
853 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
854 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
855 } return self;
856 }
857
858 - (id) initWithWidth:(float)width {
859 return [self initWithWidth:width ofClass:[self class]];
860 }
861
862 - (id) init {
863 return [self initWithWidth:0];
864 }
865
866 - (id) initWithURL:(NSURL *)url {
867 if ((self = [self init]) != nil) {
868 [self setURL:url];
869 } return self;
870 }
871
872 - (void) callFunction:(WebScriptObject *)function {
873 WebThreadLocked lock;
874
875 WebView *webview([[webview_ _documentView] webView]);
876 WebFrame *frame([webview mainFrame]);
877
878 JSGlobalContextRef context([frame globalContext]);
879 JSObjectRef object([function JSObject]);
880 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
881 }
882
883 - (void) reloadButtonClicked {
884 [self reloadURLWithCache:YES];
885 }
886
887 - (void) _customButtonClicked {
888 [self reloadButtonClicked];
889 }
890
891 - (void) customButtonClicked {
892 #if !AlwaysReload
893 if (function_ != nil)
894 [self callFunction:function_];
895 else
896 #endif
897 [self _customButtonClicked];
898 }
899
900 + (float) defaultWidth {
901 return 980;
902 }
903
904 - (void) setNavigationBarStyle:(NSString *)name {
905 UIBarStyle style;
906 if ([name isEqualToString:@"Black"])
907 style = UIBarStyleBlack;
908 else
909 style = UIBarStyleDefault;
910
911 [[[self navigationController] navigationBar] setBarStyle:style];
912 }
913
914 - (void) setNavigationBarTintColor:(UIColor *)color {
915 [[[self navigationController] navigationBar] setTintColor:color];
916 }
917
918 - (void) setBadgeValue:(id)value {
919 [[[self navigationController] tabBarItem] setBadgeValue:value];
920 }
921
922 - (void) setHidesBackButton:(bool)value {
923 [[self navigationItem] setHidesBackButton:value];
924 [self applyLeftButton];
925 }
926
927 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
928 [self setHidesBackButton:[value boolValue]];
929 }
930
931 - (void) dispatchEvent:(NSString *)event {
932 [(CyteWebView *) webview_ dispatchEvent:event];
933 }
934
935 - (bool) hidesNavigationBar {
936 return hidesNavigationBar_;
937 }
938
939 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
940 if (visible_)
941 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
942 }
943
944 - (void) setHidesNavigationBar:(bool)value {
945 if (hidesNavigationBar_ != value) {
946 hidesNavigationBar_ = value;
947 [self _setHidesNavigationBar:YES animated:YES];
948 }
949 }
950
951 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
952 [self setHidesNavigationBar:[value boolValue]];
953 }
954
955 - (void) viewWillAppear:(BOOL)animated {
956 visible_ = true;
957
958 if ([self hidesNavigationBar])
959 [self _setHidesNavigationBar:YES animated:animated];
960
961 [self dispatchEvent:@"CydiaViewWillAppear"];
962 [super viewWillAppear:animated];
963 }
964
965 - (void) viewDidAppear:(BOOL)animated {
966 [super viewDidAppear:animated];
967 [self dispatchEvent:@"CydiaViewDidAppear"];
968 }
969
970 - (void) viewWillDisappear:(BOOL)animated {
971 [self dispatchEvent:@"CydiaViewWillDisappear"];
972 [super viewWillDisappear:animated];
973
974 if ([self hidesNavigationBar])
975 [self _setHidesNavigationBar:NO animated:animated];
976
977 visible_ = false;
978 }
979
980 - (void) viewDidDisappear:(BOOL)animated {
981 [super viewDidDisappear:animated];
982 [self dispatchEvent:@"CydiaViewDidDisappear"];
983 }
984
985 @end