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_DATABASE_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
6 #define COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 13 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
14 #include "components/safe_browsing_db/v4_store.h" | 14 #include "components/safe_browsing_db/v4_store.h" |
15 | 15 |
16 namespace safe_browsing { | 16 namespace safe_browsing { |
17 | 17 |
18 class V4Database; | 18 class V4Database; |
19 | 19 |
20 // Scheduled when the database has been read from disk and is ready to process | |
21 // resource reputation requests. | |
20 typedef base::Callback<void(std::unique_ptr<V4Database>)> | 22 typedef base::Callback<void(std::unique_ptr<V4Database>)> |
21 NewDatabaseReadyCallback; | 23 NewDatabaseReadyCallback; |
22 | 24 |
25 // Scheduled when the checksum for all the stores in the database has been | |
26 // verified to match the expected value. Stores for which the checksum did not | |
27 // match are passed as the argument and need to be reset. | |
28 typedef base::Callback<void(const std::unordered_set<ListIdentifier>&)> | |
29 DatabaseReadyForUpdatesCallback; | |
Scott Hess - ex-Googler
2016/10/06 23:04:10
Why unordered_set rather than vector? AFAICT, the
vakh (use Gerrit instead)
2016/10/07 00:39:45
Done.
| |
30 | |
23 // This callback is scheduled once the database has finished processing the | 31 // This callback is scheduled once the database has finished processing the |
24 // update requests for all stores and is ready to process the next set of update | 32 // update requests for all stores and is ready to process the next set of update |
25 // requests. | 33 // requests. |
26 typedef base::Callback<void()> DatabaseUpdatedCallback; | 34 typedef base::Callback<void()> DatabaseUpdatedCallback; |
27 | 35 |
28 // Maps the ListIdentifiers to their corresponding in-memory stores, which | 36 // Maps the ListIdentifiers to their corresponding in-memory stores, which |
29 // contain the hash prefixes for that ListIdentifier as well as manage their | 37 // contain the hash prefixes for that ListIdentifier as well as manage their |
30 // storage on disk. | 38 // storage on disk. |
31 typedef base::hash_map<ListIdentifier, std::unique_ptr<V4Store>> StoreMap; | 39 typedef base::hash_map<ListIdentifier, std::unique_ptr<V4Store>> StoreMap; |
32 | 40 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 std::unique_ptr<StoreStateMap> GetStoreStateMap(); | 117 std::unique_ptr<StoreStateMap> GetStoreStateMap(); |
110 | 118 |
111 // Searches for a hash prefix matching the |full_hash| in stores in the | 119 // Searches for a hash prefix matching the |full_hash| in stores in the |
112 // database, filtered by |stores_to_check|, and returns the identifier of the | 120 // database, filtered by |stores_to_check|, and returns the identifier of the |
113 // store along with the matching hash prefix in |matched_hash_prefix_map|. | 121 // store along with the matching hash prefix in |matched_hash_prefix_map|. |
114 virtual void GetStoresMatchingFullHash( | 122 virtual void GetStoresMatchingFullHash( |
115 const FullHash& full_hash, | 123 const FullHash& full_hash, |
116 const StoresToCheck& stores_to_check, | 124 const StoresToCheck& stores_to_check, |
117 StoreAndHashPrefixes* matched_store_and_full_hashes); | 125 StoreAndHashPrefixes* matched_store_and_full_hashes); |
118 | 126 |
119 // Deletes the current database and creates a new one. | 127 // Resets the stores in |stores_to_reset| to an empty state. This is done if |
120 virtual bool ResetDatabase(); | 128 // the checksum of the prefix hashes in lexic |
129 void ResetStores(const std::unordered_set<ListIdentifier>& stores_to_reset); | |
130 | |
131 // Schedules verification of the checksum of each store read from disk on task | |
132 // runner. If the checksum doesn't match, that store is passed to the | |
133 // |db_ready_for_updates_callback|. At the end, | |
134 // |db_ready_for_updates_callback| is scheduled (on the same thread as it was | |
135 // called) to indicate that the database updates can now be scheduled. | |
136 void VerifyChecksum( | |
137 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); | |
121 | 138 |
122 protected: | 139 protected: |
123 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 140 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
124 std::unique_ptr<StoreMap> store_map); | 141 std::unique_ptr<StoreMap> store_map); |
125 | 142 |
126 private: | 143 private: |
127 friend class V4DatabaseTest; | 144 friend class V4DatabaseTest; |
128 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); | 145 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); |
129 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, | 146 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, |
130 TestSetupDatabaseWithFakeStoresFailsReset); | 147 TestSetupDatabaseWithFakeStoresFailsReset); |
(...skipping 17 matching lines...) Expand all Loading... | |
148 const ListInfos& list_infos, | 165 const ListInfos& list_infos, |
149 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 166 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
150 NewDatabaseReadyCallback callback); | 167 NewDatabaseReadyCallback callback); |
151 | 168 |
152 // Callback called when a new store has been created and is ready to be used. | 169 // Callback called when a new store has been created and is ready to be used. |
153 // This method updates the store_map_ to point to the new store, which causes | 170 // This method updates the store_map_ to point to the new store, which causes |
154 // the old store to get deleted. | 171 // the old store to get deleted. |
155 void UpdatedStoreReady(ListIdentifier identifier, | 172 void UpdatedStoreReady(ListIdentifier identifier, |
156 std::unique_ptr<V4Store> store); | 173 std::unique_ptr<V4Store> store); |
157 | 174 |
175 // Verifies the checksum of each store read from disk on task runner. If the | |
176 // checksum doesn't match, that store is passed to the | |
177 // |db_ready_for_updates_callback| to be reset. At the end, | |
178 // |db_ready_for_updates_callback| is scheduled on |callback_task_runner| to | |
179 // indicate that the database updates can now be scheduled. | |
180 void VerifyChecksumOnTaskRunner( | |
181 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | |
182 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); | |
183 | |
158 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 184 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
159 | 185 |
160 // Map of ListIdentifier to the V4Store. | 186 // Map of ListIdentifier to the V4Store. |
161 const std::unique_ptr<StoreMap> store_map_; | 187 const std::unique_ptr<StoreMap> store_map_; |
162 | 188 |
163 DatabaseUpdatedCallback db_updated_callback_; | 189 DatabaseUpdatedCallback db_updated_callback_; |
164 | 190 |
165 // The factory that controls the creation of V4Store objects. | 191 // The factory that controls the creation of V4Store objects. |
166 static V4StoreFactory* factory_; | 192 static V4StoreFactory* factory_; |
167 | 193 |
168 // The number of stores for which the update request is pending. When this | 194 // The number of stores for which the update request is pending. When this |
169 // goes down to 0, that indicates that the database has updated all the stores | 195 // goes down to 0, that indicates that the database has updated all the stores |
170 // that needed updating and is ready for the next update. It should only be | 196 // that needed updating and is ready for the next update. It should only be |
171 // accessed on the IO thread. | 197 // accessed on the IO thread. |
172 int pending_store_updates_; | 198 int pending_store_updates_; |
173 | 199 |
174 DISALLOW_COPY_AND_ASSIGN(V4Database); | 200 DISALLOW_COPY_AND_ASSIGN(V4Database); |
175 }; | 201 }; |
176 | 202 |
177 } // namespace safe_browsing | 203 } // namespace safe_browsing |
178 | 204 |
179 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 205 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
OLD | NEW |