Chromium Code Reviews| 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_store_impl.h" | 5 #include "services/preferences/public/cpp/pref_store_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <unordered_set> | |
| 8 | 9 |
| 10 #include "base/stl_util.h" | |
| 9 #include "base/values.h" | 11 #include "base/values.h" |
| 10 | 12 |
| 11 namespace prefs { | 13 namespace prefs { |
| 12 | 14 |
| 15 class PrefStoreImpl::Observer { | |
| 16 public: | |
| 17 Observer(mojom::PrefStoreObserverPtr observer, | |
| 18 std::unordered_set<std::string> prefs) | |
| 19 : observer_(std::move(observer)), prefs_(std::move(prefs)) {} | |
| 20 | |
| 21 Observer(Observer&& other) = default; | |
| 22 Observer& operator=(Observer&& other) = default; | |
| 23 | |
| 24 void OnInitializationCompleted(bool succeeded) { | |
| 25 observer_->OnInitializationCompleted(succeeded); | |
| 26 } | |
| 27 | |
| 28 void OnPrefChanged(const std::string& key, const base::Value& value) { | |
| 29 if (!base::ContainsKey(prefs_, key)) | |
| 30 return; | |
| 31 | |
| 32 observer_->OnPrefChanged(key, value.CreateDeepCopy()); | |
| 33 } | |
| 34 | |
| 35 void OnPrefRemoved(const std::string& key) { | |
| 36 if (!base::ContainsKey(prefs_, key)) | |
| 37 return; | |
| 38 | |
| 39 observer_->OnPrefChanged(key, nullptr); | |
| 40 } | |
| 41 | |
| 42 private: | |
| 43 mojom::PrefStoreObserverPtr observer_; | |
| 44 const std::unordered_set<std::string> prefs_; | |
| 45 | |
| 46 DISALLOW_COPY_AND_ASSIGN(Observer); | |
| 47 }; | |
| 48 | |
| 13 PrefStoreImpl::PrefStoreImpl(scoped_refptr<::PrefStore> pref_store, | 49 PrefStoreImpl::PrefStoreImpl(scoped_refptr<::PrefStore> pref_store, |
| 14 mojom::PrefStoreRequest request) | 50 mojom::PrefStoreRequest request) |
| 15 : backing_pref_store_(std::move(pref_store)), | 51 : backing_pref_store_(std::move(pref_store)), |
| 16 backing_pref_store_initialized_(false), | 52 backing_pref_store_initialized_(false), |
| 17 binding_(this, std::move(request)) { | 53 binding_(this, std::move(request)) { |
| 18 DCHECK(backing_pref_store_); | 54 DCHECK(backing_pref_store_); |
| 19 if (backing_pref_store_->IsInitializationComplete()) | 55 if (backing_pref_store_->IsInitializationComplete()) |
| 20 OnInitializationCompleted(true); | 56 OnInitializationCompleted(true); |
| 21 backing_pref_store_->AddObserver(this); | 57 backing_pref_store_->AddObserver(this); |
| 22 } | 58 } |
| 23 | 59 |
| 24 PrefStoreImpl::~PrefStoreImpl() { | 60 PrefStoreImpl::~PrefStoreImpl() { |
| 25 backing_pref_store_->RemoveObserver(this); | 61 backing_pref_store_->RemoveObserver(this); |
| 26 } | 62 } |
| 27 | 63 |
| 28 // static | 64 // static |
| 29 std::unique_ptr<PrefStoreImpl> PrefStoreImpl::Create( | 65 std::unique_ptr<PrefStoreImpl> PrefStoreImpl::Create( |
| 30 mojom::PrefStoreRegistryPtr registry_ptr, | 66 mojom::PrefStoreRegistry* registry_ptr, |
| 31 scoped_refptr<::PrefStore> pref_store, | 67 scoped_refptr<::PrefStore> pref_store, |
| 32 PrefValueStore::PrefStoreType type) { | 68 PrefValueStore::PrefStoreType type) { |
| 33 mojom::PrefStorePtr ptr; | 69 mojom::PrefStorePtr ptr; |
| 34 auto impl = base::MakeUnique<PrefStoreImpl>(std::move(pref_store), | 70 auto impl = base::MakeUnique<PrefStoreImpl>(std::move(pref_store), |
| 35 mojo::MakeRequest(&ptr)); | 71 mojo::MakeRequest(&ptr)); |
| 36 registry_ptr->Register(type, std::move(ptr)); | 72 registry_ptr->Register(type, std::move(ptr)); |
| 37 return impl; | 73 return impl; |
| 38 } | 74 } |
| 39 | 75 |
| 40 void PrefStoreImpl::OnPrefValueChanged(const std::string& key) { | 76 void PrefStoreImpl::OnPrefValueChanged(const std::string& key) { |
| 41 auto dictionary = base::MakeUnique<base::DictionaryValue>(); | 77 auto dictionary = base::MakeUnique<base::DictionaryValue>(); |
| 42 const base::Value* value = nullptr; | 78 const base::Value* value = nullptr; |
| 43 if (backing_pref_store_->GetValue(key, &value)) { | 79 if (backing_pref_store_->GetValue(key, &value)) { |
| 44 for (const auto& observer : observers_) | 80 for (auto& observer : observers_) |
|
tibell
2017/03/27 03:37:56
const
Sam McNally
2017/03/27 06:44:40
Done.
| |
| 45 observer->OnPrefChanged(key, value->CreateDeepCopy()); | 81 observer.OnPrefChanged(key, *value); |
| 46 } else { | 82 } else { |
| 47 for (const auto& observer : observers_) | 83 for (auto& observer : observers_) |
|
tibell
2017/03/27 03:37:56
const
Sam McNally
2017/03/27 06:44:40
Done.
| |
| 48 observer->OnPrefChanged(key, nullptr); | 84 observer.OnPrefRemoved(key); |
| 49 } | 85 } |
| 50 } | 86 } |
| 51 | 87 |
| 52 void PrefStoreImpl::OnInitializationCompleted(bool succeeded) { | 88 void PrefStoreImpl::OnInitializationCompleted(bool succeeded) { |
| 53 // Some pref stores call this more than once. We just ignore all calls after | 89 // Some pref stores call this more than once. We just ignore all calls after |
| 54 // the first. | 90 // the first. |
| 55 if (backing_pref_store_initialized_) | 91 if (backing_pref_store_initialized_) |
| 56 DCHECK(succeeded); | 92 DCHECK(succeeded); |
| 57 backing_pref_store_initialized_ = succeeded; | 93 backing_pref_store_initialized_ = succeeded; |
| 58 for (const auto& observer : observers_) | 94 for (auto& observer : observers_) |
|
tibell
2017/03/27 03:37:56
const
Sam McNally
2017/03/27 06:44:40
Done.
| |
| 59 observer->OnInitializationCompleted(succeeded); | 95 observer.OnInitializationCompleted(succeeded); |
| 60 } | 96 } |
| 61 | 97 |
| 62 void PrefStoreImpl::AddObserver(const AddObserverCallback& callback) { | 98 void PrefStoreImpl::AddObserver( |
| 99 const std::vector<std::string>& prefs_to_observe, | |
| 100 const AddObserverCallback& callback) { | |
| 63 mojom::PrefStoreObserverPtr observer_ptr; | 101 mojom::PrefStoreObserverPtr observer_ptr; |
| 64 auto request = mojo::MakeRequest(&observer_ptr); | 102 auto request = mojo::MakeRequest(&observer_ptr); |
| 65 observers_.push_back(std::move(observer_ptr)); | 103 observers_.emplace_back( |
| 104 std::move(observer_ptr), | |
| 105 std::unordered_set<std::string>(prefs_to_observe.begin(), | |
| 106 prefs_to_observe.end())); | |
| 66 callback.Run(mojom::PrefStoreConnection::New( | 107 callback.Run(mojom::PrefStoreConnection::New( |
| 67 std::move(request), backing_pref_store_->GetValues(), | 108 std::move(request), backing_pref_store_->GetValues(), |
| 68 backing_pref_store_->IsInitializationComplete())); | 109 backing_pref_store_->IsInitializationComplete())); |
| 69 } | 110 } |
| 70 | 111 |
| 71 } // namespace prefs | 112 } // namespace prefs |
| OLD | NEW |