Chromium Code Reviews| 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 // static | |
| 14 std::unique_ptr<PrefStoreImpl> PrefStoreImpl::Create( | |
| 15 mojom::PrefStoreRegistryPtr registry_ptr, | |
| 16 scoped_refptr<::PrefStore> pref_store, | |
| 17 PrefValueStore::PrefStoreType type) { | |
| 18 mojom::PrefStorePtr ptr; | |
| 19 auto impl = base::MakeUnique<PrefStoreImpl>(std::move(pref_store), | |
| 20 mojo::MakeRequest(&ptr)); | |
| 21 registry_ptr->Register(std::move(ptr), type); | |
| 22 return impl; | |
| 23 } | |
| 24 | |
| 25 PrefStoreImpl::PrefStoreImpl(scoped_refptr<::PrefStore> pref_store, | |
| 26 mojom::PrefStoreRequest request) | |
| 27 : backing_pref_store_(std::move(pref_store)), | |
| 28 initialized_(false), | |
| 29 binding_(this, std::move(request)) { | |
| 30 DCHECK(backing_pref_store_); | |
| 31 if (backing_pref_store_->IsInitializationComplete()) | |
| 32 OnInitializationCompleted(true); | |
| 33 backing_pref_store_->AddObserver(this); | |
| 34 } | |
| 35 | |
| 36 PrefStoreImpl::~PrefStoreImpl() { | |
| 37 backing_pref_store_->RemoveObserver(this); | |
| 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 dictionary->SetWithoutPathExpansion(key, value->CreateDeepCopy()); | |
| 45 } else { | |
| 46 dictionary->SetWithoutPathExpansion(key, base::Value::CreateNullValue()); | |
| 47 } | |
| 48 for (const auto& observer : observers_) | |
| 49 observer->OnPreferencesChanged(dictionary->CreateDeepCopy()); | |
| 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 (initialized_) { | |
|
Sam McNally
2017/02/24 04:14:53
Do we want to catch success after failure as well
tibell
2017/02/27 00:02:54
We could. We'd have to change initialized_ to a 3
Sam McNally
2017/02/27 03:22:20
Ideally PrefStores should behave correctly, but in
| |
| 56 DCHECK(succeeded); | |
| 57 return; | |
|
Sam McNally
2017/02/24 04:14:53
No error handling after DCHECKing.
tibell
2017/02/27 00:02:54
Done.
| |
| 58 } | |
| 59 initialized_ = succeeded; | |
| 60 for (const auto& observer : observers_) | |
| 61 observer->OnInitializationCompleted(succeeded); | |
| 62 } | |
| 63 | |
| 64 void PrefStoreImpl::AddObserver(const AddObserverCallback& callback) { | |
| 65 mojom::PrefStoreObserverPtr observer_ptr; | |
| 66 auto request = mojo::MakeRequest(&observer_ptr); | |
| 67 observers_.push_back(std::move(observer_ptr)); | |
| 68 mojom::PrefStoreConnectionPtr connection = mojom::PrefStoreConnection::New(); | |
| 69 connection->observer = std::move(request); | |
| 70 connection->initial_prefs = backing_pref_store_->GetValues(); | |
| 71 connection->is_initialized = backing_pref_store_->IsInitializationComplete(); | |
| 72 callback.Run(std::move(connection)); | |
| 73 } | |
| 74 | |
| 75 } // namespace prefs | |
| OLD | NEW |