| 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 SupportsType IsSupportedStrictMediaMimeType( | 86 bool 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 | |
| 102 MimeUtil(); | 98 MimeUtil(); |
| 103 | 99 |
| 104 // Returns true if |codecs| is nonempty and all the items in it are present in | 100 // Returns true if |codecs| is nonempty and all the items in it are present in |
| 105 // |supported_codecs|. | 101 // |supported_codecs|. |
| 106 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, | 102 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, |
| 107 const std::vector<std::string>& codecs); | 103 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); | |
| 113 | 104 |
| 114 // For faster lookup, keep hash sets. | 105 // For faster lookup, keep hash sets. |
| 115 void InitializeMimeTypeMaps(); | 106 void InitializeMimeTypeMaps(); |
| 116 | 107 |
| 117 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, | 108 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, |
| 118 bool include_platform_types, | 109 bool include_platform_types, |
| 119 std::string* mime_type) const; | 110 std::string* mime_type) const; |
| 120 | 111 |
| 121 MimeMappings image_map_; | 112 MimeMappings image_map_; |
| 122 MimeMappings media_map_; | 113 MimeMappings media_map_; |
| 123 MimeMappings non_image_map_; | 114 MimeMappings non_image_map_; |
| 124 MimeMappings unsupported_text_map_; | 115 MimeMappings unsupported_text_map_; |
| 125 MimeMappings javascript_map_; | 116 MimeMappings javascript_map_; |
| 126 MimeMappings codecs_map_; | 117 MimeMappings codecs_map_; |
| 127 | 118 |
| 128 // A map of mime_types and hash map of the supported codecs for the mime_type. | |
| 129 StrictMappings strict_format_map_; | 119 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_; | |
| 133 }; // class MimeUtil | 120 }; // class MimeUtil |
| 134 | 121 |
| 135 // This variable is Leaky because we need to access it from WorkerPool threads. | 122 // This variable is Leaky because we need to access it from WorkerPool threads. |
| 136 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | 123 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
| 137 LAZY_INSTANCE_INITIALIZER; | 124 LAZY_INSTANCE_INITIALIZER; |
| 138 | 125 |
| 139 struct MimeInfo { | 126 struct MimeInfo { |
| 140 const char* mime_type; | 127 const char* mime_type; |
| 141 const char* extensions; // comma separated list | 128 const char* extensions; // comma separated list |
| 142 }; | 129 }; |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 { "audio/wav", "1" }, | 458 { "audio/wav", "1" }, |
| 472 { "audio/x-wav", "1" }, | 459 { "audio/x-wav", "1" }, |
| 473 { "video/ogg", "opus,theora,vorbis" }, | 460 { "video/ogg", "opus,theora,vorbis" }, |
| 474 { "audio/ogg", "opus,vorbis" }, | 461 { "audio/ogg", "opus,vorbis" }, |
| 475 { "application/ogg", "opus,theora,vorbis" }, | 462 { "application/ogg", "opus,theora,vorbis" }, |
| 476 { "audio/mpeg", "" }, | 463 { "audio/mpeg", "" }, |
| 477 { "audio/mp3", "" }, | 464 { "audio/mp3", "" }, |
| 478 { "audio/x-mp3", "" } | 465 { "audio/x-mp3", "" } |
| 479 }; | 466 }; |
| 480 | 467 |
| 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 | |
| 512 MimeUtil::MimeUtil() { | 468 MimeUtil::MimeUtil() { |
| 513 InitializeMimeTypeMaps(); | 469 InitializeMimeTypeMaps(); |
| 514 } | 470 } |
| 515 | 471 |
| 516 // static | 472 // static |
| 517 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, | 473 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, |
| 518 const std::vector<std::string>& codecs) { | 474 const std::vector<std::string>& codecs) { |
| 519 if (supported_codecs.empty()) | 475 if (supported_codecs.empty()) |
| 520 return codecs.empty(); | 476 return codecs.empty(); |
| 521 | 477 |
| 522 for (size_t i = 0; i < codecs.size(); ++i) { | 478 for (size_t i = 0; i < codecs.size(); ++i) { |
| 523 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) | 479 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) |
| 524 return false; | 480 return false; |
| 525 } | 481 } |
| 526 return !codecs.empty(); | 482 return !codecs.empty(); |
| 527 } | 483 } |
| 528 | 484 |
| 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 | |
| 564 void MimeUtil::InitializeMimeTypeMaps() { | 485 void MimeUtil::InitializeMimeTypeMaps() { |
| 565 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 486 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
| 566 image_map_.insert(supported_image_types[i]); | 487 image_map_.insert(supported_image_types[i]); |
| 567 | 488 |
| 568 // Initialize the supported non-image types. | 489 // Initialize the supported non-image types. |
| 569 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 490 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
| 570 non_image_map_.insert(supported_non_image_types[i]); | 491 non_image_map_.insert(supported_non_image_types[i]); |
| 571 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) | 492 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) |
| 572 non_image_map_.insert(supported_certificate_types[i].mime_type); | 493 non_image_map_.insert(supported_certificate_types[i].mime_type); |
| 573 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) | 494 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 624 MimeMappings codecs; | 545 MimeMappings codecs; |
| 625 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | 546 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { |
| 626 #if defined(OS_ANDROID) | 547 #if defined(OS_ANDROID) |
| 627 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) | 548 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) |
| 628 continue; | 549 continue; |
| 629 #endif | 550 #endif |
| 630 codecs.insert(mime_type_codecs[j]); | 551 codecs.insert(mime_type_codecs[j]); |
| 631 } | 552 } |
| 632 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; | 553 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; |
| 633 } | 554 } |
| 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 } | |
| 644 } | 555 } |
| 645 | 556 |
| 646 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { | 557 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { |
| 647 return image_map_.find(mime_type) != image_map_.end(); | 558 return image_map_.find(mime_type) != image_map_.end(); |
| 648 } | 559 } |
| 649 | 560 |
| 650 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { | 561 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { |
| 651 return media_map_.find(mime_type) != media_map_.end(); | 562 return media_map_.find(mime_type) != media_map_.end(); |
| 652 } | 563 } |
| 653 | 564 |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 814 for (std::vector<std::string>::iterator it = codecs_out->begin(); | 725 for (std::vector<std::string>::iterator it = codecs_out->begin(); |
| 815 it != codecs_out->end(); | 726 it != codecs_out->end(); |
| 816 ++it) { | 727 ++it) { |
| 817 size_t found = it->find_first_of('.'); | 728 size_t found = it->find_first_of('.'); |
| 818 if (found != std::string::npos) | 729 if (found != std::string::npos) |
| 819 it->resize(found); | 730 it->resize(found); |
| 820 } | 731 } |
| 821 } | 732 } |
| 822 | 733 |
| 823 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { | 734 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { |
| 824 if (strict_format_map_.find(mime_type) == strict_format_map_.end() && | 735 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) |
| 825 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end()) | |
| 826 return false; | 736 return false; |
| 827 return true; | 737 return true; |
| 828 } | 738 } |
| 829 | 739 |
| 830 SupportsType MimeUtil::IsSupportedStrictMediaMimeType( | 740 bool MimeUtil::IsSupportedStrictMediaMimeType( |
| 831 const std::string& mime_type, | 741 const std::string& mime_type, |
| 832 const std::vector<std::string>& codecs) const { | 742 const std::vector<std::string>& codecs) const { |
| 833 StrictMappings::const_iterator it_strict_map = | 743 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); |
| 834 strict_format_map_.find(mime_type); | 744 return (it != strict_format_map_.end()) && |
| 835 if ((it_strict_map != strict_format_map_.end()) && | 745 AreSupportedCodecs(it->second, codecs); |
| 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; | |
| 851 } | 746 } |
| 852 | 747 |
| 853 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { | 748 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { |
| 854 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { | 749 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { |
| 855 non_image_map_.erase(proprietary_media_types[i]); | 750 non_image_map_.erase(proprietary_media_types[i]); |
| 856 media_map_.erase(proprietary_media_types[i]); | 751 media_map_.erase(proprietary_media_types[i]); |
| 857 } | 752 } |
| 858 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) | 753 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) |
| 859 codecs_map_.erase(proprietary_media_codecs[i]); | 754 codecs_map_.erase(proprietary_media_codecs[i]); |
| 860 } | 755 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 } | 820 } |
| 926 | 821 |
| 927 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 822 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
| 928 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); | 823 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
| 929 } | 824 } |
| 930 | 825 |
| 931 bool IsStrictMediaMimeType(const std::string& mime_type) { | 826 bool IsStrictMediaMimeType(const std::string& mime_type) { |
| 932 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | 827 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
| 933 } | 828 } |
| 934 | 829 |
| 935 SupportsType IsSupportedStrictMediaMimeType( | 830 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, |
| 936 const std::string& mime_type, | 831 const std::vector<std::string>& codecs) { |
| 937 const std::vector<std::string>& codecs) { | |
| 938 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); | 832 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); |
| 939 } | 833 } |
| 940 | 834 |
| 941 void ParseCodecString(const std::string& codecs, | 835 void ParseCodecString(const std::string& codecs, |
| 942 std::vector<std::string>* codecs_out, | 836 std::vector<std::string>* codecs_out, |
| 943 const bool strip) { | 837 const bool strip) { |
| 944 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); | 838 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
| 945 } | 839 } |
| 946 | 840 |
| 947 namespace { | 841 namespace { |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1175 post_data->append("\r\n" + value + "\r\n"); | 1069 post_data->append("\r\n" + value + "\r\n"); |
| 1176 } | 1070 } |
| 1177 | 1071 |
| 1178 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1072 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
| 1179 std::string* post_data) { | 1073 std::string* post_data) { |
| 1180 DCHECK(post_data); | 1074 DCHECK(post_data); |
| 1181 post_data->append("--" + mime_boundary + "--\r\n"); | 1075 post_data->append("--" + mime_boundary + "--\r\n"); |
| 1182 } | 1076 } |
| 1183 | 1077 |
| 1184 } // namespace net | 1078 } // namespace net |
| OLD | NEW |