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