pref_registry.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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
  3. // found in the LICENSE file.
  4. #include "components/prefs/pref_registry.h"
  5. #include <ostream>
  6. #include <utility>
  7. #include "base/check_op.h"
  8. #include "base/containers/contains.h"
  9. #include "base/values.h"
  10. #include "components/prefs/default_pref_store.h"
  11. #include "components/prefs/pref_store.h"
  12. PrefRegistry::PrefRegistry()
  13. : defaults_(base::MakeRefCounted<DefaultPrefStore>()) {}
  14. PrefRegistry::~PrefRegistry() {
  15. }
  16. uint32_t PrefRegistry::GetRegistrationFlags(
  17. const std::string& pref_name) const {
  18. const auto& it = registration_flags_.find(pref_name);
  19. return it != registration_flags_.end() ? it->second : NO_REGISTRATION_FLAGS;
  20. }
  21. scoped_refptr<PrefStore> PrefRegistry::defaults() {
  22. return defaults_.get();
  23. }
  24. PrefRegistry::const_iterator PrefRegistry::begin() const {
  25. return defaults_->begin();
  26. }
  27. PrefRegistry::const_iterator PrefRegistry::end() const {
  28. return defaults_->end();
  29. }
  30. void PrefRegistry::SetDefaultPrefValue(const std::string& pref_name,
  31. base::Value value) {
  32. const base::Value* current_value = nullptr;
  33. DCHECK(defaults_->GetValue(pref_name, &current_value))
  34. << "Setting default for unregistered pref: " << pref_name;
  35. DCHECK(value.type() == current_value->type())
  36. << "Wrong type for new default: " << pref_name;
  37. defaults_->ReplaceDefaultValue(pref_name, std::move(value));
  38. }
  39. void PrefRegistry::SetDefaultForeignPrefValue(const std::string& path,
  40. base::Value default_value,
  41. uint32_t flags) {
  42. auto erased = foreign_pref_keys_.erase(path);
  43. DCHECK_EQ(1u, erased);
  44. RegisterPreference(path, std::move(default_value), flags);
  45. }
  46. void PrefRegistry::RegisterPreference(const std::string& path,
  47. base::Value default_value,
  48. uint32_t flags) {
  49. base::Value::Type orig_type = default_value.type();
  50. DCHECK(orig_type != base::Value::Type::NONE &&
  51. orig_type != base::Value::Type::BINARY) <<
  52. "invalid preference type: " << orig_type;
  53. DCHECK(!defaults_->GetValue(path, nullptr))
  54. << "Trying to register a previously registered pref: " << path;
  55. DCHECK(!base::Contains(registration_flags_, path))
  56. << "Trying to register a previously registered pref: " << path;
  57. defaults_->SetDefaultValue(path, std::move(default_value));
  58. if (flags != NO_REGISTRATION_FLAGS)
  59. registration_flags_[path] = flags;
  60. OnPrefRegistered(path, flags);
  61. }
  62. void PrefRegistry::RegisterForeignPref(const std::string& path) {
  63. bool inserted = foreign_pref_keys_.insert(path).second;
  64. DCHECK(inserted);
  65. }
  66. void PrefRegistry::OnPrefRegistered(const std::string& path,
  67. uint32_t flags) {}