]>
Commit | Line | Data |
---|---|---|
22f8bed9 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; | |
fe468f45 | 98 | error_ = false; |
22f8bed9 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) | |
fe468f45 | 108 | [self loadRequest:request_]; |
22f8bed9 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"; | |
59c011d8 JF |
207 | else if (selector == @selector(du:)) |
208 | return @"du"; | |
209 | else if (selector == @selector(statfs:)) | |
210 | return @"statfs"; | |
22f8bed9 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 | ||
59c011d8 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 | ||
243 | /* XXX: omfg this is stupid */ | |
244 | if (FILE *du = popen([[@"du -s " stringByAppendingString:path] UTF8String], "r")) { | |
245 | char line[1024]; | |
246 | while (fgets(line, sizeof(line), du) != NULL) { | |
247 | size_t length(strlen(line)); | |
248 | while (length != 0 && line[length - 1] == '\n') | |
249 | line[--length] = '\0'; | |
250 | if (char *tab = strchr(line, '\t')) { | |
251 | *tab = '\0'; | |
252 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
253 | } | |
254 | } | |
255 | ||
256 | pclose(du); | |
257 | } | |
258 | ||
259 | return value; | |
260 | } | |
261 | ||
22f8bed9 JF |
262 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { |
263 | if (button_ != nil) | |
264 | [button_ autorelease]; | |
265 | button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain]; | |
266 | ||
267 | if (style_ != nil) | |
268 | [style_ autorelease]; | |
269 | style_ = style == nil ? nil : [style retain]; | |
270 | ||
271 | if (function_ != nil) | |
272 | [function_ autorelease]; | |
273 | function_ = function == nil ? nil : [function retain]; | |
274 | } | |
275 | ||
276 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
277 | if (button_ != nil) | |
278 | [button_ autorelease]; | |
279 | button_ = button == nil ? nil : [button retain]; | |
280 | ||
281 | if (style_ != nil) | |
282 | [style_ autorelease]; | |
283 | style_ = style == nil ? nil : [style retain]; | |
284 | ||
285 | if (function_ != nil) | |
286 | [function_ autorelease]; | |
287 | function_ = function == nil ? nil : [function retain]; | |
288 | } | |
289 | /* }}} */ | |
290 | ||
291 | - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { | |
292 | [window setValue:self forKey:@"cydia"]; | |
293 | } | |
294 | ||
295 | - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame { | |
296 | NSLog(@"err:%@", error); | |
297 | } | |
298 | ||
299 | - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener { | |
300 | if (NSURL *url = [request URL]) { | |
301 | if (name != nil && [name isEqualToString:@"_open"]) | |
302 | [delegate_ openURL:url]; | |
303 | ||
304 | NSLog(@"win:%@:%@", url, [action description]); | |
305 | if (![self getSpecial:url]) { | |
306 | NSString *scheme([[url scheme] lowercaseString]); | |
307 | if ([scheme isEqualToString:@"mailto"]) | |
308 | [delegate_ openMailToURL:url]; | |
309 | else goto use; | |
310 | } | |
311 | ||
312 | [listener ignore]; | |
313 | } else use: | |
314 | [listener use]; | |
315 | } | |
316 | ||
317 | - (void) webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
318 | if ([WebView canShowMIMEType:type]) | |
319 | [listener use]; | |
320 | else { | |
321 | // XXX: handle more mime types! | |
322 | [listener ignore]; | |
323 | if (frame == [webView mainFrame]) | |
324 | [UIApp openURL:[request URL]]; | |
325 | } | |
326 | } | |
327 | ||
328 | - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
329 | if (request == nil) ignore: { | |
330 | [listener ignore]; | |
331 | return; | |
332 | } | |
333 | ||
334 | NSURL *url([request URL]); | |
335 | ||
336 | if (url == nil) use: { | |
fe468f45 | 337 | if (!error_ && [frame parentFrame] == nil) { |
22f8bed9 JF |
338 | if (request_ != nil) |
339 | [request_ autorelease]; | |
340 | request_ = [request retain]; | |
341 | NSLog(@"dpn:%@", request_); | |
342 | } | |
343 | ||
344 | [listener use]; | |
345 | return; | |
346 | } | |
347 | #if ForSaurik | |
348 | else NSLog(@"nav:%@:%@", url, [action description]); | |
349 | #endif | |
350 | ||
351 | const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability))); | |
352 | ||
353 | if ( | |
354 | [capability containsObject:@"com.apple.Maps"] && [url mapsURL] || | |
355 | [capability containsObject:@"com.apple.youtube"] && [url youTubeURL] | |
356 | ) { | |
357 | open: | |
358 | [UIApp openURL:url]; | |
359 | goto ignore; | |
360 | } | |
361 | ||
362 | int store(_not(int)); | |
363 | if (NSURL *itms = [url itmsURL:&store]) { | |
364 | NSLog(@"itms#%@#%u#%@", url, store, itms); | |
365 | if ( | |
366 | store == 1 && [capability containsObject:@"com.apple.MobileStore"] || | |
367 | store == 2 && [capability containsObject:@"com.apple.AppStore"] | |
368 | ) { | |
369 | url = itms; | |
370 | goto open; | |
371 | } | |
372 | } | |
373 | ||
374 | NSString *scheme([[url scheme] lowercaseString]); | |
375 | ||
376 | if ([scheme isEqualToString:@"tel"]) { | |
377 | // XXX: intelligence | |
378 | goto open; | |
379 | } | |
380 | ||
381 | if ([scheme isEqualToString:@"mailto"]) { | |
382 | [delegate_ openMailToURL:url]; | |
383 | goto ignore; | |
384 | } | |
385 | ||
386 | if ([self getSpecial:url]) | |
387 | goto ignore; | |
388 | else if ([WebView _canHandleRequest:request]) | |
389 | goto use; | |
390 | else if ([url isSpringboardHandledURL]) | |
391 | goto open; | |
392 | else | |
393 | goto use; | |
394 | } | |
395 | ||
396 | - (void) webView:(WebView *)sender setStatusText:(NSString *)text { | |
397 | //lprintf("Status:%s\n", [text UTF8String]); | |
398 | } | |
399 | ||
400 | - (void) _pushPage { | |
401 | if (pushed_) | |
402 | return; | |
403 | pushed_ = true; | |
404 | [book_ pushPage:self]; | |
405 | } | |
406 | ||
407 | - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button { | |
408 | NSString *context([sheet context]); | |
409 | ||
410 | if ([context isEqualToString:@"alert"]) | |
411 | [sheet dismiss]; | |
412 | else if ([context isEqualToString:@"confirm"]) { | |
413 | switch (button) { | |
414 | case 1: | |
415 | confirm_ = [NSNumber numberWithBool:YES]; | |
416 | break; | |
417 | ||
418 | case 2: | |
419 | confirm_ = [NSNumber numberWithBool:NO]; | |
420 | break; | |
421 | } | |
422 | ||
423 | [sheet dismiss]; | |
424 | } else if ([context isEqualToString:@"challenge"]) { | |
425 | id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]); | |
426 | ||
427 | switch (button) { | |
428 | case 1: { | |
429 | NSString *username([[sheet textFieldAtIndex:0] text]); | |
430 | NSString *password([[sheet textFieldAtIndex:1] text]); | |
431 | ||
432 | NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]); | |
433 | ||
434 | [sender useCredential:credential forAuthenticationChallenge:challenge_]; | |
435 | } break; | |
436 | ||
437 | case 2: | |
438 | [sender cancelAuthenticationChallenge:challenge_]; | |
439 | break; | |
440 | ||
441 | default: | |
442 | _assert(false); | |
443 | } | |
444 | ||
445 | [challenge_ release]; | |
446 | challenge_ = nil; | |
447 | ||
448 | [sheet dismiss]; | |
449 | } else if ([context isEqualToString:@"submit"]) { | |
450 | switch (button) { | |
451 | case 1: | |
452 | break; | |
453 | ||
454 | case 2: | |
455 | if (request_ != nil) | |
456 | [webview_ loadRequest:request_]; | |
457 | break; | |
458 | ||
459 | default: | |
460 | _assert(false); | |
461 | } | |
462 | ||
463 | [sheet dismiss]; | |
464 | } | |
465 | } | |
466 | ||
467 | - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source { | |
468 | challenge_ = [challenge retain]; | |
469 | ||
470 | NSURLProtectionSpace *space([challenge protectionSpace]); | |
471 | NSString *realm([space realm]); | |
472 | if (realm == nil) | |
473 | realm = @""; | |
474 | ||
475 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
476 | initWithTitle:realm | |
477 | buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil] | |
478 | defaultButtonIndex:0 | |
479 | delegate:self | |
480 | context:@"challenge" | |
481 | ] autorelease]; | |
482 | ||
483 | [sheet setNumberOfRows:1]; | |
484 | ||
485 | [sheet addTextFieldWithValue:@"" label:@"username"]; | |
486 | [sheet addTextFieldWithValue:@"" label:@"password"]; | |
487 | ||
488 | UITextField *username([sheet textFieldAtIndex:0]); { | |
489 | UITextInputTraits *traits([username textInputTraits]); | |
490 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
491 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
492 | [traits setKeyboardType:UIKeyboardTypeASCIICapable]; | |
493 | [traits setReturnKeyType:UIReturnKeyNext]; | |
494 | } | |
495 | ||
496 | UITextField *password([sheet textFieldAtIndex:1]); { | |
497 | UITextInputTraits *traits([password textInputTraits]); | |
498 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
499 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
500 | [traits setKeyboardType:UIKeyboardTypeASCIICapable]; | |
501 | // XXX: UIReturnKeyDone | |
502 | [traits setReturnKeyType:UIReturnKeyNext]; | |
503 | [traits setSecureTextEntry:YES]; | |
504 | } | |
505 | ||
506 | [sheet popupAlertAnimated:YES]; | |
507 | } | |
508 | ||
509 | - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source { | |
510 | NSURL *url = [request URL]; | |
511 | if ([self getSpecial:url]) | |
512 | return nil; | |
513 | [self _pushPage]; | |
514 | return [self _addHeadersToRequest:request]; | |
515 | } | |
516 | ||
517 | - (WebView *) _createWebViewWithRequest:(NSURLRequest *)request pushed:(BOOL)pushed { | |
518 | [self setBackButtonTitle:title_]; | |
519 | ||
520 | BrowserView *browser = [[[BrowserView alloc] initWithBook:book_] autorelease]; | |
521 | [browser setDelegate:delegate_]; | |
522 | ||
523 | if (pushed) { | |
524 | [browser loadRequest:request]; | |
525 | [book_ pushPage:browser]; | |
526 | } | |
527 | ||
528 | return [browser webView]; | |
529 | } | |
530 | ||
531 | - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request { | |
532 | return [self _createWebViewWithRequest:request pushed:(request != nil)]; | |
533 | } | |
534 | ||
535 | - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features { | |
536 | return [self _createWebViewWithRequest:request pushed:YES]; | |
537 | } | |
538 | ||
539 | - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { | |
540 | if ([frame parentFrame] != nil) | |
541 | return; | |
542 | ||
543 | title_ = [title retain]; | |
544 | [book_ reloadTitleForPage:self]; | |
545 | } | |
546 | ||
547 | - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame { | |
548 | if ([frame parentFrame] != nil) | |
549 | return; | |
550 | ||
551 | reloading_ = false; | |
552 | loading_ = true; | |
553 | [self reloadButtons]; | |
554 | ||
555 | if (title_ != nil) { | |
556 | [title_ release]; | |
557 | title_ = nil; | |
558 | } | |
559 | ||
560 | if (button_ != nil) { | |
561 | [button_ release]; | |
562 | button_ = nil; | |
563 | } | |
564 | ||
565 | if (style_ != nil) { | |
566 | [style_ release]; | |
567 | style_ = nil; | |
568 | } | |
569 | ||
570 | if (function_ != nil) { | |
571 | [function_ release]; | |
572 | function_ = nil; | |
573 | } | |
574 | ||
575 | [book_ reloadTitleForPage:self]; | |
576 | ||
577 | [scroller_ scrollPointVisibleAtTopLeft:CGPointZero]; | |
578 | ||
579 | CGRect webrect = [scroller_ bounds]; | |
580 | webrect.size.height = 0; | |
581 | [webview_ setFrame:webrect]; | |
582 | } | |
583 | ||
584 | - (void) _finishLoading { | |
585 | if (!reloading_) { | |
586 | loading_ = false; | |
587 | [self reloadButtons]; | |
588 | } | |
589 | } | |
590 | ||
591 | - (bool) _loading { | |
592 | return loading_; | |
593 | } | |
594 | ||
595 | - (void) reloadButtons { | |
596 | if ([self _loading]) | |
597 | [indicator_ startAnimation]; | |
598 | else | |
599 | [indicator_ stopAnimation]; | |
600 | [super reloadButtons]; | |
601 | } | |
602 | ||
603 | - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame { | |
604 | return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame]; | |
605 | } | |
606 | ||
607 | - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame { | |
608 | return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame]; | |
609 | } | |
610 | ||
611 | - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame { | |
612 | return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame]; | |
613 | } | |
614 | ||
615 | - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame { | |
616 | return [webview_ webView:sender didCommitLoadForFrame:frame]; | |
617 | } | |
618 | ||
619 | - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame { | |
620 | return [webview_ webView:sender didReceiveDocTypeForFrame:frame]; | |
621 | } | |
622 | ||
623 | - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { | |
624 | if ([frame parentFrame] == nil) { | |
625 | [self _finishLoading]; | |
626 | ||
627 | if (DOMDocument *document = [frame DOMDocument]) | |
628 | if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"]) | |
629 | for (DOMHTMLBodyElement *body in bodies) { | |
630 | DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]); | |
631 | ||
632 | bool colored(false); | |
633 | ||
634 | if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) { | |
fe468f45 JF |
635 | if ([color primitiveType] == DOM_CSS_RGBCOLOR) { |
636 | DOMRGBColor *rgb([color getRGBColorValue]); | |
637 | ||
638 | float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]); | |
639 | float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]); | |
640 | float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]); | |
641 | float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]); | |
642 | ||
643 | UIColor *uic(nil); | |
644 | ||
645 | if (red == 0xc7 && green == 0xce && blue == 0xd5) | |
646 | uic = [UIColor pinStripeColor]; | |
647 | else if (alpha != 0) | |
648 | uic = [UIColor | |
649 | colorWithRed:(red / 255) | |
650 | green:(green / 255) | |
651 | blue:(blue / 255) | |
652 | alpha:alpha | |
653 | ]; | |
654 | ||
655 | if (uic != nil) { | |
656 | colored = true; | |
657 | [scroller_ setBackgroundColor:uic]; | |
658 | } | |
22f8bed9 JF |
659 | } |
660 | } | |
661 | ||
662 | if (!colored) | |
663 | [scroller_ setBackgroundColor:[UIColor pinStripeColor]]; | |
664 | break; | |
665 | } | |
666 | } | |
667 | ||
668 | return [webview_ webView:sender didFinishLoadForFrame:frame]; | |
669 | } | |
670 | ||
671 | - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { | |
672 | if ([frame parentFrame] != nil) | |
673 | return; | |
674 | [self _finishLoading]; | |
675 | ||
676 | [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", | |
677 | [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString], | |
678 | [[error localizedDescription] stringByAddingPercentEscapes] | |
679 | ]]]; | |
fe468f45 JF |
680 | |
681 | error_ = true; | |
22f8bed9 JF |
682 | } |
683 | ||
684 | - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary { | |
685 | #if ForSaurik | |
686 | lprintf("Console:%s\n", [[dictionary description] UTF8String]); | |
687 | #endif | |
688 | } | |
689 | ||
690 | - (id) initWithBook:(RVBook *)book { | |
691 | if ((self = [super initWithBook:book]) != nil) { | |
692 | loading_ = false; | |
693 | ||
694 | struct CGRect bounds = [self bounds]; | |
695 | ||
696 | scroller_ = [[UIScroller alloc] initWithFrame:bounds]; | |
697 | [self addSubview:scroller_]; | |
698 | ||
699 | [scroller_ setShowBackgroundShadow:NO]; | |
700 | [scroller_ setFixedBackgroundPattern:YES]; | |
701 | [scroller_ setBackgroundColor:[UIColor pinStripeColor]]; | |
702 | ||
703 | [scroller_ setScrollingEnabled:YES]; | |
704 | [scroller_ setAdjustForContentSizeChange:YES]; | |
705 | [scroller_ setClipsSubviews:YES]; | |
706 | [scroller_ setAllowsRubberBanding:YES]; | |
707 | [scroller_ setScrollDecelerationFactor:0.99]; | |
708 | [scroller_ setDelegate:self]; | |
709 | ||
710 | CGRect webrect = [scroller_ bounds]; | |
711 | webrect.size.height = 0; | |
712 | ||
713 | WebView *webview; | |
714 | ||
715 | #if RecycleWebViews | |
716 | webview_ = [Documents_ lastObject]; | |
717 | if (webview_ != nil) { | |
718 | webview_ = [webview_ retain]; | |
719 | webview = [webview_ webView]; | |
720 | [Documents_ removeLastObject]; | |
721 | [webview_ setFrame:webrect]; | |
722 | } else { | |
723 | #else | |
724 | if (true) { | |
725 | #endif | |
726 | webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect]; | |
727 | webview = [webview_ webView]; | |
728 | ||
729 | // XXX: this is terribly (too?) expensive | |
730 | //[webview_ setDrawsBackground:NO]; | |
731 | [webview setPreferencesIdentifier:@"Cydia"]; | |
732 | ||
733 | [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)]; | |
734 | ||
735 | [webview_ setAllowsMessaging:YES]; | |
736 | ||
737 | [webview_ setTilingEnabled:YES]; | |
738 | [webview_ setDrawsGrid:NO]; | |
739 | [webview_ setLogsTilingChanges:NO]; | |
740 | [webview_ setTileMinificationFilter:kCAFilterNearest]; | |
741 | [webview_ setDetectsPhoneNumbers:NO]; | |
742 | [webview_ setAutoresizes:YES]; | |
743 | ||
744 | [webview_ setMinimumScale:0.25f forDocumentTypes:0x10]; | |
745 | [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10]; | |
746 | [webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; | |
747 | ||
748 | [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2]; | |
749 | ||
750 | [webview_ setMinimumScale:1.0f forDocumentTypes:0x8]; | |
751 | [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8]; | |
752 | [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8]; | |
753 | ||
754 | [webview_ _setDocumentType:0x4]; | |
755 | ||
756 | [webview_ setZoomsFocusedFormControl:YES]; | |
757 | [webview_ setContentsPosition:7]; | |
758 | [webview_ setEnabledGestures:0xa]; | |
759 | [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled]; | |
760 | [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller]; | |
761 | ||
762 | [webview_ setSmoothsFonts:YES]; | |
763 | ||
764 | [webview _setUsesLoaderCache:YES]; | |
765 | [webview setGroupName:@"Cydia"]; | |
766 | [webview _setLayoutInterval:0]; | |
767 | } | |
768 | ||
769 | [webview_ setDelegate:self]; | |
770 | [webview_ setGestureDelegate:self]; | |
771 | [scroller_ addSubview:webview_]; | |
772 | ||
773 | //NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
774 | ||
775 | CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]; | |
776 | indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)]; | |
777 | [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite]; | |
778 | ||
779 | Package *package([[Database sharedInstance] packageWithName:@"cydia"]); | |
780 | NSString *application = package == nil ? @"Cydia" : [NSString | |
781 | stringWithFormat:@"Cydia/%@", | |
782 | [package installed] | |
783 | ]; [webview setApplicationNameForUserAgent:application]; | |
784 | ||
785 | indirect_ = [[IndirectDelegate alloc] initWithDelegate:self]; | |
786 | ||
787 | [webview setFrameLoadDelegate:self]; | |
788 | [webview setResourceLoadDelegate:indirect_]; | |
789 | [webview setUIDelegate:self]; | |
790 | [webview setScriptDebugDelegate:self]; | |
791 | [webview setPolicyDelegate:self]; | |
792 | ||
793 | [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; | |
794 | [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; | |
795 | } return self; | |
796 | } | |
797 | ||
798 | - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event { | |
799 | [webview_ redrawScaledDocument]; | |
800 | } | |
801 | ||
802 | - (void) _rightButtonClicked { | |
803 | if (function_ == nil) { | |
804 | reloading_ = true; | |
805 | [self reloadURL]; | |
806 | } else { | |
807 | WebView *webview([webview_ webView]); | |
808 | WebFrame *frame([webview mainFrame]); | |
809 | ||
810 | id _private(MSHookIvar<id>(webview, "_private")); | |
811 | WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page")); | |
812 | WebCore::Settings *settings(page == NULL ? NULL : page->settings()); | |
813 | ||
814 | bool no; | |
815 | if (settings == NULL) | |
816 | no = 0; | |
817 | else { | |
818 | no = settings->JavaScriptCanOpenWindowsAutomatically(); | |
819 | settings->setJavaScriptCanOpenWindowsAutomatically(true); | |
820 | } | |
821 | ||
822 | [delegate_ clearFirstResponder]; | |
823 | JSObjectRef function([function_ JSObject]); | |
824 | JSGlobalContextRef context([frame globalContext]); | |
825 | JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); | |
826 | ||
827 | if (settings != NULL) | |
828 | settings->setJavaScriptCanOpenWindowsAutomatically(no); | |
829 | } | |
830 | } | |
831 | ||
832 | - (id) _rightButtonTitle { | |
833 | return button_ != nil ? button_ : @"Reload"; | |
834 | } | |
835 | ||
836 | - (id) rightButtonTitle { | |
837 | return [self _loading] ? @"" : [self _rightButtonTitle]; | |
838 | } | |
839 | ||
840 | - (UINavigationButtonStyle) rightButtonStyle { | |
841 | if (style_ == nil) normal: | |
842 | return UINavigationButtonStyleNormal; | |
843 | else if ([style_ isEqualToString:@"Normal"]) | |
844 | return UINavigationButtonStyleNormal; | |
845 | else if ([style_ isEqualToString:@"Back"]) | |
846 | return UINavigationButtonStyleBack; | |
847 | else if ([style_ isEqualToString:@"Highlighted"]) | |
848 | return UINavigationButtonStyleHighlighted; | |
849 | else if ([style_ isEqualToString:@"Destructive"]) | |
850 | return UINavigationButtonStyleDestructive; | |
851 | else goto normal; | |
852 | } | |
853 | ||
854 | - (NSString *) title { | |
855 | return title_ == nil ? @"Loading" : title_; | |
856 | } | |
857 | ||
858 | - (NSString *) backButtonTitle { | |
859 | return @"Browser"; | |
860 | } | |
861 | ||
862 | - (void) setPageActive:(BOOL)active { | |
863 | if (!active) | |
864 | [indicator_ removeFromSuperview]; | |
865 | else | |
866 | [[book_ navigationBar] addSubview:indicator_]; | |
867 | } | |
868 | ||
869 | - (void) resetViewAnimated:(BOOL)animated { | |
870 | } | |
871 | ||
872 | - (void) setPushed:(bool)pushed { | |
873 | pushed_ = pushed; | |
874 | } | |
875 | ||
876 | @end |