| 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/auto_reset.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 URLResponseBodyConsumer::URLResponseBodyConsumer( | 43 URLResponseBodyConsumer::URLResponseBodyConsumer( |
| 44 int request_id, | 44 int request_id, |
| 45 ResourceDispatcher* resource_dispatcher, | 45 ResourceDispatcher* resource_dispatcher, |
| 46 mojo::ScopedDataPipeConsumerHandle handle, | 46 mojo::ScopedDataPipeConsumerHandle handle, |
| 47 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 47 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 48 : request_id_(request_id), | 48 : request_id_(request_id), |
| 49 resource_dispatcher_(resource_dispatcher), | 49 resource_dispatcher_(resource_dispatcher), |
| 50 handle_(std::move(handle)), | 50 handle_(std::move(handle)), |
| 51 handle_watcher_(FROM_HERE, task_runner), | 51 handle_watcher_(FROM_HERE, |
| 52 mojo::SimpleWatcher::ArmingPolicy::MANUAL, |
| 53 task_runner), |
| 52 task_runner_(task_runner), | 54 task_runner_(task_runner), |
| 53 has_seen_end_of_data_(!handle_.is_valid()) { | 55 has_seen_end_of_data_(!handle_.is_valid()) { |
| 54 handle_watcher_.Start( | 56 handle_watcher_.Watch( |
| 55 handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, | 57 handle_.get(), MOJO_HANDLE_SIGNAL_READABLE, |
| 56 base::Bind(&URLResponseBodyConsumer::OnReadable, base::Unretained(this))); | 58 base::Bind(&URLResponseBodyConsumer::OnReadable, base::Unretained(this))); |
| 57 task_runner_->PostTask( | 59 handle_watcher_.ArmOrNotify(); |
| 58 FROM_HERE, base::Bind(&URLResponseBodyConsumer::OnReadable, AsWeakPtr(), | |
| 59 MOJO_RESULT_OK)); | |
| 60 } | 60 } |
| 61 | 61 |
| 62 URLResponseBodyConsumer::~URLResponseBodyConsumer() {} | 62 URLResponseBodyConsumer::~URLResponseBodyConsumer() {} |
| 63 | 63 |
| 64 void URLResponseBodyConsumer::OnComplete( | 64 void URLResponseBodyConsumer::OnComplete( |
| 65 const ResourceRequestCompletionStatus& status) { | 65 const ResourceRequestCompletionStatus& status) { |
| 66 if (has_been_cancelled_) | 66 if (has_been_cancelled_) |
| 67 return; | 67 return; |
| 68 has_received_completion_ = true; | 68 has_received_completion_ = true; |
| 69 completion_status_ = status; | 69 completion_status_ = status; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 84 OnReadable(MOJO_RESULT_OK); | 84 OnReadable(MOJO_RESULT_OK); |
| 85 } | 85 } |
| 86 | 86 |
| 87 void URLResponseBodyConsumer::Reclaim(uint32_t size) { | 87 void URLResponseBodyConsumer::Reclaim(uint32_t size) { |
| 88 MojoResult result = mojo::EndReadDataRaw(handle_.get(), size); | 88 MojoResult result = mojo::EndReadDataRaw(handle_.get(), size); |
| 89 DCHECK_EQ(MOJO_RESULT_OK, result); | 89 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 90 | 90 |
| 91 if (is_in_on_readable_) | 91 if (is_in_on_readable_) |
| 92 return; | 92 return; |
| 93 | 93 |
| 94 task_runner_->PostTask( | 94 handle_watcher_.ArmOrNotify(); |
| 95 FROM_HERE, base::Bind(&URLResponseBodyConsumer::OnReadable, AsWeakPtr(), | |
| 96 MOJO_RESULT_OK)); | |
| 97 } | 95 } |
| 98 | 96 |
| 99 void URLResponseBodyConsumer::OnReadable(MojoResult unused) { | 97 void URLResponseBodyConsumer::OnReadable(MojoResult unused) { |
| 100 if (has_been_cancelled_ || has_seen_end_of_data_ || is_deferred_) | 98 if (has_been_cancelled_ || has_seen_end_of_data_ || is_deferred_) |
| 101 return; | 99 return; |
| 102 | 100 |
| 103 DCHECK(!is_in_on_readable_); | 101 DCHECK(!is_in_on_readable_); |
| 104 uint32_t num_bytes_consumed = 0; | 102 uint32_t num_bytes_consumed = 0; |
| 105 | 103 |
| 106 // Protect |this| as RequestPeer::OnReceivedData may call deref. | 104 // Protect |this| as RequestPeer::OnReceivedData may call deref. |
| 107 scoped_refptr<URLResponseBodyConsumer> protect(this); | 105 scoped_refptr<URLResponseBodyConsumer> protect(this); |
| 108 base::AutoReset<bool> is_in_on_readable(&is_in_on_readable_, true); | 106 base::AutoReset<bool> is_in_on_readable(&is_in_on_readable_, true); |
| 109 | 107 |
| 110 while (!has_been_cancelled_ && !is_deferred_) { | 108 while (!has_been_cancelled_ && !is_deferred_) { |
| 111 const void* buffer = nullptr; | 109 const void* buffer = nullptr; |
| 112 uint32_t available = 0; | 110 uint32_t available = 0; |
| 113 MojoResult result = mojo::BeginReadDataRaw( | 111 MojoResult result = mojo::BeginReadDataRaw( |
| 114 handle_.get(), &buffer, &available, MOJO_READ_DATA_FLAG_NONE); | 112 handle_.get(), &buffer, &available, MOJO_READ_DATA_FLAG_NONE); |
| 115 if (result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_BUSY) | 113 if (result == MOJO_RESULT_SHOULD_WAIT) { |
| 114 handle_watcher_.ArmOrNotify(); |
| 115 return; |
| 116 } |
| 117 if (result == MOJO_RESULT_BUSY) |
| 116 return; | 118 return; |
| 117 if (result == MOJO_RESULT_FAILED_PRECONDITION) { | 119 if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
| 118 has_seen_end_of_data_ = true; | 120 has_seen_end_of_data_ = true; |
| 119 NotifyCompletionIfAppropriate(); | 121 NotifyCompletionIfAppropriate(); |
| 120 return; | 122 return; |
| 121 } | 123 } |
| 122 if (result != MOJO_RESULT_OK) { | 124 if (result != MOJO_RESULT_OK) { |
| 123 completion_status_.error_code = net::ERR_FAILED; | 125 completion_status_.error_code = net::ERR_FAILED; |
| 124 has_seen_end_of_data_ = true; | 126 has_seen_end_of_data_ = true; |
| 125 has_received_completion_ = true; | 127 has_received_completion_ = true; |
| 126 NotifyCompletionIfAppropriate(); | 128 NotifyCompletionIfAppropriate(); |
| 127 return; | 129 return; |
| 128 } | 130 } |
| 129 DCHECK_LE(num_bytes_consumed, kMaxNumConsumedBytesInTask); | 131 DCHECK_LE(num_bytes_consumed, kMaxNumConsumedBytesInTask); |
| 130 available = | 132 available = |
| 131 std::min(available, kMaxNumConsumedBytesInTask - num_bytes_consumed); | 133 std::min(available, kMaxNumConsumedBytesInTask - num_bytes_consumed); |
| 132 if (available == 0) { | 134 if (available == 0) { |
| 133 // We've already consumed many bytes in this task. Defer the remaining | 135 // We've already consumed many bytes in this task. Defer the remaining |
| 134 // to the next task. | 136 // to the next task. |
| 135 result = mojo::EndReadDataRaw(handle_.get(), 0); | 137 result = mojo::EndReadDataRaw(handle_.get(), 0); |
| 136 DCHECK_EQ(result, MOJO_RESULT_OK); | 138 DCHECK_EQ(result, MOJO_RESULT_OK); |
| 137 task_runner_->PostTask(FROM_HERE, | 139 handle_watcher_.ArmOrNotify(); |
| 138 base::Bind(&URLResponseBodyConsumer::OnReadable, | |
| 139 AsWeakPtr(), MOJO_RESULT_OK)); | |
| 140 return; | 140 return; |
| 141 } | 141 } |
| 142 num_bytes_consumed += available; | 142 num_bytes_consumed += available; |
| 143 ResourceDispatcher::PendingRequestInfo* request_info = | 143 ResourceDispatcher::PendingRequestInfo* request_info = |
| 144 resource_dispatcher_->GetPendingRequestInfo(request_id_); | 144 resource_dispatcher_->GetPendingRequestInfo(request_id_); |
| 145 DCHECK(request_info); | 145 DCHECK(request_info); |
| 146 | 146 |
| 147 // Check whether this response data is compliant with our cross-site | 147 // Check whether this response data is compliant with our cross-site |
| 148 // document blocking policy. We only do this for the first chunk of data. | 148 // document blocking policy. We only do this for the first chunk of data. |
| 149 if (request_info->site_isolation_metadata.get()) { | 149 if (request_info->site_isolation_metadata.get()) { |
| (...skipping 15 matching lines...) Expand all Loading... |
| 165 return; | 165 return; |
| 166 // Cancel this instance in order not to notify twice. | 166 // Cancel this instance in order not to notify twice. |
| 167 Cancel(); | 167 Cancel(); |
| 168 | 168 |
| 169 resource_dispatcher_->DispatchMessage( | 169 resource_dispatcher_->DispatchMessage( |
| 170 ResourceMsg_RequestComplete(request_id_, completion_status_)); | 170 ResourceMsg_RequestComplete(request_id_, completion_status_)); |
| 171 // |this| may be deleted. | 171 // |this| may be deleted. |
| 172 } | 172 } |
| 173 | 173 |
| 174 } // namespace content | 174 } // namespace content |
| OLD | NEW |