[Code Health] base::Value cleanup after PrefService refactor
https://crrev.com/c/3343526 recently landed as a LSC, which
refactored the PrefService GetList() and GetDictionary() methods.
The blast radius of this interface was quite large, so bare minimum
changes were made at call sites.
This CL is part of an effort to clean up these call sites.
The unsplit version of this CL can be found at
https://crrev.com/c/3351044
base::Value refactor guidelines:
http://doc/1CwYuMXnVQsRsghwVzEkWj9GZzfERputSLQaKx5xLhjQ
This CL was uploaded by git cl split.
R=sophiechang@chromium.org
Bug: 1187001, 1187061, 1187062
Change-Id: I4d4ac3d4733ef0f1e474c02888ebbeea4d013378
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3350355
Auto-Submit: Austin Sullivan <asully@chromium.org>
Reviewed-by: Sophie Chang <sophiechang@chromium.org>
Commit-Queue: Sophie Chang <sophiechang@chromium.org>
Cr-Commit-Position: refs/heads/main@{#953642}
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc
index 29117fd..1699ed7 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.cc
@@ -49,12 +49,13 @@
}
// Returns the value at |index| of |list_value| as an int64_t.
-int64_t GetInt64PrefValue(const base::ListValue& list_value, size_t index) {
+int64_t GetInt64PrefValue(const base::Value& list_value, size_t index) {
int64_t val = 0;
base::Value::ConstListView list_value_view = list_value.GetList();
if (index < list_value_view.size() && list_value_view[index].is_string()) {
- std::string pref_value = list_value_view[index].GetString();
- bool rv = base::StringToInt64(pref_value, &val);
+ const base::Value& pref_value = list_value_view[index];
+ DCHECK(pref_value.is_string());
+ bool rv = base::StringToInt64(pref_value.GetString(), &val);
DCHECK(rv);
}
return val;
@@ -62,7 +63,7 @@
// Ensure list has exactly |length| elements, either by truncating at the
// front, or appending "0"'s to the back.
-void MaintainContentLengthPrefsWindow(base::ListValue* list, size_t length) {
+void MaintainContentLengthPrefsWindow(base::Value* list, size_t length) {
// Remove data for old days from the front.
base::Value::ListView list_view = list->GetList();
while (list_view.size() > length)
@@ -79,7 +80,7 @@
// number.
void AddInt64ToListPref(size_t index,
int64_t length,
- base::ListValue* list_update) {
+ base::Value* list_update) {
int64_t value = GetInt64PrefValue(*list_update, index) + length;
list_update->GetList()[index] = base::Value(base::NumberToString(value));
}
@@ -111,7 +112,7 @@
int key,
int value) {
DictionaryPrefUpdate pref_update(pref_service, pref);
- base::DictionaryValue* pref_dict = pref_update.Get();
+ base::Value* pref_dict = pref_update.Get();
const std::string key_str = base::NumberToString(key);
base::Value* dict_value = pref_dict->FindKey(key_str);
if (dict_value)
@@ -264,7 +265,7 @@
}
// Non-owned. Lazily initialized, set to nullptr until initialized.
- raw_ptr<base::ListValue> update_;
+ raw_ptr<base::Value> update_;
// Non-owned pointer.
raw_ptr<DataReductionProxyCompressionStats> compression_stats_;
// The path of the content length pref for |this|.
@@ -396,8 +397,7 @@
void DataReductionProxyCompressionStats::InitListPref(const char* pref) {
base::Value pref_value = pref_service_->GetList(pref)->Clone();
- list_pref_map_[pref] = base::ListValue::From(
- base::Value::ToUniquePtrValue(std::move(pref_value)));
+ list_pref_map_[pref] = std::move(pref_value);
}
int64_t DataReductionProxyCompressionStats::GetInt64(const char* pref_path) {
@@ -425,7 +425,7 @@
SetInt64(pref_path, GetInt64(pref_path) + delta);
}
-base::ListValue* DataReductionProxyCompressionStats::GetList(
+base::Value* DataReductionProxyCompressionStats::GetList(
const char* pref_path) {
if (delay_.is_zero())
return ListPrefUpdate(pref_service_, pref_path).Get();
@@ -434,7 +434,7 @@
auto it = list_pref_map_.find(pref_path);
if (it == list_pref_map_.end())
return nullptr;
- return it->second.get();
+ return &it->second;
}
void DataReductionProxyCompressionStats::WritePrefs() {
@@ -448,7 +448,7 @@
for (auto iter = list_pref_map_.begin(); iter != list_pref_map_.end();
++iter) {
- TransferList(*(iter->second.get()),
+ TransferList(iter->second,
ListPrefUpdate(pref_service_, iter->first).Get());
}
}
@@ -461,9 +461,9 @@
}
void DataReductionProxyCompressionStats::ResetStatistics() {
- base::ListValue* original_update =
+ base::Value* original_update =
GetList(prefs::kDailyHttpOriginalContentLength);
- base::ListValue* received_update =
+ base::Value* received_update =
GetList(prefs::kDailyHttpReceivedContentLength);
original_update->ClearList();
received_update->ClearList();
@@ -484,7 +484,7 @@
ContentLengthList DataReductionProxyCompressionStats::GetDailyContentLengths(
const char* pref_name) {
ContentLengthList content_lengths;
- const base::ListValue* list_value = GetList(pref_name);
+ const base::Value* list_value = GetList(pref_name);
if (list_value->GetList().size() == kNumDaysInHistory) {
for (size_t i = 0; i < kNumDaysInHistory; ++i)
content_lengths.push_back(GetInt64PrefValue(*list_value, i));
@@ -499,9 +499,9 @@
int64_t* last_update_time) {
DCHECK_LE(days, kNumDaysInHistory);
- const base::ListValue* original_list =
+ const base::Value* original_list =
GetList(prefs::kDailyHttpOriginalContentLength);
- const base::ListValue* received_list =
+ const base::Value* received_list =
GetList(prefs::kDailyHttpReceivedContentLength);
if (original_list->GetList().size() != kNumDaysInHistory ||
@@ -611,7 +611,7 @@
for (auto iter = list_pref_map_.begin(); iter != list_pref_map_.end();
++iter) {
- iter->second->ClearList();
+ iter->second.ClearList();
}
RecordSavingsClearedMetric(reason);
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h
index cc86976..4595582 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats.h
@@ -28,7 +28,6 @@
class PrefService;
namespace base {
-class ListValue;
class Value;
}
@@ -169,7 +168,7 @@
friend class DataReductionProxyCompressionStatsTest;
typedef std::map<const char*, int64_t> DataReductionProxyPrefMap;
- typedef std::unordered_map<const char*, std::unique_ptr<base::ListValue>>
+ typedef std::unordered_map<const char*, base::Value>
DataReductionProxyListPrefMap;
class DailyContentLengthUpdate;
@@ -201,7 +200,7 @@
void IncreaseInt64Pref(const char* pref_path, int64_t delta);
// Gets the pref list at |pref_path| from the |DataReductionProxyPrefMap|.
- base::ListValue* GetList(const char* pref_path);
+ base::Value* GetList(const char* pref_path);
// Writes the prefs stored in |DataReductionProxyPrefMap| and
// |DataReductionProxyListPrefMap| to |pref_service|.
diff --git a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc
index d45ccd464..932ff0e0 100644
--- a/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc
+++ b/components/data_reduction_proxy/core/browser/data_reduction_proxy_compression_stats_unittest.cc
@@ -38,8 +38,7 @@
// for 60 days.
const int kNumExpectedBuckets = 60 * 24 * 60 / 15;
-int64_t GetListPrefInt64Value(const base::ListValue& list_update,
- size_t index) {
+int64_t GetListPrefInt64Value(const base::Value& list_update, size_t index) {
std::string string_value;
base::Value::ConstListView list_view = list_update.GetList();
if (index < list_view.size() && list_view[index].is_string()) {
@@ -157,9 +156,9 @@
compression_stats_->SetInt64(
prefs::kHttpReceivedContentLength, kReceivedLength);
- base::ListValue* original_daily_content_length_list =
+ base::Value* original_daily_content_length_list =
compression_stats_->GetList(prefs::kDailyHttpOriginalContentLength);
- base::ListValue* received_daily_content_length_list =
+ base::Value* received_daily_content_length_list =
compression_stats_->GetList(prefs::kDailyHttpReceivedContentLength);
for (size_t i = 0; i < kNumDaysInHistory; ++i) {
@@ -185,9 +184,8 @@
// Verify the pref list values in |pref_service_| are equal to those in
// |simple_pref_service| for |pref|.
void VerifyPrefListWasWritten(const char* pref) {
- const base::ListValue* delayed_list = compression_stats_->GetList(pref);
- const base::ListValue* written_list =
- &base::Value::AsListValue(*pref_service()->GetList(pref));
+ const base::Value* delayed_list = compression_stats_->GetList(pref);
+ const base::Value* written_list = pref_service()->GetList(pref);
ASSERT_EQ(delayed_list->GetList().size(), written_list->GetList().size());
size_t count = delayed_list->GetList().size();
@@ -205,25 +203,6 @@
EXPECT_EQ(delayed_pref, written_pref);
}
- // Verify the pref values in |dict| are equal to that in |compression_stats_|.
- void VerifyPrefs(base::DictionaryValue* dict) {
- std::u16string dict_pref_string;
- int64_t dict_pref;
- int64_t service_pref;
-
- dict->GetString("historic_original_content_length", &dict_pref_string);
- base::StringToInt64(dict_pref_string, &dict_pref);
- service_pref =
- compression_stats_->GetInt64(prefs::kHttpOriginalContentLength);
- EXPECT_EQ(service_pref, dict_pref);
-
- dict->GetString("historic_received_content_length", &dict_pref_string);
- base::StringToInt64(dict_pref_string, &dict_pref);
- service_pref =
- compression_stats_->GetInt64(prefs::kHttpReceivedContentLength);
- EXPECT_EQ(service_pref, dict_pref);
- }
-
// Verify the pref list values are equal to the given values.
// If the count of values is less than kNumDaysInHistory, zeros are assumed
// at the beginning.
@@ -232,7 +211,7 @@
size_t count,
size_t num_days_in_history) {
ASSERT_GE(num_days_in_history, count);
- base::ListValue* update = compression_stats_->GetList(pref);
+ base::Value* update = compression_stats_->GetList(pref);
ASSERT_EQ(num_days_in_history, update->GetList().size())
<< "Pref: " << pref;