Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 1 | // Copyright 2017 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 | |
John Abd-El-Malek | 6b56ef71 | 2017-10-21 22:52:46 | [diff] [blame] | 5 | #include "content/renderer/loader/sync_load_context.h" |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 6 | |
| 7 | #include <string> |
| 8 | |
| 9 | #include "base/logging.h" |
| 10 | #include "base/synchronization/waitable_event.h" |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 11 | #include "content/public/common/resource_request.h" |
| 12 | #include "content/public/common/resource_response_info.h" |
| 13 | #include "content/public/common/url_loader_throttle.h" |
John Abd-El-Malek | 6b56ef71 | 2017-10-21 22:52:46 | [diff] [blame] | 14 | #include "content/renderer/loader/sync_load_response.h" |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 15 | #include "net/url_request/redirect_info.h" |
| 16 | |
| 17 | namespace content { |
| 18 | |
| 19 | // static |
| 20 | void SyncLoadContext::StartAsyncWithWaitableEvent( |
| 21 | std::unique_ptr<ResourceRequest> request, |
| 22 | int routing_id, |
| 23 | const url::Origin& frame_origin, |
Ramin Halavati | efd9745 | 2017-09-25 08:50:47 | [diff] [blame] | 24 | const net::NetworkTrafficAnnotationTag& traffic_annotation, |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 25 | mojom::URLLoaderFactoryPtrInfo url_loader_factory_pipe, |
Reilly Grant | 520f84b | 2017-09-01 20:47:52 | [diff] [blame] | 26 | std::vector<std::unique_ptr<URLLoaderThrottle>> throttles, |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 27 | SyncLoadResponse* response, |
| 28 | base::WaitableEvent* event) { |
| 29 | auto* context = new SyncLoadContext( |
| 30 | request.get(), std::move(url_loader_factory_pipe), response, event); |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 31 | |
| 32 | context->request_id_ = context->resource_dispatcher_->StartAsync( |
Ramin Halavati | efd9745 | 2017-09-25 08:50:47 | [diff] [blame] | 33 | std::move(request), routing_id, nullptr, frame_origin, traffic_annotation, |
| 34 | true /* is_sync */, base::WrapUnique(context), |
| 35 | blink::WebURLRequest::LoadingIPCType::kMojo, |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 36 | context->url_loader_factory_.get(), std::move(throttles), |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame^] | 37 | mojom::URLLoaderClientEndpointsPtr()); |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | SyncLoadContext::SyncLoadContext( |
| 41 | ResourceRequest* request, |
| 42 | mojom::URLLoaderFactoryPtrInfo url_loader_factory, |
| 43 | SyncLoadResponse* response, |
| 44 | base::WaitableEvent* event) |
| 45 | : response_(response), event_(event) { |
| 46 | url_loader_factory_.Bind(std::move(url_loader_factory)); |
| 47 | |
| 48 | // Constructs a new ResourceDispatcher specifically for this request. |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 49 | resource_dispatcher_ = std::make_unique<ResourceDispatcher>( |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 50 | nullptr, base::ThreadTaskRunnerHandle::Get()); |
| 51 | |
| 52 | // Initialize the final URL with the original request URL. It will be |
| 53 | // overwritten on redirects. |
| 54 | response_->url = request->url; |
| 55 | } |
| 56 | |
| 57 | SyncLoadContext::~SyncLoadContext() {} |
| 58 | |
| 59 | void SyncLoadContext::OnUploadProgress(uint64_t position, uint64_t size) {} |
| 60 | |
| 61 | bool SyncLoadContext::OnReceivedRedirect(const net::RedirectInfo& redirect_info, |
| 62 | const ResourceResponseInfo& info) { |
| 63 | if (redirect_info.new_url.GetOrigin() != response_->url.GetOrigin()) { |
| 64 | LOG(ERROR) << "Cross origin redirect denied"; |
| 65 | response_->error_code = net::ERR_ABORTED; |
| 66 | event_->Signal(); |
| 67 | |
| 68 | // Returning false here will cause the request to be cancelled and this |
| 69 | // object deleted. |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | response_->url = redirect_info.new_url; |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | void SyncLoadContext::OnReceivedResponse(const ResourceResponseInfo& info) { |
| 78 | response_->headers = info.headers; |
| 79 | response_->mime_type = info.mime_type; |
| 80 | response_->charset = info.charset; |
| 81 | response_->request_time = info.request_time; |
| 82 | response_->response_time = info.response_time; |
| 83 | response_->load_timing = info.load_timing; |
| 84 | response_->devtools_info = info.devtools_info; |
| 85 | response_->download_file_path = info.download_file_path; |
| 86 | response_->socket_address = info.socket_address; |
| 87 | } |
| 88 | |
| 89 | void SyncLoadContext::OnDownloadedData(int len, int encoded_data_length) { |
| 90 | // This method is only called when RequestInfo::download_to_file is true which |
| 91 | // is not allowed when processing a synchronous request. |
| 92 | NOTREACHED(); |
| 93 | } |
| 94 | |
| 95 | void SyncLoadContext::OnReceivedData(std::unique_ptr<ReceivedData> data) { |
| 96 | response_->data.append(data->payload(), data->length()); |
| 97 | } |
| 98 | |
| 99 | void SyncLoadContext::OnTransferSizeUpdated(int transfer_size_diff) {} |
| 100 | |
Takashi Toyoshima | f871692 | 2017-11-08 04:40:58 | [diff] [blame] | 101 | void SyncLoadContext::OnCompletedRequest( |
Takashi Toyoshima | aa27866 | 2017-11-20 11:11:26 | [diff] [blame] | 102 | const network::URLLoaderCompletionStatus& status) { |
Takashi Toyoshima | 8f98853 | 2017-11-13 07:32:37 | [diff] [blame] | 103 | response_->error_code = status.error_code; |
Takashi Toyoshima | ccd0bd54 | 2017-11-20 05:47:54 | [diff] [blame] | 104 | if (status.cors_error_status) |
| 105 | response_->cors_error = status.cors_error_status->cors_error; |
Takashi Toyoshima | 8f98853 | 2017-11-13 07:32:37 | [diff] [blame] | 106 | response_->encoded_data_length = status.encoded_data_length; |
| 107 | response_->encoded_body_length = status.encoded_body_length; |
Reilly Grant | ccca59ad | 2017-08-11 00:05:10 | [diff] [blame] | 108 | event_->Signal(); |
| 109 | |
| 110 | // This will indirectly cause this object to be deleted. |
| 111 | resource_dispatcher_->RemovePendingRequest(request_id_); |
| 112 | } |
| 113 | |
| 114 | } // namespace content |