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

Unified Diff: components/prefs/pref_observer_store.cc

Issue 2092453002: Mojom interface for Preferences (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 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: components/prefs/pref_observer_store.cc
diff --git a/components/prefs/pref_observer_store.cc b/components/prefs/pref_observer_store.cc
new file mode 100644
index 0000000000000000000000000000000000000000..ffebc01e03d8c148347d10ee62c0aaab1bf40c25
--- /dev/null
+++ b/components/prefs/pref_observer_store.cc
@@ -0,0 +1,132 @@
+// 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 "components/prefs/pref_observer_store.h"
+
+#include "base/values.h"
+#include "services/shell/public/cpp/connector.h"
+
+PrefObserverStore::PrefObserverStore(shell::Connector* connector)
+ : prefs_binding_(this), prefs_manager_(nullptr), initialized_(false) {
+ if (connector) {
+ connector->ConnectToInterface("exe:chrome", &prefs_manager_ptr_);
+ prefs_manager_ = prefs_manager_ptr_.get();
+ }
+}
+
+void PrefObserverStore::Init(const std::set<mojo::String>& keys) {
+ DCHECK(prefs_manager_);
+ keys_ = keys;
+
+ prefs::mojom::PreferenceListPtr prefs_list =
+ prefs::mojom::PreferenceList::New();
+ mojo::Array<mojo::String> pref_array = mojo::Array<mojo::String>::From(keys);
+ prefs_list->preferences = std::move(pref_array);
+ prefs_manager_->AddObserver(std::move(prefs_list),
+ prefs_binding_.CreateInterfacePtrAndBind());
+}
+
+bool PrefObserverStore::GetValue(const std::string& key,
+ const base::Value** value) const {
+ // TODO(jonross): determine if we should begin observing keys not provided
+ // during Init().
+ if (keys_.find(key) == keys_.end())
+ return false;
sadrul 2016/06/23 17:30:17 Maybe start with a DCHECK()
jonross 2016/07/19 19:55:48 Done.
+
sadrul 2016/06/23 17:30:17 Should this also check for initialized_?
jonross 2016/06/24 15:13:06 Possibly. Depends on how we wish for clients to ha
sadrul 2016/07/19 15:08:53 When does it receive OnInitializedCompleted()? I
jonross 2016/07/19 17:24:11 OnInitializedCompleted() is called from OnPreferen
jonross 2016/07/19 19:55:48 Done.
+ return ValueMapPrefStore::GetValue(key, value);
+}
+
+void PrefObserverStore::SetValue(const std::string& key,
+ std::unique_ptr<base::Value> value,
+ uint32_t flags) {
+ if (keys_.find(key) == keys_.end())
+ return;
+
+ 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) {
+ // TODO(jonross): determine how we can track edits to base::Value so that we
+ // can appropriately set the value on the PreferenceManger.
+ NOTIMPLEMENTED();
+ return false;
+}
+
+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())
sadrul 2016/06/23 17:30:17 How do you add a new preference?
jonross 2016/06/24 15:13:06 First pass there is no method. I had planned to fo
+ return;
sadrul 2016/06/23 17:30:17 DCHECK()?
jonross 2016/07/19 19:55:48 Done.
+
+ // 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;
+ value->GetAsInteger(&val);
+ pref->integer_value_ = val;
+ } break;
+ case base::Value::TYPE_STRING: {
+ pref->type = prefs::mojom::Preference::PreferenceType::STRING;
+ std::string str;
+ value->GetAsString(&str);
+ pref->string_value_ = str;
+ } break;
+ default:
+ NOTIMPLEMENTED();
+ return;
+ }
+
+ prefs::mojom::PreferenceMapPtr pref_map = prefs::mojom::PreferenceMap::New();
+ pref_map->preferences.insert(key, std::move(pref));
+ prefs_manager_->SetPreferences(std::move(pref_map));
+}
+
+void PrefObserverStore::OnPreferencesChanged(
+ prefs::mojom::PreferenceMapPtr preferences) {
+ for (auto& it : preferences->preferences) {
+ if (keys_.find(it.first) == keys_.end())
+ continue;
+ // TODO(jonross): replace with struct traits for base::Value
+ base::Value* value;
+ 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:
+ value = new base::StringValue(it.second->string_value_);
+ break;
+ default:
+ NOTIMPLEMENTED();
+ return;
+ }
+ ValueMapPrefStore::SetValue(it.first, base::WrapUnique(value), 0);
+ }
+
+ if (!initialized_) {
+ initialized_ = true;
+ NotifyInitializationCompleted();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698