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 624 625 626 627 628 629 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 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
|
// Copyright 2012 The Chromium Authors
// 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 <map>
#include <string>
#include <utility>
#include "base/check_op.h"
#include "base/debug/alias.h"
#include "base/debug/dump_without_crashing.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/json/values_util.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/metrics/histogram.h"
#include "base/notreached.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/task/sequenced_task_runner.h"
#include "base/values.h"
#include "build/chromeos_buildflags.h"
#include "components/prefs/default_pref_store.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_notifier_impl.h"
#include "components/prefs/pref_registry.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "components/prefs/value_map_pref_store.h"
#endif
#if BUILDFLAG(IS_ANDROID)
#include "components/prefs/android/pref_service_android.h"
#endif
namespace {
} // namespace
PrefService::PersistentPrefStoreLoadingObserver::
PersistentPrefStoreLoadingObserver(PrefService* pref_service)
: pref_service_(pref_service) {
DCHECK(pref_service_);
}
void PrefService::PersistentPrefStoreLoadingObserver::OnInitializationCompleted(
bool succeeded) {
pref_service_->CheckPrefsLoaded();
}
PrefService::PrefService(
std::unique_ptr<PrefNotifierImpl> pref_notifier,
std::unique_ptr<PrefValueStore> pref_value_store,
scoped_refptr<PersistentPrefStore> user_prefs,
scoped_refptr<PersistentPrefStore> standalone_browser_prefs,
scoped_refptr<PrefRegistry> pref_registry,
base::RepeatingCallback<void(PersistentPrefStore::PrefReadError)>
read_error_callback,
bool async)
: pref_notifier_(std::move(pref_notifier)),
pref_value_store_(std::move(pref_value_store)),
user_pref_store_(std::move(user_prefs)),
standalone_browser_pref_store_(std::move(standalone_browser_prefs)),
read_error_callback_(std::move(read_error_callback)),
pref_registry_(std::move(pref_registry)),
pref_store_observer_(
std::make_unique<PrefService::PersistentPrefStoreLoadingObserver>(
this)) {
pref_notifier_->SetPrefService(this);
DCHECK(pref_registry_);
DCHECK(pref_value_store_);
InitFromStorage(async);
}
PrefService::~PrefService() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Remove observers. This could be necessary if this service is destroyed
// before the prefs are fully loaded.
user_pref_store_->RemoveObserver(pref_store_observer_.get());
if (standalone_browser_pref_store_) {
standalone_browser_pref_store_->RemoveObserver(pref_store_observer_.get());
}
// TODO(crbug.com/942491, 946668, 945772) The following code collects
// augments stack dumps created by ~PrefNotifierImpl() with information
// whether the profile owning the PrefService is an incognito profile.
// Delete this, once the bugs are closed.
const bool is_incognito_profile = user_pref_store_->IsInMemoryPrefStore();
base::debug::Alias(&is_incognito_profile);
// Export value of is_incognito_profile to a string so that `grep`
// is a sufficient tool to analyze crashdumps.
char is_incognito_profile_string[32];
strncpy(is_incognito_profile_string,
is_incognito_profile ? "is_incognito: yes" : "is_incognito: no",
sizeof(is_incognito_profile_string));
base::debug::Alias(&is_incognito_profile_string);
}
void PrefService::InitFromStorage(bool async) {
if (!async) {
if (!user_pref_store_->IsInitializationComplete()) {
user_pref_store_->ReadPrefs();
}
if (standalone_browser_pref_store_ &&
!standalone_browser_pref_store_->IsInitializationComplete()) {
standalone_browser_pref_store_->ReadPrefs();
}
CheckPrefsLoaded();
return;
}
CheckPrefsLoaded();
if (!user_pref_store_->IsInitializationComplete()) {
user_pref_store_->AddObserver(pref_store_observer_.get());
user_pref_store_->ReadPrefsAsync(nullptr);
}
if (standalone_browser_pref_store_ &&
!standalone_browser_pref_store_->IsInitializationComplete()) {
standalone_browser_pref_store_->AddObserver(pref_store_observer_.get());
standalone_browser_pref_store_->ReadPrefsAsync(nullptr);
}
}
void PrefService::CheckPrefsLoaded() {
if (!(user_pref_store_->IsInitializationComplete() &&
(!standalone_browser_pref_store_ ||
standalone_browser_pref_store_->IsInitializationComplete()))) {
// Not done initializing both prefstores.
return;
}
user_pref_store_->RemoveObserver(pref_store_observer_.get());
if (standalone_browser_pref_store_) {
standalone_browser_pref_store_->RemoveObserver(pref_store_observer_.get());
}
// Both prefstores are initialized, get the read errors.
PersistentPrefStore::PrefReadError user_store_error =
user_pref_store_->GetReadError();
if (!standalone_browser_pref_store_) {
read_error_callback_.Run(user_store_error);
return;
}
PersistentPrefStore::PrefReadError standalone_browser_store_error =
standalone_browser_pref_store_->GetReadError();
// If both stores have the same error (or no error), run the callback with
// either one. This avoids double-reporting (either way prefs weren't
// successfully fully loaded)
if (user_store_error == standalone_browser_store_error) {
read_error_callback_.Run(user_store_error);
} else if (user_store_error == PersistentPrefStore::PREF_READ_ERROR_NONE ||
user_store_error == PersistentPrefStore::PREF_READ_ERROR_NO_FILE) {
// Prefer to report the standalone_browser_pref_store error if the
// user_pref_store error is not significant.
read_error_callback_.Run(standalone_browser_store_error);
} else {
// Either the user_pref_store error is significant, or
// both stores failed to load but for different reasons.
// The user_store error is more significant in essentially all cases,
// so prefer to report that.
read_error_callback_.Run(user_store_error);
}
}
void PrefService::CommitPendingWrite(
base::OnceClosure reply_callback,
base::OnceClosure synchronous_done_callback) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
user_pref_store_->CommitPendingWrite(std::move(reply_callback),
std::move(synchronous_done_callback));
}
void PrefService::SchedulePendingLossyWrites() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
user_pref_store_->SchedulePendingLossyWrites();
}
bool PrefService::GetBoolean(base::StringPiece path) const {
return GetValue(path).GetBool();
}
int PrefService::GetInteger(base::StringPiece path) const {
return GetValue(path).GetInt();
}
double PrefService::GetDouble(base::StringPiece path) const {
return GetValue(path).GetDouble();
}
const std::string& PrefService::GetString(base::StringPiece path) const {
return GetValue(path).GetString();
}
base::FilePath PrefService::GetFilePath(base::StringPiece path) const {
const base::Value& value = GetValue(path);
absl::optional<base::FilePath> result = base::ValueToFilePath(value);
DCHECK(result);
return *result;
}
bool PrefService::HasPrefPath(const std::string& path) const {
const Preference* pref = FindPreference(path);
return pref && !pref->IsDefaultValue();
}
void PrefService::IteratePreferenceValues(
base::RepeatingCallback<void(const std::string& key,
const base::Value& value)> callback) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (const auto& it : *pref_registry_)
callback.Run(it.first, *GetPreferenceValue(it.first));
}
base::Value::Dict PrefService::GetPreferenceValues(
IncludeDefaults include_defaults) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
base::Value::Dict out;
for (const auto& it : *pref_registry_) {
if (include_defaults == INCLUDE_DEFAULTS) {
out.SetByDottedPath(it.first, GetPreferenceValue(it.first)->Clone());
} else {
const Preference* pref = FindPreference(it.first);
if (pref->IsDefaultValue()) {
continue;
}
out.SetByDottedPath(it.first, pref->GetValue()->Clone());
}
}
return out;
}
std::vector<PrefService::PreferenceValueAndStore>
PrefService::GetPreferencesValueAndStore() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::vector<PreferenceValueAndStore> result;
for (const auto& it : *pref_registry_) {
auto* preference = FindPreference(it.first);
CHECK(preference);
PreferenceValueAndStore pref_data{
it.first, preference->GetValue()->Clone(),
pref_value_store_->ControllingPrefStoreForPref(it.first)};
result.emplace_back(std::move(pref_data));
}
return result;
}
const PrefService::Preference* PrefService::FindPreference(
const std::string& pref_name) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto it = prefs_map_.find(pref_name);
if (it != prefs_map_.end())
return &(it->second);
const base::Value* default_value = nullptr;
if (!pref_registry_->defaults()->GetValue(pref_name, &default_value))
return nullptr;
it = prefs_map_
.insert(std::make_pair(
pref_name, Preference(this, pref_name, default_value->type())))
.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;
}
}
PrefService::PrefInitializationStatus
PrefService::GetAllPrefStoresInitializationStatus() const {
if (!pref_value_store_->IsInitializationComplete())
return INITIALIZATION_STATUS_WAITING;
return GetInitializationStatus();
}
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::Value& PrefService::GetValue(base::StringPiece path) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return *GetPreferenceValueChecked(path);
}
const base::Value::Dict& PrefService::GetDict(base::StringPiece path) const {
const base::Value& value = GetValue(path);
return value.GetDict();
}
const base::Value::List& PrefService::GetList(base::StringPiece path) const {
const base::Value& value = GetValue(path);
return value.GetList();
}
const base::Value* PrefService::GetUserPrefValue(
const std::string& path) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const Preference* pref = FindPreference(path);
if (!pref) {
NOTREACHED() << "Trying to get an unregistered pref: " << path;
return nullptr;
}
// Look for an existing preference in the user store. If it doesn't
// exist, return NULL.
base::Value* value = nullptr;
if (!user_pref_store_->GetMutableValue(path, &value))
return nullptr;
if (value->type() != pref->GetType()) {
NOTREACHED() << "Pref value type doesn't match registered type.";
return nullptr;
}
return value;
}
void PrefService::SetDefaultPrefValue(const std::string& path,
base::Value value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_registry_->SetDefaultPrefValue(path, std::move(value));
}
const base::Value* PrefService::GetDefaultPrefValue(
const std::string& path) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// Lookup the preference in the default store.
const base::Value* value = nullptr;
bool has_value = pref_registry_->defaults()->GetValue(path, &value);
DCHECK(has_value) << "Default value missing for pref: " << path;
return 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::OnceCallback<void(bool)> obs) {
pref_notifier_->AddInitObserver(std::move(obs));
}
PrefRegistry* PrefService::DeprecatedGetPrefRegistry() {
return pref_registry_.get();
}
void PrefService::ClearPref(const std::string& path) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
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::ClearPrefsWithPrefixSilently(const std::string& prefix) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
user_pref_store_->RemoveValuesByPrefixSilently(prefix);
}
void PrefService::OnStoreDeletionFromDisk() {
user_pref_store_->OnStoreDeletionFromDisk();
}
void PrefService::AddPrefObserverAllPrefs(PrefObserver* obs) {
pref_notifier_->AddPrefObserverAllPrefs(obs);
}
void PrefService::RemovePrefObserverAllPrefs(PrefObserver* obs) {
pref_notifier_->RemovePrefObserverAllPrefs(obs);
}
#if BUILDFLAG(IS_ANDROID)
base::android::ScopedJavaLocalRef<jobject> PrefService::GetJavaObject() {
if (!pref_service_android_) {
pref_service_android_ = std::make_unique<PrefServiceAndroid>(this);
}
return pref_service_android_->GetJavaObject();
}
#endif
void PrefService::Set(const std::string& path, const base::Value& value) {
SetUserPrefValue(path, value.Clone());
}
void PrefService::SetBoolean(const std::string& path, bool value) {
SetUserPrefValue(path, base::Value(value));
}
void PrefService::SetInteger(const std::string& path, int value) {
SetUserPrefValue(path, base::Value(value));
}
void PrefService::SetDouble(const std::string& path, double value) {
SetUserPrefValue(path, base::Value(value));
}
void PrefService::SetString(const std::string& path, base::StringPiece value) {
SetUserPrefValue(path, base::Value(value));
}
void PrefService::SetDict(const std::string& path, base::Value::Dict dict) {
SetUserPrefValue(path, base::Value(std::move(dict)));
}
void PrefService::SetList(const std::string& path, base::Value::List list) {
SetUserPrefValue(path, base::Value(std::move(list)));
}
void PrefService::SetFilePath(const std::string& path,
const base::FilePath& value) {
SetUserPrefValue(path, base::FilePathToValue(value));
}
void PrefService::SetInt64(const std::string& path, int64_t value) {
SetUserPrefValue(path, base::Int64ToValue(value));
}
int64_t PrefService::GetInt64(const std::string& path) const {
const base::Value& value = GetValue(path);
absl::optional<int64_t> integer = base::ValueToInt64(value);
DCHECK(integer);
return integer.value_or(0);
}
void PrefService::SetUint64(const std::string& path, uint64_t value) {
SetUserPrefValue(path, base::Value(base::NumberToString(value)));
}
uint64_t PrefService::GetUint64(const std::string& path) const {
const base::Value& value = GetValue(path);
if (!value.is_string())
return 0;
uint64_t result;
base::StringToUint64(value.GetString(), &result);
return result;
}
void PrefService::SetTime(const std::string& path, base::Time value) {
SetUserPrefValue(path, base::TimeToValue(value));
}
base::Time PrefService::GetTime(const std::string& path) const {
const base::Value& value = GetValue(path);
absl::optional<base::Time> time = base::ValueToTime(value);
DCHECK(time);
return time.value_or(base::Time());
}
void PrefService::SetTimeDelta(const std::string& path, base::TimeDelta value) {
SetUserPrefValue(path, base::TimeDeltaToValue(value));
}
base::TimeDelta PrefService::GetTimeDelta(const std::string& path) const {
const base::Value& value = GetValue(path);
absl::optional<base::TimeDelta> time_delta = base::ValueToTimeDelta(value);
DCHECK(time_delta);
return time_delta.value_or(base::TimeDelta());
}
base::Value* PrefService::GetMutableUserPref(const std::string& path,
base::Value::Type type) {
CHECK(type == base::Value::Type::DICT || type == base::Value::Type::LIST);
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const Preference* pref = FindPreference(path);
if (!pref) {
NOTREACHED() << "Trying to get an unregistered pref: " << path;
return nullptr;
}
if (pref->GetType() != type) {
NOTREACHED() << "Wrong type for GetMutableValue: " << path;
return nullptr;
}
// Look for an existing preference in the user store. Return it in case it
// exists and has the correct type.
base::Value* value = nullptr;
if (user_pref_store_->GetMutableValue(path, &value) &&
value->type() == type) {
return value;
}
// If no user preference of the correct type exists, clone default value.
const base::Value* default_value = nullptr;
pref_registry_->defaults()->GetValue(path, &default_value);
DCHECK_EQ(default_value->type(), type);
user_pref_store_->SetValueSilently(path, default_value->Clone(),
GetWriteFlags(pref));
user_pref_store_->GetMutableValue(path, &value);
return value;
}
void PrefService::ReportUserPrefChanged(const std::string& key) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
user_pref_store_->ReportValueChanged(key, GetWriteFlags(FindPreference(key)));
}
void PrefService::ReportUserPrefChanged(
const std::string& key,
std::set<std::vector<std::string>> path_components) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
user_pref_store_->ReportSubValuesChanged(key, std::move(path_components),
GetWriteFlags(FindPreference(key)));
}
void PrefService::SetUserPrefValue(const std::string& path,
base::Value new_value) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const Preference* pref = FindPreference(path);
if (!pref) {
NOTREACHED() << "Trying to write an unregistered pref: " << path;
return;
}
if (pref->GetType() != new_value.type()) {
NOTREACHED() << "Trying to set pref " << path << " of type "
<< pref->GetType() << " to value of type " << new_value.type();
return;
}
user_pref_store_->SetValue(path, std::move(new_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,
std::string name,
base::Value::Type type)
: name_(std::move(name)),
type_(type),
// Cache the registration flags at creation time to avoid multiple map
// lookups later.
registration_flags_(service->pref_registry_->GetRegistrationFlags(name_)),
pref_service_(service) {}
const base::Value* PrefService::Preference::GetValue() const {
return pref_service_->GetPreferenceValueChecked(name_);
}
const base::Value* PrefService::Preference::GetRecommendedValue() const {
DCHECK(pref_service_->FindPreference(name_))
<< "Must register pref before getting its value";
const base::Value* found_value = nullptr;
if (pref_value_store()->GetRecommendedValue(name_, type_, &found_value)) {
DCHECK(found_value->type() == type_);
return found_value;
}
// The pref has no recommended value.
return nullptr;
}
bool PrefService::Preference::IsManaged() const {
return pref_value_store()->PrefValueInManagedStore(name_);
}
bool PrefService::Preference::IsManagedByCustodian() const {
return pref_value_store()->PrefValueInSupervisedStore(name_);
}
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_);
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
bool PrefService::Preference::IsStandaloneBrowserControlled() const {
return pref_value_store()->PrefValueFromStandaloneBrowserStore(name_);
}
bool PrefService::Preference::IsStandaloneBrowserModifiable() const {
return pref_value_store()->PrefValueStandaloneBrowserModifiable(name_);
}
#endif
const base::Value* PrefService::GetPreferenceValue(
base::StringPiece path) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const base::Value* default_value = nullptr;
if (!pref_registry_->defaults()->GetValue(path, &default_value))
return nullptr;
const base::Value* found_value = nullptr;
base::Value::Type default_type = default_value->type();
if (!pref_value_store_->GetValue(path, default_type, &found_value)) {
// Every registered preference has at least a default value.
NOTREACHED() << "no valid value found for registered pref " << path;
return default_value;
}
DCHECK_EQ(found_value->type(), default_type);
return found_value;
}
const base::Value* PrefService::GetPreferenceValueChecked(
base::StringPiece path) const {
const base::Value* value = GetPreferenceValue(path);
DCHECK(value) << "Trying to read an unregistered pref: " << path;
return value;
}
#if BUILDFLAG(IS_CHROMEOS_ASH)
void PrefService::SetStandaloneBrowserPref(const std::string& path,
const base::Value& value) {
if (!standalone_browser_pref_store_) {
LOG(WARNING) << "Failure to set value of " << path
<< " in standalone browser store";
return;
}
standalone_browser_pref_store_->SetValue(
path, value.Clone(), WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
void PrefService::RemoveStandaloneBrowserPref(const std::string& path) {
if (!standalone_browser_pref_store_) {
LOG(WARNING) << "Failure to remove value of " << path
<< " in standalone browser store";
return;
}
standalone_browser_pref_store_->RemoveValue(
path, WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
}
#endif
// static
uint32_t PrefService::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;
}
|