]> git.saurik.com Git - cydia.git/blob - UICaboodle/BrowserView.m
That was at least interesting.
[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 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
571 [book setHook:indirect_];
572
573 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
574 if (page == nil) {
575 /* XXX: call createWebViewWithRequest instead? */
576
577 [self setBackButtonTitle:title_];
578
579 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
580 [browser loadURL:url];
581 page = browser;
582 }
583
584 [book setDelegate:delegate_];
585 [page setDelegate:delegate_];
586
587 [book setPage:page];
588 [book_ pushBook:book];
589 } else goto unknown;
590
591 [listener ignore];
592 } else use:
593 [listener use];
594 }
595
596 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
597 if ([WebView canShowMIMEType:type])
598 [listener use];
599 else {
600 // XXX: handle more mime types!
601 [listener ignore];
602
603 WebView *webview([webview_ webView]);
604 if (frame == [webview mainFrame])
605 [UIApp openURL:[request URL]];
606 }
607 }
608
609 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
610 if (request == nil) ignore: {
611 [listener ignore];
612 return;
613 }
614
615 NSURL *url([request URL]);
616
617 if (url == nil) use: {
618 if (!error_ && [frame parentFrame] == nil) {
619 if (request_ != nil)
620 [request_ autorelease];
621 request_ = [request retain];
622 #if LogBrowser
623 NSLog(@"dpn:%@", request_);
624 #endif
625 }
626
627 [listener use];
628
629 WebView *webview([webview_ webView]);
630 if (frame == [webview mainFrame])
631 [self _pushPage];
632 return;
633 }
634 #if LogBrowser
635 else NSLog(@"nav:%@:%@", url, [action description]);
636 #endif
637
638 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
639
640 if (
641 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
642 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
643 ) {
644 open:
645 [UIApp openURL:url];
646 goto ignore;
647 }
648
649 int store(_not(int));
650 if (NSURL *itms = [url itmsURL:&store]) {
651 #if LogBrowser
652 NSLog(@"itms#%@#%u#%@", url, store, itms);
653 #endif
654
655 if (
656 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
657 store == 2 && [capability containsObject:@"com.apple.AppStore"]
658 ) {
659 url = itms;
660 goto open;
661 }
662 }
663
664 NSString *scheme([[url scheme] lowercaseString]);
665
666 if ([scheme isEqualToString:@"tel"]) {
667 // XXX: intelligence
668 goto open;
669 }
670
671 if ([scheme isEqualToString:@"mailto"]) {
672 [delegate_ openMailToURL:url];
673 goto ignore;
674 }
675
676 if ([self getSpecial:url swap:YES])
677 goto ignore;
678 else if ([WebView _canHandleRequest:request])
679 goto use;
680 else if ([url isSpringboardHandledURL])
681 goto open;
682 else
683 goto use;
684 }
685
686 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
687 //lprintf("Status:%s\n", [text UTF8String]);
688 }
689
690 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
691 NSString *context([sheet context]);
692
693 if ([context isEqualToString:@"alert"]) {
694 [self autorelease];
695 [sheet dismiss];
696 } else if ([context isEqualToString:@"confirm"]) {
697 switch (button) {
698 case 1:
699 confirm_ = [NSNumber numberWithBool:YES];
700 break;
701
702 case 2:
703 confirm_ = [NSNumber numberWithBool:NO];
704 break;
705 }
706
707 [sheet dismiss];
708 } else if ([context isEqualToString:@"challenge"]) {
709 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
710
711 switch (button) {
712 case 1: {
713 NSString *username([[sheet textFieldAtIndex:0] text]);
714 NSString *password([[sheet textFieldAtIndex:1] text]);
715
716 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
717
718 [sender useCredential:credential forAuthenticationChallenge:challenge_];
719 } break;
720
721 case 2:
722 [sender cancelAuthenticationChallenge:challenge_];
723 break;
724
725 default:
726 _assert(false);
727 }
728
729 [challenge_ release];
730 challenge_ = nil;
731
732 [sheet dismiss];
733 } else if ([context isEqualToString:@"submit"]) {
734 switch (button) {
735 case 1:
736 break;
737
738 case 2:
739 if (request_ != nil)
740 [webview_ loadRequest:request_];
741 break;
742
743 default:
744 _assert(false);
745 }
746
747 [sheet dismiss];
748 }
749 }
750
751 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
752 challenge_ = [challenge retain];
753
754 NSURLProtectionSpace *space([challenge protectionSpace]);
755 NSString *realm([space realm]);
756 if (realm == nil)
757 realm = @"";
758
759 UIActionSheet *sheet = [[[UIActionSheet alloc]
760 initWithTitle:realm
761 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
762 defaultButtonIndex:0
763 delegate:self
764 context:@"challenge"
765 ] autorelease];
766
767 [sheet setNumberOfRows:1];
768
769 [sheet addTextFieldWithValue:@"" label:@"username"];
770 [sheet addTextFieldWithValue:@"" label:@"password"];
771
772 UITextField *username([sheet textFieldAtIndex:0]); {
773 UITextInputTraits *traits([username textInputTraits]);
774 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
775 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
776 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
777 [traits setReturnKeyType:UIReturnKeyNext];
778 }
779
780 UITextField *password([sheet textFieldAtIndex:1]); {
781 UITextInputTraits *traits([password textInputTraits]);
782 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
783 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
784 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
785 // XXX: UIReturnKeyDone
786 [traits setReturnKeyType:UIReturnKeyNext];
787 [traits setSecureTextEntry:YES];
788 }
789
790 [sheet popupAlertAnimated:YES];
791 }
792
793 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
794 return [self _addHeadersToRequest:request];
795 }
796
797 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
798 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
799 #if LogBrowser
800 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
801 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
802 #endif
803
804 NSNumber *value([features objectForKey:@"width"]);
805 float width(value == nil ? 0 : [value floatValue]);
806
807 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
808
809 /* XXX: deal with cydia:// pages */
810 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
811
812 if (features != nil && popup_) {
813 [book setDelegate:delegate_];
814 [book setHook:indirect_];
815 [browser setDelegate:delegate_];
816
817 [browser loadRequest:request];
818
819 [book setPage:browser];
820 [book_ pushBook:book];
821 } else if (request == nil) {
822 [self setBackButtonTitle:title_];
823 [browser setDelegate:delegate_];
824 [browser retain];
825 } else {
826 [self pushPage:browser];
827 [browser loadRequest:request];
828 }
829
830 return [browser webView];
831 }
832
833 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
834 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
835 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
836 }
837
838 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
839 if ([frame parentFrame] != nil)
840 return;
841
842 title_ = [title retain];
843 [book_ reloadTitleForPage:self];
844 }
845
846 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
847 if ([frame parentFrame] != nil)
848 return;
849
850 [webview_ resignFirstResponder];
851
852 reloading_ = false;
853 loading_ = true;
854 [self reloadButtons];
855
856 if (title_ != nil) {
857 [title_ release];
858 title_ = nil;
859 }
860
861 if (button_ != nil) {
862 [button_ release];
863 button_ = nil;
864 }
865
866 if (style_ != nil) {
867 [style_ release];
868 style_ = nil;
869 }
870
871 if (function_ != nil) {
872 [function_ release];
873 function_ = nil;
874 }
875
876 if (closer_ != nil) {
877 [closer_ release];
878 closer_ = nil;
879 }
880
881 [book_ reloadTitleForPage:self];
882
883 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
884 [scroller_ setZoomScale:1 duration:0];
885
886 CGRect webrect = [scroller_ bounds];
887 webrect.size.height = 0;
888 [webview_ setFrame:webrect];
889 }
890
891 - (void) _finishLoading {
892 if (!reloading_) {
893 loading_ = false;
894 [self reloadButtons];
895 }
896 }
897
898 - (bool) isLoading {
899 return loading_;
900 }
901
902 - (void) reloadButtons {
903 if ([self isLoading])
904 [indicator_ startAnimation];
905 else
906 [indicator_ stopAnimation];
907 [super reloadButtons];
908 }
909
910 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
911 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
912 }
913
914 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
915 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
916 }
917
918 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
919 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
920 }
921
922 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
923 [self _pushPage];
924 return [webview_ webView:sender didCommitLoadForFrame:frame];
925 }
926
927 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
928 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
929 }
930
931 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
932 if ([frame parentFrame] == nil) {
933 [self _finishLoading];
934
935 if (DOMDocument *document = [frame DOMDocument])
936 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
937 for (DOMHTMLBodyElement *body in bodies) {
938 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
939
940 bool colored(false);
941
942 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
943 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
944 DOMRGBColor *rgb([color getRGBColorValue]);
945
946 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
947 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
948 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
949 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
950
951 UIColor *uic(nil);
952
953 if (red == 0xc7 && green == 0xce && blue == 0xd5)
954 uic = [UIColor pinStripeColor];
955 else if (alpha != 0)
956 uic = [UIColor
957 colorWithRed:(red / 255)
958 green:(green / 255)
959 blue:(blue / 255)
960 alpha:alpha
961 ];
962
963 if (uic != nil) {
964 colored = true;
965 [scroller_ setBackgroundColor:uic];
966 }
967 }
968 }
969
970 if (!colored)
971 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
972 break;
973 }
974 }
975
976 return [webview_ webView:sender didFinishLoadForFrame:frame];
977 }
978
979 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
980 if ([frame parentFrame] != nil)
981 return;
982 if (reloading_)
983 return;
984 [self _finishLoading];
985
986 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
987 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
988 [[error localizedDescription] stringByAddingPercentEscapes]
989 ]]];
990
991 error_ = true;
992 }
993
994 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
995 #if LogBrowser || ForSaurik
996 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
997 #endif
998 }
999
1000 - (void) setViewportWidth:(float)width {
1001 width_ = width ? width != 0 : [[self class] defaultWidth];
1002 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1003 }
1004
1005 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
1006 if ((self = [super initWithBook:book]) != nil) {
1007 loading_ = false;
1008 popup_ = false;
1009
1010 struct CGRect bounds = [self bounds];
1011
1012 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
1013 [self addSubview:scroller_];
1014
1015 [scroller_ setShowBackgroundShadow:NO];
1016 [scroller_ setFixedBackgroundPattern:YES];
1017 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1018
1019 [scroller_ setScrollingEnabled:YES];
1020 [scroller_ setAdjustForContentSizeChange:YES];
1021 [scroller_ setClipsSubviews:YES];
1022 [scroller_ setAllowsRubberBanding:YES];
1023 [scroller_ setScrollDecelerationFactor:0.99];
1024 [scroller_ setDelegate:self];
1025
1026 CGRect webrect = [scroller_ bounds];
1027 webrect.size.height = 0;
1028
1029 WebView *webview;
1030
1031 #if RecycleWebViews
1032 webview_ = [Documents_ lastObject];
1033 if (webview_ != nil) {
1034 webview_ = [webview_ retain];
1035 webview = [webview_ webView];
1036 [Documents_ removeLastObject];
1037 [webview_ setFrame:webrect];
1038 } else {
1039 #else
1040 if (true) {
1041 #endif
1042 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
1043 webview = [webview_ webView];
1044
1045 // XXX: this is terribly (too?) expensive
1046 //[webview_ setDrawsBackground:NO];
1047 [webview setPreferencesIdentifier:@"Cydia"];
1048
1049 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1050
1051 [webview_ setAllowsMessaging:YES];
1052
1053 [webview_ setTilingEnabled:YES];
1054 [webview_ setDrawsGrid:NO];
1055 [webview_ setLogsTilingChanges:NO];
1056 [webview_ setTileMinificationFilter:kCAFilterNearest];
1057 [webview_ setDetectsPhoneNumbers:NO];
1058 [webview_ setAutoresizes:YES];
1059
1060 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1061 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1062 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1063 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1064
1065 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1066
1067 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1068 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1069 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1070
1071 [webview_ _setDocumentType:0x4];
1072
1073 [webview_ setZoomsFocusedFormControl:YES];
1074 [webview_ setContentsPosition:7];
1075 [webview_ setEnabledGestures:0xa];
1076 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1077 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1078
1079 [webview_ setSmoothsFonts:YES];
1080 [webview_ setAllowsImageSheet:YES];
1081 [webview _setUsesLoaderCache:YES];
1082
1083 [webview setGroupName:@"CydiaGroup"];
1084 [webview _setLayoutInterval:0];
1085 }
1086
1087 [self setViewportWidth:width];
1088
1089 [webview_ setDelegate:self];
1090 [webview_ setGestureDelegate:self];
1091 [webview_ setFormEditingDelegate:self];
1092 [webview_ setInteractionDelegate:self];
1093
1094 [scroller_ addSubview:webview_];
1095
1096 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1097
1098 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1099 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1100 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1101
1102 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1103 NSString *application = package == nil ? @"Cydia" : [NSString
1104 stringWithFormat:@"Cydia/%@",
1105 [package installed]
1106 ];
1107
1108 if (Product_ != nil)
1109 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
1110 if (Build_ != nil)
1111 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1112 if (Safari_ != nil)
1113 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
1114
1115 /* XXX: lookup application directory? */
1116 /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"])
1117 if (NSString *version = [safari objectForKey:@"SafariProductVersion"])
1118 application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/
1119
1120 [webview setApplicationNameForUserAgent:application];
1121
1122 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1123 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
1124
1125 [webview setFrameLoadDelegate:self];
1126 [webview setResourceLoadDelegate:indirect_];
1127 [webview setUIDelegate:self];
1128 [webview setScriptDebugDelegate:self];
1129 [webview setPolicyDelegate:self];
1130
1131 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1132 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1133 } return self;
1134 }
1135
1136 - (id) initWithBook:(RVBook *)book {
1137 return [self initWithBook:book forWidth:0];
1138 }
1139
1140 - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
1141 [webview_ redrawScaledDocument];
1142 }
1143
1144 - (void) callFunction:(WebScriptObject *)function {
1145 WebView *webview([webview_ webView]);
1146 WebFrame *frame([webview mainFrame]);
1147
1148 id _private(MSHookIvar<id>(webview, "_private"));
1149 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1150 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1151
1152 bool no;
1153 if (settings == NULL)
1154 no = 0;
1155 else {
1156 no = settings->JavaScriptCanOpenWindowsAutomatically();
1157 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1158 }
1159
1160 [delegate_ clearFirstResponder];
1161 JSObjectRef object([function JSObject]);
1162 JSGlobalContextRef context([frame globalContext]);
1163 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1164
1165 if (settings != NULL)
1166 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1167 }
1168
1169 - (void) didCloseBook:(RVBook *)book {
1170 if (closer_ != nil)
1171 [self callFunction:closer_];
1172 }
1173
1174 - (void) __rightButtonClicked {
1175 reloading_ = true;
1176 [self reloadURL];
1177 }
1178
1179 - (void) _rightButtonClicked {
1180 if (function_ == nil)
1181 [self __rightButtonClicked];
1182 else
1183 [self callFunction:function_];
1184 }
1185
1186 - (id) _rightButtonTitle {
1187 return @"Reload";
1188 }
1189
1190 - (id) rightButtonTitle {
1191 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
1192 }
1193
1194 - (UINavigationButtonStyle) rightButtonStyle {
1195 if (style_ == nil) normal:
1196 return UINavigationButtonStyleNormal;
1197 else if ([style_ isEqualToString:@"Normal"])
1198 return UINavigationButtonStyleNormal;
1199 else if ([style_ isEqualToString:@"Back"])
1200 return UINavigationButtonStyleBack;
1201 else if ([style_ isEqualToString:@"Highlighted"])
1202 return UINavigationButtonStyleHighlighted;
1203 else if ([style_ isEqualToString:@"Destructive"])
1204 return UINavigationButtonStyleDestructive;
1205 else goto normal;
1206 }
1207
1208 - (NSString *) title {
1209 return title_ == nil ? @"Loading" : title_;
1210 }
1211
1212 - (NSString *) backButtonTitle {
1213 return @"Browser";
1214 }
1215
1216 - (void) setPageActive:(BOOL)active {
1217 if (!active)
1218 [indicator_ removeFromSuperview];
1219 else
1220 [[book_ navigationBar] addSubview:indicator_];
1221 }
1222
1223 - (void) resetViewAnimated:(BOOL)animated {
1224 }
1225
1226 - (void) setPushed:(bool)pushed {
1227 pushed_ = pushed;
1228 }
1229
1230 + (float) defaultWidth {
1231 return 980;
1232 }
1233
1234 @end