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