Chromium Code Reviews| Index: chrome/browser/prefs/pref_registry_syncable.cc |
| diff --git a/chrome/browser/prefs/pref_registry_syncable.cc b/chrome/browser/prefs/pref_registry_syncable.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f4e91ec61de1dfc0a2d40e7976be307a7b40b859 |
| --- /dev/null |
| +++ b/chrome/browser/prefs/pref_registry_syncable.cc |
| @@ -0,0 +1,195 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/prefs/pref_registry_syncable.h" |
| + |
| +#include "base/file_path.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/values.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +namespace { |
| + |
| +// A helper function for RegisterLocalized*Pref that creates a Value* |
| +// based on a localized resource. Because we control the values in a |
| +// locale dll, this should always return a Value of the appropriate |
| +// type. |
| +base::Value* CreateLocaleDefaultValue(base::Value::Type type, |
| + int message_id) { |
| + const std::string& resource_string = l10n_util::GetStringUTF8(message_id); |
| + DCHECK(!resource_string.empty()); |
| + switch (type) { |
| + case Value::TYPE_BOOLEAN: { |
| + if ("true" == resource_string) |
| + return Value::CreateBooleanValue(true); |
| + if ("false" == resource_string) |
| + return Value::CreateBooleanValue(false); |
| + break; |
| + } |
| + |
| + case Value::TYPE_INTEGER: { |
| + int val; |
| + base::StringToInt(resource_string, &val); |
| + return Value::CreateIntegerValue(val); |
| + } |
| + |
| + case Value::TYPE_DOUBLE: { |
| + double val; |
| + base::StringToDouble(resource_string, &val); |
| + return Value::CreateDoubleValue(val); |
| + } |
| + |
| + case Value::TYPE_STRING: { |
| + return Value::CreateStringValue(resource_string); |
| + } |
| + |
| + default: { |
| + NOTREACHED() << |
| + "list and dictionary types cannot have default locale values"; |
| + } |
| + } |
| + NOTREACHED(); |
| + return Value::CreateNullValue(); |
| +} |
| + |
| +} // namespace |
| + |
| +PrefRegistrySyncable::PrefRegistrySyncable() { |
| +} |
| + |
| +PrefRegistrySyncable::~PrefRegistrySyncable() { |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterBooleanPref(const char* path, |
| + bool default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, |
| + Value::CreateBooleanValue(default_value), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterIntegerPref(const char* path, |
| + int default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, |
| + Value::CreateIntegerValue(default_value), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterDoublePref(const char* path, |
| + double default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, |
| + Value::CreateDoubleValue(default_value), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterStringPref(const char* path, |
| + const std::string& default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, |
| + Value::CreateStringValue(default_value), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterFilePathPref(const char* path, |
| + const FilePath& default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, |
| + Value::CreateStringValue(default_value.value()), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterListPref(const char* path, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, new ListValue(), sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterListPref(const char* path, |
| + ListValue* default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, default_value, sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterDictionaryPref(const char* path, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, new DictionaryValue(), sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterDictionaryPref( |
| + const char* path, |
| + DictionaryValue* default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference(path, default_value, sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterLocalizedBooleanPref( |
| + const char* path, |
| + int locale_default_message_id, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference( |
| + path, |
| + CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterLocalizedIntegerPref( |
| + const char* path, |
| + int locale_default_message_id, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference( |
| + path, |
| + CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterLocalizedDoublePref( |
| + const char* path, |
| + int locale_default_message_id, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference( |
| + path, |
| + CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterLocalizedStringPref( |
| + const char* path, |
| + int locale_default_message_id, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference( |
| + path, |
| + CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterInt64Pref( |
| + const char* path, |
| + int64 default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference( |
| + path, |
| + Value::CreateStringValue(base::Int64ToString(default_value)), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterUint64Pref( |
| + const char* path, |
| + uint64 default_value, |
| + PrefSyncStatus sync_status) { |
| + RegisterSyncablePreference( |
| + path, |
| + Value::CreateStringValue(base::Uint64ToString(default_value)), |
| + sync_status); |
| +} |
| + |
| +void PrefRegistrySyncable::RegisterSyncablePreference( |
| + const char* path, |
| + base::Value* default_value, |
| + PrefSyncStatus sync_status) { |
|
Mattias Nissler (ping if slow)
2013/01/31 15:30:19
Some thoughts here: We were thinking of splitting
|
| + PrefRegistry::RegisterPreference(path, default_value); |
| + |
| + if (sync_status == SYNCABLE_PREF && !callback_.is_null()) |
| + callback_.Run(path); |
| +} |