]>
Commit | Line | Data |
---|---|---|
1 | #include <BrowserView.h> | |
2 | ||
3 | #include <WebCore/WebCoreThread.h> | |
4 | ||
5 | /* Indirect Delegate {{{ */ | |
6 | @interface IndirectDelegate : NSObject { | |
7 | _transient volatile id delegate_; | |
8 | } | |
9 | ||
10 | - (void) setDelegate:(id)delegate; | |
11 | - (id) initWithDelegate:(id)delegate; | |
12 | @end | |
13 | ||
14 | @implementation IndirectDelegate | |
15 | ||
16 | - (void) setDelegate:(id)delegate { | |
17 | delegate_ = delegate; | |
18 | } | |
19 | ||
20 | - (id) initWithDelegate:(id)delegate { | |
21 | delegate_ = delegate; | |
22 | return self; | |
23 | } | |
24 | ||
25 | - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { | |
26 | if (delegate_ != nil) | |
27 | return [delegate_ webView:sender didClearWindowObject:window forFrame:frame]; | |
28 | } | |
29 | ||
30 | - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame { | |
31 | if (delegate_ != nil) | |
32 | return [delegate_ webView:sender didCommitLoadForFrame:frame]; | |
33 | } | |
34 | ||
35 | - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { | |
36 | if (delegate_ != nil) | |
37 | return [delegate_ webView:sender didFailLoadWithError:error forFrame:frame]; | |
38 | } | |
39 | ||
40 | - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { | |
41 | if (delegate_ != nil) | |
42 | return [delegate_ webView:sender didFailProvisionalLoadWithError:error forFrame:frame]; | |
43 | } | |
44 | ||
45 | - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { | |
46 | if (delegate_ != nil) | |
47 | return [delegate_ webView:sender didFinishLoadForFrame:frame]; | |
48 | } | |
49 | ||
50 | - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { | |
51 | if (delegate_ != nil) | |
52 | return [delegate_ webView:sender didReceiveTitle:title forFrame:frame]; | |
53 | } | |
54 | ||
55 | - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame { | |
56 | if (delegate_ != nil) | |
57 | return [delegate_ webView:sender didStartProvisionalLoadForFrame:frame]; | |
58 | } | |
59 | ||
60 | - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source { | |
61 | if (delegate_ != nil) | |
62 | return [delegate_ webView:sender resource:identifier didReceiveAuthenticationChallenge:challenge fromDataSource:source]; | |
63 | } | |
64 | ||
65 | - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source { | |
66 | if (delegate_ != nil) | |
67 | return [delegate_ webView:sender resource:identifier willSendRequest:request redirectResponse:redirectResponse fromDataSource:source]; | |
68 | return nil; | |
69 | } | |
70 | ||
71 | - (IMP) methodForSelector:(SEL)sel { | |
72 | if (IMP method = [super methodForSelector:sel]) | |
73 | return method; | |
74 | fprintf(stderr, "methodForSelector:[%s] == NULL\n", sel_getName(sel)); | |
75 | return NULL; | |
76 | } | |
77 | ||
78 | - (BOOL) respondsToSelector:(SEL)sel { | |
79 | if ([super respondsToSelector:sel]) | |
80 | return YES; | |
81 | // XXX: WebThreadCreateNSInvocation returns nil | |
82 | //fprintf(stderr, "[%s]R?%s\n", class_getName(self->isa), sel_getName(sel)); | |
83 | return delegate_ == nil ? NO : [delegate_ respondsToSelector:sel]; | |
84 | } | |
85 | ||
86 | - (NSMethodSignature *) methodSignatureForSelector:(SEL)sel { | |
87 | if (NSMethodSignature *method = [super methodSignatureForSelector:sel]) | |
88 | return method; | |
89 | //fprintf(stderr, "[%s]S?%s\n", class_getName(self->isa), sel_getName(sel)); | |
90 | if (delegate_ != nil) | |
91 | if (NSMethodSignature *sig = [delegate_ methodSignatureForSelector:sel]) | |
92 | return sig; | |
93 | // XXX: I fucking hate Apple so very very bad | |
94 | return [NSMethodSignature signatureWithObjCTypes:"v@:"]; | |
95 | } | |
96 | ||
97 | - (void) forwardInvocation:(NSInvocation *)inv { | |
98 | SEL sel = [inv selector]; | |
99 | if (delegate_ != nil && [delegate_ respondsToSelector:sel]) | |
100 | [inv invokeWithTarget:delegate_]; | |
101 | } | |
102 | ||
103 | @end | |
104 | /* }}} */ | |
105 | ||
106 | @interface WebView (Cydia) | |
107 | - (void) setScriptDebugDelegate:(id)delegate; | |
108 | - (void) _setFormDelegate:(id)delegate; | |
109 | - (void) _setUIKitDelegate:(id)delegate; | |
110 | - (void) setWebMailDelegate:(id)delegate; | |
111 | - (void) _setLayoutInterval:(float)interval; | |
112 | @end | |
113 | ||
114 | @interface WebScriptObject (Cydia) | |
115 | ||
116 | - (unsigned) count; | |
117 | - (id) objectAtIndex:(unsigned)index; | |
118 | ||
119 | @end | |
120 | ||
121 | @implementation WebScriptObject (Cydia) | |
122 | ||
123 | - (unsigned) count { | |
124 | id length([self valueForKey:@"length"]); | |
125 | if ([length respondsToSelector:@selector(intValue)]) | |
126 | return [length intValue]; | |
127 | else | |
128 | return 0; | |
129 | } | |
130 | ||
131 | - (id) objectAtIndex:(unsigned)index { | |
132 | return [self webScriptValueAtIndex:index]; | |
133 | } | |
134 | ||
135 | @end | |
136 | ||
137 | /* Web Scripting {{{ */ | |
138 | @interface CydiaObject : NSObject { | |
139 | id indirect_; | |
140 | } | |
141 | ||
142 | - (id) initWithDelegate:(IndirectDelegate *)indirect; | |
143 | @end | |
144 | ||
145 | @implementation CydiaObject | |
146 | ||
147 | - (void) dealloc { | |
148 | [indirect_ release]; | |
149 | [super dealloc]; | |
150 | } | |
151 | ||
152 | - (id) initWithDelegate:(IndirectDelegate *)indirect { | |
153 | if ((self = [super init]) != nil) { | |
154 | indirect_ = [indirect retain]; | |
155 | } return self; | |
156 | } | |
157 | ||
158 | + (NSArray *) _attributeKeys { | |
159 | return [NSArray arrayWithObjects:@"device", nil]; | |
160 | } | |
161 | ||
162 | - (NSArray *) attributeKeys { | |
163 | return [[self class] _attributeKeys]; | |
164 | } | |
165 | ||
166 | + (BOOL) isKeyExcludedFromWebScript:(const char *)name { | |
167 | return ![[self _attributeKeys] containsObject:[NSString stringWithUTF8String:name]] && [super isKeyExcludedFromWebScript:name]; | |
168 | } | |
169 | ||
170 | - (NSString *) device { | |
171 | return [[UIDevice currentDevice] uniqueIdentifier]; | |
172 | } | |
173 | ||
174 | + (NSString *) webScriptNameForSelector:(SEL)selector { | |
175 | if (selector == @selector(close)) | |
176 | return @"close"; | |
177 | else if (selector == @selector(getPackageById:)) | |
178 | return @"getPackageById"; | |
179 | else if (selector == @selector(setAutoPopup:)) | |
180 | return @"setAutoPopup"; | |
181 | else if (selector == @selector(setButtonImage:withStyle:toFunction:)) | |
182 | return @"setButtonImage"; | |
183 | else if (selector == @selector(setButtonTitle:withStyle:toFunction:)) | |
184 | return @"setButtonTitle"; | |
185 | else if (selector == @selector(setFinishHook:)) | |
186 | return @"setFinishHook"; | |
187 | else if (selector == @selector(setPopupHook:)) | |
188 | return @"setPopupHook"; | |
189 | else if (selector == @selector(setSpecial:)) | |
190 | return @"setSpecial"; | |
191 | else if (selector == @selector(setViewportWidth:)) | |
192 | return @"setViewportWidth"; | |
193 | else if (selector == @selector(supports:)) | |
194 | return @"supports"; | |
195 | else if (selector == @selector(stringWithFormat:arguments:)) | |
196 | return @"format"; | |
197 | else if (selector == @selector(localizedStringForKey:value:table:)) | |
198 | return @"localize"; | |
199 | else if (selector == @selector(du:)) | |
200 | return @"du"; | |
201 | else if (selector == @selector(statfs:)) | |
202 | return @"statfs"; | |
203 | else | |
204 | return nil; | |
205 | } | |
206 | ||
207 | + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector { | |
208 | return [self webScriptNameForSelector:selector] == nil; | |
209 | } | |
210 | ||
211 | - (BOOL) supports:(NSString *)feature { | |
212 | return [feature isEqualToString:@"window.open"]; | |
213 | } | |
214 | ||
215 | - (Package *) getPackageById:(NSString *)id { | |
216 | return [[Database sharedInstance] packageWithName:id]; | |
217 | } | |
218 | ||
219 | - (NSArray *) statfs:(NSString *)path { | |
220 | struct statfs stat; | |
221 | ||
222 | if (path == nil || statfs([path UTF8String], &stat) == -1) | |
223 | return nil; | |
224 | ||
225 | return [NSArray arrayWithObjects: | |
226 | [NSNumber numberWithUnsignedLong:stat.f_bsize], | |
227 | [NSNumber numberWithUnsignedLong:stat.f_blocks], | |
228 | [NSNumber numberWithUnsignedLong:stat.f_bfree], | |
229 | nil]; | |
230 | } | |
231 | ||
232 | - (NSNumber *) du:(NSString *)path { | |
233 | NSNumber *value(nil); | |
234 | ||
235 | int fds[2]; | |
236 | _assert(pipe(fds) != -1); | |
237 | ||
238 | pid_t pid(ExecFork()); | |
239 | if (pid == 0) { | |
240 | _assert(dup2(fds[1], 1) != -1); | |
241 | _assert(close(fds[0]) != -1); | |
242 | _assert(close(fds[1]) != -1); | |
243 | execlp("du", "du", "-s", [path UTF8String], NULL); | |
244 | exit(1); | |
245 | _assert(false); | |
246 | } | |
247 | ||
248 | _assert(close(fds[1]) != -1); | |
249 | ||
250 | if (FILE *du = fdopen(fds[0], "r")) { | |
251 | char line[1024]; | |
252 | while (fgets(line, sizeof(line), du) != NULL) { | |
253 | size_t length(strlen(line)); | |
254 | while (length != 0 && line[length - 1] == '\n') | |
255 | line[--length] = '\0'; | |
256 | if (char *tab = strchr(line, '\t')) { | |
257 | *tab = '\0'; | |
258 | value = [NSNumber numberWithUnsignedLong:strtoul(line, NULL, 0)]; | |
259 | } | |
260 | } | |
261 | ||
262 | fclose(du); | |
263 | } else _assert(close(fds[0])); | |
264 | ||
265 | int status; | |
266 | wait: | |
267 | if (waitpid(pid, &status, 0) == -1) | |
268 | if (errno == EINTR) | |
269 | goto wait; | |
270 | else _assert(false); | |
271 | ||
272 | return value; | |
273 | } | |
274 | ||
275 | - (void) close { | |
276 | [indirect_ close]; | |
277 | } | |
278 | ||
279 | - (void) setAutoPopup:(BOOL)popup { | |
280 | [indirect_ setAutoPopup:popup]; | |
281 | } | |
282 | ||
283 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
284 | [indirect_ setButtonImage:button withStyle:style toFunction:function]; | |
285 | } | |
286 | ||
287 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
288 | [indirect_ setButtonTitle:button withStyle:style toFunction:function]; | |
289 | } | |
290 | ||
291 | - (void) setSpecial:(id)function { | |
292 | [indirect_ setSpecial:function]; | |
293 | } | |
294 | ||
295 | - (void) setFinishHook:(id)function { | |
296 | [indirect_ setFinishHook:function]; | |
297 | } | |
298 | ||
299 | - (void) setPopupHook:(id)function { | |
300 | [indirect_ setPopupHook:function]; | |
301 | } | |
302 | ||
303 | - (void) setViewportWidth:(float)width { | |
304 | [indirect_ setViewportWidth:width]; | |
305 | } | |
306 | ||
307 | - (NSString *) stringWithFormat:(NSString *)format arguments:(WebScriptObject *)arguments { | |
308 | //NSLog(@"SWF:\"%@\" A:%@", format, [arguments description]); | |
309 | unsigned count([arguments count]); | |
310 | id values[count]; | |
311 | for (unsigned i(0); i != count; ++i) | |
312 | values[i] = [arguments objectAtIndex:i]; | |
313 | return [[[NSString alloc] initWithFormat:format arguments:reinterpret_cast<va_list>(values)] autorelease]; | |
314 | } | |
315 | ||
316 | - (NSString *) localizedStringForKey:(NSString *)key value:(NSString *)value table:(NSString *)table { | |
317 | if (reinterpret_cast<id>(table) == [WebUndefined undefined]) | |
318 | table = nil; | |
319 | return [[NSBundle mainBundle] localizedStringForKey:key value:value table:table]; | |
320 | } | |
321 | ||
322 | @end | |
323 | /* }}} */ | |
324 | ||
325 | @implementation BrowserView | |
326 | ||
327 | #if ShowInternals | |
328 | #include "Internals.h" | |
329 | #endif | |
330 | ||
331 | - (void) dealloc { | |
332 | #if LogBrowser | |
333 | NSLog(@"[BrowserView dealloc]"); | |
334 | #endif | |
335 | ||
336 | if (challenge_ != nil) | |
337 | [challenge_ release]; | |
338 | ||
339 | WebThreadLock(); | |
340 | ||
341 | WebView *webview = [webview_ webView]; | |
342 | [webview setFrameLoadDelegate:nil]; | |
343 | [webview setResourceLoadDelegate:nil]; | |
344 | [webview setUIDelegate:nil]; | |
345 | [webview setScriptDebugDelegate:nil]; | |
346 | [webview setPolicyDelegate:nil]; | |
347 | ||
348 | [webview setDownloadDelegate:nil]; | |
349 | ||
350 | /* XXX: these are set by UIWebDocumentView | |
351 | [webview _setFormDelegate:nil]; | |
352 | [webview _setUIKitDelegate:nil]; | |
353 | [webview setEditingDelegate:nil];*/ | |
354 | ||
355 | /* XXX: no one sets this, ever | |
356 | [webview setWebMailDelegate:nil];*/ | |
357 | ||
358 | [webview_ setDelegate:nil]; | |
359 | [webview_ setGestureDelegate:nil]; | |
360 | [webview_ setFormEditingDelegate:nil]; | |
361 | [webview_ setInteractionDelegate:nil]; | |
362 | ||
363 | [indirect_ setDelegate:nil]; | |
364 | ||
365 | //NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
366 | ||
367 | [webview close]; | |
368 | ||
369 | #if RecycleWebViews | |
370 | [webview_ removeFromSuperview]; | |
371 | [Documents_ addObject:[webview_ autorelease]]; | |
372 | #else | |
373 | [webview_ release]; | |
374 | #endif | |
375 | ||
376 | [indirect_ release]; | |
377 | ||
378 | WebThreadUnlock(); | |
379 | ||
380 | [cydia_ release]; | |
381 | ||
382 | [scroller_ setDelegate:nil]; | |
383 | ||
384 | if (button_ != nil) | |
385 | [button_ release]; | |
386 | if (style_ != nil) | |
387 | [style_ release]; | |
388 | if (function_ != nil) | |
389 | [function_ release]; | |
390 | if (finish_ != nil) | |
391 | [finish_ release]; | |
392 | if (closer_ != nil) | |
393 | [closer_ release]; | |
394 | if (special_ != nil) | |
395 | [special_ release]; | |
396 | ||
397 | [scroller_ release]; | |
398 | [indicator_ release]; | |
399 | if (confirm_ != nil) | |
400 | [confirm_ release]; | |
401 | if (title_ != nil) | |
402 | [title_ release]; | |
403 | [super dealloc]; | |
404 | } | |
405 | ||
406 | - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy { | |
407 | [self loadRequest:[NSURLRequest | |
408 | requestWithURL:url | |
409 | cachePolicy:policy | |
410 | timeoutInterval:30.0 | |
411 | ]]; | |
412 | } | |
413 | ||
414 | - (void) loadURL:(NSURL *)url { | |
415 | [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy]; | |
416 | } | |
417 | ||
418 | - (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request { | |
419 | NSMutableURLRequest *copy = [request mutableCopy]; | |
420 | ||
421 | if (Machine_ != NULL) | |
422 | [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"]; | |
423 | if (UniqueID_ != nil) | |
424 | [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"]; | |
425 | ||
426 | if (Role_ != nil) | |
427 | [copy setValue:Role_ forHTTPHeaderField:@"X-Role"]; | |
428 | ||
429 | return copy; | |
430 | } | |
431 | ||
432 | - (void) loadRequest:(NSURLRequest *)request { | |
433 | pushed_ = true; | |
434 | error_ = false; | |
435 | ||
436 | WebThreadLock(); | |
437 | [webview_ loadRequest:request]; | |
438 | WebThreadUnlock(); | |
439 | } | |
440 | ||
441 | - (void) reloadURL { | |
442 | if (request_ == nil) | |
443 | return; | |
444 | ||
445 | if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil) | |
446 | [self loadRequest:request_]; | |
447 | else { | |
448 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
449 | initWithTitle:CYLocalize("RESUBMIT_FORM") | |
450 | buttons:[NSArray arrayWithObjects:CYLocalize("CANCEL"), CYLocalize("SUBMIT"), nil] | |
451 | defaultButtonIndex:0 | |
452 | delegate:self | |
453 | context:@"submit" | |
454 | ] autorelease]; | |
455 | ||
456 | [sheet setNumberOfRows:1]; | |
457 | [sheet popupAlertAnimated:YES]; | |
458 | } | |
459 | } | |
460 | ||
461 | - (WebView *) webView { | |
462 | return [webview_ webView]; | |
463 | } | |
464 | ||
465 | - (UIWebDocumentView *) documentView { | |
466 | return webview_; | |
467 | } | |
468 | ||
469 | /* XXX: WebThreadLock? */ | |
470 | - (void) _fixScroller:(CGRect)bounds { | |
471 | float extra; | |
472 | if (!editing_) | |
473 | extra = 0; | |
474 | else { | |
475 | UIFormAssistant *assistant([UIFormAssistant sharedFormAssistant]); | |
476 | CGRect peripheral([assistant peripheralFrame]); | |
477 | #if LogBrowser | |
478 | NSLog(@"per:%f", peripheral.size.height); | |
479 | #endif | |
480 | extra = peripheral.size.height; | |
481 | } | |
482 | ||
483 | CGRect subrect([scroller_ frame]); | |
484 | subrect.size.height -= extra; | |
485 | [scroller_ setScrollerIndicatorSubrect:subrect]; | |
486 | ||
487 | NSSize visible(NSMakeSize(subrect.size.width, subrect.size.height)); | |
488 | [webview_ setValue:[NSValue valueWithSize:visible] forGestureAttribute:UIGestureAttributeVisibleSize]; | |
489 | ||
490 | CGSize size(size_); | |
491 | size.height += extra; | |
492 | [scroller_ setContentSize:size]; | |
493 | ||
494 | [scroller_ releaseRubberBandIfNecessary]; | |
495 | } | |
496 | ||
497 | - (void) fixScroller { | |
498 | CGRect bounds([webview_ documentBounds]); | |
499 | #if TrackResize | |
500 | NSLog(@"_fs:(%f,%f+%f,%f)", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height); | |
501 | #endif | |
502 | [self _fixScroller:bounds]; | |
503 | } | |
504 | ||
505 | - (void) view:(UIView *)sender didSetFrame:(CGRect)frame { | |
506 | size_ = frame.size; | |
507 | #if TrackResize | |
508 | NSLog(@"dsf:(%f,%f+%f,%f)", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); | |
509 | #endif | |
510 | [self _fixScroller:frame]; | |
511 | } | |
512 | ||
513 | - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old { | |
514 | [self view:sender didSetFrame:frame]; | |
515 | } | |
516 | ||
517 | - (void) pushPage:(RVPage *)page { | |
518 | [page setDelegate:delegate_]; | |
519 | [self setBackButtonTitle:title_]; | |
520 | [book_ pushPage:page]; | |
521 | } | |
522 | ||
523 | - (void) _pushPage { | |
524 | if (pushed_) | |
525 | return; | |
526 | // WTR: [self autorelease]; | |
527 | pushed_ = true; | |
528 | [book_ pushPage:self]; | |
529 | } | |
530 | ||
531 | - (void) swapPage:(RVPage *)page { | |
532 | [page setDelegate:delegate_]; | |
533 | if (pushed_) | |
534 | [book_ swapPage:page]; | |
535 | else | |
536 | [book_ pushPage:page]; | |
537 | } | |
538 | ||
539 | - (BOOL) getSpecial:(NSURL *)url swap:(BOOL)swap { | |
540 | #if LogBrowser | |
541 | NSLog(@"getSpecial:%@", url); | |
542 | #endif | |
543 | ||
544 | NSString *href([url absoluteString]); | |
545 | NSString *scheme([[url scheme] lowercaseString]); | |
546 | ||
547 | RVPage *page = nil; | |
548 | ||
549 | if ([href hasPrefix:@"apptapp://package/"]) | |
550 | page = [delegate_ pageForPackage:[href substringFromIndex:18]]; | |
551 | else if ([scheme isEqualToString:@"cydia"]) { | |
552 | page = [delegate_ pageForURL:url hasTag:NULL]; | |
553 | if (page == nil) | |
554 | return false; | |
555 | } else if (![scheme isEqualToString:@"apptapp"]) | |
556 | return false; | |
557 | ||
558 | if (page != nil) | |
559 | if (swap) | |
560 | [self swapPage:page]; | |
561 | else | |
562 | [self pushPage:page]; | |
563 | return true; | |
564 | } | |
565 | ||
566 | - (void) webViewShow:(WebView *)sender { | |
567 | /* XXX: this is where I cry myself to sleep */ | |
568 | } | |
569 | ||
570 | - (bool) _allowJavaScriptPanel { | |
571 | return true; | |
572 | } | |
573 | ||
574 | - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { | |
575 | if (![self _allowJavaScriptPanel]) | |
576 | return; | |
577 | ||
578 | // WTR: [self retain]; | |
579 | ||
580 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
581 | initWithTitle:nil | |
582 | buttons:[NSArray arrayWithObjects:CYLocalize("OK"), nil] | |
583 | defaultButtonIndex:0 | |
584 | delegate:self | |
585 | context:@"alert" | |
586 | ] autorelease]; | |
587 | ||
588 | [sheet setBodyText:message]; | |
589 | [sheet popupAlertAnimated:YES]; | |
590 | } | |
591 | ||
592 | - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame { | |
593 | if (![self _allowJavaScriptPanel]) | |
594 | return NO; | |
595 | ||
596 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
597 | initWithTitle:nil | |
598 | buttons:[NSArray arrayWithObjects:CYLocalize("OK"), CYLocalize("Cancel"), nil] | |
599 | defaultButtonIndex:0 | |
600 | delegate:indirect_ | |
601 | context:@"confirm" | |
602 | ] autorelease]; | |
603 | ||
604 | [sheet setNumberOfRows:1]; | |
605 | [sheet setBodyText:message]; | |
606 | [sheet popupAlertAnimated:YES]; | |
607 | ||
608 | NSRunLoop *loop([NSRunLoop currentRunLoop]); | |
609 | NSDate *future([NSDate distantFuture]); | |
610 | ||
611 | while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]); | |
612 | ||
613 | NSNumber *confirm([confirm_ autorelease]); | |
614 | confirm_ = nil; | |
615 | return [confirm boolValue]; | |
616 | } | |
617 | ||
618 | - (void) setAutoPopup:(BOOL)popup { | |
619 | popup_ = popup; | |
620 | } | |
621 | ||
622 | - (void) setSpecial:(id)function { | |
623 | if (special_ != nil) | |
624 | [special_ autorelease]; | |
625 | special_ = function == nil ? nil : [function retain]; | |
626 | } | |
627 | ||
628 | - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
629 | if (button_ != nil) | |
630 | [button_ autorelease]; | |
631 | button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain]; | |
632 | ||
633 | if (style_ != nil) | |
634 | [style_ autorelease]; | |
635 | style_ = style == nil ? nil : [style retain]; | |
636 | ||
637 | if (function_ != nil) | |
638 | [function_ autorelease]; | |
639 | function_ = function == nil ? nil : [function retain]; | |
640 | ||
641 | [self reloadButtons]; | |
642 | } | |
643 | ||
644 | - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function { | |
645 | if (button_ != nil) | |
646 | [button_ autorelease]; | |
647 | button_ = button == nil ? nil : [button retain]; | |
648 | ||
649 | if (style_ != nil) | |
650 | [style_ autorelease]; | |
651 | style_ = style == nil ? nil : [style retain]; | |
652 | ||
653 | if (function_ != nil) | |
654 | [function_ autorelease]; | |
655 | function_ = function == nil ? nil : [function retain]; | |
656 | ||
657 | [self reloadButtons]; | |
658 | } | |
659 | ||
660 | - (void) setFinishHook:(id)function { | |
661 | if (finish_ != nil) | |
662 | [finish_ autorelease]; | |
663 | finish_ = function == nil ? nil : [function retain]; | |
664 | } | |
665 | ||
666 | - (void) setPopupHook:(id)function { | |
667 | if (closer_ != nil) | |
668 | [closer_ autorelease]; | |
669 | closer_ = function == nil ? nil : [function retain]; | |
670 | } | |
671 | ||
672 | - (void) webView:(WebView *)sender willBeginEditingFormElement:(id)element { | |
673 | editing_ = true; | |
674 | } | |
675 | ||
676 | - (void) webView:(WebView *)sender didBeginEditingFormElement:(id)element { | |
677 | [self fixScroller]; | |
678 | } | |
679 | ||
680 | - (void) webViewDidEndEditingFormElements:(WebView *)sender { | |
681 | editing_ = false; | |
682 | [self fixScroller]; | |
683 | } | |
684 | ||
685 | - (void) webViewClose:(WebView *)sender { | |
686 | [book_ close]; | |
687 | } | |
688 | ||
689 | - (void) close { | |
690 | [book_ close]; | |
691 | } | |
692 | ||
693 | - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame { | |
694 | [window setValue:cydia_ forKey:@"cydia"]; | |
695 | } | |
696 | ||
697 | - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame { | |
698 | NSLog(@"err:%@", error); | |
699 | } | |
700 | ||
701 | - (void) webView:(WebView *)sender decidePolicyForNewWindowAction:(NSDictionary *)action request:(NSURLRequest *)request newFrameName:(NSString *)name decisionListener:(id<WebPolicyDecisionListener>)listener { | |
702 | #if LogBrowser | |
703 | NSLog(@"nwa:%@", name); | |
704 | #endif | |
705 | ||
706 | if (NSURL *url = [request URL]) { | |
707 | if (name == nil) unknown: { | |
708 | if (![self getSpecial:url swap:NO]) { | |
709 | NSString *scheme([[url scheme] lowercaseString]); | |
710 | if ([scheme isEqualToString:@"mailto"]) | |
711 | [delegate_ openMailToURL:url]; | |
712 | else goto use; | |
713 | } | |
714 | } else if ([name isEqualToString:@"_open"]) | |
715 | [delegate_ openURL:url]; | |
716 | else if ([name isEqualToString:@"_popup"]) { | |
717 | NSString *scheme([[url scheme] lowercaseString]); | |
718 | if ([scheme isEqualToString:@"mailto"]) | |
719 | [delegate_ openMailToURL:url]; | |
720 | else { | |
721 | RVBook *book([[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]); | |
722 | [book setHook:indirect_]; | |
723 | ||
724 | RVPage *page([delegate_ pageForURL:url hasTag:NULL]); | |
725 | if (page == nil) { | |
726 | /* XXX: call createWebViewWithRequest instead? */ | |
727 | ||
728 | [self setBackButtonTitle:title_]; | |
729 | ||
730 | BrowserView *browser([[[BrowserView alloc] initWithBook:book] autorelease]); | |
731 | [browser loadURL:url]; | |
732 | page = browser; | |
733 | } | |
734 | ||
735 | [book setDelegate:delegate_]; | |
736 | [page setDelegate:delegate_]; | |
737 | ||
738 | [book setPage:page]; | |
739 | [book_ pushBook:book]; | |
740 | } | |
741 | } else goto unknown; | |
742 | ||
743 | [listener ignore]; | |
744 | } else use: | |
745 | [listener use]; | |
746 | } | |
747 | ||
748 | - (void) webView:(WebView *)sender decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
749 | if ([WebView canShowMIMEType:type]) | |
750 | [listener use]; | |
751 | else { | |
752 | // XXX: handle more mime types! | |
753 | [listener ignore]; | |
754 | ||
755 | WebView *webview([webview_ webView]); | |
756 | if (frame == [webview mainFrame]) | |
757 | [UIApp openURL:[request URL]]; | |
758 | } | |
759 | } | |
760 | ||
761 | - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener { | |
762 | if (request == nil) ignore: { | |
763 | [listener ignore]; | |
764 | return; | |
765 | } | |
766 | ||
767 | NSURL *url([request URL]); | |
768 | ||
769 | if (url == nil) use: { | |
770 | if (!error_ && [frame parentFrame] == nil) { | |
771 | if (request_ != nil) | |
772 | [request_ autorelease]; | |
773 | request_ = [request retain]; | |
774 | #if LogBrowser | |
775 | NSLog(@"dpn:%@", request_); | |
776 | #endif | |
777 | } | |
778 | ||
779 | [listener use]; | |
780 | ||
781 | WebView *webview([webview_ webView]); | |
782 | if (frame == [webview mainFrame]) | |
783 | [self _pushPage]; | |
784 | return; | |
785 | } | |
786 | #if LogBrowser | |
787 | else NSLog(@"nav:%@:%@", url, [action description]); | |
788 | #endif | |
789 | ||
790 | const NSArray *capability; | |
791 | ||
792 | #if 0 // XXX:3:GSSystemCopyCapability | |
793 | capability = reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)); | |
794 | #else | |
795 | capability = nil; | |
796 | #endif | |
797 | ||
798 | if (capability != nil && ( | |
799 | [capability containsObject:@"com.apple.Maps"] && [url mapsURL] || | |
800 | [capability containsObject:@"com.apple.youtube"] && [url youTubeURL] | |
801 | )) { | |
802 | open: | |
803 | [UIApp openURL:url]; | |
804 | goto ignore; | |
805 | } | |
806 | ||
807 | int store(_not(int)); | |
808 | if (NSURL *itms = [url itmsURL:&store]) { | |
809 | #if LogBrowser | |
810 | NSLog(@"itms#%@#%u#%@", url, store, itms); | |
811 | #endif | |
812 | ||
813 | if (capability != nil && ( | |
814 | store == 1 && [capability containsObject:@"com.apple.MobileStore"] || | |
815 | store == 2 && [capability containsObject:@"com.apple.AppStore"] | |
816 | )) { | |
817 | url = itms; | |
818 | goto open; | |
819 | } | |
820 | } | |
821 | ||
822 | NSString *scheme([[url scheme] lowercaseString]); | |
823 | ||
824 | if ([scheme isEqualToString:@"tel"]) { | |
825 | // XXX: intelligence | |
826 | goto open; | |
827 | } | |
828 | ||
829 | if ([scheme isEqualToString:@"mailto"]) { | |
830 | [delegate_ openMailToURL:url]; | |
831 | goto ignore; | |
832 | } | |
833 | ||
834 | if ([self getSpecial:url swap:YES]) | |
835 | goto ignore; | |
836 | else if ([WebView _canHandleRequest:request]) | |
837 | goto use; | |
838 | else if ([url isSpringboardHandledURL]) | |
839 | goto open; | |
840 | else | |
841 | goto use; | |
842 | } | |
843 | ||
844 | - (void) webView:(WebView *)sender setStatusText:(NSString *)text { | |
845 | //lprintf("Status:%s\n", [text UTF8String]); | |
846 | } | |
847 | ||
848 | - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button { | |
849 | NSString *context([sheet context]); | |
850 | ||
851 | if ([context isEqualToString:@"alert"]) { | |
852 | [self autorelease]; | |
853 | [sheet dismiss]; | |
854 | } else if ([context isEqualToString:@"confirm"]) { | |
855 | switch (button) { | |
856 | case 1: | |
857 | confirm_ = [NSNumber numberWithBool:YES]; | |
858 | break; | |
859 | ||
860 | case 2: | |
861 | confirm_ = [NSNumber numberWithBool:NO]; | |
862 | break; | |
863 | } | |
864 | ||
865 | [sheet dismiss]; | |
866 | } else if ([context isEqualToString:@"challenge"]) { | |
867 | id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]); | |
868 | ||
869 | switch (button) { | |
870 | case 1: { | |
871 | NSString *username([[sheet textFieldAtIndex:0] text]); | |
872 | NSString *password([[sheet textFieldAtIndex:1] text]); | |
873 | ||
874 | NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]); | |
875 | ||
876 | [sender useCredential:credential forAuthenticationChallenge:challenge_]; | |
877 | } break; | |
878 | ||
879 | case 2: | |
880 | [sender cancelAuthenticationChallenge:challenge_]; | |
881 | break; | |
882 | ||
883 | default: | |
884 | _assert(false); | |
885 | } | |
886 | ||
887 | [challenge_ release]; | |
888 | challenge_ = nil; | |
889 | ||
890 | [sheet dismiss]; | |
891 | } else if ([context isEqualToString:@"submit"]) { | |
892 | switch (button) { | |
893 | case 1: | |
894 | break; | |
895 | ||
896 | case 2: | |
897 | if (request_ != nil) { | |
898 | WebThreadLock(); | |
899 | [webview_ loadRequest:request_]; | |
900 | WebThreadUnlock(); | |
901 | } | |
902 | break; | |
903 | ||
904 | default: | |
905 | _assert(false); | |
906 | } | |
907 | ||
908 | [sheet dismiss]; | |
909 | } | |
910 | } | |
911 | ||
912 | - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source { | |
913 | challenge_ = [challenge retain]; | |
914 | ||
915 | NSURLProtectionSpace *space([challenge protectionSpace]); | |
916 | NSString *realm([space realm]); | |
917 | if (realm == nil) | |
918 | realm = @""; | |
919 | ||
920 | UIActionSheet *sheet = [[[UIActionSheet alloc] | |
921 | initWithTitle:realm | |
922 | buttons:[NSArray arrayWithObjects:CYLocalize("LOGIN"), CYLocalize("CANCEL"), nil] | |
923 | defaultButtonIndex:0 | |
924 | delegate:self | |
925 | context:@"challenge" | |
926 | ] autorelease]; | |
927 | ||
928 | [sheet setNumberOfRows:1]; | |
929 | ||
930 | [sheet addTextFieldWithValue:@"" label:CYLocalize("USERNAME")]; | |
931 | [sheet addTextFieldWithValue:@"" label:CYLocalize("PASSWORD")]; | |
932 | ||
933 | UITextField *username([sheet textFieldAtIndex:0]); { | |
934 | UITextInputTraits *traits([username textInputTraits]); | |
935 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
936 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
937 | [traits setKeyboardType:UIKeyboardTypeASCIICapable]; | |
938 | [traits setReturnKeyType:UIReturnKeyNext]; | |
939 | } | |
940 | ||
941 | UITextField *password([sheet textFieldAtIndex:1]); { | |
942 | UITextInputTraits *traits([password textInputTraits]); | |
943 | [traits setAutocapitalizationType:UITextAutocapitalizationTypeNone]; | |
944 | [traits setAutocorrectionType:UITextAutocorrectionTypeNo]; | |
945 | [traits setKeyboardType:UIKeyboardTypeASCIICapable]; | |
946 | // XXX: UIReturnKeyDone | |
947 | [traits setReturnKeyType:UIReturnKeyNext]; | |
948 | [traits setSecureTextEntry:YES]; | |
949 | } | |
950 | ||
951 | [sheet popupAlertAnimated:YES]; | |
952 | } | |
953 | ||
954 | - (NSURLRequest *) webView:(WebView *)sender resource:(id)identifier willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse fromDataSource:(WebDataSource *)source { | |
955 | return [self _addHeadersToRequest:request]; | |
956 | } | |
957 | ||
958 | - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features { | |
959 | //- (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request userGesture:(BOOL)gesture { | |
960 | #if LogBrowser | |
961 | NSLog(@"cwv:%@ (%@): %@", request, title_, features == nil ? @"{}" : [features description]); | |
962 | //NSLog(@"cwv:%@ (%@): %@", request, title_, gesture ? @"Yes" : @"No"); | |
963 | #endif | |
964 | ||
965 | NSNumber *value([features objectForKey:@"width"]); | |
966 | float width(value == nil ? 0 : [value floatValue]); | |
967 | ||
968 | RVBook *book(!popup_ ? book_ : [[[RVPopUpBook alloc] initWithFrame:[delegate_ popUpBounds]] autorelease]); | |
969 | ||
970 | /* XXX: deal with cydia:// pages */ | |
971 | BrowserView *browser([[[BrowserView alloc] initWithBook:book forWidth:width] autorelease]); | |
972 | ||
973 | if (features != nil && popup_) { | |
974 | [book setDelegate:delegate_]; | |
975 | [book setHook:indirect_]; | |
976 | [browser setDelegate:delegate_]; | |
977 | ||
978 | [browser loadRequest:request]; | |
979 | ||
980 | [book setPage:browser]; | |
981 | [book_ pushBook:book]; | |
982 | } else if (request == nil) { | |
983 | [self setBackButtonTitle:title_]; | |
984 | [browser setDelegate:delegate_]; | |
985 | [browser retain]; | |
986 | } else { | |
987 | [self pushPage:browser]; | |
988 | [browser loadRequest:request]; | |
989 | } | |
990 | ||
991 | return [browser webView]; | |
992 | } | |
993 | ||
994 | - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request { | |
995 | return [self webView:sender createWebViewWithRequest:request windowFeatures:nil]; | |
996 | //return [self webView:sender createWebViewWithRequest:request userGesture:YES]; | |
997 | } | |
998 | ||
999 | - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame { | |
1000 | if ([frame parentFrame] != nil) | |
1001 | return; | |
1002 | ||
1003 | title_ = [title retain]; | |
1004 | [book_ reloadTitleForPage:self]; | |
1005 | } | |
1006 | ||
1007 | - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame { | |
1008 | [loading_ addObject:frame]; | |
1009 | ||
1010 | if ([frame parentFrame] == nil) { | |
1011 | [webview_ resignFirstResponder]; | |
1012 | ||
1013 | reloading_ = false; | |
1014 | ||
1015 | if (title_ != nil) { | |
1016 | [title_ release]; | |
1017 | title_ = nil; | |
1018 | } | |
1019 | ||
1020 | if (button_ != nil) { | |
1021 | [button_ release]; | |
1022 | button_ = nil; | |
1023 | } | |
1024 | ||
1025 | if (style_ != nil) { | |
1026 | [style_ release]; | |
1027 | style_ = nil; | |
1028 | } | |
1029 | ||
1030 | if (function_ != nil) { | |
1031 | [function_ release]; | |
1032 | function_ = nil; | |
1033 | } | |
1034 | ||
1035 | if (finish_ != nil) { | |
1036 | [finish_ release]; | |
1037 | finish_ = nil; | |
1038 | } | |
1039 | ||
1040 | if (closer_ != nil) { | |
1041 | [closer_ release]; | |
1042 | closer_ = nil; | |
1043 | } | |
1044 | ||
1045 | if (special_ != nil) { | |
1046 | [special_ release]; | |
1047 | special_ = nil; | |
1048 | } | |
1049 | ||
1050 | [book_ reloadTitleForPage:self]; | |
1051 | ||
1052 | [scroller_ scrollPointVisibleAtTopLeft:CGPointZero]; | |
1053 | ||
1054 | if ([scroller_ respondsToSelector:@selector(setZoomScale:duration:)]) | |
1055 | [scroller_ setZoomScale:1 duration:0]; | |
1056 | else | |
1057 | [scroller_ setZoomScale:1 animated:NO]; | |
1058 | ||
1059 | CGRect webrect = [scroller_ bounds]; | |
1060 | webrect.size.height = 0; | |
1061 | [webview_ setFrame:webrect]; | |
1062 | } | |
1063 | ||
1064 | [self reloadButtons]; | |
1065 | } | |
1066 | ||
1067 | - (void) _finishLoading { | |
1068 | if (reloading_ || [loading_ count] != 0) | |
1069 | return; | |
1070 | if (finish_ != nil) | |
1071 | [self callFunction:finish_]; | |
1072 | [self reloadButtons]; | |
1073 | } | |
1074 | ||
1075 | - (bool) isLoading { | |
1076 | return [loading_ count] != 0; | |
1077 | } | |
1078 | ||
1079 | - (void) reloadButtons { | |
1080 | if ([self isLoading]) | |
1081 | [indicator_ startAnimation]; | |
1082 | else | |
1083 | [indicator_ stopAnimation]; | |
1084 | [super reloadButtons]; | |
1085 | } | |
1086 | ||
1087 | - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame { | |
1088 | return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame]; | |
1089 | } | |
1090 | ||
1091 | - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame { | |
1092 | return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame]; | |
1093 | } | |
1094 | ||
1095 | - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame { | |
1096 | return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame]; | |
1097 | } | |
1098 | ||
1099 | - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame { | |
1100 | [self _pushPage]; | |
1101 | return [webview_ webView:sender didCommitLoadForFrame:frame]; | |
1102 | } | |
1103 | ||
1104 | - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame { | |
1105 | return [webview_ webView:sender didReceiveDocTypeForFrame:frame]; | |
1106 | } | |
1107 | ||
1108 | - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { | |
1109 | [loading_ removeObject:frame]; | |
1110 | ||
1111 | [self _finishLoading]; | |
1112 | ||
1113 | if ([frame parentFrame] == nil) { | |
1114 | if (DOMDocument *document = [frame DOMDocument]) | |
1115 | if (DOMNodeList<NSFastEnumeration> *bodies = [document getElementsByTagName:@"body"]) | |
1116 | for (DOMHTMLBodyElement *body in bodies) { | |
1117 | DOMCSSStyleDeclaration *style([document getComputedStyle:body pseudoElement:nil]); | |
1118 | ||
1119 | bool colored(false); | |
1120 | ||
1121 | if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) { | |
1122 | if ([color primitiveType] == DOM_CSS_RGBCOLOR) { | |
1123 | DOMRGBColor *rgb([color getRGBColorValue]); | |
1124 | ||
1125 | float red([[rgb red] getFloatValue:DOM_CSS_NUMBER]); | |
1126 | float green([[rgb green] getFloatValue:DOM_CSS_NUMBER]); | |
1127 | float blue([[rgb blue] getFloatValue:DOM_CSS_NUMBER]); | |
1128 | float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]); | |
1129 | ||
1130 | UIColor *uic(nil); | |
1131 | ||
1132 | if (red == 0xc7 && green == 0xce && blue == 0xd5) | |
1133 | uic = [UIColor pinStripeColor]; | |
1134 | else if (alpha != 0) | |
1135 | uic = [UIColor | |
1136 | colorWithRed:(red / 255) | |
1137 | green:(green / 255) | |
1138 | blue:(blue / 255) | |
1139 | alpha:alpha | |
1140 | ]; | |
1141 | ||
1142 | if (uic != nil) { | |
1143 | colored = true; | |
1144 | [scroller_ setBackgroundColor:uic]; | |
1145 | } | |
1146 | } | |
1147 | } | |
1148 | ||
1149 | if (!colored) | |
1150 | [scroller_ setBackgroundColor:[UIColor pinStripeColor]]; | |
1151 | break; | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | return [webview_ webView:sender didFinishLoadForFrame:frame]; | |
1156 | } | |
1157 | ||
1158 | - (void) _didFailWithError:(NSError *)error forFrame:(WebFrame *)frame { | |
1159 | [loading_ removeObject:frame]; | |
1160 | if (reloading_) | |
1161 | return; | |
1162 | ||
1163 | [self _finishLoading]; | |
1164 | ||
1165 | if ([frame parentFrame] == nil) { | |
1166 | [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@", | |
1167 | [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString], | |
1168 | [[error localizedDescription] stringByAddingPercentEscapes] | |
1169 | ]]]; | |
1170 | ||
1171 | error_ = true; | |
1172 | } | |
1173 | } | |
1174 | ||
1175 | - (void) webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { | |
1176 | [self _didFailWithError:error forFrame:frame]; | |
1177 | } | |
1178 | ||
1179 | - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame { | |
1180 | [self _didFailWithError:error forFrame:frame]; | |
1181 | } | |
1182 | ||
1183 | - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary { | |
1184 | #if LogBrowser || ForSaurik | |
1185 | lprintf("Console:%s\n", [[dictionary description] UTF8String]); | |
1186 | #endif | |
1187 | } | |
1188 | ||
1189 | /* XXX: fix this stupid include file | |
1190 | - (void) webView:(WebView *)sender frame:(WebFrame *)frame exceededDatabaseQuotaForSecurityOrigin:(WebSecurityOrigin *)origin database:(NSString *)database { | |
1191 | [origin setQuota:0x500000]; | |
1192 | }*/ | |
1193 | ||
1194 | - (void) _setTileDrawingEnabled:(BOOL)enabled { | |
1195 | //[webview_ setTileDrawingEnabled:enabled]; | |
1196 | } | |
1197 | ||
1198 | - (void) setViewportWidth:(float)width { | |
1199 | width_ = width ? width != 0 : [[self class] defaultWidth]; | |
1200 | [webview_ setViewportSize:CGSizeMake(width_, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; | |
1201 | } | |
1202 | ||
1203 | - (void) willStartGesturesInView:(UIView *)view forEvent:(GSEventRef)event { | |
1204 | [self _setTileDrawingEnabled:NO]; | |
1205 | } | |
1206 | ||
1207 | - (void) didFinishGesturesInView:(UIView *)view forEvent:(GSEventRef)event { | |
1208 | [self _setTileDrawingEnabled:YES]; | |
1209 | [webview_ redrawScaledDocument]; | |
1210 | } | |
1211 | ||
1212 | - (void) scrollerWillStartDragging:(UIScroller *)scroller { | |
1213 | [self _setTileDrawingEnabled:NO]; | |
1214 | } | |
1215 | ||
1216 | - (void) scrollerDidEndDragging:(UIScroller *)scroller willSmoothScroll:(BOOL)smooth { | |
1217 | [self _setTileDrawingEnabled:YES]; | |
1218 | } | |
1219 | ||
1220 | - (void) scrollerDidEndDragging:(UIScroller *)scroller { | |
1221 | [self _setTileDrawingEnabled:YES]; | |
1222 | } | |
1223 | ||
1224 | - (id) initWithBook:(RVBook *)book forWidth:(float)width { | |
1225 | if ((self = [super initWithBook:book]) != nil) { | |
1226 | loading_ = [[NSMutableSet alloc] initWithCapacity:3]; | |
1227 | popup_ = false; | |
1228 | ||
1229 | struct CGRect bounds = [self bounds]; | |
1230 | ||
1231 | scroller_ = [[UIScroller alloc] initWithFrame:bounds]; | |
1232 | [self addSubview:scroller_]; | |
1233 | ||
1234 | [scroller_ setFixedBackgroundPattern:YES]; | |
1235 | [scroller_ setBackgroundColor:[UIColor pinStripeColor]]; | |
1236 | ||
1237 | [scroller_ setScrollingEnabled:YES]; | |
1238 | [scroller_ setClipsSubviews:YES]; | |
1239 | [scroller_ setAllowsRubberBanding:YES]; | |
1240 | ||
1241 | [scroller_ setDelegate:self]; | |
1242 | [scroller_ setBounces:YES]; | |
1243 | [scroller_ setScrollHysteresis:8]; | |
1244 | [scroller_ setThumbDetectionEnabled:NO]; | |
1245 | [scroller_ setDirectionalScrolling:YES]; | |
1246 | [scroller_ setScrollDecelerationFactor:0.99]; /* 0.989324 */ | |
1247 | [scroller_ setEventMode:YES]; | |
1248 | [scroller_ setShowBackgroundShadow:NO]; /* YES */ | |
1249 | [scroller_ setAllowsRubberBanding:YES]; /* Vertical */ | |
1250 | [scroller_ setAdjustForContentSizeChange:YES]; /* NO */ | |
1251 | ||
1252 | CGRect webrect = [scroller_ bounds]; | |
1253 | webrect.size.height = 0; | |
1254 | ||
1255 | WebView *webview; | |
1256 | ||
1257 | WebThreadLock(); | |
1258 | ||
1259 | #if RecycleWebViews | |
1260 | webview_ = [Documents_ lastObject]; | |
1261 | if (webview_ != nil) { | |
1262 | webview_ = [webview_ retain]; | |
1263 | webview = [webview_ webView]; | |
1264 | [Documents_ removeLastObject]; | |
1265 | [webview_ setFrame:webrect]; | |
1266 | } else { | |
1267 | #else | |
1268 | if (true) { | |
1269 | #endif | |
1270 | webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect]; | |
1271 | webview = [webview_ webView]; | |
1272 | ||
1273 | // XXX: this is terribly (too?) expensive | |
1274 | //[webview_ setDrawsBackground:NO]; | |
1275 | [webview setPreferencesIdentifier:@"Cydia"]; | |
1276 | ||
1277 | [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)]; | |
1278 | ||
1279 | [webview_ setAllowsMessaging:YES]; | |
1280 | ||
1281 | [webview_ setTilingEnabled:YES]; | |
1282 | [webview_ setDrawsGrid:NO]; | |
1283 | [webview_ setLogsTilingChanges:NO]; | |
1284 | [webview_ setTileMinificationFilter:kCAFilterNearest]; | |
1285 | if ([webview_ respondsToSelector:@selector(setDataDetectorTypes:)]) | |
1286 | /* XXX: abstractify */ | |
1287 | [webview_ setDataDetectorTypes:0x80000000]; | |
1288 | else | |
1289 | [webview_ setDetectsPhoneNumbers:NO]; | |
1290 | [webview_ setAutoresizes:YES]; | |
1291 | ||
1292 | [webview_ setMinimumScale:0.25f forDocumentTypes:0x10]; | |
1293 | [webview_ setMaximumScale:5.00f forDocumentTypes:0x10]; | |
1294 | [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10]; | |
1295 | //[webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10]; | |
1296 | ||
1297 | [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2]; | |
1298 | ||
1299 | [webview_ setMinimumScale:1.00f forDocumentTypes:0x8]; | |
1300 | [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8]; | |
1301 | [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8]; | |
1302 | ||
1303 | [webview_ _setDocumentType:0x4]; | |
1304 | ||
1305 | if ([webview_ respondsToSelector:@selector(UIWebDocumentView:)]) | |
1306 | [webview_ setZoomsFocusedFormControl:YES]; | |
1307 | [webview_ setContentsPosition:7]; | |
1308 | [webview_ setEnabledGestures:0xa]; | |
1309 | [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeIsZoomRubberBandEnabled]; | |
1310 | [webview_ setValue:[NSNumber numberWithBool:YES] forGestureAttribute:UIGestureAttributeUpdatesScroller]; | |
1311 | ||
1312 | [webview_ setSmoothsFonts:YES]; | |
1313 | [webview_ setAllowsImageSheet:YES]; | |
1314 | [webview _setUsesLoaderCache:YES]; | |
1315 | ||
1316 | [webview setGroupName:@"CydiaGroup"]; | |
1317 | if ([webview respondsToSelector:@selector(_setLayoutInterval:)]) | |
1318 | [webview _setLayoutInterval:0]; | |
1319 | } | |
1320 | ||
1321 | [self setViewportWidth:width]; | |
1322 | ||
1323 | [webview_ setDelegate:self]; | |
1324 | [webview_ setGestureDelegate:self]; | |
1325 | [webview_ setFormEditingDelegate:self]; | |
1326 | [webview_ setInteractionDelegate:self]; | |
1327 | ||
1328 | [scroller_ addSubview:webview_]; | |
1329 | ||
1330 | //NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; | |
1331 | ||
1332 | Package *package([[Database sharedInstance] packageWithName:@"cydia"]); | |
1333 | NSString *application = package == nil ? @"Cydia" : [NSString | |
1334 | stringWithFormat:@"Cydia/%@", | |
1335 | [package installed] | |
1336 | ]; | |
1337 | ||
1338 | if (Product_ != nil) | |
1339 | application = [NSString stringWithFormat:@"%@ Version/%@", application, Product_]; | |
1340 | if (Build_ != nil) | |
1341 | application = [NSString stringWithFormat:@"%@ Mobile/%@", application, Build_]; | |
1342 | if (Safari_ != nil) | |
1343 | application = [NSString stringWithFormat:@"%@ Safari/%@", application, Safari_]; | |
1344 | ||
1345 | [webview setApplicationNameForUserAgent:application]; | |
1346 | ||
1347 | indirect_ = [[IndirectDelegate alloc] initWithDelegate:self]; | |
1348 | cydia_ = [[CydiaObject alloc] initWithDelegate:indirect_]; | |
1349 | ||
1350 | [webview setFrameLoadDelegate:indirect_]; | |
1351 | [webview setResourceLoadDelegate:indirect_]; | |
1352 | [webview setUIDelegate:indirect_]; | |
1353 | [webview setScriptDebugDelegate:indirect_]; | |
1354 | [webview setPolicyDelegate:indirect_]; | |
1355 | ||
1356 | WebThreadUnlock(); | |
1357 | ||
1358 | CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite]; | |
1359 | indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)]; | |
1360 | [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite]; | |
1361 | ||
1362 | [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; | |
1363 | [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight]; | |
1364 | ||
1365 | /*UIWebView *test([[[UIWebView alloc] initWithFrame:[self bounds]] autorelease]); | |
1366 | [test loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.saurik.com/"]]]; | |
1367 | [self addSubview:test];*/ | |
1368 | } return self; | |
1369 | } | |
1370 | ||
1371 | - (id) initWithBook:(RVBook *)book { | |
1372 | return [self initWithBook:book forWidth:0]; | |
1373 | } | |
1374 | ||
1375 | - (NSString *) stringByEvaluatingJavaScriptFromString:(NSString *)script { | |
1376 | WebThreadLock(); | |
1377 | WebView *webview([webview_ webView]); | |
1378 | NSString *string([webview stringByEvaluatingJavaScriptFromString:script]); | |
1379 | WebThreadUnlock(); | |
1380 | return string; | |
1381 | } | |
1382 | ||
1383 | - (void) callFunction:(WebScriptObject *)function { | |
1384 | WebThreadLock(); | |
1385 | ||
1386 | WebView *webview([webview_ webView]); | |
1387 | WebFrame *frame([webview mainFrame]); | |
1388 | ||
1389 | id _private(MSHookIvar<id>(webview, "_private")); | |
1390 | WebCore::Page *page(_private == nil ? NULL : MSHookIvar<WebCore::Page *>(_private, "page")); | |
1391 | WebCore::Settings *settings(page == NULL ? NULL : page->settings()); | |
1392 | ||
1393 | bool no; | |
1394 | if (settings == NULL) | |
1395 | no = 0; | |
1396 | else { | |
1397 | no = settings->JavaScriptCanOpenWindowsAutomatically(); | |
1398 | settings->setJavaScriptCanOpenWindowsAutomatically(true); | |
1399 | } | |
1400 | ||
1401 | [delegate_ clearFirstResponder]; | |
1402 | JSObjectRef object([function JSObject]); | |
1403 | JSGlobalContextRef context([frame globalContext]); | |
1404 | JSObjectCallAsFunction(context, object, NULL, 0, NULL, NULL); | |
1405 | ||
1406 | if (settings != NULL) | |
1407 | settings->setJavaScriptCanOpenWindowsAutomatically(no); | |
1408 | ||
1409 | WebThreadUnlock(); | |
1410 | } | |
1411 | ||
1412 | - (void) didCloseBook:(RVBook *)book { | |
1413 | if (closer_ != nil) | |
1414 | [self callFunction:closer_]; | |
1415 | } | |
1416 | ||
1417 | - (void) __rightButtonClicked { | |
1418 | reloading_ = true; | |
1419 | [self reloadURL]; | |
1420 | } | |
1421 | ||
1422 | - (void) _rightButtonClicked { | |
1423 | #if !AlwaysReload | |
1424 | if (function_ != nil) | |
1425 | [self callFunction:function_]; | |
1426 | else | |
1427 | #endif | |
1428 | [self __rightButtonClicked]; | |
1429 | } | |
1430 | ||
1431 | - (id) _rightButtonTitle { | |
1432 | return CYLocalize("RELOAD"); | |
1433 | } | |
1434 | ||
1435 | - (id) rightButtonTitle { | |
1436 | return [self isLoading] ? @"" : button_ != nil ? button_ : [self _rightButtonTitle]; | |
1437 | } | |
1438 | ||
1439 | - (UINavigationButtonStyle) rightButtonStyle { | |
1440 | if (style_ == nil) normal: | |
1441 | return UINavigationButtonStyleNormal; | |
1442 | else if ([style_ isEqualToString:@"Normal"]) | |
1443 | return UINavigationButtonStyleNormal; | |
1444 | else if ([style_ isEqualToString:@"Back"]) | |
1445 | return UINavigationButtonStyleBack; | |
1446 | else if ([style_ isEqualToString:@"Highlighted"]) | |
1447 | return UINavigationButtonStyleHighlighted; | |
1448 | else if ([style_ isEqualToString:@"Destructive"]) | |
1449 | return UINavigationButtonStyleDestructive; | |
1450 | else goto normal; | |
1451 | } | |
1452 | ||
1453 | - (NSString *) title { | |
1454 | return title_ == nil ? CYLocalize("LOADING") : title_; | |
1455 | } | |
1456 | ||
1457 | - (NSString *) backButtonTitle { | |
1458 | return CYLocalize("BROWSER"); | |
1459 | } | |
1460 | ||
1461 | - (void) setPageActive:(BOOL)active { | |
1462 | if (!active) | |
1463 | [indicator_ removeFromSuperview]; | |
1464 | else | |
1465 | [[book_ navigationBar] addSubview:indicator_]; | |
1466 | } | |
1467 | ||
1468 | - (void) resetViewAnimated:(BOOL)animated { | |
1469 | } | |
1470 | ||
1471 | - (void) setPushed:(bool)pushed { | |
1472 | pushed_ = pushed; | |
1473 | } | |
1474 | ||
1475 | + (float) defaultWidth { | |
1476 | return 980; | |
1477 | } | |
1478 | ||
1479 | @end |