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