Skip to content

Commit

Permalink
Remove RefCountedBytes unsafe ctor, use spans in devtools fromSpan
Browse files Browse the repository at this point in the history
Bug: 40284755
Low-Coverage-Reason: changing types in existing code
Change-Id: Ifcd1248c45e631df7f78bc3637538fdf597dabce
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5836909
Commit-Queue: danakj <[email protected]>
Reviewed-by: Dmitry Gozman <[email protected]>
Cr-Commit-Position: refs/heads/main@{#1352005}
  • Loading branch information
danakj authored and Chromium LUCI CQ committed Sep 6, 2024
1 parent 207d350 commit a4c24b9
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 32 deletions.
7 changes: 3 additions & 4 deletions content/browser/devtools/protocol/network_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -760,13 +760,12 @@ bool GetPostData(
// TODO(caseq): Also support blobs.
if (element.type() != network::DataElement::Tag::kBytes)
return false;
const std::vector<uint8_t>& bytes =
base::span<const uint8_t> bytes =
element.As<network::DataElementBytes>().bytes();
auto data_entry = protocol::Network::PostDataEntry::Create().Build();
data_entry->SetBytes(
protocol::Binary::fromSpan(bytes.data(), bytes.size()));
data_entry->SetBytes(protocol::Binary::fromSpan(bytes));
data_entries->push_back(std::move(data_entry));
result->append(reinterpret_cast<const char*>(bytes.data()), bytes.size());
result->append(base::as_string_view(bytes));
}
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion content/browser/devtools/protocol/webauthn_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ std::unique_ptr<WebAuthn::Credential> BuildCredentialFromRegistration(
base::span<const uint8_t> id,
const device::VirtualFidoDevice::RegistrationData* registration) {
auto credential = WebAuthn::Credential::Create()
.SetCredentialId(Binary::fromSpan(id.data(), id.size()))
.SetCredentialId(Binary::fromSpan(id))
.SetPrivateKey(Binary::fromVector(
registration->private_key->GetPKCS8PrivateKey()))
.SetSignCount(registration->counter)
Expand Down
1 change: 1 addition & 0 deletions third_party/blink/renderer/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ include_rules = [
"+base/containers/heap_array.h",
"+base/containers/lru_cache.h",
"+base/containers/span.h",
"+base/containers/span_or_size.h",
"+base/containers/span_reader.h",
"+base/containers/span_writer.h",
"+base/dcheck_is_on.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <utility>

#include "base/containers/span.h"
#include "base/containers/span_or_size.h"
#include "base/memory/scoped_refptr.h"
#include "base/numerics/safe_conversions.h"
#include "base/task/single_thread_task_runner.h"
Expand Down Expand Up @@ -830,10 +831,8 @@ static bool FormDataToString(

for (const auto& element : body->Elements()) {
auto data_entry = protocol::Network::PostDataEntry::create().build();
auto bytes = protocol::Binary::fromSpan(
reinterpret_cast<const uint8_t*>(element.data_.data()),
element.data_.size());
data_entry->setBytes(std::move(bytes));
data_entry->setBytes(
protocol::Binary::fromSpan(base::as_byte_span(element.data_)));
data_entries->push_back(std::move(data_entry));
}

Expand Down Expand Up @@ -1602,31 +1601,39 @@ protocol::Response InspectorNetworkAgent::streamResourceContent(

void InspectorNetworkAgent::DidReceiveData(uint64_t identifier,
DocumentLoader* loader,
const char* data,
const char* data_ptr,
uint64_t data_length) {
auto data = data_ptr
? base::SpanOrSize<const char>(
// DidReceiveData should receive a base::SpanOrSize
// instead of a pointer
// and size.
UNSAFE_TODO(base::span(
data_ptr, base::checked_cast<size_t>(data_length))))
: base::SpanOrSize<const char>(data_length);
String request_id = RequestId(loader, identifier);
Maybe<protocol::Binary> binary_data;

if (data) {
if (auto data_span = data.span(); data_span) {
NetworkResourcesData::ResourceData const* resource_data =
resources_data_->Data(request_id);
if (resource_data && !resource_data->HasContent() &&
(!resource_data->CachedResource() ||
resource_data->CachedResource()->GetDataBufferingPolicy() ==
kDoNotBufferData ||
IsErrorStatusCode(resource_data->HttpStatusCode())))
resources_data_->MaybeAddResourceData(request_id, data, data_length);
IsErrorStatusCode(resource_data->HttpStatusCode()))) {
resources_data_->MaybeAddResourceData(request_id, data_span->data(),
data_span->size());
}

if (streaming_request_ids_.Contains(request_id)) {
binary_data =
protocol::Binary::fromSpan(reinterpret_cast<const uint8_t*>(data),
base::checked_cast<size_t>(data_length));
binary_data = protocol::Binary::fromSpan(base::as_bytes(*data_span));
}
}

GetFrontend()->dataReceived(
request_id, base::TimeTicks::Now().since_origin().InSecondsF(),
static_cast<int>(data_length),
static_cast<int>(data.size()),
static_cast<int>(
resources_data_->GetAndClearPendingEncodedDataLength(request_id)),
std::move(binary_data));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ Binary Binary::fromVector(Vector<uint8_t> in) {
}

// static
Binary Binary::fromSpan(const uint8_t* data, size_t size) {
Binary Binary::fromSpan(base::span<const uint8_t> data) {
Vector<uint8_t> in;
in.Append(data, base::checked_cast<wtf_size_t>(size));
in.Append(data.data(), base::checked_cast<wtf_size_t>(data.size()));
return Binary::fromVector(std::move(in));
}

Expand Down Expand Up @@ -184,8 +184,7 @@ bool ProtocolTypeTraits<blink::protocol::Binary>::Deserialize(
blink::protocol::Binary* value) {
auto* tokenizer = state->tokenizer();
if (tokenizer->TokenTag() == crdtp::cbor::CBORTokenTag::BINARY) {
const span<uint8_t> bin = tokenizer->GetBinary();
*value = Binary::fromSpan(bin.data(), bin.size());
*value = Binary::fromSpan(tokenizer->GetBinary());
return true;
}
if (tokenizer->TokenTag() == crdtp::cbor::CBORTokenTag::STRING8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <memory>

#include "base/containers/span.h"
#include "third_party/blink/public/platform/web_vector.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
Expand Down Expand Up @@ -81,7 +82,7 @@ class CORE_EXPORT Binary : public crdtp::Serializable {
String toBase64() const;
static Binary fromBase64(const String& base64, bool* success);
static Binary fromVector(Vector<uint8_t> in);
static Binary fromSpan(const uint8_t* data, size_t size);
static Binary fromSpan(base::span<const uint8_t> data);

// Note: |data.buffer_policy| must be
// ScriptCompiler::ScriptCompiler::CachedData::BufferOwned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,16 @@ Binary Binary::fromString(std::string data) {
}

// static
Binary Binary::fromSpan(const uint8_t* data, size_t size) {
return Binary(scoped_refptr<base::RefCountedBytes>(
new base::RefCountedBytes(UNSAFE_TODO(base::span(data, size)))));
Binary Binary::fromSpan(base::span<const uint8_t> data) {
return Binary(base::MakeRefCounted<base::RefCountedBytes>(data));
}

// static
bool ProtocolTypeTraits<Binary>::Deserialize(DeserializerState* state,
Binary* value) {
auto* tokenizer = state->tokenizer();
if (tokenizer->TokenTag() == cbor::CBORTokenTag::BINARY) {
const span<uint8_t> bin = tokenizer->GetBinary();
*value = Binary::fromSpan(bin.data(), bin.size());
*value = Binary::fromSpan(tokenizer->GetBinary());
return true;
}
if (tokenizer->TokenTag() == cbor::CBORTokenTag::STRING8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <string_view>
#include <vector>

#include "base/containers/span.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/values.h"
Expand Down Expand Up @@ -112,7 +113,7 @@ class CRDTP_EXPORT Binary : public Serializable {
static Binary fromRefCounted(scoped_refptr<base::RefCountedMemory> memory);
static Binary fromVector(std::vector<uint8_t> data);
static Binary fromString(std::string data);
static Binary fromSpan(const uint8_t* data, size_t size);
static Binary fromSpan(base::span<const uint8_t> data);

private:
explicit Binary(scoped_refptr<base::RefCountedMemory> bytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ TEST(ProtocolTraits, BinaryBasic) {
constexpr uint8_t data[] = {'H', 'e', 'l', 'l', 'o', ',', 0,
'w', 'o', 'r', 'l', 'd', '!', 0x80};
const std::vector<uint8_t> data_vec(std::cbegin(data), std::cend(data));
Binary binary = Binary::fromSpan(data, sizeof data);
Binary binary = Binary::fromSpan(data);
EXPECT_THAT(binary.toBase64(), Eq("SGVsbG8sAHdvcmxkIYA="));
EXPECT_THAT(MakeVector(binary), Eq(data_vec));
EXPECT_THAT(MakeVector(Binary::fromVector(data_vec)), Eq(data_vec));
Expand All @@ -74,7 +74,7 @@ TEST(ProtocolTraits, BinaryBasic) {
TEST(ProtocolTraits, BinarySerialization) {
constexpr uint8_t data[] = {'H', 'e', 'l', 'l', 'o', ',', 0,
'w', 'o', 'r', 'l', 'd', '!', 0x80};
Binary binary = Binary::fromSpan(data, sizeof data);
Binary binary = Binary::fromSpan(data);

EXPECT_THAT(MakeVector(RoundTrip(binary)), Eq(MakeVector(binary)));
}
Expand Down
3 changes: 1 addition & 2 deletions third_party/inspector_protocol/lib/Values_cpp.template
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ class ValueParserHandler : public ParserHandler {
}

void HandleBinary(span<uint8_t> bytes) override {
AddValueToParent(
BinaryValue::create(Binary::fromSpan(bytes.data(), bytes.size())));
AddValueToParent(BinaryValue::create(Binary::fromSpan(bytes)));
}

void HandleDouble(double value) override {
Expand Down

0 comments on commit a4c24b9

Please sign in to comment.