1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 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 "Substrate.hpp"
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:didCancelAuthenticationChallenge:fromDataSource: {{{
264 - (void) webView:(WebView *)view resource:(id)identifier didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
265 id<CyteWebViewDelegate> delegate([self delegate]);
266 if ([UIWebView respondsToSelector:@selector(webView:resource:didCancelAuthenticationChallenge:fromDataSource:)])
267 [super webView:view resource:identifier didCancelAuthenticationChallenge:challenge fromDataSource:source];
268 if ([delegate respondsToSelector:@selector(webView:resource:didCancelAuthenticationChallenge:fromDataSource:)])
269 [delegate webView:view resource:identifier didCancelAuthenticationChallenge:challenge fromDataSource:source];
272 // webView:resource:didReceiveAuthenticationChallenge:fromDataSource: {{{
273 - (void) webView:(WebView *)view resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
274 id<CyteWebViewDelegate> delegate([self delegate]);
275 if ([UIWebView respondsToSelector:@selector(webView:resource:didReceiveAuthenticationChallenge:fromDataSource:)])
276 [super webView:view resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
277 if ([delegate respondsToSelector:@selector(webView:resource:didReceiveAuthenticationChallenge:fromDataSource:)])
278 [delegate webView:view resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source];
281 // webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
282 static NSURLRequest *$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view, id identifier, NSURLRequest *request, NSURLResponse *response, WebDataSource *source) {
283 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
284 if ([uiWebView respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
285 request = [uiWebView webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
289 - (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
290 id<CyteWebViewDelegate> delegate([self delegate]);
291 if ([UIWebView instancesRespondToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
292 request = [super webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
293 if ([delegate respondsToSelector:@selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:)])
294 request = [delegate webView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
298 // webThreadWebView:resource:willSendRequest:redirectResponse:fromDataSource: {{{
299 - (NSURLRequest *) webThreadWebView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
300 id<CyteWebViewDelegate> delegate([self delegate]);
301 if ([UIWebView instancesRespondToSelector:@selector(webThreadWebView:resource:willSendRequest:redirectResponse:fromDataSource:)])
302 request = [super webThreadWebView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
303 if ([delegate respondsToSelector:@selector(webThreadWebView:resource:willSendRequest:redirectResponse:fromDataSource:)])
304 request = [delegate webThreadWebView:view resource:identifier willSendRequest:request redirectResponse:response fromDataSource:source];
308 // webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
309 - (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
310 [[self retain] autorelease];
311 id<CyteWebViewDelegate> delegate([self delegate]);
312 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
314 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
315 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
317 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
320 // webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
321 - (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
322 [[self retain] autorelease];
323 id<CyteWebViewDelegate> delegate([self delegate]);
324 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
326 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
327 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
329 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
333 // webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
334 - (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
335 [[self retain] autorelease];
336 id<CyteWebViewDelegate> delegate([self delegate]);
337 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
339 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
340 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
342 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
346 // webViewClose: (3.2+) {{{
347 static void $UIWebViewWebViewDelegate$webViewClose$(UIWebViewWebViewDelegate *self, SEL sel, WebView *view) {
348 UIWebView *uiWebView(MSHookIvar<UIWebView *>(self, "uiWebView"));
349 if ([uiWebView respondsToSelector:@selector(webViewClose:)])
350 [uiWebView webViewClose:view];
353 - (void) webViewClose:(WebView *)view {
354 id<CyteWebViewDelegate> delegate([self delegate]);
355 if ([delegate respondsToSelector:@selector(webViewClose:)])
356 [delegate webViewClose:view];
357 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
358 [super webViewClose:view];
362 - (void) _updateViewSettings {
363 [super _updateViewSettings];
365 id<CyteWebViewDelegate> delegate([self delegate]);
366 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
367 [delegate webViewUpdateViewSettings:self];
370 - (void) dispatchEvent:(NSString *)event {
371 [[self _documentView] dispatchEvent:event];
374 - (void) reloadFromOrigin {
375 [[[self _documentView] webView] reloadFromOrigin:nil];
378 - (UIScrollView *) scrollView {
379 if ([self respondsToSelector:@selector(_scrollView)])
380 return [self _scrollView];
381 else if ([self respondsToSelector:@selector(_scroller)])
382 return (UIScrollView *) [self _scroller];
386 - (void) setNeedsLayout {
387 [super setNeedsLayout];
389 WebFrame *frame([[[self _documentView] webView] mainFrame]);
390 if ([frame respondsToSelector:@selector(setNeedsLayout)])
391 [frame setNeedsLayout];
394 - (NSURLRequest *) request {
395 WebFrame *frame([[[self _documentView] webView] mainFrame]);
396 return [([frame provisionalDataSource] ?: [frame dataSource]) request];
401 static void $UIWebViewWebViewDelegate$_clearUIWebView(UIWebViewWebViewDelegate *self, SEL sel) {
402 MSHookIvar<UIWebView *>(self, "uiWebView") = nil;
405 __attribute__((__constructor__)) static void $() {
406 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
407 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
408 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
409 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
410 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didCommitLoadForFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didCommitLoadForFrame$, "v16@0:4@8@12");
411 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
412 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
413 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
414 class_addMethod($UIWebViewWebViewDelegate, @selector(_clearUIWebView), (IMP) &$UIWebViewWebViewDelegate$_clearUIWebView, "v8@0:4");