1 // Copyright (c) 2012 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/browser/loader/resource_loader.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/metrics/histogram.h"
10 #include "base/time/time.h"
11 #include "content/browser/appcache/appcache_interceptor.h"
12 #include "content/browser/child_process_security_policy_impl.h"
13 #include "content/browser/loader/cross_site_resource_handler.h"
14 #include "content/browser/loader/detachable_resource_handler.h"
15 #include "content/browser/loader/resource_loader_delegate.h"
16 #include "content/browser/loader/resource_request_info_impl.h"
17 #include "content/browser/service_worker/service_worker_request_handler.h"
18 #include "content/browser/ssl/ssl_client_auth_handler.h"
19 #include "content/browser/ssl/ssl_manager.h"
20 #include "content/common/ssl_status_serialization.h"
21 #include "content/public/browser/cert_store.h"
22 #include "content/public/browser/resource_context.h"
23 #include "content/public/browser/resource_dispatcher_host_login_delegate.h"
24 #include "content/public/browser/signed_certificate_timestamp_store.h"
25 #include "content/public/common/content_client.h"
26 #include "content/public/common/content_switches.h"
27 #include "content/public/common/process_type.h"
28 #include "content/public/common/resource_response.h"
29 #include "net/base/io_buffer.h"
30 #include "net/base/load_flags.h"
31 #include "net/http/http_response_headers.h"
32 #include "net/ssl/client_cert_store.h"
33 #include "net/url_request/redirect_info.h"
34 #include "net/url_request/url_request_status.h"
36 using base::TimeDelta
;
37 using base::TimeTicks
;
42 void PopulateResourceResponse(ResourceRequestInfoImpl
* info
,
43 net::URLRequest
* request
,
44 ResourceResponse
* response
) {
45 response
->head
.error_code
= request
->status().error();
46 response
->head
.request_time
= request
->request_time();
47 response
->head
.response_time
= request
->response_time();
48 response
->head
.headers
= request
->response_headers();
49 request
->GetCharset(&response
->head
.charset
);
50 response
->head
.content_length
= request
->GetExpectedContentSize();
51 request
->GetMimeType(&response
->head
.mime_type
);
52 net::HttpResponseInfo response_info
= request
->response_info();
53 response
->head
.was_fetched_via_spdy
= response_info
.was_fetched_via_spdy
;
54 response
->head
.was_npn_negotiated
= response_info
.was_npn_negotiated
;
55 response
->head
.npn_negotiated_protocol
=
56 response_info
.npn_negotiated_protocol
;
57 response
->head
.connection_info
= response_info
.connection_info
;
58 response
->head
.was_fetched_via_proxy
= request
->was_fetched_via_proxy();
59 response
->head
.socket_address
= request
->GetSocketAddress();
60 if (ServiceWorkerRequestHandler
* handler
=
61 ServiceWorkerRequestHandler::GetHandler(request
)) {
62 handler
->GetExtraResponseInfo(
63 &response
->head
.was_fetched_via_service_worker
,
64 &response
->head
.original_url_via_service_worker
);
66 AppCacheInterceptor::GetExtraResponseInfo(
68 &response
->head
.appcache_id
,
69 &response
->head
.appcache_manifest_url
);
70 if (info
->is_load_timing_enabled())
71 request
->GetLoadTimingInfo(&response
->head
.load_timing
);
76 ResourceLoader::ResourceLoader(scoped_ptr
<net::URLRequest
> request
,
77 scoped_ptr
<ResourceHandler
> handler
,
78 ResourceLoaderDelegate
* delegate
)
79 : deferred_stage_(DEFERRED_NONE
),
80 request_(request
.Pass()),
81 handler_(handler
.Pass()),
83 last_upload_position_(0),
84 waiting_for_upload_progress_ack_(false),
85 is_transferring_(false),
86 weak_ptr_factory_(this) {
87 request_
->set_delegate(this);
88 handler_
->SetController(this);
91 ResourceLoader::~ResourceLoader() {
92 if (login_delegate_
.get())
93 login_delegate_
->OnRequestCancelled();
94 if (ssl_client_auth_handler_
.get())
95 ssl_client_auth_handler_
->OnRequestCancelled();
97 // Run ResourceHandler destructor before we tear-down the rest of our state
98 // as the ResourceHandler may want to inspect the URLRequest and other state.
102 void ResourceLoader::StartRequest() {
103 if (delegate_
->HandleExternalProtocol(this, request_
->url())) {
108 // Give the handler a chance to delay the URLRequest from being started.
109 bool defer_start
= false;
110 if (!handler_
->OnWillStart(request_
->url(), &defer_start
)) {
116 deferred_stage_
= DEFERRED_START
;
118 StartRequestInternal();
122 void ResourceLoader::CancelRequest(bool from_renderer
) {
123 CancelRequestInternal(net::ERR_ABORTED
, from_renderer
);
126 void ResourceLoader::CancelAndIgnore() {
127 ResourceRequestInfoImpl
* info
= GetRequestInfo();
128 info
->set_was_ignored_by_handler(true);
129 CancelRequest(false);
132 void ResourceLoader::CancelWithError(int error_code
) {
133 CancelRequestInternal(error_code
, false);
136 void ResourceLoader::ReportUploadProgress() {
137 if (waiting_for_upload_progress_ack_
)
138 return; // Send one progress event at a time.
140 net::UploadProgress progress
= request_
->GetUploadProgress();
141 if (!progress
.size())
142 return; // Nothing to upload.
144 if (progress
.position() == last_upload_position_
)
145 return; // No progress made since last time.
147 const uint64 kHalfPercentIncrements
= 200;
148 const TimeDelta kOneSecond
= TimeDelta::FromMilliseconds(1000);
150 uint64 amt_since_last
= progress
.position() - last_upload_position_
;
151 TimeDelta time_since_last
= TimeTicks::Now() - last_upload_ticks_
;
153 bool is_finished
= (progress
.size() == progress
.position());
154 bool enough_new_progress
=
155 (amt_since_last
> (progress
.size() / kHalfPercentIncrements
));
156 bool too_much_time_passed
= time_since_last
> kOneSecond
;
158 if (is_finished
|| enough_new_progress
|| too_much_time_passed
) {
159 if (request_
->load_flags() & net::LOAD_ENABLE_UPLOAD_PROGRESS
) {
160 handler_
->OnUploadProgress(progress
.position(), progress
.size());
161 waiting_for_upload_progress_ack_
= true;
163 last_upload_ticks_
= TimeTicks::Now();
164 last_upload_position_
= progress
.position();
168 void ResourceLoader::MarkAsTransferring() {
169 CHECK(IsResourceTypeFrame(GetRequestInfo()->GetResourceType()))
170 << "Can only transfer for navigations";
171 is_transferring_
= true;
174 void ResourceLoader::CompleteTransfer() {
175 // Although CrossSiteResourceHandler defers at OnResponseStarted
176 // (DEFERRED_READ), it may be seeing a replay of events via
177 // BufferedResourceHandler, and so the request itself is actually deferred at
178 // a later read stage.
179 DCHECK(DEFERRED_READ
== deferred_stage_
||
180 DEFERRED_RESPONSE_COMPLETE
== deferred_stage_
);
182 is_transferring_
= false;
183 GetRequestInfo()->cross_site_handler()->ResumeResponse();
186 ResourceRequestInfoImpl
* ResourceLoader::GetRequestInfo() {
187 return ResourceRequestInfoImpl::ForRequest(request_
.get());
190 void ResourceLoader::ClearLoginDelegate() {
191 login_delegate_
= NULL
;
194 void ResourceLoader::ClearSSLClientAuthHandler() {
195 ssl_client_auth_handler_
= NULL
;
198 void ResourceLoader::OnUploadProgressACK() {
199 waiting_for_upload_progress_ack_
= false;
202 void ResourceLoader::OnReceivedRedirect(net::URLRequest
* unused
,
203 const net::RedirectInfo
& redirect_info
,
205 DCHECK_EQ(request_
.get(), unused
);
207 VLOG(1) << "OnReceivedRedirect: " << request_
->url().spec();
208 DCHECK(request_
->status().is_success());
210 ResourceRequestInfoImpl
* info
= GetRequestInfo();
212 if (info
->GetProcessType() != PROCESS_TYPE_PLUGIN
&&
213 !ChildProcessSecurityPolicyImpl::GetInstance()->
214 CanRequestURL(info
->GetChildID(), redirect_info
.new_url
)) {
215 VLOG(1) << "Denied unauthorized request for "
216 << redirect_info
.new_url
.possibly_invalid_spec();
218 // Tell the renderer that this request was disallowed.
223 delegate_
->DidReceiveRedirect(this, redirect_info
.new_url
);
225 if (delegate_
->HandleExternalProtocol(this, redirect_info
.new_url
)) {
226 // The request is complete so we can remove it.
231 scoped_refptr
<ResourceResponse
> response(new ResourceResponse());
232 PopulateResourceResponse(info
, request_
.get(), response
.get());
234 if (!handler_
->OnRequestRedirected(redirect_info
, response
.get(), defer
)) {
237 deferred_stage_
= DEFERRED_REDIRECT
; // Follow redirect when resumed.
241 void ResourceLoader::OnAuthRequired(net::URLRequest
* unused
,
242 net::AuthChallengeInfo
* auth_info
) {
243 DCHECK_EQ(request_
.get(), unused
);
245 if (request_
->load_flags() & net::LOAD_DO_NOT_PROMPT_FOR_LOGIN
) {
246 request_
->CancelAuth();
250 // Create a login dialog on the UI thread to get authentication data, or pull
251 // from cache and continue on the IO thread.
253 DCHECK(!login_delegate_
.get())
254 << "OnAuthRequired called with login_delegate pending";
255 login_delegate_
= delegate_
->CreateLoginDelegate(this, auth_info
);
256 if (!login_delegate_
.get())
257 request_
->CancelAuth();
260 void ResourceLoader::OnCertificateRequested(
261 net::URLRequest
* unused
,
262 net::SSLCertRequestInfo
* cert_info
) {
263 DCHECK_EQ(request_
.get(), unused
);
265 if (request_
->load_flags() & net::LOAD_PREFETCH
) {
270 DCHECK(!ssl_client_auth_handler_
.get())
271 << "OnCertificateRequested called with ssl_client_auth_handler pending";
272 ssl_client_auth_handler_
= new SSLClientAuthHandler(
273 GetRequestInfo()->GetContext()->CreateClientCertStore(),
276 ssl_client_auth_handler_
->SelectCertificate();
279 void ResourceLoader::OnSSLCertificateError(net::URLRequest
* request
,
280 const net::SSLInfo
& ssl_info
,
282 ResourceRequestInfoImpl
* info
= GetRequestInfo();
284 int render_process_id
;
286 if (!info
->GetAssociatedRenderFrame(&render_process_id
, &render_frame_id
))
289 SSLManager::OnSSLCertificateError(
290 weak_ptr_factory_
.GetWeakPtr(),
291 info
->GetGlobalRequestID(),
292 info
->GetResourceType(),
300 void ResourceLoader::OnBeforeNetworkStart(net::URLRequest
* unused
,
302 DCHECK_EQ(request_
.get(), unused
);
304 // Give the handler a chance to delay the URLRequest from using the network.
305 if (!handler_
->OnBeforeNetworkStart(request_
->url(), defer
)) {
309 deferred_stage_
= DEFERRED_NETWORK_START
;
313 void ResourceLoader::OnResponseStarted(net::URLRequest
* unused
) {
314 DCHECK_EQ(request_
.get(), unused
);
316 VLOG(1) << "OnResponseStarted: " << request_
->url().spec();
318 // The CanLoadPage check should take place after any server redirects have
319 // finished, at the point in time that we know a page will commit in the
321 ResourceRequestInfoImpl
* info
= GetRequestInfo();
322 ChildProcessSecurityPolicyImpl
* policy
=
323 ChildProcessSecurityPolicyImpl::GetInstance();
324 if (!policy
->CanLoadPage(info
->GetChildID(),
326 info
->GetResourceType())) {
331 if (!request_
->status().is_success()) {
336 // We want to send a final upload progress message prior to sending the
337 // response complete message even if we're waiting for an ack to to a
338 // previous upload progress message.
339 waiting_for_upload_progress_ack_
= false;
340 ReportUploadProgress();
342 CompleteResponseStarted();
347 if (request_
->status().is_success()) {
348 StartReading(false); // Read the first chunk.
354 void ResourceLoader::OnReadCompleted(net::URLRequest
* unused
, int bytes_read
) {
355 DCHECK_EQ(request_
.get(), unused
);
356 VLOG(1) << "OnReadCompleted: \"" << request_
->url().spec() << "\""
357 << " bytes_read = " << bytes_read
;
359 // bytes_read == -1 always implies an error.
360 if (bytes_read
== -1 || !request_
->status().is_success()) {
365 CompleteRead(bytes_read
);
367 // If the handler cancelled or deferred the request, do not continue
368 // processing the read. If cancelled, the URLRequest has already been
369 // cancelled and will schedule an erroring OnReadCompleted later. If deferred,
370 // do nothing until resumed.
372 // Note: if bytes_read is 0 (EOF) and the handler defers, resumption will call
373 // ResponseCompleted().
374 if (is_deferred() || !request_
->status().is_success())
377 if (bytes_read
> 0) {
378 StartReading(true); // Read the next chunk.
380 // URLRequest reported an EOF. Call ResponseCompleted.
381 DCHECK_EQ(0, bytes_read
);
386 void ResourceLoader::CancelSSLRequest(const GlobalRequestID
& id
,
388 const net::SSLInfo
* ssl_info
) {
389 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
391 // The request can be NULL if it was cancelled by the renderer (as the
392 // request of the user navigating to a new page from the location bar).
393 if (!request_
->is_pending())
395 DVLOG(1) << "CancelSSLRequest() url: " << request_
->url().spec();
398 request_
->CancelWithSSLError(error
, *ssl_info
);
400 request_
->CancelWithError(error
);
404 void ResourceLoader::ContinueSSLRequest(const GlobalRequestID
& id
) {
405 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
407 DVLOG(1) << "ContinueSSLRequest() url: " << request_
->url().spec();
409 request_
->ContinueDespiteLastError();
412 void ResourceLoader::Resume() {
413 DCHECK(!is_transferring_
);
415 DeferredStage stage
= deferred_stage_
;
416 deferred_stage_
= DEFERRED_NONE
;
422 StartRequestInternal();
424 case DEFERRED_NETWORK_START
:
425 request_
->ResumeNetworkStart();
427 case DEFERRED_REDIRECT
:
428 request_
->FollowDeferredRedirect();
431 base::MessageLoop::current()->PostTask(
433 base::Bind(&ResourceLoader::ResumeReading
,
434 weak_ptr_factory_
.GetWeakPtr()));
436 case DEFERRED_RESPONSE_COMPLETE
:
437 base::MessageLoop::current()->PostTask(
439 base::Bind(&ResourceLoader::ResponseCompleted
,
440 weak_ptr_factory_
.GetWeakPtr()));
442 case DEFERRED_FINISH
:
443 // Delay self-destruction since we don't know how we were reached.
444 base::MessageLoop::current()->PostTask(
446 base::Bind(&ResourceLoader::CallDidFinishLoading
,
447 weak_ptr_factory_
.GetWeakPtr()));
452 void ResourceLoader::Cancel() {
453 CancelRequest(false);
456 void ResourceLoader::StartRequestInternal() {
457 DCHECK(!request_
->is_pending());
459 if (!request_
->status().is_success()) {
465 delegate_
->DidStartRequest(this);
468 void ResourceLoader::CancelRequestInternal(int error
, bool from_renderer
) {
469 VLOG(1) << "CancelRequestInternal: " << request_
->url().spec();
471 ResourceRequestInfoImpl
* info
= GetRequestInfo();
473 // WebKit will send us a cancel for downloads since it no longer handles
474 // them. In this case, ignore the cancel since we handle downloads in the
476 if (from_renderer
&& (info
->IsDownload() || info
->is_stream()))
479 if (from_renderer
&& info
->detachable_handler()) {
480 // TODO(davidben): Fix Blink handling of prefetches so they are not
481 // cancelled on navigate away and end up in the local cache.
482 info
->detachable_handler()->Detach();
486 // TODO(darin): Perhaps we should really be looking to see if the status is
488 bool was_pending
= request_
->is_pending();
490 if (login_delegate_
.get()) {
491 login_delegate_
->OnRequestCancelled();
492 login_delegate_
= NULL
;
494 if (ssl_client_auth_handler_
.get()) {
495 ssl_client_auth_handler_
->OnRequestCancelled();
496 ssl_client_auth_handler_
= NULL
;
499 request_
->CancelWithError(error
);
502 // If the request isn't in flight, then we won't get an asynchronous
503 // notification from the request, so we have to signal ourselves to finish
505 base::MessageLoop::current()->PostTask(
507 base::Bind(&ResourceLoader::ResponseCompleted
,
508 weak_ptr_factory_
.GetWeakPtr()));
512 void ResourceLoader::StoreSignedCertificateTimestamps(
513 const net::SignedCertificateTimestampAndStatusList
& sct_list
,
515 SignedCertificateTimestampIDStatusList
* sct_ids
) {
516 SignedCertificateTimestampStore
* sct_store(
517 SignedCertificateTimestampStore::GetInstance());
519 for (net::SignedCertificateTimestampAndStatusList::const_iterator iter
=
520 sct_list
.begin(); iter
!= sct_list
.end(); ++iter
) {
521 const int sct_id(sct_store
->Store(iter
->sct
.get(), process_id
));
523 SignedCertificateTimestampIDAndStatus(sct_id
, iter
->status
));
527 void ResourceLoader::CompleteResponseStarted() {
528 ResourceRequestInfoImpl
* info
= GetRequestInfo();
530 scoped_refptr
<ResourceResponse
> response(new ResourceResponse());
531 PopulateResourceResponse(info
, request_
.get(), response
.get());
533 if (request_
->ssl_info().cert
.get()) {
534 int cert_id
= CertStore::GetInstance()->StoreCert(
535 request_
->ssl_info().cert
.get(), info
->GetChildID());
537 SignedCertificateTimestampIDStatusList signed_certificate_timestamp_ids
;
538 StoreSignedCertificateTimestamps(
539 request_
->ssl_info().signed_certificate_timestamps
,
541 &signed_certificate_timestamp_ids
);
543 response
->head
.security_info
= SerializeSecurityInfo(
545 request_
->ssl_info().cert_status
,
546 request_
->ssl_info().security_bits
,
547 request_
->ssl_info().connection_status
,
548 signed_certificate_timestamp_ids
);
550 // We should not have any SSL state.
551 DCHECK(!request_
->ssl_info().cert_status
&&
552 request_
->ssl_info().security_bits
== -1 &&
553 !request_
->ssl_info().connection_status
);
556 delegate_
->DidReceiveResponse(this);
559 if (!handler_
->OnResponseStarted(response
.get(), &defer
)) {
562 read_deferral_start_time_
= base::TimeTicks::Now();
563 deferred_stage_
= DEFERRED_READ
; // Read first chunk when resumed.
567 void ResourceLoader::StartReading(bool is_continuation
) {
569 ReadMore(&bytes_read
);
571 // If IO is pending, wait for the URLRequest to call OnReadCompleted.
572 if (request_
->status().is_io_pending())
575 if (!is_continuation
|| bytes_read
<= 0) {
576 OnReadCompleted(request_
.get(), bytes_read
);
578 // Else, trigger OnReadCompleted asynchronously to avoid starving the IO
579 // thread in case the URLRequest can provide data synchronously.
580 base::MessageLoop::current()->PostTask(
582 base::Bind(&ResourceLoader::OnReadCompleted
,
583 weak_ptr_factory_
.GetWeakPtr(),
589 void ResourceLoader::ResumeReading() {
590 DCHECK(!is_deferred());
592 if (!read_deferral_start_time_
.is_null()) {
593 UMA_HISTOGRAM_TIMES("Net.ResourceLoader.ReadDeferral",
594 base::TimeTicks::Now() - read_deferral_start_time_
);
595 read_deferral_start_time_
= base::TimeTicks();
597 if (request_
->status().is_success()) {
598 StartReading(false); // Read the next chunk (OK to complete synchronously).
604 void ResourceLoader::ReadMore(int* bytes_read
) {
605 DCHECK(!is_deferred());
607 // Make sure we track the buffer in at least one place. This ensures it gets
608 // deleted even in the case the request has already finished its job and
609 // doesn't use the buffer.
610 scoped_refptr
<net::IOBuffer
> buf
;
612 if (!handler_
->OnWillRead(&buf
, &buf_size
, -1)) {
618 DCHECK(buf_size
> 0);
620 request_
->Read(buf
.get(), buf_size
, bytes_read
);
622 // No need to check the return value here as we'll detect errors by
623 // inspecting the URLRequest's status.
626 void ResourceLoader::CompleteRead(int bytes_read
) {
627 DCHECK(bytes_read
>= 0);
628 DCHECK(request_
->status().is_success());
631 if (!handler_
->OnReadCompleted(bytes_read
, &defer
)) {
635 bytes_read
> 0 ? DEFERRED_READ
: DEFERRED_RESPONSE_COMPLETE
;
638 // Note: the request may still have been cancelled while OnReadCompleted
639 // returns true if OnReadCompleted caused request to get cancelled
640 // out-of-band. (In AwResourceDispatcherHostDelegate::DownloadStarting, for
644 void ResourceLoader::ResponseCompleted() {
645 VLOG(1) << "ResponseCompleted: " << request_
->url().spec();
647 ResourceRequestInfoImpl
* info
= GetRequestInfo();
649 std::string security_info
;
650 const net::SSLInfo
& ssl_info
= request_
->ssl_info();
651 if (ssl_info
.cert
.get() != NULL
) {
652 int cert_id
= CertStore::GetInstance()->StoreCert(ssl_info
.cert
.get(),
654 SignedCertificateTimestampIDStatusList signed_certificate_timestamp_ids
;
655 StoreSignedCertificateTimestamps(ssl_info
.signed_certificate_timestamps
,
657 &signed_certificate_timestamp_ids
);
659 security_info
= SerializeSecurityInfo(
660 cert_id
, ssl_info
.cert_status
, ssl_info
.security_bits
,
661 ssl_info
.connection_status
, signed_certificate_timestamp_ids
);
665 handler_
->OnResponseCompleted(request_
->status(), security_info
, &defer
);
667 // The handler is not ready to die yet. We will call DidFinishLoading when
669 deferred_stage_
= DEFERRED_FINISH
;
671 // This will result in our destruction.
672 CallDidFinishLoading();
676 void ResourceLoader::CallDidFinishLoading() {
677 delegate_
->DidFinishLoading(this);
680 void ResourceLoader::RecordHistograms() {
681 ResourceRequestInfoImpl
* info
= GetRequestInfo();
683 if (info
->GetResourceType() == RESOURCE_TYPE_PREFETCH
) {
684 PrefetchStatus status
= STATUS_UNDEFINED
;
685 TimeDelta total_time
= base::TimeTicks::Now() - request_
->creation_time();
687 switch (request_
->status().status()) {
688 case net::URLRequestStatus::SUCCESS
:
689 if (request_
->was_cached()) {
690 status
= STATUS_SUCCESS_FROM_CACHE
;
691 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentPrefetchingFromCache",
694 status
= STATUS_SUCCESS_FROM_NETWORK
;
695 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentPrefetchingFromNetwork",
699 case net::URLRequestStatus::CANCELED
:
700 status
= STATUS_CANCELED
;
701 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeBeforeCancel", total_time
);
703 case net::URLRequestStatus::IO_PENDING
:
704 case net::URLRequestStatus::FAILED
:
705 status
= STATUS_UNDEFINED
;
709 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", status
, STATUS_MAX
);
713 } // namespace content