| 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/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 namespace preferences { |
| 13 |
| 12 PrefObserverStore::PrefObserverStore( | 14 PrefObserverStore::PrefObserverStore( |
| 13 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr) | 15 prefs::mojom::PreferencesManagerPtr prefs_manager_ptr) |
| 14 : prefs_binding_(this), | 16 : prefs_binding_(this), |
| 15 prefs_manager_ptr_(std::move(prefs_manager_ptr)), | 17 prefs_manager_ptr_(std::move(prefs_manager_ptr)), |
| 16 initialized_(false) {} | 18 initialized_(false) {} |
| 17 | 19 |
| 18 void PrefObserverStore::Init(const std::set<std::string>& keys) { | 20 void PrefObserverStore::Subscribe(const std::set<std::string>& keys) { |
| 19 DCHECK(!initialized_); | 21 if (keys_.empty()) |
| 20 keys_ = keys; | 22 prefs_manager_ptr_->AddObserver(prefs_binding_.CreateInterfacePtrAndBind()); |
| 23 keys_.insert(keys.begin(), keys.end()); |
| 21 | 24 |
| 22 std::vector<std::string> pref_array; | 25 std::vector<std::string> pref_array; |
| 23 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array)); | 26 std::copy(keys_.begin(), keys_.end(), std::back_inserter(pref_array)); |
| 24 prefs_manager_ptr_->AddObserver(pref_array, | 27 prefs_manager_ptr_->Subscribe(pref_array); |
| 25 prefs_binding_.CreateInterfacePtrAndBind()); | |
| 26 } | 28 } |
| 27 | 29 |
| 28 bool PrefObserverStore::GetValue(const std::string& key, | 30 bool PrefObserverStore::GetValue(const std::string& key, |
| 29 const base::Value** value) const { | 31 const base::Value** value) const { |
| 30 DCHECK(initialized_); | 32 DCHECK(initialized_); |
| 31 DCHECK(keys_.find(key) != keys_.end()); | 33 DCHECK(keys_.find(key) != keys_.end()); |
| 32 | 34 |
| 33 return ValueMapPrefStore::GetValue(key, value); | 35 return ValueMapPrefStore::GetValue(key, value); |
| 34 } | 36 } |
| 35 | 37 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 } |
| 102 } |
| 96 | 103 |
| 97 if (!initialized_) { | 104 } // namespace preferences |
| 98 initialized_ = true; | |
| 99 NotifyInitializationCompleted(); | |
| 100 } | |
| 101 } | |
| OLD | NEW |