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

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: Update test to provide Ptr directly 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 initialized_(false) {}
17
18 void PrefObserverStore::Init(const std::set<std::string>& keys) {
19 DCHECK(!initialized_);
20 keys_ = keys;
21
22 std::vector<std::string> pref_array;
23 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array));
24 prefs_manager_ptr_->AddObserver(std::move(pref_array),
dcheng 2016/10/31 19:42:46 Does this std::move() have an effect? I think the
jonross 2016/11/01 15:53:03 The move is unnecessary, and removed. For mojo, t
25 prefs_binding_.CreateInterfacePtrAndBind());
26 }
27
28 bool PrefObserverStore::GetValue(const std::string& key,
29 const base::Value** value) const {
30 DCHECK(initialized_);
31 DCHECK(keys_.find(key) != keys_.end());
32
33 return ValueMapPrefStore::GetValue(key, value);
34 }
35
36 void PrefObserverStore::SetValue(const std::string& key,
37 std::unique_ptr<base::Value> value,
38 uint32_t flags) {
39 DCHECK(keys_.find(key) != keys_.end());
40
41 // TODO(jonross): only notify the server if the value changed.
42 SetValueOnPreferenceManager(key, value.get());
43 ValueMapPrefStore::SetValue(key, std::move(value), flags);
44 }
45
46 void PrefObserverStore::RemoveValue(const std::string& key, uint32_t flags) {
47 // TODO(jonross): add preference removal to preferences.mojom
48 NOTIMPLEMENTED();
49 }
50
51 bool PrefObserverStore::GetMutableValue(const std::string& key,
52 base::Value** value) {
53 DCHECK(initialized_);
54 DCHECK(keys_.find(key) != keys_.end());
55
56 return ValueMapPrefStore::GetMutableValue(key, value);
57 }
58
59 void PrefObserverStore::ReportValueChanged(const std::string& key,
60 uint32_t flags) {
61 ValueMapPrefStore::ReportValueChanged(key, flags);
62 }
63
64 void PrefObserverStore::SetValueSilently(const std::string& key,
65 std::unique_ptr<base::Value> value,
66 uint32_t flags) {
67 SetValueOnPreferenceManager(key, value.get());
68 ValueMapPrefStore::SetValueSilently(key, std::move(value), flags);
69 }
70
71 PrefObserverStore::~PrefObserverStore() {}
72
73 void PrefObserverStore::SetValueOnPreferenceManager(const std::string& key,
74 base::Value* value) {
75 if (keys_.find(key) == keys_.end())
76 return;
77
78 base::DictionaryValue prefs;
79 prefs.Set(key, value->CreateDeepCopy());
80 prefs_manager_ptr_->SetPreferences(prefs);
81 }
82
83 void PrefObserverStore::OnPreferencesChanged(
84 const base::DictionaryValue& preferences) {
85 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd();
86 it.Advance()) {
87 if (keys_.find(it.key()) == keys_.end())
88 continue;
89 // We deliberately call the parent to avoid notifying the server again.
90 ValueMapPrefStore::SetValue(it.key(), it.value().CreateDeepCopy(), 0);
91 }
92
93 if (!initialized_) {
94 initialized_ = true;
95 NotifyInitializationCompleted();
96 }
97 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698