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