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 iterator to the last element merged from the HashPrefixMap for a | 35 // Stores the index of the last element merged from the HashPrefixMap for a |
36 // given prefix size. | 36 // given prefix size. For instance: {4:3, 5:1} means that we have already merged |
37 // For instance: {4:iter(3), 5:iter(1)} means that we have already merged | |
38 // 3 hash prefixes of length 4, and 1 hash prefix of length 5. | 37 // 3 hash prefixes of length 4, and 1 hash prefix of length 5. |
39 typedef base::hash_map<PrefixSize, HashPrefixes::const_iterator> IteratorMap; | 38 typedef base::hash_map<PrefixSize, size_t> CounterMap; |
40 | 39 |
41 // Enumerate different failure events while parsing the file read from disk for | 40 // Enumerate different failure events while parsing the file read from disk for |
42 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. | 41 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. |
43 enum StoreReadResult { | 42 enum StoreReadResult { |
44 // No errors. | 43 // No errors. |
45 READ_SUCCESS = 0, | 44 READ_SUCCESS = 0, |
46 | 45 |
47 // Reserved for errors in parsing this enum. | 46 // Reserved for errors in parsing this enum. |
48 UNEXPECTED_READ_FAILURE = 1, | 47 UNEXPECTED_READ_FAILURE = 1, |
49 | 48 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); | 178 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); |
180 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); | 179 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); |
181 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); | 180 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); |
182 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); | 181 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); |
183 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); | 182 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); |
184 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 183 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
185 TestAddUnlumpedHashesWithInvalidAddition); | 184 TestAddUnlumpedHashesWithInvalidAddition); |
186 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes); | 185 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes); |
187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString); | 186 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString); |
188 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
189 TestGetNextSmallestUnmergedPrefixWithEmptyPrefixMap); | 188 TestGetNextSmallestPrefixSizeWithEmptyPrefixMap); |
190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestUnmergedPrefix); | 189 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestPrefixSize); |
| 190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextUnmergedPrefix); |
191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesWithSameSizesInEachMap); | 191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesWithSameSizesInEachMap); |
192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
193 TestMergeUpdatesWithDifferentSizesInEachMap); | 193 TestMergeUpdatesWithDifferentSizesInEachMap); |
194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesOldMapRunsOutFirst); | 194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesOldMapRunsOutFirst); |
195 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 195 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
196 TestMergeUpdatesAdditionsMapRunsOutFirst); | 196 TestMergeUpdatesAdditionsMapRunsOutFirst); |
197 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 197 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
198 TestMergeUpdatesFailsForRepeatedHashPrefix); | 198 TestMergeUpdatesFailsForRepeatedHashPrefix); |
199 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 199 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
200 TestReadFullResponseWithValidHashPrefixMap); | 200 TestReadFullResponseWithValidHashPrefixMap); |
201 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 201 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
202 TestReadFullResponseWithInvalidHashPrefixMap); | 202 TestReadFullResponseWithInvalidHashPrefixMap); |
203 | 203 |
204 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, | 204 // 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 | 205 // then it sets |raw_hashes| as the value at key |prefix_size| in |
206 // |additions_map| | 206 // |additions_map| |
207 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, | 207 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, |
208 const std::string& raw_hashes, | 208 const std::string& raw_hashes, |
209 HashPrefixMap* additions_map); | 209 HashPrefixMap* additions_map); |
210 | 210 |
211 // Get the next unmerged hash prefix in dictionary order from | 211 // Get the size of the next unmerged hash prefix in dictionary order from |
212 // |hash_prefix_map|. |iterator_map| is used to determine which hash prefixes | 212 // |hash_prefix_map|. |counter_map| is used to determine which hash prefixes |
213 // have been merged already. Returns true if there are any unmerged hash | 213 // have been merged already. Returns true if there are any unmerged hash |
214 // prefixes in the list. | 214 // prefixes in the list. |
215 static bool GetNextSmallestUnmergedPrefix( | 215 static bool GetNextSmallestPrefixSize(const HashPrefixMap& hash_prefix_map, |
| 216 const CounterMap& counter_map, |
| 217 PrefixSize* smallest_prefix_size); |
| 218 |
| 219 // 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 // index of the next prefix of size |prefix_size| to merge. |
| 222 static HashPrefix GetNextUnmergedPrefixForSize( |
| 223 PrefixSize prefix_size, |
216 const HashPrefixMap& hash_prefix_map, | 224 const HashPrefixMap& hash_prefix_map, |
217 const IteratorMap& iterator_map, | 225 const CounterMap& counter_map); |
218 HashPrefix* smallest_hash_prefix); | |
219 | 226 |
220 // For each key in |hash_prefix_map|, sets the iterator at that key | 227 // Sets a value of 0 in |counter_map| for all keys in |hash_prefix_map|. |
221 // |iterator_map| to hash_prefix_map[key].begin(). | 228 static void InitializeCounterMap(const HashPrefixMap& hash_prefix_map, |
222 static void InitializeIteratorMap(const HashPrefixMap& hash_prefix_map, | 229 CounterMap* counter_map); |
223 IteratorMap* iterator_map); | |
224 | 230 |
225 // Reserve the appropriate string size so that the string size of the merged | 231 // Reserve the appropriate string size so that the string size of the merged |
226 // list is exact. This ignores the space that would otherwise be released by | 232 // list is exact. This ignores the space that would otherwise be released by |
227 // deletions specified in the update because it is non-trivial to calculate | 233 // deletions specified in the update because it is non-trivial to calculate |
228 // those deletions upfront. This isn't so bad since deletions are supposed to | 234 // those deletions upfront. This isn't so bad since deletions are supposed to |
229 // be small and infrequent. | 235 // be small and infrequent. |
230 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, | 236 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, |
231 HashPrefixMap* prefix_map_to_update); | 237 HashPrefixMap* prefix_map_to_update); |
232 | 238 |
233 // Updates the |additions_map| with the additions received in the partial | 239 // Updates the |additions_map| with the additions received in the partial |
(...skipping 22 matching lines...) Expand all Loading... |
256 const base::FilePath store_path_; | 262 const base::FilePath store_path_; |
257 HashPrefixMap hash_prefix_map_; | 263 HashPrefixMap hash_prefix_map_; |
258 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 264 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
259 }; | 265 }; |
260 | 266 |
261 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 267 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
262 | 268 |
263 } // namespace safe_browsing | 269 } // namespace safe_browsing |
264 | 270 |
265 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 271 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
OLD | NEW |