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