Index: net/base/mime_util.cc |
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc |
index 8ff58e6d579b674bddb20da134a4aaf7f7e0e8bf..2ea2834c29649182f727adf8c7e2815da5b7cec6 100644 |
--- a/net/base/mime_util.cc |
+++ b/net/base/mime_util.cc |
@@ -43,6 +43,8 @@ class MimeUtil : public PlatformMimeUtil { |
bool MatchesMimeType(const std::string &mime_type_pattern, |
const std::string &mime_type) const; |
+ bool IsStringMimeType(const std::string& type_string) const; |
+ |
bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const; |
void ParseCodecString(const std::string& codecs, |
@@ -486,6 +488,52 @@ bool MimeUtil::MatchesMimeType(const std::string& mime_type_pattern, |
return true; |
} |
+// See http://www.iana.org/assignments/media-types/index.html |
+static const char* legal_top_level_types[] = { |
+ "application/", |
+ "audio/", |
+ "example/", |
+ "image/", |
+ "message/", |
+ "model/", |
+ "multipart/", |
+ "text/", |
+ "video/", |
+}; |
+ |
+bool MimeUtil::IsStringMimeType(const std::string& type_string) const { |
+ // MIME types are always ASCII and case-insensitive (at least, the top-level |
+ // and secondary types we care about). |
+ if (!IsStringASCII(type_string)) |
+ return false; |
+ |
+ if (type_string == "*/*" || type_string == "*") |
+ return true; |
+ |
+ bool top_level_legal = false; |
+ for (size_t i = 0; i < arraysize(legal_top_level_types); ++i) { |
+ if (StartsWithASCII(type_string, legal_top_level_types[i], false) && |
+ type_string.length() > strlen(legal_top_level_types[i])) { |
+ top_level_legal = true; |
rvargas (doing something else)
2012/06/05 17:54:07
Why not return true instead?
Greg Billock
2012/06/05 18:09:53
Thanks, I failed the refactor there.
|
+ break; |
+ } |
+ } |
+ if (top_level_legal) |
+ return true; |
+ |
+ // If there's a "/" separator character, and the token before it is |
+ // "x-" + (ascii characters), it is also a MIME type. |
+ size_t slash = type_string.find('/'); |
+ if (slash < 3 || |
+ slash == std::string::npos || slash == type_string.length() - 1) |
+ return false; |
rvargas (doing something else)
2012/06/05 17:54:07
nit: use {} here.
Greg Billock
2012/06/05 18:09:53
Done.
|
+ |
+ if (StartsWithASCII(type_string, "x-", false)) |
+ return true; |
+ |
+ return false; |
+} |
+ |
bool MimeUtil::AreSupportedMediaCodecs( |
const std::vector<std::string>& codecs) const { |
return AreSupportedCodecs(codecs_map_, codecs); |
@@ -578,6 +626,10 @@ bool MatchesMimeType(const std::string& mime_type_pattern, |
return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type); |
} |
+bool IsStringMimeType(const std::string& type_string) { |
+ return g_mime_util.Get().IsStringMimeType(type_string); |
+} |
+ |
bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) { |
return g_mime_util.Get().AreSupportedMediaCodecs(codecs); |
} |