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/dispatchEvent.h"
41 #include "CyteKit/WebView.h"
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 - (NSString *) description {
125 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[self request] URL] absoluteString]];
128 - (id<CyteWebViewDelegate>) delegate {
129 return (id<CyteWebViewDelegate>) [super delegate];
132 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
133 id<CyteWebViewDelegate> delegate([self delegate]);
134 WebView *created(nil);
135 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
136 created = [delegate webView:view createWebViewWithRequest:request];
137 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
138 created = [super webView:view createWebViewWithRequest:request];
142 // webView:addMessageToConsole: (X.Xx) {{{
143 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
144 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
145 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
146 [uiWebView webView:view addMessageToConsole:message];
149 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
150 id<CyteWebViewDelegate> delegate([self delegate]);
151 if ([delegate respondsToSelector:@selector(webView:addMessageToConsole:)])
152 [delegate webView:view addMessageToConsole:message];
153 if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
154 [super webView:view addMessageToConsole:message];
157 // webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
158 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
159 id<CyteWebViewDelegate> delegate([self delegate]);
160 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
161 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
162 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
163 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
164 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
165 [delegate webView:view didDecidePolicy:[mediator decision] forNavigationAction:action request:request frame:frame];
169 // webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
170 static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
171 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
172 if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
173 [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
176 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
177 id<CyteWebViewDelegate> delegate([self delegate]);
178 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
179 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
180 [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
181 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
182 [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
186 // webView:didClearWindowObject:forFrame: (3.2+) {{{
187 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
188 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
189 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
190 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
193 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
194 id<CyteWebViewDelegate> delegate([self delegate]);
195 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
196 [delegate webView:view didClearWindowObject:window forFrame:frame];
197 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
198 [super webView:view didClearWindowObject:window forFrame:frame];
201 // webView:didFailLoadWithError:forFrame: (2.0+) {{{
202 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
203 id<CyteWebViewDelegate> delegate([self delegate]);
204 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
205 [delegate webView:view didFailLoadWithError:error forFrame:frame];
206 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
207 [super webView:view didFailLoadWithError:error forFrame:frame];
210 // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
211 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
212 id<CyteWebViewDelegate> delegate([self delegate]);
213 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
214 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
215 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
216 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
219 // webView:didFinishLoadForFrame: (2.0+) {{{
220 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
221 id<CyteWebViewDelegate> delegate([self delegate]);
222 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
223 [delegate webView:view didFinishLoadForFrame:frame];
224 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
225 [super webView:view didFinishLoadForFrame:frame];
228 // webView:didReceiveTitle:forFrame: (3.2+) {{{
229 static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
230 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
231 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
232 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
235 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
236 id<CyteWebViewDelegate> delegate([self delegate]);
237 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
238 [delegate webView:view didReceiveTitle:title forFrame:frame];
239 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
240 [super webView:view didReceiveTitle:title forFrame:frame];
243 // webView:didStartProvisionalLoadForFrame: (2.0+) {{{
244 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
245 id<CyteWebViewDelegate> delegate([self delegate]);
246 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
247 [delegate webView:view didStartProvisionalLoadForFrame:frame];
248 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
249 [super webView:view didStartProvisionalLoadForFrame:frame];
252 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
253 static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
254 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
255 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
256 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
260 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
261 id<CyteWebViewDelegate> delegate([self delegate]);
262 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
263 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
264 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
265 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
269 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
270 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
271 [[self retain] autorelease];
272 id<CyteWebViewDelegate> delegate([self delegate]);
273 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
275 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
276 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
278 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
281 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
282 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
283 [[self retain] autorelease];
284 id<CyteWebViewDelegate> delegate([self delegate]);
285 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
287 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
288 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
290 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
294 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
295 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
296 [[self retain] autorelease];
297 id<CyteWebViewDelegate> delegate([self delegate]);
298 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
300 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
301 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
303 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
307 // webViewClose: (3.2+) {{{
308 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
309 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
310 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
311 [uiWebView webViewClose:view];
314 - (void) webViewClose:(WebView *)view {
315 id<CyteWebViewDelegate> delegate([self delegate]);
316 if ([delegate respondsToSelector:@selector(webViewClose:)])
317 [delegate webViewClose:view];
318 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
319 [super webViewClose:view];
323 - (void) _updateViewSettings {
324 [super _updateViewSettings];
326 id<CyteWebViewDelegate> delegate([self delegate]);
327 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
328 [delegate webViewUpdateViewSettings:self];
331 - (void) dispatchEvent:(NSString *)event {
332 [[self _documentView] dispatchEvent:event];
335 - (void) reloadFromOrigin {
336 [[[self _documentView] webView] reloadFromOrigin:nil];
341 __attribute__((__constructor__)) static void $() {
342 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
343 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
344 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
345 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
346 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
347 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
348 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");