Index: content/browser/loader/async_resource_handler.cc |
diff --git a/content/browser/loader/async_resource_handler.cc b/content/browser/loader/async_resource_handler.cc |
index 3b3cb0c5b926d0db247ca97f2a391ecd9ec2dc0a..c72ac5c5f20a85b39c94c29a5b5ef88b56807b2c 100644 |
--- a/content/browser/loader/async_resource_handler.cc |
+++ b/content/browser/loader/async_resource_handler.cc |
@@ -29,6 +29,7 @@ |
#include "content/public/browser/resource_dispatcher_host_delegate.h" |
#include "content/public/common/content_features.h" |
#include "content/public/common/resource_response.h" |
+#include "mojo/message_pump/handle_watcher.h" |
#include "net/base/io_buffer.h" |
#include "net/base/load_flags.h" |
#include "net/log/net_log.h" |
@@ -191,9 +192,75 @@ class DependentIOBuffer : public net::WrappedIOBuffer { |
scoped_refptr<ResourceBuffer> backing_; |
}; |
-AsyncResourceHandler::AsyncResourceHandler( |
- net::URLRequest* request, |
- ResourceDispatcherHostImpl* rdh) |
+class AsyncResourceHandler::MojoHelper final { |
mmenke
2016/05/23 17:34:53
Think this class needs some docs
yhirano
2016/05/24 06:40:02
Done.
|
+ public: |
+ explicit MojoHelper(AsyncResourceHandler* owner) : owner_(owner) {} |
+ |
+ void OnResponseStarted() { |
+ MojoCreateDataPipeOptions options; |
+ options.struct_size = sizeof(MojoCreateDataPipeOptions); |
+ options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
+ options.element_num_bytes = 1; |
+ options.capacity_num_bytes = kMaxAllocationSize; |
+ mojo::DataPipe data_pipe(options); |
+ |
+ writer_ = std::move(data_pipe.producer_handle); |
+ owner_->GetRequestInfo()->InstallBodyReader( |
+ std::move(data_pipe.consumer_handle)); |
+ } |
+ |
+ bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
+ int* buf_size, |
+ int min_size) { |
+ void* buffer = nullptr; |
+ uint32_t available = 0; |
+ MojoResult result = mojo::BeginWriteDataRaw( |
+ writer_.get(), &buffer, &available, MOJO_WRITE_DATA_FLAG_NONE); |
+ // Note that we cannot handle SHOULD_WAIT here. It should be handled in |
+ // OnReadCompleted. |
mmenke
2016/05/23 17:34:53
Avoid "we" in comments.
yhirano
2016/05/24 06:40:03
Done.
|
+ if (result == MOJO_RESULT_OK) { |
+ *buf = new net::WrappedIOBuffer(static_cast<const char*>(buffer)); |
+ *buf_size = available; |
+ return true; |
+ } |
+ return false; |
+ } |
+ |
+ bool OnReadCompleted(int bytes_read, bool* defer) { |
+ MojoResult result = mojo::EndWriteDataRaw(writer_.get(), bytes_read); |
+ if (result != MOJO_RESULT_OK) |
+ return false; |
+ void* buffer = nullptr; |
+ uint32_t available = 0; |
+ // To see if we can continue writing. |
+ result = mojo::BeginWriteDataRaw(writer_.get(), &buffer, &available, |
+ MOJO_WRITE_DATA_FLAG_NONE); |
+ if (result == MOJO_RESULT_SHOULD_WAIT || |
+ (result == MOJO_RESULT_OK && available == 0)) { |
+ *defer = owner_->did_defer_ = true; |
+ owner_->OnDefer(); |
+ handle_watcher_.Start( |
+ writer_.get(), MOJO_HANDLE_SIGNAL_WRITABLE, MOJO_DEADLINE_INDEFINITE, |
+ base::Bind(&MojoHelper::OnWritable, base::Unretained(this))); |
+ } |
+ if (result == MOJO_RESULT_OK) |
+ mojo::EndWriteDataRaw(writer_.get(), 0); |
+ return true; |
+ } |
+ |
+ private: |
+ void OnWritable(MojoResult result) { owner_->ResumeIfDeferred(); } |
+ |
+ mojo::ScopedDataPipeProducerHandle writer_; |
+ mojo::common::HandleWatcher handle_watcher_; |
+ AsyncResourceHandler* owner_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MojoHelper); |
+}; |
+ |
+AsyncResourceHandler::AsyncResourceHandler(net::URLRequest* request, |
+ ResourceDispatcherHostImpl* rdh, |
+ bool using_mojo_data_handle) |
: ResourceHandler(request), |
ResourceMessageDelegate(request), |
rdh_(rdh), |
@@ -206,7 +273,8 @@ AsyncResourceHandler::AsyncResourceHandler( |
inlining_helper_(new InliningHelper), |
last_upload_position_(0), |
waiting_for_upload_progress_ack_(false), |
- reported_transfer_size_(0) { |
+ reported_transfer_size_(0), |
+ mojo_helper_(using_mojo_data_handle ? new MojoHelper(this) : nullptr) { |
InitializeResourceBufferConstants(); |
} |
@@ -355,6 +423,9 @@ bool AsyncResourceHandler::OnResponseStarted(ResourceResponse* response, |
response->head.request_start = request()->creation_time(); |
response->head.response_start = TimeTicks::Now(); |
+ if (mojo_helper_) |
+ mojo_helper_->OnResponseStarted(); |
+ |
info->filter()->Send(new ResourceMsg_ReceivedResponse(GetRequestID(), |
response->head)); |
sent_received_response_msg_ = true; |
@@ -396,6 +467,9 @@ bool AsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
if (!CheckForSufficientResource()) |
return false; |
+ if (mojo_helper_) |
+ return mojo_helper_->OnWillRead(buf, buf_size, min_size); |
mmenke
2016/05/23 17:34:53
When using the helper, we also send the results ov
yhirano
2016/05/24 06:40:03
No.
|
+ |
// Return early if InliningHelper allocates the buffer, so that we should |
// inline the data into the IPC message without allocating SharedMemory. |
if (inlining_helper_->PrepareInlineBufferIfApplicable(buf, buf_size)) |
@@ -420,6 +494,9 @@ bool AsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
if (!bytes_read) |
return true; |
+ if (mojo_helper_) |
+ return mojo_helper_->OnReadCompleted(bytes_read, defer); |
+ |
ResourceMessageFilter* filter = GetFilter(); |
if (!filter) |
return false; |