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