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