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