Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / simple_webmimeregistry_impl.cc
blobfbf264632d7f5303246425d7de66e0b0a73cf081
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/child/simple_webmimeregistry_impl.h"
7 #include "base/files/file_path.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/sys_string_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "net/base/mime_util.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
14 using blink::WebString;
15 using blink::WebMimeRegistry;
17 namespace content {
19 //static
20 std::string SimpleWebMimeRegistryImpl::ToASCIIOrEmpty(const WebString& string) {
21 return base::IsStringASCII(string) ? base::UTF16ToASCII(string)
22 : std::string();
25 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMIMEType(
26 const WebString& mime_type) {
27 return net::IsSupportedMimeType(ToASCIIOrEmpty(mime_type)) ?
28 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
31 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsImageMIMEType(
32 const WebString& mime_type) {
33 return net::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type)) ?
34 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
37 WebMimeRegistry::SupportsType
38 SimpleWebMimeRegistryImpl::supportsImagePrefixedMIMEType(
39 const WebString& mime_type) {
40 std::string ascii_mime_type = ToASCIIOrEmpty(mime_type);
41 return (net::IsSupportedImageMimeType(ascii_mime_type) ||
42 (StartsWithASCII(ascii_mime_type, "image/", true) &&
43 net::IsSupportedNonImageMimeType(ascii_mime_type))) ?
44 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
47 WebMimeRegistry::SupportsType
48 SimpleWebMimeRegistryImpl::supportsJavaScriptMIMEType(
49 const WebString& mime_type) {
50 return net::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type)) ?
51 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
54 // When debugging layout tests failures in the test shell,
55 // see TestShellWebMimeRegistryImpl.
56 WebMimeRegistry::SupportsType SimpleWebMimeRegistryImpl::supportsMediaMIMEType(
57 const WebString& mime_type,
58 const WebString& codecs,
59 const WebString& key_system) {
60 // Media features are only supported at the content/renderer/ layer.
61 return IsNotSupported;
64 bool SimpleWebMimeRegistryImpl::supportsMediaSourceMIMEType(
65 const WebString& mime_type,
66 const WebString& codecs) {
67 // Media features are only supported at the content/renderer layer.
68 return false;
71 WebMimeRegistry::SupportsType
72 SimpleWebMimeRegistryImpl::supportsNonImageMIMEType(
73 const WebString& mime_type) {
74 return net::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type)) ?
75 WebMimeRegistry::IsSupported : WebMimeRegistry::IsNotSupported;
78 WebString SimpleWebMimeRegistryImpl::mimeTypeForExtension(
79 const WebString& file_extension) {
80 std::string mime_type;
81 net::GetMimeTypeFromExtension(
82 base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type);
83 return WebString::fromUTF8(mime_type);
86 WebString SimpleWebMimeRegistryImpl::wellKnownMimeTypeForExtension(
87 const WebString& file_extension) {
88 std::string mime_type;
89 net::GetWellKnownMimeTypeFromExtension(
90 base::FilePath::FromUTF16Unsafe(file_extension).value(), &mime_type);
91 return WebString::fromUTF8(mime_type);
94 WebString SimpleWebMimeRegistryImpl::mimeTypeFromFile(
95 const WebString& file_path) {
96 std::string mime_type;
97 net::GetMimeTypeFromFile(base::FilePath::FromUTF16Unsafe(file_path),
98 &mime_type);
99 return WebString::fromUTF8(mime_type);
102 } // namespace content