• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
9 #include <string_view>
10 #include <utility>
11 
12 #include "base/check_op.h"
13 #include "base/containers/contains.h"
14 #include "base/values.h"
15 #include "components/prefs/default_pref_store.h"
16 #include "components/prefs/pref_store.h"
17 
PrefRegistry()18 PrefRegistry::PrefRegistry()
19     : defaults_(base::MakeRefCounted<DefaultPrefStore>()) {}
20 
~PrefRegistry()21 PrefRegistry::~PrefRegistry() {
22 }
23 
GetRegistrationFlags(std::string_view pref_name) const24 uint32_t PrefRegistry::GetRegistrationFlags(std::string_view pref_name) const {
25   const auto& it = registration_flags_.find(pref_name);
26   return it != registration_flags_.end() ? it->second : NO_REGISTRATION_FLAGS;
27 }
28 
defaults()29 scoped_refptr<PrefStore> PrefRegistry::defaults() {
30   return defaults_.get();
31 }
32 
begin() const33 PrefRegistry::const_iterator PrefRegistry::begin() const {
34   return defaults_->begin();
35 }
36 
end() const37 PrefRegistry::const_iterator PrefRegistry::end() const {
38   return defaults_->end();
39 }
40 
SetDefaultPrefValue(std::string_view pref_name,base::Value value)41 void PrefRegistry::SetDefaultPrefValue(std::string_view pref_name,
42                                        base::Value value) {
43   const base::Value* current_value = nullptr;
44   DCHECK(defaults_->GetValue(pref_name, &current_value))
45       << "Setting default for unregistered pref: " << pref_name;
46   DCHECK(value.type() == current_value->type())
47       << "Wrong type for new default: " << pref_name;
48 
49   defaults_->ReplaceDefaultValue(pref_name, std::move(value));
50 }
51 
RegisterPreference(std::string_view path,base::Value default_value,uint32_t flags)52 void PrefRegistry::RegisterPreference(std::string_view path,
53                                       base::Value default_value,
54                                       uint32_t flags) {
55   base::Value::Type orig_type = default_value.type();
56   DCHECK(orig_type != base::Value::Type::NONE &&
57          orig_type != base::Value::Type::BINARY) <<
58          "invalid preference type: " << orig_type;
59   DCHECK(!defaults_->GetValue(path, nullptr))
60       << "Trying to register a previously registered pref: " << path;
61   DCHECK(!base::Contains(registration_flags_, std::string(path)))
62       << "Trying to register a previously registered pref: " << path;
63 
64   defaults_->SetDefaultValue(path, std::move(default_value));
65   if (flags != NO_REGISTRATION_FLAGS) {
66     registration_flags_.insert_or_assign(std::string(path), flags);
67   }
68 
69   OnPrefRegistered(path, flags);
70 }
71 
OnPrefRegistered(std::string_view path,uint32_t flags)72 void PrefRegistry::OnPrefRegistered(std::string_view path, uint32_t flags) {}
73