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