| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/child/url_response_body_consumer.h" | 5 #include "content/child/url_response_body_consumer.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" |
| 7 #include "base/bind.h" | 8 #include "base/bind.h" |
| 8 #include "base/macros.h" | 9 #include "base/macros.h" |
| 9 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 10 #include "content/child/resource_dispatcher.h" | 11 #include "content/child/resource_dispatcher.h" |
| 11 #include "content/common/resource_messages.h" | 12 #include "content/common/resource_messages.h" |
| 12 #include "content/common/resource_request_completion_status.h" | 13 #include "content/common/resource_request_completion_status.h" |
| 13 #include "content/public/child/request_peer.h" | 14 #include "content/public/child/request_peer.h" |
| 14 | 15 |
| 15 namespace content { | 16 namespace content { |
| 16 | 17 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 38 | 39 |
| 39 URLResponseBodyConsumer::URLResponseBodyConsumer( | 40 URLResponseBodyConsumer::URLResponseBodyConsumer( |
| 40 int request_id, | 41 int request_id, |
| 41 ResourceDispatcher* resource_dispatcher, | 42 ResourceDispatcher* resource_dispatcher, |
| 42 mojo::ScopedDataPipeConsumerHandle handle, | 43 mojo::ScopedDataPipeConsumerHandle handle, |
| 43 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 44 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 44 : request_id_(request_id), | 45 : request_id_(request_id), |
| 45 resource_dispatcher_(resource_dispatcher), | 46 resource_dispatcher_(resource_dispatcher), |
| 46 handle_(std::move(handle)), | 47 handle_(std::move(handle)), |
| 47 handle_watcher_(task_runner), | 48 handle_watcher_(task_runner), |
| 49 task_runner_(task_runner), |
| 48 has_seen_end_of_data_(!handle_.is_valid()) {} | 50 has_seen_end_of_data_(!handle_.is_valid()) {} |
| 49 | 51 |
| 50 URLResponseBodyConsumer::~URLResponseBodyConsumer() {} | 52 URLResponseBodyConsumer::~URLResponseBodyConsumer() {} |
| 51 | 53 |
| 52 void URLResponseBodyConsumer::Start(base::SingleThreadTaskRunner* task_runner) { | 54 void URLResponseBodyConsumer::Start() { |
| 53 if (has_been_cancelled_) | 55 if (has_been_cancelled_) |
| 54 return; | 56 return; |
| 55 handle_watcher_.Start( | 57 handle_watcher_.Start( |
| 56 handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, | 58 handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 57 base::Bind(&URLResponseBodyConsumer::OnReadable, base::Unretained(this))); | 59 base::Bind(&URLResponseBodyConsumer::OnReadable, base::Unretained(this))); |
| 58 task_runner->PostTask( | 60 task_runner_->PostTask( |
| 59 FROM_HERE, base::Bind(&URLResponseBodyConsumer::OnReadable, AsWeakPtr(), | 61 FROM_HERE, base::Bind(&URLResponseBodyConsumer::OnReadable, AsWeakPtr(), |
| 60 MOJO_RESULT_OK)); | 62 MOJO_RESULT_OK)); |
| 61 } | 63 } |
| 62 | 64 |
| 63 void URLResponseBodyConsumer::OnComplete( | 65 void URLResponseBodyConsumer::OnComplete( |
| 64 const ResourceRequestCompletionStatus& status) { | 66 const ResourceRequestCompletionStatus& status) { |
| 65 if (has_been_cancelled_) | 67 if (has_been_cancelled_) |
| 66 return; | 68 return; |
| 67 has_received_completion_ = true; | 69 has_received_completion_ = true; |
| 68 completion_status_ = status; | 70 completion_status_ = status; |
| 69 NotifyCompletionIfAppropriate(); | 71 NotifyCompletionIfAppropriate(); |
| 70 } | 72 } |
| 71 | 73 |
| 72 void URLResponseBodyConsumer::Cancel() { | 74 void URLResponseBodyConsumer::Cancel() { |
| 73 has_been_cancelled_ = true; | 75 has_been_cancelled_ = true; |
| 74 handle_watcher_.Cancel(); | 76 handle_watcher_.Cancel(); |
| 75 } | 77 } |
| 76 | 78 |
| 77 void URLResponseBodyConsumer::Reclaim(uint32_t size) { | 79 void URLResponseBodyConsumer::Reclaim(uint32_t size) { |
| 78 MojoResult result = mojo::EndReadDataRaw(handle_.get(), size); | 80 MojoResult result = mojo::EndReadDataRaw(handle_.get(), size); |
| 79 DCHECK_EQ(MOJO_RESULT_OK, result); | 81 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 82 |
| 83 if (is_in_on_readable_) |
| 84 return; |
| 85 |
| 86 task_runner_->PostTask( |
| 87 FROM_HERE, base::Bind(&URLResponseBodyConsumer::OnReadable, AsWeakPtr(), |
| 88 MOJO_RESULT_OK)); |
| 80 } | 89 } |
| 81 | 90 |
| 82 void URLResponseBodyConsumer::OnReadable(MojoResult unused) { | 91 void URLResponseBodyConsumer::OnReadable(MojoResult unused) { |
| 92 DCHECK(!is_in_on_readable_); |
| 93 |
| 83 if (has_been_cancelled_ || has_seen_end_of_data_) | 94 if (has_been_cancelled_ || has_seen_end_of_data_) |
| 84 return; | 95 return; |
| 85 | 96 |
| 86 // Protect |this| as RequestPeer::OnReceivedData may call deref. | 97 // Protect |this| as RequestPeer::OnReceivedData may call deref. |
| 87 scoped_refptr<URLResponseBodyConsumer> protect(this); | 98 scoped_refptr<URLResponseBodyConsumer> protect(this); |
| 99 base::AutoReset<bool> is_in_on_readable(&is_in_on_readable_, true); |
| 88 | 100 |
| 89 // TODO(yhirano): Suppress notification when deferred. | 101 // TODO(yhirano): Suppress notification when deferred. |
| 90 while (!has_been_cancelled_) { | 102 while (!has_been_cancelled_) { |
| 91 const void* buffer = nullptr; | 103 const void* buffer = nullptr; |
| 92 uint32_t available = 0; | 104 uint32_t available = 0; |
| 93 MojoResult result = mojo::BeginReadDataRaw( | 105 MojoResult result = mojo::BeginReadDataRaw( |
| 94 handle_.get(), &buffer, &available, MOJO_READ_DATA_FLAG_NONE); | 106 handle_.get(), &buffer, &available, MOJO_READ_DATA_FLAG_NONE); |
| 95 if (result == MOJO_RESULT_SHOULD_WAIT) | 107 if (result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_BUSY) |
| 96 return; | 108 return; |
| 97 if (result == MOJO_RESULT_FAILED_PRECONDITION) { | 109 if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
| 98 has_seen_end_of_data_ = true; | 110 has_seen_end_of_data_ = true; |
| 99 NotifyCompletionIfAppropriate(); | 111 NotifyCompletionIfAppropriate(); |
| 100 return; | 112 return; |
| 101 } | 113 } |
| 102 if (result != MOJO_RESULT_OK) { | 114 if (result != MOJO_RESULT_OK) { |
| 103 completion_status_.error_code = net::ERR_FAILED; | 115 completion_status_.error_code = net::ERR_FAILED; |
| 104 has_seen_end_of_data_ = true; | 116 has_seen_end_of_data_ = true; |
| 105 has_received_completion_ = true; | 117 has_received_completion_ = true; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 121 return; | 133 return; |
| 122 // Cancel this instance in order not to notify twice. | 134 // Cancel this instance in order not to notify twice. |
| 123 Cancel(); | 135 Cancel(); |
| 124 | 136 |
| 125 resource_dispatcher_->OnMessageReceived( | 137 resource_dispatcher_->OnMessageReceived( |
| 126 ResourceMsg_RequestComplete(request_id_, completion_status_)); | 138 ResourceMsg_RequestComplete(request_id_, completion_status_)); |
| 127 // |this| may be deleted. | 139 // |this| may be deleted. |
| 128 } | 140 } |
| 129 | 141 |
| 130 } // namespace content | 142 } // namespace content |
| OLD | NEW |