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_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; | |
| 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| | |
|
vakh (use Gerrit instead)
2016/10/06 21:45:21
TODO: format
| |
| 135 // is scheduled (on the same thread as it was called) to indicate that the | |
| 136 // database updates can now be scheduled. | |
| 137 void VerifyChecksum( | |
| 138 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); | |
| 121 | 139 |
| 122 protected: | 140 protected: |
| 123 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 141 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 124 std::unique_ptr<StoreMap> store_map); | 142 std::unique_ptr<StoreMap> store_map); |
| 125 | 143 |
| 126 private: | 144 private: |
| 127 friend class V4DatabaseTest; | 145 friend class V4DatabaseTest; |
| 128 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); | 146 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); |
| 129 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, | 147 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, |
| 130 TestSetupDatabaseWithFakeStoresFailsReset); | 148 TestSetupDatabaseWithFakeStoresFailsReset); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 148 const ListInfos& list_infos, | 166 const ListInfos& list_infos, |
| 149 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 167 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
| 150 NewDatabaseReadyCallback callback); | 168 NewDatabaseReadyCallback callback); |
| 151 | 169 |
| 152 // Callback called when a new store has been created and is ready to be used. | 170 // 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 | 171 // This method updates the store_map_ to point to the new store, which causes |
| 154 // the old store to get deleted. | 172 // the old store to get deleted. |
| 155 void UpdatedStoreReady(ListIdentifier identifier, | 173 void UpdatedStoreReady(ListIdentifier identifier, |
| 156 std::unique_ptr<V4Store> store); | 174 std::unique_ptr<V4Store> store); |
| 157 | 175 |
| 176 // Verifies the checksum of each store read from disk on task runner. If the | |
| 177 // checksum doesn't match, that store is passed to the | |
| 178 // |db_ready_for_updates_callback| to be reset. At the end, | |
| 179 // |db_ready_for_updates_callback| is scheduled on |callback_task_runner| to | |
| 180 // indicate that the database updates can now be scheduled. | |
| 181 void VerifyChecksumOnTaskRunner( | |
| 182 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | |
| 183 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); | |
| 184 | |
| 158 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 185 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
| 159 | 186 |
| 160 // Map of ListIdentifier to the V4Store. | 187 // Map of ListIdentifier to the V4Store. |
| 161 const std::unique_ptr<StoreMap> store_map_; | 188 const std::unique_ptr<StoreMap> store_map_; |
| 162 | 189 |
| 163 DatabaseUpdatedCallback db_updated_callback_; | 190 DatabaseUpdatedCallback db_updated_callback_; |
| 164 | 191 |
| 165 // The factory that controls the creation of V4Store objects. | 192 // The factory that controls the creation of V4Store objects. |
| 166 static V4StoreFactory* factory_; | 193 static V4StoreFactory* factory_; |
| 167 | 194 |
| 168 // The number of stores for which the update request is pending. When this | 195 // 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 | 196 // 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 | 197 // that needed updating and is ready for the next update. It should only be |
| 171 // accessed on the IO thread. | 198 // accessed on the IO thread. |
| 172 int pending_store_updates_; | 199 int pending_store_updates_; |
| 173 | 200 |
| 174 DISALLOW_COPY_AND_ASSIGN(V4Database); | 201 DISALLOW_COPY_AND_ASSIGN(V4Database); |
| 175 }; | 202 }; |
| 176 | 203 |
| 177 } // namespace safe_browsing | 204 } // namespace safe_browsing |
| 178 | 205 |
| 179 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 206 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
| OLD | NEW |