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 "components/mime_util/mime_util.h"
12 #include "net/base/mime_util.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
15 using blink::WebString
;
16 using blink::WebMimeRegistry
;
21 std::string
SimpleWebMimeRegistryImpl::ToASCIIOrEmpty(const WebString
& string
) {
22 return base::IsStringASCII(string
) ? base::UTF16ToASCII(string
)
26 WebMimeRegistry::SupportsType
SimpleWebMimeRegistryImpl::supportsMIMEType(
27 const WebString
& mime_type
) {
28 return mime_util::IsSupportedMimeType(ToASCIIOrEmpty(mime_type
))
29 ? WebMimeRegistry::IsSupported
30 : WebMimeRegistry::IsNotSupported
;
33 WebMimeRegistry::SupportsType
SimpleWebMimeRegistryImpl::supportsImageMIMEType(
34 const WebString
& mime_type
) {
35 return mime_util::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type
))
36 ? WebMimeRegistry::IsSupported
37 : WebMimeRegistry::IsNotSupported
;
40 WebMimeRegistry::SupportsType
41 SimpleWebMimeRegistryImpl::supportsImagePrefixedMIMEType(
42 const WebString
& mime_type
) {
43 std::string ascii_mime_type
= ToASCIIOrEmpty(mime_type
);
44 return (mime_util::IsSupportedImageMimeType(ascii_mime_type
) ||
45 (base::StartsWithASCII(ascii_mime_type
, "image/", true) &&
46 mime_util::IsSupportedNonImageMimeType(ascii_mime_type
)))
47 ? WebMimeRegistry::IsSupported
48 : WebMimeRegistry::IsNotSupported
;
51 WebMimeRegistry::SupportsType
52 SimpleWebMimeRegistryImpl::supportsJavaScriptMIMEType(
53 const WebString
& mime_type
) {
54 return mime_util::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type
))
55 ? WebMimeRegistry::IsSupported
56 : WebMimeRegistry::IsNotSupported
;
59 // When debugging layout tests failures in the test shell,
60 // see TestShellWebMimeRegistryImpl.
61 WebMimeRegistry::SupportsType
SimpleWebMimeRegistryImpl::supportsMediaMIMEType(
62 const WebString
& mime_type
,
63 const WebString
& codecs
,
64 const WebString
& key_system
) {
65 // Media features are only supported at the content/renderer/ layer.
66 return IsNotSupported
;
69 bool SimpleWebMimeRegistryImpl::supportsMediaSourceMIMEType(
70 const WebString
& mime_type
,
71 const WebString
& codecs
) {
72 // Media features are only supported at the content/renderer layer.
76 WebMimeRegistry::SupportsType
77 SimpleWebMimeRegistryImpl::supportsNonImageMIMEType(
78 const WebString
& mime_type
) {
79 return mime_util::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type
))
80 ? WebMimeRegistry::IsSupported
81 : WebMimeRegistry::IsNotSupported
;
84 WebString
SimpleWebMimeRegistryImpl::mimeTypeForExtension(
85 const WebString
& file_extension
) {
86 std::string mime_type
;
87 net::GetMimeTypeFromExtension(
88 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
);
89 return WebString::fromUTF8(mime_type
);
92 WebString
SimpleWebMimeRegistryImpl::wellKnownMimeTypeForExtension(
93 const WebString
& file_extension
) {
94 std::string mime_type
;
95 net::GetWellKnownMimeTypeFromExtension(
96 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
);
97 return WebString::fromUTF8(mime_type
);
100 WebString
SimpleWebMimeRegistryImpl::mimeTypeFromFile(
101 const WebString
& file_path
) {
102 std::string mime_type
;
103 net::GetMimeTypeFromFile(base::FilePath::FromUTF16Unsafe(file_path
),
105 return WebString::fromUTF8(mime_type
);
108 } // namespace content