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