Add test for the ItemChooserDialog class.
[chromium-blink-merge.git] / content / child / web_url_loader_impl.cc
blobaed3d1d062a34c6c07550f23bc4f6152b6aeb0f9
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 <string>
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.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/shared_memory_data_consumer_handle.h"
26 #include "content/child/sync_load_response.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/common/ssl_status_serialization.h"
33 #include "content/public/child/fixed_received_data.h"
34 #include "content/public/child/request_peer.h"
35 #include "content/public/common/content_switches.h"
36 #include "net/base/data_url.h"
37 #include "net/base/filename_util.h"
38 #include "net/base/net_errors.h"
39 #include "net/http/http_response_headers.h"
40 #include "net/http/http_util.h"
41 #include "net/ssl/ssl_cipher_suite_names.h"
42 #include "net/ssl/ssl_connection_status_flags.h"
43 #include "net/url_request/redirect_info.h"
44 #include "net/url_request/url_request_data_job.h"
45 #include "third_party/WebKit/public/platform/WebHTTPLoadInfo.h"
46 #include "third_party/WebKit/public/platform/WebURL.h"
47 #include "third_party/WebKit/public/platform/WebURLError.h"
48 #include "third_party/WebKit/public/platform/WebURLLoadTiming.h"
49 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
50 #include "third_party/WebKit/public/platform/WebURLRequest.h"
51 #include "third_party/WebKit/public/platform/WebURLResponse.h"
52 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
54 using base::Time;
55 using base::TimeTicks;
56 using blink::WebData;
57 using blink::WebHTTPBody;
58 using blink::WebHTTPHeaderVisitor;
59 using blink::WebHTTPLoadInfo;
60 using blink::WebReferrerPolicy;
61 using blink::WebSecurityPolicy;
62 using blink::WebString;
63 using blink::WebURL;
64 using blink::WebURLError;
65 using blink::WebURLLoadTiming;
66 using blink::WebURLLoader;
67 using blink::WebURLLoaderClient;
68 using blink::WebURLRequest;
69 using blink::WebURLResponse;
71 namespace content {
73 // Utilities ------------------------------------------------------------------
75 namespace {
77 using HeadersVector = ResourceDevToolsInfo::HeadersVector;
79 // Converts timing data from |load_timing| to the format used by WebKit.
80 void PopulateURLLoadTiming(const net::LoadTimingInfo& load_timing,
81 WebURLLoadTiming* url_timing) {
82 DCHECK(!load_timing.request_start.is_null());
84 const TimeTicks kNullTicks;
85 url_timing->initialize();
86 url_timing->setRequestTime(
87 (load_timing.request_start - kNullTicks).InSecondsF());
88 url_timing->setProxyStart(
89 (load_timing.proxy_resolve_start - kNullTicks).InSecondsF());
90 url_timing->setProxyEnd(
91 (load_timing.proxy_resolve_end - kNullTicks).InSecondsF());
92 url_timing->setDNSStart(
93 (load_timing.connect_timing.dns_start - kNullTicks).InSecondsF());
94 url_timing->setDNSEnd(
95 (load_timing.connect_timing.dns_end - kNullTicks).InSecondsF());
96 url_timing->setConnectStart(
97 (load_timing.connect_timing.connect_start - kNullTicks).InSecondsF());
98 url_timing->setConnectEnd(
99 (load_timing.connect_timing.connect_end - kNullTicks).InSecondsF());
100 url_timing->setSSLStart(
101 (load_timing.connect_timing.ssl_start - kNullTicks).InSecondsF());
102 url_timing->setSSLEnd(
103 (load_timing.connect_timing.ssl_end - kNullTicks).InSecondsF());
104 url_timing->setSendStart(
105 (load_timing.send_start - kNullTicks).InSecondsF());
106 url_timing->setSendEnd(
107 (load_timing.send_end - kNullTicks).InSecondsF());
108 url_timing->setReceiveHeadersEnd(
109 (load_timing.receive_headers_end - kNullTicks).InSecondsF());
112 net::RequestPriority ConvertWebKitPriorityToNetPriority(
113 const WebURLRequest::Priority& priority) {
114 switch (priority) {
115 case WebURLRequest::PriorityVeryHigh:
116 return net::HIGHEST;
118 case WebURLRequest::PriorityHigh:
119 return net::MEDIUM;
121 case WebURLRequest::PriorityMedium:
122 return net::LOW;
124 case WebURLRequest::PriorityLow:
125 return net::LOWEST;
127 case WebURLRequest::PriorityVeryLow:
128 return net::IDLE;
130 case WebURLRequest::PriorityUnresolved:
131 default:
132 NOTREACHED();
133 return net::LOW;
137 // Extracts info from a data scheme URL |url| into |info| and |data|. Returns
138 // net::OK if successful. Returns a net error code otherwise.
139 int GetInfoFromDataURL(const GURL& url,
140 ResourceResponseInfo* info,
141 std::string* data) {
142 // Assure same time for all time fields of data: URLs.
143 Time now = Time::Now();
144 info->load_timing.request_start = TimeTicks::Now();
145 info->load_timing.request_start_time = now;
146 info->request_time = now;
147 info->response_time = now;
149 std::string mime_type;
150 std::string charset;
151 scoped_refptr<net::HttpResponseHeaders> headers(
152 new net::HttpResponseHeaders(std::string()));
153 int result = net::URLRequestDataJob::BuildResponse(
154 url, &mime_type, &charset, data, headers.get());
155 if (result != net::OK)
156 return result;
158 info->headers = headers;
159 info->mime_type.swap(mime_type);
160 info->charset.swap(charset);
161 info->security_info.clear();
162 info->content_length = data->length();
163 info->encoded_data_length = 0;
165 return net::OK;
168 void SetSecurityStyleAndDetails(const GURL& url,
169 const std::string& security_info,
170 WebURLResponse* response,
171 bool report_security_info) {
172 if (!report_security_info) {
173 response->setSecurityStyle(WebURLResponse::SecurityStyleUnknown);
174 return;
176 if (!url.SchemeIsCryptographic()) {
177 response->setSecurityStyle(WebURLResponse::SecurityStyleUnauthenticated);
178 return;
181 // There are cases where an HTTPS request can come in without security
182 // info attached (such as a redirect response).
183 if (security_info.empty()) {
184 response->setSecurityStyle(WebURLResponse::SecurityStyleUnknown);
185 return;
188 SSLStatus ssl_status;
189 if (!DeserializeSecurityInfo(security_info, &ssl_status)) {
190 response->setSecurityStyle(WebURLResponse::SecurityStyleUnknown);
191 DLOG(ERROR)
192 << "DeserializeSecurityInfo() failed for an authenticated request.";
193 return;
196 int ssl_version =
197 net::SSLConnectionStatusToVersion(ssl_status.connection_status);
198 const char* protocol;
199 net::SSLVersionToString(&protocol, ssl_version);
201 const char* key_exchange;
202 const char* cipher;
203 const char* mac;
204 bool is_aead;
205 uint16_t cipher_suite =
206 net::SSLConnectionStatusToCipherSuite(ssl_status.connection_status);
207 net::SSLCipherSuiteToStrings(&key_exchange, &cipher, &mac, &is_aead,
208 cipher_suite);
209 if (mac == NULL) {
210 DCHECK(is_aead);
211 mac = "";
214 blink::WebURLResponse::SecurityStyle securityStyle =
215 WebURLResponse::SecurityStyleUnknown;
216 switch (ssl_status.security_style) {
217 case SECURITY_STYLE_UNKNOWN:
218 securityStyle = WebURLResponse::SecurityStyleUnknown;
219 break;
220 case SECURITY_STYLE_UNAUTHENTICATED:
221 securityStyle = WebURLResponse::SecurityStyleUnauthenticated;
222 break;
223 case SECURITY_STYLE_AUTHENTICATION_BROKEN:
224 securityStyle = WebURLResponse::SecurityStyleAuthenticationBroken;
225 break;
226 case SECURITY_STYLE_WARNING:
227 securityStyle = WebURLResponse::SecurityStyleWarning;
228 break;
229 case SECURITY_STYLE_AUTHENTICATED:
230 securityStyle = WebURLResponse::SecurityStyleAuthenticated;
231 break;
234 response->setSecurityStyle(securityStyle);
236 blink::WebString protocol_string = blink::WebString::fromUTF8(protocol);
237 blink::WebString cipher_string = blink::WebString::fromUTF8(cipher);
238 blink::WebString key_exchange_string =
239 blink::WebString::fromUTF8(key_exchange);
240 blink::WebString mac_string = blink::WebString::fromUTF8(mac);
241 response->setSecurityDetails(protocol_string, key_exchange_string,
242 cipher_string, mac_string,
243 ssl_status.cert_id);
246 } // namespace
248 // WebURLLoaderImpl::Context --------------------------------------------------
250 // This inner class exists since the WebURLLoader may be deleted while inside a
251 // call to WebURLLoaderClient. Refcounting is to keep the context from being
252 // deleted if it may have work to do after calling into the client.
253 class WebURLLoaderImpl::Context : public base::RefCounted<Context>,
254 public RequestPeer {
255 public:
256 Context(WebURLLoaderImpl* loader,
257 ResourceDispatcher* resource_dispatcher,
258 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
260 WebURLLoaderClient* client() const { return client_; }
261 void set_client(WebURLLoaderClient* client) { client_ = client; }
263 void Cancel();
264 void SetDefersLoading(bool value);
265 void DidChangePriority(WebURLRequest::Priority new_priority,
266 int intra_priority_value);
267 bool AttachThreadedDataReceiver(
268 blink::WebThreadedDataReceiver* threaded_data_receiver);
269 void Start(const WebURLRequest& request,
270 SyncLoadResponse* sync_load_response);
272 // RequestPeer methods:
273 void OnUploadProgress(uint64 position, uint64 size) override;
274 bool OnReceivedRedirect(const net::RedirectInfo& redirect_info,
275 const ResourceResponseInfo& info) override;
276 void OnReceivedResponse(const ResourceResponseInfo& info) override;
277 void OnDownloadedData(int len, int encoded_data_length) override;
278 void OnReceivedData(scoped_ptr<ReceivedData> data) override;
279 void OnReceivedCachedMetadata(const char* data, int len) override;
280 void OnCompletedRequest(int error_code,
281 bool was_ignored_by_handler,
282 bool stale_copy_in_cache,
283 const std::string& security_info,
284 const base::TimeTicks& completion_time,
285 int64 total_transfer_size) override;
286 void OnReceivedCompletedResponse(const ResourceResponseInfo& info,
287 scoped_ptr<ReceivedData> data,
288 int error_code,
289 bool was_ignored_by_handler,
290 bool stale_copy_in_cache,
291 const std::string& security_info,
292 const base::TimeTicks& completion_time,
293 int64 total_transfer_size) override;
295 private:
296 friend class base::RefCounted<Context>;
297 ~Context() override;
299 // Called when the body data stream is detached from the reader side.
300 void CancelBodyStreaming();
301 // We can optimize the handling of data URLs in most cases.
302 bool CanHandleDataURLRequestLocally() const;
303 void HandleDataURL();
305 WebURLLoaderImpl* loader_;
306 WebURLRequest request_;
307 WebURLLoaderClient* client_;
308 ResourceDispatcher* resource_dispatcher_;
309 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
310 WebReferrerPolicy referrer_policy_;
311 scoped_ptr<FtpDirectoryListingResponseDelegate> ftp_listing_delegate_;
312 scoped_ptr<MultipartResponseDelegate> multipart_delegate_;
313 scoped_ptr<StreamOverrideParameters> stream_override_;
314 scoped_ptr<SharedMemoryDataConsumerHandle::Writer> body_stream_writer_;
315 enum DeferState {NOT_DEFERRING, SHOULD_DEFER, DEFERRED_DATA};
316 DeferState defers_loading_;
317 int request_id_;
320 WebURLLoaderImpl::Context::Context(
321 WebURLLoaderImpl* loader,
322 ResourceDispatcher* resource_dispatcher,
323 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
324 : loader_(loader),
325 client_(NULL),
326 resource_dispatcher_(resource_dispatcher),
327 task_runner_(task_runner),
328 referrer_policy_(blink::WebReferrerPolicyDefault),
329 defers_loading_(NOT_DEFERRING),
330 request_id_(-1) {
333 void WebURLLoaderImpl::Context::Cancel() {
334 if (resource_dispatcher_ && // NULL in unittest.
335 request_id_ != -1) {
336 resource_dispatcher_->Cancel(request_id_);
337 request_id_ = -1;
340 if (body_stream_writer_)
341 body_stream_writer_->Fail();
343 // Ensure that we do not notify the multipart delegate anymore as it has
344 // its own pointer to the client.
345 if (multipart_delegate_)
346 multipart_delegate_->Cancel();
347 // Ditto for the ftp delegate.
348 if (ftp_listing_delegate_)
349 ftp_listing_delegate_->Cancel();
351 // Do not make any further calls to the client.
352 client_ = NULL;
353 loader_ = NULL;
356 void WebURLLoaderImpl::Context::SetDefersLoading(bool value) {
357 if (request_id_ != -1)
358 resource_dispatcher_->SetDefersLoading(request_id_, value);
359 if (value && defers_loading_ == NOT_DEFERRING) {
360 defers_loading_ = SHOULD_DEFER;
361 } else if (!value && defers_loading_ != NOT_DEFERRING) {
362 if (defers_loading_ == DEFERRED_DATA) {
363 task_runner_->PostTask(FROM_HERE,
364 base::Bind(&Context::HandleDataURL, this));
366 defers_loading_ = NOT_DEFERRING;
370 void WebURLLoaderImpl::Context::DidChangePriority(
371 WebURLRequest::Priority new_priority, int intra_priority_value) {
372 if (request_id_ != -1) {
373 resource_dispatcher_->DidChangePriority(
374 request_id_,
375 ConvertWebKitPriorityToNetPriority(new_priority),
376 intra_priority_value);
380 bool WebURLLoaderImpl::Context::AttachThreadedDataReceiver(
381 blink::WebThreadedDataReceiver* threaded_data_receiver) {
382 if (request_id_ != -1) {
383 return resource_dispatcher_->AttachThreadedDataReceiver(
384 request_id_, threaded_data_receiver);
387 return false;
390 void WebURLLoaderImpl::Context::Start(const WebURLRequest& request,
391 SyncLoadResponse* sync_load_response) {
392 DCHECK(request_id_ == -1);
393 request_ = request; // Save the request.
394 if (request.extraData()) {
395 RequestExtraData* extra_data =
396 static_cast<RequestExtraData*>(request.extraData());
397 stream_override_ = extra_data->TakeStreamOverrideOwnership();
400 GURL url = request.url();
402 // PlzNavigate: during navigation, the renderer should request a stream which
403 // contains the body of the response. The request has already been made by the
404 // browser.
405 if (stream_override_.get()) {
406 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
407 switches::kEnableBrowserSideNavigation));
408 DCHECK(!sync_load_response);
409 DCHECK_NE(WebURLRequest::FrameTypeNone, request.frameType());
410 DCHECK_EQ("GET", request.httpMethod().latin1());
411 url = stream_override_->stream_url;
414 if (CanHandleDataURLRequestLocally()) {
415 if (sync_load_response) {
416 // This is a sync load. Do the work now.
417 sync_load_response->url = url;
418 sync_load_response->error_code =
419 GetInfoFromDataURL(sync_load_response->url, sync_load_response,
420 &sync_load_response->data);
421 } else {
422 task_runner_->PostTask(FROM_HERE,
423 base::Bind(&Context::HandleDataURL, this));
425 return;
428 // PlzNavigate: outside of tests, the only navigation requests going through
429 // the WebURLLoader are the ones created by CommitNavigation. Several browser
430 // tests load HTML directly through a data url which will be handled by the
431 // block above.
432 DCHECK_IMPLIES(base::CommandLine::ForCurrentProcess()->HasSwitch(
433 switches::kEnableBrowserSideNavigation),
434 stream_override_.get() ||
435 request.frameType() == WebURLRequest::FrameTypeNone);
437 GURL referrer_url(
438 request.httpHeaderField(WebString::fromUTF8("Referer")).latin1());
439 const std::string& method = request.httpMethod().latin1();
441 // TODO(brettw) this should take parameter encoding into account when
442 // creating the GURLs.
444 // TODO(horo): Check credentials flag is unset when credentials mode is omit.
445 // Check credentials flag is set when credentials mode is include.
447 RequestInfo request_info;
448 request_info.method = method;
449 request_info.url = url;
450 request_info.first_party_for_cookies = request.firstPartyForCookies();
451 referrer_policy_ = request.referrerPolicy();
452 request_info.referrer = Referrer(referrer_url, referrer_policy_);
453 request_info.headers = GetWebURLRequestHeaders(request);
454 request_info.load_flags = GetLoadFlagsForWebURLRequest(request);
455 request_info.enable_load_timing = true;
456 request_info.enable_upload_progress = request.reportUploadProgress();
457 if (request.requestContext() == WebURLRequest::RequestContextXMLHttpRequest &&
458 (url.has_username() || url.has_password())) {
459 request_info.do_not_prompt_for_login = true;
461 // requestor_pid only needs to be non-zero if the request originates outside
462 // the render process, so we can use requestorProcessID even for requests
463 // from in-process plugins.
464 request_info.requestor_pid = request.requestorProcessID();
465 request_info.request_type = WebURLRequestToResourceType(request);
466 request_info.priority =
467 ConvertWebKitPriorityToNetPriority(request.priority());
468 request_info.appcache_host_id = request.appCacheHostID();
469 request_info.routing_id = request.requestorID();
470 request_info.download_to_file = request.downloadToFile();
471 request_info.has_user_gesture = request.hasUserGesture();
472 request_info.skip_service_worker = request.skipServiceWorker();
473 request_info.should_reset_appcache = request.shouldResetAppCache();
474 request_info.fetch_request_mode =
475 GetFetchRequestModeForWebURLRequest(request);
476 request_info.fetch_credentials_mode =
477 GetFetchCredentialsModeForWebURLRequest(request);
478 request_info.fetch_redirect_mode =
479 GetFetchRedirectModeForWebURLRequest(request);
480 request_info.fetch_request_context_type =
481 GetRequestContextTypeForWebURLRequest(request);
482 request_info.fetch_frame_type =
483 GetRequestContextFrameTypeForWebURLRequest(request);
484 request_info.extra_data = request.extraData();
485 request_info.report_raw_headers = request.reportRawHeaders();
487 scoped_refptr<ResourceRequestBody> request_body =
488 GetRequestBodyForWebURLRequest(request).get();
490 if (sync_load_response) {
491 resource_dispatcher_->StartSync(
492 request_info, request_body.get(), sync_load_response);
493 return;
496 request_id_ = resource_dispatcher_->StartAsync(
497 request_info, request_body.get(), this);
500 void WebURLLoaderImpl::Context::OnUploadProgress(uint64 position, uint64 size) {
501 if (client_)
502 client_->didSendData(loader_, position, size);
505 bool WebURLLoaderImpl::Context::OnReceivedRedirect(
506 const net::RedirectInfo& redirect_info,
507 const ResourceResponseInfo& info) {
508 if (!client_)
509 return false;
511 WebURLResponse response;
512 response.initialize();
513 PopulateURLResponse(request_.url(), info, &response,
514 request_.reportRawHeaders());
516 // TODO(darin): We lack sufficient information to construct the actual
517 // request that resulted from the redirect.
518 WebURLRequest new_request(redirect_info.new_url);
519 new_request.setFirstPartyForCookies(
520 redirect_info.new_first_party_for_cookies);
521 new_request.setDownloadToFile(request_.downloadToFile());
522 new_request.setUseStreamOnResponse(request_.useStreamOnResponse());
523 new_request.setRequestContext(request_.requestContext());
524 new_request.setFrameType(request_.frameType());
525 new_request.setSkipServiceWorker(!info.was_fetched_via_service_worker);
526 new_request.setShouldResetAppCache(request_.shouldResetAppCache());
527 new_request.setFetchRequestMode(request_.fetchRequestMode());
528 new_request.setFetchCredentialsMode(request_.fetchCredentialsMode());
530 new_request.setHTTPReferrer(WebString::fromUTF8(redirect_info.new_referrer),
531 referrer_policy_);
533 std::string old_method = request_.httpMethod().utf8();
534 new_request.setHTTPMethod(WebString::fromUTF8(redirect_info.new_method));
535 if (redirect_info.new_method == old_method)
536 new_request.setHTTPBody(request_.httpBody());
538 // Protect from deletion during call to willSendRequest.
539 scoped_refptr<Context> protect(this);
541 client_->willSendRequest(loader_, new_request, response);
542 request_ = new_request;
544 // Only follow the redirect if WebKit left the URL unmodified.
545 if (redirect_info.new_url == GURL(new_request.url())) {
546 // First-party cookie logic moved from DocumentLoader in Blink to
547 // net::URLRequest in the browser. Assert that Blink didn't try to change it
548 // to something else.
549 DCHECK_EQ(redirect_info.new_first_party_for_cookies.spec(),
550 request_.firstPartyForCookies().string().utf8());
551 return true;
554 // We assume that WebKit only changes the URL to suppress a redirect, and we
555 // assume that it does so by setting it to be invalid.
556 DCHECK(!new_request.url().isValid());
557 return false;
560 void WebURLLoaderImpl::Context::OnReceivedResponse(
561 const ResourceResponseInfo& initial_info) {
562 if (!client_)
563 return;
565 ResourceResponseInfo info = initial_info;
567 // PlzNavigate: during navigations, the ResourceResponse has already been
568 // received on the browser side, and has been passed down to the renderer.
569 if (stream_override_.get()) {
570 CHECK(base::CommandLine::ForCurrentProcess()->HasSwitch(
571 switches::kEnableBrowserSideNavigation));
572 info = stream_override_->response;
575 WebURLResponse response;
576 response.initialize();
577 PopulateURLResponse(request_.url(), info, &response,
578 request_.reportRawHeaders());
580 bool show_raw_listing = (GURL(request_.url()).query() == "raw");
582 if (info.mime_type == "text/vnd.chromium.ftp-dir") {
583 if (show_raw_listing) {
584 // Set the MIME type to plain text to prevent any active content.
585 response.setMIMEType("text/plain");
586 } else {
587 // We're going to produce a parsed listing in HTML.
588 response.setMIMEType("text/html");
592 // Prevent |this| from being destroyed if the client destroys the loader,
593 // ether in didReceiveResponse, or when the multipart/ftp delegate calls into
594 // it.
595 scoped_refptr<Context> protect(this);
597 if (request_.useStreamOnResponse()) {
598 SharedMemoryDataConsumerHandle::BackpressureMode mode =
599 SharedMemoryDataConsumerHandle::kDoNotApplyBackpressure;
600 if (info.headers &&
601 info.headers->HasHeaderValue("Cache-Control", "no-store")) {
602 mode = SharedMemoryDataConsumerHandle::kApplyBackpressure;
605 auto read_handle = make_scoped_ptr(new SharedMemoryDataConsumerHandle(
606 mode, base::Bind(&Context::CancelBodyStreaming, this),
607 &body_stream_writer_));
609 // Here |body_stream_writer_| has an indirect reference to |this| and that
610 // creates a reference cycle, but it is not a problem because the cycle
611 // will break if one of the following happens:
612 // 1) The body data transfer is done (with or without an error).
613 // 2) |read_handle| (and its reader) is detached.
615 // The client takes |read_handle|'s ownership.
616 client_->didReceiveResponse(loader_, response, read_handle.release());
617 // TODO(yhirano): Support ftp listening and multipart
618 return;
619 } else {
620 client_->didReceiveResponse(loader_, response);
623 // We may have been cancelled after didReceiveResponse, which would leave us
624 // without a client and therefore without much need to do further handling.
625 if (!client_)
626 return;
628 DCHECK(!ftp_listing_delegate_.get());
629 DCHECK(!multipart_delegate_.get());
630 if (info.headers.get() && info.mime_type == "multipart/x-mixed-replace") {
631 std::string content_type;
632 info.headers->EnumerateHeader(NULL, "content-type", &content_type);
634 std::string mime_type;
635 std::string charset;
636 bool had_charset = false;
637 std::string boundary;
638 net::HttpUtil::ParseContentType(content_type, &mime_type, &charset,
639 &had_charset, &boundary);
640 base::TrimString(boundary, " \"", &boundary);
642 // If there's no boundary, just handle the request normally. In the gecko
643 // code, nsMultiMixedConv::OnStartRequest throws an exception.
644 if (!boundary.empty()) {
645 multipart_delegate_.reset(
646 new MultipartResponseDelegate(client_, loader_, response, boundary));
648 } else if (info.mime_type == "text/vnd.chromium.ftp-dir" &&
649 !show_raw_listing) {
650 ftp_listing_delegate_.reset(
651 new FtpDirectoryListingResponseDelegate(client_, loader_, response));
655 void WebURLLoaderImpl::Context::OnDownloadedData(int len,
656 int encoded_data_length) {
657 if (client_)
658 client_->didDownloadData(loader_, len, encoded_data_length);
661 void WebURLLoaderImpl::Context::OnReceivedData(scoped_ptr<ReceivedData> data) {
662 const char* payload = data->payload();
663 int data_length = data->length();
664 int encoded_data_length = data->encoded_length();
665 if (!client_)
666 return;
668 if (ftp_listing_delegate_) {
669 // The FTP listing delegate will make the appropriate calls to
670 // client_->didReceiveData and client_->didReceiveResponse. Since the
671 // delegate may want to do work after sending data to the delegate, keep
672 // |this| and the delegate alive until it's finished handling the data.
673 scoped_refptr<Context> protect(this);
674 ftp_listing_delegate_->OnReceivedData(payload, data_length);
675 } else if (multipart_delegate_) {
676 // The multipart delegate will make the appropriate calls to
677 // client_->didReceiveData and client_->didReceiveResponse. Since the
678 // delegate may want to do work after sending data to the delegate, keep
679 // |this| and the delegate alive until it's finished handling the data.
680 scoped_refptr<Context> protect(this);
681 multipart_delegate_->OnReceivedData(payload, data_length,
682 encoded_data_length);
683 } else {
684 scoped_refptr<Context> protect(this);
685 // We dispatch the data even when |useStreamOnResponse()| is set, in order
686 // to make Devtools work.
687 client_->didReceiveData(loader_, payload, data_length, encoded_data_length);
689 if (request_.useStreamOnResponse()) {
690 // We don't support ftp_listening_delegate_ and multipart_delegate_ for
691 // now.
692 // TODO(yhirano): Support ftp listening and multipart.
693 body_stream_writer_->AddData(data.Pass());
698 void WebURLLoaderImpl::Context::OnReceivedCachedMetadata(
699 const char* data, int len) {
700 if (client_)
701 client_->didReceiveCachedMetadata(loader_, data, len);
704 void WebURLLoaderImpl::Context::OnCompletedRequest(
705 int error_code,
706 bool was_ignored_by_handler,
707 bool stale_copy_in_cache,
708 const std::string& security_info,
709 const base::TimeTicks& completion_time,
710 int64 total_transfer_size) {
711 // The WebURLLoaderImpl may be deleted in any of the calls to the client or
712 // the delegates below (As they also may call in to the client). Keep |this|
713 // alive in that case, to avoid a crash. If that happens, the request will be
714 // cancelled and |client_| will be set to NULL.
715 scoped_refptr<Context> protect(this);
717 if (ftp_listing_delegate_) {
718 ftp_listing_delegate_->OnCompletedRequest();
719 ftp_listing_delegate_.reset(NULL);
720 } else if (multipart_delegate_) {
721 multipart_delegate_->OnCompletedRequest();
722 multipart_delegate_.reset(NULL);
725 if (body_stream_writer_ && error_code != net::OK)
726 body_stream_writer_->Fail();
727 body_stream_writer_.reset();
729 if (client_) {
730 if (error_code != net::OK) {
731 client_->didFail(
732 loader_,
733 CreateWebURLError(request_.url(), stale_copy_in_cache, error_code,
734 was_ignored_by_handler));
735 } else {
736 client_->didFinishLoading(loader_,
737 (completion_time - TimeTicks()).InSecondsF(),
738 total_transfer_size);
743 void WebURLLoaderImpl::Context::OnReceivedCompletedResponse(
744 const ResourceResponseInfo& info,
745 scoped_ptr<ReceivedData> data,
746 int error_code,
747 bool was_ignored_by_handler,
748 bool stale_copy_in_cache,
749 const std::string& security_info,
750 const base::TimeTicks& completion_time,
751 int64 total_transfer_size) {
752 scoped_refptr<Context> protect(this);
754 OnReceivedResponse(info);
755 if (data)
756 OnReceivedData(data.Pass());
757 OnCompletedRequest(error_code, was_ignored_by_handler, stale_copy_in_cache,
758 security_info, completion_time, total_transfer_size);
761 WebURLLoaderImpl::Context::~Context() {
762 if (request_id_ >= 0) {
763 resource_dispatcher_->RemovePendingRequest(request_id_);
767 void WebURLLoaderImpl::Context::CancelBodyStreaming() {
768 scoped_refptr<Context> protect(this);
770 // Notify renderer clients that the request is canceled.
771 if (ftp_listing_delegate_) {
772 ftp_listing_delegate_->OnCompletedRequest();
773 ftp_listing_delegate_.reset(NULL);
774 } else if (multipart_delegate_) {
775 multipart_delegate_->OnCompletedRequest();
776 multipart_delegate_.reset(NULL);
779 if (body_stream_writer_) {
780 body_stream_writer_->Fail();
781 body_stream_writer_.reset();
783 if (client_) {
784 // TODO(yhirano): Set |stale_copy_in_cache| appropriately if possible.
785 client_->didFail(
786 loader_, CreateWebURLError(request_.url(), false, net::ERR_ABORTED));
789 // Notify the browser process that the request is canceled.
790 Cancel();
793 bool WebURLLoaderImpl::Context::CanHandleDataURLRequestLocally() const {
794 GURL url = request_.url();
795 if (!url.SchemeIs(url::kDataScheme))
796 return false;
798 // The fast paths for data URL, Start() and HandleDataURL(), don't support
799 // the downloadToFile option.
800 if (request_.downloadToFile())
801 return false;
803 // Data url requests from object tags may need to be intercepted as streams
804 // and so need to be sent to the browser.
805 if (request_.requestContext() == WebURLRequest::RequestContextObject)
806 return false;
808 // Optimize for the case where we can handle a data URL locally. We must
809 // skip this for data URLs targetted at frames since those could trigger a
810 // download.
812 // NOTE: We special case MIME types we can render both for performance
813 // reasons as well as to support unit tests.
815 #if defined(OS_ANDROID)
816 // For compatibility reasons on Android we need to expose top-level data://
817 // to the browser.
818 if (request_.frameType() == WebURLRequest::FrameTypeTopLevel)
819 return false;
820 #endif
822 if (request_.frameType() != WebURLRequest::FrameTypeTopLevel &&
823 request_.frameType() != WebURLRequest::FrameTypeNested)
824 return true;
826 std::string mime_type, unused_charset;
827 if (net::DataURL::Parse(request_.url(), &mime_type, &unused_charset, NULL) &&
828 mime_util::IsSupportedMimeType(mime_type))
829 return true;
831 return false;
834 void WebURLLoaderImpl::Context::HandleDataURL() {
835 DCHECK_NE(defers_loading_, DEFERRED_DATA);
836 if (defers_loading_ == SHOULD_DEFER) {
837 defers_loading_ = DEFERRED_DATA;
838 return;
841 ResourceResponseInfo info;
842 std::string data;
844 int error_code = GetInfoFromDataURL(request_.url(), &info, &data);
846 if (error_code == net::OK) {
847 OnReceivedResponse(info);
848 if (!data.empty())
849 OnReceivedData(
850 make_scoped_ptr(new FixedReceivedData(data.data(), data.size(), 0)));
853 OnCompletedRequest(error_code, false, false, info.security_info,
854 base::TimeTicks::Now(), 0);
857 // WebURLLoaderImpl -----------------------------------------------------------
859 WebURLLoaderImpl::WebURLLoaderImpl(
860 ResourceDispatcher* resource_dispatcher,
861 scoped_refptr<base::SingleThreadTaskRunner> task_runner)
862 : context_(new Context(this, resource_dispatcher, task_runner)) {
865 WebURLLoaderImpl::~WebURLLoaderImpl() {
866 cancel();
869 void WebURLLoaderImpl::PopulateURLResponse(const GURL& url,
870 const ResourceResponseInfo& info,
871 WebURLResponse* response,
872 bool report_security_info) {
873 response->setURL(url);
874 response->setResponseTime(info.response_time.ToInternalValue());
875 response->setMIMEType(WebString::fromUTF8(info.mime_type));
876 response->setTextEncodingName(WebString::fromUTF8(info.charset));
877 response->setExpectedContentLength(info.content_length);
878 response->setSecurityInfo(info.security_info);
879 response->setAppCacheID(info.appcache_id);
880 response->setAppCacheManifestURL(info.appcache_manifest_url);
881 response->setWasCached(!info.load_timing.request_start_time.is_null() &&
882 info.response_time < info.load_timing.request_start_time);
883 response->setRemoteIPAddress(
884 WebString::fromUTF8(info.socket_address.host()));
885 response->setRemotePort(info.socket_address.port());
886 response->setConnectionID(info.load_timing.socket_log_id);
887 response->setConnectionReused(info.load_timing.socket_reused);
888 response->setDownloadFilePath(info.download_file_path.AsUTF16Unsafe());
889 response->setWasFetchedViaSPDY(info.was_fetched_via_spdy);
890 response->setWasFetchedViaServiceWorker(info.was_fetched_via_service_worker);
891 response->setWasFallbackRequiredByServiceWorker(
892 info.was_fallback_required_by_service_worker);
893 response->setServiceWorkerResponseType(info.response_type_via_service_worker);
894 response->setOriginalURLViaServiceWorker(
895 info.original_url_via_service_worker);
897 SetSecurityStyleAndDetails(url, info.security_info, response,
898 report_security_info);
900 WebURLResponseExtraDataImpl* extra_data =
901 new WebURLResponseExtraDataImpl(info.npn_negotiated_protocol);
902 response->setExtraData(extra_data);
903 extra_data->set_was_fetched_via_spdy(info.was_fetched_via_spdy);
904 extra_data->set_was_npn_negotiated(info.was_npn_negotiated);
905 extra_data->set_was_alternate_protocol_available(
906 info.was_alternate_protocol_available);
907 extra_data->set_connection_info(info.connection_info);
908 extra_data->set_was_fetched_via_proxy(info.was_fetched_via_proxy);
909 extra_data->set_proxy_server(info.proxy_server);
911 // If there's no received headers end time, don't set load timing. This is
912 // the case for non-HTTP requests, requests that don't go over the wire, and
913 // certain error cases.
914 if (!info.load_timing.receive_headers_end.is_null()) {
915 WebURLLoadTiming timing;
916 PopulateURLLoadTiming(info.load_timing, &timing);
917 const TimeTicks kNullTicks;
918 timing.setWorkerStart(
919 (info.service_worker_start_time - kNullTicks).InSecondsF());
920 timing.setWorkerReady(
921 (info.service_worker_ready_time - kNullTicks).InSecondsF());
922 response->setLoadTiming(timing);
925 if (info.devtools_info.get()) {
926 WebHTTPLoadInfo load_info;
928 load_info.setHTTPStatusCode(info.devtools_info->http_status_code);
929 load_info.setHTTPStatusText(WebString::fromLatin1(
930 info.devtools_info->http_status_text));
931 load_info.setEncodedDataLength(info.encoded_data_length);
933 load_info.setRequestHeadersText(WebString::fromLatin1(
934 info.devtools_info->request_headers_text));
935 load_info.setResponseHeadersText(WebString::fromLatin1(
936 info.devtools_info->response_headers_text));
937 const HeadersVector& request_headers = info.devtools_info->request_headers;
938 for (HeadersVector::const_iterator it = request_headers.begin();
939 it != request_headers.end(); ++it) {
940 load_info.addRequestHeader(WebString::fromLatin1(it->first),
941 WebString::fromLatin1(it->second));
943 const HeadersVector& response_headers =
944 info.devtools_info->response_headers;
945 for (HeadersVector::const_iterator it = response_headers.begin();
946 it != response_headers.end(); ++it) {
947 load_info.addResponseHeader(WebString::fromLatin1(it->first),
948 WebString::fromLatin1(it->second));
950 load_info.setNPNNegotiatedProtocol(WebString::fromLatin1(
951 info.npn_negotiated_protocol));
952 response->setHTTPLoadInfo(load_info);
955 const net::HttpResponseHeaders* headers = info.headers.get();
956 if (!headers)
957 return;
959 WebURLResponse::HTTPVersion version = WebURLResponse::Unknown;
960 if (headers->GetHttpVersion() == net::HttpVersion(0, 9))
961 version = WebURLResponse::HTTP_0_9;
962 else if (headers->GetHttpVersion() == net::HttpVersion(1, 0))
963 version = WebURLResponse::HTTP_1_0;
964 else if (headers->GetHttpVersion() == net::HttpVersion(1, 1))
965 version = WebURLResponse::HTTP_1_1;
966 response->setHTTPVersion(version);
967 response->setHTTPStatusCode(headers->response_code());
968 response->setHTTPStatusText(WebString::fromLatin1(headers->GetStatusText()));
970 // TODO(darin): We should leverage HttpResponseHeaders for this, and this
971 // should be using the same code as ResourceDispatcherHost.
972 // TODO(jungshik): Figure out the actual value of the referrer charset and
973 // pass it to GetSuggestedFilename.
974 std::string value;
975 headers->EnumerateHeader(NULL, "content-disposition", &value);
976 response->setSuggestedFileName(
977 net::GetSuggestedFilename(url,
978 value,
979 std::string(), // referrer_charset
980 std::string(), // suggested_name
981 std::string(), // mime_type
982 std::string())); // default_name
984 Time time_val;
985 if (headers->GetLastModifiedValue(&time_val))
986 response->setLastModifiedDate(time_val.ToDoubleT());
988 // Build up the header map.
989 void* iter = NULL;
990 std::string name;
991 while (headers->EnumerateHeaderLines(&iter, &name, &value)) {
992 response->addHTTPHeaderField(WebString::fromLatin1(name),
993 WebString::fromLatin1(value));
997 void WebURLLoaderImpl::loadSynchronously(const WebURLRequest& request,
998 WebURLResponse& response,
999 WebURLError& error,
1000 WebData& data) {
1001 SyncLoadResponse sync_load_response;
1002 context_->Start(request, &sync_load_response);
1004 const GURL& final_url = sync_load_response.url;
1006 // TODO(tc): For file loads, we may want to include a more descriptive
1007 // status code or status text.
1008 int error_code = sync_load_response.error_code;
1009 if (error_code != net::OK) {
1010 response.setURL(final_url);
1011 error.domain = WebString::fromUTF8(net::kErrorDomain);
1012 error.reason = error_code;
1013 error.unreachableURL = final_url;
1014 return;
1017 PopulateURLResponse(final_url, sync_load_response, &response,
1018 request.reportRawHeaders());
1020 data.assign(sync_load_response.data.data(),
1021 sync_load_response.data.size());
1024 void WebURLLoaderImpl::loadAsynchronously(const WebURLRequest& request,
1025 WebURLLoaderClient* client) {
1026 DCHECK(!context_->client());
1028 context_->set_client(client);
1029 context_->Start(request, NULL);
1032 void WebURLLoaderImpl::cancel() {
1033 context_->Cancel();
1036 void WebURLLoaderImpl::setDefersLoading(bool value) {
1037 context_->SetDefersLoading(value);
1040 void WebURLLoaderImpl::didChangePriority(WebURLRequest::Priority new_priority,
1041 int intra_priority_value) {
1042 context_->DidChangePriority(new_priority, intra_priority_value);
1045 bool WebURLLoaderImpl::attachThreadedDataReceiver(
1046 blink::WebThreadedDataReceiver* threaded_data_receiver) {
1047 return context_->AttachThreadedDataReceiver(threaded_data_receiver);
1050 } // namespace content