| 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 merging the update fetched fom the server |
| 102 // for histogramming purposes. |
| 103 // DO NOT CHANGE THE ORDERING OF THESE VALUES. |
| 104 enum MergeResult { |
| 105 // No errors. |
| 106 MERGE_SUCCESS = 0, |
| 107 |
| 108 // Reserved for errors in parsing this enum. |
| 109 UNEXPECTED_MERGE_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 // Memory space for histograms is determined by the max. ALWAYS |
| 121 // ADD NEW VALUES BEFORE THIS ONE. |
| 122 MERGE_RESULT_MAX |
| 123 }; |
| 124 |
| 82 // Factory for creating V4Store. Tests implement this factory to create fake | 125 // Factory for creating V4Store. Tests implement this factory to create fake |
| 83 // stores for testing. | 126 // stores for testing. |
| 84 class V4StoreFactory { | 127 class V4StoreFactory { |
| 85 public: | 128 public: |
| 86 virtual ~V4StoreFactory() {} | 129 virtual ~V4StoreFactory() {} |
| 87 virtual V4Store* CreateV4Store( | 130 virtual V4Store* CreateV4Store( |
| 88 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 131 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 89 const base::FilePath& store_path); | 132 const base::FilePath& store_path); |
| 90 }; | 133 }; |
| 91 | 134 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 120 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); | 163 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); |
| 121 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); | 164 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); |
| 122 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); | 165 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); |
| 123 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); | 166 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); |
| 124 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); | 167 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); |
| 125 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); | 168 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); |
| 126 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); | 169 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); |
| 127 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); | 170 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); |
| 128 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); | 171 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); |
| 129 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); | 172 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); |
| 173 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 174 TestAddUnlumpedHashesWithInvalidAddition); |
| 175 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes); |
| 176 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString); |
| 177 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, |
| 178 TestGetNextSmallestPrefixSizeWithEmptyPrefixMap); |
| 179 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestPrefixSize); |
| 180 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextUnmergedPrefix); |
| 181 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdates); |
| 182 |
| 183 // If |prefix_size| is within expected range, and |raw_hashes| is not invalid, |
| 184 // then it sets |raw_hashes| as the value at key |prefix_size| in |
| 185 // |additions_map| |
| 186 static MergeResult AddUnlumpedHashes(PrefixSize prefix_size, |
| 187 const std::string& raw_hashes, |
| 188 HashPrefixMap* additions_map); |
| 189 |
| 190 // Get the size of the next unmerged hash prefix in dictionary order from |
| 191 // |hash_prefix_map|. |counter_map| is used to determine which hash prefixes |
| 192 // have been merged already. Returns true if there are any unmerged hash |
| 193 // prefixes in the list. |
| 194 static bool GetNextSmallestPrefixSize(const HashPrefixMap& hash_prefix_map, |
| 195 const CounterMap& counter_map, |
| 196 PrefixSize* smallest_prefix_size); |
| 197 |
| 198 // Returns the next hash prefix of length |prefix_size| from |hash_prefix_map| |
| 199 // that hasn't been merged already. |counter_map| is used to determine the |
| 200 // index of the next prefix of size |prefix_size| to merge. |
| 201 static HashPrefix GetNextUnmergedPrefixForSize( |
| 202 PrefixSize prefix_size, |
| 203 const HashPrefixMap& hash_prefix_map, |
| 204 const CounterMap& counter_map); |
| 205 |
| 206 // Sets a value of 0 in |counter_map| for all keys in |hash_prefix_map|. |
| 207 static void InitializeCounterMap(const HashPrefixMap& hash_prefix_map, |
| 208 CounterMap* counter_map); |
| 209 |
| 210 // Reserve the appropriate string size so that the string size of the merged |
| 211 // list is exact. |
| 212 static void ReserveSpaceInPrefixMap(const HashPrefixMap& other_prefixes_map, |
| 213 HashPrefixMap* prefix_map_to_update); |
| 214 |
| 215 // Updates the |additions_map| with the additions received in the partial |
| 216 // update from the server. |
| 217 static void UpdateHashPrefixMapFromAdditions( |
| 218 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions, |
| 219 HashPrefixMap* additions_map); |
| 220 |
| 221 // Merges the prefix map from the old store (|old_hash_prefix_map|) and the |
| 222 // update (additions_map) to populate the prefix map for the current store. |
| 223 // TODO(vakh): Process removals also. |
| 224 void MergeUpdate(const HashPrefixMap& old_hash_prefix_map, |
| 225 const HashPrefixMap& additions_map); |
| 130 | 226 |
| 131 // Reads the state of the store from the file on disk and returns the reason | 227 // Reads the state of the store from the file on disk and returns the reason |
| 132 // for the failure or reports success. | 228 // for the failure or reports success. |
| 133 StoreReadResult ReadFromDisk(); | 229 StoreReadResult ReadFromDisk(); |
| 134 | 230 |
| 135 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. | 231 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. |
| 136 StoreWriteResult WriteToDisk( | 232 StoreWriteResult WriteToDisk( |
| 137 std::unique_ptr<ListUpdateResponse> response) const; | 233 std::unique_ptr<ListUpdateResponse> response) const; |
| 138 | 234 |
| 139 // The state of the store as returned by the PVer4 server in the last applied | 235 // The state of the store as returned by the PVer4 server in the last applied |
| 140 // update response. | 236 // update response. |
| 141 std::string state_; | 237 std::string state_; |
| 142 const base::FilePath store_path_; | 238 const base::FilePath store_path_; |
| 239 HashPrefixMap hash_prefix_map_; |
| 143 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 240 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 144 }; | 241 }; |
| 145 | 242 |
| 146 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 243 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
| 147 | 244 |
| 148 } // namespace safe_browsing | 245 } // namespace safe_browsing |
| 149 | 246 |
| 150 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 247 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
| OLD | NEW |