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_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #include "base/values.h" |
| 10 |
| 11 namespace prefs { |
| 12 |
| 13 PrefStoreImpl::PrefStoreImpl(scoped_refptr<::PrefStore> pref_store, |
| 14 mojom::PrefStoreRequest request) |
| 15 : backing_pref_store_(std::move(pref_store)), |
| 16 backing_pref_store_initialized_(false), |
| 17 binding_(this, std::move(request)) { |
| 18 DCHECK(backing_pref_store_); |
| 19 if (backing_pref_store_->IsInitializationComplete()) |
| 20 OnInitializationCompleted(true); |
| 21 backing_pref_store_->AddObserver(this); |
| 22 } |
| 23 |
| 24 PrefStoreImpl::~PrefStoreImpl() { |
| 25 backing_pref_store_->RemoveObserver(this); |
| 26 } |
| 27 |
| 28 // static |
| 29 std::unique_ptr<PrefStoreImpl> PrefStoreImpl::Create( |
| 30 mojom::PrefStoreRegistryPtr registry_ptr, |
| 31 scoped_refptr<::PrefStore> pref_store, |
| 32 PrefValueStore::PrefStoreType type) { |
| 33 mojom::PrefStorePtr ptr; |
| 34 auto impl = base::MakeUnique<PrefStoreImpl>(std::move(pref_store), |
| 35 mojo::MakeRequest(&ptr)); |
| 36 registry_ptr->Register(type, std::move(ptr)); |
| 37 return impl; |
| 38 } |
| 39 |
| 40 void PrefStoreImpl::OnPrefValueChanged(const std::string& key) { |
| 41 auto dictionary = base::MakeUnique<base::DictionaryValue>(); |
| 42 const base::Value* value = nullptr; |
| 43 if (backing_pref_store_->GetValue(key, &value)) { |
| 44 for (const auto& observer : observers_) |
| 45 observer->OnPrefChanged(key, value->CreateDeepCopy()); |
| 46 } else { |
| 47 for (const auto& observer : observers_) |
| 48 observer->OnPrefChanged(key, nullptr); |
| 49 } |
| 50 } |
| 51 |
| 52 void PrefStoreImpl::OnInitializationCompleted(bool succeeded) { |
| 53 // Some pref stores call this more than once. We just ignore all calls after |
| 54 // the first. |
| 55 if (backing_pref_store_initialized_) |
| 56 DCHECK(succeeded); |
| 57 backing_pref_store_initialized_ = succeeded; |
| 58 for (const auto& observer : observers_) |
| 59 observer->OnInitializationCompleted(succeeded); |
| 60 } |
| 61 |
| 62 void PrefStoreImpl::AddObserver(const AddObserverCallback& callback) { |
| 63 mojom::PrefStoreObserverPtr observer_ptr; |
| 64 auto request = mojo::MakeRequest(&observer_ptr); |
| 65 observers_.push_back(std::move(observer_ptr)); |
| 66 callback.Run(mojom::PrefStoreConnection::New( |
| 67 std::move(request), backing_pref_store_->GetValues(), |
| 68 backing_pref_store_->IsInitializationComplete())); |
| 69 } |
| 70 |
| 71 } // namespace prefs |
OLD | NEW |