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