[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 | |
7 | #include "base/logging.h" | ||||
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 8 | #include "base/stl_util.h" |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 9 | #include "base/values.h" |
brettw | f00b9b4 | 2016-02-01 22:11:38 | [diff] [blame^] | 10 | #include "components/prefs/default_pref_store.h" |
11 | #include "components/prefs/pref_store.h" | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 12 | |
13 | PrefRegistry::PrefRegistry() | ||||
14 | : defaults_(new DefaultPrefStore()) { | ||||
15 | } | ||||
16 | |||||
17 | PrefRegistry::~PrefRegistry() { | ||||
18 | } | ||||
19 | |||||
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 20 | uint32_t PrefRegistry::GetRegistrationFlags( |
21 | const std::string& pref_name) const { | ||||
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 22 | 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] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 28 | scoped_refptr<PrefStore> PrefRegistry::defaults() { |
29 | return defaults_.get(); | ||||
30 | } | ||||
31 | |||||
32 | PrefRegistry::const_iterator PrefRegistry::begin() const { | ||||
33 | return defaults_->begin(); | ||||
34 | } | ||||
35 | |||||
36 | PrefRegistry::const_iterator PrefRegistry::end() const { | ||||
37 | return defaults_->end(); | ||||
38 | } | ||||
39 | |||||
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 40 | void PrefRegistry::SetDefaultPrefValue(const std::string& pref_name, |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 41 | base::Value* value) { |
42 | DCHECK(value); | ||||
[email protected] | c02cb801 | 2014-03-14 18:39:53 | [diff] [blame] | 43 | const base::Value* current_value = NULL; |
44 | DCHECK(defaults_->GetValue(pref_name, ¤t_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] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 48 | |
[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 49 | defaults_->ReplaceDefaultValue(pref_name, make_scoped_ptr(value)); |
[email protected] | 5879cef | 2013-03-02 17:02:25 | [diff] [blame] | 50 | } |
51 | |||||
georgesak | 7da6e9d | 2014-12-03 01:10:29 | [diff] [blame] | 52 | void PrefRegistry::RegisterPreference(const std::string& path, |
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 53 | base::Value* default_value, |
avi | 9ef8bb0 | 2015-12-24 05:29:36 | [diff] [blame] | 54 | uint32_t flags) { |
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 55 | 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; | ||||
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 61 | DCHECK(!ContainsKey(registration_flags_, path)) << |
62 | "Trying to register a previously registered pref: " << path; | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 63 | |
[email protected] | f8f8b67 | 2013-03-07 09:12:58 | [diff] [blame] | 64 | defaults_->SetDefaultValue(path, make_scoped_ptr(default_value)); |
raymes | 51b41a6 | 2015-04-24 02:45:04 | [diff] [blame] | 65 | if (flags != NO_REGISTRATION_FLAGS) |
66 | registration_flags_[path] = flags; | ||||
[email protected] | b1de2c7 | 2013-02-06 02:45:47 | [diff] [blame] | 67 | } |