Commit 8c1432bf authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Add base::Value::AsDictionaryValue().

This is an adaptor to allow a caller to use base::Value instead of
base::DictionaryValue to construct a dictionary value, and able to
easily pass the value to a function that still expects const
base::DictionaryValue&. This makes it easier to move away from
base::DictionaryValue in smaller increments. Use it in one place to
demonstrate it works.

Do the same for list values with base::Value::AsListValue().

Bug: 646113
Change-Id: I9116525ef5a5a80d3752cdca1960d21b2288ff42
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1838684Reviewed-by: default avatarPaul Jensen <pauljensen@chromium.org>
Reviewed-by: default avatarJan Wilken Dörrie <jdoerrie@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#703845}
parent e69c9b26
...@@ -147,6 +147,18 @@ std::unique_ptr<Value> Value::ToUniquePtrValue(Value val) { ...@@ -147,6 +147,18 @@ std::unique_ptr<Value> Value::ToUniquePtrValue(Value val) {
return std::make_unique<Value>(std::move(val)); return std::make_unique<Value>(std::move(val));
} }
// static
const DictionaryValue& Value::AsDictionaryValue(const Value& val) {
CHECK(val.is_dict());
return static_cast<const DictionaryValue&>(val);
}
// static
const ListValue& Value::AsListValue(const Value& val) {
CHECK(val.is_list());
return static_cast<const ListValue&>(val);
}
Value::Value(Value&& that) noexcept { Value::Value(Value&& that) noexcept {
InternalMoveConstructFrom(std::move(that)); InternalMoveConstructFrom(std::move(that));
} }
......
...@@ -111,6 +111,8 @@ class BASE_EXPORT Value { ...@@ -111,6 +111,8 @@ class BASE_EXPORT Value {
// Adaptors for converting from the old way to the new way and vice versa. // Adaptors for converting from the old way to the new way and vice versa.
static Value FromUniquePtrValue(std::unique_ptr<Value> val); static Value FromUniquePtrValue(std::unique_ptr<Value> val);
static std::unique_ptr<Value> ToUniquePtrValue(Value val); static std::unique_ptr<Value> ToUniquePtrValue(Value val);
static const DictionaryValue& AsDictionaryValue(const Value& val);
static const ListValue& AsListValue(const Value& val);
Value(Value&& that) noexcept; Value(Value&& that) noexcept;
Value() noexcept {} // A null value Value() noexcept {} // A null value
......
...@@ -701,7 +701,7 @@ TEST_F(PrintPreviewHandlerTest, SendPreviewUpdates) { ...@@ -701,7 +701,7 @@ TEST_F(PrintPreviewHandlerTest, SendPreviewUpdates) {
// Simulate renderer responses: PageLayoutReady, PageCountReady, // Simulate renderer responses: PageLayoutReady, PageCountReady,
// PagePreviewReady, and OnPrintPreviewReady will be called in that order. // PagePreviewReady, and OnPrintPreviewReady will be called in that order.
base::DictionaryValue layout; base::Value layout(base::Value::Type::DICTIONARY);
layout.SetDoubleKey(kSettingMarginTop, 34.0); layout.SetDoubleKey(kSettingMarginTop, 34.0);
layout.SetDoubleKey(kSettingMarginLeft, 34.0); layout.SetDoubleKey(kSettingMarginLeft, 34.0);
layout.SetDoubleKey(kSettingMarginBottom, 34.0); layout.SetDoubleKey(kSettingMarginBottom, 34.0);
...@@ -712,7 +712,8 @@ TEST_F(PrintPreviewHandlerTest, SendPreviewUpdates) { ...@@ -712,7 +712,8 @@ TEST_F(PrintPreviewHandlerTest, SendPreviewUpdates) {
layout.SetIntKey(kSettingPrintableAreaY, 17); layout.SetIntKey(kSettingPrintableAreaY, 17);
layout.SetIntKey(kSettingPrintableAreaWidth, 578); layout.SetIntKey(kSettingPrintableAreaWidth, 578);
layout.SetIntKey(kSettingPrintableAreaHeight, 734); layout.SetIntKey(kSettingPrintableAreaHeight, 734);
handler()->SendPageLayoutReady(layout, /*has_custom_page_size_style,=*/false, handler()->SendPageLayoutReady(base::Value::AsDictionaryValue(layout),
/*has_custom_page_size_style,=*/false,
preview_request_id); preview_request_id);
// Verify that page-layout-ready webUI event was fired. // Verify that page-layout-ready webUI event was fired.
......
...@@ -45,14 +45,14 @@ class HostCachePersistenceManagerTest : public testing::Test { ...@@ -45,14 +45,14 @@ class HostCachePersistenceManagerTest : public testing::Test {
// not the full contents, since the tests in this file are only intended // not the full contents, since the tests in this file are only intended
// to test that writes happen when they're supposed to, not serialization // to test that writes happen when they're supposed to, not serialization
// correctness. // correctness.
void CheckPref(size_t size) { void CheckPref(size_t expected_size) {
const base::Value* value = pref_service_->GetUserPref(kPrefName); const base::Value* value = pref_service_->GetUserPref(kPrefName);
base::ListValue list; base::Value list(base::Value::Type::LIST);
if (value) if (value)
list = base::ListValue(value->GetList()); list = base::Value(value->GetList());
net::HostCache temp_cache(10); net::HostCache temp_cache(10);
temp_cache.RestoreFromListValue(list); temp_cache.RestoreFromListValue(base::Value::AsListValue(list));
ASSERT_EQ(size, temp_cache.size()); ASSERT_EQ(expected_size, temp_cache.size());
} }
// Generates a temporary HostCache with a few entries and uses it to // Generates a temporary HostCache with a few entries and uses it to
......
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