]>
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:)]) | |
f97db630 JF |
321 | request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source]; |
322 | return request; | |
d49718a1 | 323 | } |
22f8bed9 | 324 | |
2634b249 | 325 | - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
d49718a1 JF |
326 | id<CYWebViewDelegate> delegate([self delegate]); |
327 | if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)]) | |
2634b249 | 328 | request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source]; |
d49718a1 JF |
329 | if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)]) |
330 | request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source]; | |
331 | return request; | |
2634b249 | 332 | } |
d49718a1 | 333 | // }}} |
427754d6 | 334 | // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{ |
2634b249 | 335 | - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
d49718a1 JF |
336 | id<CYWebViewDelegate> delegate([self delegate]); |
337 | if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)]) | |
338 | // XXX: check delegate | |
339 | if ([delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]) | |
2634b249 JF |
340 | [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]; |
341 | } | |
d49718a1 | 342 | // }}} |
427754d6 | 343 | // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{ |
2634b249 | 344 | - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
d49718a1 JF |
345 | id<CYWebViewDelegate> delegate([self delegate]); |
346 | if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)]) | |
347 | // XXX: check delegate | |
348 | if ([delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]) | |
2634b249 JF |
349 | return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]; |
350 | return NO; | |
351 | } | |
d49718a1 | 352 | // }}} |
427754d6 | 353 | // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{ |
2634b249 | 354 | - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame { |
d49718a1 JF |
355 | id<CYWebViewDelegate> delegate([self delegate]); |
356 | if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)]) | |
357 | // XXX: check delegate | |
358 | if ([delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]) | |
2634b249 JF |
359 | return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]; |
360 | return nil; | |
361 | } | |
d49718a1 | 362 | // }}} |
3a1ff609 | 363 | // webViewClose: (3.2+) {{{ |
d49718a1 JF |
364 | static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) { |
365 | UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView")); | |
366 | if ([uiWebView respondsToSelector:@selector(webViewClose:)]) | |
367 | [uiWebView webViewClose:view]; | |
368 | } | |
600d005d | 369 | |
2634b249 | 370 | - (void) webViewClose:(WebView *)view { |
d49718a1 JF |
371 | id<CYWebViewDelegate> delegate([self delegate]); |
372 | if ([delegate respondsToSelector:@selector(webViewClose:)]) | |
373 | [delegate webViewClose:view]; | |
374 | if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)]) | |
2634b249 JF |
375 | [super webViewClose:view]; |
376 | } | |
d49718a1 JF |
377 | // }}} |
378 | ||
7e37a676 JF |
379 | - (void) _updateViewSettings { |
380 | [super _updateViewSettings]; | |
381 | ||
382 | id<CYWebViewDelegate> delegate([self delegate]); | |
383 | if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)]) | |
384 | [delegate webViewUpdateViewSettings:self]; | |
385 | } | |
386 | ||
d49718a1 JF |
387 | + (void) initialize { |
388 | if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) { | |
389 | class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24"); | |
390 | class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16"); | |
391 | class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16"); | |
392 | class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24"); | |
393 | class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8"); | |
394 | } | |
395 | } | |
22f8bed9 | 396 | |
2634b249 | 397 | @end |
caa427d1 | 398 | |
2634b249 | 399 | #define ShowInternals 0 |
652006e7 | 400 | #define LogBrowser 0 |
22f8bed9 | 401 | |
2634b249 | 402 | #define lprintf(args...) fprintf(stderr, args) |
22f8bed9 | 403 | |
2634b249 JF |
404 | @implementation BrowserController |
405 | ||
406 | #if ShowInternals | |
407 | #include "UICaboodle/UCInternal.h" | |
22f8bed9 JF |
408 | #endif |
409 | ||
2634b249 JF |
410 | + (void) _initialize { |
411 | [WebPreferences _setInitialDefaultTextEncodingToSystemEncoding]; | |
9c1605e2 JF |
412 | |
413 | if (float *_UIScrollViewDecelerationRateNormal = reinterpret_cast<float *>(dlsym(RTLD_DEFAULT, "UIScrollViewDecelerationRateNormal"))) | |
414 | CYScrollViewDecelerationRateNormal = *_UIScrollViewDecelerationRateNormal; | |
415 | else // XXX: this actually might be fast on some older systems: we should look into this | |
416 | CYScrollViewDecelerationRateNormal = 0.998; | |
2634b249 JF |
417 | } |
418 | ||
419 | - (void) dealloc { | |
420 | #if LogBrowser | |
421 | NSLog(@"[BrowserController dealloc]"); | |
422 | #endif | |
423 | ||
424 | [webview_ setDelegate:nil]; | |
425 | ||
426 | [indirect_ setDelegate:nil]; | |
22f8bed9 JF |
427 | [indirect_ release]; |
428 | ||
2634b249 JF |
429 | if (challenge_ != nil) |
430 | [challenge_ release]; | |
caa427d1 | 431 | |
3931b718 JF |
432 | if (request_ != nil) |
433 | [request_ release]; | |
434 | ||
2634b249 | 435 | //NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; |
22f8bed9 | 436 | |
2634b249 JF |
437 | if (custom_ != nil) |
438 | [custom_ release]; | |
22f8bed9 JF |
439 | if (style_ != nil) |
440 | [style_ release]; | |
2634b249 | 441 | |
22f8bed9 JF |
442 | if (function_ != nil) |
443 | [function_ release]; | |
12b59862 JF |
444 | if (closer_ != nil) |
445 | [closer_ release]; | |
22f8bed9 | 446 | |
22f8bed9 JF |
447 | if (title_ != nil) |
448 | [title_ release]; | |
2634b249 | 449 | |
3931b718 JF |
450 | [loading_ release]; |
451 | ||
2634b249 JF |
452 | [reloaditem_ release]; |
453 | [loadingitem_ release]; | |
454 | ||
455 | [indicator_ release]; | |
bc11cf5b | 456 | |
22f8bed9 JF |
457 | [super dealloc]; |
458 | } | |
459 | ||
460 | - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy { | |
461 | [self loadRequest:[NSURLRequest | |
462 | requestWithURL:url | |
463 | cachePolicy:policy | |
77801ff1 | 464 | timeoutInterval:120.0 |
22f8bed9 JF |
465 | ]]; |
466 | } | |
467 | ||
468 | - (void) loadURL:(NSURL *)url { | |
469 | [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]; | |
470 | } | |
471 | ||
22f8bed9 | 472 | - (void) loadRequest:(NSURLRequest *)request { |
0352f238 JF |
473 | #if LogBrowser |
474 | NSLog(@"loadRequest:%@", request); | |
475 | #endif | |
476 | ||
fe468f45 | 477 | error_ = false; |
caa427d1 JF |
478 | |
479 | WebThreadLock(); | |
2634b249 | 480 | [webview_ loadRequest:request]; |
caa427d1 | 481 | WebThreadUnlock(); |
22f8bed9 JF |
482 | } |
483 | ||
484 | - (void) reloadURL { | |
22f8bed9 JF |
485 | if (request_ == nil) |
486 | return; | |
487 | ||
488 | if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil) | |
fe468f45 | 489 | [self loadRequest:request_]; |
22f8bed9 | 490 | else { |
79ed082a | 491 | UIAlertView *alert = [[[UIAlertView alloc] |
43f3d7f6 | 492 | initWithTitle:UCLocalize("RESUBMIT_FORM") |
79ed082a | 493 | message:nil |
22f8bed9 | 494 | delegate:self |
79ed082a GP |
495 | cancelButtonTitle:UCLocalize("CANCEL") |
496 | otherButtonTitles:UCLocalize("SUBMIT"), nil | |
22f8bed9 | 497 | ] autorelease]; |
2634b249 | 498 | |
79ed082a GP |
499 | [alert setContext:@"submit"]; |
500 | [alert show]; | |
22f8bed9 JF |
501 | } |
502 | } | |
503 | ||
22f8bed9 | 504 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
2634b249 JF |
505 | if (custom_ != nil) |
506 | [custom_ autorelease]; | |
3931b718 JF |
507 | if (button == nil) |
508 | custom_ = nil; | |
509 | else | |
510 | custom_ = [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain]; | |
22f8bed9 JF |
511 | |
512 | if (style_ != nil) | |
513 | [style_ autorelease]; | |
3931b718 JF |
514 | if (style == nil) |
515 | style_ = nil; | |
516 | else | |
517 | style_ = [style retain]; | |
22f8bed9 JF |
518 | |
519 | if (function_ != nil) | |
520 | [function_ autorelease]; | |
3931b718 JF |
521 | if (function == nil) |
522 | function_ = nil; | |
523 | else | |
524 | function_ = [function retain]; | |
12b59862 | 525 | |
bc11cf5b | 526 | [self applyRightButton]; |
22f8bed9 JF |
527 | } |
528 | ||
529 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
2634b249 JF |
530 | if (custom_ != nil) |
531 | [custom_ autorelease]; | |
3931b718 JF |
532 | if (button == nil) |
533 | custom_ = nil; | |
534 | else | |
535 | custom_ = [button retain]; | |
22f8bed9 JF |
536 | |
537 | if (style_ != nil) | |
538 | [style_ autorelease]; | |
3931b718 JF |
539 | if (style == nil) |
540 | style_ = nil; | |
541 | else | |
542 | style_ = [style retain]; | |
22f8bed9 JF |
543 | |
544 | if (function_ != nil) | |
545 | [function_ autorelease]; | |
3931b718 JF |
546 | if (function == nil) |
547 | function_ = nil; | |
548 | else | |
549 | function_ = [function retain]; | |
12b59862 | 550 | |
bc11cf5b | 551 | [self applyRightButton]; |
22f8bed9 | 552 | } |
22f8bed9 | 553 | |
caa427d1 JF |
554 | - (void) setPopupHook:(id)function { |
555 | if (closer_ != nil) | |
556 | [closer_ autorelease]; | |
3931b718 JF |
557 | if (function == nil) |
558 | closer_ = nil; | |
559 | else | |
560 | closer_ = [function retain]; | |
caa427d1 JF |
561 | } |
562 | ||
7e37a676 JF |
563 | - (void) _setViewportWidth { |
564 | [[webview_ _documentView] setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; | |
565 | } | |
566 | ||
2634b249 JF |
567 | - (void) setViewportWidth:(float)width { |
568 | width_ = width != 0 ? width : [[self class] defaultWidth]; | |
7e37a676 JF |
569 | [self _setViewportWidth]; |
570 | } | |
571 | ||
572 | - (void) webViewUpdateViewSettings:(UIWebView *)view { | |
573 | [self _setViewportWidth]; | |
22f8bed9 JF |
574 | } |
575 | ||
2634b249 JF |
576 | - (void) _openMailToURL:(NSURL *)url { |
577 | [[UIApplication sharedApplication] openURL:url];// asPanel:YES]; | |
22f8bed9 JF |
578 | } |
579 | ||
2634b249 JF |
580 | - (bool) _allowJavaScriptPanel { |
581 | return true; | |
22f8bed9 JF |
582 | } |
583 | ||
2634b249 JF |
584 | - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame { |
585 | [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]]; | |
586 | [self _didFinishLoading]; | |
22f8bed9 | 587 | |
2634b249 | 588 | if ([error code] == NSURLErrorCancelled) |
22f8bed9 JF |
589 | return; |
590 | ||
caa427d1 | 591 | if ([frame parentFrame] == nil) { |
2634b249 JF |
592 | [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", |
593 | [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString], | |
594 | [[error localizedDescription] stringByAddingPercentEscapes] | |
595 | ]]]; | |
22f8bed9 | 596 | |
2634b249 JF |
597 | error_ = true; |
598 | } | |
599 | } | |
caa427d1 | 600 | |
2634b249 JF |
601 | // CYWebViewDelegate {{{ |
602 | - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
603 | #if LogBrowser | |
604 | NSLog(@"decidePolicyForNavigationAction:%@ request:%@ frame:%@", action, request, frame); | |
605 | #endif | |
600d005d | 606 | |
ee32e123 JF |
607 | if ([frame parentFrame] == nil) { |
608 | if (!error_) { | |
609 | if (request_ != nil) | |
610 | [request_ autorelease]; | |
611 | if (request == nil) | |
612 | request_ = nil; | |
613 | else | |
614 | request_ = [request retain]; | |
615 | } | |
caa427d1 | 616 | } |
22f8bed9 JF |
617 | } |
618 | ||
2634b249 JF |
619 | - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { |
620 | #if LogBrowser | |
621 | NSLog(@"decidePolicyForNewWindowAction:%@ request:%@ newFrameName:%@", action, request, frame); | |
622 | #endif | |
2e6c1426 | 623 | |
2634b249 JF |
624 | NSURL *url([request URL]); |
625 | if (url == nil) | |
626 | return; | |
bc11cf5b | 627 | |
2634b249 JF |
628 | if ([frame isEqualToString:@"_open"]) |
629 | [delegate_ openURL:url]; | |
2e6c1426 | 630 | |
2634b249 JF |
631 | NSString *scheme([[url scheme] lowercaseString]); |
632 | if ([scheme isEqualToString:@"mailto"]) | |
633 | [self _openMailToURL:url]; | |
a5938ea5 | 634 | |
2634b249 | 635 | CYViewController *page([delegate_ pageForURL:url hasTag:NULL]); |
2fad210a | 636 | |
2634b249 JF |
637 | if (page == nil) { |
638 | BrowserController *browser([[[class_ alloc] init] autorelease]); | |
639 | [browser loadRequest:request]; | |
640 | page = browser; | |
bc11cf5b | 641 | } |
ce041f4f | 642 | |
2634b249 | 643 | [page setDelegate:delegate_]; |
b5e7eebb | 644 | |
2634b249 JF |
645 | if (![frame isEqualToString:@"_popup"]) { |
646 | [[self navigationItem] setTitle:title_]; | |
b5e7eebb | 647 | |
2634b249 JF |
648 | [[self navigationController] pushViewController:page animated:YES]; |
649 | } else { | |
650 | UCNavigationController *navigation([[[UCNavigationController alloc] init] autorelease]); | |
22f8bed9 | 651 | |
2634b249 JF |
652 | [navigation setHook:indirect_]; |
653 | [navigation setDelegate:delegate_]; | |
22f8bed9 | 654 | |
2634b249 JF |
655 | [navigation setViewControllers:[NSArray arrayWithObject:page]]; |
656 | ||
657 | [[page navigationItem] setLeftBarButtonItem:[[[UIBarButtonItem alloc] | |
658 | initWithTitle:UCLocalize("CLOSE") | |
659 | style:UIBarButtonItemStylePlain | |
660 | target:page | |
661 | action:@selector(close) | |
662 | ] autorelease]]; | |
663 | ||
664 | [[self navigationController] presentModalViewController:navigation animated:YES]; | |
665 | } | |
22f8bed9 | 666 | |
2634b249 | 667 | [listener ignore]; |
22f8bed9 JF |
668 | } |
669 | ||
2634b249 | 670 | - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
22f8bed9 JF |
671 | } |
672 | ||
2634b249 JF |
673 | - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { |
674 | #if LogBrowser | |
675 | NSLog(@"didFailLoadWithError:%@ forFrame:%@", error, frame); | |
676 | #endif | |
677 | ||
678 | [self _didFailWithError:error forFrame:frame]; | |
22f8bed9 JF |
679 | } |
680 | ||
2634b249 JF |
681 | - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { |
682 | #if LogBrowser | |
683 | NSLog(@"didFailProvisionalLoadWithError:%@ forFrame:%@", error, frame); | |
684 | #endif | |
685 | ||
686 | [self _didFailWithError:error forFrame:frame]; | |
22f8bed9 JF |
687 | } |
688 | ||
2634b249 | 689 | - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame { |
7592e053 | 690 | [loading_ removeObject:[NSValue valueWithNonretainedObject:frame]]; |
22f8bed9 | 691 | |
caa427d1 | 692 | if ([frame parentFrame] == nil) { |
22f8bed9 JF |
693 | if (DOMDocument *document = [frame DOMDocument]) |
694 | if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"]) | |
96f3833b | 695 | for (DOMHTMLBodyElement *body in (id) bodies) { |
22f8bed9 JF |
696 | DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]); |
697 | ||
698 | bool colored(false); | |
699 | ||
700 | if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) { | |
fe468f45 JF |
701 | if ([color primitiveType] == DOM_CSS_RGBCOLOR) { |
702 | DOMRGBColor *rgb([color getRGBColorValue]); | |
703 | ||
704 | float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]); | |
705 | float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]); | |
706 | float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]); | |
707 | float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]); | |
708 | ||
709 | UIColor *uic(nil); | |
710 | ||
711 | if (red == 0xc7 && green == 0xce && blue == 0xd5) | |
4f6056df | 712 | uic = [UIColor groupTableViewBackgroundColor]; |
fe468f45 JF |
713 | else if (alpha != 0) |
714 | uic = [UIColor | |
715 | colorWithRed:(red / 255) | |
716 | green:(green / 255) | |
717 | blue:(blue / 255) | |
718 | alpha:alpha | |
719 | ]; | |
720 | ||
721 | if (uic != nil) { | |
722 | colored = true; | |
723 | [scroller_ setBackgroundColor:uic]; | |
724 | } | |
22f8bed9 JF |
725 | } |
726 | } | |
727 | ||
728 | if (!colored) | |
4f6056df | 729 | [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; |
22f8bed9 JF |
730 | break; |
731 | } | |
732 | } | |
733 | ||
2634b249 | 734 | [self _didFinishLoading]; |
22f8bed9 JF |
735 | } |
736 | ||
2634b249 JF |
737 | - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { |
738 | if ([frame parentFrame] != nil) | |
739 | return; | |
caa427d1 | 740 | |
3931b718 JF |
741 | if (title_ != nil) |
742 | [title_ autorelease]; | |
2634b249 | 743 | title_ = [title retain]; |
3931b718 | 744 | |
2634b249 JF |
745 | [[self navigationItem] setTitle:title_]; |
746 | } | |
22f8bed9 | 747 | |
2634b249 JF |
748 | - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame { |
749 | [loading_ addObject:[NSValue valueWithNonretainedObject:frame]]; | |
7592e053 | 750 | |
caa427d1 | 751 | if ([frame parentFrame] == nil) { |
2634b249 JF |
752 | CYRelease(title_); |
753 | CYRelease(custom_); | |
754 | CYRelease(style_); | |
755 | CYRelease(function_); | |
756 | CYRelease(closer_); | |
757 | ||
758 | // XXX: do we still need to do this? | |
759 | [[self navigationItem] setTitle:nil]; | |
caa427d1 | 760 | } |
fe468f45 | 761 | |
2634b249 | 762 | [self _didStartLoading]; |
caa427d1 JF |
763 | } |
764 | ||
2634b249 | 765 | - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source { |
0352f238 JF |
766 | #if LogBrowser |
767 | NSLog(@"resource:%@ willSendRequest:%@ redirectResponse:%@ fromDataSource:%@", identifier, request, response, source); | |
768 | #endif | |
769 | ||
2634b249 | 770 | return request; |
22f8bed9 JF |
771 | } |
772 | ||
2634b249 JF |
773 | - (bool) webView:(WebView *)view shouldRunJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
774 | return [self _allowJavaScriptPanel]; | |
22f8bed9 JF |
775 | } |
776 | ||
2634b249 JF |
777 | - (bool) webView:(WebView *)view shouldRunJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
778 | return [self _allowJavaScriptPanel]; | |
600d005d JF |
779 | } |
780 | ||
2634b249 JF |
781 | - (bool) webView:(WebView *)view shouldRunJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame { |
782 | return [self _allowJavaScriptPanel]; | |
600d005d JF |
783 | } |
784 | ||
2634b249 JF |
785 | - (void) webViewClose:(WebView *)view { |
786 | [self close]; | |
600d005d | 787 | } |
2634b249 | 788 | // }}} |
600d005d | 789 | |
2634b249 JF |
790 | - (void) close { |
791 | [[self navigationController] dismissModalViewControllerAnimated:YES]; | |
600d005d JF |
792 | } |
793 | ||
2634b249 JF |
794 | - (void) alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)button { |
795 | NSString *context([alert context]); | |
600d005d | 796 | |
2634b249 JF |
797 | if ([context isEqualToString:@"sensitive"]) { |
798 | switch (button) { | |
799 | case 1: | |
800 | sensitive_ = [NSNumber numberWithBool:YES]; | |
801 | break; | |
600d005d | 802 | |
2634b249 JF |
803 | case 2: |
804 | sensitive_ = [NSNumber numberWithBool:NO]; | |
805 | break; | |
806 | } | |
600d005d | 807 | |
2634b249 JF |
808 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
809 | } else if ([context isEqualToString:@"challenge"]) { | |
810 | id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]); | |
caa427d1 | 811 | |
2634b249 JF |
812 | switch (button) { |
813 | case 1: { | |
814 | NSString *username([[alert textFieldAtIndex:0] text]); | |
815 | NSString *password([[alert textFieldAtIndex:1] text]); | |
600d005d | 816 | |
2634b249 | 817 | NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]); |
600d005d | 818 | |
2634b249 JF |
819 | [sender useCredential:credential forAuthenticationChallenge:challenge_]; |
820 | } break; | |
600d005d | 821 | |
2634b249 JF |
822 | case 2: |
823 | [sender cancelAuthenticationChallenge:challenge_]; | |
824 | break; | |
600d005d | 825 | |
2634b249 JF |
826 | _nodefault |
827 | } | |
600d005d | 828 | |
2634b249 JF |
829 | [challenge_ release]; |
830 | challenge_ = nil; | |
600d005d | 831 | |
2634b249 JF |
832 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
833 | } else if ([context isEqualToString:@"submit"]) { | |
834 | switch (button) { | |
835 | case 1: | |
836 | break; | |
600d005d | 837 | |
2634b249 JF |
838 | case 2: |
839 | if (request_ != nil) { | |
840 | WebThreadLock(); | |
841 | [webview_ loadRequest:request_]; | |
842 | WebThreadUnlock(); | |
843 | } | |
844 | break; | |
600d005d | 845 | |
2634b249 JF |
846 | _nodefault |
847 | } | |
600d005d | 848 | |
2634b249 JF |
849 | [alert dismissWithClickedButtonIndex:-1 animated:YES]; |
850 | } | |
600d005d JF |
851 | } |
852 | ||
2634b249 JF |
853 | - (UIBarButtonItemStyle) rightButtonStyle { |
854 | if (style_ == nil) normal: | |
855 | return UIBarButtonItemStylePlain; | |
856 | else if ([style_ isEqualToString:@"Normal"]) | |
857 | return UIBarButtonItemStylePlain; | |
858 | else if ([style_ isEqualToString:@"Highlighted"]) | |
859 | return UIBarButtonItemStyleDone; | |
860 | else goto normal; | |
600d005d JF |
861 | } |
862 | ||
2634b249 JF |
863 | - (UIBarButtonItem *) customButton { |
864 | return [[[UIBarButtonItem alloc] | |
865 | initWithTitle:custom_ | |
866 | style:[self rightButtonStyle] | |
867 | target:self | |
868 | action:@selector(customButtonClicked) | |
869 | ] autorelease]; | |
caa427d1 JF |
870 | } |
871 | ||
2634b249 JF |
872 | - (UIBarButtonItem *) rightButton { |
873 | return reloaditem_; | |
eb35c522 JF |
874 | } |
875 | ||
2634b249 JF |
876 | - (void) applyLoadingTitle { |
877 | [[self navigationItem] setTitle:UCLocalize("LOADING")]; | |
caa427d1 JF |
878 | } |
879 | ||
2634b249 JF |
880 | - (void) applyRightButton { |
881 | if ([self isLoading]) { | |
882 | [[self navigationItem] setRightBarButtonItem:loadingitem_ animated:YES]; | |
3931b718 | 883 | // XXX: why do we do this again here? (if we don't, just remove indicator_) |
2634b249 JF |
884 | [[loadingitem_ view] addSubview:indicator_]; |
885 | [self applyLoadingTitle]; | |
886 | } else if (custom_ != nil) { | |
887 | [[self navigationItem] setRightBarButtonItem:[self customButton] animated:YES]; | |
888 | } else { | |
889 | [[self navigationItem] setRightBarButtonItem:[self rightButton] animated:YES]; | |
890 | } | |
caa427d1 JF |
891 | } |
892 | ||
2634b249 JF |
893 | - (void) _didStartLoading { |
894 | [self applyRightButton]; | |
caa427d1 JF |
895 | } |
896 | ||
2634b249 JF |
897 | - (void) _didFinishLoading { |
898 | if ([loading_ count] != 0) | |
899 | return; | |
900 | ||
901 | [self applyRightButton]; | |
902 | ||
903 | // XXX: wtf? | |
904 | if (![self isLoading]) | |
905 | [[self navigationItem] setTitle:title_]; | |
caa427d1 JF |
906 | } |
907 | ||
2634b249 JF |
908 | - (bool) isLoading { |
909 | return [loading_ count] != 0; | |
caa427d1 JF |
910 | } |
911 | ||
b5e7eebb GP |
912 | - (id) initWithWidth:(float)width ofClass:(Class)_class { |
913 | if ((self = [super init]) != nil) { | |
245cce8a | 914 | class_ = _class; |
2634b249 | 915 | loading_ = [[NSMutableSet alloc] initWithCapacity:5]; |
bc11cf5b | 916 | |
2634b249 | 917 | indirect_ = [[IndirectDelegate alloc] initWithDelegate:self]; |
caa427d1 | 918 | |
2634b249 JF |
919 | webview_ = [[[CYWebView alloc] initWithFrame:[[self view] bounds]] autorelease]; |
920 | [webview_ setDelegate:self]; | |
921 | [self setView:webview_]; | |
22f8bed9 | 922 | |
2634b249 JF |
923 | if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)]) |
924 | [webview_ setDataDetectorTypes:UIDataDetectorTypeAutomatic]; | |
925 | else | |
926 | [webview_ setDetectsPhoneNumbers:NO]; | |
22f8bed9 | 927 | |
2634b249 | 928 | [webview_ setScalesPageToFit:YES]; |
22f8bed9 | 929 | |
2634b249 | 930 | UIWebDocumentView *document([webview_ _documentView]); |
ea173384 | 931 | |
2634b249 JF |
932 | // XXX: I think this improves scrolling; the hardcoded-ness sucks |
933 | [document setTileSize:CGSizeMake(320, 500)]; | |
ea173384 | 934 | |
2634b249 | 935 | [document setBackgroundColor:[UIColor clearColor]]; |
14f17703 JF |
936 | |
937 | // XXX: this is terribly (too?) expensive | |
2634b249 | 938 | [document setDrawsBackground:NO]; |
ea173384 | 939 | |
2634b249 JF |
940 | WebView *webview([document webView]); |
941 | WebPreferences *preferences([webview preferences]); | |
22f8bed9 | 942 | |
2634b249 JF |
943 | // XXX: I have no clue if I actually /want/ this modification |
944 | if ([webview respondsToSelector:@selector(_setLayoutInterval:)]) | |
945 | [webview _setLayoutInterval:0]; | |
946 | else if ([preferences respondsToSelector:@selector(_setLayoutInterval:)]) | |
947 | [preferences _setLayoutInterval:0]; | |
22f8bed9 | 948 | |
2634b249 JF |
949 | [preferences setCacheModel:WebCacheModelDocumentBrowser]; |
950 | [preferences setOfflineWebApplicationCacheEnabled:YES]; | |
22f8bed9 | 951 | |
2634b249 JF |
952 | if ([webview_ respondsToSelector:@selector(_scrollView)]) { |
953 | scroller_ = [webview_ _scrollView]; | |
22f8bed9 | 954 | |
2634b249 | 955 | [scroller_ setDirectionalLockEnabled:YES]; |
9c1605e2 | 956 | [scroller_ setDecelerationRate:CYScrollViewDecelerationRateNormal]; |
2634b249 | 957 | [scroller_ setDelaysContentTouches:NO]; |
22f8bed9 | 958 | |
2634b249 JF |
959 | [scroller_ setCanCancelContentTouches:YES]; |
960 | } else if ([webview_ respondsToSelector:@selector(_scroller)]) { | |
961 | UIScroller *scroller([webview_ _scroller]); | |
962 | scroller_ = (UIScrollView *) scroller; | |
22f8bed9 | 963 | |
2634b249 | 964 | [scroller setDirectionalScrolling:YES]; |
9c1605e2 JF |
965 | // XXX: we might be better off /not/ setting this on older systems |
966 | [scroller setScrollDecelerationFactor:CYScrollViewDecelerationRateNormal]; /* 0.989324 */ | |
2634b249 | 967 | [scroller setScrollHysteresis:0]; /* 8 */ |
3e9c9e85 | 968 | |
2634b249 | 969 | [scroller setThumbDetectionEnabled:NO]; |
ea173384 | 970 | |
2634b249 JF |
971 | // use NO with UIApplicationUseLegacyEvents(YES) |
972 | [scroller setEventMode:YES]; | |
ea173384 | 973 | |
2634b249 JF |
974 | // XXX: this is handled by setBounces, right? |
975 | //[scroller setAllowsRubberBanding:YES]; | |
22f8bed9 | 976 | } |
bc11cf5b | 977 | |
2634b249 JF |
978 | [scroller_ setFixedBackgroundPattern:YES]; |
979 | [scroller_ setBackgroundColor:[UIColor groupTableViewBackgroundColor]]; | |
980 | [scroller_ setClipsSubviews:YES]; | |
caa427d1 | 981 | |
2634b249 JF |
982 | [scroller_ setBounces:YES]; |
983 | [scroller_ setScrollingEnabled:YES]; | |
984 | [scroller_ setShowBackgroundShadow:NO]; | |
caa427d1 | 985 | |
2634b249 | 986 | [self setViewportWidth:width]; |
bc11cf5b JF |
987 | |
988 | reloaditem_ = [[UIBarButtonItem alloc] | |
989 | initWithTitle:UCLocalize("RELOAD") | |
990 | style:[self rightButtonStyle] | |
991 | target:self | |
992 | action:@selector(reloadButtonClicked) | |
993 | ]; | |
994 | ||
995 | loadingitem_ = [[UIBarButtonItem alloc] | |
996 | initWithTitle:@" " | |
997 | style:UIBarButtonItemStylePlain | |
998 | target:self | |
999 | action:@selector(reloadButtonClicked) | |
1000 | ]; | |
2634b249 JF |
1001 | |
1002 | CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]; | |
1003 | indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(15, 5, indsize.width, indsize.height)]; | |
1004 | [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite]; | |
1005 | [indicator_ startAnimation]; | |
bc11cf5b | 1006 | [[loadingitem_ view] addSubview:indicator_]; |
22f8bed9 | 1007 | |
2634b249 | 1008 | [webview_ setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)]; |
04fe1349 | 1009 | [indicator_ setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin]; |
22f8bed9 JF |
1010 | } return self; |
1011 | } | |
1012 | ||
b5e7eebb GP |
1013 | - (id) initWithWidth:(float)width { |
1014 | return [self initWithWidth:width ofClass:[self class]]; | |
245cce8a JF |
1015 | } |
1016 | ||
b5e7eebb GP |
1017 | - (id) init { |
1018 | return [self initWithWidth:0]; | |
3e9c9e85 JF |
1019 | } |
1020 | ||
2634b249 JF |
1021 | - (void) didDismissModalViewController { |
1022 | if (closer_ != nil) | |
1023 | [self callFunction:closer_]; | |
22f8bed9 JF |
1024 | } |
1025 | ||
12b59862 | 1026 | - (void) callFunction:(WebScriptObject *)function { |
caa427d1 JF |
1027 | WebThreadLock(); |
1028 | ||
2634b249 | 1029 | WebView *webview([[webview_ _documentView] webView]); |
12b59862 | 1030 | WebFrame *frame([webview mainFrame]); |
c21004b9 JF |
1031 | WebPreferences *preferences([webview preferences]); |
1032 | ||
1033 | bool maybe([preferences javaScriptCanOpenWindowsAutomatically]); | |
1034 | [preferences setJavaScriptCanOpenWindowsAutomatically:NO]; | |
22f8bed9 | 1035 | |
c21004b9 | 1036 | /*id _private(MSHookIvar<id>(webview, "_private")); |
12b59862 JF |
1037 | WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page")); |
1038 | WebCore::Settings *settings(page == NULL ? NULL : page->settings()); | |
22f8bed9 | 1039 | |
12b59862 JF |
1040 | bool no; |
1041 | if (settings == NULL) | |
1042 | no = 0; | |
1043 | else { | |
1044 | no = settings->JavaScriptCanOpenWindowsAutomatically(); | |
1045 | settings->setJavaScriptCanOpenWindowsAutomatically(true); | |
c21004b9 | 1046 | }*/ |
12b59862 | 1047 | |
b5e7eebb | 1048 | if (UIWindow *window = [[self view] window]) |
43f3d7f6 JF |
1049 | if (UIResponder *responder = [window firstResponder]) |
1050 | [responder resignFirstResponder]; | |
1051 | ||
12b59862 JF |
1052 | JSObjectRef object([function JSObject]); |
1053 | JSGlobalContextRef context([frame globalContext]); | |
1054 | JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL); | |
1055 | ||
c21004b9 JF |
1056 | /*if (settings != NULL) |
1057 | settings->setJavaScriptCanOpenWindowsAutomatically(no);*/ | |
1058 | ||
1059 | [preferences setJavaScriptCanOpenWindowsAutomatically:maybe]; | |
caa427d1 JF |
1060 | |
1061 | WebThreadUnlock(); | |
12b59862 JF |
1062 | } |
1063 | ||
ce041f4f | 1064 | - (void) reloadButtonClicked { |
12b59862 JF |
1065 | [self reloadURL]; |
1066 | } | |
1067 | ||
719d6c2f DH |
1068 | - (void) _customButtonClicked { |
1069 | [self reloadButtonClicked]; | |
1070 | } | |
1071 | ||
ce041f4f | 1072 | - (void) customButtonClicked { |
caa427d1 JF |
1073 | #if !AlwaysReload |
1074 | if (function_ != nil) | |
12b59862 | 1075 | [self callFunction:function_]; |
caa427d1 JF |
1076 | else |
1077 | #endif | |
719d6c2f | 1078 | [self _customButtonClicked]; |
22f8bed9 JF |
1079 | } |
1080 | ||
3e9c9e85 JF |
1081 | + (float) defaultWidth { |
1082 | return 980; | |
1083 | } | |
1084 | ||
a5938ea5 | 1085 | @end |