Commit fc0abb9b authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Change PrefService::GetPreferenceValues() to return base::Value.

Update callers and remove deprecated base::Value API usage along the
way.

Bug: 1012811
Change-Id: Ic2094c106538cf547cb69bfecdab976e6a095674
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2478683Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Reviewed-by: default avatarAlexei Svitkine <asvitkine@chromium.org>
Reviewed-by: default avatarRohit Rao <rohitrao@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#818674}
parent 80a372d9
......@@ -173,28 +173,28 @@ IN_PROC_BROWSER_TEST_F(PreferencesTest, MultiProfiles) {
// Check that changing prefs of the active user doesn't affect prefs of the
// inactive user.
std::unique_ptr<base::DictionaryValue> prefs_backup =
base::Value prefs_backup =
prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS);
SetPrefs(prefs2, false);
CheckSettingsCorrespondToPrefs(prefs2);
EXPECT_TRUE(prefs_backup->Equals(
prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS).get()));
EXPECT_EQ(prefs_backup,
prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS));
SetPrefs(prefs2, true);
CheckSettingsCorrespondToPrefs(prefs2);
EXPECT_TRUE(prefs_backup->Equals(
prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS).get()));
EXPECT_EQ(prefs_backup,
prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS));
// Check that changing prefs of the inactive user doesn't affect prefs of the
// active user.
prefs_backup = prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS);
SetPrefs(prefs1, true);
CheckSettingsCorrespondToPrefs(prefs2);
EXPECT_TRUE(prefs_backup->Equals(
prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS).get()));
EXPECT_EQ(prefs_backup,
prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS));
SetPrefs(prefs1, false);
CheckSettingsCorrespondToPrefs(prefs2);
EXPECT_TRUE(prefs_backup->Equals(
prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS).get()));
EXPECT_EQ(prefs_backup,
prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS));
// Check that changing non-owner prefs doesn't change corresponding local
// state prefs and vice versa.
......
......@@ -464,11 +464,12 @@ void ChromeInternalLogSource::PopulateLocalStateSettings(
SystemLogsResponse* response) {
// Extract the "settings" entry in the local state and serialize back to
// a string.
std::unique_ptr<base::DictionaryValue> local_state =
base::Value local_state =
g_browser_process->local_state()->GetPreferenceValues(
PrefService::EXCLUDE_DEFAULTS);
const base::DictionaryValue* local_state_settings = nullptr;
if (!local_state->GetDictionary(kSettingsKey, &local_state_settings)) {
const base::Value* local_state_settings =
local_state.FindDictKey(kSettingsKey);
if (!local_state_settings) {
VLOG(1) << "Failed to extract the settings entry from Local State.";
return;
}
......
......@@ -243,7 +243,7 @@ IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) {
// Verify that we have some Local State prefs.
IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) {
EXPECT_TRUE(g_browser_process->local_state()
->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS)
.get());
base::Value prefs = g_browser_process->local_state()->GetPreferenceValues(
PrefService::INCLUDE_DEFAULTS);
EXPECT_TRUE(prefs.is_dict());
}
......@@ -65,18 +65,18 @@ void LocalStateUIHandler::RegisterMessages() {
void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) {
AllowJavascript();
std::unique_ptr<base::DictionaryValue> local_state_values(
base::Value local_state_values =
g_browser_process->local_state()->GetPreferenceValues(
PrefService::EXCLUDE_DEFAULTS));
PrefService::EXCLUDE_DEFAULTS);
if (ENABLE_FILTERING) {
std::vector<std::string> allowlisted_prefixes = {
"variations", "user_experience_metrics", "uninstall_metrics"};
internal::FilterPrefs(allowlisted_prefixes, local_state_values.get());
internal::FilterPrefs(allowlisted_prefixes, local_state_values);
}
std::string json;
JSONStringValueSerializer serializer(&json);
serializer.set_pretty_print(true);
bool result = serializer.Serialize(*local_state_values);
bool result = serializer.Serialize(local_state_values);
if (!result)
json = "Error loading Local State file.";
......@@ -99,16 +99,14 @@ bool HasValidPrefix(const std::string& pref_name,
namespace internal {
void FilterPrefs(const std::vector<std::string>& valid_prefixes,
base::DictionaryValue* prefs) {
base::Value& prefs) {
std::vector<std::string> prefs_to_remove;
for (base::DictionaryValue::Iterator it(*prefs); !it.IsAtEnd();
it.Advance()) {
if (!HasValidPrefix(it.key(), valid_prefixes))
prefs_to_remove.push_back(it.key());
for (const auto& it : prefs.DictItems()) {
if (!HasValidPrefix(it.first, valid_prefixes))
prefs_to_remove.push_back(it.first);
}
for (const std::string& pref_to_remove : prefs_to_remove) {
std::unique_ptr<base::Value> removed_value;
bool successfully_removed = prefs->Remove(pref_to_remove, &removed_value);
bool successfully_removed = prefs.RemovePath(pref_to_remove);
DCHECK(successfully_removed);
}
}
......
......@@ -12,7 +12,7 @@
#include "content/public/browser/web_ui_controller.h"
namespace base {
class DictionaryValue;
class Value;
}
// Namespace for exposing the method for unit tests.
......@@ -21,7 +21,7 @@ namespace internal {
// Removes elements from |prefs| where the key does not match any of the
// prefixes in |valid_prefixes|.
void FilterPrefs(const std::vector<std::string>& valid_prefixes,
base::DictionaryValue* prefs);
base::Value& prefs);
} // namespace internal
......
......@@ -10,28 +10,25 @@
TEST(LocalStateUiTest, FilterPrefs) {
std::vector<std::string> prefixes = {"foo", "bar", "baz"};
std::vector<std::string> invalid_pref_keys = {"fo", "ar", "afoo"};
std::vector<std::string> valid_pref_keys = {"foo", "foom", "bar.stuff"};
std::vector<std::string> invalid_pref_paths = {"fo", "ar", "afoo"};
std::vector<std::string> valid_pref_paths = {"foo", "foom", "bar.stuff"};
std::vector<std::string> all_pref_keys = invalid_pref_keys;
all_pref_keys.insert(all_pref_keys.end(), valid_pref_keys.begin(),
valid_pref_keys.end());
std::vector<std::string> all_pref_paths = invalid_pref_paths;
all_pref_paths.insert(all_pref_paths.end(), valid_pref_paths.begin(),
valid_pref_paths.end());
base::DictionaryValue prefs;
for (const std::string& key : all_pref_keys) {
prefs.SetString(key, key + "_value");
}
base::Value prefs(base::Value::Type::DICTIONARY);
for (const std::string& path : all_pref_paths)
prefs.SetStringPath(path, path + "_value");
internal::FilterPrefs(prefixes, &prefs);
internal::FilterPrefs(prefixes, prefs);
for (const std::string& invalid_key : invalid_pref_keys) {
std::string value;
EXPECT_FALSE(prefs.GetString(invalid_key, &value));
}
for (const std::string& invalid_path : invalid_pref_paths)
EXPECT_FALSE(prefs.FindStringPath(invalid_path));
for (const std::string& valid_key : valid_pref_keys) {
std::string value;
EXPECT_TRUE(prefs.GetString(valid_key, &value));
EXPECT_EQ(valid_key + "_value", value);
for (const std::string& valid_path : valid_pref_paths) {
const std::string* result = prefs.FindStringPath(valid_path);
ASSERT_TRUE(result);
EXPECT_EQ(valid_path + "_value", *result);
}
}
......@@ -33,10 +33,9 @@ void PrefsInternalsSource::StartDataRequest(
content::URLDataSource::GotDataCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::string json;
std::unique_ptr<base::DictionaryValue> prefs =
base::Value prefs =
profile_->GetPrefs()->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS);
DCHECK(prefs);
CHECK(base::JSONWriter::WriteWithOptions(
*prefs, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json));
prefs, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json));
std::move(callback).Run(base::RefCountedString::TakeString(&json));
}
......@@ -214,18 +214,19 @@ void PrefService::IteratePreferenceValues(
callback.Run(it.first, *GetPreferenceValue(it.first));
}
std::unique_ptr<base::DictionaryValue> PrefService::GetPreferenceValues(
base::Value PrefService::GetPreferenceValues(
IncludeDefaults include_defaults) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
std::unique_ptr<base::DictionaryValue> out(new base::DictionaryValue);
base::Value out(base::Value::Type::DICTIONARY);
for (const auto& it : *pref_registry_) {
if (include_defaults == INCLUDE_DEFAULTS) {
out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy());
out.SetPath(it.first, GetPreferenceValue(it.first)->Clone());
} else {
const Preference* pref = FindPreference(it.first);
if (pref->IsDefaultValue())
continue;
out->Set(it.first, pref->GetValue()->CreateDeepCopy());
out.SetPath(it.first, pref->GetValue()->Clone());
}
}
return out;
......
......@@ -306,8 +306,7 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// If INCLUDE_DEFAULTS is requested, preferences set to their default values
// will be included. Otherwise, these will be omitted from the returned
// dictionary.
std::unique_ptr<base::DictionaryValue> GetPreferenceValues(
IncludeDefaults include_defaults) const;
base::Value GetPreferenceValues(IncludeDefaults include_defaults) const;
bool ReadOnly() const;
......
......@@ -45,12 +45,10 @@ class PrefsInternalsSource : public web::URLDataSourceIOS {
DCHECK_CURRENTLY_ON(web::WebThread::UI);
std::string json;
std::unique_ptr<base::DictionaryValue> prefs =
browser_state_->GetPrefs()->GetPreferenceValues(
PrefService::INCLUDE_DEFAULTS);
DCHECK(prefs);
base::Value prefs = browser_state_->GetPrefs()->GetPreferenceValues(
PrefService::INCLUDE_DEFAULTS);
CHECK(base::JSONWriter::WriteWithOptions(
*prefs, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json));
prefs, base::JSONWriter::OPTIONS_PRETTY_PRINT, &json));
std::move(callback).Run(base::RefCountedString::TakeString(&json));
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment