]> git.saurik.com Git - cydia.git/blame - CyteKit/WebView.mm
I normally use tangelo-3.7: let's agree for now.
[cydia.git] / CyteKit / WebView.mm
CommitLineData
f172aa8f
JF
1/* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2011 Jay Freeman (saurik)
3*/
4
5/* Modified BSD License {{{ */
6/*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37*/
38/* }}} */
39
d458596e 40#include "CyteKit/WebView.h"
4bcafc01 41#include "CyteKit/WebThreadLocked.hpp"
aa35fd7b 42
435cb857
JF
43#include <CydiaSubstrate/CydiaSubstrate.h>
44
aa35fd7b 45#include "iPhonePrivate.h"
aa35fd7b
JF
46
47// CYWebPolicyDecision* {{{
aa35fd7b
JF
48@interface CYWebPolicyDecisionMediator : NSObject <
49 WebPolicyDecisionListener
50> {
51 id<WebPolicyDecisionListener> listener_;
52 CYWebPolicyDecision decision_;
53}
54
55- (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
56
57- (CYWebPolicyDecision) decision;
58- (bool) decided;
59- (bool) decide;
60
61@end
62
63@implementation CYWebPolicyDecisionMediator
64
65- (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
66 if ((self = [super init]) != nil) {
67 listener_ = listener;
68 } return self;
69}
70
71- (CYWebPolicyDecision) decision {
72 return decision_;
73}
74
75- (bool) decided {
76 return decision_ != CYWebPolicyDecisionUnknown;
77}
78
79- (bool) decide {
80 switch (decision_) {
81 case CYWebPolicyDecisionUnknown:
82 default:
83 NSLog(@"CYWebPolicyDecisionUnknown");
84 return false;
85
86 case CYWebPolicyDecisionDownload: [listener_ download]; break;
87 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
88 case CYWebPolicyDecisionUse: [listener_ use]; break;
89 }
90
91 return true;
92}
93
94- (void) download {
95 decision_ = CYWebPolicyDecisionDownload;
96}
97
98- (void) ignore {
99 decision_ = CYWebPolicyDecisionIgnore;
100}
101
102- (void) use {
103 decision_ = CYWebPolicyDecisionUse;
104}
105
106@end
107// }}}
108
1ee69bb4 109@implementation CyteWebView : UIWebView
aa35fd7b
JF
110
111#if ShowInternals
112#include "CyteKit/UCInternal.h"
113#endif
114
115- (id) initWithFrame:(CGRect)frame {
116 if ((self = [super initWithFrame:frame]) != nil) {
117 } return self;
118}
119
120- (void) dealloc {
121 [super dealloc];
122}
123
1ee69bb4
JF
124- (id<CyteWebViewDelegate>) delegate {
125 return (id<CyteWebViewDelegate>) [super delegate];
aa35fd7b
JF
126}
127
128/*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
1ee69bb4 129 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
130 WebView *created(nil);
131 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
132 created = [delegate webView:view createWebViewWithRequest:request];
133 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
134 created = [super webView:view createWebViewWithRequest:request];
135 return created;
136}*/
137
138// webView:addMessageToConsole: (X.Xx) {{{
139static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
140 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
141 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
142 [uiWebView webView:view addMessageToConsole:message];
143}
144
145- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
1ee69bb4 146 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
147 if ([delegate respondsToSelector:@selector(webView:addMessageToConsole:)])
148 [delegate webView:view addMessageToConsole:message];
149 if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
150 [super webView:view addMessageToConsole:message];
151}
152// }}}
153// webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
154- (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
1ee69bb4 155 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
156 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
157 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
158 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
159 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
160 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
d323285e 161 [delegate webView:view didDecidePolicy:[mediator decision] forNavigationAction:action request:request frame:frame];
aa35fd7b
JF
162 [mediator decide];
163}
164// }}}
165// webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
166static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
167 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
168 if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
169 [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
170}
171
172- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
1ee69bb4 173 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
174 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
175 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
176 [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
177 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
178 [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
179 [mediator decide];
180}
181// }}}
182// webView:didClearWindowObject:forFrame: (3.2+) {{{
183static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
184 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
185 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
186 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
187}
188
189- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
1ee69bb4 190 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
191 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
192 [delegate webView:view didClearWindowObject:window forFrame:frame];
193 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
194 [super webView:view didClearWindowObject:window forFrame:frame];
195}
196// }}}
197// webView:didFailLoadWithError:forFrame: (2.0+) {{{
198- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1ee69bb4 199 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
200 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
201 [delegate webView:view didFailLoadWithError:error forFrame:frame];
202 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
203 [super webView:view didFailLoadWithError:error forFrame:frame];
204}
205// }}}
206// webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
207- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1ee69bb4 208 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
209 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
210 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
211 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
212 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
213}
214// }}}
215// webView:didFinishLoadForFrame: (2.0+) {{{
216- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
1ee69bb4 217 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
218 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
219 [delegate webView:view didFinishLoadForFrame:frame];
220 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
221 [super webView:view didFinishLoadForFrame:frame];
222}
223// }}}
224// webView:didReceiveTitle:forFrame: (3.2+) {{{
225static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
226 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
227 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
228 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
229}
230
231- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
1ee69bb4 232 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
233 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
234 [delegate webView:view didReceiveTitle:title forFrame:frame];
235 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
236 [super webView:view didReceiveTitle:title forFrame:frame];
237}
238// }}}
239// webView:didStartProvisionalLoadForFrame: (2.0+) {{{
240- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
1ee69bb4 241 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
242 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
243 [delegate webView:view didStartProvisionalLoadForFrame:frame];
244 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
245 [super webView:view didStartProvisionalLoadForFrame:frame];
246}
247// }}}
248// webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
249static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
250 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
251 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
252 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
253 return request;
254}
255
256- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
1ee69bb4 257 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
258 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
259 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
260 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
261 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
262 return request;
263}
264// }}}
265// webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
266- (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
eea75e8f 267 [[self retain] autorelease];
1ee69bb4 268 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
269 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
270 if (
271 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
272 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
273 )
274 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
275}
276// }}}
277// webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
278- (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
eea75e8f 279 [[self retain] autorelease];
1ee69bb4 280 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
281 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
282 if (
283 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
284 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
285 )
286 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
287 return NO;
288}
289// }}}
290// webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
291- (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
eea75e8f 292 [[self retain] autorelease];
1ee69bb4 293 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
294 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
295 if (
296 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
297 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
298 )
299 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
300 return nil;
301}
302// }}}
303// webViewClose: (3.2+) {{{
304static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
305 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
306 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
307 [uiWebView webViewClose:view];
308}
309
310- (void) webViewClose:(WebView *)view {
1ee69bb4 311 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
312 if ([delegate respondsToSelector:@selector(webViewClose:)])
313 [delegate webViewClose:view];
314 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
315 [super webViewClose:view];
316}
317// }}}
318
319- (void) _updateViewSettings {
320 [super _updateViewSettings];
321
1ee69bb4 322 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
323 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
324 [delegate webViewUpdateViewSettings:self];
325}
326
4bcafc01
JF
327- (void) dispatchEvent:(NSString *)event {
328 WebThreadLocked lock;
329
330 NSString *script([NSString stringWithFormat:@
331 "(function() {"
332 "var event = this.document.createEvent('Events');"
333 "event.initEvent('%@', false, false);"
334 "this.document.dispatchEvent(event);"
335 "})();"
336 , event]);
337
338 NSMutableArray *frames([NSMutableArray arrayWithObjects:
339 [[[self _documentView] webView] mainFrame]
340 , nil]);
341
342 while (WebFrame *frame = [frames lastObject]) {
343 WebScriptObject *object([frame windowObject]);
344 [object evaluateWebScript:script];
345 [frames removeLastObject];
346 [frames addObjectsFromArray:[frame childFrames]];
347 }
348}
349
a713fcd3
JF
350- (void) reloadFromOrigin {
351 [[[self _documentView] webView] reloadFromOrigin:nil];
352}
353
aa35fd7b
JF
354+ (void) initialize {
355 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
356 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
357 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
358 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
359 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
360 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
361 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
362 }
363}
364
365@end