Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(275)

Unified Diff: services/preferences/public/cpp/pref_store_client.cc

Issue 2635153002: Pref service: expose all read-only PrefStores through Mojo (Closed)
Patch Set: Address bauerb's review comments Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..fd6708cd679f9822def4979d6eec8615e1e797b8
--- /dev/null
+++ b/services/preferences/public/cpp/pref_store_client.cc
@@ -0,0 +1,77 @@
+// 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)
+ : cached_prefs_(std::move(connection->initial_prefs)),
+ initialized_(connection->is_initialized),
+ observer_binding_(this, std::move(connection->observer)) {}
+
+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 cached_prefs_->Get(key, result);
+}
+
+std::unique_ptr<base::DictionaryValue> PrefStoreClient::GetValues() const {
+ DCHECK(initialized_);
+ return cached_prefs_->CreateDeepCopy();
+}
+
+PrefStoreClient::~PrefStoreClient() = default;
+
+void PrefStoreClient::OnPrefChanged(const std::string& key,
+ std::unique_ptr<base::Value> value) {
+ bool changed = false;
+ if (!value) { // Delete
+ if (cached_prefs_->RemovePath(key, nullptr))
+ changed = true;
+ } else {
+ const base::Value* prev;
+ if (cached_prefs_->Get(key, &prev)) {
+ if (!prev->Equals(value.get())) {
+ cached_prefs_->Set(key, std::move(value));
+ changed = true;
+ }
+ } else {
+ cached_prefs_->Set(key, std::move(value));
+ changed = true;
+ }
+ }
+ if (changed) {
+ for (Observer& observer : observers_)
+ observer.OnPrefValueChanged(key);
+ }
+}
+
+void PrefStoreClient::OnInitializationCompleted(bool succeeded) {
+ if (!initialized_) {
+ initialized_ = true;
+ for (Observer& observer : observers_)
+ observer.OnInitializationCompleted(true);
+ }
+}
+
+} // namespace prefs
« no previous file with comments | « services/preferences/public/cpp/pref_store_client.h ('k') | services/preferences/public/cpp/pref_store_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698