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 "components/html_viewer/web_mime_registry_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 "media/base/key_systems.h"
13 #include "media/base/mime_util.h"
14 #include "media/filters/stream_parser_factory.h"
15 #include "net/base/mime_util.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
18 namespace html_viewer
{
21 std::string
ToASCIIOrEmpty(const blink::WebString
& string
) {
22 return base::IsStringASCII(string
)
23 ? base::UTF16ToASCII(base::StringPiece16(string
))
29 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsMIMEType(
30 const blink::WebString
& mime_type
) {
31 return mime_util::IsSupportedMimeType(ToASCIIOrEmpty(mime_type
))
32 ? blink::WebMimeRegistry::IsSupported
33 : blink::WebMimeRegistry::IsNotSupported
;
36 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsImageMIMEType(
37 const blink::WebString
& mime_type
) {
38 return mime_util::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type
))
39 ? blink::WebMimeRegistry::IsSupported
40 : blink::WebMimeRegistry::IsNotSupported
;
43 blink::WebMimeRegistry::SupportsType
44 WebMimeRegistryImpl::supportsImagePrefixedMIMEType(
45 const blink::WebString
& mime_type
) {
46 std::string ascii_mime_type
= ToASCIIOrEmpty(mime_type
);
47 return (mime_util::IsSupportedImageMimeType(ascii_mime_type
) ||
48 (base::StartsWith(ascii_mime_type
, "image/",
49 base::CompareCase::SENSITIVE
) &&
50 mime_util::IsSupportedNonImageMimeType(ascii_mime_type
)))
51 ? WebMimeRegistry::IsSupported
52 : WebMimeRegistry::IsNotSupported
;
55 blink::WebMimeRegistry::SupportsType
56 WebMimeRegistryImpl::supportsJavaScriptMIMEType(
57 const blink::WebString
& mime_type
) {
58 return mime_util::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type
))
59 ? blink::WebMimeRegistry::IsSupported
60 : blink::WebMimeRegistry::IsNotSupported
;
63 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsMediaMIMEType(
64 const blink::WebString
& mime_type
,
65 const blink::WebString
& codecs
,
66 const blink::WebString
& key_system
) {
67 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
68 // Not supporting the container is a flat-out no.
69 if (!media::IsSupportedMediaMimeType(mime_type_ascii
))
70 return IsNotSupported
;
72 // Mojo does not currently support any key systems.
73 if (!key_system
.isEmpty())
74 return IsNotSupported
;
76 // Check list of strict codecs to see if it is supported.
77 if (media::IsStrictMediaMimeType(mime_type_ascii
)) {
78 // Check if the codecs are a perfect match.
79 std::vector
<std::string
> strict_codecs
;
80 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &strict_codecs
, false);
81 return static_cast<WebMimeRegistry::SupportsType
>(
82 media::IsSupportedStrictMediaMimeType(mime_type_ascii
, strict_codecs
));
85 // If we don't recognize the codec, it's possible we support it.
86 std::vector
<std::string
> parsed_codecs
;
87 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codecs
, true);
88 if (!media::AreSupportedMediaCodecs(parsed_codecs
))
89 return MayBeSupported
;
91 // Otherwise we have a perfect match.
95 bool WebMimeRegistryImpl::supportsMediaSourceMIMEType(
96 const blink::WebString
& mime_type
,
97 const blink::WebString
& codecs
) {
98 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
99 if (mime_type_ascii
.empty())
102 std::vector
<std::string
> parsed_codec_ids
;
103 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codec_ids
, false);
104 return media::StreamParserFactory::IsTypeSupported(mime_type_ascii
,
108 blink::WebMimeRegistry::SupportsType
109 WebMimeRegistryImpl::supportsNonImageMIMEType(
110 const blink::WebString
& mime_type
) {
111 return mime_util::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type
))
112 ? blink::WebMimeRegistry::IsSupported
113 : blink::WebMimeRegistry::IsNotSupported
;
116 blink::WebString
WebMimeRegistryImpl::mimeTypeForExtension(
117 const blink::WebString
& file_extension
) {
118 std::string mime_type
;
119 net::GetMimeTypeFromExtension(
120 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
);
121 return blink::WebString::fromUTF8(mime_type
);
124 blink::WebString
WebMimeRegistryImpl::wellKnownMimeTypeForExtension(
125 const blink::WebString
& file_extension
) {
126 std::string mime_type
;
127 net::GetWellKnownMimeTypeFromExtension(
128 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
);
129 return blink::WebString::fromUTF8(mime_type
);
132 blink::WebString
WebMimeRegistryImpl::mimeTypeFromFile(
133 const blink::WebString
& file_path
) {
134 std::string mime_type
;
135 net::GetMimeTypeFromFile(base::FilePath::FromUTF16Unsafe(file_path
),
137 return blink::WebString::fromUTF8(mime_type
);
140 } // namespace html_viewer