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
5 #include "components/user_prefs/pref_registry_syncable.h"
6
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"
12
13 namespace user_prefs {
14
15 namespace {
16
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.
CreateLocaleDefaultValue(base::Value::Type type,int message_id)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 Value::TYPE_BOOLEAN: {
27 if ("true" == resource_string)
28 return Value::CreateBooleanValue(true);
29 if ("false" == resource_string)
30 return Value::CreateBooleanValue(false);
31 break;
32 }
33
34 case Value::TYPE_INTEGER: {
35 int val;
36 base::StringToInt(resource_string, &val);
37 return Value::CreateIntegerValue(val);
38 }
39
40 case Value::TYPE_DOUBLE: {
41 double val;
42 base::StringToDouble(resource_string, &val);
43 return Value::CreateDoubleValue(val);
44 }
45
46 case Value::TYPE_STRING: {
47 return Value::CreateStringValue(resource_string);
48 }
49
50 default: {
51 NOTREACHED() <<
52 "list and dictionary types cannot have default locale values";
53 }
54 }
55 NOTREACHED();
56 return Value::CreateNullValue();
57 }
58
59 } // namespace
60
PrefRegistrySyncable()61 PrefRegistrySyncable::PrefRegistrySyncable() {
62 }
63
~PrefRegistrySyncable()64 PrefRegistrySyncable::~PrefRegistrySyncable() {
65 }
66
67 const PrefRegistrySyncable::PrefToStatus&
syncable_preferences() const68 PrefRegistrySyncable::syncable_preferences() const {
69 return syncable_preferences_;
70 }
71
SetSyncableRegistrationCallback(const SyncableRegistrationCallback & cb)72 void PrefRegistrySyncable::SetSyncableRegistrationCallback(
73 const SyncableRegistrationCallback& cb) {
74 callback_ = cb;
75 }
76
RegisterBooleanPref(const char * path,bool default_value,PrefSyncStatus sync_status)77 void PrefRegistrySyncable::RegisterBooleanPref(const char* path,
78 bool default_value,
79 PrefSyncStatus sync_status) {
80 RegisterSyncablePreference(path,
81 Value::CreateBooleanValue(default_value),
82 sync_status);
83 }
84
RegisterIntegerPref(const char * path,int default_value,PrefSyncStatus sync_status)85 void PrefRegistrySyncable::RegisterIntegerPref(const char* path,
86 int default_value,
87 PrefSyncStatus sync_status) {
88 RegisterSyncablePreference(path,
89 Value::CreateIntegerValue(default_value),
90 sync_status);
91 }
92
RegisterDoublePref(const char * path,double default_value,PrefSyncStatus sync_status)93 void PrefRegistrySyncable::RegisterDoublePref(const char* path,
94 double default_value,
95 PrefSyncStatus sync_status) {
96 RegisterSyncablePreference(path,
97 Value::CreateDoubleValue(default_value),
98 sync_status);
99 }
100
RegisterStringPref(const char * path,const std::string & default_value,PrefSyncStatus sync_status)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),
106 sync_status);
107 }
108
RegisterFilePathPref(const char * path,const base::FilePath & default_value,PrefSyncStatus sync_status)109 void PrefRegistrySyncable::RegisterFilePathPref(
110 const char* path,
111 const base::FilePath& default_value,
112 PrefSyncStatus sync_status) {
113 RegisterSyncablePreference(path,
114 Value::CreateStringValue(default_value.value()),
115 sync_status);
116 }
117
RegisterListPref(const char * path,PrefSyncStatus sync_status)118 void PrefRegistrySyncable::RegisterListPref(const char* path,
119 PrefSyncStatus sync_status) {
120 RegisterSyncablePreference(path, new ListValue(), sync_status);
121 }
122
RegisterListPref(const char * path,ListValue * default_value,PrefSyncStatus sync_status)123 void PrefRegistrySyncable::RegisterListPref(const char* path,
124 ListValue* default_value,
125 PrefSyncStatus sync_status) {
126 RegisterSyncablePreference(path, default_value, sync_status);
127 }
128
RegisterDictionaryPref(const char * path,PrefSyncStatus sync_status)129 void PrefRegistrySyncable::RegisterDictionaryPref(const char* path,
130 PrefSyncStatus sync_status) {
131 RegisterSyncablePreference(path, new DictionaryValue(), sync_status);
132 }
133
RegisterDictionaryPref(const char * path,DictionaryValue * default_value,PrefSyncStatus sync_status)134 void PrefRegistrySyncable::RegisterDictionaryPref(
135 const char* path,
136 DictionaryValue* default_value,
137 PrefSyncStatus sync_status) {
138 RegisterSyncablePreference(path, default_value, sync_status);
139 }
140
RegisterLocalizedBooleanPref(const char * path,int locale_default_message_id,PrefSyncStatus sync_status)141 void PrefRegistrySyncable::RegisterLocalizedBooleanPref(
142 const char* path,
143 int locale_default_message_id,
144 PrefSyncStatus sync_status) {
145 RegisterSyncablePreference(
146 path,
147 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id),
148 sync_status);
149 }
150
RegisterLocalizedIntegerPref(const char * path,int locale_default_message_id,PrefSyncStatus sync_status)151 void PrefRegistrySyncable::RegisterLocalizedIntegerPref(
152 const char* path,
153 int locale_default_message_id,
154 PrefSyncStatus sync_status) {
155 RegisterSyncablePreference(
156 path,
157 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id),
158 sync_status);
159 }
160
RegisterLocalizedDoublePref(const char * path,int locale_default_message_id,PrefSyncStatus sync_status)161 void PrefRegistrySyncable::RegisterLocalizedDoublePref(
162 const char* path,
163 int locale_default_message_id,
164 PrefSyncStatus sync_status) {
165 RegisterSyncablePreference(
166 path,
167 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id),
168 sync_status);
169 }
170
RegisterLocalizedStringPref(const char * path,int locale_default_message_id,PrefSyncStatus sync_status)171 void PrefRegistrySyncable::RegisterLocalizedStringPref(
172 const char* path,
173 int locale_default_message_id,
174 PrefSyncStatus sync_status) {
175 RegisterSyncablePreference(
176 path,
177 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id),
178 sync_status);
179 }
180
RegisterInt64Pref(const char * path,int64 default_value,PrefSyncStatus sync_status)181 void PrefRegistrySyncable::RegisterInt64Pref(
182 const char* path,
183 int64 default_value,
184 PrefSyncStatus sync_status) {
185 RegisterSyncablePreference(
186 path,
187 Value::CreateStringValue(base::Int64ToString(default_value)),
188 sync_status);
189 }
190
RegisterUint64Pref(const char * path,uint64 default_value,PrefSyncStatus sync_status)191 void PrefRegistrySyncable::RegisterUint64Pref(
192 const char* path,
193 uint64 default_value,
194 PrefSyncStatus sync_status) {
195 RegisterSyncablePreference(
196 path,
197 Value::CreateStringValue(base::Uint64ToString(default_value)),
198 sync_status);
199 }
200
RegisterSyncablePreference(const char * path,base::Value * default_value,PrefSyncStatus sync_status)201 void PrefRegistrySyncable::RegisterSyncablePreference(
202 const char* path,
203 base::Value* default_value,
204 PrefSyncStatus sync_status) {
205 PrefRegistry::RegisterPreference(path, default_value);
206
207 if (sync_status == PrefRegistrySyncable::SYNCABLE_PREF ||
208 sync_status == PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF) {
209 syncable_preferences_[path] = sync_status;
210
211 if (!callback_.is_null())
212 callback_.Run(path, sync_status);
213 }
214 }
215
ForkForIncognito()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
219 // unregistration.
220 scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable());
221 registry->defaults_ = defaults_;
222 return registry;
223 }
224
225 } // namespace user_prefs
226