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