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..0cb9be2b69d2c76fcc9499c922c0b8a38a6e90bc |
--- /dev/null |
+++ b/services/preferences/public/cpp/pref_store_impl.cc |
@@ -0,0 +1,75 @@ |
+// 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(std::move(ptr), type); |
+ 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)) { |
+ dictionary->SetWithoutPathExpansion(key, value->CreateDeepCopy()); |
+ } else { |
+ dictionary->SetWithoutPathExpansion(key, base::Value::CreateNullValue()); |
+ } |
+ for (const auto& observer : observers_) |
+ observer->OnPreferencesChanged(dictionary->CreateDeepCopy()); |
+} |
+ |
+void PrefStoreImpl::OnInitializationCompleted(bool succeeded) { |
+ // Some pref stores call this more than once. We just ignore all calls after |
+ // the first. |
+ if (initialized_) { |
Sam McNally
2017/02/24 04:14:53
Do we want to catch success after failure as well
tibell
2017/02/27 00:02:54
We could. We'd have to change initialized_ to a 3
Sam McNally
2017/02/27 03:22:20
Ideally PrefStores should behave correctly, but in
|
+ DCHECK(succeeded); |
+ return; |
Sam McNally
2017/02/24 04:14:53
No error handling after DCHECKing.
tibell
2017/02/27 00:02:54
Done.
|
+ } |
+ 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)); |
+} |
+ |
+} // namespace prefs |