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