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