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

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

Issue 2635153002: Pref service: expose all read-only PrefStores through Mojo (Closed)
Patch Set: Merge Created 3 years, 10 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_impl.cc
diff --git a/services/preferences/public/cpp/pref_store_impl.cc b/services/preferences/public/cpp/pref_store_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..adcb367cae91ca1ac9812bb5e38caa5b607f45a2
--- /dev/null
+++ b/services/preferences/public/cpp/pref_store_impl.cc
@@ -0,0 +1,73 @@
+// 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_impl.h"
+
+#include <memory>
+
+#include "base/values.h"
+
+namespace prefs {
+
+// static
+std::unique_ptr<PrefStoreImpl> PrefStoreImpl::Create(
+ mojom::PrefStoreRegistryPtr registry_ptr,
+ scoped_refptr<::PrefStore> pref_store,
+ PrefValueStore::PrefStoreType type) {
+ mojom::PrefStorePtr ptr;
+ auto impl = base::MakeUnique<PrefStoreImpl>(std::move(pref_store),
+ mojo::MakeRequest(&ptr));
+ registry_ptr->Register(type, std::move(ptr));
+ return impl;
+}
+
+PrefStoreImpl::PrefStoreImpl(scoped_refptr<::PrefStore> pref_store,
+ mojom::PrefStoreRequest request)
+ : backing_pref_store_(std::move(pref_store)),
+ initialized_(false),
+ binding_(this, std::move(request)) {
+ DCHECK(backing_pref_store_);
+ if (backing_pref_store_->IsInitializationComplete())
+ OnInitializationCompleted(true);
+ backing_pref_store_->AddObserver(this);
+}
+
+PrefStoreImpl::~PrefStoreImpl() {
+ backing_pref_store_->RemoveObserver(this);
+}
+
+void PrefStoreImpl::OnPrefValueChanged(const std::string& key) {
+ auto dictionary = base::MakeUnique<base::DictionaryValue>();
+ const base::Value* value = nullptr;
+ if (backing_pref_store_->GetValue(key, &value)) {
+ for (const auto& observer : observers_)
+ observer->OnPrefChanged(key, value->CreateDeepCopy());
+ } else {
+ for (const auto& observer : observers_)
+ observer->OnPrefChanged(key, base::Value::CreateNullValue());
Sam McNally 2017/03/03 03:47:58 nullptr?
tibell 2017/03/07 00:52:52 Done.
+ }
+}
+
+void PrefStoreImpl::OnInitializationCompleted(bool succeeded) {
+ // Some pref stores call this more than once. We just ignore all calls after
+ // the first.
+ if (initialized_)
+ DCHECK(succeeded);
+ initialized_ = succeeded;
+ for (const auto& observer : observers_)
+ observer->OnInitializationCompleted(succeeded);
+}
+
+void PrefStoreImpl::AddObserver(const AddObserverCallback& callback) {
+ mojom::PrefStoreObserverPtr observer_ptr;
+ auto request = mojo::MakeRequest(&observer_ptr);
+ observers_.push_back(std::move(observer_ptr));
+ mojom::PrefStoreConnectionPtr connection = mojom::PrefStoreConnection::New();
+ connection->observer = std::move(request);
+ connection->initial_prefs = backing_pref_store_->GetValues();
+ connection->is_initialized = backing_pref_store_->IsInitializationComplete();
+ callback.Run(std::move(connection));
Sam McNally 2017/03/03 03:47:58 Inline constructing |connection|.
tibell 2017/03/07 00:52:52 Done.
+}
+
+} // namespace prefs

Powered by Google App Engine
This is Rietveld 408576698