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 != std::string::npos && |
| 546 wildcard_pos < supported_codecs[j].length(); |
| 547 wildcard_pos = supported_codecs[j].find('?', wildcard_pos + 1)) { |
| 548 // Don't enforce case sensitivity, even though it's called for, as it |
| 549 // would break some websites. |
| 550 if (wildcard_pos >= codecs[i].length() || |
| 551 !IsHexDigit(codecs[i].at(wildcard_pos))) { |
| 552 return false; |
| 553 } |
| 554 } |
| 555 codec_matched = true; |
| 556 break; |
| 557 } |
| 558 if (!codec_matched) |
| 559 return false; |
| 560 } |
| 561 return !codecs.empty(); |
| 562 } |
| 563 |
485 void MimeUtil::InitializeMimeTypeMaps() { | 564 void MimeUtil::InitializeMimeTypeMaps() { |
486 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 565 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
487 image_map_.insert(supported_image_types[i]); | 566 image_map_.insert(supported_image_types[i]); |
488 | 567 |
489 // Initialize the supported non-image types. | 568 // Initialize the supported non-image types. |
490 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 569 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
491 non_image_map_.insert(supported_non_image_types[i]); | 570 non_image_map_.insert(supported_non_image_types[i]); |
492 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) | 571 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) |
493 non_image_map_.insert(supported_certificate_types[i].mime_type); | 572 non_image_map_.insert(supported_certificate_types[i].mime_type); |
494 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) | 573 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; | 624 MimeMappings codecs; |
546 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | 625 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { |
547 #if defined(OS_ANDROID) | 626 #if defined(OS_ANDROID) |
548 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) | 627 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) |
549 continue; | 628 continue; |
550 #endif | 629 #endif |
551 codecs.insert(mime_type_codecs[j]); | 630 codecs.insert(mime_type_codecs[j]); |
552 } | 631 } |
553 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; | 632 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; |
554 } | 633 } |
| 634 for (size_t i = 0; i < arraysize(format_mp4_codec_mappings); ++i) { |
| 635 std::vector<std::string> mime_type_codecs; |
| 636 ParseCodecString( |
| 637 format_mp4_codec_mappings[i].codecs_list, &mime_type_codecs, false); |
| 638 |
| 639 MimeExpressionMappings codecs; |
| 640 for (size_t j = 0; j < mime_type_codecs.size(); ++j) |
| 641 codecs.push_back(mime_type_codecs[j]); |
| 642 strict_mp4_format_map_[format_mp4_codec_mappings[i].mime_type] = codecs; |
| 643 } |
555 } | 644 } |
556 | 645 |
557 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { | 646 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { |
558 return image_map_.find(mime_type) != image_map_.end(); | 647 return image_map_.find(mime_type) != image_map_.end(); |
559 } | 648 } |
560 | 649 |
561 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { | 650 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { |
562 return media_map_.find(mime_type) != media_map_.end(); | 651 return media_map_.find(mime_type) != media_map_.end(); |
563 } | 652 } |
564 | 653 |
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
725 for (std::vector<std::string>::iterator it = codecs_out->begin(); | 814 for (std::vector<std::string>::iterator it = codecs_out->begin(); |
726 it != codecs_out->end(); | 815 it != codecs_out->end(); |
727 ++it) { | 816 ++it) { |
728 size_t found = it->find_first_of('.'); | 817 size_t found = it->find_first_of('.'); |
729 if (found != std::string::npos) | 818 if (found != std::string::npos) |
730 it->resize(found); | 819 it->resize(found); |
731 } | 820 } |
732 } | 821 } |
733 | 822 |
734 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { | 823 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { |
735 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) | 824 if (strict_format_map_.find(mime_type) == strict_format_map_.end() && |
| 825 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end()) |
736 return false; | 826 return false; |
737 return true; | 827 return true; |
738 } | 828 } |
739 | 829 |
740 bool MimeUtil::IsSupportedStrictMediaMimeType( | 830 SupportsType MimeUtil::IsSupportedStrictMediaMimeType( |
741 const std::string& mime_type, | 831 const std::string& mime_type, |
742 const std::vector<std::string>& codecs) const { | 832 const std::vector<std::string>& codecs) const { |
743 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); | 833 StrictMappings::const_iterator it_strict_map = |
744 return (it != strict_format_map_.end()) && | 834 strict_format_map_.find(mime_type); |
745 AreSupportedCodecs(it->second, codecs); | 835 if ((it_strict_map != strict_format_map_.end()) && |
| 836 AreSupportedCodecs(it_strict_map->second, codecs)) { |
| 837 return IsSupported; |
| 838 } |
| 839 |
| 840 StrictExpressionMappings::const_iterator it_expression_map = |
| 841 strict_mp4_format_map_.find(mime_type); |
| 842 if ((it_expression_map != strict_mp4_format_map_.end()) && |
| 843 AreSupportedCodecsWithProfile(it_expression_map->second, codecs)) { |
| 844 return MayBeSupported; |
| 845 } |
| 846 |
| 847 if (codecs.empty()) |
| 848 return MayBeSupported; |
| 849 |
| 850 return IsNotSupported; |
746 } | 851 } |
747 | 852 |
748 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { | 853 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { |
749 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { | 854 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { |
750 non_image_map_.erase(proprietary_media_types[i]); | 855 non_image_map_.erase(proprietary_media_types[i]); |
751 media_map_.erase(proprietary_media_types[i]); | 856 media_map_.erase(proprietary_media_types[i]); |
752 } | 857 } |
753 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) | 858 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) |
754 codecs_map_.erase(proprietary_media_codecs[i]); | 859 codecs_map_.erase(proprietary_media_codecs[i]); |
755 } | 860 } |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 } | 925 } |
821 | 926 |
822 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 927 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
823 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); | 928 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
824 } | 929 } |
825 | 930 |
826 bool IsStrictMediaMimeType(const std::string& mime_type) { | 931 bool IsStrictMediaMimeType(const std::string& mime_type) { |
827 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | 932 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
828 } | 933 } |
829 | 934 |
830 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, | 935 SupportsType IsSupportedStrictMediaMimeType( |
831 const std::vector<std::string>& codecs) { | 936 const std::string& mime_type, |
| 937 const std::vector<std::string>& codecs) { |
832 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); | 938 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); |
833 } | 939 } |
834 | 940 |
835 void ParseCodecString(const std::string& codecs, | 941 void ParseCodecString(const std::string& codecs, |
836 std::vector<std::string>* codecs_out, | 942 std::vector<std::string>* codecs_out, |
837 const bool strip) { | 943 const bool strip) { |
838 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); | 944 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
839 } | 945 } |
840 | 946 |
841 namespace { | 947 namespace { |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1069 post_data->append("\r\n" + value + "\r\n"); | 1175 post_data->append("\r\n" + value + "\r\n"); |
1070 } | 1176 } |
1071 | 1177 |
1072 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1178 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1073 std::string* post_data) { | 1179 std::string* post_data) { |
1074 DCHECK(post_data); | 1180 DCHECK(post_data); |
1075 post_data->append("--" + mime_boundary + "--\r\n"); | 1181 post_data->append("--" + mime_boundary + "--\r\n"); |
1076 } | 1182 } |
1077 | 1183 |
1078 } // namespace net | 1184 } // namespace net |
OLD | NEW |