| 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/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
| 11 #include "base/single_thread_task_runner.h" | |
| 12 #include "components/safe_browsing_db/v4_protocol_manager_util.h" | 11 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 13 #include "components/safe_browsing_db/v4_store.h" | 12 #include "components/safe_browsing_db/v4_store.h" |
| 14 | 13 |
| 15 namespace safe_browsing { | 14 namespace safe_browsing { |
| 16 | 15 |
| 17 class V4Database; | 16 class V4Database; |
| 18 | 17 |
| 19 typedef base::Callback<void(std::unique_ptr<V4Database>)> | |
| 20 NewDatabaseReadyCallback; | |
| 21 | |
| 22 typedef const base::hash_map<UpdateListIdentifier, | 18 typedef const base::hash_map<UpdateListIdentifier, |
| 23 const base::FilePath::CharType> | 19 const base::FilePath::CharType> |
| 24 ListInfoMap; | 20 ListInfoMap; |
| 25 | 21 |
| 26 typedef std::unique_ptr< | 22 typedef std::unique_ptr< |
| 27 base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>>> | 23 base::hash_map<UpdateListIdentifier, std::unique_ptr<V4Store>>> |
| 28 StoreMap; | 24 StoreMap; |
| 29 | 25 |
| 30 // Factory for creating V4Database. Tests implement this factory to create fake | 26 // Factory for creating V4Database. Tests implement this factory to create fake |
| 31 // databases for testing. | 27 // databases for testing. |
| 32 class V4DatabaseFactory { | 28 class V4DatabaseFactory { |
| 33 public: | 29 public: |
| 34 virtual ~V4DatabaseFactory() {} | 30 virtual ~V4DatabaseFactory() {} |
| 35 virtual V4Database* CreateV4Database( | 31 virtual V4Database* CreateV4Database( |
| 36 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 32 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 37 const base::FilePath& base_path, | 33 const base::FilePath& base_path, |
| 38 ListInfoMap list_info_map) = 0; | 34 ListInfoMap list_info_map) = 0; |
| 39 }; | 35 }; |
| 40 | 36 |
| 41 // The on-disk databases are shared among all profiles, as it doesn't contain | 37 // The on-disk databases are shared among all profiles, as it doesn't contain |
| 42 // user-specific data. This object is not thread-safe, i.e. all its methods | 38 // user-specific data. This object is not thread-safe, i.e. all its methods |
| 43 // should be used on the same thread that it was created on, unless specified | 39 // should be used on the same thread that it was created on, unless specified |
| 44 // otherwise. | 40 // otherwise. |
| 45 // The hash-prefixes of each type are managed by a V4Store (including saving to | 41 // The hash-prefixes of each type are managed by a V4Store (including saving to |
| 46 // and reading from disk). | 42 // and reading from disk). |
| 47 // The V4Database serves as a single place to manage all the V4Stores. | 43 // The V4Database serves as a single place to manage all the V4Stores. |
| 48 class V4Database { | 44 class V4Database { |
| 49 public: | 45 public: |
| 50 // Factory method to create a V4Database. It creates the database on the | 46 // Factory method for obtaining a V4Database implementation. |
| 51 // provided |db_task_runner|. When the database creation is complete, it calls | 47 // It is not thread safe. |
| 52 // the NewDatabaseReadyCallback on the same thread as it was called. | 48 // The availability of each list is controlled by the one flag on this |
| 53 static void Create( | 49 // method. |
| 50 static V4Database* Create( |
| 54 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | 51 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 55 const base::FilePath& base_path, | 52 const base::FilePath& base_path, |
| 56 ListInfoMap list_info_map, | 53 ListInfoMap list_info_map); |
| 57 NewDatabaseReadyCallback callback); | |
| 58 | 54 |
| 59 // Destroys the provided v4_database on its task_runner since this may be a | 55 V4Database( |
| 60 // long operation. | 56 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, |
| 61 static void Destroy(std::unique_ptr<V4Database> v4_database); | 57 StoreMap store_map); |
| 62 | |
| 63 virtual ~V4Database(); | 58 virtual ~V4Database(); |
| 64 | 59 |
| 65 // Deletes the current database and creates a new one. | 60 // Deletes the current database and creates a new one. |
| 66 virtual bool ResetDatabase(); | 61 virtual bool ResetDatabase(); |
| 67 | 62 |
| 68 // Makes the passed |factory| the factory used to instantiate | 63 // Makes the passed |factory| the factory used to instantiate |
| 69 // a V4Database. Only for tests. | 64 // a V4Database. Only for tests. |
| 70 static void RegisterFactoryForTest(V4DatabaseFactory* factory) { | 65 static void RegisterFactoryForTest(V4DatabaseFactory* factory) { |
| 71 factory_ = factory; | 66 factory_ = factory; |
| 72 } | 67 } |
| 73 | 68 |
| 74 protected: | |
| 75 V4Database(const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
| 76 StoreMap store_map); | |
| 77 | |
| 78 private: | 69 private: |
| 79 // Factory method to create a V4Database. When the database creation is | |
| 80 // complete, it calls the NewDatabaseReadyCallback on |callback_task_runner|. | |
| 81 static void CreateOnTaskRunner( | |
| 82 const scoped_refptr<base::SequencedTaskRunner>& db_task_runner, | |
| 83 const base::FilePath& base_path, | |
| 84 ListInfoMap list_info_map, | |
| 85 const scoped_refptr<base::SingleThreadTaskRunner>& callback_task_runner, | |
| 86 NewDatabaseReadyCallback callback); | |
| 87 | |
| 88 const scoped_refptr<base::SequencedTaskRunner> db_task_runner_; | |
| 89 | |
| 90 // Map of UpdateListIdentifier to the V4Store. | |
| 91 StoreMap store_map_; | |
| 92 | |
| 93 // The factory that controls the creation of V4Database objects. | 70 // The factory that controls the creation of V4Database objects. |
| 94 // This is used *only* by tests. | 71 // This is used *only* by tests. |
| 95 static V4DatabaseFactory* factory_; | 72 static V4DatabaseFactory* factory_; |
| 96 | 73 |
| 97 DISALLOW_COPY_AND_ASSIGN(V4Database); | 74 DISALLOW_COPY_AND_ASSIGN(V4Database); |
| 98 }; | 75 }; |
| 99 | 76 |
| 100 } // namespace safe_browsing | 77 } // namespace safe_browsing |
| 101 | 78 |
| 102 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ | 79 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_DATABASE_H_ |
| OLD | NEW |