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