| 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/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 10 #include "base/sequenced_task_runner.h" | 11 #include "base/sequenced_task_runner.h" |
| 11 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 12 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 13 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 13 #include "components/safe_browsing_db/v4_store.h" | 14 #include "components/safe_browsing_db/v4_store.h" |
| 14 | 15 |
| 15 namespace safe_browsing { | 16 namespace safe_browsing { |
| 16 | 17 |
| 17 class V4Database; | 18 class V4Database; |
| 18 | 19 |
| 19 typedef base::Callback<void(std::unique_ptr<V4Database>)> | 20 typedef base::Callback<void(std::unique_ptr<V4Database>)> |
| 20 NewDatabaseReadyCallback; | 21 NewDatabaseReadyCallback; |
| 21 | 22 |
| 22 // This defines a hash_map that is used to create the backing files for the | 23 // This callback is scheduled once the database has finished processing the |
| 23 // stores that contain the hash-prefixes. The map key identifies the list that | 24 // update requests for all stores and is ready to process the next set of update |
| 24 // we're interested in and the value represents the ASCII file-name that'll be | 25 // requests. |
| 25 // created on-disk to store the hash-prefixes for that list. This file is | 26 typedef base::Callback<void()> DatabaseUpdatedCallback; |
| 26 // created inside the user's profile directory. | |
| 27 // For instance, the UpdateListIdentifier could be for URL expressions for UwS | |
| 28 // on Windows platform, and the corresponding file on disk could be named: | |
| 29 // "uws_win_url.store" | |
| 30 typedef base::hash_map<UpdateListIdentifier, std::string> StoreFileNameMap; | |
| 31 | 27 |
| 32 // This hash_map maps the UpdateListIdentifiers to their corresponding in-memory | 28 // This hash_map maps the UpdateListIdentifiers to their corresponding in-memory |
| 33 // stores, which contain the hash prefixes for that UpdateListIdentifier as well | 29 // stores, which contain the hash prefixes for that UpdateListIdentifier as well |
| 34 // as manage their storage on disk. | 30 // as manage their storage on disk. |
| 35 typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; | 31 typedef base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>> StoreMap; |
| 36 | 32 |
| 37 // Factory for creating V4Database. Tests implement this factory to create fake | 33 // Factory for creating V4Database. Tests implement this factory to create fake |
| 38 // databases for testing. | 34 // databases for testing. |
| 39 class V4DatabaseFactory { | 35 class V4DatabaseFactory { |
| 40 public: | 36 public: |
| 41 virtual ~V4DatabaseFactory() {} | 37 virtual ~V4DatabaseFactory() {} |
| 42 virtual V4Database* CreateV4Database( | 38 virtual V4Database* CreateV4Database( |
| 43 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 39 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 44 const base::FilePath& base_dir_path, | 40 const base::FilePath& base_dir_path, |
| 45 const StoreFileNameMap& store_file_name_map) = 0; | 41 const StoreFileNameMap& store_file_name_map) = 0; |
| 46 }; | 42 }; |
| 47 | 43 |
| 48 // The on-disk databases are shared among all profiles, as it doesn't contain | 44 // The on-disk databases are shared among all profiles, as it doesn't contain |
| 49 // user-specific data. This object is not thread-safe, i.e. all its methods | 45 // user-specific data. This object is not thread-safe, i.e. all its methods |
| 50 // should be used on the same thread that it was created on, unless specified | 46 // should be used on the same thread that it was created on, unless specified |
| 51 // otherwise. | 47 // otherwise. |
| 52 // The hash-prefixes of each type are managed by a V4Store (including saving to | 48 // The hash-prefixes of each type are managed by a V4Store (including saving to |
| 53 // and reading from disk). | 49 // and reading from disk). |
| 54 // The V4Database serves as a single place to manage all the V4Stores. | 50 // The V4Database serves as a single place to manage all the V4Stores. |
| 55 class V4Database { | 51 class V4Database { |
| 56 public: | 52 public: |
| 57 // Factory method to create a V4Database. It creates the database on the | 53 // Factory method to create a V4Database. It creates the database on the |
| 58 // provided |db_task_runner|. When the database creation is complete, it calls | 54 // provided |db_task_runner| containing stores in |store_file_name_map|. When |
| 59 // the NewDatabaseReadyCallback on the same thread as it was called. | 55 // the database creation is complete, it runs the NewDatabaseReadyCallback on |
| 56 // the same thread as it was called. |
| 60 static void Create( | 57 static void Create( |
| 61 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 58 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 62 const base::FilePath& base_path, | 59 const base::FilePath& base_path, |
| 63 const StoreFileNameMap& store_file_name_map, | 60 const StoreFileNameMap& store_file_name_map, |
| 61 DatabaseUpdatedCallback db_updated_callback, |
| 64 NewDatabaseReadyCallback callback); | 62 NewDatabaseReadyCallback callback); |
| 65 | 63 |
| 66 // Destroys the provided v4_database on its task_runner since this may be a | 64 // Destroys the provided v4_database on its task_runner since this may be a |
| 67 // long operation. | 65 // long operation. |
| 68 static void Destroy(std::unique_ptr<V4Database> v4_database); | 66 static void Destroy(std::unique_ptr<V4Database> v4_database); |
| 69 | 67 |
| 70 virtual ~V4Database(); | 68 virtual ~V4Database(); |
| 71 | 69 |
| 70 // Updates the stores with the response received from the SafeBrowsing service |
| 71 // and calls the db_updated_callback_ when done. |
| 72 void ApplyUpdate(const std::vector<ListUpdateResponse>& response); |
| 73 |
| 74 // Returns the current state of each of the stores being managed. |
| 75 std::unique_ptr<StoreStateMap> GetStoreStateMap(); |
| 76 |
| 72 // Deletes the current database and creates a new one. | 77 // Deletes the current database and creates a new one. |
| 73 virtual bool ResetDatabase(); | 78 virtual bool ResetDatabase(); |
| 74 | 79 |
| 75 protected: | 80 protected: |
| 76 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 81 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 77 std::unique_ptr<StoreMap> store_map); | 82 std::unique_ptr<StoreMap> store_map, |
| 83 DatabaseUpdatedCallback db_updated_callback); |
| 78 | 84 |
| 79 private: | 85 private: |
| 80 friend class SafeBrowsingV4DatabaseTest; | 86 friend class SafeBrowsingV4DatabaseTest; |
| 81 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, | 87 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| 82 TestSetupDatabaseWithFakeStores); | 88 TestSetupDatabaseWithFakeStores); |
| 83 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, | 89 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| 84 TestSetupDatabaseWithFakeStoresFailsReset); | 90 TestSetupDatabaseWithFakeStoresFailsReset); |
| 91 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| 92 TestApplyUpdateWithNewStates); |
| 93 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| 94 TestApplyUpdateWithNoNewState); |
| 95 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingV4DatabaseTest, |
| 96 TestApplyUpdateWithEmptyUpdate); |
| 85 | 97 |
| 86 // Makes the passed |factory| the factory used to instantiate a V4Store. Only | 98 // Makes the passed |factory| the factory used to instantiate a V4Store. Only |
| 87 // for tests. | 99 // for tests. |
| 88 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { | 100 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { |
| 89 factory_ = factory; | 101 factory_ = factory; |
| 90 } | 102 } |
| 91 | 103 |
| 92 // Factory method to create a V4Database. When the database creation is | 104 // Factory method to create a V4Database. When the database creation is |
| 93 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. | 105 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. |
| 94 static void CreateOnTaskRunner( | 106 static void CreateOnTaskRunner( |
| 95 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 107 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 96 const base::FilePath& base_path, | 108 const base::FilePath& base_path, |
| 97 const StoreFileNameMap& store_file_name_map, | 109 const StoreFileNameMap& store_file_name_map, |
| 110 DatabaseUpdatedCallback db_updated_callback, |
| 98 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 111 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
| 99 NewDatabaseReadyCallback callback); | 112 NewDatabaseReadyCallback callback); |
| 100 | 113 |
| 114 // Callback called when a new store has been created and is ready to be used. |
| 115 // This method updates the store_map_ to point to the new store, which causes |
| 116 // the old store to get deleted. |
| 117 void UpdatedStoreReady(UpdateListIdentifier identifier, |
| 118 std::unique_ptr<V4Store> store); |
| 119 |
| 101 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 120 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
| 102 | 121 |
| 103 // Map of UpdateListIdentifier to the V4Store. | 122 // Map of UpdateListIdentifier to the V4Store. |
| 104 const std::unique_ptr<StoreMap> store_map_; | 123 const std::unique_ptr<StoreMap> store_map_; |
| 105 | 124 |
| 125 DatabaseUpdatedCallback db_updated_callback_; |
| 126 |
| 106 // The factory that controls the creation of V4Store objects. | 127 // The factory that controls the creation of V4Store objects. |
| 107 static V4StoreFactory* factory_; | 128 static V4StoreFactory* factory_; |
| 108 | 129 |
| 130 // The number of stores for which the update request is pending. When this |
| 131 // goes down to 0, that indicates that the database has updated all the stores |
| 132 // that needed updating and is ready for the next update. It should only be |
| 133 // accessed on the IO thread. |
| 134 int pending_store_updates_; |
| 135 |
| 109 DISALLOW_COPY_AND_ASSIGN(V4Database); | 136 DISALLOW_COPY_AND_ASSIGN(V4Database); |
| 110 }; | 137 }; |
| 111 | 138 |
| 112 } // namespace safe_browsing | 139 } // namespace safe_browsing |
| 113 | 140 |
| 114 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 141 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
| OLD | NEW |