| Index: services/preferences/public/cpp/pref_observer_store.cc
|
| diff --git a/services/preferences/public/cpp/pref_observer_store.cc b/services/preferences/public/cpp/pref_observer_store.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..07f0fca51f267e80f4520f2f671d7b64d68bd978
|
| --- /dev/null
|
| +++ b/services/preferences/public/cpp/pref_observer_store.cc
|
| @@ -0,0 +1,136 @@
|
| +// Copyright 2016 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_observer_store.h"
|
| +
|
| +#include "base/values.h"
|
| +#include "mojo/public/cpp/bindings/array.h"
|
| +#include "services/shell/public/cpp/connector.h"
|
| +
|
| +PrefObserverStore::PrefObserverStore(
|
| + prefs::mojom::PreferenceManagerPtr prefs_manager_ptr)
|
| + : prefs_binding_(this),
|
| + prefs_manager_ptr_(std::move(prefs_manager_ptr)),
|
| + prefs_manager_(nullptr),
|
| + initialized_(false) {
|
| + prefs_manager_ = prefs_manager_ptr_.get();
|
| +}
|
| +
|
| +void PrefObserverStore::Init(const std::set<std::string>& keys) {
|
| + DCHECK(!initialized_);
|
| + DCHECK(prefs_manager_);
|
| + keys_ = keys;
|
| +
|
| + std::vector<std::string> pref_array;
|
| + std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array));
|
| + prefs_manager_->AddObserver(std::move(pref_array),
|
| + prefs_binding_.CreateInterfacePtrAndBind());
|
| +}
|
| +
|
| +bool PrefObserverStore::GetValue(const std::string& key,
|
| + const base::Value** value) const {
|
| + DCHECK(initialized_);
|
| + DCHECK(keys_.find(key) != keys_.end());
|
| +
|
| + return ValueMapPrefStore::GetValue(key, value);
|
| +}
|
| +
|
| +void PrefObserverStore::SetValue(const std::string& key,
|
| + std::unique_ptr<base::Value> value,
|
| + uint32_t flags) {
|
| + DCHECK(keys_.find(key) != keys_.end());
|
| +
|
| + SetValueOnPreferenceManager(key, value.get());
|
| + ValueMapPrefStore::SetValue(key, std::move(value), flags);
|
| +}
|
| +
|
| +void PrefObserverStore::RemoveValue(const std::string& key, uint32_t flags) {
|
| + // TODO(jonross): add preference removal to preferences.mojom
|
| + NOTIMPLEMENTED();
|
| +}
|
| +
|
| +bool PrefObserverStore::GetMutableValue(const std::string& key,
|
| + base::Value** value) {
|
| + DCHECK(initialized_);
|
| + DCHECK(keys_.find(key) != keys_.end());
|
| +
|
| + // TODO(jonross): add unittests once a mutable class of base::Value is used.
|
| + // Such as base::DictionaryValue.
|
| + return ValueMapPrefStore::GetMutableValue(key, value);
|
| +}
|
| +
|
| +void PrefObserverStore::ReportValueChanged(const std::string& key,
|
| + uint32_t flags) {
|
| + ValueMapPrefStore::ReportValueChanged(key, flags);
|
| +}
|
| +
|
| +void PrefObserverStore::SetValueSilently(const std::string& key,
|
| + std::unique_ptr<base::Value> value,
|
| + uint32_t flags) {
|
| + SetValueOnPreferenceManager(key, value.get());
|
| + ValueMapPrefStore::SetValueSilently(key, std::move(value), flags);
|
| +}
|
| +
|
| +PrefObserverStore::~PrefObserverStore() {}
|
| +
|
| +void PrefObserverStore::SetValueOnPreferenceManager(const std::string& key,
|
| + base::Value* value) {
|
| + if (keys_.find(key) == keys_.end())
|
| + return;
|
| +
|
| + // TODO(jonross): replace with struct traits for base::Value
|
| + prefs::mojom::PreferencePtr pref = prefs::mojom::Preference::New();
|
| + switch (value->GetType()) {
|
| + case base::Value::TYPE_INTEGER: {
|
| + pref->type = prefs::mojom::Preference::PreferenceType::INT;
|
| + int val = 0;
|
| + if (!value->GetAsInteger(&val))
|
| + return;
|
| + pref->integer_value_ = val;
|
| + } break;
|
| + case base::Value::TYPE_STRING: {
|
| + pref->type = prefs::mojom::Preference::PreferenceType::STRING;
|
| + std::string str;
|
| + if (!value->GetAsString(&str))
|
| + return;
|
| + pref->string_value_ = str;
|
| + } break;
|
| + default:
|
| + NOTIMPLEMENTED();
|
| + return;
|
| + }
|
| +
|
| + std::unordered_map<std::string, prefs::mojom::PreferencePtr> pref_map;
|
| + pref_map.emplace(std::make_pair(key, std::move(pref)));
|
| + prefs_manager_->SetPreferences(std::move(pref_map));
|
| +}
|
| +
|
| +void PrefObserverStore::OnPreferencesChanged(
|
| + std::unordered_map<std::string, prefs::mojom::PreferencePtr> preferences) {
|
| + for (auto& it : preferences) {
|
| + if (keys_.find(it.first) == keys_.end())
|
| + continue;
|
| + // TODO(jonross): replace with struct traits for base::Value
|
| + base::Value* value = nullptr;
|
| + switch (it.second->type) {
|
| + case prefs::mojom::Preference::PreferenceType::INT:
|
| + value = new base::FundamentalValue((int)it.second->integer_value_);
|
| + break;
|
| + case prefs::mojom::Preference::PreferenceType::STRING: {
|
| + base::Optional<std::string> string_value = it.second->string_value_;
|
| + if (string_value && !string_value.value().empty())
|
| + value = new base::StringValue(string_value.value());
|
| + } break;
|
| + default:
|
| + NOTIMPLEMENTED();
|
| + return;
|
| + }
|
| + ValueMapPrefStore::SetValue(it.first, base::WrapUnique(value), 0);
|
| + }
|
| +
|
| + if (!initialized_) {
|
| + initialized_ = true;
|
| + NotifyInitializationCompleted();
|
| + }
|
| +}
|
|
|