| 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 <algorithm> |
| 7 #include <utility> | 8 #include <utility> |
| 8 | 9 |
| 10 #include "base/bind.h" |
| 9 #include "base/command_line.h" | 11 #include "base/command_line.h" |
| 10 #include "base/containers/hash_tables.h" | |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 15 #include "content/browser/loader/downloaded_temp_file_impl.h" | 17 #include "content/browser/loader/downloaded_temp_file_impl.h" |
| 16 #include "content/browser/loader/netlog_observer.h" | 18 #include "content/browser/loader/netlog_observer.h" |
| 17 #include "content/browser/loader/resource_controller.h" | 19 #include "content/browser/loader/resource_controller.h" |
| 18 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 20 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 19 #include "content/browser/loader/resource_request_info_impl.h" | 21 #include "content/browser/loader/resource_request_info_impl.h" |
| 22 #include "content/browser/loader/upload_progress_tracker.h" |
| 20 #include "content/common/resource_request_completion_status.h" | 23 #include "content/common/resource_request_completion_status.h" |
| 21 #include "content/public/browser/global_request_id.h" | 24 #include "content/public/browser/global_request_id.h" |
| 22 #include "content/public/browser/resource_dispatcher_host_delegate.h" | 25 #include "content/public/browser/resource_dispatcher_host_delegate.h" |
| 23 #include "content/public/common/resource_response.h" | 26 #include "content/public/common/resource_response.h" |
| 24 #include "mojo/public/c/system/data_pipe.h" | 27 #include "mojo/public/c/system/data_pipe.h" |
| 25 #include "mojo/public/cpp/bindings/message.h" | 28 #include "mojo/public/cpp/bindings/message.h" |
| 26 #include "mojo/public/cpp/system/data_pipe.h" | 29 #include "mojo/public/cpp/system/data_pipe.h" |
| 27 #include "net/base/io_buffer.h" | 30 #include "net/base/io_buffer.h" |
| 28 #include "net/base/load_flags.h" | |
| 29 #include "net/base/mime_sniffer.h" | 31 #include "net/base/mime_sniffer.h" |
| 30 #include "net/url_request/redirect_info.h" | 32 #include "net/url_request/redirect_info.h" |
| 31 | 33 |
| 32 namespace content { | 34 namespace content { |
| 33 namespace { | 35 namespace { |
| 34 | 36 |
| 35 int g_allocation_size = MojoAsyncResourceHandler::kDefaultAllocationSize; | 37 int g_allocation_size = MojoAsyncResourceHandler::kDefaultAllocationSize; |
| 36 | 38 |
| 37 // MimeTypeResourceHandler *implicitly* requires that the buffer size | 39 // MimeTypeResourceHandler *implicitly* requires that the buffer size |
| 38 // returned from OnWillRead should be larger than certain size. | 40 // returned from OnWillRead should be larger than certain size. |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 // TODO(davidben): Is it necessary to pass the new first party URL for | 151 // 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 | 152 // 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 | 153 // and hopefully those will eventually all be owned by the browser. It's |
| 152 // possible this is still needed while renderer-owned ones exist. | 154 // possible this is still needed while renderer-owned ones exist. |
| 153 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); | 155 url_loader_client_->OnReceiveRedirect(redirect_info, response->head); |
| 154 return true; | 156 return true; |
| 155 } | 157 } |
| 156 | 158 |
| 157 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, | 159 bool MojoAsyncResourceHandler::OnResponseStarted(ResourceResponse* response, |
| 158 bool* defer) { | 160 bool* defer) { |
| 161 if (upload_progress_tracker_) { |
| 162 upload_progress_tracker_->OnUploadCompleted(); |
| 163 upload_progress_tracker_ = nullptr; |
| 164 } |
| 165 |
| 159 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 166 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 160 | |
| 161 if (rdh_->delegate()) { | 167 if (rdh_->delegate()) { |
| 162 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), | 168 rdh_->delegate()->OnResponseStarted(request(), info->GetContext(), |
| 163 response); | 169 response); |
| 164 } | 170 } |
| 165 | 171 |
| 166 NetLogObserver::PopulateResponseInfo(request(), response); | 172 NetLogObserver::PopulateResponseInfo(request(), response); |
| 167 response->head.encoded_data_length = request()->raw_header_size(); | 173 response->head.encoded_data_length = request()->raw_header_size(); |
| 168 reported_total_received_bytes_ = response->head.encoded_data_length; | 174 reported_total_received_bytes_ = response->head.encoded_data_length; |
| 169 | 175 |
| 170 response->head.request_start = request()->creation_time(); | 176 response->head.request_start = request()->creation_time(); |
| 171 response->head.response_start = base::TimeTicks::Now(); | 177 response->head.response_start = base::TimeTicks::Now(); |
| 172 sent_received_response_message_ = true; | 178 sent_received_response_message_ = true; |
| 173 | 179 |
| 174 mojom::DownloadedTempFilePtr downloaded_file_ptr; | 180 mojom::DownloadedTempFilePtr downloaded_file_ptr; |
| 175 if (!response->head.download_file_path.empty()) { | 181 if (!response->head.download_file_path.empty()) { |
| 176 downloaded_file_ptr = DownloadedTempFileImpl::Create(info->GetChildID(), | 182 downloaded_file_ptr = DownloadedTempFileImpl::Create(info->GetChildID(), |
| 177 info->GetRequestID()); | 183 info->GetRequestID()); |
| 178 rdh_->RegisterDownloadedTempFile(info->GetChildID(), info->GetRequestID(), | 184 rdh_->RegisterDownloadedTempFile(info->GetChildID(), info->GetRequestID(), |
| 179 response->head.download_file_path); | 185 response->head.download_file_path); |
| 180 } | 186 } |
| 181 | 187 |
| 182 url_loader_client_->OnReceiveResponse(response->head, | 188 url_loader_client_->OnReceiveResponse(response->head, |
| 183 std::move(downloaded_file_ptr)); | 189 std::move(downloaded_file_ptr)); |
| 184 return true; | 190 return true; |
| 185 } | 191 } |
| 186 | 192 |
| 187 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { | 193 bool MojoAsyncResourceHandler::OnWillStart(const GURL& url, bool* defer) { |
| 194 if (GetRequestInfo()->is_upload_progress_enabled() && |
| 195 request()->has_upload()) { |
| 196 upload_progress_tracker_ = base::MakeUnique<UploadProgressTracker>( |
| 197 FROM_HERE, |
| 198 base::BindRepeating(&MojoAsyncResourceHandler::SendUploadProgress, |
| 199 base::Unretained(this)), |
| 200 request()); |
| 201 } |
| 202 |
| 188 return true; | 203 return true; |
| 189 } | 204 } |
| 190 | 205 |
| 191 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 206 bool MojoAsyncResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
| 192 int* buf_size, | 207 int* buf_size, |
| 193 int min_size) { | 208 int min_size) { |
| 194 DCHECK_EQ(-1, min_size); | 209 DCHECK_EQ(-1, min_size); |
| 195 | 210 |
| 196 if (!CheckForSufficientResource()) | 211 if (!CheckForSufficientResource()) |
| 197 return false; | 212 return false; |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 return result; | 334 return result; |
| 320 } | 335 } |
| 321 | 336 |
| 322 MojoResult MojoAsyncResourceHandler::EndWrite(uint32_t written) { | 337 MojoResult MojoAsyncResourceHandler::EndWrite(uint32_t written) { |
| 323 return mojo::EndWriteDataRaw(shared_writer_->writer(), written); | 338 return mojo::EndWriteDataRaw(shared_writer_->writer(), written); |
| 324 } | 339 } |
| 325 | 340 |
| 326 void MojoAsyncResourceHandler::OnResponseCompleted( | 341 void MojoAsyncResourceHandler::OnResponseCompleted( |
| 327 const net::URLRequestStatus& status, | 342 const net::URLRequestStatus& status, |
| 328 bool* defer) { | 343 bool* defer) { |
| 344 if (upload_progress_tracker_) { |
| 345 upload_progress_tracker_->OnUploadCompleted(); |
| 346 upload_progress_tracker_ = nullptr; |
| 347 } |
| 348 |
| 329 shared_writer_ = nullptr; | 349 shared_writer_ = nullptr; |
| 330 buffer_ = nullptr; | 350 buffer_ = nullptr; |
| 331 handle_watcher_.Cancel(); | 351 handle_watcher_.Cancel(); |
| 332 | 352 |
| 333 const ResourceRequestInfoImpl* info = GetRequestInfo(); | 353 const ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 334 | 354 |
| 335 // TODO(gavinp): Remove this CHECK when we figure out the cause of | 355 // TODO(gavinp): Remove this CHECK when we figure out the cause of |
| 336 // http://crbug.com/124680 . This check mirrors closely check in | 356 // http://crbug.com/124680 . This check mirrors closely check in |
| 337 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore | 357 // WebURLLoaderImpl::OnCompletedRequest that routes this message to a WebCore |
| 338 // ResourceHandleInternal which asserts on its state and crashes. By crashing | 358 // ResourceHandleInternal which asserts on its state and crashes. By crashing |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 void MojoAsyncResourceHandler::OnTransfer( | 490 void MojoAsyncResourceHandler::OnTransfer( |
| 471 mojom::URLLoaderAssociatedRequest mojo_request, | 491 mojom::URLLoaderAssociatedRequest mojo_request, |
| 472 mojom::URLLoaderClientAssociatedPtr url_loader_client) { | 492 mojom::URLLoaderClientAssociatedPtr url_loader_client) { |
| 473 binding_.Unbind(); | 493 binding_.Unbind(); |
| 474 binding_.Bind(std::move(mojo_request)); | 494 binding_.Bind(std::move(mojo_request)); |
| 475 binding_.set_connection_error_handler( | 495 binding_.set_connection_error_handler( |
| 476 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); | 496 base::Bind(&MojoAsyncResourceHandler::Cancel, base::Unretained(this))); |
| 477 url_loader_client_ = std::move(url_loader_client); | 497 url_loader_client_ = std::move(url_loader_client); |
| 478 } | 498 } |
| 479 | 499 |
| 500 void MojoAsyncResourceHandler::OnUploadProgressACK() { |
| 501 if (upload_progress_tracker_) |
| 502 upload_progress_tracker_->OnAckReceived(); |
| 503 } |
| 504 |
| 505 void MojoAsyncResourceHandler::SendUploadProgress(int64_t current_position, |
| 506 int64_t total_size) { |
| 507 url_loader_client_->OnUploadProgress( |
| 508 current_position, total_size, |
| 509 base::Bind(&MojoAsyncResourceHandler::OnUploadProgressACK, |
| 510 weak_factory_.GetWeakPtr())); |
| 511 } |
| 512 |
| 480 } // namespace content | 513 } // namespace content |
| OLD | NEW |