OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_store_client_mixin.h" | 5 #include "services/preferences/public/cpp/pref_store_client_mixin.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
| 9 #include "base/strings/string_split.h" |
9 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "services/preferences/public/cpp/lib/util.h" |
10 #include "services/preferences/public/cpp/pref_store_client.h" | 12 #include "services/preferences/public/cpp/pref_store_client.h" |
11 | 13 |
12 namespace prefs { | 14 namespace prefs { |
13 | 15 |
14 template <typename BasePrefStore> | 16 template <typename BasePrefStore> |
15 PrefStoreClientMixin<BasePrefStore>::PrefStoreClientMixin() | 17 PrefStoreClientMixin<BasePrefStore>::PrefStoreClientMixin() |
16 : observer_binding_(this) {} | 18 : observer_binding_(this) {} |
17 | 19 |
18 template <typename BasePrefStore> | 20 template <typename BasePrefStore> |
19 void PrefStoreClientMixin<BasePrefStore>::AddObserver( | 21 void PrefStoreClientMixin<BasePrefStore>::AddObserver( |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 if (!initialized_) { | 96 if (!initialized_) { |
95 initialized_ = true; | 97 initialized_ = true; |
96 for (auto& observer : observers_) | 98 for (auto& observer : observers_) |
97 observer.OnInitializationCompleted(succeeded); | 99 observer.OnInitializationCompleted(succeeded); |
98 } | 100 } |
99 } | 101 } |
100 | 102 |
101 template <typename BasePrefStore> | 103 template <typename BasePrefStore> |
102 void PrefStoreClientMixin<BasePrefStore>::OnPrefChanged( | 104 void PrefStoreClientMixin<BasePrefStore>::OnPrefChanged( |
103 const std::string& key, | 105 const std::string& key, |
104 std::unique_ptr<base::Value> value) { | 106 mojom::PrefUpdateValuePtr update_value) { |
105 DCHECK(cached_prefs_); | 107 DCHECK(cached_prefs_); |
106 bool changed = false; | 108 bool changed = false; |
107 if (!value) { // Delete | 109 if (update_value->is_atomic_update()) { |
108 if (cached_prefs_->RemovePath(key, nullptr)) | 110 auto& value = update_value->get_atomic_update(); |
109 changed = true; | 111 if (!value) { // Delete |
110 } else { | 112 if (cached_prefs_->RemovePath(key, nullptr)) |
111 const base::Value* prev; | 113 changed = true; |
112 if (cached_prefs_->Get(key, &prev)) { | 114 } else { |
113 if (!prev->Equals(value.get())) { | 115 const base::Value* prev; |
| 116 if (cached_prefs_->Get(key, &prev)) { |
| 117 if (!prev->Equals(value.get())) { |
| 118 cached_prefs_->Set(key, std::move(value)); |
| 119 changed = true; |
| 120 } |
| 121 } else { |
114 cached_prefs_->Set(key, std::move(value)); | 122 cached_prefs_->Set(key, std::move(value)); |
115 changed = true; | 123 changed = true; |
116 } | 124 } |
117 } else { | 125 } |
118 cached_prefs_->Set(key, std::move(value)); | 126 } else if (update_value->is_split_updates()) { |
| 127 auto& updates = update_value->get_split_updates(); |
| 128 if (!updates.empty()) |
119 changed = true; | 129 changed = true; |
| 130 for (auto& update : updates) { |
| 131 if (update->path.empty()) |
| 132 continue; |
| 133 |
| 134 std::vector<base::StringPiece> full_path = base::SplitStringPiece( |
| 135 key, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 136 full_path.insert(full_path.end(), update->path.begin(), |
| 137 update->path.end()); |
| 138 prefs::SetValue(cached_prefs_.get(), full_path, std::move(update->value)); |
120 } | 139 } |
121 } | 140 } |
122 if (changed && initialized_) | 141 if (changed && initialized_) |
123 ReportPrefValueChanged(key); | 142 ReportPrefValueChanged(key); |
124 } | 143 } |
125 | 144 |
126 template class PrefStoreClientMixin<::PrefStore>; | 145 template class PrefStoreClientMixin<::PrefStore>; |
127 template class PrefStoreClientMixin<::PersistentPrefStore>; | 146 template class PrefStoreClientMixin<::PersistentPrefStore>; |
128 | 147 |
129 } // namespace prefs | 148 } // namespace prefs |
OLD | NEW |