]> git.saurik.com Git - cydia.git/blob - UICaboodle/BrowserView.m
10cc19376a28f83930195cf501ccc7670e7aa06e
[cydia.git] / UICaboodle / BrowserView.m
1 #include <BrowserView.h>
2
3 #include <WebCore/WebCoreThread.h>
4
5 /* Indirect Delegate {{{ */
6 @interface IndirectDelegate : NSObject {
7 _transient volatile id delegate_;
8 }
9
10 - (void) setDelegate:(id)delegate;
11 - (id) initWithDelegate:(id)delegate;
12 @end
13
14 @implementation IndirectDelegate
15
16 - (void) setDelegate:(id)delegate {
17 delegate_ = delegate;
18 }
19
20 - (id) initWithDelegate:(id)delegate {
21 delegate_ = delegate;
22 return self;
23 }
24
25 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
26 if (delegate_ != nil)
27 return [delegate_ webView:sender didClearWindowObject:window forFrame:frame];
28 }
29
30 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
31 if (delegate_ != nil)
32 return [delegate_ webView:sender didCommitLoadForFrame:frame];
33 }
34
35 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
36 if (delegate_ != nil)
37 return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame];
38 }
39
40 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
41 if (delegate_ != nil)
42 return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame];
43 }
44
45 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
46 if (delegate_ != nil)
47 return [delegate_ webView:sender didFinishLoadForFrame:frame];
48 }
49
50 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
51 if (delegate_ != nil)
52 return [delegate_ webView:sender didReceiveTitle:title forFrame:frame];
53 }
54
55 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
56 if (delegate_ != nil)
57 return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame];
58 }
59
60 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
61 if (delegate_ != nil)
62 return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
63 }
64
65 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
66 if (delegate_ != nil)
67 return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source];
68 return nil;
69 }
70
71 - (IMP) methodForSelector:(SEL)sel {
72 if (IMP method = [super methodForSelector:sel])
73 return method;
74 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
75 return NULL;
76 }
77
78 - (BOOL) respondsToSelector:(SEL)sel {
79 if ([super respondsToSelector:sel])
80 return YES;
81 // XXX: WebThreadCreateNSInvocation returns nil
82 //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
83 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
84 }
85
86 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
87 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
88 return method;
89 //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
90 if (delegate_ != nil)
91 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
92 return sig;
93 // XXX: I fucking hate Apple so very very bad
94 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
95 }
96
97 - (void) forwardInvocation:(NSInvocation *)inv {
98 SEL sel = [inv selector];
99 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
100 [inv invokeWithTarget:delegate_];
101 }
102
103 @end
104 /* }}} */
105
106 @interface WebView (Cydia)
107 - (void) setScriptDebugDelegate:(id)delegate;
108 - (void) _setFormDelegate:(id)delegate;
109 - (void) _setUIKitDelegate:(id)delegate;
110 - (void) setWebMailDelegate:(id)delegate;
111 - (void) _setLayoutInterval:(float)interval;
112 @end
113
114 @interface WebScriptObject (Cydia)
115
116 - (unsigned) count;
117 - (id) objectAtIndex:(unsigned)index;
118
119 @end
120
121 @implementation WebScriptObject (Cydia)
122
123 - (unsigned) count {
124 id length([self valueForKey:@"length"]);
125 if ([length respondsToSelector:@selector(intValue)])
126 return [length intValue];
127 else
128 return 0;
129 }
130
131 - (id) objectAtIndex:(unsigned)index {
132 return [self webScriptValueAtIndex:index];
133 }
134
135 @end
136
137 /* Web Scripting {{{ */
138 @interface CydiaObject : NSObject {
139 id indirect_;
140 }
141
142 - (id) initWithDelegate:(IndirectDelegate *)indirect;
143 @end
144
145 @implementation CydiaObject
146
147 - (void) dealloc {
148 [indirect_ release];
149 [super dealloc];
150 }
151
152 - (id) initWithDelegate:(IndirectDelegate *)indirect {
153 if ((self = [super init]) != nil) {
154 indirect_ = [indirect retain];
155 } return self;
156 }
157
158 + (NSArray *) _attributeKeys {
159 return [NSArray arrayWithObjects:@"device", nil];
160 }
161
162 - (NSArray *) attributeKeys {
163 return [[self class] _attributeKeys];
164 }
165
166 + (BOOL) isKeyExcludedFromWebScript:(const char *)name {
167 return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name];
168 }
169
170 - (NSString *) device {
171 return [[UIDevice currentDevice] uniqueIdentifier];
172 }
173
174 + (NSString *) webScriptNameForSelector:(SEL)selector {
175 if (selector == @selector(close))
176 return @"close";
177 else if (selector == @selector(getPackageById:))
178 return @"getPackageById";
179 else if (selector == @selector(setAutoPopup:))
180 return @"setAutoPopup";
181 else if (selector == @selector(setButtonImage:withStyle:toFunction:))
182 return @"setButtonImage";
183 else if (selector == @selector(setButtonTitle:withStyle:toFunction:))
184 return @"setButtonTitle";
185 else if (selector == @selector(setFinishHook:))
186 return @"setFinishHook";
187 else if (selector == @selector(setPopupHook:))
188 return @"setPopupHook";
189 else if (selector == @selector(setSpecial:))
190 return @"setSpecial";
191 else if (selector == @selector(setViewportWidth:))
192 return @"setViewportWidth";
193 else if (selector == @selector(supports:))
194 return @"supports";
195 else if (selector == @selector(stringWithFormat:arguments:))
196 return @"format";
197 else if (selector == @selector(localizedStringForKey:value:table:))
198 return @"localize";
199 else if (selector == @selector(du:))
200 return @"du";
201 else if (selector == @selector(statfs:))
202 return @"statfs";
203 else
204 return nil;
205 }
206
207 + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
208 return [self webScriptNameForSelector:selector] == nil;
209 }
210
211 - (BOOL) supports:(NSString *)feature {
212 return [feature isEqualToString:@"window.open"];
213 }
214
215 - (Package *) getPackageById:(NSString *)id {
216 return [[Database sharedInstance] packageWithName:id];
217 }
218
219 - (NSArray *) statfs:(NSString *)path {
220 struct statfs stat;
221
222 if (path == nil || statfs([path UTF8String], &stat) == -1)
223 return nil;
224
225 return [NSArray arrayWithObjects:
226 [NSNumber numberWithUnsignedLong:stat.f_bsize],
227 [NSNumber numberWithUnsignedLong:stat.f_blocks],
228 [NSNumber numberWithUnsignedLong:stat.f_bfree],
229 nil];
230 }
231
232 - (NSNumber *) du:(NSString *)path {
233 NSNumber *value(nil);
234
235 int fds[2];
236 _assert(pipe(fds) != -1);
237
238 pid_t pid(ExecFork());
239 if (pid == 0) {
240 _assert(dup2(fds[1], 1) != -1);
241 _assert(close(fds[0]) != -1);
242 _assert(close(fds[1]) != -1);
243 execlp("du", "du", "-s", [path UTF8String], NULL);
244 exit(1);
245 _assert(false);
246 }
247
248 _assert(close(fds[1]) != -1);
249
250 if (FILE *du = fdopen(fds[0], "r")) {
251 char line[1024];
252 while (fgets(line, sizeof(line), du) != NULL) {
253 size_t length(strlen(line));
254 while (length != 0 && line[length - 1] == '\n')
255 line[--length] = '\0';
256 if (char *tab = strchr(line, '\t')) {
257 *tab = '\0';
258 value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)];
259 }
260 }
261
262 fclose(du);
263 } else _assert(close(fds[0]));
264
265 int status;
266 wait:
267 if (waitpid(pid, &status, 0) == -1)
268 if (errno == EINTR)
269 goto wait;
270 else _assert(false);
271
272 return value;
273 }
274
275 - (void) close {
276 [indirect_ close];
277 }
278
279 - (void) setAutoPopup:(BOOL)popup {
280 [indirect_ setAutoPopup:popup];
281 }
282
283 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
284 [indirect_ setButtonImage:button withStyle:style toFunction:function];
285 }
286
287 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
288 [indirect_ setButtonTitle:button withStyle:style toFunction:function];
289 }
290
291 - (void) setSpecial:(id)function {
292 [indirect_ setSpecial:function];
293 }
294
295 - (void) setFinishHook:(id)function {
296 [indirect_ setFinishHook:function];
297 }
298
299 - (void) setPopupHook:(id)function {
300 [indirect_ setPopupHook:function];
301 }
302
303 - (void) setViewportWidth:(float)width {
304 [indirect_ setViewportWidth:width];
305 }
306
307 - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments {
308 //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]);
309 unsigned count([arguments count]);
310 id values[count];
311 for (unsigned i(0); i != count; ++i)
312 values[i] = [arguments objectAtIndex:i];
313 return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease];
314 }
315
316 - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table {
317 if (reinterpret_cast<id>(table) == [WebUndefined undefined])
318 table = nil;
319 return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table];
320 }
321
322 @end
323 /* }}} */
324
325 @implementation BrowserView
326
327 #if ShowInternals
328 #include "Internals.h"
329 #endif
330
331 - (void) dealloc {
332 #if LogBrowser
333 NSLog(@"[BrowserView dealloc]");
334 #endif
335
336 if (challenge_ != nil)
337 [challenge_ release];
338
339 WebThreadLock();
340
341 WebView *webview = [webview_ webView];
342 [webview setFrameLoadDelegate:nil];
343 [webview setResourceLoadDelegate:nil];
344 [webview setUIDelegate:nil];
345 [webview setScriptDebugDelegate:nil];
346 [webview setPolicyDelegate:nil];
347
348 [webview setDownloadDelegate:nil];
349
350 /* XXX: these are set by UIWebDocumentView
351 [webview _setFormDelegate:nil];
352 [webview _setUIKitDelegate:nil];
353 [webview setEditingDelegate:nil];*/
354
355 /* XXX: no one sets this, ever
356 [webview setWebMailDelegate:nil];*/
357
358 [webview_ setDelegate:nil];
359 [webview_ setGestureDelegate:nil];
360 [webview_ setFormEditingDelegate:nil];
361 [webview_ setInteractionDelegate:nil];
362
363 [indirect_ setDelegate:nil];
364
365 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
366
367 [webview close];
368
369 #if RecycleWebViews
370 [webview_ removeFromSuperview];
371 [Documents_ addObject:[webview_ autorelease]];
372 #else
373 [webview_ release];
374 #endif
375
376 [indirect_ release];
377
378 WebThreadUnlock();
379
380 [cydia_ release];
381
382 [scroller_ setDelegate:nil];
383
384 if (button_ != nil)
385 [button_ release];
386 if (style_ != nil)
387 [style_ release];
388 if (function_ != nil)
389 [function_ release];
390 if (finish_ != nil)
391 [finish_ release];
392 if (closer_ != nil)
393 [closer_ release];
394 if (special_ != nil)
395 [special_ release];
396
397 [scroller_ release];
398 [indicator_ release];
399 if (confirm_ != nil)
400 [confirm_ release];
401 if (title_ != nil)
402 [title_ release];
403 [super dealloc];
404 }
405
406 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
407 [self loadRequest:[NSURLRequest
408 requestWithURL:url
409 cachePolicy:policy
410 timeoutInterval:30.0
411 ]];
412 }
413
414 - (void) loadURL:(NSURL *)url {
415 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
416 }
417
418 - (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
419 NSMutableURLRequest *copy = [request mutableCopy];
420
421 if (Machine_ != NULL)
422 [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
423 if (UniqueID_ != nil)
424 [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
425
426 if (Role_ != nil)
427 [copy setValue:Role_ forHTTPHeaderField:@"X-Role"];
428
429 return copy;
430 }
431
432 - (void) loadRequest:(NSURLRequest *)request {
433 pushed_ = true;
434 error_ = false;
435
436 WebThreadLock();
437 [webview_ loadRequest:request];
438 WebThreadUnlock();
439 }
440
441 - (void) reloadURL {
442 if (request_ == nil)
443 return;
444
445 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
446 [self loadRequest:request_];
447 else {
448 UIActionSheet *sheet = [[[UIActionSheet alloc]
449 initWithTitle:CYLocalize("RESUBMIT_FORM")
450 buttons:[NSArray arrayWithObjects:CYLocalize("CANCEL"), CYLocalize("SUBMIT"), nil]
451 defaultButtonIndex:0
452 delegate:self
453 context:@"submit"
454 ] autorelease];
455
456 [sheet setNumberOfRows:1];
457 [sheet popupAlertAnimated:YES];
458 }
459 }
460
461 - (WebView *) webView {
462 return [webview_ webView];
463 }
464
465 - (UIWebDocumentView *) documentView {
466 return webview_;
467 }
468
469 /* XXX: WebThreadLock? */
470 - (void) _fixScroller:(CGRect)bounds {
471 float extra;
472 if (!editing_)
473 extra = 0;
474 else {
475 UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]);
476 CGRect peripheral([assistant peripheralFrame]);
477 #if LogBrowser
478 NSLog(@"per:%f", peripheral.size.height);
479 #endif
480 extra = peripheral.size.height;
481 }
482
483 CGRect subrect([scroller_ frame]);
484 subrect.size.height -= extra;
485 [scroller_ setScrollerIndicatorSubrect:subrect];
486
487 NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height));
488 [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize];
489
490 CGSize size(size_);
491 size.height += extra;
492 [scroller_ setContentSize:size];
493
494 [scroller_ releaseRubberBandIfNecessary];
495 }
496
497 - (void) fixScroller {
498 CGRect bounds([webview_ documentBounds]);
499 #if TrackResize
500 NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
501 #endif
502 [self _fixScroller:bounds];
503 }
504
505 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
506 size_ = frame.size;
507 #if TrackResize
508 NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
509 #endif
510 [self _fixScroller:frame];
511 }
512
513 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
514 [self view:sender didSetFrame:frame];
515 }
516
517 - (void) pushPage:(RVPage *)page {
518 [page setDelegate:delegate_];
519 [self setBackButtonTitle:title_];
520 [book_ pushPage:page];
521 }
522
523 - (void) _pushPage {
524 if (pushed_)
525 return;
526 // WTR: [self autorelease];
527 pushed_ = true;
528 [book_ pushPage:self];
529 }
530
531 - (void) swapPage:(RVPage *)page {
532 [page setDelegate:delegate_];
533 if (pushed_)
534 [book_ swapPage:page];
535 else
536 [book_ pushPage:page];
537 }
538
539 - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap {
540 #if LogBrowser
541 NSLog(@"getSpecial:%@", url);
542 #endif
543
544 NSString *href([url absoluteString]);
545 NSString *scheme([[url scheme] lowercaseString]);
546
547 RVPage *page = nil;
548
549 if ([href hasPrefix:@"apptapp://package/"])
550 page = [delegate_ pageForPackage:[href substringFromIndex:18]];
551 else if ([scheme isEqualToString:@"cydia"]) {
552 page = [delegate_ pageForURL:url hasTag:NULL];
553 if (page == nil)
554 return false;
555 } else if (![scheme isEqualToString:@"apptapp"])
556 return false;
557
558 if (page != nil)
559 if (swap)
560 [self swapPage:page];
561 else
562 [self pushPage:page];
563 return true;
564 }
565
566 - (void) webViewShow:(WebView *)sender {
567 /* XXX: this is where I cry myself to sleep */
568 }
569
570 - (bool) _allowJavaScriptPanel {
571 return true;
572 }
573
574 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
575 if (![self _allowJavaScriptPanel])
576 return;
577 [self retain];
578
579 UIActionSheet *sheet = [[[UIActionSheet alloc]
580 initWithTitle:nil
581 buttons:[NSArray arrayWithObjects:CYLocalize("OK"), nil]
582 defaultButtonIndex:0
583 delegate:self
584 context:@"alert"
585 ] autorelease];
586
587 [sheet setBodyText:message];
588 [sheet popupAlertAnimated:YES];
589 }
590
591 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
592 if (![self _allowJavaScriptPanel])
593 return NO;
594 [self retain];
595
596 UIActionSheet *sheet = [[[UIActionSheet alloc]
597 initWithTitle:nil
598 buttons:[NSArray arrayWithObjects:CYLocalize("OK"), CYLocalize("Cancel"), nil]
599 defaultButtonIndex:0
600 delegate:indirect_
601 context:@"confirm"
602 ] autorelease];
603
604 [sheet setNumberOfRows:1];
605 [sheet setBodyText:message];
606 [sheet popupAlertAnimated:YES];
607
608 NSRunLoop *loop([NSRunLoop currentRunLoop]);
609 NSDate *future([NSDate distantFuture]);
610
611 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
612
613 NSNumber *confirm([confirm_ autorelease]);
614 confirm_ = nil;
615
616 [self autorelease];
617 return [confirm boolValue];
618 }
619
620 - (void) setAutoPopup:(BOOL)popup {
621 popup_ = popup;
622 }
623
624 - (void) setSpecial:(id)function {
625 if (special_ != nil)
626 [special_ autorelease];
627 special_ = function == nil ? nil : [function retain];
628 }
629
630 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
631 if (button_ != nil)
632 [button_ autorelease];
633 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
634
635 if (style_ != nil)
636 [style_ autorelease];
637 style_ = style == nil ? nil : [style retain];
638
639 if (function_ != nil)
640 [function_ autorelease];
641 function_ = function == nil ? nil : [function retain];
642
643 [self reloadButtons];
644 }
645
646 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
647 if (button_ != nil)
648 [button_ autorelease];
649 button_ = button == nil ? nil : [button retain];
650
651 if (style_ != nil)
652 [style_ autorelease];
653 style_ = style == nil ? nil : [style retain];
654
655 if (function_ != nil)
656 [function_ autorelease];
657 function_ = function == nil ? nil : [function retain];
658
659 [self reloadButtons];
660 }
661
662 - (void) setFinishHook:(id)function {
663 if (finish_ != nil)
664 [finish_ autorelease];
665 finish_ = function == nil ? nil : [function retain];
666 }
667
668 - (void) setPopupHook:(id)function {
669 if (closer_ != nil)
670 [closer_ autorelease];
671 closer_ = function == nil ? nil : [function retain];
672 }
673
674 - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element {
675 editing_ = true;
676 }
677
678 - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element {
679 [self fixScroller];
680 }
681
682 - (void) webViewDidEndEditingFormElements:(WebView *)sender {
683 editing_ = false;
684 [self fixScroller];
685 }
686
687 - (void) webViewClose:(WebView *)sender {
688 [book_ close];
689 }
690
691 - (void) close {
692 [book_ close];
693 }
694
695 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
696 [window setValue:cydia_ forKey:@"cydia"];
697 }
698
699 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
700 NSLog(@"err:%@", error);
701 }
702
703 - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
704 #if LogBrowser
705 NSLog(@"nwa:%@", name);
706 #endif
707
708 if (NSURL *url = [request URL]) {
709 if (name == nil) unknown: {
710 if (![self getSpecial:url swap:NO]) {
711 NSString *scheme([[url scheme] lowercaseString]);
712 if ([scheme isEqualToString:@"mailto"])
713 [delegate_ openMailToURL:url];
714 else goto use;
715 }
716 } else if ([name isEqualToString:@"_open"])
717 [delegate_ openURL:url];
718 else if ([name isEqualToString:@"_popup"]) {
719 NSString *scheme([[url scheme] lowercaseString]);
720 if ([scheme isEqualToString:@"mailto"])
721 [delegate_ openMailToURL:url];
722 else {
723 RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
724 [book setHook:indirect_];
725
726 RVPage *page([delegate_ pageForURL:url hasTag:NULL]);
727 if (page == nil) {
728 /* XXX: call createWebViewWithRequest instead? */
729
730 [self setBackButtonTitle:title_];
731
732 BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]);
733 [browser loadURL:url];
734 page = browser;
735 }
736
737 [book setDelegate:delegate_];
738 [page setDelegate:delegate_];
739
740 [book setPage:page];
741 [book_ pushBook:book];
742 }
743 } else goto unknown;
744
745 [listener ignore];
746 } else use:
747 [listener use];
748 }
749
750 - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
751 if ([WebView canShowMIMEType:type])
752 [listener use];
753 else {
754 // XXX: handle more mime types!
755 [listener ignore];
756
757 WebView *webview([webview_ webView]);
758 if (frame == [webview mainFrame])
759 [UIApp openURL:[request URL]];
760 }
761 }
762
763 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
764 if (request == nil) ignore: {
765 [listener ignore];
766 return;
767 }
768
769 NSURL *url([request URL]);
770
771 if (url == nil) use: {
772 if (!error_ && [frame parentFrame] == nil) {
773 if (request_ != nil)
774 [request_ autorelease];
775 request_ = [request retain];
776 #if LogBrowser
777 NSLog(@"dpn:%@", request_);
778 #endif
779 }
780
781 [listener use];
782
783 WebView *webview([webview_ webView]);
784 if (frame == [webview mainFrame])
785 [self _pushPage];
786 return;
787 }
788 #if LogBrowser
789 else NSLog(@"nav:%@:%@", url, [action description]);
790 #endif
791
792 const NSArray *capability;
793
794 #if 0 // XXX:3:GSSystemCopyCapability
795 capability = reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability));
796 #else
797 capability = nil;
798 #endif
799
800 if (capability != nil && (
801 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
802 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
803 )) {
804 open:
805 [UIApp openURL:url];
806 goto ignore;
807 }
808
809 int store(_not(int));
810 if (NSURL *itms = [url itmsURL:&store]) {
811 #if LogBrowser
812 NSLog(@"itms#%@#%u#%@", url, store, itms);
813 #endif
814
815 if (capability != nil && (
816 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
817 store == 2 && [capability containsObject:@"com.apple.AppStore"]
818 )) {
819 url = itms;
820 goto open;
821 }
822 }
823
824 NSString *scheme([[url scheme] lowercaseString]);
825
826 if ([scheme isEqualToString:@"tel"]) {
827 // XXX: intelligence
828 goto open;
829 }
830
831 if ([scheme isEqualToString:@"mailto"]) {
832 [delegate_ openMailToURL:url];
833 goto ignore;
834 }
835
836 if ([self getSpecial:url swap:YES])
837 goto ignore;
838 else if ([WebView _canHandleRequest:request])
839 goto use;
840 else if ([url isSpringboardHandledURL])
841 goto open;
842 else
843 goto use;
844 }
845
846 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
847 //lprintf("Status:%s\n", [text UTF8String]);
848 }
849
850 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
851 NSString *context([sheet context]);
852
853 if ([context isEqualToString:@"alert"]) {
854 [self autorelease];
855 [sheet dismiss];
856 } else if ([context isEqualToString:@"confirm"]) {
857 switch (button) {
858 case 1:
859 confirm_ = [NSNumber numberWithBool:YES];
860 break;
861
862 case 2:
863 confirm_ = [NSNumber numberWithBool:NO];
864 break;
865 }
866
867 [sheet dismiss];
868 } else if ([context isEqualToString:@"challenge"]) {
869 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
870
871 switch (button) {
872 case 1: {
873 NSString *username([[sheet textFieldAtIndex:0] text]);
874 NSString *password([[sheet textFieldAtIndex:1] text]);
875
876 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
877
878 [sender useCredential:credential forAuthenticationChallenge:challenge_];
879 } break;
880
881 case 2:
882 [sender cancelAuthenticationChallenge:challenge_];
883 break;
884
885 default:
886 _assert(false);
887 }
888
889 [challenge_ release];
890 challenge_ = nil;
891
892 [sheet dismiss];
893 } else if ([context isEqualToString:@"submit"]) {
894 switch (button) {
895 case 1:
896 break;
897
898 case 2:
899 if (request_ != nil) {
900 WebThreadLock();
901 [webview_ loadRequest:request_];
902 WebThreadUnlock();
903 }
904 break;
905
906 default:
907 _assert(false);
908 }
909
910 [sheet dismiss];
911 }
912 }
913
914 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
915 challenge_ = [challenge retain];
916
917 NSURLProtectionSpace *space([challenge protectionSpace]);
918 NSString *realm([space realm]);
919 if (realm == nil)
920 realm = @"";
921
922 UIActionSheet *sheet = [[[UIActionSheet alloc]
923 initWithTitle:realm
924 buttons:[NSArray arrayWithObjects:CYLocalize("LOGIN"), CYLocalize("CANCEL"), nil]
925 defaultButtonIndex:0
926 delegate:self
927 context:@"challenge"
928 ] autorelease];
929
930 [sheet setNumberOfRows:1];
931
932 [sheet addTextFieldWithValue:@"" label:CYLocalize("USERNAME")];
933 [sheet addTextFieldWithValue:@"" label:CYLocalize("PASSWORD")];
934
935 UITextField *username([sheet textFieldAtIndex:0]); {
936 UITextInputTraits *traits([username textInputTraits]);
937 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
938 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
939 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
940 [traits setReturnKeyType:UIReturnKeyNext];
941 }
942
943 UITextField *password([sheet textFieldAtIndex:1]); {
944 UITextInputTraits *traits([password textInputTraits]);
945 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
946 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
947 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
948 // XXX: UIReturnKeyDone
949 [traits setReturnKeyType:UIReturnKeyNext];
950 [traits setSecureTextEntry:YES];
951 }
952
953 [sheet popupAlertAnimated:YES];
954 }
955
956 - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source {
957 return [self _addHeadersToRequest:request];
958 }
959
960 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
961 //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture {
962 #if LogBrowser
963 NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]);
964 //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No");
965 #endif
966
967 NSNumber *value([features objectForKey:@"width"]);
968 float width(value == nil ? 0 : [value floatValue]);
969
970 RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]);
971
972 /* XXX: deal with cydia:// pages */
973 BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]);
974
975 if (features != nil && popup_) {
976 [book setDelegate:delegate_];
977 [book setHook:indirect_];
978 [browser setDelegate:delegate_];
979
980 [browser loadRequest:request];
981
982 [book setPage:browser];
983 [book_ pushBook:book];
984 } else if (request == nil) {
985 [self setBackButtonTitle:title_];
986 [browser setDelegate:delegate_];
987 [browser retain];
988 } else {
989 [self pushPage:browser];
990 [browser loadRequest:request];
991 }
992
993 return [browser webView];
994 }
995
996 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
997 return [self webView:sender createWebViewWithRequest:request windowFeatures:nil];
998 //return [self webView:sender createWebViewWithRequest:request userGesture:YES];
999 }
1000
1001 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
1002 if ([frame parentFrame] != nil)
1003 return;
1004
1005 title_ = [title retain];
1006 [book_ reloadTitleForPage:self];
1007 }
1008
1009 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
1010 if ([loading_ count] == 0)
1011 [self retain];
1012 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
1013
1014 if ([frame parentFrame] == nil) {
1015 [webview_ resignFirstResponder];
1016
1017 reloading_ = false;
1018
1019 if (title_ != nil) {
1020 [title_ release];
1021 title_ = nil;
1022 }
1023
1024 if (button_ != nil) {
1025 [button_ release];
1026 button_ = nil;
1027 }
1028
1029 if (style_ != nil) {
1030 [style_ release];
1031 style_ = nil;
1032 }
1033
1034 if (function_ != nil) {
1035 [function_ release];
1036 function_ = nil;
1037 }
1038
1039 if (finish_ != nil) {
1040 [finish_ release];
1041 finish_ = nil;
1042 }
1043
1044 if (closer_ != nil) {
1045 [closer_ release];
1046 closer_ = nil;
1047 }
1048
1049 if (special_ != nil) {
1050 [special_ release];
1051 special_ = nil;
1052 }
1053
1054 [book_ reloadTitleForPage:self];
1055
1056 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
1057
1058 if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)])
1059 [scroller_ setZoomScale:1 duration:0];
1060 else
1061 [scroller_ setZoomScale:1 animated:NO];
1062
1063 CGRect webrect = [scroller_ bounds];
1064 webrect.size.height = 0;
1065 [webview_ setFrame:webrect];
1066 }
1067
1068 [self reloadButtons];
1069 }
1070
1071 - (void) _finishLoading {
1072 size_t count([loading_ count]);
1073 if (count == 0)
1074 [self autorelease];
1075 if (reloading_ || count != 0)
1076 return;
1077 if (finish_ != nil)
1078 [self callFunction:finish_];
1079 [self reloadButtons];
1080 }
1081
1082 - (bool) isLoading {
1083 return [loading_ count] != 0;
1084 }
1085
1086 - (void) reloadButtons {
1087 if ([self isLoading])
1088 [indicator_ startAnimation];
1089 else
1090 [indicator_ stopAnimation];
1091 [super reloadButtons];
1092 }
1093
1094 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
1095 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
1096 }
1097
1098 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
1099 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
1100 }
1101
1102 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
1103 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
1104 }
1105
1106 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
1107 [self _pushPage];
1108 return [webview_ webView:sender didCommitLoadForFrame:frame];
1109 }
1110
1111 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
1112 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
1113 }
1114
1115 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
1116 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1117 [self _finishLoading];
1118
1119 if ([frame parentFrame] == nil) {
1120 if (DOMDocument *document = [frame DOMDocument])
1121 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
1122 for (DOMHTMLBodyElement *body in bodies) {
1123 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
1124
1125 bool colored(false);
1126
1127 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
1128 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
1129 DOMRGBColor *rgb([color getRGBColorValue]);
1130
1131 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
1132 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
1133 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
1134 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
1135
1136 UIColor *uic(nil);
1137
1138 if (red == 0xc7 && green == 0xce && blue == 0xd5)
1139 uic = [UIColor pinStripeColor];
1140 else if (alpha != 0)
1141 uic = [UIColor
1142 colorWithRed:(red / 255)
1143 green:(green / 255)
1144 blue:(blue / 255)
1145 alpha:alpha
1146 ];
1147
1148 if (uic != nil) {
1149 colored = true;
1150 [scroller_ setBackgroundColor:uic];
1151 }
1152 }
1153 }
1154
1155 if (!colored)
1156 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1157 break;
1158 }
1159 }
1160
1161 return [webview_ webView:sender didFinishLoadForFrame:frame];
1162 }
1163
1164 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
1165 if ([frame parentFrame] == nil)
1166 [self autorelease];
1167
1168 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
1169 [self _finishLoading];
1170
1171 if (reloading_)
1172 return;
1173
1174 if ([frame parentFrame] == nil) {
1175 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
1176 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
1177 [[error localizedDescription] stringByAddingPercentEscapes]
1178 ]]];
1179
1180 error_ = true;
1181 }
1182 }
1183
1184 - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1185 [self _didFailWithError:error forFrame:frame];
1186 }
1187
1188 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1189 [self _didFailWithError:error forFrame:frame];
1190 }
1191
1192 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
1193 #if LogBrowser || ForSaurik
1194 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
1195 #endif
1196 }
1197
1198 /* XXX: fix this stupid include file
1199 - (void) webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)database {
1200 [origin setQuota:0x500000];
1201 }*/
1202
1203 - (void) _setTileDrawingEnabled:(BOOL)enabled {
1204 //[webview_ setTileDrawingEnabled:enabled];
1205 }
1206
1207 - (void) setViewportWidth:(float)width {
1208 width_ = width ? width != 0 : [[self class] defaultWidth];
1209 [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1210 }
1211
1212 - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1213 [self _setTileDrawingEnabled:NO];
1214 }
1215
1216 - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event {
1217 [self _setTileDrawingEnabled:YES];
1218 [webview_ redrawScaledDocument];
1219 }
1220
1221 - (void) scrollerWillStartDragging:(UIScroller *)scroller {
1222 [self _setTileDrawingEnabled:NO];
1223 }
1224
1225 - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth {
1226 [self _setTileDrawingEnabled:YES];
1227 }
1228
1229 - (void) scrollerDidEndDragging:(UIScroller *)scroller {
1230 [self _setTileDrawingEnabled:YES];
1231 }
1232
1233 - (id) initWithBook:(RVBook *)book forWidth:(float)width {
1234 if ((self = [super initWithBook:book]) != nil) {
1235 loading_ = [[NSMutableSet alloc] initWithCapacity:3];
1236 popup_ = false;
1237
1238 struct CGRect bounds = [self bounds];
1239
1240 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
1241 [self addSubview:scroller_];
1242
1243 [scroller_ setFixedBackgroundPattern:YES];
1244 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
1245
1246 [scroller_ setScrollingEnabled:YES];
1247 [scroller_ setClipsSubviews:YES];
1248 [scroller_ setAllowsRubberBanding:YES];
1249
1250 [scroller_ setDelegate:self];
1251 [scroller_ setBounces:YES];
1252 [scroller_ setScrollHysteresis:8];
1253 [scroller_ setThumbDetectionEnabled:NO];
1254 [scroller_ setDirectionalScrolling:YES];
1255 [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */
1256 [scroller_ setEventMode:YES];
1257 [scroller_ setShowBackgroundShadow:NO]; /* YES */
1258 [scroller_ setAllowsRubberBanding:YES]; /* Vertical */
1259 [scroller_ setAdjustForContentSizeChange:YES]; /* NO */
1260
1261 CGRect webrect = [scroller_ bounds];
1262 webrect.size.height = 0;
1263
1264 WebView *webview;
1265
1266 WebThreadLock();
1267
1268 #if RecycleWebViews
1269 webview_ = [Documents_ lastObject];
1270 if (webview_ != nil) {
1271 webview_ = [webview_ retain];
1272 webview = [webview_ webView];
1273 [Documents_ removeLastObject];
1274 [webview_ setFrame:webrect];
1275 } else {
1276 #else
1277 if (true) {
1278 #endif
1279 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
1280 webview = [webview_ webView];
1281
1282 // XXX: this is terribly (too?) expensive
1283 //[webview_ setDrawsBackground:NO];
1284 [webview setPreferencesIdentifier:@"Cydia"];
1285
1286 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
1287
1288 [webview_ setAllowsMessaging:YES];
1289
1290 [webview_ setTilingEnabled:YES];
1291 [webview_ setDrawsGrid:NO];
1292 [webview_ setLogsTilingChanges:NO];
1293 [webview_ setTileMinificationFilter:kCAFilterNearest];
1294 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
1295 /* XXX: abstractify */
1296 [webview_ setDataDetectorTypes:0x80000000];
1297 else
1298 [webview_ setDetectsPhoneNumbers:NO];
1299 [webview_ setAutoresizes:YES];
1300
1301 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
1302 [webview_ setMaximumScale:5.00f forDocumentTypes:0x10];
1303 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
1304 //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
1305
1306 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
1307
1308 [webview_ setMinimumScale:1.00f forDocumentTypes:0x8];
1309 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
1310 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
1311
1312 [webview_ _setDocumentType:0x4];
1313
1314 if ([webview_ respondsToSelector:@selector(UIWebDocumentView:)])
1315 [webview_ setZoomsFocusedFormControl:YES];
1316 [webview_ setContentsPosition:7];
1317 [webview_ setEnabledGestures:0xa];
1318 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled];
1319 [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller];
1320
1321 [webview_ setSmoothsFonts:YES];
1322 [webview_ setAllowsImageSheet:YES];
1323 [webview _setUsesLoaderCache:YES];
1324
1325 [webview setGroupName:@"CydiaGroup"];
1326 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1327 [webview _setLayoutInterval:0];
1328 }
1329
1330 [self setViewportWidth:width];
1331
1332 [webview_ setDelegate:self];
1333 [webview_ setGestureDelegate:self];
1334 [webview_ setFormEditingDelegate:self];
1335 [webview_ setInteractionDelegate:self];
1336
1337 [scroller_ addSubview:webview_];
1338
1339 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
1340
1341 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
1342 NSString *application = package == nil ? @"Cydia" : [NSString
1343 stringWithFormat:@"Cydia/%@",
1344 [package installed]
1345 ];
1346
1347 if (Product_ != nil)
1348 application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_];
1349 if (Build_ != nil)
1350 application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_];
1351 if (Safari_ != nil)
1352 application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_];
1353
1354 [webview setApplicationNameForUserAgent:application];
1355
1356 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1357 cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_];
1358
1359 [webview setFrameLoadDelegate:indirect_];
1360 [webview setResourceLoadDelegate:indirect_];
1361 [webview setUIDelegate:indirect_];
1362 [webview setScriptDebugDelegate:indirect_];
1363 [webview setPolicyDelegate:indirect_];
1364
1365 WebThreadUnlock();
1366
1367 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
1368 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
1369 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
1370
1371 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1372 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
1373
1374 /*UIWebView *test([[[UIWebView alloc] initWithFrame:[self bounds]] autorelease]);
1375 [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]];
1376 [self addSubview:test];*/
1377 } return self;
1378 }
1379
1380 - (id) initWithBook:(RVBook *)book {
1381 return [self initWithBook:book forWidth:0];
1382 }
1383
1384 - (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script {
1385 WebThreadLock();
1386 WebView *webview([webview_ webView]);
1387 NSString *string([webview stringByEvaluatingJavaScriptFromString:script]);
1388 WebThreadUnlock();
1389 return string;
1390 }
1391
1392 - (void) callFunction:(WebScriptObject *)function {
1393 WebThreadLock();
1394
1395 WebView *webview([webview_ webView]);
1396 WebFrame *frame([webview mainFrame]);
1397
1398 id _private(MSHookIvar<id>(webview, "_private"));
1399 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1400 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1401
1402 bool no;
1403 if (settings == NULL)
1404 no = 0;
1405 else {
1406 no = settings->JavaScriptCanOpenWindowsAutomatically();
1407 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1408 }
1409
1410 [delegate_ clearFirstResponder];
1411 JSObjectRef object([function JSObject]);
1412 JSGlobalContextRef context([frame globalContext]);
1413 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1414
1415 if (settings != NULL)
1416 settings->setJavaScriptCanOpenWindowsAutomatically(no);
1417
1418 WebThreadUnlock();
1419 }
1420
1421 - (void) didCloseBook:(RVBook *)book {
1422 if (closer_ != nil)
1423 [self callFunction:closer_];
1424 }
1425
1426 - (void) __rightButtonClicked {
1427 reloading_ = true;
1428 [self reloadURL];
1429 }
1430
1431 - (void) _rightButtonClicked {
1432 #if !AlwaysReload
1433 if (function_ != nil)
1434 [self callFunction:function_];
1435 else
1436 #endif
1437 [self __rightButtonClicked];
1438 }
1439
1440 - (id) _rightButtonTitle {
1441 return CYLocalize("RELOAD");
1442 }
1443
1444 - (id) rightButtonTitle {
1445 return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle];
1446 }
1447
1448 - (UINavigationButtonStyle) rightButtonStyle {
1449 if (style_ == nil) normal:
1450 return UINavigationButtonStyleNormal;
1451 else if ([style_ isEqualToString:@"Normal"])
1452 return UINavigationButtonStyleNormal;
1453 else if ([style_ isEqualToString:@"Back"])
1454 return UINavigationButtonStyleBack;
1455 else if ([style_ isEqualToString:@"Highlighted"])
1456 return UINavigationButtonStyleHighlighted;
1457 else if ([style_ isEqualToString:@"Destructive"])
1458 return UINavigationButtonStyleDestructive;
1459 else goto normal;
1460 }
1461
1462 - (NSString *) title {
1463 return title_ == nil ? CYLocalize("LOADING") : title_;
1464 }
1465
1466 - (NSString *) backButtonTitle {
1467 return CYLocalize("BROWSER");
1468 }
1469
1470 - (void) setPageActive:(BOOL)active {
1471 if (!active)
1472 [indicator_ removeFromSuperview];
1473 else
1474 [[book_ navigationBar] addSubview:indicator_];
1475 }
1476
1477 - (void) resetViewAnimated:(BOOL)animated {
1478 }
1479
1480 - (void) setPushed:(bool)pushed {
1481 pushed_ = pushed;
1482 }
1483
1484 + (float) defaultWidth {
1485 return 980;
1486 }
1487
1488 @end