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 "mojo/services/html_viewer/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 "media/filters/stream_parser_factory.h"
12 #include "net/base/mime_util.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
18 std::string
ToASCIIOrEmpty(const blink::WebString
& string
) {
19 return base::IsStringASCII(string
) ? base::UTF16ToASCII(string
)
25 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsMIMEType(
26 const blink::WebString
& mime_type
) {
27 return net::IsSupportedMimeType(ToASCIIOrEmpty(mime_type
)) ?
28 blink::WebMimeRegistry::IsSupported
:
29 blink::WebMimeRegistry::IsNotSupported
;
32 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsImageMIMEType(
33 const blink::WebString
& mime_type
) {
34 return net::IsSupportedImageMimeType(ToASCIIOrEmpty(mime_type
)) ?
35 blink::WebMimeRegistry::IsSupported
:
36 blink::WebMimeRegistry::IsNotSupported
;
39 blink::WebMimeRegistry::SupportsType
40 WebMimeRegistryImpl::supportsImagePrefixedMIMEType(
41 const blink::WebString
& mime_type
) {
42 std::string ascii_mime_type
= ToASCIIOrEmpty(mime_type
);
43 return (net::IsSupportedImageMimeType(ascii_mime_type
) ||
44 (StartsWithASCII(ascii_mime_type
, "image/", true) &&
45 net::IsSupportedNonImageMimeType(ascii_mime_type
)))
46 ? WebMimeRegistry::IsSupported
47 : WebMimeRegistry::IsNotSupported
;
50 blink::WebMimeRegistry::SupportsType
51 WebMimeRegistryImpl::supportsJavaScriptMIMEType(
52 const blink::WebString
& mime_type
) {
53 return net::IsSupportedJavascriptMimeType(ToASCIIOrEmpty(mime_type
)) ?
54 blink::WebMimeRegistry::IsSupported
:
55 blink::WebMimeRegistry::IsNotSupported
;
58 blink::WebMimeRegistry::SupportsType
WebMimeRegistryImpl::supportsMediaMIMEType(
59 const blink::WebString
& mime_type
,
60 const blink::WebString
& codecs
,
61 const blink::WebString
& key_system
) {
62 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
63 // Not supporting the container is a flat-out no.
64 if (!net::IsSupportedMediaMimeType(mime_type_ascii
))
65 return IsNotSupported
;
67 // Mojo does not currently support any key systems.
68 if (!key_system
.isEmpty())
69 return IsNotSupported
;
71 // Check list of strict codecs to see if it is supported.
72 if (net::IsStrictMediaMimeType(mime_type_ascii
)) {
73 // Check if the codecs are a perfect match.
74 std::vector
<std::string
> strict_codecs
;
75 net::ParseCodecString(ToASCIIOrEmpty(codecs
), &strict_codecs
, false);
76 return static_cast<WebMimeRegistry::SupportsType
>(
77 net::IsSupportedStrictMediaMimeType(mime_type_ascii
, strict_codecs
));
80 // If we don't recognize the codec, it's possible we support it.
81 std::vector
<std::string
> parsed_codecs
;
82 net::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codecs
, true);
83 if (!net::AreSupportedMediaCodecs(parsed_codecs
))
84 return MayBeSupported
;
86 // Otherwise we have a perfect match.
90 bool WebMimeRegistryImpl::supportsMediaSourceMIMEType(
91 const blink::WebString
& mime_type
,
92 const blink::WebString
& codecs
) {
93 const std::string mime_type_ascii
= ToASCIIOrEmpty(mime_type
);
94 if (mime_type_ascii
.empty())
97 std::vector
<std::string
> parsed_codec_ids
;
98 net::ParseCodecString(ToASCIIOrEmpty(codecs
), &parsed_codec_ids
, false);
99 return media::StreamParserFactory::IsTypeSupported(mime_type_ascii
,
103 bool WebMimeRegistryImpl::supportsEncryptedMediaMIMEType(
104 const blink::WebString
& key_system
,
105 const blink::WebString
& mime_type
,
106 const blink::WebString
& codecs
) {
111 blink::WebMimeRegistry::SupportsType
112 WebMimeRegistryImpl::supportsNonImageMIMEType(
113 const blink::WebString
& mime_type
) {
114 return net::IsSupportedNonImageMimeType(ToASCIIOrEmpty(mime_type
)) ?
115 blink::WebMimeRegistry::IsSupported
:
116 blink::WebMimeRegistry::IsNotSupported
;
119 blink::WebString
WebMimeRegistryImpl::mimeTypeForExtension(
120 const blink::WebString
& file_extension
) {
122 return blink::WebString();
125 blink::WebString
WebMimeRegistryImpl::wellKnownMimeTypeForExtension(
126 const blink::WebString
& file_extension
) {
128 return blink::WebString();
131 blink::WebString
WebMimeRegistryImpl::mimeTypeFromFile(
132 const blink::WebString
& file_path
) {
134 return blink::WebString();