]> git.saurik.com Git - cydia.git/blame - UICaboodle/BrowserView.m
This is getting nuts, I need an internal branch.
[cydia.git] / UICaboodle / BrowserView.m
CommitLineData
bde2d79b
JF
1#include <BrowserView.h>
2
04700693
JF
3/* Indirect Delegate {{{ */
4@interface IndirectDelegate : NSProxy {
5 _transient volatile id delegate_;
6}
7
8- (void) setDelegate:(id)delegate;
9- (id) initWithDelegate:(id)delegate;
10@end
11
12@implementation IndirectDelegate
13
14- (void) setDelegate:(id)delegate {
15 delegate_ = delegate;
16}
17
18- (id) initWithDelegate:(id)delegate {
19 delegate_ = delegate;
20 return self;
21}
22
23- (BOOL) respondsToSelector:(SEL)sel {
24 return delegate_ == nil ? FALSE : [delegate_ respondsToSelector:sel];
25}
26
27- (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
28 if (delegate_ != nil)
29 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
30 return sig;
31 // XXX: I fucking hate Apple so very very bad
32 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
33}
34
35- (void) forwardInvocation:(NSInvocation *)inv {
36 SEL sel = [inv selector];
37 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
38 [inv invokeWithTarget:delegate_];
39}
40
41@end
42/* }}} */
43
bde2d79b
JF
44@interface WebView (Cydia)
45- (void) setScriptDebugDelegate:(id)delegate;
46- (void) _setFormDelegate:(id)delegate;
47- (void) _setUIKitDelegate:(id)delegate;
48- (void) setWebMailDelegate:(id)delegate;
49- (void) _setLayoutInterval:(float)interval;
50@end
51
45f88d41
JF
52/* Web Scripting {{{ */
53@interface CydiaObject : NSObject {
54 id indirect_;
55}
56
57- (id) initWithDelegate:(IndirectDelegate *)indirect;
58@end
59
60@implementation CydiaObject
61
62- (void) dealloc {
63 [indirect_ release];
64 [super dealloc];
65}
66
67- (id) initWithDelegate:(IndirectDelegate *)indirect {
68 if ((self = [super init]) != nil) {
69 indirect_ = [indirect retain];
70 } return self;
71}
72
3ff1504e
JF
73+ (NSArray *) _attributeKeys {
74 return [NSArray arrayWithObjects:@"device", nil];
75}
76
77- (NSArray *) attributeKeys {
78 return [[self class] _attributeKeys];
79}
80
81+ (BOOL) isKeyExcludedFromWebScript:(const char *)name {
82 return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
83}
84
85- (NSString *) device {
86 return [[UIDevice currentDevice] uniqueIdentifier];
87}
88
45f88d41 89+ (NSString *) webScriptNameForSelector:(SEL)selector {
7afcea1c
JF
90 if (selector == @selector(close))
91 return @"close";
92 else if (selector == @selector(getPackageById:))
45f88d41 93 return @"getPackageById";
7b00c562
JF
94 else if (selector == @selector(setAutoPopup:))
95 return @"setAutoPopup";
45f88d41
JF
96 else if (selector == @selector(setButtonImage:withStyle:toFunction:))
97 return @"setButtonImage";
98 else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
99 return @"setButtonTitle";
1ce016d4
JF
100 else if (selector == @selector(setPopupHook:))
101 return @"setPopupHook";
dcc55b8b
JF
102 else if (selector == @selector(setViewportWidth:))
103 return @"setViewportWidth";
45f88d41
JF
104 else if (selector == @selector(supports:))
105 return @"supports";
106 else if (selector == @selector(du:))
107 return @"du";
108 else if (selector == @selector(statfs:))
109 return @"statfs";
110 else
111 return nil;
112}
113
114+ (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
115 return [self webScriptNameForSelector:selector] == nil;
116}
117
118- (BOOL) supports:(NSString *)feature {
119 return [feature isEqualToString:@"window.open"];
120}
121
122- (Package *) getPackageById:(NSString *)id {
123 return [[Database sharedInstance] packageWithName:id];
124}
125
126- (NSArray *) statfs:(NSString *)path {
127 struct statfs stat;
128
129 if (path == nil || statfs([path UTF8String], &stat) == -1)
130 return nil;
131
132 return [NSArray arrayWithObjects:
133 [NSNumber numberWithUnsignedLong:stat.f_bsize],
134 [NSNumber numberWithUnsignedLong:stat.f_blocks],
135 [NSNumber numberWithUnsignedLong:stat.f_bfree],
136 nil];
137}
138
139- (NSNumber *) du:(NSString *)path {
140 NSNumber *value(nil);
141
142 int fds[2];
143 _assert(pipe(fds) != -1);
144
145 pid_t pid(ExecFork());
146 if (pid == 0) {
147 _assert(dup2(fds[1], 1) != -1);
148 _assert(close(fds[0]) != -1);
149 _assert(close(fds[1]) != -1);
150 execlp("du", "du", "-s", [path UTF8String], NULL);
151 exit(1);
152 _assert(false);
153 }
154
155 _assert(close(fds[1]) != -1);
156
157 if (FILE *du = fdopen(fds[0], "r")) {
158 char line[1024];
159 while (fgets(line, sizeof(line), du) != NULL) {
160 size_t length(strlen(line));
161 while (length != 0 && line[length - 1] == '\n')
162 line[--length] = '\0';
163 if (char *tab = strchr(line, '\t')) {
164 *tab = '\0';
165 value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)];
166 }
167 }
168
169 fclose(du);
170 } else _assert(close(fds[0]));
171
172 int status;
173 wait:
174 if (waitpid(pid, &status, 0) == -1)
175 if (errno == EINTR)
176 goto wait;
177 else _assert(false);
178
179 return value;
180}
181
7afcea1c
JF
182- (void) close {
183 [indirect_ close];
184}
185
7b00c562
JF
186- (void) setAutoPopup:(BOOL)popup {
187 [indirect_ setAutoPopup:popup];
188}
189
45f88d41
JF
190- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
191 [indirect_ setButtonImage:button withStyle:style toFunction:function];
192}
193
194- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
195 [indirect_ setButtonTitle:button withStyle:style toFunction:function];
196}
197
1ce016d4
JF
198- (void) setPopupHook:(id)function {
199 [indirect_ setPopupHook:function];
200}
201
dcc55b8b
JF
202- (void) setViewportWidth:(float)width {
203 [indirect_ setViewportWidth:width];
204}
205
45f88d41
JF
206@end
207/* }}} */
208
bde2d79b
JF
209@implementation BrowserView
210
5a5c1e4b 211#if ShowInternals
bde2d79b
JF
212#include "Internals.h"
213#endif
214
215- (void) dealloc {
1ce016d4 216#if LogBrowser
7b00c562
JF
217 NSLog(@"[BrowserView dealloc]");
218#endif
5a5c1e4b 219
bde2d79b
JF
220 if (challenge_ != nil)
221 [challenge_ release];
222
223 WebView *webview = [webview_ webView];
224 [webview setFrameLoadDelegate:nil];
225 [webview setResourceLoadDelegate:nil];
226 [webview setUIDelegate:nil];
227 [webview setScriptDebugDelegate:nil];
228 [webview setPolicyDelegate:nil];
229
230 [webview setDownloadDelegate:nil];
231
232 [webview _setFormDelegate:nil];
233 [webview _setUIKitDelegate:nil];
234 [webview setWebMailDelegate:nil];
235 [webview setEditingDelegate:nil];
236
237 [webview_ setDelegate:nil];
238 [webview_ setGestureDelegate:nil];
dcc55b8b
JF
239 [webview_ setFormEditingDelegate:nil];
240 [webview_ setInteractionDelegate:nil];
bde2d79b
JF
241
242 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
243
244 [webview close];
245
246#if RecycleWebViews
247 [webview_ removeFromSuperview];
248 [Documents_ addObject:[webview_ autorelease]];
249#else
250 [webview_ release];
251#endif
252
253 [indirect_ setDelegate:nil];
254 [indirect_ release];
255
45f88d41
JF
256 [cydia_ release];
257
bde2d79b
JF
258 [scroller_ setDelegate:nil];
259
260 if (button_ != nil)
261 [button_ release];
262 if (style_ != nil)
263 [style_ release];
264 if (function_ != nil)
265 [function_ release];
1ce016d4
JF
266 if (closer_ != nil)
267 [closer_ release];
bde2d79b
JF
268
269 [scroller_ release];
270 [indicator_ release];
271 if (confirm_ != nil)
272 [confirm_ release];
273 if (title_ != nil)
274 [title_ release];
275 [super dealloc];
276}
277
278- (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
279 [self loadRequest:[NSURLRequest
280 requestWithURL:url
281 cachePolicy:policy
282 timeoutInterval:30.0
283 ]];
284}
285
286- (void) loadURL:(NSURL *)url {
287 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
288}
289
290- (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
291 NSMutableURLRequest *copy = [request mutableCopy];
292
293 if (Machine_ != NULL)
294 [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
295 if (UniqueID_ != nil)
296 [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
297
298 if (Role_ != nil)
299 [copy setValue:Role_ forHTTPHeaderField:@"X-Role"];
300
301 return copy;
302}
303
304- (void) loadRequest:(NSURLRequest *)request {
305 pushed_ = true;
f4772bd1 306 error_ = false;
bde2d79b
JF
307 [webview_ loadRequest:request];
308}
309
310- (void) reloadURL {
bde2d79b
JF
311 if (request_ == nil)
312 return;
313
314 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
f4772bd1 315 [self loadRequest:request_];
bde2d79b
JF
316 else {
317 UIActionSheet *sheet = [[[UIActionSheet alloc]
318 initWithTitle:@"Are you sure you want to submit this form again?"
319 buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil]
320 defaultButtonIndex:0
321 delegate:self
322 context:@"submit"
323 ] autorelease];
324
325 [sheet setNumberOfRows:1];
326 [sheet popupAlertAnimated:YES];
327 }
328}
329
330- (WebView *) webView {
331 return [webview_ webView];
332}
333
5a5c1e4b
JF
334- (UIWebDocumentView *) documentView {
335 return webview_;
336}
337
dcc55b8b 338- (void) _fixScroller {
5889429e 339 CGRect bounds([webview_ documentBounds]);
1ce016d4 340#if LogBrowser
5889429e
JF
341 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
342#endif
343
dcc55b8b
JF
344 float extra;
345 if (!editing_)
346 extra = 0;
347 else {
348 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
349 CGRect peripheral([assistant peripheralFrame]);
1ce016d4 350#if LogBrowser
dcc55b8b 351 NSLog(@"per:%f", peripheral.size.height);
5889429e 352#endif
dcc55b8b
JF
353 extra = peripheral.size.height;
354 }
355
356 CGRect subrect([scroller_ frame]);
357 subrect.size.height -= extra;
358 [scroller_ setScrollerIndicatorSubrect:subrect];
359
5889429e
JF
360 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
361 [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
362
dcc55b8b
JF
363 CGSize size(size_);
364 size.height += extra;
365 [scroller_ setContentSize:size];
5889429e
JF
366
367 [scroller_ releaseRubberBandIfNecessary];
dcc55b8b
JF
368}
369
bde2d79b 370- (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
dcc55b8b 371 size_ = frame.size;
1ce016d4 372#if LogBrowser
5889429e
JF
373 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
374#endif
dcc55b8b 375 [self _fixScroller];
bde2d79b
JF
376}
377
378- (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
379 [self view:sender didSetFrame:frame];
380}
381
382- (void) pushPage:(RVPage *)page {
bde2d79b 383 [page setDelegate:delegate_];
2517c95f 384 [self setBackButtonTitle:title_];
5a5c1e4b 385 [book_ pushPage:page];
bde2d79b
JF
386}
387
a55054a2
JF
388- (void) _pushPage {
389 if (pushed_)
390 return;
391 [self autorelease];
392 pushed_ = true;
393 [book_ pushPage:self];
394}
395
5889429e
JF
396- (void) swapPage:(RVPage *)page {
397 [page setDelegate:delegate_];
398 if (pushed_)
399 [book_ swapPage:page];
400 else
401 [book_ pushPage:page];
402}
403
b8de369a 404- (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
1ce016d4 405#if LogBrowser
a55054a2
JF
406 NSLog(@"getSpecial:%@", url);
407#endif
408
bde2d79b
JF
409 NSString *href([url absoluteString]);
410 NSString *scheme([[url scheme] lowercaseString]);
411
412 RVPage *page = nil;
413
414 if ([href hasPrefix:@"apptapp://package/"])
415 page = [delegate_ pageForPackage:[href substringFromIndex:18]];
416 else if ([scheme isEqualToString:@"cydia"]) {
417 page = [delegate_ pageForURL:url hasTag:NULL];
418 if (page == nil)
419 return false;
420 } else if (![scheme isEqualToString:@"apptapp"])
421 return false;
422
423 if (page != nil)
b8de369a
JF
424 if (swap)
425 [self swapPage:page];
426 else
427 [self pushPage:page];
bde2d79b
JF
428 return true;
429}
430
5a5c1e4b 431- (void) webViewShow:(WebView *)sender {
5b2d5cf3 432 /* XXX: this is where I cry myself to sleep */
5a5c1e4b
JF
433}
434
7b00c562
JF
435- (bool) _allowJavaScriptPanel {
436 return true;
437}
438
bde2d79b 439- (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
263b91b4 440 if (![self _allowJavaScriptPanel])
7b00c562
JF
441 return;
442
b456f1c9
JF
443 [self retain];
444
bde2d79b
JF
445 UIActionSheet *sheet = [[[UIActionSheet alloc]
446 initWithTitle:nil
447 buttons:[NSArray arrayWithObjects:@"OK", nil]
448 defaultButtonIndex:0
449 delegate:self
450 context:@"alert"
451 ] autorelease];
452
453 [sheet setBodyText:message];
454 [sheet popupAlertAnimated:YES];
455}
456
457- (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
7b00c562
JF
458 if (![self _allowJavaScriptPanel])
459 return NO;
460
bde2d79b
JF
461 UIActionSheet *sheet = [[[UIActionSheet alloc]
462 initWithTitle:nil
463 buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
464 defaultButtonIndex:0
b456f1c9 465 delegate:indirect_
bde2d79b
JF
466 context:@"confirm"
467 ] autorelease];
468
469 [sheet setNumberOfRows:1];
470 [sheet setBodyText:message];
471 [sheet popupAlertAnimated:YES];
472
473 NSRunLoop *loop([NSRunLoop currentRunLoop]);
474 NSDate *future([NSDate distantFuture]);
475
476 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
477
478 NSNumber *confirm([confirm_ autorelease]);
479 confirm_ = nil;
480 return [confirm boolValue];
481}
482
7b00c562
JF
483- (void) setAutoPopup:(BOOL)popup {
484 popup_ = popup;
485}
486
1ce016d4
JF
487- (void) setPopupHook:(id)function {
488 if (closer_ != nil)
489 [closer_ autorelease];
490 closer_ = function == nil ? nil : [function retain];
491}
492
bde2d79b
JF
493- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
494 if (button_ != nil)
495 [button_ autorelease];
496 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
497
498 if (style_ != nil)
499 [style_ autorelease];
500 style_ = style == nil ? nil : [style retain];
501
502 if (function_ != nil)
503 [function_ autorelease];
504 function_ = function == nil ? nil : [function retain];
1ce016d4
JF
505
506 [self reloadButtons];
bde2d79b
JF
507}
508
509- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
510 if (button_ != nil)
511 [button_ autorelease];
512 button_ = button == nil ? nil : [button retain];
513
514 if (style_ != nil)
515 [style_ autorelease];
516 style_ = style == nil ? nil : [style retain];
517
518 if (function_ != nil)
519 [function_ autorelease];
520 function_ = function == nil ? nil : [function retain];
1ce016d4
JF
521
522 [self reloadButtons];
bde2d79b 523}
bde2d79b 524
dcc55b8b
JF
525- (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
526 editing_ = true;
527}
528
529- (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
530 [self _fixScroller];
531}
532
533- (void) webViewDidEndEditingFormElements:(WebView *)sender {
534 editing_ = false;
535 [self _fixScroller];
536}
537
04700693
JF
538- (void) webViewClose:(WebView *)sender {
539 [book_ close];
540}
541
7afcea1c
JF
542- (void) close {
543 [book_ close];
544}
545
bde2d79b 546- (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
45f88d41 547 [window setValue:cydia_ forKey:@"cydia"];
bde2d79b
JF
548}
549
550- (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
551 NSLog(@"err:%@", error);
552}
553
554- (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
1ce016d4 555#if LogBrowser
5a5c1e4b 556 NSLog(@"nwa:%@", name);
7b00c562 557#endif
5a5c1e4b 558
bde2d79b 559 if (NSURL *url = [request URL]) {
04700693 560 if (name == nil) unknown: {
b8de369a 561 if (![self getSpecial:url swap:NO]) {
04700693
JF
562 NSString *scheme([[url scheme] lowercaseString]);
563 if ([scheme isEqualToString:@"mailto"])
564 [delegate_ openMailToURL:url];
565 else goto use;
566 }
567 } else if ([name isEqualToString:@"_open"])
bde2d79b 568 [delegate_ openURL:url];
04700693 569 else if ([name isEqualToString:@"_popup"]) {
3ac7a20a
JF
570 NSString *scheme([[url scheme] lowercaseString]);
571 if ([scheme isEqualToString:@"mailto"])
572 [delegate_ openMailToURL:url];
573 else {
574 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
575 [book setHook:indirect_];
04700693 576
3ac7a20a
JF
577 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
578 if (page == nil) {
579 /* XXX: call createWebViewWithRequest instead? */
5a5c1e4b 580
3ac7a20a 581 [self setBackButtonTitle:title_];
5a5c1e4b 582
3ac7a20a
JF
583 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
584 [browser loadURL:url];
585 page = browser;
586 }
bde2d79b 587
3ac7a20a
JF
588 [book setDelegate:delegate_];
589 [page setDelegate:delegate_];
0a7e5478 590
3ac7a20a
JF
591 [book setPage:page];
592 [book_ pushBook:book];
593 }
04700693 594 } else goto unknown;
bde2d79b
JF
595
596 [listener ignore];
597 } else use:
598 [listener use];
599}
600
5889429e 601- (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
bde2d79b
JF
602 if ([WebView canShowMIMEType:type])
603 [listener use];
604 else {
605 // XXX: handle more mime types!
606 [listener ignore];
5889429e
JF
607
608 WebView *webview([webview_ webView]);
609 if (frame == [webview mainFrame])
bde2d79b
JF
610 [UIApp openURL:[request URL]];
611 }
612}
613
614- (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
615 if (request == nil) ignore: {
616 [listener ignore];
617 return;
618 }
619
620 NSURL *url([request URL]);
621
622 if (url == nil) use: {
f4772bd1 623 if (!error_ && [frame parentFrame] == nil) {
bde2d79b
JF
624 if (request_ != nil)
625 [request_ autorelease];
626 request_ = [request retain];
1ce016d4 627#if LogBrowser
bde2d79b 628 NSLog(@"dpn:%@", request_);
04700693 629#endif
bde2d79b
JF
630 }
631
632 [listener use];
5889429e
JF
633
634 WebView *webview([webview_ webView]);
635 if (frame == [webview mainFrame])
636 [self _pushPage];
bde2d79b
JF
637 return;
638 }
1ce016d4 639#if LogBrowser
bde2d79b
JF
640 else NSLog(@"nav:%@:%@", url, [action description]);
641#endif
642
643 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
644
645 if (
646 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
647 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
648 ) {
649 open:
650 [UIApp openURL:url];
651 goto ignore;
652 }
653
654 int store(_not(int));
655 if (NSURL *itms = [url itmsURL:&store]) {
1ce016d4 656#if LogBrowser
bde2d79b 657 NSLog(@"itms#%@#%u#%@", url, store, itms);
7b00c562
JF
658#endif
659
bde2d79b
JF
660 if (
661 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
662 store == 2 && [capability containsObject:@"com.apple.AppStore"]
663 ) {
664 url = itms;
665 goto open;
666 }
667 }
668
669 NSString *scheme([[url scheme] lowercaseString]);
670
671 if ([scheme isEqualToString:@"tel"]) {
672 // XXX: intelligence
673 goto open;
674 }
675
676 if ([scheme isEqualToString:@"mailto"]) {
677 [delegate_ openMailToURL:url];
678 goto ignore;
679 }
680
b8de369a 681 if ([self getSpecial:url swap:YES])
bde2d79b
JF
682 goto ignore;
683 else if ([WebView _canHandleRequest:request])
684 goto use;
685 else if ([url isSpringboardHandledURL])
686 goto open;
687 else
688 goto use;
689}
690
691- (void) webView:(WebView *)sender setStatusText:(NSString *)text {
692 //lprintf("Status:%s\n", [text UTF8String]);
693}
694
bde2d79b
JF
695- (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
696 NSString *context([sheet context]);
697
b456f1c9
JF
698 if ([context isEqualToString:@"alert"]) {
699 [self autorelease];
bde2d79b 700 [sheet dismiss];
b456f1c9 701 } else if ([context isEqualToString:@"confirm"]) {
bde2d79b
JF
702 switch (button) {
703 case 1:
704 confirm_ = [NSNumber numberWithBool:YES];
705 break;
706
707 case 2:
708 confirm_ = [NSNumber numberWithBool:NO];
709 break;
710 }
711
712 [sheet dismiss];
713 } else if ([context isEqualToString:@"challenge"]) {
714 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
715
716 switch (button) {
717 case 1: {
718 NSString *username([[sheet textFieldAtIndex:0] text]);
719 NSString *password([[sheet textFieldAtIndex:1] text]);
720
721 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
722
723 [sender useCredential:credential forAuthenticationChallenge:challenge_];
724 } break;
725
726 case 2:
727 [sender cancelAuthenticationChallenge:challenge_];
728 break;
729
730 default:
731 _assert(false);
732 }
733
734 [challenge_ release];
735 challenge_ = nil;
736
737 [sheet dismiss];
738 } else if ([context isEqualToString:@"submit"]) {
739 switch (button) {
740 case 1:
741 break;
742
743 case 2:
744 if (request_ != nil)
745 [webview_ loadRequest:request_];
746 break;
747
748 default:
749 _assert(false);
750 }
751
752 [sheet dismiss];
753 }
754}
755
756- (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
757 challenge_ = [challenge retain];
758
759 NSURLProtectionSpace *space([challenge protectionSpace]);
760 NSString *realm([space realm]);
761 if (realm == nil)
762 realm = @"";
763
764 UIActionSheet *sheet = [[[UIActionSheet alloc]
765 initWithTitle:realm
766 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
767 defaultButtonIndex:0
768 delegate:self
769 context:@"challenge"
770 ] autorelease];
771
772 [sheet setNumberOfRows:1];
773
774 [sheet addTextFieldWithValue:@"" label:@"username"];
775 [sheet addTextFieldWithValue:@"" label:@"password"];
776
777 UITextField *username([sheet textFieldAtIndex:0]); {
778 UITextInputTraits *traits([username textInputTraits]);
779 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
780 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
781 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
782 [traits setReturnKeyType:UIReturnKeyNext];
783 }
784
785 UITextField *password([sheet textFieldAtIndex:1]); {
786 UITextInputTraits *traits([password textInputTraits]);
787 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
788 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
789 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
790 // XXX: UIReturnKeyDone
791 [traits setReturnKeyType:UIReturnKeyNext];
792 [traits setSecureTextEntry:YES];
793 }
794
795 [sheet popupAlertAnimated:YES];
796}
797
798- (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
bde2d79b
JF
799 return [self _addHeadersToRequest:request];
800}
801
5a5c1e4b 802- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
7b00c562 803//- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
1ce016d4 804#if LogBrowser
7b00c562
JF
805 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
806 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
5a5c1e4b 807#endif
bde2d79b 808
7b00c562 809 NSNumber *value([features objectForKey:@"width"]);
5889429e 810 float width(value == nil ? 0 : [value floatValue]);
7b00c562
JF
811
812 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
813
814 /* XXX: deal with cydia:// pages */
815 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
816
b456f1c9 817 if (features != nil && popup_) {
7b00c562 818 [book setDelegate:delegate_];
1ce016d4 819 [book setHook:indirect_];
7b00c562
JF
820 [browser setDelegate:delegate_];
821
822 [browser loadRequest:request];
a55054a2 823
7b00c562
JF
824 [book setPage:browser];
825 [book_ pushBook:book];
826 } else if (request == nil) {
a55054a2
JF
827 [self setBackButtonTitle:title_];
828 [browser setDelegate:delegate_];
829 [browser retain];
830 } else {
831 [self pushPage:browser];
832 [browser loadRequest:request];
833 }
834
bde2d79b
JF
835 return [browser webView];
836}
837
838- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
5a5c1e4b 839 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
7b00c562 840 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
bde2d79b
JF
841}
842
843- (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
844 if ([frame parentFrame] != nil)
845 return;
846
847 title_ = [title retain];
848 [book_ reloadTitleForPage:self];
849}
850
851- (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
852 if ([frame parentFrame] != nil)
853 return;
854
5889429e
JF
855 [webview_ resignFirstResponder];
856
bde2d79b
JF
857 reloading_ = false;
858 loading_ = true;
859 [self reloadButtons];
860
861 if (title_ != nil) {
862 [title_ release];
863 title_ = nil;
864 }
865
866 if (button_ != nil) {
867 [button_ release];
868 button_ = nil;
869 }
870
871 if (style_ != nil) {
872 [style_ release];
873 style_ = nil;
874 }
875
876 if (function_ != nil) {
877 [function_ release];
878 function_ = nil;
879 }
880
1ce016d4
JF
881 if (closer_ != nil) {
882 [closer_ release];
883 closer_ = nil;
884 }
885
bde2d79b
JF
886 [book_ reloadTitleForPage:self];
887
888 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
5889429e 889 [scroller_ setZoomScale:1 duration:0];
bde2d79b
JF
890
891 CGRect webrect = [scroller_ bounds];
892 webrect.size.height = 0;
893 [webview_ setFrame:webrect];
894}
895
896- (void) _finishLoading {
897 if (!reloading_) {
898 loading_ = false;
899 [self reloadButtons];
900 }
901}
902
1ce016d4 903- (bool) isLoading {
bde2d79b
JF
904 return loading_;
905}
906
907- (void) reloadButtons {
1ce016d4 908 if ([self isLoading])
bde2d79b
JF
909 [indicator_ startAnimation];
910 else
911 [indicator_ stopAnimation];
912 [super reloadButtons];
913}
914
915- (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
916 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
917}
918
919- (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
920 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
921}
922
923- (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
924 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
925}
926
927- (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
10c3a9c1 928 [self _pushPage];
bde2d79b
JF
929 return [webview_ webView:sender didCommitLoadForFrame:frame];
930}
931
932- (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
933 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
934}
935
936- (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
937 if ([frame parentFrame] == nil) {
938 [self _finishLoading];
939
940 if (DOMDocument *document = [frame DOMDocument])
941 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
942 for (DOMHTMLBodyElement *body in bodies) {
943 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
944
945 bool colored(false);
946
947 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
f4772bd1
JF
948 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
949 DOMRGBColor *rgb([color getRGBColorValue]);
950
951 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
952 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
953 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
954 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
955
956 UIColor *uic(nil);
957
958 if (red == 0xc7 && green == 0xce && blue == 0xd5)
959 uic = [UIColor pinStripeColor];
960 else if (alpha != 0)
961 uic = [UIColor
962 colorWithRed:(red / 255)
963 green:(green / 255)
964 blue:(blue / 255)
965 alpha:alpha
966 ];
967
968 if (uic != nil) {
969 colored = true;
970 [scroller_ setBackgroundColor:uic];
971 }
bde2d79b
JF
972 }
973 }
974
975 if (!colored)
976 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
977 break;
978 }
979 }
980
981 return [webview_ webView:sender didFinishLoadForFrame:frame];
982}
983
984- (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
985 if ([frame parentFrame] != nil)
986 return;
2517c95f
JF
987 if (reloading_)
988 return;
bde2d79b
JF
989 [self _finishLoading];
990
991 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
992 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
993 [[error localizedDescription] stringByAddingPercentEscapes]
994 ]]];
f4772bd1
JF
995
996 error_ = true;
bde2d79b
JF
997}
998
999- (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1ce016d4 1000#if LogBrowser || ForSaurik
bde2d79b
JF
1001 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1002#endif
1003}
1004
dcc55b8b 1005- (void) setViewportWidth:(float)width {
5889429e 1006 width_ = width ? width != 0 : [[self class] defaultWidth];
dcc55b8b
JF
1007 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1008}
1009
7b00c562 1010- (id) initWithBook:(RVBook *)book forWidth:(float)width {
bde2d79b
JF
1011 if ((self = [super initWithBook:book]) != nil) {
1012 loading_ = false;
7b00c562 1013 popup_ = false;
bde2d79b
JF
1014
1015 struct CGRect bounds = [self bounds];
1016
1017 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
1018 [self addSubview:scroller_];
1019
1020 [scroller_ setShowBackgroundShadow:NO];
1021 [scroller_ setFixedBackgroundPattern:YES];
1022 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1023
1024 [scroller_ setScrollingEnabled:YES];
1025 [scroller_ setAdjustForContentSizeChange:YES];
1026 [scroller_ setClipsSubviews:YES];
1027 [scroller_ setAllowsRubberBanding:YES];
1028 [scroller_ setScrollDecelerationFactor:0.99];
1029 [scroller_ setDelegate:self];
1030
1031 CGRect webrect = [scroller_ bounds];
1032 webrect.size.height = 0;
1033
1034 WebView *webview;
1035
1036#if RecycleWebViews
1037 webview_ = [Documents_ lastObject];
1038 if (webview_ != nil) {
1039 webview_ = [webview_ retain];
1040 webview = [webview_ webView];
1041 [Documents_ removeLastObject];
1042 [webview_ setFrame:webrect];
1043 } else {
1044#else
1045 if (true) {
1046#endif
1047 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
1048 webview = [webview_ webView];
1049
1050 // XXX: this is terribly (too?) expensive
1051 //[webview_ setDrawsBackground:NO];
1052 [webview setPreferencesIdentifier:@"Cydia"];
1053
1054 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1055
1056 [webview_ setAllowsMessaging:YES];
1057
1058 [webview_ setTilingEnabled:YES];
1059 [webview_ setDrawsGrid:NO];
1060 [webview_ setLogsTilingChanges:NO];
1061 [webview_ setTileMinificationFilter:kCAFilterNearest];
1062 [webview_ setDetectsPhoneNumbers:NO];
1063 [webview_ setAutoresizes:YES];
1064
1065 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
7b00c562 1066 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
bde2d79b 1067 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
7b00c562 1068 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
bde2d79b
JF
1069
1070 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1071
7b00c562 1072 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
bde2d79b
JF
1073 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1074 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1075
1076 [webview_ _setDocumentType:0x4];
1077
1078 [webview_ setZoomsFocusedFormControl:YES];
1079 [webview_ setContentsPosition:7];
1080 [webview_ setEnabledGestures:0xa];
1081 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1082 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1083
1084 [webview_ setSmoothsFonts:YES];
7b00c562 1085 [webview_ setAllowsImageSheet:YES];
bde2d79b 1086 [webview _setUsesLoaderCache:YES];
7b00c562
JF
1087
1088 [webview setGroupName:@"CydiaGroup"];
bde2d79b
JF
1089 [webview _setLayoutInterval:0];
1090 }
1091
dcc55b8b 1092 [self setViewportWidth:width];
7b00c562 1093
bde2d79b
JF
1094 [webview_ setDelegate:self];
1095 [webview_ setGestureDelegate:self];
dcc55b8b
JF
1096 [webview_ setFormEditingDelegate:self];
1097 [webview_ setInteractionDelegate:self];
1098
bde2d79b
JF
1099 [scroller_ addSubview:webview_];
1100
1101 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1102
1103 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1104 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1105 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1106
1107 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1108 NSString *application = package == nil ? @"Cydia" : [NSString
1109 stringWithFormat:@"Cydia/%@",
1110 [package installed]
68c05606
JF
1111 ];
1112
7b00c562
JF
1113 if (Product_ != nil)
1114 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
68c05606 1115 if (Build_ != nil)
7b00c562
JF
1116 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1117 if (Safari_ != nil)
1118 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
68c05606
JF
1119
1120 /* XXX: lookup application directory? */
1121 /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
1122 if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
1123 application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
1124
1125 [webview setApplicationNameForUserAgent:application];
bde2d79b
JF
1126
1127 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
45f88d41 1128 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
bde2d79b
JF
1129
1130 [webview setFrameLoadDelegate:self];
1131 [webview setResourceLoadDelegate:indirect_];
1132 [webview setUIDelegate:self];
1133 [webview setScriptDebugDelegate:self];
1134 [webview setPolicyDelegate:self];
1135
1136 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1137 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1138 } return self;
1139}
1140
7b00c562 1141- (id) initWithBook:(RVBook *)book {
5889429e 1142 return [self initWithBook:book forWidth:0];
7b00c562
JF
1143}
1144
bde2d79b
JF
1145- (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1146 [webview_ redrawScaledDocument];
1147}
1148
1ce016d4
JF
1149- (void) callFunction:(WebScriptObject *)function {
1150 WebView *webview([webview_ webView]);
1151 WebFrame *frame([webview mainFrame]);
bde2d79b 1152
1ce016d4
JF
1153 id _private(MSHookIvar<id>(webview, "_private"));
1154 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1155 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
bde2d79b 1156
1ce016d4
JF
1157 bool no;
1158 if (settings == NULL)
1159 no = 0;
1160 else {
1161 no = settings->JavaScriptCanOpenWindowsAutomatically();
1162 settings->setJavaScriptCanOpenWindowsAutomatically(true);
bde2d79b 1163 }
1ce016d4
JF
1164
1165 [delegate_ clearFirstResponder];
1166 JSObjectRef object([function JSObject]);
1167 JSGlobalContextRef context([frame globalContext]);
1168 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1169
1170 if (settings != NULL)
1171 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1172}
1173
1174- (void) didCloseBook:(RVBook *)book {
1175 if (closer_ != nil)
1176 [self callFunction:closer_];
1177}
1178
1179- (void) __rightButtonClicked {
1180 reloading_ = true;
1181 [self reloadURL];
1182}
1183
1184- (void) _rightButtonClicked {
1185 if (function_ == nil)
1186 [self __rightButtonClicked];
1187 else
1188 [self callFunction:function_];
bde2d79b
JF
1189}
1190
1191- (id) _rightButtonTitle {
1ce016d4 1192 return @"Reload";
bde2d79b
JF
1193}
1194
1195- (id) rightButtonTitle {
1ce016d4 1196 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
bde2d79b
JF
1197}
1198
1199- (UINavigationButtonStyle) rightButtonStyle {
1200 if (style_ == nil) normal:
1201 return UINavigationButtonStyleNormal;
1202 else if ([style_ isEqualToString:@"Normal"])
1203 return UINavigationButtonStyleNormal;
1204 else if ([style_ isEqualToString:@"Back"])
1205 return UINavigationButtonStyleBack;
1206 else if ([style_ isEqualToString:@"Highlighted"])
1207 return UINavigationButtonStyleHighlighted;
1208 else if ([style_ isEqualToString:@"Destructive"])
1209 return UINavigationButtonStyleDestructive;
1210 else goto normal;
1211}
1212
1213- (NSString *) title {
1214 return title_ == nil ? @"Loading" : title_;
1215}
1216
1217- (NSString *) backButtonTitle {
1218 return @"Browser";
1219}
1220
1221- (void) setPageActive:(BOOL)active {
1222 if (!active)
1223 [indicator_ removeFromSuperview];
1224 else
1225 [[book_ navigationBar] addSubview:indicator_];
1226}
1227
1228- (void) resetViewAnimated:(BOOL)animated {
1229}
1230
1231- (void) setPushed:(bool)pushed {
1232 pushed_ = pushed;
1233}
1234
7b00c562
JF
1235+ (float) defaultWidth {
1236 return 980;
1237}
1238
bde2d79b 1239@end