Fix max_samples in command buffer capabilities.
[chromium-blink-merge.git] / content / child / web_url_loader_impl.cc
blob818f5d525e30c74f81981a653c312092eb9e94dd
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 "content/child/web_url_loader_impl.h"
7 #include <algorithm>
8 #include <deque>
9 #include <string>
11 #include "base/bind.h"
12 #include "base/command_line.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/single_thread_task_runner.h"
16 #include "base/strings/string_util.h"
17 #include "base/time/time.h"
18 #include "components/mime_util/mime_util.h"
19 #include "content/child/child_thread_impl.h"
20 #include "content/child/ftp_directory_listing_response_delegate.h"
21 #include "content/child/multipart_response_delegate.h"
22 #include "content/child/request_extra_data.h"
23 #include "content/child/request_info.h"
24 #include "content/child/resource_dispatcher.h"
25 #include "content/child/sync_load_response.h"
26 #include "content/child/web_data_consumer_handle_impl.h"
27 #include "content/child/web_url_request_util.h"
28 #include "content/child/weburlresponse_extradata_impl.h"
29 #include "content/common/resource_messages.h"
30 #include "content/common/resource_request_body.h"
31 #include "content/common/service_worker/service_worker_types.h"
32 #include "content/public/child/request_peer.h"
33 #include "content/public/common/content_switches.h"
34 #include "net/base/data_url.h"
35 #include "net/base/filename_util.h"
36 #include "net/base/net_errors.h"
37 #include "net/http/http_response_headers.h"
38 #include "net/http/http_util.h"
39 #include "net/url_request/redirect_info.h"
40 #include "net/url_request/url_request_data_job.h"
41 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h"
42 #include "third_party/WebKit/public/platform/WebURL.h"
43 #include "third_party/WebKit/public/platform/WebURLError.h"
44 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
45 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
46 #include "third_party/WebKit/public/platform/WebURLRequest.h"
47 #include "third_party/WebKit/public/platform/WebURLResponse.h"
48 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
49 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h"
51 using base::Time;
52 using base::TimeTicks;
53 using blink::WebData;
54 using blink::WebHTTPBody;
55 using blink::WebHTTPHeaderVisitor;
56 using blink::WebHTTPLoadInfo;
57 using blink::WebReferrerPolicy;
58 using blink::WebSecurityPolicy;
59 using blink::WebString;
60 using blink::WebURL;
61 using blink::WebURLError;
62 using blink::WebURLLoadTiming;
63 using blink::WebURLLoader;
64 using blink::WebURLLoaderClient;
65 using blink::WebURLRequest;
66 using blink::WebURLResponse;
68 namespace content {
70 // Utilities ------------------------------------------------------------------
72 namespace {
74 const size_t kBodyStreamPipeCapacity = 4 * 1024;
76 typedef ResourceDevToolsInfo::HeadersVector HeadersVector;
78 // Converts timing data from |load_timing| to the format used by WebKit.
79 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing,
80 WebURLLoadTiming* url_timing) {
81 DCHECK(!load_timing.request_start.is_null());
83 const TimeTicks kNullTicks;
84 url_timing->initialize();
85 url_timing->setRequestTime(
86 (load_timing.request_start - kNullTicks).InSecondsF());
87 url_timing->setProxyStart(
88 (load_timing.proxy_resolve_start - kNullTicks).InSecondsF());
89 url_timing->setProxyEnd(
90 (load_timing.proxy_resolve_end - kNullTicks).InSecondsF());
91 url_timing->setDNSStart(
92 (load_timing.connect_timing.dns_start - kNullTicks).InSecondsF());
93 url_timing->setDNSEnd(
94 (load_timing.connect_timing.dns_end - kNullTicks).InSecondsF());
95 url_timing->setConnectStart(
96 (load_timing.connect_timing.connect_start - kNullTicks).InSecondsF());
97 url_timing->setConnectEnd(
98 (load_timing.connect_timing.connect_end - kNullTicks).InSecondsF());
99 url_timing->setSSLStart(
100 (load_timing.connect_timing.ssl_start - kNullTicks).InSecondsF());
101 url_timing->setSSLEnd(
102 (load_timing.connect_timing.ssl_end - kNullTicks).InSecondsF());
103 url_timing->setSendStart(
104 (load_timing.send_start - kNullTicks).InSecondsF());
105 url_timing->setSendEnd(
106 (load_timing.send_end - kNullTicks).InSecondsF());
107 url_timing->setReceiveHeadersEnd(
108 (load_timing.receive_headers_end - kNullTicks).InSecondsF());
111 net::RequestPriority ConvertWebKitPriorityToNetPriority(
112 const WebURLRequest::Priority& priority) {
113 switch (priority) {
114 case WebURLRequest::PriorityVeryHigh:
115 return net::HIGHEST;
117 case WebURLRequest::PriorityHigh:
118 return net::MEDIUM;
120 case WebURLRequest::PriorityMedium:
121 return net::LOW;
123 case WebURLRequest::PriorityLow:
124 return net::LOWEST;
126 case WebURLRequest::PriorityVeryLow:
127 return net::IDLE;
129 case WebURLRequest::PriorityUnresolved:
130 default:
131 NOTREACHED();
132 return net::LOW;
136 // Extracts info from a data scheme URL into |info| and |data|. Returns net::OK
137 // if successful. Returns a net error code otherwise. Exported only for testing.
138 int GetInfoFromDataURL(const GURL& url,
139 ResourceResponseInfo* info,
140 std::string* data) {
141 // Assure same time for all time fields of data: URLs.
142 Time now = Time::Now();
143 info->load_timing.request_start = TimeTicks::Now();
144 info->load_timing.request_start_time = now;
145 info->request_time = now;
146 info->response_time = now;
148 std::string mime_type;
149 std::string charset;
150 scoped_refptr<net::HttpResponseHeaders> headers(
151 new net::HttpResponseHeaders(std::string()));
152 int result = net::URLRequestDataJob::BuildResponse(
153 url, &mime_type, &charset, data, headers.get());
154 if (result != net::OK)
155 return result;
157 info->headers = headers;
158 info->mime_type.swap(mime_type);
159 info->charset.swap(charset);
160 info->security_info.clear();
161 info->content_length = data->length();
162 info->encoded_data_length = 0;
164 return net::OK;
167 #define STATIC_ASSERT_MATCHING_ENUMS(content_name, blink_name) \
168 static_assert( \
169 static_cast<int>(content_name) == static_cast<int>(blink_name), \
170 "mismatching enums: " #content_name)
172 STATIC_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_SAME_ORIGIN,
173 WebURLRequest::FetchRequestModeSameOrigin);
174 STATIC_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_NO_CORS,
175 WebURLRequest::FetchRequestModeNoCORS);
176 STATIC_ASSERT_MATCHING_ENUMS(FETCH_REQUEST_MODE_CORS,
177 WebURLRequest::FetchRequestModeCORS);
178 STATIC_ASSERT_MATCHING_ENUMS(
179 FETCH_REQUEST_MODE_CORS_WITH_FORCED_PREFLIGHT,
180 WebURLRequest::FetchRequestModeCORSWithForcedPreflight);
182 FetchRequestMode GetFetchRequestMode(const WebURLRequest& request) {
183 return static_cast<FetchRequestMode>(request.fetchRequestMode());
186 STATIC_ASSERT_MATCHING_ENUMS(FETCH_CREDENTIALS_MODE_OMIT,
187 WebURLRequest::FetchCredentialsModeOmit);
188 STATIC_ASSERT_MATCHING_ENUMS(FETCH_CREDENTIALS_MODE_SAME_ORIGIN,
189 WebURLRequest::FetchCredentialsModeSameOrigin);
190 STATIC_ASSERT_MATCHING_ENUMS(FETCH_CREDENTIALS_MODE_INCLUDE,
191 WebURLRequest::FetchCredentialsModeInclude);
193 FetchCredentialsMode GetFetchCredentialsMode(const WebURLRequest& request) {
194 return static_cast<FetchCredentialsMode>(request.fetchCredentialsMode());
197 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_FRAME_TYPE_AUXILIARY,
198 WebURLRequest::FrameTypeAuxiliary);
199 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_FRAME_TYPE_NESTED,
200 WebURLRequest::FrameTypeNested);
201 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_FRAME_TYPE_NONE,
202 WebURLRequest::FrameTypeNone);
203 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL,
204 WebURLRequest::FrameTypeTopLevel);
206 RequestContextFrameType GetRequestContextFrameType(
207 const WebURLRequest& request) {
208 return static_cast<RequestContextFrameType>(request.frameType());
211 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_UNSPECIFIED,
212 WebURLRequest::RequestContextUnspecified);
213 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_AUDIO,
214 WebURLRequest::RequestContextAudio);
215 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_BEACON,
216 WebURLRequest::RequestContextBeacon);
217 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_CSP_REPORT,
218 WebURLRequest::RequestContextCSPReport);
219 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_DOWNLOAD,
220 WebURLRequest::RequestContextDownload);
221 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_EMBED,
222 WebURLRequest::RequestContextEmbed);
223 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_EVENT_SOURCE,
224 WebURLRequest::RequestContextEventSource);
225 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FAVICON,
226 WebURLRequest::RequestContextFavicon);
227 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FETCH,
228 WebURLRequest::RequestContextFetch);
229 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FONT,
230 WebURLRequest::RequestContextFont);
231 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FORM,
232 WebURLRequest::RequestContextForm);
233 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_FRAME,
234 WebURLRequest::RequestContextFrame);
235 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_HYPERLINK,
236 WebURLRequest::RequestContextHyperlink);
237 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_IFRAME,
238 WebURLRequest::RequestContextIframe);
239 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_IMAGE,
240 WebURLRequest::RequestContextImage);
241 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_IMAGE_SET,
242 WebURLRequest::RequestContextImageSet);
243 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_IMPORT,
244 WebURLRequest::RequestContextImport);
245 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_INTERNAL,
246 WebURLRequest::RequestContextInternal);
247 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_LOCATION,
248 WebURLRequest::RequestContextLocation);
249 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_MANIFEST,
250 WebURLRequest::RequestContextManifest);
251 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_OBJECT,
252 WebURLRequest::RequestContextObject);
253 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_PING,
254 WebURLRequest::RequestContextPing);
255 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_PLUGIN,
256 WebURLRequest::RequestContextPlugin);
257 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_PREFETCH,
258 WebURLRequest::RequestContextPrefetch);
259 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_SCRIPT,
260 WebURLRequest::RequestContextScript);
261 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_SERVICE_WORKER,
262 WebURLRequest::RequestContextServiceWorker);
263 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_SHARED_WORKER,
264 WebURLRequest::RequestContextSharedWorker);
265 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_SUBRESOURCE,
266 WebURLRequest::RequestContextSubresource);
267 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_STYLE,
268 WebURLRequest::RequestContextStyle);
269 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_TRACK,
270 WebURLRequest::RequestContextTrack);
271 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_VIDEO,
272 WebURLRequest::RequestContextVideo);
273 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_WORKER,
274 WebURLRequest::RequestContextWorker);
275 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_XML_HTTP_REQUEST,
276 WebURLRequest::RequestContextXMLHttpRequest);
277 STATIC_ASSERT_MATCHING_ENUMS(REQUEST_CONTEXT_TYPE_XSLT,
278 WebURLRequest::RequestContextXSLT);
280 RequestContextType GetRequestContextType(const WebURLRequest& request) {
281 return static_cast<RequestContextType>(request.requestContext());
284 } // namespace
286 // WebURLLoaderImpl::Context --------------------------------------------------
288 // This inner class exists since the WebURLLoader may be deleted while inside a
289 // call to WebURLLoaderClient. Refcounting is to keep the context from being
290 // deleted if it may have work to do after calling into the client.
291 class WebURLLoaderImpl::Context : public base::RefCounted<Context>,
292 public RequestPeer {
293 public:
294 Context(WebURLLoaderImpl* loader,
295 ResourceDispatcher* resource_dispatcher,
296 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
298 WebURLLoaderClient* client() const { return client_; }
299 void set_client(WebURLLoaderClient* client) { client_ = client; }
301 void Cancel();
302 void SetDefersLoading(bool value);
303 void DidChangePriority(WebURLRequest::Priority new_priority,
304 int intra_priority_value);
305 bool AttachThreadedDataReceiver(
306 blink::WebThreadedDataReceiver* threaded_data_receiver);
307 void Start(const WebURLRequest& request,
308 SyncLoadResponse* sync_load_response);
310 // RequestPeer methods:
311 void OnUploadProgress(uint64 position, uint64 size) override;
312 bool OnReceivedRedirect(const net::RedirectInfo& redirect_info,
313 const ResourceResponseInfo& info) override;
314 void OnReceivedResponse(const ResourceResponseInfo& info) override;
315 void OnDownloadedData(int len, int encoded_data_length) override;
316 void OnReceivedData(const char* data,
317 int data_length,
318 int encoded_data_length) override;
319 void OnReceivedCachedMetadata(const char* data, int len) override;
320 void OnCompletedRequest(int error_code,
321 bool was_ignored_by_handler,
322 bool stale_copy_in_cache,
323 const std::string& security_info,
324 const base::TimeTicks& completion_time,
325 int64 total_transfer_size) override;
327 private:
328 friend class base::RefCounted<Context>;
329 ~Context() override;
331 // We can optimize the handling of data URLs in most cases.
332 bool CanHandleDataURLRequestLocally() const;
333 void HandleDataURL();
334 MojoResult WriteDataOnBodyStream(const char* data, size_t size);
335 void OnHandleGotWritable(MojoResult);
337 WebURLLoaderImpl* loader_;
338 WebURLRequest request_;
339 WebURLLoaderClient* client_;
340 ResourceDispatcher* resource_dispatcher_;
341 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
342 WebReferrerPolicy referrer_policy_;
343 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_;
344 scoped_ptr<MultipartResponseDelegate> multipart_delegate_;
345 scoped_ptr<StreamOverrideParameters> stream_override_;
346 mojo::ScopedDataPipeProducerHandle body_stream_writer_;
347 mojo::common::HandleWatcher body_stream_writer_watcher_;
348 // TODO(yhirano): Delete this buffer after implementing the back-pressure
349 // mechanism.
350 std::deque<char> body_stream_buffer_;
351 bool got_all_stream_body_data_;
352 enum DeferState {NOT_DEFERRING, SHOULD_DEFER, DEFERRED_DATA};
353 DeferState defers_loading_;
354 int request_id_;
357 WebURLLoaderImpl::Context::Context(
358 WebURLLoaderImpl* loader,
359 ResourceDispatcher* resource_dispatcher,
360 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
361 : loader_(loader),
362 client_(NULL),
363 resource_dispatcher_(resource_dispatcher),
364 task_runner_(task_runner),
365 referrer_policy_(blink::WebReferrerPolicyDefault),
366 got_all_stream_body_data_(false),
367 defers_loading_(NOT_DEFERRING),
368 request_id_(-1) {
371 void WebURLLoaderImpl::Context::Cancel() {
372 if (resource_dispatcher_ && // NULL in unittest.
373 request_id_ != -1) {
374 resource_dispatcher_->Cancel(request_id_);
375 request_id_ = -1;
378 // Ensure that we do not notify the multipart delegate anymore as it has
379 // its own pointer to the client.
380 if (multipart_delegate_)
381 multipart_delegate_->Cancel();
382 // Ditto for the ftp delegate.
383 if (ftp_listing_delegate_)
384 ftp_listing_delegate_->Cancel();
386 // Do not make any further calls to the client.
387 client_ = NULL;
388 loader_ = NULL;
391 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) {
392 if (request_id_ != -1)
393 resource_dispatcher_->SetDefersLoading(request_id_, value);
394 if (value && defers_loading_ == NOT_DEFERRING) {
395 defers_loading_ = SHOULD_DEFER;
396 } else if (!value && defers_loading_ != NOT_DEFERRING) {
397 if (defers_loading_ == DEFERRED_DATA) {
398 task_runner_->PostTask(FROM_HERE,
399 base::Bind(&Context::HandleDataURL, this));
401 defers_loading_ = NOT_DEFERRING;
405 void WebURLLoaderImpl::Context::DidChangePriority(
406 WebURLRequest::Priority new_priority, int intra_priority_value) {
407 if (request_id_ != -1) {
408 resource_dispatcher_->DidChangePriority(
409 request_id_,
410 ConvertWebKitPriorityToNetPriority(new_priority),
411 intra_priority_value);
415 bool WebURLLoaderImpl::Context::AttachThreadedDataReceiver(
416 blink::WebThreadedDataReceiver* threaded_data_receiver) {
417 if (request_id_ != -1) {
418 return resource_dispatcher_->AttachThreadedDataReceiver(
419 request_id_, threaded_data_receiver);
422 return false;
425 void WebURLLoaderImpl::Context::Start(const WebURLRequest& request,
426 SyncLoadResponse* sync_load_response) {
427 DCHECK(request_id_ == -1);
428 request_ = request; // Save the request.
429 if (request.extraData()) {
430 RequestExtraData* extra_data =
431 static_cast<RequestExtraData*>(request.extraData());
432 stream_override_ = extra_data->TakeStreamOverrideOwnership();
435 GURL url = request.url();
437 // PlzNavigate: during navigation, the renderer should request a stream which
438 // contains the body of the response. The request has already been made by the
439 // browser.
440 if (stream_override_.get()) {
441 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
442 switches::kEnableBrowserSideNavigation));
443 DCHECK(!sync_load_response);
444 DCHECK_NE(WebURLRequest::FrameTypeNone, request.frameType());
445 DCHECK_EQ("GET", request.httpMethod().latin1());
446 url = stream_override_->stream_url;
449 if (CanHandleDataURLRequestLocally()) {
450 if (sync_load_response) {
451 // This is a sync load. Do the work now.
452 sync_load_response->url = url;
453 sync_load_response->error_code =
454 GetInfoFromDataURL(sync_load_response->url, sync_load_response,
455 &sync_load_response->data);
456 } else {
457 task_runner_->PostTask(FROM_HERE,
458 base::Bind(&Context::HandleDataURL, this));
460 return;
463 // PlzNavigate: outside of tests, the only navigation requests going through
464 // the WebURLLoader are the ones created by CommitNavigation. Several browser
465 // tests load HTML directly through a data url which will be handled by the
466 // block above.
467 DCHECK_IMPLIES(base::CommandLine::ForCurrentProcess()->HasSwitch(
468 switches::kEnableBrowserSideNavigation),
469 stream_override_.get() ||
470 request.frameType() == WebURLRequest::FrameTypeNone);
472 GURL referrer_url(
473 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1());
474 const std::string& method = request.httpMethod().latin1();
476 // TODO(brettw) this should take parameter encoding into account when
477 // creating the GURLs.
479 // TODO(horo): Check credentials flag is unset when credentials mode is omit.
480 // Check credentials flag is set when credentials mode is include.
482 RequestInfo request_info;
483 request_info.method = method;
484 request_info.url = url;
485 request_info.first_party_for_cookies = request.firstPartyForCookies();
486 referrer_policy_ = request.referrerPolicy();
487 request_info.referrer = Referrer(referrer_url, referrer_policy_);
488 request_info.headers = GetWebURLRequestHeaders(request);
489 request_info.load_flags = GetLoadFlagsForWebURLRequest(request);
490 request_info.enable_load_timing = true;
491 request_info.enable_upload_progress = request.reportUploadProgress();
492 if (request.requestContext() == WebURLRequest::RequestContextXMLHttpRequest &&
493 (url.has_username() || url.has_password())) {
494 request_info.do_not_prompt_for_login = true;
496 // requestor_pid only needs to be non-zero if the request originates outside
497 // the render process, so we can use requestorProcessID even for requests
498 // from in-process plugins.
499 request_info.requestor_pid = request.requestorProcessID();
500 request_info.request_type = WebURLRequestToResourceType(request);
501 request_info.priority =
502 ConvertWebKitPriorityToNetPriority(request.priority());
503 request_info.appcache_host_id = request.appCacheHostID();
504 request_info.routing_id = request.requestorID();
505 request_info.download_to_file = request.downloadToFile();
506 request_info.has_user_gesture = request.hasUserGesture();
507 request_info.skip_service_worker = request.skipServiceWorker();
508 request_info.should_reset_appcache = request.shouldResetAppCache();
509 request_info.fetch_request_mode = GetFetchRequestMode(request);
510 request_info.fetch_credentials_mode = GetFetchCredentialsMode(request);
511 request_info.fetch_request_context_type = GetRequestContextType(request);
512 request_info.fetch_frame_type = GetRequestContextFrameType(request);
513 request_info.extra_data = request.extraData();
515 scoped_refptr<ResourceRequestBody> request_body =
516 GetRequestBodyForWebURLRequest(request).get();
518 if (sync_load_response) {
519 resource_dispatcher_->StartSync(
520 request_info, request_body.get(), sync_load_response);
521 return;
524 request_id_ = resource_dispatcher_->StartAsync(
525 request_info, request_body.get(), this);
528 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) {
529 if (client_)
530 client_->didSendData(loader_, position, size);
533 bool WebURLLoaderImpl::Context::OnReceivedRedirect(
534 const net::RedirectInfo& redirect_info,
535 const ResourceResponseInfo& info) {
536 if (!client_)
537 return false;
539 WebURLResponse response;
540 response.initialize();
541 PopulateURLResponse(request_.url(), info, &response);
543 // TODO(darin): We lack sufficient information to construct the actual
544 // request that resulted from the redirect.
545 WebURLRequest new_request(redirect_info.new_url);
546 new_request.setFirstPartyForCookies(
547 redirect_info.new_first_party_for_cookies);
548 new_request.setDownloadToFile(request_.downloadToFile());
549 new_request.setRequestContext(request_.requestContext());
550 new_request.setFrameType(request_.frameType());
551 new_request.setSkipServiceWorker(request_.skipServiceWorker());
552 new_request.setShouldResetAppCache(request_.shouldResetAppCache());
553 new_request.setFetchRequestMode(request_.fetchRequestMode());
554 new_request.setFetchCredentialsMode(request_.fetchCredentialsMode());
556 new_request.setHTTPReferrer(WebString::fromUTF8(redirect_info.new_referrer),
557 referrer_policy_);
559 std::string old_method = request_.httpMethod().utf8();
560 new_request.setHTTPMethod(WebString::fromUTF8(redirect_info.new_method));
561 if (redirect_info.new_method == old_method)
562 new_request.setHTTPBody(request_.httpBody());
564 // Protect from deletion during call to willSendRequest.
565 scoped_refptr<Context> protect(this);
567 client_->willSendRequest(loader_, new_request, response);
568 request_ = new_request;
570 // Only follow the redirect if WebKit left the URL unmodified.
571 if (redirect_info.new_url == GURL(new_request.url())) {
572 // First-party cookie logic moved from DocumentLoader in Blink to
573 // net::URLRequest in the browser. Assert that Blink didn't try to change it
574 // to something else.
575 DCHECK_EQ(redirect_info.new_first_party_for_cookies.spec(),
576 request_.firstPartyForCookies().string().utf8());
577 return true;
580 // We assume that WebKit only changes the URL to suppress a redirect, and we
581 // assume that it does so by setting it to be invalid.
582 DCHECK(!new_request.url().isValid());
583 return false;
586 void WebURLLoaderImpl::Context::OnReceivedResponse(
587 const ResourceResponseInfo& initial_info) {
588 if (!client_)
589 return;
591 ResourceResponseInfo info = initial_info;
593 // PlzNavigate: during navigations, the ResourceResponse has already been
594 // received on the browser side, and has been passed down to the renderer.
595 if (stream_override_.get()) {
596 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
597 switches::kEnableBrowserSideNavigation));
598 info = stream_override_->response;
601 WebURLResponse response;
602 response.initialize();
603 PopulateURLResponse(request_.url(), info, &response);
605 bool show_raw_listing = (GURL(request_.url()).query() == "raw");
607 if (info.mime_type == "text/vnd.chromium.ftp-dir") {
608 if (show_raw_listing) {
609 // Set the MIME type to plain text to prevent any active content.
610 response.setMIMEType("text/plain");
611 } else {
612 // We're going to produce a parsed listing in HTML.
613 response.setMIMEType("text/html");
617 // Prevent |this| from being destroyed if the client destroys the loader,
618 // ether in didReceiveResponse, or when the multipart/ftp delegate calls into
619 // it.
620 scoped_refptr<Context> protect(this);
622 if (request_.useStreamOnResponse()) {
623 MojoCreateDataPipeOptions options;
624 options.struct_size = sizeof(MojoCreateDataPipeOptions);
625 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
626 options.element_num_bytes = 1;
627 options.capacity_num_bytes = kBodyStreamPipeCapacity;
629 mojo::ScopedDataPipeConsumerHandle consumer;
630 MojoResult result = mojo::CreateDataPipe(&options,
631 &body_stream_writer_,
632 &consumer);
633 if (result != MOJO_RESULT_OK) {
634 // TODO(yhirano): Handle the error.
635 return;
637 client_->didReceiveResponse(
638 loader_, response, new WebDataConsumerHandleImpl(consumer.Pass()));
639 } else {
640 client_->didReceiveResponse(loader_, response);
643 // We may have been cancelled after didReceiveResponse, which would leave us
644 // without a client and therefore without much need to do further handling.
645 if (!client_)
646 return;
648 DCHECK(!ftp_listing_delegate_.get());
649 DCHECK(!multipart_delegate_.get());
650 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") {
651 std::string content_type;
652 info.headers->EnumerateHeader(NULL, "content-type", &content_type);
654 std::string mime_type;
655 std::string charset;
656 bool had_charset = false;
657 std::string boundary;
658 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset,
659 &had_charset, &boundary);
660 base::TrimString(boundary, " \"", &boundary);
662 // If there's no boundary, just handle the request normally. In the gecko
663 // code, nsMultiMixedConv::OnStartRequest throws an exception.
664 if (!boundary.empty()) {
665 multipart_delegate_.reset(
666 new MultipartResponseDelegate(client_, loader_, response, boundary));
668 } else if (info.mime_type == "text/vnd.chromium.ftp-dir" &&
669 !show_raw_listing) {
670 ftp_listing_delegate_.reset(
671 new FtpDirectoryListingResponseDelegate(client_, loader_, response));
675 void WebURLLoaderImpl::Context::OnDownloadedData(int len,
676 int encoded_data_length) {
677 if (client_)
678 client_->didDownloadData(loader_, len, encoded_data_length);
681 void WebURLLoaderImpl::Context::OnReceivedData(const char* data,
682 int data_length,
683 int encoded_data_length) {
684 if (!client_)
685 return;
687 if (request_.useStreamOnResponse()) {
688 // We don't support ftp_listening_delegate_ and multipart_delegate_ for now.
689 // TODO(yhirano): Support ftp listening and multipart.
690 MojoResult rv = WriteDataOnBodyStream(data, data_length);
691 if (rv != MOJO_RESULT_OK && client_) {
692 client_->didFail(
693 loader_, CreateWebURLError(request_.url(), false, net::ERR_FAILED));
695 } else if (ftp_listing_delegate_) {
696 // The FTP listing delegate will make the appropriate calls to
697 // client_->didReceiveData and client_->didReceiveResponse. Since the
698 // delegate may want to do work after sending data to the delegate, keep
699 // |this| and the delegate alive until it's finished handling the data.
700 scoped_refptr<Context> protect(this);
701 ftp_listing_delegate_->OnReceivedData(data, data_length);
702 } else if (multipart_delegate_) {
703 // The multipart delegate will make the appropriate calls to
704 // client_->didReceiveData and client_->didReceiveResponse. Since the
705 // delegate may want to do work after sending data to the delegate, keep
706 // |this| and the delegate alive until it's finished handling the data.
707 scoped_refptr<Context> protect(this);
708 multipart_delegate_->OnReceivedData(data, data_length, encoded_data_length);
709 } else {
710 client_->didReceiveData(loader_, data, data_length, encoded_data_length);
714 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
715 const char* data, int len) {
716 if (client_)
717 client_->didReceiveCachedMetadata(loader_, data, len);
720 void WebURLLoaderImpl::Context::OnCompletedRequest(
721 int error_code,
722 bool was_ignored_by_handler,
723 bool stale_copy_in_cache,
724 const std::string& security_info,
725 const base::TimeTicks& completion_time,
726 int64 total_transfer_size) {
727 // The WebURLLoaderImpl may be deleted in any of the calls to the client or
728 // the delegates below (As they also may call in to the client). Keep |this|
729 // alive in that case, to avoid a crash. If that happens, the request will be
730 // cancelled and |client_| will be set to NULL.
731 scoped_refptr<Context> protect(this);
733 if (ftp_listing_delegate_) {
734 ftp_listing_delegate_->OnCompletedRequest();
735 ftp_listing_delegate_.reset(NULL);
736 } else if (multipart_delegate_) {
737 multipart_delegate_->OnCompletedRequest();
738 multipart_delegate_.reset(NULL);
741 if (client_) {
742 if (error_code != net::OK) {
743 client_->didFail(
744 loader_,
745 CreateWebURLError(request_.url(), stale_copy_in_cache, error_code));
746 } else {
747 if (request_.useStreamOnResponse()) {
748 got_all_stream_body_data_ = true;
749 if (body_stream_buffer_.empty()) {
750 // Close the handle to notify the end of data.
751 body_stream_writer_.reset();
752 client_->didFinishLoading(
753 loader_, (completion_time - TimeTicks()).InSecondsF(),
754 total_transfer_size);
756 } else {
757 client_->didFinishLoading(
758 loader_, (completion_time - TimeTicks()).InSecondsF(),
759 total_transfer_size);
765 WebURLLoaderImpl::Context::~Context() {
766 if (request_id_ >= 0) {
767 resource_dispatcher_->RemovePendingRequest(request_id_);
771 bool WebURLLoaderImpl::Context::CanHandleDataURLRequestLocally() const {
772 GURL url = request_.url();
773 if (!url.SchemeIs(url::kDataScheme))
774 return false;
776 // The fast paths for data URL, Start() and HandleDataURL(), don't support
777 // the downloadToFile option.
778 if (request_.downloadToFile())
779 return false;
781 // Data url requests from object tags may need to be intercepted as streams
782 // and so need to be sent to the browser.
783 if (request_.requestContext() == WebURLRequest::RequestContextObject)
784 return false;
786 // Optimize for the case where we can handle a data URL locally. We must
787 // skip this for data URLs targetted at frames since those could trigger a
788 // download.
790 // NOTE: We special case MIME types we can render both for performance
791 // reasons as well as to support unit tests.
793 #if defined(OS_ANDROID)
794 // For compatibility reasons on Android we need to expose top-level data://
795 // to the browser.
796 if (request_.frameType() == WebURLRequest::FrameTypeTopLevel)
797 return false;
798 #endif
800 if (request_.frameType() != WebURLRequest::FrameTypeTopLevel &&
801 request_.frameType() != WebURLRequest::FrameTypeNested)
802 return true;
804 std::string mime_type, unused_charset;
805 if (net::DataURL::Parse(request_.url(), &mime_type, &unused_charset, NULL) &&
806 mime_util::IsSupportedMimeType(mime_type))
807 return true;
809 return false;
812 void WebURLLoaderImpl::Context::HandleDataURL() {
813 DCHECK_NE(defers_loading_, DEFERRED_DATA);
814 if (defers_loading_ == SHOULD_DEFER) {
815 defers_loading_ = DEFERRED_DATA;
816 return;
819 ResourceResponseInfo info;
820 std::string data;
822 int error_code = GetInfoFromDataURL(request_.url(), &info, &data);
824 if (error_code == net::OK) {
825 OnReceivedResponse(info);
826 if (!data.empty())
827 OnReceivedData(data.data(), data.size(), 0);
830 OnCompletedRequest(error_code, false, false, info.security_info,
831 base::TimeTicks::Now(), 0);
834 MojoResult WebURLLoaderImpl::Context::WriteDataOnBodyStream(const char* data,
835 size_t size) {
836 if (body_stream_buffer_.empty() && size == 0) {
837 // Nothing to do.
838 return MOJO_RESULT_OK;
841 if (!body_stream_writer_.is_valid()) {
842 // The handle is already cleared.
843 return MOJO_RESULT_OK;
846 char* buffer = nullptr;
847 uint32_t num_bytes_writable = 0;
848 MojoResult rv = mojo::BeginWriteDataRaw(body_stream_writer_.get(),
849 reinterpret_cast<void**>(&buffer),
850 &num_bytes_writable,
851 MOJO_WRITE_DATA_FLAG_NONE);
852 if (rv == MOJO_RESULT_SHOULD_WAIT) {
853 body_stream_buffer_.insert(body_stream_buffer_.end(), data, data + size);
854 body_stream_writer_watcher_.Start(
855 body_stream_writer_.get(),
856 MOJO_HANDLE_SIGNAL_WRITABLE,
857 MOJO_DEADLINE_INDEFINITE,
858 base::Bind(&WebURLLoaderImpl::Context::OnHandleGotWritable,
859 base::Unretained(this)));
860 return MOJO_RESULT_OK;
863 if (rv != MOJO_RESULT_OK)
864 return rv;
866 uint32_t num_bytes_to_write = 0;
867 if (num_bytes_writable < body_stream_buffer_.size()) {
868 auto begin = body_stream_buffer_.begin();
869 auto end = body_stream_buffer_.begin() + num_bytes_writable;
871 std::copy(begin, end, buffer);
872 num_bytes_to_write = num_bytes_writable;
873 body_stream_buffer_.erase(begin, end);
874 body_stream_buffer_.insert(body_stream_buffer_.end(), data, data + size);
875 } else {
876 std::copy(body_stream_buffer_.begin(), body_stream_buffer_.end(), buffer);
877 num_bytes_writable -= body_stream_buffer_.size();
878 num_bytes_to_write += body_stream_buffer_.size();
879 buffer += body_stream_buffer_.size();
880 body_stream_buffer_.clear();
882 size_t num_newbytes_to_write =
883 std::min(size, static_cast<size_t>(num_bytes_writable));
884 std::copy(data, data + num_newbytes_to_write, buffer);
885 num_bytes_to_write += num_newbytes_to_write;
886 body_stream_buffer_.insert(body_stream_buffer_.end(),
887 data + num_newbytes_to_write,
888 data + size);
891 rv = mojo::EndWriteDataRaw(body_stream_writer_.get(), num_bytes_to_write);
892 if (rv == MOJO_RESULT_OK && !body_stream_buffer_.empty()) {
893 body_stream_writer_watcher_.Start(
894 body_stream_writer_.get(),
895 MOJO_HANDLE_SIGNAL_WRITABLE,
896 MOJO_DEADLINE_INDEFINITE,
897 base::Bind(&WebURLLoaderImpl::Context::OnHandleGotWritable,
898 base::Unretained(this)));
900 return rv;
903 void WebURLLoaderImpl::Context::OnHandleGotWritable(MojoResult result) {
904 if (result != MOJO_RESULT_OK) {
905 if (client_) {
906 client_->didFail(
907 loader_, CreateWebURLError(request_.url(), false, net::ERR_FAILED));
908 // |this| can be deleted here.
910 return;
913 if (body_stream_buffer_.empty())
914 return;
916 MojoResult rv = WriteDataOnBodyStream(nullptr, 0);
917 if (rv == MOJO_RESULT_OK) {
918 if (got_all_stream_body_data_ && body_stream_buffer_.empty()) {
919 // Close the handle to notify the end of data.
920 body_stream_writer_.reset();
921 if (client_) {
922 // TODO(yhirano): Pass appropriate arguments.
923 client_->didFinishLoading(loader_, 0, 0);
924 // |this| can be deleted here.
927 } else {
928 if (client_) {
929 client_->didFail(
930 loader_, CreateWebURLError(request_.url(), false, net::ERR_FAILED));
931 // |this| can be deleted here.
936 // WebURLLoaderImpl -----------------------------------------------------------
938 WebURLLoaderImpl::WebURLLoaderImpl(
939 ResourceDispatcher* resource_dispatcher,
940 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
941 : context_(new Context(this, resource_dispatcher, task_runner)) {
944 WebURLLoaderImpl::~WebURLLoaderImpl() {
945 cancel();
948 void WebURLLoaderImpl::PopulateURLResponse(const GURL& url,
949 const ResourceResponseInfo& info,
950 WebURLResponse* response) {
951 response->setURL(url);
952 response->setResponseTime(info.response_time.ToInternalValue());
953 response->setMIMEType(WebString::fromUTF8(info.mime_type));
954 response->setTextEncodingName(WebString::fromUTF8(info.charset));
955 response->setExpectedContentLength(info.content_length);
956 response->setSecurityInfo(info.security_info);
957 response->setAppCacheID(info.appcache_id);
958 response->setAppCacheManifestURL(info.appcache_manifest_url);
959 response->setWasCached(!info.load_timing.request_start_time.is_null() &&
960 info.response_time < info.load_timing.request_start_time);
961 response->setRemoteIPAddress(
962 WebString::fromUTF8(info.socket_address.host()));
963 response->setRemotePort(info.socket_address.port());
964 response->setConnectionID(info.load_timing.socket_log_id);
965 response->setConnectionReused(info.load_timing.socket_reused);
966 response->setDownloadFilePath(info.download_file_path.AsUTF16Unsafe());
967 response->setWasFetchedViaSPDY(info.was_fetched_via_spdy);
968 response->setWasFetchedViaServiceWorker(info.was_fetched_via_service_worker);
969 response->setWasFallbackRequiredByServiceWorker(
970 info.was_fallback_required_by_service_worker);
971 response->setServiceWorkerResponseType(info.response_type_via_service_worker);
972 response->setOriginalURLViaServiceWorker(
973 info.original_url_via_service_worker);
975 WebURLResponseExtraDataImpl* extra_data =
976 new WebURLResponseExtraDataImpl(info.npn_negotiated_protocol);
977 response->setExtraData(extra_data);
978 extra_data->set_was_fetched_via_spdy(info.was_fetched_via_spdy);
979 extra_data->set_was_npn_negotiated(info.was_npn_negotiated);
980 extra_data->set_was_alternate_protocol_available(
981 info.was_alternate_protocol_available);
982 extra_data->set_connection_info(info.connection_info);
983 extra_data->set_was_fetched_via_proxy(info.was_fetched_via_proxy);
984 extra_data->set_proxy_server(info.proxy_server);
986 // If there's no received headers end time, don't set load timing. This is
987 // the case for non-HTTP requests, requests that don't go over the wire, and
988 // certain error cases.
989 if (!info.load_timing.receive_headers_end.is_null()) {
990 WebURLLoadTiming timing;
991 PopulateURLLoadTiming(info.load_timing, &timing);
992 const TimeTicks kNullTicks;
993 timing.setServiceWorkerFetchStart(
994 (info.service_worker_fetch_start - kNullTicks).InSecondsF());
995 timing.setServiceWorkerFetchReady(
996 (info.service_worker_fetch_ready - kNullTicks).InSecondsF());
997 timing.setServiceWorkerFetchEnd(
998 (info.service_worker_fetch_end - kNullTicks).InSecondsF());
999 response->setLoadTiming(timing);
1002 if (info.devtools_info.get()) {
1003 WebHTTPLoadInfo load_info;
1005 load_info.setHTTPStatusCode(info.devtools_info->http_status_code);
1006 load_info.setHTTPStatusText(WebString::fromLatin1(
1007 info.devtools_info->http_status_text));
1008 load_info.setEncodedDataLength(info.encoded_data_length);
1010 load_info.setRequestHeadersText(WebString::fromLatin1(
1011 info.devtools_info->request_headers_text));
1012 load_info.setResponseHeadersText(WebString::fromLatin1(
1013 info.devtools_info->response_headers_text));
1014 const HeadersVector& request_headers = info.devtools_info->request_headers;
1015 for (HeadersVector::const_iterator it = request_headers.begin();
1016 it != request_headers.end(); ++it) {
1017 load_info.addRequestHeader(WebString::fromLatin1(it->first),
1018 WebString::fromLatin1(it->second));
1020 const HeadersVector& response_headers =
1021 info.devtools_info->response_headers;
1022 for (HeadersVector::const_iterator it = response_headers.begin();
1023 it != response_headers.end(); ++it) {
1024 load_info.addResponseHeader(WebString::fromLatin1(it->first),
1025 WebString::fromLatin1(it->second));
1027 load_info.setNPNNegotiatedProtocol(WebString::fromLatin1(
1028 info.npn_negotiated_protocol));
1029 response->setHTTPLoadInfo(load_info);
1032 const net::HttpResponseHeaders* headers = info.headers.get();
1033 if (!headers)
1034 return;
1036 WebURLResponse::HTTPVersion version = WebURLResponse::Unknown;
1037 if (headers->GetHttpVersion() == net::HttpVersion(0, 9))
1038 version = WebURLResponse::HTTP_0_9;
1039 else if (headers->GetHttpVersion() == net::HttpVersion(1, 0))
1040 version = WebURLResponse::HTTP_1_0;
1041 else if (headers->GetHttpVersion() == net::HttpVersion(1, 1))
1042 version = WebURLResponse::HTTP_1_1;
1043 response->setHTTPVersion(version);
1044 response->setHTTPStatusCode(headers->response_code());
1045 response->setHTTPStatusText(WebString::fromLatin1(headers->GetStatusText()));
1047 // TODO(darin): We should leverage HttpResponseHeaders for this, and this
1048 // should be using the same code as ResourceDispatcherHost.
1049 // TODO(jungshik): Figure out the actual value of the referrer charset and
1050 // pass it to GetSuggestedFilename.
1051 std::string value;
1052 headers->EnumerateHeader(NULL, "content-disposition", &value);
1053 response->setSuggestedFileName(
1054 net::GetSuggestedFilename(url,
1055 value,
1056 std::string(), // referrer_charset
1057 std::string(), // suggested_name
1058 std::string(), // mime_type
1059 std::string())); // default_name
1061 Time time_val;
1062 if (headers->GetLastModifiedValue(&time_val))
1063 response->setLastModifiedDate(time_val.ToDoubleT());
1065 // Build up the header map.
1066 void* iter = NULL;
1067 std::string name;
1068 while (headers->EnumerateHeaderLines(&iter, &name, &value)) {
1069 response->addHTTPHeaderField(WebString::fromLatin1(name),
1070 WebString::fromLatin1(value));
1074 void WebURLLoaderImpl::loadSynchronously(const WebURLRequest& request,
1075 WebURLResponse& response,
1076 WebURLError& error,
1077 WebData& data) {
1078 SyncLoadResponse sync_load_response;
1079 context_->Start(request, &sync_load_response);
1081 const GURL& final_url = sync_load_response.url;
1083 // TODO(tc): For file loads, we may want to include a more descriptive
1084 // status code or status text.
1085 int error_code = sync_load_response.error_code;
1086 if (error_code != net::OK) {
1087 response.setURL(final_url);
1088 error.domain = WebString::fromUTF8(net::kErrorDomain);
1089 error.reason = error_code;
1090 error.unreachableURL = final_url;
1091 return;
1094 PopulateURLResponse(final_url, sync_load_response, &response);
1096 data.assign(sync_load_response.data.data(),
1097 sync_load_response.data.size());
1100 void WebURLLoaderImpl::loadAsynchronously(const WebURLRequest& request,
1101 WebURLLoaderClient* client) {
1102 DCHECK(!context_->client());
1104 context_->set_client(client);
1105 context_->Start(request, NULL);
1108 void WebURLLoaderImpl::cancel() {
1109 context_->Cancel();
1112 void WebURLLoaderImpl::setDefersLoading(bool value) {
1113 context_->SetDefersLoading(value);
1116 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority,
1117 int intra_priority_value) {
1118 context_->DidChangePriority(new_priority, intra_priority_value);
1121 bool WebURLLoaderImpl::attachThreadedDataReceiver(
1122 blink::WebThreadedDataReceiver* threaded_data_receiver) {
1123 return context_->AttachThreadedDataReceiver(threaded_data_receiver);
1126 } // namespace content