Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_PROVIDER_H_ |
| 6 | #define COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_PROVIDER_H_ |
| 7 | |
| 8 | #include "base/files/file_path.h" |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 9 | #include "base/sequenced_task_runner.h" |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 10 | #include "components/keyed_service/core/keyed_service.h" |
| 11 | #include "components/leveldb_proto/proto_database.h" |
| 12 | #include "components/leveldb_proto/proto_database_wrapper.h" |
Troy Hildebrandt | 3b7014b | 2018-12-18 17:32:14 | [diff] [blame] | 13 | #include "components/leveldb_proto/shared_proto_database_provider.h" |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 14 | |
| 15 | namespace leveldb_proto { |
| 16 | |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 17 | class SharedProtoDatabase; |
| 18 | |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 19 | // A KeyedService that provides instances of ProtoDatabase tied to the current |
| 20 | // profile directory. |
| 21 | class ProtoDatabaseProvider : public KeyedService { |
| 22 | public: |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 23 | using GetSharedDBInstanceCallback = |
| 24 | base::OnceCallback<void(scoped_refptr<SharedProtoDatabase>)>; |
| 25 | |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 26 | static ProtoDatabaseProvider* Create(const base::FilePath& profile_dir); |
| 27 | |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 28 | // |client_namespace| is the unique prefix to be used in the shared database |
Siddhartha | 1528d239 | 2019-01-08 23:39:10 | [diff] [blame] | 29 | // if the database returned is a SharedDatabaseClient<T>. This name must be |
| 30 | // present in |kCurrentSharedProtoDatabaseClients|. |type_prefix| is a unique |
| 31 | // prefix within the |client_namespace| to be used in the shared database if |
| 32 | // the database returned is a SharedProtoDatabaseClient<T>. |unique_db_dir|: |
| 33 | // the subdirectory this database should live in within the profile directory. |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 34 | // |task_runner|: the SequencedTaskRunner to run all database operations on. |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 35 | // This isn't used by SharedProtoDatabaseClients since all calls using |
| 36 | // the SharedProtoDatabase will run on its TaskRunner. |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 37 | template <typename T> |
| 38 | std::unique_ptr<ProtoDatabase<T>> GetDB( |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 39 | const std::string& client_namespace, |
| 40 | const std::string& type_prefix, |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 41 | const base::FilePath& unique_db_dir, |
| 42 | const scoped_refptr<base::SequencedTaskRunner>& task_runner); |
| 43 | |
Troy Hildebrandt | 3b7014b | 2018-12-18 17:32:14 | [diff] [blame] | 44 | virtual void GetSharedDBInstance( |
| 45 | GetSharedDBInstanceCallback callback, |
| 46 | scoped_refptr<base::SequencedTaskRunner> callback_task_runner); |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 47 | |
| 48 | ~ProtoDatabaseProvider() override; |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 49 | |
| 50 | private: |
Troy Hildebrandt | 3b7014b | 2018-12-18 17:32:14 | [diff] [blame] | 51 | friend class TestProtoDatabaseProvider; |
| 52 | friend class ProtoDatabaseWrapperTest; |
| 53 | |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 54 | ProtoDatabaseProvider(const base::FilePath& profile_dir); |
| 55 | |
| 56 | base::FilePath profile_dir_; |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 57 | scoped_refptr<SharedProtoDatabase> db_; |
Troy Hildebrandt | 5d5c9a3 | 2018-12-12 22:11:12 | [diff] [blame] | 58 | base::Lock get_db_lock_; |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 59 | // The SequencedTaskRunner used to ensure thread-safe behaviour for |
Troy Hildebrandt | 3b7014b | 2018-12-18 17:32:14 | [diff] [blame] | 60 | // GetSharedDBInstance when called from multiple clients. |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 61 | scoped_refptr<base::SequencedTaskRunner> task_runner_; |
| 62 | |
Troy Hildebrandt | 3b7014b | 2018-12-18 17:32:14 | [diff] [blame] | 63 | // We store the creation sequence because we want to use that to make requests |
| 64 | // to the main provider that rely on WeakPtrs from this, so they're all |
| 65 | // invalidated/checked on the same sequence. |
| 66 | scoped_refptr<base::SequencedTaskRunner> creation_sequence_; |
| 67 | |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 68 | base::WeakPtrFactory<ProtoDatabaseProvider> weak_factory_; |
| 69 | |
| 70 | DISALLOW_COPY_AND_ASSIGN(ProtoDatabaseProvider); |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | template <typename T> |
| 74 | std::unique_ptr<ProtoDatabase<T>> ProtoDatabaseProvider::GetDB( |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 75 | const std::string& client_namespace, |
| 76 | const std::string& type_prefix, |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 77 | const base::FilePath& unique_db_dir, |
| 78 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) { |
Troy Hildebrandt | 3b7014b | 2018-12-18 17:32:14 | [diff] [blame] | 79 | return base::WrapUnique(new ProtoDatabaseWrapper<T>( |
| 80 | client_namespace, type_prefix, unique_db_dir, task_runner, |
| 81 | base::WrapUnique(new SharedProtoDatabaseProvider( |
| 82 | creation_sequence_, weak_factory_.GetWeakPtr())))); |
Troy Hildebrandt | bc4ea7f | 2018-11-28 21:32:51 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | } // namespace leveldb_proto |
| 86 | |
Troy Hildebrandt | 7ea6f1cd | 2018-11-29 02:39:46 | [diff] [blame] | 87 | #endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_PROVIDER_H_ |