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_client.h" |
| 6 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 7 #include "services/preferences/public/cpp/pref_store_impl.h" |
| 8 |
| 9 namespace prefs { |
| 10 |
| 11 PrefStoreClient::PrefStoreClient(mojom::PrefStoreConnectionPtr connection) |
| 12 : cached_prefs_(std::move(connection->initial_prefs)), |
| 13 initialized_(connection->is_initialized), |
| 14 observer_binding_(this, std::move(connection->observer)) {} |
| 15 |
| 16 PrefStoreClient::~PrefStoreClient() = default; |
| 17 |
| 18 void PrefStoreClient::AddObserver(PrefStore::Observer* observer) { |
| 19 observers_.AddObserver(observer); |
| 20 } |
| 21 |
| 22 void PrefStoreClient::RemoveObserver(PrefStore::Observer* observer) { |
| 23 observers_.RemoveObserver(observer); |
| 24 } |
| 25 |
| 26 bool PrefStoreClient::HasObservers() const { |
| 27 return observers_.might_have_observers(); |
| 28 } |
| 29 |
| 30 bool PrefStoreClient::IsInitializationComplete() const { |
| 31 return initialized_; |
| 32 } |
| 33 |
| 34 bool PrefStoreClient::GetValue(const std::string& key, |
| 35 const base::Value** result) const { |
| 36 DCHECK(initialized_); |
| 37 return cached_prefs_->Get(key, result); |
| 38 } |
| 39 |
| 40 std::unique_ptr<base::DictionaryValue> PrefStoreClient::GetValues() const { |
| 41 return cached_prefs_->CreateDeepCopy(); |
| 42 } |
| 43 |
| 44 void PrefStoreClient::OnPrefChanged(const std::string& key, |
| 45 std::unique_ptr<base::Value> value) { |
| 46 bool changed = false; |
| 47 if (!value) { // Delete |
| 48 if (cached_prefs_->RemovePath(key, nullptr)) |
| 49 changed = true; |
| 50 } else { |
| 51 const base::Value* prev; |
| 52 if (cached_prefs_->Get(key, &prev)) { |
| 53 if (!prev->Equals(value.get())) { |
| 54 cached_prefs_->Set(key, std::move(value)); |
| 55 changed = true; |
| 56 } |
| 57 } else { |
| 58 cached_prefs_->Set(key, std::move(value)); |
| 59 changed = true; |
| 60 } |
| 61 } |
| 62 if (changed) { |
| 63 for (Observer& observer : observers_) |
| 64 observer.OnPrefValueChanged(key); |
| 65 } |
| 66 } |
| 67 |
| 68 void PrefStoreClient::OnInitializationCompleted(bool succeeded) { |
| 69 if (!initialized_) { |
| 70 initialized_ = true; |
| 71 for (Observer& observer : observers_) |
| 72 observer.OnInitializationCompleted(true); |
| 73 } |
| 74 } |
| 75 |
| 76 } // namespace prefs |
OLD | NEW |