1 #include <BrowserView.h>
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;
11 @implementation BrowserView
14 #include "Internals.h"
18 if (challenge_ != nil)
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];
28 [webview setDownloadDelegate:nil];
30 [webview _setFormDelegate:nil];
31 [webview _setUIKitDelegate:nil];
32 [webview setWebMailDelegate:nil];
33 [webview setEditingDelegate:nil];
35 [webview_ setDelegate:nil];
36 [webview_ setGestureDelegate:nil];
38 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
43 [webview_ removeFromSuperview];
44 [Documents_ addObject:[webview_ autorelease]];
49 [indirect_ setDelegate:nil];
52 [scroller_ setDelegate:nil];
70 - (void) loadURL:(NSURL *)url cachePolicy:(NSURLRequestCachePolicy)policy {
71 [self loadRequest:[NSURLRequest
78 - (void) loadURL:(NSURL *)url {
79 [self loadURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy];
82 - (NSMutableURLRequest *) _addHeadersToRequest:(NSURLRequest *)request {
83 NSMutableURLRequest *copy = [request mutableCopy];
86 [copy setValue:[NSString stringWithUTF8String:Machine_] forHTTPHeaderField:@"X-Machine"];
88 [copy setValue:UniqueID_ forHTTPHeaderField:@"X-Unique-ID"];
91 [copy setValue:Role_ forHTTPHeaderField:@"X-Role"];
96 - (void) loadRequest:(NSURLRequest *)request {
98 [webview_ loadRequest:request];
102 NSLog(@"rlu:%@", request_);
106 if ([request_ HTTPBody] == nil && [request_ HTTPBodyStream] == nil)
107 [webview_ loadRequest:request_];
109 UIActionSheet *sheet = [[[UIActionSheet alloc]
110 initWithTitle:@"Are you sure you want to submit this form again?"
111 buttons:[NSArray arrayWithObjects:@"Cancel", @"Submit", nil]
117 [sheet setNumberOfRows:1];
118 [sheet popupAlertAnimated:YES];
122 - (WebView *) webView {
123 return [webview_ webView];
126 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame {
127 [scroller_ setContentSize:frame.size];
130 - (void) view:(UIView *)sender didSetFrame:(CGRect)frame oldFrame:(CGRect)old {
131 [self view:sender didSetFrame:frame];
134 - (void) pushPage:(RVPage *)page {
135 [self setBackButtonTitle:title_];
136 [page setDelegate:delegate_];
137 [book_ pushPage:page];
140 - (BOOL) getSpecial:(NSURL *)url {
141 NSString *href([url absoluteString]);
142 NSString *scheme([[url scheme] lowercaseString]);
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];
152 } else if (![scheme isEqualToString:@"apptapp"])
156 [self pushPage:page];
160 - (void) webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
161 UIActionSheet *sheet = [[[UIActionSheet alloc]
163 buttons:[NSArray arrayWithObjects:@"OK", nil]
169 [sheet setBodyText:message];
170 [sheet popupAlertAnimated:YES];
173 - (BOOL) webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
174 UIActionSheet *sheet = [[[UIActionSheet alloc]
176 buttons:[NSArray arrayWithObjects:@"OK", @"Cancel", nil]
182 [sheet setNumberOfRows:1];
183 [sheet setBodyText:message];
184 [sheet popupAlertAnimated:YES];
186 NSRunLoop *loop([NSRunLoop currentRunLoop]);
187 NSDate *future([NSDate distantFuture]);
189 while (confirm_ == nil && [loop runMode:NSDefaultRunLoopMode beforeDate:future]);
191 NSNumber *confirm([confirm_ autorelease]);
193 return [confirm boolValue];
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:))
210 + (BOOL) isSelectorExcludedFromWebScript:(SEL)selector {
211 return [self webScriptNameForSelector:selector] == nil;
214 - (BOOL) supports:(NSString *)feature {
215 return [feature isEqualToString:@"window.open"];
218 - (Package *) getPackageById:(NSString *)id {
219 return [[Database sharedInstance] packageWithName:id];
222 - (void) setButtonImage:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
224 [button_ autorelease];
225 button_ = button == nil ? nil : [[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:button]]] retain];
228 [style_ autorelease];
229 style_ = style == nil ? nil : [style retain];
231 if (function_ != nil)
232 [function_ autorelease];
233 function_ = function == nil ? nil : [function retain];
236 - (void) setButtonTitle:(NSString *)button withStyle:(NSString *)style toFunction:(id)function {
238 [button_ autorelease];
239 button_ = button == nil ? nil : [button retain];
242 [style_ autorelease];
243 style_ = style == nil ? nil : [style retain];
245 if (function_ != nil)
246 [function_ autorelease];
247 function_ = function == nil ? nil : [function retain];
251 - (void) webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)window forFrame:(WebFrame *)frame {
252 [window setValue:self forKey:@"cydia"];
255 - (void) webView:(WebView *)sender unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame {
256 NSLog(@"err:%@", error);
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];
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];
277 - (void) webView:(WebView *)webView decidePolicyForMIMEType:(NSString *)type request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
278 if ([WebView canShowMIMEType:type])
281 // XXX: handle more mime types!
283 if (frame == [webView mainFrame])
284 [UIApp openURL:[request URL]];
288 - (void) webView:(WebView *)sender decidePolicyForNavigationAction:(NSDictionary *)action request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener {
289 if (request == nil) ignore: {
294 NSURL *url([request URL]);
296 if (url == nil) use: {
297 if ([frame parentFrame] == nil) {
299 [request_ autorelease];
300 request_ = [request retain];
301 NSLog(@"dpn:%@", request_);
308 else NSLog(@"nav:%@:%@", url, [action description]);
311 const NSArray *capability(reinterpret_cast<const NSArray *>(GSSystemGetCapability(kGSDisplayIdentifiersCapability)));
314 [capability containsObject:@"com.apple.Maps"] && [url mapsURL] ||
315 [capability containsObject:@"com.apple.youtube"] && [url youTubeURL]
322 int store(_not(int));
323 if (NSURL *itms = [url itmsURL:&store]) {
324 NSLog(@"itms#%@#%u#%@", url, store, itms);
326 store == 1 && [capability containsObject:@"com.apple.MobileStore"] ||
327 store == 2 && [capability containsObject:@"com.apple.AppStore"]
334 NSString *scheme([[url scheme] lowercaseString]);
336 if ([scheme isEqualToString:@"tel"]) {
341 if ([scheme isEqualToString:@"mailto"]) {
342 [delegate_ openMailToURL:url];
346 if ([self getSpecial:url])
348 else if ([WebView _canHandleRequest:request])
350 else if ([url isSpringboardHandledURL])
356 - (void) webView:(WebView *)sender setStatusText:(NSString *)text {
357 //lprintf("Status:%s\n", [text UTF8String]);
364 [book_ pushPage:self];
367 - (void) alertSheet:(UIActionSheet *)sheet buttonClicked:(int)button {
368 NSString *context([sheet context]);
370 if ([context isEqualToString:@"alert"])
372 else if ([context isEqualToString:@"confirm"]) {
375 confirm_ = [NSNumber numberWithBool:YES];
379 confirm_ = [NSNumber numberWithBool:NO];
384 } else if ([context isEqualToString:@"challenge"]) {
385 id<NSURLAuthenticationChallengeSender> sender([challenge_ sender]);
389 NSString *username([[sheet textFieldAtIndex:0] text]);
390 NSString *password([[sheet textFieldAtIndex:1] text]);
392 NSURLCredential *credential([NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistenceForSession]);
394 [sender useCredential:credential forAuthenticationChallenge:challenge_];
398 [sender cancelAuthenticationChallenge:challenge_];
405 [challenge_ release];
409 } else if ([context isEqualToString:@"submit"]) {
416 [webview_ loadRequest:request_];
427 - (void) webView:(WebView *)sender resource:(id)identifier didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge fromDataSource:(WebDataSource *)source {
428 challenge_ = [challenge retain];
430 NSURLProtectionSpace *space([challenge protectionSpace]);
431 NSString *realm([space realm]);
435 UIActionSheet *sheet = [[[UIActionSheet alloc]
437 buttons:[NSArray arrayWithObjects:@"Login", @"Cancel", nil]
443 [sheet setNumberOfRows:1];
445 [sheet addTextFieldWithValue:@"" label:@"username"];
446 [sheet addTextFieldWithValue:@"" label:@"password"];
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];
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];
466 [sheet popupAlertAnimated:YES];
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])
474 return [self _addHeadersToRequest:request];
477 - (WebView *) _createWebViewWithRequest:(NSURLRequest *)request pushed:(BOOL)pushed {
478 [self setBackButtonTitle:title_];
480 BrowserView *browser = [[[BrowserView alloc] initWithBook:book_] autorelease];
481 [browser setDelegate:delegate_];
484 [browser loadRequest:request];
485 [book_ pushPage:browser];
488 return [browser webView];
491 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request {
492 return [self _createWebViewWithRequest:request pushed:(request != nil)];
495 - (WebView *) webView:(WebView *)sender createWebViewWithRequest:(NSURLRequest *)request windowFeatures:(NSDictionary *)features {
496 return [self _createWebViewWithRequest:request pushed:YES];
499 - (void) webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame {
500 if ([frame parentFrame] != nil)
503 title_ = [title retain];
504 [book_ reloadTitleForPage:self];
507 - (void) webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
508 if ([frame parentFrame] != nil)
513 [self reloadButtons];
520 if (button_ != nil) {
530 if (function_ != nil) {
535 [book_ reloadTitleForPage:self];
537 [scroller_ scrollPointVisibleAtTopLeft:CGPointZero];
539 CGRect webrect = [scroller_ bounds];
540 webrect.size.height = 0;
541 [webview_ setFrame:webrect];
544 - (void) _finishLoading {
547 [self reloadButtons];
555 - (void) reloadButtons {
557 [indicator_ startAnimation];
559 [indicator_ stopAnimation];
560 [super reloadButtons];
563 - (BOOL) webView:(WebView *)sender shouldScrollToPoint:(struct CGPoint)point forFrame:(WebFrame *)frame {
564 return [webview_ webView:sender shouldScrollToPoint:point forFrame:frame];
567 - (void) webView:(WebView *)sender didReceiveViewportArguments:(id)arguments forFrame:(WebFrame *)frame {
568 return [webview_ webView:sender didReceiveViewportArguments:arguments forFrame:frame];
571 - (void) webView:(WebView *)sender needsScrollNotifications:(id)notifications forFrame:(WebFrame *)frame {
572 return [webview_ webView:sender needsScrollNotifications:notifications forFrame:frame];
575 - (void) webView:(WebView *)sender didCommitLoadForFrame:(WebFrame *)frame {
576 return [webview_ webView:sender didCommitLoadForFrame:frame];
579 - (void) webView:(WebView *)sender didReceiveDocTypeForFrame:(WebFrame *)frame {
580 return [webview_ webView:sender didReceiveDocTypeForFrame:frame];
583 - (void) webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
584 if ([frame parentFrame] == nil) {
585 [self _finishLoading];
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]);
594 if (DOMCSSPrimitiveValue *color = static_cast<DOMCSSPrimitiveValue *>([style getPropertyCSSValue:@"background-color"])) {
595 DOMRGBColor *rgb([color getRGBColorValue]);
597 float alpha([[rgb alpha] getFloatValue:DOM_CSS_NUMBER]);
598 NSLog(@"alpha:%g", alpha);
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)
613 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
618 return [webview_ webView:sender didFinishLoadForFrame:frame];
621 - (void) webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
622 if ([frame parentFrame] != nil)
624 [self _finishLoading];
626 [self loadURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",
627 [[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"error" ofType:@"html"]] absoluteString],
628 [[error localizedDescription] stringByAddingPercentEscapes]
632 - (void) webView:(WebView *)sender addMessageToConsole:(NSDictionary *)dictionary {
634 lprintf("Console:%s\n", [[dictionary description] UTF8String]);
638 - (id) initWithBook:(RVBook *)book {
639 if ((self = [super initWithBook:book]) != nil) {
642 struct CGRect bounds = [self bounds];
644 scroller_ = [[UIScroller alloc] initWithFrame:bounds];
645 [self addSubview:scroller_];
647 [scroller_ setShowBackgroundShadow:NO];
648 [scroller_ setFixedBackgroundPattern:YES];
649 [scroller_ setBackgroundColor:[UIColor pinStripeColor]];
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];
658 CGRect webrect = [scroller_ bounds];
659 webrect.size.height = 0;
664 webview_ = [Documents_ lastObject];
665 if (webview_ != nil) {
666 webview_ = [webview_ retain];
667 webview = [webview_ webView];
668 [Documents_ removeLastObject];
669 [webview_ setFrame:webrect];
674 webview_ = [[UIWebDocumentView alloc] initWithFrame:webrect];
675 webview = [webview_ webView];
677 // XXX: this is terribly (too?) expensive
678 //[webview_ setDrawsBackground:NO];
679 [webview setPreferencesIdentifier:@"Cydia"];
681 [webview_ setTileSize:CGSizeMake(webrect.size.width, 500)];
683 [webview_ setAllowsMessaging:YES];
685 [webview_ setTilingEnabled:YES];
686 [webview_ setDrawsGrid:NO];
687 [webview_ setLogsTilingChanges:NO];
688 [webview_ setTileMinificationFilter:kCAFilterNearest];
689 [webview_ setDetectsPhoneNumbers:NO];
690 [webview_ setAutoresizes:YES];
692 [webview_ setMinimumScale:0.25f forDocumentTypes:0x10];
693 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x10];
694 [webview_ setViewportSize:CGSizeMake(980, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x10];
696 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x2];
698 [webview_ setMinimumScale:1.0f forDocumentTypes:0x8];
699 [webview_ setInitialScale:UIWebViewScalesToFitScale forDocumentTypes:0x8];
700 [webview_ setViewportSize:CGSizeMake(320, UIWebViewGrowsAndShrinksToFitHeight) forDocumentTypes:0x8];
702 [webview_ _setDocumentType:0x4];
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];
710 [webview_ setSmoothsFonts:YES];
712 [webview _setUsesLoaderCache:YES];
713 [webview setGroupName:@"Cydia"];
714 [webview _setLayoutInterval:0];
717 [webview_ setDelegate:self];
718 [webview_ setGestureDelegate:self];
719 [scroller_ addSubview:webview_];
721 //NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
723 CGSize indsize = [UIProgressIndicator defaultSizeForStyle:UIProgressIndicatorStyleMediumWhite];
724 indicator_ = [[UIProgressIndicator alloc] initWithFrame:CGRectMake(281, 12, indsize.width, indsize.height)];
725 [indicator_ setStyle:UIProgressIndicatorStyleMediumWhite];
727 Package *package([[Database sharedInstance] packageWithName:@"cydia"]);
728 NSString *application = package == nil ? @"Cydia" : [NSString
729 stringWithFormat:@"Cydia/%@",
731 ]; [webview setApplicationNameForUserAgent:application];
733 indirect_ = [[IndirectDelegate alloc] initWithDelegate:self];
735 [webview setFrameLoadDelegate:self];
736 [webview setResourceLoadDelegate:indirect_];
737 [webview setUIDelegate:self];
738 [webview setScriptDebugDelegate:self];
739 [webview setPolicyDelegate:self];
741 [self setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
742 [scroller_ setAutoresizingMask:UIViewAutoresizingFlexibleHeight];
746 - (void) didFinishGesturesInView:(UIView *)view forEvent:(id)event {
747 [webview_ redrawScaledDocument];
750 - (void) _rightButtonClicked {
751 if (function_ == nil) {
755 WebView *webview([webview_ webView]);
756 WebFrame *frame([webview mainFrame]);
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());
763 if (settings == NULL)
766 no = settings->JavaScriptCanOpenWindowsAutomatically();
767 settings->setJavaScriptCanOpenWindowsAutomatically(true);
770 [delegate_ clearFirstResponder];
771 JSObjectRef function([function_ JSObject]);
772 JSGlobalContextRef context([frame globalContext]);
773 JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL);
775 if (settings != NULL)
776 settings->setJavaScriptCanOpenWindowsAutomatically(no);
780 - (id) _rightButtonTitle {
781 return button_ != nil ? button_ : @"Reload";
784 - (id) rightButtonTitle {
785 return [self _loading] ? @"" : [self _rightButtonTitle];
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;
802 - (NSString *) title {
803 return title_ == nil ? @"Loading" : title_;
806 - (NSString *) backButtonTitle {
810 - (void) setPageActive:(BOOL)active {
812 [indicator_ removeFromSuperview];
814 [[book_ navigationBar] addSubview:indicator_];
817 - (void) resetViewAnimated:(BOOL)animated {
820 - (void) setPushed:(bool)pushed {