blob: 04c5f82eb35b8fe975b1ed1719d345f223a37dd1 [file] [log] [blame]
tibell11141bd2017-03-10 00:16:291// Copyright 2017 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
sammc939cdb52017-03-23 01:21:135#ifndef SERVICES_PREFERENCES_PREF_STORE_MANAGER_IMPL_H_
6#define SERVICES_PREFERENCES_PREF_STORE_MANAGER_IMPL_H_
tibell11141bd2017-03-10 00:16:297
sammc4c081132017-03-12 23:35:338#include <memory>
9#include <set>
10#include <unordered_map>
11#include <vector>
12
tibell11141bd2017-03-10 00:16:2913#include "base/compiler_specific.h"
14#include "base/macros.h"
15#include "base/memory/ref_counted.h"
16#include "components/prefs/pref_value_store.h"
17#include "mojo/public/cpp/bindings/binding_set.h"
Sam McNally11c6c8a2017-06-08 01:44:1918#include "mojo/public/cpp/bindings/strong_binding_set.h"
tibell11141bd2017-03-10 00:16:2919#include "services/preferences/public/interfaces/preferences.mojom.h"
ben58ef184b2017-04-07 22:52:4120#include "services/service_manager/public/cpp/binder_registry.h"
tibell11141bd2017-03-10 00:16:2921#include "services/service_manager/public/cpp/service.h"
22
sammc4c081132017-03-12 23:35:3323namespace base {
24class SequencedWorkerPool;
25}
26
tibell11141bd2017-03-10 00:16:2927namespace prefs {
Sam McNally11c6c8a2017-06-08 01:44:1928class SharedPrefRegistry;
sammc4c081132017-03-12 23:35:3329class PersistentPrefStoreImpl;
Sam McNally5c8104c2017-05-26 00:23:5430class ScopedPrefConnectionBuilder;
tibell11141bd2017-03-10 00:16:2931
32// This class mediates the connection of clients who wants to read preferences
33// and the pref stores that store those preferences. Pref stores use the
34// |PrefStoreRegistry| interface to register themselves with the manager and
35// clients use the |PrefStoreConnector| interface to connect to these stores.
ben2c386b22017-05-03 00:42:1836class PrefStoreManagerImpl : public mojom::PrefStoreRegistry,
ben2c386b22017-05-03 00:42:1837 public mojom::PrefServiceControl,
38 public service_manager::Service {
tibell11141bd2017-03-10 00:16:2939 public:
tibell11141bd2017-03-10 00:16:2940 // Only replies to Connect calls when all |expected_pref_stores| have
tibell2961ff42017-04-06 05:31:1641 // registered. |expected_pref_stores| must contain
42 // PrefValueStore::DEFAULT_STORE and PrefValueStore::USER_STORE for
43 // consistency, as the service always registers these
44 // internally. |worker_pool| is used for any I/O performed by the service.
sammc939cdb52017-03-23 01:21:1345 PrefStoreManagerImpl(
46 std::set<PrefValueStore::PrefStoreType> expected_pref_stores,
47 scoped_refptr<base::SequencedWorkerPool> worker_pool);
tibell11141bd2017-03-10 00:16:2948 ~PrefStoreManagerImpl() override;
49
50 private:
Sam McNally11c6c8a2017-06-08 01:44:1951 class ConnectorConnection;
52
tibell11141bd2017-03-10 00:16:2953 // mojom::PrefStoreRegistry:
54 void Register(PrefValueStore::PrefStoreType type,
55 mojom::PrefStorePtr pref_store_ptr) override;
56
ben2c386b22017-05-03 00:42:1857 void BindPrefStoreConnectorRequest(
58 const service_manager::BindSourceInfo& source_info,
59 prefs::mojom::PrefStoreConnectorRequest request);
60 void BindPrefStoreRegistryRequest(
61 const service_manager::BindSourceInfo& source_info,
62 prefs::mojom::PrefStoreRegistryRequest request);
63 void BindPrefServiceControlRequest(
64 const service_manager::BindSourceInfo& source_info,
65 prefs::mojom::PrefServiceControlRequest request);
sammc4c081132017-03-12 23:35:3366
67 // PrefServiceControl:
68 void Init(mojom::PersistentPrefStoreConfigurationPtr configuration) override;
69
tibell11141bd2017-03-10 00:16:2970 // service_manager::Service:
71 void OnStart() override;
ben2c36828d2017-05-02 04:09:0172 void OnBindInterface(const service_manager::BindSourceInfo& source_info,
ben58ef184b2017-04-07 22:52:4173 const std::string& interface_name,
74 mojo::ScopedMessagePipeHandle interface_pipe) override;
tibell11141bd2017-03-10 00:16:2975
76 // Called when a PrefStore previously registered using |Register| disconnects.
77 void OnPrefStoreDisconnect(PrefValueStore::PrefStoreType type);
78
sammc6a6b1422017-03-26 23:06:0779 void OnPersistentPrefStoreReady();
80
Johan Tibelldcf9a3a82017-06-01 01:01:1381 // Has |Init| been called?
82 bool Initialized() const;
83
tibell2961ff42017-04-06 05:31:1684 // PrefStores that need to register before replying to any Connect calls. This
85 // does not include the PersistentPrefStore, which is handled separately.
sammc939cdb52017-03-23 01:21:1386 std::set<PrefValueStore::PrefStoreType> expected_pref_stores_;
tibell11141bd2017-03-10 00:16:2987
88 // Registered pref stores.
tibell2961ff42017-04-06 05:31:1689 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>
90 pref_store_ptrs_;
tibell11141bd2017-03-10 00:16:2991
Sam McNally11c6c8a2017-06-08 01:44:1992 mojo::StrongBindingSet<mojom::PrefStoreConnector> connector_bindings_;
tibell11141bd2017-03-10 00:16:2993 mojo::BindingSet<mojom::PrefStoreRegistry> registry_bindings_;
sammc4c081132017-03-12 23:35:3394 std::unique_ptr<PersistentPrefStoreImpl> persistent_pref_store_;
sammc4c081132017-03-12 23:35:3395 mojo::Binding<mojom::PrefServiceControl> init_binding_;
96
Johan Tibelldcf9a3a82017-06-01 01:01:1397 mojom::PrefStoreConnectorPtr incognito_connector_;
98
Sam McNally11c6c8a2017-06-08 01:44:1999 const std::unique_ptr<SharedPrefRegistry> shared_pref_registry_;
sammc69266322017-03-29 03:29:55100
Sam McNally5c8104c2017-05-26 00:23:54101 // The same |ScopedPrefConnectionBuilder| instance may appear multiple times
102 // in |pending_connections_|, once per type of pref store it's waiting for,
103 // and at most once in |pending_persistent_connections_|.
104 std::unordered_map<PrefValueStore::PrefStoreType,
105 std::vector<scoped_refptr<ScopedPrefConnectionBuilder>>>
106 pending_connections_;
107 std::vector<scoped_refptr<ScopedPrefConnectionBuilder>>
108 pending_persistent_connections_;
Johan Tibelldcf9a3a82017-06-01 01:01:13109 std::vector<scoped_refptr<ScopedPrefConnectionBuilder>>
110 pending_incognito_connections_;
Sam McNally5c8104c2017-05-26 00:23:54111
sammc69266322017-03-29 03:29:55112 const scoped_refptr<base::SequencedWorkerPool> worker_pool_;
tibell11141bd2017-03-10 00:16:29113
ben58ef184b2017-04-07 22:52:41114 service_manager::BinderRegistry registry_;
115
tibell11141bd2017-03-10 00:16:29116 DISALLOW_COPY_AND_ASSIGN(PrefStoreManagerImpl);
117};
118
119} // namespace prefs
120
sammc939cdb52017-03-23 01:21:13121#endif // SERVICES_PREFERENCES_PREF_STORE_MANAGER_IMPL_H_