Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / net / base / mime_util.cc
blob2dee17dab1d2d41a89a94093acd84b3d7a913f27
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.
5 #include <algorithm>
6 #include <iterator>
7 #include <map>
8 #include <string>
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"
22 #endif
24 using std::string;
26 namespace {
28 struct MediaType {
29 const char name[12];
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/" },
41 { "text", "text/" },
42 { "video", "video/" },
45 } // namespace
47 namespace net {
49 // Singleton utility class for mime types.
50 class MimeUtil : public PlatformMimeUtil {
51 public:
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,
78 bool strip);
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();
87 private:
88 friend struct base::DefaultLazyInstanceTraits<MimeUtil>;
90 typedef base::hash_set<std::string> MimeMappings;
91 typedef std::map<std::string, MimeMappings> StrictMappings;
93 MimeUtil();
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_;
115 }; // class MimeUtil
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;
121 struct MimeInfo {
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 { "message/rfc822", "eml" },
162 { "text/plain", "txt,text" },
163 { "text/html", "ehtml" },
164 { "application/rss+xml", "rss" },
165 { "application/rdf+xml", "rdf" },
166 { "text/xml", "xsl,xbl,xslt" },
167 { "application/vnd.mozilla.xul+xml", "xul" },
168 { "application/x-shockwave-flash", "swf,swl" },
169 { "application/pkcs7-mime", "p7m,p7c,p7z" },
170 { "application/pkcs7-signature", "p7s" }
173 static const char* FindMimeType(const MimeInfo* mappings,
174 size_t mappings_len,
175 const char* ext) {
176 size_t ext_len = strlen(ext);
178 for (size_t i = 0; i < mappings_len; ++i) {
179 const char* extensions = mappings[i].extensions;
180 for (;;) {
181 size_t end_pos = strcspn(extensions, ",");
182 if (end_pos == ext_len &&
183 base::strncasecmp(extensions, ext, ext_len) == 0)
184 return mappings[i].mime_type;
185 extensions += end_pos;
186 if (!*extensions)
187 break;
188 extensions += 1; // skip over comma
191 return NULL;
194 bool MimeUtil::GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
195 string* result) const {
196 return GetMimeTypeFromExtensionHelper(ext, true, result);
199 bool MimeUtil::GetWellKnownMimeTypeFromExtension(
200 const base::FilePath::StringType& ext,
201 string* result) const {
202 return GetMimeTypeFromExtensionHelper(ext, false, result);
205 bool MimeUtil::GetMimeTypeFromFile(const base::FilePath& file_path,
206 string* result) const {
207 base::FilePath::StringType file_name_str = file_path.Extension();
208 if (file_name_str.empty())
209 return false;
210 return GetMimeTypeFromExtension(file_name_str.substr(1), result);
213 bool MimeUtil::GetMimeTypeFromExtensionHelper(
214 const base::FilePath::StringType& ext,
215 bool include_platform_types,
216 string* result) const {
217 // Avoids crash when unable to handle a long file path. See crbug.com/48733.
218 const unsigned kMaxFilePathSize = 65536;
219 if (ext.length() > kMaxFilePathSize)
220 return false;
222 // We implement the same algorithm as Mozilla for mapping a file extension to
223 // a mime type. That is, we first check a hard-coded list (that cannot be
224 // overridden), and then if not found there, we defer to the system registry.
225 // Finally, we scan a secondary hard-coded list to catch types that we can
226 // deduce but that we also want to allow the OS to override.
228 base::FilePath path_ext(ext);
229 const string ext_narrow_str = path_ext.AsUTF8Unsafe();
230 const char* mime_type = FindMimeType(primary_mappings,
231 arraysize(primary_mappings),
232 ext_narrow_str.c_str());
233 if (mime_type) {
234 *result = mime_type;
235 return true;
238 if (include_platform_types && GetPlatformMimeTypeFromExtension(ext, result))
239 return true;
241 mime_type = FindMimeType(secondary_mappings, arraysize(secondary_mappings),
242 ext_narrow_str.c_str());
243 if (mime_type) {
244 *result = mime_type;
245 return true;
248 return false;
251 // From WebKit's WebCore/platform/MIMETypeRegistry.cpp:
253 static const char* const supported_image_types[] = {
254 "image/jpeg",
255 "image/pjpeg",
256 "image/jpg",
257 "image/webp",
258 "image/png",
259 "image/gif",
260 "image/bmp",
261 "image/vnd.microsoft.icon", // ico
262 "image/x-icon", // ico
263 "image/x-xbitmap" // xbm
266 // A list of media types: http://en.wikipedia.org/wiki/Internet_media_type
267 // A comprehensive mime type list: http://plugindoc.mozdev.org/winmime.php
268 // This set of codecs is supported by all variations of Chromium.
269 static const char* const common_media_types[] = {
270 // Ogg.
271 "audio/ogg",
272 "application/ogg",
273 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
274 "video/ogg",
275 #endif
277 // WebM.
278 "video/webm",
279 "audio/webm",
281 // Wav.
282 "audio/wav",
283 "audio/x-wav",
285 #if defined(OS_ANDROID)
286 // HLS. Supported by Android ICS and above.
287 "application/vnd.apple.mpegurl",
288 "application/x-mpegurl",
289 #endif
292 // List of proprietary types only supported by Google Chrome.
293 static const char* const proprietary_media_types[] = {
294 // MPEG-4.
295 "video/mp4",
296 "video/x-m4v",
297 "audio/mp4",
298 "audio/x-m4a",
300 // MP3.
301 "audio/mp3",
302 "audio/x-mp3",
303 "audio/mpeg",
306 // List of supported codecs when passed in with <source type="...">.
307 // This set of codecs is supported by all variations of Chromium.
309 // Refer to http://wiki.whatwg.org/wiki/Video_type_parameters#Browser_Support
310 // for more information.
312 // The codecs for WAV are integers as defined in Appendix A of RFC2361:
313 // http://tools.ietf.org/html/rfc2361
314 static const char* const common_media_codecs[] = {
315 #if !defined(OS_ANDROID) // Android doesn't support Ogg Theora.
316 "theora",
317 #endif
318 "opus",
319 "vorbis",
320 "vp8",
321 "vp9",
322 "1" // WAVE_FORMAT_PCM.
325 // List of proprietary codecs only supported by Google Chrome.
326 static const char* const proprietary_media_codecs[] = {
327 "avc1",
328 "avc3",
329 "mp4a"
332 // Note:
333 // - does not include javascript types list (see supported_javascript_types)
334 // - does not include types starting with "text/" (see
335 // IsSupportedNonImageMimeType())
336 static const char* const supported_non_image_types[] = {
337 "image/svg+xml", // SVG is text-based XML, even though it has an image/ type
338 "application/xml",
339 "application/atom+xml",
340 "application/rss+xml",
341 "application/xhtml+xml",
342 "application/json",
343 "multipart/related", // For MHTML support.
344 "multipart/x-mixed-replace"
345 // Note: ADDING a new type here will probably render it AS HTML. This can
346 // result in cross site scripting.
349 // Dictionary of cryptographic file mime types.
350 struct CertificateMimeTypeInfo {
351 const char* mime_type;
352 CertificateMimeType cert_type;
355 static const CertificateMimeTypeInfo supported_certificate_types[] = {
356 { "application/x-x509-user-cert",
357 CERTIFICATE_MIME_TYPE_X509_USER_CERT },
358 #if defined(OS_ANDROID)
359 { "application/x-x509-ca-cert", CERTIFICATE_MIME_TYPE_X509_CA_CERT },
360 { "application/x-pkcs12", CERTIFICATE_MIME_TYPE_PKCS12_ARCHIVE },
361 #endif
364 // These types are excluded from the logic that allows all text/ types because
365 // while they are technically text, it's very unlikely that a user expects to
366 // see them rendered in text form.
367 static const char* const unsupported_text_types[] = {
368 "text/calendar",
369 "text/x-calendar",
370 "text/x-vcalendar",
371 "text/vcalendar",
372 "text/vcard",
373 "text/x-vcard",
374 "text/directory",
375 "text/ldif",
376 "text/qif",
377 "text/x-qif",
378 "text/x-csv",
379 "text/x-vcf",
380 "text/rtf",
381 "text/comma-separated-values",
382 "text/csv",
383 "text/tab-separated-values",
384 "text/tsv",
385 "text/ofx", // http://crbug.com/162238
386 "text/vnd.sun.j2me.app-descriptor" // http://crbug.com/176450
389 // Mozilla 1.8 and WinIE 7 both accept text/javascript and text/ecmascript.
390 // Mozilla 1.8 accepts application/javascript, application/ecmascript, and
391 // application/x-javascript, but WinIE 7 doesn't.
392 // WinIE 7 accepts text/javascript1.1 - text/javascript1.3, text/jscript, and
393 // text/livescript, but Mozilla 1.8 doesn't.
394 // Mozilla 1.8 allows leading and trailing whitespace, but WinIE 7 doesn't.
395 // Mozilla 1.8 and WinIE 7 both accept the empty string, but neither accept a
396 // whitespace-only string.
397 // We want to accept all the values that either of these browsers accept, but
398 // not other values.
399 static const char* const supported_javascript_types[] = {
400 "text/javascript",
401 "text/ecmascript",
402 "application/javascript",
403 "application/ecmascript",
404 "application/x-javascript",
405 "text/javascript1.1",
406 "text/javascript1.2",
407 "text/javascript1.3",
408 "text/jscript",
409 "text/livescript"
412 #if defined(OS_ANDROID)
413 static bool IsCodecSupportedOnAndroid(const std::string& codec) {
414 // VP9 is supported only in KitKat+ (API Level 19).
415 if ((!codec.compare("vp9") || !codec.compare("vp9.0")) &&
416 base::android::BuildInfo::GetInstance()->sdk_int() < 19) {
417 return false;
420 // TODO(vigneshv): Change this similar to the VP9 check once Opus is
421 // supported on Android (http://crbug.com/318436).
422 if (!codec.compare("opus")) {
423 return false;
425 return true;
427 #endif
429 struct MediaFormatStrict {
430 const char* mime_type;
431 const char* codecs_list;
434 static const MediaFormatStrict format_codec_mappings[] = {
435 { "video/webm", "opus,vorbis,vp8,vp8.0,vp9,vp9.0" },
436 { "audio/webm", "opus,vorbis" },
437 { "audio/wav", "1" }
440 MimeUtil::MimeUtil() {
441 InitializeMimeTypeMaps();
444 // static
445 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs,
446 const std::vector<std::string>& codecs) {
447 for (size_t i = 0; i < codecs.size(); ++i) {
448 if (supported_codecs.find(codecs[i]) == supported_codecs.end())
449 return false;
451 return !codecs.empty();
454 void MimeUtil::InitializeMimeTypeMaps() {
455 for (size_t i = 0; i < arraysize(supported_image_types); ++i)
456 image_map_.insert(supported_image_types[i]);
458 // Initialize the supported non-image types.
459 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i)
460 non_image_map_.insert(supported_non_image_types[i]);
461 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i)
462 non_image_map_.insert(supported_certificate_types[i].mime_type);
463 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i)
464 unsupported_text_map_.insert(unsupported_text_types[i]);
465 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i)
466 non_image_map_.insert(supported_javascript_types[i]);
467 for (size_t i = 0; i < arraysize(common_media_types); ++i)
468 non_image_map_.insert(common_media_types[i]);
469 #if defined(USE_PROPRIETARY_CODECS)
470 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i)
471 non_image_map_.insert(proprietary_media_types[i]);
472 #endif
474 // Initialize the supported media types.
475 for (size_t i = 0; i < arraysize(common_media_types); ++i)
476 media_map_.insert(common_media_types[i]);
477 #if defined(USE_PROPRIETARY_CODECS)
478 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i)
479 media_map_.insert(proprietary_media_types[i]);
480 #endif
482 for (size_t i = 0; i < arraysize(supported_javascript_types); ++i)
483 javascript_map_.insert(supported_javascript_types[i]);
485 for (size_t i = 0; i < arraysize(common_media_codecs); ++i) {
486 #if defined(OS_ANDROID)
487 if (!IsCodecSupportedOnAndroid(common_media_codecs[i]))
488 continue;
489 #endif
490 codecs_map_.insert(common_media_codecs[i]);
492 #if defined(USE_PROPRIETARY_CODECS)
493 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i)
494 codecs_map_.insert(proprietary_media_codecs[i]);
495 #endif
497 // Initialize the strict supported media types.
498 for (size_t i = 0; i < arraysize(format_codec_mappings); ++i) {
499 std::vector<std::string> mime_type_codecs;
500 ParseCodecString(format_codec_mappings[i].codecs_list,
501 &mime_type_codecs,
502 false);
504 MimeMappings codecs;
505 for (size_t j = 0; j < mime_type_codecs.size(); ++j) {
506 #if defined(OS_ANDROID)
507 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j]))
508 continue;
509 #endif
510 codecs.insert(mime_type_codecs[j]);
512 strict_format_map_[format_codec_mappings[i].mime_type] = codecs;
516 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const {
517 return image_map_.find(mime_type) != image_map_.end();
520 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const {
521 return media_map_.find(mime_type) != media_map_.end();
524 bool MimeUtil::IsSupportedNonImageMimeType(const std::string& mime_type) const {
525 return non_image_map_.find(mime_type) != non_image_map_.end() ||
526 (mime_type.compare(0, 5, "text/") == 0 &&
527 !IsUnsupportedTextMimeType(mime_type)) ||
528 (mime_type.compare(0, 12, "application/") == 0 &&
529 MatchesMimeType("application/*+json", mime_type));
532 bool MimeUtil::IsUnsupportedTextMimeType(const std::string& mime_type) const {
533 return unsupported_text_map_.find(mime_type) != unsupported_text_map_.end();
536 bool MimeUtil::IsSupportedJavascriptMimeType(
537 const std::string& mime_type) const {
538 return javascript_map_.find(mime_type) != javascript_map_.end();
541 // Mirrors WebViewImpl::CanShowMIMEType()
542 bool MimeUtil::IsSupportedMimeType(const std::string& mime_type) const {
543 return (mime_type.compare(0, 6, "image/") == 0 &&
544 IsSupportedImageMimeType(mime_type)) ||
545 IsSupportedNonImageMimeType(mime_type);
548 // Tests for MIME parameter equality. Each parameter in the |mime_type_pattern|
549 // must be matched by a parameter in the |mime_type|. If there are no
550 // parameters in the pattern, the match is a success.
551 bool MatchesMimeTypeParameters(const std::string& mime_type_pattern,
552 const std::string& mime_type) {
553 const std::string::size_type semicolon = mime_type_pattern.find(';');
554 const std::string::size_type test_semicolon = mime_type.find(';');
555 if (semicolon != std::string::npos) {
556 if (test_semicolon == std::string::npos)
557 return false;
559 std::vector<std::string> pattern_parameters;
560 base::SplitString(mime_type_pattern.substr(semicolon + 1),
561 ';', &pattern_parameters);
563 std::vector<std::string> test_parameters;
564 base::SplitString(mime_type.substr(test_semicolon + 1),
565 ';', &test_parameters);
567 sort(pattern_parameters.begin(), pattern_parameters.end());
568 sort(test_parameters.begin(), test_parameters.end());
569 std::vector<std::string> difference =
570 base::STLSetDifference<std::vector<std::string> >(pattern_parameters,
571 test_parameters);
572 return difference.size() == 0;
574 return true;
577 // This comparison handles absolute maching and also basic
578 // wildcards. The plugin mime types could be:
579 // application/x-foo
580 // application/*
581 // application/*+xml
582 // *
583 // Also tests mime parameters -- all parameters in the pattern must be present
584 // in the tested type for a match to succeed.
585 bool MimeUtil::MatchesMimeType(const std::string& mime_type_pattern,
586 const std::string& mime_type) const {
587 // Verify caller is passing lowercase strings.
588 DCHECK_EQ(StringToLowerASCII(mime_type_pattern), mime_type_pattern);
589 DCHECK_EQ(StringToLowerASCII(mime_type), mime_type);
591 if (mime_type_pattern.empty())
592 return false;
594 std::string::size_type semicolon = mime_type_pattern.find(';');
595 const std::string base_pattern(mime_type_pattern.substr(0, semicolon));
596 semicolon = mime_type.find(';');
597 const std::string base_type(mime_type.substr(0, semicolon));
599 if (base_pattern == "*" || base_pattern == "*/*")
600 return MatchesMimeTypeParameters(mime_type_pattern, mime_type);
602 const std::string::size_type star = base_pattern.find('*');
603 if (star == std::string::npos) {
604 if (base_pattern == base_type)
605 return MatchesMimeTypeParameters(mime_type_pattern, mime_type);
606 else
607 return false;
610 // Test length to prevent overlap between |left| and |right|.
611 if (base_type.length() < base_pattern.length() - 1)
612 return false;
614 const std::string left(base_pattern.substr(0, star));
615 const std::string right(base_pattern.substr(star + 1));
617 if (base_type.find(left) != 0)
618 return false;
620 if (!right.empty() &&
621 base_type.rfind(right) != base_type.length() - right.length())
622 return false;
624 return MatchesMimeTypeParameters(mime_type_pattern, mime_type);
627 // See http://www.iana.org/assignments/media-types/index.html
628 static const char* legal_top_level_types[] = {
629 "application/",
630 "audio/",
631 "example/",
632 "image/",
633 "message/",
634 "model/",
635 "multipart/",
636 "text/",
637 "video/",
640 bool MimeUtil::IsMimeType(const std::string& type_string) const {
641 // MIME types are always ASCII and case-insensitive (at least, the top-level
642 // and secondary types we care about).
643 if (!IsStringASCII(type_string))
644 return false;
646 if (type_string == "*/*" || type_string == "*")
647 return true;
649 for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) {
650 if (StartsWithASCII(type_string, legal_top_level_types[i], false) &&
651 type_string.length() > strlen(legal_top_level_types[i])) {
652 return true;
656 // If there's a "/" separator character, and the token before it is
657 // "x-" + (ascii characters), it is also a MIME type.
658 size_t slash = type_string.find('/');
659 if (slash < 3 ||
660 slash == std::string::npos || slash == type_string.length() - 1) {
661 return false;
664 if (StartsWithASCII(type_string, "x-", false))
665 return true;
667 return false;
670 bool MimeUtil::AreSupportedMediaCodecs(
671 const std::vector<std::string>& codecs) const {
672 return AreSupportedCodecs(codecs_map_, codecs);
675 void MimeUtil::ParseCodecString(const std::string& codecs,
676 std::vector<std::string>* codecs_out,
677 bool strip) {
678 std::string no_quote_codecs;
679 base::TrimString(codecs, "\"", &no_quote_codecs);
680 base::SplitString(no_quote_codecs, ',', codecs_out);
682 if (!strip)
683 return;
685 // Strip everything past the first '.'
686 for (std::vector<std::string>::iterator it = codecs_out->begin();
687 it != codecs_out->end();
688 ++it) {
689 size_t found = it->find_first_of('.');
690 if (found != std::string::npos)
691 it->resize(found);
695 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const {
696 if (strict_format_map_.find(mime_type) == strict_format_map_.end())
697 return false;
698 return true;
701 bool MimeUtil::IsSupportedStrictMediaMimeType(
702 const std::string& mime_type,
703 const std::vector<std::string>& codecs) const {
704 StrictMappings::const_iterator it = strict_format_map_.find(mime_type);
705 return (it != strict_format_map_.end()) &&
706 AreSupportedCodecs(it->second, codecs);
709 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() {
710 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) {
711 non_image_map_.erase(proprietary_media_types[i]);
712 media_map_.erase(proprietary_media_types[i]);
714 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i)
715 codecs_map_.erase(proprietary_media_codecs[i]);
718 //----------------------------------------------------------------------------
719 // Wrappers for the singleton
720 //----------------------------------------------------------------------------
722 bool GetMimeTypeFromExtension(const base::FilePath::StringType& ext,
723 std::string* mime_type) {
724 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type);
727 bool GetMimeTypeFromFile(const base::FilePath& file_path,
728 std::string* mime_type) {
729 return g_mime_util.Get().GetMimeTypeFromFile(file_path, mime_type);
732 bool GetWellKnownMimeTypeFromExtension(const base::FilePath::StringType& ext,
733 std::string* mime_type) {
734 return g_mime_util.Get().GetWellKnownMimeTypeFromExtension(ext, mime_type);
737 bool GetPreferredExtensionForMimeType(const std::string& mime_type,
738 base::FilePath::StringType* extension) {
739 return g_mime_util.Get().GetPreferredExtensionForMimeType(mime_type,
740 extension);
743 bool IsSupportedImageMimeType(const std::string& mime_type) {
744 return g_mime_util.Get().IsSupportedImageMimeType(mime_type);
747 bool IsSupportedMediaMimeType(const std::string& mime_type) {
748 return g_mime_util.Get().IsSupportedMediaMimeType(mime_type);
751 bool IsSupportedNonImageMimeType(const std::string& mime_type) {
752 return g_mime_util.Get().IsSupportedNonImageMimeType(mime_type);
755 bool IsUnsupportedTextMimeType(const std::string& mime_type) {
756 return g_mime_util.Get().IsUnsupportedTextMimeType(mime_type);
759 bool IsSupportedJavascriptMimeType(const std::string& mime_type) {
760 return g_mime_util.Get().IsSupportedJavascriptMimeType(mime_type);
763 bool IsSupportedMimeType(const std::string& mime_type) {
764 return g_mime_util.Get().IsSupportedMimeType(mime_type);
767 bool MatchesMimeType(const std::string& mime_type_pattern,
768 const std::string& mime_type) {
769 return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type);
772 bool IsMimeType(const std::string& type_string) {
773 return g_mime_util.Get().IsMimeType(type_string);
776 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) {
777 return g_mime_util.Get().AreSupportedMediaCodecs(codecs);
780 bool IsStrictMediaMimeType(const std::string& mime_type) {
781 return g_mime_util.Get().IsStrictMediaMimeType(mime_type);
784 bool IsSupportedStrictMediaMimeType(const std::string& mime_type,
785 const std::vector<std::string>& codecs) {
786 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs);
789 void ParseCodecString(const std::string& codecs,
790 std::vector<std::string>* codecs_out,
791 const bool strip) {
792 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip);
795 namespace {
797 // From http://www.w3schools.com/media/media_mimeref.asp and
798 // http://plugindoc.mozdev.org/winmime.php
799 static const char* const kStandardImageTypes[] = {
800 "image/bmp",
801 "image/cis-cod",
802 "image/gif",
803 "image/ief",
804 "image/jpeg",
805 "image/webp",
806 "image/pict",
807 "image/pipeg",
808 "image/png",
809 "image/svg+xml",
810 "image/tiff",
811 "image/vnd.microsoft.icon",
812 "image/x-cmu-raster",
813 "image/x-cmx",
814 "image/x-icon",
815 "image/x-portable-anymap",
816 "image/x-portable-bitmap",
817 "image/x-portable-graymap",
818 "image/x-portable-pixmap",
819 "image/x-rgb",
820 "image/x-xbitmap",
821 "image/x-xpixmap",
822 "image/x-xwindowdump"
824 static const char* const kStandardAudioTypes[] = {
825 "audio/aac",
826 "audio/aiff",
827 "audio/amr",
828 "audio/basic",
829 "audio/midi",
830 "audio/mp3",
831 "audio/mp4",
832 "audio/mpeg",
833 "audio/mpeg3",
834 "audio/ogg",
835 "audio/vorbis",
836 "audio/wav",
837 "audio/webm",
838 "audio/x-m4a",
839 "audio/x-ms-wma",
840 "audio/vnd.rn-realaudio",
841 "audio/vnd.wave"
843 static const char* const kStandardVideoTypes[] = {
844 "video/avi",
845 "video/divx",
846 "video/flc",
847 "video/mp4",
848 "video/mpeg",
849 "video/ogg",
850 "video/quicktime",
851 "video/sd-video",
852 "video/webm",
853 "video/x-dv",
854 "video/x-m4v",
855 "video/x-mpeg",
856 "video/x-ms-asf",
857 "video/x-ms-wmv"
860 struct StandardType {
861 const char* leading_mime_type;
862 const char* const* standard_types;
863 size_t standard_types_len;
865 static const StandardType kStandardTypes[] = {
866 { "image/", kStandardImageTypes, arraysize(kStandardImageTypes) },
867 { "audio/", kStandardAudioTypes, arraysize(kStandardAudioTypes) },
868 { "video/", kStandardVideoTypes, arraysize(kStandardVideoTypes) },
869 { NULL, NULL, 0 }
872 void GetExtensionsFromHardCodedMappings(
873 const MimeInfo* mappings,
874 size_t mappings_len,
875 const std::string& leading_mime_type,
876 base::hash_set<base::FilePath::StringType>* extensions) {
877 base::FilePath::StringType extension;
878 for (size_t i = 0; i < mappings_len; ++i) {
879 if (StartsWithASCII(mappings[i].mime_type, leading_mime_type, false)) {
880 std::vector<string> this_extensions;
881 base::SplitStringUsingSubstr(mappings[i].extensions, ",",
882 &this_extensions);
883 for (size_t j = 0; j < this_extensions.size(); ++j) {
884 #if defined(OS_WIN)
885 base::FilePath::StringType extension(
886 base::UTF8ToWide(this_extensions[j]));
887 #else
888 base::FilePath::StringType extension(this_extensions[j]);
889 #endif
890 extensions->insert(extension);
896 void GetExtensionsHelper(
897 const char* const* standard_types,
898 size_t standard_types_len,
899 const std::string& leading_mime_type,
900 base::hash_set<base::FilePath::StringType>* extensions) {
901 for (size_t i = 0; i < standard_types_len; ++i) {
902 g_mime_util.Get().GetPlatformExtensionsForMimeType(standard_types[i],
903 extensions);
906 // Also look up the extensions from hard-coded mappings in case that some
907 // supported extensions are not registered in the system registry, like ogg.
908 GetExtensionsFromHardCodedMappings(primary_mappings,
909 arraysize(primary_mappings),
910 leading_mime_type,
911 extensions);
913 GetExtensionsFromHardCodedMappings(secondary_mappings,
914 arraysize(secondary_mappings),
915 leading_mime_type,
916 extensions);
919 // Note that the elements in the source set will be appended to the target
920 // vector.
921 template<class T>
922 void HashSetToVector(base::hash_set<T>* source, std::vector<T>* target) {
923 size_t old_target_size = target->size();
924 target->resize(old_target_size + source->size());
925 size_t i = 0;
926 for (typename base::hash_set<T>::iterator iter = source->begin();
927 iter != source->end(); ++iter, ++i)
928 (*target)[old_target_size + i] = *iter;
932 void GetExtensionsForMimeType(
933 const std::string& unsafe_mime_type,
934 std::vector<base::FilePath::StringType>* extensions) {
935 if (unsafe_mime_type == "*/*" || unsafe_mime_type == "*")
936 return;
938 const std::string mime_type = StringToLowerASCII(unsafe_mime_type);
939 base::hash_set<base::FilePath::StringType> unique_extensions;
941 if (EndsWith(mime_type, "/*", true)) {
942 std::string leading_mime_type = mime_type.substr(0, mime_type.length() - 1);
944 // Find the matching StandardType from within kStandardTypes, or fall
945 // through to the last (default) StandardType.
946 const StandardType* type = NULL;
947 for (size_t i = 0; i < arraysize(kStandardTypes); ++i) {
948 type = &(kStandardTypes[i]);
949 if (type->leading_mime_type &&
950 leading_mime_type == type->leading_mime_type)
951 break;
953 DCHECK(type);
954 GetExtensionsHelper(type->standard_types,
955 type->standard_types_len,
956 leading_mime_type,
957 &unique_extensions);
958 } else {
959 g_mime_util.Get().GetPlatformExtensionsForMimeType(mime_type,
960 &unique_extensions);
962 // Also look up the extensions from hard-coded mappings in case that some
963 // supported extensions are not registered in the system registry, like ogg.
964 GetExtensionsFromHardCodedMappings(primary_mappings,
965 arraysize(primary_mappings),
966 mime_type,
967 &unique_extensions);
969 GetExtensionsFromHardCodedMappings(secondary_mappings,
970 arraysize(secondary_mappings),
971 mime_type,
972 &unique_extensions);
975 HashSetToVector(&unique_extensions, extensions);
978 void RemoveProprietaryMediaTypesAndCodecsForTests() {
979 g_mime_util.Get().RemoveProprietaryMediaTypesAndCodecsForTests();
982 const std::string GetIANAMediaType(const std::string& mime_type) {
983 for (size_t i = 0; i < arraysize(kIanaMediaTypes); ++i) {
984 if (StartsWithASCII(mime_type, kIanaMediaTypes[i].matcher, true)) {
985 return kIanaMediaTypes[i].name;
988 return std::string();
991 CertificateMimeType GetCertificateMimeTypeForMimeType(
992 const std::string& mime_type) {
993 // Don't create a map, there is only one entry in the table,
994 // except on Android.
995 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) {
996 if (mime_type == net::supported_certificate_types[i].mime_type)
997 return net::supported_certificate_types[i].cert_type;
999 return CERTIFICATE_MIME_TYPE_UNKNOWN;
1002 bool IsSupportedCertificateMimeType(const std::string& mime_type) {
1003 CertificateMimeType file_type =
1004 GetCertificateMimeTypeForMimeType(mime_type);
1005 return file_type != CERTIFICATE_MIME_TYPE_UNKNOWN;
1008 void AddMultipartValueForUpload(const std::string& value_name,
1009 const std::string& value,
1010 const std::string& mime_boundary,
1011 const std::string& content_type,
1012 std::string* post_data) {
1013 DCHECK(post_data);
1014 // First line is the boundary.
1015 post_data->append("--" + mime_boundary + "\r\n");
1016 // Next line is the Content-disposition.
1017 post_data->append("Content-Disposition: form-data; name=\"" +
1018 value_name + "\"\r\n");
1019 if (!content_type.empty()) {
1020 // If Content-type is specified, the next line is that.
1021 post_data->append("Content-Type: " + content_type + "\r\n");
1023 // Leave an empty line and append the value.
1024 post_data->append("\r\n" + value + "\r\n");
1027 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary,
1028 std::string* post_data) {
1029 DCHECK(post_data);
1030 post_data->append("--" + mime_boundary + "--\r\n");
1033 } // namespace net