OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <algorithm> | 5 #include <algorithm> |
6 #include <iterator> | 6 #include <iterator> |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
76 | 76 |
77 bool IsValidTopLevelMimeType(const std::string& type_string) const; | 77 bool IsValidTopLevelMimeType(const std::string& type_string) const; |
78 | 78 |
79 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; | 79 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; |
80 | 80 |
81 void ParseCodecString(const std::string& codecs, | 81 void ParseCodecString(const std::string& codecs, |
82 std::vector<std::string>* codecs_out, | 82 std::vector<std::string>* codecs_out, |
83 bool strip); | 83 bool strip); |
84 | 84 |
85 bool IsStrictMediaMimeType(const std::string& mime_type) const; | 85 bool IsStrictMediaMimeType(const std::string& mime_type) const; |
86 bool IsSupportedStrictMediaMimeType( | 86 SupportsType IsSupportedStrictMediaMimeType( |
87 const std::string& mime_type, | 87 const std::string& mime_type, |
88 const std::vector<std::string>& codecs) const; | 88 const std::vector<std::string>& codecs) const; |
89 | 89 |
90 void RemoveProprietaryMediaTypesAndCodecsForTests(); | 90 void RemoveProprietaryMediaTypesAndCodecsForTests(); |
91 | 91 |
92 private: | 92 private: |
93 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; | 93 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; |
94 | 94 |
95 typedef base::hash_set<std::string> MimeMappings; | 95 typedef base::hash_set<std::string> MimeMappings; |
96 typedef std::map<std::string, MimeMappings> StrictMappings; | 96 typedef std::map<std::string, MimeMappings> StrictMappings; |
97 | 97 |
98 typedef std::vector<std::string> MimeExpressionMappings; | |
99 typedef std::map<std::string, MimeExpressionMappings> | |
100 StrictExpressionMappings; | |
101 | |
98 MimeUtil(); | 102 MimeUtil(); |
99 | 103 |
100 // Returns true if |codecs| is nonempty and all the items in it are present in | 104 // Returns true if |codecs| is nonempty and all the items in it are present in |
101 // |supported_codecs|. | 105 // |supported_codecs|. |
102 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, | 106 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, |
103 const std::vector<std::string>& codecs); | 107 const std::vector<std::string>& codecs); |
108 // Returns true is |codecs| is nonempty and all the items in it match with the | |
109 // codecs expression in |supported_codecs|. | |
110 static bool AreSupportedCodecsWithProfile( | |
111 const MimeExpressionMappings& supported_codecs, | |
112 const std::vector<std::string>& codecs); | |
104 | 113 |
105 // For faster lookup, keep hash sets. | 114 // For faster lookup, keep hash sets. |
106 void InitializeMimeTypeMaps(); | 115 void InitializeMimeTypeMaps(); |
107 | 116 |
108 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, | 117 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, |
109 bool include_platform_types, | 118 bool include_platform_types, |
110 std::string* mime_type) const; | 119 std::string* mime_type) const; |
111 | 120 |
112 MimeMappings image_map_; | 121 MimeMappings image_map_; |
113 MimeMappings media_map_; | 122 MimeMappings media_map_; |
114 MimeMappings non_image_map_; | 123 MimeMappings non_image_map_; |
115 MimeMappings unsupported_text_map_; | 124 MimeMappings unsupported_text_map_; |
116 MimeMappings javascript_map_; | 125 MimeMappings javascript_map_; |
117 MimeMappings codecs_map_; | 126 MimeMappings codecs_map_; |
118 | 127 |
128 // A map of mime_types and hash map of the supported codecs for the mime_type. | |
119 StrictMappings strict_format_map_; | 129 StrictMappings strict_format_map_; |
130 // A map of MP4 mime_types which expect codecs with profile parameter and | |
131 // vector of supported codecs expressions for the mime_type. | |
132 StrictExpressionMappings strict_mp4_format_map_; | |
120 }; // class MimeUtil | 133 }; // class MimeUtil |
121 | 134 |
122 // This variable is Leaky because we need to access it from WorkerPool threads. | 135 // This variable is Leaky because we need to access it from WorkerPool threads. |
123 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | 136 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
124 LAZY_INSTANCE_INITIALIZER; | 137 LAZY_INSTANCE_INITIALIZER; |
125 | 138 |
126 struct MimeInfo { | 139 struct MimeInfo { |
127 const char* mime_type; | 140 const char* mime_type; |
128 const char* extensions; // comma separated list | 141 const char* extensions; // comma separated list |
129 }; | 142 }; |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 { "audio/wav", "1" }, | 471 { "audio/wav", "1" }, |
459 { "audio/x-wav", "1" }, | 472 { "audio/x-wav", "1" }, |
460 { "video/ogg", "opus,theora,vorbis" }, | 473 { "video/ogg", "opus,theora,vorbis" }, |
461 { "audio/ogg", "opus,vorbis" }, | 474 { "audio/ogg", "opus,vorbis" }, |
462 { "application/ogg", "opus,theora,vorbis" }, | 475 { "application/ogg", "opus,theora,vorbis" }, |
463 { "audio/mpeg", "" }, | 476 { "audio/mpeg", "" }, |
464 { "audio/mp3", "" }, | 477 { "audio/mp3", "" }, |
465 { "audio/x-mp3", "" } | 478 { "audio/x-mp3", "" } |
466 }; | 479 }; |
467 | 480 |
481 // Following is the list of RFC 6381 compliant codecs: | |
482 // mp4a.6B - MPEG-1 audio | |
483 // mp4a.69 - MPEG-2 extension to MPEG-1 | |
484 // mp4a.67 - MPEG-2 AAC | |
485 // mp4a.40.2 - MPEG-4 AAC | |
486 // mp4a.40.5 - MPEG-4 HE-AAC | |
487 // | |
488 // avc1.42E0xx - H.264 Baseline | |
489 // avc1.4D40xx - H.264 Main | |
490 // avc1.6400xx - H.264 High | |
491 // | |
492 // Additionally, several non-RFC compliant codecs are allowed, due to their | |
493 // existing use on web. | |
494 // mp4a.40 | |
495 // avc1.xxxxxx | |
496 // avc3.xxxxxx | |
497 // mp4a.6x | |
498 static const char kProprietaryAudioCodecsExpression[] = | |
499 "mp4a.6?,mp4a.40,mp4a.40.?"; | |
500 static const char kProprietaryCodecsExpression[] = | |
501 "avc1,avc3,avc1.??????,avc3.??????,mp4a.6?,mp4a.40,mp4a.40.?"; | |
502 | |
503 static const MediaFormatStrict format_mp4_codec_mappings[] = { | |
504 { "audio/mp4", kProprietaryAudioCodecsExpression }, | |
505 { "audio/x-m4a", kProprietaryAudioCodecsExpression }, | |
506 { "video/mp4", kProprietaryCodecsExpression }, | |
507 { "video/x-m4v", kProprietaryCodecsExpression }, | |
508 { "application/x-mpegurl", kProprietaryCodecsExpression }, | |
509 { "application/vnd.apple.mpegurl", kProprietaryCodecsExpression } | |
510 }; | |
511 | |
468 MimeUtil::MimeUtil() { | 512 MimeUtil::MimeUtil() { |
469 InitializeMimeTypeMaps(); | 513 InitializeMimeTypeMaps(); |
470 } | 514 } |
471 | 515 |
472 // static | 516 // static |
473 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, | 517 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, |
474 const std::vector<std::string>& codecs) { | 518 const std::vector<std::string>& codecs) { |
475 if (supported_codecs.empty()) | 519 if (supported_codecs.empty()) |
476 return codecs.empty(); | 520 return codecs.empty(); |
477 | 521 |
478 for (size_t i = 0; i < codecs.size(); ++i) { | 522 for (size_t i = 0; i < codecs.size(); ++i) { |
479 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) | 523 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) |
480 return false; | 524 return false; |
481 } | 525 } |
482 return !codecs.empty(); | 526 return !codecs.empty(); |
483 } | 527 } |
484 | 528 |
529 // Checks all the codecs present in the |codecs| against the entries in | |
530 // |supported_codecs|. Returns true only if |codecs| is non-empty and all the | |
531 // codecs match |supported_codecs| expressions. | |
532 bool MimeUtil::AreSupportedCodecsWithProfile( | |
533 const MimeExpressionMappings& supported_codecs, | |
534 const std::vector<std::string>& codecs) { | |
535 DCHECK(!supported_codecs.empty()); | |
536 for (size_t i = 0; i < codecs.size(); ++i) { | |
537 bool codec_matched = false; | |
538 for (size_t j = 0; j < supported_codecs.size(); ++j) { | |
539 if (!MatchPattern(base::StringPiece(codecs[i]), | |
540 base::StringPiece(supported_codecs[j]))) { | |
541 continue; | |
542 } | |
543 // If suffix exists, check whether it is hexadecimal. | |
544 for (size_t wildcard_pos = supported_codecs[j].find('?'); | |
545 wildcard_pos < supported_codecs[j].length(); | |
Ryan Sleevi
2014/06/13 22:58:04
BUG: if wildcard_pos is not found, it equals std::
| |
546 wildcard_pos = supported_codecs[j].find('?', wildcard_pos + 1)) { | |
547 // Don't enforce case sensitivity, even though it's called for, as it | |
548 // would break some websites. | |
549 if (wildcard_pos >= codecs[i].length() || | |
550 !IsHexDigit(codecs[i].at(wildcard_pos))) { | |
551 return false; | |
552 } | |
553 } | |
554 codec_matched = true; | |
555 break; | |
556 } | |
557 if (!codec_matched) | |
558 return false; | |
559 } | |
560 return !codecs.empty(); | |
561 } | |
562 | |
485 void MimeUtil::InitializeMimeTypeMaps() { | 563 void MimeUtil::InitializeMimeTypeMaps() { |
486 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 564 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
487 image_map_.insert(supported_image_types[i]); | 565 image_map_.insert(supported_image_types[i]); |
488 | 566 |
489 // Initialize the supported non-image types. | 567 // Initialize the supported non-image types. |
490 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 568 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
491 non_image_map_.insert(supported_non_image_types[i]); | 569 non_image_map_.insert(supported_non_image_types[i]); |
492 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) | 570 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) |
493 non_image_map_.insert(supported_certificate_types[i].mime_type); | 571 non_image_map_.insert(supported_certificate_types[i].mime_type); |
494 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) | 572 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
545 MimeMappings codecs; | 623 MimeMappings codecs; |
546 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | 624 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { |
547 #if defined(OS_ANDROID) | 625 #if defined(OS_ANDROID) |
548 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) | 626 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) |
549 continue; | 627 continue; |
550 #endif | 628 #endif |
551 codecs.insert(mime_type_codecs[j]); | 629 codecs.insert(mime_type_codecs[j]); |
552 } | 630 } |
553 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; | 631 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; |
554 } | 632 } |
633 for (size_t i = 0; i < arraysize(format_mp4_codec_mappings); ++i) { | |
634 std::vector<std::string> mime_type_codecs; | |
635 ParseCodecString( | |
636 format_mp4_codec_mappings[i].codecs_list, &mime_type_codecs, false); | |
637 | |
638 MimeExpressionMappings codecs; | |
639 for (size_t j = 0; j < mime_type_codecs.size(); ++j) | |
640 codecs.push_back(mime_type_codecs[j]); | |
641 strict_mp4_format_map_[format_mp4_codec_mappings[i].mime_type] = codecs; | |
642 } | |
555 } | 643 } |
556 | 644 |
557 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { | 645 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { |
558 return image_map_.find(mime_type) != image_map_.end(); | 646 return image_map_.find(mime_type) != image_map_.end(); |
559 } | 647 } |
560 | 648 |
561 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { | 649 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { |
562 return media_map_.find(mime_type) != media_map_.end(); | 650 return media_map_.find(mime_type) != media_map_.end(); |
563 } | 651 } |
564 | 652 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
725 for (std::vector<std::string>::iterator it = codecs_out->begin(); | 813 for (std::vector<std::string>::iterator it = codecs_out->begin(); |
726 it != codecs_out->end(); | 814 it != codecs_out->end(); |
727 ++it) { | 815 ++it) { |
728 size_t found = it->find_first_of('.'); | 816 size_t found = it->find_first_of('.'); |
729 if (found != std::string::npos) | 817 if (found != std::string::npos) |
730 it->resize(found); | 818 it->resize(found); |
731 } | 819 } |
732 } | 820 } |
733 | 821 |
734 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { | 822 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { |
735 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) | 823 if (strict_format_map_.find(mime_type) == strict_format_map_.end() && |
824 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end()) | |
736 return false; | 825 return false; |
737 return true; | 826 return true; |
738 } | 827 } |
739 | 828 |
740 bool MimeUtil::IsSupportedStrictMediaMimeType( | 829 SupportsType MimeUtil::IsSupportedStrictMediaMimeType( |
741 const std::string& mime_type, | 830 const std::string& mime_type, |
742 const std::vector<std::string>& codecs) const { | 831 const std::vector<std::string>& codecs) const { |
743 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); | 832 StrictMappings::const_iterator it_strict_map = |
744 return (it != strict_format_map_.end()) && | 833 strict_format_map_.find(mime_type); |
745 AreSupportedCodecs(it->second, codecs); | 834 if ((it_strict_map != strict_format_map_.end()) && |
835 AreSupportedCodecs(it_strict_map->second, codecs)) { | |
836 return IsSupported; | |
837 } | |
838 | |
839 StrictExpressionMappings::const_iterator it_expression_map = | |
840 strict_mp4_format_map_.find(mime_type); | |
841 if ((it_expression_map != strict_mp4_format_map_.end()) && | |
842 AreSupportedCodecsWithProfile(it_expression_map->second, codecs)) { | |
843 return MayBeSupported; | |
844 } | |
845 | |
846 if (codecs.empty()) | |
847 return MayBeSupported; | |
848 | |
849 return IsNotSupported; | |
746 } | 850 } |
747 | 851 |
748 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { | 852 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { |
749 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { | 853 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { |
750 non_image_map_.erase(proprietary_media_types[i]); | 854 non_image_map_.erase(proprietary_media_types[i]); |
751 media_map_.erase(proprietary_media_types[i]); | 855 media_map_.erase(proprietary_media_types[i]); |
752 } | 856 } |
753 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) | 857 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) |
754 codecs_map_.erase(proprietary_media_codecs[i]); | 858 codecs_map_.erase(proprietary_media_codecs[i]); |
755 } | 859 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
820 } | 924 } |
821 | 925 |
822 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 926 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
823 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); | 927 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
824 } | 928 } |
825 | 929 |
826 bool IsStrictMediaMimeType(const std::string& mime_type) { | 930 bool IsStrictMediaMimeType(const std::string& mime_type) { |
827 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | 931 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
828 } | 932 } |
829 | 933 |
830 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, | 934 SupportsType IsSupportedStrictMediaMimeType( |
831 const std::vector<std::string>& codecs) { | 935 const std::string& mime_type, |
936 const std::vector<std::string>& codecs) { | |
832 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); | 937 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); |
833 } | 938 } |
834 | 939 |
835 void ParseCodecString(const std::string& codecs, | 940 void ParseCodecString(const std::string& codecs, |
836 std::vector<std::string>* codecs_out, | 941 std::vector<std::string>* codecs_out, |
837 const bool strip) { | 942 const bool strip) { |
838 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); | 943 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
839 } | 944 } |
840 | 945 |
841 namespace { | 946 namespace { |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1069 post_data->append("\r\n" + value + "\r\n"); | 1174 post_data->append("\r\n" + value + "\r\n"); |
1070 } | 1175 } |
1071 | 1176 |
1072 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1177 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1073 std::string* post_data) { | 1178 std::string* post_data) { |
1074 DCHECK(post_data); | 1179 DCHECK(post_data); |
1075 post_data->append("--" + mime_boundary + "--\r\n"); | 1180 post_data->append("--" + mime_boundary + "--\r\n"); |
1076 } | 1181 } |
1077 | 1182 |
1078 } // namespace net | 1183 } // namespace net |
OLD | NEW |