Treat regular and always-on hotwording settings as mutually exclusive.
[chromium-blink-merge.git] / components / pref_registry / pref_registry_syncable.cc
blob1edcb1ea518bb37d2bb89b3bd952d846fbae0b39
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.
5 #include "components/pref_registry/pref_registry_syncable.h"
7 #include "base/files/file_path.h"
8 #include "base/prefs/default_pref_store.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/values.h"
11 #include "ui/base/l10n/l10n_util.h"
13 namespace user_prefs {
15 namespace {
17 // A helper function for RegisterLocalized*Pref that creates a Value*
18 // based on a localized resource. Because we control the values in a
19 // locale dll, this should always return a Value of the appropriate
20 // type.
21 base::Value* CreateLocaleDefaultValue(base::Value::Type type,
22 int message_id) {
23 const std::string resource_string = l10n_util::GetStringUTF8(message_id);
24 DCHECK(!resource_string.empty());
25 switch (type) {
26 case base::Value::TYPE_BOOLEAN: {
27 if ("true" == resource_string)
28 return new base::FundamentalValue(true);
29 if ("false" == resource_string)
30 return new base::FundamentalValue(false);
31 break;
34 case base::Value::TYPE_INTEGER: {
35 int val;
36 base::StringToInt(resource_string, &val);
37 return new base::FundamentalValue(val);
40 case base::Value::TYPE_DOUBLE: {
41 double val;
42 base::StringToDouble(resource_string, &val);
43 return new base::FundamentalValue(val);
46 case base::Value::TYPE_STRING: {
47 return new base::StringValue(resource_string);
50 default: {
51 NOTREACHED() <<
52 "list and dictionary types cannot have default locale values";
55 NOTREACHED();
56 return base::Value::CreateNullValue();
59 } // namespace
61 PrefRegistrySyncable::PrefRegistrySyncable() {
64 PrefRegistrySyncable::~PrefRegistrySyncable() {
67 const PrefRegistrySyncable::PrefToStatus&
68 PrefRegistrySyncable::syncable_preferences() const {
69 return syncable_preferences_;
72 void PrefRegistrySyncable::SetSyncableRegistrationCallback(
73 const SyncableRegistrationCallback& cb) {
74 callback_ = cb;
77 void PrefRegistrySyncable::RegisterBooleanPref(const char* path,
78 bool default_value,
79 PrefSyncStatus sync_status) {
80 RegisterSyncablePreference(
81 path, new base::FundamentalValue(default_value), sync_status);
84 void PrefRegistrySyncable::RegisterIntegerPref(const char* path,
85 int default_value,
86 PrefSyncStatus sync_status) {
87 RegisterSyncablePreference(
88 path, new base::FundamentalValue(default_value), sync_status);
91 void PrefRegistrySyncable::RegisterDoublePref(const char* path,
92 double default_value,
93 PrefSyncStatus sync_status) {
94 RegisterSyncablePreference(
95 path, new base::FundamentalValue(default_value), sync_status);
98 void PrefRegistrySyncable::RegisterStringPref(const char* path,
99 const std::string& default_value,
100 PrefSyncStatus sync_status) {
101 RegisterSyncablePreference(
102 path, new base::StringValue(default_value), sync_status);
105 void PrefRegistrySyncable::RegisterFilePathPref(
106 const char* path,
107 const base::FilePath& default_value,
108 PrefSyncStatus sync_status) {
109 RegisterSyncablePreference(
110 path, new base::StringValue(default_value.value()), sync_status);
113 void PrefRegistrySyncable::RegisterListPref(const char* path,
114 PrefSyncStatus sync_status) {
115 RegisterSyncablePreference(path, new base::ListValue(), sync_status);
118 void PrefRegistrySyncable::RegisterListPref(const char* path,
119 base::ListValue* default_value,
120 PrefSyncStatus sync_status) {
121 RegisterSyncablePreference(path, default_value, sync_status);
124 void PrefRegistrySyncable::RegisterDictionaryPref(const char* path,
125 PrefSyncStatus sync_status) {
126 RegisterSyncablePreference(path, new base::DictionaryValue(), sync_status);
129 void PrefRegistrySyncable::RegisterDictionaryPref(
130 const char* path,
131 base::DictionaryValue* default_value,
132 PrefSyncStatus sync_status) {
133 RegisterSyncablePreference(path, default_value, sync_status);
136 void PrefRegistrySyncable::RegisterLocalizedBooleanPref(
137 const char* path,
138 int locale_default_message_id,
139 PrefSyncStatus sync_status) {
140 RegisterSyncablePreference(
141 path,
142 CreateLocaleDefaultValue(base::Value::TYPE_BOOLEAN,
143 locale_default_message_id),
144 sync_status);
147 void PrefRegistrySyncable::RegisterLocalizedIntegerPref(
148 const char* path,
149 int locale_default_message_id,
150 PrefSyncStatus sync_status) {
151 RegisterSyncablePreference(
152 path,
153 CreateLocaleDefaultValue(base::Value::TYPE_INTEGER,
154 locale_default_message_id),
155 sync_status);
158 void PrefRegistrySyncable::RegisterLocalizedDoublePref(
159 const char* path,
160 int locale_default_message_id,
161 PrefSyncStatus sync_status) {
162 RegisterSyncablePreference(
163 path,
164 CreateLocaleDefaultValue(base::Value::TYPE_DOUBLE,
165 locale_default_message_id),
166 sync_status);
169 void PrefRegistrySyncable::RegisterLocalizedStringPref(
170 const char* path,
171 int locale_default_message_id,
172 PrefSyncStatus sync_status) {
173 RegisterSyncablePreference(
174 path,
175 CreateLocaleDefaultValue(base::Value::TYPE_STRING,
176 locale_default_message_id),
177 sync_status);
180 void PrefRegistrySyncable::RegisterInt64Pref(
181 const char* path,
182 int64 default_value,
183 PrefSyncStatus sync_status) {
184 RegisterSyncablePreference(
185 path,
186 new base::StringValue(base::Int64ToString(default_value)),
187 sync_status);
190 void PrefRegistrySyncable::RegisterUint64Pref(
191 const char* path,
192 uint64 default_value,
193 PrefSyncStatus sync_status) {
194 RegisterSyncablePreference(
195 path,
196 new base::StringValue(base::Uint64ToString(default_value)),
197 sync_status);
200 void PrefRegistrySyncable::RegisterSyncablePreference(
201 const char* path,
202 base::Value* default_value,
203 PrefSyncStatus sync_status) {
204 PrefRegistry::RegisterPreference(path, default_value);
206 if (sync_status == PrefRegistrySyncable::SYNCABLE_PREF ||
207 sync_status == PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF) {
208 syncable_preferences_[path] = sync_status;
210 if (!callback_.is_null())
211 callback_.Run(path, sync_status);
215 scoped_refptr<PrefRegistrySyncable> PrefRegistrySyncable::ForkForIncognito() {
216 // TODO(joi): We can directly reuse the same PrefRegistry once
217 // PrefService no longer registers for callbacks on registration and
218 // unregistration.
219 scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable());
220 registry->defaults_ = defaults_;
221 return registry;
224 } // namespace user_prefs