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/url_type_converters.h"
15 #include "mojo/services/network/public/interfaces/url_loader_factory.mojom.h"
16 #include "net/base/net_errors.h"
17 #include "third_party/WebKit/public/platform/WebURLError.h"
18 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
19 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
20 #include "third_party/WebKit/public/platform/WebURLResponse.h"
22 using blink::WebString
;
23 using mojo::URLResponsePtr
;
25 namespace html_viewer
{
28 blink::WebURLResponse::HTTPVersion
StatusLineToHTTPVersion(
29 const mojo::String
& status_line
) {
30 if (status_line
.is_null())
31 return blink::WebURLResponse::HTTP_0_9
;
33 if (base::StartsWithASCII(status_line
, "HTTP/1.0", true))
34 return blink::WebURLResponse::HTTP_1_0
;
36 if (base::StartsWithASCII(status_line
, "HTTP/1.1", true))
37 return blink::WebURLResponse::HTTP_1_1
;
39 return blink::WebURLResponse::Unknown
;
42 blink::WebURLResponse
ToWebURLResponse(const URLResponsePtr
& url_response
) {
43 blink::WebURLResponse result
;
45 result
.setURL(GURL(url_response
->url
));
46 result
.setMIMEType(blink::WebString::fromUTF8(url_response
->mime_type
));
47 result
.setTextEncodingName(blink::WebString::fromUTF8(url_response
->charset
));
48 result
.setHTTPVersion(StatusLineToHTTPVersion(url_response
->status_line
));
49 result
.setHTTPStatusCode(url_response
->status_code
);
50 result
.setExpectedContentLength(-1); // Not available.
52 // TODO(darin): Initialize timing properly.
53 blink::WebURLLoadTiming timing
;
55 result
.setLoadTiming(timing
);
57 for (size_t i
= 0; i
< url_response
->headers
.size(); ++i
) {
58 result
.setHTTPHeaderField(
59 blink::WebString::fromUTF8(url_response
->headers
[i
]->name
),
60 blink::WebString::fromUTF8(url_response
->headers
[i
]->value
));
68 WebURLRequestExtraData::WebURLRequestExtraData() {
71 WebURLRequestExtraData::~WebURLRequestExtraData() {
74 WebURLLoaderImpl::WebURLLoaderImpl(mojo::URLLoaderFactory
* url_loader_factory
,
75 MockWebBlobRegistryImpl
* web_blob_registry
)
77 web_blob_registry_(web_blob_registry
),
78 referrer_policy_(blink::WebReferrerPolicyDefault
),
80 url_loader_factory
->CreateURLLoader(GetProxy(&url_loader_
));
83 WebURLLoaderImpl::~WebURLLoaderImpl() {
86 void WebURLLoaderImpl::loadSynchronously(
87 const blink::WebURLRequest
& request
,
88 blink::WebURLResponse
& response
,
89 blink::WebURLError
& error
,
90 blink::WebData
& data
) {
94 void WebURLLoaderImpl::loadAsynchronously(const blink::WebURLRequest
& request
,
95 blink::WebURLLoaderClient
* client
) {
99 mojo::URLRequestPtr url_request
= mojo::URLRequest::From(request
);
100 url_request
->auto_follow_redirects
= false;
101 referrer_policy_
= request
.referrerPolicy();
103 if (request
.extraData()) {
104 WebURLRequestExtraData
* extra_data
=
105 static_cast<WebURLRequestExtraData
*>(request
.extraData());
106 base::ThreadTaskRunnerHandle::Get()->PostTask(
108 base::Bind(&WebURLLoaderImpl::OnReceivedResponse
,
109 weak_factory_
.GetWeakPtr(),
111 base::Passed(&extra_data
->synthetic_response
)));
115 blink::WebString uuid
;
116 if (web_blob_registry_
->GetUUIDForURL(url_
, &uuid
)) {
117 blink::WebVector
<blink::WebBlobData::Item
*> items
;
118 if (web_blob_registry_
->GetBlobItems(uuid
, &items
)) {
119 // The blob data exists in our service, and we don't want to create a
120 // data pipe just to do a funny dance where at the end, we stuff data
121 // from memory into data pipes so we can read back the data.
122 OnReceiveWebBlobData(request
, items
);
127 url_loader_
->Start(url_request
.Pass(),
128 base::Bind(&WebURLLoaderImpl::OnReceivedResponse
,
129 weak_factory_
.GetWeakPtr(), request
));
132 void WebURLLoaderImpl::cancel() {
134 response_body_stream_
.reset();
136 URLResponsePtr
failed_response(mojo::URLResponse::New());
137 failed_response
->url
= mojo::String::From(url_
);
138 failed_response
->error
= mojo::NetworkError::New();
139 failed_response
->error
->code
= net::ERR_ABORTED
;
141 base::ThreadTaskRunnerHandle::Get()->PostTask(
143 base::Bind(&WebURLLoaderImpl::OnReceivedResponse
,
144 weak_factory_
.GetWeakPtr(),
145 blink::WebURLRequest(),
146 base::Passed(&failed_response
)));
149 void WebURLLoaderImpl::setDefersLoading(bool defers_loading
) {
153 void WebURLLoaderImpl::OnReceivedResponse(const blink::WebURLRequest
& request
,
154 URLResponsePtr url_response
) {
155 url_
= GURL(url_response
->url
);
157 if (url_response
->error
) {
158 OnReceivedError(url_response
.Pass());
159 } else if (url_response
->redirect_url
) {
160 OnReceivedRedirect(request
, url_response
.Pass());
162 base::WeakPtr
<WebURLLoaderImpl
> self(weak_factory_
.GetWeakPtr());
163 client_
->didReceiveResponse(this, ToWebURLResponse(url_response
));
165 // We may have been deleted during didReceiveResponse.
169 // Start streaming data
170 response_body_stream_
= url_response
->body
.Pass();
175 void WebURLLoaderImpl::OnReceivedError(URLResponsePtr url_response
) {
176 blink::WebURLError web_error
;
177 web_error
.domain
= blink::WebString::fromUTF8(net::kErrorDomain
);
178 web_error
.reason
= url_response
->error
->code
;
179 web_error
.unreachableURL
= GURL(url_response
->url
);
180 web_error
.staleCopyInCache
= false;
181 web_error
.isCancellation
=
182 url_response
->error
->code
== net::ERR_ABORTED
? true : false;
184 client_
->didFail(this, web_error
);
187 void WebURLLoaderImpl::OnReceivedRedirect(const blink::WebURLRequest
& request
,
188 URLResponsePtr url_response
) {
189 // TODO(erg): setFirstPartyForCookies() and setHTTPReferrer() are unset here.
190 blink::WebURLRequest new_request
;
191 new_request
.initialize();
192 new_request
.setURL(GURL(url_response
->redirect_url
));
193 new_request
.setDownloadToFile(request
.downloadToFile());
194 new_request
.setRequestContext(request
.requestContext());
195 new_request
.setFrameType(request
.frameType());
196 new_request
.setSkipServiceWorker(request
.skipServiceWorker());
197 new_request
.setFetchRequestMode(request
.fetchRequestMode());
198 new_request
.setFetchCredentialsMode(request
.fetchCredentialsMode());
199 new_request
.setHTTPReferrer(
200 WebString::fromUTF8(url_response
->redirect_referrer
),
203 std::string old_method
= request
.httpMethod().utf8();
204 new_request
.setHTTPMethod(
205 blink::WebString::fromUTF8(url_response
->redirect_method
));
206 if (url_response
->redirect_method
== old_method
)
207 new_request
.setHTTPBody(request
.httpBody());
209 base::WeakPtr
<WebURLLoaderImpl
> self(weak_factory_
.GetWeakPtr());
210 client_
->willSendRequest(this, new_request
, ToWebURLResponse(url_response
));
211 // TODO(darin): Check if new_request was rejected.
213 // We may have been deleted during willSendRequest.
217 url_loader_
->FollowRedirect(
218 base::Bind(&WebURLLoaderImpl::OnReceivedResponse
,
219 weak_factory_
.GetWeakPtr(),
223 void WebURLLoaderImpl::OnReceiveWebBlobData(
224 const blink::WebURLRequest
& request
,
225 const blink::WebVector
<blink::WebBlobData::Item
*>& items
) {
226 blink::WebURLResponse result
;
229 result
.setHTTPStatusCode(200);
230 result
.setExpectedContentLength(-1); // Not available.
232 base::WeakPtr
<WebURLLoaderImpl
> self(weak_factory_
.GetWeakPtr());
233 client_
->didReceiveResponse(this, result
);
235 // We may have been deleted during didReceiveResponse.
239 // Send a receive data for each blob item.
240 for (size_t i
= 0; i
< items
.size(); ++i
) {
241 const int data_size
= base::checked_cast
<int>(items
[i
]->data
.size());
242 client_
->didReceiveData(this, items
[i
]->data
.data(), data_size
, -1);
245 // Send a closing finish.
246 double finish_time
= base::Time::Now().ToDoubleT();
247 client_
->didFinishLoading(
248 this, finish_time
, blink::WebURLLoaderClient::kUnknownEncodedDataLength
);
251 void WebURLLoaderImpl::ReadMore() {
254 MojoResult rv
= BeginReadDataRaw(response_body_stream_
.get(),
257 MOJO_READ_DATA_FLAG_NONE
);
258 if (rv
== MOJO_RESULT_OK
) {
259 base::WeakPtr
<WebURLLoaderImpl
> self(weak_factory_
.GetWeakPtr());
260 client_
->didReceiveData(this, static_cast<const char*>(buf
), buf_size
, -1);
261 // We may have been deleted during didReceiveData.
264 EndReadDataRaw(response_body_stream_
.get(), buf_size
);
266 } else if (rv
== MOJO_RESULT_SHOULD_WAIT
) {
268 } else if (rv
== MOJO_RESULT_FAILED_PRECONDITION
) {
269 // We reached end-of-file.
270 double finish_time
= base::Time::Now().ToDoubleT();
271 client_
->didFinishLoading(
274 blink::WebURLLoaderClient::kUnknownEncodedDataLength
);
276 // TODO(darin): Oops!
280 void WebURLLoaderImpl::WaitToReadMore() {
281 handle_watcher_
.Start(
282 response_body_stream_
.get(),
283 MOJO_HANDLE_SIGNAL_READABLE
,
284 MOJO_DEADLINE_INDEFINITE
,
285 base::Bind(&WebURLLoaderImpl::OnResponseBodyStreamReady
,
286 weak_factory_
.GetWeakPtr()));
289 void WebURLLoaderImpl::OnResponseBodyStreamReady(MojoResult result
) {
293 } // namespace html_viewer