Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | 10 #include "app/l10n_util.h" |
| 11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/file_util.h" | 13 #include "base/file_util.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
| 16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
| 17 #include "base/stl_util-inl.h" | 17 #include "base/stl_util-inl.h" |
| 18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
| 19 #include "base/string_util.h" | 19 #include "base/string_util.h" |
| 20 #include "base/sys_string_conversions.h" | 20 #include "base/sys_string_conversions.h" |
| 21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
| 22 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 23 #include "chrome/browser/browser_thread.h" | 23 #include "chrome/browser/browser_thread.h" |
| 24 #include "chrome/browser/extensions/extension_pref_store.h" | |
| 24 #include "chrome/browser/policy/configuration_policy_pref_store.h" | 25 #include "chrome/browser/policy/configuration_policy_pref_store.h" |
| 25 #include "chrome/browser/prefs/command_line_pref_store.h" | 26 #include "chrome/browser/prefs/command_line_pref_store.h" |
| 26 #include "chrome/browser/prefs/default_pref_store.h" | 27 #include "chrome/browser/prefs/default_pref_store.h" |
| 28 #include "chrome/browser/prefs/overlay_persistent_pref_store.h" | |
| 27 #include "chrome/browser/prefs/pref_notifier_impl.h" | 29 #include "chrome/browser/prefs/pref_notifier_impl.h" |
| 28 #include "chrome/browser/prefs/pref_value_store.h" | 30 #include "chrome/browser/prefs/pref_value_store.h" |
| 29 #include "chrome/common/json_pref_store.h" | 31 #include "chrome/common/json_pref_store.h" |
| 30 #include "chrome/common/notification_service.h" | 32 #include "chrome/common/notification_service.h" |
| 31 #include "grit/chromium_strings.h" | 33 #include "grit/chromium_strings.h" |
| 32 #include "grit/generated_resources.h" | 34 #include "grit/generated_resources.h" |
| 33 | 35 |
| 34 namespace { | 36 namespace { |
| 35 | 37 |
| 36 // A helper function for RegisterLocalized*Pref that creates a Value* based on | 38 // A helper function for RegisterLocalized*Pref that creates a Value* based on |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 107 ConfigurationPolicyPrefStore* device_management = | 109 ConfigurationPolicyPrefStore* device_management = |
| 108 ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( | 110 ConfigurationPolicyPrefStore::CreateDeviceManagementPolicyPrefStore( |
| 109 profile); | 111 profile); |
| 110 CommandLinePrefStore* command_line = | 112 CommandLinePrefStore* command_line = |
| 111 new CommandLinePrefStore(CommandLine::ForCurrentProcess()); | 113 new CommandLinePrefStore(CommandLine::ForCurrentProcess()); |
| 112 JsonPrefStore* user = new JsonPrefStore( | 114 JsonPrefStore* user = new JsonPrefStore( |
| 113 pref_filename, | 115 pref_filename, |
| 114 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); | 116 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::FILE)); |
| 115 ConfigurationPolicyPrefStore* recommended = | 117 ConfigurationPolicyPrefStore* recommended = |
| 116 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); | 118 ConfigurationPolicyPrefStore::CreateRecommendedPolicyPrefStore(); |
| 119 DefaultPrefStore* default_pref_store = new DefaultPrefStore(); | |
| 117 | 120 |
| 118 return new PrefService(managed, device_management, extension_prefs, | 121 return new PrefService(managed, device_management, extension_prefs, |
| 119 command_line, user, recommended); | 122 command_line, user, recommended, default_pref_store, |
| 123 true); | |
| 124 } | |
| 125 | |
| 126 PrefService* PrefService::CreateIncognitoPrefService( | |
| 127 PrefStore* incognito_extension_prefs) { | |
| 128 return new PrefService( | |
| 129 managed_platform_prefs_, | |
| 130 device_management_prefs_, | |
| 131 incognito_extension_prefs, | |
| 132 command_line_prefs_, | |
| 133 new OverlayPersistentPrefStore(user_pref_store_.get()), | |
| 134 recommended_prefs_, | |
| 135 default_store_, | |
| 136 false | |
| 137 ); | |
| 120 } | 138 } |
| 121 | 139 |
| 122 PrefService::PrefService(PrefStore* managed_platform_prefs, | 140 PrefService::PrefService(PrefStore* managed_platform_prefs, |
| 123 PrefStore* device_management_prefs, | 141 PrefStore* device_management_prefs, |
| 124 PrefStore* extension_prefs, | 142 PrefStore* extension_prefs, |
| 125 PrefStore* command_line_prefs, | 143 PrefStore* command_line_prefs, |
| 126 PersistentPrefStore* user_prefs, | 144 PersistentPrefStore* user_prefs, |
| 127 PrefStore* recommended_prefs) | 145 PrefStore* recommended_prefs, |
| 128 : user_pref_store_(user_prefs) { | 146 DefaultPrefStore* default_store, |
| 147 bool init_from_storage) | |
| 148 : managed_platform_prefs_(managed_platform_prefs), | |
| 149 device_management_prefs_(device_management_prefs), | |
| 150 extension_prefs_(extension_prefs), | |
| 151 command_line_prefs_(command_line_prefs), | |
| 152 recommended_prefs_(recommended_prefs), | |
| 153 user_pref_store_(user_prefs), | |
| 154 default_store_(default_store) { | |
| 129 pref_notifier_.reset(new PrefNotifierImpl(this)); | 155 pref_notifier_.reset(new PrefNotifierImpl(this)); |
| 130 default_store_ = new DefaultPrefStore(); | |
| 131 pref_value_store_ = | 156 pref_value_store_ = |
| 132 new PrefValueStore(managed_platform_prefs, | 157 new PrefValueStore(managed_platform_prefs_, |
| 133 device_management_prefs, | 158 device_management_prefs_, |
| 134 extension_prefs, | 159 extension_prefs_, |
| 135 command_line_prefs, | 160 command_line_prefs_, |
| 136 user_pref_store_, | 161 user_pref_store_, |
| 137 recommended_prefs, | 162 recommended_prefs_, |
| 138 default_store_, | 163 default_store_, |
| 139 pref_notifier_.get()); | 164 pref_notifier_.get()); |
| 140 InitFromStorage(); | 165 if (init_from_storage) |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
I believe I have commented on this before but don'
battre
2011/01/05 20:23:08
Done.
| |
| 166 InitFromStorage(); | |
| 141 } | 167 } |
| 142 | 168 |
| 143 PrefService::~PrefService() { | 169 PrefService::~PrefService() { |
| 144 DCHECK(CalledOnValidThread()); | 170 DCHECK(CalledOnValidThread()); |
| 145 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); | 171 STLDeleteContainerPointers(prefs_.begin(), prefs_.end()); |
| 146 prefs_.clear(); | 172 prefs_.clear(); |
| 147 } | 173 } |
| 148 | 174 |
| 149 void PrefService::InitFromStorage() { | 175 void PrefService::InitFromStorage() { |
| 150 const PersistentPrefStore::PrefReadError error = | 176 const PersistentPrefStore::PrefReadError error = |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 bool rv = pref->GetValue()->GetAsString(&result); | 344 bool rv = pref->GetValue()->GetAsString(&result); |
| 319 DCHECK(rv); | 345 DCHECK(rv); |
| 320 #if defined(OS_POSIX) | 346 #if defined(OS_POSIX) |
| 321 // We store filepaths as UTF8, so convert it back to the system type. | 347 // We store filepaths as UTF8, so convert it back to the system type. |
| 322 result = base::SysWideToNativeMB(UTF8ToWide(result)); | 348 result = base::SysWideToNativeMB(UTF8ToWide(result)); |
| 323 #endif | 349 #endif |
| 324 return FilePath(result); | 350 return FilePath(result); |
| 325 } | 351 } |
| 326 | 352 |
| 327 bool PrefService::HasPrefPath(const char* path) const { | 353 bool PrefService::HasPrefPath(const char* path) const { |
| 328 return pref_value_store_->HasPrefPath(path); | 354 const Preference* pref = FindPreference(path); |
| 355 return pref && !pref->IsDefaultValue(); | |
| 329 } | 356 } |
| 330 | 357 |
| 331 const PrefService::Preference* PrefService::FindPreference( | 358 const PrefService::Preference* PrefService::FindPreference( |
| 332 const char* pref_name) const { | 359 const char* pref_name) const { |
| 333 DCHECK(CalledOnValidThread()); | 360 DCHECK(CalledOnValidThread()); |
| 334 Preference p(this, pref_name); | 361 Preference p(this, pref_name, Value::TYPE_NULL); |
| 335 PreferenceSet::const_iterator it = prefs_.find(&p); | 362 PreferenceSet::const_iterator it = prefs_.find(&p); |
| 336 return it == prefs_.end() ? NULL : *it; | 363 if (it != prefs_.end()) |
| 364 return *it; | |
| 365 const Value::ValueType type = default_store_->GetType(pref_name); | |
| 366 if (type == Value::TYPE_NULL) | |
| 367 return NULL; | |
| 368 Preference* new_pref = new Preference(this, pref_name, type); | |
| 369 prefs_.insert(new_pref); | |
| 370 return new_pref; | |
| 337 } | 371 } |
| 338 | 372 |
| 339 bool PrefService::ReadOnly() const { | 373 bool PrefService::ReadOnly() const { |
| 340 return user_pref_store_->ReadOnly(); | 374 return user_pref_store_->ReadOnly(); |
| 341 } | 375 } |
| 342 | 376 |
| 343 PrefNotifier* PrefService::pref_notifier() const { | 377 PrefNotifier* PrefService::pref_notifier() const { |
| 344 return pref_notifier_.get(); | 378 return pref_notifier_.get(); |
| 345 } | 379 } |
| 346 | 380 |
| 347 bool PrefService::IsManagedPreference(const char* pref_name) const { | 381 bool PrefService::IsManagedPreference(const char* pref_name) const { |
| 348 const Preference* pref = FindPreference(pref_name); | 382 const Preference* pref = FindPreference(pref_name); |
| 349 if (pref && pref->IsManaged()) { | 383 return pref && pref->IsManaged(); |
| 350 return true; | |
| 351 } | |
| 352 return false; | |
| 353 } | 384 } |
| 354 | 385 |
| 355 const DictionaryValue* PrefService::GetDictionary(const char* path) const { | 386 const DictionaryValue* PrefService::GetDictionary(const char* path) const { |
| 356 DCHECK(CalledOnValidThread()); | 387 DCHECK(CalledOnValidThread()); |
| 357 | 388 |
| 358 const Preference* pref = FindPreference(path); | 389 const Preference* pref = FindPreference(path); |
| 359 if (!pref) { | 390 if (!pref) { |
| 360 NOTREACHED() << "Trying to read an unregistered pref: " << path; | 391 NOTREACHED() << "Trying to read an unregistered pref: " << path; |
| 361 return NULL; | 392 return NULL; |
| 362 } | 393 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 398 | 429 |
| 399 if (FindPreference(path)) { | 430 if (FindPreference(path)) { |
| 400 NOTREACHED() << "Tried to register duplicate pref " << path; | 431 NOTREACHED() << "Tried to register duplicate pref " << path; |
| 401 return; | 432 return; |
| 402 } | 433 } |
| 403 | 434 |
| 404 Value::ValueType orig_type = default_value->GetType(); | 435 Value::ValueType orig_type = default_value->GetType(); |
| 405 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << | 436 DCHECK(orig_type != Value::TYPE_NULL && orig_type != Value::TYPE_BINARY) << |
| 406 "invalid preference type: " << orig_type; | 437 "invalid preference type: " << orig_type; |
| 407 | 438 |
| 408 // We set the default value of dictionaries and lists to be null so it's | 439 // Hand off ownership. |
| 409 // easier for callers to check for empty dict/list prefs. The PrefValueStore | 440 default_store_->SetDefaultValue(path, scoped_value.release()); |
| 410 // accepts ownership of the value (null or default_value). | |
| 411 if (Value::TYPE_LIST == orig_type || Value::TYPE_DICTIONARY == orig_type) { | |
| 412 default_store_->SetDefaultValue(path, Value::CreateNullValue()); | |
| 413 } else { | |
| 414 // Hand off ownership. | |
| 415 default_store_->SetDefaultValue(path, scoped_value.release()); | |
| 416 } | |
| 417 | 441 |
| 418 pref_value_store_->RegisterPreferenceType(path, orig_type); | 442 prefs_.insert(new Preference(this, path, orig_type)); |
|
Mattias Nissler (ping if slow)
2011/01/05 12:08:07
If new do this, we don't need the lazy creation co
battre
2011/01/05 20:23:08
Done.
| |
| 419 prefs_.insert(new Preference(this, path)); | |
| 420 } | 443 } |
| 421 | 444 |
| 422 void PrefService::ClearPref(const char* path) { | 445 void PrefService::ClearPref(const char* path) { |
| 423 DCHECK(CalledOnValidThread()); | 446 DCHECK(CalledOnValidThread()); |
| 424 | 447 |
| 425 const Preference* pref = FindPreference(path); | 448 const Preference* pref = FindPreference(path); |
| 426 if (!pref) { | 449 if (!pref) { |
| 427 NOTREACHED() << "Trying to clear an unregistered pref: " << path; | 450 NOTREACHED() << "Trying to clear an unregistered pref: " << path; |
| 428 return; | 451 return; |
| 429 } | 452 } |
| 430 user_pref_store_->RemoveValue(path); | 453 user_pref_store_->RemoveValue(path); |
| 431 } | 454 } |
| 432 | 455 |
| 433 void PrefService::Set(const char* path, const Value& value) { | 456 void PrefService::Set(const char* path, const Value& value) { |
| 434 DCHECK(CalledOnValidThread()); | 457 DCHECK(CalledOnValidThread()); |
| 435 | 458 |
| 436 const Preference* pref = FindPreference(path); | 459 const Preference* pref = FindPreference(path); |
| 437 if (!pref) { | 460 if (!pref) { |
| 438 NOTREACHED() << "Trying to write an unregistered pref: " << path; | 461 NOTREACHED() << "Trying to write an unregistered pref: " << path; |
| 439 return; | 462 return; |
| 440 } | 463 } |
| 441 | 464 |
| 442 // Allow dictionary and list types to be set to null, which removes their | 465 if (pref->GetType() != value.GetType()) { |
| 443 // user values. | |
| 444 if (value.GetType() == Value::TYPE_NULL && | |
| 445 (pref->GetType() == Value::TYPE_DICTIONARY || | |
| 446 pref->GetType() == Value::TYPE_LIST)) { | |
| 447 user_pref_store_->RemoveValue(path); | |
| 448 } else if (pref->GetType() != value.GetType()) { | |
| 449 NOTREACHED() << "Trying to set pref " << path | 466 NOTREACHED() << "Trying to set pref " << path |
| 450 << " of type " << pref->GetType() | 467 << " of type " << pref->GetType() |
| 451 << " to value of type " << value.GetType(); | 468 << " to value of type " << value.GetType(); |
| 452 } else { | 469 } else { |
| 453 user_pref_store_->SetValue(path, value.DeepCopy()); | 470 user_pref_store_->SetValue(path, value.DeepCopy()); |
| 454 } | 471 } |
| 455 } | 472 } |
| 456 | 473 |
| 457 void PrefService::SetBoolean(const char* path, bool value) { | 474 void PrefService::SetBoolean(const char* path, bool value) { |
| 458 SetUserPrefValue(path, Value::CreateBooleanValue(value)); | 475 SetUserPrefValue(path, Value::CreateBooleanValue(value)); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 580 return; | 597 return; |
| 581 } | 598 } |
| 582 | 599 |
| 583 user_pref_store_->SetValue(path, new_value); | 600 user_pref_store_->SetValue(path, new_value); |
| 584 } | 601 } |
| 585 | 602 |
| 586 /////////////////////////////////////////////////////////////////////////////// | 603 /////////////////////////////////////////////////////////////////////////////// |
| 587 // PrefService::Preference | 604 // PrefService::Preference |
| 588 | 605 |
| 589 PrefService::Preference::Preference(const PrefService* service, | 606 PrefService::Preference::Preference(const PrefService* service, |
| 590 const char* name) | 607 const char* name, |
| 608 Value::ValueType type) | |
| 591 : name_(name), | 609 : name_(name), |
| 610 type_(type), | |
| 592 pref_service_(service) { | 611 pref_service_(service) { |
| 593 DCHECK(name); | 612 DCHECK(name); |
| 594 DCHECK(service); | 613 DCHECK(service); |
| 595 } | 614 } |
| 596 | 615 |
| 597 Value::ValueType PrefService::Preference::GetType() const { | 616 Value::ValueType PrefService::Preference::GetType() const { |
| 598 return pref_service_->pref_value_store_->GetRegisteredType(name_); | 617 return type_; |
| 599 } | 618 } |
| 600 | 619 |
| 601 const Value* PrefService::Preference::GetValue() const { | 620 const Value* PrefService::Preference::GetValue() const { |
| 602 DCHECK(pref_service_->FindPreference(name_.c_str())) << | 621 DCHECK(pref_service_->FindPreference(name_.c_str())) << |
| 603 "Must register pref before getting its value"; | 622 "Must register pref before getting its value"; |
| 604 | 623 |
| 605 Value* found_value = NULL; | 624 Value* found_value = NULL; |
| 606 if (pref_service_->pref_value_store_->GetValue(name_, &found_value)) | 625 if (pref_service_->pref_value_store_->GetValue(name_, type_, &found_value)) { |
| 626 DCHECK(found_value->IsType(type_)); | |
| 607 return found_value; | 627 return found_value; |
| 628 } | |
| 608 | 629 |
| 609 // Every registered preference has at least a default value. | 630 // Every registered preference has at least a default value. |
| 610 NOTREACHED() << "no valid value found for registered pref " << name_; | 631 NOTREACHED() << "no valid value found for registered pref " << name_; |
| 611 return NULL; | 632 return NULL; |
| 612 } | 633 } |
| 613 | 634 |
| 614 bool PrefService::Preference::IsManaged() const { | 635 bool PrefService::Preference::IsManaged() const { |
| 615 PrefValueStore* pref_value_store = pref_service_->pref_value_store_; | 636 PrefValueStore* pref_value_store = pref_service_->pref_value_store_; |
| 616 return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || | 637 return pref_value_store->PrefValueInManagedPlatformStore(name_.c_str()) || |
| 617 pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); | 638 pref_value_store->PrefValueInDeviceManagementStore(name_.c_str()); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 639 | 660 |
| 640 bool PrefService::Preference::IsDefaultValue() const { | 661 bool PrefService::Preference::IsDefaultValue() const { |
| 641 return pref_service_->pref_value_store_-> | 662 return pref_service_->pref_value_store_-> |
| 642 PrefValueFromDefaultStore(name_.c_str()); | 663 PrefValueFromDefaultStore(name_.c_str()); |
| 643 } | 664 } |
| 644 | 665 |
| 645 bool PrefService::Preference::IsUserModifiable() const { | 666 bool PrefService::Preference::IsUserModifiable() const { |
| 646 return pref_service_->pref_value_store_-> | 667 return pref_service_->pref_value_store_-> |
| 647 PrefValueUserModifiable(name_.c_str()); | 668 PrefValueUserModifiable(name_.c_str()); |
| 648 } | 669 } |
| OLD | NEW |