Chromium Code Reviews| Index: components/safe_browsing_db/v4_database.h |
| diff --git a/components/safe_browsing_db/v4_database.h b/components/safe_browsing_db/v4_database.h |
| index 8e634492e6537955f1b31f736bec2fddfe6a74e7..85dbd375bf33f8d2f747abbabfbbc4fa26bf3381 100644 |
| --- a/components/safe_browsing_db/v4_database.h |
| +++ b/components/safe_browsing_db/v4_database.h |
| @@ -5,6 +5,7 @@ |
| #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
| #define COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
| +#include "base/callback.h" |
| #include "base/files/file_path.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/sequenced_task_runner.h" |
| @@ -19,15 +20,10 @@ class V4Database; |
| typedef base::Callback<void(std::unique_ptr<V4Database>)> |
| NewDatabaseReadyCallback; |
| -// This defines a hash_map that is used to create the backing files for the |
| -// stores that contain the hash-prefixes. The map key identifies the list that |
| -// we're interested in and the value represents the ASCII file-name that'll be |
| -// created on-disk to store the hash-prefixes for that list. This file is |
| -// created inside the user's profile directory. |
| -// For instance, the UpdateListIdentifier could be for URL expressions for UwS |
| -// on Windows platform, and the corresponding file on disk could be named: |
| -// "uws_win_url.store" |
| -typedef base::hash_map<UpdateListIdentifier, std::string> ListInfoMap; |
| +// This callback is Run once the database has finished processing the update |
| +// requests for all stores and is ready to process the next set of update |
| +// requests. |
| +typedef base::Callback<void()> DatabaseUpdatedCallback; |
| // This hash_map maps the UpdateListIdentifiers to their corresponding in-memory |
| // stores, which contain the hash prefixes for that UpdateListIdentifier as well |
| @@ -42,7 +38,7 @@ class V4DatabaseFactory { |
| virtual V4Database* CreateV4Database( |
| const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| const base::FilePath& base_dir_path, |
| - const ListInfoMap& list_info_map) = 0; |
| + const StoreFileNameMap& store_file_name_map) = 0; |
| }; |
| // The on-disk databases are shared among all profiles, as it doesn't contain |
| @@ -55,12 +51,19 @@ class V4DatabaseFactory { |
| class V4Database { |
| public: |
| // Factory method to create a V4Database. It creates the database on the |
| - // provided |db_task_runner|. When the database creation is complete, it calls |
| - // the NewDatabaseReadyCallback on the same thread as it was called. |
| - static void Create( |
| + // provided |db_task_runner| containing stores for which the filename has |
| + // been specified in \store_file_name_map\. The created V4Database runs |
| + // |db_updated_callback| after processing updates passed to the ApplyUpdate |
| + // method. When the database creation is complete, it runs the |
| + // NewDatabaseReadyCallback on the same thread as it was called. If the |
| + // |base_path| argument isn't an absolute path or if the |
| + // |store_file_name_map| is empty, it returns false right away and doesn't |
| + // attempt to create the database at all. |
| + static bool Create( |
| const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| const base::FilePath& base_path, |
| - const ListInfoMap& list_info_map, |
| + const StoreFileNameMap& store_file_name_map, |
| + DatabaseUpdatedCallback db_updated_callback, |
| NewDatabaseReadyCallback callback); |
| // Destroys the provided v4_database on its task_runner since this may be a |
| @@ -69,12 +72,19 @@ class V4Database { |
| virtual ~V4Database(); |
| + // Updates the stores with the reponse received from the SafeBrowsing service |
|
Nathan Parker
2016/06/15 21:24:13
nit: response
vakh (use Gerrit instead)
2016/06/16 01:07:35
Done.
|
| + // and calls the db_updated_callback_ when done. |
| + void ApplyUpdate(const std::vector<ListUpdateResponse>&); |
| + |
| + const StoreStateMap* store_state_map() { return store_state_map_.get(); } |
| + |
| // Deletes the current database and creates a new one. |
| virtual bool ResetDatabase(); |
| protected: |
| V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| - std::unique_ptr<StoreMap> store_map); |
| + std::unique_ptr<StoreMap> store_map, |
| + DatabaseUpdatedCallback db_updated_callback); |
| private: |
| friend class SafeBrowsingV4DatabaseTest; |
| @@ -82,6 +92,12 @@ class V4Database { |
| TestSetupDatabaseWithFakeStores); |
| FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| TestSetupDatabaseWithFakeStoresFailsReset); |
| + FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| + TestApplyUpdateWithNewStates); |
| + FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| + TestApplyUpdateWithNoNewState); |
| + FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| + TestApplyUpdateWithEmptyUpdate); |
| // Makes the passed |factory| the factory used to instantiate a V4Store. Only |
| // for tests. |
| @@ -94,18 +110,37 @@ class V4Database { |
| static void CreateOnTaskRunner( |
| const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| const base::FilePath& base_path, |
| - const ListInfoMap& list_info_map, |
| + const StoreFileNameMap& store_file_name_map, |
| + DatabaseUpdatedCallback db_updated_callback, |
| const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
| NewDatabaseReadyCallback callback); |
| + // Callback called when a new store has been created and is ready to be used. |
| + // This method updates the store_map_ to point to the new store, which causes |
| + // the old store to get deleted. |
| + void UpdatedStoreReady(UpdateListIdentifier, std::unique_ptr<V4Store>); |
|
Nathan Parker
2016/06/15 21:24:15
missing argument names.
vakh (use Gerrit instead)
2016/06/16 01:07:35
Done.
|
| + |
| + // Updates the store state map after reading the stores from disk on startup. |
| + void UpdateStoreStateMap(); |
| + |
| const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
| // Map of UpdateListIdentifier to the V4Store. |
| const std::unique_ptr<StoreMap> store_map_; |
| + // Map of UpdateListIdentifier to the state of the V4Store it represents. |
| + const std::unique_ptr<StoreStateMap> store_state_map_; |
|
Nathan Parker
2016/06/15 21:24:14
Could this be just a StoreStateMap, and not a ptr?
vakh (use Gerrit instead)
2016/06/16 01:07:35
Done.
|
| + |
| + DatabaseUpdatedCallback db_updated_callback_; |
| + |
| // The factory that controls the creation of V4Store objects. |
| static V4StoreFactory* factory_; |
| + // The number of stores for which the update request is pending. When this |
| + // goes down to 0, that indicates that the database has updated all the stores |
| + // that needed updating and is ready for the next update. |
| + unsigned pending_store_updates_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(V4Database); |
| }; |