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