Chromium Code Reviews| 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 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); | 179 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); |
| 179 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); | 180 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); |
| 180 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); | 181 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); |
| 181 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); | 182 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); |
| 182 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); | 183 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); |
| 183 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 184 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 184 TestAddUnlumpedHashesWithInvalidAddition); | 185 TestAddUnlumpedHashesWithInvalidAddition); |
| 185 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes); | 186 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes); |
| 186 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString); | 187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString); |
| 187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 188 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 188 TestGetNextSmallestPrefixSizeWithEmptyPrefixMap); | 189 TestGetNextSmallestUnmergedPrefixWithEmptyPrefixMap); |
| 189 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestPrefixSize); | 190 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestUnmergedPrefix); |
| 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 size of the next unmerged hash prefix in dictionary order from | 211 // Get the next unmerged hash prefix in dictionary order from |
| 212 // |hash_prefix_map|. |counter_map| is used to determine which hash prefixes | 212 // |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 | 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 GetNextSmallestPrefixSize(const HashPrefixMap& hash_prefix_map, | 215 static bool GetNextSmallestUnmergedPrefix( |
| 216 const CounterMap& counter_map, | 216 const HashPrefixMap& hash_prefix_map, |
| 217 PrefixSize* smallest_prefix_size); | 217 const IteratorMap& iterator_map, |
| 218 HashPrefix* smallest_hash_prefix); | |
| 218 | 219 |
| 219 // Returns the next hash prefix of length |prefix_size| from |hash_prefix_map| | 220 // Sets a value of 0 in |iterator_map| for all keys in |hash_prefix_map|. |
|
Nathan Parker
2016/07/15 00:39:48
This isn't zero, since it's an iterator, ya?
| |
| 220 // that hasn't been merged already. |counter_map| is used to determine the | 221 static void InitializeIteratorMap(const HashPrefixMap& hash_prefix_map, |
| 221 // index of the next prefix of size |prefix_size| to merge. | 222 IteratorMap* iterator_map); |
| 222 static HashPrefix GetNextUnmergedPrefixForSize( | |
| 223 PrefixSize prefix_size, | |
| 224 const HashPrefixMap& hash_prefix_map, | |
| 225 const CounterMap& counter_map); | |
| 226 | |
| 227 // Sets a value of 0 in |counter_map| for all keys in |hash_prefix_map|. | |
| 228 static void InitializeCounterMap(const HashPrefixMap& hash_prefix_map, | |
| 229 CounterMap* counter_map); | |
| 230 | 223 |
| 231 // Reserve the appropriate string size so that the string size of the merged | 224 // 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 | 225 // 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 | 226 // 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 | 227 // those deletions upfront. This isn't so bad since deletions are supposed to |
| 235 // be small and infrequent. | 228 // be small and infrequent. |
| 236 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, | 229 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, |
| 237 HashPrefixMap* prefix_map_to_update); | 230 HashPrefixMap* prefix_map_to_update); |
| 238 | 231 |
| 239 // Updates the |additions_map| with the additions received in the partial | 232 // Updates the |additions_map| with the additions received in the partial |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 262 const base::FilePath store_path_; | 255 const base::FilePath store_path_; |
| 263 HashPrefixMap hash_prefix_map_; | 256 HashPrefixMap hash_prefix_map_; |
| 264 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 257 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 265 }; | 258 }; |
| 266 | 259 |
| 267 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 260 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
| 268 | 261 |
| 269 } // namespace safe_browsing | 262 } // namespace safe_browsing |
| 270 | 263 |
| 271 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 264 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
| OLD | NEW |