| 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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 has_received_completion_ = true; | 69 has_received_completion_ = true; |
| 70 completion_status_ = status; | 70 completion_status_ = status; |
| 71 NotifyCompletionIfAppropriate(); | 71 NotifyCompletionIfAppropriate(); |
| 72 } | 72 } |
| 73 | 73 |
| 74 void URLResponseBodyConsumer::Cancel() { | 74 void URLResponseBodyConsumer::Cancel() { |
| 75 has_been_cancelled_ = true; | 75 has_been_cancelled_ = true; |
| 76 handle_watcher_.Cancel(); | 76 handle_watcher_.Cancel(); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void URLResponseBodyConsumer::SetDefersLoading() { |
| 80 is_deferred_ = true; |
| 81 } |
| 82 |
| 83 void URLResponseBodyConsumer::UnsetDefersLoading() { |
| 84 is_deferred_ = false; |
| 85 OnReadable(MOJO_RESULT_OK); |
| 86 } |
| 87 |
| 79 void URLResponseBodyConsumer::Reclaim(uint32_t size) { | 88 void URLResponseBodyConsumer::Reclaim(uint32_t size) { |
| 80 MojoResult result = mojo::EndReadDataRaw(handle_.get(), size); | 89 MojoResult result = mojo::EndReadDataRaw(handle_.get(), size); |
| 81 DCHECK_EQ(MOJO_RESULT_OK, result); | 90 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 82 | 91 |
| 83 if (is_in_on_readable_) | 92 if (is_in_on_readable_) |
| 84 return; | 93 return; |
| 85 | 94 |
| 86 task_runner_->PostTask( | 95 task_runner_->PostTask( |
| 87 FROM_HERE, base::Bind(&URLResponseBodyConsumer::OnReadable, AsWeakPtr(), | 96 FROM_HERE, base::Bind(&URLResponseBodyConsumer::OnReadable, AsWeakPtr(), |
| 88 MOJO_RESULT_OK)); | 97 MOJO_RESULT_OK)); |
| 89 } | 98 } |
| 90 | 99 |
| 91 void URLResponseBodyConsumer::OnReadable(MojoResult unused) { | 100 void URLResponseBodyConsumer::OnReadable(MojoResult unused) { |
| 101 if (has_been_cancelled_ || has_seen_end_of_data_ || is_deferred_) |
| 102 return; |
| 103 |
| 92 DCHECK(!is_in_on_readable_); | 104 DCHECK(!is_in_on_readable_); |
| 93 | 105 |
| 94 if (has_been_cancelled_ || has_seen_end_of_data_) | |
| 95 return; | |
| 96 | |
| 97 // Protect |this| as RequestPeer::OnReceivedData may call deref. | 106 // Protect |this| as RequestPeer::OnReceivedData may call deref. |
| 98 scoped_refptr<URLResponseBodyConsumer> protect(this); | 107 scoped_refptr<URLResponseBodyConsumer> protect(this); |
| 99 base::AutoReset<bool> is_in_on_readable(&is_in_on_readable_, true); | 108 base::AutoReset<bool> is_in_on_readable(&is_in_on_readable_, true); |
| 100 | 109 |
| 101 // TODO(yhirano): Suppress notification when deferred. | 110 // TODO(yhirano): Suppress notification when deferred. |
| 102 while (!has_been_cancelled_) { | 111 while (!has_been_cancelled_ && !is_deferred_) { |
| 103 const void* buffer = nullptr; | 112 const void* buffer = nullptr; |
| 104 uint32_t available = 0; | 113 uint32_t available = 0; |
| 105 MojoResult result = mojo::BeginReadDataRaw( | 114 MojoResult result = mojo::BeginReadDataRaw( |
| 106 handle_.get(), &buffer, &available, MOJO_READ_DATA_FLAG_NONE); | 115 handle_.get(), &buffer, &available, MOJO_READ_DATA_FLAG_NONE); |
| 107 if (result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_BUSY) | 116 if (result == MOJO_RESULT_SHOULD_WAIT || result == MOJO_RESULT_BUSY) |
| 108 return; | 117 return; |
| 109 if (result == MOJO_RESULT_FAILED_PRECONDITION) { | 118 if (result == MOJO_RESULT_FAILED_PRECONDITION) { |
| 110 has_seen_end_of_data_ = true; | 119 has_seen_end_of_data_ = true; |
| 111 NotifyCompletionIfAppropriate(); | 120 NotifyCompletionIfAppropriate(); |
| 112 return; | 121 return; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 133 return; | 142 return; |
| 134 // Cancel this instance in order not to notify twice. | 143 // Cancel this instance in order not to notify twice. |
| 135 Cancel(); | 144 Cancel(); |
| 136 | 145 |
| 137 resource_dispatcher_->OnMessageReceived( | 146 resource_dispatcher_->OnMessageReceived( |
| 138 ResourceMsg_RequestComplete(request_id_, completion_status_)); | 147 ResourceMsg_RequestComplete(request_id_, completion_status_)); |
| 139 // |this| may be deleted. | 148 // |this| may be deleted. |
| 140 } | 149 } |
| 141 | 150 |
| 142 } // namespace content | 151 } // namespace content |
| OLD | NEW |