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 20 matching lines...) Expand all Loading... | |
| 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 iterator to the last element merged from the HashPrefixMap for a |
| 36 // given prefix size. | 36 // given prefix size. |
| 37 // For instance: {4:iter(3), 5:iter(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. | 38 // 3 hash prefixes of length 4, and 1 hash prefix of length 5. |
| 39 typedef base::hash_map<PrefixSize, HashPrefixes::const_iterator> IteratorMap; | 39 typedef base::hash_map<PrefixSize, HashPrefixes::const_iterator> IteratorMap; |
| 40 | 40 |
| 41 // A full SHA256 hash. | |
| 42 typedef HashPrefix FullHash; | |
|
Nathan Parker
2016/07/19 23:01:31
comment: This is basically just a notation in the
vakh (use Gerrit instead)
2016/07/20 00:24:52
Yes, but makes following the code much easier.
| |
| 43 | |
| 41 // Enumerate different failure events while parsing the file read from disk for | 44 // Enumerate different failure events while parsing the file read from disk for |
| 42 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. | 45 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. |
| 43 enum StoreReadResult { | 46 enum StoreReadResult { |
| 44 // No errors. | 47 // No errors. |
| 45 READ_SUCCESS = 0, | 48 READ_SUCCESS = 0, |
| 46 | 49 |
| 47 // Reserved for errors in parsing this enum. | 50 // Reserved for errors in parsing this enum. |
| 48 UNEXPECTED_READ_FAILURE = 1, | 51 UNEXPECTED_READ_FAILURE = 1, |
| 49 | 52 |
| 50 // The contents of the file could not be read. | 53 // The contents of the file could not be read. |
| 51 FILE_UNREADABLE_FAILURE = 2, | 54 FILE_UNREADABLE_FAILURE = 2, |
| 52 | 55 |
| 53 // The file was found to be empty. | 56 // The file was found to be empty. |
| 54 FILE_EMPTY_FAILURE = 3, | 57 FILE_EMPTY_FAILURE = 3, |
| 55 | 58 |
| 56 // The contents of the file could not be interpreted as a valid | 59 // The contents of the file could not be interpreted as a valid |
| 57 // V4StoreFileFormat proto. | 60 // V4StoreFileFormat proto. |
| 58 PROTO_PARSING_FAILURE = 4, | 61 PROTO_PARSING_FAILURE = 4, |
| 59 | 62 |
| 60 // The magic number didn't match. We're most likely trying to read a file | 63 // The magic number didn't match. We're most likely trying to read a file |
| 61 // that doesn't contain hash-prefixes. | 64 // that doesn't contain hash prefixes. |
| 62 UNEXPECTED_MAGIC_NUMBER_FAILURE = 5, | 65 UNEXPECTED_MAGIC_NUMBER_FAILURE = 5, |
| 63 | 66 |
| 64 // The version of the file is different from expected and Chromium doesn't | 67 // The version of the file is different from expected and Chromium doesn't |
| 65 // know how to interpret this version of the file. | 68 // know how to interpret this version of the file. |
| 66 FILE_VERSION_INCOMPATIBLE_FAILURE = 6, | 69 FILE_VERSION_INCOMPATIBLE_FAILURE = 6, |
| 67 | 70 |
| 68 // The rest of the file could not be parsed as a ListUpdateResponse protobuf. | 71 // The rest of the file could not be parsed as a ListUpdateResponse protobuf. |
| 69 // This can happen if the machine crashed before the file was fully written to | 72 // This can happen if the machine crashed before the file was fully written to |
| 70 // disk or if there was disk corruption. | 73 // disk or if there was disk corruption. |
| 71 HASH_PREFIX_INFO_MISSING_FAILURE = 7, | 74 HASH_PREFIX_INFO_MISSING_FAILURE = 7, |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 virtual ~V4Store(); | 160 virtual ~V4Store(); |
| 158 | 161 |
| 159 const std::string& state() const { return state_; } | 162 const std::string& state() const { return state_; } |
| 160 | 163 |
| 161 const base::FilePath& store_path() const { return store_path_; } | 164 const base::FilePath& store_path() const { return store_path_; } |
| 162 | 165 |
| 163 void ApplyUpdate(std::unique_ptr<ListUpdateResponse> response, | 166 void ApplyUpdate(std::unique_ptr<ListUpdateResponse> response, |
| 164 const scoped_refptr<base::SingleThreadTaskRunner>&, | 167 const scoped_refptr<base::SingleThreadTaskRunner>&, |
| 165 UpdatedStoreReadyCallback); | 168 UpdatedStoreReadyCallback); |
| 166 | 169 |
| 170 // If a hash prefix in this store matches |full_hash|, returns that hash | |
| 171 // prefix; otherwise returns an empty hash prefix. | |
| 172 HashPrefix GetMatchingHashPrefix(const FullHash& full_hash); | |
| 173 | |
| 167 std::string DebugString() const; | 174 std::string DebugString() const; |
| 168 | 175 |
| 169 // Reads the store file from disk and populates the in-memory representation | 176 // Reads the store file from disk and populates the in-memory representation |
| 170 // of the hash prefixes. | 177 // of the hash prefixes. |
| 171 void Initialize(); | 178 void Initialize(); |
| 172 | 179 |
| 173 // Reset internal state and delete the backing file. | 180 // Reset internal state and delete the backing file. |
| 174 virtual bool Reset(); | 181 virtual bool Reset(); |
| 175 | 182 |
| 176 private: | 183 private: |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 206 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesRemovesFirstElement); | 213 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesRemovesFirstElement); |
| 207 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesRemovesMiddleElement); | 214 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesRemovesMiddleElement); |
| 208 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesRemovesLastElement); | 215 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesRemovesLastElement); |
| 209 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 216 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 210 TestMergeUpdatesRemovesWhenOldHasDifferentSizes); | 217 TestMergeUpdatesRemovesWhenOldHasDifferentSizes); |
| 211 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 218 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 212 TestMergeUpdatesRemovesMultipleAcrossDifferentSizes); | 219 TestMergeUpdatesRemovesMultipleAcrossDifferentSizes); |
| 213 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 220 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 214 TestReadFullResponseWithValidHashPrefixMap); | 221 TestReadFullResponseWithValidHashPrefixMap); |
| 215 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 222 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 216 TestReadFullResponseWithInvalidHashPrefixMap); | 223 TestReadFullResponseWithInvalidHashPrefixMap); |
|
Nathan Parker
2016/07/19 23:01:31
Do all of these really need to be friends? I wond
vakh (use Gerrit instead)
2016/07/20 00:24:52
Friend class?
Style guide allows it and specifical
| |
| 224 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestHashPrefixExistsAtTheBeginning); | |
| 225 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestHashPrefixExistsInTheMiddle); | |
| 226 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestHashPrefixExistsAtTheEnd); | |
| 227 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 228 TestHashPrefixExistsAtTheBeginningOfEven); | |
| 229 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestHashPrefixExistsAtTheEndOfEven); | |
| 230 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 231 TestHashPrefixDoesNotExistInConcatenatedList); | |
| 232 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestFullHashExistsInMapWithSingleSize); | |
| 233 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 234 TestFullHashExistsInMapWithDifferentSizes); | |
| 235 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 236 TestHashPrefixExistsInMapWithSingleSize); | |
| 237 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 238 TestHashPrefixExistsInMapWithDifferentSizes); | |
| 239 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 240 TestHashPrefixDoesNotExistInMapWithDifferentSizes); | |
| 217 | 241 |
| 218 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, | 242 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, |
| 219 // then it sets |raw_hashes| as the value at key |prefix_size| in | 243 // then it sets |raw_hashes| as the value at key |prefix_size| in |
| 220 // |additions_map| | 244 // |additions_map| |
| 221 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, | 245 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, |
| 222 const std::string& raw_hashes, | 246 const std::string& raw_hashes, |
| 223 HashPrefixMap* additions_map); | 247 HashPrefixMap* additions_map); |
| 224 | 248 |
| 225 // Get the next unmerged hash prefix in dictionary order from | 249 // Get the next unmerged hash prefix in dictionary order from |
| 226 // |hash_prefix_map|. |iterator_map| is used to determine which hash prefixes | 250 // |hash_prefix_map|. |iterator_map| is used to determine which hash prefixes |
| 227 // have been merged already. Returns true if there are any unmerged hash | 251 // have been merged already. Returns true if there are any unmerged hash |
| 228 // prefixes in the list. | 252 // prefixes in the list. |
| 229 static bool GetNextSmallestUnmergedPrefix( | 253 static bool GetNextSmallestUnmergedPrefix( |
| 230 const HashPrefixMap& hash_prefix_map, | 254 const HashPrefixMap& hash_prefix_map, |
| 231 const IteratorMap& iterator_map, | 255 const IteratorMap& iterator_map, |
| 232 HashPrefix* smallest_hash_prefix); | 256 HashPrefix* smallest_hash_prefix); |
| 233 | 257 |
| 258 // Returns true if |hash_prefix| exists between |begin| and |end| iterators. | |
| 259 static bool HashPrefixMatches(const HashPrefix& hash_prefix, | |
| 260 const HashPrefixes::const_iterator& begin, | |
| 261 const HashPrefixes::const_iterator& end); | |
| 262 | |
| 234 // For each key in |hash_prefix_map|, sets the iterator at that key | 263 // For each key in |hash_prefix_map|, sets the iterator at that key |
| 235 // |iterator_map| to hash_prefix_map[key].begin(). | 264 // |iterator_map| to hash_prefix_map[key].begin(). |
| 236 static void InitializeIteratorMap(const HashPrefixMap& hash_prefix_map, | 265 static void InitializeIteratorMap(const HashPrefixMap& hash_prefix_map, |
| 237 IteratorMap* iterator_map); | 266 IteratorMap* iterator_map); |
| 238 | 267 |
| 239 // Reserve the appropriate string size so that the string size of the merged | 268 // Reserve the appropriate string size so that the string size of the merged |
| 240 // list is exact. This ignores the space that would otherwise be released by | 269 // list is exact. This ignores the space that would otherwise be released by |
| 241 // deletions specified in the update because it is non-trivial to calculate | 270 // deletions specified in the update because it is non-trivial to calculate |
| 242 // those deletions upfront. This isn't so bad since deletions are supposed to | 271 // those deletions upfront. This isn't so bad since deletions are supposed to |
| 243 // be small and infrequent. | 272 // be small and infrequent. |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 272 const base::FilePath store_path_; | 301 const base::FilePath store_path_; |
| 273 HashPrefixMap hash_prefix_map_; | 302 HashPrefixMap hash_prefix_map_; |
| 274 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 303 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 275 }; | 304 }; |
| 276 | 305 |
| 277 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 306 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
| 278 | 307 |
| 279 } // namespace safe_browsing | 308 } // namespace safe_browsing |
| 280 | 309 |
| 281 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 310 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
| OLD | NEW |