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

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

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: unique_ptr for V4GetHasProtocolManager. Fix the lone failing unit test. 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 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_
7 7
8 // A class that implements the stateless methods used by the GetHashUpdate and 8 // A class that implements the stateless methods used by the GetHashUpdate and
9 // GetFullHash stubby calls made by Chrome using the SafeBrowsing V4 protocol. 9 // GetFullHash stubby calls made by Chrome using the SafeBrowsing V4 protocol.
10 10
11 #include <ostream> 11 #include <ostream>
12 #include <string> 12 #include <string>
13 13
14 #include "base/gtest_prod_util.h" 14 #include "base/gtest_prod_util.h"
15 #include "base/hash.h" 15 #include "base/hash.h"
16 #include "base/strings/string_piece.h" 16 #include "base/strings/string_piece.h"
17 #include "components/safe_browsing_db/safebrowsing.pb.h" 17 #include "components/safe_browsing_db/safebrowsing.pb.h"
18 #include "net/url_request/url_request_status.h" 18 #include "net/url_request/url_request_status.h"
19 #include "url/gurl.h" 19 #include "url/gurl.h"
20 20
21 namespace net { 21 namespace net {
22 class HttpRequestHeaders; 22 class HttpRequestHeaders;
23 } // namespace net 23 } // namespace net
24 24
25 namespace safe_browsing { 25 namespace safe_browsing {
26 26
27 // The size of the hash prefix, in bytes. It should be between 4 to 32 (full
28 // hash).
29 typedef size_t PrefixSize;
30
31 // The minimum expected size (in bytes) of a hash-prefix.
32 const PrefixSize kMinHashPrefixLength = 4;
33
34 // The maximum expected size (in bytes) of a hash-prefix. This represents the
35 // length of a SHA256 hash.
36 const PrefixSize kMaxHashPrefixLength = 32;
37
27 // A hash prefix sent by the SafeBrowsing PVer4 service. 38 // A hash prefix sent by the SafeBrowsing PVer4 service.
28 typedef std::string HashPrefix; 39 typedef std::string HashPrefix;
29 40
30 // A full SHA256 hash. 41 // A full SHA256 hash.
31 typedef HashPrefix FullHash; 42 typedef HashPrefix FullHash;
32 43
33 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; 44 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest;
34 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; 45 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse;
35 46
36 // Config passed to the constructor of a V4 protocol manager. 47 // Config passed to the constructor of a V4 protocol manager.
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 bool operator==(const UpdateListIdentifier& other) const; 81 bool operator==(const UpdateListIdentifier& other) const;
71 bool operator!=(const UpdateListIdentifier& other) const; 82 bool operator!=(const UpdateListIdentifier& other) const;
72 size_t hash() const; 83 size_t hash() const;
73 84
74 private: 85 private:
75 UpdateListIdentifier(); 86 UpdateListIdentifier();
76 }; 87 };
77 88
78 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id); 89 std::ostream& operator<<(std::ostream& os, const UpdateListIdentifier& id);
79 90
91 PlatformType GetCurrentPlatformType();
80 const UpdateListIdentifier GetUrlMalwareId(); 92 const UpdateListIdentifier GetUrlMalwareId();
81 const UpdateListIdentifier GetUrlSocEngId(); 93 const UpdateListIdentifier GetUrlSocEngId();
82 94 const UpdateListIdentifier GetChromeUrlApiId();
83 // The set of interesting lists and ASCII filenames for their hash prefix
84 // stores. The stores are created inside the user-data directory.
85 // For instance, the UpdateListIdentifier could be for URL expressions for UwS
86 // on Windows platform, and the corresponding file on disk could be named:
87 // "uws_win_url.store"
88 // TODO(vakh): Find the canonical place where these are defined and update the
89 // comment to point to that place.
90 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap;
91 95
92 // Represents the state of each store. 96 // Represents the state of each store.
93 typedef base::hash_map<UpdateListIdentifier, std::string> StoreStateMap; 97 typedef base::hash_map<UpdateListIdentifier, std::string> StoreStateMap;
94 98
95 // Sever response, parsed in vector form. 99 // Sever response, parsed in vector form.
96 typedef std::vector<std::unique_ptr<ListUpdateResponse>> ParsedServerResponse; 100 typedef std::vector<std::unique_ptr<ListUpdateResponse>> ParsedServerResponse;
97 101
102 // TODO(vakh): Consider using a std::pair for this.
103 // Holds the hash prefix and the store that it matched in.
104 struct StoreAndHashPrefix {
105 public:
106 UpdateListIdentifier list_id;
107 HashPrefix hash_prefix;
108
109 explicit StoreAndHashPrefix(UpdateListIdentifier, HashPrefix);
110 ~StoreAndHashPrefix();
111
112 bool operator==(const StoreAndHashPrefix& other) const;
113 bool operator!=(const StoreAndHashPrefix& other) const;
114 size_t hash() const;
115
116 private:
117 StoreAndHashPrefix();
118 };
119
120 typedef std::vector<StoreAndHashPrefix> StoreAndHashPrefixes;
121
98 // Enumerate failures for histogramming purposes. DO NOT CHANGE THE 122 // Enumerate failures for histogramming purposes. DO NOT CHANGE THE
99 // ORDERING OF THESE VALUES. 123 // ORDERING OF THESE VALUES.
100 enum V4OperationResult { 124 enum V4OperationResult {
101 // 200 response code means that the server recognized the request. 125 // 200 response code means that the server recognized the request.
102 STATUS_200 = 0, 126 STATUS_200 = 0,
103 127
104 // Subset of successful responses where the response body wasn't parsable. 128 // Subset of successful responses where the response body wasn't parsable.
105 PARSE_ERROR = 1, 129 PARSE_ERROR = 1,
106 130
107 // Operation request failed (network error). 131 // Operation request failed (network error).
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 // error code against, |status| represents the status of the HTTP request, and 200 // error code against, |status| represents the status of the HTTP request, and
177 // |response code| represents the HTTP response code received from the server. 201 // |response code| represents the HTTP response code received from the server.
178 static void RecordHttpResponseOrErrorCode(const char* metric_name, 202 static void RecordHttpResponseOrErrorCode(const char* metric_name,
179 const net::URLRequestStatus& status, 203 const net::URLRequestStatus& status,
180 int response_code); 204 int response_code);
181 205
182 // Generate the set of FullHashes to check for |url|. 206 // Generate the set of FullHashes to check for |url|.
183 static void UrlToFullHashes(const GURL& url, 207 static void UrlToFullHashes(const GURL& url,
184 base::hash_set<FullHash>* full_hashes); 208 base::hash_set<FullHash>* full_hashes);
185 209
210 static bool FullHashToHashPrefix(const FullHash& full_hash,
211 PrefixSize prefix_size,
212 HashPrefix* hash_prefix);
213
214 static bool FullHashToSmallestHashPrefix(const FullHash& full_hash,
215 HashPrefix* hash_prefix);
216
217 static bool FullHashMatchesHashPrefix(const FullHash& full_hash,
218 const HashPrefix& hash_prefix);
219
186 private: 220 private:
187 V4ProtocolManagerUtil(){}; 221 V4ProtocolManagerUtil(){};
188 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, TestBackOffLogic); 222 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, TestBackOffLogic);
189 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, 223 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest,
190 TestGetRequestUrlAndUpdateHeaders); 224 TestGetRequestUrlAndUpdateHeaders);
191 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, UrlParsing); 225 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, UrlParsing);
192 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, CanonicalizeUrl); 226 FRIEND_TEST_ALL_PREFIXES(V4ProtocolManagerUtilTest, CanonicalizeUrl);
193 227
194 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes). 228 // Composes a URL using |prefix|, |method| (e.g.: encodedFullHashes).
195 // |request_base64|, |client_id|, |version| and |key_param|. |prefix| 229 // |request_base64|, |client_id|, |version| and |key_param|. |prefix|
(...skipping 18 matching lines...) Expand all
214 static std::string RemoveConsecutiveChars(base::StringPiece str, 248 static std::string RemoveConsecutiveChars(base::StringPiece str,
215 const char c); 249 const char c);
216 250
217 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil); 251 DISALLOW_COPY_AND_ASSIGN(V4ProtocolManagerUtil);
218 }; 252 };
219 253
220 } // namespace safe_browsing 254 } // namespace safe_browsing
221 255
222 namespace std { 256 namespace std {
223 template <> 257 template <>
258 struct hash<safe_browsing::PlatformType> {
259 std::size_t operator()(const safe_browsing::PlatformType& p) const {
260 return std::hash<unsigned int>()(p);
261 }
262 };
263
264 template <>
265 struct hash<safe_browsing::ThreatEntryType> {
266 std::size_t operator()(const safe_browsing::ThreatEntryType& tet) const {
267 return std::hash<unsigned int>()(tet);
268 }
269 };
270
271 template <>
272 struct hash<safe_browsing::ThreatType> {
273 std::size_t operator()(const safe_browsing::ThreatType& tt) const {
274 return std::hash<unsigned int>()(tt);
275 }
276 };
277
278 template <>
279 struct hash<safe_browsing::StoreAndHashPrefix> {
280 std::size_t operator()(const safe_browsing::StoreAndHashPrefix& s) const {
281 return s.hash();
282 }
283 };
284
285 template <>
224 struct hash<safe_browsing::UpdateListIdentifier> { 286 struct hash<safe_browsing::UpdateListIdentifier> {
225 std::size_t operator()(const safe_browsing::UpdateListIdentifier& s) const { 287 std::size_t operator()(const safe_browsing::UpdateListIdentifier& id) const {
226 return s.hash(); 288 return id.hash();
227 } 289 }
228 }; 290 };
229 } 291 }
230 292
231 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_ 293 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_PROTOCOL_MANAGER_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698