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::unique_ptr<char> HashPrefix; | |
|
Nathan Parker
2016/07/11 18:09:58
I think we talked about this already, but keeping
vakh (use Gerrit instead)
2016/07/12 07:34:19
As discussed offline, using a std::string instead.
| |
| 27 | |
| 28 // The sorted list of hash prefixes. | |
| 29 typedef std::vector<HashPrefix> 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; | |
|
Nathan Parker
2016/07/11 18:09:58
I was going to say you should think about using a
vakh (use Gerrit instead)
2016/07/12 07:34:19
Acknowledged.
| |
| 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 MergeUpdateResult { | |
| 105 // No errors. | |
| 106 MERGE_SUCCESS = 0, | |
| 107 | |
| 108 // Reserved for errors in parsing this enum. | |
| 109 UNEXPECTED_MERGE_FAILURE = 1, | |
| 110 | |
| 111 // The number of bytes in additions isn't a multiple of prefix size. | |
| 112 ADDITIONS_SIZE_UNEXPECTED_FAILURE = 2, | |
| 113 | |
| 114 // Memory space for histograms is determined by the max. ALWAYS | |
| 115 // ADD NEW VALUES BEFORE THIS ONE. | |
| 116 MERGE_UPDATE_RESULT_MAX | |
| 117 }; | |
| 118 | |
| 82 // Factory for creating V4Store. Tests implement this factory to create fake | 119 // Factory for creating V4Store. Tests implement this factory to create fake |
| 83 // stores for testing. | 120 // stores for testing. |
| 84 class V4StoreFactory { | 121 class V4StoreFactory { |
| 85 public: | 122 public: |
| 86 virtual ~V4StoreFactory() {} | 123 virtual ~V4StoreFactory() {} |
| 87 virtual V4Store* CreateV4Store( | 124 virtual V4Store* CreateV4Store( |
| 88 const scoped_refptr<base::SequencedTaskRunner>& task_runner, | 125 const scoped_refptr<base::SequencedTaskRunner>& task_runner, |
| 89 const base::FilePath& store_path); | 126 const base::FilePath& store_path); |
| 90 }; | 127 }; |
| 91 | 128 |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 120 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); | 157 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromAbsentFile); |
| 121 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); | 158 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromInvalidContentsFile); |
| 122 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); | 159 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromUnexpectedMagicFile); |
| 123 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); | 160 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromLowVersionFile); |
| 124 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); | 161 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixInfoFile); |
| 125 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); | 162 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromNoHashPrefixesFile); |
| 126 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); | 163 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteNoResponseType); |
| 127 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); | 164 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWritePartialResponseType); |
| 128 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); | 165 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestWriteFullResponseType); |
| 129 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); | 166 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestReadFromFileWithUnknownProto); |
| 167 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 168 TestAddUnlumpedHashesWithInvalidAddition); | |
| 169 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashes); | |
| 170 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestAddUnlumpedHashesWithEmptyString); | |
| 171 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, | |
| 172 TestGetNextSmallestPrefixSizeWithEmptyPrefixMap); | |
| 173 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextSmallestPrefixSize); | |
| 174 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestGetNextUnmergedPrefix); | |
| 175 FRIEND_TEST_ALL_PREFIXES(V4StoreTest, TestMergeUpdates); | |
| 176 | |
| 177 // Breaks down the |lumped_hashes|, which is a single string, into hash | |
| 178 // prefixes, each of size |prefix_size|. These prefixes are stored in a | |
| 179 // |prefix_map| as a vector, with |prefix_size| as key. | |
| 180 static MergeUpdateResult AddUnlumpedHashes(PrefixSize prefix_size, | |
| 181 const std::string& lumped_hashes, | |
| 182 HashPrefixMap* prefix_map); | |
| 183 | |
| 184 // Returns the next hash prefix of length |prefix_size| from |hash_prefix_map| | |
| 185 // that hasn't been merged already. |counter_map| is used to determine the | |
| 186 // index of the next prefix of size |prefix_size| to merge. | |
| 187 static HashPrefix& GetNextUnmergedPrefixForSize( | |
| 188 PrefixSize prefix_size, | |
| 189 HashPrefixMap& hash_prefix_map, | |
|
Nathan Parker
2016/07/11 18:09:58
I think the style guide says output-args should be
vakh (use Gerrit instead)
2016/07/12 07:34:19
Done.
| |
| 190 const CounterMap& counter_map); | |
| 191 | |
| 192 // Generates the HashPrefixMap for the additions received in the partial | |
| 193 // update from the seriver. | |
| 194 static HashPrefixMap GetHashPrefixMapFromAdditions( | |
|
Nathan Parker
2016/07/11 18:09:58
Make HashPrefixMap a ptr output-arg. Same below.
vakh (use Gerrit instead)
2016/07/12 07:34:19
Done.
| |
| 195 const ::google::protobuf::RepeatedPtrField<ThreatEntrySet>& additions); | |
| 196 | |
| 197 static CounterMap GetInitializedCounterMap( | |
| 198 const HashPrefixMap& hash_prefix_map); | |
| 199 | |
| 200 static bool GetNextSmallestPrefixSize(const HashPrefixMap& hash_prefix_map, | |
| 201 const CounterMap& counter_map, | |
| 202 PrefixSize* smallest_prefix_size); | |
| 203 | |
| 204 // Merges the prefix map from the old store and the update to populate the | |
| 205 // prefix map for the current store. | |
| 206 // TODO(vakh): Process removals also. | |
| 207 void MergeUpdate(HashPrefixMap& old_hash_prefix_map, | |
| 208 HashPrefixMap& additions_map); | |
| 130 | 209 |
| 131 // Reads the state of the store from the file on disk and returns the reason | 210 // Reads the state of the store from the file on disk and returns the reason |
| 132 // for the failure or reports success. | 211 // for the failure or reports success. |
| 133 StoreReadResult ReadFromDisk(); | 212 StoreReadResult ReadFromDisk(); |
| 134 | 213 |
| 135 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. | 214 // Writes the FULL_UPDATE |response| to disk as a V4StoreFileFormat proto. |
| 136 StoreWriteResult WriteToDisk( | 215 StoreWriteResult WriteToDisk( |
| 137 std::unique_ptr<ListUpdateResponse> response) const; | 216 std::unique_ptr<ListUpdateResponse> response) const; |
| 138 | 217 |
| 139 // The state of the store as returned by the PVer4 server in the last applied | 218 // The state of the store as returned by the PVer4 server in the last applied |
| 140 // update response. | 219 // update response. |
| 141 std::string state_; | 220 std::string state_; |
| 142 const base::FilePath store_path_; | 221 const base::FilePath store_path_; |
| 222 HashPrefixMap hash_prefix_map_; | |
| 143 const scoped_refptr<base::SequencedTaskRunner> task_runner_; | 223 const scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 144 }; | 224 }; |
| 145 | 225 |
| 146 std::ostream& operator<<(std::ostream& os, const V4Store& store); | 226 std::ostream& operator<<(std::ostream& os, const V4Store& store); |
| 147 | 227 |
| 148 } // namespace safe_browsing | 228 } // namespace safe_browsing |
| 149 | 229 |
| 150 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ | 230 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_STORE_H_ |
| OLD | NEW |