blob: b7c91de53c1ad2ae2ee9cafcc278bf7c28851243 [file] [log] [blame]
[email protected]b1de2c72013-02-06 02:45:471// 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
brettwf00b9b42016-02-01 22:11:385#include "components/prefs/pref_registry.h"
[email protected]b1de2c72013-02-06 02:45:476
7#include "base/logging.h"
raymes51b41a62015-04-24 02:45:048#include "base/stl_util.h"
[email protected]b1de2c72013-02-06 02:45:479#include "base/values.h"
brettwf00b9b42016-02-01 22:11:3810#include "components/prefs/default_pref_store.h"
11#include "components/prefs/pref_store.h"
[email protected]b1de2c72013-02-06 02:45:4712
13PrefRegistry::PrefRegistry()
14 : defaults_(new DefaultPrefStore()) {
15}
16
17PrefRegistry::~PrefRegistry() {
18}
19
avi9ef8bb02015-12-24 05:29:3620uint32_t PrefRegistry::GetRegistrationFlags(
21 const std::string& pref_name) const {
raymes51b41a62015-04-24 02:45:0422 const auto& it = registration_flags_.find(pref_name);
23 if (it == registration_flags_.end())
24 return NO_REGISTRATION_FLAGS;
25 return it->second;
26}
27
[email protected]b1de2c72013-02-06 02:45:4728scoped_refptr<PrefStore> PrefRegistry::defaults() {
29 return defaults_.get();
30}
31
32PrefRegistry::const_iterator PrefRegistry::begin() const {
33 return defaults_->begin();
34}
35
36PrefRegistry::const_iterator PrefRegistry::end() const {
37 return defaults_->end();
38}
39
georgesak7da6e9d2014-12-03 01:10:2940void PrefRegistry::SetDefaultPrefValue(const std::string& pref_name,
[email protected]5879cef2013-03-02 17:02:2541 base::Value* value) {
42 DCHECK(value);
[email protected]c02cb8012014-03-14 18:39:5343 const base::Value* current_value = NULL;
44 DCHECK(defaults_->GetValue(pref_name, &current_value))
45 << "Setting default for unregistered pref: " << pref_name;
46 DCHECK(value->IsType(current_value->GetType()))
47 << "Wrong type for new default: " << pref_name;
[email protected]5879cef2013-03-02 17:02:2548
[email protected]f8f8b672013-03-07 09:12:5849 defaults_->ReplaceDefaultValue(pref_name, make_scoped_ptr(value));
[email protected]5879cef2013-03-02 17:02:2550}
51
georgesak7da6e9d2014-12-03 01:10:2952void PrefRegistry::RegisterPreference(const std::string& path,
raymes51b41a62015-04-24 02:45:0453 base::Value* default_value,
avi9ef8bb02015-12-24 05:29:3654 uint32_t flags) {
[email protected]b1de2c72013-02-06 02:45:4755 base::Value::Type orig_type = default_value->GetType();
56 DCHECK(orig_type != base::Value::TYPE_NULL &&
57 orig_type != base::Value::TYPE_BINARY) <<
58 "invalid preference type: " << orig_type;
59 DCHECK(!defaults_->GetValue(path, NULL)) <<
60 "Trying to register a previously registered pref: " << path;
raymes51b41a62015-04-24 02:45:0461 DCHECK(!ContainsKey(registration_flags_, path)) <<
62 "Trying to register a previously registered pref: " << path;
[email protected]b1de2c72013-02-06 02:45:4763
[email protected]f8f8b672013-03-07 09:12:5864 defaults_->SetDefaultValue(path, make_scoped_ptr(default_value));
raymes51b41a62015-04-24 02:45:0465 if (flags != NO_REGISTRATION_FLAGS)
66 registration_flags_[path] = flags;
[email protected]b1de2c72013-02-06 02:45:4767}