Skip to content

Commit

Permalink
Migrate from PrefService::Get to PrefService::GetValue
Browse files Browse the repository at this point in the history
We recently introduced PrefService::GetValue which yields a reference
to a constant base::Value.
Most call sites of PrefService::Get are straight forward to
migrate.
The changes in this CL were created by a script, see bug.

Bug: 1342019
Change-Id: Ib54a9e330657301bc6eae69af7e5a04a38f39e53
Tests: The existing tests
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3721627
Owners-Override: Gabriel Charette <gab@chromium.org>
Reviewed-by: Gabriel Charette <gab@chromium.org>
Commit-Queue: Roland Bock <rbock@google.com>
Cr-Commit-Position: refs/heads/main@{#1022215}
  • Loading branch information
Roland Bock authored and Chromium LUCI CQ committed Jul 8, 2022
1 parent 646181b commit 23e3476
Show file tree
Hide file tree
Showing 22 changed files with 83 additions and 109 deletions.
7 changes: 3 additions & 4 deletions ash/display/display_prefs_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,10 @@ class DisplayPrefsTest : public AshTestBase {
testing::Message()
<< "Expected to read kExternalDisplayMirrorInfo with list values="
<< base::JoinString(expected_display_id_strs, ","));
const base::Value* prefs =
local_state()->Get(prefs::kExternalDisplayMirrorInfo);
ASSERT_TRUE(prefs);
const base::Value& prefs =
local_state()->GetValue(prefs::kExternalDisplayMirrorInfo);
std::set<int64_t> read_ids;
for (const auto& value : prefs->GetListDeprecated()) {
for (const auto& value : prefs.GetListDeprecated()) {
int64_t id;
EXPECT_TRUE(base::StringToInt64(value.GetString(), &id));
read_ids.insert(id);
Expand Down
8 changes: 3 additions & 5 deletions ash/quick_pair/repository/fast_pair/pending_write_store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,10 @@ PendingWriteStore::GetPendingAdds() {
return list;
}

const base::Value* result = pref_service->Get(kFastPairPendingWritesPref);
if (!result) {
return list;
}
const base::Value& result =
pref_service->GetValue(kFastPairPendingWritesPref);

for (const auto item : result->DictItems()) {
for (const auto item : result.DictItems()) {
list.emplace_back(item.first, item.second.GetString());
}

Expand Down
5 changes: 3 additions & 2 deletions ash/services/device_sync/cryptauth_device_registry_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ void CryptAuthDeviceRegistryImpl::RegisterPrefs(PrefRegistrySimple* registry) {
CryptAuthDeviceRegistryImpl::CryptAuthDeviceRegistryImpl(
PrefService* pref_service)
: pref_service_(pref_service) {
const base::Value* dict = pref_service_->Get(prefs::kCryptAuthDeviceRegistry);
const base::Value& dict =
pref_service_->GetValue(prefs::kCryptAuthDeviceRegistry);

CryptAuthDeviceRegistry::InstanceIdToDeviceMap instance_id_to_device_map;
for (const auto id_device_pair : dict->DictItems()) {
for (const auto id_device_pair : dict.DictItems()) {
absl::optional<std::string> instance_id =
util::DecodeFromString(id_device_pair.first);
absl::optional<CryptAuthDevice> device =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,9 @@ absl::optional<CertProfile> CertProvisioningSchedulerImpl::GetOneCertProfile(
const CertProfileId& cert_profile_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

const base::Value* profile_list = pref_service_->Get(pref_name_);
if (!profile_list) {
LOG(WARNING) << "Preference is not found";
return {};
}
const base::Value& profile_list = pref_service_->GetValue(pref_name_);

for (const base::Value& cur_profile : profile_list->GetListDeprecated()) {
for (const base::Value& cur_profile : profile_list.GetListDeprecated()) {
const CertProfileId* id = cur_profile.FindStringKey(kCertProfileIdKey);
if (!id || (*id != cert_profile_id)) {
continue;
Expand All @@ -574,14 +570,10 @@ absl::optional<CertProfile> CertProvisioningSchedulerImpl::GetOneCertProfile(
std::vector<CertProfile> CertProvisioningSchedulerImpl::GetCertProfiles() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

const base::Value* profile_list = pref_service_->Get(pref_name_);
if (!profile_list) {
LOG(WARNING) << "Preference is not found";
return {};
}
const base::Value& profile_list = pref_service_->GetValue(pref_name_);

std::vector<CertProfile> result_profiles;
for (const base::Value& cur_profile : profile_list->GetListDeprecated()) {
for (const base::Value& cur_profile : profile_list.GetListDeprecated()) {
absl::optional<CertProfile> p = CertProfile::MakeFromValue(cur_profile);
if (!p) {
LOG(WARNING) << "Failed to parse certificate profile";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1612,9 +1612,8 @@ class PrefServiceObserver {
}

void OnPrefsChange() {
const base::Value* pref_value = service_->Get(pref_name_);
DCHECK(pref_value);
OnPrefValueUpdated(*pref_value);
const base::Value& pref_value = service_->GetValue(pref_name_);
OnPrefValueUpdated(pref_value);
}

// Allows to add expectations about preference changes and verify new values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ void SetUserConsentInfoListForProfile(

std::vector<UserConsentInfo> GetUserConsentInfoListForProfile(
Profile* profile) {
const base::Value* user_consent_info_dict_value =
profile->GetPrefs()->Get(prefs::kEduCoexistenceToSAcceptedVersion);
const base::Value& user_consent_info_dict_value =
profile->GetPrefs()->GetValue(prefs::kEduCoexistenceToSAcceptedVersion);

DCHECK(user_consent_info_dict_value);
DCHECK(user_consent_info_dict_value->is_dict());
DCHECK(user_consent_info_dict_value.is_dict());

std::vector<UserConsentInfo> info_list;

for (const auto entry : user_consent_info_dict_value->DictItems()) {
for (const auto entry : user_consent_info_dict_value.DictItems()) {
const std::string& gaia_id = entry.first;
const std::string& accepted_tos_version = entry.second.GetString();
info_list.push_back(UserConsentInfo(gaia_id, accepted_tos_version));
Expand All @@ -85,11 +84,11 @@ std::vector<UserConsentInfo> GetUserConsentInfoListForProfile(

std::string GetAcceptedToSVersion(Profile* profile,
const std::string& secondary_edu_gaia_id) {
const base::Value* accepted_values =
profile->GetPrefs()->Get(prefs::kEduCoexistenceToSAcceptedVersion);
const base::Value& accepted_values =
profile->GetPrefs()->GetValue(prefs::kEduCoexistenceToSAcceptedVersion);

const std::string* entry =
accepted_values->FindStringKey(secondary_edu_gaia_id);
accepted_values.FindStringKey(secondary_edu_gaia_id);
return entry ? *entry : std::string();
}

Expand Down
12 changes: 4 additions & 8 deletions chrome/browser/ash/crosapi/local_printer_ash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,11 @@ void LocalPrinterAsh::GetPrinterTypeDenyList(
return;
}

const base::Value* deny_list_from_prefs =
prefs->Get(prefs::kPrinterTypeDenyList);
if (!deny_list_from_prefs) {
std::move(callback).Run(deny_list);
return;
}
const base::Value& deny_list_from_prefs =
prefs->GetValue(prefs::kPrinterTypeDenyList);

deny_list.reserve(deny_list_from_prefs->GetList().size());
for (const base::Value& deny_list_value : deny_list_from_prefs->GetList()) {
deny_list.reserve(deny_list_from_prefs.GetList().size());
for (const base::Value& deny_list_value : deny_list_from_prefs.GetList()) {
const std::string& deny_list_str = deny_list_value.GetString();
printing::mojom::PrinterType printer_type;
if (deny_list_str == "extension")
Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/ash/crosapi/prefs_ash.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,12 @@ void PrefsAsh::GetExtensionPrefWithControl(
return;
}

const base::Value* value = state->pref_service->Get(state->path);
const base::Value& value = state->pref_service->GetValue(state->path);

if (!state->is_extension_controlled_pref) {
// Not extension controlled
std::move(callback).Run(
absl::optional<base::Value>(value->Clone()),
absl::optional<base::Value>(value.Clone()),
mojom::PrefControlState::kNotExtensionControlledPrefPath);
return;
}
Expand All @@ -171,7 +171,7 @@ void PrefsAsh::GetExtensionPrefWithControl(
// Lacros could control this.
pref_control_state = mojom::PrefControlState::kLacrosExtensionControllable;
}
std::move(callback).Run(absl::optional<base::Value>(value->Clone()),
std::move(callback).Run(absl::optional<base::Value>(value.Clone()),
pref_control_state);
}

Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/ash/policy/networking/euicc_status_uploader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ void EuiccStatusUploader::MaybeUploadStatus() {
return;
}

const base::Value* last_uploaded_pref =
local_state_->Get(kLastUploadedEuiccStatusPref);
const base::Value& last_uploaded_pref =
local_state_->GetValue(kLastUploadedEuiccStatusPref);
auto current_state = GetCurrentEuiccStatus();

// Force send the status if reset request was received.
Expand All @@ -269,7 +269,7 @@ void EuiccStatusUploader::MaybeUploadStatus() {

retry_timer_.reset();

if (!last_uploaded_pref || *last_uploaded_pref != current_state) {
if (last_uploaded_pref != current_state) {
UploadStatus(std::move(current_state));
}
}
Expand Down
18 changes: 6 additions & 12 deletions chrome/browser/ash/smb_client/smb_persisted_share_registry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,10 @@ void SmbPersistedShareRegistry::Delete(const SmbUrl& share_url) {

absl::optional<SmbShareInfo> SmbPersistedShareRegistry::Get(
const SmbUrl& share_url) const {
const base::Value* pref =
profile_->GetPrefs()->Get(prefs::kNetworkFileSharesSavedShares);
if (!pref) {
return {};
}
const base::Value& pref =
profile_->GetPrefs()->GetValue(prefs::kNetworkFileSharesSavedShares);

base::Value::ConstListView share_list = pref->GetListDeprecated();
base::Value::ConstListView share_list = pref.GetListDeprecated();
for (auto it = share_list.begin(); it != share_list.end(); ++it) {
if (GetStringValue(*it, kShareUrlKey) == share_url.ToString()) {
return DictToShare(*it);
Expand All @@ -144,14 +141,11 @@ absl::optional<SmbShareInfo> SmbPersistedShareRegistry::Get(
}

std::vector<SmbShareInfo> SmbPersistedShareRegistry::GetAll() const {
const base::Value* pref =
profile_->GetPrefs()->Get(prefs::kNetworkFileSharesSavedShares);
if (!pref) {
return {};
}
const base::Value& pref =
profile_->GetPrefs()->GetValue(prefs::kNetworkFileSharesSavedShares);

std::vector<SmbShareInfo> shares;
base::Value::ConstListView share_list = pref->GetListDeprecated();
base::Value::ConstListView share_list = pref.GetListDeprecated();
for (auto it = share_list.begin(); it != share_list.end(); ++it) {
absl::optional<SmbShareInfo> info = DictToShare(*it);
if (!info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@ ExtensionsPermissionsTracker::~ExtensionsPermissionsTracker() = default;
void ExtensionsPermissionsTracker::OnForcedExtensionsPrefChanged() {
// TODO(crbug.com/1015378): handle pref_names::kExtensionManagement with
// installation_mode: forced.
const base::Value* value = pref_service_->Get(pref_names::kInstallForceList);
if (!value || value->type() != base::Value::Type::DICTIONARY) {
const base::Value& value =
pref_service_->GetValue(pref_names::kInstallForceList);
if (value.type() != base::Value::Type::DICTIONARY) {
return;
}

extension_safety_ratings_.clear();
pending_forced_extensions_.clear();

for (const auto entry : value->DictItems()) {
for (const auto entry : value.DictItems()) {
const ExtensionId& extension_id = entry.first;
// By default the extension permissions are assumed to trigger full warning
// (false). When the extension is loaded, if all of its permissions is safe,
Expand Down
6 changes: 3 additions & 3 deletions chrome/browser/lacros/account_manager/account_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ const char kLacrosAccountIdsPref[] =

// Reads from `prefs` a list of account ids known by Lacros.
AccountCache::AccountIdSet GetLacrosAccountIdsPref(PrefService* prefs) {
const base::Value* list = prefs->Get(kLacrosAccountIdsPref);
if (!list->is_list())
const base::Value& list = prefs->GetValue(kLacrosAccountIdsPref);
if (!list.is_list())
return {};
AccountCache::AccountIdSet account_ids;
for (const base::Value& value : list->GetListDeprecated()) {
for (const base::Value& value : list.GetListDeprecated()) {
const std::string* gaia_id = value.GetIfString();
if (gaia_id)
account_ids.insert(*gaia_id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ class AccountProfileMapperTest : public testing::Test {
}

base::flat_set<std::string> GetLacrosAccountsFromLocalState() {
const base::Value* list = local_state()->Get(kLacrosAccountIdsPref);
EXPECT_TRUE(list->is_list());
const base::Value& list = local_state()->GetValue(kLacrosAccountIdsPref);
EXPECT_TRUE(list.is_list());
return base::MakeFlatSet<std::string>(
list->GetListDeprecated(), {},
list.GetListDeprecated(), {},
[](const base::Value& value) { return value.GetString(); });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,10 @@ void NearbyShareCertificateStorageImpl::GetPublicCertificates(

absl::optional<std::vector<NearbySharePrivateCertificate>>
NearbyShareCertificateStorageImpl::GetPrivateCertificates() const {
const base::Value* list =
pref_service_->Get(prefs::kNearbySharingPrivateCertificateListPrefName);
const base::Value& list = pref_service_->GetValue(
prefs::kNearbySharingPrivateCertificateListPrefName);
std::vector<NearbySharePrivateCertificate> certs;
for (const base::Value& cert_dict : list->GetListDeprecated()) {
for (const base::Value& cert_dict : list.GetListDeprecated()) {
absl::optional<NearbySharePrivateCertificate> cert(
NearbySharePrivateCertificate::FromDictionary(cert_dict));
if (!cert)
Expand Down
7 changes: 3 additions & 4 deletions chrome/browser/policy/test/proxy_policies_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ void VerifyProxyPrefs(PrefService* prefs,
absl::optional<bool> expected_proxy_pac_mandatory,
const std::string& expected_proxy_bypass_list,
const ProxyPrefs::ProxyMode& expected_proxy_mode) {
const base::Value* value = prefs->Get(proxy_config::prefs::kProxy);
ASSERT_TRUE(value);
ASSERT_TRUE(value->is_dict());
ProxyConfigDictionary dict(value->Clone());
const base::Value& value = prefs->GetValue(proxy_config::prefs::kProxy);
ASSERT_TRUE(value.is_dict());
ProxyConfigDictionary dict(value.Clone());
std::string s;
bool b;
if (expected_proxy_server.empty()) {
Expand Down
10 changes: 4 additions & 6 deletions chrome/browser/ui/webui/print_preview/print_preview_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -519,14 +519,12 @@ void PrintPreviewHandler::ReadPrinterTypeDenyListFromPrefs() {
if (!prefs->HasPrefPath(prefs::kPrinterTypeDenyList))
return;

const base::Value* deny_list_from_prefs =
prefs->Get(prefs::kPrinterTypeDenyList);
if (!deny_list_from_prefs)
return;
const base::Value& deny_list_from_prefs =
prefs->GetValue(prefs::kPrinterTypeDenyList);

std::vector<mojom::PrinterType> deny_list;
deny_list.reserve(deny_list_from_prefs->GetList().size());
for (const base::Value& deny_list_value : deny_list_from_prefs->GetList()) {
deny_list.reserve(deny_list_from_prefs.GetList().size());
for (const base::Value& deny_list_value : deny_list_from_prefs.GetList()) {
const std::string& deny_list_str = deny_list_value.GetString();
mojom::PrinterType printer_type;
if (deny_list_str == "extension")
Expand Down
16 changes: 8 additions & 8 deletions chrome/browser/web_applications/url_handler_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -872,12 +872,12 @@ bool IsHandlerForProfile(const base::Value& handler,

bool ProfileHasUrlHandlers(PrefService* local_state,
const base::FilePath& profile_path) {
const base::Value* const pref_value =
local_state->Get(prefs::kWebAppsUrlHandlerInfo);
if (!pref_value || !pref_value->is_dict())
const base::Value& pref_value =
local_state->GetValue(prefs::kWebAppsUrlHandlerInfo);
if (!pref_value.is_dict())
return false;

for (const auto origin_value : pref_value->DictItems()) {
for (const auto origin_value : pref_value.DictItems()) {
for (const auto& handler : origin_value.second.GetListDeprecated()) {
if (IsHandlerForProfile(handler, profile_path))
return true;
Expand All @@ -898,12 +898,12 @@ std::vector<UrlHandlerLaunchParams> FindMatchingUrlHandlers(
if (!url.is_valid())
return {};

const base::Value* const pref_value =
local_state->Get(prefs::kWebAppsUrlHandlerInfo);
if (!pref_value || !pref_value->is_dict())
const base::Value& pref_value =
local_state->GetValue(prefs::kWebAppsUrlHandlerInfo);
if (!pref_value.is_dict())
return {};

return FindMatches(*pref_value, url);
return FindMatches(pref_value, url);
}

void SaveOpenInApp(PrefService* local_state,
Expand Down
7 changes: 3 additions & 4 deletions chrome/browser/web_applications/url_handler_prefs_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ class UrlHandlerPrefsTest : public ::testing::Test {
}

void ExpectUrlHandlerPrefs(const std::string& expected_prefs) {
const base::Value* const stored_prefs =
LocalState()->Get(prefs::kWebAppsUrlHandlerInfo);
ASSERT_TRUE(stored_prefs);
const base::Value& stored_prefs =
LocalState()->GetValue(prefs::kWebAppsUrlHandlerInfo);
const base::Value expected_prefs_value =
base::test::ParseJson(expected_prefs);
EXPECT_EQ(*stored_prefs, expected_prefs_value);
EXPECT_EQ(stored_prefs, expected_prefs_value);
}

std::unique_ptr<WebApp> WebAppWithUrlHandlers(
Expand Down
6 changes: 3 additions & 3 deletions components/content_creation/notes/core/note_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ absl::optional<std::vector<NoteTemplateIds>> TryGetRandomOrder(
return absl::nullopt;
}

const base::Value* stored_value = prefs->Get(kTemplatesRandomOrder);
const base::Value& stored_value = prefs->GetValue(kTemplatesRandomOrder);

if (!stored_value || !stored_value->is_list()) {
if (!stored_value.is_list()) {
return absl::nullopt;
}

std::vector<NoteTemplateIds> template_ids;
for (const base::Value& current_value : stored_value->GetListDeprecated()) {
for (const base::Value& current_value : stored_value.GetListDeprecated()) {
absl::optional<int> maybe_int = current_value.GetIfInt();
if (!maybe_int) {
continue;
Expand Down
Loading

0 comments on commit 23e3476

Please sign in to comment.