1 // Copyright (c) 2012 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.
10 #include "base/containers/hash_tables.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/stl_util.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/strings/string_util.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "net/base/mime_util.h"
19 #include "net/base/platform_mime_util.h"
20 #include "net/http/http_util.h"
22 #if defined(OS_ANDROID)
23 #include "base/android/build_info.h"
32 const char matcher
[13];
35 static const MediaType kIanaMediaTypes
[] = {
36 { "application", "application/" },
37 { "audio", "audio/" },
38 { "example", "example/" },
39 { "image", "image/" },
40 { "message", "message/" },
41 { "model", "model/" },
42 { "multipart", "multipart/" },
44 { "video", "video/" },
51 // Singleton utility class for mime types.
52 class MimeUtil
: public PlatformMimeUtil
{
73 bool GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
74 std::string
* mime_type
) const;
76 bool GetMimeTypeFromFile(const base::FilePath
& file_path
,
77 std::string
* mime_type
) const;
79 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
80 std::string
* mime_type
) const;
82 bool IsSupportedImageMimeType(const std::string
& mime_type
) const;
83 bool IsSupportedMediaMimeType(const std::string
& mime_type
) const;
84 bool IsSupportedNonImageMimeType(const std::string
& mime_type
) const;
85 bool IsUnsupportedTextMimeType(const std::string
& mime_type
) const;
86 bool IsSupportedJavascriptMimeType(const std::string
& mime_type
) const;
88 bool IsSupportedMimeType(const std::string
& mime_type
) const;
90 bool MatchesMimeType(const std::string
&mime_type_pattern
,
91 const std::string
&mime_type
) const;
93 bool ParseMimeTypeWithoutParameter(const std::string
& type_string
,
94 std::string
* top_level_type
,
95 std::string
* subtype
) const;
97 bool IsValidTopLevelMimeType(const std::string
& type_string
) const;
99 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) const;
101 void ParseCodecString(const std::string
& codecs
,
102 std::vector
<std::string
>* codecs_out
,
105 bool IsStrictMediaMimeType(const std::string
& mime_type
) const;
106 SupportsType
IsSupportedStrictMediaMimeType(
107 const std::string
& mime_type
,
108 const std::vector
<std::string
>& codecs
) const;
110 void RemoveProprietaryMediaTypesAndCodecsForTests();
113 friend struct base::DefaultLazyInstanceTraits
<MimeUtil
>;
115 typedef base::hash_set
<std::string
> MimeMappings
;
117 typedef base::hash_set
<int> CodecSet
;
118 typedef std::map
<std::string
, CodecSet
> StrictMappings
;
120 CodecEntry() : codec(INVALID_CODEC
), is_ambiguous(true) {}
121 CodecEntry(Codec c
, bool ambiguous
) : codec(c
), is_ambiguous(ambiguous
) {}
125 typedef std::map
<std::string
, CodecEntry
> StringToCodecMappings
;
129 // Returns IsSupported if all codec IDs in |codecs| are unambiguous
130 // and are supported by the platform. MayBeSupported is returned if
131 // at least one codec ID in |codecs| is ambiguous but all the codecs
132 // are supported by the platform. IsNotSupported is returned if at
133 // least one codec ID is not supported by the platform.
134 SupportsType
AreSupportedCodecs(
135 const CodecSet
& supported_codecs
,
136 const std::vector
<std::string
>& codecs
) const;
138 // For faster lookup, keep hash sets.
139 void InitializeMimeTypeMaps();
141 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType
& ext
,
142 bool include_platform_types
,
143 std::string
* mime_type
) const;
145 // Converts a codec ID into an Codec enum value and indicates
146 // whether the conversion was ambiguous.
147 // Returns true if this method was able to map |codec_id| to a specific
148 // Codec enum value. |codec| and |is_ambiguous| are only valid if true
149 // is returned. Otherwise their value is undefined after the call.
150 // |is_ambiguous| is true if |codec_id| did not have enough information to
151 // unambiguously determine the proper Codec enum value. If |is_ambiguous|
152 // is true |codec| contains the best guess for the intended Codec enum value.
153 bool StringToCodec(const std::string
& codec_id
,
155 bool* is_ambiguous
) const;
157 // Returns true if |codec| is supported by the platform.
158 // Note: This method will return false if the platform supports proprietary
159 // codecs but |allow_proprietary_codecs_| is set to false.
160 bool IsCodecSupported(Codec codec
) const;
162 // Returns true if |codec| refers to a proprietary codec.
163 bool IsCodecProprietary(Codec codec
) const;
165 // Returns true and sets |*default_codec| if |mime_type| has a
166 // default codec associated with it.
167 // Returns false otherwise and the value of |*default_codec| is undefined.
168 bool GetDefaultCodec(const std::string
& mime_type
,
169 Codec
* default_codec
) const;
171 // Returns true if |mime_type| has a default codec associated with it
172 // and IsCodecSupported() returns true for that particular codec.
173 bool IsDefaultCodecSupported(const std::string
& mime_type
) const;
175 MimeMappings image_map_
;
176 MimeMappings media_map_
;
177 MimeMappings non_image_map_
;
178 MimeMappings unsupported_text_map_
;
179 MimeMappings javascript_map_
;
181 // A map of mime_types and hash map of the supported codecs for the mime_type.
182 StrictMappings strict_format_map_
;
184 // Keeps track of whether proprietary codec support should be
185 // advertised to callers.
186 bool allow_proprietary_codecs_
;
188 // Lookup table for string compare based string -> Codec mappings.
189 StringToCodecMappings string_to_codec_map_
;
192 // This variable is Leaky because we need to access it from WorkerPool threads.
193 static base::LazyInstance
<MimeUtil
>::Leaky g_mime_util
=
194 LAZY_INSTANCE_INITIALIZER
;
197 const char* mime_type
;
198 const char* extensions
; // comma separated list
201 static const MimeInfo primary_mappings
[] = {
202 { "text/html", "html,htm,shtml,shtm" },
203 { "text/css", "css" },
204 { "text/xml", "xml" },
205 { "image/gif", "gif" },
206 { "image/jpeg", "jpeg,jpg" },
207 { "image/webp", "webp" },
208 { "image/png", "png" },
209 { "video/mp4", "mp4,m4v" },
210 { "audio/x-m4a", "m4a" },
211 { "audio/mp3", "mp3" },
212 { "video/ogg", "ogv,ogm" },
213 { "audio/ogg", "ogg,oga,opus" },
214 { "video/webm", "webm" },
215 { "audio/webm", "webm" },
216 { "audio/wav", "wav" },
217 { "application/xhtml+xml", "xhtml,xht,xhtm" },
218 { "application/x-chrome-extension", "crx" },
219 { "multipart/related", "mhtml,mht" }
222 static const MimeInfo secondary_mappings
[] = {
223 { "application/octet-stream", "exe,com,bin" },
224 { "application/gzip", "gz" },
225 { "application/pdf", "pdf" },
226 { "application/postscript", "ps,eps,ai" },
227 { "application/javascript", "js" },
228 { "application/font-woff", "woff" },
229 { "image/bmp", "bmp" },
230 { "image/x-icon", "ico" },
231 { "image/vnd.microsoft.icon", "ico" },
232 { "image/jpeg", "jfif,pjpeg,pjp" },
233 { "image/tiff", "tiff,tif" },
234 { "image/x-xbitmap", "xbm" },
235 { "image/svg+xml", "svg,svgz" },
236 { "image/x-png", "png"},
237 { "message/rfc822", "eml" },
238 { "text/plain", "txt,text" },
239 { "text/html", "ehtml" },
240 { "application/rss+xml", "rss" },
241 { "application/rdf+xml", "rdf" },
242 { "text/xml", "xsl,xbl,xslt" },
243 { "application/vnd.mozilla.xul+xml", "xul" },
244 { "application/x-shockwave-flash", "swf,swl" },
245 { "application/pkcs7-mime", "p7m,p7c,p7z" },
246 { "application/pkcs7-signature", "p7s" },
247 { "application/x-mpegurl", "m3u8" },
250 static const char* FindMimeType(const MimeInfo
* mappings
,
253 size_t ext_len
= strlen(ext
);
255 for (size_t i
= 0; i
< mappings_len
; ++i
) {
256 const char* extensions
= mappings
[i
].extensions
;
258 size_t end_pos
= strcspn(extensions
, ",");
259 if (end_pos
== ext_len
&&
260 base::strncasecmp(extensions
, ext
, ext_len
) == 0)
261 return mappings
[i
].mime_type
;
262 extensions
+= end_pos
;
265 extensions
+= 1; // skip over comma
271 bool MimeUtil::GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
272 string
* result
) const {
273 return GetMimeTypeFromExtensionHelper(ext
, true, result
);
276 bool MimeUtil::GetWellKnownMimeTypeFromExtension(
277 const base::FilePath::StringType
& ext
,
278 string
* result
) const {
279 return GetMimeTypeFromExtensionHelper(ext
, false, result
);
282 bool MimeUtil::GetMimeTypeFromFile(const base::FilePath
& file_path
,
283 string
* result
) const {
284 base::FilePath::StringType file_name_str
= file_path
.Extension();
285 if (file_name_str
.empty())
287 return GetMimeTypeFromExtension(file_name_str
.substr(1), result
);
290 bool MimeUtil::GetMimeTypeFromExtensionHelper(
291 const base::FilePath::StringType
& ext
,
292 bool include_platform_types
,
293 string
* result
) const {
294 // Avoids crash when unable to handle a long file path. See crbug.com/48733.
295 const unsigned kMaxFilePathSize
= 65536;
296 if (ext
.length() > kMaxFilePathSize
)
299 // We implement the same algorithm as Mozilla for mapping a file extension to
300 // a mime type. That is, we first check a hard-coded list (that cannot be
301 // overridden), and then if not found there, we defer to the system registry.
302 // Finally, we scan a secondary hard-coded list to catch types that we can
303 // deduce but that we also want to allow the OS to override.
305 base::FilePath
path_ext(ext
);
306 const string ext_narrow_str
= path_ext
.AsUTF8Unsafe();
307 const char* mime_type
= FindMimeType(primary_mappings
,
308 arraysize(primary_mappings
),
309 ext_narrow_str
.c_str());
315 if (include_platform_types
&& GetPlatformMimeTypeFromExtension(ext
, result
))
318 mime_type
= FindMimeType(secondary_mappings
, arraysize(secondary_mappings
),
319 ext_narrow_str
.c_str());
328 // From WebKit's WebCore/platform/MIMETypeRegistry.cpp:
330 static const char* const supported_image_types
[] = {
338 "image/vnd.microsoft.icon", // ico
339 "image/x-icon", // ico
340 "image/x-xbitmap", // xbm
344 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type
345 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php
346 // This set of codecs is supported by all variations of Chromium.
347 static const char* const common_media_types
[] = {
351 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
363 #if defined(OS_ANDROID)
364 // HLS. Supported by Android ICS and above.
365 "application/vnd.apple.mpegurl",
366 "application/x-mpegurl",
370 // List of proprietary types only supported by Google Chrome.
371 static const char* const proprietary_media_types
[] = {
383 #if defined(ENABLE_MPEG2TS_STREAM_PARSER)
390 // - does not include javascript types list (see supported_javascript_types)
391 // - does not include types starting with "text/" (see
392 // IsSupportedNonImageMimeType())
393 static const char* const supported_non_image_types
[] = {
394 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type
396 "application/atom+xml",
397 "application/rss+xml",
398 "application/xhtml+xml",
400 "multipart/related", // For MHTML support.
401 "multipart/x-mixed-replace"
402 // Note: ADDING a new type here will probably render it AS HTML. This can
403 // result in cross site scripting.
406 // Dictionary of cryptographic file mime types.
407 struct CertificateMimeTypeInfo
{
408 const char* mime_type
;
409 CertificateMimeType cert_type
;
412 static const CertificateMimeTypeInfo supported_certificate_types
[] = {
413 { "application/x-x509-user-cert",
414 CERTIFICATE_MIME_TYPE_X509_USER_CERT
},
415 #if defined(OS_ANDROID)
416 { "application/x-x509-ca-cert", CERTIFICATE_MIME_TYPE_X509_CA_CERT
},
417 { "application/x-pkcs12", CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE
},
421 // These types are excluded from the logic that allows all text/ types because
422 // while they are technically text, it's very unlikely that a user expects to
423 // see them rendered in text form.
424 static const char* const unsupported_text_types
[] = {
438 "text/comma-separated-values",
440 "text/tab-separated-values",
442 "text/ofx", // http://crbug.com/162238
443 "text/vnd.sun.j2me.app-descriptor" // http://crbug.com/176450
446 // Mozilla 1.8 and WinIE 7 both accept text/javascript and text/ecmascript.
447 // Mozilla 1.8 accepts application/javascript, application/ecmascript, and
448 // application/x-javascript, but WinIE 7 doesn't.
449 // WinIE 7 accepts text/javascript1.1 - text/javascript1.3, text/jscript, and
450 // text/livescript, but Mozilla 1.8 doesn't.
451 // Mozilla 1.8 allows leading and trailing whitespace, but WinIE 7 doesn't.
452 // Mozilla 1.8 and WinIE 7 both accept the empty string, but neither accept a
453 // whitespace-only string.
454 // We want to accept all the values that either of these browsers accept, but
456 static const char* const supported_javascript_types
[] = {
459 "application/javascript",
460 "application/ecmascript",
461 "application/x-javascript",
462 "text/javascript1.1",
463 "text/javascript1.2",
464 "text/javascript1.3",
469 #if defined(OS_ANDROID)
470 static bool IsCodecSupportedOnAndroid(MimeUtil::Codec codec
) {
472 case MimeUtil::INVALID_CODEC
:
477 case MimeUtil::MPEG4_AAC_LC
:
478 case MimeUtil::MPEG4_AAC_SBRv1
:
479 case MimeUtil::H264_BASELINE
:
480 case MimeUtil::H264_MAIN
:
481 case MimeUtil::H264_HIGH
:
483 case MimeUtil::VORBIS
:
486 case MimeUtil::MPEG2_AAC_LC
:
487 case MimeUtil::MPEG2_AAC_MAIN
:
488 case MimeUtil::MPEG2_AAC_SSR
:
489 // MPEG-2 variants of AAC are not supported on Android.
493 // VP9 is supported only in KitKat+ (API Level 19).
494 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
497 // TODO(vigneshv): Change this similar to the VP9 check once Opus is
498 // supported on Android (http://crbug.com/318436).
501 case MimeUtil::THEORA
:
508 static bool IsMimeTypeSupportedOnAndroid(const std::string
& mimeType
) {
509 // HLS codecs are supported in ICS and above (API level 14)
510 if ((!mimeType
.compare("application/vnd.apple.mpegurl") ||
511 !mimeType
.compare("application/x-mpegurl")) &&
512 base::android::BuildInfo::GetInstance()->sdk_int() < 14) {
519 struct MediaFormatStrict
{
520 const char* mime_type
;
521 const char* codecs_list
;
524 // Following is the list of RFC 6381 compliant codecs:
525 // mp4a.66 - MPEG-2 AAC MAIN
526 // mp4a.67 - MPEG-2 AAC LC
527 // mp4a.68 - MPEG-2 AAC SSR
528 // mp4a.69 - MPEG-2 extension to MPEG-1
529 // mp4a.6B - MPEG-1 audio
530 // mp4a.40.2 - MPEG-4 AAC LC
531 // mp4a.40.5 - MPEG-4 AAC SBRv1
533 // avc1.42E0xx - H.264 Baseline
534 // avc1.4D40xx - H.264 Main
535 // avc1.6400xx - H.264 High
536 static const char kMP4AudioCodecsExpression
[] =
537 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.5";
538 static const char kMP4VideoCodecsExpression
[] =
539 "avc1.42E00A,avc1.4D400A,avc1.64000A," \
540 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.5";
542 static const MediaFormatStrict format_codec_mappings
[] = {
543 { "video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0" },
544 { "audio/webm", "opus,vorbis" },
545 { "audio/wav", "1" },
546 { "audio/x-wav", "1" },
547 { "video/ogg", "opus,theora,vorbis" },
548 { "audio/ogg", "opus,vorbis" },
549 { "application/ogg", "opus,theora,vorbis" },
550 { "audio/mpeg", "mp3" },
552 { "audio/x-mp3", "" },
553 { "audio/mp4", kMP4AudioCodecsExpression
},
554 { "audio/x-m4a", kMP4AudioCodecsExpression
},
555 { "video/mp4", kMP4VideoCodecsExpression
},
556 { "video/x-m4v", kMP4VideoCodecsExpression
},
557 { "application/x-mpegurl", kMP4VideoCodecsExpression
},
558 { "application/vnd.apple.mpegurl", kMP4VideoCodecsExpression
}
561 struct CodecIDMappings
{
562 const char* const codec_id
;
563 MimeUtil::Codec codec
;
566 // List of codec IDs that provide enough information to determine the
567 // codec and profile being requested.
569 // The "mp4a" strings come from RFC 6381.
570 static const CodecIDMappings kUnambiguousCodecIDs
[] = {
571 { "1", MimeUtil::PCM
}, // We only allow this for WAV so it isn't ambiguous.
572 { "mp3", MimeUtil::MP3
},
573 { "mp4a.66", MimeUtil::MPEG2_AAC_MAIN
},
574 { "mp4a.67", MimeUtil::MPEG2_AAC_LC
},
575 { "mp4a.68", MimeUtil::MPEG2_AAC_SSR
},
576 { "mp4a.69", MimeUtil::MP3
},
577 { "mp4a.6B", MimeUtil::MP3
},
578 { "mp4a.40.2", MimeUtil::MPEG4_AAC_LC
},
579 { "mp4a.40.5", MimeUtil::MPEG4_AAC_SBRv1
},
580 { "vorbis", MimeUtil::VORBIS
},
581 { "opus", MimeUtil::OPUS
},
582 { "vp8", MimeUtil::VP8
},
583 { "vp8.0", MimeUtil::VP8
},
584 { "vp9", MimeUtil::VP9
},
585 { "vp9.0", MimeUtil::VP9
},
586 { "theora", MimeUtil::THEORA
}
589 // List of codec IDs that are ambiguous and don't provide
590 // enough information to determine the codec and profile.
591 // The codec in these entries indicate the codec and profile
592 // we assume the user is trying to indicate.
593 static const CodecIDMappings kAmbiguousCodecIDs
[] = {
594 { "mp4a.40", MimeUtil::MPEG4_AAC_LC
},
595 { "avc1", MimeUtil::H264_BASELINE
},
596 { "avc3", MimeUtil::H264_BASELINE
},
599 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) {
600 InitializeMimeTypeMaps();
603 SupportsType
MimeUtil::AreSupportedCodecs(
604 const CodecSet
& supported_codecs
,
605 const std::vector
<std::string
>& codecs
) const {
606 DCHECK(!supported_codecs
.empty());
607 DCHECK(!codecs
.empty());
609 SupportsType result
= IsSupported
;
610 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
611 bool is_ambiguous
= true;
612 Codec codec
= INVALID_CODEC
;
613 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
))
614 return IsNotSupported
;
616 if (!IsCodecSupported(codec
) ||
617 supported_codecs
.find(codec
) == supported_codecs
.end()) {
618 return IsNotSupported
;
622 result
= MayBeSupported
;
628 void MimeUtil::InitializeMimeTypeMaps() {
629 for (size_t i
= 0; i
< arraysize(supported_image_types
); ++i
)
630 image_map_
.insert(supported_image_types
[i
]);
632 // Initialize the supported non-image types.
633 for (size_t i
= 0; i
< arraysize(supported_non_image_types
); ++i
)
634 non_image_map_
.insert(supported_non_image_types
[i
]);
635 for (size_t i
= 0; i
< arraysize(supported_certificate_types
); ++i
)
636 non_image_map_
.insert(supported_certificate_types
[i
].mime_type
);
637 for (size_t i
= 0; i
< arraysize(unsupported_text_types
); ++i
)
638 unsupported_text_map_
.insert(unsupported_text_types
[i
]);
639 for (size_t i
= 0; i
< arraysize(supported_javascript_types
); ++i
)
640 non_image_map_
.insert(supported_javascript_types
[i
]);
641 for (size_t i
= 0; i
< arraysize(common_media_types
); ++i
) {
642 #if defined(OS_ANDROID)
643 if (!IsMimeTypeSupportedOnAndroid(common_media_types
[i
]))
646 non_image_map_
.insert(common_media_types
[i
]);
648 #if defined(USE_PROPRIETARY_CODECS)
649 allow_proprietary_codecs_
= true;
651 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
652 non_image_map_
.insert(proprietary_media_types
[i
]);
655 // Initialize the supported media types.
656 for (size_t i
= 0; i
< arraysize(common_media_types
); ++i
) {
657 #if defined(OS_ANDROID)
658 if (!IsMimeTypeSupportedOnAndroid(common_media_types
[i
]))
661 media_map_
.insert(common_media_types
[i
]);
663 #if defined(USE_PROPRIETARY_CODECS)
664 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
665 media_map_
.insert(proprietary_media_types
[i
]);
668 for (size_t i
= 0; i
< arraysize(supported_javascript_types
); ++i
)
669 javascript_map_
.insert(supported_javascript_types
[i
]);
671 for (size_t i
= 0; i
< arraysize(kUnambiguousCodecIDs
); ++i
) {
672 string_to_codec_map_
[kUnambiguousCodecIDs
[i
].codec_id
] =
673 CodecEntry(kUnambiguousCodecIDs
[i
].codec
, false);
676 for (size_t i
= 0; i
< arraysize(kAmbiguousCodecIDs
); ++i
) {
677 string_to_codec_map_
[kAmbiguousCodecIDs
[i
].codec_id
] =
678 CodecEntry(kAmbiguousCodecIDs
[i
].codec
, true);
681 // Initialize the strict supported media types.
682 for (size_t i
= 0; i
< arraysize(format_codec_mappings
); ++i
) {
683 std::vector
<std::string
> mime_type_codecs
;
684 ParseCodecString(format_codec_mappings
[i
].codecs_list
,
689 for (size_t j
= 0; j
< mime_type_codecs
.size(); ++j
) {
690 Codec codec
= INVALID_CODEC
;
691 bool is_ambiguous
= true;
692 CHECK(StringToCodec(mime_type_codecs
[j
], &codec
, &is_ambiguous
));
693 DCHECK(!is_ambiguous
);
694 codecs
.insert(codec
);
697 strict_format_map_
[format_codec_mappings
[i
].mime_type
] = codecs
;
701 bool MimeUtil::IsSupportedImageMimeType(const std::string
& mime_type
) const {
702 return image_map_
.find(mime_type
) != image_map_
.end();
705 bool MimeUtil::IsSupportedMediaMimeType(const std::string
& mime_type
) const {
706 return media_map_
.find(mime_type
) != media_map_
.end();
709 bool MimeUtil::IsSupportedNonImageMimeType(const std::string
& mime_type
) const {
710 return non_image_map_
.find(mime_type
) != non_image_map_
.end() ||
711 (mime_type
.compare(0, 5, "text/") == 0 &&
712 !IsUnsupportedTextMimeType(mime_type
)) ||
713 (mime_type
.compare(0, 12, "application/") == 0 &&
714 MatchesMimeType("application/*+json", mime_type
));
717 bool MimeUtil::IsUnsupportedTextMimeType(const std::string
& mime_type
) const {
718 return unsupported_text_map_
.find(mime_type
) != unsupported_text_map_
.end();
721 bool MimeUtil::IsSupportedJavascriptMimeType(
722 const std::string
& mime_type
) const {
723 return javascript_map_
.find(mime_type
) != javascript_map_
.end();
726 // Mirrors WebViewImpl::CanShowMIMEType()
727 bool MimeUtil::IsSupportedMimeType(const std::string
& mime_type
) const {
728 return (mime_type
.compare(0, 6, "image/") == 0 &&
729 IsSupportedImageMimeType(mime_type
)) ||
730 IsSupportedNonImageMimeType(mime_type
);
733 // Tests for MIME parameter equality. Each parameter in the |mime_type_pattern|
734 // must be matched by a parameter in the |mime_type|. If there are no
735 // parameters in the pattern, the match is a success.
736 bool MatchesMimeTypeParameters(const std::string
& mime_type_pattern
,
737 const std::string
& mime_type
) {
738 const std::string::size_type semicolon
= mime_type_pattern
.find(';');
739 const std::string::size_type test_semicolon
= mime_type
.find(';');
740 if (semicolon
!= std::string::npos
) {
741 if (test_semicolon
== std::string::npos
)
744 std::vector
<std::string
> pattern_parameters
;
745 base::SplitString(mime_type_pattern
.substr(semicolon
+ 1),
746 ';', &pattern_parameters
);
748 std::vector
<std::string
> test_parameters
;
749 base::SplitString(mime_type
.substr(test_semicolon
+ 1),
750 ';', &test_parameters
);
752 sort(pattern_parameters
.begin(), pattern_parameters
.end());
753 sort(test_parameters
.begin(), test_parameters
.end());
754 std::vector
<std::string
> difference
=
755 base::STLSetDifference
<std::vector
<std::string
> >(pattern_parameters
,
757 return difference
.size() == 0;
762 // This comparison handles absolute maching and also basic
763 // wildcards. The plugin mime types could be:
768 // Also tests mime parameters -- all parameters in the pattern must be present
769 // in the tested type for a match to succeed.
770 bool MimeUtil::MatchesMimeType(const std::string
& mime_type_pattern
,
771 const std::string
& mime_type
) const {
772 // Verify caller is passing lowercase strings.
773 DCHECK_EQ(base::StringToLowerASCII(mime_type_pattern
), mime_type_pattern
);
774 DCHECK_EQ(base::StringToLowerASCII(mime_type
), mime_type
);
776 if (mime_type_pattern
.empty())
779 std::string::size_type semicolon
= mime_type_pattern
.find(';');
780 const std::string
base_pattern(mime_type_pattern
.substr(0, semicolon
));
781 semicolon
= mime_type
.find(';');
782 const std::string
base_type(mime_type
.substr(0, semicolon
));
784 if (base_pattern
== "*" || base_pattern
== "*/*")
785 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
787 const std::string::size_type star
= base_pattern
.find('*');
788 if (star
== std::string::npos
) {
789 if (base_pattern
== base_type
)
790 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
795 // Test length to prevent overlap between |left| and |right|.
796 if (base_type
.length() < base_pattern
.length() - 1)
799 const std::string
left(base_pattern
.substr(0, star
));
800 const std::string
right(base_pattern
.substr(star
+ 1));
802 if (base_type
.find(left
) != 0)
805 if (!right
.empty() &&
806 base_type
.rfind(right
) != base_type
.length() - right
.length())
809 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
812 // See http://www.iana.org/assignments/media-types/media-types.xhtml
813 static const char* legal_top_level_types
[] = {
825 bool MimeUtil::ParseMimeTypeWithoutParameter(
826 const std::string
& type_string
,
827 std::string
* top_level_type
,
828 std::string
* subtype
) const {
829 std::vector
<std::string
> components
;
830 base::SplitString(type_string
, '/', &components
);
831 if (components
.size() != 2 ||
832 !HttpUtil::IsToken(components
[0]) ||
833 !HttpUtil::IsToken(components
[1]))
837 *top_level_type
= components
[0];
839 *subtype
= components
[1];
843 bool MimeUtil::IsValidTopLevelMimeType(const std::string
& type_string
) const {
844 std::string lower_type
= base::StringToLowerASCII(type_string
);
845 for (size_t i
= 0; i
< arraysize(legal_top_level_types
); ++i
) {
846 if (lower_type
.compare(legal_top_level_types
[i
]) == 0)
850 return type_string
.size() > 2 && StartsWithASCII(type_string
, "x-", false);
853 bool MimeUtil::AreSupportedMediaCodecs(
854 const std::vector
<std::string
>& codecs
) const {
855 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
856 Codec codec
= INVALID_CODEC
;
857 bool is_ambiguous
= true;
858 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
) ||
859 !IsCodecSupported(codec
)) {
866 void MimeUtil::ParseCodecString(const std::string
& codecs
,
867 std::vector
<std::string
>* codecs_out
,
869 std::string no_quote_codecs
;
870 base::TrimString(codecs
, "\"", &no_quote_codecs
);
871 base::SplitString(no_quote_codecs
, ',', codecs_out
);
876 // Strip everything past the first '.'
877 for (std::vector
<std::string
>::iterator it
= codecs_out
->begin();
878 it
!= codecs_out
->end();
880 size_t found
= it
->find_first_of('.');
881 if (found
!= std::string::npos
)
886 bool MimeUtil::IsStrictMediaMimeType(const std::string
& mime_type
) const {
887 return strict_format_map_
.find(mime_type
) != strict_format_map_
.end();
890 SupportsType
MimeUtil::IsSupportedStrictMediaMimeType(
891 const std::string
& mime_type
,
892 const std::vector
<std::string
>& codecs
) const {
893 StrictMappings::const_iterator it_strict_map
=
894 strict_format_map_
.find(mime_type
);
895 if (it_strict_map
== strict_format_map_
.end())
896 return codecs
.empty() ? MayBeSupported
: IsNotSupported
;
898 if (it_strict_map
->second
.empty()) {
899 // We get here if the mimetype does not expect a codecs parameter.
900 return (codecs
.empty() && IsDefaultCodecSupported(mime_type
)) ?
901 IsSupported
: IsNotSupported
;
904 if (codecs
.empty()) {
905 // We get here if the mimetype expects to get a codecs parameter,
906 // but didn't get one. If |mime_type| does not have a default codec
907 // the best we can do is say "maybe" because we don't have enough
909 Codec default_codec
= INVALID_CODEC
;
910 if (!GetDefaultCodec(mime_type
, &default_codec
))
911 return MayBeSupported
;
913 return IsCodecSupported(default_codec
) ? IsSupported
: IsNotSupported
;
916 return AreSupportedCodecs(it_strict_map
->second
, codecs
);
919 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
920 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
) {
921 non_image_map_
.erase(proprietary_media_types
[i
]);
922 media_map_
.erase(proprietary_media_types
[i
]);
924 allow_proprietary_codecs_
= false;
927 static bool IsValidH264Level(const std::string
& level_str
) {
929 if (level_str
.size() != 2 || !base::HexStringToUInt(level_str
, &level
))
932 // Valid levels taken from Table A-1 in ISO-14496-10.
933 // Essentially |level_str| is toHex(10 * level).
934 return ((level
>= 10 && level
<= 13) ||
935 (level
>= 20 && level
<= 22) ||
936 (level
>= 30 && level
<= 32) ||
937 (level
>= 40 && level
<= 42) ||
938 (level
>= 50 && level
<= 51));
941 // Handle parsing H.264 codec IDs as outlined in RFC 6381
942 // avc1.42E0xx - H.264 Baseline
943 // avc1.4D40xx - H.264 Main
944 // avc1.6400xx - H.264 High
946 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that
947 // are trying to signal H.264 Baseline.
948 static bool ParseH264CodecID(const std::string
& codec_id
,
949 MimeUtil::Codec
* codec
,
950 bool* is_ambiguous
) {
951 // Make sure we have avc1.xxxxxx or avc3.xxxxxx
952 if (codec_id
.size() != 11 ||
953 (!StartsWithASCII(codec_id
, "avc1.", true) &&
954 !StartsWithASCII(codec_id
, "avc3.", true))) {
958 std::string profile
= StringToUpperASCII(codec_id
.substr(5, 4));
959 if (profile
== "42E0") {
960 *codec
= MimeUtil::H264_BASELINE
;
961 } else if (profile
== "4D40") {
962 *codec
= MimeUtil::H264_MAIN
;
963 } else if (profile
== "6400") {
964 *codec
= MimeUtil::H264_HIGH
;
966 *codec
= MimeUtil::H264_BASELINE
;
967 *is_ambiguous
= true;
971 *is_ambiguous
= !IsValidH264Level(StringToUpperASCII(codec_id
.substr(9)));
975 bool MimeUtil::StringToCodec(const std::string
& codec_id
,
977 bool* is_ambiguous
) const {
978 StringToCodecMappings::const_iterator itr
=
979 string_to_codec_map_
.find(codec_id
);
980 if (itr
!= string_to_codec_map_
.end()) {
981 *codec
= itr
->second
.codec
;
982 *is_ambiguous
= itr
->second
.is_ambiguous
;
986 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is
987 // an H.264 codec ID because currently those are the only ones that can't be
988 // stored in the |string_to_codec_map_| and require parsing.
989 return ParseH264CodecID(codec_id
, codec
, is_ambiguous
);
992 bool MimeUtil::IsCodecSupported(Codec codec
) const {
993 DCHECK_NE(codec
, INVALID_CODEC
);
995 #if defined(OS_ANDROID)
996 if (!IsCodecSupportedOnAndroid(codec
))
1000 return allow_proprietary_codecs_
|| !IsCodecProprietary(codec
);
1003 bool MimeUtil::IsCodecProprietary(Codec codec
) const {
1008 case MPEG2_AAC_MAIN
:
1011 case MPEG4_AAC_SBRv1
:
1029 bool MimeUtil::GetDefaultCodec(const std::string
& mime_type
,
1030 Codec
* default_codec
) const {
1031 if (mime_type
== "audio/mpeg" ||
1032 mime_type
== "audio/mp3" ||
1033 mime_type
== "audio/x-mp3") {
1034 *default_codec
= MimeUtil::MP3
;
1042 bool MimeUtil::IsDefaultCodecSupported(const std::string
& mime_type
) const {
1043 Codec default_codec
= Codec::INVALID_CODEC
;
1044 if (!GetDefaultCodec(mime_type
, &default_codec
))
1046 return IsCodecSupported(default_codec
);
1049 //----------------------------------------------------------------------------
1050 // Wrappers for the singleton
1051 //----------------------------------------------------------------------------
1053 bool GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
1054 std::string
* mime_type
) {
1055 return g_mime_util
.Get().GetMimeTypeFromExtension(ext
, mime_type
);
1058 bool GetMimeTypeFromFile(const base::FilePath
& file_path
,
1059 std::string
* mime_type
) {
1060 return g_mime_util
.Get().GetMimeTypeFromFile(file_path
, mime_type
);
1063 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
1064 std::string
* mime_type
) {
1065 return g_mime_util
.Get().GetWellKnownMimeTypeFromExtension(ext
, mime_type
);
1068 bool GetPreferredExtensionForMimeType(const std::string
& mime_type
,
1069 base::FilePath::StringType
* extension
) {
1070 return g_mime_util
.Get().GetPreferredExtensionForMimeType(mime_type
,
1074 bool IsSupportedImageMimeType(const std::string
& mime_type
) {
1075 return g_mime_util
.Get().IsSupportedImageMimeType(mime_type
);
1078 bool IsSupportedMediaMimeType(const std::string
& mime_type
) {
1079 return g_mime_util
.Get().IsSupportedMediaMimeType(mime_type
);
1082 bool IsSupportedNonImageMimeType(const std::string
& mime_type
) {
1083 return g_mime_util
.Get().IsSupportedNonImageMimeType(mime_type
);
1086 bool IsUnsupportedTextMimeType(const std::string
& mime_type
) {
1087 return g_mime_util
.Get().IsUnsupportedTextMimeType(mime_type
);
1090 bool IsSupportedJavascriptMimeType(const std::string
& mime_type
) {
1091 return g_mime_util
.Get().IsSupportedJavascriptMimeType(mime_type
);
1094 bool IsSupportedMimeType(const std::string
& mime_type
) {
1095 return g_mime_util
.Get().IsSupportedMimeType(mime_type
);
1098 bool MatchesMimeType(const std::string
& mime_type_pattern
,
1099 const std::string
& mime_type
) {
1100 return g_mime_util
.Get().MatchesMimeType(mime_type_pattern
, mime_type
);
1103 bool ParseMimeTypeWithoutParameter(const std::string
& type_string
,
1104 std::string
* top_level_type
,
1105 std::string
* subtype
) {
1106 return g_mime_util
.Get().ParseMimeTypeWithoutParameter(
1107 type_string
, top_level_type
, subtype
);
1110 bool IsValidTopLevelMimeType(const std::string
& type_string
) {
1111 return g_mime_util
.Get().IsValidTopLevelMimeType(type_string
);
1114 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) {
1115 return g_mime_util
.Get().AreSupportedMediaCodecs(codecs
);
1118 bool IsStrictMediaMimeType(const std::string
& mime_type
) {
1119 return g_mime_util
.Get().IsStrictMediaMimeType(mime_type
);
1122 SupportsType
IsSupportedStrictMediaMimeType(
1123 const std::string
& mime_type
,
1124 const std::vector
<std::string
>& codecs
) {
1125 return g_mime_util
.Get().IsSupportedStrictMediaMimeType(mime_type
, codecs
);
1128 void ParseCodecString(const std::string
& codecs
,
1129 std::vector
<std::string
>* codecs_out
,
1131 g_mime_util
.Get().ParseCodecString(codecs
, codecs_out
, strip
);
1136 // From http://www.w3schools.com/media/media_mimeref.asp and
1137 // http://plugindoc.mozdev.org/winmime.php
1138 static const char* const kStandardImageTypes
[] = {
1150 "image/vnd.microsoft.icon",
1151 "image/x-cmu-raster",
1154 "image/x-portable-anymap",
1155 "image/x-portable-bitmap",
1156 "image/x-portable-graymap",
1157 "image/x-portable-pixmap",
1161 "image/x-xwindowdump"
1163 static const char* const kStandardAudioTypes
[] = {
1179 "audio/vnd.rn-realaudio",
1182 static const char* const kStandardVideoTypes
[] = {
1199 struct StandardType
{
1200 const char* leading_mime_type
;
1201 const char* const* standard_types
;
1202 size_t standard_types_len
;
1204 static const StandardType kStandardTypes
[] = {
1205 { "image/", kStandardImageTypes
, arraysize(kStandardImageTypes
) },
1206 { "audio/", kStandardAudioTypes
, arraysize(kStandardAudioTypes
) },
1207 { "video/", kStandardVideoTypes
, arraysize(kStandardVideoTypes
) },
1211 void GetExtensionsFromHardCodedMappings(
1212 const MimeInfo
* mappings
,
1213 size_t mappings_len
,
1214 const std::string
& leading_mime_type
,
1215 base::hash_set
<base::FilePath::StringType
>* extensions
) {
1216 base::FilePath::StringType extension
;
1217 for (size_t i
= 0; i
< mappings_len
; ++i
) {
1218 if (StartsWithASCII(mappings
[i
].mime_type
, leading_mime_type
, false)) {
1219 std::vector
<string
> this_extensions
;
1220 base::SplitString(mappings
[i
].extensions
, ',', &this_extensions
);
1221 for (size_t j
= 0; j
< this_extensions
.size(); ++j
) {
1223 base::FilePath::StringType
extension(
1224 base::UTF8ToWide(this_extensions
[j
]));
1226 base::FilePath::StringType
extension(this_extensions
[j
]);
1228 extensions
->insert(extension
);
1234 void GetExtensionsHelper(
1235 const char* const* standard_types
,
1236 size_t standard_types_len
,
1237 const std::string
& leading_mime_type
,
1238 base::hash_set
<base::FilePath::StringType
>* extensions
) {
1239 for (size_t i
= 0; i
< standard_types_len
; ++i
) {
1240 g_mime_util
.Get().GetPlatformExtensionsForMimeType(standard_types
[i
],
1244 // Also look up the extensions from hard-coded mappings in case that some
1245 // supported extensions are not registered in the system registry, like ogg.
1246 GetExtensionsFromHardCodedMappings(primary_mappings
,
1247 arraysize(primary_mappings
),
1251 GetExtensionsFromHardCodedMappings(secondary_mappings
,
1252 arraysize(secondary_mappings
),
1257 // Note that the elements in the source set will be appended to the target
1260 void HashSetToVector(base::hash_set
<T
>* source
, std::vector
<T
>* target
) {
1261 size_t old_target_size
= target
->size();
1262 target
->resize(old_target_size
+ source
->size());
1264 for (typename
base::hash_set
<T
>::iterator iter
= source
->begin();
1265 iter
!= source
->end(); ++iter
, ++i
)
1266 (*target
)[old_target_size
+ i
] = *iter
;
1270 void GetExtensionsForMimeType(
1271 const std::string
& unsafe_mime_type
,
1272 std::vector
<base::FilePath::StringType
>* extensions
) {
1273 if (unsafe_mime_type
== "*/*" || unsafe_mime_type
== "*")
1276 const std::string mime_type
= base::StringToLowerASCII(unsafe_mime_type
);
1277 base::hash_set
<base::FilePath::StringType
> unique_extensions
;
1279 if (EndsWith(mime_type
, "/*", true)) {
1280 std::string leading_mime_type
= mime_type
.substr(0, mime_type
.length() - 1);
1282 // Find the matching StandardType from within kStandardTypes, or fall
1283 // through to the last (default) StandardType.
1284 const StandardType
* type
= NULL
;
1285 for (size_t i
= 0; i
< arraysize(kStandardTypes
); ++i
) {
1286 type
= &(kStandardTypes
[i
]);
1287 if (type
->leading_mime_type
&&
1288 leading_mime_type
== type
->leading_mime_type
)
1292 GetExtensionsHelper(type
->standard_types
,
1293 type
->standard_types_len
,
1295 &unique_extensions
);
1297 g_mime_util
.Get().GetPlatformExtensionsForMimeType(mime_type
,
1298 &unique_extensions
);
1300 // Also look up the extensions from hard-coded mappings in case that some
1301 // supported extensions are not registered in the system registry, like ogg.
1302 GetExtensionsFromHardCodedMappings(primary_mappings
,
1303 arraysize(primary_mappings
),
1305 &unique_extensions
);
1307 GetExtensionsFromHardCodedMappings(secondary_mappings
,
1308 arraysize(secondary_mappings
),
1310 &unique_extensions
);
1313 HashSetToVector(&unique_extensions
, extensions
);
1316 void RemoveProprietaryMediaTypesAndCodecsForTests() {
1317 g_mime_util
.Get().RemoveProprietaryMediaTypesAndCodecsForTests();
1320 const std::string
GetIANAMediaType(const std::string
& mime_type
) {
1321 for (size_t i
= 0; i
< arraysize(kIanaMediaTypes
); ++i
) {
1322 if (StartsWithASCII(mime_type
, kIanaMediaTypes
[i
].matcher
, true)) {
1323 return kIanaMediaTypes
[i
].name
;
1326 return std::string();
1329 CertificateMimeType
GetCertificateMimeTypeForMimeType(
1330 const std::string
& mime_type
) {
1331 // Don't create a map, there is only one entry in the table,
1332 // except on Android.
1333 for (size_t i
= 0; i
< arraysize(supported_certificate_types
); ++i
) {
1334 if (mime_type
== net::supported_certificate_types
[i
].mime_type
)
1335 return net::supported_certificate_types
[i
].cert_type
;
1337 return CERTIFICATE_MIME_TYPE_UNKNOWN
;
1340 bool IsSupportedCertificateMimeType(const std::string
& mime_type
) {
1341 CertificateMimeType file_type
=
1342 GetCertificateMimeTypeForMimeType(mime_type
);
1343 return file_type
!= CERTIFICATE_MIME_TYPE_UNKNOWN
;
1346 void AddMultipartValueForUpload(const std::string
& value_name
,
1347 const std::string
& value
,
1348 const std::string
& mime_boundary
,
1349 const std::string
& content_type
,
1350 std::string
* post_data
) {
1352 // First line is the boundary.
1353 post_data
->append("--" + mime_boundary
+ "\r\n");
1354 // Next line is the Content-disposition.
1355 post_data
->append("Content-Disposition: form-data; name=\"" +
1356 value_name
+ "\"\r\n");
1357 if (!content_type
.empty()) {
1358 // If Content-type is specified, the next line is that.
1359 post_data
->append("Content-Type: " + content_type
+ "\r\n");
1361 // Leave an empty line and append the value.
1362 post_data
->append("\r\n" + value
+ "\r\n");
1365 void AddMultipartFinalDelimiterForUpload(const std::string
& mime_boundary
,
1366 std::string
* post_data
) {
1368 post_data
->append("--" + mime_boundary
+ "--\r\n");