blob: 90d036d830444c24d9e890994ce555591be33d34 [file] [log] [blame]
Troy Hildebrandtbc4ea7f2018-11-28 21:32:511// 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 Hildebrandt7ea6f1cd2018-11-29 02:39:469#include "base/sequenced_task_runner.h"
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5110#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 Hildebrandt3b7014b2018-12-18 17:32:1413#include "components/leveldb_proto/shared_proto_database_provider.h"
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5114
15namespace leveldb_proto {
16
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4617class SharedProtoDatabase;
18
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5119// A KeyedService that provides instances of ProtoDatabase tied to the current
20// profile directory.
21class ProtoDatabaseProvider : public KeyedService {
22 public:
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4623 using GetSharedDBInstanceCallback =
24 base::OnceCallback<void(scoped_refptr<SharedProtoDatabase>)>;
25
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5126 static ProtoDatabaseProvider* Create(const base::FilePath& profile_dir);
27
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4628 // |client_namespace| is the unique prefix to be used in the shared database
Siddhartha1528d2392019-01-08 23:39:1029 // 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 Hildebrandtbc4ea7f2018-11-28 21:32:5134 // |task_runner|: the SequencedTaskRunner to run all database operations on.
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4635 // This isn't used by SharedProtoDatabaseClients since all calls using
36 // the SharedProtoDatabase will run on its TaskRunner.
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5137 template <typename T>
38 std::unique_ptr<ProtoDatabase<T>> GetDB(
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4639 const std::string& client_namespace,
40 const std::string& type_prefix,
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5141 const base::FilePath& unique_db_dir,
42 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
43
Troy Hildebrandt3b7014b2018-12-18 17:32:1444 virtual void GetSharedDBInstance(
45 GetSharedDBInstanceCallback callback,
46 scoped_refptr<base::SequencedTaskRunner> callback_task_runner);
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4647
48 ~ProtoDatabaseProvider() override;
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5149
50 private:
Troy Hildebrandt3b7014b2018-12-18 17:32:1451 friend class TestProtoDatabaseProvider;
52 friend class ProtoDatabaseWrapperTest;
53
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5154 ProtoDatabaseProvider(const base::FilePath& profile_dir);
55
56 base::FilePath profile_dir_;
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4657 scoped_refptr<SharedProtoDatabase> db_;
Troy Hildebrandt5d5c9a32018-12-12 22:11:1258 base::Lock get_db_lock_;
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4659 // The SequencedTaskRunner used to ensure thread-safe behaviour for
Troy Hildebrandt3b7014b2018-12-18 17:32:1460 // GetSharedDBInstance when called from multiple clients.
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4661 scoped_refptr<base::SequencedTaskRunner> task_runner_;
62
Troy Hildebrandt3b7014b2018-12-18 17:32:1463 // 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 Hildebrandt7ea6f1cd2018-11-29 02:39:4668 base::WeakPtrFactory<ProtoDatabaseProvider> weak_factory_;
69
70 DISALLOW_COPY_AND_ASSIGN(ProtoDatabaseProvider);
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5171};
72
73template <typename T>
74std::unique_ptr<ProtoDatabase<T>> ProtoDatabaseProvider::GetDB(
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4675 const std::string& client_namespace,
76 const std::string& type_prefix,
Troy Hildebrandtbc4ea7f2018-11-28 21:32:5177 const base::FilePath& unique_db_dir,
78 const scoped_refptr<base::SequencedTaskRunner>& task_runner) {
Troy Hildebrandt3b7014b2018-12-18 17:32:1479 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 Hildebrandtbc4ea7f2018-11-28 21:32:5183}
84
85} // namespace leveldb_proto
86
Troy Hildebrandt7ea6f1cd2018-11-29 02:39:4687#endif // COMPONENTS_LEVELDB_PROTO_PROTO_DATABASE_PROVIDER_H_