]>
Commit | Line | Data |
---|---|---|
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 |
11 | extern 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 |
bfc87a4d | 30 | |
2634b249 JF |
31 | template <typename Type_> |
32 | static inline void CYRelease(Type_ &value) { | |
33 | if (value != nil) { | |
34 | [value release]; | |
35 | value = nil; | |
36 | } | |
37 | } | |
04fe1349 | 38 | |
9c1605e2 JF |
39 | float CYScrollViewDecelerationRateNormal; |
40 | ||
2634b249 JF |
41 | @interface WebView (Apple) |
42 | - (void) _setLayoutInterval:(float)interval; | |
43 | @end | |
44 | ||
45 | @interface WebPreferences (Apple) | |
46 | + (void) _setInitialDefaultTextEncodingToSystemEncoding; | |
47 | - (void) _setLayoutInterval:(NSInteger)interval; | |
48 | - (void) setOfflineWebApplicationCacheEnabled:(BOOL)enabled; | |
49 | @end | |
327624b6 | 50 | |
aa5d0de7 | 51 | /* Indirect Delegate {{{ */ |
c21004b9 JF |
52 | @interface IndirectDelegate : NSObject < |
53 | HookProtocol | |
54 | > { | |
aa5d0de7 JF |
55 | _transient volatile id delegate_; |
56 | } | |
57 | ||
58 | - (void) setDelegate:(id)delegate; | |
59 | - (id) initWithDelegate:(id)delegate; | |
60 | @end | |
61 | ||
62 | @implementation IndirectDelegate | |
63 | ||
64 | - (void) setDelegate:(id)delegate { | |
65 | delegate_ = delegate; | |
66 | } | |
67 | ||
68 | - (id) initWithDelegate:(id)delegate { | |
69 | delegate_ = delegate; | |
70 | return self; | |
71 | } | |
72 | ||
c21004b9 JF |
73 | - (void) didDismissModalViewController { |
74 | if (delegate_ != nil) | |
75 | return [delegate_ didDismissModalViewController]; | |
76 | } | |
77 | ||
caa427d1 JF |
78 | - (IMP) methodForSelector:(SEL)sel { |
79 | if (IMP method = [super methodForSelector:sel]) | |
80 | return method; | |
81 | fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel)); | |
82 | return NULL; | |
83 | } | |
84 | ||
aa5d0de7 | 85 | - (BOOL) respondsToSelector:(SEL)sel { |
caa427d1 JF |
86 | if ([super respondsToSelector:sel]) |
87 | return YES; | |
88 | // XXX: WebThreadCreateNSInvocation returns nil | |
89 | //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel)); | |
90 | return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel]; | |
aa5d0de7 JF |
91 | } |
92 | ||
93 | - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel { | |
caa427d1 JF |
94 | if (NSMethodSignature *method = [super methodSignatureForSelector:sel]) |
95 | return method; | |
96 | //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel)); | |
aa5d0de7 JF |
97 | if (delegate_ != nil) |
98 | if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel]) | |
99 | return sig; | |
100 | // XXX: I fucking hate Apple so very very bad | |
101 | return [NSMethodSignature signatureWithObjCTypes:"v@:"]; | |
102 | } | |
103 | ||
104 | - (void) forwardInvocation:(NSInvocation *)inv { | |
105 | SEL sel = [inv selector]; | |
106 | if (delegate_ != nil && [delegate_ respondsToSelector:sel]) | |
107 | [inv invokeWithTarget:delegate_]; | |
108 | } | |
109 | ||
110 | @end | |
111 | /* }}} */ | |
112 | ||
43f3d7f6 | 113 | @implementation WebScriptObject (UICaboodle) |
caa427d1 | 114 | |
96f3833b | 115 | - (NSUInteger) count { |
caa427d1 JF |
116 | id length([self valueForKey:@"length"]); |
117 | if ([length respondsToSelector:@selector(intValue)]) | |
118 | return [length intValue]; | |
119 | else | |
120 | return 0; | |
121 | } | |
122 | ||
123 | - (id) objectAtIndex:(unsigned)index { | |
124 | return [self webScriptValueAtIndex:index]; | |
125 | } | |
126 | ||
127 | @end | |
128 | ||
2634b249 JF |
129 | // CYWebPolicyDecision* {{{ |
130 | enum CYWebPolicyDecision { | |
131 | CYWebPolicyDecisionUnknown, | |
132 | CYWebPolicyDecisionDownload, | |
133 | CYWebPolicyDecisionIgnore, | |
134 | CYWebPolicyDecisionUse, | |
135 | }; | |
136 | ||
137 | @interface CYWebPolicyDecisionMediator : NSObject < | |
138 | WebPolicyDecisionListener | |
139 | > { | |
140 | id<WebPolicyDecisionListener> listener_; | |
141 | CYWebPolicyDecision decision_; | |
69b82e6a | 142 | } |
2634b249 JF |
143 | |
144 | - (id) initWithListener:(id<WebPolicyDecisionListener>)listener; | |
145 | ||
146 | - (CYWebPolicyDecision) decision; | |
147 | - (bool) decided; | |
148 | - (bool) decide; | |
149 | ||
69b82e6a RP |
150 | @end |
151 | ||
2634b249 | 152 | @implementation CYWebPolicyDecisionMediator |
69b82e6a | 153 | |
2634b249 JF |
154 | - (id) initWithListener:(id<WebPolicyDecisionListener>)listener { |
155 | if ((self = [super init]) != nil) { | |
156 | listener_ = listener; | |
157 | } return self; | |
158 | } | |
69b82e6a | 159 | |
2634b249 JF |
160 | - (CYWebPolicyDecision) decision { |
161 | return decision_; | |
162 | } | |
163 | ||
164 | - (bool) decided { | |
165 | return decision_ != CYWebPolicyDecisionUnknown; | |
166 | } | |
167 | ||
168 | - (bool) decide { | |
169 | switch (decision_) { | |
170 | case CYWebPolicyDecisionUnknown: | |
171 | default: | |
0352f238 | 172 | NSLog(@"CYWebPolicyDecisionUnknown"); |
2634b249 JF |
173 | return false; |
174 | ||
175 | case CYWebPolicyDecisionDownload: [listener_ download]; break; | |
176 | case CYWebPolicyDecisionIgnore: [listener_ ignore]; break; | |
177 | case CYWebPolicyDecisionUse: [listener_ use]; break; | |
178 | } | |
179 | ||
180 | return true; | |
181 | } | |
182 | ||
183 | - (void) download { | |
184 | decision_ = CYWebPolicyDecisionDownload; | |
69b82e6a RP |
185 | } |
186 | ||
2634b249 JF |
187 | - (void) ignore { |
188 | decision_ = CYWebPolicyDecisionIgnore; | |
189 | } | |
190 | ||
191 | - (void) use { | |
192 | decision_ = CYWebPolicyDecisionUse; | |
69b82e6a RP |
193 | } |
194 | ||
195 | @end | |
2634b249 | 196 | // }}} |
69b82e6a | 197 | |
2634b249 | 198 | @implementation CYWebView : UIWebView |
ea173384 | 199 | |
2634b249 JF |
200 | - (id) initWithFrame:(CGRect)frame { |
201 | if ((self = [super initWithFrame:frame]) != nil) { | |
202 | } return self; | |
203 | } | |
ea173384 | 204 | |
2634b249 JF |
205 | - (void) dealloc { |
206 | [super dealloc]; | |
207 | } | |
22f8bed9 | 208 | |
2634b249 JF |
209 | - (id<CYWebViewDelegate>) delegate { |
210 | return (id<CYWebViewDelegate>) [super delegate]; | |
211 | } | |
22f8bed9 | 212 | |
2634b249 | 213 | /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request { |
7880ff90 JF |
214 | id<CYWebViewDelegate> delegate([self delegate]); |
215 | WebView *created(nil); | |
216 | if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)]) | |
217 | created = [delegate webView:view createWebViewWithRequest:request]; | |
d49718a1 | 218 | if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)]) |
7880ff90 JF |
219 | created = [super webView:view createWebViewWithRequest:request]; |
220 | return created; | |
2634b249 | 221 | }*/ |
ea173384 | 222 | |
d49718a1 | 223 | // webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{ |
2634b249 | 224 | - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { |
d49718a1 | 225 | id<CYWebViewDelegate> delegate([self delegate]); |
2634b249 | 226 | CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]); |
d49718a1 JF |
227 | if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)]) |
228 | [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator]; | |
229 | if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)]) | |
2634b249 JF |
230 | [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator]; |
231 | [mediator decide]; | |
232 | } | |
d49718a1 JF |
233 | // }}} |
234 | // webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{ | |
235 | static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) { | |
236 | UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView")); | |
237 | if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)]) | |
238 | [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener]; | |
239 | } | |
ea173384 | 240 | |
2634b249 | 241 | - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { |
d49718a1 | 242 | id<CYWebViewDelegate> delegate([self delegate]); |
2634b249 | 243 | CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]); |
d49718a1 JF |
244 | if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)]) |
245 | [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator]; | |
246 | if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)]) | |
2634b249 JF |
247 | [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator]; |
248 | [mediator decide]; | |
249 | } | |
d49718a1 | 250 | // }}} |
3a1ff609 | 251 | // webView:didClearWindowObject:forFrame: (3.2+) {{{ |
d49718a1 JF |
252 | static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) { |
253 | UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView")); | |
254 | if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)]) | |
255 | [uiWebView webView:view didClearWindowObject:window forFrame:frame]; | |
256 | } | |
327624b6 | 257 | |
2634b249 | 258 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
d49718a1 JF |
259 | id<CYWebViewDelegate> delegate([self delegate]); |
260 | if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)]) | |
261 | [delegate webView:view didClearWindowObject:window forFrame:frame]; | |
262 | if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)]) | |
2634b249 JF |
263 | [super webView:view didClearWindowObject:window forFrame:frame]; |
264 | } | |
d49718a1 JF |
265 | // }}} |
266 | // webView:didFailLoadWithError:forFrame: (2.0+) {{{ | |
2634b249 | 267 | - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { |
d49718a1 JF |
268 | id<CYWebViewDelegate> delegate([self delegate]); |
269 | if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)]) | |
270 | [delegate webView:view didFailLoadWithError:error forFrame:frame]; | |
271 | if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)]) | |
2634b249 | 272 | [super webView:view didFailLoadWithError:error forFrame:frame]; |
ea173384 | 273 | } |
d49718a1 JF |
274 | // }}} |
275 | // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{ | |
2634b249 | 276 | - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { |
d49718a1 JF |
277 | id<CYWebViewDelegate> delegate([self delegate]); |
278 | if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)]) | |
279 | [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame]; | |
280 | if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)]) | |
2634b249 JF |
281 | [super webView:view didFailProvisionalLoadWithError:error forFrame:frame]; |
282 | } | |
d49718a1 JF |
283 | // }}} |
284 | // webView:didFinishLoadForFrame: (2.0+) {{{ | |
2634b249 | 285 | - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame { |
d49718a1 JF |
286 | id<CYWebViewDelegate> delegate([self delegate]); |
287 | if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)]) | |
288 | [delegate webView:view didFinishLoadForFrame:frame]; | |
289 | if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)]) | |
2634b249 JF |
290 | [super webView:view didFinishLoadForFrame:frame]; |
291 | } | |
d49718a1 | 292 | // }}} |
3a1ff609 | 293 | // webView:didReceiveTitle:forFrame: (3.2+) {{{ |
d49718a1 JF |
294 | static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) { |
295 | UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView")); | |
296 | if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)]) | |
297 | [uiWebView webView:view didReceiveTitle:title forFrame:frame]; | |
298 | } | |
22f8bed9 | 299 | |
2634b249 | 300 | - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { |
d49718a1 JF |
301 | id<CYWebViewDelegate> delegate([self delegate]); |
302 | if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)]) | |
303 | [delegate webView:view didReceiveTitle:title forFrame:frame]; | |
304 | if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)]) | |
2634b249 JF |
305 | [super webView:view didReceiveTitle:title forFrame:frame]; |
306 | } | |
d49718a1 JF |
307 | // }}} |
308 | // webView:didStartProvisionalLoadForFrame: (2.0+) {{{ | |
2634b249 | 309 | - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame { |
d49718a1 JF |
310 | id<CYWebViewDelegate> delegate([self delegate]); |
311 | if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)]) | |
312 | [delegate webView:view didStartProvisionalLoadForFrame:frame]; | |
313 | if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)]) | |
2634b249 JF |
314 | [super webView:view didStartProvisionalLoadForFrame:frame]; |
315 | } | |
d49718a1 | 316 | // }}} |
3a1ff609 | 317 | // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{ |
d49718a1 JF |
318 | static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) { |
319 | UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView")); | |
320 | if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)]) | |
321 | return [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source]; | |
322 | else | |
323 | return request; | |
324 | } | |
22f8bed9 | 325 | |
2634b249 | 326 | - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
d49718a1 JF |
327 | id<CYWebViewDelegate> delegate([self delegate]); |
328 | if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)]) | |
2634b249 | 329 | request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source]; |
d49718a1 JF |
330 | if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)]) |
331 | request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source]; | |
332 | return request; | |
2634b249 | 333 | } |
d49718a1 | 334 | // }}} |
427754d6 | 335 | // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{ |
2634b249 | 336 | - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
d49718a1 JF |
337 | id<CYWebViewDelegate> delegate([self delegate]); |
338 | if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)]) | |
339 | // XXX: check delegate | |
340 | if ([delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]) | |
2634b249 JF |
341 | [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]; |
342 | } | |
d49718a1 | 343 | // }}} |
427754d6 | 344 | // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{ |
2634b249 | 345 | - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
d49718a1 JF |
346 | id<CYWebViewDelegate> delegate([self delegate]); |
347 | if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)]) | |
348 | // XXX: check delegate | |
349 | if ([delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]) | |
2634b249 JF |
350 | return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]; |
351 | return NO; | |
352 | } | |
d49718a1 | 353 | // }}} |
427754d6 | 354 | // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{ |
2634b249 | 355 | - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame { |
d49718a1 JF |
356 | id<CYWebViewDelegate> delegate([self delegate]); |
357 | if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)]) | |
358 | // XXX: check delegate | |
359 | if ([delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]) | |
2634b249 JF |
360 | return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]; |
361 | return nil; | |
362 | } | |
d49718a1 | 363 | // }}} |
3a1ff609 | 364 | // webViewClose: (3.2+) {{{ |
d49718a1 JF |
365 | static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) { |
366 | UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView")); | |
367 | if ([uiWebView respondsToSelector:@selector(webViewClose:)]) | |
368 | [uiWebView webViewClose:view]; | |
369 | } | |
600d005d | 370 | |
2634b249 | 371 | - (void) webViewClose:(WebView *)view { |
d49718a1 JF |
372 | id<CYWebViewDelegate> delegate([self delegate]); |
373 | if ([delegate respondsToSelector:@selector(webViewClose:)]) | |
374 | [delegate webViewClose:view]; | |
375 | if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)]) | |
2634b249 JF |
376 | [super webViewClose:view]; |
377 | } | |
d49718a1 JF |
378 | // }}} |
379 | ||
380 | + (void) initialize { | |
381 | if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) { | |
382 | class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24"); | |
383 | class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16"); | |
384 | class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16"); | |
385 | class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24"); | |
386 | class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8"); | |
387 | } | |
388 | } | |
22f8bed9 | 389 | |
2634b249 | 390 | @end |
caa427d1 | 391 | |
2634b249 JF |
392 | #define ShowInternals 0 |
393 | #define LogBrowser 1 | |
22f8bed9 | 394 | |
2634b249 | 395 | #define lprintf(args...) fprintf(stderr, args) |
22f8bed9 | 396 | |
2634b249 JF |
397 | @implementation BrowserController |
398 | ||
399 | #if ShowInternals | |
400 | #include "UICaboodle/UCInternal.h" | |
22f8bed9 JF |
401 | #endif |
402 | ||
2634b249 JF |
403 | + (void) _initialize { |
404 | [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding]; | |
9c1605e2 JF |
405 | |
406 | if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal"))) | |
407 | CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal; | |
408 | else // XXX: this actually might be fast on some older systems: we should look into this | |
409 | CYScrollViewDecelerationRateNormal = 0.998; | |
2634b249 JF |
410 | } |
411 | ||
412 | - (void) dealloc { | |
413 | #if LogBrowser | |
414 | NSLog(@"[BrowserController dealloc]"); | |
415 | #endif | |
416 | ||
417 | [webview_ setDelegate:nil]; | |
418 | ||
419 | [indirect_ setDelegate:nil]; | |
22f8bed9 JF |
420 | [indirect_ release]; |
421 | ||
2634b249 JF |
422 | if (challenge_ != nil) |
423 | [challenge_ release]; | |
caa427d1 | 424 | |
2634b249 | 425 | //NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
22f8bed9 | 426 | |
2634b249 JF |
427 | if (custom_ != nil) |
428 | [custom_ release]; | |
22f8bed9 JF |
429 | if (style_ != nil) |
430 | [style_ release]; | |
2634b249 | 431 | |
22f8bed9 JF |
432 | if (function_ != nil) |
433 | [function_ release]; | |
12b59862 JF |
434 | if (closer_ != nil) |
435 | [closer_ release]; | |
22f8bed9 | 436 | |
0dceb29b JF |
437 | if (sensitive_ != nil) |
438 | [sensitive_ release]; | |
22f8bed9 JF |
439 | if (title_ != nil) |
440 | [title_ release]; | |
2634b249 JF |
441 | |
442 | [reloaditem_ release]; | |
443 | [loadingitem_ release]; | |
444 | ||
445 | [indicator_ release]; | |
bc11cf5b | 446 | |
22f8bed9 JF |
447 | [super dealloc]; |
448 | } | |
449 | ||
450 | - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy { | |
451 | [self loadRequest:[NSURLRequest | |
452 | requestWithURL:url | |
453 | cachePolicy:policy | |
77801ff1 | 454 | timeoutInterval:120.0 |
22f8bed9 JF |
455 | ]]; |
456 | } | |
457 | ||
458 | - (void) loadURL:(NSURL *)url { | |
459 | [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]; | |
460 | } | |
461 | ||
22f8bed9 | 462 | - (void) loadRequest:(NSURLRequest *)request { |
0352f238 JF |
463 | #if LogBrowser |
464 | NSLog(@"loadRequest:%@", request); | |
465 | #endif | |
466 | ||
fe468f45 | 467 | error_ = false; |
caa427d1 JF |
468 | |
469 | WebThreadLock(); | |
2634b249 | 470 | [webview_ loadRequest:request]; |
caa427d1 | 471 | WebThreadUnlock(); |
22f8bed9 JF |
472 | } |
473 | ||
474 | - (void) reloadURL { | |
22f8bed9 JF |
475 | if (request_ == nil) |
476 | return; | |
477 | ||
478 | if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil) | |
fe468f45 | 479 | [self loadRequest:request_]; |
22f8bed9 | 480 | else { |
79ed082a | 481 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 482 | initWithTitle:UCLocalize("RESUBMIT_FORM") |
79ed082a | 483 | message:nil |
22f8bed9 | 484 | delegate:self |
79ed082a GP |
485 | cancelButtonTitle:UCLocalize("CANCEL") |
486 | otherButtonTitles:UCLocalize("SUBMIT"), nil | |
22f8bed9 | 487 | ] autorelease]; |
2634b249 | 488 | |
79ed082a GP |
489 | [alert setContext:@"submit"]; |
490 | [alert show]; | |
22f8bed9 JF |
491 | } |
492 | } | |
493 | ||
22f8bed9 | 494 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
2634b249 JF |
495 | if (custom_ != nil) |
496 | [custom_ autorelease]; | |
497 | custom_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain]; | |
22f8bed9 JF |
498 | |
499 | if (style_ != nil) | |
500 | [style_ autorelease]; | |
501 | style_ = style == nil ? nil : [style retain]; | |
502 | ||
503 | if (function_ != nil) | |
504 | [function_ autorelease]; | |
505 | function_ = function == nil ? nil : [function retain]; | |
12b59862 | 506 | |
bc11cf5b | 507 | [self applyRightButton]; |
22f8bed9 JF |
508 | } |
509 | ||
510 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
2634b249 JF |
511 | if (custom_ != nil) |
512 | [custom_ autorelease]; | |
513 | custom_ = button == nil ? nil : [button retain]; | |
22f8bed9 JF |
514 | |
515 | if (style_ != nil) | |
516 | [style_ autorelease]; | |
517 | style_ = style == nil ? nil : [style retain]; | |
518 | ||
519 | if (function_ != nil) | |
520 | [function_ autorelease]; | |
521 | function_ = function == nil ? nil : [function retain]; | |
12b59862 | 522 | |
bc11cf5b | 523 | [self applyRightButton]; |
22f8bed9 | 524 | } |
22f8bed9 | 525 | |
caa427d1 JF |
526 | - (void) setPopupHook:(id)function { |
527 | if (closer_ != nil) | |
528 | [closer_ autorelease]; | |
529 | closer_ = function == nil ? nil : [function retain]; | |
530 | } | |
531 | ||
2634b249 JF |
532 | - (void) setViewportWidth:(float)width { |
533 | width_ = width != 0 ? width : [[self class] defaultWidth]; | |
534 | [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; | |
22f8bed9 JF |
535 | } |
536 | ||
2634b249 JF |
537 | - (void) _openMailToURL:(NSURL *)url { |
538 | [[UIApplication sharedApplication] openURL:url];// asPanel:YES]; | |
22f8bed9 JF |
539 | } |
540 | ||
2634b249 JF |
541 | - (bool) _allowJavaScriptPanel { |
542 | return true; | |
22f8bed9 JF |
543 | } |
544 | ||
2634b249 JF |
545 | - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame { |
546 | [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]]; | |
547 | [self _didFinishLoading]; | |
22f8bed9 | 548 | |
2634b249 | 549 | if ([error code] == NSURLErrorCancelled) |
22f8bed9 JF |
550 | return; |
551 | ||
caa427d1 | 552 | if ([frame parentFrame] == nil) { |
2634b249 JF |
553 | [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", |
554 | [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString], | |
555 | [[error localizedDescription] stringByAddingPercentEscapes] | |
556 | ]]]; | |
22f8bed9 | 557 | |
2634b249 JF |
558 | error_ = true; |
559 | } | |
560 | } | |
caa427d1 | 561 | |
2634b249 JF |
562 | // CYWebViewDelegate {{{ |
563 | - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
564 | #if LogBrowser | |
565 | NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame); | |
566 | #endif | |
600d005d | 567 | |
2634b249 JF |
568 | if (!error_ && [frame parentFrame] == nil) { |
569 | if (request_ != nil) | |
570 | [request_ autorelease]; | |
571 | if (request == nil) | |
572 | request_ = nil; | |
600d005d | 573 | else |
2634b249 | 574 | request_ = [request retain]; |
caa427d1 | 575 | } |
22f8bed9 JF |
576 | } |
577 | ||
2634b249 JF |
578 | - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { |
579 | #if LogBrowser | |
580 | NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame); | |
581 | #endif | |
2e6c1426 | 582 | |
2634b249 JF |
583 | NSURL *url([request URL]); |
584 | if (url == nil) | |
585 | return; | |
bc11cf5b | 586 | |
2634b249 JF |
587 | if ([frame isEqualToString:@"_open"]) |
588 | [delegate_ openURL:url]; | |
2e6c1426 | 589 | |
2634b249 JF |
590 | NSString *scheme([[url scheme] lowercaseString]); |
591 | if ([scheme isEqualToString:@"mailto"]) | |
592 | [self _openMailToURL:url]; | |
a5938ea5 | 593 | |
2634b249 | 594 | CYViewController *page([delegate_ pageForURL:url hasTag:NULL]); |
2fad210a | 595 | |
2634b249 JF |
596 | if (page == nil) { |
597 | BrowserController *browser([[[class_ alloc] init] autorelease]); | |
598 | [browser loadRequest:request]; | |
599 | page = browser; | |
bc11cf5b | 600 | } |
ce041f4f | 601 | |
2634b249 | 602 | [page setDelegate:delegate_]; |
b5e7eebb | 603 | |
2634b249 JF |
604 | if (![frame isEqualToString:@"_popup"]) { |
605 | [[self navigationItem] setTitle:title_]; | |
b5e7eebb | 606 | |
2634b249 JF |
607 | [[self navigationController] pushViewController:page animated:YES]; |
608 | } else { | |
609 | UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]); | |
22f8bed9 | 610 | |
2634b249 JF |
611 | [navigation setHook:indirect_]; |
612 | [navigation setDelegate:delegate_]; | |
22f8bed9 | 613 | |
2634b249 JF |
614 | [navigation setViewControllers:[NSArray arrayWithObject:page]]; |
615 | ||
616 | [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
617 | initWithTitle:UCLocalize("CLOSE") | |
618 | style:UIBarButtonItemStylePlain | |
619 | target:page | |
620 | action:@selector(close) | |
621 | ] autorelease]]; | |
622 | ||
623 | [[self navigationController] presentModalViewController:navigation animated:YES]; | |
624 | } | |
22f8bed9 | 625 | |
2634b249 | 626 | [listener ignore]; |
22f8bed9 JF |
627 | } |
628 | ||
2634b249 | 629 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
22f8bed9 JF |
630 | } |
631 | ||
2634b249 JF |
632 | - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { |
633 | #if LogBrowser | |
634 | NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame); | |
635 | #endif | |
636 | ||
637 | [self _didFailWithError:error forFrame:frame]; | |
22f8bed9 JF |
638 | } |
639 | ||
2634b249 JF |
640 | - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { |
641 | #if LogBrowser | |
642 | NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame); | |
643 | #endif | |
644 | ||
645 | [self _didFailWithError:error forFrame:frame]; | |
22f8bed9 JF |
646 | } |
647 | ||
2634b249 | 648 | - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame { |
7592e053 | 649 | [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]]; |
22f8bed9 | 650 | |
caa427d1 | 651 | if ([frame parentFrame] == nil) { |
22f8bed9 JF |
652 | if (DOMDocument *document = [frame DOMDocument]) |
653 | if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"]) | |
96f3833b | 654 | for (DOMHTMLBodyElement *body in (id) bodies) { |
22f8bed9 JF |
655 | DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]); |
656 | ||
657 | bool colored(false); | |
658 | ||
659 | if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) { | |
fe468f45 JF |
660 | if ([color primitiveType] == DOM_CSS_RGBCOLOR) { |
661 | DOMRGBColor *rgb([color getRGBColorValue]); | |
662 | ||
663 | float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]); | |
664 | float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]); | |
665 | float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]); | |
666 | float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]); | |
667 | ||
668 | UIColor *uic(nil); | |
669 | ||
670 | if (red == 0xc7 && green == 0xce && blue == 0xd5) | |
4f6056df | 671 | uic = [UIColor groupTableViewBackgroundColor]; |
fe468f45 JF |
672 | else if (alpha != 0) |
673 | uic = [UIColor | |
674 | colorWithRed:(red / 255) | |
675 | green:(green / 255) | |
676 | blue:(blue / 255) | |
677 | alpha:alpha | |
678 | ]; | |
679 | ||
680 | if (uic != nil) { | |
681 | colored = true; | |
682 | [scroller_ setBackgroundColor:uic]; | |
683 | } | |
22f8bed9 JF |
684 | } |
685 | } | |
686 | ||
687 | if (!colored) | |
4f6056df | 688 | [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; |
22f8bed9 JF |
689 | break; |
690 | } | |
691 | } | |
692 | ||
2634b249 | 693 | [self _didFinishLoading]; |
22f8bed9 JF |
694 | } |
695 | ||
2634b249 JF |
696 | - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { |
697 | if ([frame parentFrame] != nil) | |
698 | return; | |
caa427d1 | 699 | |
2634b249 JF |
700 | title_ = [title retain]; |
701 | [[self navigationItem] setTitle:title_]; | |
702 | } | |
22f8bed9 | 703 | |
2634b249 JF |
704 | - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame { |
705 | [loading_ addObject:[NSValue valueWithNonretainedObject:frame]]; | |
7592e053 | 706 | |
caa427d1 | 707 | if ([frame parentFrame] == nil) { |
2634b249 JF |
708 | CYRelease(title_); |
709 | CYRelease(custom_); | |
710 | CYRelease(style_); | |
711 | CYRelease(function_); | |
712 | CYRelease(closer_); | |
713 | ||
714 | // XXX: do we still need to do this? | |
715 | [[self navigationItem] setTitle:nil]; | |
caa427d1 | 716 | } |
fe468f45 | 717 | |
2634b249 | 718 | [self _didStartLoading]; |
caa427d1 JF |
719 | } |
720 | ||
2634b249 | 721 | - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
0352f238 JF |
722 | #if LogBrowser |
723 | NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source); | |
724 | #endif | |
725 | ||
2634b249 | 726 | return request; |
22f8bed9 JF |
727 | } |
728 | ||
2634b249 JF |
729 | - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
730 | return [self _allowJavaScriptPanel]; | |
22f8bed9 JF |
731 | } |
732 | ||
2634b249 JF |
733 | - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
734 | return [self _allowJavaScriptPanel]; | |
600d005d JF |
735 | } |
736 | ||
2634b249 JF |
737 | - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame { |
738 | return [self _allowJavaScriptPanel]; | |
600d005d JF |
739 | } |
740 | ||
2634b249 JF |
741 | - (void) webViewClose:(WebView *)view { |
742 | [self close]; | |
600d005d | 743 | } |
2634b249 | 744 | // }}} |
600d005d | 745 | |
2634b249 JF |
746 | - (void) close { |
747 | [[self navigationController] dismissModalViewControllerAnimated:YES]; | |
600d005d JF |
748 | } |
749 | ||
2634b249 JF |
750 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
751 | NSString *context([alert context]); | |
600d005d | 752 | |
2634b249 JF |
753 | if ([context isEqualToString:@"sensitive"]) { |
754 | switch (button) { | |
755 | case 1: | |
756 | sensitive_ = [NSNumber numberWithBool:YES]; | |
757 | break; | |
600d005d | 758 | |
2634b249 JF |
759 | case 2: |
760 | sensitive_ = [NSNumber numberWithBool:NO]; | |
761 | break; | |
762 | } | |
600d005d | 763 | |
2634b249 JF |
764 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
765 | } else if ([context isEqualToString:@"challenge"]) { | |
766 | id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]); | |
caa427d1 | 767 | |
2634b249 JF |
768 | switch (button) { |
769 | case 1: { | |
770 | NSString *username([[alert textFieldAtIndex:0] text]); | |
771 | NSString *password([[alert textFieldAtIndex:1] text]); | |
600d005d | 772 | |
2634b249 | 773 | NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]); |
600d005d | 774 | |
2634b249 JF |
775 | [sender useCredential:credential forAuthenticationChallenge:challenge_]; |
776 | } break; | |
600d005d | 777 | |
2634b249 JF |
778 | case 2: |
779 | [sender cancelAuthenticationChallenge:challenge_]; | |
780 | break; | |
600d005d | 781 | |
2634b249 JF |
782 | _nodefault |
783 | } | |
600d005d | 784 | |
2634b249 JF |
785 | [challenge_ release]; |
786 | challenge_ = nil; | |
600d005d | 787 | |
2634b249 JF |
788 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
789 | } else if ([context isEqualToString:@"submit"]) { | |
790 | switch (button) { | |
791 | case 1: | |
792 | break; | |
600d005d | 793 | |
2634b249 JF |
794 | case 2: |
795 | if (request_ != nil) { | |
796 | WebThreadLock(); | |
797 | [webview_ loadRequest:request_]; | |
798 | WebThreadUnlock(); | |
799 | } | |
800 | break; | |
600d005d | 801 | |
2634b249 JF |
802 | _nodefault |
803 | } | |
600d005d | 804 | |
2634b249 JF |
805 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
806 | } | |
600d005d JF |
807 | } |
808 | ||
2634b249 JF |
809 | - (UIBarButtonItemStyle) rightButtonStyle { |
810 | if (style_ == nil) normal: | |
811 | return UIBarButtonItemStylePlain; | |
812 | else if ([style_ isEqualToString:@"Normal"]) | |
813 | return UIBarButtonItemStylePlain; | |
814 | else if ([style_ isEqualToString:@"Highlighted"]) | |
815 | return UIBarButtonItemStyleDone; | |
816 | else goto normal; | |
600d005d JF |
817 | } |
818 | ||
2634b249 JF |
819 | - (UIBarButtonItem *) customButton { |
820 | return [[[UIBarButtonItem alloc] | |
821 | initWithTitle:custom_ | |
822 | style:[self rightButtonStyle] | |
823 | target:self | |
824 | action:@selector(customButtonClicked) | |
825 | ] autorelease]; | |
caa427d1 JF |
826 | } |
827 | ||
2634b249 JF |
828 | - (UIBarButtonItem *) rightButton { |
829 | return reloaditem_; | |
eb35c522 JF |
830 | } |
831 | ||
2634b249 JF |
832 | - (void) applyLoadingTitle { |
833 | [[self navigationItem] setTitle:UCLocalize("LOADING")]; | |
caa427d1 JF |
834 | } |
835 | ||
2634b249 JF |
836 | - (void) applyRightButton { |
837 | if ([self isLoading]) { | |
838 | [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES]; | |
839 | // XXX: why do we do this again here? | |
840 | [[loadingitem_ view] addSubview:indicator_]; | |
841 | [self applyLoadingTitle]; | |
842 | } else if (custom_ != nil) { | |
843 | [[self navigationItem] setRightBarButtonItem:[self customButton] animated:YES]; | |
844 | } else { | |
845 | [[self navigationItem] setRightBarButtonItem:[self rightButton] animated:YES]; | |
846 | } | |
caa427d1 JF |
847 | } |
848 | ||
2634b249 JF |
849 | - (void) _didStartLoading { |
850 | [self applyRightButton]; | |
caa427d1 JF |
851 | } |
852 | ||
2634b249 JF |
853 | - (void) _didFinishLoading { |
854 | if ([loading_ count] != 0) | |
855 | return; | |
856 | ||
857 | [self applyRightButton]; | |
858 | ||
859 | // XXX: wtf? | |
860 | if (![self isLoading]) | |
861 | [[self navigationItem] setTitle:title_]; | |
caa427d1 JF |
862 | } |
863 | ||
2634b249 JF |
864 | - (bool) isLoading { |
865 | return [loading_ count] != 0; | |
caa427d1 JF |
866 | } |
867 | ||
b5e7eebb GP |
868 | - (id) initWithWidth:(float)width ofClass:(Class)_class { |
869 | if ((self = [super init]) != nil) { | |
245cce8a | 870 | class_ = _class; |
2634b249 | 871 | loading_ = [[NSMutableSet alloc] initWithCapacity:5]; |
bc11cf5b | 872 | |
2634b249 | 873 | indirect_ = [[IndirectDelegate alloc] initWithDelegate:self]; |
caa427d1 | 874 | |
2634b249 JF |
875 | webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease]; |
876 | [webview_ setDelegate:self]; | |
877 | [self setView:webview_]; | |
22f8bed9 | 878 | |
2634b249 JF |
879 | if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)]) |
880 | [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic]; | |
881 | else | |
882 | [webview_ setDetectsPhoneNumbers:NO]; | |
22f8bed9 | 883 | |
2634b249 | 884 | [webview_ setScalesPageToFit:YES]; |
22f8bed9 | 885 | |
2634b249 | 886 | UIWebDocumentView *document([webview_ _documentView]); |
ea173384 | 887 | |
2634b249 JF |
888 | // XXX: I think this improves scrolling; the hardcoded-ness sucks |
889 | [document setTileSize:CGSizeMake(320, 500)]; | |
ea173384 | 890 | |
2634b249 JF |
891 | [document setBackgroundColor:[UIColor clearColor]]; |
892 | [document setDrawsBackground:NO]; | |
ea173384 | 893 | |
2634b249 JF |
894 | WebView *webview([document webView]); |
895 | WebPreferences *preferences([webview preferences]); | |
22f8bed9 | 896 | |
2634b249 JF |
897 | // XXX: I have no clue if I actually /want/ this modification |
898 | if ([webview respondsToSelector:@selector(_setLayoutInterval:)]) | |
899 | [webview _setLayoutInterval:0]; | |
900 | else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)]) | |
901 | [preferences _setLayoutInterval:0]; | |
22f8bed9 | 902 | |
2634b249 JF |
903 | [preferences setCacheModel:WebCacheModelDocumentBrowser]; |
904 | [preferences setOfflineWebApplicationCacheEnabled:YES]; | |
22f8bed9 | 905 | |
2634b249 JF |
906 | if ([webview_ respondsToSelector:@selector(_scrollView)]) { |
907 | scroller_ = [webview_ _scrollView]; | |
22f8bed9 | 908 | |
2634b249 | 909 | [scroller_ setDirectionalLockEnabled:YES]; |
9c1605e2 | 910 | [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal]; |
2634b249 | 911 | [scroller_ setDelaysContentTouches:NO]; |
22f8bed9 | 912 | |
2634b249 JF |
913 | [scroller_ setCanCancelContentTouches:YES]; |
914 | } else if ([webview_ respondsToSelector:@selector(_scroller)]) { | |
915 | UIScroller *scroller([webview_ _scroller]); | |
916 | scroller_ = (UIScrollView *) scroller; | |
22f8bed9 | 917 | |
2634b249 | 918 | [scroller setDirectionalScrolling:YES]; |
9c1605e2 JF |
919 | // XXX: we might be better off /not/ setting this on older systems |
920 | [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */ | |
2634b249 | 921 | [scroller setScrollHysteresis:0]; /* 8 */ |
3e9c9e85 | 922 | |
2634b249 | 923 | [scroller setThumbDetectionEnabled:NO]; |
ea173384 | 924 | |
2634b249 JF |
925 | // use NO with UIApplicationUseLegacyEvents(YES) |
926 | [scroller setEventMode:YES]; | |
ea173384 | 927 | |
2634b249 JF |
928 | // XXX: this is handled by setBounces, right? |
929 | //[scroller setAllowsRubberBanding:YES]; | |
22f8bed9 | 930 | } |
bc11cf5b | 931 | |
2634b249 JF |
932 | [scroller_ setFixedBackgroundPattern:YES]; |
933 | [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; | |
934 | [scroller_ setClipsSubviews:YES]; | |
caa427d1 | 935 | |
2634b249 JF |
936 | [scroller_ setBounces:YES]; |
937 | [scroller_ setScrollingEnabled:YES]; | |
938 | [scroller_ setShowBackgroundShadow:NO]; | |
caa427d1 | 939 | |
2634b249 | 940 | [self setViewportWidth:width]; |
bc11cf5b JF |
941 | |
942 | reloaditem_ = [[UIBarButtonItem alloc] | |
943 | initWithTitle:UCLocalize("RELOAD") | |
944 | style:[self rightButtonStyle] | |
945 | target:self | |
946 | action:@selector(reloadButtonClicked) | |
947 | ]; | |
948 | ||
949 | loadingitem_ = [[UIBarButtonItem alloc] | |
950 | initWithTitle:@" " | |
951 | style:UIBarButtonItemStylePlain | |
952 | target:self | |
953 | action:@selector(reloadButtonClicked) | |
954 | ]; | |
2634b249 JF |
955 | |
956 | CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]; | |
957 | indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(15, 5, indsize.width, indsize.height)]; | |
958 | [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite]; | |
959 | [indicator_ startAnimation]; | |
bc11cf5b | 960 | [[loadingitem_ view] addSubview:indicator_]; |
22f8bed9 | 961 | |
2634b249 | 962 | [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; |
04fe1349 | 963 | [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
22f8bed9 JF |
964 | } return self; |
965 | } | |
966 | ||
b5e7eebb GP |
967 | - (id) initWithWidth:(float)width { |
968 | return [self initWithWidth:width ofClass:[self class]]; | |
245cce8a JF |
969 | } |
970 | ||
b5e7eebb GP |
971 | - (id) init { |
972 | return [self initWithWidth:0]; | |
3e9c9e85 JF |
973 | } |
974 | ||
2634b249 JF |
975 | - (void) didDismissModalViewController { |
976 | if (closer_ != nil) | |
977 | [self callFunction:closer_]; | |
22f8bed9 JF |
978 | } |
979 | ||
12b59862 | 980 | - (void) callFunction:(WebScriptObject *)function { |
caa427d1 JF |
981 | WebThreadLock(); |
982 | ||
2634b249 | 983 | WebView *webview([[webview_ _documentView] webView]); |
12b59862 | 984 | WebFrame *frame([webview mainFrame]); |
c21004b9 JF |
985 | WebPreferences *preferences([webview preferences]); |
986 | ||
987 | bool maybe([preferences javaScriptCanOpenWindowsAutomatically]); | |
988 | [preferences setJavaScriptCanOpenWindowsAutomatically:NO]; | |
22f8bed9 | 989 | |
c21004b9 | 990 | /*id _private(MSHookIvar<id>(webview, "_private")); |
12b59862 JF |
991 | WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page")); |
992 | WebCore::Settings *settings(page == NULL ? NULL : page->settings()); | |
22f8bed9 | 993 | |
12b59862 JF |
994 | bool no; |
995 | if (settings == NULL) | |
996 | no = 0; | |
997 | else { | |
998 | no = settings->JavaScriptCanOpenWindowsAutomatically(); | |
999 | settings->setJavaScriptCanOpenWindowsAutomatically(true); | |
c21004b9 | 1000 | }*/ |
12b59862 | 1001 | |
b5e7eebb | 1002 | if (UIWindow *window = [[self view] window]) |
43f3d7f6 JF |
1003 | if (UIResponder *responder = [window firstResponder]) |
1004 | [responder resignFirstResponder]; | |
1005 | ||
12b59862 JF |
1006 | JSObjectRef object([function JSObject]); |
1007 | JSGlobalContextRef context([frame globalContext]); | |
1008 | JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL); | |
1009 | ||
c21004b9 JF |
1010 | /*if (settings != NULL) |
1011 | settings->setJavaScriptCanOpenWindowsAutomatically(no);*/ | |
1012 | ||
1013 | [preferences setJavaScriptCanOpenWindowsAutomatically:maybe]; | |
caa427d1 JF |
1014 | |
1015 | WebThreadUnlock(); | |
12b59862 JF |
1016 | } |
1017 | ||
ce041f4f | 1018 | - (void) reloadButtonClicked { |
12b59862 JF |
1019 | [self reloadURL]; |
1020 | } | |
1021 | ||
719d6c2f DH |
1022 | - (void) _customButtonClicked { |
1023 | [self reloadButtonClicked]; | |
1024 | } | |
1025 | ||
ce041f4f | 1026 | - (void) customButtonClicked { |
caa427d1 JF |
1027 | #if !AlwaysReload |
1028 | if (function_ != nil) | |
12b59862 | 1029 | [self callFunction:function_]; |
caa427d1 JF |
1030 | else |
1031 | #endif | |
719d6c2f | 1032 | [self _customButtonClicked]; |
22f8bed9 JF |
1033 | } |
1034 | ||
3e9c9e85 JF |
1035 | + (float) defaultWidth { |
1036 | return 980; | |
1037 | } | |
1038 | ||
a5938ea5 | 1039 | @end |