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

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

Issue 2762333003: Pref service: add a factory to create the client lib (Closed)
Patch Set: Make tests compile on Windows 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_service_factory.h"
6
7 #include "base/callback_helpers.h"
8 #include "components/prefs/persistent_pref_store.h"
9 #include "components/prefs/pref_notifier_impl.h"
10 #include "components/prefs/pref_registry.h"
11 #include "components/prefs/pref_service.h"
12 #include "components/prefs/pref_value_store.h"
13 #include "services/preferences/public/cpp/persistent_pref_store_client.h"
14 #include "services/preferences/public/cpp/pref_store_client.h"
15 #include "services/preferences/public/interfaces/preferences.mojom.h"
16 #include "services/service_manager/public/cpp/connector.h"
17
18 namespace prefs {
19 namespace {
20
21 // Used to implement a "fire and forget" pattern where we call an interface
22 // method, with an attached error handler, but don't care to hold on to the
23 // InterfacePtr after.
24 template <typename Interface>
25 class RefCountedInterfacePtr
26 : public base::RefCounted<RefCountedInterfacePtr<Interface>> {
27 public:
28 mojo::InterfacePtr<Interface>& get() { return ptr_; }
29 void reset() { ptr_.reset(); }
30
31 private:
32 friend class base::RefCounted<RefCountedInterfacePtr<Interface>>;
33 ~RefCountedInterfacePtr() = default;
34
35 mojo::InterfacePtr<Interface> ptr_;
36 };
37
38 void DoNothingHandleReadError(PersistentPrefStore::PrefReadError error) {}
39
40 class ConnectionBarrier : public base::RefCounted<ConnectionBarrier> {
41 public:
42 static void Create(service_manager::Connector* connector,
43 scoped_refptr<PrefRegistry> pref_registry,
44 CreateCallback callback);
45
46 private:
47 friend class base::RefCounted<ConnectionBarrier>;
48 ConnectionBarrier(scoped_refptr<PrefRegistry> pref_registry,
49 scoped_refptr<PersistentPrefStore> persistent_pref_store,
50 CreateCallback callback);
51 ~ConnectionBarrier() = default;
52
53 // |unused| exists so that the InterfacePtr won't go out of scope and close
Sam McNally 2017/03/22 04:34:03 They aren't unused anymore.
tibell 2017/03/22 05:32:04 Done.
54 // the |pipe.
55 void OnConnect(
56 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> unused,
57 scoped_refptr<PrefRegistry> pref_registry,
58 std::unordered_map<PrefValueStore::PrefStoreType,
59 mojom::PrefStoreConnectionPtr> connections);
60
61 // |unused| exists so that the InterfacePtr won't go out of scope and close
62 // the |pipe.
63 void OnConnectError(
64 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> unused);
65
66 scoped_refptr<PrefRegistry> pref_registry_;
67 scoped_refptr<PersistentPrefStore> persistent_pref_store_;
68 CreateCallback callback_;
69 };
70
71 ConnectionBarrier::ConnectionBarrier(
72 scoped_refptr<PrefRegistry> pref_registry,
73 scoped_refptr<PersistentPrefStore> persistent_pref_store,
74 CreateCallback callback)
75 : pref_registry_(std::move(pref_registry)),
76 persistent_pref_store_(std::move(persistent_pref_store)),
77 callback_(std::move(callback)) {}
78
79 void ConnectionBarrier::OnConnect(
80 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>>
81 connector_ptr,
82 scoped_refptr<PrefRegistry> pref_registry,
83 std::unordered_map<PrefValueStore::PrefStoreType,
84 mojom::PrefStoreConnectionPtr> connections) {
85 scoped_refptr<PrefStore> managed_prefs =
86 connections.count(PrefValueStore::MANAGED_STORE)
Sam McNally 2017/03/22 04:34:03 Could each of these be a function call instead? Th
tibell 2017/03/22 05:32:04 Done.
87 ? make_scoped_refptr(new PrefStoreClient(std::move(
88 connections.find(PrefValueStore::MANAGED_STORE)->second)))
89 : nullptr;
90 scoped_refptr<PrefStore> supervised_user_prefs =
91 connections.count(PrefValueStore::SUPERVISED_USER_STORE)
92 ? make_scoped_refptr(new PrefStoreClient(std::move(
93 connections.find(PrefValueStore::SUPERVISED_USER_STORE)
94 ->second)))
95 : nullptr;
96 scoped_refptr<PrefStore> extension_prefs =
97 connections.count(PrefValueStore::EXTENSION_STORE)
98 ? make_scoped_refptr(new PrefStoreClient(std::move(
99 connections.find(PrefValueStore::EXTENSION_STORE)->second)))
100 : nullptr;
101 scoped_refptr<PrefStore> command_line_prefs =
102 connections.count(PrefValueStore::COMMAND_LINE_STORE)
103 ? make_scoped_refptr(new PrefStoreClient(std::move(
104 connections.find(PrefValueStore::COMMAND_LINE_STORE)->second)))
105 : nullptr;
106 scoped_refptr<PrefStore> recommended_prefs =
107 connections.count(PrefValueStore::RECOMMENDED_STORE)
108 ? make_scoped_refptr(new PrefStoreClient(std::move(
109 connections.find(PrefValueStore::RECOMMENDED_STORE)->second)))
110 : nullptr;
111 scoped_refptr<PrefStore> default_prefs =
112 connections.count(PrefValueStore::DEFAULT_STORE)
113 ? make_scoped_refptr(new PrefStoreClient(std::move(
114 connections.find(PrefValueStore::DEFAULT_STORE)->second)))
115 : nullptr;
116 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl();
117 auto* pref_value_store = new PrefValueStore(
118 managed_prefs.get(), supervised_user_prefs.get(), extension_prefs.get(),
119 command_line_prefs.get(), persistent_pref_store_.get(),
120 recommended_prefs.get(), default_prefs.get(), pref_notifier);
121 base::ResetAndReturn(&callback_)
122 .Run(base::MakeUnique<::PrefService>(
123 pref_notifier, pref_value_store, persistent_pref_store_.get(),
124 pref_registry_.get(), base::Bind(&DoNothingHandleReadError), true));
125 connector_ptr->reset();
126 }
127
128 void ConnectionBarrier::OnConnectError(
129 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>>
130 connector_ptr) {
131 callback_.Run(nullptr);
132 connector_ptr->reset();
133 }
134
135 // static
136 void ConnectionBarrier::Create(service_manager::Connector* connector,
137 scoped_refptr<PrefRegistry> pref_registry,
138 CreateCallback callback) {
139 // Connect to user pref store.
140 mojom::PersistentPrefStoreConnectorPtr persistent_connector_ptr;
141 connector->BindInterface(mojom::kPrefStoreServiceName,
142 &persistent_connector_ptr);
143 auto barrier = make_scoped_refptr(new ConnectionBarrier(
144 std::move(pref_registry),
145 make_scoped_refptr(
146 new PersistentPrefStoreClient(std::move(persistent_connector_ptr))),
147 std::move(callback)));
148
149 // Connect to all other pref stores.
150 auto connector_ptr = make_scoped_refptr(
151 new RefCountedInterfacePtr<mojom::PrefStoreConnector>());
152 connector->BindInterface(mojom::kPrefStoreServiceName, &connector_ptr->get());
153 connector_ptr->get().set_connection_error_handler(
154 base::Bind(&ConnectionBarrier::OnConnectError, barrier, connector_ptr));
155 connector_ptr->get()->Connect(base::Bind(&ConnectionBarrier::OnConnect,
156 barrier, connector_ptr,
157 std::move(pref_registry)));
158 }
159
160 } // namespace
161
162 void CreatePrefService(service_manager::Connector* connector,
163 scoped_refptr<PrefRegistry> pref_registry,
164 const CreateCallback& callback) {
165 ConnectionBarrier::Create(connector, std::move(pref_registry), callback);
166 }
167
168 } // namespace prefs
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698