From 704def3b29d00bf39aca6553a079152171af4ad9 Mon Sep 17 00:00:00 2001 From: stkhapugin Date: Mon, 23 Nov 2015 05:49:34 -0800 Subject: [PATCH] Fixes document type detection on iOS 9. On iOS 9, evaluating '' + document always results in [object HTMLDocument], even for PDFs. This introduces another way of checking document type on iOS 9. Unfortunately, it doesn't work on iOS 8, so the old implementation needs to stay too. BUG=549604 Review URL: https://codereview.chromium.org/1458703004 Cr-Commit-Position: refs/heads/master@{#361095} --- .../ui/crw_ui_web_view_web_controller.mm | 27 ++++++++++++++----- .../ui/crw_web_controller+protected.h | 3 +++ ios/web/web_state/ui/crw_web_controller.mm | 14 ++++++++++ .../ui/crw_wk_web_view_web_controller.mm | 13 ++------- 4 files changed, 39 insertions(+), 18 deletions(-) diff --git a/ios/web/web_state/ui/crw_ui_web_view_web_controller.mm b/ios/web/web_state/ui/crw_ui_web_view_web_controller.mm index 8888ca50ed4dc..9bc07e431b9b4 100644 --- a/ios/web/web_state/ui/crw_ui_web_view_web_controller.mm +++ b/ios/web/web_state/ui/crw_ui_web_view_web_controller.mm @@ -4,6 +4,7 @@ #import "ios/web/web_state/ui/crw_ui_web_view_web_controller.h" +#import "base/ios/ios_util.h" #import "base/ios/ns_error_util.h" #import "base/ios/weak_nsobject.h" #include "base/json/json_reader.h" @@ -550,13 +551,25 @@ - (BOOL)isCurrentNavigationItemPOST { if (!_uiWebView) { return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; } - NSString* documentType = - [_uiWebView stringByEvaluatingJavaScriptFromString: - @"'' + document"]; - if ([documentType isEqualToString:@"[object HTMLDocument]"]) - return web::WEB_VIEW_DOCUMENT_TYPE_HTML; - else if ([documentType isEqualToString:@"[object Document]"]) - return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; + + if (base::ios::IsRunningOnIOS9OrLater()) { + // On iOS 9, evaluating '' + document always results in [object + // HTMLDocument], even for PDFs. However, document.contentType is properly + // defined. + NSString* MIMEType = [_uiWebView + stringByEvaluatingJavaScriptFromString:@"document.contentType"]; + return [self documentTypeFromMIMEType:MIMEType]; + } else { + // On iOS 8 and below, document.contentType is always undefined. Use this + // instead. + NSString* documentType = + [_uiWebView stringByEvaluatingJavaScriptFromString:@"'' + document"]; + if ([documentType isEqualToString:@"[object HTMLDocument]"]) + return web::WEB_VIEW_DOCUMENT_TYPE_HTML; + else if ([documentType isEqualToString:@"[object Document]"]) + return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; + } + return web::WEB_VIEW_DOCUMENT_TYPE_UNKNOWN; } diff --git a/ios/web/web_state/ui/crw_web_controller+protected.h b/ios/web/web_state/ui/crw_web_controller+protected.h index 34f27150a9f45..02a76381f2757 100644 --- a/ios/web/web_state/ui/crw_web_controller+protected.h +++ b/ios/web/web_state/ui/crw_web_controller+protected.h @@ -410,6 +410,9 @@ struct NewWindowInfo { // Resets pending external request information. - (void)resetExternalRequest; +// Converts MIME type string to WebViewDocumentType. +- (web::WebViewDocumentType)documentTypeFromMIMEType:(NSString*)MIMEType; + @end #endif // IOS_WEB_WEB_STATE_UI_CRW_WEB_CONTROLLER_PROTECTED_H_ diff --git a/ios/web/web_state/ui/crw_web_controller.mm b/ios/web/web_state/ui/crw_web_controller.mm index 28f95f402d761..43b1f97c2e818 100644 --- a/ios/web/web_state/ui/crw_web_controller.mm +++ b/ios/web/web_state/ui/crw_web_controller.mm @@ -3858,4 +3858,18 @@ - (void)resetExternalRequest { _externalRequest.reset(); } +- (web::WebViewDocumentType)documentTypeFromMIMEType:(NSString*)MIMEType { + if (!MIMEType.length) { + return web::WEB_VIEW_DOCUMENT_TYPE_UNKNOWN; + } + + if ([MIMEType isEqualToString:@"text/html"] || + [MIMEType isEqualToString:@"application/xhtml+xml"] || + [MIMEType isEqualToString:@"application/xml"]) { + return web::WEB_VIEW_DOCUMENT_TYPE_HTML; + } + + return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; +} + @end diff --git a/ios/web/web_state/ui/crw_wk_web_view_web_controller.mm b/ios/web/web_state/ui/crw_wk_web_view_web_controller.mm index 8fbeae696f023..c1be173796985 100644 --- a/ios/web/web_state/ui/crw_wk_web_view_web_controller.mm +++ b/ios/web/web_state/ui/crw_wk_web_view_web_controller.mm @@ -517,17 +517,8 @@ - (BOOL)isCurrentNavigationItemPOST { return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; } - std::string mimeType = self.webState->GetContentsMimeType(); - if (mimeType.empty()) { - return web::WEB_VIEW_DOCUMENT_TYPE_UNKNOWN; - } - - if (mimeType == "text/html" || mimeType == "application/xhtml+xml" || - mimeType == "application/xml") { - return web::WEB_VIEW_DOCUMENT_TYPE_HTML; - } - - return web::WEB_VIEW_DOCUMENT_TYPE_GENERIC; + std::string MIMEType = self.webState->GetContentsMimeType(); + return [self documentTypeFromMIMEType:base::SysUTF8ToNSString(MIMEType)]; } - (void)loadRequest:(NSMutableURLRequest*)request {