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 int encoded_length() const override { return length_; } |
| 30 |
| 31 private: |
| 32 const char* const payload_; |
| 33 const uint32_t length_; |
| 34 |
| 35 scoped_refptr<URLResponseBodyConsumer> consumer_; |
| 36 |
| 37 DISALLOW_COPY_AND_ASSIGN(ReceivedData); |
| 38 }; |
| 39 |
| 40 URLResponseBodyConsumer::URLResponseBodyConsumer( |
| 41 int request_id, |
| 42 ResourceDispatcher* resource_dispatcher, |
| 43 mojo::ScopedDataPipeConsumerHandle handle) |
| 44 : request_id_(request_id), |
| 45 resource_dispatcher_(resource_dispatcher), |
| 46 handle_(std::move(handle)), |
| 47 has_seen_end_of_data_(!handle_.is_valid()) { |
| 48 StartWatching(); |
| 49 } |
| 50 |
| 51 URLResponseBodyConsumer::~URLResponseBodyConsumer() {} |
| 52 |
| 53 void URLResponseBodyConsumer::OnComplete( |
| 54 const ResourceRequestCompletionStatus& status) { |
| 55 if (has_been_cancelled_) |
| 56 return; |
| 57 has_received_completion_ = true; |
| 58 completion_status_ = status; |
| 59 NotifyCompletionIfAppropriate(); |
| 60 } |
| 61 |
| 62 void URLResponseBodyConsumer::Cancel() { |
| 63 has_been_cancelled_ = true; |
| 64 handle_watcher_.Stop(); |
| 65 } |
| 66 |
| 67 void URLResponseBodyConsumer::Reclaim(uint32_t size) { |
| 68 MojoResult result = mojo::EndReadDataRaw(handle_.get(), size); |
| 69 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 70 StartWatching(); |
| 71 } |
| 72 |
| 73 void URLResponseBodyConsumer::OnReadable(MojoResult unused) { |
| 74 DCHECK(!has_been_cancelled_); |
| 75 |
| 76 // TODO(yhirano): Suppress notification when deferred. |
| 77 // TODO(yhirano): Run this operation on the loading task runner. |
| 78 const void* buffer = nullptr; |
| 79 uint32_t available = 0; |
| 80 MojoResult result = mojo::BeginReadDataRaw(handle_.get(), &buffer, &available, |
| 81 MOJO_READ_DATA_FLAG_NONE); |
| 82 if (result == MOJO_RESULT_OK) { |
| 83 ResourceDispatcher::PendingRequestInfo* request_info = |
| 84 resource_dispatcher_->GetPendingRequestInfo(request_id_); |
| 85 DCHECK(request_info); |
| 86 request_info->peer->OnReceivedData(base::WrapUnique( |
| 87 new ReceivedData(static_cast<const char*>(buffer), available, this))); |
| 88 // |this| may be deleted. |
| 89 return; |
| 90 } |
| 91 if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
| 92 has_seen_end_of_data_ = true; |
| 93 NotifyCompletionIfAppropriate(); |
| 94 // |this| may be deleted. |
| 95 return; |
| 96 } |
| 97 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 98 StartWatching(); |
| 99 return; |
| 100 } |
| 101 completion_status_.error_code = net::ERR_FAILED; |
| 102 has_seen_end_of_data_ = true; |
| 103 has_received_completion_ = true; |
| 104 NotifyCompletionIfAppropriate(); |
| 105 // |this| may be deleted. |
| 106 } |
| 107 |
| 108 void URLResponseBodyConsumer::StartWatching() { |
| 109 if (has_been_cancelled_ || has_seen_end_of_data_) |
| 110 return; |
| 111 handle_watcher_.Start( |
| 112 handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, MOJO_DEADLINE_INDEFINITE, |
| 113 base::Bind(&URLResponseBodyConsumer::OnReadable, base::Unretained(this))); |
| 114 } |
| 115 |
| 116 void URLResponseBodyConsumer::NotifyCompletionIfAppropriate() { |
| 117 if (has_been_cancelled_) |
| 118 return; |
| 119 if (has_received_completion_ && has_seen_end_of_data_) { |
| 120 // Cancel this instance in order not to notify twice. |
| 121 Cancel(); |
| 122 |
| 123 resource_dispatcher_->OnMessageReceived( |
| 124 ResourceMsg_RequestComplete(request_id_, completion_status_)); |
| 125 // |this| may be deleted. |
| 126 } |
| 127 } |
| 128 |
| 129 } // namespace content |
OLD | NEW |