| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <map> | 5 #include <map> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "net/base/mime_util.h" | 8 #include "net/base/mime_util.h" |
| 9 #include "net/base/platform_mime_util.h" | 9 #include "net/base/platform_mime_util.h" |
| 10 | 10 |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/singleton.h" | |
| 14 #include "base/string_split.h" | 14 #include "base/string_split.h" |
| 15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 | 17 |
| 18 using std::string; | 18 using std::string; |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 // Singleton utility class for mime types. | 22 // Singleton utility class for mime types. |
| 23 class MimeUtil : public PlatformMimeUtil { | 23 class MimeUtil : public PlatformMimeUtil { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 44 | 44 |
| 45 void ParseCodecString(const std::string& codecs, | 45 void ParseCodecString(const std::string& codecs, |
| 46 std::vector<std::string>* codecs_out, | 46 std::vector<std::string>* codecs_out, |
| 47 bool strip); | 47 bool strip); |
| 48 | 48 |
| 49 bool IsStrictMediaMimeType(const std::string& mime_type) const; | 49 bool IsStrictMediaMimeType(const std::string& mime_type) const; |
| 50 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, | 50 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, |
| 51 const std::vector<std::string>& codecs) const; | 51 const std::vector<std::string>& codecs) const; |
| 52 | 52 |
| 53 private: | 53 private: |
| 54 friend struct DefaultSingletonTraits<MimeUtil>; | 54 friend struct base::DefaultLazyInstanceTraits<MimeUtil>; |
| 55 MimeUtil() { | 55 MimeUtil() { |
| 56 InitializeMimeTypeMaps(); | 56 InitializeMimeTypeMaps(); |
| 57 } | 57 } |
| 58 | 58 |
| 59 // For faster lookup, keep hash sets. | 59 // For faster lookup, keep hash sets. |
| 60 void InitializeMimeTypeMaps(); | 60 void InitializeMimeTypeMaps(); |
| 61 | 61 |
| 62 typedef base::hash_set<std::string> MimeMappings; | 62 typedef base::hash_set<std::string> MimeMappings; |
| 63 MimeMappings image_map_; | 63 MimeMappings image_map_; |
| 64 MimeMappings media_map_; | 64 MimeMappings media_map_; |
| 65 MimeMappings non_image_map_; | 65 MimeMappings non_image_map_; |
| 66 MimeMappings javascript_map_; | 66 MimeMappings javascript_map_; |
| 67 MimeMappings view_source_map_; | 67 MimeMappings view_source_map_; |
| 68 MimeMappings codecs_map_; | 68 MimeMappings codecs_map_; |
| 69 | 69 |
| 70 typedef std::map<std::string, base::hash_set<std::string> > StrictMappings; | 70 typedef std::map<std::string, base::hash_set<std::string> > StrictMappings; |
| 71 StrictMappings strict_format_map_; | 71 StrictMappings strict_format_map_; |
| 72 }; // class MimeUtil | 72 }; // class MimeUtil |
| 73 | 73 |
| 74 static base::LazyInstance<MimeUtil> g_mime_util(base::LINKER_INITIALIZED); |
| 75 |
| 74 struct MimeInfo { | 76 struct MimeInfo { |
| 75 const char* mime_type; | 77 const char* mime_type; |
| 76 const char* extensions; // comma separated list | 78 const char* extensions; // comma separated list |
| 77 }; | 79 }; |
| 78 | 80 |
| 79 static const MimeInfo primary_mappings[] = { | 81 static const MimeInfo primary_mappings[] = { |
| 80 { "text/html", "html,htm" }, | 82 { "text/html", "html,htm" }, |
| 81 { "text/css", "css" }, | 83 { "text/css", "css" }, |
| 82 { "text/xml", "xml" }, | 84 { "text/xml", "xml" }, |
| 83 { "image/gif", "gif" }, | 85 { "image/gif", "gif" }, |
| (...skipping 382 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 return false; | 468 return false; |
| 467 } | 469 } |
| 468 } | 470 } |
| 469 return true; | 471 return true; |
| 470 } | 472 } |
| 471 | 473 |
| 472 //---------------------------------------------------------------------------- | 474 //---------------------------------------------------------------------------- |
| 473 // Wrappers for the singleton | 475 // Wrappers for the singleton |
| 474 //---------------------------------------------------------------------------- | 476 //---------------------------------------------------------------------------- |
| 475 | 477 |
| 476 static MimeUtil* GetMimeUtil() { | |
| 477 return Singleton<MimeUtil>::get(); | |
| 478 } | |
| 479 | |
| 480 bool GetMimeTypeFromExtension(const FilePath::StringType& ext, | 478 bool GetMimeTypeFromExtension(const FilePath::StringType& ext, |
| 481 std::string* mime_type) { | 479 std::string* mime_type) { |
| 482 return GetMimeUtil()->GetMimeTypeFromExtension(ext, mime_type); | 480 return g_mime_util.Get().GetMimeTypeFromExtension(ext, mime_type); |
| 483 } | 481 } |
| 484 | 482 |
| 485 bool GetMimeTypeFromFile(const FilePath& file_path, std::string* mime_type) { | 483 bool GetMimeTypeFromFile(const FilePath& file_path, std::string* mime_type) { |
| 486 return GetMimeUtil()->GetMimeTypeFromFile(file_path, mime_type); | 484 return g_mime_util.Get().GetMimeTypeFromFile(file_path, mime_type); |
| 487 } | 485 } |
| 488 | 486 |
| 489 bool GetPreferredExtensionForMimeType(const std::string& mime_type, | 487 bool GetPreferredExtensionForMimeType(const std::string& mime_type, |
| 490 FilePath::StringType* extension) { | 488 FilePath::StringType* extension) { |
| 491 return GetMimeUtil()->GetPreferredExtensionForMimeType(mime_type, extension); | 489 return g_mime_util.Get().GetPreferredExtensionForMimeType(mime_type, |
| 490 extension); |
| 492 } | 491 } |
| 493 | 492 |
| 494 bool IsSupportedImageMimeType(const char* mime_type) { | 493 bool IsSupportedImageMimeType(const char* mime_type) { |
| 495 return GetMimeUtil()->IsSupportedImageMimeType(mime_type); | 494 return g_mime_util.Get().IsSupportedImageMimeType(mime_type); |
| 496 } | 495 } |
| 497 | 496 |
| 498 bool IsSupportedMediaMimeType(const char* mime_type) { | 497 bool IsSupportedMediaMimeType(const char* mime_type) { |
| 499 return GetMimeUtil()->IsSupportedMediaMimeType(mime_type); | 498 return g_mime_util.Get().IsSupportedMediaMimeType(mime_type); |
| 500 } | 499 } |
| 501 | 500 |
| 502 bool IsSupportedNonImageMimeType(const char* mime_type) { | 501 bool IsSupportedNonImageMimeType(const char* mime_type) { |
| 503 return GetMimeUtil()->IsSupportedNonImageMimeType(mime_type); | 502 return g_mime_util.Get().IsSupportedNonImageMimeType(mime_type); |
| 504 } | 503 } |
| 505 | 504 |
| 506 bool IsSupportedJavascriptMimeType(const char* mime_type) { | 505 bool IsSupportedJavascriptMimeType(const char* mime_type) { |
| 507 return GetMimeUtil()->IsSupportedJavascriptMimeType(mime_type); | 506 return g_mime_util.Get().IsSupportedJavascriptMimeType(mime_type); |
| 508 } | 507 } |
| 509 | 508 |
| 510 bool IsViewSourceMimeType(const char* mime_type) { | 509 bool IsViewSourceMimeType(const char* mime_type) { |
| 511 return GetMimeUtil()->IsViewSourceMimeType(mime_type); | 510 return g_mime_util.Get().IsViewSourceMimeType(mime_type); |
| 512 } | 511 } |
| 513 | 512 |
| 514 bool IsSupportedMimeType(const std::string& mime_type) { | 513 bool IsSupportedMimeType(const std::string& mime_type) { |
| 515 return GetMimeUtil()->IsSupportedMimeType(mime_type); | 514 return g_mime_util.Get().IsSupportedMimeType(mime_type); |
| 516 } | 515 } |
| 517 | 516 |
| 518 bool MatchesMimeType(const std::string &mime_type_pattern, | 517 bool MatchesMimeType(const std::string &mime_type_pattern, |
| 519 const std::string &mime_type) { | 518 const std::string &mime_type) { |
| 520 return GetMimeUtil()->MatchesMimeType(mime_type_pattern, mime_type); | 519 return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type); |
| 521 } | 520 } |
| 522 | 521 |
| 523 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { | 522 bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
| 524 return GetMimeUtil()->AreSupportedMediaCodecs(codecs); | 523 return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
| 525 } | 524 } |
| 526 | 525 |
| 527 bool IsStrictMediaMimeType(const std::string& mime_type) { | 526 bool IsStrictMediaMimeType(const std::string& mime_type) { |
| 528 return GetMimeUtil()->IsStrictMediaMimeType(mime_type); | 527 return g_mime_util.Get().IsStrictMediaMimeType(mime_type); |
| 529 } | 528 } |
| 530 | 529 |
| 531 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, | 530 bool IsSupportedStrictMediaMimeType(const std::string& mime_type, |
| 532 const std::vector<std::string>& codecs) { | 531 const std::vector<std::string>& codecs) { |
| 533 return GetMimeUtil()->IsSupportedStrictMediaMimeType(mime_type, codecs); | 532 return g_mime_util.Get().IsSupportedStrictMediaMimeType(mime_type, codecs); |
| 534 } | 533 } |
| 535 | 534 |
| 536 void ParseCodecString(const std::string& codecs, | 535 void ParseCodecString(const std::string& codecs, |
| 537 std::vector<std::string>* codecs_out, | 536 std::vector<std::string>* codecs_out, |
| 538 const bool strip) { | 537 const bool strip) { |
| 539 GetMimeUtil()->ParseCodecString(codecs, codecs_out, strip); | 538 g_mime_util.Get().ParseCodecString(codecs, codecs_out, strip); |
| 540 } | 539 } |
| 541 | 540 |
| 542 namespace { | 541 namespace { |
| 543 | 542 |
| 544 // From http://www.w3schools.com/media/media_mimeref.asp and | 543 // From http://www.w3schools.com/media/media_mimeref.asp and |
| 545 // http://plugindoc.mozdev.org/winmime.php | 544 // http://plugindoc.mozdev.org/winmime.php |
| 546 static const char* kStandardImageTypes[] = { | 545 static const char* kStandardImageTypes[] = { |
| 547 "image/bmp", | 546 "image/bmp", |
| 548 "image/cis-cod", | 547 "image/cis-cod", |
| 549 "image/gif", | 548 "image/gif", |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 708 | 707 |
| 709 GetExtensionsFromHardCodedMappings(secondary_mappings, | 708 GetExtensionsFromHardCodedMappings(secondary_mappings, |
| 710 arraysize(secondary_mappings), | 709 arraysize(secondary_mappings), |
| 711 mime_type, | 710 mime_type, |
| 712 &unique_extensions); | 711 &unique_extensions); |
| 713 | 712 |
| 714 HashSetToVector(&unique_extensions, extensions); | 713 HashSetToVector(&unique_extensions, extensions); |
| 715 } | 714 } |
| 716 | 715 |
| 717 } // namespace net | 716 } // namespace net |
| OLD | NEW |