]> git.saurik.com Git - cydia.git/blob - UICaboodle/BrowserView.mm
201e85415c385f7e4db48ea67eb0dc291fbd2d8f
[cydia.git] / UICaboodle / BrowserView.mm
1 #include <UIKit/UIKit.h>
2 #include "iPhonePrivate.h"
3
4 #include "UCPlatform.h"
5
6 #include <UICaboodle/BrowserView.h>
7 #include <UICaboodle/UCLocalize.h>
8
9 //#include <QuartzCore/CALayer.h>
10 // XXX: fix the minimum requirement
11 extern NSString * const kCAFilterNearest;
12
13 #include <WebCore/WebCoreThread.h>
14
15 #include <WebKit/WebPolicyDelegate.h>
16 #include <WebKit/WebPreferences.h>
17
18 #include <WebKit/DOMCSSPrimitiveValue.h>
19 #include <WebKit/DOMCSSStyleDeclaration.h>
20 #include <WebKit/DOMDocument.h>
21 #include <WebKit/DOMHTMLBodyElement.h>
22 #include <WebKit/DOMRGBColor.h>
23
24 //#include <WebCore/Page.h>
25 //#include <WebCore/Settings.h>
26
27 #include "substrate.h"
28
29 #define ForSaurik 0
30 #define DefaultTimeout_ 120.0
31
32 #define ShowInternals 0
33 #define LogBrowser 0
34 #define LogMessages 0
35
36 #define lprintf(args...) fprintf(stderr, args)
37
38 // WebThreadLocked {{{
39 struct WebThreadLocked {
40 _finline WebThreadLocked() {
41 WebThreadLock();
42 }
43
44 _finline ~WebThreadLocked() {
45 WebThreadUnlock();
46 }
47 };
48 // }}}
49
50 template <typename Type_>
51 static inline void CYRelease(Type_ &value) {
52 if (value != nil) {
53 [value release];
54 value = nil;
55 }
56 }
57
58 float CYScrollViewDecelerationRateNormal;
59
60 @interface WebView (Apple)
61 - (void) _setLayoutInterval:(float)interval;
62 - (void) _setAllowsMessaging:(BOOL)allows;
63 @end
64
65 @interface WebPreferences (Apple)
66 + (void) _setInitialDefaultTextEncodingToSystemEncoding;
67 - (void) _setLayoutInterval:(NSInteger)interval;
68 - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled;
69 @end
70
71 /* Indirect Delegate {{{ */
72 @interface IndirectDelegate : NSObject <
73 HookProtocol
74 > {
75 _transient volatile id delegate_;
76 }
77
78 - (void) setDelegate:(id)delegate;
79 - (id) initWithDelegate:(id)delegate;
80 @end
81
82 @implementation IndirectDelegate
83
84 - (void) setDelegate:(id)delegate {
85 delegate_ = delegate;
86 }
87
88 - (id) initWithDelegate:(id)delegate {
89 delegate_ = delegate;
90 return self;
91 }
92
93 - (void) didDismissModalViewController {
94 if (delegate_ != nil)
95 return [delegate_ didDismissModalViewController];
96 }
97
98 - (IMP) methodForSelector:(SEL)sel {
99 if (IMP method = [super methodForSelector:sel])
100 return method;
101 fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel));
102 return NULL;
103 }
104
105 - (BOOL) respondsToSelector:(SEL)sel {
106 if ([super respondsToSelector:sel])
107 return YES;
108
109 // XXX: WebThreadCreateNSInvocation returns nil
110
111 #if ShowInternals
112 fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel));
113 #endif
114
115 return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel];
116 }
117
118 - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel {
119 if (NSMethodSignature *method = [super methodSignatureForSelector:sel])
120 return method;
121
122 #if ShowInternals
123 fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel));
124 #endif
125
126 if (delegate_ != nil)
127 if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel])
128 return sig;
129
130 // XXX: I fucking hate Apple so very very bad
131 return [NSMethodSignature signatureWithObjCTypes:"v@:"];
132 }
133
134 - (void) forwardInvocation:(NSInvocation *)inv {
135 SEL sel = [inv selector];
136 if (delegate_ != nil && [delegate_ respondsToSelector:sel])
137 [inv invokeWithTarget:delegate_];
138 }
139
140 @end
141 /* }}} */
142
143 @implementation WebScriptObject (UICaboodle)
144
145 - (NSUInteger) count {
146 id length([self valueForKey:@"length"]);
147 if ([length respondsToSelector:@selector(intValue)])
148 return [length intValue];
149 else
150 return 0;
151 }
152
153 - (id) objectAtIndex:(unsigned)index {
154 return [self webScriptValueAtIndex:index];
155 }
156
157 @end
158
159 // CYWebPolicyDecision* {{{
160 enum CYWebPolicyDecision {
161 CYWebPolicyDecisionUnknown,
162 CYWebPolicyDecisionDownload,
163 CYWebPolicyDecisionIgnore,
164 CYWebPolicyDecisionUse,
165 };
166
167 @interface CYWebPolicyDecisionMediator : NSObject <
168 WebPolicyDecisionListener
169 > {
170 id<WebPolicyDecisionListener> listener_;
171 CYWebPolicyDecision decision_;
172 }
173
174 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
175
176 - (CYWebPolicyDecision) decision;
177 - (bool) decided;
178 - (bool) decide;
179
180 @end
181
182 @implementation CYWebPolicyDecisionMediator
183
184 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
185 if ((self = [super init]) != nil) {
186 listener_ = listener;
187 } return self;
188 }
189
190 - (CYWebPolicyDecision) decision {
191 return decision_;
192 }
193
194 - (bool) decided {
195 return decision_ != CYWebPolicyDecisionUnknown;
196 }
197
198 - (bool) decide {
199 switch (decision_) {
200 case CYWebPolicyDecisionUnknown:
201 default:
202 NSLog(@"CYWebPolicyDecisionUnknown");
203 return false;
204
205 case CYWebPolicyDecisionDownload: [listener_ download]; break;
206 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
207 case CYWebPolicyDecisionUse: [listener_ use]; break;
208 }
209
210 return true;
211 }
212
213 - (void) download {
214 decision_ = CYWebPolicyDecisionDownload;
215 }
216
217 - (void) ignore {
218 decision_ = CYWebPolicyDecisionIgnore;
219 }
220
221 - (void) use {
222 decision_ = CYWebPolicyDecisionUse;
223 }
224
225 @end
226 // }}}
227
228 @implementation CYWebView : UIWebView
229
230 #if ShowInternals
231 #include "UICaboodle/UCInternal.h"
232 #endif
233
234 - (id) initWithFrame:(CGRect)frame {
235 if ((self = [super initWithFrame:frame]) != nil) {
236 } return self;
237 }
238
239 - (void) dealloc {
240 [super dealloc];
241 }
242
243 - (id<CYWebViewDelegate>) delegate {
244 return (id<CYWebViewDelegate>) [super delegate];
245 }
246
247 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
248 id<CYWebViewDelegate> delegate([self delegate]);
249 WebView *created(nil);
250 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
251 created = [delegate webView:view createWebViewWithRequest:request];
252 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
253 created = [super webView:view createWebViewWithRequest:request];
254 return created;
255 }*/
256
257 // webView:addMessageToConsole: (X.Xx) {{{
258 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
259 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
260 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
261 [uiWebView webView:view addMessageToConsole:message];
262 }
263
264 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
265 id<CYWebViewDelegate> delegate([self delegate]);
266 if ([delegate respondsToSelector:@selector(webView:addMessageToConsole:)])
267 [delegate webView:view addMessageToConsole:message];
268 if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
269 [super webView:view addMessageToConsole:message];
270 }
271 // }}}
272 // webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
273 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
274 id<CYWebViewDelegate> delegate([self delegate]);
275 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
276 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
277 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
278 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
279 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
280 [mediator decide];
281 }
282 // }}}
283 // webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
284 static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
285 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
286 if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
287 [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
288 }
289
290 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
291 id<CYWebViewDelegate> delegate([self delegate]);
292 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
293 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
294 [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
295 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
296 [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
297 [mediator decide];
298 }
299 // }}}
300 // webView:didClearWindowObject:forFrame: (3.2+) {{{
301 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
302 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
303 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
304 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
305 }
306
307 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
308 id<CYWebViewDelegate> delegate([self delegate]);
309 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
310 [delegate webView:view didClearWindowObject:window forFrame:frame];
311 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
312 [super webView:view didClearWindowObject:window forFrame:frame];
313 }
314 // }}}
315 // webView:didFailLoadWithError:forFrame: (2.0+) {{{
316 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
317 id<CYWebViewDelegate> delegate([self delegate]);
318 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
319 [delegate webView:view didFailLoadWithError:error forFrame:frame];
320 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
321 [super webView:view didFailLoadWithError:error forFrame:frame];
322 }
323 // }}}
324 // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
325 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
326 id<CYWebViewDelegate> delegate([self delegate]);
327 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
328 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
329 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
330 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
331 }
332 // }}}
333 // webView:didFinishLoadForFrame: (2.0+) {{{
334 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
335 id<CYWebViewDelegate> delegate([self delegate]);
336 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
337 [delegate webView:view didFinishLoadForFrame:frame];
338 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
339 [super webView:view didFinishLoadForFrame:frame];
340 }
341 // }}}
342 // webView:didReceiveTitle:forFrame: (3.2+) {{{
343 static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
344 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
345 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
346 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
347 }
348
349 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
350 id<CYWebViewDelegate> delegate([self delegate]);
351 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
352 [delegate webView:view didReceiveTitle:title forFrame:frame];
353 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
354 [super webView:view didReceiveTitle:title forFrame:frame];
355 }
356 // }}}
357 // webView:didStartProvisionalLoadForFrame: (2.0+) {{{
358 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
359 id<CYWebViewDelegate> delegate([self delegate]);
360 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
361 [delegate webView:view didStartProvisionalLoadForFrame:frame];
362 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
363 [super webView:view didStartProvisionalLoadForFrame:frame];
364 }
365 // }}}
366 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
367 static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
368 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
369 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
370 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
371 return request;
372 }
373
374 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
375 id<CYWebViewDelegate> delegate([self delegate]);
376 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
377 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
378 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
379 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
380 return request;
381 }
382 // }}}
383 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
384 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
385 id<CYWebViewDelegate> delegate([self delegate]);
386 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
387 if (
388 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
389 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
390 )
391 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
392 }
393 // }}}
394 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
395 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
396 id<CYWebViewDelegate> delegate([self delegate]);
397 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
398 if (
399 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
400 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
401 )
402 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
403 return NO;
404 }
405 // }}}
406 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
407 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
408 id<CYWebViewDelegate> delegate([self delegate]);
409 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
410 if (
411 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
412 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
413 )
414 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
415 return nil;
416 }
417 // }}}
418 // webViewClose: (3.2+) {{{
419 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
420 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
421 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
422 [uiWebView webViewClose:view];
423 }
424
425 - (void) webViewClose:(WebView *)view {
426 id<CYWebViewDelegate> delegate([self delegate]);
427 if ([delegate respondsToSelector:@selector(webViewClose:)])
428 [delegate webViewClose:view];
429 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
430 [super webViewClose:view];
431 }
432 // }}}
433
434 - (void) _updateViewSettings {
435 [super _updateViewSettings];
436
437 id<CYWebViewDelegate> delegate([self delegate]);
438 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
439 [delegate webViewUpdateViewSettings:self];
440 }
441
442 + (void) initialize {
443 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
444 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
445 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
446 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
447 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
448 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
449 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
450 }
451 }
452
453 @end
454
455 @implementation BrowserController
456
457 #if ShowInternals
458 #include "UICaboodle/UCInternal.h"
459 #endif
460
461 + (void) _initialize {
462 [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding];
463
464 if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal")))
465 CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal;
466 else // XXX: this actually might be fast on some older systems: we should look into this
467 CYScrollViewDecelerationRateNormal = 0.998;
468 }
469
470 - (void) dealloc {
471 #if LogBrowser
472 NSLog(@"[BrowserController dealloc]");
473 #endif
474
475 [webview_ setDelegate:nil];
476
477 [indirect_ setDelegate:nil];
478 [indirect_ release];
479
480 if (challenge_ != nil)
481 [challenge_ release];
482
483 if (closer_ != nil)
484 [closer_ release];
485
486 if (title_ != nil)
487 [title_ release];
488
489 if ([loading_ count] != 0)
490 [delegate_ releaseNetworkActivityIndicator];
491 [loading_ release];
492
493 [reloaditem_ release];
494 [loadingitem_ release];
495
496 [indicator_ release];
497
498 [super dealloc];
499 }
500
501 - (NSURL *) URLWithURL:(NSURL *)url {
502 return url;
503 }
504
505 - (NSURLRequest *) requestWithURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
506 return [NSURLRequest
507 requestWithURL:[self URLWithURL:url]
508 cachePolicy:policy
509 timeoutInterval:DefaultTimeout_
510 ];
511 }
512
513 - (void) setURL:(NSURL *)url {
514 _assert(request_ == nil);
515 request_ = [self requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
516 }
517
518 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
519 [self loadRequest:[self requestWithURL:url cachePolicy:policy]];
520 }
521
522 - (void) loadURL:(NSURL *)url {
523 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
524 }
525
526 - (void) loadRequest:(NSURLRequest *)request {
527 #if LogBrowser
528 NSLog(@"loadRequest:%@", request);
529 #endif
530
531 error_ = false;
532
533 WebThreadLocked lock;
534 [webview_ loadRequest:request];
535 }
536
537 - (void) reloadURLWithCache:(BOOL)cache {
538 if (request_ == nil)
539 return;
540
541 NSMutableURLRequest *request([request_ mutableCopy]);
542 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
543
544 request_ = request;
545
546 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
547 [self loadRequest:request_];
548 else {
549 UIAlertView *alert = [[[UIAlertView alloc]
550 initWithTitle:UCLocalize("RESUBMIT_FORM")
551 message:nil
552 delegate:self
553 cancelButtonTitle:UCLocalize("CANCEL")
554 otherButtonTitles:
555 UCLocalize("SUBMIT"),
556 nil
557 ] autorelease];
558
559 [alert setContext:@"submit"];
560 [alert show];
561 }
562 }
563
564 - (void) reloadURL {
565 [self reloadURLWithCache:YES];
566 }
567
568 - (void) reloadData {
569 [super reloadData];
570 [self reloadURLWithCache:YES];
571 }
572
573 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
574 custom_ = button;
575 style_ = style;
576 function_ = function;
577
578 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
579 }
580
581 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
582 custom_ = button;
583 style_ = style;
584 function_ = function;
585
586 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
587 }
588
589 - (void) removeButton {
590 custom_ = [NSNull null];
591 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
592 }
593
594 - (void) setPopupHook:(id)function {
595 if (closer_ != nil)
596 [closer_ autorelease];
597 if (function == nil)
598 closer_ = nil;
599 else
600 closer_ = [function retain];
601 }
602
603 - (void) scrollToBottomAnimated:(NSNumber *)animated {
604 CGSize size([scroller_ contentSize]);
605 CGPoint offset([scroller_ contentOffset]);
606 CGRect frame([scroller_ frame]);
607
608 if (size.height - offset.y < frame.size.height + 20.f) {
609 CGRect rect = {{0, size.height-1}, {size.width, 1}};
610 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
611 }
612 }
613
614 - (void) _setViewportWidth {
615 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
616 }
617
618 - (void) setViewportWidth:(float)width {
619 width_ = width != 0 ? width : [[self class] defaultWidth];
620 [self _setViewportWidth];
621 }
622
623 - (void) _setViewportWidthOnMainThread:(NSNumber *)width {
624 [self setViewportWidth:[width floatValue]];
625 }
626
627 - (void) setViewportWidthOnMainThread:(float)width {
628 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
629 }
630
631 - (void) webViewUpdateViewSettings:(UIWebView *)view {
632 [self _setViewportWidth];
633 }
634
635 - (void) _openMailToURL:(NSURL *)url {
636 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
637 }
638
639 - (bool) _allowJavaScriptPanel {
640 return true;
641 }
642
643 - (bool) allowsNavigationAction {
644 return allowsNavigationAction_;
645 }
646
647 - (void) setAllowsNavigationAction:(bool)value {
648 allowsNavigationAction_ = value;
649 }
650
651 - (void) setAllowsNavigationActionByNumber:(NSNumber *)value {
652 [self setAllowsNavigationAction:[value boolValue]];
653 }
654
655 - (void) popViewControllerWithNumber:(NSNumber *)value {
656 UINavigationController *navigation([self navigationController]);
657 if ([navigation topViewController] == self)
658 [navigation popViewControllerAnimated:[value boolValue]];
659 }
660
661 - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
662 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
663 [self _didFinishLoading];
664
665 if ([error code] == NSURLErrorCancelled)
666 return;
667
668 if ([frame parentFrame] == nil) {
669 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
670 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
671 [[error localizedDescription] stringByAddingPercentEscapes]
672 ]]];
673
674 error_ = true;
675 }
676 }
677
678 - (void) pushRequest:(NSURLRequest *)request asPop:(bool)pop {
679 NSURL *url([request URL]);
680
681 // XXX: filter to internal usage?
682 CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
683
684 if (page == nil) {
685 BrowserController *browser([[[class_ alloc] init] autorelease]);
686 [browser loadRequest:request];
687 page = browser;
688 }
689
690 [page setDelegate:delegate_];
691
692 if (!pop) {
693 [[self navigationItem] setTitle:title_];
694
695 [[self navigationController] pushViewController:page animated:YES];
696 } else {
697 UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
698
699 [navigation setHook:indirect_];
700 [navigation setDelegate:delegate_];
701
702 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
703 initWithTitle:UCLocalize("CLOSE")
704 style:UIBarButtonItemStylePlain
705 target:page
706 action:@selector(close)
707 ] autorelease]];
708
709 [[self navigationController] presentModalViewController:navigation animated:YES];
710 }
711 }
712
713 // CYWebViewDelegate {{{
714 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
715 #if LogMessages
716 NSLog(@"addMessageToConsole:%@", message);
717 #endif
718 }
719
720 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
721 #if LogBrowser
722 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
723 #endif
724
725 if ([frame parentFrame] == nil) {
726 if (!error_) {
727 NSURL *url(request == nil ? nil : [request URL]);
728
729 if (request_ == nil || [self allowsNavigationAction] || [[request_ URL] isEqual:url])
730 request_ = request;
731 else {
732 if (url != nil)
733 [self pushRequest:request asPop:NO];
734 [listener ignore];
735 }
736 }
737 }
738 }
739
740 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
741 #if LogBrowser
742 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
743 #endif
744
745 NSURL *url([request URL]);
746 if (url == nil)
747 return;
748
749 if ([frame isEqualToString:@"_open"])
750 [delegate_ openURL:url];
751 else {
752 NSString *scheme([[url scheme] lowercaseString]);
753 if ([scheme isEqualToString:@"mailto"])
754 [self _openMailToURL:url];
755 else
756 [self pushRequest:request asPop:[frame isEqualToString:@"_popup"]];
757 }
758
759 [listener ignore];
760 }
761
762 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
763 }
764
765 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
766 #if LogBrowser
767 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
768 #endif
769
770 [self _didFailWithError:error forFrame:frame];
771 }
772
773 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
774 #if LogBrowser
775 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
776 #endif
777
778 [self _didFailWithError:error forFrame:frame];
779 }
780
781 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
782 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
783
784 if ([frame parentFrame] == nil) {
785 if (DOMDocument *document = [frame DOMDocument])
786 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
787 for (DOMHTMLBodyElement *body in (id) bodies) {
788 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
789
790 UIColor *uic(nil);
791
792 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
793 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
794 DOMRGBColor *rgb([color getRGBColorValue]);
795
796 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
797 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
798 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
799 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
800
801 if (red == 0xc7 && green == 0xce && blue == 0xd5)
802 uic = [UIColor pinStripeColor];
803 else if (alpha != 0)
804 uic = [UIColor
805 colorWithRed:(red / 255)
806 green:(green / 255)
807 blue:(blue / 255)
808 alpha:alpha
809 ];
810 }
811 }
812
813 [scroller_ setBackgroundColor:(uic ?: [UIColor clearColor])];
814 break;
815 }
816 }
817
818 [self _didFinishLoading];
819 }
820
821 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
822 if ([frame parentFrame] != nil)
823 return;
824
825 if (title_ != nil)
826 [title_ autorelease];
827 title_ = [title retain];
828
829 [[self navigationItem] setTitle:title_];
830 }
831
832 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
833 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
834
835 if ([frame parentFrame] == nil) {
836 CYRelease(title_);
837 custom_ = nil;
838 style_ = nil;
839 function_ = nil;
840 CYRelease(closer_);
841
842 [self setHidesNavigationBar:NO];
843
844 // XXX: do we still need to do this?
845 [[self navigationItem] setTitle:nil];
846 }
847
848 [self _didStartLoading];
849 }
850
851 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
852 #if LogBrowser
853 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
854 #endif
855
856 return request;
857 }
858
859 - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
860 return [self _allowJavaScriptPanel];
861 }
862
863 - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
864 return [self _allowJavaScriptPanel];
865 }
866
867 - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
868 return [self _allowJavaScriptPanel];
869 }
870
871 - (void) webViewClose:(WebView *)view {
872 [self close];
873 }
874 // }}}
875
876 - (void) close {
877 [[self navigationController] dismissModalViewControllerAnimated:YES];
878 }
879
880 - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
881 NSString *context([alert context]);
882
883 if ([context isEqualToString:@"sensitive"]) {
884 switch (button) {
885 case 1:
886 sensitive_ = [NSNumber numberWithBool:YES];
887 break;
888
889 case 2:
890 sensitive_ = [NSNumber numberWithBool:NO];
891 break;
892 }
893
894 [alert dismissWithClickedButtonIndex:-1 animated:YES];
895 } else if ([context isEqualToString:@"challenge"]) {
896 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
897
898 switch (button) {
899 case 1: {
900 NSString *username([[alert textFieldAtIndex:0] text]);
901 NSString *password([[alert textFieldAtIndex:1] text]);
902
903 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
904
905 [sender useCredential:credential forAuthenticationChallenge:challenge_];
906 } break;
907
908 case 2:
909 [sender cancelAuthenticationChallenge:challenge_];
910 break;
911
912 _nodefault
913 }
914
915 [challenge_ release];
916 challenge_ = nil;
917
918 [alert dismissWithClickedButtonIndex:-1 animated:YES];
919 } else if ([context isEqualToString:@"submit"]) {
920 if (button == [alert cancelButtonIndex]) {
921 } else if (button == [alert firstOtherButtonIndex]) {
922 if (request_ != nil) {
923 WebThreadLocked lock;
924 [webview_ loadRequest:request_];
925 }
926 }
927
928 [alert dismissWithClickedButtonIndex:-1 animated:YES];
929 }
930 }
931
932 - (UIBarButtonItemStyle) rightButtonStyle {
933 if (style_ == nil) normal:
934 return UIBarButtonItemStylePlain;
935 else if ([style_ isEqualToString:@"Normal"])
936 return UIBarButtonItemStylePlain;
937 else if ([style_ isEqualToString:@"Highlighted"])
938 return UIBarButtonItemStyleDone;
939 else goto normal;
940 }
941
942 - (UIBarButtonItem *) customButton {
943 return custom_ == [NSNull null] ? nil : [[[UIBarButtonItem alloc]
944 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
945 style:[self rightButtonStyle]
946 target:self
947 action:@selector(customButtonClicked)
948 ] autorelease];
949 }
950
951 - (UIBarButtonItem *) rightButton {
952 return reloaditem_;
953 }
954
955 - (void) applyLoadingTitle {
956 [[self navigationItem] setTitle:UCLocalize("LOADING")];
957 }
958
959 - (void) layoutRightButton {
960 [[loadingitem_ view] addSubview:indicator_];
961 [[loadingitem_ view] bringSubviewToFront:indicator_];
962 }
963
964 - (void) applyRightButton {
965 if ([self isLoading]) {
966 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
967 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
968
969 [indicator_ startAnimating];
970 [self applyLoadingTitle];
971 } else {
972 [indicator_ stopAnimating];
973
974 [[self navigationItem] setRightBarButtonItem:(
975 custom_ != nil ? [self customButton] : [self rightButton]
976 ) animated:YES];
977 }
978 }
979
980 - (void) didStartLoading {
981 // Overridden in subclasses.
982 }
983
984 - (void) _didStartLoading {
985 [self applyRightButton];
986
987 if ([loading_ count] != 1)
988 return;
989
990 [delegate_ retainNetworkActivityIndicator];
991 [self didStartLoading];
992 }
993
994 - (void) didFinishLoading {
995 // Overridden in subclasses.
996 }
997
998 - (void) _didFinishLoading {
999 if ([loading_ count] != 0)
1000 return;
1001
1002 [self applyRightButton];
1003 [[self navigationItem] setTitle:title_];
1004
1005 [delegate_ releaseNetworkActivityIndicator];
1006 [self didFinishLoading];
1007 }
1008
1009 - (bool) isLoading {
1010 return [loading_ count] != 0;
1011 }
1012
1013 - (id) initWithWidth:(float)width ofClass:(Class)_class {
1014 if ((self = [super init]) != nil) {
1015 allowsNavigationAction_ = true;
1016
1017 class_ = _class;
1018 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
1019
1020 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
1021
1022 CGRect bounds([[self view] bounds]);
1023
1024 webview_ = [[[CYWebView alloc] initWithFrame:bounds] autorelease];
1025 [webview_ setDelegate:self];
1026 [self setView:webview_];
1027
1028 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
1029 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
1030 else
1031 [webview_ setDetectsPhoneNumbers:NO];
1032
1033 [webview_ setScalesPageToFit:YES];
1034
1035 UIWebDocumentView *document([webview_ _documentView]);
1036
1037 // XXX: I think this improves scrolling; the hardcoded-ness sucks
1038 [document setTileSize:CGSizeMake(320, 500)];
1039
1040 [document setBackgroundColor:[UIColor clearColor]];
1041
1042 // XXX: this is terribly (too?) expensive
1043 [document setDrawsBackground:NO];
1044
1045 WebView *webview([document webView]);
1046 WebPreferences *preferences([webview preferences]);
1047
1048 // XXX: I have no clue if I actually /want/ this modification
1049 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1050 [webview _setLayoutInterval:0];
1051 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
1052 [preferences _setLayoutInterval:0];
1053
1054 [preferences setCacheModel:WebCacheModelDocumentBrowser];
1055 [preferences setOfflineWebApplicationCacheEnabled:YES];
1056
1057 #if LogMessages
1058 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
1059 [document setAllowsMessaging:YES];
1060 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
1061 [webview _setAllowsMessaging:YES];
1062 #endif
1063
1064 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1065 scroller_ = [webview_ _scrollView];
1066
1067 [scroller_ setDirectionalLockEnabled:YES];
1068 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
1069 [scroller_ setDelaysContentTouches:NO];
1070
1071 [scroller_ setCanCancelContentTouches:YES];
1072 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1073 UIScroller *scroller([webview_ _scroller]);
1074 scroller_ = (UIScrollView *) scroller;
1075
1076 [scroller setDirectionalScrolling:YES];
1077 // XXX: we might be better off /not/ setting this on older systems
1078 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
1079 [scroller setScrollHysteresis:0]; /* 8 */
1080
1081 [scroller setThumbDetectionEnabled:NO];
1082
1083 // use NO with UIApplicationUseLegacyEvents(YES)
1084 [scroller setEventMode:YES];
1085
1086 // XXX: this is handled by setBounces, right?
1087 //[scroller setAllowsRubberBanding:YES];
1088 }
1089
1090 [scroller_ setFixedBackgroundPattern:YES];
1091 [scroller_ setBackgroundColor:[UIColor clearColor]];
1092 [scroller_ setClipsSubviews:YES];
1093
1094 [scroller_ setBounces:YES];
1095 [scroller_ setScrollingEnabled:YES];
1096 [scroller_ setShowBackgroundShadow:NO];
1097
1098 [self setViewportWidth:width];
1099
1100 reloaditem_ = [[UIBarButtonItem alloc]
1101 initWithTitle:UCLocalize("RELOAD")
1102 style:[self rightButtonStyle]
1103 target:self
1104 action:@selector(reloadButtonClicked)
1105 ];
1106
1107 loadingitem_ = [[UIBarButtonItem alloc]
1108 initWithTitle:@" "
1109 style:UIBarButtonItemStylePlain
1110 target:self
1111 action:@selector(reloadButtonClicked)
1112 ];
1113
1114 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1115 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
1116
1117 UITableView *table([[[UITableView alloc] initWithFrame:bounds style:UITableViewStyleGrouped] autorelease]);
1118 [webview_ insertSubview:table atIndex:0];
1119
1120 [table setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1121 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
1122 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
1123 } return self;
1124 }
1125
1126 - (id) initWithWidth:(float)width {
1127 return [self initWithWidth:width ofClass:[self class]];
1128 }
1129
1130 - (id) init {
1131 return [self initWithWidth:0];
1132 }
1133
1134 - (id) initWithURL:(NSURL *)url {
1135 if ((self = [self init]) != nil) {
1136 [self setURL:url];
1137 } return self;
1138 }
1139
1140 - (void) didDismissModalViewController {
1141 if (closer_ != nil)
1142 [self callFunction:closer_];
1143 }
1144
1145 - (void) callFunction:(WebScriptObject *)function {
1146 WebThreadLocked lock;
1147
1148 WebView *webview([[webview_ _documentView] webView]);
1149 WebFrame *frame([webview mainFrame]);
1150 WebPreferences *preferences([webview preferences]);
1151
1152 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
1153 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
1154
1155 /*id _private(MSHookIvar<id>(webview, "_private"));
1156 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1157 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
1158
1159 bool no;
1160 if (settings == NULL)
1161 no = 0;
1162 else {
1163 no = settings->JavaScriptCanOpenWindowsAutomatically();
1164 settings->setJavaScriptCanOpenWindowsAutomatically(true);
1165 }*/
1166
1167 if (UIWindow *window = [[self view] window])
1168 if (UIResponder *responder = [window firstResponder])
1169 [responder resignFirstResponder];
1170
1171 JSObjectRef object([function JSObject]);
1172 JSGlobalContextRef context([frame globalContext]);
1173 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1174
1175 /*if (settings != NULL)
1176 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
1177
1178 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
1179 }
1180
1181 - (void) reloadButtonClicked {
1182 [self reloadURLWithCache:YES];
1183 }
1184
1185 - (void) _customButtonClicked {
1186 [self reloadButtonClicked];
1187 }
1188
1189 - (void) customButtonClicked {
1190 #if !AlwaysReload
1191 if (function_ != nil)
1192 [self callFunction:function_];
1193 else
1194 #endif
1195 [self _customButtonClicked];
1196 }
1197
1198 + (float) defaultWidth {
1199 return 980;
1200 }
1201
1202 - (void) setNavigationBarStyle:(NSString *)name {
1203 UIBarStyle style;
1204 if ([name isEqualToString:@"Black"])
1205 style = UIBarStyleBlack;
1206 else
1207 style = UIBarStyleDefault;
1208
1209 [[[self navigationController] navigationBar] setBarStyle:style];
1210 }
1211
1212 - (void) setNavigationBarTintColor:(UIColor *)color {
1213 [[[self navigationController] navigationBar] setTintColor:color];
1214 }
1215
1216 - (void) setHidesBackButton:(bool)value {
1217 [[self navigationItem] setHidesBackButton:value];
1218 }
1219
1220 - (void) setHidesBackButtonByNumber:(NSNumber *)value {
1221 [self setHidesBackButton:[value boolValue]];
1222 }
1223
1224 - (void) dispatchEvent:(NSString *)event {
1225 WebThreadLocked lock;
1226
1227 NSString *script([NSString stringWithFormat:@
1228 "(function() {"
1229 "var event = this.document.createEvent('Events');"
1230 "event.initEvent('%@', false, false);"
1231 "this.document.dispatchEvent(event);"
1232 "})();"
1233 , event]);
1234
1235 NSMutableArray *frames([NSMutableArray arrayWithObjects:
1236 [[[webview_ _documentView] webView] mainFrame]
1237 , nil]);
1238
1239 while (WebFrame *frame = [frames lastObject]) {
1240 WebScriptObject *object([frame windowObject]);
1241 [object evaluateWebScript:script];
1242 [frames removeLastObject];
1243 [frames addObjectsFromArray:[frame childFrames]];
1244 }
1245 }
1246
1247 - (bool) hidesNavigationBar {
1248 return hidesNavigationBar_;
1249 }
1250
1251 - (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1252 if (visible_)
1253 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1254 }
1255
1256 - (void) setHidesNavigationBar:(bool)value {
1257 if (hidesNavigationBar_ != value) {
1258 hidesNavigationBar_ = value;
1259 [self _setHidesNavigationBar:YES animated:YES];
1260 }
1261 }
1262
1263 - (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1264 [self setHidesNavigationBar:[value boolValue]];
1265 }
1266
1267 - (void) viewWillAppear:(BOOL)animated {
1268 visible_ = true;
1269
1270 if ([self hidesNavigationBar])
1271 [self _setHidesNavigationBar:YES animated:animated];
1272
1273 [self dispatchEvent:@"CydiaViewWillAppear"];
1274 [super viewWillAppear:animated];
1275 }
1276
1277 - (void) viewDidAppear:(BOOL)animated {
1278 [super viewDidAppear:animated];
1279 [self dispatchEvent:@"CydiaViewDidAppear"];
1280 }
1281
1282 - (void) viewWillDisappear:(BOOL)animated {
1283 [self dispatchEvent:@"CydiaViewWillDisappear"];
1284 [super viewWillDisappear:animated];
1285
1286 if ([self hidesNavigationBar])
1287 [self _setHidesNavigationBar:NO animated:animated];
1288
1289 visible_ = false;
1290 }
1291
1292 - (void) viewDidDisappear:(BOOL)animated {
1293 [super viewDidDisappear:animated];
1294 [self dispatchEvent:@"CydiaViewDidDisappear"];
1295 }
1296
1297 @end