| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "components/prefs/pref_service.h" | 5 #include "components/prefs/pref_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 bool rv = base::GetValueAsFilePath(*value, &result); | 186 bool rv = base::GetValueAsFilePath(*value, &result); |
| 187 DCHECK(rv); | 187 DCHECK(rv); |
| 188 return result; | 188 return result; |
| 189 } | 189 } |
| 190 | 190 |
| 191 bool PrefService::HasPrefPath(const std::string& path) const { | 191 bool PrefService::HasPrefPath(const std::string& path) const { |
| 192 const Preference* pref = FindPreference(path); | 192 const Preference* pref = FindPreference(path); |
| 193 return pref && !pref->IsDefaultValue(); | 193 return pref && !pref->IsDefaultValue(); |
| 194 } | 194 } |
| 195 | 195 |
| 196 std::unique_ptr<base::DictionaryValue> PrefService::GetPreferenceValues() | 196 void PrefService::IteratePreferenceValues( |
| 197 const { | 197 base::RepeatingCallback<void(const std::string& key, |
| 198 const base::Value& value)> callback) const { |
| 199 DCHECK(CalledOnValidThread()); |
| 200 for (const auto& it : *pref_registry_) |
| 201 callback.Run(it.first, *GetPreferenceValue(it.first)); |
| 202 } |
| 203 |
| 204 std::unique_ptr<base::DictionaryValue> PrefService::GetPreferenceValues( |
| 205 IncludeDefaults include_defaults) const { |
| 198 DCHECK(CalledOnValidThread()); | 206 DCHECK(CalledOnValidThread()); |
| 199 std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 207 std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
| 200 for (const auto& it : *pref_registry_) { | 208 for (const auto& it : *pref_registry_) { |
| 201 out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy()); | 209 if (include_defaults == INCLUDE_DEFAULTS) { |
| 210 out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy()); |
| 211 } else { |
| 212 const Preference* pref = FindPreference(it.first); |
| 213 if (pref->IsDefaultValue()) |
| 214 continue; |
| 215 out->Set(it.first, pref->GetValue()->CreateDeepCopy()); |
| 216 } |
| 202 } | 217 } |
| 203 return out; | 218 return out; |
| 204 } | 219 } |
| 205 | |
| 206 std::unique_ptr<base::DictionaryValue> | |
| 207 PrefService::GetPreferenceValuesOmitDefaults() const { | |
| 208 DCHECK(CalledOnValidThread()); | |
| 209 std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); | |
| 210 for (const auto& it : *pref_registry_) { | |
| 211 const Preference* pref = FindPreference(it.first); | |
| 212 if (pref->IsDefaultValue()) | |
| 213 continue; | |
| 214 out->Set(it.first, pref->GetValue()->CreateDeepCopy()); | |
| 215 } | |
| 216 return out; | |
| 217 } | |
| 218 | |
| 219 std::unique_ptr<base::DictionaryValue> | |
| 220 PrefService::GetPreferenceValuesWithoutPathExpansion() const { | |
| 221 DCHECK(CalledOnValidThread()); | |
| 222 std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue); | |
| 223 for (const auto& it : *pref_registry_) { | |
| 224 const base::Value* value = GetPreferenceValue(it.first); | |
| 225 DCHECK(value); | |
| 226 out->SetWithoutPathExpansion(it.first, value->CreateDeepCopy()); | |
| 227 } | |
| 228 return out; | |
| 229 } | |
| 230 | 220 |
| 231 const PrefService::Preference* PrefService::FindPreference( | 221 const PrefService::Preference* PrefService::FindPreference( |
| 232 const std::string& pref_name) const { | 222 const std::string& pref_name) const { |
| 233 DCHECK(CalledOnValidThread()); | 223 DCHECK(CalledOnValidThread()); |
| 234 PreferenceMap::iterator it = prefs_map_.find(pref_name); | 224 PreferenceMap::iterator it = prefs_map_.find(pref_name); |
| 235 if (it != prefs_map_.end()) | 225 if (it != prefs_map_.end()) |
| 236 return &(it->second); | 226 return &(it->second); |
| 237 const base::Value* default_value = NULL; | 227 const base::Value* default_value = NULL; |
| 238 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) | 228 if (!pref_registry_->defaults()->GetValue(pref_name, &default_value)) |
| 239 return NULL; | 229 return NULL; |
| (...skipping 375 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 615 DCHECK(found_value->IsType(default_type)); | 605 DCHECK(found_value->IsType(default_type)); |
| 616 return found_value; | 606 return found_value; |
| 617 } else { | 607 } else { |
| 618 // Every registered preference has at least a default value. | 608 // Every registered preference has at least a default value. |
| 619 NOTREACHED() << "no valid value found for registered pref " << path; | 609 NOTREACHED() << "no valid value found for registered pref " << path; |
| 620 } | 610 } |
| 621 } | 611 } |
| 622 | 612 |
| 623 return NULL; | 613 return NULL; |
| 624 } | 614 } |
| OLD | NEW |