]>
Commit | Line | Data |
---|---|---|
819a0ab1 | 1 | #include "CyteKit/UCPlatform.h" |
2b2a4e33 JF |
2 | #include "CyteKit/WebViewController.h" |
3 | ||
4 | #include "CyteKit/MFMailComposeViewController-MailToURL.h" | |
819a0ab1 | 5 | |
c21004b9 JF |
6 | #include "iPhonePrivate.h" |
7 | ||
cfc530e6 | 8 | #include "CyteKit/IndirectDelegate.h" |
d458596e JF |
9 | #include "CyteKit/Localize.h" |
10 | #include "CyteKit/WebViewController.h" | |
819a0ab1 | 11 | #include "CyteKit/PerlCompatibleRegEx.hpp" |
4bcafc01 | 12 | #include "CyteKit/WebThreadLocked.hpp" |
43f3d7f6 | 13 | |
c21004b9 | 14 | //#include <QuartzCore/CALayer.h> |
43f3d7f6 JF |
15 | // XXX: fix the minimum requirement |
16 | extern NSString * const kCAFilterNearest; | |
22f8bed9 | 17 | |
caa427d1 | 18 | #include <WebCore/WebCoreThread.h> |
c21004b9 | 19 | |
5e640a64 JF |
20 | #import <WebKit/WebKitErrors.h> |
21 | #import <WebKit/WebPreferences.h> | |
c21004b9 JF |
22 | |
23 | #include <WebKit/DOMCSSPrimitiveValue.h> | |
24 | #include <WebKit/DOMCSSStyleDeclaration.h> | |
25 | #include <WebKit/DOMDocument.h> | |
26 | #include <WebKit/DOMHTMLBodyElement.h> | |
27 | #include <WebKit/DOMRGBColor.h> | |
28 | ||
bf7c998c JF |
29 | #include <dlfcn.h> |
30 | #include <objc/runtime.h> | |
31 | ||
adcb0422 | 32 | #define ForSaurik 0 |
eb09425a | 33 | #define DefaultTimeout_ 120.0 |
bfc87a4d | 34 | |
0815487b JF |
35 | #define ShowInternals 0 |
36 | #define LogBrowser 0 | |
72bdb258 | 37 | #define LogMessages 0 |
0815487b JF |
38 | |
39 | #define lprintf(args...) fprintf(stderr, args) | |
40 | ||
7c80833f JF |
41 | JSValueRef (*$JSObjectCallAsFunction)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *); |
42 | ||
2b2a4e33 JF |
43 | // XXX: centralize these special class things to some file or mechanism? |
44 | static Class $MFMailComposeViewController; | |
45 | ||
9c1605e2 JF |
46 | float CYScrollViewDecelerationRateNormal; |
47 | ||
aa1e1906 JF |
48 | @interface WebFrame (Cydia) |
49 | - (void) cydia$updateHeight; | |
50 | @end | |
51 | ||
dffe1eb1 JF |
52 | @implementation WebFrame (Cydia) |
53 | ||
54 | - (NSString *) description { | |
55 | return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[([self provisionalDataSource] ?: [self dataSource]) request] URL] absoluteString]]; | |
56 | } | |
57 | ||
aa1e1906 JF |
58 | - (void) cydia$updateHeight { |
59 | [[[self frameElement] style] | |
60 | setProperty:@"height" | |
61 | value:[NSString stringWithFormat:@"%dpx", | |
62 | [[[self DOMDocument] body] scrollHeight]] | |
63 | priority:nil]; | |
64 | } | |
65 | ||
dffe1eb1 JF |
66 | @end |
67 | ||
aa5d0de7 | 68 | /* Indirect Delegate {{{ */ |
aa5d0de7 JF |
69 | @implementation IndirectDelegate |
70 | ||
353dda5b JF |
71 | - (id) delegate { |
72 | return delegate_; | |
73 | } | |
74 | ||
aa5d0de7 JF |
75 | - (void) setDelegate:(id)delegate { |
76 | delegate_ = delegate; | |
77 | } | |
78 | ||
79 | - (id) initWithDelegate:(id)delegate { | |
80 | delegate_ = delegate; | |
81 | return self; | |
82 | } | |
83 | ||
caa427d1 JF |
84 | - (IMP) methodForSelector:(SEL)sel { |
85 | if (IMP method = [super methodForSelector:sel]) | |
86 | return method; | |
87 | fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel)); | |
88 | return NULL; | |
89 | } | |
90 | ||
aa5d0de7 | 91 | - (BOOL) respondsToSelector:(SEL)sel { |
caa427d1 JF |
92 | if ([super respondsToSelector:sel]) |
93 | return YES; | |
0815487b | 94 | |
caa427d1 | 95 | // XXX: WebThreadCreateNSInvocation returns nil |
0815487b JF |
96 | |
97 | #if ShowInternals | |
98 | fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel)); | |
99 | #endif | |
100 | ||
caa427d1 | 101 | return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel]; |
aa5d0de7 JF |
102 | } |
103 | ||
104 | - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel { | |
caa427d1 JF |
105 | if (NSMethodSignature *method = [super methodSignatureForSelector:sel]) |
106 | return method; | |
0815487b JF |
107 | |
108 | #if ShowInternals | |
109 | fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel)); | |
110 | #endif | |
111 | ||
aa5d0de7 JF |
112 | if (delegate_ != nil) |
113 | if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel]) | |
114 | return sig; | |
0815487b | 115 | |
aa5d0de7 JF |
116 | // XXX: I fucking hate Apple so very very bad |
117 | return [NSMethodSignature signatureWithObjCTypes:"v@:"]; | |
118 | } | |
119 | ||
120 | - (void) forwardInvocation:(NSInvocation *)inv { | |
121 | SEL sel = [inv selector]; | |
122 | if (delegate_ != nil && [delegate_ respondsToSelector:sel]) | |
123 | [inv invokeWithTarget:delegate_]; | |
124 | } | |
125 | ||
126 | @end | |
127 | /* }}} */ | |
128 | ||
09e89a8a | 129 | @implementation CyteWebViewController |
2634b249 JF |
130 | |
131 | #if ShowInternals | |
819a0ab1 | 132 | #include "CyteKit/UCInternal.h" |
22f8bed9 JF |
133 | #endif |
134 | ||
2634b249 JF |
135 | + (void) _initialize { |
136 | [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding]; | |
9c1605e2 | 137 | |
7c80833f JF |
138 | void *js(NULL); |
139 | if (js == NULL) | |
140 | js = dlopen("/System/Library/Frameworks/JavaScriptCore.framework/JavaScriptCore", RTLD_GLOBAL | RTLD_LAZY); | |
141 | if (js == NULL) | |
142 | js = dlopen("/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore", RTLD_GLOBAL | RTLD_LAZY); | |
143 | if (js != NULL) | |
144 | $JSObjectCallAsFunction = reinterpret_cast<JSValueRef (*)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *)>(dlsym(js, "JSObjectCallAsFunction")); | |
145 | ||
2b2a4e33 JF |
146 | dlopen("/System/Library/Frameworks/MessageUI.framework/MessageUI", RTLD_GLOBAL | RTLD_LAZY); |
147 | $MFMailComposeViewController = objc_getClass("MFMailComposeViewController"); | |
148 | ||
9c1605e2 JF |
149 | if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal"))) |
150 | CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal; | |
151 | else // XXX: this actually might be fast on some older systems: we should look into this | |
152 | CYScrollViewDecelerationRateNormal = 0.998; | |
2634b249 JF |
153 | } |
154 | ||
2713be8e JF |
155 | - (bool) retainsNetworkActivityIndicator { |
156 | return true; | |
157 | } | |
158 | ||
8b74eaff JF |
159 | - (void) releaseNetworkActivityIndicator { |
160 | if ([loading_ count] != 0) { | |
161 | [loading_ removeAllObjects]; | |
162 | ||
163 | if ([self retainsNetworkActivityIndicator]) | |
164 | [delegate_ releaseNetworkActivityIndicator]; | |
165 | } | |
166 | } | |
167 | ||
2634b249 JF |
168 | - (void) dealloc { |
169 | #if LogBrowser | |
09e89a8a | 170 | NSLog(@"[CyteWebViewController dealloc]"); |
2634b249 JF |
171 | #endif |
172 | ||
8b74eaff | 173 | [self releaseNetworkActivityIndicator]; |
bc11cf5b | 174 | |
22f8bed9 JF |
175 | [super dealloc]; |
176 | } | |
177 | ||
e3e0246d JF |
178 | - (NSString *) description { |
179 | return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[request_ URL] absoluteString]]; | |
180 | } | |
181 | ||
6f837280 JF |
182 | - (CyteWebView *) webView { |
183 | return (CyteWebView *) [self view]; | |
184 | } | |
185 | ||
dd48f2e6 JF |
186 | - (NSURL *) URLWithURL:(NSURL *)url { |
187 | return url; | |
188 | } | |
eb09425a | 189 | |
f050e4d9 JF |
190 | - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy referrer:(NSString *)referrer { |
191 | NSMutableURLRequest *request([NSMutableURLRequest | |
dd48f2e6 JF |
192 | requestWithURL:[self URLWithURL:url] |
193 | cachePolicy:policy | |
eb09425a | 194 | timeoutInterval:DefaultTimeout_ |
f050e4d9 JF |
195 | ]); |
196 | ||
197 | [request setValue:referrer forHTTPHeaderField:@"Referer"]; | |
198 | ||
199 | return request; | |
eb09425a JF |
200 | } |
201 | ||
e8eda555 | 202 | - (void) setRequest:(NSURLRequest *)request { |
dd48f2e6 | 203 | _assert(request_ == nil); |
e8eda555 JF |
204 | request_ = request; |
205 | } | |
206 | ||
207 | - (void) setURL:(NSURL *)url { | |
f050e4d9 JF |
208 | [self setURL:url withReferrer:nil]; |
209 | } | |
210 | ||
211 | - (void) setURL:(NSURL *)url withReferrer:(NSString *)referrer { | |
212 | [self setRequest:[self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy referrer:referrer]]; | |
dd48f2e6 JF |
213 | } |
214 | ||
22f8bed9 | 215 | - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy { |
f050e4d9 | 216 | [self loadRequest:[self requestWithURL:url cachePolicy:policy referrer:nil]]; |
22f8bed9 JF |
217 | } |
218 | ||
219 | - (void) loadURL:(NSURL *)url { | |
220 | [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]; | |
221 | } | |
222 | ||
22f8bed9 | 223 | - (void) loadRequest:(NSURLRequest *)request { |
0352f238 JF |
224 | #if LogBrowser |
225 | NSLog(@"loadRequest:%@", request); | |
226 | #endif | |
227 | ||
fe468f45 | 228 | error_ = false; |
d9fc1d37 | 229 | ready_ = true; |
caa427d1 | 230 | |
0893a034 | 231 | WebThreadLocked lock; |
6f837280 | 232 | [[self webView] loadRequest:request]; |
22f8bed9 JF |
233 | } |
234 | ||
b13b8664 | 235 | - (void) reloadURLWithCache:(BOOL)cache { |
22f8bed9 JF |
236 | if (request_ == nil) |
237 | return; | |
238 | ||
b13b8664 JF |
239 | NSMutableURLRequest *request([request_ mutableCopy]); |
240 | [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)]; | |
241 | ||
a374f380 | 242 | request_ = request; |
b13b8664 | 243 | |
d69dbfc5 | 244 | if (cache || [request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil) |
fe468f45 | 245 | [self loadRequest:request_]; |
22f8bed9 | 246 | else { |
79ed082a | 247 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 248 | initWithTitle:UCLocalize("RESUBMIT_FORM") |
79ed082a | 249 | message:nil |
22f8bed9 | 250 | delegate:self |
79ed082a | 251 | cancelButtonTitle:UCLocalize("CANCEL") |
1aa29546 JF |
252 | otherButtonTitles: |
253 | UCLocalize("SUBMIT"), | |
254 | nil | |
22f8bed9 | 255 | ] autorelease]; |
2634b249 | 256 | |
79ed082a GP |
257 | [alert setContext:@"submit"]; |
258 | [alert show]; | |
22f8bed9 JF |
259 | } |
260 | } | |
261 | ||
eb09425a JF |
262 | - (void) reloadData { |
263 | [super reloadData]; | |
d9fc1d37 JF |
264 | |
265 | if (ready_) | |
266 | [self dispatchEvent:@"CydiaReloadData"]; | |
267 | else | |
268 | [self reloadURLWithCache:YES]; | |
eb09425a JF |
269 | } |
270 | ||
22f8bed9 | 271 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
46d3a5cf JF |
272 | custom_ = button; |
273 | style_ = style; | |
274 | function_ = function; | |
12b59862 | 275 | |
70a9ff4e | 276 | [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO]; |
22f8bed9 JF |
277 | } |
278 | ||
279 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
46d3a5cf JF |
280 | custom_ = button; |
281 | style_ = style; | |
282 | function_ = function; | |
22f8bed9 | 283 | |
ed5566c7 JF |
284 | [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO]; |
285 | } | |
12b59862 | 286 | |
ed5566c7 JF |
287 | - (void) removeButton { |
288 | custom_ = [NSNull null]; | |
70a9ff4e | 289 | [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO]; |
22f8bed9 | 290 | } |
22f8bed9 | 291 | |
8e3b68d4 JF |
292 | - (void) scrollToBottomAnimated:(NSNumber *)animated { |
293 | CGSize size([scroller_ contentSize]); | |
294 | CGPoint offset([scroller_ contentOffset]); | |
295 | CGRect frame([scroller_ frame]); | |
296 | ||
297 | if (size.height - offset.y < frame.size.height + 20.f) { | |
298 | CGRect rect = {{0, size.height-1}, {size.width, 1}}; | |
299 | [scroller_ scrollRectToVisible:rect animated:[animated boolValue]]; | |
300 | } | |
301 | } | |
302 | ||
7e37a676 | 303 | - (void) _setViewportWidth { |
6f837280 | 304 | [[[self webView] _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; |
7e37a676 JF |
305 | } |
306 | ||
2634b249 JF |
307 | - (void) setViewportWidth:(float)width { |
308 | width_ = width != 0 ? width : [[self class] defaultWidth]; | |
7e37a676 JF |
309 | [self _setViewportWidth]; |
310 | } | |
311 | ||
8dbdaafa JF |
312 | - (void) _setViewportWidthOnMainThread:(NSNumber *)width { |
313 | [self setViewportWidth:[width floatValue]]; | |
314 | } | |
315 | ||
316 | - (void) setViewportWidthOnMainThread:(float)width { | |
317 | [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO]; | |
318 | } | |
319 | ||
7e37a676 JF |
320 | - (void) webViewUpdateViewSettings:(UIWebView *)view { |
321 | [self _setViewportWidth]; | |
22f8bed9 JF |
322 | } |
323 | ||
2b2a4e33 JF |
324 | - (void) mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error { |
325 | [self dismissModalViewControllerAnimated:YES]; | |
326 | } | |
327 | ||
22485d93 JF |
328 | - (void) _setupMail:(MFMailComposeViewController *)controller { |
329 | } | |
330 | ||
2634b249 | 331 | - (void) _openMailToURL:(NSURL *)url { |
2b2a4e33 JF |
332 | if ($MFMailComposeViewController != nil && [$MFMailComposeViewController canSendMail]) { |
333 | MFMailComposeViewController *controller([[[$MFMailComposeViewController alloc] init] autorelease]); | |
334 | [controller setMailComposeDelegate:self]; | |
335 | ||
336 | [controller setMailToURL:url]; | |
337 | ||
22485d93 JF |
338 | [self _setupMail:controller]; |
339 | ||
2b2a4e33 JF |
340 | [self presentModalViewController:controller animated:YES]; |
341 | return; | |
342 | } | |
343 | ||
8ea72491 JF |
344 | UIApplication *app([UIApplication sharedApplication]); |
345 | if ([app respondsToSelector:@selector(openURL:asPanel:)]) | |
346 | [app openURL:url asPanel:YES]; | |
347 | else | |
348 | [app openURL:url]; | |
22f8bed9 JF |
349 | } |
350 | ||
2634b249 JF |
351 | - (bool) _allowJavaScriptPanel { |
352 | return true; | |
22f8bed9 JF |
353 | } |
354 | ||
8366df5e JF |
355 | - (bool) allowsNavigationAction { |
356 | return allowsNavigationAction_; | |
357 | } | |
358 | ||
359 | - (void) setAllowsNavigationAction:(bool)value { | |
360 | allowsNavigationAction_ = value; | |
361 | } | |
362 | ||
363 | - (void) setAllowsNavigationActionByNumber:(NSNumber *)value { | |
364 | [self setAllowsNavigationAction:[value boolValue]]; | |
52498c7e JF |
365 | } |
366 | ||
8d497e2a JF |
367 | - (void) popViewControllerWithNumber:(NSNumber *)value { |
368 | UINavigationController *navigation([self navigationController]); | |
369 | if ([navigation topViewController] == self) | |
370 | [navigation popViewControllerAnimated:[value boolValue]]; | |
371 | } | |
372 | ||
2634b249 | 373 | - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame { |
8b74eaff JF |
374 | NSValue *object([NSValue valueWithNonretainedObject:frame]); |
375 | if (![loading_ containsObject:object]) | |
376 | return; | |
377 | [loading_ removeObject:object]; | |
378 | ||
2634b249 | 379 | [self _didFinishLoading]; |
22f8bed9 | 380 | |
46b423a7 | 381 | if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled) |
22f8bed9 JF |
382 | return; |
383 | ||
fe2d3954 | 384 | if ([[error domain] isEqualToString:WebKitErrorDomain] && [error code] == WebKitErrorFrameLoadInterruptedByPolicyChange) { |
5bb0da03 | 385 | request_ = nil; |
fe2d3954 JF |
386 | return; |
387 | } | |
388 | ||
caa427d1 | 389 | if ([frame parentFrame] == nil) { |
2634b249 JF |
390 | [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", |
391 | [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString], | |
392 | [[error localizedDescription] stringByAddingPercentEscapes] | |
393 | ]]]; | |
22f8bed9 | 394 | |
2634b249 JF |
395 | error_ = true; |
396 | } | |
397 | } | |
caa427d1 | 398 | |
f050e4d9 JF |
399 | - (void) pushRequest:(NSURLRequest *)request forAction:(NSDictionary *)action asPop:(bool)pop { |
400 | WebFrame *frame(nil); | |
401 | if (NSDictionary *WebActionElement = [action objectForKey:@"WebActionElementKey"]) | |
402 | frame = [WebActionElement objectForKey:@"WebElementFrame"]; | |
403 | if (frame == nil) | |
404 | frame = [[[[self webView] _documentView] webView] mainFrame]; | |
405 | ||
406 | WebDataSource *source([frame provisionalDataSource] ?: [frame dataSource]); | |
407 | NSString *referrer([request valueForHTTPHeaderField:@"Referer"] ?: [[[source request] URL] absoluteString]); | |
408 | ||
2634b249 | 409 | NSURL *url([request URL]); |
a5938ea5 | 410 | |
028dbd1c | 411 | // XXX: filter to internal usage? |
f050e4d9 | 412 | CyteViewController *page([delegate_ pageForURL:url forExternal:NO withReferrer:referrer]); |
2fad210a | 413 | |
2634b249 | 414 | if (page == nil) { |
09e89a8a | 415 | CyteWebViewController *browser([[[class_ alloc] init] autorelease]); |
0620c393 | 416 | [browser setRequest:request]; |
2634b249 | 417 | page = browser; |
bc11cf5b | 418 | } |
ce041f4f | 419 | |
2634b249 | 420 | [page setDelegate:delegate_]; |
d32f96be | 421 | [page setPageColor:color_]; |
b5e7eebb | 422 | |
52498c7e | 423 | if (!pop) { |
2634b249 | 424 | [[self navigationItem] setTitle:title_]; |
b5e7eebb | 425 | |
2634b249 JF |
426 | [[self navigationController] pushViewController:page animated:YES]; |
427 | } else { | |
2e26757e | 428 | UINavigationController *navigation([[[UINavigationController alloc] initWithRootViewController:page] autorelease]); |
22f8bed9 | 429 | |
2634b249 | 430 | [navigation setDelegate:delegate_]; |
22f8bed9 | 431 | |
2634b249 JF |
432 | [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] |
433 | initWithTitle:UCLocalize("CLOSE") | |
434 | style:UIBarButtonItemStylePlain | |
435 | target:page | |
436 | action:@selector(close) | |
437 | ] autorelease]]; | |
438 | ||
45e66037 | 439 | [[self navigationController] presentModalViewController:navigation animated:YES]; |
2634b249 | 440 | } |
52498c7e JF |
441 | } |
442 | ||
1ee69bb4 | 443 | // CyteWebViewDelegate {{{ |
52498c7e JF |
444 | - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message { |
445 | #if LogMessages | |
c8f62968 JF |
446 | static Pcre irritating("^(?" |
447 | ":" "The page at .* displayed insecure content from .*\\." | |
448 | "|" "Unsafe JavaScript attempt to access frame with URL .* from frame with URL .*\\. Domains, protocols and ports must match\\." | |
449 | ")\\n$"); | |
450 | ||
c81b955f JF |
451 | if (NSString *data = [message objectForKey:@"message"]) |
452 | if (irritating(data)) | |
453 | return; | |
454 | ||
52498c7e JF |
455 | NSLog(@"addMessageToConsole:%@", message); |
456 | #endif | |
457 | } | |
458 | ||
459 | - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
460 | #if LogBrowser | |
f050e4d9 | 461 | NSLog(@"decidePolicyForNavigationAction:%@ request:%@ %@ frame:%@", action, request, [request allHTTPHeaderFields], frame); |
52498c7e JF |
462 | #endif |
463 | ||
1002f4d9 JF |
464 | NSURL *url(request == nil ? nil : [request URL]); |
465 | if ([[url scheme] isEqualToString:@"itms-appss"] || [[url absoluteString] hasPrefix:@"https://itunes.apple.com/app/"]) { | |
466 | appstore_ = url; | |
467 | ||
468 | UIAlertView *alert = [[[UIAlertView alloc] | |
469 | initWithTitle:UCLocalize("APP_STORE_REDIRECT") | |
470 | message:nil | |
471 | delegate:self | |
472 | cancelButtonTitle:UCLocalize("CANCEL") | |
473 | otherButtonTitles: | |
474 | UCLocalize("ALLOW"), | |
475 | nil | |
476 | ] autorelease]; | |
477 | ||
478 | [alert setContext:@"itmsappss"]; | |
479 | [alert show]; | |
480 | ||
481 | [listener ignore]; | |
482 | return; | |
483 | } | |
484 | ||
52498c7e JF |
485 | if ([frame parentFrame] == nil) { |
486 | if (!error_) { | |
d323285e | 487 | if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) { |
8366df5e | 488 | if (url != nil) |
f050e4d9 | 489 | [self pushRequest:request forAction:action asPop:NO]; |
52498c7e JF |
490 | [listener ignore]; |
491 | } | |
492 | } | |
493 | } | |
494 | } | |
495 | ||
d323285e | 496 | - (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame { |
77d4e15b JF |
497 | #if LogBrowser |
498 | NSLog(@"didDecidePolicy:%u forNavigationAction:%@ request:%@ frame:%@", decision, action, request, [request allHTTPHeaderFields], frame); | |
499 | #endif | |
500 | ||
501 | if ([frame parentFrame] == nil) { | |
502 | switch (decision) { | |
503 | case CYWebPolicyDecisionIgnore: | |
504 | if ([[request_ URL] isEqual:[request URL]]) | |
505 | request_ = nil; | |
506 | break; | |
507 | ||
508 | case CYWebPolicyDecisionUse: | |
509 | if (!error_) | |
510 | request_ = request; | |
511 | break; | |
512 | ||
513 | default: | |
514 | break; | |
515 | } | |
516 | } | |
d323285e JF |
517 | } |
518 | ||
f050e4d9 | 519 | - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener { |
52498c7e | 520 | #if LogBrowser |
f050e4d9 | 521 | NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ %@ newFrameName:%@", action, request, [request allHTTPHeaderFields], name); |
52498c7e JF |
522 | #endif |
523 | ||
524 | NSURL *url([request URL]); | |
525 | if (url == nil) | |
526 | return; | |
527 | ||
f050e4d9 | 528 | if ([name isEqualToString:@"_open"]) |
52498c7e JF |
529 | [delegate_ openURL:url]; |
530 | else { | |
531 | NSString *scheme([[url scheme] lowercaseString]); | |
532 | if ([scheme isEqualToString:@"mailto"]) | |
533 | [self _openMailToURL:url]; | |
534 | else | |
f050e4d9 | 535 | [self pushRequest:request forAction:action asPop:[name isEqualToString:@"_popup"]]; |
52498c7e | 536 | } |
22f8bed9 | 537 | |
2634b249 | 538 | [listener ignore]; |
22f8bed9 JF |
539 | } |
540 | ||
2634b249 | 541 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
c215c5ed JF |
542 | #if LogBrowser |
543 | NSLog(@"didClearWindowObject:%@ forFrame:%@", window, frame); | |
544 | #endif | |
22f8bed9 JF |
545 | } |
546 | ||
64ffa332 JF |
547 | - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame { |
548 | #if LogBrowser | |
549 | NSLog(@"didCommitLoadForFrame:%@", frame); | |
550 | #endif | |
551 | ||
552 | if ([frame parentFrame] == nil) { | |
b3be8c0c | 553 | loaded_ = true; |
64ffa332 JF |
554 | } |
555 | } | |
556 | ||
2634b249 JF |
557 | - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { |
558 | #if LogBrowser | |
559 | NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame); | |
560 | #endif | |
561 | ||
562 | [self _didFailWithError:error forFrame:frame]; | |
22f8bed9 JF |
563 | } |
564 | ||
2634b249 JF |
565 | - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { |
566 | #if LogBrowser | |
567 | NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame); | |
568 | #endif | |
569 | ||
570 | [self _didFailWithError:error forFrame:frame]; | |
22f8bed9 JF |
571 | } |
572 | ||
2634b249 | 573 | - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame { |
8b74eaff JF |
574 | NSValue *object([NSValue valueWithNonretainedObject:frame]); |
575 | if (![loading_ containsObject:object]) | |
576 | return; | |
577 | [loading_ removeObject:object]; | |
22f8bed9 | 578 | |
caa427d1 | 579 | if ([frame parentFrame] == nil) { |
22f8bed9 | 580 | if (DOMDocument *document = [frame DOMDocument]) |
4cc9e99a | 581 | if (DOMNodeList *bodies = [document getElementsByTagName:@"body"]) |
96f3833b | 582 | for (DOMHTMLBodyElement *body in (id) bodies) { |
22f8bed9 JF |
583 | DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]); |
584 | ||
65ea9562 | 585 | UIColor *uic(nil); |
22f8bed9 JF |
586 | |
587 | if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) { | |
fe468f45 JF |
588 | if ([color primitiveType] == DOM_CSS_RGBCOLOR) { |
589 | DOMRGBColor *rgb([color getRGBColorValue]); | |
590 | ||
591 | float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]); | |
592 | float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]); | |
593 | float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]); | |
594 | float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]); | |
595 | ||
d32f96be JF |
596 | if (alpha == 1) |
597 | uic = [UIColor | |
598 | colorWithRed:(red / 255) | |
599 | green:(green / 255) | |
600 | blue:(blue / 255) | |
601 | alpha:alpha | |
602 | ]; | |
22f8bed9 JF |
603 | } |
604 | } | |
605 | ||
77259cab | 606 | [super setPageColor:uic]; |
d32f96be | 607 | [scroller_ setBackgroundColor:color_]; |
22f8bed9 JF |
608 | break; |
609 | } | |
610 | } | |
611 | ||
2634b249 | 612 | [self _didFinishLoading]; |
22f8bed9 JF |
613 | } |
614 | ||
2634b249 JF |
615 | - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { |
616 | if ([frame parentFrame] != nil) | |
617 | return; | |
caa427d1 | 618 | |
7b33d201 | 619 | title_ = title; |
3931b718 | 620 | |
2634b249 JF |
621 | [[self navigationItem] setTitle:title_]; |
622 | } | |
22f8bed9 | 623 | |
2634b249 | 624 | - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame { |
a86042fa JF |
625 | #if LogBrowser |
626 | NSLog(@"didStartProvisionalLoadForFrame:%@", frame); | |
627 | #endif | |
628 | ||
2634b249 | 629 | [loading_ addObject:[NSValue valueWithNonretainedObject:frame]]; |
7592e053 | 630 | |
caa427d1 | 631 | if ([frame parentFrame] == nil) { |
7b33d201 | 632 | title_ = nil; |
46d3a5cf JF |
633 | custom_ = nil; |
634 | style_ = nil; | |
635 | function_ = nil; | |
2634b249 | 636 | |
aa1e1906 JF |
637 | [registered_ removeAllObjects]; |
638 | timer_ = nil; | |
639 | ||
622a9912 JF |
640 | allowsNavigationAction_ = true; |
641 | ||
5cdfcd6f | 642 | [self setHidesNavigationBar:NO]; |
db698f42 | 643 | [self setScrollAlwaysBounceVertical:true]; |
4886cc81 | 644 | [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault]; |
5cdfcd6f | 645 | |
2634b249 JF |
646 | // XXX: do we still need to do this? |
647 | [[self navigationItem] setTitle:nil]; | |
caa427d1 | 648 | } |
fe468f45 | 649 | |
2634b249 | 650 | [self _didStartLoading]; |
caa427d1 JF |
651 | } |
652 | ||
754456f5 JF |
653 | - (void) webView:(WebView *)view resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source { |
654 | challenge_ = [challenge retain]; | |
655 | ||
656 | NSURLProtectionSpace *space([challenge protectionSpace]); | |
657 | NSString *realm([space realm]); | |
658 | if (realm == nil) | |
659 | realm = @""; | |
660 | ||
661 | UIAlertView *alert = [[[UIAlertView alloc] | |
662 | initWithTitle:realm | |
663 | message:nil | |
664 | delegate:self | |
665 | cancelButtonTitle:UCLocalize("CANCEL") | |
666 | otherButtonTitles:UCLocalize("LOGIN"), nil | |
667 | ] autorelease]; | |
668 | ||
669 | [alert setContext:@"challenge"]; | |
670 | [alert setNumberOfRows:1]; | |
671 | ||
672 | [alert addTextFieldWithValue:@"" label:UCLocalize("USERNAME")]; | |
673 | [alert addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")]; | |
674 | ||
675 | UITextField *username([alert textFieldAtIndex:0]); { | |
676 | UITextInputTraits *traits([username textInputTraits]); | |
677 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
678 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
679 | [traits setKeyboardType:UIKeyboardTypeASCIICapable]; | |
680 | [traits setReturnKeyType:UIReturnKeyNext]; | |
681 | } | |
682 | ||
683 | UITextField *password([alert textFieldAtIndex:1]); { | |
684 | UITextInputTraits *traits([password textInputTraits]); | |
685 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
686 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
687 | [traits setKeyboardType:UIKeyboardTypeASCIICapable]; | |
688 | // XXX: UIReturnKeyDone | |
689 | [traits setReturnKeyType:UIReturnKeyNext]; | |
690 | [traits setSecureTextEntry:YES]; | |
691 | } | |
692 | ||
693 | [alert show]; | |
694 | } | |
695 | ||
2634b249 | 696 | - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
0352f238 JF |
697 | #if LogBrowser |
698 | NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source); | |
699 | #endif | |
700 | ||
2634b249 | 701 | return request; |
22f8bed9 JF |
702 | } |
703 | ||
2634b249 JF |
704 | - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
705 | return [self _allowJavaScriptPanel]; | |
22f8bed9 JF |
706 | } |
707 | ||
2634b249 JF |
708 | - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
709 | return [self _allowJavaScriptPanel]; | |
600d005d JF |
710 | } |
711 | ||
2634b249 JF |
712 | - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame { |
713 | return [self _allowJavaScriptPanel]; | |
600d005d JF |
714 | } |
715 | ||
2634b249 JF |
716 | - (void) webViewClose:(WebView *)view { |
717 | [self close]; | |
600d005d | 718 | } |
2634b249 | 719 | // }}} |
600d005d | 720 | |
2634b249 | 721 | - (void) close { |
19f2d77f | 722 | [[[self navigationController] parentOrPresentingViewController] dismissModalViewControllerAnimated:YES]; |
600d005d JF |
723 | } |
724 | ||
2634b249 JF |
725 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
726 | NSString *context([alert context]); | |
600d005d | 727 | |
2634b249 JF |
728 | if ([context isEqualToString:@"sensitive"]) { |
729 | switch (button) { | |
730 | case 1: | |
731 | sensitive_ = [NSNumber numberWithBool:YES]; | |
732 | break; | |
600d005d | 733 | |
2634b249 JF |
734 | case 2: |
735 | sensitive_ = [NSNumber numberWithBool:NO]; | |
736 | break; | |
737 | } | |
600d005d | 738 | |
2634b249 JF |
739 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
740 | } else if ([context isEqualToString:@"challenge"]) { | |
741 | id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]); | |
caa427d1 | 742 | |
754456f5 JF |
743 | if (button == [alert cancelButtonIndex]) |
744 | [sender cancelAuthenticationChallenge:challenge_]; | |
745 | else if (button == [alert firstOtherButtonIndex]) { | |
746 | NSString *username([[alert textFieldAtIndex:0] text]); | |
747 | NSString *password([[alert textFieldAtIndex:1] text]); | |
600d005d | 748 | |
754456f5 | 749 | NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]); |
600d005d | 750 | |
754456f5 | 751 | [sender useCredential:credential forAuthenticationChallenge:challenge_]; |
2634b249 | 752 | } |
600d005d | 753 | |
2634b249 | 754 | challenge_ = nil; |
600d005d | 755 | |
1002f4d9 JF |
756 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
757 | } else if ([context isEqualToString:@"itmsappss"]) { | |
758 | if (button == [alert cancelButtonIndex]) { | |
759 | } else if (button == [alert firstOtherButtonIndex]) { | |
760 | [delegate_ openURL:appstore_]; | |
761 | } | |
762 | ||
2634b249 JF |
763 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
764 | } else if ([context isEqualToString:@"submit"]) { | |
75b95256 JF |
765 | if (button == [alert cancelButtonIndex]) { |
766 | } else if (button == [alert firstOtherButtonIndex]) { | |
767 | if (request_ != nil) { | |
0893a034 | 768 | WebThreadLocked lock; |
6f837280 | 769 | [[self webView] loadRequest:request_]; |
75b95256 | 770 | } |
2634b249 | 771 | } |
600d005d | 772 | |
2634b249 JF |
773 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
774 | } | |
600d005d JF |
775 | } |
776 | ||
2634b249 JF |
777 | - (UIBarButtonItemStyle) rightButtonStyle { |
778 | if (style_ == nil) normal: | |
779 | return UIBarButtonItemStylePlain; | |
780 | else if ([style_ isEqualToString:@"Normal"]) | |
781 | return UIBarButtonItemStylePlain; | |
782 | else if ([style_ isEqualToString:@"Highlighted"]) | |
783 | return UIBarButtonItemStyleDone; | |
784 | else goto normal; | |
600d005d JF |
785 | } |
786 | ||
2634b249 | 787 | - (UIBarButtonItem *) customButton { |
29756674 JF |
788 | if (custom_ == nil) |
789 | return nil; | |
4cc9e99a | 790 | else if ((/*clang:*/id) custom_ == [NSNull null]) |
29756674 JF |
791 | return (UIBarButtonItem *) [NSNull null]; |
792 | ||
793 | return [[[UIBarButtonItem alloc] | |
ed5566c7 | 794 | initWithTitle:static_cast<NSString *>(custom_.operator NSObject *()) |
2634b249 JF |
795 | style:[self rightButtonStyle] |
796 | target:self | |
797 | action:@selector(customButtonClicked) | |
798 | ] autorelease]; | |
caa427d1 JF |
799 | } |
800 | ||
e6124cb6 JF |
801 | - (UIBarButtonItem *) leftButton { |
802 | UINavigationItem *item([self navigationItem]); | |
803 | if ([item backBarButtonItem] != nil && ![item hidesBackButton]) | |
804 | return nil; | |
805 | ||
806 | if (UINavigationController *navigation = [self navigationController]) | |
19f2d77f | 807 | if ([[navigation parentOrPresentingViewController] modalViewController] == navigation) |
e6124cb6 JF |
808 | return [[[UIBarButtonItem alloc] |
809 | initWithTitle:UCLocalize("CLOSE") | |
810 | style:UIBarButtonItemStylePlain | |
811 | target:self | |
812 | action:@selector(close) | |
813 | ] autorelease]; | |
814 | ||
815 | return nil; | |
816 | } | |
817 | ||
818 | - (void) applyLeftButton { | |
819 | [[self navigationItem] setLeftBarButtonItem:[self leftButton]]; | |
820 | } | |
821 | ||
2634b249 JF |
822 | - (UIBarButtonItem *) rightButton { |
823 | return reloaditem_; | |
eb35c522 JF |
824 | } |
825 | ||
2634b249 JF |
826 | - (void) applyLoadingTitle { |
827 | [[self navigationItem] setTitle:UCLocalize("LOADING")]; | |
caa427d1 JF |
828 | } |
829 | ||
c83a464d JF |
830 | - (void) layoutRightButton { |
831 | [[loadingitem_ view] addSubview:indicator_]; | |
832 | [[loadingitem_ view] bringSubviewToFront:indicator_]; | |
833 | } | |
834 | ||
2634b249 JF |
835 | - (void) applyRightButton { |
836 | if ([self isLoading]) { | |
837 | [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES]; | |
c83a464d | 838 | [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0]; |
8d603a7d JF |
839 | |
840 | [indicator_ startAnimating]; | |
2634b249 | 841 | [self applyLoadingTitle]; |
2634b249 | 842 | } else { |
8d603a7d JF |
843 | [indicator_ stopAnimating]; |
844 | ||
29756674 JF |
845 | UIBarButtonItem *button([self customButton]); |
846 | if (button == nil) | |
847 | button = [self rightButton]; | |
848 | else if (button == (UIBarButtonItem *) [NSNull null]) | |
849 | button = nil; | |
850 | ||
bf965437 | 851 | [[self navigationItem] setRightBarButtonItem:button animated:YES]; |
2634b249 | 852 | } |
caa427d1 JF |
853 | } |
854 | ||
df30fbee GP |
855 | - (void) didStartLoading { |
856 | // Overridden in subclasses. | |
857 | } | |
858 | ||
2634b249 JF |
859 | - (void) _didStartLoading { |
860 | [self applyRightButton]; | |
54043703 JF |
861 | |
862 | if ([loading_ count] != 1) | |
863 | return; | |
df30fbee | 864 | |
2713be8e JF |
865 | if ([self retainsNetworkActivityIndicator]) |
866 | [delegate_ retainNetworkActivityIndicator]; | |
867 | ||
df30fbee GP |
868 | [self didStartLoading]; |
869 | } | |
870 | ||
871 | - (void) didFinishLoading { | |
872 | // Overridden in subclasses. | |
caa427d1 JF |
873 | } |
874 | ||
2634b249 JF |
875 | - (void) _didFinishLoading { |
876 | if ([loading_ count] != 0) | |
877 | return; | |
878 | ||
879 | [self applyRightButton]; | |
df30fbee | 880 | [[self navigationItem] setTitle:title_]; |
2634b249 | 881 | |
2713be8e JF |
882 | if ([self retainsNetworkActivityIndicator]) |
883 | [delegate_ releaseNetworkActivityIndicator]; | |
884 | ||
df30fbee | 885 | [self didFinishLoading]; |
caa427d1 JF |
886 | } |
887 | ||
2634b249 JF |
888 | - (bool) isLoading { |
889 | return [loading_ count] != 0; | |
caa427d1 JF |
890 | } |
891 | ||
b5e7eebb GP |
892 | - (id) initWithWidth:(float)width ofClass:(Class)_class { |
893 | if ((self = [super init]) != nil) { | |
6f837280 JF |
894 | width_ = width; |
895 | class_ = _class; | |
896 | ||
77259cab | 897 | [super setPageColor:nil]; |
5738f070 | 898 | |
df08be87 JF |
899 | allowsNavigationAction_ = true; |
900 | ||
7b33d201 | 901 | loading_ = [NSMutableSet setWithCapacity:5]; |
aa1e1906 | 902 | registered_ = [NSMutableSet setWithCapacity:5]; |
7b33d201 | 903 | indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease]; |
caa427d1 | 904 | |
6f837280 JF |
905 | reloaditem_ = [[[UIBarButtonItem alloc] |
906 | initWithTitle:UCLocalize("RELOAD") | |
907 | style:[self rightButtonStyle] | |
908 | target:self | |
909 | action:@selector(reloadButtonClicked) | |
910 | ] autorelease]; | |
65ea9562 | 911 | |
6f837280 | 912 | loadingitem_ = [[[UIBarButtonItem alloc] |
5328a9f0 | 913 | initWithTitle:(kCFCoreFoundationVersionNumber >= 800 ? @" " : @" ") |
6f837280 JF |
914 | style:UIBarButtonItemStylePlain |
915 | target:self | |
916 | action:@selector(reloadButtonClicked) | |
917 | ] autorelease]; | |
22f8bed9 | 918 | |
5328a9f0 JF |
919 | UIActivityIndicatorViewStyle style; |
920 | float left; | |
921 | if (kCFCoreFoundationVersionNumber >= 800) { | |
922 | style = UIActivityIndicatorViewStyleGray; | |
923 | left = 7; | |
924 | } else { | |
925 | style = UIActivityIndicatorViewStyleWhite; | |
926 | left = 15; | |
927 | } | |
928 | ||
929 | indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style] autorelease]; | |
930 | [indicator_ setFrame:CGRectMake(left, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)]; | |
6f837280 | 931 | [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
22f8bed9 | 932 | |
6f837280 JF |
933 | [self applyLeftButton]; |
934 | [self applyRightButton]; | |
935 | } return self; | |
936 | } | |
22f8bed9 | 937 | |
c6cf66c7 JF |
938 | - (NSString *) applicationNameForUserAgent { |
939 | return nil; | |
940 | } | |
941 | ||
6f837280 JF |
942 | - (void) loadView { |
943 | CGRect bounds([[UIScreen mainScreen] applicationFrame]); | |
ea173384 | 944 | |
6f837280 JF |
945 | webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease]; |
946 | [webview_ setDelegate:self]; | |
947 | [self setView:webview_]; | |
ea173384 | 948 | |
6f837280 JF |
949 | if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)]) |
950 | [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic]; | |
951 | else | |
952 | [webview_ setDetectsPhoneNumbers:NO]; | |
14f17703 | 953 | |
6f837280 | 954 | [webview_ setScalesPageToFit:YES]; |
ea173384 | 955 | |
6f837280 | 956 | UIWebDocumentView *document([webview_ _documentView]); |
22f8bed9 | 957 | |
6f837280 JF |
958 | // XXX: I think this improves scrolling; the hardcoded-ness sucks |
959 | [document setTileSize:CGSizeMake(320, 500)]; | |
22f8bed9 | 960 | |
6f837280 JF |
961 | WebView *webview([document webView]); |
962 | WebPreferences *preferences([webview preferences]); | |
72bdb258 | 963 | |
6f837280 JF |
964 | // XXX: I have no clue if I actually /want/ this modification |
965 | if ([webview respondsToSelector:@selector(_setLayoutInterval:)]) | |
966 | [webview _setLayoutInterval:0]; | |
967 | else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)]) | |
968 | [preferences _setLayoutInterval:0]; | |
22f8bed9 | 969 | |
6f837280 | 970 | [preferences setCacheModel:WebCacheModelDocumentBrowser]; |
d331bfc3 | 971 | [preferences setJavaScriptCanOpenWindowsAutomatically:NO]; |
64cc10e2 JF |
972 | |
973 | if ([preferences respondsToSelector:@selector(setOfflineWebApplicationCacheEnabled:)]) | |
974 | [preferences setOfflineWebApplicationCacheEnabled:YES]; | |
22f8bed9 | 975 | |
c6cf66c7 JF |
976 | if (NSString *agent = [self applicationNameForUserAgent]) |
977 | [webview setApplicationNameForUserAgent:agent]; | |
978 | ||
6f837280 JF |
979 | if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)]) |
980 | [webview setShouldUpdateWhileOffscreen:NO]; | |
22f8bed9 | 981 | |
6f837280 JF |
982 | #if LogMessages |
983 | if ([document respondsToSelector:@selector(setAllowsMessaging:)]) | |
984 | [document setAllowsMessaging:YES]; | |
985 | if ([webview respondsToSelector:@selector(_setAllowsMessaging:)]) | |
986 | [webview _setAllowsMessaging:YES]; | |
987 | #endif | |
3e9c9e85 | 988 | |
6f837280 JF |
989 | if ([webview_ respondsToSelector:@selector(_scrollView)]) { |
990 | scroller_ = [webview_ _scrollView]; | |
ea173384 | 991 | |
6f837280 JF |
992 | [scroller_ setDirectionalLockEnabled:YES]; |
993 | [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal]; | |
994 | [scroller_ setDelaysContentTouches:NO]; | |
ea173384 | 995 | |
6f837280 JF |
996 | [scroller_ setCanCancelContentTouches:YES]; |
997 | } else if ([webview_ respondsToSelector:@selector(_scroller)]) { | |
998 | UIScroller *scroller([webview_ _scroller]); | |
999 | scroller_ = (UIScrollView *) scroller; | |
bc11cf5b | 1000 | |
6f837280 JF |
1001 | [scroller setDirectionalScrolling:YES]; |
1002 | // XXX: we might be better off /not/ setting this on older systems | |
1003 | [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */ | |
1004 | [scroller setScrollHysteresis:0]; /* 8 */ | |
caa427d1 | 1005 | |
6f837280 | 1006 | [scroller setThumbDetectionEnabled:NO]; |
caa427d1 | 1007 | |
6f837280 JF |
1008 | // use NO with UIApplicationUseLegacyEvents(YES) |
1009 | [scroller setEventMode:YES]; | |
bc11cf5b | 1010 | |
6f837280 JF |
1011 | // XXX: this is handled by setBounces, right? |
1012 | //[scroller setAllowsRubberBanding:YES]; | |
1013 | } | |
bc11cf5b | 1014 | |
d32f96be | 1015 | [webview_ setOpaque:NO]; |
69b1d328 | 1016 | [webview_ setBackgroundColor:nil]; |
d32f96be | 1017 | |
6f837280 | 1018 | [scroller_ setFixedBackgroundPattern:YES]; |
5738f070 | 1019 | [scroller_ setBackgroundColor:color_]; |
6f837280 | 1020 | [scroller_ setClipsSubviews:YES]; |
2634b249 | 1021 | |
6f837280 JF |
1022 | [scroller_ setBounces:YES]; |
1023 | [scroller_ setScrollingEnabled:YES]; | |
1024 | [scroller_ setShowBackgroundShadow:NO]; | |
22f8bed9 | 1025 | |
6f837280 | 1026 | [self setViewportWidth:width_]; |
65ea9562 | 1027 | |
494216be JF |
1028 | if ([[UIColor groupTableViewBackgroundColor] isEqual:[UIColor clearColor]]) { |
1029 | UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]); | |
1030 | [table setScrollsToTop:NO]; | |
1031 | [webview_ insertSubview:table atIndex:0]; | |
1032 | [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; | |
1033 | } | |
1034 | ||
6f837280 | 1035 | [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; |
47a40da6 JF |
1036 | |
1037 | ready_ = false; | |
6f837280 JF |
1038 | } |
1039 | ||
1040 | - (void) releaseSubviews { | |
1041 | webview_ = nil; | |
1042 | scroller_ = nil; | |
7be3eb32 | 1043 | |
8b74eaff JF |
1044 | [self releaseNetworkActivityIndicator]; |
1045 | ||
7be3eb32 | 1046 | [super releaseSubviews]; |
22f8bed9 JF |
1047 | } |
1048 | ||
b5e7eebb GP |
1049 | - (id) initWithWidth:(float)width { |
1050 | return [self initWithWidth:width ofClass:[self class]]; | |
245cce8a JF |
1051 | } |
1052 | ||
b5e7eebb GP |
1053 | - (id) init { |
1054 | return [self initWithWidth:0]; | |
3e9c9e85 JF |
1055 | } |
1056 | ||
eb09425a JF |
1057 | - (id) initWithURL:(NSURL *)url { |
1058 | if ((self = [self init]) != nil) { | |
1059 | [self setURL:url]; | |
1060 | } return self; | |
1061 | } | |
1062 | ||
68bd7c60 JF |
1063 | - (id) initWithRequest:(NSURLRequest *)request { |
1064 | if ((self = [self init]) != nil) { | |
1065 | [self setRequest:request]; | |
1066 | } return self; | |
1067 | } | |
1068 | ||
d331bfc3 JF |
1069 | + (void) _lockJavaScript:(WebPreferences *)preferences { |
1070 | WebThreadLocked lock; | |
1071 | [preferences setJavaScriptCanOpenWindowsAutomatically:NO]; | |
1072 | } | |
1073 | ||
12b59862 | 1074 | - (void) callFunction:(WebScriptObject *)function { |
0893a034 | 1075 | WebThreadLocked lock; |
caa427d1 | 1076 | |
6f837280 | 1077 | WebView *webview([[[self webView] _documentView] webView]); |
d331bfc3 | 1078 | WebPreferences *preferences([webview preferences]); |
c21004b9 | 1079 | |
d331bfc3 JF |
1080 | [preferences setJavaScriptCanOpenWindowsAutomatically:YES]; |
1081 | if ([webview respondsToSelector:@selector(_preferencesChanged:)]) | |
1082 | [webview _preferencesChanged:preferences]; | |
1083 | else | |
1084 | [webview _preferencesChangedNotification:[NSNotification notificationWithName:@"" object:preferences]]; | |
1085 | ||
1086 | WebFrame *frame([webview mainFrame]); | |
12b59862 | 1087 | JSGlobalContextRef context([frame globalContext]); |
d331bfc3 | 1088 | |
9dbfe708 | 1089 | JSObjectRef object([function JSObject]); |
7c80833f JF |
1090 | if ($JSObjectCallAsFunction != NULL) |
1091 | ($JSObjectCallAsFunction)(context, object, NULL, 0, NULL, NULL); | |
d331bfc3 JF |
1092 | |
1093 | // XXX: the JavaScript code submits a form, which seems to happen asynchronously | |
1094 | NSObject *target([CyteWebViewController class]); | |
1095 | [NSObject cancelPreviousPerformRequestsWithTarget:target selector:@selector(_lockJavaScript:) object:preferences]; | |
1096 | [target performSelector:@selector(_lockJavaScript:) withObject:preferences afterDelay:1]; | |
12b59862 JF |
1097 | } |
1098 | ||
ce041f4f | 1099 | - (void) reloadButtonClicked { |
d69dbfc5 | 1100 | [self reloadURLWithCache:NO]; |
12b59862 JF |
1101 | } |
1102 | ||
719d6c2f DH |
1103 | - (void) _customButtonClicked { |
1104 | [self reloadButtonClicked]; | |
1105 | } | |
1106 | ||
ce041f4f | 1107 | - (void) customButtonClicked { |
caa427d1 JF |
1108 | #if !AlwaysReload |
1109 | if (function_ != nil) | |
12b59862 | 1110 | [self callFunction:function_]; |
caa427d1 JF |
1111 | else |
1112 | #endif | |
719d6c2f | 1113 | [self _customButtonClicked]; |
22f8bed9 JF |
1114 | } |
1115 | ||
3e9c9e85 JF |
1116 | + (float) defaultWidth { |
1117 | return 980; | |
1118 | } | |
1119 | ||
82406217 JF |
1120 | - (void) setNavigationBarStyle:(NSString *)name { |
1121 | UIBarStyle style; | |
1122 | if ([name isEqualToString:@"Black"]) | |
1123 | style = UIBarStyleBlack; | |
1124 | else | |
1125 | style = UIBarStyleDefault; | |
1126 | ||
1127 | [[[self navigationController] navigationBar] setBarStyle:style]; | |
1128 | } | |
1129 | ||
00984204 JF |
1130 | - (void) setNavigationBarTintColor:(UIColor *)color { |
1131 | [[[self navigationController] navigationBar] setTintColor:color]; | |
1132 | } | |
1133 | ||
c31c825d JF |
1134 | - (void) setBadgeValue:(id)value { |
1135 | [[[self navigationController] tabBarItem] setBadgeValue:value]; | |
1136 | } | |
1137 | ||
b8a5d89d JF |
1138 | - (void) setHidesBackButton:(bool)value { |
1139 | [[self navigationItem] setHidesBackButton:value]; | |
e6124cb6 | 1140 | [self applyLeftButton]; |
b8a5d89d JF |
1141 | } |
1142 | ||
1143 | - (void) setHidesBackButtonByNumber:(NSNumber *)value { | |
1144 | [self setHidesBackButton:[value boolValue]]; | |
1145 | } | |
1146 | ||
f196b921 | 1147 | - (void) dispatchEvent:(NSString *)event { |
6f837280 | 1148 | [[self webView] dispatchEvent:event]; |
f196b921 JF |
1149 | } |
1150 | ||
5cdfcd6f JF |
1151 | - (bool) hidesNavigationBar { |
1152 | return hidesNavigationBar_; | |
1153 | } | |
1154 | ||
1155 | - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated { | |
1156 | if (visible_) | |
1157 | [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated]; | |
1158 | } | |
1159 | ||
1160 | - (void) setHidesNavigationBar:(bool)value { | |
1161 | if (hidesNavigationBar_ != value) { | |
1162 | hidesNavigationBar_ = value; | |
1163 | [self _setHidesNavigationBar:YES animated:YES]; | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | - (void) setHidesNavigationBarByNumber:(NSNumber *)value { | |
1168 | [self setHidesNavigationBar:[value boolValue]]; | |
1169 | } | |
1170 | ||
db698f42 JF |
1171 | - (void) setScrollAlwaysBounceVertical:(bool)value { |
1172 | if ([webview_ respondsToSelector:@selector(_scrollView)]) { | |
1173 | UIScrollView *scroller([webview_ _scrollView]); | |
1174 | [scroller setAlwaysBounceVertical:value]; | |
1175 | } else if ([webview_ respondsToSelector:@selector(_scroller)]) { | |
1176 | //UIScroller *scroller([webview_ _scroller]); | |
1177 | // XXX: I am sad here. | |
1178 | } else return; | |
1179 | } | |
1180 | ||
1181 | - (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value { | |
1182 | [self setScrollAlwaysBounceVertical:[value boolValue]]; | |
1183 | } | |
1184 | ||
4886cc81 JF |
1185 | - (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style { |
1186 | if ([webview_ respondsToSelector:@selector(_scrollView)]) { | |
1187 | UIScrollView *scroller([webview_ _scrollView]); | |
1188 | [scroller setIndicatorStyle:style]; | |
1189 | } else if ([webview_ respondsToSelector:@selector(_scroller)]) { | |
1190 | UIScroller *scroller([webview_ _scroller]); | |
1191 | [scroller setScrollerIndicatorStyle:style]; | |
1192 | } else return; | |
1193 | } | |
1194 | ||
1195 | - (void) setScrollIndicatorStyleWithName:(NSString *)style { | |
1196 | UIScrollViewIndicatorStyle value; | |
1197 | ||
1198 | if (false); | |
1199 | else if ([style isEqualToString:@"default"]) | |
1200 | value = UIScrollViewIndicatorStyleDefault; | |
1201 | else if ([style isEqualToString:@"black"]) | |
1202 | value = UIScrollViewIndicatorStyleBlack; | |
1203 | else if ([style isEqualToString:@"white"]) | |
1204 | value = UIScrollViewIndicatorStyleWhite; | |
1205 | else return; | |
1206 | ||
1207 | [self setScrollIndicatorStyle:value]; | |
1208 | } | |
1209 | ||
f196b921 | 1210 | - (void) viewWillAppear:(BOOL)animated { |
5cdfcd6f JF |
1211 | visible_ = true; |
1212 | ||
1213 | if ([self hidesNavigationBar]) | |
1214 | [self _setHidesNavigationBar:YES animated:animated]; | |
1215 | ||
3cf38067 JF |
1216 | // XXX: why isn't this evern called automatically? |
1217 | [[self webView] setNeedsLayout]; | |
1218 | ||
f196b921 JF |
1219 | [self dispatchEvent:@"CydiaViewWillAppear"]; |
1220 | [super viewWillAppear:animated]; | |
1221 | } | |
1222 | ||
1223 | - (void) viewDidAppear:(BOOL)animated { | |
f196b921 | 1224 | [super viewDidAppear:animated]; |
5cdfcd6f | 1225 | [self dispatchEvent:@"CydiaViewDidAppear"]; |
f196b921 JF |
1226 | } |
1227 | ||
1228 | - (void) viewWillDisappear:(BOOL)animated { | |
1229 | [self dispatchEvent:@"CydiaViewWillDisappear"]; | |
1230 | [super viewWillDisappear:animated]; | |
5cdfcd6f JF |
1231 | |
1232 | if ([self hidesNavigationBar]) | |
1233 | [self _setHidesNavigationBar:NO animated:animated]; | |
1234 | ||
1235 | visible_ = false; | |
f196b921 JF |
1236 | } |
1237 | ||
1238 | - (void) viewDidDisappear:(BOOL)animated { | |
f196b921 | 1239 | [super viewDidDisappear:animated]; |
5cdfcd6f | 1240 | [self dispatchEvent:@"CydiaViewDidDisappear"]; |
f196b921 JF |
1241 | } |
1242 | ||
aa1e1906 JF |
1243 | - (void) updateHeights:(NSTimer *)timer { |
1244 | for (WebFrame *frame in (id) registered_) | |
1245 | [frame cydia$updateHeight]; | |
1246 | } | |
1247 | ||
1248 | - (void) registerFrame:(WebFrame *)frame { | |
1249 | [registered_ addObject:frame]; | |
1250 | ||
1251 | if (timer_ == nil) | |
1252 | timer_ = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateHeights:) userInfo:nil repeats:YES]; | |
1253 | } | |
1254 | ||
a5938ea5 | 1255 | @end |