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

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

Issue 2474653003: PreferencesManager (Closed)
Patch Set: Address Review Comments Created 4 years 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "services/preferences/public/cpp/pref_observer_store.h" 5 #include "services/preferences/public/cpp/pref_observer_store.h"
6 6
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "mojo/common/common_custom_types.mojom.h" 8 #include "mojo/common/common_custom_types.mojom.h"
9 #include "mojo/public/cpp/bindings/array.h" 9 #include "mojo/public/cpp/bindings/array.h"
10 #include "services/service_manager/public/cpp/connector.h" 10 #include "services/service_manager/public/cpp/connector.h"
11 11
12 PrefObserverStore::PrefObserverStore( 12 PrefObserverStore::PrefObserverStore(
13 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr) 13 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr)
14 : prefs_binding_(this), 14 : prefs_binding_(this),
15 prefs_manager_ptr_(std::move(prefs_manager_ptr)), 15 prefs_manager_ptr_(std::move(prefs_manager_ptr)),
16 initialized_(false) {} 16 initialized_(false) {}
17 17
18 void PrefObserverStore::Init(const std::set<std::string>& keys) { 18 void PrefObserverStore::Subscribe(const std::set<std::string>& keys) {
19 DCHECK(!initialized_); 19 if (keys_.empty())
20 keys_ = keys; 20 prefs_manager_ptr_->AddObserver(prefs_binding_.CreateInterfacePtrAndBind());
21 keys_.insert(keys.begin(), keys.end());
21 22
22 std::vector<std::string> pref_array; 23 std::vector<std::string> pref_array;
23 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array)); 24 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array));
24 prefs_manager_ptr_->AddObserver(pref_array, 25 prefs_manager_ptr_->Subscribe(pref_array);
25 prefs_binding_.CreateInterfacePtrAndBind());
26 } 26 }
27 27
28 bool PrefObserverStore::GetValue(const std::string& key, 28 bool PrefObserverStore::GetValue(const std::string& key,
29 const base::Value** value) const { 29 const base::Value** value) const {
30 DCHECK(initialized_); 30 DCHECK(initialized_);
31 DCHECK(keys_.find(key) != keys_.end()); 31 DCHECK(keys_.find(key) != keys_.end());
32 32
33 return ValueMapPrefStore::GetValue(key, value); 33 return ValueMapPrefStore::GetValue(key, value);
34 } 34 }
35 35
(...skipping 29 matching lines...) Expand all
65 ValueMapPrefStore::ReportValueChanged(key, flags); 65 ValueMapPrefStore::ReportValueChanged(key, flags);
66 } 66 }
67 67
68 void PrefObserverStore::SetValueSilently(const std::string& key, 68 void PrefObserverStore::SetValueSilently(const std::string& key,
69 std::unique_ptr<base::Value> value, 69 std::unique_ptr<base::Value> value,
70 uint32_t flags) { 70 uint32_t flags) {
71 SetValueOnPreferenceManager(key, *value); 71 SetValueOnPreferenceManager(key, *value);
72 ValueMapPrefStore::SetValueSilently(key, std::move(value), flags); 72 ValueMapPrefStore::SetValueSilently(key, std::move(value), flags);
73 } 73 }
74 74
75 PrefObserverStore::~PrefObserverStore() {} 75 PrefObserverStore::~PrefObserverStore() {
76 ClearObservers();
77 }
76 78
77 void PrefObserverStore::SetValueOnPreferenceManager(const std::string& key, 79 void PrefObserverStore::SetValueOnPreferenceManager(const std::string& key,
78 const base::Value& value) { 80 const base::Value& value) {
79 if (keys_.find(key) == keys_.end()) 81 if (keys_.find(key) == keys_.end())
80 return; 82 return;
81 83
82 base::DictionaryValue prefs; 84 base::DictionaryValue prefs;
83 prefs.Set(key, value.CreateDeepCopy()); 85 prefs.Set(key, value.CreateDeepCopy());
84 prefs_manager_ptr_->SetPreferences(prefs); 86 prefs_manager_ptr_->SetPreferences(prefs);
85 } 87 }
86 88
87 void PrefObserverStore::OnPreferencesChanged( 89 void PrefObserverStore::OnPreferencesChanged(
88 const base::DictionaryValue& preferences) { 90 const base::DictionaryValue& preferences) {
91 if (!initialized_) {
92 initialized_ = true;
93 NotifyInitializationCompleted();
94 }
95
89 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd(); 96 for (base::DictionaryValue::Iterator it(preferences); !it.IsAtEnd();
90 it.Advance()) { 97 it.Advance()) {
91 if (keys_.find(it.key()) == keys_.end()) 98 if (keys_.find(it.key()) == keys_.end())
92 continue; 99 continue;
93 // We deliberately call the parent to avoid notifying the server again.
94 ValueMapPrefStore::SetValue(it.key(), it.value().CreateDeepCopy(), 0); 100 ValueMapPrefStore::SetValue(it.key(), it.value().CreateDeepCopy(), 0);
95 } 101 }
96
97 if (!initialized_) {
98 initialized_ = true;
99 NotifyInitializationCompleted();
100 }
101 } 102 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698