1 // Copyright 2014 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.
5 #include "components/html_viewer/web_url_loader_impl.h"
8 #include "base/logging.h"
9 #include "base/numerics/safe_conversions.h"
10 #include "base/strings/string_util.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "components/html_viewer/blink_url_request_type_converters.h"
13 #include "mojo/common/common_type_converters.h"
14 #include "mojo/common/data_pipe_utils.h"
15 #include "mojo/common/url_type_converters.h"
16 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h"
17 #include "net/base/net_errors.h"
18 #include "third_party/WebKit/public/platform/WebURLError.h"
19 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
20 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
21 #include "third_party/WebKit/public/platform/WebURLResponse.h"
23 using blink::WebString
;
24 using mojo::URLResponsePtr
;
26 namespace html_viewer
{
29 blink::WebURLResponse::HTTPVersion
StatusLineToHTTPVersion(
30 const mojo::String
& status_line
) {
31 if (status_line
.is_null())
32 return blink::WebURLResponse::HTTP_0_9
;
34 if (base::StartsWith(status_line
.get(), "HTTP/1.0",
35 base::CompareCase::SENSITIVE
))
36 return blink::WebURLResponse::HTTP_1_0
;
38 if (base::StartsWith(status_line
.get(), "HTTP/1.1",
39 base::CompareCase::SENSITIVE
))
40 return blink::WebURLResponse::HTTP_1_1
;
42 return blink::WebURLResponse::Unknown
;
45 blink::WebURLResponse
ToWebURLResponse(const URLResponsePtr
& url_response
) {
46 blink::WebURLResponse result
;
48 result
.setURL(GURL(url_response
->url
));
49 result
.setMIMEType(blink::WebString::fromUTF8(url_response
->mime_type
));
50 result
.setTextEncodingName(blink::WebString::fromUTF8(url_response
->charset
));
51 result
.setHTTPVersion(StatusLineToHTTPVersion(url_response
->status_line
));
52 result
.setHTTPStatusCode(url_response
->status_code
);
53 result
.setExpectedContentLength(-1); // Not available.
55 // TODO(darin): Initialize timing properly.
56 blink::WebURLLoadTiming timing
;
58 result
.setLoadTiming(timing
);
60 for (size_t i
= 0; i
< url_response
->headers
.size(); ++i
) {
61 result
.setHTTPHeaderField(
62 blink::WebString::fromUTF8(url_response
->headers
[i
]->name
),
63 blink::WebString::fromUTF8(url_response
->headers
[i
]->value
));
71 WebURLRequestExtraData::WebURLRequestExtraData() {
74 WebURLRequestExtraData::~WebURLRequestExtraData() {
77 WebURLLoaderImpl::WebURLLoaderImpl(mojo::URLLoaderFactory
* url_loader_factory
,
78 MockWebBlobRegistryImpl
* web_blob_registry
)
80 web_blob_registry_(web_blob_registry
),
81 referrer_policy_(blink::WebReferrerPolicyDefault
),
83 url_loader_factory
->CreateURLLoader(GetProxy(&url_loader_
));
86 WebURLLoaderImpl::~WebURLLoaderImpl() {
89 void WebURLLoaderImpl::loadSynchronously(
90 const blink::WebURLRequest
& request
,
91 blink::WebURLResponse
& response
,
92 blink::WebURLError
& error
,
93 blink::WebData
& data
) {
94 mojo::URLRequestPtr url_request
= mojo::URLRequest::From(request
);
95 url_request
->auto_follow_redirects
= true;
96 URLResponsePtr url_response
;
97 url_loader_
->Start(url_request
.Pass(),
98 [&url_response
](URLResponsePtr url_response_result
) {
99 url_response
= url_response_result
.Pass();
101 url_loader_
.WaitForIncomingResponse();
102 if (url_response
->error
) {
103 error
.domain
= WebString::fromUTF8(net::kErrorDomain
);
104 error
.reason
= url_response
->error
->code
;
105 error
.unreachableURL
= GURL(url_response
->url
);
109 response
= ToWebURLResponse(url_response
);
111 mojo::common::BlockingCopyToString(url_response
->body
.Pass(), &body
);
112 data
.assign(body
.data(), body
.length());
115 void WebURLLoaderImpl::loadAsynchronously(const blink::WebURLRequest
& request
,
116 blink::WebURLLoaderClient
* client
) {
118 url_
= request
.url();
120 mojo::URLRequestPtr url_request
= mojo::URLRequest::From(request
);
121 url_request
->auto_follow_redirects
= false;
122 referrer_policy_
= request
.referrerPolicy();
124 if (request
.extraData()) {
125 WebURLRequestExtraData
* extra_data
=
126 static_cast<WebURLRequestExtraData
*>(request
.extraData());
127 base::ThreadTaskRunnerHandle::Get()->PostTask(
129 base::Bind(&WebURLLoaderImpl::OnReceivedResponse
,
130 weak_factory_
.GetWeakPtr(),
132 base::Passed(&extra_data
->synthetic_response
)));
136 blink::WebString uuid
;
137 if (web_blob_registry_
->GetUUIDForURL(url_
, &uuid
)) {
138 blink::WebVector
<blink::WebBlobData::Item
*> items
;
139 if (web_blob_registry_
->GetBlobItems(uuid
, &items
)) {
140 // The blob data exists in our service, and we don't want to create a
141 // data pipe just to do a funny dance where at the end, we stuff data
142 // from memory into data pipes so we can read back the data.
143 OnReceiveWebBlobData(request
, items
);
148 url_loader_
->Start(url_request
.Pass(),
149 base::Bind(&WebURLLoaderImpl::OnReceivedResponse
,
150 weak_factory_
.GetWeakPtr(), request
));
153 void WebURLLoaderImpl::cancel() {
155 response_body_stream_
.reset();
157 URLResponsePtr
failed_response(mojo::URLResponse::New());
158 failed_response
->url
= mojo::String::From(url_
);
159 failed_response
->error
= mojo::NetworkError::New();
160 failed_response
->error
->code
= net::ERR_ABORTED
;
162 base::ThreadTaskRunnerHandle::Get()->PostTask(
164 base::Bind(&WebURLLoaderImpl::OnReceivedResponse
,
165 weak_factory_
.GetWeakPtr(),
166 blink::WebURLRequest(),
167 base::Passed(&failed_response
)));
170 void WebURLLoaderImpl::setDefersLoading(bool defers_loading
) {
174 void WebURLLoaderImpl::OnReceivedResponse(const blink::WebURLRequest
& request
,
175 URLResponsePtr url_response
) {
176 url_
= GURL(url_response
->url
);
178 if (url_response
->error
) {
179 OnReceivedError(url_response
.Pass());
180 } else if (url_response
->redirect_url
) {
181 OnReceivedRedirect(request
, url_response
.Pass());
183 base::WeakPtr
<WebURLLoaderImpl
> self(weak_factory_
.GetWeakPtr());
184 client_
->didReceiveResponse(this, ToWebURLResponse(url_response
));
186 // We may have been deleted during didReceiveResponse.
190 // Start streaming data
191 response_body_stream_
= url_response
->body
.Pass();
196 void WebURLLoaderImpl::OnReceivedError(URLResponsePtr url_response
) {
197 blink::WebURLError web_error
;
198 web_error
.domain
= blink::WebString::fromUTF8(net::kErrorDomain
);
199 web_error
.reason
= url_response
->error
->code
;
200 web_error
.unreachableURL
= GURL(url_response
->url
);
201 web_error
.staleCopyInCache
= false;
202 web_error
.isCancellation
=
203 url_response
->error
->code
== net::ERR_ABORTED
? true : false;
205 client_
->didFail(this, web_error
);
208 void WebURLLoaderImpl::OnReceivedRedirect(const blink::WebURLRequest
& request
,
209 URLResponsePtr url_response
) {
210 // TODO(erg): setFirstPartyForCookies() and setHTTPReferrer() are unset here.
211 blink::WebURLRequest new_request
;
212 new_request
.initialize();
213 new_request
.setURL(GURL(url_response
->redirect_url
));
214 new_request
.setDownloadToFile(request
.downloadToFile());
215 new_request
.setRequestContext(request
.requestContext());
216 new_request
.setFrameType(request
.frameType());
217 new_request
.setSkipServiceWorker(request
.skipServiceWorker());
218 new_request
.setFetchRequestMode(request
.fetchRequestMode());
219 new_request
.setFetchCredentialsMode(request
.fetchCredentialsMode());
220 new_request
.setHTTPReferrer(
221 WebString::fromUTF8(url_response
->redirect_referrer
),
224 std::string old_method
= request
.httpMethod().utf8();
225 new_request
.setHTTPMethod(
226 blink::WebString::fromUTF8(url_response
->redirect_method
));
227 if (url_response
->redirect_method
== old_method
)
228 new_request
.setHTTPBody(request
.httpBody());
230 base::WeakPtr
<WebURLLoaderImpl
> self(weak_factory_
.GetWeakPtr());
231 client_
->willSendRequest(this, new_request
, ToWebURLResponse(url_response
));
232 // TODO(darin): Check if new_request was rejected.
234 // We may have been deleted during willSendRequest.
238 url_loader_
->FollowRedirect(
239 base::Bind(&WebURLLoaderImpl::OnReceivedResponse
,
240 weak_factory_
.GetWeakPtr(),
244 void WebURLLoaderImpl::OnReceiveWebBlobData(
245 const blink::WebURLRequest
& request
,
246 const blink::WebVector
<blink::WebBlobData::Item
*>& items
) {
247 blink::WebURLResponse result
;
250 result
.setHTTPStatusCode(200);
251 result
.setExpectedContentLength(-1); // Not available.
253 base::WeakPtr
<WebURLLoaderImpl
> self(weak_factory_
.GetWeakPtr());
254 client_
->didReceiveResponse(this, result
);
256 // We may have been deleted during didReceiveResponse.
260 // Send a receive data for each blob item.
261 for (size_t i
= 0; i
< items
.size(); ++i
) {
262 const int data_size
= base::checked_cast
<int>(items
[i
]->data
.size());
263 client_
->didReceiveData(this, items
[i
]->data
.data(), data_size
, -1);
266 // Send a closing finish.
267 double finish_time
= base::Time::Now().ToDoubleT();
268 client_
->didFinishLoading(
269 this, finish_time
, blink::WebURLLoaderClient::kUnknownEncodedDataLength
);
272 void WebURLLoaderImpl::ReadMore() {
275 MojoResult rv
= BeginReadDataRaw(response_body_stream_
.get(),
278 MOJO_READ_DATA_FLAG_NONE
);
279 if (rv
== MOJO_RESULT_OK
) {
280 base::WeakPtr
<WebURLLoaderImpl
> self(weak_factory_
.GetWeakPtr());
281 client_
->didReceiveData(this, static_cast<const char*>(buf
), buf_size
, -1);
282 // We may have been deleted during didReceiveData.
285 EndReadDataRaw(response_body_stream_
.get(), buf_size
);
287 } else if (rv
== MOJO_RESULT_SHOULD_WAIT
) {
289 } else if (rv
== MOJO_RESULT_FAILED_PRECONDITION
) {
290 // We reached end-of-file.
291 double finish_time
= base::Time::Now().ToDoubleT();
292 client_
->didFinishLoading(
295 blink::WebURLLoaderClient::kUnknownEncodedDataLength
);
297 // TODO(darin): Oops!
301 void WebURLLoaderImpl::WaitToReadMore() {
302 handle_watcher_
.Start(
303 response_body_stream_
.get(),
304 MOJO_HANDLE_SIGNAL_READABLE
,
305 MOJO_DEADLINE_INDEFINITE
,
306 base::Bind(&WebURLLoaderImpl::OnResponseBodyStreamReady
,
307 weak_factory_
.GetWeakPtr()));
310 void WebURLLoaderImpl::OnResponseBodyStreamReady(MojoResult result
) {
314 } // namespace html_viewer