]> git.saurik.com Git - cydia.git/blob - UICaboodle/BrowserView.m
This is getting nuts, I need an internal branch.
[cydia.git] / UICaboodle / BrowserView.m
1 #include <BrowserView.h>
2
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
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
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 + (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
89 + (NSString *) webScriptNameForSelector:(SEL)selector {
90 if (selector == @selector(close))
91 return @"close";
92 else if (selector == @selector(getPackageById:))
93 return @"getPackageById";
94 else if (selector == @selector(setAutoPopup:))
95 return @"setAutoPopup";
96 else if (selector == @selector(setButtonImage:withStyle:toFunction:))
97 return @"setButtonImage";
98 else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
99 return @"setButtonTitle";
100 else if (selector == @selector(setPopupHook:))
101 return @"setPopupHook";
102 else if (selector == @selector(setViewportWidth:))
103 return @"setViewportWidth";
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
182 - (void) close {
183 [indirect_ close];
184 }
185
186 - (void) setAutoPopup:(BOOL)popup {
187 [indirect_ setAutoPopup:popup];
188 }
189
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
198 - (void) setPopupHook:(id)function {
199 [indirect_ setPopupHook:function];
200 }
201
202 - (void) setViewportWidth:(float)width {
203 [indirect_ setViewportWidth:width];
204 }
205
206 @end
207 /* }}} */
208
209 @implementation BrowserView
210
211 #if ShowInternals
212 #include "Internals.h"
213 #endif
214
215 - (void) dealloc {
216 #if LogBrowser
217 NSLog(@"[BrowserView dealloc]");
218 #endif
219
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];
239 [webview_ setFormEditingDelegate:nil];
240 [webview_ setInteractionDelegate:nil];
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
256 [cydia_ release];
257
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];
266 if (closer_ != nil)
267 [closer_ release];
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;
306 error_ = false;
307 [webview_ loadRequest:request];
308 }
309
310 - (void) reloadURL {
311 if (request_ == nil)
312 return;
313
314 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
315 [self loadRequest:request_];
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
334 - (UIWebDocumentView *) documentView {
335 return webview_;
336 }
337
338 - (void) _fixScroller {
339 CGRect bounds([webview_ documentBounds]);
340 #if LogBrowser
341 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
342 #endif
343
344 float extra;
345 if (!editing_)
346 extra = 0;
347 else {
348 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
349 CGRect peripheral([assistant peripheralFrame]);
350 #if LogBrowser
351 NSLog(@"per:%f", peripheral.size.height);
352 #endif
353 extra = peripheral.size.height;
354 }
355
356 CGRect subrect([scroller_ frame]);
357 subrect.size.height -= extra;
358 [scroller_ setScrollerIndicatorSubrect:subrect];
359
360 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
361 [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
362
363 CGSize size(size_);
364 size.height += extra;
365 [scroller_ setContentSize:size];
366
367 [scroller_ releaseRubberBandIfNecessary];
368 }
369
370 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
371 size_ = frame.size;
372 #if LogBrowser
373 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
374 #endif
375 [self _fixScroller];
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 {
383 [page setDelegate:delegate_];
384 [self setBackButtonTitle:title_];
385 [book_ pushPage:page];
386 }
387
388 - (void) _pushPage {
389 if (pushed_)
390 return;
391 [self autorelease];
392 pushed_ = true;
393 [book_ pushPage:self];
394 }
395
396 - (void) swapPage:(RVPage *)page {
397 [page setDelegate:delegate_];
398 if (pushed_)
399 [book_ swapPage:page];
400 else
401 [book_ pushPage:page];
402 }
403
404 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
405 #if LogBrowser
406 NSLog(@"getSpecial:%@", url);
407 #endif
408
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)
424 if (swap)
425 [self swapPage:page];
426 else
427 [self pushPage:page];
428 return true;
429 }
430
431 - (void) webViewShow:(WebView *)sender {
432 /* XXX: this is where I cry myself to sleep */
433 }
434
435 - (bool) _allowJavaScriptPanel {
436 return true;
437 }
438
439 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
440 if (![self _allowJavaScriptPanel])
441 return;
442
443 [self retain];
444
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 {
458 if (![self _allowJavaScriptPanel])
459 return NO;
460
461 UIActionSheet *sheet = [[[UIActionSheet alloc]
462 initWithTitle:nil
463 buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
464 defaultButtonIndex:0
465 delegate:indirect_
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
483 - (void) setAutoPopup:(BOOL)popup {
484 popup_ = popup;
485 }
486
487 - (void) setPopupHook:(id)function {
488 if (closer_ != nil)
489 [closer_ autorelease];
490 closer_ = function == nil ? nil : [function retain];
491 }
492
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];
505
506 [self reloadButtons];
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];
521
522 [self reloadButtons];
523 }
524
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
538 - (void) webViewClose:(WebView *)sender {
539 [book_ close];
540 }
541
542 - (void) close {
543 [book_ close];
544 }
545
546 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
547 [window setValue:cydia_ forKey:@"cydia"];
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 {
555 #if LogBrowser
556 NSLog(@"nwa:%@", name);
557 #endif
558
559 if (NSURL *url = [request URL]) {
560 if (name == nil) unknown: {
561 if (![self getSpecial:url swap:NO]) {
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"])
568 [delegate_ openURL:url];
569 else if ([name isEqualToString:@"_popup"]) {
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_];
576
577 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
578 if (page == nil) {
579 /* XXX: call createWebViewWithRequest instead? */
580
581 [self setBackButtonTitle:title_];
582
583 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
584 [browser loadURL:url];
585 page = browser;
586 }
587
588 [book setDelegate:delegate_];
589 [page setDelegate:delegate_];
590
591 [book setPage:page];
592 [book_ pushBook:book];
593 }
594 } else goto unknown;
595
596 [listener ignore];
597 } else use:
598 [listener use];
599 }
600
601 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
602 if ([WebView canShowMIMEType:type])
603 [listener use];
604 else {
605 // XXX: handle more mime types!
606 [listener ignore];
607
608 WebView *webview([webview_ webView]);
609 if (frame == [webview mainFrame])
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: {
623 if (!error_ && [frame parentFrame] == nil) {
624 if (request_ != nil)
625 [request_ autorelease];
626 request_ = [request retain];
627 #if LogBrowser
628 NSLog(@"dpn:%@", request_);
629 #endif
630 }
631
632 [listener use];
633
634 WebView *webview([webview_ webView]);
635 if (frame == [webview mainFrame])
636 [self _pushPage];
637 return;
638 }
639 #if LogBrowser
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]) {
656 #if LogBrowser
657 NSLog(@"itms#%@#%u#%@", url, store, itms);
658 #endif
659
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
681 if ([self getSpecial:url swap:YES])
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
695 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
696 NSString *context([sheet context]);
697
698 if ([context isEqualToString:@"alert"]) {
699 [self autorelease];
700 [sheet dismiss];
701 } else if ([context isEqualToString:@"confirm"]) {
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 {
799 return [self _addHeadersToRequest:request];
800 }
801
802 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
803 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
804 #if LogBrowser
805 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
806 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
807 #endif
808
809 NSNumber *value([features objectForKey:@"width"]);
810 float width(value == nil ? 0 : [value floatValue]);
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
817 if (features != nil && popup_) {
818 [book setDelegate:delegate_];
819 [book setHook:indirect_];
820 [browser setDelegate:delegate_];
821
822 [browser loadRequest:request];
823
824 [book setPage:browser];
825 [book_ pushBook:book];
826 } else if (request == nil) {
827 [self setBackButtonTitle:title_];
828 [browser setDelegate:delegate_];
829 [browser retain];
830 } else {
831 [self pushPage:browser];
832 [browser loadRequest:request];
833 }
834
835 return [browser webView];
836 }
837
838 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
839 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
840 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
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
855 [webview_ resignFirstResponder];
856
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
881 if (closer_ != nil) {
882 [closer_ release];
883 closer_ = nil;
884 }
885
886 [book_ reloadTitleForPage:self];
887
888 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
889 [scroller_ setZoomScale:1 duration:0];
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
903 - (bool) isLoading {
904 return loading_;
905 }
906
907 - (void) reloadButtons {
908 if ([self isLoading])
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 {
928 [self _pushPage];
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"])) {
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 }
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;
987 if (reloading_)
988 return;
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 ]]];
995
996 error_ = true;
997 }
998
999 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1000 #if LogBrowser || ForSaurik
1001 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1002 #endif
1003 }
1004
1005 - (void) setViewportWidth:(float)width {
1006 width_ = width ? width != 0 : [[self class] defaultWidth];
1007 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1008 }
1009
1010 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
1011 if ((self = [super initWithBook:book]) != nil) {
1012 loading_ = false;
1013 popup_ = false;
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];
1066 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1067 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1068 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1069
1070 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1071
1072 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
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];
1085 [webview_ setAllowsImageSheet:YES];
1086 [webview _setUsesLoaderCache:YES];
1087
1088 [webview setGroupName:@"CydiaGroup"];
1089 [webview _setLayoutInterval:0];
1090 }
1091
1092 [self setViewportWidth:width];
1093
1094 [webview_ setDelegate:self];
1095 [webview_ setGestureDelegate:self];
1096 [webview_ setFormEditingDelegate:self];
1097 [webview_ setInteractionDelegate:self];
1098
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]
1111 ];
1112
1113 if (Product_ != nil)
1114 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
1115 if (Build_ != nil)
1116 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1117 if (Safari_ != nil)
1118 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
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];
1126
1127 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1128 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
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
1141 - (id) initWithBook:(RVBook *)book {
1142 return [self initWithBook:book forWidth:0];
1143 }
1144
1145 - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1146 [webview_ redrawScaledDocument];
1147 }
1148
1149 - (void) callFunction:(WebScriptObject *)function {
1150 WebView *webview([webview_ webView]);
1151 WebFrame *frame([webview mainFrame]);
1152
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());
1156
1157 bool no;
1158 if (settings == NULL)
1159 no = 0;
1160 else {
1161 no = settings->JavaScriptCanOpenWindowsAutomatically();
1162 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1163 }
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_];
1189 }
1190
1191 - (id) _rightButtonTitle {
1192 return @"Reload";
1193 }
1194
1195 - (id) rightButtonTitle {
1196 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
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
1235 + (float) defaultWidth {
1236 return 980;
1237 }
1238
1239 @end