blob: 898e358dae8bf4514b21a2ca339baa34e2b1e1dc [file] [log] [blame]
Reilly Grantccca59ad2017-08-11 00:05:101// 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-Malek6b56ef712017-10-21 22:52:465#include "content/renderer/loader/sync_load_context.h"
Reilly Grantccca59ad2017-08-11 00:05:106
7#include <string>
8
9#include "base/logging.h"
10#include "base/synchronization/waitable_event.h"
Reilly Grantccca59ad2017-08-11 00:05:1011#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-Malek6b56ef712017-10-21 22:52:4614#include "content/renderer/loader/sync_load_response.h"
Reilly Grantccca59ad2017-08-11 00:05:1015#include "net/url_request/redirect_info.h"
16
17namespace content {
18
19// static
20void SyncLoadContext::StartAsyncWithWaitableEvent(
21 std::unique_ptr<ResourceRequest> request,
22 int routing_id,
23 const url::Origin& frame_origin,
Ramin Halavatiefd97452017-09-25 08:50:4724 const net::NetworkTrafficAnnotationTag& traffic_annotation,
Reilly Grantccca59ad2017-08-11 00:05:1025 mojom::URLLoaderFactoryPtrInfo url_loader_factory_pipe,
Reilly Grant520f84b2017-09-01 20:47:5226 std::vector<std::unique_ptr<URLLoaderThrottle>> throttles,
Reilly Grantccca59ad2017-08-11 00:05:1027 SyncLoadResponse* response,
28 base::WaitableEvent* event) {
29 auto* context = new SyncLoadContext(
30 request.get(), std::move(url_loader_factory_pipe), response, event);
Reilly Grantccca59ad2017-08-11 00:05:1031
32 context->request_id_ = context->resource_dispatcher_->StartAsync(
Ramin Halavatiefd97452017-09-25 08:50:4733 std::move(request), routing_id, nullptr, frame_origin, traffic_annotation,
34 true /* is_sync */, base::WrapUnique(context),
35 blink::WebURLRequest::LoadingIPCType::kMojo,
Reilly Grantccca59ad2017-08-11 00:05:1036 context->url_loader_factory_.get(), std::move(throttles),
arthursonzogni3a4ca9f2017-12-07 17:58:3437 mojom::URLLoaderClientEndpointsPtr());
Reilly Grantccca59ad2017-08-11 00:05:1038}
39
40SyncLoadContext::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 Roman04f27c372017-10-27 15:20:5549 resource_dispatcher_ = std::make_unique<ResourceDispatcher>(
Reilly Grantccca59ad2017-08-11 00:05:1050 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
57SyncLoadContext::~SyncLoadContext() {}
58
59void SyncLoadContext::OnUploadProgress(uint64_t position, uint64_t size) {}
60
61bool 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
77void 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
89void 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
95void SyncLoadContext::OnReceivedData(std::unique_ptr<ReceivedData> data) {
96 response_->data.append(data->payload(), data->length());
97}
98
99void SyncLoadContext::OnTransferSizeUpdated(int transfer_size_diff) {}
100
Takashi Toyoshimaf8716922017-11-08 04:40:58101void SyncLoadContext::OnCompletedRequest(
Takashi Toyoshimaaa278662017-11-20 11:11:26102 const network::URLLoaderCompletionStatus& status) {
Takashi Toyoshima8f988532017-11-13 07:32:37103 response_->error_code = status.error_code;
Takashi Toyoshimaccd0bd542017-11-20 05:47:54104 if (status.cors_error_status)
105 response_->cors_error = status.cors_error_status->cors_error;
Takashi Toyoshima8f988532017-11-13 07:32:37106 response_->encoded_data_length = status.encoded_data_length;
107 response_->encoded_body_length = status.encoded_body_length;
Reilly Grantccca59ad2017-08-11 00:05:10108 event_->Signal();
109
110 // This will indirectly cause this object to be deleted.
111 resource_dispatcher_->RemovePendingRequest(request_id_);
112}
113
114} // namespace content