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

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

Issue 2635153002: Pref service: expose all read-only PrefStores through Mojo (Closed)
Patch Set: Address bauerb's review comments 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "services/preferences/public/cpp/pref_store_manager_impl.h"
6
7 #include "base/memory/ref_counted.h"
8 #include "components/prefs/pref_value_store.h"
9 #include "mojo/public/cpp/bindings/interface_request.h"
10 #include "services/service_manager/public/cpp/interface_registry.h"
11
12 namespace prefs {
13
14 using ConnectCallback = mojom::PrefStoreConnector::ConnectCallback;
15 using PrefStorePtrs =
16 std::unordered_map<PrefValueStore::PrefStoreType, mojom::PrefStorePtr>;
17
18 // Used to make sure all pref stores have been registered before replying to any
19 // Connect calls.
20 class ConnectionBarrier : public base::RefCounted<ConnectionBarrier> {
21 public:
22 static void Create(const PrefStorePtrs& pref_store_ptrs,
23 const ConnectCallback& callback);
24
25 void Init(const PrefStorePtrs& pref_store_ptrs);
26
27 private:
28 friend class base::RefCounted<ConnectionBarrier>;
29 ConnectionBarrier(const PrefStorePtrs& pref_store_ptrs,
30 const ConnectCallback& callback);
31 ~ConnectionBarrier() = default;
32
33 void OnConnect(PrefValueStore::PrefStoreType type,
34 mojom::PrefStoreConnectionPtr connection_ptr);
35
36 ConnectCallback callback_;
37
38 std::unordered_map<PrefValueStore::PrefStoreType,
39 mojom::PrefStoreConnectionPtr>
40 connections_;
41
42 const size_t expected_connections_;
43
44 DISALLOW_COPY_AND_ASSIGN(ConnectionBarrier);
45 };
46
47 // static
48 void ConnectionBarrier::Create(const PrefStorePtrs& pref_store_ptrs,
49 const ConnectCallback& callback) {
50 make_scoped_refptr(new ConnectionBarrier(pref_store_ptrs, callback))
51 ->Init(pref_store_ptrs);
52 }
53
54 void ConnectionBarrier::Init(const PrefStorePtrs& pref_store_ptrs) {
55 for (const auto& ptr : pref_store_ptrs) {
56 ptr.second->AddObserver(
57 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first));
58 }
59 }
60
61 ConnectionBarrier::ConnectionBarrier(const PrefStorePtrs& pref_store_ptrs,
62 const ConnectCallback& callback)
63 : callback_(callback), expected_connections_(pref_store_ptrs.size()) {}
64
65 void ConnectionBarrier::OnConnect(
66 PrefValueStore::PrefStoreType type,
67 mojom::PrefStoreConnectionPtr connection_ptr) {
68 connections_.insert(std::make_pair(type, std::move(connection_ptr)));
69 if (connections_.size() == expected_connections_) {
70 // After this method exits |this| will get destroyed so it's safe to move
71 // out the map.
72 callback_.Run(std::move(connections_));
73 }
74 }
75
76 PrefStoreManagerImpl::PrefStoreManagerImpl(PrefStoreTypes expected_pref_stores)
77 : expected_pref_stores_(std::move(expected_pref_stores)) {}
78
79 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default;
80
81 void PrefStoreManagerImpl::Register(PrefValueStore::PrefStoreType type,
82 mojom::PrefStorePtr pref_store_ptr) {
83 if (expected_pref_stores_.count(type) == 0) {
84 LOG(WARNING) << "Not registering unexpected pref store: " << type;
85 return;
86 }
87 DVLOG(1) << "Registering pref store: " << type;
88 pref_store_ptr.set_connection_error_handler(
89 base::Bind(&PrefStoreManagerImpl::OnPrefStoreDisconnect,
90 base::Unretained(this), type));
91 const bool success =
92 pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr)))
93 .second;
94 DCHECK(success) << "The same pref store registered twice: " << type;
95 if (AllConnected()) {
96 DVLOG(1) << "All pref stores registered.";
97 for (const auto& callback : pending_callbacks_)
98 ConnectionBarrier::Create(pref_store_ptrs_, callback);
99 pending_callbacks_.clear();
100 }
101 }
102
103 void PrefStoreManagerImpl::Connect(const ConnectCallback& callback) {
104 if (AllConnected())
105 ConnectionBarrier::Create(pref_store_ptrs_, callback);
106 else
107 pending_callbacks_.push_back(callback);
108 }
109
110 void PrefStoreManagerImpl::Create(
111 const service_manager::Identity& remote_identity,
112 prefs::mojom::PrefStoreConnectorRequest request) {
113 connector_bindings_.AddBinding(this, std::move(request));
114 }
115
116 void PrefStoreManagerImpl::Create(
117 const service_manager::Identity& remote_identity,
118 prefs::mojom::PrefStoreRegistryRequest request) {
119 registry_bindings_.AddBinding(this, std::move(request));
120 }
121
122 void PrefStoreManagerImpl::OnStart() {}
123
124 bool PrefStoreManagerImpl::OnConnect(
125 const service_manager::ServiceInfo& remote_info,
126 service_manager::InterfaceRegistry* registry) {
127 registry->AddInterface<prefs::mojom::PrefStoreConnector>(this);
128 registry->AddInterface<prefs::mojom::PrefStoreRegistry>(this);
129 return true;
130 }
131
132 void PrefStoreManagerImpl::OnPrefStoreDisconnect(
133 PrefValueStore::PrefStoreType type) {
134 DVLOG(1) << "Deregistering pref store: " << type;
135 pref_store_ptrs_.erase(type);
136 }
137
138 bool PrefStoreManagerImpl::AllConnected() const {
139 return pref_store_ptrs_.size() == expected_pref_stores_.size();
140 }
141
142 } // namespace prefs
OLDNEW
« no previous file with comments | « services/preferences/public/cpp/pref_store_manager_impl.h ('k') | services/preferences/public/cpp/preferences.typemap » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698