1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/prefs/pref_service.h"
#include <algorithm>
#include <utility>
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/value_conversions.h"
#include "build/build_config.h"
#include "components/prefs/default_pref_store.h"
#include "components/prefs/pref_notifier_impl.h"
#include "components/prefs/pref_registry.h"
#include "components/prefs/pref_value_store.h"
namespace {
class ReadErrorHandler : public PersistentPrefStore::ReadErrorDelegate {
public:
ReadErrorHandler(base::Callback<void(PersistentPrefStore::PrefReadError)> cb)
: callback_(cb) {}
void OnError(PersistentPrefStore::PrefReadError error) override {
callback_.Run(error);
}
private:
base::Callback<void(PersistentPrefStore::PrefReadError)> callback_;
};
// Returns the WriteablePrefStore::PrefWriteFlags for the pref with the given
// |path|.
uint32_t GetWriteFlags(const PrefService::Preference* pref) {
uint32_t write_flags = WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS;
if (!pref)
return write_flags;
if (pref->registration_flags() & PrefRegistry::LOSSY_PREF)
write_flags |= WriteablePrefStore::LOSSY_PREF_WRITE_FLAG;
return write_flags;
}
} // namespace
PrefService::PrefService(
PrefNotifierImpl* pref_notifier,
PrefValueStore* pref_value_store,
PersistentPrefStore* user_prefs,
PrefRegistry* pref_registry,
base::Callback<void(PersistentPrefStore::PrefReadError)>
read_error_callback,
bool async)
: pref_notifier_(pref_notifier),
pref_value_store_(pref_value_store),
pref_registry_(pref_registry),
user_pref_store_(user_prefs),
read_error_callback_(read_error_callback) {
pref_notifier_->SetPrefService(this);
// TODO(battre): This is a check for crbug.com/435208 to make sure that
// access violations are caused by a use-after-free bug and not by an
// initialization bug.
CHECK(pref_registry_);
CHECK(pref_value_store_);
InitFromStorage(async);
}
PrefService::~PrefService() {
DCHECK(CalledOnValidThread());
// Reset pointers so accesses after destruction reliably crash.
pref_value_store_.reset();
pref_registry_ = NULL;
user_pref_store_ = NULL;
pref_notifier_.reset();
}
void PrefService::InitFromStorage(bool async) {
if (user_pref_store_->IsInitializationComplete()) {
read_error_callback_.Run(user_pref_store_->GetReadError());
} else if (!async) {
read_error_callback_.Run(user_pref_store_->ReadPrefs());
} else {
// Guarantee that initialization happens after this function returned.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(&PersistentPrefStore::ReadPrefsAsync, user_pref_store_,
new ReadErrorHandler(read_error_callback_)));
}
}
void PrefService::CommitPendingWrite() {
DCHECK(CalledOnValidThread());
user_pref_store_->CommitPendingWrite();
}
void PrefService::SchedulePendingLossyWrites() {
DCHECK(CalledOnValidThread());
user_pref_store_->SchedulePendingLossyWrites();
}
bool PrefService::GetBoolean(const std::string& path) const {
DCHECK(CalledOnValidThread());
bool result = false;
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return result;
}
bool rv = value->GetAsBoolean(&result);
DCHECK(rv);
return result;
}
int PrefService::GetInteger(const std::string& path) const {
DCHECK(CalledOnValidThread());
int result = 0;
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return result;
}
bool rv = value->GetAsInteger(&result);
DCHECK(rv);
return result;
}
double PrefService::GetDouble(const std::string& path) const {
DCHECK(CalledOnValidThread());
double result = 0.0;
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return result;
}
bool rv = value->GetAsDouble(&result);
DCHECK(rv);
return result;
}
std::string PrefService::GetString(const std::string& path) const {
DCHECK(CalledOnValidThread());
std::string result;
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return result;
}
bool rv = value->GetAsString(&result);
DCHECK(rv);
return result;
}
base::FilePath PrefService::GetFilePath(const std::string& path) const {
DCHECK(CalledOnValidThread());
base::FilePath result;
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return base::FilePath(result);
}
bool rv = base::GetValueAsFilePath(*value, &result);
DCHECK(rv);
return result;
}
bool PrefService::HasPrefPath(const std::string& path) const {
const Preference* pref = FindPreference(path);
return pref && !pref->IsDefaultValue();
}
std::unique_ptr<base::DictionaryValue> PrefService::GetPreferenceValues()
const {
DCHECK(CalledOnValidThread());
std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue);
for (const auto& it : *pref_registry_) {
out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy());
}
return out;
}
std::unique_ptr<base::DictionaryValue>
PrefService::GetPreferenceValuesOmitDefaults() const {
DCHECK(CalledOnValidThread());
std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue);
for (const auto& it : *pref_registry_) {
const Preference* pref = FindPreference(it.first);
if (pref->IsDefaultValue())
continue;
out->Set(it.first, pref->GetValue()->CreateDeepCopy());
}
return out;
}
std::unique_ptr<base::DictionaryValue>
PrefService::GetPreferenceValuesWithoutPathExpansion() const {
DCHECK(CalledOnValidThread());
std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue);
for (const auto& it : *pref_registry_) {
const base::Value* value = GetPreferenceValue(it.first);
DCHECK(value);
out->SetWithoutPathExpansion(it.first, value->CreateDeepCopy());
}
return out;
}
const PrefService::Preference* PrefService::FindPreference(
const std::string& pref_name) const {
DCHECK(CalledOnValidThread());
PreferenceMap::iterator it = prefs_map_.find(pref_name);
if (it != prefs_map_.end())
return &(it->second);
const base::Value* default_value = NULL;
if (!pref_registry_->defaults()->GetValue(pref_name, &default_value))
return NULL;
it = prefs_map_.insert(
std::make_pair(pref_name, Preference(
this, pref_name, default_value->GetType()))).first;
return &(it->second);
}
bool PrefService::ReadOnly() const {
return user_pref_store_->ReadOnly();
}
PrefService::PrefInitializationStatus PrefService::GetInitializationStatus()
const {
if (!user_pref_store_->IsInitializationComplete())
return INITIALIZATION_STATUS_WAITING;
switch (user_pref_store_->GetReadError()) {
case PersistentPrefStore::PREF_READ_ERROR_NONE:
return INITIALIZATION_STATUS_SUCCESS;
case PersistentPrefStore::PREF_READ_ERROR_NO_FILE:
return INITIALIZATION_STATUS_CREATED_NEW_PREF_STORE;
default:
return INITIALIZATION_STATUS_ERROR;
}
}
bool PrefService::IsManagedPreference(const std::string& pref_name) const {
const Preference* pref = FindPreference(pref_name);
return pref && pref->IsManaged();
}
bool PrefService::IsPreferenceManagedByCustodian(
const std::string& pref_name) const {
const Preference* pref = FindPreference(pref_name);
return pref && pref->IsManagedByCustodian();
}
bool PrefService::IsUserModifiablePreference(
const std::string& pref_name) const {
const Preference* pref = FindPreference(pref_name);
return pref && pref->IsUserModifiable();
}
const base::DictionaryValue* PrefService::GetDictionary(
const std::string& path) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return NULL;
}
if (value->GetType() != base::Value::Type::DICTIONARY) {
NOTREACHED();
return NULL;
}
return static_cast<const base::DictionaryValue*>(value);
}
const base::Value* PrefService::GetUserPrefValue(
const std::string& path) const {
DCHECK(CalledOnValidThread());
const Preference* pref = FindPreference(path);
if (!pref) {
NOTREACHED() << "Trying to get an unregistered pref: " << path;
return NULL;
}
// Look for an existing preference in the user store. If it doesn't
// exist, return NULL.
base::Value* value = NULL;
if (!user_pref_store_->GetMutableValue(path, &value))
return NULL;
if (!value->IsType(pref->GetType())) {
NOTREACHED() << "Pref value type doesn't match registered type.";
return NULL;
}
return value;
}
void PrefService::SetDefaultPrefValue(const std::string& path,
base::Value* value) {
DCHECK(CalledOnValidThread());
pref_registry_->SetDefaultPrefValue(path, value);
}
const base::Value* PrefService::GetDefaultPrefValue(
const std::string& path) const {
DCHECK(CalledOnValidThread());
// Lookup the preference in the default store.
const base::Value* value = NULL;
if (!pref_registry_->defaults()->GetValue(path, &value)) {
NOTREACHED() << "Default value missing for pref: " << path;
return NULL;
}
return value;
}
const base::ListValue* PrefService::GetList(const std::string& path) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return NULL;
}
if (value->GetType() != base::Value::Type::LIST) {
NOTREACHED();
return NULL;
}
return static_cast<const base::ListValue*>(value);
}
void PrefService::AddPrefObserver(const std::string& path, PrefObserver* obs) {
pref_notifier_->AddPrefObserver(path, obs);
}
void PrefService::RemovePrefObserver(const std::string& path,
PrefObserver* obs) {
pref_notifier_->RemovePrefObserver(path, obs);
}
void PrefService::AddPrefInitObserver(base::Callback<void(bool)> obs) {
pref_notifier_->AddInitObserver(obs);
}
PrefRegistry* PrefService::DeprecatedGetPrefRegistry() {
return pref_registry_.get();
}
void PrefService::ClearPref(const std::string& path) {
DCHECK(CalledOnValidThread());
const Preference* pref = FindPreference(path);
if (!pref) {
NOTREACHED() << "Trying to clear an unregistered pref: " << path;
return;
}
user_pref_store_->RemoveValue(path, GetWriteFlags(pref));
}
void PrefService::ClearMutableValues() {
user_pref_store_->ClearMutableValues();
}
void PrefService::Set(const std::string& path, const base::Value& value) {
SetUserPrefValue(path, value.DeepCopy());
}
void PrefService::SetBoolean(const std::string& path, bool value) {
SetUserPrefValue(path, new base::FundamentalValue(value));
}
void PrefService::SetInteger(const std::string& path, int value) {
SetUserPrefValue(path, new base::FundamentalValue(value));
}
void PrefService::SetDouble(const std::string& path, double value) {
SetUserPrefValue(path, new base::FundamentalValue(value));
}
void PrefService::SetString(const std::string& path, const std::string& value) {
SetUserPrefValue(path, new base::StringValue(value));
}
void PrefService::SetFilePath(const std::string& path,
const base::FilePath& value) {
SetUserPrefValue(path, base::CreateFilePathValue(value));
}
void PrefService::SetInt64(const std::string& path, int64_t value) {
SetUserPrefValue(path, new base::StringValue(base::Int64ToString(value)));
}
int64_t PrefService::GetInt64(const std::string& path) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return 0;
}
std::string result("0");
bool rv = value->GetAsString(&result);
DCHECK(rv);
int64_t val;
base::StringToInt64(result, &val);
return val;
}
void PrefService::SetUint64(const std::string& path, uint64_t value) {
SetUserPrefValue(path, new base::StringValue(base::Uint64ToString(value)));
}
uint64_t PrefService::GetUint64(const std::string& path) const {
DCHECK(CalledOnValidThread());
const base::Value* value = GetPreferenceValue(path);
if (!value) {
NOTREACHED() << "Trying to read an unregistered pref: " << path;
return 0;
}
std::string result("0");
bool rv = value->GetAsString(&result);
DCHECK(rv);
uint64_t val;
base::StringToUint64(result, &val);
return val;
}
base::Value* PrefService::GetMutableUserPref(const std::string& path,
base::Value::Type type) {
CHECK(type == base::Value::Type::DICTIONARY ||
type == base::Value::Type::LIST);
DCHECK(CalledOnValidThread());
const Preference* pref = FindPreference(path);
if (!pref) {
NOTREACHED() << "Trying to get an unregistered pref: " << path;
return NULL;
}
if (pref->GetType() != type) {
NOTREACHED() << "Wrong type for GetMutableValue: " << path;
return NULL;
}
// Look for an existing preference in the user store. If it doesn't
// exist or isn't the correct type, create a new user preference.
base::Value* value = NULL;
if (!user_pref_store_->GetMutableValue(path, &value) ||
!value->IsType(type)) {
if (type == base::Value::Type::DICTIONARY) {
value = new base::DictionaryValue;
} else if (type == base::Value::Type::LIST) {
value = new base::ListValue;
} else {
NOTREACHED();
}
user_pref_store_->SetValueSilently(path, base::WrapUnique(value),
GetWriteFlags(pref));
}
return value;
}
void PrefService::ReportUserPrefChanged(const std::string& key) {
DCHECK(CalledOnValidThread());
user_pref_store_->ReportValueChanged(key, GetWriteFlags(FindPreference(key)));
}
void PrefService::SetUserPrefValue(const std::string& path,
base::Value* new_value) {
std::unique_ptr<base::Value> owned_value(new_value);
DCHECK(CalledOnValidThread());
const Preference* pref = FindPreference(path);
if (!pref) {
NOTREACHED() << "Trying to write an unregistered pref: " << path;
return;
}
if (pref->GetType() != new_value->GetType()) {
NOTREACHED() << "Trying to set pref " << path
<< " of type " << pref->GetType()
<< " to value of type " << new_value->GetType();
return;
}
user_pref_store_->SetValue(path, std::move(owned_value), GetWriteFlags(pref));
}
void PrefService::UpdateCommandLinePrefStore(PrefStore* command_line_store) {
pref_value_store_->UpdateCommandLinePrefStore(command_line_store);
}
///////////////////////////////////////////////////////////////////////////////
// PrefService::Preference
PrefService::Preference::Preference(const PrefService* service,
const std::string& name,
base::Value::Type type)
: name_(name), type_(type), pref_service_(service) {
DCHECK(service);
// Cache the registration flags at creation time to avoid multiple map lookups
// later.
registration_flags_ = service->pref_registry_->GetRegistrationFlags(name_);
}
const std::string PrefService::Preference::name() const {
return name_;
}
base::Value::Type PrefService::Preference::GetType() const {
return type_;
}
const base::Value* PrefService::Preference::GetValue() const {
const base::Value* result= pref_service_->GetPreferenceValue(name_);
DCHECK(result) << "Must register pref before getting its value";
return result;
}
const base::Value* PrefService::Preference::GetRecommendedValue() const {
DCHECK(pref_service_->FindPreference(name_))
<< "Must register pref before getting its value";
const base::Value* found_value = NULL;
if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) {
DCHECK(found_value->IsType(type_));
return found_value;
}
// The pref has no recommended value.
return NULL;
}
bool PrefService::Preference::IsManaged() const {
return pref_value_store()->PrefValueInManagedStore(name_);
}
bool PrefService::Preference::IsManagedByCustodian() const {
return pref_value_store()->PrefValueInSupervisedStore(name_.c_str());
}
bool PrefService::Preference::IsRecommended() const {
return pref_value_store()->PrefValueFromRecommendedStore(name_);
}
bool PrefService::Preference::HasExtensionSetting() const {
return pref_value_store()->PrefValueInExtensionStore(name_);
}
bool PrefService::Preference::HasUserSetting() const {
return pref_value_store()->PrefValueInUserStore(name_);
}
bool PrefService::Preference::IsExtensionControlled() const {
return pref_value_store()->PrefValueFromExtensionStore(name_);
}
bool PrefService::Preference::IsUserControlled() const {
return pref_value_store()->PrefValueFromUserStore(name_);
}
bool PrefService::Preference::IsDefaultValue() const {
return pref_value_store()->PrefValueFromDefaultStore(name_);
}
bool PrefService::Preference::IsUserModifiable() const {
return pref_value_store()->PrefValueUserModifiable(name_);
}
bool PrefService::Preference::IsExtensionModifiable() const {
return pref_value_store()->PrefValueExtensionModifiable(name_);
}
const base::Value* PrefService::GetPreferenceValue(
const std::string& path) const {
DCHECK(CalledOnValidThread());
// TODO(battre): This is a check for crbug.com/435208. After analyzing some
// crash dumps it looks like the PrefService is accessed even though it has
// been cleared already.
CHECK(pref_registry_);
CHECK(pref_registry_->defaults());
CHECK(pref_value_store_);
const base::Value* default_value = NULL;
if (pref_registry_->defaults()->GetValue(path, &default_value)) {
const base::Value* found_value = NULL;
base::Value::Type default_type = default_value->GetType();
if (pref_value_store_->GetValue(path, default_type, &found_value)) {
DCHECK(found_value->IsType(default_type));
return found_value;
} else {
// Every registered preference has at least a default value.
NOTREACHED() << "no valid value found for registered pref " << path;
}
}
return NULL;
}
|