]> git.saurik.com Git - cydia.git/blame - UICaboodle/BrowserView.mm
Use _H<> for BrowserController::request_.
[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
12b59862
JF
483 if (closer_ != nil)
484 [closer_ release];
22f8bed9 485
22f8bed9
JF
486 if (title_ != nil)
487 [title_ release];
2634b249 488
54043703
JF
489 if ([loading_ count] != 0)
490 [delegate_ releaseNetworkActivityIndicator];
3931b718
JF
491 [loading_ release];
492
2634b249
JF
493 [reloaditem_ release];
494 [loadingitem_ release];
495
496 [indicator_ release];
bc11cf5b 497
22f8bed9
JF
498 [super dealloc];
499}
500
eb09425a
JF
501- (void) setURL:(NSURL *)url {
502 _assert(request_ == nil);
503
a374f380 504 request_ = [NSURLRequest
eb09425a
JF
505 requestWithURL:url
506 cachePolicy:NSURLRequestUseProtocolCachePolicy
507 timeoutInterval:DefaultTimeout_
a374f380 508 ];
eb09425a
JF
509}
510
22f8bed9
JF
511- (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
512 [self loadRequest:[NSURLRequest
513 requestWithURL:url
514 cachePolicy:policy
eb09425a 515 timeoutInterval:DefaultTimeout_
22f8bed9
JF
516 ]];
517}
518
519- (void) loadURL:(NSURL *)url {
520 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
521}
522
22f8bed9 523- (void) loadRequest:(NSURLRequest *)request {
0352f238
JF
524#if LogBrowser
525 NSLog(@"loadRequest:%@", request);
526#endif
527
fe468f45 528 error_ = false;
caa427d1 529
0893a034 530 WebThreadLocked lock;
2634b249 531 [webview_ loadRequest:request];
22f8bed9
JF
532}
533
b13b8664 534- (void) reloadURLWithCache:(BOOL)cache {
22f8bed9
JF
535 if (request_ == nil)
536 return;
537
b13b8664
JF
538 NSMutableURLRequest *request([request_ mutableCopy]);
539 [request setCachePolicy:(cache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData)];
540
a374f380 541 request_ = request;
b13b8664 542
22f8bed9 543 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
fe468f45 544 [self loadRequest:request_];
22f8bed9 545 else {
79ed082a 546 UIAlertView *alert = [[[UIAlertView alloc]
43f3d7f6 547 initWithTitle:UCLocalize("RESUBMIT_FORM")
79ed082a 548 message:nil
22f8bed9 549 delegate:self
79ed082a 550 cancelButtonTitle:UCLocalize("CANCEL")
1aa29546
JF
551 otherButtonTitles:
552 UCLocalize("SUBMIT"),
553 nil
22f8bed9 554 ] autorelease];
2634b249 555
79ed082a
GP
556 [alert setContext:@"submit"];
557 [alert show];
22f8bed9
JF
558 }
559}
560
b13b8664
JF
561- (void) reloadURL {
562 [self reloadURLWithCache:YES];
563}
564
eb09425a
JF
565- (void) reloadData {
566 [super reloadData];
b13b8664 567 [self reloadURLWithCache:YES];
eb09425a
JF
568}
569
22f8bed9 570- (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
46d3a5cf
JF
571 custom_ = button;
572 style_ = style;
573 function_ = function;
12b59862 574
70a9ff4e 575 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
22f8bed9
JF
576}
577
578- (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
46d3a5cf
JF
579 custom_ = button;
580 style_ = style;
581 function_ = function;
22f8bed9 582
ed5566c7
JF
583 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
584}
12b59862 585
ed5566c7
JF
586- (void) removeButton {
587 custom_ = [NSNull null];
70a9ff4e 588 [self performSelectorOnMainThread:@selector(applyRightButton) withObject:nil waitUntilDone:NO];
22f8bed9 589}
22f8bed9 590
caa427d1
JF
591- (void) setPopupHook:(id)function {
592 if (closer_ != nil)
593 [closer_ autorelease];
3931b718
JF
594 if (function == nil)
595 closer_ = nil;
596 else
597 closer_ = [function retain];
caa427d1
JF
598}
599
8e3b68d4
JF
600- (void) scrollToBottomAnimated:(NSNumber *)animated {
601 CGSize size([scroller_ contentSize]);
602 CGPoint offset([scroller_ contentOffset]);
603 CGRect frame([scroller_ frame]);
604
605 if (size.height - offset.y < frame.size.height + 20.f) {
606 CGRect rect = {{0, size.height-1}, {size.width, 1}};
607 [scroller_ scrollRectToVisible:rect animated:[animated boolValue]];
608 }
609}
610
7e37a676
JF
611- (void) _setViewportWidth {
612 [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
613}
614
2634b249
JF
615- (void) setViewportWidth:(float)width {
616 width_ = width != 0 ? width : [[self class] defaultWidth];
7e37a676
JF
617 [self _setViewportWidth];
618}
619
8dbdaafa
JF
620- (void) _setViewportWidthOnMainThread:(NSNumber *)width {
621 [self setViewportWidth:[width floatValue]];
622}
623
624- (void) setViewportWidthOnMainThread:(float)width {
625 [self performSelectorOnMainThread:@selector(_setViewportWidthOnMainThread:) withObject:[NSNumber numberWithFloat:width] waitUntilDone:NO];
626}
627
7e37a676
JF
628- (void) webViewUpdateViewSettings:(UIWebView *)view {
629 [self _setViewportWidth];
22f8bed9
JF
630}
631
2634b249
JF
632- (void) _openMailToURL:(NSURL *)url {
633 [[UIApplication sharedApplication] openURL:url];// asPanel:YES];
22f8bed9
JF
634}
635
2634b249
JF
636- (bool) _allowJavaScriptPanel {
637 return true;
22f8bed9
JF
638}
639
2634b249
JF
640- (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame {
641 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
642 [self _didFinishLoading];
22f8bed9 643
2634b249 644 if ([error code] == NSURLErrorCancelled)
22f8bed9
JF
645 return;
646
caa427d1 647 if ([frame parentFrame] == nil) {
2634b249
JF
648 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
649 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
650 [[error localizedDescription] stringByAddingPercentEscapes]
651 ]]];
22f8bed9 652
2634b249
JF
653 error_ = true;
654 }
655}
caa427d1 656
2634b249 657// CYWebViewDelegate {{{
72bdb258
JF
658- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
659#if LogMessages
660 NSLog(@"addMessageToConsole:%@", message);
661#endif
662}
663
2634b249
JF
664- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
665#if LogBrowser
666 NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame);
667#endif
600d005d 668
ee32e123 669 if ([frame parentFrame] == nil) {
a374f380
JF
670 if (!error_)
671 request_ = request;
caa427d1 672 }
22f8bed9
JF
673}
674
2634b249
JF
675- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
676#if LogBrowser
677 NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame);
678#endif
2e6c1426 679
2634b249
JF
680 NSURL *url([request URL]);
681 if (url == nil)
682 return;
bc11cf5b 683
2634b249
JF
684 if ([frame isEqualToString:@"_open"])
685 [delegate_ openURL:url];
2e6c1426 686
2634b249
JF
687 NSString *scheme([[url scheme] lowercaseString]);
688 if ([scheme isEqualToString:@"mailto"])
689 [self _openMailToURL:url];
a5938ea5 690
028dbd1c
JF
691 // XXX: filter to internal usage?
692 CYViewController *page([delegate_ pageForURL:url forExternal:NO]);
2fad210a 693
2634b249
JF
694 if (page == nil) {
695 BrowserController *browser([[[class_ alloc] init] autorelease]);
696 [browser loadRequest:request];
697 page = browser;
bc11cf5b 698 }
ce041f4f 699
2634b249 700 [page setDelegate:delegate_];
b5e7eebb 701
2634b249
JF
702 if (![frame isEqualToString:@"_popup"]) {
703 [[self navigationItem] setTitle:title_];
b5e7eebb 704
2634b249
JF
705 [[self navigationController] pushViewController:page animated:YES];
706 } else {
f6e13561 707 UCNavigationController *navigation([[[UCNavigationController alloc] initWithRootViewController:page] autorelease]);
22f8bed9 708
2634b249
JF
709 [navigation setHook:indirect_];
710 [navigation setDelegate:delegate_];
22f8bed9 711
2634b249
JF
712 [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc]
713 initWithTitle:UCLocalize("CLOSE")
714 style:UIBarButtonItemStylePlain
715 target:page
716 action:@selector(close)
717 ] autorelease]];
718
719 [[self navigationController] presentModalViewController:navigation animated:YES];
720 }
22f8bed9 721
2634b249 722 [listener ignore];
22f8bed9
JF
723}
724
2634b249 725- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
22f8bed9
JF
726}
727
2634b249
JF
728- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
729#if LogBrowser
730 NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame);
731#endif
732
733 [self _didFailWithError:error forFrame:frame];
22f8bed9
JF
734}
735
2634b249
JF
736- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
737#if LogBrowser
738 NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame);
739#endif
740
741 [self _didFailWithError:error forFrame:frame];
22f8bed9
JF
742}
743
e665fe98
JF
744// XXX: factor this out somewhere
745- (UIColor *) groupTableViewBackgroundColor {
746 UIDevice *device([UIDevice currentDevice]);
747 bool iPad([device respondsToSelector:@selector(userInterfaceIdiom)] && [device userInterfaceIdiom] == UIUserInterfaceIdiomPad);
748 return iPad ? [UIColor colorWithRed:0.821 green:0.834 blue:0.860 alpha:1] : [UIColor groupTableViewBackgroundColor];
749}
750
2634b249 751- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
7592e053 752 [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]];
22f8bed9 753
caa427d1 754 if ([frame parentFrame] == nil) {
22f8bed9
JF
755 if (DOMDocument *document = [frame DOMDocument])
756 if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"])
96f3833b 757 for (DOMHTMLBodyElement *body in (id) bodies) {
22f8bed9
JF
758 DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]);
759
e665fe98 760 UIColor *uic([self groupTableViewBackgroundColor]);
22f8bed9
JF
761
762 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
fe468f45
JF
763 if ([color primitiveType] == DOM_CSS_RGBCOLOR) {
764 DOMRGBColor *rgb([color getRGBColorValue]);
765
766 float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]);
767 float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]);
768 float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]);
769 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
770
fe468f45 771 if (red == 0xc7 && green == 0xce && blue == 0xd5)
e665fe98 772 uic = [UIColor pinStripeColor];
fe468f45
JF
773 else if (alpha != 0)
774 uic = [UIColor
775 colorWithRed:(red / 255)
776 green:(green / 255)
777 blue:(blue / 255)
778 alpha:alpha
779 ];
22f8bed9
JF
780 }
781 }
782
e665fe98 783 [scroller_ setBackgroundColor:uic];
22f8bed9
JF
784 break;
785 }
786 }
787
2634b249 788 [self _didFinishLoading];
22f8bed9
JF
789}
790
2634b249
JF
791- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
792 if ([frame parentFrame] != nil)
793 return;
caa427d1 794
3931b718
JF
795 if (title_ != nil)
796 [title_ autorelease];
2634b249 797 title_ = [title retain];
3931b718 798
2634b249
JF
799 [[self navigationItem] setTitle:title_];
800}
22f8bed9 801
2634b249
JF
802- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
803 [loading_ addObject:[NSValue valueWithNonretainedObject:frame]];
7592e053 804
caa427d1 805 if ([frame parentFrame] == nil) {
2634b249 806 CYRelease(title_);
46d3a5cf
JF
807 custom_ = nil;
808 style_ = nil;
809 function_ = nil;
2634b249
JF
810 CYRelease(closer_);
811
5cdfcd6f
JF
812 [self setHidesNavigationBar:NO];
813
2634b249
JF
814 // XXX: do we still need to do this?
815 [[self navigationItem] setTitle:nil];
caa427d1 816 }
fe468f45 817
2634b249 818 [self _didStartLoading];
caa427d1
JF
819}
820
2634b249 821- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
0352f238
JF
822#if LogBrowser
823 NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source);
824#endif
825
2634b249 826 return request;
22f8bed9
JF
827}
828
2634b249
JF
829- (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
830 return [self _allowJavaScriptPanel];
22f8bed9
JF
831}
832
2634b249
JF
833- (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
834 return [self _allowJavaScriptPanel];
600d005d
JF
835}
836
2634b249
JF
837- (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
838 return [self _allowJavaScriptPanel];
600d005d
JF
839}
840
2634b249
JF
841- (void) webViewClose:(WebView *)view {
842 [self close];
600d005d 843}
2634b249 844// }}}
600d005d 845
2634b249
JF
846- (void) close {
847 [[self navigationController] dismissModalViewControllerAnimated:YES];
600d005d
JF
848}
849
2634b249
JF
850- (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button {
851 NSString *context([alert context]);
600d005d 852
2634b249
JF
853 if ([context isEqualToString:@"sensitive"]) {
854 switch (button) {
855 case 1:
856 sensitive_ = [NSNumber numberWithBool:YES];
857 break;
600d005d 858
2634b249
JF
859 case 2:
860 sensitive_ = [NSNumber numberWithBool:NO];
861 break;
862 }
600d005d 863
2634b249
JF
864 [alert dismissWithClickedButtonIndex:-1 animated:YES];
865 } else if ([context isEqualToString:@"challenge"]) {
866 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
caa427d1 867
2634b249
JF
868 switch (button) {
869 case 1: {
870 NSString *username([[alert textFieldAtIndex:0] text]);
871 NSString *password([[alert textFieldAtIndex:1] text]);
600d005d 872
2634b249 873 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
600d005d 874
2634b249
JF
875 [sender useCredential:credential forAuthenticationChallenge:challenge_];
876 } break;
600d005d 877
2634b249
JF
878 case 2:
879 [sender cancelAuthenticationChallenge:challenge_];
880 break;
600d005d 881
2634b249
JF
882 _nodefault
883 }
600d005d 884
2634b249
JF
885 [challenge_ release];
886 challenge_ = nil;
600d005d 887
2634b249
JF
888 [alert dismissWithClickedButtonIndex:-1 animated:YES];
889 } else if ([context isEqualToString:@"submit"]) {
75b95256
JF
890 if (button == [alert cancelButtonIndex]) {
891 } else if (button == [alert firstOtherButtonIndex]) {
892 if (request_ != nil) {
0893a034 893 WebThreadLocked lock;
75b95256 894 [webview_ loadRequest:request_];
75b95256 895 }
2634b249 896 }
600d005d 897
2634b249
JF
898 [alert dismissWithClickedButtonIndex:-1 animated:YES];
899 }
600d005d
JF
900}
901
2634b249
JF
902- (UIBarButtonItemStyle) rightButtonStyle {
903 if (style_ == nil) normal:
904 return UIBarButtonItemStylePlain;
905 else if ([style_ isEqualToString:@"Normal"])
906 return UIBarButtonItemStylePlain;
907 else if ([style_ isEqualToString:@"Highlighted"])
908 return UIBarButtonItemStyleDone;
909 else goto normal;
600d005d
JF
910}
911
2634b249 912- (UIBarButtonItem *) customButton {
ed5566c7
JF
913 return custom_ == [NSNull null] ? nil : [[[UIBarButtonItem alloc]
914 initWithTitle:static_cast<NSString *>(custom_.operator NSObject *())
2634b249
JF
915 style:[self rightButtonStyle]
916 target:self
917 action:@selector(customButtonClicked)
918 ] autorelease];
caa427d1
JF
919}
920
2634b249
JF
921- (UIBarButtonItem *) rightButton {
922 return reloaditem_;
eb35c522
JF
923}
924
2634b249
JF
925- (void) applyLoadingTitle {
926 [[self navigationItem] setTitle:UCLocalize("LOADING")];
caa427d1
JF
927}
928
c83a464d
JF
929- (void) layoutRightButton {
930 [[loadingitem_ view] addSubview:indicator_];
931 [[loadingitem_ view] bringSubviewToFront:indicator_];
932}
933
2634b249
JF
934- (void) applyRightButton {
935 if ([self isLoading]) {
936 [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES];
c83a464d 937 [self performSelector:@selector(layoutRightButton) withObject:nil afterDelay:0];
8d603a7d
JF
938
939 [indicator_ startAnimating];
2634b249 940 [self applyLoadingTitle];
2634b249 941 } else {
8d603a7d
JF
942 [indicator_ stopAnimating];
943
944 [[self navigationItem] setRightBarButtonItem:(
945 custom_ != nil ? [self customButton] : [self rightButton]
946 ) animated:YES];
2634b249 947 }
caa427d1
JF
948}
949
df30fbee
GP
950- (void) didStartLoading {
951 // Overridden in subclasses.
952}
953
2634b249
JF
954- (void) _didStartLoading {
955 [self applyRightButton];
54043703
JF
956
957 if ([loading_ count] != 1)
958 return;
df30fbee 959
54043703 960 [delegate_ retainNetworkActivityIndicator];
df30fbee
GP
961 [self didStartLoading];
962}
963
964- (void) didFinishLoading {
965 // Overridden in subclasses.
caa427d1
JF
966}
967
2634b249
JF
968- (void) _didFinishLoading {
969 if ([loading_ count] != 0)
970 return;
971
972 [self applyRightButton];
df30fbee 973 [[self navigationItem] setTitle:title_];
2634b249 974
df30fbee
GP
975 [delegate_ releaseNetworkActivityIndicator];
976 [self didFinishLoading];
caa427d1
JF
977}
978
2634b249
JF
979- (bool) isLoading {
980 return [loading_ count] != 0;
caa427d1
JF
981}
982
b5e7eebb
GP
983- (id) initWithWidth:(float)width ofClass:(Class)_class {
984 if ((self = [super init]) != nil) {
245cce8a 985 class_ = _class;
2634b249 986 loading_ = [[NSMutableSet alloc] initWithCapacity:5];
bc11cf5b 987
2634b249 988 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
caa427d1 989
2634b249
JF
990 webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease];
991 [webview_ setDelegate:self];
992 [self setView:webview_];
22f8bed9 993
2634b249
JF
994 if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)])
995 [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic];
996 else
997 [webview_ setDetectsPhoneNumbers:NO];
22f8bed9 998
2634b249 999 [webview_ setScalesPageToFit:YES];
22f8bed9 1000
2634b249 1001 UIWebDocumentView *document([webview_ _documentView]);
ea173384 1002
2634b249
JF
1003 // XXX: I think this improves scrolling; the hardcoded-ness sucks
1004 [document setTileSize:CGSizeMake(320, 500)];
ea173384 1005
2634b249 1006 [document setBackgroundColor:[UIColor clearColor]];
14f17703
JF
1007
1008 // XXX: this is terribly (too?) expensive
2634b249 1009 [document setDrawsBackground:NO];
ea173384 1010
2634b249
JF
1011 WebView *webview([document webView]);
1012 WebPreferences *preferences([webview preferences]);
22f8bed9 1013
2634b249
JF
1014 // XXX: I have no clue if I actually /want/ this modification
1015 if ([webview respondsToSelector:@selector(_setLayoutInterval:)])
1016 [webview _setLayoutInterval:0];
1017 else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)])
1018 [preferences _setLayoutInterval:0];
22f8bed9 1019
2634b249
JF
1020 [preferences setCacheModel:WebCacheModelDocumentBrowser];
1021 [preferences setOfflineWebApplicationCacheEnabled:YES];
22f8bed9 1022
72bdb258
JF
1023#if LogMessages
1024 if ([document respondsToSelector:@selector(setAllowsMessaging:)])
1025 [document setAllowsMessaging:YES];
1026 if ([webview respondsToSelector:@selector(_setAllowsMessaging:)])
1027 [webview _setAllowsMessaging:YES];
1028#endif
1029
2634b249
JF
1030 if ([webview_ respondsToSelector:@selector(_scrollView)]) {
1031 scroller_ = [webview_ _scrollView];
22f8bed9 1032
2634b249 1033 [scroller_ setDirectionalLockEnabled:YES];
9c1605e2 1034 [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal];
2634b249 1035 [scroller_ setDelaysContentTouches:NO];
22f8bed9 1036
2634b249
JF
1037 [scroller_ setCanCancelContentTouches:YES];
1038 } else if ([webview_ respondsToSelector:@selector(_scroller)]) {
1039 UIScroller *scroller([webview_ _scroller]);
1040 scroller_ = (UIScrollView *) scroller;
22f8bed9 1041
2634b249 1042 [scroller setDirectionalScrolling:YES];
9c1605e2
JF
1043 // XXX: we might be better off /not/ setting this on older systems
1044 [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */
2634b249 1045 [scroller setScrollHysteresis:0]; /* 8 */
3e9c9e85 1046
2634b249 1047 [scroller setThumbDetectionEnabled:NO];
ea173384 1048
2634b249
JF
1049 // use NO with UIApplicationUseLegacyEvents(YES)
1050 [scroller setEventMode:YES];
ea173384 1051
2634b249
JF
1052 // XXX: this is handled by setBounces, right?
1053 //[scroller setAllowsRubberBanding:YES];
22f8bed9 1054 }
bc11cf5b 1055
2634b249 1056 [scroller_ setFixedBackgroundPattern:YES];
e665fe98 1057 [scroller_ setBackgroundColor:[self groupTableViewBackgroundColor]];
2634b249 1058 [scroller_ setClipsSubviews:YES];
caa427d1 1059
2634b249
JF
1060 [scroller_ setBounces:YES];
1061 [scroller_ setScrollingEnabled:YES];
1062 [scroller_ setShowBackgroundShadow:NO];
caa427d1 1063
2634b249 1064 [self setViewportWidth:width];
bc11cf5b
JF
1065
1066 reloaditem_ = [[UIBarButtonItem alloc]
1067 initWithTitle:UCLocalize("RELOAD")
1068 style:[self rightButtonStyle]
1069 target:self
1070 action:@selector(reloadButtonClicked)
1071 ];
1072
1073 loadingitem_ = [[UIBarButtonItem alloc]
1074 initWithTitle:@" "
1075 style:UIBarButtonItemStylePlain
1076 target:self
1077 action:@selector(reloadButtonClicked)
1078 ];
2634b249 1079
7a11fbb9
GP
1080 indicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
1081 [indicator_ setFrame:CGRectMake(15, 5, [indicator_ frame].size.width, [indicator_ frame].size.height)];
22f8bed9 1082
2634b249 1083 [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
04fe1349 1084 [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];
22f8bed9
JF
1085 } return self;
1086}
1087
b5e7eebb
GP
1088- (id) initWithWidth:(float)width {
1089 return [self initWithWidth:width ofClass:[self class]];
245cce8a
JF
1090}
1091
b5e7eebb
GP
1092- (id) init {
1093 return [self initWithWidth:0];
3e9c9e85
JF
1094}
1095
eb09425a
JF
1096- (id) initWithURL:(NSURL *)url {
1097 if ((self = [self init]) != nil) {
1098 [self setURL:url];
1099 } return self;
1100}
1101
2634b249
JF
1102- (void) didDismissModalViewController {
1103 if (closer_ != nil)
1104 [self callFunction:closer_];
22f8bed9
JF
1105}
1106
12b59862 1107- (void) callFunction:(WebScriptObject *)function {
0893a034 1108 WebThreadLocked lock;
caa427d1 1109
2634b249 1110 WebView *webview([[webview_ _documentView] webView]);
12b59862 1111 WebFrame *frame([webview mainFrame]);
c21004b9
JF
1112 WebPreferences *preferences([webview preferences]);
1113
1114 bool maybe([preferences javaScriptCanOpenWindowsAutomatically]);
1115 [preferences setJavaScriptCanOpenWindowsAutomatically:NO];
22f8bed9 1116
c21004b9 1117 /*id _private(MSHookIvar<id>(webview, "_private"));
12b59862
JF
1118 WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page"));
1119 WebCore::Settings *settings(page == NULL ? NULL : page->settings());
22f8bed9 1120
12b59862
JF
1121 bool no;
1122 if (settings == NULL)
1123 no = 0;
1124 else {
1125 no = settings->JavaScriptCanOpenWindowsAutomatically();
1126 settings->setJavaScriptCanOpenWindowsAutomatically(true);
c21004b9 1127 }*/
12b59862 1128
b5e7eebb 1129 if (UIWindow *window = [[self view] window])
43f3d7f6
JF
1130 if (UIResponder *responder = [window firstResponder])
1131 [responder resignFirstResponder];
1132
12b59862
JF
1133 JSObjectRef object([function JSObject]);
1134 JSGlobalContextRef context([frame globalContext]);
1135 JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL);
1136
c21004b9
JF
1137 /*if (settings != NULL)
1138 settings->setJavaScriptCanOpenWindowsAutomatically(no);*/
1139
1140 [preferences setJavaScriptCanOpenWindowsAutomatically:maybe];
12b59862
JF
1141}
1142
ce041f4f 1143- (void) reloadButtonClicked {
b13b8664 1144 [self reloadURLWithCache:YES];
12b59862
JF
1145}
1146
719d6c2f
DH
1147- (void) _customButtonClicked {
1148 [self reloadButtonClicked];
1149}
1150
ce041f4f 1151- (void) customButtonClicked {
caa427d1
JF
1152#if !AlwaysReload
1153 if (function_ != nil)
12b59862 1154 [self callFunction:function_];
caa427d1
JF
1155 else
1156#endif
719d6c2f 1157 [self _customButtonClicked];
22f8bed9
JF
1158}
1159
3e9c9e85
JF
1160+ (float) defaultWidth {
1161 return 980;
1162}
1163
82406217
JF
1164- (void) setNavigationBarStyle:(NSString *)name {
1165 UIBarStyle style;
1166 if ([name isEqualToString:@"Black"])
1167 style = UIBarStyleBlack;
1168 else
1169 style = UIBarStyleDefault;
1170
1171 [[[self navigationController] navigationBar] setBarStyle:style];
1172}
1173
00984204
JF
1174- (void) setNavigationBarTintColor:(UIColor *)color {
1175 [[[self navigationController] navigationBar] setTintColor:color];
1176}
1177
b8a5d89d
JF
1178- (void) setHidesBackButton:(bool)value {
1179 [[self navigationItem] setHidesBackButton:value];
1180}
1181
1182- (void) setHidesBackButtonByNumber:(NSNumber *)value {
1183 [self setHidesBackButton:[value boolValue]];
1184}
1185
f196b921
JF
1186- (void) dispatchEvent:(NSString *)event {
1187 WebThreadLocked lock;
1188
1189 NSString *script([NSString stringWithFormat:@
1190 "(function() {"
1191 "var event = this.document.createEvent('Events');"
1192 "event.initEvent('%@', false, false);"
1193 "this.document.dispatchEvent(event);"
1194 "})();"
1195 , event]);
1196
1197 NSMutableArray *frames([NSMutableArray arrayWithObjects:
1198 [[[webview_ _documentView] webView] mainFrame]
1199 , nil]);
1200
1201 while (WebFrame *frame = [frames lastObject]) {
1202 WebScriptObject *object([frame windowObject]);
1203 [object evaluateWebScript:script];
1204 [frames removeLastObject];
1205 [frames addObjectsFromArray:[frame childFrames]];
1206 }
1207}
1208
5cdfcd6f
JF
1209- (bool) hidesNavigationBar {
1210 return hidesNavigationBar_;
1211}
1212
1213- (void) _setHidesNavigationBar:(bool)value animated:(bool)animated {
1214 if (visible_)
1215 [[self navigationController] setNavigationBarHidden:(value && [self hidesNavigationBar]) animated:animated];
1216}
1217
1218- (void) setHidesNavigationBar:(bool)value {
1219 if (hidesNavigationBar_ != value) {
1220 hidesNavigationBar_ = value;
1221 [self _setHidesNavigationBar:YES animated:YES];
1222 }
1223}
1224
1225- (void) setHidesNavigationBarByNumber:(NSNumber *)value {
1226 [self setHidesNavigationBar:[value boolValue]];
1227}
1228
f196b921 1229- (void) viewWillAppear:(BOOL)animated {
5cdfcd6f
JF
1230 visible_ = true;
1231
1232 if ([self hidesNavigationBar])
1233 [self _setHidesNavigationBar:YES animated:animated];
1234
f196b921
JF
1235 [self dispatchEvent:@"CydiaViewWillAppear"];
1236 [super viewWillAppear:animated];
1237}
1238
1239- (void) viewDidAppear:(BOOL)animated {
f196b921 1240 [super viewDidAppear:animated];
5cdfcd6f 1241 [self dispatchEvent:@"CydiaViewDidAppear"];
f196b921
JF
1242}
1243
1244- (void) viewWillDisappear:(BOOL)animated {
1245 [self dispatchEvent:@"CydiaViewWillDisappear"];
1246 [super viewWillDisappear:animated];
5cdfcd6f
JF
1247
1248 if ([self hidesNavigationBar])
1249 [self _setHidesNavigationBar:NO animated:animated];
1250
1251 visible_ = false;
f196b921
JF
1252}
1253
1254- (void) viewDidDisappear:(BOOL)animated {
f196b921 1255 [super viewDidDisappear:animated];
5cdfcd6f 1256 [self dispatchEvent:@"CydiaViewDidDisappear"];
f196b921
JF
1257}
1258
a5938ea5 1259@end