| 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 | 131 |
| 132 // Failed to decode the Rice-encoded additions/removals field. | 132 // Failed to decode the Rice-encoded additions/removals field. |
| 133 RICE_DECODING_FAILURE = 8, | 133 RICE_DECODING_FAILURE = 8, |
| 134 | 134 |
| 135 // Compression type other than RAW and RICE for additions. | 135 // Compression type other than RAW and RICE for additions. |
| 136 UNEXPECTED_COMPRESSION_TYPE_ADDITIONS_FAILURE = 9, | 136 UNEXPECTED_COMPRESSION_TYPE_ADDITIONS_FAILURE = 9, |
| 137 | 137 |
| 138 // Compression type other than RAW and RICE for removals. | 138 // Compression type other than RAW and RICE for removals. |
| 139 UNEXPECTED_COMPRESSION_TYPE_REMOVALS_FAILURE = 10, | 139 UNEXPECTED_COMPRESSION_TYPE_REMOVALS_FAILURE = 10, |
| 140 | 140 |
| 141 // The state of the store did not match the expected checksum sent by the |
| 142 // server. |
| 143 CHECKSUM_MISMATCH_FAILURE = 11, |
| 144 |
| 141 // Memory space for histograms is determined by the max. ALWAYS | 145 // Memory space for histograms is determined by the max. ALWAYS |
| 142 // ADD NEW VALUES BEFORE THIS ONE. | 146 // ADD NEW VALUES BEFORE THIS ONE. |
| 143 APPLY_UPDATE_RESULT_MAX | 147 APPLY_UPDATE_RESULT_MAX |
| 144 }; | 148 }; |
| 145 | 149 |
| 146 // Factory for creating V4Store. Tests implement this factory to create fake | 150 // Factory for creating V4Store. Tests implement this factory to create fake |
| 147 // stores for testing. | 151 // stores for testing. |
| 148 class V4StoreFactory { | 152 class V4StoreFactory { |
| 149 public: | 153 public: |
| 150 virtual ~V4StoreFactory() {} | 154 virtual ~V4StoreFactory() {} |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 242 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 239 TestHashPrefixExistsInMapWithSingleSize); | 243 TestHashPrefixExistsInMapWithSingleSize); |
| 240 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 244 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 241 TestHashPrefixExistsInMapWithDifferentSizes); | 245 TestHashPrefixExistsInMapWithDifferentSizes); |
| 242 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 246 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 243 TestHashPrefixDoesNotExistInMapWithDifferentSizes); | 247 TestHashPrefixDoesNotExistInMapWithDifferentSizes); |
| 244 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | 248 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 245 TestAdditionsWithRiceEncodingFailsWithInvalidInput); | 249 TestAdditionsWithRiceEncodingFailsWithInvalidInput); |
| 246 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAdditionsWithRiceEncodingSucceeds); | 250 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAdditionsWithRiceEncodingSucceeds); |
| 247 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestRemovalsWithRiceEncodingSucceeds); | 251 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestRemovalsWithRiceEncodingSucceeds); |
| 252 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesFailsChecksum); |
| 248 friend class V4StoreTest; | 253 friend class V4StoreTest; |
| 249 | 254 |
| 250 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, | 255 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, |
| 251 // then it sets |raw_hashes| as the value at key |prefix_size| in | 256 // then it sets |raw_hashes| as the value at key |prefix_size| in |
| 252 // |additions_map| | 257 // |additions_map| |
| 253 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, | 258 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, |
| 254 const std::string& raw_hashes, | 259 const std::string& raw_hashes, |
| 255 HashPrefixMap* additions_map); | 260 HashPrefixMap* additions_map); |
| 256 | 261 |
| 257 // Get the next unmerged hash prefix in dictionary order from | 262 // Get the next unmerged hash prefix in dictionary order from |
| (...skipping 25 matching lines...) Expand all Loading... |
| 283 | 288 |
| 284 // Updates the |additions_map| with the additions received in the partial | 289 // Updates the |additions_map| with the additions received in the partial |
| 285 // update from the server. | 290 // update from the server. |
| 286 static ApplyUpdateResult UpdateHashPrefixMapFromAdditions( | 291 static ApplyUpdateResult UpdateHashPrefixMapFromAdditions( |
| 287 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions, | 292 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions, |
| 288 HashPrefixMap* additions_map); | 293 HashPrefixMap* additions_map); |
| 289 | 294 |
| 290 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the | 295 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the |
| 291 // update (additions_map) to populate the prefix map for the current store. | 296 // update (additions_map) to populate the prefix map for the current store. |
| 292 // The indices in the |raw_removals| list, which may be NULL, are not merged. | 297 // The indices in the |raw_removals| list, which may be NULL, are not merged. |
| 298 // The SHA256 checksum of the final list of hash prefixes, in lexographically |
| 299 // sorted order, must match |expected_checksum| (if it's not empty). |
| 293 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map, | 300 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map, |
| 294 const HashPrefixMap& additions_map, | 301 const HashPrefixMap& additions_map, |
| 295 const ::google::protobuf::RepeatedField< | 302 const ::google::protobuf::RepeatedField< |
| 296 ::google::protobuf::int32>* raw_removals); | 303 ::google::protobuf::int32>* raw_removals, |
| 304 const std::string& expected_checksum); |
| 297 | 305 |
| 298 // Processes the FULL_UPDATE |response| from the server and updates the | 306 // Processes the FULL_UPDATE |response| from the server, and writes the |
| 299 // V4Store in |new_store| and writes it to disk. If processing the |response| | 307 // merged V4Store to disk. If processing the |response| succeeds, it returns |
| 308 // APPLY_UPDATE_SUCCESS. |
| 309 // This method is only called when we receive a FULL_UPDATE from the server. |
| 310 ApplyUpdateResult ProcessFullUpdateAndWriteToDisk( |
| 311 std::unique_ptr<ListUpdateResponse> response); |
| 312 |
| 313 // Processes a FULL_UPDATE |response| and updates the V4Store. If processing |
| 314 // the |response| succeeds, it returns APPLY_UPDATE_SUCCESS. |
| 315 // This method is called when we receive a FULL_UPDATE from the server, and |
| 316 // when we read a store file from disk on startup. |
| 317 ApplyUpdateResult ProcessFullUpdate( |
| 318 const std::unique_ptr<ListUpdateResponse>& response); |
| 319 |
| 320 // Merges the hash prefixes in |hash_prefix_map_old| and |response|, updates |
| 321 // the |hash_prefix_map_| and |state_| in the V4Store, and writes the merged |
| 322 // store to disk. If processing succeeds, it returns APPLY_UPDATE_SUCCESS. |
| 323 // This method is only called when we receive a PARTIAL_UPDATE from the |
| 324 // server. |
| 325 ApplyUpdateResult ProcessPartialUpdateAndWriteToDisk( |
| 326 const HashPrefixMap& hash_prefix_map_old, |
| 327 std::unique_ptr<ListUpdateResponse> response); |
| 328 |
| 329 // Merges the hash prefixes in |hash_prefix_map_old| and |response|, and |
| 330 // updates the |hash_prefix_map_| and |state_| in the V4Store. If processing |
| 300 // succeeds, it returns APPLY_UPDATE_SUCCESS. | 331 // succeeds, it returns APPLY_UPDATE_SUCCESS. |
| 301 ApplyUpdateResult ProcessFullUpdate( | 332 ApplyUpdateResult ProcessUpdate( |
| 302 std::unique_ptr<ListUpdateResponse> response, | 333 const HashPrefixMap& hash_prefix_map_old, |
| 303 const std::unique_ptr<V4Store>& new_store); | 334 const std::unique_ptr<ListUpdateResponse>& response); |
| 304 | |
| 305 // Processes the PARTIAL_UPDATE |response| from the server and updates the | |
| 306 // V4Store in |new_store|. If processing the |response| succeeds, it returns | |
| 307 // APPLY_UPDATE_SUCCESS. | |
| 308 ApplyUpdateResult ProcessPartialUpdate( | |
| 309 std::unique_ptr<ListUpdateResponse> response, | |
| 310 const std::unique_ptr<V4Store>& new_store); | |
| 311 | 335 |
| 312 // Reads the state of the store from the file on disk and returns the reason | 336 // Reads the state of the store from the file on disk and returns the reason |
| 313 // for the failure or reports success. | 337 // for the failure or reports success. |
| 314 StoreReadResult ReadFromDisk(); | 338 StoreReadResult ReadFromDisk(); |
| 315 | 339 |
| 316 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. | 340 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. |
| 317 StoreWriteResult WriteToDisk( | 341 StoreWriteResult WriteToDisk( |
| 318 std::unique_ptr<ListUpdateResponse> response) const; | 342 std::unique_ptr<ListUpdateResponse> response) const; |
| 319 | 343 |
| 320 // The state of the store as returned by the PVer4 server in the last applied | 344 // The state of the store as returned by the PVer4 server in the last applied |
| 321 // update response. | 345 // update response. |
| 322 std::string state_; | 346 std::string state_; |
| 323 const base::FilePath store_path_; | 347 const base::FilePath store_path_; |
| 324 HashPrefixMap hash_prefix_map_; | 348 HashPrefixMap hash_prefix_map_; |
| 325 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 349 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 326 }; | 350 }; |
| 327 | 351 |
| 328 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 352 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
| 329 | 353 |
| 330 } // namespace safe_browsing | 354 } // namespace safe_browsing |
| 331 | 355 |
| 332 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 356 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
| OLD | NEW |