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