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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 bool 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 bool IsSupportedStrictMP4MediaMimeType( |
| 85 const std::string& mime_type, |
| 86 const std::vector<std::string>& codecs) const; |
84 | 87 |
85 void RemoveProprietaryMediaTypesAndCodecsForTests(); | 88 void RemoveProprietaryMediaTypesAndCodecsForTests(); |
86 | 89 |
87 private: | 90 private: |
88 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; | 91 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; |
89 | 92 |
90 typedef base::hash_set<std::string> MimeMappings; | 93 typedef base::hash_set<std::string> MimeMappings; |
91 typedef std::map<std::string, MimeMappings> StrictMappings; | 94 typedef std::map<std::string, MimeMappings> StrictMappings; |
92 | 95 |
| 96 typedef std::vector<std::string> MimeExpressionMappings; |
| 97 typedef std::map<std::string, MimeExpressionMappings> |
| 98 StrictExpressionMappings; |
| 99 |
93 MimeUtil(); | 100 MimeUtil(); |
94 | 101 |
95 // Returns true if |codecs| is nonempty and all the items in it are present in | 102 // Returns true if |codecs| is nonempty and all the items in it are present in |
96 // |supported_codecs|. | 103 // |supported_codecs|. |
97 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, | 104 static bool AreSupportedCodecs(const MimeMappings& supported_codecs, |
98 const std::vector<std::string>& codecs); | 105 const std::vector<std::string>& codecs); |
| 106 static bool AreSupportedCodecsWithProfile( |
| 107 const MimeExpressionMappings& supported_codecs, |
| 108 const std::vector<std::string>& codecs); |
99 | 109 |
100 // For faster lookup, keep hash sets. | 110 // For faster lookup, keep hash sets. |
101 void InitializeMimeTypeMaps(); | 111 void InitializeMimeTypeMaps(); |
102 | 112 |
103 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, | 113 bool GetMimeTypeFromExtensionHelper(const base::FilePath::StringType& ext, |
104 bool include_platform_types, | 114 bool include_platform_types, |
105 std::string* mime_type) const; | 115 std::string* mime_type) const; |
106 | 116 |
107 MimeMappings image_map_; | 117 MimeMappings image_map_; |
108 MimeMappings media_map_; | 118 MimeMappings media_map_; |
109 MimeMappings non_image_map_; | 119 MimeMappings non_image_map_; |
110 MimeMappings unsupported_text_map_; | 120 MimeMappings unsupported_text_map_; |
111 MimeMappings javascript_map_; | 121 MimeMappings javascript_map_; |
112 MimeMappings codecs_map_; | 122 MimeMappings codecs_map_; |
113 | 123 |
114 StrictMappings strict_format_map_; | 124 StrictMappings strict_format_map_; |
| 125 StrictExpressionMappings strict_mp4_format_map_; |
115 }; // class MimeUtil | 126 }; // class MimeUtil |
116 | 127 |
117 // This variable is Leaky because we need to access it from WorkerPool threads. | 128 // This variable is Leaky because we need to access it from WorkerPool threads. |
118 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = | 129 static base::LazyInstance<MimeUtil>::Leaky g_mime_util = |
119 LAZY_INSTANCE_INITIALIZER; | 130 LAZY_INSTANCE_INITIALIZER; |
120 | 131 |
121 struct MimeInfo { | 132 struct MimeInfo { |
122 const char* mime_type; | 133 const char* mime_type; |
123 const char* extensions; // comma separated list | 134 const char* extensions; // comma separated list |
124 }; | 135 }; |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
437 { "audio/wav", "1" }, | 448 { "audio/wav", "1" }, |
438 { "audio/x-wav", "1" }, | 449 { "audio/x-wav", "1" }, |
439 { "video/ogg", "opus,theora,vorbis" }, | 450 { "video/ogg", "opus,theora,vorbis" }, |
440 { "audio/ogg", "opus,vorbis" }, | 451 { "audio/ogg", "opus,vorbis" }, |
441 { "application/ogg", "opus,theora,vorbis" }, | 452 { "application/ogg", "opus,theora,vorbis" }, |
442 { "audio/mpeg", "" }, | 453 { "audio/mpeg", "" }, |
443 { "audio/mp3", "" }, | 454 { "audio/mp3", "" }, |
444 { "audio/x-mp3", "" } | 455 { "audio/x-mp3", "" } |
445 }; | 456 }; |
446 | 457 |
| 458 static const MediaFormatStrict format_mp4_codec_mappings[] = { |
| 459 {"audio/mp4", "mp4a,mp4a.40,mp4a.40.?"}, |
| 460 {"audio/x-m4a", "mp4a,mp4a.40,mp4a.40.?"}, |
| 461 {"video/mp4", "avc1,avc3,avc1.??????,avc3.??????,mp4a,mp4a.40,mp4a.40.?"}, |
| 462 {"video/x-m4v", "avc1,avc3,avc1.??????,avc3.??????,mp4a,mp4a.40,mp4a.40.?"}, |
| 463 {"application/x-mpegurl", |
| 464 "avc1,avc3,avc1.??????,avc3.??????,mp4a,mp4a.40,mp4a.40.?"}, |
| 465 {"application/vnd.apple.mpegurl", |
| 466 "avc1,avc3,avc1.??????,avc3.??????,mp4a,mp4a.40,mp4a.40.?"}}; |
| 467 |
447 MimeUtil::MimeUtil() { | 468 MimeUtil::MimeUtil() { |
448 InitializeMimeTypeMaps(); | 469 InitializeMimeTypeMaps(); |
449 } | 470 } |
450 | 471 |
451 // static | 472 // static |
452 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, | 473 bool MimeUtil::AreSupportedCodecs(const MimeMappings& supported_codecs, |
453 const std::vector<std::string>& codecs) { | 474 const std::vector<std::string>& codecs) { |
454 if (supported_codecs.empty()) | 475 if (supported_codecs.empty()) |
455 return codecs.empty(); | 476 return codecs.empty(); |
456 | 477 |
457 for (size_t i = 0; i < codecs.size(); ++i) { | 478 for (size_t i = 0; i < codecs.size(); ++i) { |
458 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) | 479 if (supported_codecs.find(codecs[i]) == supported_codecs.end()) |
459 return false; | 480 return false; |
460 } | 481 } |
461 return !codecs.empty(); | 482 return !codecs.empty(); |
462 } | 483 } |
463 | 484 |
| 485 bool MimeUtil::AreSupportedCodecsWithProfile( |
| 486 const MimeExpressionMappings& supported_codecs, |
| 487 const std::vector<std::string>& codecs) { |
| 488 for (size_t i = 0; i < codecs.size(); ++i) { |
| 489 bool codec_matched = false; |
| 490 for (size_t j = 0; j < supported_codecs.size(); ++j) { |
| 491 if (MatchPattern(static_cast<base::StringPiece>(codecs[i]), |
| 492 static_cast<base::StringPiece>(supported_codecs[j]))) { |
| 493 codec_matched = true; |
| 494 std::vector<std::string> split_string; |
| 495 base::SplitString(codecs[i], '.', &split_string); |
| 496 if (split_string.size() > 1 && |
| 497 !IsUpperCaseHexNumber(split_string.back())) { |
| 498 codec_matched = false; |
| 499 } |
| 500 break; |
| 501 } |
| 502 } |
| 503 // Return false if any of the codecs is not matching. |
| 504 if (!codec_matched) |
| 505 return false; |
| 506 } |
| 507 return !codecs.empty(); |
| 508 } |
| 509 |
464 void MimeUtil::InitializeMimeTypeMaps() { | 510 void MimeUtil::InitializeMimeTypeMaps() { |
465 for (size_t i = 0; i < arraysize(supported_image_types); ++i) | 511 for (size_t i = 0; i < arraysize(supported_image_types); ++i) |
466 image_map_.insert(supported_image_types[i]); | 512 image_map_.insert(supported_image_types[i]); |
467 | 513 |
468 // Initialize the supported non-image types. | 514 // Initialize the supported non-image types. |
469 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) | 515 for (size_t i = 0; i < arraysize(supported_non_image_types); ++i) |
470 non_image_map_.insert(supported_non_image_types[i]); | 516 non_image_map_.insert(supported_non_image_types[i]); |
471 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) | 517 for (size_t i = 0; i < arraysize(supported_certificate_types); ++i) |
472 non_image_map_.insert(supported_certificate_types[i].mime_type); | 518 non_image_map_.insert(supported_certificate_types[i].mime_type); |
473 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) | 519 for (size_t i = 0; i < arraysize(unsupported_text_types); ++i) |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
514 MimeMappings codecs; | 560 MimeMappings codecs; |
515 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { | 561 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { |
516 #if defined(OS_ANDROID) | 562 #if defined(OS_ANDROID) |
517 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) | 563 if (!IsCodecSupportedOnAndroid(mime_type_codecs[j])) |
518 continue; | 564 continue; |
519 #endif | 565 #endif |
520 codecs.insert(mime_type_codecs[j]); | 566 codecs.insert(mime_type_codecs[j]); |
521 } | 567 } |
522 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; | 568 strict_format_map_[format_codec_mappings[i].mime_type] = codecs; |
523 } | 569 } |
| 570 for (size_t i = 0; i < arraysize(format_mp4_codec_mappings); ++i) { |
| 571 std::vector<std::string> mime_type_codecs; |
| 572 ParseCodecString( |
| 573 format_mp4_codec_mappings[i].codecs_list, &mime_type_codecs, false); |
| 574 |
| 575 MimeExpressionMappings codecs; |
| 576 for (size_t j = 0; j < mime_type_codecs.size(); ++j) { |
| 577 codecs.push_back(mime_type_codecs[j]); |
| 578 } |
| 579 strict_mp4_format_map_[format_mp4_codec_mappings[i].mime_type] = codecs; |
| 580 } |
524 } | 581 } |
525 | 582 |
526 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { | 583 bool MimeUtil::IsSupportedImageMimeType(const std::string& mime_type) const { |
527 return image_map_.find(mime_type) != image_map_.end(); | 584 return image_map_.find(mime_type) != image_map_.end(); |
528 } | 585 } |
529 | 586 |
530 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { | 587 bool MimeUtil::IsSupportedMediaMimeType(const std::string& mime_type) const { |
531 return media_map_.find(mime_type) != media_map_.end(); | 588 return media_map_.find(mime_type) != media_map_.end(); |
532 } | 589 } |
533 | 590 |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
696 for (std::vector<std::string>::iterator it = codecs_out->begin(); | 753 for (std::vector<std::string>::iterator it = codecs_out->begin(); |
697 it != codecs_out->end(); | 754 it != codecs_out->end(); |
698 ++it) { | 755 ++it) { |
699 size_t found = it->find_first_of('.'); | 756 size_t found = it->find_first_of('.'); |
700 if (found != std::string::npos) | 757 if (found != std::string::npos) |
701 it->resize(found); | 758 it->resize(found); |
702 } | 759 } |
703 } | 760 } |
704 | 761 |
705 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { | 762 bool MimeUtil::IsStrictMediaMimeType(const std::string& mime_type) const { |
706 if (strict_format_map_.find(mime_type) == strict_format_map_.end()) | 763 if (strict_format_map_.find(mime_type) == strict_format_map_.end() && |
| 764 strict_mp4_format_map_.find(mime_type) == strict_mp4_format_map_.end()) |
707 return false; | 765 return false; |
708 return true; | 766 return true; |
709 } | 767 } |
710 | 768 |
711 bool MimeUtil::IsSupportedStrictMediaMimeType( | 769 bool MimeUtil::IsSupportedStrictMediaMimeType( |
712 const std::string& mime_type, | 770 const std::string& mime_type, |
713 const std::vector<std::string>& codecs) const { | 771 const std::vector<std::string>& codecs) const { |
714 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); | 772 StrictMappings::const_iterator it = strict_format_map_.find(mime_type); |
715 return (it != strict_format_map_.end()) && | 773 return (it != strict_format_map_.end()) && |
716 AreSupportedCodecs(it->second, codecs); | 774 AreSupportedCodecs(it->second, codecs); |
717 } | 775 } |
718 | 776 |
| 777 bool MimeUtil::IsSupportedStrictMP4MediaMimeType( |
| 778 const std::string& mime_type, |
| 779 const std::vector<std::string>& codecs) const { |
| 780 StrictExpressionMappings::const_iterator it = |
| 781 strict_mp4_format_map_.find(mime_type); |
| 782 return (it != strict_mp4_format_map_.end() && |
| 783 AreSupportedCodecsWithProfile(it->second, codecs)); |
| 784 } |
| 785 |
719 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { | 786 void MimeUtil::RemoveProprietaryMediaTypesAndCodecsForTests() { |
720 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { | 787 for (size_t i = 0; i < arraysize(proprietary_media_types); ++i) { |
721 non_image_map_.erase(proprietary_media_types[i]); | 788 non_image_map_.erase(proprietary_media_types[i]); |
722 media_map_.erase(proprietary_media_types[i]); | 789 media_map_.erase(proprietary_media_types[i]); |
723 } | 790 } |
724 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) | 791 for (size_t i = 0; i < arraysize(proprietary_media_codecs); ++i) |
725 codecs_map_.erase(proprietary_media_codecs[i]); | 792 codecs_map_.erase(proprietary_media_codecs[i]); |
726 } | 793 } |
727 | 794 |
728 //---------------------------------------------------------------------------- | 795 //---------------------------------------------------------------------------- |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
789 | 856 |
790 bool IsStrictMediaMimeType(const std::string& mime_type) { | 857 bool IsStrictMediaMimeType(const std::string& mime_type) { |
791 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); | 858 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
792 } | 859 } |
793 | 860 |
794 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, | 861 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, |
795 const std::vector<std::string>& codecs) { | 862 const std::vector<std::string>& codecs) { |
796 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); | 863 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); |
797 } | 864 } |
798 | 865 |
| 866 bool IsSupportedStrictMP4MediaMimeType(const std::string& mime_type, |
| 867 const std::vector<std::string>& codecs) { |
| 868 return g_mime_util.Get().IsSupportedStrictMP4MediaMimeType(mime_type, codecs); |
| 869 } |
| 870 |
799 void ParseCodecString(const std::string& codecs, | 871 void ParseCodecString(const std::string& codecs, |
800 std::vector<std::string>* codecs_out, | 872 std::vector<std::string>* codecs_out, |
801 const bool strip) { | 873 const bool strip) { |
802 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); | 874 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
803 } | 875 } |
804 | 876 |
805 namespace { | 877 namespace { |
806 | 878 |
807 // From http://www.w3schools.com/media/media_mimeref.asp and | 879 // From http://www.w3schools.com/media/media_mimeref.asp and |
808 // http://plugindoc.mozdev.org/winmime.php | 880 // http://plugindoc.mozdev.org/winmime.php |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1034 post_data->append("\r\n" + value + "\r\n"); | 1106 post_data->append("\r\n" + value + "\r\n"); |
1035 } | 1107 } |
1036 | 1108 |
1037 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, | 1109 void AddMultipartFinalDelimiterForUpload(const std::string& mime_boundary, |
1038 std::string* post_data) { | 1110 std::string* post_data) { |
1039 DCHECK(post_data); | 1111 DCHECK(post_data); |
1040 post_data->append("--" + mime_boundary + "--\r\n"); | 1112 post_data->append("--" + mime_boundary + "--\r\n"); |
1041 } | 1113 } |
1042 | 1114 |
1043 } // namespace net | 1115 } // namespace net |
OLD | NEW |