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" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 12 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 13 | 13 |
| 14 namespace safe_browsing { | 14 namespace safe_browsing { |
| 15 | 15 |
| 16 class V4Store; | 16 class V4Store; |
| 17 | 17 |
| 18 typedef base::Callback<void(std::unique_ptr<V4Store>)> | 18 typedef base::Callback<void(std::unique_ptr<V4Store>)> |
| 19 UpdatedStoreReadyCallback; | 19 UpdatedStoreReadyCallback; |
| 20 | 20 |
| 21 // The size of the hash prefix, in bytes. It should be between 4 to 32 (full | |
| 22 // hash). | |
| 23 typedef size_t PrefixSize; | |
| 24 | |
| 25 // A hash prefix sent by the SafeBrowsing PVer4 service. | |
| 26 typedef std::string HashPrefix; | |
| 27 | |
| 28 // The sorted list of hash prefixes. | |
| 29 typedef std::string HashPrefixes; | |
| 30 | |
| 31 // Stores the list of sorted hash prefixes, by size. | |
| 32 // For instance: {4: ["abcd", "bcde", "cdef", "gggg"], 5: ["fffff"]} | |
| 33 typedef base::hash_map<PrefixSize, HashPrefixes> HashPrefixMap; | |
| 34 | |
| 35 // Stores the index of 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 | |
| 37 // 3 hash prefixes of length 4, and 1 hash prefix of length 5. | |
| 38 typedef base::hash_map<PrefixSize, size_t> CounterMap; | |
| 39 | |
| 21 // 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 |
| 22 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. | 41 // histogramming purposes. DO NOT CHANGE THE ORDERING OF THESE VALUES. |
| 23 enum StoreReadResult { | 42 enum StoreReadResult { |
| 24 // No errors. | 43 // No errors. |
| 25 READ_SUCCESS = 0, | 44 READ_SUCCESS = 0, |
| 26 | 45 |
| 27 // Reserved for errors in parsing this enum. | 46 // Reserved for errors in parsing this enum. |
| 28 UNEXPECTED_READ_FAILURE = 1, | 47 UNEXPECTED_READ_FAILURE = 1, |
| 29 | 48 |
| 30 // The contents of the file could not be read. | 49 // The contents of the file could not be read. |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 72 UNEXPECTED_BYTES_WRITTEN_FAILURE = 3, | 91 UNEXPECTED_BYTES_WRITTEN_FAILURE = 3, |
| 73 | 92 |
| 74 // Renaming the temporary file to store file failed. | 93 // Renaming the temporary file to store file failed. |
| 75 UNABLE_TO_RENAME_FAILURE = 4, | 94 UNABLE_TO_RENAME_FAILURE = 4, |
| 76 | 95 |
| 77 // Memory space for histograms is determined by the max. ALWAYS | 96 // Memory space for histograms is determined by the max. ALWAYS |
| 78 // ADD NEW VALUES BEFORE THIS ONE. | 97 // ADD NEW VALUES BEFORE THIS ONE. |
| 79 STORE_WRITE_RESULT_MAX | 98 STORE_WRITE_RESULT_MAX |
| 80 }; | 99 }; |
| 81 | 100 |
| 101 // Enumerate different events while applying the update fetched fom the server | |
| 102 // for histogramming purposes. | |
| 103 // DO NOT CHANGE THE ORDERING OF THESE VALUES. | |
| 104 enum ApplyUpdateResult { | |
| 105 // No errors. | |
| 106 APPLY_UPDATE_SUCCESS = 0, | |
| 107 | |
| 108 // Reserved for errors in parsing this enum. | |
| 109 UNEXPECTED_APPLY_UPDATE_FAILURE = 1, | |
| 110 | |
| 111 // Prefix size smaller than 4 (which is the lowest expected). | |
| 112 PREFIX_SIZE_TOO_SMALL_FAILURE = 2, | |
| 113 | |
| 114 // Prefix size larger than 32 (length of a full SHA256 hash). | |
| 115 PREFIX_SIZE_TOO_LARGE_FAILURE = 3, | |
| 116 | |
| 117 // The number of bytes in additions isn't a multiple of prefix size. | |
| 118 ADDITIONS_SIZE_UNEXPECTED_FAILURE = 4, | |
| 119 | |
| 120 // The update received from the server contains a prefix that's already | |
| 121 // present in the map. | |
| 122 ADDITIONS_HAS_EXISTING_PREFIX_FAILURE = 5, | |
| 123 | |
| 124 // The server sent a response_type that the client did not expect. | |
| 125 UNEXPECTED_RESPONSE_TYPE_FAILURE = 6, | |
| 126 | |
| 127 // Memory space for histograms is determined by the max. ALWAYS | |
| 128 // ADD NEW VALUES BEFORE THIS ONE. | |
| 129 APPLY_UPDATE_RESULT_MAX | |
| 130 }; | |
| 131 | |
| 82 // Factory for creating V4Store. Tests implement this factory to create fake | 132 // Factory for creating V4Store. Tests implement this factory to create fake |
| 83 // stores for testing. | 133 // stores for testing. |
| 84 class V4StoreFactory { | 134 class V4StoreFactory { |
| 85 public: | 135 public: |
| 86 virtual ~V4StoreFactory() {} | 136 virtual ~V4StoreFactory() {} |
| 87 virtual V4Store* CreateV4Store( | 137 virtual V4Store* CreateV4Store( |
| 88 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 138 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 89 const base::FilePath& store_path); | 139 const base::FilePath& store_path); |
| 90 }; | 140 }; |
| 91 | 141 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 120 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); | 170 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); |
| 121 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); | 171 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); |
| 122 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); | 172 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); |
| 123 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); | 173 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); |
| 124 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); | 174 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); |
| 125 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); | 175 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); |
| 126 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); | 176 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); |
| 127 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); | 177 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); |
| 128 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); | 178 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); |
| 129 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); | 179 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); |
| 180 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 181 TestAddUnlumpedHashesWithInvalidAddition); | |
| 182 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes); | |
| 183 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString); | |
| 184 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 185 TestGetNextSmallestPrefixSizeWithEmptyPrefixMap); | |
| 186 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestPrefixSize); | |
| 187 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextUnmergedPrefix); | |
| 188 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesWithSameSizesInEachMap); | |
| 189 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 190 TestMergeUpdatesWithDifferentSizesInEachMap); | |
| 191 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdatesOldMapRunsOutFirst); | |
| 192 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 193 TestMergeUpdatesAdditionsMapRunsOutFirst); | |
| 194 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 195 TestMergeUpdatesFailsForRepeatedhashPrefix); | |
| 196 | |
| 197 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, | |
| 198 // then it sets |raw_hashes| as the value at key |prefix_size| in | |
| 199 // |additions_map| | |
| 200 static ApplyUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, | |
| 201 const std::string& raw_hashes, | |
| 202 HashPrefixMap* additions_map); | |
| 203 | |
| 204 // Get the size of the next unmerged hash prefix in dictionary order from | |
| 205 // |hash_prefix_map|. |counter_map| is used to determine which hash prefixes | |
| 206 // have been merged already. Returns true if there are any unmerged hash | |
| 207 // prefixes in the list. | |
| 208 static bool GetNextSmallestPrefixSize(const HashPrefixMap& hash_prefix_map, | |
| 209 const CounterMap& counter_map, | |
| 210 PrefixSize* smallest_prefix_size); | |
| 211 | |
| 212 // Returns the next hash prefix of length |prefix_size| from |hash_prefix_map| | |
| 213 // that hasn't been merged already. |counter_map| is used to determine the | |
| 214 // index of the next prefix of size |prefix_size| to merge. | |
| 215 static HashPrefix GetNextUnmergedPrefixForSize( | |
| 216 PrefixSize prefix_size, | |
| 217 const HashPrefixMap& hash_prefix_map, | |
| 218 const CounterMap& counter_map); | |
| 219 | |
| 220 // Sets a value of 0 in |counter_map| for all keys in |hash_prefix_map|. | |
| 221 static void InitializeCounterMap(const HashPrefixMap& hash_prefix_map, | |
| 222 CounterMap* counter_map); | |
| 223 | |
| 224 // Reserve the appropriate string size so that the string size of the merged | |
| 225 // list is exact. This ignores the soace that would otherwise be released by | |
|
Scott Hess - ex-Googler
2016/07/13 05:46:08
s/soace/space/
| |
| 226 // deletions specified in the update because it is non-trivial to calculate | |
| 227 // those deletions upfront. This isn't so bad since deletions are supposed to | |
| 228 // be small and infrequent. | |
| 229 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, | |
| 230 HashPrefixMap* prefix_map_to_update); | |
| 231 | |
| 232 // Updates the |additions_map| with the additions received in the partial | |
| 233 // update from the server. | |
| 234 static ApplyUpdateResult UpdateHashPrefixMapFromAdditions( | |
| 235 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions, | |
| 236 HashPrefixMap* additions_map); | |
| 237 | |
| 238 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the | |
| 239 // update (additions_map) to populate the prefix map for the current store. | |
| 240 // TODO(vakh): Process removals also. | |
| 241 ApplyUpdateResult MergeUpdate(const HashPrefixMap& old_hash_prefix_map, | |
| 242 const HashPrefixMap& additions_map); | |
| 130 | 243 |
| 131 // Reads the state of the store from the file on disk and returns the reason | 244 // Reads the state of the store from the file on disk and returns the reason |
| 132 // for the failure or reports success. | 245 // for the failure or reports success. |
| 133 StoreReadResult ReadFromDisk(); | 246 StoreReadResult ReadFromDisk(); |
| 134 | 247 |
| 135 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. | 248 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. |
| 136 StoreWriteResult WriteToDisk( | 249 StoreWriteResult WriteToDisk( |
| 137 std::unique_ptr<ListUpdateResponse> response) const; | 250 std::unique_ptr<ListUpdateResponse> response) const; |
| 138 | 251 |
| 139 // The state of the store as returned by the PVer4 server in the last applied | 252 // The state of the store as returned by the PVer4 server in the last applied |
| 140 // update response. | 253 // update response. |
| 141 std::string state_; | 254 std::string state_; |
| 142 const base::FilePath store_path_; | 255 const base::FilePath store_path_; |
| 256 HashPrefixMap hash_prefix_map_; | |
| 143 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 257 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 144 }; | 258 }; |
| 145 | 259 |
| 146 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 260 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
| 147 | 261 |
| 148 } // namespace safe_browsing | 262 } // namespace safe_browsing |
| 149 | 263 |
| 150 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 264 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
| OLD | NEW |