OLD | NEW |
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_STORE_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
7 | 7 |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 // A hash prefix sent by the SafeBrowsing PVer4 service. | 25 // A hash prefix sent by the SafeBrowsing PVer4 service. |
26 typedef std::string HashPrefix; | 26 typedef std::string HashPrefix; |
27 | 27 |
28 // The sorted list of hash prefixes. | 28 // The sorted list of hash prefixes. |
29 typedef std::string HashPrefixes; | 29 typedef std::string HashPrefixes; |
30 | 30 |
31 // Stores the list of sorted hash prefixes, by size. | 31 // Stores the list of sorted hash prefixes, by size. |
32 // For instance: {4: ["abcd", "bcde", "cdef", "gggg"], 5: ["fffff"]} | 32 // For instance: {4: ["abcd", "bcde", "cdef", "gggg"], 5: ["fffff"]} |
33 typedef base::hash_map<PrefixSize, HashPrefixes> HashPrefixMap; | 33 typedef base::hash_map<PrefixSize, HashPrefixes> HashPrefixMap; |
34 | 34 |
35 // Stores the index of the last element merged from the HashPrefixMap for a | 35 // Stores the iterator to the last element merged from the HashPrefixMap for a |
36 // given prefix size. For instance: {4:3, 5:1} means that we have already merged | 36 // given prefix size. |
| 37 // For instance: {4:iter(3), 5:iter(1)} means that we have already merged |
37 // 3 hash prefixes of length 4, and 1 hash prefix of length 5. | 38 // 3 hash prefixes of length 4, and 1 hash prefix of length 5. |
38 typedef base::hash_map<PrefixSize, size_t> CounterMap; | 39 typedef base::hash_map<PrefixSize, HashPrefixes::const_iterator> IteratorMap; |
39 | 40 |
40 // Enumerate different failure events while parsing the file read from disk for | 41 // Enumerate different failure events while parsing the file read from disk for |
41 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. | 42 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. |
42 enum StoreReadResult { | 43 enum StoreReadResult { |
43 // No errors. | 44 // No errors. |
44 READ_SUCCESS = 0, | 45 READ_SUCCESS = 0, |
45 | 46 |
46 // Reserved for errors in parsing this enum. | 47 // Reserved for errors in parsing this enum. |
47 UNEXPECTED_READ_FAILURE = 1, | 48 UNEXPECTED_READ_FAILURE = 1, |
48 | 49 |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 TestReadFullResponseInWithValidHashPrefixMap); | 203 TestReadFullResponseInWithValidHashPrefixMap); |
203 | 204 |
204 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, | 205 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, |
205 // then it sets |raw_hashes| as the value at key |prefix_size| in | 206 // then it sets |raw_hashes| as the value at key |prefix_size| in |
206 // |additions_map| | 207 // |additions_map| |
207 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, | 208 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, |
208 const std::string& raw_hashes, | 209 const std::string& raw_hashes, |
209 HashPrefixMap* additions_map); | 210 HashPrefixMap* additions_map); |
210 | 211 |
211 // Get the size of the next unmerged hash prefix in dictionary order from | 212 // Get the size of the next unmerged hash prefix in dictionary order from |
212 // |hash_prefix_map|. |counter_map| is used to determine which hash prefixes | 213 // |hash_prefix_map|. |iterator_map| is used to determine which hash prefixes |
213 // have been merged already. Returns true if there are any unmerged hash | 214 // have been merged already. Returns true if there are any unmerged hash |
214 // prefixes in the list. | 215 // prefixes in the list. |
215 static bool GetNextSmallestPrefixSize(const HashPrefixMap& hash_prefix_map, | 216 static bool GetNextSmallestPrefixSize(const HashPrefixMap& hash_prefix_map, |
216 const CounterMap& counter_map, | 217 const IteratorMap& iterator_map, |
217 PrefixSize* smallest_prefix_size); | 218 PrefixSize* smallest_prefix_size); |
218 | 219 |
219 // Returns the next hash prefix of length |prefix_size| from |hash_prefix_map| | 220 // Returns the next hash prefix of length |prefix_size| from |hash_prefix_map| |
220 // that hasn't been merged already. |counter_map| is used to determine the | 221 // that hasn't been merged already. |iterator_map| is used to determine the |
221 // index of the next prefix of size |prefix_size| to merge. | 222 // index of the next prefix of size |prefix_size| to merge. |
222 static HashPrefix GetNextUnmergedPrefixForSize( | 223 static HashPrefix GetNextUnmergedPrefixForSize( |
223 PrefixSize prefix_size, | 224 PrefixSize prefix_size, |
224 const HashPrefixMap& hash_prefix_map, | 225 const HashPrefixMap& hash_prefix_map, |
225 const CounterMap& counter_map); | 226 const IteratorMap& iterator_map); |
226 | 227 |
227 // Sets a value of 0 in |counter_map| for all keys in |hash_prefix_map|. | 228 // Sets a value of 0 in |iterator_map| for all keys in |hash_prefix_map|. |
228 static void InitializeCounterMap(const HashPrefixMap& hash_prefix_map, | 229 static void InitializeIteratorMap(const HashPrefixMap& hash_prefix_map, |
229 CounterMap* counter_map); | 230 IteratorMap* iterator_map); |
230 | 231 |
231 // Reserve the appropriate string size so that the string size of the merged | 232 // Reserve the appropriate string size so that the string size of the merged |
232 // list is exact. This ignores the space that would otherwise be released by | 233 // list is exact. This ignores the space that would otherwise be released by |
233 // deletions specified in the update because it is non-trivial to calculate | 234 // deletions specified in the update because it is non-trivial to calculate |
234 // those deletions upfront. This isn't so bad since deletions are supposed to | 235 // those deletions upfront. This isn't so bad since deletions are supposed to |
235 // be small and infrequent. | 236 // be small and infrequent. |
236 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, | 237 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, |
237 HashPrefixMap* prefix_map_to_update); | 238 HashPrefixMap* prefix_map_to_update); |
238 | 239 |
239 // Updates the |additions_map| with the additions received in the partial | 240 // Updates the |additions_map| with the additions received in the partial |
(...skipping 22 matching lines...) Expand all Loading... |
262 const base::FilePath store_path_; | 263 const base::FilePath store_path_; |
263 HashPrefixMap hash_prefix_map_; | 264 HashPrefixMap hash_prefix_map_; |
264 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 265 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
265 }; | 266 }; |
266 | 267 |
267 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 268 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
268 | 269 |
269 } // namespace safe_browsing | 270 } // namespace safe_browsing |
270 | 271 |
271 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 272 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
OLD | NEW |