[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 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 | |||||
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 5 | #include "components/prefs/pref_registry.h" |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 6 | |
vabr | 88e50721 | 2017-03-29 13:22:26 | [diff] [blame^] | 7 | #include <utility> |
8 | |||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 9 | #include "base/logging.h" |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 11 | #include "base/stl_util.h" |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 12 | #include "base/values.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame] | 13 | #include "components/prefs/default_pref_store.h" |
14 | #include "components/prefs/pref_store.h" | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 15 | |
16 | PrefRegistry::PrefRegistry() | ||||
17 | : defaults_(new DefaultPrefStore()) { | ||||
18 | } | ||||
19 | |||||
20 | PrefRegistry::~PrefRegistry() { | ||||
21 | } | ||||
22 | |||||
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 23 | uint32_t PrefRegistry::GetRegistrationFlags( |
24 | const std::string& pref_name) const { | ||||
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 25 | const auto& it = registration_flags_.find(pref_name); |
26 | if (it == registration_flags_.end()) | ||||
27 | return NO_REGISTRATION_FLAGS; | ||||
28 | return it->second; | ||||
29 | } | ||||
30 | |||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 31 | scoped_refptr<PrefStore> PrefRegistry::defaults() { |
32 | return defaults_.get(); | ||||
33 | } | ||||
34 | |||||
35 | PrefRegistry::const_iterator PrefRegistry::begin() const { | ||||
36 | return defaults_->begin(); | ||||
37 | } | ||||
38 | |||||
39 | PrefRegistry::const_iterator PrefRegistry::end() const { | ||||
40 | return defaults_->end(); | ||||
41 | } | ||||
42 | |||||
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 43 | void PrefRegistry::SetDefaultPrefValue(const std::string& pref_name, |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 44 | base::Value* value) { |
45 | DCHECK(value); | ||||
[email protected] | c02cb801 | 2014-03-14 18:39:53 | [diff] [blame] | 46 | const base::Value* current_value = NULL; |
47 | DCHECK(defaults_->GetValue(pref_name, ¤t_value)) | ||||
48 | << "Setting default for unregistered pref: " << pref_name; | ||||
49 | DCHECK(value->IsType(current_value->GetType())) | ||||
50 | << "Wrong type for new default: " << pref_name; | ||||
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 51 | |
dcheng | 5f043bc | 2016-04-22 19:09:06 | [diff] [blame] | 52 | defaults_->ReplaceDefaultValue(pref_name, base::WrapUnique(value)); |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 53 | } |
54 | |||||
vabr | 88e50721 | 2017-03-29 13:22:26 | [diff] [blame^] | 55 | void PrefRegistry::RegisterPreference( |
56 | const std::string& path, | ||||
57 | std::unique_ptr<base::Value> default_value, | ||||
58 | uint32_t flags) { | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 59 | base::Value::Type orig_type = default_value->GetType(); |
jdoerrie | dc72ee94 | 2016-12-07 15:43:28 | [diff] [blame] | 60 | DCHECK(orig_type != base::Value::Type::NONE && |
61 | orig_type != base::Value::Type::BINARY) << | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 62 | "invalid preference type: " << orig_type; |
63 | DCHECK(!defaults_->GetValue(path, NULL)) << | ||||
64 | "Trying to register a previously registered pref: " << path; | ||||
skyostil | 4638441 | 2016-08-12 14:33:39 | [diff] [blame] | 65 | DCHECK(!base::ContainsKey(registration_flags_, path)) |
66 | << "Trying to register a previously registered pref: " << path; | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 67 | |
vabr | 88e50721 | 2017-03-29 13:22:26 | [diff] [blame^] | 68 | defaults_->SetDefaultValue(path, std::move(default_value)); |
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 69 | if (flags != NO_REGISTRATION_FLAGS) |
70 | registration_flags_[path] = flags; | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 71 | } |