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) {
121 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) {
122 UIWebViewInternal *&_internal(MSHookIvar<UIWebViewInternal *>(self, "_internal"));
123 if (&_internal != NULL) {
124 UIWebViewWebViewDelegate *&webViewDelegate(MSHookIvar<UIWebViewWebViewDelegate *>(_internal, "webViewDelegate"));
125 if (&webViewDelegate != NULL)
126 [webViewDelegate _clearUIWebView];
133 - (NSString *) description {
134 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[self request] URL] absoluteString]];
137 - (id<CyteWebViewDelegate>) delegate {
138 return (id<CyteWebViewDelegate>) [super delegate];
141 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
142 id<CyteWebViewDelegate> delegate([self delegate]);
143 WebView *created(nil);
144 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
145 created = [delegate webView:view createWebViewWithRequest:request];
146 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
147 created = [super webView:view createWebViewWithRequest:request];
151 // webView:addMessageToConsole: (X.Xx) {{{
152 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
153 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
154 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
155 [uiWebView webView:view addMessageToConsole:message];
158 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
159 id<CyteWebViewDelegate> delegate([self delegate]);
160 if ([delegate respondsToSelector:@selector(webView:addMessageToConsole:)])
161 [delegate webView:view addMessageToConsole:message];
162 if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
163 [super webView:view addMessageToConsole:message];
166 // webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
167 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
168 id<CyteWebViewDelegate> delegate([self delegate]);
169 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
170 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
171 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
172 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
173 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
174 if ([delegate respondsToSelector:@selector(webView:didDecidePolicy:forNavigationAction:request:frame:)])
175 [delegate webView:view didDecidePolicy:[mediator decision] forNavigationAction:action request:request frame:frame];
179 // webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
180 static void $UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *action, NSURLRequest *request, NSString *frame, id<WebPolicyDecisionListener> listener) {
181 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
182 if ([uiWebView respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
183 [uiWebView webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:listener];
186 - (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
187 id<CyteWebViewDelegate> delegate([self delegate]);
188 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
189 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
190 [delegate webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
191 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:)])
192 [super webView:view decidePolicyForNewWindowAction:action request:request newFrameName:frame decisionListener:mediator];
196 // webView:didClearWindowObject:forFrame: (3.2+) {{{
197 static void $UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebScriptObject *window, WebFrame *frame) {
198 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
199 if ([uiWebView respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
200 [uiWebView webView:view didClearWindowObject:window forFrame:frame];
203 - (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
204 id<CyteWebViewDelegate> delegate([self delegate]);
205 if ([delegate respondsToSelector:@selector(webView:didClearWindowObject:forFrame:)])
206 [delegate webView:view didClearWindowObject:window forFrame:frame];
207 if ([UIWebView instancesRespondToSelector:@selector(webView:didClearWindowObject:forFrame:)])
208 [super webView:view didClearWindowObject:window forFrame:frame];
211 // webView:didCommitLoadForFrame: (3.0+) {{{
212 static void $UIWebViewWebViewDelegate$webView$didCommitLoadForFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebFrame *frame) {
213 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
214 if ([uiWebView respondsToSelector:@selector(webView:didCommitLoadForFrame:)])
215 [uiWebView webView:view didCommitLoadForFrame:frame];
218 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
219 id<CyteWebViewDelegate> delegate([self delegate]);
220 if ([delegate respondsToSelector:@selector(webView:didCommitLoadForFrame:)])
221 [delegate webView:view didCommitLoadForFrame:frame];
222 if ([UIWebView instancesRespondToSelector:@selector(webView:didCommitLoadForFrame:)])
223 [super webView:view didCommitLoadForFrame:frame];
226 // webView:didFailLoadWithError:forFrame: (2.0+) {{{
227 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
228 id<CyteWebViewDelegate> delegate([self delegate]);
229 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
230 [delegate webView:view didFailLoadWithError:error forFrame:frame];
231 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
232 [super webView:view didFailLoadWithError:error forFrame:frame];
235 // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
236 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
237 id<CyteWebViewDelegate> delegate([self delegate]);
238 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
239 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
240 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
241 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
244 // webView:didFinishLoadForFrame: (2.0+) {{{
245 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
246 id<CyteWebViewDelegate> delegate([self delegate]);
247 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
248 [delegate webView:view didFinishLoadForFrame:frame];
249 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
250 [super webView:view didFinishLoadForFrame:frame];
253 // webView:didReceiveTitle:forFrame: (3.2+) {{{
254 static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
255 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
256 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
257 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
260 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
261 id<CyteWebViewDelegate> delegate([self delegate]);
262 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
263 [delegate webView:view didReceiveTitle:title forFrame:frame];
264 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
265 [super webView:view didReceiveTitle:title forFrame:frame];
268 // webView:didStartProvisionalLoadForFrame: (2.0+) {{{
269 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
270 id<CyteWebViewDelegate> delegate([self delegate]);
271 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
272 [delegate webView:view didStartProvisionalLoadForFrame:frame];
273 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
274 [super webView:view didStartProvisionalLoadForFrame:frame];
277 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
278 static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
279 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
280 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
281 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
285 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
286 id<CyteWebViewDelegate> delegate([self delegate]);
287 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
288 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
289 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
290 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
294 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
295 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
296 [[self retain] autorelease];
297 id<CyteWebViewDelegate> delegate([self delegate]);
298 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
300 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
301 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
303 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
306 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
307 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
308 [[self retain] autorelease];
309 id<CyteWebViewDelegate> delegate([self delegate]);
310 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
312 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
313 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
315 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
319 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
320 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
321 [[self retain] autorelease];
322 id<CyteWebViewDelegate> delegate([self delegate]);
323 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
325 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
326 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
328 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
332 // webViewClose: (3.2+) {{{
333 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
334 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
335 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
336 [uiWebView webViewClose:view];
339 - (void) webViewClose:(WebView *)view {
340 id<CyteWebViewDelegate> delegate([self delegate]);
341 if ([delegate respondsToSelector:@selector(webViewClose:)])
342 [delegate webViewClose:view];
343 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
344 [super webViewClose:view];
348 - (void) _updateViewSettings {
349 [super _updateViewSettings];
351 id<CyteWebViewDelegate> delegate([self delegate]);
352 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
353 [delegate webViewUpdateViewSettings:self];
356 - (void) dispatchEvent:(NSString *)event {
357 [[self _documentView] dispatchEvent:event];
360 - (void) reloadFromOrigin {
361 [[[self _documentView] webView] reloadFromOrigin:nil];
364 - (UIScrollView *) scrollView {
365 if ([self respondsToSelector:@selector(_scrollView)])
366 return [self _scrollView];
367 else if ([self respondsToSelector:@selector(_scroller)])
368 return (UIScrollView *) [self _scroller];
372 - (void) setNeedsLayout {
373 [super setNeedsLayout];
375 WebFrame *frame([[[self _documentView] webView] mainFrame]);
376 if ([frame respondsToSelector:@selector(setNeedsLayout)])
377 [frame setNeedsLayout];
380 - (NSURLRequest *) request {
381 WebFrame *frame([[[self _documentView] webView] mainFrame]);
382 return [([frame provisionalDataSource] ?: [frame dataSource]) request];
387 static void $UIWebViewWebViewDelegate$_clearUIWebView(UIWebViewWebViewDelegate *self, SEL sel) {
388 MSHookIvar<UIWebView *>(self, "uiWebView") = nil;
391 __attribute__((__constructor__)) static void $() {
392 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
393 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
394 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
395 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
396 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didCommitLoadForFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didCommitLoadForFrame$, "v16@0:4@8@12");
397 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
398 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
399 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
400 class_addMethod($UIWebViewWebViewDelegate, @selector(_clearUIWebView), (IMP) &$UIWebViewWebViewDelegate$_clearUIWebView, "v8@0:4");