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

Side by Side Diff: services/preferences/public/cpp/pref_observer_store.cc

Issue 2092453002: Mojom interface for Preferences (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Expand Unittests Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "services/preferences/public/cpp/pref_observer_store.h"
6
7 #include "base/values.h"
8 #include "mojo/common/common_custom_types.mojom.h"
9 #include "mojo/public/cpp/bindings/array.h"
10 #include "services/service_manager/public/cpp/connector.h"
11
12 PrefObserverStore::PrefObserverStore(
13 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr)
14 : prefs_binding_(this),
15 prefs_manager_ptr_(std::move(prefs_manager_ptr)),
16 prefs_manager_(nullptr),
17 initialized_(false) {
18 prefs_manager_ = prefs_manager_ptr_.get();
19 }
20
21 void PrefObserverStore::Init(const std::set<std::string>& keys) {
22 DCHECK(!initialized_);
23 DCHECK(prefs_manager_);
24 keys_ = keys;
25
26 std::vector<std::string> pref_array;
27 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array));
28 prefs_manager_->AddObserver(std::move(pref_array),
29 prefs_binding_.CreateInterfacePtrAndBind());
30 }
31
32 bool PrefObserverStore::GetValue(const std::string& key,
33 const base::Value** value) const {
34 DCHECK(initialized_);
35 DCHECK(keys_.find(key) != keys_.end());
36
37 return ValueMapPrefStore::GetValue(key, value);
38 }
39
40 void PrefObserverStore::SetValue(const std::string& key,
41 std::unique_ptr<base::Value> value,
42 uint32_t flags) {
43 DCHECK(keys_.find(key) != keys_.end());
44
45 // TODO(jonross): only notify the server if the value changed.
46 SetValueOnPreferenceManager(key, value.get());
47 ValueMapPrefStore::SetValue(key, std::move(value), flags);
48 }
49
50 void PrefObserverStore::RemoveValue(const std::string& key, uint32_t flags) {
51 // TODO(jonross): add preference removal to preferences.mojom
52 NOTIMPLEMENTED();
53 }
54
55 bool PrefObserverStore::GetMutableValue(const std::string& key,
56 base::Value** value) {
57 DCHECK(initialized_);
58 DCHECK(keys_.find(key) != keys_.end());
59
60 return ValueMapPrefStore::GetMutableValue(key, value);
61 }
62
63 void PrefObserverStore::ReportValueChanged(const std::string& key,
64 uint32_t flags) {
65 ValueMapPrefStore::ReportValueChanged(key, flags);
66 }
67
68 void PrefObserverStore::SetValueSilently(const std::string& key,
69 std::unique_ptr<base::Value> value,
70 uint32_t flags) {
71 SetValueOnPreferenceManager(key, value.get());
72 ValueMapPrefStore::SetValueSilently(key, std::move(value), flags);
73 }
74
75 PrefObserverStore::~PrefObserverStore() {}
76
77 void PrefObserverStore::SetValueOnPreferenceManager(const std::string& key,
78 base::Value* value) {
79 if (keys_.find(key) == keys_.end())
80 return;
81
82 base::DictionaryValue prefs;
83 prefs.Set(key, value->CreateDeepCopy());
84 prefs_manager_->SetPreferences(prefs);
85 }
86
87 void PrefObserverStore::OnPreferencesChanged(
88 const base::DictionaryValue& preferences) {
89 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd();
90 it.Advance()) {
91 if (keys_.find(it.key()) == keys_.end())
92 continue;
93 // We deliberately call the parent to avoid notifying the server again.
94 ValueMapPrefStore::SetValue(it.key(), it.value().CreateDeepCopy(), 0);
95 }
96
97 if (!initialized_) {
98 initialized_ = true;
99 NotifyInitializationCompleted();
100 }
101 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698