Index: services/preferences/public/cpp/pref_store_client.cc |
diff --git a/services/preferences/public/cpp/pref_store_client.cc b/services/preferences/public/cpp/pref_store_client.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..040f57488b062c88a6273ce25be73bb8dbd11e06 |
--- /dev/null |
+++ b/services/preferences/public/cpp/pref_store_client.cc |
@@ -0,0 +1,74 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "services/preferences/public/cpp/pref_store_client.h" |
+#include "mojo/public/cpp/bindings/strong_binding.h" |
+#include "services/preferences/public/cpp/pref_store_impl.h" |
+ |
+namespace prefs { |
+ |
+PrefStoreClient::PrefStoreClient(mojom::PrefStoreConnectionPtr connection) |
+ : initialized_(connection->is_initialized), |
+ observer_binding_(this, std::move(connection->observer)) { |
+ if (initialized_) |
+ OnInitializationCompleted(true); |
+ OnPreferencesChanged(std::move(connection->initial_prefs)); |
+} |
+ |
+PrefStoreClient::~PrefStoreClient() = default; |
+ |
+void PrefStoreClient::AddObserver(PrefStore::Observer* observer) { |
+ observers_.AddObserver(observer); |
+} |
+ |
+void PrefStoreClient::RemoveObserver(PrefStore::Observer* observer) { |
+ observers_.RemoveObserver(observer); |
+} |
+ |
+bool PrefStoreClient::HasObservers() const { |
+ return observers_.might_have_observers(); |
+} |
+ |
+bool PrefStoreClient::IsInitializationComplete() const { |
+ return initialized_; |
+} |
+ |
+bool PrefStoreClient::GetValue(const std::string& key, |
+ const base::Value** result) const { |
+ DCHECK(initialized_); |
+ return prefs_.GetValue(key, result); |
+} |
+ |
+std::unique_ptr<base::DictionaryValue> PrefStoreClient::GetValues() const { |
+ return prefs_.AsDictionaryValue(); |
+} |
+ |
+void PrefStoreClient::OnPreferencesChanged( |
+ std::unique_ptr<base::DictionaryValue> preferences) { |
+ for (base::DictionaryValue::Iterator it(*preferences); !it.IsAtEnd(); |
+ it.Advance()) { |
+ bool changed = false; |
+ if (it.value().IsType(base::Value::Type::NONE)) { // Delete |
+ if (prefs_.RemoveValue(it.key())) |
+ changed = true; |
+ } else { |
+ if (prefs_.SetValue(it.key(), it.value().CreateDeepCopy())) |
+ changed = true; |
+ } |
+ if (changed) { |
+ for (Observer& observer : observers_) |
+ observer.OnPrefValueChanged(it.key()); |
+ } |
+ } |
+} |
+ |
+void PrefStoreClient::OnInitializationCompleted(bool succeeded) { |
+ if (!initialized_) { |
+ initialized_ = true; |
+ for (Observer& observer : observers_) |
+ observer.OnInitializationCompleted(true); |
+ } |
+} |
+ |
+} // namespace prefs |