Chromium Code Reviews| 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_loader_client_impl.h" | |
| 6 | |
| 7 #include "base/callback.h" | |
| 8 #include "base/single_thread_task_runner.h" | |
| 9 #include "content/child/resource_dispatcher.h" | |
| 10 #include "content/child/url_response_body_consumer.h" | |
| 11 #include "content/common/resource_messages.h" | |
| 12 #include "mojo/public/cpp/bindings/associated_group.h" | |
| 13 #include "net/url_request/redirect_info.h" | |
| 14 | |
| 15 namespace content { | |
| 16 | |
| 17 URLLoaderClientImpl::URLLoaderClientImpl( | |
| 18 int request_id, | |
| 19 ResourceDispatcher* resource_dispatcher, | |
| 20 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | |
| 21 : binding_(this), | |
| 22 request_id_(request_id), | |
| 23 resource_dispatcher_(resource_dispatcher), | |
| 24 is_destroyed_(new SharedBoolean()), | |
| 25 task_runner_(std::move(task_runner)) {} | |
| 26 | |
| 27 URLLoaderClientImpl::~URLLoaderClientImpl() { | |
| 28 if (body_consumer_) | |
| 29 body_consumer_->Cancel(); | |
| 30 is_destroyed_->set(true); | |
| 31 } | |
| 32 | |
| 33 void URLLoaderClientImpl::OnReceiveResponse( | |
| 34 const ResourceResponseHead& response_head, | |
| 35 mojom::DownloadedTempFilePtr downloaded_file) { | |
| 36 has_received_response_ = true; | |
| 37 if (body_consumer_) | |
| 38 body_consumer_->Start(); | |
| 39 downloaded_file_ = std::move(downloaded_file); | |
| 40 Dispatch(ResourceMsg_ReceivedResponse(request_id_, response_head)); | |
| 41 } | |
| 42 | |
| 43 void URLLoaderClientImpl::OnReceiveRedirect( | |
| 44 const net::RedirectInfo& redirect_info, | |
| 45 const ResourceResponseHead& response_head) { | |
| 46 DCHECK(!has_received_response_); | |
| 47 DCHECK(!body_consumer_); | |
| 48 Dispatch( | |
| 49 ResourceMsg_ReceivedRedirect(request_id_, redirect_info, response_head)); | |
| 50 } | |
| 51 | |
| 52 void URLLoaderClientImpl::OnDataDownloaded(int64_t data_len, | |
| 53 int64_t encoded_data_len) { | |
| 54 Dispatch(ResourceMsg_DataDownloaded(request_id_, data_len, encoded_data_len)); | |
| 55 } | |
| 56 | |
| 57 void URLLoaderClientImpl::OnTransferSizeUpdated(int32_t transfer_size_diff) { | |
| 58 if (is_deferred_) { | |
| 59 accumulated_transfer_size_diff_during_deferred_ += transfer_size_diff; | |
| 60 } else { | |
| 61 resource_dispatcher_->OnTransferSizeUpdated(request_id_, | |
| 62 transfer_size_diff); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void URLLoaderClientImpl::OnStartLoadingResponseBody( | |
| 67 mojo::ScopedDataPipeConsumerHandle body) { | |
| 68 DCHECK(!body_consumer_); | |
| 69 body_consumer_ = new URLResponseBodyConsumer( | |
| 70 request_id_, resource_dispatcher_, std::move(body), task_runner_); | |
| 71 if (has_received_response_) | |
| 72 body_consumer_->Start(); | |
| 73 if (is_deferred_) | |
| 74 body_consumer_->SetDefersLoading(); | |
| 75 } | |
| 76 | |
| 77 void URLLoaderClientImpl::OnComplete( | |
| 78 const ResourceRequestCompletionStatus& status) { | |
| 79 if (!body_consumer_) { | |
| 80 Dispatch(ResourceMsg_RequestComplete(request_id_, status)); | |
| 81 return; | |
| 82 } | |
| 83 body_consumer_->OnComplete(status); | |
| 84 } | |
| 85 | |
| 86 void URLLoaderClientImpl::FlushDeferredMessages() { | |
| 87 DCHECK(!is_deferred_); | |
| 88 std::vector<IPC::Message> messages; | |
| 89 messages.swap(deferred_messages_); | |
| 90 bool has_completion_message = false; | |
| 91 scoped_refptr<SharedBoolean> is_destroyed = is_destroyed_; | |
| 92 size_t index = 0; | |
| 93 // First, dispatch all messages excluding the followings: | |
| 94 // - response body (dispatched by |body_consumer_|) | |
| 95 // - transfer size change (dispatched later) | |
| 96 // - completion (dispatched by |body_consumer_| or dispatched later) | |
| 97 for (index = 0; index < messages.size(); ++index) { | |
| 98 if (messages[index].type() == ResourceMsg_RequestComplete::ID) { | |
| 99 // The completion message arrives at the end of the message queue. | |
| 100 DCHECK(!has_completion_message); | |
| 101 DCHECK_EQ(index, messages.size() - 1); | |
| 102 has_completion_message = true; | |
| 103 } else { | |
| 104 Dispatch(messages[index]); | |
| 105 } | |
| 106 if (is_destroyed->value()) | |
| 107 return; | |
| 108 if (is_deferred_) { | |
| 109 deferred_messages_.insert(deferred_messages_.begin(), | |
| 110 messages.begin() + index + 1, messages.end()); | |
| 111 return; | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 // Dispatch the transfer size update. | |
| 116 if (accumulated_transfer_size_diff_during_deferred_ > 0) { | |
| 117 auto transfer_size_diff = accumulated_transfer_size_diff_during_deferred_; | |
| 118 accumulated_transfer_size_diff_during_deferred_ = 0; | |
| 119 resource_dispatcher_->OnTransferSizeUpdated(request_id_, | |
| 120 transfer_size_diff); | |
| 121 if (is_destroyed->value()) | |
| 122 return; | |
| 123 if (is_deferred_) { | |
| 124 deferred_messages_.insert(deferred_messages_.begin(), | |
| 125 messages.begin() + index, messages.end()); | |
| 126 return; | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 if (body_consumer_) { | |
| 131 // Dispatch the response body. | |
| 132 body_consumer_->UnsetDefersLoading(); | |
| 133 if (is_destroyed->value()) | |
| 134 return; | |
| 135 if (is_deferred_) { | |
| 136 deferred_messages_.insert(deferred_messages_.begin(), | |
| 137 messages.begin() + index, messages.end()); | |
| 138 return; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 // Dispatch the completion message. | |
| 143 if (has_completion_message) { | |
| 144 DCHECK_GT(messages.size(), 0u); | |
| 145 DCHECK_EQ(messages.back().type(), | |
| 146 static_cast<uint32_t>(ResourceMsg_RequestComplete::ID)); | |
| 147 Dispatch(messages.back()); | |
| 148 } | |
| 149 } | |
| 150 | |
| 151 void URLLoaderClientImpl::SetDefersLoading() { | |
|
kinuko
2016/12/22 06:34:18
Any reason we don't make this take a boolean flag
| |
| 152 is_deferred_ = true; | |
| 153 if (body_consumer_) | |
| 154 body_consumer_->SetDefersLoading(); | |
| 155 } | |
| 156 | |
| 157 void URLLoaderClientImpl::UnsetDefersLoading() { | |
| 158 is_deferred_ = false; | |
| 159 } | |
| 160 | |
| 161 void URLLoaderClientImpl::Bind( | |
| 162 mojom::URLLoaderClientAssociatedPtrInfo* client_ptr_info, | |
| 163 mojo::AssociatedGroup* associated_group) { | |
| 164 binding_.Bind(client_ptr_info, associated_group); | |
| 165 } | |
| 166 | |
| 167 void URLLoaderClientImpl::Dispatch(const IPC::Message& message) { | |
| 168 if (is_deferred_) { | |
| 169 deferred_messages_.push_back(message); | |
| 170 } else if (deferred_messages_.size() > 0) { | |
| 171 deferred_messages_.push_back(message); | |
| 172 FlushDeferredMessages(); | |
| 173 } else { | |
| 174 resource_dispatcher_->OnMessageReceived(message); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 } // namespace content | |
| OLD | NEW |