OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/loader/resource_loader.h" | 5 #include "content/browser/loader/resource_loader.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/profiler/scoped_tracker.h" | 12 #include "base/profiler/scoped_tracker.h" |
13 #include "base/single_thread_task_runner.h" | 13 #include "base/single_thread_task_runner.h" |
14 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "base/trace_event/trace_event.h" | 16 #include "base/trace_event/trace_event.h" |
17 #include "content/browser/appcache/appcache_interceptor.h" | 17 #include "content/browser/appcache/appcache_interceptor.h" |
18 #include "content/browser/child_process_security_policy_impl.h" | 18 #include "content/browser/child_process_security_policy_impl.h" |
19 #include "content/browser/loader/cross_site_resource_handler.h" | 19 #include "content/browser/loader/cross_site_resource_handler.h" |
20 #include "content/browser/loader/detachable_resource_handler.h" | 20 #include "content/browser/loader/detachable_resource_handler.h" |
21 #include "content/browser/loader/resource_loader_delegate.h" | 21 #include "content/browser/loader/resource_loader_delegate.h" |
22 #include "content/browser/loader/resource_request_info_impl.h" | 22 #include "content/browser/loader/resource_request_info_impl.h" |
23 #include "content/browser/service_worker/service_worker_request_handler.h" | 23 #include "content/browser/service_worker/service_worker_request_handler.h" |
24 #include "content/browser/service_worker/service_worker_response_info.h" | 24 #include "content/browser/service_worker/service_worker_response_info.h" |
25 #include "content/browser/ssl/ssl_client_auth_handler.h" | 25 #include "content/browser/ssl/ssl_client_auth_handler.h" |
26 #include "content/browser/ssl/ssl_manager.h" | 26 #include "content/browser/ssl/ssl_manager.h" |
27 #include "content/browser/ssl/ssl_policy.h" | 27 #include "content/browser/ssl/ssl_policy.h" |
28 #include "content/common/security_style_util.h" | 28 #include "content/common/security_style_util.h" |
29 #include "content/public/browser/cert_store.h" | |
30 #include "content/public/browser/resource_dispatcher_host_login_delegate.h" | 29 #include "content/public/browser/resource_dispatcher_host_login_delegate.h" |
31 #include "content/public/common/content_client.h" | 30 #include "content/public/common/content_client.h" |
32 #include "content/public/common/content_switches.h" | 31 #include "content/public/common/content_switches.h" |
33 #include "content/public/common/process_type.h" | 32 #include "content/public/common/process_type.h" |
34 #include "content/public/common/resource_response.h" | 33 #include "content/public/common/resource_response.h" |
35 #include "content/public/common/resource_type.h" | 34 #include "content/public/common/resource_type.h" |
36 #include "content/public/common/security_style.h" | 35 #include "content/public/common/security_style.h" |
37 #include "content/public/common/ssl_status.h" | 36 #include "content/public/common/ssl_status.h" |
38 #include "net/base/io_buffer.h" | 37 #include "net/base/io_buffer.h" |
39 #include "net/base/load_flags.h" | 38 #include "net/base/load_flags.h" |
40 #include "net/http/http_response_headers.h" | 39 #include "net/http/http_response_headers.h" |
41 #include "net/nqe/effective_connection_type.h" | 40 #include "net/nqe/effective_connection_type.h" |
42 #include "net/nqe/network_quality_estimator.h" | 41 #include "net/nqe/network_quality_estimator.h" |
43 #include "net/ssl/client_cert_store.h" | 42 #include "net/ssl/client_cert_store.h" |
44 #include "net/ssl/ssl_platform_key.h" | 43 #include "net/ssl/ssl_platform_key.h" |
45 #include "net/ssl/ssl_private_key.h" | 44 #include "net/ssl/ssl_private_key.h" |
46 #include "net/url_request/redirect_info.h" | 45 #include "net/url_request/redirect_info.h" |
47 #include "net/url_request/url_request_context.h" | 46 #include "net/url_request/url_request_context.h" |
48 #include "net/url_request/url_request_status.h" | 47 #include "net/url_request/url_request_status.h" |
49 | 48 |
50 using base::TimeDelta; | 49 using base::TimeDelta; |
51 using base::TimeTicks; | 50 using base::TimeTicks; |
52 | 51 |
53 namespace content { | 52 namespace content { |
54 namespace { | 53 namespace { |
55 | 54 |
56 void PopulateResourceResponse(ResourceRequestInfoImpl* info, | 55 void PopulateResourceResponse(ResourceRequestInfoImpl* info, |
57 net::URLRequest* request, | 56 net::URLRequest* request, |
58 CertStore* cert_store, | |
59 ResourceResponse* response) { | 57 ResourceResponse* response) { |
60 response->head.request_time = request->request_time(); | 58 response->head.request_time = request->request_time(); |
61 response->head.response_time = request->response_time(); | 59 response->head.response_time = request->response_time(); |
62 response->head.headers = request->response_headers(); | 60 response->head.headers = request->response_headers(); |
63 request->GetCharset(&response->head.charset); | 61 request->GetCharset(&response->head.charset); |
64 response->head.content_length = request->GetExpectedContentSize(); | 62 response->head.content_length = request->GetExpectedContentSize(); |
65 request->GetMimeType(&response->head.mime_type); | 63 request->GetMimeType(&response->head.mime_type); |
66 net::HttpResponseInfo response_info = request->response_info(); | 64 net::HttpResponseInfo response_info = request->response_info(); |
67 response->head.was_fetched_via_spdy = response_info.was_fetched_via_spdy; | 65 response->head.was_fetched_via_spdy = response_info.was_fetched_via_spdy; |
68 response->head.was_npn_negotiated = response_info.was_npn_negotiated; | 66 response->head.was_npn_negotiated = response_info.was_npn_negotiated; |
(...skipping 29 matching lines...) Expand all Loading... |
98 request, &response->head.appcache_id, | 96 request, &response->head.appcache_id, |
99 &response->head.appcache_manifest_url); | 97 &response->head.appcache_manifest_url); |
100 if (info->is_load_timing_enabled()) | 98 if (info->is_load_timing_enabled()) |
101 request->GetLoadTimingInfo(&response->head.load_timing); | 99 request->GetLoadTimingInfo(&response->head.load_timing); |
102 | 100 |
103 if (request->ssl_info().cert.get()) { | 101 if (request->ssl_info().cert.get()) { |
104 SSLStatus ssl_status; | 102 SSLStatus ssl_status; |
105 // TODO(jam): keep this call temporarily since it's what adds the | 103 // TODO(jam): keep this call temporarily since it's what adds the |
106 // certificate to the CertStore. | 104 // certificate to the CertStore. |
107 ResourceLoader::GetSSLStatusForRequest( | 105 ResourceLoader::GetSSLStatusForRequest( |
108 request->url(), request->ssl_info(), info->GetChildID(), | 106 request->url(), request->ssl_info(), info->GetChildID(), &ssl_status); |
109 cert_store, &ssl_status); | |
110 response->head.has_major_certificate_errors = | 107 response->head.has_major_certificate_errors = |
111 net::IsCertStatusError(request->ssl_info().cert_status) && | 108 net::IsCertStatusError(request->ssl_info().cert_status) && |
112 !net::IsCertStatusMinorError(request->ssl_info().cert_status); | 109 !net::IsCertStatusMinorError(request->ssl_info().cert_status); |
113 if (info->ShouldReportRawHeaders()) { | 110 if (info->ShouldReportRawHeaders()) { |
114 // Only pass these members when the network panel of the DevTools is open, | 111 // Only pass these members when the network panel of the DevTools is open, |
115 // i.e. ShouldReportRawHeaders() is set. These data are used to populate | 112 // i.e. ShouldReportRawHeaders() is set. These data are used to populate |
116 // the requests in the security panel too. | 113 // the requests in the security panel too. |
117 response->head.cert_status = request->ssl_info().cert_status; | 114 response->head.cert_status = request->ssl_info().cert_status; |
118 response->head.ssl_connection_status = | 115 response->head.ssl_connection_status = |
119 request->ssl_info().connection_status; | 116 request->ssl_info().connection_status; |
(...skipping 18 matching lines...) Expand all Loading... |
138 DCHECK_EQ(request->ssl_info().key_exchange_info, 0); | 135 DCHECK_EQ(request->ssl_info().key_exchange_info, 0); |
139 DCHECK(!request->ssl_info().connection_status); | 136 DCHECK(!request->ssl_info().connection_status); |
140 } | 137 } |
141 } | 138 } |
142 | 139 |
143 } // namespace | 140 } // namespace |
144 | 141 |
145 void ResourceLoader::GetSSLStatusForRequest(const GURL& url, | 142 void ResourceLoader::GetSSLStatusForRequest(const GURL& url, |
146 const net::SSLInfo& ssl_info, | 143 const net::SSLInfo& ssl_info, |
147 int child_id, | 144 int child_id, |
148 CertStore* cert_store, | |
149 SSLStatus* ssl_status) { | 145 SSLStatus* ssl_status) { |
150 DCHECK(ssl_info.cert); | 146 DCHECK(ssl_info.cert); |
151 int cert_id = cert_store->StoreCert(ssl_info.cert.get(), child_id); | |
152 | |
153 *ssl_status = SSLStatus(GetSecurityStyleForResource( | 147 *ssl_status = SSLStatus(GetSecurityStyleForResource( |
154 url, !!cert_id, ssl_info.cert_status), | 148 url, !!ssl_info.cert, ssl_info.cert_status), |
155 cert_id, ssl_info); | 149 ssl_info.cert, ssl_info); |
156 } | 150 } |
157 | 151 |
158 ResourceLoader::ResourceLoader(std::unique_ptr<net::URLRequest> request, | 152 ResourceLoader::ResourceLoader(std::unique_ptr<net::URLRequest> request, |
159 std::unique_ptr<ResourceHandler> handler, | 153 std::unique_ptr<ResourceHandler> handler, |
160 CertStore* cert_store, | |
161 ResourceLoaderDelegate* delegate) | 154 ResourceLoaderDelegate* delegate) |
162 : deferred_stage_(DEFERRED_NONE), | 155 : deferred_stage_(DEFERRED_NONE), |
163 request_(std::move(request)), | 156 request_(std::move(request)), |
164 handler_(std::move(handler)), | 157 handler_(std::move(handler)), |
165 delegate_(delegate), | 158 delegate_(delegate), |
166 is_transferring_(false), | 159 is_transferring_(false), |
167 times_cancelled_before_request_start_(0), | 160 times_cancelled_before_request_start_(0), |
168 started_request_(false), | 161 started_request_(false), |
169 times_cancelled_after_request_start_(0), | 162 times_cancelled_after_request_start_(0), |
170 cert_store_(cert_store), | |
171 weak_ptr_factory_(this) { | 163 weak_ptr_factory_(this) { |
172 request_->set_delegate(this); | 164 request_->set_delegate(this); |
173 handler_->SetController(this); | 165 handler_->SetController(this); |
174 } | 166 } |
175 | 167 |
176 ResourceLoader::~ResourceLoader() { | 168 ResourceLoader::~ResourceLoader() { |
177 if (login_delegate_.get()) | 169 if (login_delegate_.get()) |
178 login_delegate_->OnRequestCancelled(); | 170 login_delegate_->OnRequestCancelled(); |
179 ssl_client_auth_handler_.reset(); | 171 ssl_client_auth_handler_.reset(); |
180 | 172 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CancelWithError", this, | 213 TRACE_EVENT_WITH_FLOW0("loading", "ResourceLoader::CancelWithError", this, |
222 TRACE_EVENT_FLAG_FLOW_IN); | 214 TRACE_EVENT_FLAG_FLOW_IN); |
223 CancelRequestInternal(error_code, false); | 215 CancelRequestInternal(error_code, false); |
224 } | 216 } |
225 | 217 |
226 void ResourceLoader::MarkAsTransferring( | 218 void ResourceLoader::MarkAsTransferring( |
227 const scoped_refptr<ResourceResponse>& response) { | 219 const scoped_refptr<ResourceResponse>& response) { |
228 CHECK(IsResourceTypeFrame(GetRequestInfo()->GetResourceType())) | 220 CHECK(IsResourceTypeFrame(GetRequestInfo()->GetResourceType())) |
229 << "Can only transfer for navigations"; | 221 << "Can only transfer for navigations"; |
230 is_transferring_ = true; | 222 is_transferring_ = true; |
231 transferring_response_ = response; | |
232 | 223 |
233 int child_id = GetRequestInfo()->GetChildID(); | 224 int child_id = GetRequestInfo()->GetChildID(); |
234 AppCacheInterceptor::PrepareForCrossSiteTransfer(request(), child_id); | 225 AppCacheInterceptor::PrepareForCrossSiteTransfer(request(), child_id); |
235 ServiceWorkerRequestHandler* handler = | 226 ServiceWorkerRequestHandler* handler = |
236 ServiceWorkerRequestHandler::GetHandler(request()); | 227 ServiceWorkerRequestHandler::GetHandler(request()); |
237 if (handler) | 228 if (handler) |
238 handler->PrepareForCrossSiteTransfer(child_id); | 229 handler->PrepareForCrossSiteTransfer(child_id); |
239 } | 230 } |
240 | 231 |
241 void ResourceLoader::CompleteTransfer() { | 232 void ResourceLoader::CompleteTransfer() { |
242 // Although CrossSiteResourceHandler defers at OnResponseStarted | 233 // Although CrossSiteResourceHandler defers at OnResponseStarted |
243 // (DEFERRED_READ), it may be seeing a replay of events via | 234 // (DEFERRED_READ), it may be seeing a replay of events via |
244 // MimeTypeResourceHandler, and so the request itself is actually deferred | 235 // MimeTypeResourceHandler, and so the request itself is actually deferred |
245 // at a later read stage. | 236 // at a later read stage. |
246 DCHECK(DEFERRED_READ == deferred_stage_ || | 237 DCHECK(DEFERRED_READ == deferred_stage_ || |
247 DEFERRED_RESPONSE_COMPLETE == deferred_stage_); | 238 DEFERRED_RESPONSE_COMPLETE == deferred_stage_); |
248 DCHECK(is_transferring_); | 239 DCHECK(is_transferring_); |
249 DCHECK(transferring_response_); | |
250 | 240 |
251 // In some cases, a process transfer doesn't really happen and the | 241 // In some cases, a process transfer doesn't really happen and the |
252 // request is resumed in the original process. Real transfers to a new process | 242 // request is resumed in the original process. Real transfers to a new process |
253 // are completed via ResourceDispatcherHostImpl::UpdateRequestForTransfer. | 243 // are completed via ResourceDispatcherHostImpl::UpdateRequestForTransfer. |
254 int child_id = GetRequestInfo()->GetChildID(); | 244 int child_id = GetRequestInfo()->GetChildID(); |
255 AppCacheInterceptor::MaybeCompleteCrossSiteTransferInOldProcess( | 245 AppCacheInterceptor::MaybeCompleteCrossSiteTransferInOldProcess( |
256 request(), child_id); | 246 request(), child_id); |
257 ServiceWorkerRequestHandler* handler = | 247 ServiceWorkerRequestHandler* handler = |
258 ServiceWorkerRequestHandler::GetHandler(request()); | 248 ServiceWorkerRequestHandler::GetHandler(request()); |
259 if (handler) | 249 if (handler) |
260 handler->MaybeCompleteCrossSiteTransferInOldProcess(child_id); | 250 handler->MaybeCompleteCrossSiteTransferInOldProcess(child_id); |
261 | 251 |
262 is_transferring_ = false; | 252 is_transferring_ = false; |
263 transferring_response_ = nullptr; | |
264 GetRequestInfo()->cross_site_handler()->ResumeResponse(); | 253 GetRequestInfo()->cross_site_handler()->ResumeResponse(); |
265 } | 254 } |
266 | 255 |
267 ResourceRequestInfoImpl* ResourceLoader::GetRequestInfo() { | 256 ResourceRequestInfoImpl* ResourceLoader::GetRequestInfo() { |
268 return ResourceRequestInfoImpl::ForRequest(request_.get()); | 257 return ResourceRequestInfoImpl::ForRequest(request_.get()); |
269 } | 258 } |
270 | 259 |
271 void ResourceLoader::ClearLoginDelegate() { | 260 void ResourceLoader::ClearLoginDelegate() { |
272 login_delegate_ = NULL; | 261 login_delegate_ = NULL; |
273 } | 262 } |
(...skipping 20 matching lines...) Expand all Loading... |
294 return; | 283 return; |
295 } | 284 } |
296 | 285 |
297 if (delegate_->HandleExternalProtocol(this, redirect_info.new_url)) { | 286 if (delegate_->HandleExternalProtocol(this, redirect_info.new_url)) { |
298 // The request is complete so we can remove it. | 287 // The request is complete so we can remove it. |
299 CancelAndIgnore(); | 288 CancelAndIgnore(); |
300 return; | 289 return; |
301 } | 290 } |
302 | 291 |
303 scoped_refptr<ResourceResponse> response = new ResourceResponse(); | 292 scoped_refptr<ResourceResponse> response = new ResourceResponse(); |
304 PopulateResourceResponse(info, request_.get(), cert_store_, response.get()); | 293 PopulateResourceResponse(info, request_.get(), response.get()); |
305 delegate_->DidReceiveRedirect(this, redirect_info.new_url, response.get()); | 294 delegate_->DidReceiveRedirect(this, redirect_info.new_url, response.get()); |
306 if (!handler_->OnRequestRedirected(redirect_info, response.get(), defer)) { | 295 if (!handler_->OnRequestRedirected(redirect_info, response.get(), defer)) { |
307 Cancel(); | 296 Cancel(); |
308 } else if (*defer) { | 297 } else if (*defer) { |
309 deferred_stage_ = DEFERRED_REDIRECT; // Follow redirect when resumed. | 298 deferred_stage_ = DEFERRED_REDIRECT; // Follow redirect when resumed. |
310 } | 299 } |
311 } | 300 } |
312 | 301 |
313 void ResourceLoader::OnAuthRequired(net::URLRequest* unused, | 302 void ResourceLoader::OnAuthRequired(net::URLRequest* unused, |
314 net::AuthChallengeInfo* auth_info) { | 303 net::AuthChallengeInfo* auth_info) { |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 // this request. | 546 // this request. |
558 base::ThreadTaskRunnerHandle::Get()->PostTask( | 547 base::ThreadTaskRunnerHandle::Get()->PostTask( |
559 FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted, | 548 FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted, |
560 weak_ptr_factory_.GetWeakPtr())); | 549 weak_ptr_factory_.GetWeakPtr())); |
561 } | 550 } |
562 } | 551 } |
563 | 552 |
564 void ResourceLoader::CompleteResponseStarted() { | 553 void ResourceLoader::CompleteResponseStarted() { |
565 ResourceRequestInfoImpl* info = GetRequestInfo(); | 554 ResourceRequestInfoImpl* info = GetRequestInfo(); |
566 scoped_refptr<ResourceResponse> response = new ResourceResponse(); | 555 scoped_refptr<ResourceResponse> response = new ResourceResponse(); |
567 PopulateResourceResponse(info, request_.get(), cert_store_, response.get()); | 556 PopulateResourceResponse(info, request_.get(), response.get()); |
568 | 557 |
569 delegate_->DidReceiveResponse(this); | 558 delegate_->DidReceiveResponse(this); |
570 | 559 |
571 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. | 560 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. |
572 tracked_objects::ScopedTracker tracking_profile( | 561 tracked_objects::ScopedTracker tracking_profile( |
573 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseStarted()")); | 562 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseStarted()")); |
574 | 563 |
575 bool defer = false; | 564 bool defer = false; |
576 if (!handler_->OnResponseStarted(response.get(), &defer)) { | 565 if (!handler_->OnResponseStarted(response.get(), &defer)) { |
577 Cancel(); | 566 Cancel(); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 DVLOG(1) << "ResponseCompleted: " << request_->url().spec(); | 667 DVLOG(1) << "ResponseCompleted: " << request_->url().spec(); |
679 RecordHistograms(); | 668 RecordHistograms(); |
680 ResourceRequestInfoImpl* info = GetRequestInfo(); | 669 ResourceRequestInfoImpl* info = GetRequestInfo(); |
681 | 670 |
682 const net::SSLInfo& ssl_info = request_->ssl_info(); | 671 const net::SSLInfo& ssl_info = request_->ssl_info(); |
683 if (ssl_info.cert.get() != NULL) { | 672 if (ssl_info.cert.get() != NULL) { |
684 SSLStatus ssl_status; | 673 SSLStatus ssl_status; |
685 // TODO(jam): keep this call temporarily since it's what adds the | 674 // TODO(jam): keep this call temporarily since it's what adds the |
686 // certificate to the CertStore. | 675 // certificate to the CertStore. |
687 GetSSLStatusForRequest(request_->url(), ssl_info, info->GetChildID(), | 676 GetSSLStatusForRequest(request_->url(), ssl_info, info->GetChildID(), |
688 cert_store_, &ssl_status); | 677 &ssl_status); |
689 } | 678 } |
690 | 679 |
691 bool defer = false; | 680 bool defer = false; |
692 { | 681 { |
693 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. | 682 // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. |
694 tracked_objects::ScopedTracker tracking_profile( | 683 tracked_objects::ScopedTracker tracking_profile( |
695 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseCompleted()")); | 684 FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseCompleted()")); |
696 | 685 |
697 handler_->OnResponseCompleted(request_->status(), &defer); | 686 handler_->OnResponseCompleted(request_->status(), &defer); |
698 } | 687 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 } | 743 } |
755 | 744 |
756 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", status, STATUS_MAX); | 745 UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", status, STATUS_MAX); |
757 } else if (request_->response_info().unused_since_prefetch) { | 746 } else if (request_->response_info().unused_since_prefetch) { |
758 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time(); | 747 TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time(); |
759 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time); | 748 UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentOnPrefetchHit", total_time); |
760 } | 749 } |
761 } | 750 } |
762 | 751 |
763 } // namespace content | 752 } // namespace content |
OLD | NEW |