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