OLD | NEW |
---|---|
(Empty) | |
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 "chrome/browser/prefs/pref_registry_syncable.h" | |
6 | |
7 #include "base/file_path.h" | |
8 #include "base/string_number_conversions.h" | |
9 #include "base/values.h" | |
10 #include "ui/base/l10n/l10n_util.h" | |
11 | |
12 namespace { | |
13 | |
14 // A helper function for RegisterLocalized*Pref that creates a Value* | |
15 // based on a localized resource. Because we control the values in a | |
16 // locale dll, this should always return a Value of the appropriate | |
17 // type. | |
18 base::Value* CreateLocaleDefaultValue(base::Value::Type type, | |
19 int message_id) { | |
20 const std::string& resource_string = l10n_util::GetStringUTF8(message_id); | |
21 DCHECK(!resource_string.empty()); | |
22 switch (type) { | |
23 case Value::TYPE_BOOLEAN: { | |
24 if ("true" == resource_string) | |
25 return Value::CreateBooleanValue(true); | |
26 if ("false" == resource_string) | |
27 return Value::CreateBooleanValue(false); | |
28 break; | |
29 } | |
30 | |
31 case Value::TYPE_INTEGER: { | |
32 int val; | |
33 base::StringToInt(resource_string, &val); | |
34 return Value::CreateIntegerValue(val); | |
35 } | |
36 | |
37 case Value::TYPE_DOUBLE: { | |
38 double val; | |
39 base::StringToDouble(resource_string, &val); | |
40 return Value::CreateDoubleValue(val); | |
41 } | |
42 | |
43 case Value::TYPE_STRING: { | |
44 return Value::CreateStringValue(resource_string); | |
45 } | |
46 | |
47 default: { | |
48 NOTREACHED() << | |
49 "list and dictionary types cannot have default locale values"; | |
50 } | |
51 } | |
52 NOTREACHED(); | |
53 return Value::CreateNullValue(); | |
54 } | |
55 | |
56 } // namespace | |
57 | |
58 PrefRegistrySyncable::PrefRegistrySyncable() { | |
59 } | |
60 | |
61 PrefRegistrySyncable::~PrefRegistrySyncable() { | |
62 } | |
63 | |
64 void PrefRegistrySyncable::RegisterBooleanPref(const char* path, | |
65 bool default_value, | |
66 PrefSyncStatus sync_status) { | |
67 RegisterSyncablePreference(path, | |
68 Value::CreateBooleanValue(default_value), | |
69 sync_status); | |
70 } | |
71 | |
72 void PrefRegistrySyncable::RegisterIntegerPref(const char* path, | |
73 int default_value, | |
74 PrefSyncStatus sync_status) { | |
75 RegisterSyncablePreference(path, | |
76 Value::CreateIntegerValue(default_value), | |
77 sync_status); | |
78 } | |
79 | |
80 void PrefRegistrySyncable::RegisterDoublePref(const char* path, | |
81 double default_value, | |
82 PrefSyncStatus sync_status) { | |
83 RegisterSyncablePreference(path, | |
84 Value::CreateDoubleValue(default_value), | |
85 sync_status); | |
86 } | |
87 | |
88 void PrefRegistrySyncable::RegisterStringPref(const char* path, | |
89 const std::string& default_value, | |
90 PrefSyncStatus sync_status) { | |
91 RegisterSyncablePreference(path, | |
92 Value::CreateStringValue(default_value), | |
93 sync_status); | |
94 } | |
95 | |
96 void PrefRegistrySyncable::RegisterFilePathPref(const char* path, | |
97 const FilePath& default_value, | |
98 PrefSyncStatus sync_status) { | |
99 RegisterSyncablePreference(path, | |
100 Value::CreateStringValue(default_value.value()), | |
101 sync_status); | |
102 } | |
103 | |
104 void PrefRegistrySyncable::RegisterListPref(const char* path, | |
105 PrefSyncStatus sync_status) { | |
106 RegisterSyncablePreference(path, new ListValue(), sync_status); | |
107 } | |
108 | |
109 void PrefRegistrySyncable::RegisterListPref(const char* path, | |
110 ListValue* default_value, | |
111 PrefSyncStatus sync_status) { | |
112 RegisterSyncablePreference(path, default_value, sync_status); | |
113 } | |
114 | |
115 void PrefRegistrySyncable::RegisterDictionaryPref(const char* path, | |
116 PrefSyncStatus sync_status) { | |
117 RegisterSyncablePreference(path, new DictionaryValue(), sync_status); | |
118 } | |
119 | |
120 void PrefRegistrySyncable::RegisterDictionaryPref( | |
121 const char* path, | |
122 DictionaryValue* default_value, | |
123 PrefSyncStatus sync_status) { | |
124 RegisterSyncablePreference(path, default_value, sync_status); | |
125 } | |
126 | |
127 void PrefRegistrySyncable::RegisterLocalizedBooleanPref( | |
128 const char* path, | |
129 int locale_default_message_id, | |
130 PrefSyncStatus sync_status) { | |
131 RegisterSyncablePreference( | |
132 path, | |
133 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), | |
134 sync_status); | |
135 } | |
136 | |
137 void PrefRegistrySyncable::RegisterLocalizedIntegerPref( | |
138 const char* path, | |
139 int locale_default_message_id, | |
140 PrefSyncStatus sync_status) { | |
141 RegisterSyncablePreference( | |
142 path, | |
143 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), | |
144 sync_status); | |
145 } | |
146 | |
147 void PrefRegistrySyncable::RegisterLocalizedDoublePref( | |
148 const char* path, | |
149 int locale_default_message_id, | |
150 PrefSyncStatus sync_status) { | |
151 RegisterSyncablePreference( | |
152 path, | |
153 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), | |
154 sync_status); | |
155 } | |
156 | |
157 void PrefRegistrySyncable::RegisterLocalizedStringPref( | |
158 const char* path, | |
159 int locale_default_message_id, | |
160 PrefSyncStatus sync_status) { | |
161 RegisterSyncablePreference( | |
162 path, | |
163 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), | |
164 sync_status); | |
165 } | |
166 | |
167 void PrefRegistrySyncable::RegisterInt64Pref( | |
168 const char* path, | |
169 int64 default_value, | |
170 PrefSyncStatus sync_status) { | |
171 RegisterSyncablePreference( | |
172 path, | |
173 Value::CreateStringValue(base::Int64ToString(default_value)), | |
174 sync_status); | |
175 } | |
176 | |
177 void PrefRegistrySyncable::RegisterUint64Pref( | |
178 const char* path, | |
179 uint64 default_value, | |
180 PrefSyncStatus sync_status) { | |
181 RegisterSyncablePreference( | |
182 path, | |
183 Value::CreateStringValue(base::Uint64ToString(default_value)), | |
184 sync_status); | |
185 } | |
186 | |
187 void PrefRegistrySyncable::RegisterSyncablePreference( | |
188 const char* path, | |
189 base::Value* default_value, | |
190 PrefSyncStatus sync_status) { | |
Mattias Nissler (ping if slow)
2013/01/31 15:30:19
Some thoughts here: We were thinking of splitting
| |
191 PrefRegistry::RegisterPreference(path, default_value); | |
192 | |
193 if (sync_status == SYNCABLE_PREF && !callback_.is_null()) | |
194 callback_.Run(path); | |
195 } | |
OLD | NEW |