| OLD | NEW |
| 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/public/cpp/pref_service_factory.h" | 5 #include "services/preferences/public/cpp/pref_service_factory.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" | 7 #include "base/callback_helpers.h" |
| 8 #include "components/prefs/persistent_pref_store.h" | 8 #include "components/prefs/persistent_pref_store.h" |
| 9 #include "components/prefs/pref_notifier_impl.h" | 9 #include "components/prefs/pref_notifier_impl.h" |
| 10 #include "components/prefs/pref_registry.h" | 10 #include "components/prefs/pref_registry.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 private: | 31 private: |
| 32 friend class base::RefCounted<RefCountedInterfacePtr<Interface>>; | 32 friend class base::RefCounted<RefCountedInterfacePtr<Interface>>; |
| 33 ~RefCountedInterfacePtr() = default; | 33 ~RefCountedInterfacePtr() = default; |
| 34 | 34 |
| 35 mojo::InterfacePtr<Interface> ptr_; | 35 mojo::InterfacePtr<Interface> ptr_; |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 void DoNothingHandleReadError(PersistentPrefStore::PrefReadError error) {} | 38 void DoNothingHandleReadError(PersistentPrefStore::PrefReadError error) {} |
| 39 | 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( | 40 scoped_refptr<PrefStore> CreatePrefStore( |
| 68 PrefValueStore::PrefStoreType store_type, | 41 PrefValueStore::PrefStoreType store_type, |
| 69 std::unordered_map<PrefValueStore::PrefStoreType, | 42 std::unordered_map<PrefValueStore::PrefStoreType, |
| 70 mojom::PrefStoreConnectionPtr>* connections) { | 43 mojom::PrefStoreConnectionPtr>* connections) { |
| 71 auto pref_store_it = connections->find(store_type); | 44 auto pref_store_it = connections->find(store_type); |
| 72 if (pref_store_it != connections->end()) { | 45 if (pref_store_it != connections->end()) { |
| 73 return make_scoped_refptr( | 46 return make_scoped_refptr( |
| 74 new PrefStoreClient(std::move(pref_store_it->second))); | 47 new PrefStoreClient(std::move(pref_store_it->second))); |
| 75 } else { | 48 } else { |
| 76 return nullptr; | 49 return nullptr; |
| 77 } | 50 } |
| 78 } | 51 } |
| 79 | 52 |
| 80 ConnectionBarrier::ConnectionBarrier( | 53 void OnConnect( |
| 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>> | 54 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> |
| 90 connector_ptr, | 55 connector_ptr, |
| 91 scoped_refptr<PrefRegistry> pref_registry, | 56 scoped_refptr<PrefRegistry> pref_registry, |
| 57 ConnectCallback callback, |
| 58 mojom::PersistentPrefStoreConnectionPtr persistent_pref_store_connection, |
| 92 std::unordered_map<PrefValueStore::PrefStoreType, | 59 std::unordered_map<PrefValueStore::PrefStoreType, |
| 93 mojom::PrefStoreConnectionPtr> connections) { | 60 mojom::PrefStoreConnectionPtr> connections) { |
| 94 scoped_refptr<PrefStore> managed_prefs = | 61 scoped_refptr<PrefStore> managed_prefs = |
| 95 CreatePrefStore(PrefValueStore::MANAGED_STORE, &connections); | 62 CreatePrefStore(PrefValueStore::MANAGED_STORE, &connections); |
| 96 scoped_refptr<PrefStore> supervised_user_prefs = | 63 scoped_refptr<PrefStore> supervised_user_prefs = |
| 97 CreatePrefStore(PrefValueStore::SUPERVISED_USER_STORE, &connections); | 64 CreatePrefStore(PrefValueStore::SUPERVISED_USER_STORE, &connections); |
| 98 scoped_refptr<PrefStore> extension_prefs = | 65 scoped_refptr<PrefStore> extension_prefs = |
| 99 CreatePrefStore(PrefValueStore::EXTENSION_STORE, &connections); | 66 CreatePrefStore(PrefValueStore::EXTENSION_STORE, &connections); |
| 100 scoped_refptr<PrefStore> command_line_prefs = | 67 scoped_refptr<PrefStore> command_line_prefs = |
| 101 CreatePrefStore(PrefValueStore::COMMAND_LINE_STORE, &connections); | 68 CreatePrefStore(PrefValueStore::COMMAND_LINE_STORE, &connections); |
| 102 scoped_refptr<PrefStore> recommended_prefs = | 69 scoped_refptr<PrefStore> recommended_prefs = |
| 103 CreatePrefStore(PrefValueStore::RECOMMENDED_STORE, &connections); | 70 CreatePrefStore(PrefValueStore::RECOMMENDED_STORE, &connections); |
| 104 scoped_refptr<PrefStore> default_prefs = | 71 scoped_refptr<PrefStore> default_prefs = |
| 105 CreatePrefStore(PrefValueStore::DEFAULT_STORE, &connections); | 72 CreatePrefStore(PrefValueStore::DEFAULT_STORE, &connections); |
| 73 scoped_refptr<PersistentPrefStore> persistent_pref_store( |
| 74 new PersistentPrefStoreClient( |
| 75 std::move(persistent_pref_store_connection))); |
| 106 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); | 76 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); |
| 107 auto* pref_value_store = new PrefValueStore( | 77 auto* pref_value_store = new PrefValueStore( |
| 108 managed_prefs.get(), supervised_user_prefs.get(), extension_prefs.get(), | 78 managed_prefs.get(), supervised_user_prefs.get(), extension_prefs.get(), |
| 109 command_line_prefs.get(), persistent_pref_store_.get(), | 79 command_line_prefs.get(), persistent_pref_store.get(), |
| 110 recommended_prefs.get(), default_prefs.get(), pref_notifier); | 80 recommended_prefs.get(), default_prefs.get(), pref_notifier); |
| 111 base::ResetAndReturn(&callback_) | 81 callback.Run(base::MakeUnique<::PrefService>( |
| 112 .Run(base::MakeUnique<::PrefService>( | 82 pref_notifier, pref_value_store, persistent_pref_store.get(), |
| 113 pref_notifier, pref_value_store, persistent_pref_store_.get(), | 83 pref_registry.get(), base::Bind(&DoNothingHandleReadError), true)); |
| 114 pref_registry_.get(), base::Bind(&DoNothingHandleReadError), true)); | |
| 115 connector_ptr->reset(); | 84 connector_ptr->reset(); |
| 116 } | 85 } |
| 117 | 86 |
| 118 void ConnectionBarrier::OnConnectError( | 87 void OnConnectError( |
| 119 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> | 88 scoped_refptr<RefCountedInterfacePtr<mojom::PrefStoreConnector>> |
| 120 connector_ptr) { | 89 connector_ptr, |
| 121 callback_.Run(nullptr); | 90 ConnectCallback callback) { |
| 91 callback.Run(nullptr); |
| 122 connector_ptr->reset(); | 92 connector_ptr->reset(); |
| 123 } | 93 } |
| 124 | 94 |
| 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 | 95 } // namespace |
| 151 | 96 |
| 152 void ConnectToPrefService(service_manager::Connector* connector, | 97 void ConnectToPrefService(service_manager::Connector* connector, |
| 153 scoped_refptr<PrefRegistry> pref_registry, | 98 scoped_refptr<PrefRegistry> pref_registry, |
| 154 const ConnectCallback& callback) { | 99 ConnectCallback callback) { |
| 155 ConnectionBarrier::Create(connector, std::move(pref_registry), callback); | 100 auto connector_ptr = make_scoped_refptr( |
| 101 new RefCountedInterfacePtr<mojom::PrefStoreConnector>()); |
| 102 connector->BindInterface(mojom::kPrefStoreServiceName, &connector_ptr->get()); |
| 103 connector_ptr->get().set_connection_error_handler(base::Bind( |
| 104 &OnConnectError, connector_ptr, base::Passed(ConnectCallback{callback}))); |
| 105 connector_ptr->get()->Connect(base::Bind(&OnConnect, connector_ptr, |
| 106 base::Passed(&pref_registry), |
| 107 base::Passed(&callback))); |
| 156 } | 108 } |
| 157 | 109 |
| 158 } // namespace prefs | 110 } // namespace prefs |
| OLD | NEW |