fix: modernize ListValue in net converters (#34657)

This commit is contained in:
Jeremy Rose 2022-06-22 01:14:57 -07:00 committed by GitHub
parent 73c85410c5
commit bf52318c76
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -153,20 +153,20 @@ v8::Local<v8::Value> Converter<net::CertPrincipal>::ToV8(
v8::Local<v8::Value> Converter<net::HttpResponseHeaders*>::ToV8( v8::Local<v8::Value> Converter<net::HttpResponseHeaders*>::ToV8(
v8::Isolate* isolate, v8::Isolate* isolate,
net::HttpResponseHeaders* headers) { net::HttpResponseHeaders* headers) {
base::DictionaryValue response_headers; base::Value::Dict response_headers;
if (headers) { if (headers) {
size_t iter = 0; size_t iter = 0;
std::string key; std::string key;
std::string value; std::string value;
while (headers->EnumerateHeaderLines(&iter, &key, &value)) { while (headers->EnumerateHeaderLines(&iter, &key, &value)) {
key = base::ToLowerASCII(key); key = base::ToLowerASCII(key);
base::Value* values = response_headers.FindListKey(key); base::Value::List* values = response_headers.FindList(key);
if (!values) if (!values)
values = response_headers.SetKey(key, base::ListValue()); values = &response_headers.Set(key, base::Value::List())->GetList();
values->Append(value); values->Append(value);
} }
} }
return ConvertToV8(isolate, response_headers); return ConvertToV8(isolate, base::Value(std::move(response_headers)));
} }
bool Converter<net::HttpResponseHeaders*>::FromV8( bool Converter<net::HttpResponseHeaders*>::FromV8(
@ -313,33 +313,31 @@ bool Converter<scoped_refptr<network::ResourceRequestBody>>::FromV8(
v8::Isolate* isolate, v8::Isolate* isolate,
v8::Local<v8::Value> val, v8::Local<v8::Value> val,
scoped_refptr<network::ResourceRequestBody>* out) { scoped_refptr<network::ResourceRequestBody>* out) {
auto list = std::make_unique<base::ListValue>(); base::Value list_value;
if (!ConvertFromV8(isolate, val, list.get())) if (!ConvertFromV8(isolate, val, &list_value) || !list_value.is_list())
return false; return false;
base::Value::List& list = list_value.GetList();
*out = base::MakeRefCounted<network::ResourceRequestBody>(); *out = base::MakeRefCounted<network::ResourceRequestBody>();
for (size_t i = 0; i < list->GetListDeprecated().size(); ++i) { for (size_t i = 0; i < list.size(); ++i) {
base::DictionaryValue* dict = nullptr; base::Value& dict_value = list[i];
std::string type; if (!dict_value.is_dict())
if (!list->GetDictionary(i, &dict))
return false; return false;
dict->GetString("type", &type); base::Value::Dict& dict = dict_value.GetDict();
if (type == "rawData") { std::string* type = dict.FindString("type");
const base::Value::BlobStorage* bytes = dict->FindBlobKey("bytes"); if (!type)
return false;
if (*type == "rawData") {
const base::Value::BlobStorage* bytes = dict.FindBlob("bytes");
(*out)->AppendBytes(reinterpret_cast<const char*>(bytes->data()), (*out)->AppendBytes(reinterpret_cast<const char*>(bytes->data()),
base::checked_cast<int>(bytes->size())); base::checked_cast<int>(bytes->size()));
} else if (type == "file") { } else if (*type == "file") {
const std::string* file = dict->FindStringKey("filePath"); const std::string* file = dict.FindString("filePath");
if (file == nullptr) { if (!file)
return false; return false;
} double modification_time =
int offset = 0, length = -1; dict.FindDouble("modificationTime").value_or(0.0);
double modification_time = 0.0; int offset = dict.FindInt("offset").value_or(0);
absl::optional<double> maybe_modification_time = int length = dict.FindInt("length").value_or(-1);
dict->FindDoubleKey("modificationTime");
if (maybe_modification_time.has_value())
modification_time = maybe_modification_time.value();
dict->GetInteger("offset", &offset);
dict->GetInteger("file", &length);
(*out)->AppendFileRange(base::FilePath::FromUTF8Unsafe(*file), (*out)->AppendFileRange(base::FilePath::FromUTF8Unsafe(*file),
static_cast<uint64_t>(offset), static_cast<uint64_t>(offset),
static_cast<uint64_t>(length), static_cast<uint64_t>(length),