Index: chrome/browser/safe_browsing/database_manager.cc |
diff --git a/chrome/browser/safe_browsing/database_manager.cc b/chrome/browser/safe_browsing/database_manager.cc |
index 7a44db9a29e242bd78f4a3435592b087f6a61485..1a1004bd0bfa145a0858abea34fb85a098718d78 100644 |
--- a/chrome/browser/safe_browsing/database_manager.cc |
+++ b/chrome/browser/safe_browsing/database_manager.cc |
@@ -68,30 +68,71 @@ bool IsExpectedThreat( |
threat_type); |
} |
-// |list_id| is from |safe_browsing_util::ListType|. |
-SBThreatType GetThreatTypeFromListId(int list_id) { |
- if (list_id == safe_browsing_util::PHISH) { |
- return SB_THREAT_TYPE_URL_PHISHING; |
+// Return the list id from the first result in |full_hashes| which matches |
+// |hash|, or INVALID if none match. |
+safe_browsing_util::ListType GetHashThreatListType( |
+ const SBFullHash& hash, |
+ const std::vector<SBFullHashResult>& full_hashes) { |
+ for (size_t i = 0; i < full_hashes.size(); ++i) { |
+ if (SBFullHashEqual(hash, full_hashes[i].hash)) |
+ return static_cast<safe_browsing_util::ListType>(full_hashes[i].list_id); |
} |
+ return safe_browsing_util::INVALID; |
+} |
- if (list_id == safe_browsing_util::MALWARE) { |
- return SB_THREAT_TYPE_URL_MALWARE; |
- } |
+// Given a URL, compare all the possible host + path full hashes to the set of |
+// provided full hashes. Returns the list id of the a matching result from |
+// |full_hashes|, or INVALID if none match. |
+safe_browsing_util::ListType GetUrlThreatListType( |
+ const GURL& url, |
+ const std::vector<SBFullHashResult>& full_hashes) { |
+ if (full_hashes.empty()) |
+ return safe_browsing_util::INVALID; |
- if (list_id == safe_browsing_util::BINURL) { |
- return SB_THREAT_TYPE_BINARY_MALWARE_URL; |
- } |
+ std::vector<std::string> patterns; |
+ safe_browsing_util::GeneratePatternsToCheck(url, &patterns); |
- if (list_id == safe_browsing_util::EXTENSIONBLACKLIST) { |
- return SB_THREAT_TYPE_EXTENSION; |
+ for (size_t i = 0; i < patterns.size(); ++i) { |
+ safe_browsing_util::ListType threat = |
+ GetHashThreatListType(SBFullHashForString(patterns[i]), full_hashes); |
+ if (threat != safe_browsing_util::INVALID) |
+ return threat; |
} |
+ return safe_browsing_util::INVALID; |
+} |
- DVLOG(1) << "Unknown safe browsing list id " << list_id; |
- return SB_THREAT_TYPE_SAFE; |
+SBThreatType GetThreatTypeFromListType(safe_browsing_util::ListType list_type) { |
+ switch (list_type) { |
+ case safe_browsing_util::PHISH: |
+ return SB_THREAT_TYPE_URL_PHISHING; |
+ case safe_browsing_util::MALWARE: |
+ return SB_THREAT_TYPE_URL_MALWARE; |
+ case safe_browsing_util::BINURL: |
+ return SB_THREAT_TYPE_BINARY_MALWARE_URL; |
+ case safe_browsing_util::EXTENSIONBLACKLIST: |
+ return SB_THREAT_TYPE_EXTENSION; |
+ default: |
+ DVLOG(1) << "Unknown safe browsing list id " << list_type; |
+ return SB_THREAT_TYPE_SAFE; |
+ } |
} |
} // namespace |
+// static |
+SBThreatType SafeBrowsingDatabaseManager::GetHashThreatType( |
+ const SBFullHash& hash, |
+ const std::vector<SBFullHashResult>& full_hashes) { |
+ return GetThreatTypeFromListType(GetHashThreatListType(hash, full_hashes)); |
+} |
+ |
+// static |
+SBThreatType SafeBrowsingDatabaseManager::GetUrlThreatType( |
+ const GURL& url, |
+ const std::vector<SBFullHashResult>& full_hashes) { |
+ return GetThreatTypeFromListType(GetUrlThreatListType(url, full_hashes)); |
+} |
+ |
SafeBrowsingDatabaseManager::SafeBrowsingCheck::SafeBrowsingCheck( |
const std::vector<GURL>& urls, |
const std::vector<SBFullHash>& full_hashes, |
@@ -881,13 +922,19 @@ bool SafeBrowsingDatabaseManager::HandleOneCheck( |
bool is_threat = false; |
+ // TODO(shess): GetHashThreadListType() contains a loop, |
+ // GetUrlThreatListType() a loop around that loop. Having another loop out |
+ // here concerns me. It is likely that SAFE is an expected outcome, which |
+ // means all of those loops run to completion. Refactoring this to generate a |
+ // set of sorted items to compare in sequence would probably improve things. |
+ // |
+ // Additionally, the set of patterns generated from the urls is very similar |
+ // to the patterns generated in ContainsBrowseUrl() and other database checks, |
+ // which are called from this code. Refactoring that across the checks could |
+ // interact well with batching the checks here. |
+ |
for (size_t i = 0; i < check->urls.size(); ++i) { |
- int index = |
- safe_browsing_util::GetUrlHashIndex(check->urls[i], full_hashes); |
- if (index == -1) |
- continue; |
- SBThreatType threat = |
- GetThreatTypeFromListId(full_hashes[index].list_id); |
+ SBThreatType threat = GetUrlThreatType(check->urls[i], full_hashes); |
if (threat != SB_THREAT_TYPE_SAFE && |
IsExpectedThreat(threat, check->expected_threats)) { |
check->url_results[i] = threat; |
@@ -896,12 +943,7 @@ bool SafeBrowsingDatabaseManager::HandleOneCheck( |
} |
for (size_t i = 0; i < check->full_hashes.size(); ++i) { |
- int index = |
- safe_browsing_util::GetHashIndex(check->full_hashes[i], full_hashes); |
- if (index == -1) |
- continue; |
- SBThreatType threat = |
- GetThreatTypeFromListId(full_hashes[index].list_id); |
+ SBThreatType threat = GetHashThreatType(check->full_hashes[i], full_hashes); |
if (threat != SB_THREAT_TYPE_SAFE && |
IsExpectedThreat(threat, check->expected_threats)) { |
check->full_hash_results[i] = threat; |