Skip to content

Commit

Permalink
Remove deprecated base::Value::CreateWithCopiedBuffer().
Browse files Browse the repository at this point in the history
Convert remaining callers.

Bug: 646113
Change-Id: I66cf237f210ffb70bb03adfcb13c6de604f28525
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2482247
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Reviewed-by: Jan Wilken Dörrie <jdoerrie@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#820510}
  • Loading branch information
leizleiz authored and Commit Bot committed Oct 24, 2020
1 parent 8a7c8bd commit 2048124
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 54 deletions.
6 changes: 0 additions & 6 deletions base/values.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,6 @@ class PathSplitter {

} // namespace

// static
std::unique_ptr<Value> Value::CreateWithCopiedBuffer(const char* buffer,
size_t size) {
return std::make_unique<Value>(BlobStorage(buffer, buffer + size));
}

// static
Value Value::FromUniquePtrValue(std::unique_ptr<Value> val) {
return std::move(*val);
Expand Down
8 changes: 0 additions & 8 deletions base/values.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,6 @@ class BASE_EXPORT Value {
// Note: Do not add more types. See the file-level comment above for why.
};

// For situations where you want to keep ownership of your buffer, this
// factory method creates a new BinaryValue by copying the contents of the
// buffer that's passed in.
// DEPRECATED, use std::make_unique<Value>(const BlobStorage&) instead.
// TODO(crbug.com/646113): Delete this and migrate callsites.
static std::unique_ptr<Value> CreateWithCopiedBuffer(const char* buffer,
size_t size);

// Adaptors for converting from the old way to the new way and vice versa.
static Value FromUniquePtrValue(std::unique_ptr<Value> val);
static std::unique_ptr<Value> ToUniquePtrValue(Value val);
Expand Down
27 changes: 12 additions & 15 deletions base/values_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1362,29 +1362,26 @@ TEST(ValuesTest, List) {

TEST(ValuesTest, BinaryValue) {
// Default constructor creates a BinaryValue with a buffer of size 0.
auto binary = std::make_unique<Value>(Value::Type::BINARY);
ASSERT_TRUE(binary.get());
ASSERT_TRUE(binary->GetBlob().empty());
Value binary(Value::Type::BINARY);
ASSERT_TRUE(binary.GetBlob().empty());

// Test the common case of a non-empty buffer
Value::BlobStorage buffer(15);
uint8_t* original_buffer = buffer.data();
binary.reset(new Value(std::move(buffer)));
ASSERT_TRUE(binary.get());
ASSERT_TRUE(binary->GetBlob().data());
ASSERT_EQ(original_buffer, binary->GetBlob().data());
ASSERT_EQ(15U, binary->GetBlob().size());
binary = Value(std::move(buffer));
ASSERT_TRUE(binary.GetBlob().data());
ASSERT_EQ(original_buffer, binary.GetBlob().data());
ASSERT_EQ(15U, binary.GetBlob().size());

char stack_buffer[42];
memset(stack_buffer, '!', 42);
binary = Value::CreateWithCopiedBuffer(stack_buffer, 42);
ASSERT_TRUE(binary.get());
ASSERT_TRUE(binary->GetBlob().data());
binary = Value(Value::BlobStorage(stack_buffer, stack_buffer + 42));
ASSERT_TRUE(binary.GetBlob().data());
ASSERT_NE(stack_buffer,
reinterpret_cast<const char*>(binary->GetBlob().data()));
ASSERT_EQ(42U, binary->GetBlob().size());
ASSERT_EQ(0, memcmp(stack_buffer, binary->GetBlob().data(),
binary->GetBlob().size()));
reinterpret_cast<const char*>(binary.GetBlob().data()));
ASSERT_EQ(42U, binary.GetBlob().size());
ASSERT_EQ(0, memcmp(stack_buffer, binary.GetBlob().data(),
binary.GetBlob().size()));
}

TEST(ValuesTest, StringValue) {
Expand Down
10 changes: 5 additions & 5 deletions content/browser/renderer_host/pepper/pepper_socket_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <string>
#include <vector>

#include "base/containers/span.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/strings/string_util.h"
Expand Down Expand Up @@ -109,10 +110,9 @@ bool GetCertificateFields(const net::X509Certificate& cert,
std::make_unique<base::Value>(base::JoinString(
subject.organization_unit_names, "\n")));

const std::string& serial_number = cert.serial_number();
fields->SetField(PP_X509CERTIFICATE_PRIVATE_SERIAL_NUMBER,
base::Value::CreateWithCopiedBuffer(serial_number.data(),
serial_number.length()));
base::Value::ToUniquePtrValue(base::Value(
base::as_bytes(base::make_span(cert.serial_number())))));
fields->SetField(
PP_X509CERTIFICATE_PRIVATE_VALIDITY_NOT_BEFORE,
std::make_unique<base::Value>(cert.valid_start().ToDoubleT()));
Expand All @@ -122,8 +122,8 @@ bool GetCertificateFields(const net::X509Certificate& cert,
base::StringPiece cert_der =
net::x509_util::CryptoBufferAsStringPiece(cert.cert_buffer());
fields->SetField(PP_X509CERTIFICATE_PRIVATE_RAW,
std::make_unique<base::Value>(base::Value::BlobStorage(
cert_der.begin(), cert_der.end())));
base::Value::ToUniquePtrValue(
base::Value(base::as_bytes(base::make_span(cert_der)))));
return true;
}

Expand Down
7 changes: 5 additions & 2 deletions content/common/android/gin_java_bridge_value.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include "content/common/android/gin_java_bridge_value.h"

#include "base/containers/span.h"

namespace content {

namespace {
Expand Down Expand Up @@ -118,8 +120,9 @@ GinJavaBridgeValue::GinJavaBridgeValue(const base::Value* value)
}

std::unique_ptr<base::Value> GinJavaBridgeValue::SerializeToBinaryValue() {
return base::Value::CreateWithCopiedBuffer(
reinterpret_cast<const char*>(pickle_.data()), pickle_.size());
const auto* data = static_cast<const uint8_t*>(pickle_.data());
return base::Value::ToUniquePtrValue(
base::Value(base::make_span(data, pickle_.size())));
}

} // namespace content
7 changes: 3 additions & 4 deletions content/common/android/gin_java_bridge_value_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cmath>
#include <memory>

#include "base/containers/span.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {
Expand Down Expand Up @@ -83,10 +84,8 @@ TEST_F(GinJavaBridgeValueTest, BrokenValues) {
GinJavaBridgeValue::ContainsGinJavaBridgeValue(non_binary.get()));

const char dummy_data[] = "\000\001\002\003\004\005\006\007\010\011\012\013";
std::unique_ptr<base::Value> broken_binary(
base::Value::CreateWithCopiedBuffer(dummy_data, sizeof(dummy_data)));
EXPECT_FALSE(
GinJavaBridgeValue::ContainsGinJavaBridgeValue(broken_binary.get()));
base::Value broken_binary(base::as_bytes(base::make_span(dummy_data)));
EXPECT_FALSE(GinJavaBridgeValue::ContainsGinJavaBridgeValue(&broken_binary));
}

} // namespace
15 changes: 9 additions & 6 deletions content/renderer/v8_value_converter_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/containers/span.h"
#include "base/logging.h"
#include "base/values.h"
#include "v8/include/v8.h"
Expand Down Expand Up @@ -497,18 +498,20 @@ std::unique_ptr<base::Value> V8ValueConverterImpl::FromV8ArrayBuffer(

if (val->IsArrayBuffer()) {
auto contents = val.As<v8::ArrayBuffer>()->GetContents();
return base::Value::CreateWithCopiedBuffer(
static_cast<const char*>(contents.Data()), contents.ByteLength());
} else if (val->IsArrayBufferView()) {
const auto* data = static_cast<const uint8_t*>(contents.Data());
return base::Value::ToUniquePtrValue(
base::Value(base::make_span(data, contents.ByteLength())));
}
if (val->IsArrayBufferView()) {
v8::Local<v8::ArrayBufferView> view = val.As<v8::ArrayBufferView>();
size_t byte_length = view->ByteLength();
std::vector<char> buffer(byte_length);
view->CopyContents(buffer.data(), buffer.size());
return std::make_unique<base::Value>(std::move(buffer));
} else {
NOTREACHED() << "Only ArrayBuffer and ArrayBufferView should get here.";
return nullptr;
}

NOTREACHED() << "Only ArrayBuffer and ArrayBufferView should get here.";
return nullptr;
}

std::unique_ptr<base::Value> V8ValueConverterImpl::FromV8Object(
Expand Down
13 changes: 7 additions & 6 deletions content/renderer/v8_value_converter_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cmath>
#include <memory>

#include "base/containers/span.h"
#include "base/macros.h"
#include "base/stl_util.h"
#include "base/test/task_environment.h"
Expand Down Expand Up @@ -1220,18 +1221,18 @@ TEST_F(V8ValueConverterImplTest, StrategyBypass) {
std::unique_ptr<base::Value> binary_value(
converter.FromV8Value(array_buffer, context));
ASSERT_TRUE(binary_value);
std::unique_ptr<base::Value> reference_binary_value(
base::Value::CreateWithCopiedBuffer(kExampleData, sizeof(kExampleData)));
EXPECT_EQ(*reference_binary_value, *binary_value);
base::Value reference_binary_value(
base::as_bytes(base::make_span(kExampleData)));
EXPECT_EQ(reference_binary_value, *binary_value);

v8::Local<v8::ArrayBufferView> array_buffer_view(
v8::Uint8Array::New(array_buffer, 1, 3));
std::unique_ptr<base::Value> binary_view_value(
converter.FromV8Value(array_buffer_view, context));
ASSERT_TRUE(binary_view_value);
std::unique_ptr<base::Value> reference_binary_view_value(
base::Value::CreateWithCopiedBuffer(&kExampleData[1], 3));
EXPECT_EQ(*reference_binary_view_value, *binary_view_value);
base::Value reference_binary_view_value(
base::as_bytes(base::make_span(kExampleData).subspan(1, 3)));
EXPECT_EQ(reference_binary_view_value, *binary_view_value);

v8::Local<v8::Number> number(v8::Number::New(isolate_, 0.0));
std::unique_ptr<base::Value> number_value(
Expand Down
7 changes: 5 additions & 2 deletions tools/ipc_fuzzer/fuzzer/fuzzer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <vector>

#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/memory/ptr_util.h"
#include "base/stl_util.h"
#include "base/strings/nullable_string16.h"
Expand Down Expand Up @@ -535,7 +536,8 @@ struct FuzzTraits<base::ListValue> {
char tmp[200];
size_t bin_length = RandInRange(sizeof(tmp));
fuzzer->FuzzData(tmp, bin_length);
p->Set(index, base::Value::CreateWithCopiedBuffer(tmp, bin_length));
p->Set(index, base::Value::ToUniquePtrValue(base::Value(
base::as_bytes(base::make_span(tmp, bin_length)))));
break;
}
case base::Value::Type::DICTIONARY: {
Expand Down Expand Up @@ -612,7 +614,8 @@ struct FuzzTraits<base::DictionaryValue> {
size_t bin_length = RandInRange(sizeof(tmp));
fuzzer->FuzzData(tmp, bin_length);
p->SetWithoutPathExpansion(
property, base::Value::CreateWithCopiedBuffer(tmp, bin_length));
property, base::Value::ToUniquePtrValue(base::Value(
base::as_bytes(base::make_span(tmp, bin_length)))));
break;
}
case base::Value::Type::DICTIONARY: {
Expand Down

0 comments on commit 2048124

Please sign in to comment.