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