OLD | NEW |
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/public/cpp/bindings/array.h" | 8 #include "mojo/public/cpp/bindings/array.h" |
9 #include "services/service_manager/public/cpp/connector.h" | 9 #include "services/service_manager/public/cpp/connector.h" |
10 | 10 |
| 11 namespace preferences { |
| 12 |
11 PrefObserverStore::PrefObserverStore( | 13 PrefObserverStore::PrefObserverStore( |
12 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr) | 14 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr) |
13 : prefs_binding_(this), | 15 : prefs_binding_(this), |
14 prefs_manager_ptr_(std::move(prefs_manager_ptr)), | 16 prefs_manager_ptr_(std::move(prefs_manager_ptr)), |
15 initialized_(false) {} | 17 initialized_(false) {} |
16 | 18 |
17 void PrefObserverStore::Init(const std::set<std::string>& keys) { | 19 void PrefObserverStore::Subscribe(const std::set<std::string>& keys) { |
18 DCHECK(!initialized_); | 20 if (keys_.empty()) |
19 keys_ = keys; | 21 prefs_manager_ptr_->AddObserver(prefs_binding_.CreateInterfacePtrAndBind()); |
| 22 keys_.insert(keys.begin(), keys.end()); |
20 | 23 |
21 std::vector<std::string> pref_array; | 24 std::vector<std::string> pref_array; |
22 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array)); | 25 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array)); |
23 prefs_manager_ptr_->AddObserver(pref_array, | 26 prefs_manager_ptr_->Subscribe(pref_array); |
24 prefs_binding_.CreateInterfacePtrAndBind()); | |
25 } | 27 } |
26 | 28 |
27 bool PrefObserverStore::GetValue(const std::string& key, | 29 bool PrefObserverStore::GetValue(const std::string& key, |
28 const base::Value** value) const { | 30 const base::Value** value) const { |
29 DCHECK(initialized_); | 31 DCHECK(initialized_); |
30 DCHECK(keys_.find(key) != keys_.end()); | 32 DCHECK(keys_.find(key) != keys_.end()); |
31 | 33 |
32 return ValueMapPrefStore::GetValue(key, value); | 34 return ValueMapPrefStore::GetValue(key, value); |
33 } | 35 } |
34 | 36 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 if (keys_.find(key) == keys_.end()) | 80 if (keys_.find(key) == keys_.end()) |
79 return; | 81 return; |
80 | 82 |
81 auto prefs = base::MakeUnique<base::DictionaryValue>(); | 83 auto prefs = base::MakeUnique<base::DictionaryValue>(); |
82 prefs->Set(key, value.CreateDeepCopy()); | 84 prefs->Set(key, value.CreateDeepCopy()); |
83 prefs_manager_ptr_->SetPreferences(std::move(prefs)); | 85 prefs_manager_ptr_->SetPreferences(std::move(prefs)); |
84 } | 86 } |
85 | 87 |
86 void PrefObserverStore::OnPreferencesChanged( | 88 void PrefObserverStore::OnPreferencesChanged( |
87 std::unique_ptr<base::DictionaryValue> preferences) { | 89 std::unique_ptr<base::DictionaryValue> preferences) { |
| 90 if (!initialized_) { |
| 91 initialized_ = true; |
| 92 NotifyInitializationCompleted(); |
| 93 } |
| 94 |
88 for (base::DictionaryValue::Iterator it(*preferences); !it.IsAtEnd(); | 95 for (base::DictionaryValue::Iterator it(*preferences); !it.IsAtEnd(); |
89 it.Advance()) { | 96 it.Advance()) { |
90 if (keys_.find(it.key()) == keys_.end()) | 97 if (keys_.find(it.key()) == keys_.end()) |
91 continue; | 98 continue; |
92 // We deliberately call the parent to avoid notifying the server again. | |
93 ValueMapPrefStore::SetValue(it.key(), it.value().CreateDeepCopy(), 0); | 99 ValueMapPrefStore::SetValue(it.key(), it.value().CreateDeepCopy(), 0); |
94 } | 100 } |
| 101 } |
95 | 102 |
96 if (!initialized_) { | 103 } // namespace preferences |
97 initialized_ = true; | |
98 NotifyInitializationCompleted(); | |
99 } | |
100 } | |
OLD | NEW |