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

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: Create and register service Created 3 years, 10 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 class PrefStoreManagerImpl::ConnectionBarrier
Sam McNally 2017/02/24 04:14:53 Why is this nested?
tibell 2017/02/27 00:02:54 Done.
15 : public base::RefCounted<ConnectionBarrier> {
16 public:
17 ConnectionBarrier(const PrefStorePtrs& pref_store_ptrs,
18 const ConnectCallback& callback);
19
20 private:
21 friend class base::RefCounted<ConnectionBarrier>;
22 ~ConnectionBarrier();
23
24 void OnConnect(PrefValueStore::PrefStoreType type,
25 mojom::PrefStoreConnectionPtr connection_ptr);
26
27 ConnectCallback callback_;
28
29 std::unordered_map<PrefValueStore::PrefStoreType,
30 mojom::PrefStoreConnectionPtr>
31 connections_;
32
33 const size_t expected_connections_;
34
35 DISALLOW_COPY_AND_ASSIGN(ConnectionBarrier);
36 };
37
38 PrefStoreManagerImpl::ConnectionBarrier::ConnectionBarrier(
39 const PrefStorePtrs& pref_store_ptrs,
40 const ConnectCallback& callback)
41 : callback_(callback), expected_connections_(pref_store_ptrs.size()) {
42 for (const auto& ptr : pref_store_ptrs) {
43 ptr.second->AddObserver(
Sam McNally 2017/02/24 04:14:53 This is perilous if the first ptr is closed. RefCo
tibell 2017/02/27 00:02:54 Done.
44 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first));
45 }
46 }
47
48 PrefStoreManagerImpl::ConnectionBarrier::~ConnectionBarrier() = default;
49
50 // static
51 PrefStoreManagerImpl::PrefStoreTypes PrefStoreManagerImpl::DefaultStores() {
52 return {PrefValueStore::MANAGED_STORE};
Sam McNally 2017/02/24 04:14:53 Is this correct?
tibell 2017/02/27 00:02:54 It's temporary in two senses: 1) I'm messing with
53 }
54
55 void PrefStoreManagerImpl::ConnectionBarrier::OnConnect(
Sam McNally 2017/02/24 04:14:53 This should be above DefaultStores().
tibell 2017/02/27 00:02:54 Done.
56 PrefValueStore::PrefStoreType type,
57 mojom::PrefStoreConnectionPtr connection_ptr) {
58 connections_.insert(std::make_pair(type, std::move(connection_ptr)));
59 if (connections_.size() == expected_connections_) {
60 // After this method exits |this| will get destroyed so it's safe to move
61 // out the map.
62 callback_.Run(std::move(connections_));
63 }
64 }
65
66 PrefStoreManagerImpl::PrefStoreManagerImpl(PrefStoreTypes expected_pref_stores)
67 : expected_pref_stores_(std::move(expected_pref_stores)) {}
68
69 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default;
70
71 void PrefStoreManagerImpl::Register(mojom::PrefStorePtr pref_store_ptr,
72 PrefValueStore::PrefStoreType type) {
73 if (expected_pref_stores_.count(type) == 0) {
74 LOG(WARNING) << "Not registering unexpected pref store: " << type;
75 return;
76 }
77 DVLOG(1) << "Registering pref store: " << type;
78 if (!pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr)))
79 .second) {
80 DLOG(FATAL) << "The same pref store registered twice: " << type;
81 }
82 if (AllConnected()) {
83 DVLOG(1) << "All pref stores registered.";
84 if (!pending_callbacks_.empty()) {
85 for (const auto& callback : pending_callbacks_)
86 make_scoped_refptr(new ConnectionBarrier(pref_store_ptrs_, callback));
87 pending_callbacks_.clear();
88 }
89 }
90 }
91
92 void PrefStoreManagerImpl::Connect(const ConnectCallback& callback) {
93 if (AllConnected())
94 make_scoped_refptr(new ConnectionBarrier(pref_store_ptrs_, callback));
95 else
96 pending_callbacks_.push_back(callback);
97 }
98
99 void PrefStoreManagerImpl::Create(
100 const service_manager::Identity& remote_identity,
101 prefs::mojom::PrefStoreConnectorRequest request) {
102 connector_bindings_.AddBinding(this, std::move(request));
103 }
104
105 void PrefStoreManagerImpl::Create(
106 const service_manager::Identity& remote_identity,
107 prefs::mojom::PrefStoreRegistryRequest request) {
108 registry_bindings_.AddBinding(this, std::move(request));
109 }
110
111 void PrefStoreManagerImpl::OnStart() {}
112
113 bool PrefStoreManagerImpl::OnConnect(
114 const service_manager::ServiceInfo& remote_info,
115 service_manager::InterfaceRegistry* registry) {
116 registry->AddInterface<prefs::mojom::PrefStoreConnector>(this);
117 registry->AddInterface<prefs::mojom::PrefStoreRegistry>(this);
118 return true;
119 }
120
121 bool PrefStoreManagerImpl::AllConnected() const {
122 return pref_store_ptrs_.size() == expected_pref_stores_.size();
123 }
124
125 } // namespace prefs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698