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