refactor: spanify mojo Read/WriteData methods

Ref: https://chromium-review.googlesource.com/c/chromium/src/+/5619291
This commit is contained in:
Samuel Attard
2024-06-27 12:59:34 -07:00
parent ba573b1101
commit d94601a13e
3 changed files with 19 additions and 13 deletions

View File

@@ -69,7 +69,7 @@ class DataPipeReader {
return;
}
buffer_.resize(size);
head_ = &buffer_.front();
head_offset_ = 0;
remaining_size_ = size;
handle_watcher_.ArmOrNotify();
}
@@ -84,10 +84,12 @@ class DataPipeReader {
// Read.
size_t length = remaining_size_;
result = data_pipe_->ReadData(head_, &length, MOJO_READ_DATA_FLAG_NONE);
result = data_pipe_->ReadData(
MOJO_READ_DATA_FLAG_NONE,
base::as_writable_byte_span(buffer_).subspan(head_offset_), length);
if (result == MOJO_RESULT_OK) { // success
remaining_size_ -= length;
head_ += length;
head_offset_ += length;
if (remaining_size_ == 0) {
OnSuccess();
} else {
@@ -130,7 +132,7 @@ class DataPipeReader {
std::vector<char> buffer_;
// The head of buffer.
raw_ptr<char, AllowPtrArithmetic> head_ = nullptr;
size_t head_offset_ = 0;
// Remaining data to read.
uint64_t remaining_size_ = 0;

View File

@@ -221,13 +221,15 @@ class AsarURLLoader : public network::mojom::URLLoader {
// Write any data we read for MIME sniffing, constraining by range where
// applicable. This will always fit in the pipe (see assertion near
// |kDefaultFileUrlPipeSize| definition).
size_t write_size = std::min(
const size_t write_size = std::min(
(read_result.bytes_read - first_byte_to_send), total_bytes_to_send);
const size_t expected_write_size = write_size;
MojoResult result =
producer_handle->WriteData(&initial_read_buffer[first_byte_to_send],
&write_size, MOJO_WRITE_DATA_FLAG_NONE);
if (result != MOJO_RESULT_OK || write_size != expected_write_size) {
base::span<const uint8_t> bytes =
base::as_byte_span(initial_read_buffer)
.subspan(first_byte_to_send, write_size);
size_t bytes_written = 0;
MojoResult result = producer_handle->WriteData(
bytes, MOJO_WRITE_DATA_FLAG_NONE, bytes_written);
if (result != MOJO_RESULT_OK || write_size != bytes_written) {
OnFileWritten(result);
return;
}

View File

@@ -365,9 +365,11 @@ class ChunkedDataPipeReadableStream
if (size_ && num_bytes > *size_ - bytes_read_)
num_bytes = *size_ - bytes_read_;
MojoResult rv = data_pipe_->ReadData(
static_cast<void*>(static_cast<char*>(buf->Buffer()->Data()) +
buf->ByteOffset()),
&num_bytes, MOJO_READ_DATA_FLAG_NONE);
MOJO_READ_DATA_FLAG_NONE,
base::span(static_cast<uint8_t*>(buf->Buffer()->Data()),
buf->ByteLength())
.subspan(buf->ByteOffset()),
num_bytes);
if (rv == MOJO_RESULT_OK) {
bytes_read_ += num_bytes;
// Not needed for correctness, but this allows the consumer to send the