]> git.saurik.com Git - cydia.git/blob - CyteKit/WebView.mm
Remove the section "promoted" cells (just easier).
[cydia.git] / CyteKit / WebView.mm
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
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.
11 *
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.
16 *
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/>.
19 **/
20 /* }}} */
21
22 #include "CyteKit/dispatchEvent.h"
23 #include "CyteKit/WebView.h"
24
25 #include <CydiaSubstrate/CydiaSubstrate.h>
26
27 #include "iPhonePrivate.h"
28
29 // CYWebPolicyDecision* {{{
30 @interface CYWebPolicyDecisionMediator : NSObject <
31 WebPolicyDecisionListener
32 > {
33 id<WebPolicyDecisionListener> listener_;
34 CYWebPolicyDecision decision_;
35 }
36
37 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
38
39 - (CYWebPolicyDecision) decision;
40 - (bool) decided;
41 - (bool) decide;
42
43 @end
44
45 @implementation CYWebPolicyDecisionMediator
46
47 - (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
48 if ((self = [super init]) != nil) {
49 listener_ = listener;
50 } return self;
51 }
52
53 - (CYWebPolicyDecision) decision {
54 return decision_;
55 }
56
57 - (bool) decided {
58 return decision_ != CYWebPolicyDecisionUnknown;
59 }
60
61 - (bool) decide {
62 switch (decision_) {
63 case CYWebPolicyDecisionUnknown:
64 default:
65 NSLog(@"CYWebPolicyDecisionUnknown");
66 return false;
67
68 case CYWebPolicyDecisionDownload: [listener_ download]; break;
69 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
70 case CYWebPolicyDecisionUse: [listener_ use]; break;
71 }
72
73 return true;
74 }
75
76 - (void) download {
77 decision_ = CYWebPolicyDecisionDownload;
78 }
79
80 - (void) ignore {
81 decision_ = CYWebPolicyDecisionIgnore;
82 }
83
84 - (void) use {
85 decision_ = CYWebPolicyDecisionUse;
86 }
87
88 @end
89 // }}}
90
91 @implementation CyteWebView : UIWebView
92
93 #if ShowInternals
94 #include "CyteKit/UCInternal.h"
95 #endif
96
97 - (id) initWithFrame:(CGRect)frame {
98 if ((self = [super initWithFrame:frame]) != nil) {
99 } return self;
100 }
101
102 - (void) dealloc {
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];
109 }
110 }
111
112 [super dealloc];
113 }
114
115 - (NSString *) description {
116 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[self request] URL] absoluteString]];
117 }
118
119 - (id<CyteWebViewDelegate>) delegate {
120 return (id<CyteWebViewDelegate>) [super delegate];
121 }
122
123 - (void) setDelegate:(id<CyteWebViewDelegate>)delegate {
124 [super setDelegate:delegate];
125 }
126
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];
134 return created;
135 }*/
136
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];
142 }
143
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];
150 }
151 // }}}
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];
162 [mediator decide];
163 }
164 // }}}
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];
170 }
171
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];
179 [mediator decide];
180 }
181 // }}}
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];
187 }
188
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];
195 }
196 // }}}
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];
202 }
203
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];
210 }
211 // }}}
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];
219 }
220 // }}}
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];
228 }
229 // }}}
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];
237 }
238 // }}}
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];
244 }
245
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];
252 }
253 // }}}
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];
261 }
262 // }}}
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];
268 return request;
269 }
270
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];
277 return request;
278 }
279 // }}}
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:)])
285 if (
286 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
287 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
288 )
289 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
290 }
291 // }}}
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:)])
297 if (
298 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
299 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
300 )
301 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
302 return NO;
303 }
304 // }}}
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:)])
310 if (
311 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
312 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
313 )
314 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
315 return nil;
316 }
317 // }}}
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];
323 }
324
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];
331 }
332 // }}}
333
334 - (void) _updateViewSettings {
335 [super _updateViewSettings];
336
337 id<CyteWebViewDelegate> delegate([self delegate]);
338 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
339 [delegate webViewUpdateViewSettings:self];
340 }
341
342 - (void) dispatchEvent:(NSString *)event {
343 [[self _documentView] dispatchEvent:event];
344 }
345
346 - (void) reloadFromOrigin {
347 [[[self _documentView] webView] reloadFromOrigin:nil];
348 }
349
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];
355 else return nil;
356 }
357
358 - (void) setNeedsLayout {
359 [super setNeedsLayout];
360
361 WebFrame *frame([[[self _documentView] webView] mainFrame]);
362 if ([frame respondsToSelector:@selector(setNeedsLayout)])
363 [frame setNeedsLayout];
364 }
365
366 - (NSURLRequest *) request {
367 WebFrame *frame([[[self _documentView] webView] mainFrame]);
368 return [([frame provisionalDataSource] ?: [frame dataSource]) request];
369 }
370
371 @end
372
373 static void $UIWebViewWebViewDelegate$_clearUIWebView(UIWebViewWebViewDelegate *self, SEL sel) {
374 MSHookIvar<UIWebView *>(self, "uiWebView") = nil;
375 }
376
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");
387 }
388 }