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_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "net/base/mime_util.h"
18 #include "net/base/platform_mime_util.h"
20 #if defined(OS_ANDROID)
21 #include "base/android/build_info.h"
30 const char matcher
[13];
33 static const MediaType kIanaMediaTypes
[] = {
34 { "application", "application/" },
35 { "audio", "audio/" },
36 { "example", "example/" },
37 { "image", "image/" },
38 { "message", "message/" },
39 { "model", "model/" },
40 { "multipart", "multipart/" },
42 { "video", "video/" },
49 // Singleton utility class for mime types.
50 class MimeUtil
: public PlatformMimeUtil
{
52 bool GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
53 std::string
* mime_type
) const;
55 bool GetMimeTypeFromFile(const base::FilePath
& file_path
,
56 std::string
* mime_type
) const;
58 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
59 std::string
* mime_type
) const;
61 bool IsSupportedImageMimeType(const std::string
& mime_type
) const;
62 bool IsSupportedMediaMimeType(const std::string
& mime_type
) const;
63 bool IsSupportedNonImageMimeType(const std::string
& mime_type
) const;
64 bool IsUnsupportedTextMimeType(const std::string
& mime_type
) const;
65 bool IsSupportedJavascriptMimeType(const std::string
& mime_type
) const;
67 bool IsSupportedMimeType(const std::string
& mime_type
) const;
69 bool MatchesMimeType(const std::string
&mime_type_pattern
,
70 const std::string
&mime_type
) const;
72 bool IsMimeType(const std::string
& type_string
) const;
74 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) const;
76 void ParseCodecString(const std::string
& codecs
,
77 std::vector
<std::string
>* codecs_out
,
80 bool IsStrictMediaMimeType(const std::string
& mime_type
) const;
81 bool IsSupportedStrictMediaMimeType(
82 const std::string
& mime_type
,
83 const std::vector
<std::string
>& codecs
) const;
85 void RemoveProprietaryMediaTypesAndCodecsForTests();
88 friend struct base::DefaultLazyInstanceTraits
<MimeUtil
>;
90 typedef base::hash_set
<std::string
> MimeMappings
;
91 typedef std::map
<std::string
, MimeMappings
> StrictMappings
;
95 // Returns true if |codecs| is nonempty and all the items in it are present in
96 // |supported_codecs|.
97 static bool AreSupportedCodecs(const MimeMappings
& supported_codecs
,
98 const std::vector
<std::string
>& codecs
);
100 // For faster lookup, keep hash sets.
101 void InitializeMimeTypeMaps();
103 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType
& ext
,
104 bool include_platform_types
,
105 std::string
* mime_type
) const;
107 MimeMappings image_map_
;
108 MimeMappings media_map_
;
109 MimeMappings non_image_map_
;
110 MimeMappings unsupported_text_map_
;
111 MimeMappings javascript_map_
;
112 MimeMappings codecs_map_
;
114 StrictMappings strict_format_map_
;
117 // This variable is Leaky because we need to access it from WorkerPool threads.
118 static base::LazyInstance
<MimeUtil
>::Leaky g_mime_util
=
119 LAZY_INSTANCE_INITIALIZER
;
122 const char* mime_type
;
123 const char* extensions
; // comma separated list
126 static const MimeInfo primary_mappings
[] = {
127 { "text/html", "html,htm,shtml,shtm" },
128 { "text/css", "css" },
129 { "text/xml", "xml" },
130 { "image/gif", "gif" },
131 { "image/jpeg", "jpeg,jpg" },
132 { "image/webp", "webp" },
133 { "image/png", "png" },
134 { "video/mp4", "mp4,m4v" },
135 { "audio/x-m4a", "m4a" },
136 { "audio/mp3", "mp3" },
137 { "video/ogg", "ogv,ogm" },
138 { "audio/ogg", "ogg,oga,opus" },
139 { "video/webm", "webm" },
140 { "audio/webm", "webm" },
141 { "audio/wav", "wav" },
142 { "application/xhtml+xml", "xhtml,xht,xhtm" },
143 { "application/x-chrome-extension", "crx" },
144 { "multipart/related", "mhtml,mht" }
147 static const MimeInfo secondary_mappings
[] = {
148 { "application/octet-stream", "exe,com,bin" },
149 { "application/gzip", "gz" },
150 { "application/pdf", "pdf" },
151 { "application/postscript", "ps,eps,ai" },
152 { "application/javascript", "js" },
153 { "application/font-woff", "woff" },
154 { "image/bmp", "bmp" },
155 { "image/x-icon", "ico" },
156 { "image/vnd.microsoft.icon", "ico" },
157 { "image/jpeg", "jfif,pjpeg,pjp" },
158 { "image/tiff", "tiff,tif" },
159 { "image/x-xbitmap", "xbm" },
160 { "image/svg+xml", "svg,svgz" },
161 { "image/x-png", "png"},
162 { "message/rfc822", "eml" },
163 { "text/plain", "txt,text" },
164 { "text/html", "ehtml" },
165 { "application/rss+xml", "rss" },
166 { "application/rdf+xml", "rdf" },
167 { "text/xml", "xsl,xbl,xslt" },
168 { "application/vnd.mozilla.xul+xml", "xul" },
169 { "application/x-shockwave-flash", "swf,swl" },
170 { "application/pkcs7-mime", "p7m,p7c,p7z" },
171 { "application/pkcs7-signature", "p7s" }
174 static const char* FindMimeType(const MimeInfo
* mappings
,
177 size_t ext_len
= strlen(ext
);
179 for (size_t i
= 0; i
< mappings_len
; ++i
) {
180 const char* extensions
= mappings
[i
].extensions
;
182 size_t end_pos
= strcspn(extensions
, ",");
183 if (end_pos
== ext_len
&&
184 base::strncasecmp(extensions
, ext
, ext_len
) == 0)
185 return mappings
[i
].mime_type
;
186 extensions
+= end_pos
;
189 extensions
+= 1; // skip over comma
195 bool MimeUtil::GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
196 string
* result
) const {
197 return GetMimeTypeFromExtensionHelper(ext
, true, result
);
200 bool MimeUtil::GetWellKnownMimeTypeFromExtension(
201 const base::FilePath::StringType
& ext
,
202 string
* result
) const {
203 return GetMimeTypeFromExtensionHelper(ext
, false, result
);
206 bool MimeUtil::GetMimeTypeFromFile(const base::FilePath
& file_path
,
207 string
* result
) const {
208 base::FilePath::StringType file_name_str
= file_path
.Extension();
209 if (file_name_str
.empty())
211 return GetMimeTypeFromExtension(file_name_str
.substr(1), result
);
214 bool MimeUtil::GetMimeTypeFromExtensionHelper(
215 const base::FilePath::StringType
& ext
,
216 bool include_platform_types
,
217 string
* result
) const {
218 // Avoids crash when unable to handle a long file path. See crbug.com/48733.
219 const unsigned kMaxFilePathSize
= 65536;
220 if (ext
.length() > kMaxFilePathSize
)
223 // We implement the same algorithm as Mozilla for mapping a file extension to
224 // a mime type. That is, we first check a hard-coded list (that cannot be
225 // overridden), and then if not found there, we defer to the system registry.
226 // Finally, we scan a secondary hard-coded list to catch types that we can
227 // deduce but that we also want to allow the OS to override.
229 base::FilePath
path_ext(ext
);
230 const string ext_narrow_str
= path_ext
.AsUTF8Unsafe();
231 const char* mime_type
= FindMimeType(primary_mappings
,
232 arraysize(primary_mappings
),
233 ext_narrow_str
.c_str());
239 if (include_platform_types
&& GetPlatformMimeTypeFromExtension(ext
, result
))
242 mime_type
= FindMimeType(secondary_mappings
, arraysize(secondary_mappings
),
243 ext_narrow_str
.c_str());
252 // From WebKit's WebCore/platform/MIMETypeRegistry.cpp:
254 static const char* const supported_image_types
[] = {
262 "image/vnd.microsoft.icon", // ico
263 "image/x-icon", // ico
264 "image/x-xbitmap", // xbm
268 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type
269 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php
270 // This set of codecs is supported by all variations of Chromium.
271 static const char* const common_media_types
[] = {
275 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
287 #if defined(OS_ANDROID)
288 // HLS. Supported by Android ICS and above.
289 "application/vnd.apple.mpegurl",
290 "application/x-mpegurl",
294 // List of proprietary types only supported by Google Chrome.
295 static const char* const proprietary_media_types
[] = {
308 // List of supported codecs when passed in with <source type="...">.
309 // This set of codecs is supported by all variations of Chromium.
311 // Refer to http://wiki.whatwg.org/wiki/Video_type_parameters#Browser_Support
312 // for more information.
314 // The codecs for WAV are integers as defined in Appendix A of RFC2361:
315 // http://tools.ietf.org/html/rfc2361
316 static const char* const common_media_codecs
[] = {
317 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
324 "1" // WAVE_FORMAT_PCM.
327 // List of proprietary codecs only supported by Google Chrome.
328 static const char* const proprietary_media_codecs
[] = {
335 // - does not include javascript types list (see supported_javascript_types)
336 // - does not include types starting with "text/" (see
337 // IsSupportedNonImageMimeType())
338 static const char* const supported_non_image_types
[] = {
339 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type
341 "application/atom+xml",
342 "application/rss+xml",
343 "application/xhtml+xml",
345 "multipart/related", // For MHTML support.
346 "multipart/x-mixed-replace"
347 // Note: ADDING a new type here will probably render it AS HTML. This can
348 // result in cross site scripting.
351 // Dictionary of cryptographic file mime types.
352 struct CertificateMimeTypeInfo
{
353 const char* mime_type
;
354 CertificateMimeType cert_type
;
357 static const CertificateMimeTypeInfo supported_certificate_types
[] = {
358 { "application/x-x509-user-cert",
359 CERTIFICATE_MIME_TYPE_X509_USER_CERT
},
360 #if defined(OS_ANDROID)
361 { "application/x-x509-ca-cert", CERTIFICATE_MIME_TYPE_X509_CA_CERT
},
362 { "application/x-pkcs12", CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE
},
366 // These types are excluded from the logic that allows all text/ types because
367 // while they are technically text, it's very unlikely that a user expects to
368 // see them rendered in text form.
369 static const char* const unsupported_text_types
[] = {
383 "text/comma-separated-values",
385 "text/tab-separated-values",
387 "text/ofx", // http://crbug.com/162238
388 "text/vnd.sun.j2me.app-descriptor" // http://crbug.com/176450
391 // Mozilla 1.8 and WinIE 7 both accept text/javascript and text/ecmascript.
392 // Mozilla 1.8 accepts application/javascript, application/ecmascript, and
393 // application/x-javascript, but WinIE 7 doesn't.
394 // WinIE 7 accepts text/javascript1.1 - text/javascript1.3, text/jscript, and
395 // text/livescript, but Mozilla 1.8 doesn't.
396 // Mozilla 1.8 allows leading and trailing whitespace, but WinIE 7 doesn't.
397 // Mozilla 1.8 and WinIE 7 both accept the empty string, but neither accept a
398 // whitespace-only string.
399 // We want to accept all the values that either of these browsers accept, but
401 static const char* const supported_javascript_types
[] = {
404 "application/javascript",
405 "application/ecmascript",
406 "application/x-javascript",
407 "text/javascript1.1",
408 "text/javascript1.2",
409 "text/javascript1.3",
414 #if defined(OS_ANDROID)
415 static bool IsCodecSupportedOnAndroid(const std::string
& codec
) {
416 // VP9 is supported only in KitKat+ (API Level 19).
417 if ((!codec
.compare("vp9") || !codec
.compare("vp9.0")) &&
418 base::android::BuildInfo::GetInstance()->sdk_int() < 19) {
422 // TODO(vigneshv): Change this similar to the VP9 check once Opus is
423 // supported on Android (http://crbug.com/318436).
424 if (!codec
.compare("opus")) {
431 struct MediaFormatStrict
{
432 const char* mime_type
;
433 const char* codecs_list
;
436 static const MediaFormatStrict format_codec_mappings
[] = {
437 { "video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0" },
438 { "audio/webm", "opus,vorbis" },
439 { "audio/wav", "1" },
440 { "audio/x-wav", "1" },
441 { "video/ogg", "opus,theora,vorbis" },
442 { "audio/ogg", "opus,vorbis" },
443 { "application/ogg", "opus,theora,vorbis" },
444 { "audio/mpeg", "" },
446 { "audio/x-mp3", "" }
449 MimeUtil::MimeUtil() {
450 InitializeMimeTypeMaps();
454 bool MimeUtil::AreSupportedCodecs(const MimeMappings
& supported_codecs
,
455 const std::vector
<std::string
>& codecs
) {
456 if (supported_codecs
.empty())
457 return codecs
.empty();
459 for (size_t i
= 0; i
< codecs
.size(); ++i
) {
460 if (supported_codecs
.find(codecs
[i
]) == supported_codecs
.end())
463 return !codecs
.empty();
466 void MimeUtil::InitializeMimeTypeMaps() {
467 for (size_t i
= 0; i
< arraysize(supported_image_types
); ++i
)
468 image_map_
.insert(supported_image_types
[i
]);
470 // Initialize the supported non-image types.
471 for (size_t i
= 0; i
< arraysize(supported_non_image_types
); ++i
)
472 non_image_map_
.insert(supported_non_image_types
[i
]);
473 for (size_t i
= 0; i
< arraysize(supported_certificate_types
); ++i
)
474 non_image_map_
.insert(supported_certificate_types
[i
].mime_type
);
475 for (size_t i
= 0; i
< arraysize(unsupported_text_types
); ++i
)
476 unsupported_text_map_
.insert(unsupported_text_types
[i
]);
477 for (size_t i
= 0; i
< arraysize(supported_javascript_types
); ++i
)
478 non_image_map_
.insert(supported_javascript_types
[i
]);
479 for (size_t i
= 0; i
< arraysize(common_media_types
); ++i
)
480 non_image_map_
.insert(common_media_types
[i
]);
481 #if defined(USE_PROPRIETARY_CODECS)
482 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
483 non_image_map_
.insert(proprietary_media_types
[i
]);
486 // Initialize the supported media types.
487 for (size_t i
= 0; i
< arraysize(common_media_types
); ++i
)
488 media_map_
.insert(common_media_types
[i
]);
489 #if defined(USE_PROPRIETARY_CODECS)
490 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
)
491 media_map_
.insert(proprietary_media_types
[i
]);
494 for (size_t i
= 0; i
< arraysize(supported_javascript_types
); ++i
)
495 javascript_map_
.insert(supported_javascript_types
[i
]);
497 for (size_t i
= 0; i
< arraysize(common_media_codecs
); ++i
) {
498 #if defined(OS_ANDROID)
499 if (!IsCodecSupportedOnAndroid(common_media_codecs
[i
]))
502 codecs_map_
.insert(common_media_codecs
[i
]);
504 #if defined(USE_PROPRIETARY_CODECS)
505 for (size_t i
= 0; i
< arraysize(proprietary_media_codecs
); ++i
)
506 codecs_map_
.insert(proprietary_media_codecs
[i
]);
509 // Initialize the strict supported media types.
510 for (size_t i
= 0; i
< arraysize(format_codec_mappings
); ++i
) {
511 std::vector
<std::string
> mime_type_codecs
;
512 ParseCodecString(format_codec_mappings
[i
].codecs_list
,
517 for (size_t j
= 0; j
< mime_type_codecs
.size(); ++j
) {
518 #if defined(OS_ANDROID)
519 if (!IsCodecSupportedOnAndroid(mime_type_codecs
[j
]))
522 codecs
.insert(mime_type_codecs
[j
]);
524 strict_format_map_
[format_codec_mappings
[i
].mime_type
] = codecs
;
528 bool MimeUtil::IsSupportedImageMimeType(const std::string
& mime_type
) const {
529 return image_map_
.find(mime_type
) != image_map_
.end();
532 bool MimeUtil::IsSupportedMediaMimeType(const std::string
& mime_type
) const {
533 return media_map_
.find(mime_type
) != media_map_
.end();
536 bool MimeUtil::IsSupportedNonImageMimeType(const std::string
& mime_type
) const {
537 return non_image_map_
.find(mime_type
) != non_image_map_
.end() ||
538 (mime_type
.compare(0, 5, "text/") == 0 &&
539 !IsUnsupportedTextMimeType(mime_type
)) ||
540 (mime_type
.compare(0, 12, "application/") == 0 &&
541 MatchesMimeType("application/*+json", mime_type
));
544 bool MimeUtil::IsUnsupportedTextMimeType(const std::string
& mime_type
) const {
545 return unsupported_text_map_
.find(mime_type
) != unsupported_text_map_
.end();
548 bool MimeUtil::IsSupportedJavascriptMimeType(
549 const std::string
& mime_type
) const {
550 return javascript_map_
.find(mime_type
) != javascript_map_
.end();
553 // Mirrors WebViewImpl::CanShowMIMEType()
554 bool MimeUtil::IsSupportedMimeType(const std::string
& mime_type
) const {
555 return (mime_type
.compare(0, 6, "image/") == 0 &&
556 IsSupportedImageMimeType(mime_type
)) ||
557 IsSupportedNonImageMimeType(mime_type
);
560 // Tests for MIME parameter equality. Each parameter in the |mime_type_pattern|
561 // must be matched by a parameter in the |mime_type|. If there are no
562 // parameters in the pattern, the match is a success.
563 bool MatchesMimeTypeParameters(const std::string
& mime_type_pattern
,
564 const std::string
& mime_type
) {
565 const std::string::size_type semicolon
= mime_type_pattern
.find(';');
566 const std::string::size_type test_semicolon
= mime_type
.find(';');
567 if (semicolon
!= std::string::npos
) {
568 if (test_semicolon
== std::string::npos
)
571 std::vector
<std::string
> pattern_parameters
;
572 base::SplitString(mime_type_pattern
.substr(semicolon
+ 1),
573 ';', &pattern_parameters
);
575 std::vector
<std::string
> test_parameters
;
576 base::SplitString(mime_type
.substr(test_semicolon
+ 1),
577 ';', &test_parameters
);
579 sort(pattern_parameters
.begin(), pattern_parameters
.end());
580 sort(test_parameters
.begin(), test_parameters
.end());
581 std::vector
<std::string
> difference
=
582 base::STLSetDifference
<std::vector
<std::string
> >(pattern_parameters
,
584 return difference
.size() == 0;
589 // This comparison handles absolute maching and also basic
590 // wildcards. The plugin mime types could be:
595 // Also tests mime parameters -- all parameters in the pattern must be present
596 // in the tested type for a match to succeed.
597 bool MimeUtil::MatchesMimeType(const std::string
& mime_type_pattern
,
598 const std::string
& mime_type
) const {
599 // Verify caller is passing lowercase strings.
600 DCHECK_EQ(StringToLowerASCII(mime_type_pattern
), mime_type_pattern
);
601 DCHECK_EQ(StringToLowerASCII(mime_type
), mime_type
);
603 if (mime_type_pattern
.empty())
606 std::string::size_type semicolon
= mime_type_pattern
.find(';');
607 const std::string
base_pattern(mime_type_pattern
.substr(0, semicolon
));
608 semicolon
= mime_type
.find(';');
609 const std::string
base_type(mime_type
.substr(0, semicolon
));
611 if (base_pattern
== "*" || base_pattern
== "*/*")
612 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
614 const std::string::size_type star
= base_pattern
.find('*');
615 if (star
== std::string::npos
) {
616 if (base_pattern
== base_type
)
617 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
622 // Test length to prevent overlap between |left| and |right|.
623 if (base_type
.length() < base_pattern
.length() - 1)
626 const std::string
left(base_pattern
.substr(0, star
));
627 const std::string
right(base_pattern
.substr(star
+ 1));
629 if (base_type
.find(left
) != 0)
632 if (!right
.empty() &&
633 base_type
.rfind(right
) != base_type
.length() - right
.length())
636 return MatchesMimeTypeParameters(mime_type_pattern
, mime_type
);
639 // See http://www.iana.org/assignments/media-types/index.html
640 static const char* legal_top_level_types
[] = {
652 bool MimeUtil::IsMimeType(const std::string
& type_string
) const {
653 // MIME types are always ASCII and case-insensitive (at least, the top-level
654 // and secondary types we care about).
655 if (!base::IsStringASCII(type_string
))
658 if (type_string
== "*/*" || type_string
== "*")
661 for (size_t i
= 0; i
< arraysize(legal_top_level_types
); ++i
) {
662 if (StartsWithASCII(type_string
, legal_top_level_types
[i
], false) &&
663 type_string
.length() > strlen(legal_top_level_types
[i
])) {
668 // If there's a "/" separator character, and the token before it is
669 // "x-" + (ascii characters), it is also a MIME type.
670 size_t slash
= type_string
.find('/');
672 slash
== std::string::npos
|| slash
== type_string
.length() - 1) {
676 if (StartsWithASCII(type_string
, "x-", false))
682 bool MimeUtil::AreSupportedMediaCodecs(
683 const std::vector
<std::string
>& codecs
) const {
684 return AreSupportedCodecs(codecs_map_
, codecs
);
687 void MimeUtil::ParseCodecString(const std::string
& codecs
,
688 std::vector
<std::string
>* codecs_out
,
690 std::string no_quote_codecs
;
691 base::TrimString(codecs
, "\"", &no_quote_codecs
);
692 base::SplitString(no_quote_codecs
, ',', codecs_out
);
697 // Strip everything past the first '.'
698 for (std::vector
<std::string
>::iterator it
= codecs_out
->begin();
699 it
!= codecs_out
->end();
701 size_t found
= it
->find_first_of('.');
702 if (found
!= std::string::npos
)
707 bool MimeUtil::IsStrictMediaMimeType(const std::string
& mime_type
) const {
708 if (strict_format_map_
.find(mime_type
) == strict_format_map_
.end())
713 bool MimeUtil::IsSupportedStrictMediaMimeType(
714 const std::string
& mime_type
,
715 const std::vector
<std::string
>& codecs
) const {
716 StrictMappings::const_iterator it
= strict_format_map_
.find(mime_type
);
717 return (it
!= strict_format_map_
.end()) &&
718 AreSupportedCodecs(it
->second
, codecs
);
721 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
722 for (size_t i
= 0; i
< arraysize(proprietary_media_types
); ++i
) {
723 non_image_map_
.erase(proprietary_media_types
[i
]);
724 media_map_
.erase(proprietary_media_types
[i
]);
726 for (size_t i
= 0; i
< arraysize(proprietary_media_codecs
); ++i
)
727 codecs_map_
.erase(proprietary_media_codecs
[i
]);
730 //----------------------------------------------------------------------------
731 // Wrappers for the singleton
732 //----------------------------------------------------------------------------
734 bool GetMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
735 std::string
* mime_type
) {
736 return g_mime_util
.Get().GetMimeTypeFromExtension(ext
, mime_type
);
739 bool GetMimeTypeFromFile(const base::FilePath
& file_path
,
740 std::string
* mime_type
) {
741 return g_mime_util
.Get().GetMimeTypeFromFile(file_path
, mime_type
);
744 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType
& ext
,
745 std::string
* mime_type
) {
746 return g_mime_util
.Get().GetWellKnownMimeTypeFromExtension(ext
, mime_type
);
749 bool GetPreferredExtensionForMimeType(const std::string
& mime_type
,
750 base::FilePath::StringType
* extension
) {
751 return g_mime_util
.Get().GetPreferredExtensionForMimeType(mime_type
,
755 bool IsSupportedImageMimeType(const std::string
& mime_type
) {
756 return g_mime_util
.Get().IsSupportedImageMimeType(mime_type
);
759 bool IsSupportedMediaMimeType(const std::string
& mime_type
) {
760 return g_mime_util
.Get().IsSupportedMediaMimeType(mime_type
);
763 bool IsSupportedNonImageMimeType(const std::string
& mime_type
) {
764 return g_mime_util
.Get().IsSupportedNonImageMimeType(mime_type
);
767 bool IsUnsupportedTextMimeType(const std::string
& mime_type
) {
768 return g_mime_util
.Get().IsUnsupportedTextMimeType(mime_type
);
771 bool IsSupportedJavascriptMimeType(const std::string
& mime_type
) {
772 return g_mime_util
.Get().IsSupportedJavascriptMimeType(mime_type
);
775 bool IsSupportedMimeType(const std::string
& mime_type
) {
776 return g_mime_util
.Get().IsSupportedMimeType(mime_type
);
779 bool MatchesMimeType(const std::string
& mime_type_pattern
,
780 const std::string
& mime_type
) {
781 return g_mime_util
.Get().MatchesMimeType(mime_type_pattern
, mime_type
);
784 bool IsMimeType(const std::string
& type_string
) {
785 return g_mime_util
.Get().IsMimeType(type_string
);
788 bool AreSupportedMediaCodecs(const std::vector
<std::string
>& codecs
) {
789 return g_mime_util
.Get().AreSupportedMediaCodecs(codecs
);
792 bool IsStrictMediaMimeType(const std::string
& mime_type
) {
793 return g_mime_util
.Get().IsStrictMediaMimeType(mime_type
);
796 bool IsSupportedStrictMediaMimeType(const std::string
& mime_type
,
797 const std::vector
<std::string
>& codecs
) {
798 return g_mime_util
.Get().IsSupportedStrictMediaMimeType(mime_type
, codecs
);
801 void ParseCodecString(const std::string
& codecs
,
802 std::vector
<std::string
>* codecs_out
,
804 g_mime_util
.Get().ParseCodecString(codecs
, codecs_out
, strip
);
809 // From http://www.w3schools.com/media/media_mimeref.asp and
810 // http://plugindoc.mozdev.org/winmime.php
811 static const char* const kStandardImageTypes
[] = {
823 "image/vnd.microsoft.icon",
824 "image/x-cmu-raster",
827 "image/x-portable-anymap",
828 "image/x-portable-bitmap",
829 "image/x-portable-graymap",
830 "image/x-portable-pixmap",
834 "image/x-xwindowdump"
836 static const char* const kStandardAudioTypes
[] = {
852 "audio/vnd.rn-realaudio",
855 static const char* const kStandardVideoTypes
[] = {
872 struct StandardType
{
873 const char* leading_mime_type
;
874 const char* const* standard_types
;
875 size_t standard_types_len
;
877 static const StandardType kStandardTypes
[] = {
878 { "image/", kStandardImageTypes
, arraysize(kStandardImageTypes
) },
879 { "audio/", kStandardAudioTypes
, arraysize(kStandardAudioTypes
) },
880 { "video/", kStandardVideoTypes
, arraysize(kStandardVideoTypes
) },
884 void GetExtensionsFromHardCodedMappings(
885 const MimeInfo
* mappings
,
887 const std::string
& leading_mime_type
,
888 base::hash_set
<base::FilePath::StringType
>* extensions
) {
889 base::FilePath::StringType extension
;
890 for (size_t i
= 0; i
< mappings_len
; ++i
) {
891 if (StartsWithASCII(mappings
[i
].mime_type
, leading_mime_type
, false)) {
892 std::vector
<string
> this_extensions
;
893 base::SplitStringUsingSubstr(mappings
[i
].extensions
, ",",
895 for (size_t j
= 0; j
< this_extensions
.size(); ++j
) {
897 base::FilePath::StringType
extension(
898 base::UTF8ToWide(this_extensions
[j
]));
900 base::FilePath::StringType
extension(this_extensions
[j
]);
902 extensions
->insert(extension
);
908 void GetExtensionsHelper(
909 const char* const* standard_types
,
910 size_t standard_types_len
,
911 const std::string
& leading_mime_type
,
912 base::hash_set
<base::FilePath::StringType
>* extensions
) {
913 for (size_t i
= 0; i
< standard_types_len
; ++i
) {
914 g_mime_util
.Get().GetPlatformExtensionsForMimeType(standard_types
[i
],
918 // Also look up the extensions from hard-coded mappings in case that some
919 // supported extensions are not registered in the system registry, like ogg.
920 GetExtensionsFromHardCodedMappings(primary_mappings
,
921 arraysize(primary_mappings
),
925 GetExtensionsFromHardCodedMappings(secondary_mappings
,
926 arraysize(secondary_mappings
),
931 // Note that the elements in the source set will be appended to the target
934 void HashSetToVector(base::hash_set
<T
>* source
, std::vector
<T
>* target
) {
935 size_t old_target_size
= target
->size();
936 target
->resize(old_target_size
+ source
->size());
938 for (typename
base::hash_set
<T
>::iterator iter
= source
->begin();
939 iter
!= source
->end(); ++iter
, ++i
)
940 (*target
)[old_target_size
+ i
] = *iter
;
944 void GetExtensionsForMimeType(
945 const std::string
& unsafe_mime_type
,
946 std::vector
<base::FilePath::StringType
>* extensions
) {
947 if (unsafe_mime_type
== "*/*" || unsafe_mime_type
== "*")
950 const std::string mime_type
= StringToLowerASCII(unsafe_mime_type
);
951 base::hash_set
<base::FilePath::StringType
> unique_extensions
;
953 if (EndsWith(mime_type
, "/*", true)) {
954 std::string leading_mime_type
= mime_type
.substr(0, mime_type
.length() - 1);
956 // Find the matching StandardType from within kStandardTypes, or fall
957 // through to the last (default) StandardType.
958 const StandardType
* type
= NULL
;
959 for (size_t i
= 0; i
< arraysize(kStandardTypes
); ++i
) {
960 type
= &(kStandardTypes
[i
]);
961 if (type
->leading_mime_type
&&
962 leading_mime_type
== type
->leading_mime_type
)
966 GetExtensionsHelper(type
->standard_types
,
967 type
->standard_types_len
,
971 g_mime_util
.Get().GetPlatformExtensionsForMimeType(mime_type
,
974 // Also look up the extensions from hard-coded mappings in case that some
975 // supported extensions are not registered in the system registry, like ogg.
976 GetExtensionsFromHardCodedMappings(primary_mappings
,
977 arraysize(primary_mappings
),
981 GetExtensionsFromHardCodedMappings(secondary_mappings
,
982 arraysize(secondary_mappings
),
987 HashSetToVector(&unique_extensions
, extensions
);
990 void RemoveProprietaryMediaTypesAndCodecsForTests() {
991 g_mime_util
.Get().RemoveProprietaryMediaTypesAndCodecsForTests();
994 const std::string
GetIANAMediaType(const std::string
& mime_type
) {
995 for (size_t i
= 0; i
< arraysize(kIanaMediaTypes
); ++i
) {
996 if (StartsWithASCII(mime_type
, kIanaMediaTypes
[i
].matcher
, true)) {
997 return kIanaMediaTypes
[i
].name
;
1000 return std::string();
1003 CertificateMimeType
GetCertificateMimeTypeForMimeType(
1004 const std::string
& mime_type
) {
1005 // Don't create a map, there is only one entry in the table,
1006 // except on Android.
1007 for (size_t i
= 0; i
< arraysize(supported_certificate_types
); ++i
) {
1008 if (mime_type
== net::supported_certificate_types
[i
].mime_type
)
1009 return net::supported_certificate_types
[i
].cert_type
;
1011 return CERTIFICATE_MIME_TYPE_UNKNOWN
;
1014 bool IsSupportedCertificateMimeType(const std::string
& mime_type
) {
1015 CertificateMimeType file_type
=
1016 GetCertificateMimeTypeForMimeType(mime_type
);
1017 return file_type
!= CERTIFICATE_MIME_TYPE_UNKNOWN
;
1020 void AddMultipartValueForUpload(const std::string
& value_name
,
1021 const std::string
& value
,
1022 const std::string
& mime_boundary
,
1023 const std::string
& content_type
,
1024 std::string
* post_data
) {
1026 // First line is the boundary.
1027 post_data
->append("--" + mime_boundary
+ "\r\n");
1028 // Next line is the Content-disposition.
1029 post_data
->append("Content-Disposition: form-data; name=\"" +
1030 value_name
+ "\"\r\n");
1031 if (!content_type
.empty()) {
1032 // If Content-type is specified, the next line is that.
1033 post_data
->append("Content-Type: " + content_type
+ "\r\n");
1035 // Leave an empty line and append the value.
1036 post_data
->append("\r\n" + value
+ "\r\n");
1039 void AddMultipartFinalDelimiterForUpload(const std::string
& mime_boundary
,
1040 std::string
* post_data
) {
1042 post_data
->append("--" + mime_boundary
+ "--\r\n");