OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/user_prefs/pref_registry_syncable.h" | 5 #include "components/user_prefs/pref_registry_syncable.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/prefs/default_pref_store.h" | 8 #include "base/prefs/default_pref_store.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
11 #include "ui/base/l10n/l10n_util.h" | 11 #include "ui/base/l10n/l10n_util.h" |
12 | 12 |
13 namespace user_prefs { | 13 namespace user_prefs { |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // A helper function for RegisterLocalized*Pref that creates a Value* | 17 // A helper function for RegisterLocalized*Pref that creates a Value* |
18 // based on a localized resource. Because we control the values in a | 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 | 19 // locale dll, this should always return a Value of the appropriate |
20 // type. | 20 // type. |
21 base::Value* CreateLocaleDefaultValue(base::Value::Type type, | 21 base::Value* CreateLocaleDefaultValue(base::Value::Type type, |
22 int message_id) { | 22 int message_id) { |
23 const std::string resource_string = l10n_util::GetStringUTF8(message_id); | 23 const std::string resource_string = l10n_util::GetStringUTF8(message_id); |
24 DCHECK(!resource_string.empty()); | 24 DCHECK(!resource_string.empty()); |
25 switch (type) { | 25 switch (type) { |
26 case Value::TYPE_BOOLEAN: { | 26 case base::Value::TYPE_BOOLEAN: { |
27 if ("true" == resource_string) | 27 if ("true" == resource_string) |
28 return Value::CreateBooleanValue(true); | 28 return base::Value::CreateBooleanValue(true); |
29 if ("false" == resource_string) | 29 if ("false" == resource_string) |
30 return Value::CreateBooleanValue(false); | 30 return base::Value::CreateBooleanValue(false); |
31 break; | 31 break; |
32 } | 32 } |
33 | 33 |
34 case Value::TYPE_INTEGER: { | 34 case base::Value::TYPE_INTEGER: { |
35 int val; | 35 int val; |
36 base::StringToInt(resource_string, &val); | 36 base::StringToInt(resource_string, &val); |
37 return Value::CreateIntegerValue(val); | 37 return base::Value::CreateIntegerValue(val); |
38 } | 38 } |
39 | 39 |
40 case Value::TYPE_DOUBLE: { | 40 case base::Value::TYPE_DOUBLE: { |
41 double val; | 41 double val; |
42 base::StringToDouble(resource_string, &val); | 42 base::StringToDouble(resource_string, &val); |
43 return Value::CreateDoubleValue(val); | 43 return base::Value::CreateDoubleValue(val); |
44 } | 44 } |
45 | 45 |
46 case Value::TYPE_STRING: { | 46 case base::Value::TYPE_STRING: { |
47 return Value::CreateStringValue(resource_string); | 47 return base::Value::CreateStringValue(resource_string); |
48 } | 48 } |
49 | 49 |
50 default: { | 50 default: { |
51 NOTREACHED() << | 51 NOTREACHED() << |
52 "list and dictionary types cannot have default locale values"; | 52 "list and dictionary types cannot have default locale values"; |
53 } | 53 } |
54 } | 54 } |
55 NOTREACHED(); | 55 NOTREACHED(); |
56 return Value::CreateNullValue(); | 56 return base::Value::CreateNullValue(); |
57 } | 57 } |
58 | 58 |
59 } // namespace | 59 } // namespace |
60 | 60 |
61 PrefRegistrySyncable::PrefRegistrySyncable() { | 61 PrefRegistrySyncable::PrefRegistrySyncable() { |
62 } | 62 } |
63 | 63 |
64 PrefRegistrySyncable::~PrefRegistrySyncable() { | 64 PrefRegistrySyncable::~PrefRegistrySyncable() { |
65 } | 65 } |
66 | 66 |
67 const PrefRegistrySyncable::PrefToStatus& | 67 const PrefRegistrySyncable::PrefToStatus& |
68 PrefRegistrySyncable::syncable_preferences() const { | 68 PrefRegistrySyncable::syncable_preferences() const { |
69 return syncable_preferences_; | 69 return syncable_preferences_; |
70 } | 70 } |
71 | 71 |
72 void PrefRegistrySyncable::SetSyncableRegistrationCallback( | 72 void PrefRegistrySyncable::SetSyncableRegistrationCallback( |
73 const SyncableRegistrationCallback& cb) { | 73 const SyncableRegistrationCallback& cb) { |
74 callback_ = cb; | 74 callback_ = cb; |
75 } | 75 } |
76 | 76 |
77 void PrefRegistrySyncable::RegisterBooleanPref(const char* path, | 77 void PrefRegistrySyncable::RegisterBooleanPref(const char* path, |
78 bool default_value, | 78 bool default_value, |
79 PrefSyncStatus sync_status) { | 79 PrefSyncStatus sync_status) { |
80 RegisterSyncablePreference(path, | 80 RegisterSyncablePreference(path, |
81 Value::CreateBooleanValue(default_value), | 81 base::Value::CreateBooleanValue(default_value), |
82 sync_status); | 82 sync_status); |
83 } | 83 } |
84 | 84 |
85 void PrefRegistrySyncable::RegisterIntegerPref(const char* path, | 85 void PrefRegistrySyncable::RegisterIntegerPref(const char* path, |
86 int default_value, | 86 int default_value, |
87 PrefSyncStatus sync_status) { | 87 PrefSyncStatus sync_status) { |
88 RegisterSyncablePreference(path, | 88 RegisterSyncablePreference(path, |
89 Value::CreateIntegerValue(default_value), | 89 base::Value::CreateIntegerValue(default_value), |
90 sync_status); | 90 sync_status); |
91 } | 91 } |
92 | 92 |
93 void PrefRegistrySyncable::RegisterDoublePref(const char* path, | 93 void PrefRegistrySyncable::RegisterDoublePref(const char* path, |
94 double default_value, | 94 double default_value, |
95 PrefSyncStatus sync_status) { | 95 PrefSyncStatus sync_status) { |
96 RegisterSyncablePreference(path, | 96 RegisterSyncablePreference(path, |
97 Value::CreateDoubleValue(default_value), | 97 base::Value::CreateDoubleValue(default_value), |
98 sync_status); | 98 sync_status); |
99 } | 99 } |
100 | 100 |
101 void PrefRegistrySyncable::RegisterStringPref(const char* path, | 101 void PrefRegistrySyncable::RegisterStringPref(const char* path, |
102 const std::string& default_value, | 102 const std::string& default_value, |
103 PrefSyncStatus sync_status) { | 103 PrefSyncStatus sync_status) { |
104 RegisterSyncablePreference(path, | 104 RegisterSyncablePreference(path, |
105 Value::CreateStringValue(default_value), | 105 base::Value::CreateStringValue(default_value), |
106 sync_status); | 106 sync_status); |
107 } | 107 } |
108 | 108 |
109 void PrefRegistrySyncable::RegisterFilePathPref( | 109 void PrefRegistrySyncable::RegisterFilePathPref( |
110 const char* path, | 110 const char* path, |
111 const base::FilePath& default_value, | 111 const base::FilePath& default_value, |
112 PrefSyncStatus sync_status) { | 112 PrefSyncStatus sync_status) { |
113 RegisterSyncablePreference(path, | 113 RegisterSyncablePreference(path, |
114 Value::CreateStringValue(default_value.value()), | 114 base::Value::CreateStringValue( |
| 115 default_value.value()), |
115 sync_status); | 116 sync_status); |
116 } | 117 } |
117 | 118 |
118 void PrefRegistrySyncable::RegisterListPref(const char* path, | 119 void PrefRegistrySyncable::RegisterListPref(const char* path, |
119 PrefSyncStatus sync_status) { | 120 PrefSyncStatus sync_status) { |
120 RegisterSyncablePreference(path, new ListValue(), sync_status); | 121 RegisterSyncablePreference(path, new base::ListValue(), sync_status); |
121 } | 122 } |
122 | 123 |
123 void PrefRegistrySyncable::RegisterListPref(const char* path, | 124 void PrefRegistrySyncable::RegisterListPref(const char* path, |
124 ListValue* default_value, | 125 base::ListValue* default_value, |
125 PrefSyncStatus sync_status) { | 126 PrefSyncStatus sync_status) { |
126 RegisterSyncablePreference(path, default_value, sync_status); | 127 RegisterSyncablePreference(path, default_value, sync_status); |
127 } | 128 } |
128 | 129 |
129 void PrefRegistrySyncable::RegisterDictionaryPref(const char* path, | 130 void PrefRegistrySyncable::RegisterDictionaryPref(const char* path, |
130 PrefSyncStatus sync_status) { | 131 PrefSyncStatus sync_status) { |
131 RegisterSyncablePreference(path, new DictionaryValue(), sync_status); | 132 RegisterSyncablePreference(path, new base::DictionaryValue(), sync_status); |
132 } | 133 } |
133 | 134 |
134 void PrefRegistrySyncable::RegisterDictionaryPref( | 135 void PrefRegistrySyncable::RegisterDictionaryPref( |
135 const char* path, | 136 const char* path, |
136 DictionaryValue* default_value, | 137 base::DictionaryValue* default_value, |
137 PrefSyncStatus sync_status) { | 138 PrefSyncStatus sync_status) { |
138 RegisterSyncablePreference(path, default_value, sync_status); | 139 RegisterSyncablePreference(path, default_value, sync_status); |
139 } | 140 } |
140 | 141 |
141 void PrefRegistrySyncable::RegisterLocalizedBooleanPref( | 142 void PrefRegistrySyncable::RegisterLocalizedBooleanPref( |
142 const char* path, | 143 const char* path, |
143 int locale_default_message_id, | 144 int locale_default_message_id, |
144 PrefSyncStatus sync_status) { | 145 PrefSyncStatus sync_status) { |
145 RegisterSyncablePreference( | 146 RegisterSyncablePreference( |
146 path, | 147 path, |
147 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), | 148 CreateLocaleDefaultValue(base::Value::TYPE_BOOLEAN, |
| 149 locale_default_message_id), |
148 sync_status); | 150 sync_status); |
149 } | 151 } |
150 | 152 |
151 void PrefRegistrySyncable::RegisterLocalizedIntegerPref( | 153 void PrefRegistrySyncable::RegisterLocalizedIntegerPref( |
152 const char* path, | 154 const char* path, |
153 int locale_default_message_id, | 155 int locale_default_message_id, |
154 PrefSyncStatus sync_status) { | 156 PrefSyncStatus sync_status) { |
155 RegisterSyncablePreference( | 157 RegisterSyncablePreference( |
156 path, | 158 path, |
157 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), | 159 CreateLocaleDefaultValue(base::Value::TYPE_INTEGER, |
| 160 locale_default_message_id), |
158 sync_status); | 161 sync_status); |
159 } | 162 } |
160 | 163 |
161 void PrefRegistrySyncable::RegisterLocalizedDoublePref( | 164 void PrefRegistrySyncable::RegisterLocalizedDoublePref( |
162 const char* path, | 165 const char* path, |
163 int locale_default_message_id, | 166 int locale_default_message_id, |
164 PrefSyncStatus sync_status) { | 167 PrefSyncStatus sync_status) { |
165 RegisterSyncablePreference( | 168 RegisterSyncablePreference( |
166 path, | 169 path, |
167 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), | 170 CreateLocaleDefaultValue(base::Value::TYPE_DOUBLE, |
| 171 locale_default_message_id), |
168 sync_status); | 172 sync_status); |
169 } | 173 } |
170 | 174 |
171 void PrefRegistrySyncable::RegisterLocalizedStringPref( | 175 void PrefRegistrySyncable::RegisterLocalizedStringPref( |
172 const char* path, | 176 const char* path, |
173 int locale_default_message_id, | 177 int locale_default_message_id, |
174 PrefSyncStatus sync_status) { | 178 PrefSyncStatus sync_status) { |
175 RegisterSyncablePreference( | 179 RegisterSyncablePreference( |
176 path, | 180 path, |
177 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), | 181 CreateLocaleDefaultValue(base::Value::TYPE_STRING, |
| 182 locale_default_message_id), |
178 sync_status); | 183 sync_status); |
179 } | 184 } |
180 | 185 |
181 void PrefRegistrySyncable::RegisterInt64Pref( | 186 void PrefRegistrySyncable::RegisterInt64Pref( |
182 const char* path, | 187 const char* path, |
183 int64 default_value, | 188 int64 default_value, |
184 PrefSyncStatus sync_status) { | 189 PrefSyncStatus sync_status) { |
185 RegisterSyncablePreference( | 190 RegisterSyncablePreference( |
186 path, | 191 path, |
187 Value::CreateStringValue(base::Int64ToString(default_value)), | 192 base::Value::CreateStringValue(base::Int64ToString(default_value)), |
188 sync_status); | 193 sync_status); |
189 } | 194 } |
190 | 195 |
191 void PrefRegistrySyncable::RegisterUint64Pref( | 196 void PrefRegistrySyncable::RegisterUint64Pref( |
192 const char* path, | 197 const char* path, |
193 uint64 default_value, | 198 uint64 default_value, |
194 PrefSyncStatus sync_status) { | 199 PrefSyncStatus sync_status) { |
195 RegisterSyncablePreference( | 200 RegisterSyncablePreference( |
196 path, | 201 path, |
197 Value::CreateStringValue(base::Uint64ToString(default_value)), | 202 base::Value::CreateStringValue(base::Uint64ToString(default_value)), |
198 sync_status); | 203 sync_status); |
199 } | 204 } |
200 | 205 |
201 void PrefRegistrySyncable::RegisterSyncablePreference( | 206 void PrefRegistrySyncable::RegisterSyncablePreference( |
202 const char* path, | 207 const char* path, |
203 base::Value* default_value, | 208 base::Value* default_value, |
204 PrefSyncStatus sync_status) { | 209 PrefSyncStatus sync_status) { |
205 PrefRegistry::RegisterPreference(path, default_value); | 210 PrefRegistry::RegisterPreference(path, default_value); |
206 | 211 |
207 if (sync_status == PrefRegistrySyncable::SYNCABLE_PREF || | 212 if (sync_status == PrefRegistrySyncable::SYNCABLE_PREF || |
208 sync_status == PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF) { | 213 sync_status == PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF) { |
209 syncable_preferences_[path] = sync_status; | 214 syncable_preferences_[path] = sync_status; |
210 | 215 |
211 if (!callback_.is_null()) | 216 if (!callback_.is_null()) |
212 callback_.Run(path, sync_status); | 217 callback_.Run(path, sync_status); |
213 } | 218 } |
214 } | 219 } |
215 | 220 |
216 scoped_refptr<PrefRegistrySyncable> PrefRegistrySyncable::ForkForIncognito() { | 221 scoped_refptr<PrefRegistrySyncable> PrefRegistrySyncable::ForkForIncognito() { |
217 // TODO(joi): We can directly reuse the same PrefRegistry once | 222 // TODO(joi): We can directly reuse the same PrefRegistry once |
218 // PrefService no longer registers for callbacks on registration and | 223 // PrefService no longer registers for callbacks on registration and |
219 // unregistration. | 224 // unregistration. |
220 scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable()); | 225 scoped_refptr<PrefRegistrySyncable> registry(new PrefRegistrySyncable()); |
221 registry->defaults_ = defaults_; | 226 registry->defaults_ = defaults_; |
222 return registry; | 227 return registry; |
223 } | 228 } |
224 | 229 |
225 } // namespace user_prefs | 230 } // namespace user_prefs |
OLD | NEW |