Skip to content
This repository has been archived by the owner on Apr 3, 2020. It is now read-only.

Commit

Permalink
Fixes document type detection on iOS 9.
Browse files Browse the repository at this point in the history
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 704def3)

Review URL: https://codereview.chromium.org/1491043004 .

Cr-Commit-Position: refs/branch-heads/2564@{#201}
Cr-Branched-From: 1283eca-refs/heads/master@{#359700}
  • Loading branch information
Jicks committed Dec 2, 2015
1 parent 8761add commit f5f194b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
27 changes: 20 additions & 7 deletions ios/web/web_state/ui/crw_ui_web_view_web_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 3 additions & 0 deletions ios/web/web_state/ui/crw_web_controller+protected.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
14 changes: 14 additions & 0 deletions ios/web/web_state/ui/crw_web_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 2 additions & 11 deletions ios/web/web_state/ui/crw_wk_web_view_web_controller.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit f5f194b

Please sign in to comment.