OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/child/url_response_body_consumer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "content/child/resource_dispatcher.h" |
| 11 #include "content/common/resource_messages.h" |
| 12 #include "content/common/resource_request_completion_status.h" |
| 13 #include "content/public/child/request_peer.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 class URLResponseBodyConsumer::ReceivedData final |
| 18 : public RequestPeer::ReceivedData { |
| 19 public: |
| 20 ReceivedData(const char* payload, |
| 21 int length, |
| 22 scoped_refptr<URLResponseBodyConsumer> consumer) |
| 23 : payload_(payload), length_(length), consumer_(consumer) {} |
| 24 |
| 25 ~ReceivedData() override { consumer_->Reclaim(length_); } |
| 26 |
| 27 const char* payload() const override { return payload_; } |
| 28 int length() const override { return length_; } |
| 29 // TODO(yhirano): These return incorrect values. Remove these from |
| 30 // ReceivedData before enabling Mojo-Loading. |
| 31 int encoded_data_length() const override { return length_; } |
| 32 int encoded_body_length() const override { return length_; } |
| 33 |
| 34 private: |
| 35 const char* const payload_; |
| 36 const uint32_t length_; |
| 37 |
| 38 scoped_refptr<URLResponseBodyConsumer> consumer_; |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(ReceivedData); |
| 41 }; |
| 42 |
| 43 URLResponseBodyConsumer::URLResponseBodyConsumer( |
| 44 int request_id, |
| 45 ResourceDispatcher* resource_dispatcher, |
| 46 mojo::ScopedDataPipeConsumerHandle handle, |
| 47 base::SingleThreadTaskRunner* task_runner) |
| 48 : request_id_(request_id), |
| 49 resource_dispatcher_(resource_dispatcher), |
| 50 handle_(std::move(handle)), |
| 51 has_seen_end_of_data_(!handle_.is_valid()) { |
| 52 handle_watcher_.Start( |
| 53 handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 54 base::Bind(&URLResponseBodyConsumer::OnReadable, base::Unretained(this))); |
| 55 task_runner->PostTask( |
| 56 FROM_HERE, base::Bind(&URLResponseBodyConsumer::OnReadable, AsWeakPtr(), |
| 57 MOJO_RESULT_OK)); |
| 58 } |
| 59 |
| 60 URLResponseBodyConsumer::~URLResponseBodyConsumer() {} |
| 61 |
| 62 void URLResponseBodyConsumer::OnComplete( |
| 63 const ResourceRequestCompletionStatus& status) { |
| 64 if (has_been_cancelled_) |
| 65 return; |
| 66 has_received_completion_ = true; |
| 67 completion_status_ = status; |
| 68 NotifyCompletionIfAppropriate(); |
| 69 } |
| 70 |
| 71 void URLResponseBodyConsumer::Cancel() { |
| 72 has_been_cancelled_ = true; |
| 73 handle_watcher_.Cancel(); |
| 74 } |
| 75 |
| 76 void URLResponseBodyConsumer::Reclaim(uint32_t size) { |
| 77 MojoResult result = mojo::EndReadDataRaw(handle_.get(), size); |
| 78 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 79 } |
| 80 |
| 81 void URLResponseBodyConsumer::OnReadable(MojoResult unused) { |
| 82 if (has_been_cancelled_ || has_seen_end_of_data_) |
| 83 return; |
| 84 |
| 85 // TODO(yhirano): Suppress notification when deferred. |
| 86 // TODO(yhirano): Run this operation on the loading task runner. |
| 87 while (!has_been_cancelled_) { |
| 88 const void* buffer = nullptr; |
| 89 uint32_t available = 0; |
| 90 MojoResult result = mojo::BeginReadDataRaw( |
| 91 handle_.get(), &buffer, &available, MOJO_READ_DATA_FLAG_NONE); |
| 92 if (result == MOJO_RESULT_SHOULD_WAIT) |
| 93 return; |
| 94 if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
| 95 has_seen_end_of_data_ = true; |
| 96 NotifyCompletionIfAppropriate(); |
| 97 return; |
| 98 } |
| 99 if (result != MOJO_RESULT_OK) { |
| 100 completion_status_.error_code = net::ERR_FAILED; |
| 101 has_seen_end_of_data_ = true; |
| 102 has_received_completion_ = true; |
| 103 NotifyCompletionIfAppropriate(); |
| 104 return; |
| 105 } |
| 106 ResourceDispatcher::PendingRequestInfo* request_info = |
| 107 resource_dispatcher_->GetPendingRequestInfo(request_id_); |
| 108 DCHECK(request_info); |
| 109 request_info->peer->OnReceivedData(base::WrapUnique( |
| 110 new ReceivedData(static_cast<const char*>(buffer), available, this))); |
| 111 } |
| 112 } |
| 113 |
| 114 void URLResponseBodyConsumer::NotifyCompletionIfAppropriate() { |
| 115 if (has_been_cancelled_) |
| 116 return; |
| 117 if (!has_received_completion_ || !has_seen_end_of_data_) |
| 118 return; |
| 119 // Cancel this instance in order not to notify twice. |
| 120 Cancel(); |
| 121 |
| 122 resource_dispatcher_->OnMessageReceived( |
| 123 ResourceMsg_RequestComplete(request_id_, completion_status_)); |
| 124 // |this| may be deleted. |
| 125 } |
| 126 |
| 127 } // namespace content |
OLD | NEW |