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 : initialized_(connection->is_initialized), |
| 13 observer_binding_(this, std::move(connection->observer)) { |
| 14 if (initialized_) |
| 15 OnInitializationCompleted(true); |
| 16 OnPreferencesChanged(std::move(connection->initial_prefs)); |
| 17 } |
| 18 |
| 19 PrefStoreClient::~PrefStoreClient() = default; |
| 20 |
| 21 void PrefStoreClient::AddObserver(PrefStore::Observer* observer) { |
| 22 observers_.AddObserver(observer); |
| 23 } |
| 24 |
| 25 void PrefStoreClient::RemoveObserver(PrefStore::Observer* observer) { |
| 26 observers_.RemoveObserver(observer); |
| 27 } |
| 28 |
| 29 bool PrefStoreClient::HasObservers() const { |
| 30 return observers_.might_have_observers(); |
| 31 } |
| 32 |
| 33 bool PrefStoreClient::IsInitializationComplete() const { |
| 34 return initialized_; |
| 35 } |
| 36 |
| 37 bool PrefStoreClient::GetValue(const std::string& key, |
| 38 const base::Value** result) const { |
| 39 DCHECK(initialized_); |
| 40 return prefs_.GetValue(key, result); |
| 41 } |
| 42 |
| 43 std::unique_ptr<base::DictionaryValue> PrefStoreClient::GetValues() const { |
| 44 return prefs_.AsDictionaryValue(); |
| 45 } |
| 46 |
| 47 void PrefStoreClient::OnPreferencesChanged( |
| 48 std::unique_ptr<base::DictionaryValue> preferences) { |
| 49 for (base::DictionaryValue::Iterator it(*preferences); !it.IsAtEnd(); |
| 50 it.Advance()) { |
| 51 bool changed = false; |
| 52 if (it.value().IsType(base::Value::Type::NONE)) { // Delete |
| 53 if (prefs_.RemoveValue(it.key())) |
| 54 changed = true; |
| 55 } else { |
| 56 if (prefs_.SetValue(it.key(), it.value().CreateDeepCopy())) |
| 57 changed = true; |
| 58 } |
| 59 if (changed) { |
| 60 for (Observer& observer : observers_) |
| 61 observer.OnPrefValueChanged(it.key()); |
| 62 } |
| 63 } |
| 64 } |
| 65 |
| 66 void PrefStoreClient::OnInitializationCompleted(bool succeeded) { |
| 67 if (!initialized_) { |
| 68 initialized_ = true; |
| 69 for (Observer& observer : observers_) |
| 70 observer.OnInitializationCompleted(true); |
| 71 } |
| 72 } |
| 73 |
| 74 } // namespace prefs |
OLD | NEW |