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

Side by Side Diff: components/safe_browsing_db/v4_protocol_manager_util.cc

Issue 2233103002: Move full hash caching logic to v4_get_hash_protocol_manager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Bring back the histogram to check if there were any hits in the response from the server Created 4 years, 3 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/safe_browsing_db/v4_protocol_manager_util.h" 5 #include "components/safe_browsing_db/v4_protocol_manager_util.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/metrics/sparse_histogram.h" 8 #include "base/metrics/sparse_histogram.h"
9 #include "base/rand_util.h" 9 #include "base/rand_util.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
61 61
62 } // namespace 62 } // namespace
63 63
64 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id) { 64 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id) {
65 os << "{hash: " << id.hash() << "; platform_type: " << id.platform_type 65 os << "{hash: " << id.hash() << "; platform_type: " << id.platform_type
66 << "; threat_entry_type: " << id.threat_entry_type 66 << "; threat_entry_type: " << id.threat_entry_type
67 << "; threat_type: " << id.threat_type << "}"; 67 << "; threat_type: " << id.threat_type << "}";
68 return os; 68 return os;
69 } 69 }
70 70
71 PlatformType GetCurrentPlatformType() {
71 #if defined(OS_WIN) 72 #if defined(OS_WIN)
72 #define PLATFORM_TYPE WINDOWS_PLATFORM 73 return WINDOWS_PLATFORM;
73 #elif defined(OS_LINUX) 74 #elif defined(OS_LINUX)
74 #define PLATFORM_TYPE LINUX_PLATFORM 75 return LINUX_PLATFORM;
75 #elif defined(OS_MACOSX) 76 #elif defined(OS_MACOSX)
76 #define PLATFORM_TYPE OSX_PLATFORM 77 return OSX_PLATFORM;
77 #else 78 #else
78 // This should ideally never compile but it is getting compiled on Android. 79 // This should ideally never compile but it is getting compiled on Android.
79 // See: https://bugs.chromium.org/p/chromium/issues/detail?id=621647 80 // See: https://bugs.chromium.org/p/chromium/issues/detail?id=621647
80 // TODO(vakh): Once that bug is fixed, this should be removed. If we leave 81 // TODO(vakh): Once that bug is fixed, this should be removed. If we leave
81 // the platform_type empty, the server won't recognize the request and 82 // the platform_type empty, the server won't recognize the request and
82 // return an error response which will pollute our UMA metrics. 83 // return an error response which will pollute our UMA metrics.
83 #define PLATFORM_TYPE LINUX_PLATFORM 84 return LINUX_PLATFORM;
84 #endif 85 #endif
86 }
85 87
86 const UpdateListIdentifier GetUrlMalwareId() { 88 const UpdateListIdentifier GetUrlMalwareId() {
87 return UpdateListIdentifier(PLATFORM_TYPE, URL, MALWARE_THREAT); 89 return UpdateListIdentifier(GetCurrentPlatformType(), URL, MALWARE_THREAT);
88 } 90 }
89 91
90 const UpdateListIdentifier GetUrlSocEngId() { 92 const UpdateListIdentifier GetUrlSocEngId() {
91 return UpdateListIdentifier(PLATFORM_TYPE, URL, SOCIAL_ENGINEERING_PUBLIC); 93 return UpdateListIdentifier(GetCurrentPlatformType(), URL,
94 SOCIAL_ENGINEERING_PUBLIC);
95 }
96
97 const UpdateListIdentifier GetChromeUrlApiId() {
98 return UpdateListIdentifier(CHROME_PLATFORM, URL, API_ABUSE);
92 } 99 }
93 100
94 // The Safe Browsing V4 server URL prefix. 101 // The Safe Browsing V4 server URL prefix.
95 const char kSbV4UrlPrefix[] = "https://safebrowsing.googleapis.com/v4"; 102 const char kSbV4UrlPrefix[] = "https://safebrowsing.googleapis.com/v4";
96 103
104 StoreAndHashPrefix::StoreAndHashPrefix(UpdateListIdentifier list_id,
105 HashPrefix hash_prefix)
106 : list_id(list_id), hash_prefix(hash_prefix) {}
107
108 StoreAndHashPrefix::~StoreAndHashPrefix() {}
109
110 bool StoreAndHashPrefix::operator==(const StoreAndHashPrefix& other) const {
111 return list_id == other.list_id && hash_prefix == other.hash_prefix;
112 }
113
114 bool StoreAndHashPrefix::operator!=(const StoreAndHashPrefix& other) const {
115 return !operator==(other);
116 }
117
118 size_t StoreAndHashPrefix::hash() const {
119 std::size_t first = list_id.hash();
120 std::size_t second = std::hash<std::string>()(hash_prefix);
121
122 return base::HashInts(first, second);
123 }
124
97 bool UpdateListIdentifier::operator==(const UpdateListIdentifier& other) const { 125 bool UpdateListIdentifier::operator==(const UpdateListIdentifier& other) const {
98 return platform_type == other.platform_type && 126 return platform_type == other.platform_type &&
99 threat_entry_type == other.threat_entry_type && 127 threat_entry_type == other.threat_entry_type &&
100 threat_type == other.threat_type; 128 threat_type == other.threat_type;
101 } 129 }
102 130
103 bool UpdateListIdentifier::operator!=(const UpdateListIdentifier& other) const { 131 bool UpdateListIdentifier::operator!=(const UpdateListIdentifier& other) const {
104 return !operator==(other); 132 return !operator==(other);
105 } 133 }
106 134
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 std::vector<std::string> paths; 251 std::vector<std::string> paths;
224 GeneratePathVariantsToCheck(canon_path, canon_query, &paths); 252 GeneratePathVariantsToCheck(canon_path, canon_query, &paths);
225 for (const std::string& host : hosts) { 253 for (const std::string& host : hosts) {
226 for (const std::string& path : paths) { 254 for (const std::string& path : paths) {
227 full_hashes->insert(crypto::SHA256HashString(host + path)); 255 full_hashes->insert(crypto::SHA256HashString(host + path));
228 } 256 }
229 } 257 }
230 } 258 }
231 259
232 // static 260 // static
261 bool V4ProtocolManagerUtil::FullHashToHashPrefix(const FullHash& full_hash,
262 PrefixSize prefix_size,
263 HashPrefix* hash_prefix) {
264 if (full_hash.size() < prefix_size) {
265 return false;
266 }
267 *hash_prefix = full_hash.substr(prefix_size);
268 return true;
269 }
270
271 // static
272 bool V4ProtocolManagerUtil::FullHashToSmallestHashPrefix(
273 const FullHash& full_hash,
274 HashPrefix* hash_prefix) {
275 return FullHashToHashPrefix(full_hash, kMinHashPrefixLength, hash_prefix);
276 }
277
278 // static
279 bool V4ProtocolManagerUtil::FullHashMatchesHashPrefix(
280 const FullHash& full_hash,
281 const HashPrefix& hash_prefix) {
282 return full_hash.compare(0, hash_prefix.length(), hash_prefix) == 0;
283 }
284
285 // static
233 void V4ProtocolManagerUtil::GenerateHostsToCheck( 286 void V4ProtocolManagerUtil::GenerateHostsToCheck(
234 const GURL& url, 287 const GURL& url,
235 std::vector<std::string>* hosts) { 288 std::vector<std::string>* hosts) {
236 std::string canon_host; 289 std::string canon_host;
237 CanonicalizeUrl(url, &canon_host, NULL, NULL); 290 CanonicalizeUrl(url, &canon_host, NULL, NULL);
238 GenerateHostVariantsToCheck(canon_host, hosts); 291 GenerateHostVariantsToCheck(canon_host, hosts);
239 } 292 }
240 293
241 // static 294 // static
242 void V4ProtocolManagerUtil::GeneratePathsToCheck( 295 void V4ProtocolManagerUtil::GeneratePathsToCheck(
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
437 } 490 }
438 491
439 if (!paths->empty() && paths->back() != path) 492 if (!paths->empty() && paths->back() != path)
440 paths->push_back(path); 493 paths->push_back(path);
441 494
442 if (!query.empty()) 495 if (!query.empty())
443 paths->push_back(path + "?" + query); 496 paths->push_back(path + "?" + query);
444 } 497 }
445 498
446 } // namespace safe_browsing 499 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698