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::vector<ListIdentifier>&)> |
| 29 DatabaseReadyForUpdatesCallback; |
| 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 doesn't match the expected value. |
| 129 void ResetStores(const std::vector<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 // See |VerifyChecksum|. |
| 176 void VerifyChecksumOnTaskRunner( |
| 177 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
| 178 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); |
| 179 |
158 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 180 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
159 | 181 |
160 // Map of ListIdentifier to the V4Store. | 182 // Map of ListIdentifier to the V4Store. |
161 const std::unique_ptr<StoreMap> store_map_; | 183 const std::unique_ptr<StoreMap> store_map_; |
162 | 184 |
163 DatabaseUpdatedCallback db_updated_callback_; | 185 DatabaseUpdatedCallback db_updated_callback_; |
164 | 186 |
165 // The factory that controls the creation of V4Store objects. | 187 // The factory that controls the creation of V4Store objects. |
166 static V4StoreFactory* factory_; | 188 static V4StoreFactory* factory_; |
167 | 189 |
168 // The number of stores for which the update request is pending. When this | 190 // 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 | 191 // 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 | 192 // that needed updating and is ready for the next update. It should only be |
171 // accessed on the IO thread. | 193 // accessed on the IO thread. |
172 int pending_store_updates_; | 194 int pending_store_updates_; |
173 | 195 |
174 DISALLOW_COPY_AND_ASSIGN(V4Database); | 196 DISALLOW_COPY_AND_ASSIGN(V4Database); |
175 }; | 197 }; |
176 | 198 |
177 } // namespace safe_browsing | 199 } // namespace safe_browsing |
178 | 200 |
179 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 201 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
OLD | NEW |