1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/prefs/pref_registry.h"
6
7 #include <ostream>
8 #include <utility>
9
10 #include "base/check_op.h"
11 #include "base/containers/contains.h"
12 #include "base/values.h"
13 #include "components/prefs/default_pref_store.h"
14 #include "components/prefs/pref_store.h"
15
PrefRegistry()16 PrefRegistry::PrefRegistry()
17 : defaults_(base::MakeRefCounted<DefaultPrefStore>()) {}
18
~PrefRegistry()19 PrefRegistry::~PrefRegistry() {
20 }
21
GetRegistrationFlags(const std::string & pref_name) const22 uint32_t PrefRegistry::GetRegistrationFlags(
23 const std::string& pref_name) const {
24 const auto& it = registration_flags_.find(pref_name);
25 return it != registration_flags_.end() ? it->second : NO_REGISTRATION_FLAGS;
26 }
27
defaults()28 scoped_refptr<PrefStore> PrefRegistry::defaults() {
29 return defaults_.get();
30 }
31
begin() const32 PrefRegistry::const_iterator PrefRegistry::begin() const {
33 return defaults_->begin();
34 }
35
end() const36 PrefRegistry::const_iterator PrefRegistry::end() const {
37 return defaults_->end();
38 }
39
SetDefaultPrefValue(const std::string & pref_name,base::Value value)40 void PrefRegistry::SetDefaultPrefValue(const std::string& pref_name,
41 base::Value value) {
42 const base::Value* current_value = nullptr;
43 DCHECK(defaults_->GetValue(pref_name, ¤t_value))
44 << "Setting default for unregistered pref: " << pref_name;
45 DCHECK(value.type() == current_value->type())
46 << "Wrong type for new default: " << pref_name;
47
48 defaults_->ReplaceDefaultValue(pref_name, std::move(value));
49 }
50
SetDefaultForeignPrefValue(const std::string & path,base::Value default_value,uint32_t flags)51 void PrefRegistry::SetDefaultForeignPrefValue(const std::string& path,
52 base::Value default_value,
53 uint32_t flags) {
54 auto erased = foreign_pref_keys_.erase(path);
55 DCHECK_EQ(1u, erased);
56 RegisterPreference(path, std::move(default_value), flags);
57 }
58
RegisterPreference(const std::string & path,base::Value default_value,uint32_t flags)59 void PrefRegistry::RegisterPreference(const std::string& path,
60 base::Value default_value,
61 uint32_t flags) {
62 base::Value::Type orig_type = default_value.type();
63 DCHECK(orig_type != base::Value::Type::NONE &&
64 orig_type != base::Value::Type::BINARY) <<
65 "invalid preference type: " << orig_type;
66 DCHECK(!defaults_->GetValue(path, nullptr))
67 << "Trying to register a previously registered pref: " << path;
68 DCHECK(!base::Contains(registration_flags_, path))
69 << "Trying to register a previously registered pref: " << path;
70
71 defaults_->SetDefaultValue(path, std::move(default_value));
72 if (flags != NO_REGISTRATION_FLAGS)
73 registration_flags_[path] = flags;
74
75 OnPrefRegistered(path, flags);
76 }
77
RegisterForeignPref(const std::string & path)78 void PrefRegistry::RegisterForeignPref(const std::string& path) {
79 bool inserted = foreign_pref_keys_.insert(path).second;
80 DCHECK(inserted);
81 }
82
OnPrefRegistered(const std::string & path,uint32_t flags)83 void PrefRegistry::OnPrefRegistered(const std::string& path,
84 uint32_t flags) {}
85