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

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: Switch to using DictionaryValue Created 4 years, 3 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 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/shell/public/cpp/connector.h"
11
12 PrefObserverStore::PrefObserverStore(
13 prefs::mojom::PreferenceManagerPtr 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 SetValueOnPreferenceManager(key, value.get());
sadrul 2016/10/06 14:09:41 It'd be nice to not notify the server if the value
jonross 2016/10/06 21:36:20 Done.
46 ValueMapPrefStore::SetValue(key, std::move(value), flags);
47 }
48
49 void PrefObserverStore::RemoveValue(const std::string& key, uint32_t flags) {
50 // TODO(jonross): add preference removal to preferences.mojom
51 NOTIMPLEMENTED();
52 }
53
54 bool PrefObserverStore::GetMutableValue(const std::string& key,
55 base::Value** value) {
56 DCHECK(initialized_);
57 DCHECK(keys_.find(key) != keys_.end());
58
59 // TODO(jonross): add unittests once a mutable class of base::Value is used.
60 // Such as base::DictionaryValue.
61 return ValueMapPrefStore::GetMutableValue(key, value);
62 }
63
64 void PrefObserverStore::ReportValueChanged(const std::string& key,
65 uint32_t flags) {
66 ValueMapPrefStore::ReportValueChanged(key, flags);
67 }
68
69 void PrefObserverStore::SetValueSilently(const std::string& key,
70 std::unique_ptr<base::Value> value,
71 uint32_t flags) {
72 SetValueOnPreferenceManager(key, value.get());
73 ValueMapPrefStore::SetValueSilently(key, std::move(value), flags);
74 }
75
76 PrefObserverStore::~PrefObserverStore() {}
77
78 void PrefObserverStore::SetValueOnPreferenceManager(const std::string& key,
79 base::Value* value) {
80 if (keys_.find(key) == keys_.end())
81 return;
82
83 base::DictionaryValue prefs;
84 prefs.Set(key, value->CreateDeepCopy());
85 prefs_manager_->SetPreferences(prefs);
86 }
87
88 void PrefObserverStore::OnPreferencesChanged(
89 const base::DictionaryValue& preferences) {
90 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd();
91 it.Advance()) {
92 if (keys_.find(it.key()) == keys_.end())
93 continue;
94 ValueMapPrefStore::SetValue(it.key(), it.value().CreateDeepCopy(), 0);
sadrul 2016/10/06 14:09:41 Add a comment here that we deliberately call the p
jonross 2016/10/06 21:36:20 Done.
95 }
96
97 if (!initialized_) {
98 initialized_ = true;
99 NotifyInitializationCompleted();
100 }
101 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698