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_service_syncable.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" |
| 9 #include "base/prefs/default_pref_store.h" |
| 10 #include "base/prefs/overlay_user_pref_store.h" |
| 11 #include "base/string_number_conversions.h" |
| 12 #include "base/value_conversions.h" |
| 13 #include "chrome/browser/prefs/pref_model_associator.h" |
| 14 #include "chrome/browser/prefs/pref_notifier_impl.h" |
| 15 #include "chrome/browser/prefs/pref_service_observer.h" |
| 16 #include "chrome/browser/prefs/pref_value_store.h" |
| 17 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" |
| 18 #include "ui/base/l10n/l10n_util.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 // A helper function for RegisterLocalized*Pref that creates a Value* |
| 23 // based on a localized resource. Because we control the values in a |
| 24 // locale dll, this should always return a Value of the appropriate |
| 25 // type. |
| 26 Value* CreateLocaleDefaultValue(base::Value::Type type, |
| 27 int message_id) { |
| 28 const std::string& resource_string = l10n_util::GetStringUTF8(message_id); |
| 29 DCHECK(!resource_string.empty()); |
| 30 switch (type) { |
| 31 case Value::TYPE_BOOLEAN: { |
| 32 if ("true" == resource_string) |
| 33 return Value::CreateBooleanValue(true); |
| 34 if ("false" == resource_string) |
| 35 return Value::CreateBooleanValue(false); |
| 36 break; |
| 37 } |
| 38 |
| 39 case Value::TYPE_INTEGER: { |
| 40 int val; |
| 41 base::StringToInt(resource_string, &val); |
| 42 return Value::CreateIntegerValue(val); |
| 43 } |
| 44 |
| 45 case Value::TYPE_DOUBLE: { |
| 46 double val; |
| 47 base::StringToDouble(resource_string, &val); |
| 48 return Value::CreateDoubleValue(val); |
| 49 } |
| 50 |
| 51 case Value::TYPE_STRING: { |
| 52 return Value::CreateStringValue(resource_string); |
| 53 } |
| 54 |
| 55 default: { |
| 56 NOTREACHED() << |
| 57 "list and dictionary types cannot have default locale values"; |
| 58 } |
| 59 } |
| 60 NOTREACHED(); |
| 61 return Value::CreateNullValue(); |
| 62 } |
| 63 |
| 64 } // namespace |
| 65 |
| 66 PrefServiceSyncable::PrefServiceSyncable( |
| 67 PrefNotifierImpl* pref_notifier, |
| 68 PrefValueStore* pref_value_store, |
| 69 PersistentPrefStore* user_prefs, |
| 70 DefaultPrefStore* default_store, |
| 71 base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 72 read_error_callback, |
| 73 bool async) |
| 74 : PrefService(pref_notifier, |
| 75 pref_value_store, |
| 76 user_prefs, |
| 77 default_store, |
| 78 read_error_callback, |
| 79 async) { |
| 80 pref_sync_associator_.SetPrefService(this); |
| 81 pref_value_store->set_sync_associator(&pref_sync_associator_); |
| 82 } |
| 83 |
| 84 PrefServiceSyncable::~PrefServiceSyncable() {} |
| 85 |
| 86 PrefServiceSyncable* PrefServiceSyncable::CreateIncognitoPrefService( |
| 87 PrefStore* incognito_extension_prefs) { |
| 88 pref_service_forked_ = true; |
| 89 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); |
| 90 OverlayUserPrefStore* incognito_pref_store = |
| 91 new OverlayUserPrefStore(user_pref_store_.get()); |
| 92 PrefsTabHelper::InitIncognitoUserPrefStore(incognito_pref_store); |
| 93 PrefServiceSyncable* incognito_service = new PrefServiceSyncable( |
| 94 pref_notifier, |
| 95 pref_value_store_->CloneAndSpecialize( |
| 96 NULL, // managed |
| 97 incognito_extension_prefs, |
| 98 NULL, // command_line_prefs |
| 99 incognito_pref_store, |
| 100 NULL, // recommended |
| 101 default_store_.get(), |
| 102 pref_notifier), |
| 103 incognito_pref_store, |
| 104 default_store_.get(), |
| 105 read_error_callback_, |
| 106 false); |
| 107 return incognito_service; |
| 108 } |
| 109 |
| 110 bool PrefServiceSyncable::IsSyncing() { |
| 111 return pref_sync_associator_.models_associated(); |
| 112 } |
| 113 |
| 114 void PrefServiceSyncable::AddObserver(PrefServiceObserver* observer) { |
| 115 observer_list_.AddObserver(observer); |
| 116 } |
| 117 |
| 118 void PrefServiceSyncable::RemoveObserver(PrefServiceObserver* observer) { |
| 119 observer_list_.RemoveObserver(observer); |
| 120 } |
| 121 |
| 122 void PrefServiceSyncable::UnregisterPreference(const char* path) { |
| 123 PrefService::UnregisterPreference(path); |
| 124 if (pref_sync_associator_.IsPrefRegistered(path)) { |
| 125 pref_sync_associator_.UnregisterPref(path); |
| 126 } |
| 127 } |
| 128 |
| 129 void PrefServiceSyncable::RegisterBooleanPref(const char* path, |
| 130 bool default_value, |
| 131 PrefSyncStatus sync_status) { |
| 132 RegisterSyncablePreference(path, |
| 133 Value::CreateBooleanValue(default_value), |
| 134 sync_status); |
| 135 } |
| 136 |
| 137 void PrefServiceSyncable::RegisterIntegerPref(const char* path, |
| 138 int default_value, |
| 139 PrefSyncStatus sync_status) { |
| 140 RegisterSyncablePreference(path, |
| 141 Value::CreateIntegerValue(default_value), |
| 142 sync_status); |
| 143 } |
| 144 |
| 145 void PrefServiceSyncable::RegisterDoublePref(const char* path, |
| 146 double default_value, |
| 147 PrefSyncStatus sync_status) { |
| 148 RegisterSyncablePreference(path, |
| 149 Value::CreateDoubleValue(default_value), |
| 150 sync_status); |
| 151 } |
| 152 |
| 153 void PrefServiceSyncable::RegisterStringPref(const char* path, |
| 154 const std::string& default_value, |
| 155 PrefSyncStatus sync_status) { |
| 156 RegisterSyncablePreference(path, |
| 157 Value::CreateStringValue(default_value), |
| 158 sync_status); |
| 159 } |
| 160 |
| 161 void PrefServiceSyncable::RegisterFilePathPref(const char* path, |
| 162 const FilePath& default_value, |
| 163 PrefSyncStatus sync_status) { |
| 164 RegisterSyncablePreference(path, |
| 165 Value::CreateStringValue(default_value.value()), |
| 166 sync_status); |
| 167 } |
| 168 |
| 169 void PrefServiceSyncable::RegisterListPref(const char* path, |
| 170 PrefSyncStatus sync_status) { |
| 171 RegisterSyncablePreference(path, new ListValue(), sync_status); |
| 172 } |
| 173 |
| 174 void PrefServiceSyncable::RegisterListPref(const char* path, |
| 175 ListValue* default_value, |
| 176 PrefSyncStatus sync_status) { |
| 177 RegisterSyncablePreference(path, default_value, sync_status); |
| 178 } |
| 179 |
| 180 void PrefServiceSyncable::RegisterDictionaryPref(const char* path, |
| 181 PrefSyncStatus sync_status) { |
| 182 RegisterSyncablePreference(path, new DictionaryValue(), sync_status); |
| 183 } |
| 184 |
| 185 void PrefServiceSyncable::RegisterDictionaryPref(const char* path, |
| 186 DictionaryValue* default_value, |
| 187 PrefSyncStatus sync_status) { |
| 188 RegisterSyncablePreference(path, default_value, sync_status); |
| 189 } |
| 190 |
| 191 void PrefServiceSyncable::RegisterLocalizedBooleanPref( |
| 192 const char* path, |
| 193 int locale_default_message_id, |
| 194 PrefSyncStatus sync_status) { |
| 195 RegisterSyncablePreference( |
| 196 path, |
| 197 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), |
| 198 sync_status); |
| 199 } |
| 200 |
| 201 void PrefServiceSyncable::RegisterLocalizedIntegerPref( |
| 202 const char* path, |
| 203 int locale_default_message_id, |
| 204 PrefSyncStatus sync_status) { |
| 205 RegisterSyncablePreference( |
| 206 path, |
| 207 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), |
| 208 sync_status); |
| 209 } |
| 210 |
| 211 void PrefServiceSyncable::RegisterLocalizedDoublePref( |
| 212 const char* path, |
| 213 int locale_default_message_id, |
| 214 PrefSyncStatus sync_status) { |
| 215 RegisterSyncablePreference( |
| 216 path, |
| 217 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), |
| 218 sync_status); |
| 219 } |
| 220 |
| 221 void PrefServiceSyncable::RegisterLocalizedStringPref( |
| 222 const char* path, |
| 223 int locale_default_message_id, |
| 224 PrefSyncStatus sync_status) { |
| 225 RegisterSyncablePreference( |
| 226 path, |
| 227 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), |
| 228 sync_status); |
| 229 } |
| 230 |
| 231 void PrefServiceSyncable::RegisterInt64Pref( |
| 232 const char* path, |
| 233 int64 default_value, |
| 234 PrefSyncStatus sync_status) { |
| 235 RegisterSyncablePreference( |
| 236 path, |
| 237 Value::CreateStringValue(base::Int64ToString(default_value)), |
| 238 sync_status); |
| 239 } |
| 240 |
| 241 void PrefServiceSyncable::RegisterUint64Pref( |
| 242 const char* path, |
| 243 uint64 default_value, |
| 244 PrefSyncStatus sync_status) { |
| 245 RegisterSyncablePreference( |
| 246 path, |
| 247 Value::CreateStringValue(base::Uint64ToString(default_value)), |
| 248 sync_status); |
| 249 } |
| 250 |
| 251 syncer::SyncableService* PrefServiceSyncable::GetSyncableService() { |
| 252 return &pref_sync_associator_; |
| 253 } |
| 254 |
| 255 void PrefServiceSyncable::UpdateCommandLinePrefStore( |
| 256 PrefStore* cmd_line_store) { |
| 257 // If |pref_service_forked_| is true, then this PrefService and the forked |
| 258 // copies will be out of sync. |
| 259 DCHECK(!pref_service_forked_); |
| 260 PrefService::UpdateCommandLinePrefStore(cmd_line_store); |
| 261 } |
| 262 |
| 263 void PrefServiceSyncable::OnIsSyncingChanged() { |
| 264 FOR_EACH_OBSERVER(PrefServiceObserver, observer_list_, OnIsSyncingChanged()); |
| 265 } |
| 266 |
| 267 void PrefServiceSyncable::RegisterSyncablePreference( |
| 268 const char* path, Value* default_value, PrefSyncStatus sync_status) { |
| 269 PrefService::RegisterPreference(path, default_value); |
| 270 // Register with sync if necessary. |
| 271 if (sync_status == SYNCABLE_PREF) |
| 272 pref_sync_associator_.RegisterPref(path); |
| 273 } |
OLD | NEW |