]>
Commit | Line | Data |
---|---|---|
22f8bed9 JF |
1 | #include <BrowserView.h> |
2 | ||
aa5d0de7 JF |
3 | /* Indirect Delegate {{{ */ |
4 | @interface IndirectDelegate : NSProxy { | |
5 | _transient volatile id delegate_; | |
6 | } | |
7 | ||
8 | - (void) setDelegate:(id)delegate; | |
9 | - (id) initWithDelegate:(id)delegate; | |
10 | @end | |
11 | ||
12 | @implementation IndirectDelegate | |
13 | ||
14 | - (void) setDelegate:(id)delegate { | |
15 | delegate_ = delegate; | |
16 | } | |
17 | ||
18 | - (id) initWithDelegate:(id)delegate { | |
19 | delegate_ = delegate; | |
20 | return self; | |
21 | } | |
22 | ||
23 | - (BOOL) respondsToSelector:(SEL)sel { | |
24 | return delegate_ == nil ? FALSE : [delegate_ respondsToSelector:sel]; | |
25 | } | |
26 | ||
27 | - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel { | |
28 | if (delegate_ != nil) | |
29 | if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel]) | |
30 | return sig; | |
31 | // XXX: I fucking hate Apple so very very bad | |
32 | return [NSMethodSignature signatureWithObjCTypes:"v@:"]; | |
33 | } | |
34 | ||
35 | - (void) forwardInvocation:(NSInvocation *)inv { | |
36 | SEL sel = [inv selector]; | |
37 | if (delegate_ != nil && [delegate_ respondsToSelector:sel]) | |
38 | [inv invokeWithTarget:delegate_]; | |
39 | } | |
40 | ||
41 | @end | |
42 | /* }}} */ | |
43 | ||
22f8bed9 JF |
44 | @interface WebView (Cydia) |
45 | - (void) setScriptDebugDelegate:(id)delegate; | |
46 | - (void) _setFormDelegate:(id)delegate; | |
47 | - (void) _setUIKitDelegate:(id)delegate; | |
48 | - (void) setWebMailDelegate:(id)delegate; | |
49 | - (void) _setLayoutInterval:(float)interval; | |
50 | @end | |
51 | ||
374f3d18 JF |
52 | /* Web Scripting {{{ */ |
53 | @interface CydiaObject : NSObject { | |
54 | id indirect_; | |
55 | } | |
56 | ||
57 | - (id) initWithDelegate:(IndirectDelegate *)indirect; | |
58 | @end | |
59 | ||
60 | @implementation CydiaObject | |
61 | ||
62 | - (void) dealloc { | |
63 | [indirect_ release]; | |
64 | [super dealloc]; | |
65 | } | |
66 | ||
67 | - (id) initWithDelegate:(IndirectDelegate *)indirect { | |
68 | if ((self = [super init]) != nil) { | |
69 | indirect_ = [indirect retain]; | |
70 | } return self; | |
71 | } | |
72 | ||
73 | + (NSString *) webScriptNameForSelector:(SEL)selector { | |
74 | if (selector == @selector(getPackageById:)) | |
75 | return @"getPackageById"; | |
76 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) | |
77 | return @"setButtonImage"; | |
78 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
79 | return @"setButtonTitle"; | |
80 | else if (selector == @selector(supports:)) | |
81 | return @"supports"; | |
82 | else if (selector == @selector(du:)) | |
83 | return @"du"; | |
84 | else if (selector == @selector(statfs:)) | |
85 | return @"statfs"; | |
86 | else | |
87 | return nil; | |
88 | } | |
89 | ||
90 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
91 | return [self webScriptNameForSelector:selector] == nil; | |
92 | } | |
93 | ||
94 | - (BOOL) supports:(NSString *)feature { | |
95 | return [feature isEqualToString:@"window.open"]; | |
96 | } | |
97 | ||
98 | - (Package *) getPackageById:(NSString *)id { | |
99 | return [[Database sharedInstance] packageWithName:id]; | |
100 | } | |
101 | ||
102 | - (NSArray *) statfs:(NSString *)path { | |
103 | struct statfs stat; | |
104 | ||
105 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
106 | return nil; | |
107 | ||
108 | return [NSArray arrayWithObjects: | |
109 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
110 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
111 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
112 | nil]; | |
113 | } | |
114 | ||
115 | - (NSNumber *) du:(NSString *)path { | |
116 | NSNumber *value(nil); | |
117 | ||
118 | int fds[2]; | |
119 | _assert(pipe(fds) != -1); | |
120 | ||
121 | pid_t pid(ExecFork()); | |
122 | if (pid == 0) { | |
123 | _assert(dup2(fds[1], 1) != -1); | |
124 | _assert(close(fds[0]) != -1); | |
125 | _assert(close(fds[1]) != -1); | |
126 | execlp("du", "du", "-s", [path UTF8String], NULL); | |
127 | exit(1); | |
128 | _assert(false); | |
129 | } | |
130 | ||
131 | _assert(close(fds[1]) != -1); | |
132 | ||
133 | if (FILE *du = fdopen(fds[0], "r")) { | |
134 | char line[1024]; | |
135 | while (fgets(line, sizeof(line), du) != NULL) { | |
136 | size_t length(strlen(line)); | |
137 | while (length != 0 && line[length - 1] == '\n') | |
138 | line[--length] = '\0'; | |
139 | if (char *tab = strchr(line, '\t')) { | |
140 | *tab = '\0'; | |
141 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
142 | } | |
143 | } | |
144 | ||
145 | fclose(du); | |
146 | } else _assert(close(fds[0])); | |
147 | ||
148 | int status; | |
149 | wait: | |
150 | if (waitpid(pid, &status, 0) == -1) | |
151 | if (errno == EINTR) | |
152 | goto wait; | |
153 | else _assert(false); | |
154 | ||
155 | return value; | |
156 | } | |
157 | ||
158 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
159 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
160 | } | |
161 | ||
162 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
163 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
164 | } | |
165 | ||
166 | @end | |
167 | /* }}} */ | |
168 | ||
22f8bed9 JF |
169 | @implementation BrowserView |
170 | ||
1b1b3f4a | 171 | #if ShowInternals |
22f8bed9 JF |
172 | #include "Internals.h" |
173 | #endif | |
174 | ||
175 | - (void) dealloc { | |
1b1b3f4a JF |
176 | NSLog(@"deallocating WebView"); |
177 | ||
22f8bed9 JF |
178 | if (challenge_ != nil) |
179 | [challenge_ release]; | |
180 | ||
181 | WebView *webview = [webview_ webView]; | |
182 | [webview setFrameLoadDelegate:nil]; | |
183 | [webview setResourceLoadDelegate:nil]; | |
184 | [webview setUIDelegate:nil]; | |
185 | [webview setScriptDebugDelegate:nil]; | |
186 | [webview setPolicyDelegate:nil]; | |
187 | ||
188 | [webview setDownloadDelegate:nil]; | |
189 | ||
190 | [webview _setFormDelegate:nil]; | |
191 | [webview _setUIKitDelegate:nil]; | |
192 | [webview setWebMailDelegate:nil]; | |
193 | [webview setEditingDelegate:nil]; | |
194 | ||
195 | [webview_ setDelegate:nil]; | |
196 | [webview_ setGestureDelegate:nil]; | |
197 | ||
198 | //NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
199 | ||
200 | [webview close]; | |
201 | ||
202 | #if RecycleWebViews | |
203 | [webview_ removeFromSuperview]; | |
204 | [Documents_ addObject:[webview_ autorelease]]; | |
205 | #else | |
206 | [webview_ release]; | |
207 | #endif | |
208 | ||
209 | [indirect_ setDelegate:nil]; | |
210 | [indirect_ release]; | |
211 | ||
374f3d18 JF |
212 | [cydia_ release]; |
213 | ||
22f8bed9 JF |
214 | [scroller_ setDelegate:nil]; |
215 | ||
216 | if (button_ != nil) | |
217 | [button_ release]; | |
218 | if (style_ != nil) | |
219 | [style_ release]; | |
220 | if (function_ != nil) | |
221 | [function_ release]; | |
222 | ||
223 | [scroller_ release]; | |
224 | [indicator_ release]; | |
225 | if (confirm_ != nil) | |
226 | [confirm_ release]; | |
227 | if (title_ != nil) | |
228 | [title_ release]; | |
229 | [super dealloc]; | |
230 | } | |
231 | ||
232 | - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy { | |
233 | [self loadRequest:[NSURLRequest | |
234 | requestWithURL:url | |
235 | cachePolicy:policy | |
236 | timeoutInterval:30.0 | |
237 | ]]; | |
238 | } | |
239 | ||
240 | - (void) loadURL:(NSURL *)url { | |
241 | [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]; | |
242 | } | |
243 | ||
244 | - (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request { | |
245 | NSMutableURLRequest *copy = [request mutableCopy]; | |
246 | ||
247 | if (Machine_ != NULL) | |
248 | [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
249 | if (UniqueID_ != nil) | |
250 | [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
251 | ||
252 | if (Role_ != nil) | |
253 | [copy setValue:Role_ forHTTPHeaderField:@"X-Role"]; | |
254 | ||
255 | return copy; | |
256 | } | |
257 | ||
258 | - (void) loadRequest:(NSURLRequest *)request { | |
259 | pushed_ = true; | |
fe468f45 | 260 | error_ = false; |
22f8bed9 JF |
261 | [webview_ loadRequest:request]; |
262 | } | |
263 | ||
264 | - (void) reloadURL { | |
22f8bed9 JF |
265 | if (request_ == nil) |
266 | return; | |
267 | ||
268 | if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil) | |
fe468f45 | 269 | [self loadRequest:request_]; |
22f8bed9 JF |
270 | else { |
271 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
272 | initWithTitle:@"Are you sure you want to submit this form again?" | |
273 | buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil] | |
274 | defaultButtonIndex:0 | |
275 | delegate:self | |
276 | context:@"submit" | |
277 | ] autorelease]; | |
278 | ||
279 | [sheet setNumberOfRows:1]; | |
280 | [sheet popupAlertAnimated:YES]; | |
281 | } | |
282 | } | |
283 | ||
284 | - (WebView *) webView { | |
285 | return [webview_ webView]; | |
286 | } | |
287 | ||
1b1b3f4a JF |
288 | - (UIWebDocumentView *) documentView { |
289 | return webview_; | |
290 | } | |
291 | ||
22f8bed9 JF |
292 | - (void) view:(UIView *)sender didSetFrame:(CGRect)frame { |
293 | [scroller_ setContentSize:frame.size]; | |
294 | } | |
295 | ||
296 | - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old { | |
297 | [self view:sender didSetFrame:frame]; | |
298 | } | |
299 | ||
300 | - (void) pushPage:(RVPage *)page { | |
22f8bed9 | 301 | [page setDelegate:delegate_]; |
988f99ac | 302 | [self setBackButtonTitle:title_]; |
1b1b3f4a | 303 | [book_ pushPage:page]; |
22f8bed9 JF |
304 | } |
305 | ||
306 | - (BOOL) getSpecial:(NSURL *)url { | |
307 | NSString *href([url absoluteString]); | |
308 | NSString *scheme([[url scheme] lowercaseString]); | |
309 | ||
310 | RVPage *page = nil; | |
311 | ||
312 | if ([href hasPrefix:@"apptapp://package/"]) | |
313 | page = [delegate_ pageForPackage:[href substringFromIndex:18]]; | |
314 | else if ([scheme isEqualToString:@"cydia"]) { | |
315 | page = [delegate_ pageForURL:url hasTag:NULL]; | |
316 | if (page == nil) | |
317 | return false; | |
318 | } else if (![scheme isEqualToString:@"apptapp"]) | |
319 | return false; | |
320 | ||
321 | if (page != nil) | |
322 | [self pushPage:page]; | |
323 | return true; | |
324 | } | |
325 | ||
1b1b3f4a JF |
326 | - (void) webViewShow:(WebView *)sender { |
327 | } | |
328 | ||
22f8bed9 JF |
329 | - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { |
330 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
331 | initWithTitle:nil | |
332 | buttons:[NSArray arrayWithObjects:@"OK", nil] | |
333 | defaultButtonIndex:0 | |
334 | delegate:self | |
335 | context:@"alert" | |
336 | ] autorelease]; | |
337 | ||
338 | [sheet setBodyText:message]; | |
339 | [sheet popupAlertAnimated:YES]; | |
340 | } | |
341 | ||
342 | - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { | |
343 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
344 | initWithTitle:nil | |
345 | buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil] | |
346 | defaultButtonIndex:0 | |
347 | delegate:self | |
348 | context:@"confirm" | |
349 | ] autorelease]; | |
350 | ||
351 | [sheet setNumberOfRows:1]; | |
352 | [sheet setBodyText:message]; | |
353 | [sheet popupAlertAnimated:YES]; | |
354 | ||
355 | NSRunLoop *loop([NSRunLoop currentRunLoop]); | |
356 | NSDate *future([NSDate distantFuture]); | |
357 | ||
358 | while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]); | |
359 | ||
360 | NSNumber *confirm([confirm_ autorelease]); | |
361 | confirm_ = nil; | |
362 | return [confirm boolValue]; | |
363 | } | |
364 | ||
22f8bed9 JF |
365 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
366 | if (button_ != nil) | |
367 | [button_ autorelease]; | |
368 | button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain]; | |
369 | ||
370 | if (style_ != nil) | |
371 | [style_ autorelease]; | |
372 | style_ = style == nil ? nil : [style retain]; | |
373 | ||
374 | if (function_ != nil) | |
375 | [function_ autorelease]; | |
376 | function_ = function == nil ? nil : [function retain]; | |
377 | } | |
378 | ||
379 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
380 | if (button_ != nil) | |
381 | [button_ autorelease]; | |
382 | button_ = button == nil ? nil : [button retain]; | |
383 | ||
384 | if (style_ != nil) | |
385 | [style_ autorelease]; | |
386 | style_ = style == nil ? nil : [style retain]; | |
387 | ||
388 | if (function_ != nil) | |
389 | [function_ autorelease]; | |
390 | function_ = function == nil ? nil : [function retain]; | |
391 | } | |
22f8bed9 | 392 | |
aa5d0de7 JF |
393 | - (void) webViewClose:(WebView *)sender { |
394 | [book_ close]; | |
395 | } | |
396 | ||
22f8bed9 | 397 | - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { |
374f3d18 | 398 | [window setValue:cydia_ forKey:@"cydia"]; |
22f8bed9 JF |
399 | } |
400 | ||
401 | - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame { | |
402 | NSLog(@"err:%@", error); | |
403 | } | |
404 | ||
405 | - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener { | |
1b1b3f4a JF |
406 | NSLog(@"nwa:%@", name); |
407 | ||
22f8bed9 | 408 | if (NSURL *url = [request URL]) { |
aa5d0de7 | 409 | if (name == nil) unknown: { |
aa5d0de7 JF |
410 | if (![self getSpecial:url]) { |
411 | NSString *scheme([[url scheme] lowercaseString]); | |
412 | if ([scheme isEqualToString:@"mailto"]) | |
413 | [delegate_ openMailToURL:url]; | |
414 | else goto use; | |
415 | } | |
416 | } else if ([name isEqualToString:@"_open"]) | |
22f8bed9 | 417 | [delegate_ openURL:url]; |
aa5d0de7 JF |
418 | else if ([name isEqualToString:@"_popup"]) { |
419 | RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]); | |
aa5d0de7 JF |
420 | |
421 | RVPage *page([delegate_ pageForURL:url hasTag:NULL]); | |
422 | if (page == nil) { | |
1b1b3f4a JF |
423 | /* XXX: call createWebViewWithRequest instead */ |
424 | ||
425 | [self setBackButtonTitle:title_]; | |
426 | ||
aa5d0de7 JF |
427 | BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]); |
428 | [browser loadURL:url]; | |
429 | page = browser; | |
430 | } | |
22f8bed9 | 431 | |
00e2109e JF |
432 | [book setDelegate:delegate_]; |
433 | [page setDelegate:delegate_]; | |
434 | ||
aa5d0de7 JF |
435 | [book setPage:page]; |
436 | [book_ pushBook:book]; | |
437 | } else goto unknown; | |
22f8bed9 JF |
438 | |
439 | [listener ignore]; | |
440 | } else use: | |
441 | [listener use]; | |
442 | } | |
443 | ||
444 | - (void) webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
445 | if ([WebView canShowMIMEType:type]) | |
446 | [listener use]; | |
447 | else { | |
448 | // XXX: handle more mime types! | |
449 | [listener ignore]; | |
450 | if (frame == [webView mainFrame]) | |
451 | [UIApp openURL:[request URL]]; | |
452 | } | |
453 | } | |
454 | ||
455 | - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
456 | if (request == nil) ignore: { | |
457 | [listener ignore]; | |
458 | return; | |
459 | } | |
460 | ||
461 | NSURL *url([request URL]); | |
462 | ||
463 | if (url == nil) use: { | |
fe468f45 | 464 | if (!error_ && [frame parentFrame] == nil) { |
22f8bed9 JF |
465 | if (request_ != nil) |
466 | [request_ autorelease]; | |
467 | request_ = [request retain]; | |
aa5d0de7 | 468 | #if ForSaurik |
22f8bed9 | 469 | NSLog(@"dpn:%@", request_); |
aa5d0de7 | 470 | #endif |
22f8bed9 JF |
471 | } |
472 | ||
473 | [listener use]; | |
474 | return; | |
475 | } | |
476 | #if ForSaurik | |
477 | else NSLog(@"nav:%@:%@", url, [action description]); | |
478 | #endif | |
479 | ||
480 | const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability))); | |
481 | ||
482 | if ( | |
483 | [capability containsObject:@"com.apple.Maps"] && [url mapsURL] || | |
484 | [capability containsObject:@"com.apple.youtube"] && [url youTubeURL] | |
485 | ) { | |
486 | open: | |
487 | [UIApp openURL:url]; | |
488 | goto ignore; | |
489 | } | |
490 | ||
491 | int store(_not(int)); | |
492 | if (NSURL *itms = [url itmsURL:&store]) { | |
493 | NSLog(@"itms#%@#%u#%@", url, store, itms); | |
494 | if ( | |
495 | store == 1 && [capability containsObject:@"com.apple.MobileStore"] || | |
496 | store == 2 && [capability containsObject:@"com.apple.AppStore"] | |
497 | ) { | |
498 | url = itms; | |
499 | goto open; | |
500 | } | |
501 | } | |
502 | ||
503 | NSString *scheme([[url scheme] lowercaseString]); | |
504 | ||
505 | if ([scheme isEqualToString:@"tel"]) { | |
506 | // XXX: intelligence | |
507 | goto open; | |
508 | } | |
509 | ||
510 | if ([scheme isEqualToString:@"mailto"]) { | |
511 | [delegate_ openMailToURL:url]; | |
512 | goto ignore; | |
513 | } | |
514 | ||
515 | if ([self getSpecial:url]) | |
516 | goto ignore; | |
517 | else if ([WebView _canHandleRequest:request]) | |
518 | goto use; | |
519 | else if ([url isSpringboardHandledURL]) | |
520 | goto open; | |
521 | else | |
522 | goto use; | |
523 | } | |
524 | ||
525 | - (void) webView:(WebView *)sender setStatusText:(NSString *)text { | |
526 | //lprintf("Status:%s\n", [text UTF8String]); | |
527 | } | |
528 | ||
529 | - (void) _pushPage { | |
530 | if (pushed_) | |
531 | return; | |
532 | pushed_ = true; | |
533 | [book_ pushPage:self]; | |
534 | } | |
535 | ||
536 | - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button { | |
537 | NSString *context([sheet context]); | |
538 | ||
539 | if ([context isEqualToString:@"alert"]) | |
540 | [sheet dismiss]; | |
541 | else if ([context isEqualToString:@"confirm"]) { | |
542 | switch (button) { | |
543 | case 1: | |
544 | confirm_ = [NSNumber numberWithBool:YES]; | |
545 | break; | |
546 | ||
547 | case 2: | |
548 | confirm_ = [NSNumber numberWithBool:NO]; | |
549 | break; | |
550 | } | |
551 | ||
552 | [sheet dismiss]; | |
553 | } else if ([context isEqualToString:@"challenge"]) { | |
554 | id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]); | |
555 | ||
556 | switch (button) { | |
557 | case 1: { | |
558 | NSString *username([[sheet textFieldAtIndex:0] text]); | |
559 | NSString *password([[sheet textFieldAtIndex:1] text]); | |
560 | ||
561 | NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]); | |
562 | ||
563 | [sender useCredential:credential forAuthenticationChallenge:challenge_]; | |
564 | } break; | |
565 | ||
566 | case 2: | |
567 | [sender cancelAuthenticationChallenge:challenge_]; | |
568 | break; | |
569 | ||
570 | default: | |
571 | _assert(false); | |
572 | } | |
573 | ||
574 | [challenge_ release]; | |
575 | challenge_ = nil; | |
576 | ||
577 | [sheet dismiss]; | |
578 | } else if ([context isEqualToString:@"submit"]) { | |
579 | switch (button) { | |
580 | case 1: | |
581 | break; | |
582 | ||
583 | case 2: | |
584 | if (request_ != nil) | |
585 | [webview_ loadRequest:request_]; | |
586 | break; | |
587 | ||
588 | default: | |
589 | _assert(false); | |
590 | } | |
591 | ||
592 | [sheet dismiss]; | |
593 | } | |
594 | } | |
595 | ||
596 | - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source { | |
597 | challenge_ = [challenge retain]; | |
598 | ||
599 | NSURLProtectionSpace *space([challenge protectionSpace]); | |
600 | NSString *realm([space realm]); | |
601 | if (realm == nil) | |
602 | realm = @""; | |
603 | ||
604 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
605 | initWithTitle:realm | |
606 | buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil] | |
607 | defaultButtonIndex:0 | |
608 | delegate:self | |
609 | context:@"challenge" | |
610 | ] autorelease]; | |
611 | ||
612 | [sheet setNumberOfRows:1]; | |
613 | ||
614 | [sheet addTextFieldWithValue:@"" label:@"username"]; | |
615 | [sheet addTextFieldWithValue:@"" label:@"password"]; | |
616 | ||
617 | UITextField *username([sheet textFieldAtIndex:0]); { | |
618 | UITextInputTraits *traits([username textInputTraits]); | |
619 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
620 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
621 | [traits setKeyboardType:UIKeyboardTypeASCIICapable]; | |
622 | [traits setReturnKeyType:UIReturnKeyNext]; | |
623 | } | |
624 | ||
625 | UITextField *password([sheet textFieldAtIndex:1]); { | |
626 | UITextInputTraits *traits([password textInputTraits]); | |
627 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
628 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
629 | [traits setKeyboardType:UIKeyboardTypeASCIICapable]; | |
630 | // XXX: UIReturnKeyDone | |
631 | [traits setReturnKeyType:UIReturnKeyNext]; | |
632 | [traits setSecureTextEntry:YES]; | |
633 | } | |
634 | ||
635 | [sheet popupAlertAnimated:YES]; | |
636 | } | |
637 | ||
638 | - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source { | |
639 | NSURL *url = [request URL]; | |
640 | if ([self getSpecial:url]) | |
641 | return nil; | |
642 | [self _pushPage]; | |
643 | return [self _addHeadersToRequest:request]; | |
644 | } | |
645 | ||
1b1b3f4a JF |
646 | - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features { |
647 | #if ForSaurik | |
648 | NSLog(@"cwv:%@ (%@)", request, title_); | |
649 | #endif | |
22f8bed9 JF |
650 | |
651 | BrowserView *browser = [[[BrowserView alloc] initWithBook:book_] autorelease]; | |
1b1b3f4a JF |
652 | [self pushPage:browser]; |
653 | [browser loadRequest:request]; | |
22f8bed9 JF |
654 | return [browser webView]; |
655 | } | |
656 | ||
657 | - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request { | |
1b1b3f4a | 658 | return [self webView:sender createWebViewWithRequest:request windowFeatures:nil]; |
22f8bed9 JF |
659 | } |
660 | ||
661 | - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { | |
662 | if ([frame parentFrame] != nil) | |
663 | return; | |
664 | ||
665 | title_ = [title retain]; | |
666 | [book_ reloadTitleForPage:self]; | |
667 | } | |
668 | ||
669 | - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame { | |
670 | if ([frame parentFrame] != nil) | |
671 | return; | |
672 | ||
673 | reloading_ = false; | |
674 | loading_ = true; | |
675 | [self reloadButtons]; | |
676 | ||
677 | if (title_ != nil) { | |
678 | [title_ release]; | |
679 | title_ = nil; | |
680 | } | |
681 | ||
682 | if (button_ != nil) { | |
683 | [button_ release]; | |
684 | button_ = nil; | |
685 | } | |
686 | ||
687 | if (style_ != nil) { | |
688 | [style_ release]; | |
689 | style_ = nil; | |
690 | } | |
691 | ||
692 | if (function_ != nil) { | |
693 | [function_ release]; | |
694 | function_ = nil; | |
695 | } | |
696 | ||
697 | [book_ reloadTitleForPage:self]; | |
698 | ||
699 | [scroller_ scrollPointVisibleAtTopLeft:CGPointZero]; | |
700 | ||
701 | CGRect webrect = [scroller_ bounds]; | |
702 | webrect.size.height = 0; | |
703 | [webview_ setFrame:webrect]; | |
704 | } | |
705 | ||
706 | - (void) _finishLoading { | |
707 | if (!reloading_) { | |
708 | loading_ = false; | |
709 | [self reloadButtons]; | |
710 | } | |
711 | } | |
712 | ||
713 | - (bool) _loading { | |
714 | return loading_; | |
715 | } | |
716 | ||
717 | - (void) reloadButtons { | |
718 | if ([self _loading]) | |
719 | [indicator_ startAnimation]; | |
720 | else | |
721 | [indicator_ stopAnimation]; | |
722 | [super reloadButtons]; | |
723 | } | |
724 | ||
725 | - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame { | |
726 | return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame]; | |
727 | } | |
728 | ||
729 | - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame { | |
730 | return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame]; | |
731 | } | |
732 | ||
733 | - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame { | |
734 | return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame]; | |
735 | } | |
736 | ||
737 | - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame { | |
738 | return [webview_ webView:sender didCommitLoadForFrame:frame]; | |
739 | } | |
740 | ||
741 | - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame { | |
742 | return [webview_ webView:sender didReceiveDocTypeForFrame:frame]; | |
743 | } | |
744 | ||
745 | - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { | |
746 | if ([frame parentFrame] == nil) { | |
747 | [self _finishLoading]; | |
748 | ||
749 | if (DOMDocument *document = [frame DOMDocument]) | |
750 | if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"]) | |
751 | for (DOMHTMLBodyElement *body in bodies) { | |
752 | DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]); | |
753 | ||
754 | bool colored(false); | |
755 | ||
756 | if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) { | |
fe468f45 JF |
757 | if ([color primitiveType] == DOM_CSS_RGBCOLOR) { |
758 | DOMRGBColor *rgb([color getRGBColorValue]); | |
759 | ||
760 | float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]); | |
761 | float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]); | |
762 | float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]); | |
763 | float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]); | |
764 | ||
765 | UIColor *uic(nil); | |
766 | ||
767 | if (red == 0xc7 && green == 0xce && blue == 0xd5) | |
768 | uic = [UIColor pinStripeColor]; | |
769 | else if (alpha != 0) | |
770 | uic = [UIColor | |
771 | colorWithRed:(red / 255) | |
772 | green:(green / 255) | |
773 | blue:(blue / 255) | |
774 | alpha:alpha | |
775 | ]; | |
776 | ||
777 | if (uic != nil) { | |
778 | colored = true; | |
779 | [scroller_ setBackgroundColor:uic]; | |
780 | } | |
22f8bed9 JF |
781 | } |
782 | } | |
783 | ||
784 | if (!colored) | |
785 | [scroller_ setBackgroundColor:[UIColor pinStripeColor]]; | |
786 | break; | |
787 | } | |
788 | } | |
789 | ||
790 | return [webview_ webView:sender didFinishLoadForFrame:frame]; | |
791 | } | |
792 | ||
793 | - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { | |
794 | if ([frame parentFrame] != nil) | |
795 | return; | |
988f99ac JF |
796 | if (reloading_) |
797 | return; | |
22f8bed9 JF |
798 | [self _finishLoading]; |
799 | ||
800 | [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", | |
801 | [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString], | |
802 | [[error localizedDescription] stringByAddingPercentEscapes] | |
803 | ]]]; | |
fe468f45 JF |
804 | |
805 | error_ = true; | |
22f8bed9 JF |
806 | } |
807 | ||
808 | - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary { | |
809 | #if ForSaurik | |
810 | lprintf("Console:%s\n", [[dictionary description] UTF8String]); | |
811 | #endif | |
812 | } | |
813 | ||
814 | - (id) initWithBook:(RVBook *)book { | |
815 | if ((self = [super initWithBook:book]) != nil) { | |
816 | loading_ = false; | |
817 | ||
818 | struct CGRect bounds = [self bounds]; | |
819 | ||
820 | scroller_ = [[UIScroller alloc] initWithFrame:bounds]; | |
821 | [self addSubview:scroller_]; | |
822 | ||
823 | [scroller_ setShowBackgroundShadow:NO]; | |
824 | [scroller_ setFixedBackgroundPattern:YES]; | |
825 | [scroller_ setBackgroundColor:[UIColor pinStripeColor]]; | |
826 | ||
827 | [scroller_ setScrollingEnabled:YES]; | |
828 | [scroller_ setAdjustForContentSizeChange:YES]; | |
829 | [scroller_ setClipsSubviews:YES]; | |
830 | [scroller_ setAllowsRubberBanding:YES]; | |
831 | [scroller_ setScrollDecelerationFactor:0.99]; | |
832 | [scroller_ setDelegate:self]; | |
833 | ||
834 | CGRect webrect = [scroller_ bounds]; | |
835 | webrect.size.height = 0; | |
836 | ||
837 | WebView *webview; | |
838 | ||
839 | #if RecycleWebViews | |
840 | webview_ = [Documents_ lastObject]; | |
841 | if (webview_ != nil) { | |
842 | webview_ = [webview_ retain]; | |
843 | webview = [webview_ webView]; | |
844 | [Documents_ removeLastObject]; | |
845 | [webview_ setFrame:webrect]; | |
846 | } else { | |
847 | #else | |
848 | if (true) { | |
849 | #endif | |
850 | webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect]; | |
851 | webview = [webview_ webView]; | |
852 | ||
853 | // XXX: this is terribly (too?) expensive | |
854 | //[webview_ setDrawsBackground:NO]; | |
855 | [webview setPreferencesIdentifier:@"Cydia"]; | |
856 | ||
857 | [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)]; | |
858 | ||
859 | [webview_ setAllowsMessaging:YES]; | |
860 | ||
861 | [webview_ setTilingEnabled:YES]; | |
862 | [webview_ setDrawsGrid:NO]; | |
863 | [webview_ setLogsTilingChanges:NO]; | |
864 | [webview_ setTileMinificationFilter:kCAFilterNearest]; | |
865 | [webview_ setDetectsPhoneNumbers:NO]; | |
866 | [webview_ setAutoresizes:YES]; | |
867 | ||
868 | [webview_ setMinimumScale:0.25f forDocumentTypes:0x10]; | |
869 | [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10]; | |
870 | [webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; | |
871 | ||
872 | [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2]; | |
873 | ||
874 | [webview_ setMinimumScale:1.0f forDocumentTypes:0x8]; | |
875 | [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8]; | |
876 | [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8]; | |
877 | ||
878 | [webview_ _setDocumentType:0x4]; | |
879 | ||
880 | [webview_ setZoomsFocusedFormControl:YES]; | |
881 | [webview_ setContentsPosition:7]; | |
882 | [webview_ setEnabledGestures:0xa]; | |
883 | [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled]; | |
884 | [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller]; | |
885 | ||
886 | [webview_ setSmoothsFonts:YES]; | |
887 | ||
888 | [webview _setUsesLoaderCache:YES]; | |
889 | [webview setGroupName:@"Cydia"]; | |
890 | [webview _setLayoutInterval:0]; | |
891 | } | |
892 | ||
893 | [webview_ setDelegate:self]; | |
894 | [webview_ setGestureDelegate:self]; | |
895 | [scroller_ addSubview:webview_]; | |
896 | ||
897 | //NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
898 | ||
899 | CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]; | |
900 | indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)]; | |
901 | [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite]; | |
902 | ||
903 | Package *package([[Database sharedInstance] packageWithName:@"cydia"]); | |
904 | NSString *application = package == nil ? @"Cydia" : [NSString | |
905 | stringWithFormat:@"Cydia/%@", | |
906 | [package installed] | |
ad5d065e JF |
907 | ]; |
908 | ||
909 | if (Build_ != nil) | |
910 | application = [NSString stringWithFormat:@"Mobile/%@ %@", Build_, application]; | |
911 | ||
912 | /* XXX: lookup application directory? */ | |
913 | /*if (NSDictionary *safari = [NSDictionary dictionaryWithContentsOfFile:@"/Applications/MobileSafari.app/Info.plist"]) | |
914 | if (NSString *version = [safari objectForKey:@"SafariProductVersion"]) | |
915 | application = [NSString stringWithFormat:@"Version/%@ %@", version, application];*/ | |
916 | ||
917 | [webview setApplicationNameForUserAgent:application]; | |
22f8bed9 JF |
918 | |
919 | indirect_ = [[IndirectDelegate alloc] initWithDelegate:self]; | |
374f3d18 | 920 | cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_]; |
22f8bed9 JF |
921 | |
922 | [webview setFrameLoadDelegate:self]; | |
923 | [webview setResourceLoadDelegate:indirect_]; | |
924 | [webview setUIDelegate:self]; | |
925 | [webview setScriptDebugDelegate:self]; | |
926 | [webview setPolicyDelegate:self]; | |
927 | ||
928 | [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; | |
929 | [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; | |
930 | } return self; | |
931 | } | |
932 | ||
933 | - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event { | |
934 | [webview_ redrawScaledDocument]; | |
935 | } | |
936 | ||
937 | - (void) _rightButtonClicked { | |
938 | if (function_ == nil) { | |
939 | reloading_ = true; | |
940 | [self reloadURL]; | |
941 | } else { | |
942 | WebView *webview([webview_ webView]); | |
943 | WebFrame *frame([webview mainFrame]); | |
944 | ||
945 | id _private(MSHookIvar<id>(webview, "_private")); | |
946 | WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page")); | |
947 | WebCore::Settings *settings(page == NULL ? NULL : page->settings()); | |
948 | ||
949 | bool no; | |
950 | if (settings == NULL) | |
951 | no = 0; | |
952 | else { | |
953 | no = settings->JavaScriptCanOpenWindowsAutomatically(); | |
954 | settings->setJavaScriptCanOpenWindowsAutomatically(true); | |
955 | } | |
956 | ||
957 | [delegate_ clearFirstResponder]; | |
958 | JSObjectRef function([function_ JSObject]); | |
959 | JSGlobalContextRef context([frame globalContext]); | |
960 | JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); | |
961 | ||
962 | if (settings != NULL) | |
963 | settings->setJavaScriptCanOpenWindowsAutomatically(no); | |
964 | } | |
965 | } | |
966 | ||
967 | - (id) _rightButtonTitle { | |
968 | return button_ != nil ? button_ : @"Reload"; | |
969 | } | |
970 | ||
971 | - (id) rightButtonTitle { | |
972 | return [self _loading] ? @"" : [self _rightButtonTitle]; | |
973 | } | |
974 | ||
975 | - (UINavigationButtonStyle) rightButtonStyle { | |
976 | if (style_ == nil) normal: | |
977 | return UINavigationButtonStyleNormal; | |
978 | else if ([style_ isEqualToString:@"Normal"]) | |
979 | return UINavigationButtonStyleNormal; | |
980 | else if ([style_ isEqualToString:@"Back"]) | |
981 | return UINavigationButtonStyleBack; | |
982 | else if ([style_ isEqualToString:@"Highlighted"]) | |
983 | return UINavigationButtonStyleHighlighted; | |
984 | else if ([style_ isEqualToString:@"Destructive"]) | |
985 | return UINavigationButtonStyleDestructive; | |
986 | else goto normal; | |
987 | } | |
988 | ||
989 | - (NSString *) title { | |
990 | return title_ == nil ? @"Loading" : title_; | |
991 | } | |
992 | ||
993 | - (NSString *) backButtonTitle { | |
994 | return @"Browser"; | |
995 | } | |
996 | ||
997 | - (void) setPageActive:(BOOL)active { | |
998 | if (!active) | |
999 | [indicator_ removeFromSuperview]; | |
1000 | else | |
1001 | [[book_ navigationBar] addSubview:indicator_]; | |
1002 | } | |
1003 | ||
1004 | - (void) resetViewAnimated:(BOOL)animated { | |
1005 | } | |
1006 | ||
1007 | - (void) setPushed:(bool)pushed { | |
1008 | pushed_ = pushed; | |
1009 | } | |
1010 | ||
1011 | @end |