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