Chromium Code Reviews| 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 | 71 |
| 72 bool IsMimeType(const std::string& type_string) const; | 72 bool IsMimeType(const std::string& type_string) const; |
| 73 | 73 |
| 74 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; | 74 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; |
| 75 | 75 |
| 76 void ParseCodecString(const std::string& codecs, | 76 void ParseCodecString(const std::string& codecs, |
| 77 std::vector<std::string>* codecs_out, | 77 std::vector<std::string>* codecs_out, |
| 78 bool strip); | 78 bool strip); |
| 79 | 79 |
| 80 bool IsStrictMediaMimeType(const std::string& mime_type) const; | 80 bool IsStrictMediaMimeType(const std::string& mime_type) const; |
| 81 bool IsSupportedStrictMediaMimeType( | 81 CanPlayType IsSupportedStrictMediaMimeType( |
| 82 const std::string& mime_type, | 82 const std::string& mime_type, |
| 83 const std::vector<std::string>& codecs) const; | 83 const std::vector<std::string>& codecs) const; |
| 84 | 84 |
| 85 void RemoveProprietaryMediaTypesAndCodecsForTests(); | 85 void RemoveProprietaryMediaTypesAndCodecsForTests(); |
| 86 | 86 |
| 87 private: | 87 private: |
| 88 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; | 88 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; |
| 89 | 89 |
| 90 typedef base::hash_set<std::string> MimeMappings; | 90 typedef base::hash_set<std::string> MimeMappings; |
| 91 typedef std::map<std::string, MimeMappings> StrictMappings; | 91 typedef std::map<std::string, MimeMappings> StrictMappings; |
| 92 | 92 |
| 93 typedef std::vector<std::string> MimeExpressionMappings; | |
| 94 typedef std::map<std::string, MimeExpressionMappings> | |
| 95 StrictExpressionMappings; | |
| 96 | |
| 93 MimeUtil(); | 97 MimeUtil(); |
| 94 | 98 |
| 95 // Returns true if |codecs| is nonempty and all the items in it are present in | 99 // Returns true if |codecs| is nonempty and all the items in it are present in |
| 96 // |supported_codecs|. | 100 // |supported_codecs|. |
| 97 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, | 101 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, |
| 98 const std::vector<std::string>& codecs); | 102 const std::vector<std::string>& codecs); |
| 103 static bool AreSupportedCodecsWithProfile( | |
| 104 const MimeExpressionMappings& supported_codecs, | |
| 105 const std::vector<std::string>& codecs); | |
| 99 | 106 |
| 100 // For faster lookup, keep hash sets. | 107 // For faster lookup, keep hash sets. |
| 101 void InitializeMimeTypeMaps(); | 108 void InitializeMimeTypeMaps(); |
| 102 | 109 |
| 103 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, | 110 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, |
| 104 bool include_platform_types, | 111 bool include_platform_types, |
| 105 std::string* mime_type) const; | 112 std::string* mime_type) const; |
| 106 | 113 |
| 107 MimeMappings image_map_; | 114 MimeMappings image_map_; |
| 108 MimeMappings media_map_; | 115 MimeMappings media_map_; |
| 109 MimeMappings non_image_map_; | 116 MimeMappings non_image_map_; |
| 110 MimeMappings unsupported_text_map_; | 117 MimeMappings unsupported_text_map_; |
| 111 MimeMappings javascript_map_; | 118 MimeMappings javascript_map_; |
| 112 MimeMappings codecs_map_; | 119 MimeMappings codecs_map_; |
| 113 | 120 |
| 114 StrictMappings strict_format_map_; | 121 StrictMappings strict_format_map_; |
| 122 StrictExpressionMappings strict_mp4_format_map_; | |
| 115 }; // class MimeUtil | 123 }; // class MimeUtil |
| 116 | 124 |
| 117 // This variable is Leaky because we need to access it from WorkerPool threads. | 125 // This variable is Leaky because we need to access it from WorkerPool threads. |
| 118 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | 126 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
| 119 LAZY_INSTANCE_INITIALIZER; | 127 LAZY_INSTANCE_INITIALIZER; |
| 120 | 128 |
| 121 struct MimeInfo { | 129 struct MimeInfo { |
| 122 const char* mime_type; | 130 const char* mime_type; |
| 123 const char* extensions; // comma separated list | 131 const char* extensions; // comma separated list |
| 124 }; | 132 }; |
| (...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 453 { "audio/wav", "1" }, | 461 { "audio/wav", "1" }, |
| 454 { "audio/x-wav", "1" }, | 462 { "audio/x-wav", "1" }, |
| 455 { "video/ogg", "opus,theora,vorbis" }, | 463 { "video/ogg", "opus,theora,vorbis" }, |
| 456 { "audio/ogg", "opus,vorbis" }, | 464 { "audio/ogg", "opus,vorbis" }, |
| 457 { "application/ogg", "opus,theora,vorbis" }, | 465 { "application/ogg", "opus,theora,vorbis" }, |
| 458 { "audio/mpeg", "" }, | 466 { "audio/mpeg", "" }, |
| 459 { "audio/mp3", "" }, | 467 { "audio/mp3", "" }, |
| 460 { "audio/x-mp3", "" } | 468 { "audio/x-mp3", "" } |
| 461 }; | 469 }; |
| 462 | 470 |
| 471 static const char* kProprietaryAudioCodecsExpression = | |
| 472 "mp4a.40,mp4a.67,mp4a.40.?,mp4a.67.?"; | |
|
acolwell GONE FROM CHROMIUM
2014/06/02 21:22:42
nit: While mp4a.67.? is allowed by the RFC, it is
amogh.bihani
2014/06/03 13:26:12
Done. And added commets regarding what are the RFC
| |
| 473 static const char* kProprietaryCodecsExpression = | |
| 474 "avc1,avc3,avc1.??????,avc3.??????,mp4a.40,mp4a.67,mp4a.40.?,mp4a.67.?"; | |
| 475 | |
| 476 static const MediaFormatStrict format_mp4_codec_mappings[] = { | |
| 477 { "audio/mp4", kProprietaryAudioCodecsExpression }, | |
| 478 { "audio/x-m4a", kProprietaryAudioCodecsExpression }, | |
| 479 { "video/mp4", kProprietaryCodecsExpression }, | |
| 480 { "video/x-m4v", kProprietaryCodecsExpression }, | |
| 481 { "application/x-mpegurl", kProprietaryCodecsExpression }, | |
| 482 { "application/vnd.apple.mpegurl", kProprietaryCodecsExpression } | |
| 483 }; | |
| 484 | |
| 463 MimeUtil::MimeUtil() { | 485 MimeUtil::MimeUtil() { |
| 464 InitializeMimeTypeMaps(); | 486 InitializeMimeTypeMaps(); |
| 465 } | 487 } |
| 466 | 488 |
| 467 // static | 489 // static |
| 468 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, | 490 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, |
| 469 const std::vector<std::string>& codecs) { | 491 const std::vector<std::string>& codecs) { |
| 470 if (supported_codecs.empty()) | 492 if (supported_codecs.empty()) |
| 471 return codecs.empty(); | 493 return codecs.empty(); |
| 472 | 494 |
| 473 for (size_t i = 0; i < codecs.size(); ++i) { | 495 for (size_t i = 0; i < codecs.size(); ++i) { |
| 474 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) | 496 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) |
| 475 return false; | 497 return false; |
| 476 } | 498 } |
| 477 return !codecs.empty(); | 499 return !codecs.empty(); |
| 478 } | 500 } |
| 479 | 501 |
| 502 bool MimeUtil::AreSupportedCodecsWithProfile( | |
| 503 const MimeExpressionMappings& supported_codecs, | |
| 504 const std::vector<std::string>& codecs) { | |
| 505 DCHECK(!supported_codecs.empty()); | |
| 506 for (size_t i = 0; i < codecs.size(); ++i) { | |
| 507 bool codec_matched = false; | |
| 508 for (size_t j = 0; j < supported_codecs.size(); ++j) { | |
| 509 if (!MatchPattern(base::StringPiece(codecs[i]), | |
| 510 base::StringPiece(supported_codecs[j]))) { | |
| 511 continue; | |
| 512 } | |
| 513 // If suffix exists, check whether it is hexadecimal. | |
| 514 for (size_t wildcard_pos = supported_codecs[j].find('?'); | |
| 515 wildcard_pos < supported_codecs[j].length(); | |
| 516 wildcard_pos = supported_codecs[j].find('?', wildcard_pos + 1)) { | |
| 517 // Don't enforce case sensitivity, even though it's called for, as it | |
| 518 // would break some websites. | |
| 519 if (wildcard_pos >= codecs[i].length() || | |
| 520 !IsHexDigit(codecs[i].at(wildcard_pos))) { | |
| 521 return false; | |
| 522 } | |
| 523 } | |
| 524 codec_matched = true; | |
| 525 break; | |
| 526 } | |
| 527 if (!codec_matched) | |
| 528 return false; | |
| 529 } | |
| 530 return !codecs.empty(); | |
| 531 } | |
| 532 | |
| 480 void MimeUtil::InitializeMimeTypeMaps() { | 533 void MimeUtil::InitializeMimeTypeMaps() { |
| 481 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 534 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
| 482 image_map_.insert(supported_image_types[i]); | 535 image_map_.insert(supported_image_types[i]); |
| 483 | 536 |
| 484 // Initialize the supported non-image types. | 537 // Initialize the supported non-image types. |
| 485 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 538 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
| 486 non_image_map_.insert(supported_non_image_types[i]); | 539 non_image_map_.insert(supported_non_image_types[i]); |
| 487 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) | 540 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) |
| 488 non_image_map_.insert(supported_certificate_types[i].mime_type); | 541 non_image_map_.insert(supported_certificate_types[i].mime_type); |
| 489 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) | 542 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 540 MimeMappings codecs; | 593 MimeMappings codecs; |
| 541 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | 594 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { |
| 542 #if defined(OS_ANDROID) | 595 #if defined(OS_ANDROID) |
| 543 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) | 596 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) |
| 544 continue; | 597 continue; |
| 545 #endif | 598 #endif |
| 546 codecs.insert(mime_type_codecs[j]); | 599 codecs.insert(mime_type_codecs[j]); |
| 547 } | 600 } |
| 548 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; | 601 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; |
| 549 } | 602 } |
| 603 for (size_t i = 0; i < arraysize(format_mp4_codec_mappings); ++i) { | |
| 604 std::vector<std::string> mime_type_codecs; | |
| 605 ParseCodecString( | |
| 606 format_mp4_codec_mappings[i].codecs_list, &mime_type_codecs, false); | |
| 607 | |
| 608 MimeExpressionMappings codecs; | |
| 609 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | |
| 610 codecs.push_back(mime_type_codecs[j]); | |
| 611 } | |
| 612 strict_mp4_format_map_[format_mp4_codec_mappings[i].mime_type] = codecs; | |
| 613 } | |
| 550 } | 614 } |
| 551 | 615 |
| 552 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { | 616 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { |
| 553 return image_map_.find(mime_type) != image_map_.end(); | 617 return image_map_.find(mime_type) != image_map_.end(); |
| 554 } | 618 } |
| 555 | 619 |
| 556 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { | 620 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { |
| 557 return media_map_.find(mime_type) != media_map_.end(); | 621 return media_map_.find(mime_type) != media_map_.end(); |
| 558 } | 622 } |
| 559 | 623 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 722 for (std::vector<std::string>::iterator it = codecs_out->begin(); | 786 for (std::vector<std::string>::iterator it = codecs_out->begin(); |
| 723 it != codecs_out->end(); | 787 it != codecs_out->end(); |
| 724 ++it) { | 788 ++it) { |
| 725 size_t found = it->find_first_of('.'); | 789 size_t found = it->find_first_of('.'); |
| 726 if (found != std::string::npos) | 790 if (found != std::string::npos) |
| 727 it->resize(found); | 791 it->resize(found); |
| 728 } | 792 } |
| 729 } | 793 } |
| 730 | 794 |
| 731 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { | 795 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { |
| 732 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) | 796 if (strict_format_map_.find(mime_type) == strict_format_map_.end() && |
| 797 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end()) | |
| 733 return false; | 798 return false; |
| 734 return true; | 799 return true; |
| 735 } | 800 } |
| 736 | 801 |
| 737 bool MimeUtil::IsSupportedStrictMediaMimeType( | 802 CanPlayType MimeUtil::IsSupportedStrictMediaMimeType( |
| 738 const std::string& mime_type, | 803 const std::string& mime_type, |
| 739 const std::vector<std::string>& codecs) const { | 804 const std::vector<std::string>& codecs) const { |
| 740 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); | 805 StrictMappings::const_iterator it_strict_map = |
| 741 return (it != strict_format_map_.end()) && | 806 strict_format_map_.find(mime_type); |
| 742 AreSupportedCodecs(it->second, codecs); | 807 if ((it_strict_map != strict_format_map_.end()) && |
| 808 AreSupportedCodecs(it_strict_map->second, codecs)) { | |
| 809 return IsSupported; | |
| 810 } | |
| 811 | |
| 812 StrictExpressionMappings::const_iterator it_expression_map = | |
| 813 strict_mp4_format_map_.find(mime_type); | |
| 814 if ((it_expression_map != strict_mp4_format_map_.end()) && | |
| 815 AreSupportedCodecsWithProfile(it_expression_map->second, codecs)) { | |
| 816 return MayBeSupported; | |
| 817 } | |
| 818 | |
| 819 if (codecs.empty()) | |
| 820 return MayBeSupported; | |
| 821 | |
| 822 return IsNotSupported; | |
| 743 } | 823 } |
| 744 | 824 |
| 745 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { | 825 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { |
| 746 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { | 826 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { |
| 747 non_image_map_.erase(proprietary_media_types[i]); | 827 non_image_map_.erase(proprietary_media_types[i]); |
| 748 media_map_.erase(proprietary_media_types[i]); | 828 media_map_.erase(proprietary_media_types[i]); |
| 749 } | 829 } |
| 750 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) | 830 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) |
| 751 codecs_map_.erase(proprietary_media_codecs[i]); | 831 codecs_map_.erase(proprietary_media_codecs[i]); |
| 752 } | 832 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 810 } | 890 } |
| 811 | 891 |
| 812 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 892 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
| 813 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); | 893 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
| 814 } | 894 } |
| 815 | 895 |
| 816 bool IsStrictMediaMimeType(const std::string& mime_type) { | 896 bool IsStrictMediaMimeType(const std::string& mime_type) { |
| 817 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | 897 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
| 818 } | 898 } |
| 819 | 899 |
| 820 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, | 900 CanPlayType IsSupportedStrictMediaMimeType( |
| 821 const std::vector<std::string>& codecs) { | 901 const std::string& mime_type, |
| 902 const std::vector<std::string>& codecs) { | |
| 822 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); | 903 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); |
| 823 } | 904 } |
| 824 | 905 |
| 825 void ParseCodecString(const std::string& codecs, | 906 void ParseCodecString(const std::string& codecs, |
| 826 std::vector<std::string>* codecs_out, | 907 std::vector<std::string>* codecs_out, |
| 827 const bool strip) { | 908 const bool strip) { |
| 828 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); | 909 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
| 829 } | 910 } |
| 830 | 911 |
| 831 namespace { | 912 namespace { |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1060 post_data->append("\r\n" + value + "\r\n"); | 1141 post_data->append("\r\n" + value + "\r\n"); |
| 1061 } | 1142 } |
| 1062 | 1143 |
| 1063 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1144 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
| 1064 std::string* post_data) { | 1145 std::string* post_data) { |
| 1065 DCHECK(post_data); | 1146 DCHECK(post_data); |
| 1066 post_data->append("--" + mime_boundary + "--\r\n"); | 1147 post_data->append("--" + mime_boundary + "--\r\n"); |
| 1067 } | 1148 } |
| 1068 | 1149 |
| 1069 } // namespace net | 1150 } // namespace net |
| OLD | NEW |