OLD | NEW |
(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 ConnectCallback 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 ConnectCallback callback); |
| 51 ~ConnectionBarrier() = default; |
| 52 |
| 53 void OnConnect( |
| 54 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> unused, |
| 55 scoped_refptr<PrefRegistry> pref_registry, |
| 56 std::unordered_map<PrefValueStore::PrefStoreType, |
| 57 mojom::PrefStoreConnectionPtr> connections); |
| 58 |
| 59 void OnConnectError( |
| 60 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> unused); |
| 61 |
| 62 scoped_refptr<PrefRegistry> pref_registry_; |
| 63 scoped_refptr<PersistentPrefStore> persistent_pref_store_; |
| 64 ConnectCallback callback_; |
| 65 }; |
| 66 |
| 67 scoped_refptr<PrefStore> CreatePrefStore( |
| 68 PrefValueStore::PrefStoreType store_type, |
| 69 std::unordered_map<PrefValueStore::PrefStoreType, |
| 70 mojom::PrefStoreConnectionPtr>* connections) { |
| 71 auto pref_store_it = connections->find(store_type); |
| 72 if (pref_store_it != connections->end()) { |
| 73 return make_scoped_refptr( |
| 74 new PrefStoreClient(std::move(pref_store_it->second))); |
| 75 } else { |
| 76 return nullptr; |
| 77 } |
| 78 } |
| 79 |
| 80 ConnectionBarrier::ConnectionBarrier( |
| 81 scoped_refptr<PrefRegistry> pref_registry, |
| 82 scoped_refptr<PersistentPrefStore> persistent_pref_store, |
| 83 ConnectCallback callback) |
| 84 : pref_registry_(std::move(pref_registry)), |
| 85 persistent_pref_store_(std::move(persistent_pref_store)), |
| 86 callback_(std::move(callback)) {} |
| 87 |
| 88 void ConnectionBarrier::OnConnect( |
| 89 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> |
| 90 connector_ptr, |
| 91 scoped_refptr<PrefRegistry> pref_registry, |
| 92 std::unordered_map<PrefValueStore::PrefStoreType, |
| 93 mojom::PrefStoreConnectionPtr> connections) { |
| 94 scoped_refptr<PrefStore> managed_prefs = |
| 95 CreatePrefStore(PrefValueStore::MANAGED_STORE, &connections); |
| 96 scoped_refptr<PrefStore> supervised_user_prefs = |
| 97 CreatePrefStore(PrefValueStore::SUPERVISED_USER_STORE, &connections); |
| 98 scoped_refptr<PrefStore> extension_prefs = |
| 99 CreatePrefStore(PrefValueStore::EXTENSION_STORE, &connections); |
| 100 scoped_refptr<PrefStore> command_line_prefs = |
| 101 CreatePrefStore(PrefValueStore::COMMAND_LINE_STORE, &connections); |
| 102 scoped_refptr<PrefStore> recommended_prefs = |
| 103 CreatePrefStore(PrefValueStore::RECOMMENDED_STORE, &connections); |
| 104 scoped_refptr<PrefStore> default_prefs = |
| 105 CreatePrefStore(PrefValueStore::DEFAULT_STORE, &connections); |
| 106 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); |
| 107 auto* pref_value_store = new PrefValueStore( |
| 108 managed_prefs.get(), supervised_user_prefs.get(), extension_prefs.get(), |
| 109 command_line_prefs.get(), persistent_pref_store_.get(), |
| 110 recommended_prefs.get(), default_prefs.get(), pref_notifier); |
| 111 base::ResetAndReturn(&callback_) |
| 112 .Run(base::MakeUnique<::PrefService>( |
| 113 pref_notifier, pref_value_store, persistent_pref_store_.get(), |
| 114 pref_registry_.get(), base::Bind(&DoNothingHandleReadError), true)); |
| 115 connector_ptr->reset(); |
| 116 } |
| 117 |
| 118 void ConnectionBarrier::OnConnectError( |
| 119 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> |
| 120 connector_ptr) { |
| 121 callback_.Run(nullptr); |
| 122 connector_ptr->reset(); |
| 123 } |
| 124 |
| 125 // static |
| 126 void ConnectionBarrier::Create(service_manager::Connector* connector, |
| 127 scoped_refptr<PrefRegistry> pref_registry, |
| 128 ConnectCallback callback) { |
| 129 // Connect to user pref store. |
| 130 mojom::PersistentPrefStoreConnectorPtr persistent_connector_ptr; |
| 131 connector->BindInterface(mojom::kPrefStoreServiceName, |
| 132 &persistent_connector_ptr); |
| 133 auto barrier = make_scoped_refptr(new ConnectionBarrier( |
| 134 std::move(pref_registry), |
| 135 make_scoped_refptr( |
| 136 new PersistentPrefStoreClient(std::move(persistent_connector_ptr))), |
| 137 std::move(callback))); |
| 138 |
| 139 // Connect to all other pref stores. |
| 140 auto connector_ptr = make_scoped_refptr( |
| 141 new RefCountedInterfacePtr<mojom::PrefStoreConnector>()); |
| 142 connector->BindInterface(mojom::kPrefStoreServiceName, &connector_ptr->get()); |
| 143 connector_ptr->get().set_connection_error_handler( |
| 144 base::Bind(&ConnectionBarrier::OnConnectError, barrier, connector_ptr)); |
| 145 connector_ptr->get()->Connect(base::Bind(&ConnectionBarrier::OnConnect, |
| 146 barrier, connector_ptr, |
| 147 std::move(pref_registry))); |
| 148 } |
| 149 |
| 150 } // namespace |
| 151 |
| 152 void ConnectToPrefService(service_manager::Connector* connector, |
| 153 scoped_refptr<PrefRegistry> pref_registry, |
| 154 const ConnectCallback& callback) { |
| 155 ConnectionBarrier::Create(connector, std::move(pref_registry), callback); |
| 156 } |
| 157 |
| 158 } // namespace prefs |
OLD | NEW |