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