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" }
249 static const char* FindMimeType(const MimeInfo
* mappings
,
252 size_t ext_len
= strlen(ext
);
254 for (size_t i
= 0; i
< mappings_len
; ++i
) {
255 const char* extensions
= mappings
[i
].extensions
;
257 size_t end_pos
= strcspn(extensions
, ",");
258 if (end_pos
== ext_len
&&
259 base::strncasecmp(extensions
, ext
, ext_len
) == 0)
260 return mappings
[i
].mime_type
;
261 extensions
+= end_pos
;
264 extensions
+= 1; // skip over comma
270 bool MimeUtil::GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
271 string
* result
) const {
272 return GetMimeTypeFromExtensionHelper(ext
, true, result
);
275 bool MimeUtil::GetWellKnownMimeTypeFromExtension(
276 const base::FilePath::StringType
& ext
,
277 string
* result
) const {
278 return GetMimeTypeFromExtensionHelper(ext
, false, result
);
281 bool MimeUtil::GetMimeTypeFromFile(const base::FilePath
& file_path
,
282 string
* result
) const {
283 base::FilePath::StringType file_name_str
= file_path
.Extension();
284 if (file_name_str
.empty())
286 return GetMimeTypeFromExtension(file_name_str
.substr(1), result
);
289 bool MimeUtil::GetMimeTypeFromExtensionHelper(
290 const base::FilePath::StringType
& ext
,
291 bool include_platform_types
,
292 string
* result
) const {
293 // Avoids crash when unable to handle a long file path. See crbug.com/48733.
294 const unsigned kMaxFilePathSize
= 65536;
295 if (ext
.length() > kMaxFilePathSize
)
298 // We implement the same algorithm as Mozilla for mapping a file extension to
299 // a mime type. That is, we first check a hard-coded list (that cannot be
300 // overridden), and then if not found there, we defer to the system registry.
301 // Finally, we scan a secondary hard-coded list to catch types that we can
302 // deduce but that we also want to allow the OS to override.
304 base::FilePath
path_ext(ext
);
305 const string ext_narrow_str
= path_ext
.AsUTF8Unsafe();
306 const char* mime_type
= FindMimeType(primary_mappings
,
307 arraysize(primary_mappings
),
308 ext_narrow_str
.c_str());
314 if (include_platform_types
&& GetPlatformMimeTypeFromExtension(ext
, result
))
317 mime_type
= FindMimeType(secondary_mappings
, arraysize(secondary_mappings
),
318 ext_narrow_str
.c_str());
327 // From WebKit's WebCore/platform/MIMETypeRegistry.cpp:
329 static const char* const supported_image_types
[] = {
337 "image/vnd.microsoft.icon", // ico
338 "image/x-icon", // ico
339 "image/x-xbitmap", // xbm
343 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type
344 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php
345 // This set of codecs is supported by all variations of Chromium.
346 static const char* const common_media_types
[] = {
350 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
362 #if defined(OS_ANDROID)
363 // HLS. Supported by Android ICS and above.
364 "application/vnd.apple.mpegurl",
365 "application/x-mpegurl",
369 // List of proprietary types only supported by Google Chrome.
370 static const char* const proprietary_media_types
[] = {
384 // - does not include javascript types list (see supported_javascript_types)
385 // - does not include types starting with "text/" (see
386 // IsSupportedNonImageMimeType())
387 static const char* const supported_non_image_types
[] = {
388 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type
390 "application/atom+xml",
391 "application/rss+xml",
392 "application/xhtml+xml",
394 "multipart/related", // For MHTML support.
395 "multipart/x-mixed-replace"
396 // Note: ADDING a new type here will probably render it AS HTML. This can
397 // result in cross site scripting.
400 // Dictionary of cryptographic file mime types.
401 struct CertificateMimeTypeInfo
{
402 const char* mime_type
;
403 CertificateMimeType cert_type
;
406 static const CertificateMimeTypeInfo supported_certificate_types
[] = {
407 { "application/x-x509-user-cert",
408 CERTIFICATE_MIME_TYPE_X509_USER_CERT
},
409 #if defined(OS_ANDROID)
410 { "application/x-x509-ca-cert", CERTIFICATE_MIME_TYPE_X509_CA_CERT
},
411 { "application/x-pkcs12", CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE
},
415 // These types are excluded from the logic that allows all text/ types because
416 // while they are technically text, it's very unlikely that a user expects to
417 // see them rendered in text form.
418 static const char* const unsupported_text_types
[] = {
432 "text/comma-separated-values",
434 "text/tab-separated-values",
436 "text/ofx", // http://crbug.com/162238
437 "text/vnd.sun.j2me.app-descriptor" // http://crbug.com/176450
440 // Mozilla 1.8 and WinIE 7 both accept text/javascript and text/ecmascript.
441 // Mozilla 1.8 accepts application/javascript, application/ecmascript, and
442 // application/x-javascript, but WinIE 7 doesn't.
443 // WinIE 7 accepts text/javascript1.1 - text/javascript1.3, text/jscript, and
444 // text/livescript, but Mozilla 1.8 doesn't.
445 // Mozilla 1.8 allows leading and trailing whitespace, but WinIE 7 doesn't.
446 // Mozilla 1.8 and WinIE 7 both accept the empty string, but neither accept a
447 // whitespace-only string.
448 // We want to accept all the values that either of these browsers accept, but
450 static const char* const supported_javascript_types
[] = {
453 "application/javascript",
454 "application/ecmascript",
455 "application/x-javascript",
456 "text/javascript1.1",
457 "text/javascript1.2",
458 "text/javascript1.3",
463 #if defined(OS_ANDROID)
464 static bool IsCodecSupportedOnAndroid(MimeUtil::Codec codec
) {
466 case MimeUtil::INVALID_CODEC
:
471 case MimeUtil::MPEG4_AAC_LC
:
472 case MimeUtil::MPEG4_AAC_SBRv1
:
473 case MimeUtil::H264_BASELINE
:
475 case MimeUtil::VORBIS
:
478 case MimeUtil::MPEG2_AAC_LC
:
479 case MimeUtil::MPEG2_AAC_MAIN
:
480 case MimeUtil::MPEG2_AAC_SSR
:
481 // MPEG-2 variants of AAC are not supported on Android.
484 case MimeUtil::H264_MAIN
:
485 case MimeUtil::H264_HIGH
:
486 // H.264 Main and High profiles are not supported on Android.
490 // VP9 is supported only in KitKat+ (API Level 19).
491 return base::android::BuildInfo::GetInstance()->sdk_int() >= 19;
494 // TODO(vigneshv): Change this similar to the VP9 check once Opus is
495 // supported on Android (http://crbug.com/318436).
498 case MimeUtil::THEORA
:
505 static bool IsMimeTypeSupportedOnAndroid(const std::string
& mimeType
) {
506 // HLS codecs are supported in ICS and above (API level 14)
507 if ((!mimeType
.compare("application/vnd.apple.mpegurl") ||
508 !mimeType
.compare("application/x-mpegurl")) &&
509 base::android::BuildInfo::GetInstance()->sdk_int() < 14) {
516 struct MediaFormatStrict
{
517 const char* mime_type
;
518 const char* codecs_list
;
521 // Following is the list of RFC 6381 compliant codecs:
522 // mp4a.66 - MPEG-2 AAC MAIN
523 // mp4a.67 - MPEG-2 AAC LC
524 // mp4a.68 - MPEG-2 AAC SSR
525 // mp4a.69 - MPEG-2 extension to MPEG-1
526 // mp4a.6B - MPEG-1 audio
527 // mp4a.40.2 - MPEG-4 AAC LC
528 // mp4a.40.5 - MPEG-4 AAC SBRv1
530 // avc1.42E0xx - H.264 Baseline
531 // avc1.4D40xx - H.264 Main
532 // avc1.6400xx - H.264 High
533 static const char kMP4AudioCodecsExpression
[] =
534 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.5";
535 static const char kMP4VideoCodecsExpression
[] =
536 "avc1.42E00A,avc1.4D400A,avc1.64000A," \
537 "mp4a.66,mp4a.67,mp4a.68,mp4a.69,mp4a.6B,mp4a.40.2,mp4a.40.5";
539 static const MediaFormatStrict format_codec_mappings
[] = {
540 { "video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0" },
541 { "audio/webm", "opus,vorbis" },
542 { "audio/wav", "1" },
543 { "audio/x-wav", "1" },
544 { "video/ogg", "opus,theora,vorbis" },
545 { "audio/ogg", "opus,vorbis" },
546 { "application/ogg", "opus,theora,vorbis" },
547 { "audio/mpeg", "mp3" },
549 { "audio/x-mp3", "" },
550 { "audio/mp4", kMP4AudioCodecsExpression
},
551 { "audio/x-m4a", kMP4AudioCodecsExpression
},
552 { "video/mp4", kMP4VideoCodecsExpression
},
553 { "video/x-m4v", kMP4VideoCodecsExpression
},
554 { "application/x-mpegurl", kMP4VideoCodecsExpression
},
555 { "application/vnd.apple.mpegurl", kMP4VideoCodecsExpression
}
558 struct CodecIDMappings
{
559 const char* const codec_id
;
560 MimeUtil::Codec codec
;
563 // List of codec IDs that provide enough information to determine the
564 // codec and profile being requested.
566 // The "mp4a" strings come from RFC 6381.
567 static const CodecIDMappings kUnambiguousCodecIDs
[] = {
568 { "1", MimeUtil::PCM
}, // We only allow this for WAV so it isn't ambiguous.
569 { "mp3", MimeUtil::MP3
},
570 { "mp4a.66", MimeUtil::MPEG2_AAC_MAIN
},
571 { "mp4a.67", MimeUtil::MPEG2_AAC_LC
},
572 { "mp4a.68", MimeUtil::MPEG2_AAC_SSR
},
573 { "mp4a.69", MimeUtil::MP3
},
574 { "mp4a.6B", MimeUtil::MP3
},
575 { "mp4a.40.2", MimeUtil::MPEG4_AAC_LC
},
576 { "mp4a.40.5", MimeUtil::MPEG4_AAC_SBRv1
},
577 { "vorbis", MimeUtil::VORBIS
},
578 { "opus", MimeUtil::OPUS
},
579 { "vp8", MimeUtil::VP8
},
580 { "vp8.0", MimeUtil::VP8
},
581 { "vp9", MimeUtil::VP9
},
582 { "vp9.0", MimeUtil::VP9
},
583 { "theora", MimeUtil::THEORA
}
586 // List of codec IDs that are ambiguous and don't provide
587 // enough information to determine the codec and profile.
588 // The codec in these entries indicate the codec and profile
589 // we assume the user is trying to indicate.
590 static const CodecIDMappings kAmbiguousCodecIDs
[] = {
591 { "mp4a.40", MimeUtil::MPEG4_AAC_LC
},
592 { "avc1", MimeUtil::H264_BASELINE
},
593 { "avc3", MimeUtil::H264_BASELINE
},
596 MimeUtil::MimeUtil() : allow_proprietary_codecs_(false) {
597 InitializeMimeTypeMaps();
600 SupportsType
MimeUtil::AreSupportedCodecs(
601 const CodecSet
& supported_codecs
,
602 const std::vector
<std::string
>& codecs
) const {
603 DCHECK(!supported_codecs
.empty());
604 DCHECK(!codecs
.empty());
606 SupportsType result
= IsSupported
;
607 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
608 bool is_ambiguous
= true;
609 Codec codec
= INVALID_CODEC
;
610 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
))
611 return IsNotSupported
;
613 if (!IsCodecSupported(codec
) ||
614 supported_codecs
.find(codec
) == supported_codecs
.end()) {
615 return IsNotSupported
;
619 result
= MayBeSupported
;
625 void MimeUtil::InitializeMimeTypeMaps() {
626 for (size_t i
= 0; i
< arraysize(supported_image_types
); ++i
)
627 image_map_
.insert(supported_image_types
[i
]);
629 // Initialize the supported non-image types.
630 for (size_t i
= 0; i
< arraysize(supported_non_image_types
); ++i
)
631 non_image_map_
.insert(supported_non_image_types
[i
]);
632 for (size_t i
= 0; i
< arraysize(supported_certificate_types
); ++i
)
633 non_image_map_
.insert(supported_certificate_types
[i
].mime_type
);
634 for (size_t i
= 0; i
< arraysize(unsupported_text_types
); ++i
)
635 unsupported_text_map_
.insert(unsupported_text_types
[i
]);
636 for (size_t i
= 0; i
< arraysize(supported_javascript_types
); ++i
)
637 non_image_map_
.insert(supported_javascript_types
[i
]);
638 for (size_t i
= 0; i
< arraysize(common_media_types
); ++i
) {
639 #if defined(OS_ANDROID)
640 if (!IsMimeTypeSupportedOnAndroid(common_media_types
[i
]))
643 non_image_map_
.insert(common_media_types
[i
]);
645 #if defined(USE_PROPRIETARY_CODECS)
646 allow_proprietary_codecs_
= true;
648 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
649 non_image_map_
.insert(proprietary_media_types
[i
]);
652 // Initialize the supported media types.
653 for (size_t i
= 0; i
< arraysize(common_media_types
); ++i
) {
654 #if defined(OS_ANDROID)
655 if (!IsMimeTypeSupportedOnAndroid(common_media_types
[i
]))
658 media_map_
.insert(common_media_types
[i
]);
660 #if defined(USE_PROPRIETARY_CODECS)
661 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
662 media_map_
.insert(proprietary_media_types
[i
]);
665 for (size_t i
= 0; i
< arraysize(supported_javascript_types
); ++i
)
666 javascript_map_
.insert(supported_javascript_types
[i
]);
668 for (size_t i
= 0; i
< arraysize(kUnambiguousCodecIDs
); ++i
) {
669 string_to_codec_map_
[kUnambiguousCodecIDs
[i
].codec_id
] =
670 CodecEntry(kUnambiguousCodecIDs
[i
].codec
, false);
673 for (size_t i
= 0; i
< arraysize(kAmbiguousCodecIDs
); ++i
) {
674 string_to_codec_map_
[kAmbiguousCodecIDs
[i
].codec_id
] =
675 CodecEntry(kAmbiguousCodecIDs
[i
].codec
, true);
678 // Initialize the strict supported media types.
679 for (size_t i
= 0; i
< arraysize(format_codec_mappings
); ++i
) {
680 std::vector
<std::string
> mime_type_codecs
;
681 ParseCodecString(format_codec_mappings
[i
].codecs_list
,
686 for (size_t j
= 0; j
< mime_type_codecs
.size(); ++j
) {
687 Codec codec
= INVALID_CODEC
;
688 bool is_ambiguous
= true;
689 CHECK(StringToCodec(mime_type_codecs
[j
], &codec
, &is_ambiguous
));
690 DCHECK(!is_ambiguous
);
691 codecs
.insert(codec
);
694 strict_format_map_
[format_codec_mappings
[i
].mime_type
] = codecs
;
698 bool MimeUtil::IsSupportedImageMimeType(const std::string
& mime_type
) const {
699 return image_map_
.find(mime_type
) != image_map_
.end();
702 bool MimeUtil::IsSupportedMediaMimeType(const std::string
& mime_type
) const {
703 return media_map_
.find(mime_type
) != media_map_
.end();
706 bool MimeUtil::IsSupportedNonImageMimeType(const std::string
& mime_type
) const {
707 return non_image_map_
.find(mime_type
) != non_image_map_
.end() ||
708 (mime_type
.compare(0, 5, "text/") == 0 &&
709 !IsUnsupportedTextMimeType(mime_type
)) ||
710 (mime_type
.compare(0, 12, "application/") == 0 &&
711 MatchesMimeType("application/*+json", mime_type
));
714 bool MimeUtil::IsUnsupportedTextMimeType(const std::string
& mime_type
) const {
715 return unsupported_text_map_
.find(mime_type
) != unsupported_text_map_
.end();
718 bool MimeUtil::IsSupportedJavascriptMimeType(
719 const std::string
& mime_type
) const {
720 return javascript_map_
.find(mime_type
) != javascript_map_
.end();
723 // Mirrors WebViewImpl::CanShowMIMEType()
724 bool MimeUtil::IsSupportedMimeType(const std::string
& mime_type
) const {
725 return (mime_type
.compare(0, 6, "image/") == 0 &&
726 IsSupportedImageMimeType(mime_type
)) ||
727 IsSupportedNonImageMimeType(mime_type
);
730 // Tests for MIME parameter equality. Each parameter in the |mime_type_pattern|
731 // must be matched by a parameter in the |mime_type|. If there are no
732 // parameters in the pattern, the match is a success.
733 bool MatchesMimeTypeParameters(const std::string
& mime_type_pattern
,
734 const std::string
& mime_type
) {
735 const std::string::size_type semicolon
= mime_type_pattern
.find(';');
736 const std::string::size_type test_semicolon
= mime_type
.find(';');
737 if (semicolon
!= std::string::npos
) {
738 if (test_semicolon
== std::string::npos
)
741 std::vector
<std::string
> pattern_parameters
;
742 base::SplitString(mime_type_pattern
.substr(semicolon
+ 1),
743 ';', &pattern_parameters
);
745 std::vector
<std::string
> test_parameters
;
746 base::SplitString(mime_type
.substr(test_semicolon
+ 1),
747 ';', &test_parameters
);
749 sort(pattern_parameters
.begin(), pattern_parameters
.end());
750 sort(test_parameters
.begin(), test_parameters
.end());
751 std::vector
<std::string
> difference
=
752 base::STLSetDifference
<std::vector
<std::string
> >(pattern_parameters
,
754 return difference
.size() == 0;
759 // This comparison handles absolute maching and also basic
760 // wildcards. The plugin mime types could be:
765 // Also tests mime parameters -- all parameters in the pattern must be present
766 // in the tested type for a match to succeed.
767 bool MimeUtil::MatchesMimeType(const std::string
& mime_type_pattern
,
768 const std::string
& mime_type
) const {
769 // Verify caller is passing lowercase strings.
770 DCHECK_EQ(StringToLowerASCII(mime_type_pattern
), mime_type_pattern
);
771 DCHECK_EQ(StringToLowerASCII(mime_type
), mime_type
);
773 if (mime_type_pattern
.empty())
776 std::string::size_type semicolon
= mime_type_pattern
.find(';');
777 const std::string
base_pattern(mime_type_pattern
.substr(0, semicolon
));
778 semicolon
= mime_type
.find(';');
779 const std::string
base_type(mime_type
.substr(0, semicolon
));
781 if (base_pattern
== "*" || base_pattern
== "*/*")
782 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
784 const std::string::size_type star
= base_pattern
.find('*');
785 if (star
== std::string::npos
) {
786 if (base_pattern
== base_type
)
787 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
792 // Test length to prevent overlap between |left| and |right|.
793 if (base_type
.length() < base_pattern
.length() - 1)
796 const std::string
left(base_pattern
.substr(0, star
));
797 const std::string
right(base_pattern
.substr(star
+ 1));
799 if (base_type
.find(left
) != 0)
802 if (!right
.empty() &&
803 base_type
.rfind(right
) != base_type
.length() - right
.length())
806 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
809 // See http://www.iana.org/assignments/media-types/media-types.xhtml
810 static const char* legal_top_level_types
[] = {
822 bool MimeUtil::ParseMimeTypeWithoutParameter(
823 const std::string
& type_string
,
824 std::string
* top_level_type
,
825 std::string
* subtype
) const {
826 std::vector
<std::string
> components
;
827 base::SplitString(type_string
, '/', &components
);
828 if (components
.size() != 2 ||
829 !HttpUtil::IsToken(components
[0]) ||
830 !HttpUtil::IsToken(components
[1]))
834 *top_level_type
= components
[0];
836 *subtype
= components
[1];
840 bool MimeUtil::IsValidTopLevelMimeType(const std::string
& type_string
) const {
841 std::string lower_type
= StringToLowerASCII(type_string
);
842 for (size_t i
= 0; i
< arraysize(legal_top_level_types
); ++i
) {
843 if (lower_type
.compare(legal_top_level_types
[i
]) == 0)
847 return type_string
.size() > 2 && StartsWithASCII(type_string
, "x-", false);
850 bool MimeUtil::AreSupportedMediaCodecs(
851 const std::vector
<std::string
>& codecs
) const {
852 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
853 Codec codec
= INVALID_CODEC
;
854 bool is_ambiguous
= true;
855 if (!StringToCodec(codecs
[i
], &codec
, &is_ambiguous
) ||
856 !IsCodecSupported(codec
)) {
863 void MimeUtil::ParseCodecString(const std::string
& codecs
,
864 std::vector
<std::string
>* codecs_out
,
866 std::string no_quote_codecs
;
867 base::TrimString(codecs
, "\"", &no_quote_codecs
);
868 base::SplitString(no_quote_codecs
, ',', codecs_out
);
873 // Strip everything past the first '.'
874 for (std::vector
<std::string
>::iterator it
= codecs_out
->begin();
875 it
!= codecs_out
->end();
877 size_t found
= it
->find_first_of('.');
878 if (found
!= std::string::npos
)
883 bool MimeUtil::IsStrictMediaMimeType(const std::string
& mime_type
) const {
884 return strict_format_map_
.find(mime_type
) != strict_format_map_
.end();
887 SupportsType
MimeUtil::IsSupportedStrictMediaMimeType(
888 const std::string
& mime_type
,
889 const std::vector
<std::string
>& codecs
) const {
890 StrictMappings::const_iterator it_strict_map
=
891 strict_format_map_
.find(mime_type
);
892 if (it_strict_map
== strict_format_map_
.end())
893 return codecs
.empty() ? MayBeSupported
: IsNotSupported
;
895 if (it_strict_map
->second
.empty()) {
896 // We get here if the mimetype does not expect a codecs parameter.
897 return (codecs
.empty() && IsDefaultCodecSupported(mime_type
)) ?
898 IsSupported
: IsNotSupported
;
901 if (codecs
.empty()) {
902 // We get here if the mimetype expects to get a codecs parameter,
903 // but didn't get one. If |mime_type| does not have a default codec
904 // the best we can do is say "maybe" because we don't have enough
906 Codec default_codec
= INVALID_CODEC
;
907 if (!GetDefaultCodec(mime_type
, &default_codec
))
908 return MayBeSupported
;
910 return IsCodecSupported(default_codec
) ? IsSupported
: IsNotSupported
;
913 return AreSupportedCodecs(it_strict_map
->second
, codecs
);
916 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
917 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
) {
918 non_image_map_
.erase(proprietary_media_types
[i
]);
919 media_map_
.erase(proprietary_media_types
[i
]);
921 allow_proprietary_codecs_
= false;
924 static bool IsValidH264Level(const std::string
& level_str
) {
926 if (level_str
.size() != 2 || !base::HexStringToUInt(level_str
, &level
))
929 // Valid levels taken from Table A-1 in ISO-14496-10.
930 // Essentially |level_str| is toHex(10 * level).
931 return ((level
>= 10 && level
<= 13) ||
932 (level
>= 20 && level
<= 22) ||
933 (level
>= 30 && level
<= 32) ||
934 (level
>= 40 && level
<= 42) ||
935 (level
>= 50 && level
<= 51));
938 // Handle parsing H.264 codec IDs as outlined in RFC 6381
939 // avc1.42E0xx - H.264 Baseline
940 // avc1.4D40xx - H.264 Main
941 // avc1.6400xx - H.264 High
943 // avc1.xxxxxx & avc3.xxxxxx are considered ambiguous forms that
944 // are trying to signal H.264 Baseline.
945 static bool ParseH264CodecID(const std::string
& codec_id
,
946 MimeUtil::Codec
* codec
,
947 bool* is_ambiguous
) {
948 // Make sure we have avc1.xxxxxx or avc3.xxxxxx
949 if (codec_id
.size() != 11 ||
950 (!StartsWithASCII(codec_id
, "avc1.", true) &&
951 !StartsWithASCII(codec_id
, "avc3.", true))) {
955 std::string profile
= StringToUpperASCII(codec_id
.substr(5, 4));
956 if (profile
== "42E0") {
957 *codec
= MimeUtil::H264_BASELINE
;
958 } else if (profile
== "4D40") {
959 *codec
= MimeUtil::H264_MAIN
;
960 } else if (profile
== "6400") {
961 *codec
= MimeUtil::H264_HIGH
;
963 *codec
= MimeUtil::H264_BASELINE
;
964 *is_ambiguous
= true;
968 *is_ambiguous
= !IsValidH264Level(StringToUpperASCII(codec_id
.substr(9)));
972 bool MimeUtil::StringToCodec(const std::string
& codec_id
,
974 bool* is_ambiguous
) const {
975 StringToCodecMappings::const_iterator itr
=
976 string_to_codec_map_
.find(codec_id
);
977 if (itr
!= string_to_codec_map_
.end()) {
978 *codec
= itr
->second
.codec
;
979 *is_ambiguous
= itr
->second
.is_ambiguous
;
983 // If |codec_id| is not in |string_to_codec_map_|, then we assume that it is
984 // an H.264 codec ID because currently those are the only ones that can't be
985 // stored in the |string_to_codec_map_| and require parsing.
986 return ParseH264CodecID(codec_id
, codec
, is_ambiguous
);
989 bool MimeUtil::IsCodecSupported(Codec codec
) const {
990 DCHECK_NE(codec
, INVALID_CODEC
);
992 #if defined(OS_ANDROID)
993 if (!IsCodecSupportedOnAndroid(codec
))
997 return allow_proprietary_codecs_
|| !IsCodecProprietary(codec
);
1000 bool MimeUtil::IsCodecProprietary(Codec codec
) const {
1005 case MPEG2_AAC_MAIN
:
1008 case MPEG4_AAC_SBRv1
:
1026 bool MimeUtil::GetDefaultCodec(const std::string
& mime_type
,
1027 Codec
* default_codec
) const {
1028 if (mime_type
== "audio/mpeg" ||
1029 mime_type
== "audio/mp3" ||
1030 mime_type
== "audio/x-mp3") {
1031 *default_codec
= MimeUtil::MP3
;
1039 bool MimeUtil::IsDefaultCodecSupported(const std::string
& mime_type
) const {
1040 Codec default_codec
= Codec::INVALID_CODEC
;
1041 if (!GetDefaultCodec(mime_type
, &default_codec
))
1043 return IsCodecSupported(default_codec
);
1046 //----------------------------------------------------------------------------
1047 // Wrappers for the singleton
1048 //----------------------------------------------------------------------------
1050 bool GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
1051 std::string
* mime_type
) {
1052 return g_mime_util
.Get().GetMimeTypeFromExtension(ext
, mime_type
);
1055 bool GetMimeTypeFromFile(const base::FilePath
& file_path
,
1056 std::string
* mime_type
) {
1057 return g_mime_util
.Get().GetMimeTypeFromFile(file_path
, mime_type
);
1060 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
1061 std::string
* mime_type
) {
1062 return g_mime_util
.Get().GetWellKnownMimeTypeFromExtension(ext
, mime_type
);
1065 bool GetPreferredExtensionForMimeType(const std::string
& mime_type
,
1066 base::FilePath::StringType
* extension
) {
1067 return g_mime_util
.Get().GetPreferredExtensionForMimeType(mime_type
,
1071 bool IsSupportedImageMimeType(const std::string
& mime_type
) {
1072 return g_mime_util
.Get().IsSupportedImageMimeType(mime_type
);
1075 bool IsSupportedMediaMimeType(const std::string
& mime_type
) {
1076 return g_mime_util
.Get().IsSupportedMediaMimeType(mime_type
);
1079 bool IsSupportedNonImageMimeType(const std::string
& mime_type
) {
1080 return g_mime_util
.Get().IsSupportedNonImageMimeType(mime_type
);
1083 bool IsUnsupportedTextMimeType(const std::string
& mime_type
) {
1084 return g_mime_util
.Get().IsUnsupportedTextMimeType(mime_type
);
1087 bool IsSupportedJavascriptMimeType(const std::string
& mime_type
) {
1088 return g_mime_util
.Get().IsSupportedJavascriptMimeType(mime_type
);
1091 bool IsSupportedMimeType(const std::string
& mime_type
) {
1092 return g_mime_util
.Get().IsSupportedMimeType(mime_type
);
1095 bool MatchesMimeType(const std::string
& mime_type_pattern
,
1096 const std::string
& mime_type
) {
1097 return g_mime_util
.Get().MatchesMimeType(mime_type_pattern
, mime_type
);
1100 bool ParseMimeTypeWithoutParameter(const std::string
& type_string
,
1101 std::string
* top_level_type
,
1102 std::string
* subtype
) {
1103 return g_mime_util
.Get().ParseMimeTypeWithoutParameter(
1104 type_string
, top_level_type
, subtype
);
1107 bool IsValidTopLevelMimeType(const std::string
& type_string
) {
1108 return g_mime_util
.Get().IsValidTopLevelMimeType(type_string
);
1111 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) {
1112 return g_mime_util
.Get().AreSupportedMediaCodecs(codecs
);
1115 bool IsStrictMediaMimeType(const std::string
& mime_type
) {
1116 return g_mime_util
.Get().IsStrictMediaMimeType(mime_type
);
1119 SupportsType
IsSupportedStrictMediaMimeType(
1120 const std::string
& mime_type
,
1121 const std::vector
<std::string
>& codecs
) {
1122 return g_mime_util
.Get().IsSupportedStrictMediaMimeType(mime_type
, codecs
);
1125 void ParseCodecString(const std::string
& codecs
,
1126 std::vector
<std::string
>* codecs_out
,
1128 g_mime_util
.Get().ParseCodecString(codecs
, codecs_out
, strip
);
1133 // From http://www.w3schools.com/media/media_mimeref.asp and
1134 // http://plugindoc.mozdev.org/winmime.php
1135 static const char* const kStandardImageTypes
[] = {
1147 "image/vnd.microsoft.icon",
1148 "image/x-cmu-raster",
1151 "image/x-portable-anymap",
1152 "image/x-portable-bitmap",
1153 "image/x-portable-graymap",
1154 "image/x-portable-pixmap",
1158 "image/x-xwindowdump"
1160 static const char* const kStandardAudioTypes
[] = {
1176 "audio/vnd.rn-realaudio",
1179 static const char* const kStandardVideoTypes
[] = {
1196 struct StandardType
{
1197 const char* leading_mime_type
;
1198 const char* const* standard_types
;
1199 size_t standard_types_len
;
1201 static const StandardType kStandardTypes
[] = {
1202 { "image/", kStandardImageTypes
, arraysize(kStandardImageTypes
) },
1203 { "audio/", kStandardAudioTypes
, arraysize(kStandardAudioTypes
) },
1204 { "video/", kStandardVideoTypes
, arraysize(kStandardVideoTypes
) },
1208 void GetExtensionsFromHardCodedMappings(
1209 const MimeInfo
* mappings
,
1210 size_t mappings_len
,
1211 const std::string
& leading_mime_type
,
1212 base::hash_set
<base::FilePath::StringType
>* extensions
) {
1213 base::FilePath::StringType extension
;
1214 for (size_t i
= 0; i
< mappings_len
; ++i
) {
1215 if (StartsWithASCII(mappings
[i
].mime_type
, leading_mime_type
, false)) {
1216 std::vector
<string
> this_extensions
;
1217 base::SplitString(mappings
[i
].extensions
, ',', &this_extensions
);
1218 for (size_t j
= 0; j
< this_extensions
.size(); ++j
) {
1220 base::FilePath::StringType
extension(
1221 base::UTF8ToWide(this_extensions
[j
]));
1223 base::FilePath::StringType
extension(this_extensions
[j
]);
1225 extensions
->insert(extension
);
1231 void GetExtensionsHelper(
1232 const char* const* standard_types
,
1233 size_t standard_types_len
,
1234 const std::string
& leading_mime_type
,
1235 base::hash_set
<base::FilePath::StringType
>* extensions
) {
1236 for (size_t i
= 0; i
< standard_types_len
; ++i
) {
1237 g_mime_util
.Get().GetPlatformExtensionsForMimeType(standard_types
[i
],
1241 // Also look up the extensions from hard-coded mappings in case that some
1242 // supported extensions are not registered in the system registry, like ogg.
1243 GetExtensionsFromHardCodedMappings(primary_mappings
,
1244 arraysize(primary_mappings
),
1248 GetExtensionsFromHardCodedMappings(secondary_mappings
,
1249 arraysize(secondary_mappings
),
1254 // Note that the elements in the source set will be appended to the target
1257 void HashSetToVector(base::hash_set
<T
>* source
, std::vector
<T
>* target
) {
1258 size_t old_target_size
= target
->size();
1259 target
->resize(old_target_size
+ source
->size());
1261 for (typename
base::hash_set
<T
>::iterator iter
= source
->begin();
1262 iter
!= source
->end(); ++iter
, ++i
)
1263 (*target
)[old_target_size
+ i
] = *iter
;
1267 void GetExtensionsForMimeType(
1268 const std::string
& unsafe_mime_type
,
1269 std::vector
<base::FilePath::StringType
>* extensions
) {
1270 if (unsafe_mime_type
== "*/*" || unsafe_mime_type
== "*")
1273 const std::string mime_type
= StringToLowerASCII(unsafe_mime_type
);
1274 base::hash_set
<base::FilePath::StringType
> unique_extensions
;
1276 if (EndsWith(mime_type
, "/*", true)) {
1277 std::string leading_mime_type
= mime_type
.substr(0, mime_type
.length() - 1);
1279 // Find the matching StandardType from within kStandardTypes, or fall
1280 // through to the last (default) StandardType.
1281 const StandardType
* type
= NULL
;
1282 for (size_t i
= 0; i
< arraysize(kStandardTypes
); ++i
) {
1283 type
= &(kStandardTypes
[i
]);
1284 if (type
->leading_mime_type
&&
1285 leading_mime_type
== type
->leading_mime_type
)
1289 GetExtensionsHelper(type
->standard_types
,
1290 type
->standard_types_len
,
1292 &unique_extensions
);
1294 g_mime_util
.Get().GetPlatformExtensionsForMimeType(mime_type
,
1295 &unique_extensions
);
1297 // Also look up the extensions from hard-coded mappings in case that some
1298 // supported extensions are not registered in the system registry, like ogg.
1299 GetExtensionsFromHardCodedMappings(primary_mappings
,
1300 arraysize(primary_mappings
),
1302 &unique_extensions
);
1304 GetExtensionsFromHardCodedMappings(secondary_mappings
,
1305 arraysize(secondary_mappings
),
1307 &unique_extensions
);
1310 HashSetToVector(&unique_extensions
, extensions
);
1313 void RemoveProprietaryMediaTypesAndCodecsForTests() {
1314 g_mime_util
.Get().RemoveProprietaryMediaTypesAndCodecsForTests();
1317 const std::string
GetIANAMediaType(const std::string
& mime_type
) {
1318 for (size_t i
= 0; i
< arraysize(kIanaMediaTypes
); ++i
) {
1319 if (StartsWithASCII(mime_type
, kIanaMediaTypes
[i
].matcher
, true)) {
1320 return kIanaMediaTypes
[i
].name
;
1323 return std::string();
1326 CertificateMimeType
GetCertificateMimeTypeForMimeType(
1327 const std::string
& mime_type
) {
1328 // Don't create a map, there is only one entry in the table,
1329 // except on Android.
1330 for (size_t i
= 0; i
< arraysize(supported_certificate_types
); ++i
) {
1331 if (mime_type
== net::supported_certificate_types
[i
].mime_type
)
1332 return net::supported_certificate_types
[i
].cert_type
;
1334 return CERTIFICATE_MIME_TYPE_UNKNOWN
;
1337 bool IsSupportedCertificateMimeType(const std::string
& mime_type
) {
1338 CertificateMimeType file_type
=
1339 GetCertificateMimeTypeForMimeType(mime_type
);
1340 return file_type
!= CERTIFICATE_MIME_TYPE_UNKNOWN
;
1343 void AddMultipartValueForUpload(const std::string
& value_name
,
1344 const std::string
& value
,
1345 const std::string
& mime_boundary
,
1346 const std::string
& content_type
,
1347 std::string
* post_data
) {
1349 // First line is the boundary.
1350 post_data
->append("--" + mime_boundary
+ "\r\n");
1351 // Next line is the Content-disposition.
1352 post_data
->append("Content-Disposition: form-data; name=\"" +
1353 value_name
+ "\"\r\n");
1354 if (!content_type
.empty()) {
1355 // If Content-type is specified, the next line is that.
1356 post_data
->append("Content-Type: " + content_type
+ "\r\n");
1358 // Leave an empty line and append the value.
1359 post_data
->append("\r\n" + value
+ "\r\n");
1362 void AddMultipartFinalDelimiterForUpload(const std::string
& mime_boundary
,
1363 std::string
* post_data
) {
1365 post_data
->append("--" + mime_boundary
+ "--\r\n");