diff options
author | kylep@chromium.org <kylep@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-17 23:22:55 +0000 |
---|---|---|
committer | kylep@chromium.org <kylep@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-17 23:22:55 +0000 |
commit | e9696d5781422146bfd90766ab9307dc614c587a (patch) | |
tree | bbb2836b03cd6c6615c592892c6c698ceb248559 /net/base | |
parent | 356182ef5387439466a819e9bfd49929d5349155 (diff) | |
download | chromium_src-e9696d5781422146bfd90766ab9307dc614c587a.zip chromium_src-e9696d5781422146bfd90766ab9307dc614c587a.tar.gz chromium_src-e9696d5781422146bfd90766ab9307dc614c587a.tar.bz2 |
Add codec parsing capability to MimeUtil
BUG=16636
TEST=mime_util_unittest.cc
Review URL: http://codereview.chromium.org/149761
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21021 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base')
-rw-r--r-- | net/base/mime_util.cc | 24 | ||||
-rw-r--r-- | net/base/mime_util.h | 8 | ||||
-rw-r--r-- | net/base/mime_util_unittest.cc | 29 |
3 files changed, 61 insertions, 0 deletions
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc index 85de4a9..6be08d5 100644 --- a/net/base/mime_util.cc +++ b/net/base/mime_util.cc @@ -37,6 +37,9 @@ class MimeUtil : public PlatformMimeUtil { bool MatchesMimeType(const std::string &mime_type_pattern, const std::string &mime_type) const; + void ParseCodecString(const std::string& codecs, + std::vector<std::string>* codecs_out); + private: friend struct DefaultSingletonTraits<MimeUtil>; MimeUtil() { @@ -337,6 +340,22 @@ bool MimeUtil::MatchesMimeType(const std::string &mime_type_pattern, return true; } +void MimeUtil::ParseCodecString(const std::string& codecs, + std::vector<std::string>* codecs_out) { + std::string no_quote_codecs; + TrimString(codecs, "\"", &no_quote_codecs); + SplitString(no_quote_codecs, ',', codecs_out); + + // Truncate each string at the '.' + for (std::vector<std::string>::iterator it = codecs_out->begin(); + it != codecs_out->end(); + ++it) { + size_t found = it->find_first_of('.'); + if (found != std::string::npos) + it->resize(found); + } +} + //---------------------------------------------------------------------------- // Wrappers for the singleton //---------------------------------------------------------------------------- @@ -388,4 +407,9 @@ bool MatchesMimeType(const std::string &mime_type_pattern, return GetMimeUtil()->MatchesMimeType(mime_type_pattern, mime_type); } +void ParseCodecString(const std::string& codecs, + std::vector<std::string>* codecs_out) { + GetMimeUtil()->ParseCodecString(codecs, codecs_out); +} + } // namespace net diff --git a/net/base/mime_util.h b/net/base/mime_util.h index 6ae4a55..17620d9 100644 --- a/net/base/mime_util.h +++ b/net/base/mime_util.h @@ -6,6 +6,7 @@ #define NET_BASE_MIME_UTIL_H__ #include <string> +#include <vector> #include "base/file_path.h" @@ -45,6 +46,13 @@ bool IsSupportedMimeType(const std::string& mime_type); bool MatchesMimeType(const std::string &mime_type_pattern, const std::string &mime_type); +// Parses a codec string, populating |codecs_out| with the prefix of each codec +// in the string |codecs_in|. For example, passed "aaa.b.c,dd.eee", |codecs_out| +// will contain {"aaa", "dd"}. +// See http://www.ietf.org/rfc/rfc4281.txt. +void ParseCodecString(const std::string& codecs, + std::vector<std::string>* codecs_out); + } // namespace net #endif // NET_BASE_MIME_UTIL_H__ diff --git a/net/base/mime_util_unittest.cc b/net/base/mime_util_unittest.cc index e1d7906..0300da8 100644 --- a/net/base/mime_util_unittest.cc +++ b/net/base/mime_util_unittest.cc @@ -96,3 +96,32 @@ TEST(MimeUtilTest, MatchesMimeType) { EXPECT_EQ(false, net::MatchesMimeType("aaa*aaa", "aaaaa")); } + +// Note: codecs should only be a list of 2 or fewer; hence the restriction of +// results' length to 2. +TEST(MimeUtilTest, ParseCodecString) { + const struct { + const char* original; + size_t expected_size; + const char* results[2]; + } tests[] = { + { "\"bogus\"", 1, { "bogus" } }, + { "0", 1, { "0" } }, + { "avc1.42E01E, mp4a.40.2", 2, { "avc1", "mp4a" } }, + { "\"mp4v.20.240, mp4a.40.2\"", 2, { "mp4v", "mp4a" } }, + { "mp4v.20.8, samr", 2, { "mp4v", "samr" } }, + { "\"theora, vorbis\"", 2, { "theora", "vorbis" } }, + { "", 1, { "" } }, + { "\"\"", 1, { "" } }, + { ",", 2, { "", "" } }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { + std::vector<std::string> codecs_out; + net::ParseCodecString(tests[i].original, &codecs_out); + EXPECT_EQ(tests[i].expected_size, codecs_out.size()); + for (size_t j = 0; j < tests[i].expected_size; ++j) { + EXPECT_EQ(tests[i].results[j], codecs_out[j]); + } + } +} |