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/browser/loader/mojo_async_resource_handler.h" | 5 #include "content/browser/loader/mojo_async_resource_handler.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 // Unlike OnResponseStarted, OnRequestRedirected will NOT be preceded by | 137 // Unlike OnResponseStarted, OnRequestRedirected will NOT be preceded by |
138 // OnWillRead. | 138 // OnWillRead. |
139 DCHECK(!shared_writer_); | 139 DCHECK(!shared_writer_); |
140 | 140 |
141 *defer = true; | 141 *defer = true; |
142 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 142 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
143 did_defer_on_redirect_ = true; | 143 did_defer_on_redirect_ = true; |
144 | 144 |
145 NetLogObserver::PopulateResponseInfo(request(), response); | 145 NetLogObserver::PopulateResponseInfo(request(), response); |
146 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); | 146 response->head.encoded_data_length = request()->GetTotalReceivedBytes(); |
147 reported_total_received_bytes_ = 0; | |
mmenke
2016/12/13 20:01:25
This isn't doing anything - we don't call OnRespon
yhirano
2016/12/14 16:47:27
Done.
| |
147 response->head.request_start = request()->creation_time(); | 148 response->head.request_start = request()->creation_time(); |
148 response->head.response_start = base::TimeTicks::Now(); | 149 response->head.response_start = base::TimeTicks::Now(); |
149 // TODO(davidben): Is it necessary to pass the new first party URL for | 150 // TODO(davidben): Is it necessary to pass the new first party URL for |
150 // cookies? The only case where it can change is top-level navigation requests | 151 // cookies? The only case where it can change is top-level navigation requests |
151 // and hopefully those will eventually all be owned by the browser. It's | 152 // and hopefully those will eventually all be owned by the browser. It's |
152 // possible this is still needed while renderer-owned ones exist. | 153 // possible this is still needed while renderer-owned ones exist. |
153 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); | 154 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); |
154 return true; | 155 return true; |
155 } | 156 } |
156 | 157 |
157 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, | 158 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, |
158 bool* defer) { | 159 bool* defer) { |
159 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 160 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
160 | 161 |
161 if (rdh_->delegate()) { | 162 if (rdh_->delegate()) { |
162 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), | 163 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), |
163 response); | 164 response); |
164 } | 165 } |
165 | 166 |
166 NetLogObserver::PopulateResponseInfo(request(), response); | 167 NetLogObserver::PopulateResponseInfo(request(), response); |
168 response->head.encoded_data_length = request()->raw_header_size(); | |
169 reported_total_received_bytes_ = response->head.encoded_data_length; | |
mmenke
2016/12/13 20:01:25
Looks like the old API didn't filter these out. I
yhirano
2016/12/14 16:47:27
AsyncResourceHandler has a special handling in OnR
| |
167 | 170 |
168 response->head.request_start = request()->creation_time(); | 171 response->head.request_start = request()->creation_time(); |
169 response->head.response_start = base::TimeTicks::Now(); | 172 response->head.response_start = base::TimeTicks::Now(); |
170 sent_received_response_message_ = true; | 173 sent_received_response_message_ = true; |
171 | 174 |
172 mojom::DownloadedTempFilePtr downloaded_file_ptr; | 175 mojom::DownloadedTempFilePtr downloaded_file_ptr; |
173 if (!response->head.download_file_path.empty()) { | 176 if (!response->head.download_file_path.empty()) { |
174 downloaded_file_ptr = DownloadedTempFileImpl::Create(info->GetChildID(), | 177 downloaded_file_ptr = DownloadedTempFileImpl::Create(info->GetChildID(), |
175 info->GetRequestID()); | 178 info->GetRequestID()); |
176 rdh_->RegisterDownloadedTempFile(info->GetChildID(), info->GetRequestID(), | 179 rdh_->RegisterDownloadedTempFile(info->GetChildID(), info->GetRequestID(), |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 return true; | 241 return true; |
239 } | 242 } |
240 | 243 |
241 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | 244 bool MojoAsyncResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { |
242 DCHECK_GE(bytes_read, 0); | 245 DCHECK_GE(bytes_read, 0); |
243 DCHECK(buffer_); | 246 DCHECK(buffer_); |
244 | 247 |
245 if (!bytes_read) | 248 if (!bytes_read) |
246 return true; | 249 return true; |
247 | 250 |
251 const ResourceRequestInfoImpl* info = GetRequestInfo(); | |
252 if (info->ShouldReportRawHeaders()) { | |
253 auto transfer_size_diff = CalculateTransferSizeDiff(); | |
254 if (transfer_size_diff > 0) | |
255 url_loader_client_->OnTransferSizeUpdated(transfer_size_diff); | |
256 } | |
257 | |
248 if (is_using_io_buffer_not_from_writer_) { | 258 if (is_using_io_buffer_not_from_writer_) { |
249 // Couldn't allocate a buffer on the data pipe in OnWillRead. | 259 // Couldn't allocate a buffer on the data pipe in OnWillRead. |
250 DCHECK_EQ(0u, buffer_bytes_read_); | 260 DCHECK_EQ(0u, buffer_bytes_read_); |
251 buffer_bytes_read_ = bytes_read; | 261 buffer_bytes_read_ = bytes_read; |
252 if (!CopyReadDataToDataPipe(defer)) | 262 if (!CopyReadDataToDataPipe(defer)) |
253 return false; | 263 return false; |
254 if (*defer) { | 264 if (*defer) { |
255 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 265 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
256 did_defer_on_writing_ = true; | 266 did_defer_on_writing_ = true; |
257 } | 267 } |
258 return true; | 268 return true; |
259 } | 269 } |
260 | 270 |
261 if (EndWrite(bytes_read) != MOJO_RESULT_OK) | 271 if (EndWrite(bytes_read) != MOJO_RESULT_OK) |
262 return false; | 272 return false; |
263 // Allocate a buffer for the next OnWillRead call here, because OnWillRead | 273 // Allocate a buffer for the next OnWillRead call here, because OnWillRead |
264 // doesn't have |defer| parameter. | 274 // doesn't have |defer| parameter. |
265 if (!AllocateWriterIOBuffer(&buffer_, defer)) | 275 if (!AllocateWriterIOBuffer(&buffer_, defer)) |
266 return false; | 276 return false; |
267 if (*defer) { | 277 if (*defer) { |
268 request()->LogBlockedBy("MojoAsyncResourceHandler"); | 278 request()->LogBlockedBy("MojoAsyncResourceHandler"); |
269 did_defer_on_writing_ = true; | 279 did_defer_on_writing_ = true; |
270 } | 280 } |
271 return true; | 281 return true; |
272 } | 282 } |
273 | 283 |
274 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { | 284 void MojoAsyncResourceHandler::OnDataDownloaded(int bytes_downloaded) { |
275 int64_t total_received_bytes = request()->GetTotalReceivedBytes(); | 285 url_loader_client_->OnDataDownloaded(bytes_downloaded, |
276 int64_t bytes_to_report = | 286 CalculateTransferSizeDiff()); |
277 total_received_bytes - reported_total_received_bytes_; | |
278 reported_total_received_bytes_ = total_received_bytes; | |
279 DCHECK_LE(0, bytes_to_report); | |
280 | |
281 url_loader_client_->OnDataDownloaded(bytes_downloaded, bytes_to_report); | |
282 } | 287 } |
283 | 288 |
284 void MojoAsyncResourceHandler::FollowRedirect() { | 289 void MojoAsyncResourceHandler::FollowRedirect() { |
285 if (!request()->status().is_success()) { | 290 if (!request()->status().is_success()) { |
286 DVLOG(1) << "FollowRedirect for invalid request"; | 291 DVLOG(1) << "FollowRedirect for invalid request"; |
287 return; | 292 return; |
288 } | 293 } |
289 if (!did_defer_on_redirect_) { | 294 if (!did_defer_on_redirect_) { |
290 DVLOG(1) << "Malformed FollowRedirect request"; | 295 DVLOG(1) << "Malformed FollowRedirect request"; |
291 ReportBadMessage("Malformed FollowRedirect request"); | 296 ReportBadMessage("Malformed FollowRedirect request"); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
443 request()->LogUnblocked(); | 448 request()->LogUnblocked(); |
444 controller()->Resume(); | 449 controller()->Resume(); |
445 } | 450 } |
446 | 451 |
447 void MojoAsyncResourceHandler::Cancel() { | 452 void MojoAsyncResourceHandler::Cancel() { |
448 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 453 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
449 ResourceDispatcherHostImpl::Get()->CancelRequestFromRenderer( | 454 ResourceDispatcherHostImpl::Get()->CancelRequestFromRenderer( |
450 GlobalRequestID(info->GetChildID(), info->GetRequestID())); | 455 GlobalRequestID(info->GetChildID(), info->GetRequestID())); |
451 } | 456 } |
452 | 457 |
458 int64_t MojoAsyncResourceHandler::CalculateTransferSizeDiff() { | |
459 int64_t total_received_bytes = request()->GetTotalReceivedBytes(); | |
460 int64_t bytes_to_report = | |
461 total_received_bytes - reported_total_received_bytes_; | |
462 reported_total_received_bytes_ = total_received_bytes; | |
463 DCHECK_LE(0, bytes_to_report); | |
464 return bytes_to_report; | |
465 } | |
466 | |
453 void MojoAsyncResourceHandler::ReportBadMessage(const std::string& error) { | 467 void MojoAsyncResourceHandler::ReportBadMessage(const std::string& error) { |
454 mojo::ReportBadMessage(error); | 468 mojo::ReportBadMessage(error); |
455 } | 469 } |
456 | 470 |
457 void MojoAsyncResourceHandler::OnTransfer( | 471 void MojoAsyncResourceHandler::OnTransfer( |
458 mojom::URLLoaderAssociatedRequest mojo_request, | 472 mojom::URLLoaderAssociatedRequest mojo_request, |
459 mojom::URLLoaderClientAssociatedPtr url_loader_client) { | 473 mojom::URLLoaderClientAssociatedPtr url_loader_client) { |
460 binding_.Unbind(); | 474 binding_.Unbind(); |
461 binding_.Bind(std::move(mojo_request)); | 475 binding_.Bind(std::move(mojo_request)); |
462 binding_.set_connection_error_handler( | 476 binding_.set_connection_error_handler( |
463 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); | 477 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); |
464 url_loader_client_ = std::move(url_loader_client); | 478 url_loader_client_ = std::move(url_loader_client); |
465 } | 479 } |
466 | 480 |
467 } // namespace content | 481 } // namespace content |
OLD | NEW |