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) { ...@@ -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 // Check that changing prefs of the active user doesn't affect prefs of the
// inactive user. // inactive user.
std::unique_ptr<base::DictionaryValue> prefs_backup = base::Value prefs_backup =
prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS); prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS);
SetPrefs(prefs2, false); SetPrefs(prefs2, false);
CheckSettingsCorrespondToPrefs(prefs2); CheckSettingsCorrespondToPrefs(prefs2);
EXPECT_TRUE(prefs_backup->Equals( EXPECT_EQ(prefs_backup,
prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS).get())); prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS));
SetPrefs(prefs2, true); SetPrefs(prefs2, true);
CheckSettingsCorrespondToPrefs(prefs2); CheckSettingsCorrespondToPrefs(prefs2);
EXPECT_TRUE(prefs_backup->Equals( EXPECT_EQ(prefs_backup,
prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS).get())); prefs1->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS));
// Check that changing prefs of the inactive user doesn't affect prefs of the // Check that changing prefs of the inactive user doesn't affect prefs of the
// active user. // active user.
prefs_backup = prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS); prefs_backup = prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS);
SetPrefs(prefs1, true); SetPrefs(prefs1, true);
CheckSettingsCorrespondToPrefs(prefs2); CheckSettingsCorrespondToPrefs(prefs2);
EXPECT_TRUE(prefs_backup->Equals( EXPECT_EQ(prefs_backup,
prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS).get())); prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS));
SetPrefs(prefs1, false); SetPrefs(prefs1, false);
CheckSettingsCorrespondToPrefs(prefs2); CheckSettingsCorrespondToPrefs(prefs2);
EXPECT_TRUE(prefs_backup->Equals( EXPECT_EQ(prefs_backup,
prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS).get())); prefs2->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS));
// Check that changing non-owner prefs doesn't change corresponding local // Check that changing non-owner prefs doesn't change corresponding local
// state prefs and vice versa. // state prefs and vice versa.
......
...@@ -464,11 +464,12 @@ void ChromeInternalLogSource::PopulateLocalStateSettings( ...@@ -464,11 +464,12 @@ void ChromeInternalLogSource::PopulateLocalStateSettings(
SystemLogsResponse* response) { SystemLogsResponse* response) {
// Extract the "settings" entry in the local state and serialize back to // Extract the "settings" entry in the local state and serialize back to
// a string. // a string.
std::unique_ptr<base::DictionaryValue> local_state = base::Value local_state =
g_browser_process->local_state()->GetPreferenceValues( g_browser_process->local_state()->GetPreferenceValues(
PrefService::EXCLUDE_DEFAULTS); PrefService::EXCLUDE_DEFAULTS);
const base::DictionaryValue* local_state_settings = nullptr; const base::Value* local_state_settings =
if (!local_state->GetDictionary(kSettingsKey, &local_state_settings)) { local_state.FindDictKey(kSettingsKey);
if (!local_state_settings) {
VLOG(1) << "Failed to extract the settings entry from Local State."; VLOG(1) << "Failed to extract the settings entry from Local State.";
return; return;
} }
......
...@@ -243,7 +243,7 @@ IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) { ...@@ -243,7 +243,7 @@ IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestPrivacySecurityPrefs) {
// Verify that we have some Local State prefs. // Verify that we have some Local State prefs.
IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) { IN_PROC_BROWSER_TEST_F(PrefsFunctionalTest, TestHaveLocalStatePrefs) {
EXPECT_TRUE(g_browser_process->local_state() base::Value prefs = g_browser_process->local_state()->GetPreferenceValues(
->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS) PrefService::INCLUDE_DEFAULTS);
.get()); EXPECT_TRUE(prefs.is_dict());
} }
...@@ -65,18 +65,18 @@ void LocalStateUIHandler::RegisterMessages() { ...@@ -65,18 +65,18 @@ void LocalStateUIHandler::RegisterMessages() {
void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) { void LocalStateUIHandler::HandleRequestJson(const base::ListValue* args) {
AllowJavascript(); AllowJavascript();
std::unique_ptr<base::DictionaryValue> local_state_values( base::Value local_state_values =
g_browser_process->local_state()->GetPreferenceValues( g_browser_process->local_state()->GetPreferenceValues(
PrefService::EXCLUDE_DEFAULTS)); PrefService::EXCLUDE_DEFAULTS);
if (ENABLE_FILTERING) { if (ENABLE_FILTERING) {
std::vector<std::string> allowlisted_prefixes = { std::vector<std::string> allowlisted_prefixes = {
"variations", "user_experience_metrics", "uninstall_metrics"}; "variations", "user_experience_metrics", "uninstall_metrics"};
internal::FilterPrefs(allowlisted_prefixes, local_state_values.get()); internal::FilterPrefs(allowlisted_prefixes, local_state_values);
} }
std::string json; std::string json;
JSONStringValueSerializer serializer(&json); JSONStringValueSerializer serializer(&json);
serializer.set_pretty_print(true); serializer.set_pretty_print(true);
bool result = serializer.Serialize(*local_state_values); bool result = serializer.Serialize(local_state_values);
if (!result) if (!result)
json = "Error loading Local State file."; json = "Error loading Local State file.";
...@@ -99,16 +99,14 @@ bool HasValidPrefix(const std::string& pref_name, ...@@ -99,16 +99,14 @@ bool HasValidPrefix(const std::string& pref_name,
namespace internal { namespace internal {
void FilterPrefs(const std::vector<std::string>& valid_prefixes, void FilterPrefs(const std::vector<std::string>& valid_prefixes,
base::DictionaryValue* prefs) { base::Value& prefs) {
std::vector<std::string> prefs_to_remove; std::vector<std::string> prefs_to_remove;
for (base::DictionaryValue::Iterator it(*prefs); !it.IsAtEnd(); for (const auto& it : prefs.DictItems()) {
it.Advance()) { if (!HasValidPrefix(it.first, valid_prefixes))
if (!HasValidPrefix(it.key(), valid_prefixes)) prefs_to_remove.push_back(it.first);
prefs_to_remove.push_back(it.key());
} }
for (const std::string& pref_to_remove : prefs_to_remove) { for (const std::string& pref_to_remove : prefs_to_remove) {
std::unique_ptr<base::Value> removed_value; bool successfully_removed = prefs.RemovePath(pref_to_remove);
bool successfully_removed = prefs->Remove(pref_to_remove, &removed_value);
DCHECK(successfully_removed); DCHECK(successfully_removed);
} }
} }
......
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
#include "content/public/browser/web_ui_controller.h" #include "content/public/browser/web_ui_controller.h"
namespace base { namespace base {
class DictionaryValue; class Value;
} }
// Namespace for exposing the method for unit tests. // Namespace for exposing the method for unit tests.
...@@ -21,7 +21,7 @@ namespace internal { ...@@ -21,7 +21,7 @@ namespace internal {
// Removes elements from |prefs| where the key does not match any of the // Removes elements from |prefs| where the key does not match any of the
// prefixes in |valid_prefixes|. // prefixes in |valid_prefixes|.
void FilterPrefs(const std::vector<std::string>& valid_prefixes, void FilterPrefs(const std::vector<std::string>& valid_prefixes,
base::DictionaryValue* prefs); base::Value& prefs);
} // namespace internal } // namespace internal
......
...@@ -10,28 +10,25 @@ ...@@ -10,28 +10,25 @@
TEST(LocalStateUiTest, FilterPrefs) { TEST(LocalStateUiTest, FilterPrefs) {
std::vector<std::string> prefixes = {"foo", "bar", "baz"}; std::vector<std::string> prefixes = {"foo", "bar", "baz"};
std::vector<std::string> invalid_pref_keys = {"fo", "ar", "afoo"}; std::vector<std::string> invalid_pref_paths = {"fo", "ar", "afoo"};
std::vector<std::string> valid_pref_keys = {"foo", "foom", "bar.stuff"}; std::vector<std::string> valid_pref_paths = {"foo", "foom", "bar.stuff"};
std::vector<std::string> all_pref_keys = invalid_pref_keys; std::vector<std::string> all_pref_paths = invalid_pref_paths;
all_pref_keys.insert(all_pref_keys.end(), valid_pref_keys.begin(), all_pref_paths.insert(all_pref_paths.end(), valid_pref_paths.begin(),
valid_pref_keys.end()); valid_pref_paths.end());
base::DictionaryValue prefs; base::Value prefs(base::Value::Type::DICTIONARY);
for (const std::string& key : all_pref_keys) { for (const std::string& path : all_pref_paths)
prefs.SetString(key, key + "_value"); prefs.SetStringPath(path, path + "_value");
}
internal::FilterPrefs(prefixes, &prefs); internal::FilterPrefs(prefixes, prefs);
for (const std::string& invalid_key : invalid_pref_keys) { for (const std::string& invalid_path : invalid_pref_paths)
std::string value; EXPECT_FALSE(prefs.FindStringPath(invalid_path));
EXPECT_FALSE(prefs.GetString(invalid_key, &value));
}
for (const std::string& valid_key : valid_pref_keys) { for (const std::string& valid_path : valid_pref_paths) {
std::string value; const std::string* result = prefs.FindStringPath(valid_path);
EXPECT_TRUE(prefs.GetString(valid_key, &value)); ASSERT_TRUE(result);
EXPECT_EQ(valid_key + "_value", value); EXPECT_EQ(valid_path + "_value", *result);
} }
} }
...@@ -33,10 +33,9 @@ void PrefsInternalsSource::StartDataRequest( ...@@ -33,10 +33,9 @@ void PrefsInternalsSource::StartDataRequest(
content::URLDataSource::GotDataCallback callback) { content::URLDataSource::GotDataCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::string json; std::string json;
std::unique_ptr<base::DictionaryValue> prefs = base::Value prefs =
profile_->GetPrefs()->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS); profile_->GetPrefs()->GetPreferenceValues(PrefService::INCLUDE_DEFAULTS);
DCHECK(prefs);
CHECK(base::JSONWriter::WriteWithOptions( 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)); std::move(callback).Run(base::RefCountedString::TakeString(&json));
} }
...@@ -214,18 +214,19 @@ void PrefService::IteratePreferenceValues( ...@@ -214,18 +214,19 @@ void PrefService::IteratePreferenceValues(
callback.Run(it.first, *GetPreferenceValue(it.first)); callback.Run(it.first, *GetPreferenceValue(it.first));
} }
std::unique_ptr<base::DictionaryValue> PrefService::GetPreferenceValues( base::Value PrefService::GetPreferenceValues(
IncludeDefaults include_defaults) const { IncludeDefaults include_defaults) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); 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_) { for (const auto& it : *pref_registry_) {
if (include_defaults == INCLUDE_DEFAULTS) { if (include_defaults == INCLUDE_DEFAULTS) {
out->Set(it.first, GetPreferenceValue(it.first)->CreateDeepCopy()); out.SetPath(it.first, GetPreferenceValue(it.first)->Clone());
} else { } else {
const Preference* pref = FindPreference(it.first); const Preference* pref = FindPreference(it.first);
if (pref->IsDefaultValue()) if (pref->IsDefaultValue())
continue; continue;
out->Set(it.first, pref->GetValue()->CreateDeepCopy()); out.SetPath(it.first, pref->GetValue()->Clone());
} }
} }
return out; return out;
......
...@@ -306,8 +306,7 @@ class COMPONENTS_PREFS_EXPORT PrefService { ...@@ -306,8 +306,7 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// If INCLUDE_DEFAULTS is requested, preferences set to their default values // If INCLUDE_DEFAULTS is requested, preferences set to their default values
// will be included. Otherwise, these will be omitted from the returned // will be included. Otherwise, these will be omitted from the returned
// dictionary. // dictionary.
std::unique_ptr<base::DictionaryValue> GetPreferenceValues( base::Value GetPreferenceValues(IncludeDefaults include_defaults) const;
IncludeDefaults include_defaults) const;
bool ReadOnly() const; bool ReadOnly() const;
......
...@@ -45,12 +45,10 @@ class PrefsInternalsSource : public web::URLDataSourceIOS { ...@@ -45,12 +45,10 @@ class PrefsInternalsSource : public web::URLDataSourceIOS {
DCHECK_CURRENTLY_ON(web::WebThread::UI); DCHECK_CURRENTLY_ON(web::WebThread::UI);
std::string json; std::string json;
std::unique_ptr<base::DictionaryValue> prefs = base::Value prefs = browser_state_->GetPrefs()->GetPreferenceValues(
browser_state_->GetPrefs()->GetPreferenceValues( PrefService::INCLUDE_DEFAULTS);
PrefService::INCLUDE_DEFAULTS);
DCHECK(prefs);
CHECK(base::JSONWriter::WriteWithOptions( 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)); 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