]> git.saurik.com Git - cydia.git/blame - CyteKit/WebView.mm
Constrain the dimensions of icons rendered from repositories.
[cydia.git] / CyteKit / WebView.mm
CommitLineData
f172aa8f
JF
1/* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2011 Jay Freeman (saurik)
3*/
4
5/* Modified BSD License {{{ */
6/*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
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
18 * distribution.
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.
22 *
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.
37*/
38/* }}} */
39
d458596e 40#include "CyteKit/WebView.h"
4bcafc01 41#include "CyteKit/WebThreadLocked.hpp"
aa35fd7b 42
435cb857
JF
43#include <CydiaSubstrate/CydiaSubstrate.h>
44
aa35fd7b 45#include "iPhonePrivate.h"
aa35fd7b
JF
46
47// CYWebPolicyDecision* {{{
aa35fd7b
JF
48@interface CYWebPolicyDecisionMediator : NSObject <
49 WebPolicyDecisionListener
50> {
51 id<WebPolicyDecisionListener> listener_;
52 CYWebPolicyDecision decision_;
53}
54
55- (id) initWithListener:(id<WebPolicyDecisionListener>)listener;
56
57- (CYWebPolicyDecision) decision;
58- (bool) decided;
59- (bool) decide;
60
61@end
62
63@implementation CYWebPolicyDecisionMediator
64
65- (id) initWithListener:(id<WebPolicyDecisionListener>)listener {
66 if ((self = [super init]) != nil) {
67 listener_ = listener;
68 } return self;
69}
70
71- (CYWebPolicyDecision) decision {
72 return decision_;
73}
74
75- (bool) decided {
76 return decision_ != CYWebPolicyDecisionUnknown;
77}
78
79- (bool) decide {
80 switch (decision_) {
81 case CYWebPolicyDecisionUnknown:
82 default:
83 NSLog(@"CYWebPolicyDecisionUnknown");
84 return false;
85
86 case CYWebPolicyDecisionDownload: [listener_ download]; break;
87 case CYWebPolicyDecisionIgnore: [listener_ ignore]; break;
88 case CYWebPolicyDecisionUse: [listener_ use]; break;
89 }
90
91 return true;
92}
93
94- (void) download {
95 decision_ = CYWebPolicyDecisionDownload;
96}
97
98- (void) ignore {
99 decision_ = CYWebPolicyDecisionIgnore;
100}
101
102- (void) use {
103 decision_ = CYWebPolicyDecisionUse;
104}
105
106@end
107// }}}
108
1ee69bb4 109@implementation CyteWebView : UIWebView
aa35fd7b
JF
110
111#if ShowInternals
112#include "CyteKit/UCInternal.h"
113#endif
114
115- (id) initWithFrame:(CGRect)frame {
116 if ((self = [super initWithFrame:frame]) != nil) {
117 } return self;
118}
119
120- (void) dealloc {
121 [super dealloc];
122}
123
3479309e
JF
124- (NSString *) description {
125 return [NSString stringWithFormat:@"<%s: %p, %@>", class_getName([self class]), self, [[[self request] URL] absoluteString]];
126}
127
1ee69bb4
JF
128- (id<CyteWebViewDelegate>) delegate {
129 return (id<CyteWebViewDelegate>) [super delegate];
aa35fd7b
JF
130}
131
132/*- (WebView *) webView:(WebView *)view createWebViewWithRequest:(NSURLRequest *)request {
1ee69bb4 133 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
139 return created;
140}*/
141
142// webView:addMessageToConsole: (X.Xx) {{{
143static 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];
147}
148
149- (void) webView:(WebView *)view addMessageToConsole:(NSDictionary *)message {
1ee69bb4 150 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
155}
156// }}}
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 {
1ee69bb4 159 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
d323285e 165 [delegate webView:view didDecidePolicy:[mediator decision] forNavigationAction:action request:request frame:frame];
aa35fd7b
JF
166 [mediator decide];
167}
168// }}}
169// webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener: (3.0+) {{{
170static 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];
174}
175
176- (void) webView:(WebView *)view decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
1ee69bb4 177 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
183 [mediator decide];
184}
185// }}}
186// webView:didClearWindowObject:forFrame: (3.2+) {{{
187static 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];
191}
192
193- (void) webView:(WebView *)view didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
1ee69bb4 194 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
199}
200// }}}
201// webView:didFailLoadWithError:forFrame: (2.0+) {{{
202- (void) webView:(WebView *)view didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1ee69bb4 203 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
208}
209// }}}
210// webView:didFailProvisionalLoadWithError:forFrame: (2.0+) {{{
211- (void) webView:(WebView *)view didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
1ee69bb4 212 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
217}
218// }}}
219// webView:didFinishLoadForFrame: (2.0+) {{{
220- (void) webView:(WebView *)view didFinishLoadForFrame:(WebFrame *)frame {
1ee69bb4 221 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
226}
227// }}}
228// webView:didReceiveTitle:forFrame: (3.2+) {{{
229static 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];
233}
234
235- (void) webView:(WebView *)view didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
1ee69bb4 236 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
241}
242// }}}
243// webView:didStartProvisionalLoadForFrame: (2.0+) {{{
244- (void) webView:(WebView *)view didStartProvisionalLoadForFrame:(WebFrame *)frame {
1ee69bb4 245 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
250}
251// }}}
252// webView:resource:willSendRequest:redirectResponse:fromDataSource: (3.2+) {{{
253static 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];
257 return request;
258}
259
260- (NSURLRequest *) webView:(WebView *)view resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response fromDataSource:(WebDataSource *)source {
1ee69bb4 261 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
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];
266 return request;
267}
268// }}}
269// webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame: (2.1+) {{{
270- (void) webView:(WebView *)view runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
eea75e8f 271 [[self retain] autorelease];
1ee69bb4 272 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
273 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptAlertPanelWithMessage:initiatedByFrame:)])
274 if (
275 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptAlertPanelWithMessage:initiatedByFrame:)] ||
276 [delegate webView:view shouldRunJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame]
277 )
278 [super webView:view runJavaScriptAlertPanelWithMessage:message initiatedByFrame:frame];
279}
280// }}}
281// webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame: (2.1+) {{{
282- (BOOL) webView:(WebView *)view runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
eea75e8f 283 [[self retain] autorelease];
1ee69bb4 284 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
285 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptConfirmPanelWithMessage:initiatedByFrame:)])
286 if (
287 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptConfirmPanelWithMessage:initiatedByFrame:)] ||
288 [delegate webView:view shouldRunJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame]
289 )
290 return [super webView:view runJavaScriptConfirmPanelWithMessage:message initiatedByFrame:frame];
291 return NO;
292}
293// }}}
294// webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame: (2.1+) {{{
295- (NSString *) webView:(WebView *)view runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(NSString *)text initiatedByFrame:(WebFrame *)frame {
eea75e8f 296 [[self retain] autorelease];
1ee69bb4 297 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
298 if ([UIWebView instancesRespondToSelector:@selector(webView:runJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)])
299 if (
300 ![delegate respondsToSelector:@selector(webView:shouldRunJavaScriptTextInputPanelWithPrompt:defaultText:initiatedByFrame:)] ||
301 [delegate webView:view shouldRunJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame]
302 )
303 return [super webView:view runJavaScriptTextInputPanelWithPrompt:prompt defaultText:text initiatedByFrame:frame];
304 return nil;
305}
306// }}}
307// webViewClose: (3.2+) {{{
308static 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];
312}
313
314- (void) webViewClose:(WebView *)view {
1ee69bb4 315 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
316 if ([delegate respondsToSelector:@selector(webViewClose:)])
317 [delegate webViewClose:view];
318 if ([UIWebView instancesRespondToSelector:@selector(webViewClose:)])
319 [super webViewClose:view];
320}
321// }}}
322
323- (void) _updateViewSettings {
324 [super _updateViewSettings];
325
1ee69bb4 326 id<CyteWebViewDelegate> delegate([self delegate]);
aa35fd7b
JF
327 if ([delegate respondsToSelector:@selector(webViewUpdateViewSettings:)])
328 [delegate webViewUpdateViewSettings:self];
329}
330
4bcafc01
JF
331- (void) dispatchEvent:(NSString *)event {
332 WebThreadLocked lock;
333
334 NSString *script([NSString stringWithFormat:@
335 "(function() {"
336 "var event = this.document.createEvent('Events');"
337 "event.initEvent('%@', false, false);"
338 "this.document.dispatchEvent(event);"
339 "})();"
340 , event]);
341
342 NSMutableArray *frames([NSMutableArray arrayWithObjects:
343 [[[self _documentView] webView] mainFrame]
344 , nil]);
345
346 while (WebFrame *frame = [frames lastObject]) {
347 WebScriptObject *object([frame windowObject]);
348 [object evaluateWebScript:script];
349 [frames removeLastObject];
350 [frames addObjectsFromArray:[frame childFrames]];
351 }
352}
353
a713fcd3
JF
354- (void) reloadFromOrigin {
355 [[[self _documentView] webView] reloadFromOrigin:nil];
356}
357
aa35fd7b
JF
358+ (void) initialize {
359 if (Class $UIWebViewWebViewDelegate = objc_getClass("UIWebViewWebViewDelegate")) {
360 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:addMessageToConsole:), (IMP) &$UIWebViewWebViewDelegate$webView$addMessageToConsole$, "v16@0:4@8@12");
361 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:), (IMP) &$UIWebViewWebViewDelegate$webView$decidePolicyForNewWindowAction$request$newFrameName$decisionListener$, "v28@0:4@8@12@16@20@24");
362 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didClearWindowObject:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didClearWindowObject$forFrame$, "v20@0:4@8@12@16");
363 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:didReceiveTitle:forFrame:), (IMP) &$UIWebViewWebViewDelegate$webView$didReceiveTitle$forFrame$, "v20@0:4@8@12@16");
364 class_addMethod($UIWebViewWebViewDelegate, @selector(webView:resource:willSendRequest:redirectResponse:fromDataSource:), (IMP) &$UIWebViewWebViewDelegate$webView$resource$willSendRequest$redirectResponse$fromDataSource$, "@28@0:4@8@12@16@20@24");
365 class_addMethod($UIWebViewWebViewDelegate, @selector(webViewClose:), (IMP) &$UIWebViewWebViewDelegate$webViewClose$, "v12@0:4@8");
366 }
367}
368
369@end