]> git.saurik.com Git - cydia.git/blob - UICaboodle/BrowserView.mm
9bfeb7901737251ab1f09f55adfef7c4c867efab
[cydia.git] / UICaboodle / BrowserView.mm
1 #include <UICaboodle/BrowserView.h>
2 #include <UICaboodle/UCLocalize.h>
3
4 #import <QuartzCore/CALayer.h>
5 // XXX: fix the minimum requirement
6 extern NSString * const kCAFilterNearest;
7
8 #include <WebCore/WebCoreThread.h>
9 #include <WebKit/WebPreferences-WebPrivate.h>
10
11 #include "substrate.h"
12
13 #define ForSaurik 0
14
15 static bool Wildcat_;
16
17 static CFArrayRef (*$GSSystemCopyCapability)(CFStringRef);
18 static CFArrayRef (*$GSSystemGetCapability)(CFStringRef);
19 static Class $UIFormAssistant;
20 static Class $UIWebBrowserView;
21
22 @interface NSString (UIKit)
23 - (NSString *) stringByAddingPercentEscapes;
24 @end
25
26 /* Indirect Delegate {{{ */
27 @interface IndirectDelegate : NSObject {
28 _transient volatile id delegate_;
29 }
30
31 - (void) setDelegate:(id)delegate;
32 - (id) initWithDelegate:(id)delegate;
33 @end
34
35 @implementation IndirectDelegate
36
37 - (void) setDelegate:(id)delegate {
38 delegate_ = delegate;
39 }
40
41 - (id) initWithDelegate:(id)delegate {
42 delegate_ = delegate;
43 return self;
44 }
45
46 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
47 if (delegate_ != nil)
48 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
49 }
50
51 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
52 if (delegate_ != nil)
53 return [delegate_ webView:sender didCommitLoadForFrame:frame];
54 }
55
56 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
57 if (delegate_ != nil)
58 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
59 }
60
61 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
62 if (delegate_ != nil)
63 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
64 }
65
66 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
67 if (delegate_ != nil)
68 return [delegate_ webView:sender didFinishLoadForFrame:frame];
69 }
70
71 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
72 if (delegate_ != nil)
73 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
74 }
75
76 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
77 if (delegate_ != nil)
78 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
79 }
80
81 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
82 if (delegate_ != nil)
83 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
84 }
85
86 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
87 if (delegate_ != nil)
88 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
89 return nil;
90 }
91
92 - (IMP) methodForSelector:(SEL)sel {
93 if (IMP method = [super methodForSelector:sel])
94 return method;
95 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
96 return NULL;
97 }
98
99 - (BOOL) respondsToSelector:(SEL)sel {
100 if ([super respondsToSelector:sel])
101 return YES;
102 // XXX: WebThreadCreateNSInvocation returns nil
103 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
104 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
105 }
106
107 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
108 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
109 return method;
110 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
111 if (delegate_ != nil)
112 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
113 return sig;
114 // XXX: I fucking hate Apple so very very bad
115 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
116 }
117
118 - (void) forwardInvocation:(NSInvocation *)inv {
119 SEL sel = [inv selector];
120 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
121 [inv invokeWithTarget:delegate_];
122 }
123
124 @end
125 /* }}} */
126
127 @interface WebView (UICaboodle)
128 - (void) setScriptDebugDelegate:(id)delegate;
129 - (void) _setFormDelegate:(id)delegate;
130 - (void) _setUIKitDelegate:(id)delegate;
131 - (void) setWebMailDelegate:(id)delegate;
132 - (void) _setLayoutInterval:(float)interval;
133 @end
134
135 @implementation WebScriptObject (UICaboodle)
136
137 - (unsigned) count {
138 id length([self valueForKey:@"length"]);
139 if ([length respondsToSelector:@selector(intValue)])
140 return [length intValue];
141 else
142 return 0;
143 }
144
145 - (id) objectAtIndex:(unsigned)index {
146 return [self webScriptValueAtIndex:index];
147 }
148
149 @end
150
151 #define ShowInternals 0
152 #define LogBrowser 1
153
154 #define lprintf(args...) fprintf(stderr, args)
155
156 @implementation BrowserView
157
158 #if ShowInternals
159 #include "UICaboodle/UCInternal.h"
160 #endif
161
162 + (void) _initialize {
163 //[WebView enableWebThread];
164
165 WebPreferences *preferences([WebPreferences standardPreferences]);
166 [preferences setCacheModel:WebCacheModelDocumentBrowser];
167 [preferences setOfflineWebApplicationCacheEnabled:YES];
168
169 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
170
171 $GSSystemCopyCapability = reinterpret_cast<CFArrayRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemCopyCapability"));
172 $GSSystemGetCapability = reinterpret_cast<CFArrayRef (*)(CFStringRef)>(dlsym(RTLD_DEFAULT, "GSSystemGetCapability"));
173 $UIFormAssistant = objc_getClass("UIFormAssistant");
174
175 $UIWebBrowserView = objc_getClass("UIWebBrowserView");
176 if ($UIWebBrowserView == nil) {
177 Wildcat_ = false;
178 $UIWebBrowserView = objc_getClass("UIWebDocumentView");
179 } else {
180 Wildcat_ = true;
181 }
182 }
183
184 - (void) dealloc {
185 #if LogBrowser
186 NSLog(@"[BrowserView dealloc]");
187 #endif
188
189 if (challenge_ != nil)
190 [challenge_ release];
191
192 WebThreadLock();
193
194 WebView *webview = [document_ webView];
195 [webview setFrameLoadDelegate:nil];
196 [webview setResourceLoadDelegate:nil];
197 [webview setUIDelegate:nil];
198 [webview setScriptDebugDelegate:nil];
199 [webview setPolicyDelegate:nil];
200
201 /* XXX: these are set by UIWebDocumentView
202 [webview setDownloadDelegate:nil];
203 [webview _setFormDelegate:nil];
204 [webview _setUIKitDelegate:nil];
205 [webview setEditingDelegate:nil];*/
206
207 /* XXX: no one sets this, ever
208 [webview setWebMailDelegate:nil];*/
209
210 [document_ setDelegate:nil];
211 [document_ setGestureDelegate:nil];
212
213 if ([document_ respondsToSelector:@selector(setFormEditingDelegate:)])
214 [document_ setFormEditingDelegate:nil];
215
216 [document_ setInteractionDelegate:nil];
217
218 [indirect_ setDelegate:nil];
219
220 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
221
222 [webview close];
223
224 #if RecycleWebViews
225 [document_ removeFromSuperview];
226 [Documents_ addObject:[document_ autorelease]];
227 #else
228 [document_ release];
229 #endif
230
231 [indirect_ release];
232
233 WebThreadUnlock();
234
235 [scroller_ setDelegate:nil];
236
237 if (button_ != nil)
238 [button_ release];
239 if (style_ != nil)
240 [style_ release];
241 if (function_ != nil)
242 [function_ release];
243 if (finish_ != nil)
244 [finish_ release];
245 if (closer_ != nil)
246 [closer_ release];
247 if (special_ != nil)
248 [special_ release];
249
250 [scroller_ release];
251 [indicator_ release];
252 if (confirm_ != nil)
253 [confirm_ release];
254 if (sensitive_ != nil)
255 [sensitive_ release];
256 if (title_ != nil)
257 [title_ release];
258 [super dealloc];
259 }
260
261 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
262 [self loadRequest:[NSURLRequest
263 requestWithURL:url
264 cachePolicy:policy
265 timeoutInterval:120.0
266 ]];
267 }
268
269 - (void) loadURL:(NSURL *)url {
270 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
271 }
272
273 - (void) loadRequest:(NSURLRequest *)request {
274 pushed_ = true;
275 error_ = false;
276
277 WebThreadLock();
278 [document_ loadRequest:request];
279 WebThreadUnlock();
280 }
281
282 - (void) reloadURL {
283 if (request_ == nil)
284 return;
285
286 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
287 [self loadRequest:request_];
288 else {
289 UIActionSheet *sheet = [[[UIActionSheet alloc]
290 initWithTitle:UCLocalize("RESUBMIT_FORM")
291 buttons:[NSArray arrayWithObjects:UCLocalize("CANCEL"), UCLocalize("SUBMIT"), nil]
292 defaultButtonIndex:0
293 delegate:self
294 context:@"submit"
295 ] autorelease];
296
297 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
298
299 [sheet setNumberOfRows:1];
300 [sheet popupAlertAnimated:YES];
301 }
302 }
303
304 - (WebView *) webView {
305 return [document_ webView];
306 }
307
308 - (UIWebDocumentView *) documentView {
309 return document_;
310 }
311
312 /* XXX: WebThreadLock? */
313 - (void) _fixScroller:(CGRect)bounds {
314 float extra;
315
316 if (!editing_ || $UIFormAssistant == nil)
317 extra = 0;
318 else {
319 UIFormAssistant *assistant([$UIFormAssistant sharedFormAssistant]);
320 CGRect peripheral([assistant peripheralFrame]);
321 #if LogBrowser
322 NSLog(@"per:%f", peripheral.size.height);
323 #endif
324 extra = peripheral.size.height;
325 }
326
327 CGRect subrect([scroller_ frame]);
328 subrect.size.height -= extra;
329
330 if ([scroller_ respondsToSelector:@selector(setScrollerIndicatorSubrect:)])
331 [scroller_ setScrollerIndicatorSubrect:subrect];
332
333 [document_ setValue:[NSValue valueWithSize:NSMakeSize(subrect.size.width, subrect.size.height)] forGestureAttribute:UIGestureAttributeVisibleSize];
334
335 CGSize size(size_);
336 size.height += extra;
337 [scroller_ setContentSize:size];
338
339 if ([scroller_ respondsToSelector:@selector(releaseRubberBandIfNecessary)])
340 [scroller_ releaseRubberBandIfNecessary];
341 }
342
343 - (void) fixScroller {
344 CGRect bounds([document_ documentBounds]);
345 #if TrackResize
346 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
347 #endif
348 [self _fixScroller:bounds];
349 }
350
351 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
352 size_ = frame.size;
353 #if TrackResize
354 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
355 #endif
356 [self _fixScroller:frame];
357 }
358
359 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
360 [self view:sender didSetFrame:frame];
361 }
362
363 - (void) pushPage:(UCViewController *)page {
364 [page setDelegate:delegate_];
365 [[self navigationItem] setTitle:title_];
366 [[self navigationController] pushViewController:page animated:YES];
367 }
368
369 - (void) _pushPage {
370 if (pushed_)
371 return;
372 // WTR: [self autorelease];
373 pushed_ = true;
374 [[self navigationController] pushViewController:self animated:YES];
375 }
376
377 - (void) swapPage:(UCViewController *)page {
378 [page setDelegate:delegate_];
379 if (pushed_) [[self navigationController] popViewControllerAnimated:NO];
380
381 [[self navigationController] pushViewController:page animated:NO];
382 }
383
384 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
385 #if LogBrowser
386 NSLog(@"getSpecial:%@", url);
387 #endif
388
389 if (UCViewController *page = [delegate_ pageForURL:url hasTag:NULL]) {
390 if (swap)
391 [self swapPage:page];
392 else
393 [self pushPage:page];
394
395 return true;
396 } else
397 return false;
398 }
399
400 - (void) formAssistant:(id)sender didBeginEditingFormNode:(id)node {
401 }
402
403 - (void) formAssistant:(id)sender didEndEditingFormNode:(id)node {
404 [self fixScroller];
405 }
406
407 - (void) webViewShow:(WebView *)sender {
408 /* XXX: this is where I cry myself to sleep */
409 }
410
411 - (bool) _allowJavaScriptPanel {
412 return true;
413 }
414
415 - (bool) allowSensitiveRequests {
416 return [self _allowJavaScriptPanel];
417 }
418
419 - (void) _promptForSensitive:(NSMutableArray *)array {
420 NSString *name([array objectAtIndex:0]);
421
422 UIActionSheet *sheet = [[[UIActionSheet alloc]
423 initWithTitle:nil
424 buttons:[NSArray arrayWithObjects:UCLocalize("YES"), UCLocalize("NO"), nil]
425 defaultButtonIndex:0
426 delegate:indirect_
427 context:@"sensitive"
428 ] autorelease];
429
430 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
431
432 NSString *host(@"XXX");
433
434 [sheet setNumberOfRows:1];
435 [sheet setBodyText:[NSString stringWithFormat:@"The website at %@ is requesting your phone's %@. This is almost certainly for product licensing purposes. Will you allow this?", host, name]];
436 [sheet popupAlertAnimated:YES];
437
438 NSRunLoop *loop([NSRunLoop currentRunLoop]);
439 NSDate *future([NSDate distantFuture]);
440
441 while (sensitive_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
442
443 NSNumber *sensitive([sensitive_ autorelease]);
444 sensitive_ = nil;
445
446 [self autorelease];
447 [array replaceObjectAtIndex:0 withObject:sensitive];
448 }
449
450 - (bool) promptForSensitive:(NSString *)name {
451 if (![self allowSensitiveRequests])
452 return false;
453
454 NSMutableArray *array([NSMutableArray arrayWithCapacity:1]);
455 [array addObject:name];
456
457 [self performSelectorOnMainThread:@selector(_promptForSensitive:) withObject:array waitUntilDone:YES];
458 return [[array lastObject] boolValue];
459 }
460
461 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
462 if (![self _allowJavaScriptPanel])
463 return;
464 [self retain];
465
466 UIActionSheet *sheet = [[[UIActionSheet alloc]
467 initWithTitle:nil
468 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), nil]
469 defaultButtonIndex:0
470 delegate:self
471 context:@"alert"
472 ] autorelease];
473
474 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
475
476 [sheet setBodyText:message];
477 [sheet popupAlertAnimated:YES];
478 }
479
480 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
481 if (![self _allowJavaScriptPanel])
482 return NO;
483 [self retain];
484
485 UIActionSheet *sheet = [[[UIActionSheet alloc]
486 initWithTitle:nil
487 buttons:[NSArray arrayWithObjects:UCLocalize("OK"), UCLocalize("CANCEL"), nil]
488 defaultButtonIndex:0
489 delegate:indirect_
490 context:@"confirm"
491 ] autorelease];
492
493 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
494
495 [sheet setNumberOfRows:1];
496 [sheet setBodyText:message];
497 [sheet popupAlertAnimated:YES];
498
499 NSRunLoop *loop([NSRunLoop currentRunLoop]);
500 NSDate *future([NSDate distantFuture]);
501
502 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
503
504 NSNumber *confirm([confirm_ autorelease]);
505 confirm_ = nil;
506
507 [self autorelease];
508 return [confirm boolValue];
509 }
510
511 - (void) setAutoPopup:(BOOL)popup {
512 popup_ = popup;
513 }
514
515 - (void) setSpecial:(id)function {
516 if (special_ != nil)
517 [special_ autorelease];
518 special_ = function == nil ? nil : [function retain];
519 }
520
521 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
522 if (button_ != nil)
523 [button_ autorelease];
524 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
525
526 if (style_ != nil)
527 [style_ autorelease];
528 style_ = style == nil ? nil : [style retain];
529
530 if (function_ != nil)
531 [function_ autorelease];
532 function_ = function == nil ? nil : [function retain];
533
534 [self reloadButtons];
535 }
536
537 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
538 if (button_ != nil)
539 [button_ autorelease];
540 button_ = button == nil ? nil : [button retain];
541
542 if (style_ != nil)
543 [style_ autorelease];
544 style_ = style == nil ? nil : [style retain];
545
546 if (function_ != nil)
547 [function_ autorelease];
548 function_ = function == nil ? nil : [function retain];
549
550 [self reloadButtons];
551 }
552
553 - (void) setFinishHook:(id)function {
554 if (finish_ != nil)
555 [finish_ autorelease];
556 finish_ = function == nil ? nil : [function retain];
557 }
558
559 - (void) setPopupHook:(id)function {
560 if (closer_ != nil)
561 [closer_ autorelease];
562 closer_ = function == nil ? nil : [function retain];
563 }
564
565 - (void) _openMailToURL:(NSURL *)url {
566 [UIApp openURL:url];// asPanel:YES];
567 }
568
569 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
570 editing_ = true;
571 }
572
573 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
574 [self fixScroller];
575 }
576
577 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
578 editing_ = false;
579 [self fixScroller];
580 }
581
582 - (void) webViewClose:(WebView *)sender {
583 [self close];
584 }
585
586 - (void) close {
587 [[self navigationController] dismissModalViewControllerAnimated:YES];
588 }
589
590 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
591 }
592
593 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
594 NSLog(@"err:%@", error);
595 }
596
597 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
598 #if LogBrowser
599 NSLog(@"nwa:%@", name);
600 #endif
601
602 if (NSURL *url = [request URL]) {
603 if (name == nil) unknown: {
604 if (![self getSpecial:url swap:NO]) {
605 NSString *scheme([[url scheme] lowercaseString]);
606 if ([scheme isEqualToString:@"mailto"])
607 [self _openMailToURL:url];
608 else goto use;
609 }
610 } else if ([name isEqualToString:@"_open"])
611 [delegate_ openURL:url];
612 else if ([name isEqualToString:@"_popup"]) {
613 NSString *scheme([[url scheme] lowercaseString]);
614 if ([scheme isEqualToString:@"mailto"])
615 [self _openMailToURL:url];
616 else {
617 UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]);
618 [navigation setHook:indirect_];
619
620 UCViewController *page([delegate_ pageForURL:url hasTag:NULL]);
621 if (page == nil) {
622 /* XXX: call createWebViewWithRequest instead? */
623
624 BrowserView *browser([[[class_ alloc] init] autorelease]);
625 [browser loadURL:url];
626 page = browser;
627 }
628
629 [navigation setDelegate:delegate_];
630 [page setDelegate:delegate_];
631
632 [navigation setViewControllers:[NSArray arrayWithObject:page]];
633 UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]
634 initWithTitle:UCLocalize("CLOSE")
635 style:UIBarButtonItemStylePlain
636 target:page
637 action:@selector(close)
638 ];
639 [[page navigationItem] setLeftBarButtonItem:closeItem];
640 [closeItem release];
641
642 [[self navigationController] presentModalViewController:navigation animated:YES];
643 }
644 } else goto unknown;
645
646 [listener ignore];
647 } else use:
648 [listener use];
649 }
650
651 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
652 if ([WebView canShowMIMEType:type])
653 [listener use];
654 else {
655 // XXX: handle more mime types!
656 [listener ignore];
657
658 WebView *webview([document_ webView]);
659 if (frame == [webview mainFrame])
660 [UIApp openURL:[request URL]];
661 }
662 }
663
664 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
665 if (request == nil) ignore: {
666 [listener ignore];
667 return;
668 }
669
670 NSURL *url([request URL]);
671 NSString *host([url host]);
672
673 if (url == nil) use: {
674 if (!error_ && [frame parentFrame] == nil) {
675 if (request_ != nil)
676 [request_ autorelease];
677 request_ = [request retain];
678 #if LogBrowser
679 NSLog(@"dpn:%@", request_);
680 #endif
681 }
682
683 [listener use];
684
685 WebView *webview([document_ webView]);
686 if (frame == [webview mainFrame])
687 [self _pushPage];
688 return;
689 }
690 #if LogBrowser
691 else NSLog(@"nav:%@:%@", url, [action description]);
692 #endif
693
694 const NSArray *capability;
695
696 if ($GSSystemCopyCapability != NULL) {
697 capability = reinterpret_cast<const NSArray *>((*$GSSystemCopyCapability)(kGSDisplayIdentifiersCapability));
698 capability = [capability autorelease];
699 } else if ($GSSystemGetCapability != NULL) {
700 capability = reinterpret_cast<const NSArray *>((*$GSSystemGetCapability)(kGSDisplayIdentifiersCapability));
701 } else
702 capability = nil;
703
704 NSURL *open(nil);
705
706 if (capability != nil && (
707 [url isGoogleMapsURL] && [capability containsObject:@"com.apple.Maps"] && (open = [url mapsURL]) != nil||
708 [host hasSuffix:@"youtube.com"] && [capability containsObject:@"com.apple.youtube"] && (open = [url youTubeURL]) != nil ||
709 [url respondsToSelector:@selector(phobosURL)] && (open = [url phobosURL]) != nil
710 )) {
711 url = open;
712 open:
713 [UIApp openURL:url];
714 goto ignore;
715 }
716
717 int store(_not(int));
718 if (NSURL *itms = [url itmsURL:&store]) {
719 #if LogBrowser
720 NSLog(@"itms#%@#%u#%@", url, store, itms);
721 #endif
722
723 if (capability != nil && (
724 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
725 store == 2 && [capability containsObject:@"com.apple.AppStore"]
726 )) {
727 url = itms;
728 goto open;
729 }
730 }
731
732 NSString *scheme([[url scheme] lowercaseString]);
733
734 if ([scheme isEqualToString:@"tel"]) {
735 // XXX: intelligence
736 goto open;
737 }
738
739 if ([scheme isEqualToString:@"mailto"]) {
740 [self _openMailToURL:url];
741 goto ignore;
742 }
743
744 if ([self getSpecial:url swap:YES])
745 goto ignore;
746 else if ([WebView _canHandleRequest:request])
747 goto use;
748 else if ([url isSpringboardHandledURL])
749 goto open;
750 else
751 goto use;
752 }
753
754 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
755 //lprintf("Status:%s\n", [text UTF8String]);
756 }
757
758 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
759 NSString *context([sheet context]);
760
761 if ([context isEqualToString:@"alert"]) {
762 [self autorelease];
763 [sheet dismiss];
764 } else if ([context isEqualToString:@"confirm"]) {
765 switch (button) {
766 case 1:
767 confirm_ = [NSNumber numberWithBool:YES];
768 break;
769
770 case 2:
771 confirm_ = [NSNumber numberWithBool:NO];
772 break;
773 }
774
775 [sheet dismiss];
776 } else if ([context isEqualToString:@"sensitive"]) {
777 switch (button) {
778 case 1:
779 sensitive_ = [NSNumber numberWithBool:YES];
780 break;
781
782 case 2:
783 sensitive_ = [NSNumber numberWithBool:NO];
784 break;
785 }
786
787 [sheet dismiss];
788 } else if ([context isEqualToString:@"challenge"]) {
789 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
790
791 switch (button) {
792 case 1: {
793 NSString *username([[sheet textFieldAtIndex:0] text]);
794 NSString *password([[sheet textFieldAtIndex:1] text]);
795
796 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
797
798 [sender useCredential:credential forAuthenticationChallenge:challenge_];
799 } break;
800
801 case 2:
802 [sender cancelAuthenticationChallenge:challenge_];
803 break;
804
805 _nodefault
806 }
807
808 [challenge_ release];
809 challenge_ = nil;
810
811 [sheet dismiss];
812 } else if ([context isEqualToString:@"submit"]) {
813 switch (button) {
814 case 1:
815 break;
816
817 case 2:
818 if (request_ != nil) {
819 WebThreadLock();
820 [document_ loadRequest:request_];
821 WebThreadUnlock();
822 }
823 break;
824
825 _nodefault
826 }
827
828 [sheet dismiss];
829 }
830 }
831
832 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
833 challenge_ = [challenge retain];
834
835 NSURLProtectionSpace *space([challenge protectionSpace]);
836 NSString *realm([space realm]);
837 if (realm == nil)
838 realm = @"";
839
840 UIActionSheet *sheet = [[[UIActionSheet alloc]
841 initWithTitle:realm
842 buttons:[NSArray arrayWithObjects:UCLocalize("LOGIN"), UCLocalize("CANCEL"), nil]
843 defaultButtonIndex:0
844 delegate:self
845 context:@"challenge"
846 ] autorelease];
847
848 [sheet setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
849
850 [sheet setNumberOfRows:1];
851
852 [sheet addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
853 [sheet addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
854
855 UITextField *username([sheet textFieldAtIndex:0]); {
856 UITextInputTraits *traits([username textInputTraits]);
857 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
858 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
859 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
860 [traits setReturnKeyType:UIReturnKeyNext];
861 }
862
863 UITextField *password([sheet textFieldAtIndex:1]); {
864 UITextInputTraits *traits([password textInputTraits]);
865 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
866 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
867 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
868 // XXX: UIReturnKeyDone
869 [traits setReturnKeyType:UIReturnKeyNext];
870 [traits setSecureTextEntry:YES];
871 }
872
873 [sheet popupAlertAnimated:YES];
874 }
875
876 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
877 return request;
878 }
879
880 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
881 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
882 #if LogBrowser
883 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
884 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
885 #endif
886
887 NSNumber *value([features objectForKey:@"width"]);
888 float width(value == nil ? 0 : [value floatValue]);
889
890 UCNavigationController *navigation(!popup_ ? [self navigationController] : [[[UCNavigationController alloc] init] autorelease]);
891
892 /* XXX: deal with cydia:// pages */
893 BrowserView *browser([[[class_ alloc] initWithWidth:width] autorelease]);
894
895 if (features != nil && popup_) {
896 [navigation setDelegate:delegate_];
897 [navigation setHook:indirect_];
898 [browser setDelegate:delegate_];
899
900 [browser loadRequest:request];
901
902 [navigation setViewControllers:[NSArray arrayWithObject:browser]];
903 UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]
904 initWithTitle:UCLocalize("CLOSE")
905 style:UIBarButtonItemStylePlain
906 target:browser
907 action:@selector(close)
908 ];
909 [[browser navigationItem] setLeftBarButtonItem:closeItem];
910 [closeItem release];
911
912 [[self navigationController] presentModalViewController:navigation animated:YES];
913 } /*else if (request == nil) {
914 [[self navigationItem] setTitle:title_];
915 [browser setDelegate:delegate_];
916 [browser retain];
917 }*/ else {
918 [self pushPage:browser];
919 [browser loadRequest:request];
920 }
921
922 return [browser webView];
923 }
924
925 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
926 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
927 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
928 }
929
930 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
931 if ([frame parentFrame] != nil)
932 return;
933
934 title_ = [title retain];
935 [[self navigationItem] setTitle:title_];
936 }
937
938 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
939 /*if ([loading_ count] == 0)
940 [self retain];*/
941 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
942
943 if ([frame parentFrame] == nil) {
944 [document_ resignFirstResponder];
945
946 reloading_ = false;
947
948 if (title_ != nil) {
949 [title_ release];
950 title_ = nil;
951 }
952
953 if (button_ != nil) {
954 [button_ release];
955 button_ = nil;
956 }
957
958 if (style_ != nil) {
959 [style_ release];
960 style_ = nil;
961 }
962
963 if (function_ != nil) {
964 [function_ release];
965 function_ = nil;
966 }
967
968 if (finish_ != nil) {
969 [finish_ release];
970 finish_ = nil;
971 }
972
973 if (closer_ != nil) {
974 [closer_ release];
975 closer_ = nil;
976 }
977
978 if (special_ != nil) {
979 [special_ release];
980 special_ = nil;
981 }
982
983 [[self navigationItem] setTitle:title_];
984
985 if (Wildcat_) {
986 CGRect webrect = [scroller_ bounds];
987 webrect.size.height = 1;
988 [document_ setFrame:webrect];
989 }
990
991 if ([scroller_ respondsToSelector:@selector(scrollPointVisibleAtTopLeft:)])
992 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
993 else
994 [scroller_ scrollRectToVisible:CGRectZero animated:NO];
995
996 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
997 [scroller_ setZoomScale:1 duration:0];
998 else if ([scroller_ respondsToSelector:@selector(_setZoomScale:duration:)])
999 [scroller_ _setZoomScale:1 duration:0];
1000 /*else if ([scroller_ respondsToSelector:@selector(setZoomScale:animated:)])
1001 [scroller_ setZoomScale:1 animated:NO];*/
1002
1003 if (!Wildcat_) {
1004 CGRect webrect = [scroller_ bounds];
1005 webrect.size.height = 0;
1006 [document_ setFrame:webrect];
1007 }
1008 }
1009
1010 [self reloadButtons];
1011 }
1012
1013 - (void) didFinishLoading { }
1014
1015 - (void) _finishLoading {
1016 size_t count([loading_ count]);
1017 /*if (count == 0)
1018 [self autorelease];*/
1019 if (reloading_ || count != 0)
1020 return;
1021 if (finish_ != nil)
1022 [self callFunction:finish_];
1023
1024 [self reloadButtons];
1025 }
1026
1027 - (bool) isLoading {
1028 return [loading_ count] != 0;
1029 }
1030
1031 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
1032 return [document_ webView:sender shouldScrollToPoint:point forFrame:frame];
1033 }
1034
1035 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
1036 return [document_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
1037 }
1038
1039 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
1040 return [document_ webView:sender needsScrollNotifications:notifications forFrame:frame];
1041 }
1042
1043 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
1044 [self _pushPage];
1045 return [document_ webView:sender didCommitLoadForFrame:frame];
1046 }
1047
1048 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
1049 return [document_ webView:sender didReceiveDocTypeForFrame:frame];
1050 }
1051
1052 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
1053 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1054 [self _finishLoading];
1055
1056 if ([frame parentFrame] == nil) {
1057 if (DOMDocument *document = [frame DOMDocument])
1058 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
1059 for (DOMHTMLBodyElement *body in bodies) {
1060 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
1061
1062 bool colored(false);
1063
1064 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
1065 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
1066 DOMRGBColor *rgb([color getRGBColorValue]);
1067
1068 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
1069 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
1070 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
1071 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
1072
1073 UIColor *uic(nil);
1074
1075 if (red == 0xc7 && green == 0xce && blue == 0xd5)
1076 uic = [UIColor pinStripeColor];
1077 else if (alpha != 0)
1078 uic = [UIColor
1079 colorWithRed:(red / 255)
1080 green:(green / 255)
1081 blue:(blue / 255)
1082 alpha:alpha
1083 ];
1084
1085 if (uic != nil) {
1086 colored = true;
1087 [scroller_ setBackgroundColor:uic];
1088 }
1089 }
1090 }
1091
1092 if (!colored)
1093 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1094 break;
1095 }
1096 }
1097
1098 return [document_ webView:sender didFinishLoadForFrame:frame];
1099 }
1100
1101 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
1102 _trace();
1103 /*if ([frame parentFrame] == nil)
1104 [self autorelease];*/
1105
1106 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1107 [self _finishLoading];
1108
1109 if (reloading_)
1110 return;
1111
1112 if ([frame parentFrame] == nil) {
1113 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
1114 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
1115 [[error localizedDescription] stringByAddingPercentEscapes]
1116 ]]];
1117
1118 error_ = true;
1119 }
1120 }
1121
1122 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1123 [self _didFailWithError:error forFrame:frame];
1124 if ([document_ respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
1125 [document_ webView:sender didFailLoadWithError:error forFrame:frame];
1126 }
1127
1128 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1129 [self _didFailWithError:error forFrame:frame];
1130 }
1131
1132 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1133 #if LogBrowser || ForSaurik
1134 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1135 #endif
1136 }
1137
1138 - (void) webView:(WebView *)sender didReceiveMessage:(NSDictionary *)dictionary {
1139 #if LogBrowser || ForSaurik
1140 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1141 #endif
1142 if ([document_ respondsToSelector:@selector(webView:didReceiveMessage:)])
1143 [document_ webView:sender didReceiveMessage:dictionary];
1144 }
1145
1146 - (void) webView:(id)sender willCloseFrame:(id)frame {
1147 if ([document_ respondsToSelector:@selector(webView:willCloseFrame:)])
1148 [document_ webView:sender willCloseFrame:frame];
1149 }
1150
1151 - (void) webView:(id)sender didFinishDocumentLoadForFrame:(id)frame {
1152 if ([document_ respondsToSelector:@selector(webView:didFinishDocumentLoadForFrame:)])
1153 [document_ webView:sender didFinishDocumentLoadForFrame:frame];
1154 }
1155
1156 - (void) webView:(id)sender didFirstLayoutInFrame:(id)frame {
1157 if ([document_ respondsToSelector:@selector(webView:didFirstLayoutInFrame:)])
1158 [document_ webView:sender didFirstLayoutInFrame:frame];
1159 }
1160
1161 - (void) webViewFormEditedStatusHasChanged:(id)changed {
1162 if ([document_ respondsToSelector:@selector(webViewFormEditedStatusHasChanged:)])
1163 [document_ webViewFormEditedStatusHasChanged:changed];
1164 }
1165
1166 - (void) webView:(id)sender formStateDidFocusNode:(id)formState {
1167 if ([document_ respondsToSelector:@selector(webView:formStateDidFocusNode:)])
1168 [document_ webView:sender formStateDidFocusNode:formState];
1169 }
1170
1171 - (void) webView:(id)sender formStateDidBlurNode:(id)formState {
1172 if ([document_ respondsToSelector:@selector(webView:formStateDidBlurNode:)])
1173 [document_ webView:sender formStateDidBlurNode:formState];
1174 }
1175
1176 /* XXX: fix this stupid include file
1177 - (void) webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)database {
1178 [origin setQuota:0x500000];
1179 }*/
1180
1181 - (void) webViewDidLayout:(id)sender {
1182 [document_ webViewDidLayout:sender];
1183 }
1184
1185 - (void) webView:(id)sender didFirstVisuallyNonEmptyLayoutInFrame:(id)frame {
1186 [document_ webView:sender didFirstVisuallyNonEmptyLayoutInFrame:frame];
1187 }
1188
1189 - (void) webView:(id)sender saveStateToHistoryItem:(id)item forFrame:(id)frame {
1190 [document_ webView:sender saveStateToHistoryItem:item forFrame:frame];
1191 }
1192
1193 - (void) webView:(id)sender restoreStateFromHistoryItem:(id)item forFrame:(id)frame force:(BOOL)force {
1194 [document_ webView:sender restoreStateFromHistoryItem:item forFrame:frame force:force];
1195 }
1196
1197 - (void) webView:(id)sender attachRootLayer:(id)layer {
1198 [document_ webView:sender attachRootLayer:layer];
1199 }
1200
1201 - (id) webView:(id)sender plugInViewWithArguments:(id)arguments fromPlugInPackage:(id)package {
1202 return [document_ webView:sender plugInViewWithArguments:arguments fromPlugInPackage:package];
1203 }
1204
1205 - (void) webView:(id)sender willShowFullScreenForPlugInView:(id)view {
1206 [document_ webView:sender willShowFullScreenForPlugInView:view];
1207 }
1208
1209 - (void) webView:(id)sender didHideFullScreenForPlugInView:(id)view {
1210 [document_ webView:sender didHideFullScreenForPlugInView:view];
1211 }
1212
1213 - (void) webView:(id)sender willAddPlugInView:(id)view {
1214 [document_ webView:sender willAddPlugInView:view];
1215 }
1216
1217 - (void) webView:(id)sender didObserveDeferredContentChange:(int)change forFrame:(id)frame {
1218 [document_ webView:sender didObserveDeferredContentChange:change forFrame:frame];
1219 }
1220
1221 - (void) webViewDidPreventDefaultForEvent:(id)sender {
1222 [document_ webViewDidPreventDefaultForEvent:sender];
1223 }
1224
1225 - (void) _setTileDrawingEnabled:(BOOL)enabled {
1226 //[document_ setTileDrawingEnabled:enabled];
1227 }
1228
1229 - (void) setViewportWidth:(float)width {
1230 width_ = width != 0 ? width : [[self class] defaultWidth];
1231 [document_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1232 }
1233
1234 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1235 [self _setTileDrawingEnabled:NO];
1236 }
1237
1238 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1239 [self _setTileDrawingEnabled:YES];
1240 [document_ redrawScaledDocument];
1241 }
1242
1243 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
1244 [self _setTileDrawingEnabled:NO];
1245 }
1246
1247 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
1248 [self _setTileDrawingEnabled:YES];
1249 }
1250
1251 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
1252 [self _setTileDrawingEnabled:YES];
1253 }
1254
1255 - (id) initWithWidth:(float)width ofClass:(Class)_class {
1256 if ((self = [super init]) != nil) {
1257 class_ = _class;
1258 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
1259 popup_ = false;
1260
1261 struct CGRect bounds = [[self view] bounds];
1262
1263 scroller_ = [[objc_getClass(Wildcat_ ? "UIScrollView" : "UIScroller") alloc] initWithFrame:bounds];
1264 [[self view] addSubview:scroller_];
1265
1266 [scroller_ setFixedBackgroundPattern:YES];
1267 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1268
1269 [scroller_ setScrollingEnabled:YES];
1270 [scroller_ setClipsSubviews:YES];
1271
1272 if (!Wildcat_)
1273 [scroller_ setAllowsRubberBanding:YES];
1274
1275 [scroller_ setDelegate:self];
1276 [scroller_ setBounces:YES];
1277
1278 if (!Wildcat_) {
1279 [scroller_ setScrollHysteresis:8];
1280 [scroller_ setThumbDetectionEnabled:NO];
1281 [scroller_ setDirectionalScrolling:YES];
1282 //[scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
1283 [scroller_ setEventMode:YES];
1284 }
1285
1286 if (Wildcat_) {
1287 UIScrollView *scroller((UIScrollView *)scroller_);
1288 //[scroller setDirectionalLockEnabled:NO];
1289 [scroller setDelaysContentTouches:NO];
1290 //[scroller setScrollsToTop:NO];
1291 //[scroller setCanCancelContentTouches:NO];
1292 }
1293
1294 [scroller_ setShowBackgroundShadow:NO]; /* YES */
1295 //[scroller_ setAllowsRubberBanding:YES]; /* Vertical */
1296
1297 if (!Wildcat_)
1298 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
1299
1300 CGRect webrect = [scroller_ bounds];
1301 webrect.size.height = 0;
1302
1303 WebView *webview;
1304
1305 WebThreadLock();
1306
1307 #if RecycleWebViews
1308 document_ = [Documents_ lastObject];
1309 if (document_ != nil) {
1310 document_ = [document_ retain];
1311 webview = [document_ webView];
1312 [Documents_ removeLastObject];
1313 [document_ setFrame:webrect];
1314 } else {
1315 #else
1316 if (true) {
1317 #endif
1318 document_ = [[$UIWebBrowserView alloc] initWithFrame:webrect];
1319 webview = [document_ webView];
1320
1321 // XXX: this is terribly (too?) expensive
1322 //[document_ setDrawsBackground:NO];
1323 [webview setPreferencesIdentifier:@"Cydia"];
1324
1325 [document_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1326
1327 if ([document_ respondsToSelector:@selector(enableReachability)])
1328 [document_ enableReachability];
1329 if ([document_ respondsToSelector:@selector(setAllowsMessaging:)])
1330 [document_ setAllowsMessaging:YES];
1331 if ([document_ respondsToSelector:@selector(useSelectionAssistantWithMode:)])
1332 [document_ useSelectionAssistantWithMode:0];
1333
1334 [document_ setTilingEnabled:YES];
1335 [document_ setDrawsGrid:NO];
1336 [document_ setLogsTilingChanges:NO];
1337 [document_ setTileMinificationFilter:kCAFilterNearest];
1338
1339 if ([document_ respondsToSelector:@selector(setDataDetectorTypes:)])
1340 /* XXX: abstractify */
1341 [document_ setDataDetectorTypes:0x80000000];
1342 else
1343 [document_ setDetectsPhoneNumbers:NO];
1344
1345 [document_ setAutoresizes:YES];
1346
1347 [document_ setMinimumScale:0.25f forDocumentTypes:0x10];
1348 [document_ setMaximumScale:5.00f forDocumentTypes:0x10];
1349 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1350 //[document_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1351
1352 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1353
1354 [document_ setMinimumScale:1.00f forDocumentTypes:0x8];
1355 [document_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1356 [document_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1357
1358 [document_ _setDocumentType:0x4];
1359
1360 if ([document_ respondsToSelector:@selector(setZoomsFocusedFormControl:)])
1361 [document_ setZoomsFocusedFormControl:YES];
1362 [document_ setContentsPosition:7];
1363 [document_ setEnabledGestures:0xa];
1364 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1365 [document_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1366
1367 [document_ setSmoothsFonts:YES];
1368 [document_ setAllowsImageSheet:YES];
1369 [webview _setUsesLoaderCache:YES];
1370
1371 [webview setGroupName:@"CydiaGroup"];
1372
1373 WebPreferences *preferences([webview preferences]);
1374
1375 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1376 [webview _setLayoutInterval:0];
1377 else
1378 [preferences _setLayoutInterval:0];
1379 }
1380
1381 [self setViewportWidth:width];
1382
1383 [document_ setDelegate:self];
1384 [document_ setGestureDelegate:self];
1385
1386 if ([document_ respondsToSelector:@selector(setFormEditingDelegate:)])
1387 [document_ setFormEditingDelegate:self];
1388
1389 [document_ setInteractionDelegate:self];
1390
1391 [scroller_ addSubview:document_];
1392
1393 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1394
1395 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1396
1397 [webview setFrameLoadDelegate:indirect_];
1398 [webview setPolicyDelegate:indirect_];
1399 [webview setResourceLoadDelegate:indirect_];
1400 [webview setUIDelegate:indirect_];
1401
1402 /* XXX: do not turn this on under penalty of extreme pain */
1403 [webview setScriptDebugDelegate:nil];
1404
1405 WebThreadUnlock();
1406
1407 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1408 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(15, 5, indsize.width, indsize.height)];
1409 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1410 [indicator_ startAnimation];
1411
1412 [scroller_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1413 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1414 [document_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1415
1416 /*UIWebView *test([[[UIWebView alloc] initWithFrame:[[self view] bounds]] autorelease]);
1417 [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
1418 [[self view] addSubview:test];*/
1419 } return self;
1420 }
1421
1422 - (id) initWithWidth:(float)width {
1423 return [self initWithWidth:width ofClass:[self class]];
1424 }
1425
1426 - (id) init {
1427 return [self initWithWidth:0];
1428 }
1429
1430 - (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
1431 WebThreadLock();
1432 WebView *webview([document_ webView]);
1433 NSString *string([webview stringByEvaluatingJavaScriptFromString:script]);
1434 WebThreadUnlock();
1435 return string;
1436 }
1437
1438 - (void) callFunction:(WebScriptObject *)function {
1439 WebThreadLock();
1440
1441 WebView *webview([document_ webView]);
1442 WebFrame *frame([webview mainFrame]);
1443
1444 id _private(MSHookIvar<id>(webview, "_private"));
1445 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1446 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1447
1448 bool no;
1449 if (settings == NULL)
1450 no = 0;
1451 else {
1452 no = settings->JavaScriptCanOpenWindowsAutomatically();
1453 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1454 }
1455
1456 if (UIWindow *window = [[self view] window])
1457 if (UIResponder *responder = [window firstResponder])
1458 [responder resignFirstResponder];
1459
1460 JSObjectRef object([function JSObject]);
1461 JSGlobalContextRef context([frame globalContext]);
1462 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1463
1464 if (settings != NULL)
1465 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1466
1467 WebThreadUnlock();
1468 }
1469
1470 - (void) didDismissModalViewController {
1471 if (closer_ != nil)
1472 [self callFunction:closer_];
1473 }
1474
1475 - (void) __rightButtonClicked {
1476 reloading_ = true;
1477 [self reloadURL];
1478 }
1479
1480 - (void) _rightButtonClicked {
1481 #if !AlwaysReload
1482 if (function_ != nil)
1483 [self callFunction:function_];
1484 else
1485 #endif
1486 [self __rightButtonClicked];
1487 }
1488
1489 - (UINavigationButtonStyle) rightButtonStyle {
1490 if (style_ == nil) normal:
1491 return UINavigationButtonStyleNormal;
1492 else if ([style_ isEqualToString:@"Normal"])
1493 return UINavigationButtonStyleNormal;
1494 else if ([style_ isEqualToString:@"Back"])
1495 return UINavigationButtonStyleBack;
1496 else if ([style_ isEqualToString:@"Highlighted"])
1497 return UINavigationButtonStyleHighlighted;
1498 else if ([style_ isEqualToString:@"Destructive"])
1499 return UINavigationButtonStyleDestructive;
1500 else goto normal;
1501 }
1502
1503 - (void) reloadButtons {
1504 if ([self isLoading]) {
1505 UIBarButtonItem *reloadItem = [[UIBarButtonItem alloc]
1506 initWithTitle:@" "
1507 style:UIBarButtonItemStylePlain
1508 target:self
1509 action:@selector(_rightButtonClicked)
1510 ];
1511 [[self navigationItem] setRightBarButtonItem:reloadItem];
1512 [[reloadItem view] addSubview:indicator_];
1513 [[self navigationItem] setTitle:UCLocalize("LOADING")];
1514 [reloadItem release];
1515 } else {
1516 UIBarButtonItem *reloadItem = [[UIBarButtonItem alloc]
1517 initWithTitle:button_ ?: UCLocalize("RELOAD")
1518 style:[self rightButtonStyle]
1519 target:self
1520 action:@selector(_rightButtonClicked)
1521 ];
1522 [[self navigationItem] setRightBarButtonItem:reloadItem animated:YES];
1523 [[self navigationItem] setTitle:title_];
1524 [reloadItem release];
1525
1526 if (function_ == nil) [self didFinishLoading];
1527 }
1528 }
1529
1530 - (void) setPageActive:(BOOL)active {
1531 if (!active)
1532 [indicator_ removeFromSuperview];
1533 else
1534 [[[[self navigationItem] rightBarButtonItem] view] addSubview:indicator_];
1535 }
1536
1537 - (void) resetViewAnimated:(BOOL)animated {
1538 }
1539
1540 - (void) setPushed:(bool)pushed {
1541 pushed_ = pushed;
1542 }
1543
1544 + (float) defaultWidth {
1545 return 980;
1546 }
1547
1548 @end