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