1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2011 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
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
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.
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.
40 #include "CyteKit/WebView.h"
41 #include "CyteKit/WebThreadLocked.hpp"
43 #include <CydiaSubstrate/CydiaSubstrate.h>
45 #include "iPhonePrivate.h"
47 // CYWebPolicyDecision* {{{
48 @interface CYWebPolicyDecisionMediator : NSObject <
49 WebPolicyDecisionListener
51 id<WebPolicyDecisionListener> listener_;
52 CYWebPolicyDecision decision_;
55 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
57 - (CYWebPolicyDecision) decision;
63 @implementation CYWebPolicyDecisionMediator
65 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
66 if ((self = [super init]) != nil) {
71 - (CYWebPolicyDecision) decision {
76 return decision_ != CYWebPolicyDecisionUnknown;
81 case CYWebPolicyDecisionUnknown:
83 NSLog(@"CYWebPolicyDecisionUnknown");
86 case CYWebPolicyDecisionDownload: [listener_ download]; break;
87 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
88 case CYWebPolicyDecisionUse: [listener_ use]; break;
95 decision_ = CYWebPolicyDecisionDownload;
99 decision_ = CYWebPolicyDecisionIgnore;
103 decision_ = CYWebPolicyDecisionUse;
109 @implementation CyteWebView : UIWebView
112 #include "CyteKit/UCInternal.h"
115 - (id) initWithFrame:(CGRect)frame {
116 if ((self = [super initWithFrame:frame]) != nil) {
124 - (id<CyteWebViewDelegate>) delegate {
125 return (id<CyteWebViewDelegate>) [super delegate];
128 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
129 id<CyteWebViewDelegate> delegate([self delegate]);
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];
138 // webView:addMessageToConsole: (X.Xx) {{{
139 static 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];
145 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
146 id<CyteWebViewDelegate> delegate([self delegate]);
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];
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 {
155 id<CyteWebViewDelegate> delegate([self delegate]);
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];
161 [delegate webView:view didDecidePolicy:[mediator decision] forNavigationAction:action request:request frame:frame];
165 // webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
166 static 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];
172 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
173 id<CyteWebViewDelegate> delegate([self delegate]);
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];
182 // webView:didClearWindowObject:forFrame: (3.2+) {{{
183 static 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];
189 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
190 id<CyteWebViewDelegate> delegate([self delegate]);
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];
197 // webView:didFailLoadWithError:forFrame: (2.0+) {{{
198 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
199 id<CyteWebViewDelegate> delegate([self delegate]);
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];
206 // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
207 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
208 id<CyteWebViewDelegate> delegate([self delegate]);
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];
215 // webView:didFinishLoadForFrame: (2.0+) {{{
216 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
217 id<CyteWebViewDelegate> delegate([self delegate]);
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];
224 // webView:didReceiveTitle:forFrame: (3.2+) {{{
225 static 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];
231 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
232 id<CyteWebViewDelegate> delegate([self delegate]);
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];
239 // webView:didStartProvisionalLoadForFrame: (2.0+) {{{
240 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
241 id<CyteWebViewDelegate> delegate([self delegate]);
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];
248 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
249 static 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];
256 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
257 id<CyteWebViewDelegate> delegate([self delegate]);
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];
265 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
266 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
267 [[self retain] autorelease];
268 id<CyteWebViewDelegate> delegate([self delegate]);
269 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
271 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
272 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
274 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
277 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
278 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
279 [[self retain] autorelease];
280 id<CyteWebViewDelegate> delegate([self delegate]);
281 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
283 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
284 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
286 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
290 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
291 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
292 [[self retain] autorelease];
293 id<CyteWebViewDelegate> delegate([self delegate]);
294 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
296 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
297 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
299 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
303 // webViewClose: (3.2+) {{{
304 static 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];
310 - (void) webViewClose:(WebView *)view {
311 id<CyteWebViewDelegate> delegate([self delegate]);
312 if ([delegate respondsToSelector:@selector(webViewClose:)])
313 [delegate webViewClose:view];
314 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
315 [super webViewClose:view];
319 - (void) _updateViewSettings {
320 [super _updateViewSettings];
322 id<CyteWebViewDelegate> delegate([self delegate]);
323 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
324 [delegate webViewUpdateViewSettings:self];
327 - (void) dispatchEvent:(NSString *)event {
328 WebThreadLocked lock;
330 NSString *script([NSString stringWithFormat:@
332 "var event = this.document.createEvent('Events');"
333 "event.initEvent('%@', false, false);"
334 "this.document.dispatchEvent(event);"
338 NSMutableArray *frames([NSMutableArray arrayWithObjects:
339 [[[self _documentView] webView] mainFrame]
342 while (WebFrame *frame = [frames lastObject]) {
343 WebScriptObject *object([frame windowObject]);
344 [object evaluateWebScript:script];
345 [frames removeLastObject];
346 [frames addObjectsFromArray:[frame childFrames]];
350 - (void) reloadFromOrigin {
351 [[[self _documentView] webView] reloadFromOrigin:nil];
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");