| 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 "chrome/browser/prefs/pref_service.h" | 5 #include "chrome/browser/prefs/pref_service.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | |
| 11 #include "base/file_path.h" | 10 #include "base/file_path.h" |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 14 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 15 #include "base/prefs/default_pref_store.h" | 14 #include "base/prefs/default_pref_store.h" |
| 16 #include "base/prefs/overlay_user_pref_store.h" | |
| 17 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
| 18 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
| 19 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 20 #include "base/value_conversions.h" | 18 #include "base/value_conversions.h" |
| 21 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 22 #include "chrome/browser/browser_process.h" | |
| 23 #include "chrome/browser/prefs/command_line_pref_store.h" | |
| 24 #include "chrome/browser/prefs/pref_model_associator.h" | |
| 25 #include "chrome/browser/prefs/pref_notifier_impl.h" | 20 #include "chrome/browser/prefs/pref_notifier_impl.h" |
| 26 #include "chrome/browser/prefs/pref_service_observer.h" | |
| 27 #include "chrome/browser/prefs/pref_value_store.h" | 21 #include "chrome/browser/prefs/pref_value_store.h" |
| 28 #include "chrome/browser/ui/prefs/prefs_tab_helper.h" | |
| 29 #include "ui/base/l10n/l10n_util.h" | |
| 30 | 22 |
| 31 using content::BrowserContext; | 23 using content::BrowserContext; |
| 32 | 24 |
| 33 namespace { | 25 namespace { |
| 34 | 26 |
| 35 // A helper function for RegisterLocalized*Pref that creates a Value* based on | |
| 36 // the string value in the locale dll. Because we control the values in a | |
| 37 // locale dll, this should always return a Value of the appropriate type. | |
| 38 Value* CreateLocaleDefaultValue(base::Value::Type type, int message_id) { | |
| 39 std::string resource_string = l10n_util::GetStringUTF8(message_id); | |
| 40 DCHECK(!resource_string.empty()); | |
| 41 switch (type) { | |
| 42 case Value::TYPE_BOOLEAN: { | |
| 43 if ("true" == resource_string) | |
| 44 return Value::CreateBooleanValue(true); | |
| 45 if ("false" == resource_string) | |
| 46 return Value::CreateBooleanValue(false); | |
| 47 break; | |
| 48 } | |
| 49 | |
| 50 case Value::TYPE_INTEGER: { | |
| 51 int val; | |
| 52 base::StringToInt(resource_string, &val); | |
| 53 return Value::CreateIntegerValue(val); | |
| 54 } | |
| 55 | |
| 56 case Value::TYPE_DOUBLE: { | |
| 57 double val; | |
| 58 base::StringToDouble(resource_string, &val); | |
| 59 return Value::CreateDoubleValue(val); | |
| 60 } | |
| 61 | |
| 62 case Value::TYPE_STRING: { | |
| 63 return Value::CreateStringValue(resource_string); | |
| 64 } | |
| 65 | |
| 66 default: { | |
| 67 NOTREACHED() << | |
| 68 "list and dictionary types cannot have default locale values"; | |
| 69 } | |
| 70 } | |
| 71 NOTREACHED(); | |
| 72 return Value::CreateNullValue(); | |
| 73 } | |
| 74 | |
| 75 // TODO(joi): Change the interface on PersistentPrefStore to just take | |
| 76 // a callback of this type. Then we can also typedef the callback in | |
| 77 // PersistentPrefStore and use that as the type of the callback used | |
| 78 // to initialize PrefService. | |
| 79 class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate { | 27 class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate { |
| 80 public: | 28 public: |
| 81 ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb) | 29 ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb) |
| 82 : callback_(cb) {} | 30 : callback_(cb) {} |
| 83 | 31 |
| 84 virtual void OnError(PersistentPrefStore::PrefReadError error) { | 32 virtual void OnError(PersistentPrefStore::PrefReadError error) { |
| 85 callback_.Run(error); | 33 callback_.Run(error); |
| 86 } | 34 } |
| 87 | 35 |
| 88 private: | 36 private: |
| 89 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; | 37 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; |
| 90 }; | 38 }; |
| 91 | 39 |
| 92 } // namespace | 40 } // namespace |
| 93 | 41 |
| 94 PrefService* PrefService::CreateIncognitoPrefService( | |
| 95 PrefStore* incognito_extension_prefs) { | |
| 96 pref_service_forked_ = true; | |
| 97 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); | |
| 98 OverlayUserPrefStore* incognito_pref_store = | |
| 99 new OverlayUserPrefStore(user_pref_store_.get()); | |
| 100 PrefsTabHelper::InitIncognitoUserPrefStore(incognito_pref_store); | |
| 101 return new PrefService( | |
| 102 pref_notifier, | |
| 103 pref_value_store_->CloneAndSpecialize( | |
| 104 NULL, // managed | |
| 105 incognito_extension_prefs, | |
| 106 NULL, // command_line_prefs | |
| 107 incognito_pref_store, | |
| 108 NULL, // recommended | |
| 109 default_store_.get(), | |
| 110 NULL, // pref_sync_associator | |
| 111 pref_notifier), | |
| 112 incognito_pref_store, | |
| 113 default_store_.get(), | |
| 114 NULL, | |
| 115 read_error_callback_, | |
| 116 false); | |
| 117 } | |
| 118 | |
| 119 PrefService::PrefService( | 42 PrefService::PrefService( |
| 120 PrefNotifierImpl* pref_notifier, | 43 PrefNotifierImpl* pref_notifier, |
| 121 PrefValueStore* pref_value_store, | 44 PrefValueStore* pref_value_store, |
| 122 PersistentPrefStore* user_prefs, | 45 PersistentPrefStore* user_prefs, |
| 123 DefaultPrefStore* default_store, | 46 DefaultPrefStore* default_store, |
| 124 PrefModelAssociator* pref_sync_associator, | |
| 125 base::Callback<void(PersistentPrefStore::PrefReadError)> | 47 base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 126 read_error_callback, | 48 read_error_callback, |
| 127 bool async) | 49 bool async) |
| 128 : pref_notifier_(pref_notifier), | 50 : pref_notifier_(pref_notifier), |
| 129 pref_value_store_(pref_value_store), | 51 pref_value_store_(pref_value_store), |
| 130 user_pref_store_(user_prefs), | 52 user_pref_store_(user_prefs), |
| 131 default_store_(default_store), | 53 default_store_(default_store), |
| 132 pref_sync_associator_(pref_sync_associator), | 54 read_error_callback_(read_error_callback) { |
| 133 read_error_callback_(read_error_callback), | |
| 134 pref_service_forked_(false) { | |
| 135 pref_notifier_->SetPrefService(this); | 55 pref_notifier_->SetPrefService(this); |
| 136 if (pref_sync_associator_.get()) | |
| 137 pref_sync_associator_->SetPrefService(this); | |
| 138 InitFromStorage(async); | 56 InitFromStorage(async); |
| 139 } | 57 } |
| 140 | 58 |
| 141 PrefService::~PrefService() { | 59 PrefService::~PrefService() { |
| 142 DCHECK(CalledOnValidThread()); | 60 DCHECK(CalledOnValidThread()); |
| 143 | 61 |
| 144 // Reset pointers so accesses after destruction reliably crash. | 62 // Reset pointers so accesses after destruction reliably crash. |
| 145 pref_value_store_.reset(); | 63 pref_value_store_.reset(); |
| 146 user_pref_store_ = NULL; | 64 user_pref_store_ = NULL; |
| 147 default_store_ = NULL; | 65 default_store_ = NULL; |
| 148 pref_sync_associator_.reset(); | |
| 149 pref_notifier_.reset(); | 66 pref_notifier_.reset(); |
| 150 } | 67 } |
| 151 | 68 |
| 152 void PrefService::InitFromStorage(bool async) { | 69 void PrefService::InitFromStorage(bool async) { |
| 153 if (!async) { | 70 if (!async) { |
| 154 read_error_callback_.Run(user_pref_store_->ReadPrefs()); | 71 read_error_callback_.Run(user_pref_store_->ReadPrefs()); |
| 155 } else { | 72 } else { |
| 156 // Guarantee that initialization happens after this function returned. | 73 // Guarantee that initialization happens after this function returned. |
| 157 MessageLoop::current()->PostTask( | 74 MessageLoop::current()->PostTask( |
| 158 FROM_HERE, | 75 FROM_HERE, |
| 159 base::Bind(&PersistentPrefStore::ReadPrefsAsync, | 76 base::Bind(&PersistentPrefStore::ReadPrefsAsync, |
| 160 user_pref_store_.get(), | 77 user_pref_store_.get(), |
| 161 new ReadErrorHandler(read_error_callback_))); | 78 new ReadErrorHandler(read_error_callback_))); |
| 162 } | 79 } |
| 163 } | 80 } |
| 164 | 81 |
| 165 bool PrefService::ReloadPersistentPrefs() { | 82 bool PrefService::ReloadPersistentPrefs() { |
| 166 return user_pref_store_->ReadPrefs() == | 83 return user_pref_store_->ReadPrefs() == |
| 167 PersistentPrefStore::PREF_READ_ERROR_NONE; | 84 PersistentPrefStore::PREF_READ_ERROR_NONE; |
| 168 } | 85 } |
| 169 | 86 |
| 170 void PrefService::CommitPendingWrite() { | 87 void PrefService::CommitPendingWrite() { |
| 171 DCHECK(CalledOnValidThread()); | 88 DCHECK(CalledOnValidThread()); |
| 172 user_pref_store_->CommitPendingWrite(); | 89 user_pref_store_->CommitPendingWrite(); |
| 173 } | 90 } |
| 174 | 91 |
| 175 void PrefService::AddObserver(PrefServiceObserver* observer) { | |
| 176 observer_list_.AddObserver(observer); | |
| 177 } | |
| 178 | |
| 179 void PrefService::RemoveObserver(PrefServiceObserver* observer) { | |
| 180 observer_list_.RemoveObserver(observer); | |
| 181 } | |
| 182 | |
| 183 bool PrefService::IsSyncing() { | |
| 184 return pref_sync_associator_.get() && | |
| 185 pref_sync_associator_->models_associated(); | |
| 186 } | |
| 187 | |
| 188 void PrefService::OnIsSyncingChanged() { | |
| 189 FOR_EACH_OBSERVER(PrefServiceObserver, observer_list_, OnIsSyncingChanged()); | |
| 190 } | |
| 191 | |
| 192 namespace { | |
| 193 | |
| 194 // If there's no g_browser_process or no local state, return true (for testing). | |
| 195 bool IsLocalStatePrefService(PrefService* prefs) { | |
| 196 return (!g_browser_process || | |
| 197 !g_browser_process->local_state() || | |
| 198 g_browser_process->local_state() == prefs); | |
| 199 } | |
| 200 | |
| 201 // If there's no g_browser_process, return true (for testing). | |
| 202 bool IsProfilePrefService(PrefService* prefs) { | |
| 203 // TODO(zea): uncomment this once all preferences are only ever registered | |
| 204 // with either the local_state's pref service or the profile's pref service. | |
| 205 // return (!g_browser_process || g_browser_process->local_state() != prefs); | |
| 206 return true; | |
| 207 } | |
| 208 | |
| 209 } // namespace | |
| 210 | |
| 211 // Local State prefs. | |
| 212 void PrefService::RegisterBooleanPref(const char* path, | |
| 213 bool default_value) { | |
| 214 // If this fails, the pref service in use is a profile pref service, so the | |
| 215 // sync status must be provided (see profile pref registration calls below). | |
| 216 DCHECK(IsLocalStatePrefService(this)); | |
| 217 RegisterPreference(path, | |
| 218 Value::CreateBooleanValue(default_value), | |
| 219 UNSYNCABLE_PREF); | |
| 220 } | |
| 221 | |
| 222 void PrefService::RegisterIntegerPref(const char* path, int default_value) { | |
| 223 // If this fails, the pref service in use is a profile pref service, so the | |
| 224 // sync status must be provided (see profile pref registration calls below). | |
| 225 DCHECK(IsLocalStatePrefService(this)); | |
| 226 RegisterPreference(path, | |
| 227 Value::CreateIntegerValue(default_value), | |
| 228 UNSYNCABLE_PREF); | |
| 229 } | |
| 230 | |
| 231 void PrefService::RegisterDoublePref(const char* path, double default_value) { | |
| 232 // If this fails, the pref service in use is a profile pref service, so the | |
| 233 // sync status must be provided (see profile pref registration calls below). | |
| 234 DCHECK(IsLocalStatePrefService(this)); | |
| 235 RegisterPreference(path, | |
| 236 Value::CreateDoubleValue(default_value), | |
| 237 UNSYNCABLE_PREF); | |
| 238 } | |
| 239 | |
| 240 void PrefService::RegisterStringPref(const char* path, | |
| 241 const std::string& default_value) { | |
| 242 // If this fails, the pref service in use is a profile pref service, so the | |
| 243 // sync status must be provided (see profile pref registration calls below). | |
| 244 DCHECK(IsLocalStatePrefService(this)); | |
| 245 RegisterPreference(path, | |
| 246 Value::CreateStringValue(default_value), | |
| 247 UNSYNCABLE_PREF); | |
| 248 } | |
| 249 | |
| 250 void PrefService::RegisterFilePathPref(const char* path, | |
| 251 const FilePath& default_value) { | |
| 252 // If this fails, the pref service in use is a profile pref service, so the | |
| 253 // sync status must be provided (see profile pref registration calls below). | |
| 254 DCHECK(IsLocalStatePrefService(this)); | |
| 255 RegisterPreference(path, | |
| 256 Value::CreateStringValue(default_value.value()), | |
| 257 UNSYNCABLE_PREF); | |
| 258 } | |
| 259 | |
| 260 void PrefService::RegisterListPref(const char* path) { | |
| 261 // If this fails, the pref service in use is a profile pref service, so the | |
| 262 // sync status must be provided (see profile pref registration calls below). | |
| 263 DCHECK(IsLocalStatePrefService(this)); | |
| 264 RegisterPreference(path, | |
| 265 new ListValue(), | |
| 266 UNSYNCABLE_PREF); | |
| 267 } | |
| 268 | |
| 269 void PrefService::RegisterListPref(const char* path, ListValue* default_value) { | |
| 270 // If this fails, the pref service in use is a profile pref service, so the | |
| 271 // sync status must be provided (see profile pref registration calls below). | |
| 272 DCHECK(IsLocalStatePrefService(this)); | |
| 273 RegisterPreference(path, | |
| 274 default_value, | |
| 275 UNSYNCABLE_PREF); | |
| 276 } | |
| 277 | |
| 278 void PrefService::RegisterDictionaryPref(const char* path) { | |
| 279 // If this fails, the pref service in use is a profile pref service, so the | |
| 280 // sync status must be provided (see profile pref registration calls below). | |
| 281 DCHECK(IsLocalStatePrefService(this)); | |
| 282 RegisterPreference(path, | |
| 283 new DictionaryValue(), | |
| 284 UNSYNCABLE_PREF); | |
| 285 } | |
| 286 | |
| 287 void PrefService::RegisterDictionaryPref(const char* path, | |
| 288 DictionaryValue* default_value) { | |
| 289 // If this fails, the pref service in use is a profile pref service, so the | |
| 290 // sync status must be provided (see profile pref registration calls below). | |
| 291 DCHECK(IsLocalStatePrefService(this)); | |
| 292 RegisterPreference(path, | |
| 293 default_value, | |
| 294 UNSYNCABLE_PREF); | |
| 295 } | |
| 296 | |
| 297 void PrefService::RegisterLocalizedBooleanPref(const char* path, | |
| 298 int locale_default_message_id) { | |
| 299 // If this fails, the pref service in use is a profile pref service, so the | |
| 300 // sync status must be provided (see profile pref registration calls below). | |
| 301 DCHECK(IsLocalStatePrefService(this)); | |
| 302 RegisterPreference( | |
| 303 path, | |
| 304 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), | |
| 305 UNSYNCABLE_PREF); | |
| 306 } | |
| 307 | |
| 308 void PrefService::RegisterLocalizedIntegerPref(const char* path, | |
| 309 int locale_default_message_id) { | |
| 310 // If this fails, the pref service in use is a profile pref service, so the | |
| 311 // sync status must be provided (see profile pref registration calls below). | |
| 312 DCHECK(IsLocalStatePrefService(this)); | |
| 313 RegisterPreference( | |
| 314 path, | |
| 315 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), | |
| 316 UNSYNCABLE_PREF); | |
| 317 } | |
| 318 | |
| 319 void PrefService::RegisterLocalizedDoublePref(const char* path, | |
| 320 int locale_default_message_id) { | |
| 321 // If this fails, the pref service in use is a profile pref service, so the | |
| 322 // sync status must be provided (see profile pref registration calls below). | |
| 323 DCHECK(IsLocalStatePrefService(this)); | |
| 324 RegisterPreference( | |
| 325 path, | |
| 326 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), | |
| 327 UNSYNCABLE_PREF); | |
| 328 } | |
| 329 | |
| 330 void PrefService::RegisterLocalizedStringPref(const char* path, | |
| 331 int locale_default_message_id) { | |
| 332 // If this fails, the pref service in use is a profile pref service, so the | |
| 333 // sync status must be provided (see profile pref registration calls below). | |
| 334 DCHECK(IsLocalStatePrefService(this)); | |
| 335 RegisterPreference( | |
| 336 path, | |
| 337 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), | |
| 338 UNSYNCABLE_PREF); | |
| 339 } | |
| 340 | |
| 341 void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { | |
| 342 // If this fails, the pref service in use is a profile pref service, so the | |
| 343 // sync status must be provided (see profile pref registration calls below). | |
| 344 DCHECK(IsLocalStatePrefService(this)); | |
| 345 RegisterPreference( | |
| 346 path, | |
| 347 Value::CreateStringValue(base::Int64ToString(default_value)), | |
| 348 UNSYNCABLE_PREF); | |
| 349 } | |
| 350 | |
| 351 // Profile prefs (must use the sync_status variable). | |
| 352 void PrefService::RegisterBooleanPref(const char* path, | |
| 353 bool default_value, | |
| 354 PrefSyncStatus sync_status) { | |
| 355 DCHECK(IsProfilePrefService(this)); | |
| 356 RegisterPreference(path, | |
| 357 Value::CreateBooleanValue(default_value), | |
| 358 sync_status); | |
| 359 } | |
| 360 | |
| 361 void PrefService::RegisterIntegerPref(const char* path, | |
| 362 int default_value, | |
| 363 PrefSyncStatus sync_status) { | |
| 364 DCHECK(IsProfilePrefService(this)); | |
| 365 RegisterPreference(path, | |
| 366 Value::CreateIntegerValue(default_value), | |
| 367 sync_status); | |
| 368 } | |
| 369 | |
| 370 void PrefService::RegisterDoublePref(const char* path, | |
| 371 double default_value, | |
| 372 PrefSyncStatus sync_status) { | |
| 373 DCHECK(IsProfilePrefService(this)); | |
| 374 RegisterPreference(path, | |
| 375 Value::CreateDoubleValue(default_value), | |
| 376 sync_status); | |
| 377 } | |
| 378 | |
| 379 void PrefService::RegisterStringPref(const char* path, | |
| 380 const std::string& default_value, | |
| 381 PrefSyncStatus sync_status) { | |
| 382 DCHECK(IsProfilePrefService(this)); | |
| 383 RegisterPreference(path, | |
| 384 Value::CreateStringValue(default_value), | |
| 385 sync_status); | |
| 386 } | |
| 387 | |
| 388 void PrefService::RegisterFilePathPref(const char* path, | |
| 389 const FilePath& default_value, | |
| 390 PrefSyncStatus sync_status) { | |
| 391 DCHECK(IsProfilePrefService(this)); | |
| 392 RegisterPreference(path, | |
| 393 Value::CreateStringValue(default_value.value()), | |
| 394 sync_status); | |
| 395 } | |
| 396 | |
| 397 void PrefService::RegisterListPref(const char* path, | |
| 398 PrefSyncStatus sync_status) { | |
| 399 DCHECK(IsProfilePrefService(this)); | |
| 400 RegisterPreference(path, new ListValue(), sync_status); | |
| 401 } | |
| 402 | |
| 403 void PrefService::RegisterListPref(const char* path, | |
| 404 ListValue* default_value, | |
| 405 PrefSyncStatus sync_status) { | |
| 406 DCHECK(IsProfilePrefService(this)); | |
| 407 RegisterPreference(path, default_value, sync_status); | |
| 408 } | |
| 409 | |
| 410 void PrefService::RegisterDictionaryPref(const char* path, | |
| 411 PrefSyncStatus sync_status) { | |
| 412 DCHECK(IsProfilePrefService(this)); | |
| 413 RegisterPreference(path, new DictionaryValue(), sync_status); | |
| 414 } | |
| 415 | |
| 416 void PrefService::RegisterDictionaryPref(const char* path, | |
| 417 DictionaryValue* default_value, | |
| 418 PrefSyncStatus sync_status) { | |
| 419 DCHECK(IsProfilePrefService(this)); | |
| 420 RegisterPreference(path, default_value, sync_status); | |
| 421 } | |
| 422 | |
| 423 void PrefService::RegisterLocalizedBooleanPref(const char* path, | |
| 424 int locale_default_message_id, | |
| 425 PrefSyncStatus sync_status) { | |
| 426 DCHECK(IsProfilePrefService(this)); | |
| 427 RegisterPreference( | |
| 428 path, | |
| 429 CreateLocaleDefaultValue(Value::TYPE_BOOLEAN, locale_default_message_id), | |
| 430 sync_status); | |
| 431 } | |
| 432 | |
| 433 void PrefService::RegisterLocalizedIntegerPref(const char* path, | |
| 434 int locale_default_message_id, | |
| 435 PrefSyncStatus sync_status) { | |
| 436 DCHECK(IsProfilePrefService(this)); | |
| 437 RegisterPreference( | |
| 438 path, | |
| 439 CreateLocaleDefaultValue(Value::TYPE_INTEGER, locale_default_message_id), | |
| 440 sync_status); | |
| 441 } | |
| 442 | |
| 443 void PrefService::RegisterLocalizedDoublePref(const char* path, | |
| 444 int locale_default_message_id, | |
| 445 PrefSyncStatus sync_status) { | |
| 446 DCHECK(IsProfilePrefService(this)); | |
| 447 RegisterPreference( | |
| 448 path, | |
| 449 CreateLocaleDefaultValue(Value::TYPE_DOUBLE, locale_default_message_id), | |
| 450 sync_status); | |
| 451 } | |
| 452 | |
| 453 void PrefService::RegisterLocalizedStringPref(const char* path, | |
| 454 int locale_default_message_id, | |
| 455 PrefSyncStatus sync_status) { | |
| 456 DCHECK(IsProfilePrefService(this)); | |
| 457 RegisterPreference( | |
| 458 path, | |
| 459 CreateLocaleDefaultValue(Value::TYPE_STRING, locale_default_message_id), | |
| 460 sync_status); | |
| 461 } | |
| 462 | |
| 463 void PrefService::RegisterInt64Pref(const char* path, | |
| 464 int64 default_value, | |
| 465 PrefSyncStatus sync_status) { | |
| 466 DCHECK(IsProfilePrefService(this)); | |
| 467 RegisterPreference( | |
| 468 path, | |
| 469 Value::CreateStringValue(base::Int64ToString(default_value)), | |
| 470 sync_status); | |
| 471 } | |
| 472 | |
| 473 void PrefService::RegisterUint64Pref(const char* path, | |
| 474 uint64 default_value, | |
| 475 PrefSyncStatus sync_status) { | |
| 476 DCHECK(IsProfilePrefService(this)); | |
| 477 RegisterPreference( | |
| 478 path, | |
| 479 Value::CreateStringValue(base::Uint64ToString(default_value)), | |
| 480 sync_status); | |
| 481 } | |
| 482 | |
| 483 bool PrefService::GetBoolean(const char* path) const { | 92 bool PrefService::GetBoolean(const char* path) const { |
| 484 DCHECK(CalledOnValidThread()); | 93 DCHECK(CalledOnValidThread()); |
| 485 | 94 |
| 486 bool result = false; | 95 bool result = false; |
| 487 | 96 |
| 488 const base::Value* value = GetPreferenceValue(path); | 97 const base::Value* value = GetPreferenceValue(path); |
| 489 if (!value) { | 98 if (!value) { |
| 490 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 99 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 491 return result; | 100 return result; |
| 492 } | 101 } |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 | 294 |
| 686 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { | 295 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { |
| 687 pref_notifier_->RemovePrefObserver(path, obs); | 296 pref_notifier_->RemovePrefObserver(path, obs); |
| 688 } | 297 } |
| 689 | 298 |
| 690 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { | 299 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { |
| 691 pref_notifier_->AddInitObserver(obs); | 300 pref_notifier_->AddInitObserver(obs); |
| 692 } | 301 } |
| 693 | 302 |
| 694 void PrefService::RegisterPreference(const char* path, | 303 void PrefService::RegisterPreference(const char* path, |
| 695 Value* default_value, | 304 Value* default_value) { |
| 696 PrefSyncStatus sync_status) { | |
| 697 DCHECK(CalledOnValidThread()); | 305 DCHECK(CalledOnValidThread()); |
| 698 | 306 |
| 699 // The main code path takes ownership, but most don't. We'll be safe. | 307 // The main code path takes ownership, but most don't. We'll be safe. |
| 700 scoped_ptr<Value> scoped_value(default_value); | 308 scoped_ptr<Value> scoped_value(default_value); |
| 701 | 309 |
| 702 CHECK(!FindPreference(path)) << "Tried to register duplicate pref " << path; | 310 CHECK(!FindPreference(path)) << "Tried to register duplicate pref " << path; |
| 703 | 311 |
| 704 base::Value::Type orig_type = default_value->GetType(); | 312 base::Value::Type orig_type = default_value->GetType(); |
| 705 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 313 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 706 "invalid preference type: " << orig_type; | 314 "invalid preference type: " << orig_type; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 717 } else if (orig_type == base::Value::TYPE_DICTIONARY) { | 325 } else if (orig_type == base::Value::TYPE_DICTIONARY) { |
| 718 const base::DictionaryValue* dict = NULL; | 326 const base::DictionaryValue* dict = NULL; |
| 719 if (default_value->GetAsDictionary(&dict) && !dict->empty()) | 327 if (default_value->GetAsDictionary(&dict) && !dict->empty()) |
| 720 needs_empty_value = true; | 328 needs_empty_value = true; |
| 721 } | 329 } |
| 722 if (needs_empty_value) | 330 if (needs_empty_value) |
| 723 user_pref_store_->MarkNeedsEmptyValue(path); | 331 user_pref_store_->MarkNeedsEmptyValue(path); |
| 724 | 332 |
| 725 // Hand off ownership. | 333 // Hand off ownership. |
| 726 default_store_->SetDefaultValue(path, scoped_value.release()); | 334 default_store_->SetDefaultValue(path, scoped_value.release()); |
| 727 | |
| 728 // Register with sync if necessary. | |
| 729 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) | |
| 730 pref_sync_associator_->RegisterPref(path); | |
| 731 } | 335 } |
| 732 | 336 |
| 733 void PrefService::UnregisterPreference(const char* path) { | 337 void PrefService::UnregisterPreference(const char* path) { |
| 734 DCHECK(CalledOnValidThread()); | 338 DCHECK(CalledOnValidThread()); |
| 735 | 339 |
| 736 PreferenceMap::iterator it = prefs_map_.find(path); | 340 PreferenceMap::iterator it = prefs_map_.find(path); |
| 737 CHECK(it != prefs_map_.end()) << "Trying to unregister an unregistered pref: " | 341 CHECK(it != prefs_map_.end()) << "Trying to unregister an unregistered pref: " |
| 738 << path; | 342 << path; |
| 739 | 343 |
| 740 prefs_map_.erase(it); | 344 prefs_map_.erase(it); |
| 741 default_store_->RemoveDefaultValue(path); | 345 default_store_->RemoveDefaultValue(path); |
| 742 if (pref_sync_associator_.get() && | |
| 743 pref_sync_associator_->IsPrefRegistered(path)) { | |
| 744 pref_sync_associator_->UnregisterPref(path); | |
| 745 } | |
| 746 } | 346 } |
| 747 | 347 |
| 748 void PrefService::ClearPref(const char* path) { | 348 void PrefService::ClearPref(const char* path) { |
| 749 DCHECK(CalledOnValidThread()); | 349 DCHECK(CalledOnValidThread()); |
| 750 | 350 |
| 751 const Preference* pref = FindPreference(path); | 351 const Preference* pref = FindPreference(path); |
| 752 if (!pref) { | 352 if (!pref) { |
| 753 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 353 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
| 754 return; | 354 return; |
| 755 } | 355 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 870 if (pref->GetType() != new_value->GetType()) { | 470 if (pref->GetType() != new_value->GetType()) { |
| 871 NOTREACHED() << "Trying to set pref " << path | 471 NOTREACHED() << "Trying to set pref " << path |
| 872 << " of type " << pref->GetType() | 472 << " of type " << pref->GetType() |
| 873 << " to value of type " << new_value->GetType(); | 473 << " to value of type " << new_value->GetType(); |
| 874 return; | 474 return; |
| 875 } | 475 } |
| 876 | 476 |
| 877 user_pref_store_->SetValue(path, owned_value.release()); | 477 user_pref_store_->SetValue(path, owned_value.release()); |
| 878 } | 478 } |
| 879 | 479 |
| 880 syncer::SyncableService* PrefService::GetSyncableService() { | 480 void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) { |
| 881 return pref_sync_associator_.get(); | 481 pref_value_store_->UpdateCommandLinePrefStore(command_line_store); |
| 882 } | |
| 883 | |
| 884 void PrefService::UpdateCommandLinePrefStore(CommandLine* command_line) { | |
| 885 // If |pref_service_forked_| is true, then this PrefService and the forked | |
| 886 // copies will be out of sync. | |
| 887 DCHECK(!pref_service_forked_); | |
| 888 pref_value_store_->UpdateCommandLinePrefStore( | |
| 889 new CommandLinePrefStore(command_line)); | |
| 890 } | 482 } |
| 891 | 483 |
| 892 /////////////////////////////////////////////////////////////////////////////// | 484 /////////////////////////////////////////////////////////////////////////////// |
| 893 // PrefService::Preference | 485 // PrefService::Preference |
| 894 | 486 |
| 895 PrefService::Preference::Preference(const PrefService* service, | 487 PrefService::Preference::Preference(const PrefService* service, |
| 896 const char* name, | 488 const char* name, |
| 897 base::Value::Type type) | 489 base::Value::Type type) |
| 898 : name_(name), | 490 : name_(name), |
| 899 type_(type), | 491 type_(type), |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 975 const Value* found_value = NULL; | 567 const Value* found_value = NULL; |
| 976 if (pref_value_store_->GetValue(path, type, &found_value)) { | 568 if (pref_value_store_->GetValue(path, type, &found_value)) { |
| 977 DCHECK(found_value->IsType(type)); | 569 DCHECK(found_value->IsType(type)); |
| 978 return found_value; | 570 return found_value; |
| 979 } | 571 } |
| 980 | 572 |
| 981 // Every registered preference has at least a default value. | 573 // Every registered preference has at least a default value. |
| 982 NOTREACHED() << "no valid value found for registered pref " << path; | 574 NOTREACHED() << "no valid value found for registered pref " << path; |
| 983 return NULL; | 575 return NULL; |
| 984 } | 576 } |
| OLD | NEW |