| 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" | 10 #include "base/command_line.h" |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 virtual void OnError(PersistentPrefStore::PrefReadError error) { | 80 virtual void OnError(PersistentPrefStore::PrefReadError error) { |
| 81 callback_.Run(error); | 81 callback_.Run(error); |
| 82 } | 82 } |
| 83 | 83 |
| 84 private: | 84 private: |
| 85 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; | 85 base::Callback<void(PersistentPrefStore::PrefReadError)> callback_; |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 } // namespace | 88 } // namespace |
| 89 | 89 |
| 90 PrefService* PrefService::CreateIncognitoPrefService( | 90 PrefService::PrefService() { |
| 91 PrefStore* incognito_extension_prefs) { | |
| 92 pref_service_forked_ = true; | |
| 93 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); | |
| 94 OverlayUserPrefStore* incognito_pref_store = | |
| 95 new OverlayUserPrefStore(user_pref_store_.get()); | |
| 96 PrefsTabHelper::InitIncognitoUserPrefStore(incognito_pref_store); | |
| 97 return new PrefService( | |
| 98 pref_notifier, | |
| 99 pref_value_store_->CloneAndSpecialize( | |
| 100 NULL, // managed | |
| 101 incognito_extension_prefs, | |
| 102 NULL, // command_line_prefs | |
| 103 incognito_pref_store, | |
| 104 NULL, // recommended | |
| 105 default_store_.get(), | |
| 106 NULL, // pref_sync_associator | |
| 107 pref_notifier), | |
| 108 incognito_pref_store, | |
| 109 default_store_.get(), | |
| 110 NULL, | |
| 111 get_localized_string_method_, | |
| 112 read_error_callback_, | |
| 113 false); | |
| 114 } | 91 } |
| 115 | 92 |
| 116 PrefService::PrefService( | 93 void PrefService::Initialize( |
| 117 PrefNotifierImpl* pref_notifier, | 94 PrefNotifierImpl* pref_notifier, |
| 118 PrefValueStore* pref_value_store, | 95 PrefValueStore* pref_value_store, |
| 119 PersistentPrefStore* user_prefs, | 96 PersistentPrefStore* user_prefs, |
| 120 DefaultPrefStore* default_store, | 97 DefaultPrefStore* default_store, |
| 121 PrefModelAssociator* pref_sync_associator, | |
| 122 base::Callback<std::string(int)> get_localized_string_method, | 98 base::Callback<std::string(int)> get_localized_string_method, |
| 123 base::Callback<void(PersistentPrefStore::PrefReadError)> | 99 base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 124 read_error_callback, | 100 read_error_callback, |
| 125 bool async) | 101 bool async) { |
| 126 : pref_notifier_(pref_notifier), | 102 pref_notifier_.reset(pref_notifier); |
| 127 pref_value_store_(pref_value_store), | 103 pref_value_store_.reset(pref_value_store); |
| 128 user_pref_store_(user_prefs), | 104 user_pref_store_ = user_prefs; |
| 129 default_store_(default_store), | 105 default_store_ = default_store; |
| 130 pref_sync_associator_(pref_sync_associator), | 106 get_localized_string_method_ = get_localized_string_method; |
| 131 get_localized_string_method_(get_localized_string_method), | 107 read_error_callback_ = read_error_callback; |
| 132 read_error_callback_(read_error_callback), | |
| 133 pref_service_forked_(false) { | |
| 134 pref_notifier_->SetPrefService(this); | 108 pref_notifier_->SetPrefService(this); |
| 135 if (pref_sync_associator_.get()) | |
| 136 pref_sync_associator_->SetPrefService(this); | |
| 137 InitFromStorage(async); | 109 InitFromStorage(async); |
| 138 } | 110 } |
| 139 | 111 |
| 140 PrefService::~PrefService() { | 112 PrefService::~PrefService() { |
| 141 DCHECK(CalledOnValidThread()); | 113 DCHECK(CalledOnValidThread()); |
| 142 | 114 |
| 143 // Reset pointers so accesses after destruction reliably crash. | 115 // Reset pointers so accesses after destruction reliably crash. |
| 144 pref_value_store_.reset(); | 116 pref_value_store_.reset(); |
| 145 user_pref_store_ = NULL; | 117 user_pref_store_ = NULL; |
| 146 default_store_ = NULL; | 118 default_store_ = NULL; |
| 147 pref_sync_associator_.reset(); | |
| 148 pref_notifier_.reset(); | 119 pref_notifier_.reset(); |
| 149 } | 120 } |
| 150 | 121 |
| 151 void PrefService::InitFromStorage(bool async) { | 122 void PrefService::InitFromStorage(bool async) { |
| 152 if (!async) { | 123 if (!async) { |
| 153 read_error_callback_.Run(user_pref_store_->ReadPrefs()); | 124 read_error_callback_.Run(user_pref_store_->ReadPrefs()); |
| 154 } else { | 125 } else { |
| 155 // Guarantee that initialization happens after this function returned. | 126 // Guarantee that initialization happens after this function returned. |
| 156 MessageLoop::current()->PostTask( | 127 MessageLoop::current()->PostTask( |
| 157 FROM_HERE, | 128 FROM_HERE, |
| 158 base::Bind(&PersistentPrefStore::ReadPrefsAsync, | 129 base::Bind(&PersistentPrefStore::ReadPrefsAsync, |
| 159 user_pref_store_.get(), | 130 user_pref_store_.get(), |
| 160 new ReadErrorHandler(read_error_callback_))); | 131 new ReadErrorHandler(read_error_callback_))); |
| 161 } | 132 } |
| 162 } | 133 } |
| 163 | 134 |
| 164 bool PrefService::ReloadPersistentPrefs() { | 135 bool PrefService::ReloadPersistentPrefs() { |
| 165 return user_pref_store_->ReadPrefs() == | 136 return user_pref_store_->ReadPrefs() == |
| 166 PersistentPrefStore::PREF_READ_ERROR_NONE; | 137 PersistentPrefStore::PREF_READ_ERROR_NONE; |
| 167 } | 138 } |
| 168 | 139 |
| 169 void PrefService::CommitPendingWrite() { | 140 void PrefService::CommitPendingWrite() { |
| 170 DCHECK(CalledOnValidThread()); | 141 DCHECK(CalledOnValidThread()); |
| 171 user_pref_store_->CommitPendingWrite(); | 142 user_pref_store_->CommitPendingWrite(); |
| 172 } | 143 } |
| 173 | 144 |
| 174 void PrefService::AddObserver(PrefServiceObserver* observer) { | |
| 175 observer_list_.AddObserver(observer); | |
| 176 } | |
| 177 | |
| 178 void PrefService::RemoveObserver(PrefServiceObserver* observer) { | |
| 179 observer_list_.RemoveObserver(observer); | |
| 180 } | |
| 181 | |
| 182 bool PrefService::IsSyncing() { | |
| 183 return pref_sync_associator_.get() && | |
| 184 pref_sync_associator_->models_associated(); | |
| 185 } | |
| 186 | |
| 187 void PrefService::OnIsSyncingChanged() { | |
| 188 FOR_EACH_OBSERVER(PrefServiceObserver, observer_list_, OnIsSyncingChanged()); | |
| 189 } | |
| 190 | |
| 191 namespace { | |
| 192 | |
| 193 // If there's no g_browser_process or no local state, return true (for testing). | |
| 194 bool IsLocalStatePrefService(PrefService* prefs) { | |
| 195 return (!g_browser_process || | |
| 196 !g_browser_process->local_state() || | |
| 197 g_browser_process->local_state() == prefs); | |
| 198 } | |
| 199 | |
| 200 // If there's no g_browser_process, return true (for testing). | |
| 201 bool IsProfilePrefService(PrefService* prefs) { | |
| 202 // TODO(zea): uncomment this once all preferences are only ever registered | |
| 203 // with either the local_state's pref service or the profile's pref service. | |
| 204 // return (!g_browser_process || g_browser_process->local_state() != prefs); | |
| 205 return true; | |
| 206 } | |
| 207 | |
| 208 } // namespace | |
| 209 | |
| 210 // Local State prefs. | |
| 211 void PrefService::RegisterBooleanPref(const char* path, | |
| 212 bool default_value) { | |
| 213 // If this fails, the pref service in use is a profile pref service, so the | |
| 214 // sync status must be provided (see profile pref registration calls below). | |
| 215 DCHECK(IsLocalStatePrefService(this)); | |
| 216 RegisterPreference(path, | |
| 217 Value::CreateBooleanValue(default_value), | |
| 218 UNSYNCABLE_PREF); | |
| 219 } | |
| 220 | |
| 221 void PrefService::RegisterIntegerPref(const char* path, int default_value) { | |
| 222 // If this fails, the pref service in use is a profile pref service, so the | |
| 223 // sync status must be provided (see profile pref registration calls below). | |
| 224 DCHECK(IsLocalStatePrefService(this)); | |
| 225 RegisterPreference(path, | |
| 226 Value::CreateIntegerValue(default_value), | |
| 227 UNSYNCABLE_PREF); | |
| 228 } | |
| 229 | |
| 230 void PrefService::RegisterDoublePref(const char* path, double default_value) { | |
| 231 // If this fails, the pref service in use is a profile pref service, so the | |
| 232 // sync status must be provided (see profile pref registration calls below). | |
| 233 DCHECK(IsLocalStatePrefService(this)); | |
| 234 RegisterPreference(path, | |
| 235 Value::CreateDoubleValue(default_value), | |
| 236 UNSYNCABLE_PREF); | |
| 237 } | |
| 238 | |
| 239 void PrefService::RegisterStringPref(const char* path, | |
| 240 const std::string& default_value) { | |
| 241 // If this fails, the pref service in use is a profile pref service, so the | |
| 242 // sync status must be provided (see profile pref registration calls below). | |
| 243 DCHECK(IsLocalStatePrefService(this)); | |
| 244 RegisterPreference(path, | |
| 245 Value::CreateStringValue(default_value), | |
| 246 UNSYNCABLE_PREF); | |
| 247 } | |
| 248 | |
| 249 void PrefService::RegisterFilePathPref(const char* path, | |
| 250 const FilePath& default_value) { | |
| 251 // If this fails, the pref service in use is a profile pref service, so the | |
| 252 // sync status must be provided (see profile pref registration calls below). | |
| 253 DCHECK(IsLocalStatePrefService(this)); | |
| 254 RegisterPreference(path, | |
| 255 Value::CreateStringValue(default_value.value()), | |
| 256 UNSYNCABLE_PREF); | |
| 257 } | |
| 258 | |
| 259 void PrefService::RegisterListPref(const char* path) { | |
| 260 // If this fails, the pref service in use is a profile pref service, so the | |
| 261 // sync status must be provided (see profile pref registration calls below). | |
| 262 DCHECK(IsLocalStatePrefService(this)); | |
| 263 RegisterPreference(path, | |
| 264 new ListValue(), | |
| 265 UNSYNCABLE_PREF); | |
| 266 } | |
| 267 | |
| 268 void PrefService::RegisterListPref(const char* path, ListValue* default_value) { | |
| 269 // If this fails, the pref service in use is a profile pref service, so the | |
| 270 // sync status must be provided (see profile pref registration calls below). | |
| 271 DCHECK(IsLocalStatePrefService(this)); | |
| 272 RegisterPreference(path, | |
| 273 default_value, | |
| 274 UNSYNCABLE_PREF); | |
| 275 } | |
| 276 | |
| 277 void PrefService::RegisterDictionaryPref(const char* path) { | |
| 278 // If this fails, the pref service in use is a profile pref service, so the | |
| 279 // sync status must be provided (see profile pref registration calls below). | |
| 280 DCHECK(IsLocalStatePrefService(this)); | |
| 281 RegisterPreference(path, | |
| 282 new DictionaryValue(), | |
| 283 UNSYNCABLE_PREF); | |
| 284 } | |
| 285 | |
| 286 void PrefService::RegisterDictionaryPref(const char* path, | |
| 287 DictionaryValue* default_value) { | |
| 288 // If this fails, the pref service in use is a profile pref service, so the | |
| 289 // sync status must be provided (see profile pref registration calls below). | |
| 290 DCHECK(IsLocalStatePrefService(this)); | |
| 291 RegisterPreference(path, | |
| 292 default_value, | |
| 293 UNSYNCABLE_PREF); | |
| 294 } | |
| 295 | |
| 296 void PrefService::RegisterLocalizedBooleanPref(const char* path, | |
| 297 int locale_default_message_id) { | |
| 298 // If this fails, the pref service in use is a profile pref service, so the | |
| 299 // sync status must be provided (see profile pref registration calls below). | |
| 300 DCHECK(IsLocalStatePrefService(this)); | |
| 301 RegisterPreference( | |
| 302 path, | |
| 303 CreateLocaleDefaultValue( | |
| 304 Value::TYPE_BOOLEAN, | |
| 305 get_localized_string_method_.Run(locale_default_message_id)), | |
| 306 UNSYNCABLE_PREF); | |
| 307 } | |
| 308 | |
| 309 void PrefService::RegisterLocalizedIntegerPref(const char* path, | |
| 310 int locale_default_message_id) { | |
| 311 // If this fails, the pref service in use is a profile pref service, so the | |
| 312 // sync status must be provided (see profile pref registration calls below). | |
| 313 DCHECK(IsLocalStatePrefService(this)); | |
| 314 RegisterPreference( | |
| 315 path, | |
| 316 CreateLocaleDefaultValue( | |
| 317 Value::TYPE_INTEGER, | |
| 318 get_localized_string_method_.Run(locale_default_message_id)), | |
| 319 UNSYNCABLE_PREF); | |
| 320 } | |
| 321 | |
| 322 void PrefService::RegisterLocalizedDoublePref(const char* path, | |
| 323 int locale_default_message_id) { | |
| 324 // If this fails, the pref service in use is a profile pref service, so the | |
| 325 // sync status must be provided (see profile pref registration calls below). | |
| 326 DCHECK(IsLocalStatePrefService(this)); | |
| 327 RegisterPreference( | |
| 328 path, | |
| 329 CreateLocaleDefaultValue( | |
| 330 Value::TYPE_DOUBLE, | |
| 331 get_localized_string_method_.Run(locale_default_message_id)), | |
| 332 UNSYNCABLE_PREF); | |
| 333 } | |
| 334 | |
| 335 void PrefService::RegisterLocalizedStringPref(const char* path, | |
| 336 int locale_default_message_id) { | |
| 337 // If this fails, the pref service in use is a profile pref service, so the | |
| 338 // sync status must be provided (see profile pref registration calls below). | |
| 339 DCHECK(IsLocalStatePrefService(this)); | |
| 340 RegisterPreference( | |
| 341 path, | |
| 342 CreateLocaleDefaultValue( | |
| 343 Value::TYPE_STRING, | |
| 344 get_localized_string_method_.Run(locale_default_message_id)), | |
| 345 UNSYNCABLE_PREF); | |
| 346 } | |
| 347 | |
| 348 void PrefService::RegisterInt64Pref(const char* path, int64 default_value) { | |
| 349 // If this fails, the pref service in use is a profile pref service, so the | |
| 350 // sync status must be provided (see profile pref registration calls below). | |
| 351 DCHECK(IsLocalStatePrefService(this)); | |
| 352 RegisterPreference( | |
| 353 path, | |
| 354 Value::CreateStringValue(base::Int64ToString(default_value)), | |
| 355 UNSYNCABLE_PREF); | |
| 356 } | |
| 357 | |
| 358 // Profile prefs (must use the sync_status variable). | |
| 359 void PrefService::RegisterBooleanPref(const char* path, | |
| 360 bool default_value, | |
| 361 PrefSyncStatus sync_status) { | |
| 362 DCHECK(IsProfilePrefService(this)); | |
| 363 RegisterPreference(path, | |
| 364 Value::CreateBooleanValue(default_value), | |
| 365 sync_status); | |
| 366 } | |
| 367 | |
| 368 void PrefService::RegisterIntegerPref(const char* path, | |
| 369 int default_value, | |
| 370 PrefSyncStatus sync_status) { | |
| 371 DCHECK(IsProfilePrefService(this)); | |
| 372 RegisterPreference(path, | |
| 373 Value::CreateIntegerValue(default_value), | |
| 374 sync_status); | |
| 375 } | |
| 376 | |
| 377 void PrefService::RegisterDoublePref(const char* path, | |
| 378 double default_value, | |
| 379 PrefSyncStatus sync_status) { | |
| 380 DCHECK(IsProfilePrefService(this)); | |
| 381 RegisterPreference(path, | |
| 382 Value::CreateDoubleValue(default_value), | |
| 383 sync_status); | |
| 384 } | |
| 385 | |
| 386 void PrefService::RegisterStringPref(const char* path, | |
| 387 const std::string& default_value, | |
| 388 PrefSyncStatus sync_status) { | |
| 389 DCHECK(IsProfilePrefService(this)); | |
| 390 RegisterPreference(path, | |
| 391 Value::CreateStringValue(default_value), | |
| 392 sync_status); | |
| 393 } | |
| 394 | |
| 395 void PrefService::RegisterFilePathPref(const char* path, | |
| 396 const FilePath& default_value, | |
| 397 PrefSyncStatus sync_status) { | |
| 398 DCHECK(IsProfilePrefService(this)); | |
| 399 RegisterPreference(path, | |
| 400 Value::CreateStringValue(default_value.value()), | |
| 401 sync_status); | |
| 402 } | |
| 403 | |
| 404 void PrefService::RegisterListPref(const char* path, | |
| 405 PrefSyncStatus sync_status) { | |
| 406 DCHECK(IsProfilePrefService(this)); | |
| 407 RegisterPreference(path, new ListValue(), sync_status); | |
| 408 } | |
| 409 | |
| 410 void PrefService::RegisterListPref(const char* path, | |
| 411 ListValue* default_value, | |
| 412 PrefSyncStatus sync_status) { | |
| 413 DCHECK(IsProfilePrefService(this)); | |
| 414 RegisterPreference(path, default_value, sync_status); | |
| 415 } | |
| 416 | |
| 417 void PrefService::RegisterDictionaryPref(const char* path, | |
| 418 PrefSyncStatus sync_status) { | |
| 419 DCHECK(IsProfilePrefService(this)); | |
| 420 RegisterPreference(path, new DictionaryValue(), sync_status); | |
| 421 } | |
| 422 | |
| 423 void PrefService::RegisterDictionaryPref(const char* path, | |
| 424 DictionaryValue* default_value, | |
| 425 PrefSyncStatus sync_status) { | |
| 426 DCHECK(IsProfilePrefService(this)); | |
| 427 RegisterPreference(path, default_value, sync_status); | |
| 428 } | |
| 429 | |
| 430 void PrefService::RegisterLocalizedBooleanPref(const char* path, | |
| 431 int locale_default_message_id, | |
| 432 PrefSyncStatus sync_status) { | |
| 433 DCHECK(IsProfilePrefService(this)); | |
| 434 RegisterPreference( | |
| 435 path, | |
| 436 CreateLocaleDefaultValue( | |
| 437 Value::TYPE_BOOLEAN, | |
| 438 get_localized_string_method_.Run(locale_default_message_id)), | |
| 439 sync_status); | |
| 440 } | |
| 441 | |
| 442 void PrefService::RegisterLocalizedIntegerPref(const char* path, | |
| 443 int locale_default_message_id, | |
| 444 PrefSyncStatus sync_status) { | |
| 445 DCHECK(IsProfilePrefService(this)); | |
| 446 RegisterPreference( | |
| 447 path, | |
| 448 CreateLocaleDefaultValue( | |
| 449 Value::TYPE_INTEGER, | |
| 450 get_localized_string_method_.Run(locale_default_message_id)), | |
| 451 sync_status); | |
| 452 } | |
| 453 | |
| 454 void PrefService::RegisterLocalizedDoublePref(const char* path, | |
| 455 int locale_default_message_id, | |
| 456 PrefSyncStatus sync_status) { | |
| 457 DCHECK(IsProfilePrefService(this)); | |
| 458 RegisterPreference( | |
| 459 path, | |
| 460 CreateLocaleDefaultValue( | |
| 461 Value::TYPE_DOUBLE, | |
| 462 get_localized_string_method_.Run(locale_default_message_id)), | |
| 463 sync_status); | |
| 464 } | |
| 465 | |
| 466 void PrefService::RegisterLocalizedStringPref(const char* path, | |
| 467 int locale_default_message_id, | |
| 468 PrefSyncStatus sync_status) { | |
| 469 DCHECK(IsProfilePrefService(this)); | |
| 470 RegisterPreference( | |
| 471 path, | |
| 472 CreateLocaleDefaultValue( | |
| 473 Value::TYPE_STRING, | |
| 474 get_localized_string_method_.Run(locale_default_message_id)), | |
| 475 sync_status); | |
| 476 } | |
| 477 | |
| 478 void PrefService::RegisterInt64Pref(const char* path, | |
| 479 int64 default_value, | |
| 480 PrefSyncStatus sync_status) { | |
| 481 DCHECK(IsProfilePrefService(this)); | |
| 482 RegisterPreference( | |
| 483 path, | |
| 484 Value::CreateStringValue(base::Int64ToString(default_value)), | |
| 485 sync_status); | |
| 486 } | |
| 487 | |
| 488 void PrefService::RegisterUint64Pref(const char* path, | |
| 489 uint64 default_value, | |
| 490 PrefSyncStatus sync_status) { | |
| 491 DCHECK(IsProfilePrefService(this)); | |
| 492 RegisterPreference( | |
| 493 path, | |
| 494 Value::CreateStringValue(base::Uint64ToString(default_value)), | |
| 495 sync_status); | |
| 496 } | |
| 497 | |
| 498 bool PrefService::GetBoolean(const char* path) const { | 145 bool PrefService::GetBoolean(const char* path) const { |
| 499 DCHECK(CalledOnValidThread()); | 146 DCHECK(CalledOnValidThread()); |
| 500 | 147 |
| 501 bool result = false; | 148 bool result = false; |
| 502 | 149 |
| 503 const base::Value* value = GetPreferenceValue(path); | 150 const base::Value* value = GetPreferenceValue(path); |
| 504 if (!value) { | 151 if (!value) { |
| 505 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 152 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 506 return result; | 153 return result; |
| 507 } | 154 } |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 | 347 |
| 701 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { | 348 void PrefService::RemovePrefObserver(const char* path, PrefObserver* obs) { |
| 702 pref_notifier_->RemovePrefObserver(path, obs); | 349 pref_notifier_->RemovePrefObserver(path, obs); |
| 703 } | 350 } |
| 704 | 351 |
| 705 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { | 352 void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) { |
| 706 pref_notifier_->AddInitObserver(obs); | 353 pref_notifier_->AddInitObserver(obs); |
| 707 } | 354 } |
| 708 | 355 |
| 709 void PrefService::RegisterPreference(const char* path, | 356 void PrefService::RegisterPreference(const char* path, |
| 710 Value* default_value, | 357 Value* default_value) { |
| 711 PrefSyncStatus sync_status) { | |
| 712 DCHECK(CalledOnValidThread()); | 358 DCHECK(CalledOnValidThread()); |
| 713 | 359 |
| 714 // The main code path takes ownership, but most don't. We'll be safe. | 360 // The main code path takes ownership, but most don't. We'll be safe. |
| 715 scoped_ptr<Value> scoped_value(default_value); | 361 scoped_ptr<Value> scoped_value(default_value); |
| 716 | 362 |
| 717 CHECK(!FindPreference(path)) << "Tried to register duplicate pref " << path; | 363 CHECK(!FindPreference(path)) << "Tried to register duplicate pref " << path; |
| 718 | 364 |
| 719 base::Value::Type orig_type = default_value->GetType(); | 365 base::Value::Type orig_type = default_value->GetType(); |
| 720 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 366 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 721 "invalid preference type: " << orig_type; | 367 "invalid preference type: " << orig_type; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 732 } else if (orig_type == base::Value::TYPE_DICTIONARY) { | 378 } else if (orig_type == base::Value::TYPE_DICTIONARY) { |
| 733 const base::DictionaryValue* dict = NULL; | 379 const base::DictionaryValue* dict = NULL; |
| 734 if (default_value->GetAsDictionary(&dict) && !dict->empty()) | 380 if (default_value->GetAsDictionary(&dict) && !dict->empty()) |
| 735 needs_empty_value = true; | 381 needs_empty_value = true; |
| 736 } | 382 } |
| 737 if (needs_empty_value) | 383 if (needs_empty_value) |
| 738 user_pref_store_->MarkNeedsEmptyValue(path); | 384 user_pref_store_->MarkNeedsEmptyValue(path); |
| 739 | 385 |
| 740 // Hand off ownership. | 386 // Hand off ownership. |
| 741 default_store_->SetDefaultValue(path, scoped_value.release()); | 387 default_store_->SetDefaultValue(path, scoped_value.release()); |
| 742 | |
| 743 // Register with sync if necessary. | |
| 744 if (sync_status == SYNCABLE_PREF && pref_sync_associator_.get()) | |
| 745 pref_sync_associator_->RegisterPref(path); | |
| 746 } | 388 } |
| 747 | 389 |
| 748 void PrefService::UnregisterPreference(const char* path) { | 390 void PrefService::UnregisterPreference(const char* path) { |
| 749 DCHECK(CalledOnValidThread()); | 391 DCHECK(CalledOnValidThread()); |
| 750 | 392 |
| 751 PreferenceMap::iterator it = prefs_map_.find(path); | 393 PreferenceMap::iterator it = prefs_map_.find(path); |
| 752 CHECK(it != prefs_map_.end()) << "Trying to unregister an unregistered pref: " | 394 CHECK(it != prefs_map_.end()) << "Trying to unregister an unregistered pref: " |
| 753 << path; | 395 << path; |
| 754 | 396 |
| 755 prefs_map_.erase(it); | 397 prefs_map_.erase(it); |
| 756 default_store_->RemoveDefaultValue(path); | 398 default_store_->RemoveDefaultValue(path); |
| 757 if (pref_sync_associator_.get() && | |
| 758 pref_sync_associator_->IsPrefRegistered(path)) { | |
| 759 pref_sync_associator_->UnregisterPref(path); | |
| 760 } | |
| 761 } | 399 } |
| 762 | 400 |
| 763 void PrefService::ClearPref(const char* path) { | 401 void PrefService::ClearPref(const char* path) { |
| 764 DCHECK(CalledOnValidThread()); | 402 DCHECK(CalledOnValidThread()); |
| 765 | 403 |
| 766 const Preference* pref = FindPreference(path); | 404 const Preference* pref = FindPreference(path); |
| 767 if (!pref) { | 405 if (!pref) { |
| 768 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 406 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
| 769 return; | 407 return; |
| 770 } | 408 } |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 885 if (pref->GetType() != new_value->GetType()) { | 523 if (pref->GetType() != new_value->GetType()) { |
| 886 NOTREACHED() << "Trying to set pref " << path | 524 NOTREACHED() << "Trying to set pref " << path |
| 887 << " of type " << pref->GetType() | 525 << " of type " << pref->GetType() |
| 888 << " to value of type " << new_value->GetType(); | 526 << " to value of type " << new_value->GetType(); |
| 889 return; | 527 return; |
| 890 } | 528 } |
| 891 | 529 |
| 892 user_pref_store_->SetValue(path, owned_value.release()); | 530 user_pref_store_->SetValue(path, owned_value.release()); |
| 893 } | 531 } |
| 894 | 532 |
| 895 syncer::SyncableService* PrefService::GetSyncableService() { | |
| 896 return pref_sync_associator_.get(); | |
| 897 } | |
| 898 | |
| 899 void PrefService::UpdateCommandLinePrefStore(CommandLine* command_line) { | 533 void PrefService::UpdateCommandLinePrefStore(CommandLine* command_line) { |
| 900 // If |pref_service_forked_| is true, then this PrefService and the forked | |
| 901 // copies will be out of sync. | |
| 902 DCHECK(!pref_service_forked_); | |
| 903 pref_value_store_->UpdateCommandLinePrefStore( | 534 pref_value_store_->UpdateCommandLinePrefStore( |
| 904 new CommandLinePrefStore(command_line)); | 535 new CommandLinePrefStore(command_line)); |
| 905 } | 536 } |
| 906 | 537 |
| 907 /////////////////////////////////////////////////////////////////////////////// | 538 /////////////////////////////////////////////////////////////////////////////// |
| 908 // PrefService::Preference | 539 // PrefService::Preference |
| 909 | 540 |
| 910 PrefService::Preference::Preference(const PrefService* service, | 541 PrefService::Preference::Preference(const PrefService* service, |
| 911 const char* name, | 542 const char* name, |
| 912 base::Value::Type type) | 543 base::Value::Type type) |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 990 const Value* found_value = NULL; | 621 const Value* found_value = NULL; |
| 991 if (pref_value_store_->GetValue(path, type, &found_value)) { | 622 if (pref_value_store_->GetValue(path, type, &found_value)) { |
| 992 DCHECK(found_value->IsType(type)); | 623 DCHECK(found_value->IsType(type)); |
| 993 return found_value; | 624 return found_value; |
| 994 } | 625 } |
| 995 | 626 |
| 996 // Every registered preference has at least a default value. | 627 // Every registered preference has at least a default value. |
| 997 NOTREACHED() << "no valid value found for registered pref " << path; | 628 NOTREACHED() << "no valid value found for registered pref " << path; |
| 998 return NULL; | 629 return NULL; |
| 999 } | 630 } |
| 631 |
| 632 |
| 633 |
| 634 |
| 635 |
| 636 |
| 637 |
| 638 |
| 639 |
| 640 |
| 641 |
| 642 |
| 643 |
| 644 |
| 645 |
| 646 |
| 647 |
| 648 |
| 649 |
| 650 |
| 651 |
| 652 |
| 653 |
| 654 |
| 655 |
| 656 |
| 657 void PrefServiceSimple::RegisterBooleanPref(const char* path, |
| 658 bool default_value) { |
| 659 RegisterPreference(path, Value::CreateBooleanValue(default_value)); |
| 660 } |
| 661 |
| 662 void PrefServiceSimple::RegisterIntegerPref(const char* path, |
| 663 int default_value) { |
| 664 RegisterPreference(path, Value::CreateIntegerValue(default_value)); |
| 665 } |
| 666 |
| 667 void PrefServiceSimple::RegisterDoublePref(const char* path, |
| 668 double default_value) { |
| 669 RegisterPreference(path, Value::CreateDoubleValue(default_value)); |
| 670 } |
| 671 |
| 672 void PrefServiceSimple::RegisterStringPref(const char* path, |
| 673 const std::string& default_value) { |
| 674 RegisterPreference(path, Value::CreateStringValue(default_value)); |
| 675 } |
| 676 |
| 677 void PrefServiceSimple::RegisterFilePathPref(const char* path, |
| 678 const FilePath& default_value) { |
| 679 RegisterPreference(path, Value::CreateStringValue(default_value.value())); |
| 680 } |
| 681 |
| 682 void PrefServiceSimple::RegisterListPref(const char* path) { |
| 683 RegisterPreference(path, new ListValue()); |
| 684 } |
| 685 |
| 686 void PrefServiceSimple::RegisterListPref(const char* path, |
| 687 ListValue* default_value) { |
| 688 RegisterPreference(path, default_value); |
| 689 } |
| 690 |
| 691 void PrefServiceSimple::RegisterDictionaryPref(const char* path) { |
| 692 RegisterPreference(path, new DictionaryValue()); |
| 693 } |
| 694 |
| 695 void PrefServiceSimple::RegisterDictionaryPref(const char* path, |
| 696 DictionaryValue* default_value) { |
| 697 RegisterPreference(path, default_value); |
| 698 } |
| 699 |
| 700 void PrefServiceSimple::RegisterLocalizedBooleanPref( |
| 701 const char* path, int locale_default_message_id) { |
| 702 RegisterPreference( |
| 703 path, |
| 704 CreateLocaleDefaultValue( |
| 705 Value::TYPE_BOOLEAN, |
| 706 get_localized_string_method_.Run(locale_default_message_id))); |
| 707 } |
| 708 |
| 709 void PrefServiceSimple::RegisterLocalizedIntegerPref( |
| 710 const char* path, int locale_default_message_id) { |
| 711 RegisterPreference( |
| 712 path, |
| 713 CreateLocaleDefaultValue( |
| 714 Value::TYPE_INTEGER, |
| 715 get_localized_string_method_.Run(locale_default_message_id))); |
| 716 } |
| 717 |
| 718 void PrefServiceSimple::RegisterLocalizedDoublePref( |
| 719 const char* path, int locale_default_message_id) { |
| 720 RegisterPreference( |
| 721 path, |
| 722 CreateLocaleDefaultValue( |
| 723 Value::TYPE_DOUBLE, |
| 724 get_localized_string_method_.Run(locale_default_message_id))); |
| 725 } |
| 726 |
| 727 void PrefServiceSimple::RegisterLocalizedStringPref( |
| 728 const char* path, int locale_default_message_id) { |
| 729 RegisterPreference( |
| 730 path, |
| 731 CreateLocaleDefaultValue( |
| 732 Value::TYPE_STRING, |
| 733 get_localized_string_method_.Run(locale_default_message_id))); |
| 734 } |
| 735 |
| 736 void PrefServiceSimple::RegisterInt64Pref(const char* path, |
| 737 int64 default_value) { |
| 738 RegisterPreference( |
| 739 path, Value::CreateStringValue(base::Int64ToString(default_value))); |
| 740 } |
| 741 |
| 742 |
| 743 |
| 744 |
| 745 |
| 746 |
| 747 |
| 748 |
| 749 |
| 750 |
| 751 |
| 752 |
| 753 |
| 754 |
| 755 |
| 756 |
| 757 |
| 758 |
| 759 PrefServiceSyncable::PrefServiceSyncable() { |
| 760 pref_sync_associator_.SetPrefService(this); |
| 761 } |
| 762 |
| 763 void PrefServiceSyncable::Initialize( |
| 764 PrefNotifierImpl* pref_notifier, |
| 765 PrefValueStore* pref_value_store, |
| 766 PersistentPrefStore* user_prefs, |
| 767 DefaultPrefStore* default_store, |
| 768 base::Callback<std::string(int)> get_localized_string_method, |
| 769 base::Callback<void(PersistentPrefStore::PrefReadError)> |
| 770 read_error_callback, |
| 771 bool async) { |
| 772 PrefService::Initialize(pref_notifier, |
| 773 pref_value_store, |
| 774 user_prefs, |
| 775 default_store, |
| 776 get_localized_string_method, |
| 777 read_error_callback, |
| 778 async); |
| 779 |
| 780 pref_value_store->set_sync_associator(&pref_sync_associator_); |
| 781 } |
| 782 |
| 783 PrefServiceSyncable::~PrefServiceSyncable() { |
| 784 } |
| 785 |
| 786 PrefServiceSyncable* PrefServiceSyncable::CreateIncognitoPrefService( |
| 787 PrefStore* incognito_extension_prefs) { |
| 788 pref_service_forked_ = true; |
| 789 PrefNotifierImpl* pref_notifier = new PrefNotifierImpl(); |
| 790 OverlayUserPrefStore* incognito_pref_store = |
| 791 new OverlayUserPrefStore(user_pref_store_.get()); |
| 792 PrefsTabHelper::InitIncognitoUserPrefStore(incognito_pref_store); |
| 793 PrefServiceSyncable* incognito_service = new PrefServiceSyncable(); |
| 794 incognito_service->Initialize( |
| 795 pref_notifier, |
| 796 pref_value_store_->CloneAndSpecialize( |
| 797 NULL, // managed |
| 798 incognito_extension_prefs, |
| 799 NULL, // command_line_prefs |
| 800 incognito_pref_store, |
| 801 NULL, // recommended |
| 802 default_store_.get(), |
| 803 pref_notifier), |
| 804 incognito_pref_store, |
| 805 default_store_.get(), |
| 806 get_localized_string_method_, |
| 807 read_error_callback_, |
| 808 false); |
| 809 return incognito_service; |
| 810 } |
| 811 |
| 812 bool PrefServiceSyncable::IsSyncing() { |
| 813 return pref_sync_associator_.models_associated(); |
| 814 } |
| 815 |
| 816 void PrefServiceSyncable::AddObserver(PrefServiceObserver* observer) { |
| 817 observer_list_.AddObserver(observer); |
| 818 } |
| 819 |
| 820 void PrefServiceSyncable::RemoveObserver(PrefServiceObserver* observer) { |
| 821 observer_list_.RemoveObserver(observer); |
| 822 } |
| 823 |
| 824 void PrefServiceSyncable::UnregisterPreference(const char* path) { |
| 825 PrefService::UnregisterPreference(path); |
| 826 if (pref_sync_associator_.IsPrefRegistered(path)) { |
| 827 pref_sync_associator_.UnregisterPref(path); |
| 828 } |
| 829 } |
| 830 |
| 831 void PrefServiceSyncable::RegisterBooleanPref(const char* path, |
| 832 bool default_value, |
| 833 PrefSyncStatus sync_status) { |
| 834 RegisterSyncablePreference(path, |
| 835 Value::CreateBooleanValue(default_value), |
| 836 sync_status); |
| 837 } |
| 838 |
| 839 void PrefServiceSyncable::RegisterIntegerPref(const char* path, |
| 840 int default_value, |
| 841 PrefSyncStatus sync_status) { |
| 842 RegisterSyncablePreference(path, |
| 843 Value::CreateIntegerValue(default_value), |
| 844 sync_status); |
| 845 } |
| 846 |
| 847 void PrefServiceSyncable::RegisterDoublePref(const char* path, |
| 848 double default_value, |
| 849 PrefSyncStatus sync_status) { |
| 850 RegisterSyncablePreference(path, |
| 851 Value::CreateDoubleValue(default_value), |
| 852 sync_status); |
| 853 } |
| 854 |
| 855 void PrefServiceSyncable::RegisterStringPref(const char* path, |
| 856 const std::string& default_value, |
| 857 PrefSyncStatus sync_status) { |
| 858 RegisterSyncablePreference(path, |
| 859 Value::CreateStringValue(default_value), |
| 860 sync_status); |
| 861 } |
| 862 |
| 863 void PrefServiceSyncable::RegisterFilePathPref(const char* path, |
| 864 const FilePath& default_value, |
| 865 PrefSyncStatus sync_status) { |
| 866 RegisterSyncablePreference(path, |
| 867 Value::CreateStringValue(default_value.value()), |
| 868 sync_status); |
| 869 } |
| 870 |
| 871 void PrefServiceSyncable::RegisterListPref(const char* path, |
| 872 PrefSyncStatus sync_status) { |
| 873 RegisterSyncablePreference(path, new ListValue(), sync_status); |
| 874 } |
| 875 |
| 876 void PrefServiceSyncable::RegisterListPref(const char* path, |
| 877 ListValue* default_value, |
| 878 PrefSyncStatus sync_status) { |
| 879 RegisterSyncablePreference(path, default_value, sync_status); |
| 880 } |
| 881 |
| 882 void PrefServiceSyncable::RegisterDictionaryPref(const char* path, |
| 883 PrefSyncStatus sync_status) { |
| 884 RegisterSyncablePreference(path, new DictionaryValue(), sync_status); |
| 885 } |
| 886 |
| 887 void PrefServiceSyncable::RegisterDictionaryPref(const char* path, |
| 888 DictionaryValue* default_value, |
| 889 PrefSyncStatus sync_status) { |
| 890 RegisterSyncablePreference(path, default_value, sync_status); |
| 891 } |
| 892 |
| 893 void PrefServiceSyncable::RegisterLocalizedBooleanPref( |
| 894 const char* path, |
| 895 int locale_default_message_id, |
| 896 PrefSyncStatus sync_status) { |
| 897 RegisterSyncablePreference( |
| 898 path, |
| 899 CreateLocaleDefaultValue( |
| 900 Value::TYPE_BOOLEAN, |
| 901 get_localized_string_method_.Run(locale_default_message_id)), |
| 902 sync_status); |
| 903 } |
| 904 |
| 905 void PrefServiceSyncable::RegisterLocalizedIntegerPref( |
| 906 const char* path, |
| 907 int locale_default_message_id, |
| 908 PrefSyncStatus sync_status) { |
| 909 RegisterSyncablePreference( |
| 910 path, |
| 911 CreateLocaleDefaultValue( |
| 912 Value::TYPE_INTEGER, |
| 913 get_localized_string_method_.Run(locale_default_message_id)), |
| 914 sync_status); |
| 915 } |
| 916 |
| 917 void PrefServiceSyncable::RegisterLocalizedDoublePref( |
| 918 const char* path, |
| 919 int locale_default_message_id, |
| 920 PrefSyncStatus sync_status) { |
| 921 RegisterSyncablePreference( |
| 922 path, |
| 923 CreateLocaleDefaultValue( |
| 924 Value::TYPE_DOUBLE, |
| 925 get_localized_string_method_.Run(locale_default_message_id)), |
| 926 sync_status); |
| 927 } |
| 928 |
| 929 void PrefServiceSyncable::RegisterLocalizedStringPref( |
| 930 const char* path, |
| 931 int locale_default_message_id, |
| 932 PrefSyncStatus sync_status) { |
| 933 RegisterSyncablePreference( |
| 934 path, |
| 935 CreateLocaleDefaultValue( |
| 936 Value::TYPE_STRING, |
| 937 get_localized_string_method_.Run(locale_default_message_id)), |
| 938 sync_status); |
| 939 } |
| 940 |
| 941 void PrefServiceSyncable::RegisterInt64Pref( |
| 942 const char* path, |
| 943 int64 default_value, |
| 944 PrefSyncStatus sync_status) { |
| 945 RegisterSyncablePreference( |
| 946 path, |
| 947 Value::CreateStringValue(base::Int64ToString(default_value)), |
| 948 sync_status); |
| 949 } |
| 950 |
| 951 void PrefServiceSyncable::RegisterUint64Pref( |
| 952 const char* path, |
| 953 uint64 default_value, |
| 954 PrefSyncStatus sync_status) { |
| 955 RegisterSyncablePreference( |
| 956 path, |
| 957 Value::CreateStringValue(base::Uint64ToString(default_value)), |
| 958 sync_status); |
| 959 } |
| 960 |
| 961 syncer::SyncableService* PrefServiceSyncable::GetSyncableService() { |
| 962 return &pref_sync_associator_; |
| 963 } |
| 964 |
| 965 void PrefServiceSyncable::UpdateCommandLinePrefStore( |
| 966 CommandLine* command_line) { |
| 967 // If |pref_service_forked_| is true, then this PrefService and the forked |
| 968 // copies will be out of sync. |
| 969 DCHECK(!pref_service_forked_); |
| 970 PrefService::UpdateCommandLinePrefStore(command_line); |
| 971 } |
| 972 |
| 973 void PrefServiceSyncable::OnIsSyncingChanged() { |
| 974 FOR_EACH_OBSERVER(PrefServiceObserver, observer_list_, OnIsSyncingChanged()); |
| 975 } |
| 976 |
| 977 void PrefServiceSyncable::RegisterSyncablePreference( |
| 978 const char* path, Value* default_value, PrefSyncStatus sync_status) { |
| 979 PrefService::RegisterPreference(path, default_value); |
| 980 // Register with sync if necessary. |
| 981 if (sync_status == SYNCABLE_PREF) |
| 982 pref_sync_associator_.RegisterPref(path); |
| 983 } |
| OLD | NEW |