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
) ? base::UTF16ToASCII(string
)
28 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsMIMEType(
29 const blink::WebString
& mime_type
) {
30 return mime_util::IsSupportedMimeType(ToASCIIOrEmpty(mime_type
))
31 ? blink::WebMimeRegistry::IsSupported
32 : blink::WebMimeRegistry::IsNotSupported
;
35 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsImageMIMEType(
36 const blink::WebString
& mime_type
) {
37 return mime_util::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type
))
38 ? blink::WebMimeRegistry::IsSupported
39 : blink::WebMimeRegistry::IsNotSupported
;
42 blink::WebMimeRegistry::SupportsType
43 WebMimeRegistryImpl::supportsImagePrefixedMIMEType(
44 const blink::WebString
& mime_type
) {
45 std::string ascii_mime_type
= ToASCIIOrEmpty(mime_type
);
46 return (mime_util::IsSupportedImageMimeType(ascii_mime_type
) ||
47 (base::StartsWithASCII(ascii_mime_type
, "image/", true) &&
48 mime_util::IsSupportedNonImageMimeType(ascii_mime_type
)))
49 ? WebMimeRegistry::IsSupported
50 : WebMimeRegistry::IsNotSupported
;
53 blink::WebMimeRegistry::SupportsType
54 WebMimeRegistryImpl::supportsJavaScriptMIMEType(
55 const blink::WebString
& mime_type
) {
56 return mime_util::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type
))
57 ? blink::WebMimeRegistry::IsSupported
58 : blink::WebMimeRegistry::IsNotSupported
;
61 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsMediaMIMEType(
62 const blink::WebString
& mime_type
,
63 const blink::WebString
& codecs
,
64 const blink::WebString
& key_system
) {
65 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
66 // Not supporting the container is a flat-out no.
67 if (!media::IsSupportedMediaMimeType(mime_type_ascii
))
68 return IsNotSupported
;
70 // Mojo does not currently support any key systems.
71 if (!key_system
.isEmpty())
72 return IsNotSupported
;
74 // Check list of strict codecs to see if it is supported.
75 if (media::IsStrictMediaMimeType(mime_type_ascii
)) {
76 // Check if the codecs are a perfect match.
77 std::vector
<std::string
> strict_codecs
;
78 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &strict_codecs
, false);
79 return static_cast<WebMimeRegistry::SupportsType
>(
80 media::IsSupportedStrictMediaMimeType(mime_type_ascii
, strict_codecs
));
83 // If we don't recognize the codec, it's possible we support it.
84 std::vector
<std::string
> parsed_codecs
;
85 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codecs
, true);
86 if (!media::AreSupportedMediaCodecs(parsed_codecs
))
87 return MayBeSupported
;
89 // Otherwise we have a perfect match.
93 bool WebMimeRegistryImpl::supportsMediaSourceMIMEType(
94 const blink::WebString
& mime_type
,
95 const blink::WebString
& codecs
) {
96 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
97 if (mime_type_ascii
.empty())
100 std::vector
<std::string
> parsed_codec_ids
;
101 media::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codec_ids
, false);
102 return media::StreamParserFactory::IsTypeSupported(mime_type_ascii
,
106 blink::WebMimeRegistry::SupportsType
107 WebMimeRegistryImpl::supportsNonImageMIMEType(
108 const blink::WebString
& mime_type
) {
109 return mime_util::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type
))
110 ? blink::WebMimeRegistry::IsSupported
111 : blink::WebMimeRegistry::IsNotSupported
;
114 blink::WebString
WebMimeRegistryImpl::mimeTypeForExtension(
115 const blink::WebString
& file_extension
) {
116 std::string mime_type
;
117 net::GetMimeTypeFromExtension(
118 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
);
119 return blink::WebString::fromUTF8(mime_type
);
122 blink::WebString
WebMimeRegistryImpl::wellKnownMimeTypeForExtension(
123 const blink::WebString
& file_extension
) {
124 std::string mime_type
;
125 net::GetWellKnownMimeTypeFromExtension(
126 base::FilePath::FromUTF16Unsafe(file_extension
).value(), &mime_type
);
127 return blink::WebString::fromUTF8(mime_type
);
130 blink::WebString
WebMimeRegistryImpl::mimeTypeFromFile(
131 const blink::WebString
& file_path
) {
132 std::string mime_type
;
133 net::GetMimeTypeFromFile(base::FilePath::FromUTF16Unsafe(file_path
),
135 return blink::WebString::fromUTF8(mime_type
);
138 } // namespace html_viewer