Commit eee56c28 authored by Yi Su's avatar Yi Su Committed by Commit Bot

Add content_type_util.* to //ios/web/web_view.

This CL extracts the content-type utility into
//ios/web/web_view/content_type_util.* so that it can be shared between
CRWWebController, WebStateImpl and CRWWKNavigationHandler.

Bug: 956511
Change-Id: I0097d7b89c0f0f972beadf7753c34097a0c0847e
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1634730
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#665494}
parent 36fcae05
...@@ -22,6 +22,7 @@ source_set("web_state") { ...@@ -22,6 +22,7 @@ source_set("web_state") {
"//ios/web/session", "//ios/web/session",
"//ios/web/web_state/ui", "//ios/web/web_state/ui",
"//ios/web/web_state/ui:crw_web_view_navigation_proxy", "//ios/web/web_state/ui:crw_web_view_navigation_proxy",
"//ios/web/web_view:util",
"//ios/web/webui", "//ios/web/webui",
"//net", "//net",
"//ui/gfx", "//ui/gfx",
......
...@@ -129,6 +129,7 @@ class WebStateImpl; ...@@ -129,6 +129,7 @@ class WebStateImpl;
- (BOOL)isViewAlive; - (BOOL)isViewAlive;
// Returns YES if the current live view is a web view with HTML. // Returns YES if the current live view is a web view with HTML.
// TODO(crbug.com/949651): Remove once JSFindInPageManager is removed.
- (BOOL)contentIsHTML; - (BOOL)contentIsHTML;
// Returns the CRWWebController's view of the current URL. Moreover, this method // Returns the CRWWebController's view of the current URL. Moreover, this method
......
...@@ -114,6 +114,7 @@ ...@@ -114,6 +114,7 @@
#import "ios/web/web_state/user_interaction_state.h" #import "ios/web/web_state/user_interaction_state.h"
#import "ios/web/web_state/web_state_impl.h" #import "ios/web/web_state/web_state_impl.h"
#import "ios/web/web_state/web_view_internal_creation_util.h" #import "ios/web/web_state/web_view_internal_creation_util.h"
#import "ios/web/web_view/content_type_util.h"
#import "ios/web/web_view/error_translation_util.h" #import "ios/web/web_view/error_translation_util.h"
#import "ios/web/web_view/wk_web_view_util.h" #import "ios/web/web_view/wk_web_view_util.h"
#import "net/base/mac/url_conversions.h" #import "net/base/mac/url_conversions.h"
...@@ -857,12 +858,14 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*); ...@@ -857,12 +858,14 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*);
} }
- (BOOL)contentIsHTML { - (BOOL)contentIsHTML {
if (!self.webView) return self.webView &&
return NO; web::IsContentTypeHtml(self.webState->GetContentsMimeType());
}
std::string MIMEType = self.webState->GetContentsMimeType(); // Returns YES if the current live view is a web view with an image MIME type.
return MIMEType == "text/html" || MIMEType == "application/xhtml+xml" || - (BOOL)contentIsImage {
MIMEType == "application/xml"; return self.webView &&
web::IsContentTypeImage(self.webState->GetContentsMimeType());
} }
- (GURL)currentURLWithTrustLevel:(web::URLVerificationTrustLevel*)trustLevel { - (GURL)currentURLWithTrustLevel:(web::URLVerificationTrustLevel*)trustLevel {
...@@ -4232,16 +4235,6 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*); ...@@ -4232,16 +4235,6 @@ typedef void (^ViewportStateCompletion)(const web::PageViewportState*);
return provisionalLoad; return provisionalLoad;
} }
// Returns YES if the current live view is a web view with an image MIME type.
- (BOOL)contentIsImage {
if (!self.webView)
return NO;
const std::string image = "image";
std::string MIMEType = self.webState->GetContentsMimeType();
return MIMEType.compare(0, image.length(), image) == 0;
}
#pragma mark - CRWSSLStatusUpdaterDataSource #pragma mark - CRWSSLStatusUpdaterDataSource
- (void)SSLStatusUpdater:(CRWSSLStatusUpdater*)SSLStatusUpdater - (void)SSLStatusUpdater:(CRWSSLStatusUpdater*)SSLStatusUpdater
......
...@@ -6,6 +6,8 @@ import("//ios/build/config.gni") ...@@ -6,6 +6,8 @@ import("//ios/build/config.gni")
source_set("util") { source_set("util") {
sources = [ sources = [
"content_type_util.cc",
"content_type_util.h",
"error_translation_util.h", "error_translation_util.h",
"error_translation_util.mm", "error_translation_util.mm",
"wk_web_view_util.h", "wk_web_view_util.h",
...@@ -38,6 +40,7 @@ source_set("unittests") { ...@@ -38,6 +40,7 @@ source_set("unittests") {
] ]
sources = [ sources = [
"content_type_util_unittest.cc",
"error_translation_util_unittest.mm", "error_translation_util_unittest.mm",
"wk_web_view_util_unittest.mm", "wk_web_view_util_unittest.mm",
] ]
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/web/web_view/content_type_util.h"
#include "base/stl_util.h"
namespace web {
bool IsContentTypeHtml(const std::string& mime_type) {
return mime_type == "text/html" || mime_type == "application/xhtml+xml" ||
mime_type == "application/xml";
}
bool IsContentTypeImage(const std::string& mime_type) {
const std::string image = "image";
return mime_type.compare(0, image.size(), image) == 0;
}
} // namespace web
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_WEB_WEB_VIEW_CONTENT_TYPE_UTIL_H_
#define IOS_WEB_WEB_VIEW_CONTENT_TYPE_UTIL_H_
#include <string>
namespace web {
// Returns true if |mime_type| is one of:
// 1. text/html;
// 2. application/xhtml+xml;
// 3. application/xml.
bool IsContentTypeHtml(const std::string& mime_type);
// Returns true if |mime_type| begins with "image".
bool IsContentTypeImage(const std::string& mime_type);
} // namespace web
#endif // IOS_WEB_WEB_VIEW_CONTENT_TYPE_UTIL_H_
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ios/web/web_view/content_type_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace web {
class ContentTypeUtilTest : public PlatformTest {};
TEST_F(ContentTypeUtilTest, TestIsContentTypeHtml) {
EXPECT_TRUE(IsContentTypeHtml("text/html"));
EXPECT_TRUE(IsContentTypeHtml("application/xhtml+xml"));
EXPECT_TRUE(IsContentTypeHtml("application/xml"));
EXPECT_FALSE(IsContentTypeHtml("text/xhtml"));
EXPECT_FALSE(IsContentTypeHtml("application"));
EXPECT_FALSE(IsContentTypeHtml("application/html"));
EXPECT_FALSE(IsContentTypeHtml("application/xhtml"));
}
TEST_F(ContentTypeUtilTest, TestIsContentTypeImage) {
EXPECT_TRUE(IsContentTypeImage("image/bmp"));
EXPECT_TRUE(IsContentTypeImage("image/gif"));
EXPECT_TRUE(IsContentTypeImage("image/png"));
EXPECT_TRUE(IsContentTypeImage("image/x-icon"));
EXPECT_FALSE(IsContentTypeImage("imag"));
EXPECT_FALSE(IsContentTypeImage("text/html"));
}
} // namespace web
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment