Commit 76312568 authored by Florin Malita's avatar Florin Malita Committed by Commit Bot

Consolidate SharedBuffer::{GetAsBytes,GetPartAsBytes}

All GetPartAsBytes() callers request data from the beginning of the buffer,
so the method doesn't need a position argument.  Then it essentially turns
into GetAsBytes(), and we can consolidate the two.

(the hidden motivation here is to remove SharedBuffer random-access
semantics, to facilitate future refactoring).

BUG=728627

Change-Id: I63f9ff2f87a01cc4980d38526531dc9118dd87c8
Reviewed-on: https://chromium-review.googlesource.com/519682Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarLeon Scroggins <scroggo@chromium.org>
Reviewed-by: default avatarHiroshige Hayashizaki <hiroshige@chromium.org>
Commit-Queue: Florin Malita <fmalita@chromium.org>
Cr-Commit-Position: refs/heads/master@{#476304}
parent b8da9c95
......@@ -420,8 +420,8 @@ void ScriptStreamer::NotifyAppendData(ScriptResource* resource) {
// understanding of the data encoding.
constexpr size_t kMaximumLengthOfBOM = 4;
char maybe_bom[kMaximumLengthOfBOM] = {};
if (!resource->ResourceBuffer()->GetPartAsBytes(
maybe_bom, static_cast<size_t>(0), kMaximumLengthOfBOM)) {
if (!resource->ResourceBuffer()->GetBytes(maybe_bom,
kMaximumLengthOfBOM)) {
NOTREACHED();
return;
}
......
......@@ -394,8 +394,9 @@ DOMArrayBuffer* XMLHttpRequest::ResponseArrayBuffer() {
DOMArrayBuffer* buffer = DOMArrayBuffer::CreateUninitializedOrNull(
binary_response_builder_->size(), 1);
if (buffer) {
binary_response_builder_->GetAsBytes(
bool result = binary_response_builder_->GetBytes(
buffer->Data(), static_cast<size_t>(buffer->ByteLength()));
DCHECK(result);
response_array_buffer_ = buffer;
}
// https://xhr.spec.whatwg.org/#arraybuffer-response allows clearing
......
......@@ -157,10 +157,9 @@ bool IDBValueUnwrapper::IsWrapped(IDBValue* value) {
DCHECK(value);
uint8_t header[3];
if (!value->data_ || value->data_->size() < sizeof(header))
if (!value->data_ || !value->data_->GetBytes(header, sizeof(header)))
return false;
value->data_->GetPartAsBytes(header, static_cast<size_t>(0), sizeof(header));
return header[0] == kVersionTag &&
header[1] == kRequiresProcessingSSVPseudoVersion &&
header[2] == kBlobWrappedValue;
......
......@@ -34,8 +34,8 @@ TEST(IDBValueUnwrapperTest, IsWrapped) {
EXPECT_TRUE(IDBValueUnwrapper::IsWrapped(wrapped_value.Get()));
Vector<char> wrapped_marker_bytes(wrapped_marker_buffer->size());
wrapped_marker_buffer->GetAsBytes(wrapped_marker_bytes.data(),
wrapped_marker_bytes.size());
ASSERT_TRUE(wrapped_marker_buffer->GetBytes(wrapped_marker_bytes.data(),
wrapped_marker_bytes.size()));
// IsWrapped() looks at the first 3 bytes in the value's byte array.
// Truncating the array to fewer than 3 bytes should cause IsWrapped() to
......
......@@ -204,13 +204,12 @@ size_t SharedBuffer::GetSomeDataInternal(const char*& some_data,
return 0;
}
bool SharedBuffer::GetAsBytesInternal(void* dest,
size_t load_position,
size_t byte_length) const {
bool SharedBuffer::GetBytesInternal(void* dest, size_t byte_length) const {
if (!dest)
return false;
const char* segment = nullptr;
size_t load_position = 0;
size_t write_position = 0;
while (byte_length > 0) {
size_t load_size = GetSomeDataInternal(segment, load_position);
......
......@@ -112,26 +112,14 @@ class PLATFORM_EXPORT SharedBuffer : public RefCounted<SharedBuffer> {
return GetSomeDataInternal(data, position);
}
// Returns the content data into "dest" as a flat buffer. "byteLength" must
// exactly match with size(). |dest| must not be null even if |bytesLength|
// is 0.
// Copies |byteLength| bytes from the beginning of the content data into
// |dest| as a flat buffer. Returns true on success, otherwise the content of
// |dest| is not guaranteed.
HAS_STRICTLY_TYPED_ARG
void GetAsBytes(void* dest, STRICTLY_TYPED_ARG(byte_length)) const {
WARN_UNUSED_RESULT
bool GetBytes(void* dest, STRICTLY_TYPED_ARG(byte_length)) const {
STRICT_ARG_TYPE(size_t);
DCHECK_EQ(byte_length, size());
auto result = GetAsBytesInternal(dest, 0, byte_length);
DCHECK(result);
}
// Copies "byteLength" bytes from "position"-th bytes (0 origin) of the
// content data into "dest" as a flat buffer, Returns true on success,
// otherwise the content of "dest" is not guaranteed.
HAS_STRICTLY_TYPED_ARG
bool GetPartAsBytes(void* dest,
STRICTLY_TYPED_ARG(position),
STRICTLY_TYPED_ARG(byte_length)) const {
STRICT_ARG_TYPE(size_t);
return GetAsBytesInternal(dest, position, byte_length);
return GetBytesInternal(dest, byte_length);
}
// Creates an SkData and copies this SharedBuffer's contents to that
......@@ -150,7 +138,7 @@ class PLATFORM_EXPORT SharedBuffer : public RefCounted<SharedBuffer> {
void MergeSegmentsIntoBuffer() const;
void AppendInternal(const char* data, size_t);
bool GetAsBytesInternal(void* dest, size_t, size_t) const;
bool GetBytesInternal(void* dest, size_t) const;
size_t GetSomeDataInternal(const char*& data, size_t position) const;
size_t size_;
......
......@@ -52,7 +52,7 @@ TEST(SharedBufferTest, getAsBytes) {
const size_t size = shared_buffer->size();
std::unique_ptr<char[]> data = WrapArrayUnique(new char[size]);
shared_buffer->GetAsBytes(data.get(), size);
ASSERT_TRUE(shared_buffer->GetBytes(data.get(), size));
char expected_concatenation[] = "HelloWorldGoodbye";
ASSERT_EQ(strlen(expected_concatenation), size);
......@@ -71,16 +71,14 @@ TEST(SharedBufferTest, getPartAsBytes) {
shared_buffer->Append(test_data2, strlen(test_data2));
struct TestData {
size_t position;
size_t size;
const char* expected;
} test_data[] = {
{0, 17, "HelloWorldGoodbye"}, {0, 7, "HelloWo"}, {4, 7, "oWorldG"},
{17, "HelloWorldGoodbye"}, {7, "HelloWo"}, {3, "Hel"},
};
for (TestData& test : test_data) {
std::unique_ptr<char[]> data = WrapArrayUnique(new char[test.size]);
ASSERT_TRUE(
shared_buffer->GetPartAsBytes(data.get(), test.position, test.size));
ASSERT_TRUE(shared_buffer->GetBytes(data.get(), test.size));
EXPECT_EQ(0, memcmp(test.expected, data.get(), test.size));
}
}
......@@ -102,7 +100,7 @@ TEST(SharedBufferTest, getAsBytesLargeSegments) {
const size_t size = shared_buffer->size();
std::unique_ptr<char[]> data = WrapArrayUnique(new char[size]);
shared_buffer->GetAsBytes(data.get(), size);
ASSERT_TRUE(shared_buffer->GetBytes(data.get(), size));
ASSERT_EQ(0x4000U + 0x4000U + 0x4000U, size);
int position = 0;
......
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