Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Unified Diff: net/base/mime_util.cc

Issue 10448109: Move function for classifying a string as a mime type into MimeUtil (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adapt for windows unicode strings. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/base/mime_util.h ('k') | net/base/mime_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/mime_util.cc
diff --git a/net/base/mime_util.cc b/net/base/mime_util.cc
index 8ff58e6d579b674bddb20da134a4aaf7f7e0e8bf..364cbdf084f0764cd8a2172897c0b58d7b6b3e60 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 IsMimeType(const std::string& type_string) const;
+
bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) const;
void ParseCodecString(const std::string& codecs,
@@ -486,6 +488,49 @@ 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::IsMimeType(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;
+
+ 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])) {
+ 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;
+ }
+
+ 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 +623,10 @@ bool MatchesMimeType(const std::string& mime_type_pattern,
return g_mime_util.Get().MatchesMimeType(mime_type_pattern, mime_type);
}
+bool IsMimeType(const std::string& type_string) {
+ return g_mime_util.Get().IsMimeType(type_string);
+}
+
bool AreSupportedMediaCodecs(const std::vector<std::string>& codecs) {
return g_mime_util.Get().AreSupportedMediaCodecs(codecs);
}
« no previous file with comments | « net/base/mime_util.h ('k') | net/base/mime_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698