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 "base/prefs/pref_service.h" | 5 #include "base/prefs/pref_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 : callback_(cb) {} | 31 : callback_(cb) {} |
32 | 32 |
33 void OnError(PersistentPrefStore::PrefReadError error) override { | 33 void OnError(PersistentPrefStore::PrefReadError error) override { |
34 callback_.Run(error); | 34 callback_.Run(error); |
35 } | 35 } |
36 | 36 |
37 private: | 37 private: |
38 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; | 38 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; |
39 }; | 39 }; |
40 | 40 |
| 41 // Returns the WriteablePrefStore::PrefWriteFlags for the pref with the given |
| 42 // |path|. |
| 43 uint32 GetWriteFlags(const PrefService::Preference* pref) { |
| 44 uint32 write_flags = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS; |
| 45 |
| 46 if (!pref) |
| 47 return write_flags; |
| 48 |
| 49 if (pref->registration_flags() & PrefRegistry::LOSSY_PREF) |
| 50 write_flags |= WriteablePrefStore::LOSSY_PREF_WRITE_FLAG; |
| 51 return write_flags; |
| 52 } |
| 53 |
41 } // namespace | 54 } // namespace |
42 | 55 |
43 PrefService::PrefService( | 56 PrefService::PrefService( |
44 PrefNotifierImpl* pref_notifier, | 57 PrefNotifierImpl* pref_notifier, |
45 PrefValueStore* pref_value_store, | 58 PrefValueStore* pref_value_store, |
46 PersistentPrefStore* user_prefs, | 59 PersistentPrefStore* user_prefs, |
47 PrefRegistry* pref_registry, | 60 PrefRegistry* pref_registry, |
48 base::Callback<void(PersistentPrefStore::PrefReadError)> | 61 base::Callback<void(PersistentPrefStore::PrefReadError)> |
49 read_error_callback, | 62 read_error_callback, |
50 bool async) | 63 bool async) |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 | 183 |
171 bool PrefService::HasPrefPath(const std::string& path) const { | 184 bool PrefService::HasPrefPath(const std::string& path) const { |
172 const Preference* pref = FindPreference(path); | 185 const Preference* pref = FindPreference(path); |
173 return pref && !pref->IsDefaultValue(); | 186 return pref && !pref->IsDefaultValue(); |
174 } | 187 } |
175 | 188 |
176 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValues() const { | 189 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValues() const { |
177 DCHECK(CalledOnValidThread()); | 190 DCHECK(CalledOnValidThread()); |
178 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 191 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
179 for (const auto& it : *pref_registry_) { | 192 for (const auto& it : *pref_registry_) { |
180 const base::Value* value = GetPreferenceValue(it.first); | 193 out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy()); |
181 out->Set(it.first, value->DeepCopy()); | |
182 } | 194 } |
183 return out.Pass(); | 195 return out.Pass(); |
184 } | 196 } |
185 | 197 |
186 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValuesOmitDefaults() | 198 scoped_ptr<base::DictionaryValue> PrefService::GetPreferenceValuesOmitDefaults() |
187 const { | 199 const { |
188 DCHECK(CalledOnValidThread()); | 200 DCHECK(CalledOnValidThread()); |
189 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 201 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
190 for (const auto& it : *pref_registry_) { | 202 for (const auto& it : *pref_registry_) { |
191 const Preference* pref = FindPreference(it.first); | 203 const Preference* pref = FindPreference(it.first); |
192 if (pref->IsDefaultValue()) | 204 if (pref->IsDefaultValue()) |
193 continue; | 205 continue; |
194 out->Set(it.first, pref->GetValue()->DeepCopy()); | 206 out->Set(it.first, pref->GetValue()->CreateDeepCopy()); |
195 } | 207 } |
196 return out.Pass(); | 208 return out.Pass(); |
197 } | 209 } |
198 | 210 |
199 scoped_ptr<base::DictionaryValue> | 211 scoped_ptr<base::DictionaryValue> |
200 PrefService::GetPreferenceValuesWithoutPathExpansion() const { | 212 PrefService::GetPreferenceValuesWithoutPathExpansion() const { |
201 DCHECK(CalledOnValidThread()); | 213 DCHECK(CalledOnValidThread()); |
202 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); | 214 scoped_ptr<base::DictionaryValue> out(new base::DictionaryValue); |
203 for (const auto& it : *pref_registry_) { | 215 for (const auto& it : *pref_registry_) { |
204 const base::Value* value = GetPreferenceValue(it.first); | 216 const base::Value* value = GetPreferenceValue(it.first); |
205 DCHECK(value); | 217 DCHECK(value); |
206 out->SetWithoutPathExpansion(it.first, value->DeepCopy()); | 218 out->SetWithoutPathExpansion(it.first, value->CreateDeepCopy()); |
207 } | 219 } |
208 return out.Pass(); | 220 return out.Pass(); |
209 } | 221 } |
210 | 222 |
211 const PrefService::Preference* PrefService::FindPreference( | 223 const PrefService::Preference* PrefService::FindPreference( |
212 const std::string& pref_name) const { | 224 const std::string& pref_name) const { |
213 DCHECK(CalledOnValidThread()); | 225 DCHECK(CalledOnValidThread()); |
214 PreferenceMap::iterator it = prefs_map_.find(pref_name); | 226 PreferenceMap::iterator it = prefs_map_.find(pref_name); |
215 if (it != prefs_map_.end()) | 227 if (it != prefs_map_.end()) |
216 return &(it->second); | 228 return &(it->second); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 } | 362 } |
351 | 363 |
352 void PrefService::ClearPref(const std::string& path) { | 364 void PrefService::ClearPref(const std::string& path) { |
353 DCHECK(CalledOnValidThread()); | 365 DCHECK(CalledOnValidThread()); |
354 | 366 |
355 const Preference* pref = FindPreference(path); | 367 const Preference* pref = FindPreference(path); |
356 if (!pref) { | 368 if (!pref) { |
357 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 369 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
358 return; | 370 return; |
359 } | 371 } |
360 user_pref_store_->RemoveValue(path); | 372 user_pref_store_->RemoveValue(path, GetWriteFlags(pref)); |
361 } | 373 } |
362 | 374 |
363 void PrefService::Set(const std::string& path, const base::Value& value) { | 375 void PrefService::Set(const std::string& path, const base::Value& value) { |
364 SetUserPrefValue(path, value.DeepCopy()); | 376 SetUserPrefValue(path, value.DeepCopy()); |
365 } | 377 } |
366 | 378 |
367 void PrefService::SetBoolean(const std::string& path, bool value) { | 379 void PrefService::SetBoolean(const std::string& path, bool value) { |
368 SetUserPrefValue(path, new base::FundamentalValue(value)); | 380 SetUserPrefValue(path, new base::FundamentalValue(value)); |
369 } | 381 } |
370 | 382 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
447 base::Value* value = NULL; | 459 base::Value* value = NULL; |
448 if (!user_pref_store_->GetMutableValue(path, &value) || | 460 if (!user_pref_store_->GetMutableValue(path, &value) || |
449 !value->IsType(type)) { | 461 !value->IsType(type)) { |
450 if (type == base::Value::TYPE_DICTIONARY) { | 462 if (type == base::Value::TYPE_DICTIONARY) { |
451 value = new base::DictionaryValue; | 463 value = new base::DictionaryValue; |
452 } else if (type == base::Value::TYPE_LIST) { | 464 } else if (type == base::Value::TYPE_LIST) { |
453 value = new base::ListValue; | 465 value = new base::ListValue; |
454 } else { | 466 } else { |
455 NOTREACHED(); | 467 NOTREACHED(); |
456 } | 468 } |
457 user_pref_store_->SetValueSilently(path, value); | 469 user_pref_store_->SetValueSilently(path, value, GetWriteFlags(pref)); |
458 } | 470 } |
459 return value; | 471 return value; |
460 } | 472 } |
461 | 473 |
462 void PrefService::ReportUserPrefChanged(const std::string& key) { | 474 void PrefService::ReportUserPrefChanged(const std::string& key) { |
463 DCHECK(CalledOnValidThread()); | 475 DCHECK(CalledOnValidThread()); |
464 user_pref_store_->ReportValueChanged(key); | 476 user_pref_store_->ReportValueChanged(key, GetWriteFlags(FindPreference(key))); |
465 } | 477 } |
466 | 478 |
467 void PrefService::SetUserPrefValue(const std::string& path, | 479 void PrefService::SetUserPrefValue(const std::string& path, |
468 base::Value* new_value) { | 480 base::Value* new_value) { |
469 scoped_ptr<base::Value> owned_value(new_value); | 481 scoped_ptr<base::Value> owned_value(new_value); |
470 DCHECK(CalledOnValidThread()); | 482 DCHECK(CalledOnValidThread()); |
471 | 483 |
472 const Preference* pref = FindPreference(path); | 484 const Preference* pref = FindPreference(path); |
473 if (!pref) { | 485 if (!pref) { |
474 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 486 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
475 return; | 487 return; |
476 } | 488 } |
477 if (pref->GetType() != new_value->GetType()) { | 489 if (pref->GetType() != new_value->GetType()) { |
478 NOTREACHED() << "Trying to set pref " << path | 490 NOTREACHED() << "Trying to set pref " << path |
479 << " of type " << pref->GetType() | 491 << " of type " << pref->GetType() |
480 << " to value of type " << new_value->GetType(); | 492 << " to value of type " << new_value->GetType(); |
481 return; | 493 return; |
482 } | 494 } |
483 | 495 |
484 user_pref_store_->SetValue(path, owned_value.release()); | 496 user_pref_store_->SetValue(path, owned_value.release(), GetWriteFlags(pref)); |
485 } | 497 } |
486 | 498 |
487 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { | 499 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { |
488 pref_value_store_->UpdateCommandLinePrefStore(command_line_store); | 500 pref_value_store_->UpdateCommandLinePrefStore(command_line_store); |
489 } | 501 } |
490 | 502 |
491 /////////////////////////////////////////////////////////////////////////////// | 503 /////////////////////////////////////////////////////////////////////////////// |
492 // PrefService::Preference | 504 // PrefService::Preference |
493 | 505 |
494 PrefService::Preference::Preference(const PrefService* service, | 506 PrefService::Preference::Preference(const PrefService* service, |
495 const std::string& name, | 507 const std::string& name, |
496 base::Value::Type type) | 508 base::Value::Type type) |
497 : name_(name), type_(type), pref_service_(service) { | 509 : name_(name), type_(type), pref_service_(service) { |
498 DCHECK(service); | 510 DCHECK(service); |
| 511 // Cache the registration flags at creation time to avoid multiple map lookups |
| 512 // later. |
| 513 registration_flags_ = service->pref_registry_->GetRegistrationFlags(name_); |
499 } | 514 } |
500 | 515 |
501 const std::string PrefService::Preference::name() const { | 516 const std::string PrefService::Preference::name() const { |
502 return name_; | 517 return name_; |
503 } | 518 } |
504 | 519 |
505 base::Value::Type PrefService::Preference::GetType() const { | 520 base::Value::Type PrefService::Preference::GetType() const { |
506 return type_; | 521 return type_; |
507 } | 522 } |
508 | 523 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
585 DCHECK(found_value->IsType(default_type)); | 600 DCHECK(found_value->IsType(default_type)); |
586 return found_value; | 601 return found_value; |
587 } else { | 602 } else { |
588 // Every registered preference has at least a default value. | 603 // Every registered preference has at least a default value. |
589 NOTREACHED() << "no valid value found for registered pref " << path; | 604 NOTREACHED() << "no valid value found for registered pref " << path; |
590 } | 605 } |
591 } | 606 } |
592 | 607 |
593 return NULL; | 608 return NULL; |
594 } | 609 } |
OLD | NEW |