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

Unified Diff: net/base/mime_sniffer.cc

Issue 159345: Implement mimetype sniffing for extensions. (Closed)
Patch Set: rebase Created 11 years, 5 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 | « chrome/common/extensions/extension_unittest.cc ('k') | net/base/mime_sniffer_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/base/mime_sniffer.cc
diff --git a/net/base/mime_sniffer.cc b/net/base/mime_sniffer.cc
index 0a8bc5760d314268ef06d78ab80b931639e2c763..d464086545873637586e9a6ac270c3a84858d053 100644
--- a/net/base/mime_sniffer.cc
+++ b/net/base/mime_sniffer.cc
@@ -256,7 +256,7 @@ static bool CheckForMagicNumbers(const char* content, size_t size,
Histogram* counter, std::string* result) {
for (size_t i = 0; i < magic_len; ++i) {
if (MatchMagicNumber(content, size, &(magic[i]), result)) {
- counter->Add(static_cast<int>(i));
+ if (counter) counter->Add(static_cast<int>(i));
return true;
}
}
@@ -438,6 +438,43 @@ static bool IsUnknownMimeType(const std::string& mime_type) {
return false;
}
+// Sniff a crx (chrome extension) file.
+static bool SniffCRX(const char* content, size_t content_size, const GURL& url,
+ const std::string& type_hint, std::string* result) {
+ static SnifferHistogram counter("mime_sniffer.kSniffCRX", 3);
+
+ // Technically, the crx magic number is just Cr24, but the bytes after that
+ // are a version number which changes infrequently. Including it in the
+ // sniffing gives us less room for error. If the version number ever changes,
+ // we can just add an entry to this list.
+ //
+ // TODO(aa): If we ever have another magic number, we'll want to pass a
+ // histogram into CheckForMagicNumbers(), below, to see which one matched.
+ const struct MagicNumber kCRXMagicNumbers[] = {
+ MAGIC_NUMBER("application/x-chrome-extension", "Cr24\x02\x00\x00\x00")
+ };
+
+ // Only consider files that have the extension ".crx".
+ const char kCRXExtension[] = ".crx";
+ const int kExtensionLength = arraysize(kCRXExtension) - 1; // ignore null
+ if (url.path().rfind(kCRXExtension, std::string::npos, kExtensionLength) ==
+ url.path().size() - kExtensionLength) {
+ counter.Add(1);
+ } else {
+ return false;
+ }
+
+ if (CheckForMagicNumbers(content, content_size,
+ kCRXMagicNumbers, arraysize(kCRXMagicNumbers),
+ NULL, result)) {
+ counter.Add(2);
+ } else {
+ return false;
+ }
+
+ return true;
+}
+
bool ShouldSniffMimeType(const GURL& url, const std::string& mime_type) {
static SnifferHistogram should_sniff_counter(
"mime_sniffer.ShouldSniffMimeType2", 3);
@@ -463,7 +500,7 @@ bool ShouldSniffMimeType(const GURL& url, const std::string& mime_type) {
// their more specific mime types.
"text/xml",
"application/xml",
- };
+ };
static SnifferHistogram counter("mime_sniffer.kSniffableTypes2",
arraysize(kSniffableTypes) + 1);
for (size_t i = 0; i < arraysize(kSniffableTypes); ++i) {
@@ -535,6 +572,11 @@ bool SniffMimeType(const char* content, size_t content_size,
return content_size >= kMaxBytesToSniff;
}
+ // CRX files (chrome extensions) have a special sniffing algorithm. It is
+ // tighter than the others because we don't have to match legacy behavior.
+ if (SniffCRX(content, content_size, url, type_hint, result))
+ return true;
+
// Now we look in our large table of magic numbers to see if we can find
// anything that matches the content.
if (SniffForMagicNumbers(content, content_size, result))
« no previous file with comments | « chrome/common/extensions/extension_unittest.cc ('k') | net/base/mime_sniffer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698