Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(12)

Side by Side Diff: services/preferences/pref_store_manager_impl.cc

Issue 2771723002: Pref service: Merge PrefStoreConnector interfaces. (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 #include "services/preferences/pref_store_manager_impl.h" 5 #include "services/preferences/pref_store_manager_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "base/threading/sequenced_worker_pool.h" 10 #include "base/threading/sequenced_worker_pool.h"
11 #include "components/prefs/pref_value_store.h" 11 #include "components/prefs/pref_value_store.h"
12 #include "mojo/public/cpp/bindings/interface_request.h" 12 #include "mojo/public/cpp/bindings/interface_request.h"
13 #include "services/preferences/persistent_pref_store_factory.h" 13 #include "services/preferences/persistent_pref_store_factory.h"
14 #include "services/preferences/persistent_pref_store_impl.h" 14 #include "services/preferences/persistent_pref_store_impl.h"
15 #include "services/service_manager/public/cpp/interface_registry.h" 15 #include "services/service_manager/public/cpp/interface_registry.h"
16 16
17 namespace prefs { 17 namespace prefs {
18 namespace { 18 namespace {
19 19
20 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback; 20 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback;
21 using PrefStorePtrs = 21 using PrefStorePtrs =
22 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>; 22 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>;
23 23
24 // Used to make sure all pref stores have been registered before replying to any 24 // Used to make sure all pref stores have been registered before replying to any
25 // Connect calls. 25 // Connect calls.
26 class ConnectionBarrier : public base::RefCounted<ConnectionBarrier> { 26 class ConnectionBarrier : public base::RefCounted<ConnectionBarrier> {
27 public: 27 public:
28 static void Create(const PrefStorePtrs& pref_store_ptrs, 28 static void Create(
29 const ConnectCallback& callback); 29 const PrefStorePtrs& pref_store_ptrs,
30 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection,
31 const ConnectCallback& callback);
30 32
31 void Init(const PrefStorePtrs& pref_store_ptrs); 33 void Init(const PrefStorePtrs& pref_store_ptrs);
32 34
33 private: 35 private:
34 friend class base::RefCounted<ConnectionBarrier>; 36 friend class base::RefCounted<ConnectionBarrier>;
35 ConnectionBarrier(const PrefStorePtrs& pref_store_ptrs, 37 ConnectionBarrier(
36 const ConnectCallback& callback); 38 const PrefStorePtrs& pref_store_ptrs,
39 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection,
40 const ConnectCallback& callback);
37 ~ConnectionBarrier() = default; 41 ~ConnectionBarrier() = default;
38 42
39 void OnConnect(PrefValueStore::PrefStoreType type, 43 void OnConnect(PrefValueStore::PrefStoreType type,
40 mojom::PrefStoreConnectionPtr connection_ptr); 44 mojom::PrefStoreConnectionPtr connection_ptr);
41 45
42 ConnectCallback callback_; 46 ConnectCallback callback_;
43 47
44 std::unordered_map<PrefValueStore::PrefStoreType, 48 std::unordered_map<PrefValueStore::PrefStoreType,
45 mojom::PrefStoreConnectionPtr> 49 mojom::PrefStoreConnectionPtr>
46 connections_; 50 connections_;
47 51
48 const size_t expected_connections_; 52 const size_t expected_connections_;
49 53
54 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection_;
55
50 DISALLOW_COPY_AND_ASSIGN(ConnectionBarrier); 56 DISALLOW_COPY_AND_ASSIGN(ConnectionBarrier);
51 }; 57 };
52 58
53 // static 59 // static
54 void ConnectionBarrier::Create(const PrefStorePtrs& pref_store_ptrs, 60 void ConnectionBarrier::Create(
55 const ConnectCallback& callback) { 61 const PrefStorePtrs& pref_store_ptrs,
56 make_scoped_refptr(new ConnectionBarrier(pref_store_ptrs, callback)) 62 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection,
63 const ConnectCallback& callback) {
64 make_scoped_refptr(new ConnectionBarrier(
65 pref_store_ptrs,
66 std::move(persistent_pref_store_connection), callback))
57 ->Init(pref_store_ptrs); 67 ->Init(pref_store_ptrs);
58 } 68 }
59 69
60 void ConnectionBarrier::Init(const PrefStorePtrs& pref_store_ptrs) { 70 void ConnectionBarrier::Init(const PrefStorePtrs& pref_store_ptrs) {
61 if (expected_connections_ == 0) { 71 if (expected_connections_ == 0) {
62 // Degenerate case. We don't expect this, but it could happen in 72 // Degenerate case. We don't expect this, but it could happen in
63 // e.g. testing. 73 // e.g. testing.
64 callback_.Run(std::move(connections_)); 74 callback_.Run(std::move(persistent_pref_store_connection_),
75 std::move(connections_));
65 return; 76 return;
66 } 77 }
67 for (const auto& ptr : pref_store_ptrs) { 78 for (const auto& ptr : pref_store_ptrs) {
68 ptr.second->AddObserver( 79 ptr.second->AddObserver(
69 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first)); 80 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first));
70 } 81 }
71 } 82 }
72 83
73 ConnectionBarrier::ConnectionBarrier(const PrefStorePtrs& pref_store_ptrs, 84 ConnectionBarrier::ConnectionBarrier(
74 const ConnectCallback& callback) 85 const PrefStorePtrs& pref_store_ptrs,
75 : callback_(callback), expected_connections_(pref_store_ptrs.size()) {} 86 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection,
87 const ConnectCallback& callback)
88 : callback_(callback),
89 expected_connections_(pref_store_ptrs.size()),
90 persistent_pref_store_connection_(
91 std::move(persistent_pref_store_connection)) {}
76 92
77 void ConnectionBarrier::OnConnect( 93 void ConnectionBarrier::OnConnect(
78 PrefValueStore::PrefStoreType type, 94 PrefValueStore::PrefStoreType type,
79 mojom::PrefStoreConnectionPtr connection_ptr) { 95 mojom::PrefStoreConnectionPtr connection_ptr) {
80 connections_.insert(std::make_pair(type, std::move(connection_ptr))); 96 connections_.insert(std::make_pair(type, std::move(connection_ptr)));
81 if (connections_.size() == expected_connections_) { 97 if (connections_.size() == expected_connections_) {
82 // After this method exits |this| will get destroyed so it's safe to move 98 // After this method exits |this| will get destroyed so it's safe to move
83 // out the map. 99 // out the members.
84 callback_.Run(std::move(connections_)); 100 callback_.Run(std::move(persistent_pref_store_connection_),
101 std::move(connections_));
85 } 102 }
86 } 103 }
87 104
88 } // namespace 105 } // namespace
89 106
90 PrefStoreManagerImpl::PrefStoreManagerImpl( 107 PrefStoreManagerImpl::PrefStoreManagerImpl(
91 std::set<PrefValueStore::PrefStoreType> expected_pref_stores, 108 std::set<PrefValueStore::PrefStoreType> expected_pref_stores,
92 scoped_refptr<base::SequencedWorkerPool> worker_pool) 109 scoped_refptr<base::SequencedWorkerPool> worker_pool)
93 : expected_pref_stores_(std::move(expected_pref_stores)), 110 : expected_pref_stores_(std::move(expected_pref_stores)),
94 init_binding_(this), 111 init_binding_(this),
(...skipping 10 matching lines...) Expand all
105 DVLOG(1) << "Registering pref store: " << type; 122 DVLOG(1) << "Registering pref store: " << type;
106 pref_store_ptr.set_connection_error_handler( 123 pref_store_ptr.set_connection_error_handler(
107 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect, 124 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect,
108 base::Unretained(this), type)); 125 base::Unretained(this), type));
109 const bool success = 126 const bool success =
110 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr))) 127 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr)))
111 .second; 128 .second;
112 DCHECK(success) << "The same pref store registered twice: " << type; 129 DCHECK(success) << "The same pref store registered twice: " << type;
113 if (AllConnected()) { 130 if (AllConnected()) {
114 DVLOG(1) << "All pref stores registered."; 131 DVLOG(1) << "All pref stores registered.";
115 for (const auto& callback : pending_callbacks_) 132 ProcessPendingConnects();
116 ConnectionBarrier::Create(pref_store_ptrs_, callback);
117 pending_callbacks_.clear();
118 } 133 }
119 } 134 }
120 135
121 void PrefStoreManagerImpl::Connect(const ConnectCallback& callback) { 136 void PrefStoreManagerImpl::Connect(const ConnectCallback& callback) {
122 if (AllConnected()) 137 if (AllConnected()) {
123 ConnectionBarrier::Create(pref_store_ptrs_, callback); 138 ConnectImpl(callback);
124 else 139 } else {
125 pending_callbacks_.push_back(callback); 140 pending_callbacks_.push_back(callback);
141 }
126 } 142 }
127 143
128 void PrefStoreManagerImpl::Create( 144 void PrefStoreManagerImpl::Create(
129 const service_manager::Identity& remote_identity, 145 const service_manager::Identity& remote_identity,
130 prefs::mojom::PrefStoreConnectorRequest request) { 146 prefs::mojom::PrefStoreConnectorRequest request) {
131 connector_bindings_.AddBinding(this, std::move(request)); 147 connector_bindings_.AddBinding(this, std::move(request));
132 } 148 }
133 149
134 void PrefStoreManagerImpl::Create( 150 void PrefStoreManagerImpl::Create(
135 const service_manager::Identity& remote_identity, 151 const service_manager::Identity& remote_identity,
136 prefs::mojom::PrefStoreRegistryRequest request) { 152 prefs::mojom::PrefStoreRegistryRequest request) {
137 registry_bindings_.AddBinding(this, std::move(request)); 153 registry_bindings_.AddBinding(this, std::move(request));
138 } 154 }
139 155
140 void PrefStoreManagerImpl::Create( 156 void PrefStoreManagerImpl::Create(
141 const service_manager::Identity& remote_identity, 157 const service_manager::Identity& remote_identity,
142 prefs::mojom::PersistentPrefStoreConnectorRequest request) {
143 if (!persistent_pref_store_) {
144 pending_persistent_pref_store_requests_.push_back(std::move(request));
145 return;
146 }
147 persistent_pref_store_bindings_.AddBinding(persistent_pref_store_.get(),
148 std::move(request));
149 }
150
151 void PrefStoreManagerImpl::Create(
152 const service_manager::Identity& remote_identity,
153 prefs::mojom::PrefServiceControlRequest request) { 158 prefs::mojom::PrefServiceControlRequest request) {
154 if (init_binding_.is_bound()) { 159 if (init_binding_.is_bound()) {
155 LOG(ERROR) 160 LOG(ERROR)
156 << "Pref service received unexpected control interface connection from " 161 << "Pref service received unexpected control interface connection from "
157 << remote_identity.name(); 162 << remote_identity.name();
158 return; 163 return;
159 } 164 }
160 165
161 init_binding_.Bind(std::move(request)); 166 init_binding_.Bind(std::move(request));
162 } 167 }
163 168
164 void PrefStoreManagerImpl::Init( 169 void PrefStoreManagerImpl::Init(
165 mojom::PersistentPrefStoreConfigurationPtr configuration) { 170 mojom::PersistentPrefStoreConfigurationPtr configuration) {
166 DCHECK(!persistent_pref_store_); 171 DCHECK(!persistent_pref_store_);
167 172
168 persistent_pref_store_ = 173 persistent_pref_store_ = CreatePersistentPrefStore(
169 CreatePersistentPrefStore(std::move(configuration), worker_pool_.get()); 174 std::move(configuration), worker_pool_.get(),
175 base::Bind(&PrefStoreManagerImpl::OnPersistentPrefStoreReady,
176 base::Unretained(this)));
170 DCHECK(persistent_pref_store_); 177 DCHECK(persistent_pref_store_);
171 for (auto& request : pending_persistent_pref_store_requests_) { 178 if (AllConnected())
172 persistent_pref_store_bindings_.AddBinding(persistent_pref_store_.get(), 179 ProcessPendingConnects();
173 std::move(request));
174 }
175 pending_persistent_pref_store_requests_.clear();
176 } 180 }
177 181
178 void PrefStoreManagerImpl::OnStart() {} 182 void PrefStoreManagerImpl::OnStart() {}
179 183
180 bool PrefStoreManagerImpl::OnConnect( 184 bool PrefStoreManagerImpl::OnConnect(
181 const service_manager::ServiceInfo& remote_info, 185 const service_manager::ServiceInfo& remote_info,
182 service_manager::InterfaceRegistry* registry) { 186 service_manager::InterfaceRegistry* registry) {
183 registry->AddInterface<prefs::mojom::PrefStoreConnector>(this); 187 registry->AddInterface<prefs::mojom::PrefStoreConnector>(this);
184 registry->AddInterface<prefs::mojom::PrefStoreRegistry>(this); 188 registry->AddInterface<prefs::mojom::PrefStoreRegistry>(this);
185 registry->AddInterface<prefs::mojom::PersistentPrefStoreConnector>(this);
186 registry->AddInterface<prefs::mojom::PrefServiceControl>(this); 189 registry->AddInterface<prefs::mojom::PrefServiceControl>(this);
187 return true; 190 return true;
188 } 191 }
189 192
190 void PrefStoreManagerImpl::OnPrefStoreDisconnect( 193 void PrefStoreManagerImpl::OnPrefStoreDisconnect(
191 PrefValueStore::PrefStoreType type) { 194 PrefValueStore::PrefStoreType type) {
192 DVLOG(1) << "Deregistering pref store: " << type; 195 DVLOG(1) << "Deregistering pref store: " << type;
193 pref_store_ptrs_.erase(type); 196 pref_store_ptrs_.erase(type);
194 } 197 }
195 198
196 bool PrefStoreManagerImpl::AllConnected() const { 199 bool PrefStoreManagerImpl::AllConnected() const {
197 return pref_store_ptrs_.size() == expected_pref_stores_.size(); 200 return pref_store_ptrs_.size() == expected_pref_stores_.size() &&
201 persistent_pref_store_ && persistent_pref_store_->initialized();
202 }
203
204 void PrefStoreManagerImpl::ProcessPendingConnects() {
205 for (auto& connect : pending_callbacks_)
206 ConnectImpl(connect);
207 pending_callbacks_.clear();
208 }
209
210 void PrefStoreManagerImpl::ConnectImpl(const ConnectCallback& callback) {
211 ConnectionBarrier::Create(
212 pref_store_ptrs_, persistent_pref_store_->CreateConnection(), callback);
213 }
214
215 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() {
216 DVLOG(1) << "PersistentPrefStore ready";
217 if (AllConnected()) {
218 ProcessPendingConnects();
219 }
198 } 220 }
199 221
200 } // namespace prefs 222 } // namespace prefs
OLDNEW
« no previous file with comments | « services/preferences/pref_store_manager_impl.h ('k') | services/preferences/public/cpp/persistent_pref_store_client.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698