| 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_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 |
| 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 DISALLOW_COPY_AND_ASSIGN(ConnectionBarrier); |
| 34 }; |
| 35 |
| 36 PrefStoreManagerImpl::ConnectionBarrier::ConnectionBarrier( |
| 37 const PrefStorePtrs& pref_store_ptrs, |
| 38 const ConnectCallback& callback) |
| 39 : callback_(callback) { |
| 40 for (const auto& ptr : pref_store_ptrs) { |
| 41 ptr.second->AddObserver( |
| 42 base::Bind(&ConnectionBarrier::OnConnect, this, ptr.first)); |
| 43 } |
| 44 } |
| 45 |
| 46 PrefStoreManagerImpl::ConnectionBarrier::~ConnectionBarrier() = default; |
| 47 |
| 48 void PrefStoreManagerImpl::ConnectionBarrier::OnConnect( |
| 49 PrefValueStore::PrefStoreType type, |
| 50 mojom::PrefStoreConnectionPtr connection_ptr) { |
| 51 connections_.insert(std::make_pair(type, std::move(connection_ptr))); |
| 52 if (connections_.size() == PrefValueStore::PREF_STORE_TYPE_MAX) { |
| 53 // After this method exits |this| will get destroyed so it's safe to move |
| 54 // out the map. |
| 55 callback_.Run(std::move(connections_)); |
| 56 } |
| 57 } |
| 58 |
| 59 PrefStoreManagerImpl::PrefStoreManagerImpl() { |
| 60 LOG(INFO) << "PrefStoreManagerImpl::PrefStoreManagerImpl"; |
| 61 } |
| 62 |
| 63 PrefStoreManagerImpl::~PrefStoreManagerImpl() = default; |
| 64 |
| 65 void PrefStoreManagerImpl::Register(mojom::PrefStorePtr pref_store_ptr, |
| 66 PrefValueStore::PrefStoreType type) { |
| 67 DLOG(INFO) << "Pref store registered: " << type; |
| 68 if (!pref_store_ptrs_.insert(std::make_pair(type, std::move(pref_store_ptr))) |
| 69 .second) { |
| 70 DLOG(FATAL) << "The same pref store registered twice: " << type; |
| 71 } |
| 72 if (AllConnected() && !pending_callbacks_.empty()) { |
| 73 for (const auto& callback : pending_callbacks_) |
| 74 make_scoped_refptr(new ConnectionBarrier(pref_store_ptrs_, callback)); |
| 75 pending_callbacks_.clear(); |
| 76 } |
| 77 } |
| 78 |
| 79 void PrefStoreManagerImpl::Connect(const ConnectCallback& callback) { |
| 80 if (AllConnected()) |
| 81 make_scoped_refptr(new ConnectionBarrier(pref_store_ptrs_, callback)); |
| 82 else |
| 83 pending_callbacks_.push_back(callback); |
| 84 } |
| 85 |
| 86 void PrefStoreManagerImpl::Create( |
| 87 const service_manager::Identity& remote_identity, |
| 88 prefs::mojom::PrefStoreConnectorRequest request) { |
| 89 connector_bindings_.AddBinding(this, std::move(request)); |
| 90 } |
| 91 |
| 92 void PrefStoreManagerImpl::Create( |
| 93 const service_manager::Identity& remote_identity, |
| 94 prefs::mojom::PrefStoreRegistryRequest request) { |
| 95 LOG(INFO) << "PrefStoreManagerImpl::Create"; |
| 96 registry_bindings_.AddBinding(this, std::move(request)); |
| 97 } |
| 98 |
| 99 void PrefStoreManagerImpl::OnStart() { |
| 100 LOG(INFO) << "PrefStoreManagerImpl::OnStart"; |
| 101 } |
| 102 |
| 103 bool PrefStoreManagerImpl::OnConnect( |
| 104 const service_manager::ServiceInfo& remote_info, |
| 105 service_manager::InterfaceRegistry* registry) { |
| 106 LOG(INFO) << "PrefStoreManagerImpl::OnConnect"; |
| 107 registry->AddInterface<prefs::mojom::PrefStoreConnector>(this); |
| 108 registry->AddInterface<prefs::mojom::PrefStoreRegistry>(this); |
| 109 return true; |
| 110 } |
| 111 |
| 112 bool PrefStoreManagerImpl::AllConnected() const { |
| 113 return pref_store_ptrs_.size() == PrefValueStore::PREF_STORE_TYPE_MAX; |
| 114 } |
| 115 |
| 116 } // namespace prefs |
| OLD | NEW |