1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
22 #include "CyteKit/dispatchEvent.h"
23 #include "CyteKit/WebView.h"
25 #include <CydiaSubstrate/CydiaSubstrate.h>
27 #include "iPhonePrivate.h"
29 // CYWebPolicyDecision* {{{
30 @interface CYWebPolicyDecisionMediator : NSObject <
31 WebPolicyDecisionListener
33 id<WebPolicyDecisionListener> listener_;
34 CYWebPolicyDecision decision_;
37 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
39 - (CYWebPolicyDecision) decision;
45 @implementation CYWebPolicyDecisionMediator
47 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
48 if ((self = [super init]) != nil) {
53 - (CYWebPolicyDecision) decision {
58 return decision_ != CYWebPolicyDecisionUnknown;
63 case CYWebPolicyDecisionUnknown:
65 NSLog(@"CYWebPolicyDecisionUnknown");
68 case CYWebPolicyDecisionDownload: [listener_ download]; break;
69 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
70 case CYWebPolicyDecisionUse: [listener_ use]; break;
77 decision_ = CYWebPolicyDecisionDownload;
81 decision_ = CYWebPolicyDecisionIgnore;
85 decision_ = CYWebPolicyDecisionUse;
91 @implementation CyteWebView : UIWebView
94 #include "CyteKit/UCInternal.h"
97 - (id) initWithFrame:(CGRect)frame {
98 if ((self = [super initWithFrame:frame]) != nil) {
103 if (kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber_iPhoneOS_3_0) {
104 UIWebViewInternal *&_internal(MSHookIvar<UIWebViewInternal *>(self, "_internal"));
105 if (&_internal != NULL) {
106 UIWebViewWebViewDelegate *&webViewDelegate(MSHookIvar<UIWebViewWebViewDelegate *>(_internal, "webViewDelegate"));
107 if (&webViewDelegate != NULL)
108 [webViewDelegate _clearUIWebView];
115 - (NSString *) description {
116 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[self request] URL] absoluteString]];
119 - (id<CyteWebViewDelegate>) delegate {
120 return (id<CyteWebViewDelegate>) [super delegate];
123 - (void) setDelegate:(id<CyteWebViewDelegate>)delegate {
124 [super setDelegate:delegate];
127 /*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
128 id<CyteWebViewDelegate> delegate([self delegate]);
129 WebView *created(nil);
130 if (created == nil && [delegate respondsToSelector:@selector(webView:createWebViewWithRequest:)])
131 created = [delegate webView:view createWebViewWithRequest:request];
132 if (created == nil && [UIWebView instancesRespondToSelector:@selector(webView:createWebViewWithRequest:)])
133 created = [super webView:view createWebViewWithRequest:request];
137 // webView:addMessageToConsole: (X.Xx) {{{
138 static void $UIWebViewWebViewDelegate$webView$addMessageToConsole$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSDictionary *message) {
139 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
140 if ([uiWebView respondsToSelector:@selector(webView:addMessageToConsole:)])
141 [uiWebView webView:view addMessageToConsole:message];
144 - (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
145 id<CyteWebViewDelegate> delegate([self delegate]);
146 if ([delegate respondsToSelector:@selector(webView:addMessageToConsole:)])
147 [delegate webView:view addMessageToConsole:message];
148 if ([UIWebView instancesRespondToSelector:@selector(webView:addMessageToConsole:)])
149 [super webView:view addMessageToConsole:message];
152 // webView:decidePolicyForNavigationAction:request:frame:decisionListener: (2.0+) {{{
153 - (void) webView:(WebView *)view decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
154 id<CyteWebViewDelegate> delegate([self delegate]);
155 CYWebPolicyDecisionMediator *mediator([[[CYWebPolicyDecisionMediator alloc] initWithListener:listener] autorelease]);
156 if (![mediator decided] && [delegate respondsToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
157 [delegate webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
158 if (![mediator decided] && [UIWebView instancesRespondToSelector:@selector(webView:decidePolicyForNavigationAction:request:frame:decisionListener:)])
159 [super webView:view decidePolicyForNavigationAction:action request:request frame:frame decisionListener:mediator];
160 if ([delegate respondsToSelector:@selector(webView:didDecidePolicy:forNavigationAction:request:frame:)])
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:didCommitLoadForFrame: (3.0+) {{{
198 static void $UIWebViewWebViewDelegate$webView$didCommitLoadForFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, WebFrame *frame) {
199 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
200 if ([uiWebView respondsToSelector:@selector(webView:didCommitLoadForFrame:)])
201 [uiWebView webView:view didCommitLoadForFrame:frame];
204 - (void) webView:(WebView *)view didCommitLoadForFrame:(WebFrame *)frame {
205 id<CyteWebViewDelegate> delegate([self delegate]);
206 if ([delegate respondsToSelector:@selector(webView:didCommitLoadForFrame:)])
207 [delegate webView:view didCommitLoadForFrame:frame];
208 if ([UIWebView instancesRespondToSelector:@selector(webView:didCommitLoadForFrame:)])
209 [super webView:view didCommitLoadForFrame:frame];
212 // webView:didFailLoadWithError:forFrame: (2.0+) {{{
213 - (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
214 id<CyteWebViewDelegate> delegate([self delegate]);
215 if ([delegate respondsToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
216 [delegate webView:view didFailLoadWithError:error forFrame:frame];
217 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailLoadWithError:forFrame:)])
218 [super webView:view didFailLoadWithError:error forFrame:frame];
221 // webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
222 - (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
223 id<CyteWebViewDelegate> delegate([self delegate]);
224 if ([delegate respondsToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
225 [delegate webView:view didFailProvisionalLoadWithError:error forFrame:frame];
226 if ([UIWebView instancesRespondToSelector:@selector(webView:didFailProvisionalLoadWithError:forFrame:)])
227 [super webView:view didFailProvisionalLoadWithError:error forFrame:frame];
230 // webView:didFinishLoadForFrame: (2.0+) {{{
231 - (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
232 id<CyteWebViewDelegate> delegate([self delegate]);
233 if ([delegate respondsToSelector:@selector(webView:didFinishLoadForFrame:)])
234 [delegate webView:view didFinishLoadForFrame:frame];
235 if ([UIWebView instancesRespondToSelector:@selector(webView:didFinishLoadForFrame:)])
236 [super webView:view didFinishLoadForFrame:frame];
239 // webView:didReceiveTitle:forFrame: (3.2+) {{{
240 static void $UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, NSString *title, WebFrame *frame) {
241 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
242 if ([uiWebView respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
243 [uiWebView webView:view didReceiveTitle:title forFrame:frame];
246 - (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
247 id<CyteWebViewDelegate> delegate([self delegate]);
248 if ([delegate respondsToSelector:@selector(webView:didReceiveTitle:forFrame:)])
249 [delegate webView:view didReceiveTitle:title forFrame:frame];
250 if ([UIWebView instancesRespondToSelector:@selector(webView:didReceiveTitle:forFrame:)])
251 [super webView:view didReceiveTitle:title forFrame:frame];
254 // webView:didStartProvisionalLoadForFrame: (2.0+) {{{
255 - (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
256 id<CyteWebViewDelegate> delegate([self delegate]);
257 if ([delegate respondsToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
258 [delegate webView:view didStartProvisionalLoadForFrame:frame];
259 if ([UIWebView instancesRespondToSelector:@selector(webView:didStartProvisionalLoadForFrame:)])
260 [super webView:view didStartProvisionalLoadForFrame:frame];
263 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
264 static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
265 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
266 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
267 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
271 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
272 id<CyteWebViewDelegate> delegate([self delegate]);
273 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
274 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
275 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
276 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
280 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
281 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
282 [[self retain] autorelease];
283 id<CyteWebViewDelegate> delegate([self delegate]);
284 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
286 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
287 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
289 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
292 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
293 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
294 [[self retain] autorelease];
295 id<CyteWebViewDelegate> delegate([self delegate]);
296 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
298 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
299 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
301 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
305 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
306 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
307 [[self retain] autorelease];
308 id<CyteWebViewDelegate> delegate([self delegate]);
309 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
311 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
312 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
314 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
318 // webViewClose: (3.2+) {{{
319 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
320 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
321 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
322 [uiWebView webViewClose:view];
325 - (void) webViewClose:(WebView *)view {
326 id<CyteWebViewDelegate> delegate([self delegate]);
327 if ([delegate respondsToSelector:@selector(webViewClose:)])
328 [delegate webViewClose:view];
329 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
330 [super webViewClose:view];
334 - (void) _updateViewSettings {
335 [super _updateViewSettings];
337 id<CyteWebViewDelegate> delegate([self delegate]);
338 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
339 [delegate webViewUpdateViewSettings:self];
342 - (void) dispatchEvent:(NSString *)event {
343 [[self _documentView] dispatchEvent:event];
346 - (void) reloadFromOrigin {
347 [[[self _documentView] webView] reloadFromOrigin:nil];
350 - (UIScrollView *) scrollView {
351 if ([self respondsToSelector:@selector(_scrollView)])
352 return [self _scrollView];
353 else if ([self respondsToSelector:@selector(_scroller)])
354 return (UIScrollView *) [self _scroller];
358 - (void) setNeedsLayout {
359 [super setNeedsLayout];
361 WebFrame *frame([[[self _documentView] webView] mainFrame]);
362 if ([frame respondsToSelector:@selector(setNeedsLayout)])
363 [frame setNeedsLayout];
366 - (NSURLRequest *) request {
367 WebFrame *frame([[[self _documentView] webView] mainFrame]);
368 return [([frame provisionalDataSource] ?: [frame dataSource]) request];
373 static void $UIWebViewWebViewDelegate$_clearUIWebView(UIWebViewWebViewDelegate *self, SEL sel) {
374 MSHookIvar<UIWebView *>(self, "uiWebView") = nil;
377 __attribute__((__constructor__)) static void $() {
378 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
379 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
380 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
381 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
382 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didCommitLoadForFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didCommitLoadForFrame$, "v16@0:4@8@12");
383 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
384 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
385 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
386 class_addMethod($UIWebViewWebViewDelegate, @selector(_clearUIWebView), (IMP) &$UIWebViewWebViewDelegate$_clearUIWebView, "v8@0:4");