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

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

Issue 2767743003: Pref service: Merge connectors and send a PrefRegistry in Connect(). (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 for (const auto& ptr : pref_store_ptrs) { 71 for (const auto& ptr : pref_store_ptrs) {
62 ptr.second->AddObserver( 72 ptr.second->AddObserver(
63 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first)); 73 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first));
64 } 74 }
75 if (expected_connections_ == 0) {
76 // Degenerate case. We don't expect this, but it could happen in
77 // e.g. testing.
78 callback_.Run(std::move(persistent_pref_store_connection_),
79 std::move(connections_));
80 return;
81 }
65 } 82 }
66 83
67 ConnectionBarrier::ConnectionBarrier(const PrefStorePtrs& pref_store_ptrs, 84 ConnectionBarrier::ConnectionBarrier(
68 const ConnectCallback& callback) 85 const PrefStorePtrs& pref_store_ptrs,
69 : 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)) {}
70 92
71 void ConnectionBarrier::OnConnect( 93 void ConnectionBarrier::OnConnect(
72 PrefValueStore::PrefStoreType type, 94 PrefValueStore::PrefStoreType type,
73 mojom::PrefStoreConnectionPtr connection_ptr) { 95 mojom::PrefStoreConnectionPtr connection_ptr) {
74 connections_.insert(std::make_pair(type, std::move(connection_ptr))); 96 connections_.insert(std::make_pair(type, std::move(connection_ptr)));
75 if (connections_.size() == expected_connections_) { 97 if (connections_.size() == expected_connections_) {
76 // 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
77 // out the map. 99 // out the members.
78 callback_.Run(std::move(connections_)); 100 callback_.Run(std::move(persistent_pref_store_connection_),
101 std::move(connections_));
79 } 102 }
80 } 103 }
81 104
82 } // namespace 105 } // namespace
83 106
84 PrefStoreManagerImpl::PrefStoreManagerImpl( 107 PrefStoreManagerImpl::PrefStoreManagerImpl(
85 std::set<PrefValueStore::PrefStoreType> expected_pref_stores, 108 std::set<PrefValueStore::PrefStoreType> expected_pref_stores,
86 scoped_refptr<base::SequencedWorkerPool> worker_pool) 109 scoped_refptr<base::SequencedWorkerPool> worker_pool)
87 : expected_pref_stores_(std::move(expected_pref_stores)), 110 : expected_pref_stores_(std::move(expected_pref_stores)),
88 init_binding_(this), 111 init_binding_(this),
(...skipping 10 matching lines...) Expand all
99 DVLOG(1) << "Registering pref store: " << type; 122 DVLOG(1) << "Registering pref store: " << type;
100 pref_store_ptr.set_connection_error_handler( 123 pref_store_ptr.set_connection_error_handler(
101 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect, 124 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect,
102 base::Unretained(this), type)); 125 base::Unretained(this), type));
103 const bool success = 126 const bool success =
104 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)))
105 .second; 128 .second;
106 DCHECK(success) << "The same pref store registered twice: " << type; 129 DCHECK(success) << "The same pref store registered twice: " << type;
107 if (AllConnected()) { 130 if (AllConnected()) {
108 DVLOG(1) << "All pref stores registered."; 131 DVLOG(1) << "All pref stores registered.";
109 for (const auto& callback : pending_callbacks_) 132 ProcessQueuedConnects();
110 ConnectionBarrier::Create(pref_store_ptrs_, callback);
111 pending_callbacks_.clear();
112 } 133 }
113 } 134 }
114 135
115 void PrefStoreManagerImpl::Connect(const ConnectCallback& callback) { 136 void PrefStoreManagerImpl::Connect(mojom::PrefRegistryPtr pref_registry,
116 if (AllConnected()) 137 const ConnectCallback& callback) {
117 ConnectionBarrier::Create(pref_store_ptrs_, callback); 138 if (AllConnected()) {
118 else 139 ConnectImpl(std::move(pref_registry), callback);
119 pending_callbacks_.push_back(callback); 140 } else {
141 pending_connects_.push_back(
142 std::make_pair(callback, std::move(pref_registry)));
143 }
120 } 144 }
121 145
122 void PrefStoreManagerImpl::Create( 146 void PrefStoreManagerImpl::Create(
123 const service_manager::Identity& remote_identity, 147 const service_manager::Identity& remote_identity,
124 prefs::mojom::PrefStoreConnectorRequest request) { 148 prefs::mojom::PrefStoreConnectorRequest request) {
125 connector_bindings_.AddBinding(this, std::move(request)); 149 connector_bindings_.AddBinding(this, std::move(request));
126 } 150 }
127 151
128 void PrefStoreManagerImpl::Create( 152 void PrefStoreManagerImpl::Create(
129 const service_manager::Identity& remote_identity, 153 const service_manager::Identity& remote_identity,
130 prefs::mojom::PrefStoreRegistryRequest request) { 154 prefs::mojom::PrefStoreRegistryRequest request) {
131 registry_bindings_.AddBinding(this, std::move(request)); 155 registry_bindings_.AddBinding(this, std::move(request));
132 } 156 }
133 157
134 void PrefStoreManagerImpl::Create( 158 void PrefStoreManagerImpl::Create(
135 const service_manager::Identity& remote_identity, 159 const service_manager::Identity& remote_identity,
136 prefs::mojom::PersistentPrefStoreConnectorRequest request) {
137 if (!persistent_pref_store_) {
138 pending_persistent_pref_store_requests_.push_back(std::move(request));
139 return;
140 }
141 persistent_pref_store_bindings_.AddBinding(persistent_pref_store_.get(),
142 std::move(request));
143 }
144
145 void PrefStoreManagerImpl::Create(
146 const service_manager::Identity& remote_identity,
147 prefs::mojom::PrefServiceControlRequest request) { 160 prefs::mojom::PrefServiceControlRequest request) {
148 if (init_binding_.is_bound()) { 161 if (init_binding_.is_bound()) {
149 LOG(ERROR) 162 LOG(ERROR)
150 << "Pref service received unexpected control interface connection from " 163 << "Pref service received unexpected control interface connection from "
151 << remote_identity.name(); 164 << remote_identity.name();
152 return; 165 return;
153 } 166 }
154 167
155 init_binding_.Bind(std::move(request)); 168 init_binding_.Bind(std::move(request));
156 } 169 }
157 170
158 void PrefStoreManagerImpl::Init( 171 void PrefStoreManagerImpl::Init(
159 mojom::PersistentPrefStoreConfigurationPtr configuration) { 172 mojom::PersistentPrefStoreConfigurationPtr configuration) {
160 DCHECK(!persistent_pref_store_); 173 DCHECK(!persistent_pref_store_);
161 174
162 persistent_pref_store_ = 175 persistent_pref_store_ = CreatePersistentPrefStore(
163 CreatePersistentPrefStore(std::move(configuration), worker_pool_.get()); 176 std::move(configuration), worker_pool_.get(),
177 base::Bind(&PrefStoreManagerImpl::OnPersistentPrefStoreReady,
178 base::Unretained(this)));
164 DCHECK(persistent_pref_store_); 179 DCHECK(persistent_pref_store_);
165 for (auto& request : pending_persistent_pref_store_requests_) { 180 if (AllConnected())
166 persistent_pref_store_bindings_.AddBinding(persistent_pref_store_.get(), 181 ProcessQueuedConnects();
167 std::move(request));
168 }
169 pending_persistent_pref_store_requests_.clear();
170 } 182 }
171 183
172 void PrefStoreManagerImpl::OnStart() {} 184 void PrefStoreManagerImpl::OnStart() {}
173 185
174 bool PrefStoreManagerImpl::OnConnect( 186 bool PrefStoreManagerImpl::OnConnect(
175 const service_manager::ServiceInfo& remote_info, 187 const service_manager::ServiceInfo& remote_info,
176 service_manager::InterfaceRegistry* registry) { 188 service_manager::InterfaceRegistry* registry) {
177 registry->AddInterface<prefs::mojom::PrefStoreConnector>(this); 189 registry->AddInterface<prefs::mojom::PrefStoreConnector>(this);
178 registry->AddInterface<prefs::mojom::PrefStoreRegistry>(this); 190 registry->AddInterface<prefs::mojom::PrefStoreRegistry>(this);
179 registry->AddInterface<prefs::mojom::PersistentPrefStoreConnector>(this);
180 registry->AddInterface<prefs::mojom::PrefServiceControl>(this); 191 registry->AddInterface<prefs::mojom::PrefServiceControl>(this);
181 return true; 192 return true;
182 } 193 }
183 194
184 void PrefStoreManagerImpl::OnPrefStoreDisconnect( 195 void PrefStoreManagerImpl::OnPrefStoreDisconnect(
185 PrefValueStore::PrefStoreType type) { 196 PrefValueStore::PrefStoreType type) {
186 DVLOG(1) << "Deregistering pref store: " << type; 197 DVLOG(1) << "Deregistering pref store: " << type;
187 pref_store_ptrs_.erase(type); 198 pref_store_ptrs_.erase(type);
188 } 199 }
189 200
190 bool PrefStoreManagerImpl::AllConnected() const { 201 bool PrefStoreManagerImpl::AllConnected() const {
191 return pref_store_ptrs_.size() == expected_pref_stores_.size(); 202 return pref_store_ptrs_.size() == expected_pref_stores_.size() &&
203 persistent_pref_store_ && persistent_pref_store_->initialized();
204 }
205
206 void PrefStoreManagerImpl::ProcessQueuedConnects() {
207 for (auto& connect : pending_connects_)
208 ConnectImpl(std::move(connect.second), connect.first);
209 pending_connects_.clear();
210 }
211
212 void PrefStoreManagerImpl::ConnectImpl(mojom::PrefRegistryPtr pref_registry,
213 const ConnectCallback& callback) {
214 ConnectionBarrier::Create(
215 pref_store_ptrs_,
216 persistent_pref_store_->Connect(std::move(pref_registry)), callback);
217 }
218
219 void PrefStoreManagerImpl::OnPersistentPrefStoreReady() {
220 DVLOG(1) << "PersistentPrefStore ready";
221 if (AllConnected()) {
222 ProcessQueuedConnects();
223 }
192 } 224 }
193 225
194 } // namespace prefs 226 } // 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