]> git.saurik.com Git - cydia.git/blame - CyteKit/WebViewController.mm
Remove dependency on Substrate to have its macros.
[cydia.git] / CyteKit / WebViewController.mm
CommitLineData
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
16extern 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
41JSValueRef (*$JSObjectCallAsFunction)(JSContextRef, JSObjectRef, JSObjectRef, size_t, const JSValueRef[], JSValueRef *);
42
2b2a4e33
JF
43// XXX: centralize these special class things to some file or mechanism?
44static Class $MFMailComposeViewController;
45
9c1605e2
JF
46float 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 464 NSURL *url(request == nil ? nil : [request URL]);
c623466b
JF
465 NSString *scheme([[url scheme] lowercaseString]);
466 NSString *absolute([[url absoluteString] lowercaseString]);
467
468 if (
469 [scheme isEqualToString:@"itms"] ||
470 [scheme isEqualToString:@"itmss"] ||
471 [scheme isEqualToString:@"itms-apps"] ||
472 [scheme isEqualToString:@"itms-appss"] ||
473 [absolute hasPrefix:@"http://itunes.apple.com/"] ||
474 [absolute hasPrefix:@"https://itunes.apple.com/"] ||
475 false) {
1002f4d9
JF
476 appstore_ = url;
477
478 UIAlertView *alert = [[[UIAlertView alloc]
479 initWithTitle:UCLocalize("APP_STORE_REDIRECT")
480 message:nil
481 delegate:self
482 cancelButtonTitle:UCLocalize("CANCEL")
483 otherButtonTitles:
484 UCLocalize("ALLOW"),
485 nil
486 ] autorelease];
487
488 [alert setContext:@"itmsappss"];
489 [alert show];
490
491 [listener ignore];
492 return;
493 }
494
52498c7e
JF
495 if ([frame parentFrame] == nil) {
496 if (!error_) {
d323285e 497 if (request_ != nil && ![[request_ URL] isEqual:url] && ![self allowsNavigationAction]) {
8366df5e 498 if (url != nil)
f050e4d9 499 [self pushRequest:request forAction:action asPop:NO];
52498c7e
JF
500 [listener ignore];
501 }
502 }
503 }
504}
505
d323285e 506- (void) webView:(WebView *)view didDecidePolicy:(CYWebPolicyDecision)decision forNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame {
77d4e15b
JF
507#if LogBrowser
508 NSLog(@"didDecidePolicy:%u forNavigationAction:%@ request:%@ frame:%@", decision, action, request, [request allHTTPHeaderFields], frame);
509#endif
510
511 if ([frame parentFrame] == nil) {
512 switch (decision) {
513 case CYWebPolicyDecisionIgnore:
514 if ([[request_ URL] isEqual:[request URL]])
515 request_ = nil;
516 break;
517
518 case CYWebPolicyDecisionUse:
519 if (!error_)
520 request_ = request;
521 break;
522
523 default:
524 break;
525 }
526 }
d323285e
JF
527}
528
f050e4d9 529- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener {
52498c7e 530#if LogBrowser
f050e4d9 531 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ %@ newFrameName:%@", action, request, [request allHTTPHeaderFields], name);
52498c7e
JF
532#endif
533
534 NSURL *url([request URL]);
535 if (url == nil)
536 return;
537
f050e4d9 538 if ([name isEqualToString:@"_open"])
52498c7e
JF
539 [delegate_ openURL:url];
540 else {
541 NSString *scheme([[url scheme] lowercaseString]);
542 if ([scheme isEqualToString:@"mailto"])
543 [self _openMailToURL:url];
544 else
f050e4d9 545 [self pushRequest:request forAction:action asPop:[name isEqualToString:@"_popup"]];
52498c7e 546 }
22f8bed9 547
2634b249 548 [listener ignore];
22f8bed9
JF
549}
550
2634b249 551- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
c215c5ed
JF
552#if LogBrowser
553 NSLog(@"didClearWindowObject:%@ forFrame:%@", window, frame);
554#endif
22f8bed9
JF
555}
556
64ffa332
JF
557- (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
558#if LogBrowser
559 NSLog(@"didCommitLoadForFrame:%@", frame);
560#endif
561
562 if ([frame parentFrame] == nil) {
b3be8c0c 563 loaded_ = true;
64ffa332
JF
564 }
565}
566
2634b249
JF
567- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
568#if LogBrowser
569 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
570#endif
571
572 [self _didFailWithError:error forFrame:frame];
22f8bed9
JF
573}
574
2634b249
JF
575- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
576#if LogBrowser
577 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
578#endif
579
580 [self _didFailWithError:error forFrame:frame];
22f8bed9
JF
581}
582
2634b249 583- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
8b74eaff
JF
584 NSValue *object([NSValue valueWithNonretainedObject:frame]);
585 if (![loading_ containsObject:object])
586 return;
587 [loading_ removeObject:object];
22f8bed9 588
caa427d1 589 if ([frame parentFrame] == nil) {
22f8bed9 590 if (DOMDocument *document = [frame DOMDocument])
4cc9e99a 591 if (DOMNodeList *bodies = [document getElementsByTagName:@"body"])
96f3833b 592 for (DOMHTMLBodyElement *body in (id) bodies) {
22f8bed9
JF
593 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
594
65ea9562 595 UIColor *uic(nil);
22f8bed9
JF
596
597 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
fe468f45
JF
598 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
599 DOMRGBColor *rgb([color getRGBColorValue]);
600
601 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
602 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
603 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
604 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
605
d32f96be
JF
606 if (alpha == 1)
607 uic = [UIColor
608 colorWithRed:(red / 255)
609 green:(green / 255)
610 blue:(blue / 255)
611 alpha:alpha
612 ];
22f8bed9
JF
613 }
614 }
615
77259cab 616 [super setPageColor:uic];
d32f96be 617 [scroller_ setBackgroundColor:color_];
22f8bed9
JF
618 break;
619 }
620 }
621
2634b249 622 [self _didFinishLoading];
22f8bed9
JF
623}
624
2634b249
JF
625- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
626 if ([frame parentFrame] != nil)
627 return;
caa427d1 628
7b33d201 629 title_ = title;
3931b718 630
2634b249
JF
631 [[self navigationItem] setTitle:title_];
632}
22f8bed9 633
2634b249 634- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
a86042fa
JF
635#if LogBrowser
636 NSLog(@"didStartProvisionalLoadForFrame:%@", frame);
637#endif
638
2634b249 639 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
7592e053 640
caa427d1 641 if ([frame parentFrame] == nil) {
7b33d201 642 title_ = nil;
46d3a5cf
JF
643 custom_ = nil;
644 style_ = nil;
645 function_ = nil;
2634b249 646
aa1e1906
JF
647 [registered_ removeAllObjects];
648 timer_ = nil;
649
622a9912
JF
650 allowsNavigationAction_ = true;
651
5cdfcd6f 652 [self setHidesNavigationBar:NO];
db698f42 653 [self setScrollAlwaysBounceVertical:true];
4886cc81 654 [self setScrollIndicatorStyle:UIScrollViewIndicatorStyleDefault];
5cdfcd6f 655
2634b249
JF
656 // XXX: do we still need to do this?
657 [[self navigationItem] setTitle:nil];
caa427d1 658 }
fe468f45 659
2634b249 660 [self _didStartLoading];
caa427d1
JF
661}
662
754456f5
JF
663- (void) webView:(WebView *)view resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
664 challenge_ = [challenge retain];
665
666 NSURLProtectionSpace *space([challenge protectionSpace]);
667 NSString *realm([space realm]);
668 if (realm == nil)
669 realm = @"";
670
671 UIAlertView *alert = [[[UIAlertView alloc]
672 initWithTitle:realm
673 message:nil
674 delegate:self
675 cancelButtonTitle:UCLocalize("CANCEL")
676 otherButtonTitles:UCLocalize("LOGIN"), nil
677 ] autorelease];
678
679 [alert setContext:@"challenge"];
680 [alert setNumberOfRows:1];
681
682 [alert addTextFieldWithValue:@"" label:UCLocalize("USERNAME")];
683 [alert addTextFieldWithValue:@"" label:UCLocalize("PASSWORD")];
684
685 UITextField *username([alert textFieldAtIndex:0]); {
686 UITextInputTraits *traits([username textInputTraits]);
687 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
688 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
689 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
690 [traits setReturnKeyType:UIReturnKeyNext];
691 }
692
693 UITextField *password([alert textFieldAtIndex:1]); {
694 UITextInputTraits *traits([password textInputTraits]);
695 [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone];
696 [traits setAutocorrectionType:UITextAutocorrectionTypeNo];
697 [traits setKeyboardType:UIKeyboardTypeASCIICapable];
698 // XXX: UIReturnKeyDone
699 [traits setReturnKeyType:UIReturnKeyNext];
700 [traits setSecureTextEntry:YES];
701 }
702
703 [alert show];
704}
705
2634b249 706- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
0352f238
JF
707#if LogBrowser
708 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
709#endif
710
2634b249 711 return request;
22f8bed9
JF
712}
713
92de61aa
JF
714- (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
715#if LogBrowser
716 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
717#endif
718
719 return request;
720}
721
2634b249
JF
722- (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
723 return [self _allowJavaScriptPanel];
22f8bed9
JF
724}
725
2634b249
JF
726- (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
727 return [self _allowJavaScriptPanel];
600d005d
JF
728}
729
2634b249
JF
730- (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
731 return [self _allowJavaScriptPanel];
600d005d
JF
732}
733
2634b249
JF
734- (void) webViewClose:(WebView *)view {
735 [self close];
600d005d 736}
2634b249 737// }}}
600d005d 738
2634b249 739- (void) close {
19f2d77f 740 [[[self navigationController] parentOrPresentingViewController] dismissModalViewControllerAnimated:YES];
600d005d
JF
741}
742
2634b249
JF
743- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
744 NSString *context([alert context]);
600d005d 745
2634b249
JF
746 if ([context isEqualToString:@"sensitive"]) {
747 switch (button) {
748 case 1:
749 sensitive_ = [NSNumber numberWithBool:YES];
750 break;
600d005d 751
2634b249
JF
752 case 2:
753 sensitive_ = [NSNumber numberWithBool:NO];
754 break;
755 }
600d005d 756
2634b249
JF
757 [alert dismissWithClickedButtonIndex:-1 animated:YES];
758 } else if ([context isEqualToString:@"challenge"]) {
759 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
caa427d1 760
754456f5
JF
761 if (button == [alert cancelButtonIndex])
762 [sender cancelAuthenticationChallenge:challenge_];
763 else if (button == [alert firstOtherButtonIndex]) {
764 NSString *username([[alert textFieldAtIndex:0] text]);
765 NSString *password([[alert textFieldAtIndex:1] text]);
600d005d 766
754456f5 767 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
600d005d 768
754456f5 769 [sender useCredential:credential forAuthenticationChallenge:challenge_];
2634b249 770 }
600d005d 771
2634b249 772 challenge_ = nil;
600d005d 773
1002f4d9
JF
774 [alert dismissWithClickedButtonIndex:-1 animated:YES];
775 } else if ([context isEqualToString:@"itmsappss"]) {
776 if (button == [alert cancelButtonIndex]) {
777 } else if (button == [alert firstOtherButtonIndex]) {
778 [delegate_ openURL:appstore_];
779 }
780
2634b249
JF
781 [alert dismissWithClickedButtonIndex:-1 animated:YES];
782 } else if ([context isEqualToString:@"submit"]) {
75b95256
JF
783 if (button == [alert cancelButtonIndex]) {
784 } else if (button == [alert firstOtherButtonIndex]) {
785 if (request_ != nil) {
0893a034 786 WebThreadLocked lock;
6f837280 787 [[self webView] loadRequest:request_];
75b95256 788 }
2634b249 789 }
600d005d 790
2634b249
JF
791 [alert dismissWithClickedButtonIndex:-1 animated:YES];
792 }
600d005d
JF
793}
794
2634b249
JF
795- (UIBarButtonItemStyle) rightButtonStyle {
796 if (style_ == nil) normal:
797 return UIBarButtonItemStylePlain;
798 else if ([style_ isEqualToString:@"Normal"])
799 return UIBarButtonItemStylePlain;
800 else if ([style_ isEqualToString:@"Highlighted"])
801 return UIBarButtonItemStyleDone;
802 else goto normal;
600d005d
JF
803}
804
2634b249 805- (UIBarButtonItem *) customButton {
29756674
JF
806 if (custom_ == nil)
807 return nil;
4cc9e99a 808 else if ((/*clang:*/id) custom_ == [NSNull null])
29756674
JF
809 return (UIBarButtonItem *) [NSNull null];
810
811 return [[[UIBarButtonItem alloc]
ed5566c7 812 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
2634b249
JF
813 style:[self rightButtonStyle]
814 target:self
815 action:@selector(customButtonClicked)
816 ] autorelease];
caa427d1
JF
817}
818
e6124cb6
JF
819- (UIBarButtonItem *) leftButton {
820 UINavigationItem *item([self navigationItem]);
821 if ([item backBarButtonItem] != nil && ![item hidesBackButton])
822 return nil;
823
824 if (UINavigationController *navigation = [self navigationController])
19f2d77f 825 if ([[navigation parentOrPresentingViewController] modalViewController] == navigation)
e6124cb6
JF
826 return [[[UIBarButtonItem alloc]
827 initWithTitle:UCLocalize("CLOSE")
828 style:UIBarButtonItemStylePlain
829 target:self
830 action:@selector(close)
831 ] autorelease];
832
833 return nil;
834}
835
836- (void) applyLeftButton {
837 [[self navigationItem] setLeftBarButtonItem:[self leftButton]];
838}
839
2634b249
JF
840- (UIBarButtonItem *) rightButton {
841 return reloaditem_;
eb35c522
JF
842}
843
2634b249
JF
844- (void) applyLoadingTitle {
845 [[self navigationItem] setTitle:UCLocalize("LOADING")];
caa427d1
JF
846}
847
c83a464d
JF
848- (void) layoutRightButton {
849 [[loadingitem_ view] addSubview:indicator_];
850 [[loadingitem_ view] bringSubviewToFront:indicator_];
851}
852
2634b249
JF
853- (void) applyRightButton {
854 if ([self isLoading]) {
855 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
c83a464d 856 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
8d603a7d
JF
857
858 [indicator_ startAnimating];
2634b249 859 [self applyLoadingTitle];
2634b249 860 } else {
8d603a7d
JF
861 [indicator_ stopAnimating];
862
29756674
JF
863 UIBarButtonItem *button([self customButton]);
864 if (button == nil)
865 button = [self rightButton];
866 else if (button == (UIBarButtonItem *) [NSNull null])
867 button = nil;
868
bf965437 869 [[self navigationItem] setRightBarButtonItem:button animated:YES];
2634b249 870 }
caa427d1
JF
871}
872
df30fbee
GP
873- (void) didStartLoading {
874 // Overridden in subclasses.
875}
876
2634b249
JF
877- (void) _didStartLoading {
878 [self applyRightButton];
54043703
JF
879
880 if ([loading_ count] != 1)
881 return;
df30fbee 882
2713be8e
JF
883 if ([self retainsNetworkActivityIndicator])
884 [delegate_ retainNetworkActivityIndicator];
885
df30fbee
GP
886 [self didStartLoading];
887}
888
889- (void) didFinishLoading {
890 // Overridden in subclasses.
caa427d1
JF
891}
892
2634b249
JF
893- (void) _didFinishLoading {
894 if ([loading_ count] != 0)
895 return;
896
897 [self applyRightButton];
df30fbee 898 [[self navigationItem] setTitle:title_];
2634b249 899
2713be8e
JF
900 if ([self retainsNetworkActivityIndicator])
901 [delegate_ releaseNetworkActivityIndicator];
902
df30fbee 903 [self didFinishLoading];
caa427d1
JF
904}
905
2634b249
JF
906- (bool) isLoading {
907 return [loading_ count] != 0;
caa427d1
JF
908}
909
b5e7eebb
GP
910- (id) initWithWidth:(float)width ofClass:(Class)_class {
911 if ((self = [super init]) != nil) {
6f837280
JF
912 width_ = width;
913 class_ = _class;
914
77259cab 915 [super setPageColor:nil];
5738f070 916
df08be87
JF
917 allowsNavigationAction_ = true;
918
7b33d201 919 loading_ = [NSMutableSet setWithCapacity:5];
aa1e1906 920 registered_ = [NSMutableSet setWithCapacity:5];
7b33d201 921 indirect_ = [[[IndirectDelegate alloc] initWithDelegate:self] autorelease];
caa427d1 922
6f837280
JF
923 reloaditem_ = [[[UIBarButtonItem alloc]
924 initWithTitle:UCLocalize("RELOAD")
925 style:[self rightButtonStyle]
926 target:self
927 action:@selector(reloadButtonClicked)
928 ] autorelease];
65ea9562 929
6f837280 930 loadingitem_ = [[[UIBarButtonItem alloc]
5328a9f0 931 initWithTitle:(kCFCoreFoundationVersionNumber >= 800 ? @" " : @" ")
6f837280
JF
932 style:UIBarButtonItemStylePlain
933 target:self
934 action:@selector(reloadButtonClicked)
935 ] autorelease];
22f8bed9 936
5328a9f0
JF
937 UIActivityIndicatorViewStyle style;
938 float left;
939 if (kCFCoreFoundationVersionNumber >= 800) {
940 style = UIActivityIndicatorViewStyleGray;
941 left = 7;
942 } else {
943 style = UIActivityIndicatorViewStyleWhite;
944 left = 15;
945 }
946
947 indicator_ = [[[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:style] autorelease];
948 [indicator_ setFrame:CGRectMake(left, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
6f837280 949 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
22f8bed9 950
6f837280
JF
951 [self applyLeftButton];
952 [self applyRightButton];
953 } return self;
954}
22f8bed9 955
c6cf66c7
JF
956- (NSString *) applicationNameForUserAgent {
957 return nil;
958}
959
6f837280
JF
960- (void) loadView {
961 CGRect bounds([[UIScreen mainScreen] applicationFrame]);
ea173384 962
6f837280
JF
963 webview_ = [[[CyteWebView alloc] initWithFrame:bounds] autorelease];
964 [webview_ setDelegate:self];
965 [self setView:webview_];
ea173384 966
6f837280
JF
967 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
968 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
969 else
970 [webview_ setDetectsPhoneNumbers:NO];
14f17703 971
6f837280 972 [webview_ setScalesPageToFit:YES];
ea173384 973
6f837280 974 UIWebDocumentView *document([webview_ _documentView]);
22f8bed9 975
6f837280
JF
976 // XXX: I think this improves scrolling; the hardcoded-ness sucks
977 [document setTileSize:CGSizeMake(320, 500)];
22f8bed9 978
6f837280
JF
979 WebView *webview([document webView]);
980 WebPreferences *preferences([webview preferences]);
72bdb258 981
6f837280
JF
982 // XXX: I have no clue if I actually /want/ this modification
983 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
984 [webview _setLayoutInterval:0];
985 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
986 [preferences _setLayoutInterval:0];
22f8bed9 987
6f837280 988 [preferences setCacheModel:WebCacheModelDocumentBrowser];
d331bfc3 989 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
64cc10e2
JF
990
991 if ([preferences respondsToSelector:@selector(setOfflineWebApplicationCacheEnabled:)])
992 [preferences setOfflineWebApplicationCacheEnabled:YES];
22f8bed9 993
c6cf66c7
JF
994 if (NSString *agent = [self applicationNameForUserAgent])
995 [webview setApplicationNameForUserAgent:agent];
996
6f837280
JF
997 if ([webview respondsToSelector:@selector(setShouldUpdateWhileOffscreen:)])
998 [webview setShouldUpdateWhileOffscreen:NO];
22f8bed9 999
6f837280
JF
1000#if LogMessages
1001 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
1002 [document setAllowsMessaging:YES];
1003 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
1004 [webview _setAllowsMessaging:YES];
1005#endif
3e9c9e85 1006
6f837280
JF
1007 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1008 scroller_ = [webview_ _scrollView];
ea173384 1009
6f837280
JF
1010 [scroller_ setDirectionalLockEnabled:YES];
1011 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
1012 [scroller_ setDelaysContentTouches:NO];
ea173384 1013
6f837280
JF
1014 [scroller_ setCanCancelContentTouches:YES];
1015 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1016 UIScroller *scroller([webview_ _scroller]);
1017 scroller_ = (UIScrollView *) scroller;
bc11cf5b 1018
6f837280
JF
1019 [scroller setDirectionalScrolling:YES];
1020 // XXX: we might be better off /not/ setting this on older systems
1021 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
1022 [scroller setScrollHysteresis:0]; /* 8 */
caa427d1 1023
6f837280 1024 [scroller setThumbDetectionEnabled:NO];
caa427d1 1025
6f837280
JF
1026 // use NO with UIApplicationUseLegacyEvents(YES)
1027 [scroller setEventMode:YES];
bc11cf5b 1028
6f837280
JF
1029 // XXX: this is handled by setBounces, right?
1030 //[scroller setAllowsRubberBanding:YES];
1031 }
bc11cf5b 1032
d32f96be 1033 [webview_ setOpaque:NO];
69b1d328 1034 [webview_ setBackgroundColor:nil];
d32f96be 1035
6f837280 1036 [scroller_ setFixedBackgroundPattern:YES];
5738f070 1037 [scroller_ setBackgroundColor:color_];
6f837280 1038 [scroller_ setClipsSubviews:YES];
2634b249 1039
6f837280
JF
1040 [scroller_ setBounces:YES];
1041 [scroller_ setScrollingEnabled:YES];
1042 [scroller_ setShowBackgroundShadow:NO];
22f8bed9 1043
6f837280 1044 [self setViewportWidth:width_];
65ea9562 1045
494216be
JF
1046 if ([[UIColor groupTableViewBackgroundColor] isEqual:[UIColor clearColor]]) {
1047 UITableView *table([[[UITableView alloc] initWithFrame:[webview_ bounds] style:UITableViewStyleGrouped] autorelease]);
1048 [table setScrollsToTop:NO];
1049 [webview_ insertSubview:table atIndex:0];
1050 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1051 }
1052
6f837280 1053 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
47a40da6
JF
1054
1055 ready_ = false;
6f837280
JF
1056}
1057
1058- (void) releaseSubviews {
1059 webview_ = nil;
1060 scroller_ = nil;
7be3eb32 1061
8b74eaff
JF
1062 [self releaseNetworkActivityIndicator];
1063
7be3eb32 1064 [super releaseSubviews];
22f8bed9
JF
1065}
1066
b5e7eebb
GP
1067- (id) initWithWidth:(float)width {
1068 return [self initWithWidth:width ofClass:[self class]];
245cce8a
JF
1069}
1070
b5e7eebb
GP
1071- (id) init {
1072 return [self initWithWidth:0];
3e9c9e85
JF
1073}
1074
eb09425a
JF
1075- (id) initWithURL:(NSURL *)url {
1076 if ((self = [self init]) != nil) {
1077 [self setURL:url];
1078 } return self;
1079}
1080
68bd7c60
JF
1081- (id) initWithRequest:(NSURLRequest *)request {
1082 if ((self = [self init]) != nil) {
1083 [self setRequest:request];
1084 } return self;
1085}
1086
d331bfc3
JF
1087+ (void) _lockJavaScript:(WebPreferences *)preferences {
1088 WebThreadLocked lock;
1089 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1090}
1091
12b59862 1092- (void) callFunction:(WebScriptObject *)function {
0893a034 1093 WebThreadLocked lock;
caa427d1 1094
6f837280 1095 WebView *webview([[[self webView] _documentView] webView]);
d331bfc3 1096 WebPreferences *preferences([webview preferences]);
c21004b9 1097
d331bfc3
JF
1098 [preferences setJavaScriptCanOpenWindowsAutomatically:YES];
1099 if ([webview respondsToSelector:@selector(_preferencesChanged:)])
1100 [webview _preferencesChanged:preferences];
1101 else
1102 [webview _preferencesChangedNotification:[NSNotification notificationWithName:@"" object:preferences]];
1103
1104 WebFrame *frame([webview mainFrame]);
12b59862 1105 JSGlobalContextRef context([frame globalContext]);
d331bfc3 1106
9dbfe708 1107 JSObjectRef object([function JSObject]);
7c80833f
JF
1108 if ($JSObjectCallAsFunction != NULL)
1109 ($JSObjectCallAsFunction)(context, object, NULL, 0, NULL, NULL);
d331bfc3
JF
1110
1111 // XXX: the JavaScript code submits a form, which seems to happen asynchronously
1112 NSObject *target([CyteWebViewController class]);
1113 [NSObject cancelPreviousPerformRequestsWithTarget:target selector:@selector(_lockJavaScript:) object:preferences];
1114 [target performSelector:@selector(_lockJavaScript:) withObject:preferences afterDelay:1];
12b59862
JF
1115}
1116
ce041f4f 1117- (void) reloadButtonClicked {
d69dbfc5 1118 [self reloadURLWithCache:NO];
12b59862
JF
1119}
1120
719d6c2f
DH
1121- (void) _customButtonClicked {
1122 [self reloadButtonClicked];
1123}
1124
ce041f4f 1125- (void) customButtonClicked {
caa427d1
JF
1126#if !AlwaysReload
1127 if (function_ != nil)
12b59862 1128 [self callFunction:function_];
caa427d1
JF
1129 else
1130#endif
719d6c2f 1131 [self _customButtonClicked];
22f8bed9
JF
1132}
1133
3e9c9e85
JF
1134+ (float) defaultWidth {
1135 return 980;
1136}
1137
82406217
JF
1138- (void) setNavigationBarStyle:(NSString *)name {
1139 UIBarStyle style;
1140 if ([name isEqualToString:@"Black"])
1141 style = UIBarStyleBlack;
1142 else
1143 style = UIBarStyleDefault;
1144
1145 [[[self navigationController] navigationBar] setBarStyle:style];
1146}
1147
00984204
JF
1148- (void) setNavigationBarTintColor:(UIColor *)color {
1149 [[[self navigationController] navigationBar] setTintColor:color];
1150}
1151
c31c825d
JF
1152- (void) setBadgeValue:(id)value {
1153 [[[self navigationController] tabBarItem] setBadgeValue:value];
1154}
1155
b8a5d89d
JF
1156- (void) setHidesBackButton:(bool)value {
1157 [[self navigationItem] setHidesBackButton:value];
e6124cb6 1158 [self applyLeftButton];
b8a5d89d
JF
1159}
1160
1161- (void) setHidesBackButtonByNumber:(NSNumber *)value {
1162 [self setHidesBackButton:[value boolValue]];
1163}
1164
f196b921 1165- (void) dispatchEvent:(NSString *)event {
6f837280 1166 [[self webView] dispatchEvent:event];
f196b921
JF
1167}
1168
5cdfcd6f
JF
1169- (bool) hidesNavigationBar {
1170 return hidesNavigationBar_;
1171}
1172
1173- (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1174 if (visible_)
1175 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1176}
1177
1178- (void) setHidesNavigationBar:(bool)value {
1179 if (hidesNavigationBar_ != value) {
1180 hidesNavigationBar_ = value;
1181 [self _setHidesNavigationBar:YES animated:YES];
1182 }
1183}
1184
1185- (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1186 [self setHidesNavigationBar:[value boolValue]];
1187}
1188
db698f42
JF
1189- (void) setScrollAlwaysBounceVertical:(bool)value {
1190 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1191 UIScrollView *scroller([webview_ _scrollView]);
1192 [scroller setAlwaysBounceVertical:value];
1193 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1194 //UIScroller *scroller([webview_ _scroller]);
1195 // XXX: I am sad here.
1196 } else return;
1197}
1198
1199- (void) setScrollAlwaysBounceVerticalNumber:(NSNumber *)value {
1200 [self setScrollAlwaysBounceVertical:[value boolValue]];
1201}
1202
4886cc81
JF
1203- (void) setScrollIndicatorStyle:(UIScrollViewIndicatorStyle)style {
1204 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1205 UIScrollView *scroller([webview_ _scrollView]);
1206 [scroller setIndicatorStyle:style];
1207 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1208 UIScroller *scroller([webview_ _scroller]);
1209 [scroller setScrollerIndicatorStyle:style];
1210 } else return;
1211}
1212
1213- (void) setScrollIndicatorStyleWithName:(NSString *)style {
1214 UIScrollViewIndicatorStyle value;
1215
1216 if (false);
1217 else if ([style isEqualToString:@"default"])
1218 value = UIScrollViewIndicatorStyleDefault;
1219 else if ([style isEqualToString:@"black"])
1220 value = UIScrollViewIndicatorStyleBlack;
1221 else if ([style isEqualToString:@"white"])
1222 value = UIScrollViewIndicatorStyleWhite;
1223 else return;
1224
1225 [self setScrollIndicatorStyle:value];
1226}
1227
f196b921 1228- (void) viewWillAppear:(BOOL)animated {
5cdfcd6f
JF
1229 visible_ = true;
1230
1231 if ([self hidesNavigationBar])
1232 [self _setHidesNavigationBar:YES animated:animated];
1233
3cf38067
JF
1234 // XXX: why isn't this evern called automatically?
1235 [[self webView] setNeedsLayout];
1236
f196b921
JF
1237 [self dispatchEvent:@"CydiaViewWillAppear"];
1238 [super viewWillAppear:animated];
1239}
1240
1241- (void) viewDidAppear:(BOOL)animated {
f196b921 1242 [super viewDidAppear:animated];
5cdfcd6f 1243 [self dispatchEvent:@"CydiaViewDidAppear"];
f196b921
JF
1244}
1245
1246- (void) viewWillDisappear:(BOOL)animated {
1247 [self dispatchEvent:@"CydiaViewWillDisappear"];
1248 [super viewWillDisappear:animated];
5cdfcd6f
JF
1249
1250 if ([self hidesNavigationBar])
1251 [self _setHidesNavigationBar:NO animated:animated];
1252
1253 visible_ = false;
f196b921
JF
1254}
1255
1256- (void) viewDidDisappear:(BOOL)animated {
f196b921 1257 [super viewDidDisappear:animated];
5cdfcd6f 1258 [self dispatchEvent:@"CydiaViewDidDisappear"];
f196b921
JF
1259}
1260
aa1e1906
JF
1261- (void) updateHeights:(NSTimer *)timer {
1262 for (WebFrame *frame in (id) registered_)
1263 [frame cydia$updateHeight];
1264}
1265
1266- (void) registerFrame:(WebFrame *)frame {
1267 [registered_ addObject:frame];
1268
1269 if (timer_ == nil)
1270 timer_ = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(updateHeights:) userInfo:nil repeats:YES];
1271}
1272
a5938ea5 1273@end