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/user_prefs/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
{
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
21 base::Value
* CreateLocaleDefaultValue(base::Value::Type type
,
23 const std::string resource_string
= l10n_util::GetStringUTF8(message_id
);
24 DCHECK(!resource_string
.empty());
26 case Value::TYPE_BOOLEAN
: {
27 if ("true" == resource_string
)
28 return Value::CreateBooleanValue(true);
29 if ("false" == resource_string
)
30 return Value::CreateBooleanValue(false);
34 case Value::TYPE_INTEGER
: {
36 base::StringToInt(resource_string
, &val
);
37 return Value::CreateIntegerValue(val
);
40 case Value::TYPE_DOUBLE
: {
42 base::StringToDouble(resource_string
, &val
);
43 return Value::CreateDoubleValue(val
);
46 case Value::TYPE_STRING
: {
47 return Value::CreateStringValue(resource_string
);
52 "list and dictionary types cannot have default locale values";
56 return Value::CreateNullValue();
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
) {
77 void PrefRegistrySyncable::RegisterBooleanPref(const char* path
,
79 PrefSyncStatus sync_status
) {
80 RegisterSyncablePreference(path
,
81 Value::CreateBooleanValue(default_value
),
85 void PrefRegistrySyncable::RegisterIntegerPref(const char* path
,
87 PrefSyncStatus sync_status
) {
88 RegisterSyncablePreference(path
,
89 Value::CreateIntegerValue(default_value
),
93 void PrefRegistrySyncable::RegisterDoublePref(const char* path
,
95 PrefSyncStatus sync_status
) {
96 RegisterSyncablePreference(path
,
97 Value::CreateDoubleValue(default_value
),
101 void PrefRegistrySyncable::RegisterStringPref(const char* path
,
102 const std::string
& default_value
,
103 PrefSyncStatus sync_status
) {
104 RegisterSyncablePreference(path
,
105 Value::CreateStringValue(default_value
),
109 void PrefRegistrySyncable::RegisterFilePathPref(
111 const base::FilePath
& default_value
,
112 PrefSyncStatus sync_status
) {
113 RegisterSyncablePreference(path
,
114 Value::CreateStringValue(default_value
.value()),
118 void PrefRegistrySyncable::RegisterListPref(const char* path
,
119 PrefSyncStatus sync_status
) {
120 RegisterSyncablePreference(path
, new ListValue(), sync_status
);
123 void PrefRegistrySyncable::RegisterListPref(const char* path
,
124 ListValue
* default_value
,
125 PrefSyncStatus sync_status
) {
126 RegisterSyncablePreference(path
, default_value
, sync_status
);
129 void PrefRegistrySyncable::RegisterDictionaryPref(const char* path
,
130 PrefSyncStatus sync_status
) {
131 RegisterSyncablePreference(path
, new DictionaryValue(), sync_status
);
134 void PrefRegistrySyncable::RegisterDictionaryPref(
136 DictionaryValue
* default_value
,
137 PrefSyncStatus sync_status
) {
138 RegisterSyncablePreference(path
, default_value
, sync_status
);
141 void PrefRegistrySyncable::RegisterLocalizedBooleanPref(
143 int locale_default_message_id
,
144 PrefSyncStatus sync_status
) {
145 RegisterSyncablePreference(
147 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN
, locale_default_message_id
),
151 void PrefRegistrySyncable::RegisterLocalizedIntegerPref(
153 int locale_default_message_id
,
154 PrefSyncStatus sync_status
) {
155 RegisterSyncablePreference(
157 CreateLocaleDefaultValue(Value::TYPE_INTEGER
, locale_default_message_id
),
161 void PrefRegistrySyncable::RegisterLocalizedDoublePref(
163 int locale_default_message_id
,
164 PrefSyncStatus sync_status
) {
165 RegisterSyncablePreference(
167 CreateLocaleDefaultValue(Value::TYPE_DOUBLE
, locale_default_message_id
),
171 void PrefRegistrySyncable::RegisterLocalizedStringPref(
173 int locale_default_message_id
,
174 PrefSyncStatus sync_status
) {
175 RegisterSyncablePreference(
177 CreateLocaleDefaultValue(Value::TYPE_STRING
, locale_default_message_id
),
181 void PrefRegistrySyncable::RegisterInt64Pref(
184 PrefSyncStatus sync_status
) {
185 RegisterSyncablePreference(
187 Value::CreateStringValue(base::Int64ToString(default_value
)),
191 void PrefRegistrySyncable::RegisterUint64Pref(
193 uint64 default_value
,
194 PrefSyncStatus sync_status
) {
195 RegisterSyncablePreference(
197 Value::CreateStringValue(base::Uint64ToString(default_value
)),
201 void PrefRegistrySyncable::RegisterSyncablePreference(
203 base::Value
* default_value
,
204 PrefSyncStatus sync_status
) {
205 PrefRegistry::RegisterPreference(path
, default_value
);
207 if (sync_status
== PrefRegistrySyncable::SYNCABLE_PREF
||
208 sync_status
== PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF
) {
209 syncable_preferences_
[path
] = sync_status
;
211 if (!callback_
.is_null())
212 callback_
.Run(path
, sync_status
);
216 scoped_refptr
<PrefRegistrySyncable
> PrefRegistrySyncable::ForkForIncognito() {
217 // TODO(joi): We can directly reuse the same PrefRegistry once
218 // PrefService no longer registers for callbacks on registration and
220 scoped_refptr
<PrefRegistrySyncable
> registry(new PrefRegistrySyncable());
221 registry
->defaults_
= defaults_
;
225 } // namespace user_prefs