| 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" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 ListInfo(); | 71 ListInfo(); |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 typedef std::vector<ListInfo> ListInfos; | 74 typedef std::vector<ListInfo> ListInfos; |
| 75 | 75 |
| 76 // Factory for creating V4Database. Tests implement this factory to create fake | 76 // Factory for creating V4Database. Tests implement this factory to create fake |
| 77 // databases for testing. | 77 // databases for testing. |
| 78 class V4DatabaseFactory { | 78 class V4DatabaseFactory { |
| 79 public: | 79 public: |
| 80 virtual ~V4DatabaseFactory() {} | 80 virtual ~V4DatabaseFactory() {} |
| 81 virtual V4Database* CreateV4Database( | 81 virtual std::unique_ptr<V4Database> Create( |
| 82 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 82 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 83 const base::FilePath& base_dir_path, | 83 std::unique_ptr<StoreMap> store_map); |
| 84 const ListInfos& list_infos) = 0; | |
| 85 }; | 84 }; |
| 86 | 85 |
| 87 // The on-disk databases are shared among all profiles, as it doesn't contain | 86 // The on-disk databases are shared among all profiles, as it doesn't contain |
| 88 // user-specific data. This object is not thread-safe, i.e. all its methods | 87 // user-specific data. This object is not thread-safe, i.e. all its methods |
| 89 // should be used on the same thread that it was created on, unless specified | 88 // should be used on the same thread that it was created on, unless specified |
| 90 // otherwise. | 89 // otherwise. |
| 91 // The hash-prefixes of each type are managed by a V4Store (including saving to | 90 // The hash-prefixes of each type are managed by a V4Store (including saving to |
| 92 // and reading from disk). | 91 // and reading from disk). |
| 93 // The V4Database serves as a single place to manage all the V4Stores. | 92 // The V4Database serves as a single place to manage all the V4Stores. |
| 94 class V4Database { | 93 class V4Database { |
| 95 public: | 94 public: |
| 96 // Factory method to create a V4Database. It creates the database on the | 95 // Factory method to create a V4Database. It creates the database on the |
| 97 // provided |db_task_runner| containing stores in |store_file_name_map|. When | 96 // provided |db_task_runner| containing stores in |store_file_name_map|. When |
| 98 // the database creation is complete, it runs the NewDatabaseReadyCallback on | 97 // the database creation is complete, it runs the NewDatabaseReadyCallback on |
| 99 // the same thread as it was called. | 98 // the same thread as it was called. |
| 100 static void Create( | 99 static void Create( |
| 101 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 100 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 102 const base::FilePath& base_path, | 101 const base::FilePath& base_path, |
| 103 const ListInfos& list_infos, | 102 const ListInfos& list_infos, |
| 104 NewDatabaseReadyCallback callback); | 103 NewDatabaseReadyCallback new_db_callback); |
| 105 | 104 |
| 106 // Destroys the provided v4_database on its task_runner since this may be a | 105 // Destroys the provided v4_database on its task_runner since this may be a |
| 107 // long operation. | 106 // long operation. |
| 108 static void Destroy(std::unique_ptr<V4Database> v4_database); | 107 static void Destroy(std::unique_ptr<V4Database> v4_database); |
| 109 | 108 |
| 110 virtual ~V4Database(); | 109 virtual ~V4Database(); |
| 111 | 110 |
| 112 // Updates the stores with the response received from the SafeBrowsing service | 111 // Updates the stores with the response received from the SafeBrowsing service |
| 113 // and calls the db_updated_callback when done. | 112 // and calls the db_updated_callback when done. |
| 114 void ApplyUpdate(std::unique_ptr<ParsedServerResponse> parsed_server_response, | 113 void ApplyUpdate(std::unique_ptr<ParsedServerResponse> parsed_server_response, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 145 |
| 147 // Records the size of each of the stores managed by this database, along | 146 // Records the size of each of the stores managed by this database, along |
| 148 // with the combined size of all the stores. | 147 // with the combined size of all the stores. |
| 149 void RecordFileSizeHistograms(); | 148 void RecordFileSizeHistograms(); |
| 150 | 149 |
| 151 protected: | 150 protected: |
| 152 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 151 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 153 std::unique_ptr<StoreMap> store_map); | 152 std::unique_ptr<StoreMap> store_map); |
| 154 | 153 |
| 155 private: | 154 private: |
| 155 friend class V4DatabaseFactory; |
| 156 friend class V4DatabaseTest; | 156 friend class V4DatabaseTest; |
| 157 friend class V4SafeBrowsingServiceTest; |
| 157 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); | 158 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSetupDatabaseWithFakeStores); |
| 158 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, | 159 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, |
| 159 TestSetupDatabaseWithFakeStoresFailsReset); | 160 TestSetupDatabaseWithFakeStoresFailsReset); |
| 160 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNewStates); | 161 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNewStates); |
| 161 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNoNewState); | 162 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithNoNewState); |
| 162 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate); | 163 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithEmptyUpdate); |
| 163 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate); | 164 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestApplyUpdateWithInvalidUpdate); |
| 164 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSomeStoresMatchFullHash); | 165 FRIEND_TEST_ALL_PREFIXES(V4DatabaseTest, TestSomeStoresMatchFullHash); |
| 165 | 166 |
| 166 // Makes the passed |factory| the factory used to instantiate a V4Store. Only | |
| 167 // for tests. | |
| 168 static void RegisterStoreFactoryForTest(V4StoreFactory* factory) { | |
| 169 factory_ = factory; | |
| 170 } | |
| 171 | |
| 172 // Factory method to create a V4Database. When the database creation is | 167 // Factory method to create a V4Database. When the database creation is |
| 173 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. | 168 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. |
| 174 static void CreateOnTaskRunner( | 169 static void CreateOnTaskRunner( |
| 175 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 170 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 176 const base::FilePath& base_path, | 171 const base::FilePath& base_path, |
| 177 const ListInfos& list_infos, | 172 const ListInfos& list_infos, |
| 178 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 173 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
| 179 NewDatabaseReadyCallback callback, | 174 NewDatabaseReadyCallback callback, |
| 180 const base::TimeTicks create_start_time); | 175 const base::TimeTicks create_start_time); |
| 181 | 176 |
| 177 // Makes the passed |factory| the factory used to instantiate a V4Database. |
| 178 // Only for tests. |
| 179 static void RegisterDatabaseFactoryForTest( |
| 180 std::unique_ptr<V4DatabaseFactory> factory); |
| 181 |
| 182 // Makes the passed |factory| the factory used to instantiate a V4Store. Only |
| 183 // for tests. |
| 184 static void RegisterStoreFactoryForTest( |
| 185 std::unique_ptr<V4StoreFactory> factory); |
| 186 |
| 182 // Callback called when a new store has been created and is ready to be used. | 187 // Callback called when a new store has been created and is ready to be used. |
| 183 // This method updates the store_map_ to point to the new store, which causes | 188 // This method updates the store_map_ to point to the new store, which causes |
| 184 // the old store to get deleted. | 189 // the old store to get deleted. |
| 185 void UpdatedStoreReady(ListIdentifier identifier, | 190 void UpdatedStoreReady(ListIdentifier identifier, |
| 186 std::unique_ptr<V4Store> store); | 191 std::unique_ptr<V4Store> store); |
| 187 | 192 |
| 188 // See |VerifyChecksum|. | 193 // See |VerifyChecksum|. |
| 189 void VerifyChecksumOnTaskRunner( | 194 void VerifyChecksumOnTaskRunner( |
| 190 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | 195 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, |
| 191 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); | 196 DatabaseReadyForUpdatesCallback db_ready_for_updates_callback); |
| 192 | 197 |
| 193 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | 198 protected: |
| 194 | |
| 195 // Map of ListIdentifier to the V4Store. | 199 // Map of ListIdentifier to the V4Store. |
| 196 const std::unique_ptr<StoreMap> store_map_; | 200 const std::unique_ptr<StoreMap> store_map_; |
| 197 | 201 |
| 202 private: |
| 203 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; |
| 204 |
| 198 DatabaseUpdatedCallback db_updated_callback_; | 205 DatabaseUpdatedCallback db_updated_callback_; |
| 199 | 206 |
| 207 // The factory that controls the creation of the V4Database object. |
| 208 static V4DatabaseFactory* db_factory_; |
| 209 |
| 200 // The factory that controls the creation of V4Store objects. | 210 // The factory that controls the creation of V4Store objects. |
| 201 static V4StoreFactory* factory_; | 211 static V4StoreFactory* store_factory_; |
| 202 | 212 |
| 203 // The number of stores for which the update request is pending. When this | 213 // The number of stores for which the update request is pending. When this |
| 204 // goes down to 0, that indicates that the database has updated all the stores | 214 // goes down to 0, that indicates that the database has updated all the stores |
| 205 // that needed updating and is ready for the next update. It should only be | 215 // that needed updating and is ready for the next update. It should only be |
| 206 // accessed on the IO thread. | 216 // accessed on the IO thread. |
| 207 int pending_store_updates_; | 217 int pending_store_updates_; |
| 208 | 218 |
| 209 // Only meant to be dereferenced and invalidated on the IO thread and hence | 219 // Only meant to be dereferenced and invalidated on the IO thread and hence |
| 210 // named. For details, see the comment at the top of weak_ptr.h | 220 // named. For details, see the comment at the top of weak_ptr.h |
| 211 base::WeakPtrFactory<V4Database> weak_factory_on_io_; | 221 base::WeakPtrFactory<V4Database> weak_factory_on_io_; |
| 212 | 222 |
| 213 DISALLOW_COPY_AND_ASSIGN(V4Database); | 223 DISALLOW_COPY_AND_ASSIGN(V4Database); |
| 214 }; | 224 }; |
| 215 | 225 |
| 216 } // namespace safe_browsing | 226 } // namespace safe_browsing |
| 217 | 227 |
| 218 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 228 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
| OLD | NEW |