From f5f194b3efbdfc92886eebe0b359a25518061fe0 Mon Sep 17 00:00:00 2001 From: Benoit Zanotti Date: Wed, 2 Dec 2015 17:38:52 +0100 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} (cherry picked from commit 704def3b29d00bf39aca6553a079152171af4ad9) Review URL: https://codereview.chromium.org/1491043004 . Cr-Commit-Position: refs/branch-heads/2564@{#201} Cr-Branched-From: 1283eca15bd9f772387f75241576cde7bdec7f54-refs/heads/master@{#359700} --- .../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 072b026e1a0f5..7c0a4780f1236 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 9600772986842..1f838cd80001c 100644 --- a/ios/web/web_state/ui/crw_web_controller.mm +++ b/ios/web/web_state/ui/crw_web_controller.mm @@ -3854,4 +3854,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 547e9f9fe4fa2..b7b9ce8c8ed29 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 @@ -536,17 +536,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 {