Commit c25c2041 authored by Arthur Sonzogni's avatar Arthur Sonzogni Committed by Commit Bot

Simplify BlobURLLoader and always send OnStartLoadingResponseBody.

This is a sort of prerequisite for:
https://chromium-review.googlesource.com/c/chromium/src/+/1172290

The main goal is after sending:
  URLLoaderClient::OnReceiveResponse(response_header)
to ALWAYS send:
  URLLoaderClient::OnStartLoadingResponseBody(response_body)

Some URLLoader, when the response's body body is empty, don't even try to send
the response_body data pipe. In the URLLoaderClient, not having to handle the
case with no data pipe would be a nice simplification.

What this CL does:
1) In BlobUrlLoader, create the data pipe at ::Start() time.
2) Pass the producer handle to the MojoBlobCreate at construction time.
3) Pass the consumer handle to OnStartLoadingResponseBody(), immediately
   after OnReceiveResponse().

Some code simplification was possible. There is no more need to
implement PassDataPipe() in BlobURLLoader, ReaderDelegate and
DataPipeReaderDelegate. There is no more need to give them the pipe at
construction time neither.

Bug: 826868, 831155
Change-Id: I827cb39bdd782624ff362874e98be90cab0d47f3
Reviewed-on: https://chromium-review.googlesource.com/c/1329741Reviewed-by: default avatarMarijn Kruisselbrink <mek@chromium.org>
Commit-Queue: Arthur Sonzogni <arthursonzogni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607266}
parent 500b4b17
...@@ -21,13 +21,8 @@ namespace { ...@@ -21,13 +21,8 @@ namespace {
class ReaderDelegate : public MojoBlobReader::Delegate { class ReaderDelegate : public MojoBlobReader::Delegate {
public: public:
ReaderDelegate(mojo::ScopedDataPipeProducerHandle handle, ReaderDelegate(blink::mojom::BlobReaderClientPtr client)
blink::mojom::BlobReaderClientPtr client) : client_(std::move(client)) {}
: handle_(std::move(handle)), client_(std::move(client)) {}
mojo::ScopedDataPipeProducerHandle PassDataPipe() override {
return std::move(handle_);
}
MojoBlobReader::Delegate::RequestSideData DidCalculateSize( MojoBlobReader::Delegate::RequestSideData DidCalculateSize(
uint64_t total_size, uint64_t total_size,
...@@ -43,7 +38,6 @@ class ReaderDelegate : public MojoBlobReader::Delegate { ...@@ -43,7 +38,6 @@ class ReaderDelegate : public MojoBlobReader::Delegate {
} }
private: private:
mojo::ScopedDataPipeProducerHandle handle_;
blink::mojom::BlobReaderClientPtr client_; blink::mojom::BlobReaderClientPtr client_;
DISALLOW_COPY_AND_ASSIGN(ReaderDelegate); DISALLOW_COPY_AND_ASSIGN(ReaderDelegate);
...@@ -52,13 +46,8 @@ class ReaderDelegate : public MojoBlobReader::Delegate { ...@@ -52,13 +46,8 @@ class ReaderDelegate : public MojoBlobReader::Delegate {
class DataPipeGetterReaderDelegate : public MojoBlobReader::Delegate { class DataPipeGetterReaderDelegate : public MojoBlobReader::Delegate {
public: public:
DataPipeGetterReaderDelegate( DataPipeGetterReaderDelegate(
mojo::ScopedDataPipeProducerHandle handle,
network::mojom::DataPipeGetter::ReadCallback callback) network::mojom::DataPipeGetter::ReadCallback callback)
: handle_(std::move(handle)), callback_(std::move(callback)) {} : callback_(std::move(callback)) {}
mojo::ScopedDataPipeProducerHandle PassDataPipe() override {
return std::move(handle_);
}
MojoBlobReader::Delegate::RequestSideData DidCalculateSize( MojoBlobReader::Delegate::RequestSideData DidCalculateSize(
uint64_t total_size, uint64_t total_size,
...@@ -82,7 +71,6 @@ class DataPipeGetterReaderDelegate : public MojoBlobReader::Delegate { ...@@ -82,7 +71,6 @@ class DataPipeGetterReaderDelegate : public MojoBlobReader::Delegate {
} }
private: private:
mojo::ScopedDataPipeProducerHandle handle_;
network::mojom::DataPipeGetter::ReadCallback callback_; network::mojom::DataPipeGetter::ReadCallback callback_;
DISALLOW_COPY_AND_ASSIGN(DataPipeGetterReaderDelegate); DISALLOW_COPY_AND_ASSIGN(DataPipeGetterReaderDelegate);
...@@ -114,14 +102,14 @@ void BlobImpl::ReadRange(uint64_t offset, ...@@ -114,14 +102,14 @@ void BlobImpl::ReadRange(uint64_t offset,
(length == std::numeric_limits<uint64_t>::max()) (length == std::numeric_limits<uint64_t>::max())
? net::HttpByteRange::RightUnbounded(offset) ? net::HttpByteRange::RightUnbounded(offset)
: net::HttpByteRange::Bounded(offset, offset + length - 1), : net::HttpByteRange::Bounded(offset, offset + length - 1),
std::make_unique<ReaderDelegate>(std::move(handle), std::move(client))); std::make_unique<ReaderDelegate>(std::move(client)), std::move(handle));
} }
void BlobImpl::ReadAll(mojo::ScopedDataPipeProducerHandle handle, void BlobImpl::ReadAll(mojo::ScopedDataPipeProducerHandle handle,
blink::mojom::BlobReaderClientPtr client) { blink::mojom::BlobReaderClientPtr client) {
MojoBlobReader::Create( MojoBlobReader::Create(handle_.get(), net::HttpByteRange(),
handle_.get(), net::HttpByteRange(), std::make_unique<ReaderDelegate>(std::move(client)),
std::make_unique<ReaderDelegate>(std::move(handle), std::move(client))); std::move(handle));
} }
void BlobImpl::ReadSideData(ReadSideDataCallback callback) { void BlobImpl::ReadSideData(ReadSideDataCallback callback) {
...@@ -187,11 +175,12 @@ void BlobImpl::Clone(network::mojom::DataPipeGetterRequest request) { ...@@ -187,11 +175,12 @@ void BlobImpl::Clone(network::mojom::DataPipeGetterRequest request) {
data_pipe_getter_bindings_.AddBinding(this, std::move(request)); data_pipe_getter_bindings_.AddBinding(this, std::move(request));
} }
void BlobImpl::Read(mojo::ScopedDataPipeProducerHandle pipe, void BlobImpl::Read(mojo::ScopedDataPipeProducerHandle handle,
ReadCallback callback) { ReadCallback callback) {
MojoBlobReader::Create(handle_.get(), net::HttpByteRange(), MojoBlobReader::Create(
std::make_unique<DataPipeGetterReaderDelegate>( handle_.get(), net::HttpByteRange(),
std::move(pipe), std::move(callback))); std::make_unique<DataPipeGetterReaderDelegate>(std::move(callback)),
std::move(handle));
} }
void BlobImpl::FlushForTesting() { void BlobImpl::FlushForTesting() {
......
...@@ -88,8 +88,12 @@ void BlobURLLoader::Start(const network::ResourceRequest& request) { ...@@ -88,8 +88,12 @@ void BlobURLLoader::Start(const network::ResourceRequest& request) {
} }
} }
mojo::DataPipe data_pipe(kDefaultAllocationSize);
response_body_consumer_handle_ = std::move(data_pipe.consumer_handle);
MojoBlobReader::Create(blob_handle_.get(), byte_range_, MojoBlobReader::Create(blob_handle_.get(), byte_range_,
base::WrapUnique(this)); base::WrapUnique(this),
std::move(data_pipe.producer_handle));
} }
void BlobURLLoader::FollowRedirect( void BlobURLLoader::FollowRedirect(
...@@ -99,12 +103,6 @@ void BlobURLLoader::FollowRedirect( ...@@ -99,12 +103,6 @@ void BlobURLLoader::FollowRedirect(
NOTREACHED(); NOTREACHED();
} }
mojo::ScopedDataPipeProducerHandle BlobURLLoader::PassDataPipe() {
mojo::DataPipe data_pipe(kDefaultAllocationSize);
response_body_consumer_handle_ = std::move(data_pipe.consumer_handle);
return std::move(data_pipe.producer_handle);
}
MojoBlobReader::Delegate::RequestSideData BlobURLLoader::DidCalculateSize( MojoBlobReader::Delegate::RequestSideData BlobURLLoader::DidCalculateSize(
uint64_t total_size, uint64_t total_size,
uint64_t content_size) { uint64_t content_size) {
...@@ -130,14 +128,6 @@ void BlobURLLoader::DidReadSideData(net::IOBufferWithSize* data) { ...@@ -130,14 +128,6 @@ void BlobURLLoader::DidReadSideData(net::IOBufferWithSize* data) {
HeadersCompleted(net::HTTP_OK, total_size_, data); HeadersCompleted(net::HTTP_OK, total_size_, data);
} }
void BlobURLLoader::DidRead(int num_bytes) {
if (response_body_consumer_handle_.is_valid()) {
// Send the data pipe on the first OnReadCompleted call.
client_->OnStartLoadingResponseBody(
std::move(response_body_consumer_handle_));
}
}
void BlobURLLoader::OnComplete(net::Error error_code, void BlobURLLoader::OnComplete(net::Error error_code,
uint64_t total_written_bytes) { uint64_t total_written_bytes) {
network::URLLoaderCompletionStatus status(error_code); network::URLLoaderCompletionStatus status(error_code);
...@@ -172,6 +162,9 @@ void BlobURLLoader::HeadersCompleted(net::HttpStatusCode status_code, ...@@ -172,6 +162,9 @@ void BlobURLLoader::HeadersCompleted(net::HttpStatusCode status_code,
client_->OnReceiveCachedMetadata( client_->OnReceiveCachedMetadata(
std::vector<uint8_t>(data, data + metadata->size())); std::vector<uint8_t>(data, data + metadata->size()));
} }
client_->OnStartLoadingResponseBody(
std::move(response_body_consumer_handle_));
} }
} // namespace storage } // namespace storage
...@@ -50,11 +50,9 @@ class STORAGE_EXPORT BlobURLLoader : public storage::MojoBlobReader::Delegate, ...@@ -50,11 +50,9 @@ class STORAGE_EXPORT BlobURLLoader : public storage::MojoBlobReader::Delegate,
void ResumeReadingBodyFromNet() override {} void ResumeReadingBodyFromNet() override {}
// storage::MojoBlobReader::Delegate implementation: // storage::MojoBlobReader::Delegate implementation:
mojo::ScopedDataPipeProducerHandle PassDataPipe() override;
RequestSideData DidCalculateSize(uint64_t total_size, RequestSideData DidCalculateSize(uint64_t total_size,
uint64_t content_size) override; uint64_t content_size) override;
void DidReadSideData(net::IOBufferWithSize* data) override; void DidReadSideData(net::IOBufferWithSize* data) override;
void DidRead(int num_bytes) override;
void OnComplete(net::Error error_code, uint64_t total_written_bytes) override; void OnComplete(net::Error error_code, uint64_t total_written_bytes) override;
void HeadersCompleted(net::HttpStatusCode status_code, void HeadersCompleted(net::HttpStatusCode status_code,
......
...@@ -13,18 +13,24 @@ ...@@ -13,18 +13,24 @@
namespace storage { namespace storage {
// static // static
void MojoBlobReader::Create(const BlobDataHandle* handle, void MojoBlobReader::Create(
const net::HttpByteRange& range, const BlobDataHandle* handle,
std::unique_ptr<Delegate> delegate) { const net::HttpByteRange& range,
new MojoBlobReader(handle, range, std::move(delegate)); std::unique_ptr<Delegate> delegate,
mojo::ScopedDataPipeProducerHandle response_body_stream) {
new MojoBlobReader(handle, range, std::move(delegate),
std::move(response_body_stream));
} }
MojoBlobReader::MojoBlobReader(const BlobDataHandle* handle, MojoBlobReader::MojoBlobReader(
const net::HttpByteRange& range, const BlobDataHandle* handle,
std::unique_ptr<Delegate> delegate) const net::HttpByteRange& range,
std::unique_ptr<Delegate> delegate,
mojo::ScopedDataPipeProducerHandle response_body_stream)
: delegate_(std::move(delegate)), : delegate_(std::move(delegate)),
byte_range_(range), byte_range_(range),
blob_reader_(handle->CreateReader()), blob_reader_(handle->CreateReader()),
response_body_stream_(std::move(response_body_stream)),
writable_handle_watcher_(FROM_HERE, writable_handle_watcher_(FROM_HERE,
mojo::SimpleWatcher::ArmingPolicy::MANUAL, mojo::SimpleWatcher::ArmingPolicy::MANUAL,
base::SequencedTaskRunnerHandle::Get()), base::SequencedTaskRunnerHandle::Get()),
...@@ -82,9 +88,12 @@ void MojoBlobReader::NotifyCompletedAndDeleteIfNeeded(int result) { ...@@ -82,9 +88,12 @@ void MojoBlobReader::NotifyCompletedAndDeleteIfNeeded(int result) {
notified_completed_ = true; notified_completed_ = true;
} }
bool has_data_pipe = pending_write_ || response_body_stream_.is_valid(); // If data are being written, wait for it to complete.
if (!has_data_pipe) if (writable_handle_watcher_.IsWatching() &&
delete this; (pending_write_ || response_body_stream_.is_valid()))
return;
delete this;
} }
void MojoBlobReader::DidCalculateSize(int result) { void MojoBlobReader::DidCalculateSize(int result) {
...@@ -147,9 +156,7 @@ void MojoBlobReader::DidReadSideData(BlobReader::Status status) { ...@@ -147,9 +156,7 @@ void MojoBlobReader::DidReadSideData(BlobReader::Status status) {
void MojoBlobReader::StartReading() { void MojoBlobReader::StartReading() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
DCHECK(!response_body_stream_);
response_body_stream_ = delegate_->PassDataPipe();
peer_closed_handle_watcher_.Watch( peer_closed_handle_watcher_.Watch(
response_body_stream_.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED, response_body_stream_.get(), MOJO_HANDLE_SIGNAL_PEER_CLOSED,
MOJO_WATCH_CONDITION_SATISFIED, MOJO_WATCH_CONDITION_SATISFIED,
......
...@@ -58,11 +58,6 @@ class STORAGE_EXPORT MojoBlobReader { ...@@ -58,11 +58,6 @@ class STORAGE_EXPORT MojoBlobReader {
// data this method is called with null. // data this method is called with null.
virtual void DidReadSideData(net::IOBufferWithSize* data) {} virtual void DidReadSideData(net::IOBufferWithSize* data) {}
// Called when the MojoBlobReader actually starts reading data from the
// blob. Should return a data pipe to which all the data read from the blob
// should be written.
virtual mojo::ScopedDataPipeProducerHandle PassDataPipe() = 0;
// Called whenever some amount of data is read from the blob and about to be // Called whenever some amount of data is read from the blob and about to be
// written to the data pipe. // written to the data pipe.
virtual void DidRead(int num_bytes) {} virtual void DidRead(int num_bytes) {}
...@@ -80,12 +75,14 @@ class STORAGE_EXPORT MojoBlobReader { ...@@ -80,12 +75,14 @@ class STORAGE_EXPORT MojoBlobReader {
static void Create(const BlobDataHandle* handle, static void Create(const BlobDataHandle* handle,
const net::HttpByteRange& range, const net::HttpByteRange& range,
std::unique_ptr<Delegate> delegate); std::unique_ptr<Delegate> delegate,
mojo::ScopedDataPipeProducerHandle response_body_stream);
private: private:
MojoBlobReader(const BlobDataHandle* handle, MojoBlobReader(const BlobDataHandle* handle,
const net::HttpByteRange& range, const net::HttpByteRange& range,
std::unique_ptr<Delegate> delegate); std::unique_ptr<Delegate> delegate,
mojo::ScopedDataPipeProducerHandle response_body_stream);
~MojoBlobReader(); ~MojoBlobReader();
void Start(); void Start();
......
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