[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 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. |
| 4 | |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 5 | #include "content/browser/loader/resource_loader.h" |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 6 | |
[email protected] | bbdd1b20b | 2012-12-11 21:24:13 | [diff] [blame] | 7 | #include "base/command_line.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 8 | #include "base/location.h" |
[email protected] | 5e318348 | 2013-08-09 11:05:19 | [diff] [blame] | 9 | #include "base/metrics/histogram.h" |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 10 | #include "base/profiler/scoped_tracker.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 11 | #include "base/single_thread_task_runner.h" |
| 12 | #include "base/thread_task_runner_handle.h" |
[email protected] | a43858f | 2013-06-28 15:18:37 | [diff] [blame] | 13 | #include "base/time/time.h" |
[email protected] | db367805 | 2014-04-01 16:08:03 | [diff] [blame] | 14 | #include "content/browser/appcache/appcache_interceptor.h" |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 15 | #include "content/browser/child_process_security_policy_impl.h" |
[email protected] | fc6c187 | 2013-10-03 01:22:35 | [diff] [blame] | 16 | #include "content/browser/loader/cross_site_resource_handler.h" |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 17 | #include "content/browser/loader/detachable_resource_handler.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 18 | #include "content/browser/loader/resource_loader_delegate.h" |
| 19 | #include "content/browser/loader/resource_request_info_impl.h" |
[email protected] | 6ee72da1 | 2014-07-22 04:39:10 | [diff] [blame] | 20 | #include "content/browser/service_worker/service_worker_request_handler.h" |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 21 | #include "content/browser/ssl/ssl_client_auth_handler.h" |
| 22 | #include "content/browser/ssl/ssl_manager.h" |
estark | 294fbd9 | 2015-07-31 18:36:24 | [diff] [blame] | 23 | #include "content/browser/ssl/ssl_policy.h" |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 24 | #include "content/common/ssl_status_serialization.h" |
| 25 | #include "content/public/browser/cert_store.h" |
[email protected] | 1ccb699 | 2013-10-30 04:46:20 | [diff] [blame] | 26 | #include "content/public/browser/resource_context.h" |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 27 | #include "content/public/browser/resource_dispatcher_host_login_delegate.h" |
[email protected] | 0bbd63b | 2013-11-29 00:02:12 | [diff] [blame] | 28 | #include "content/public/browser/signed_certificate_timestamp_store.h" |
[email protected] | bbdd1b20b | 2012-12-11 21:24:13 | [diff] [blame] | 29 | #include "content/public/common/content_client.h" |
| 30 | #include "content/public/common/content_switches.h" |
[email protected] | f3b35769 | 2013-03-22 05:16:13 | [diff] [blame] | 31 | #include "content/public/common/process_type.h" |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 32 | #include "content/public/common/resource_response.h" |
estark | 294fbd9 | 2015-07-31 18:36:24 | [diff] [blame] | 33 | #include "content/public/common/security_style.h" |
[email protected] | fc6c187 | 2013-10-03 01:22:35 | [diff] [blame] | 34 | #include "net/base/io_buffer.h" |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 35 | #include "net/base/load_flags.h" |
| 36 | #include "net/http/http_response_headers.h" |
[email protected] | 536fd0b | 2013-03-14 17:41:57 | [diff] [blame] | 37 | #include "net/ssl/client_cert_store.h" |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 38 | #include "net/ssl/ssl_platform_key.h" |
| 39 | #include "net/ssl/ssl_private_key.h" |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 40 | #include "net/url_request/redirect_info.h" |
[email protected] | 3ed8472 | 2013-11-01 17:17:07 | [diff] [blame] | 41 | #include "net/url_request/url_request_status.h" |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 42 | |
| 43 | using base::TimeDelta; |
| 44 | using base::TimeTicks; |
| 45 | |
| 46 | namespace content { |
| 47 | namespace { |
| 48 | |
estark | 294fbd9 | 2015-07-31 18:36:24 | [diff] [blame] | 49 | void StoreSignedCertificateTimestamps( |
| 50 | const net::SignedCertificateTimestampAndStatusList& sct_list, |
| 51 | int process_id, |
| 52 | SignedCertificateTimestampIDStatusList* sct_ids) { |
| 53 | SignedCertificateTimestampStore* sct_store( |
| 54 | SignedCertificateTimestampStore::GetInstance()); |
| 55 | |
| 56 | for (auto iter = sct_list.begin(); iter != sct_list.end(); ++iter) { |
| 57 | const int sct_id(sct_store->Store(iter->sct.get(), process_id)); |
| 58 | sct_ids->push_back( |
| 59 | SignedCertificateTimestampIDAndStatus(sct_id, iter->status)); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void GetSSLStatusForRequest(const GURL& url, |
| 64 | const net::SSLInfo& ssl_info, |
| 65 | int child_id, |
| 66 | SSLStatus* ssl_status) { |
| 67 | DCHECK(ssl_info.cert); |
| 68 | |
| 69 | int cert_id = |
| 70 | CertStore::GetInstance()->StoreCert(ssl_info.cert.get(), child_id); |
| 71 | |
| 72 | SignedCertificateTimestampIDStatusList signed_certificate_timestamp_ids; |
| 73 | StoreSignedCertificateTimestamps(ssl_info.signed_certificate_timestamps, |
| 74 | child_id, &signed_certificate_timestamp_ids); |
| 75 | |
estark | ced641ec | 2015-09-12 02:17:31 | [diff] [blame] | 76 | *ssl_status = SSLStatus(SSLPolicy::GetSecurityStyleForResource( |
| 77 | url, cert_id, ssl_info.cert_status), |
| 78 | cert_id, signed_certificate_timestamp_ids, ssl_info); |
estark | 294fbd9 | 2015-07-31 18:36:24 | [diff] [blame] | 79 | } |
| 80 | |
estark | a7140bc | 2015-08-12 17:29:37 | [diff] [blame] | 81 | void PopulateResourceResponse(ResourceRequestInfoImpl* info, |
| 82 | net::URLRequest* request, |
| 83 | ResourceResponse* response) { |
| 84 | response->head.request_time = request->request_time(); |
| 85 | response->head.response_time = request->response_time(); |
| 86 | response->head.headers = request->response_headers(); |
| 87 | request->GetCharset(&response->head.charset); |
| 88 | response->head.content_length = request->GetExpectedContentSize(); |
| 89 | request->GetMimeType(&response->head.mime_type); |
| 90 | net::HttpResponseInfo response_info = request->response_info(); |
| 91 | response->head.was_fetched_via_spdy = response_info.was_fetched_via_spdy; |
| 92 | response->head.was_npn_negotiated = response_info.was_npn_negotiated; |
| 93 | response->head.npn_negotiated_protocol = |
| 94 | response_info.npn_negotiated_protocol; |
| 95 | response->head.connection_info = response_info.connection_info; |
| 96 | response->head.was_fetched_via_proxy = request->was_fetched_via_proxy(); |
| 97 | response->head.proxy_server = response_info.proxy_server; |
| 98 | response->head.socket_address = request->GetSocketAddress(); |
megjablon | d5ac7d5 | 2015-10-22 23:56:12 | [diff] [blame] | 99 | const content::ResourceRequestInfo* request_info = |
| 100 | content::ResourceRequestInfo::ForRequest(request); |
| 101 | if (request_info) |
| 102 | response->head.is_using_lofi = request_info->IsUsingLoFi(); |
estark | a7140bc | 2015-08-12 17:29:37 | [diff] [blame] | 103 | if (ServiceWorkerRequestHandler* handler = |
| 104 | ServiceWorkerRequestHandler::GetHandler(request)) { |
| 105 | handler->GetExtraResponseInfo(&response->head); |
| 106 | } |
| 107 | AppCacheInterceptor::GetExtraResponseInfo( |
| 108 | request, &response->head.appcache_id, |
| 109 | &response->head.appcache_manifest_url); |
| 110 | if (info->is_load_timing_enabled()) |
| 111 | request->GetLoadTimingInfo(&response->head.load_timing); |
| 112 | |
| 113 | if (request->ssl_info().cert.get()) { |
| 114 | SSLStatus ssl_status; |
| 115 | GetSSLStatusForRequest(request->url(), request->ssl_info(), |
| 116 | info->GetChildID(), &ssl_status); |
| 117 | response->head.security_info = SerializeSecurityInfo(ssl_status); |
estark | a7140bc | 2015-08-12 17:29:37 | [diff] [blame] | 118 | } else { |
| 119 | // We should not have any SSL state. |
sigbjorn | 79cf372 | 2015-09-18 09:15:20 | [diff] [blame] | 120 | DCHECK(!request->ssl_info().cert_status); |
| 121 | DCHECK_EQ(request->ssl_info().security_bits, -1); |
| 122 | DCHECK_EQ(request->ssl_info().key_exchange_info, 0); |
| 123 | DCHECK(!request->ssl_info().connection_status); |
estark | a7140bc | 2015-08-12 17:29:37 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 127 | } // namespace |
| 128 | |
| 129 | ResourceLoader::ResourceLoader(scoped_ptr<net::URLRequest> request, |
| 130 | scoped_ptr<ResourceHandler> handler, |
| 131 | ResourceLoaderDelegate* delegate) |
[email protected] | 1ccb699 | 2013-10-30 04:46:20 | [diff] [blame] | 132 | : deferred_stage_(DEFERRED_NONE), |
| 133 | request_(request.Pass()), |
| 134 | handler_(handler.Pass()), |
| 135 | delegate_(delegate), |
[email protected] | 1ccb699 | 2013-10-30 04:46:20 | [diff] [blame] | 136 | is_transferring_(false), |
mmenke | e0b2bd62 | 2015-06-24 20:23:10 | [diff] [blame] | 137 | times_cancelled_before_request_start_(0), |
| 138 | started_request_(false), |
| 139 | times_cancelled_after_request_start_(0), |
[email protected] | 1ccb699 | 2013-10-30 04:46:20 | [diff] [blame] | 140 | weak_ptr_factory_(this) { |
| 141 | request_->set_delegate(this); |
| 142 | handler_->SetController(this); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | ResourceLoader::~ResourceLoader() { |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 146 | if (login_delegate_.get()) |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 147 | login_delegate_->OnRequestCancelled(); |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 148 | ssl_client_auth_handler_.reset(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 149 | |
| 150 | // Run ResourceHandler destructor before we tear-down the rest of our state |
| 151 | // as the ResourceHandler may want to inspect the URLRequest and other state. |
| 152 | handler_.reset(); |
| 153 | } |
| 154 | |
| 155 | void ResourceLoader::StartRequest() { |
| 156 | if (delegate_->HandleExternalProtocol(this, request_->url())) { |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 157 | CancelAndIgnore(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
| 161 | // Give the handler a chance to delay the URLRequest from being started. |
| 162 | bool defer_start = false; |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 163 | if (!handler_->OnWillStart(request_->url(), &defer_start)) { |
| 164 | Cancel(); |
| 165 | return; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | if (defer_start) { |
| 169 | deferred_stage_ = DEFERRED_START; |
| 170 | } else { |
| 171 | StartRequestInternal(); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | void ResourceLoader::CancelRequest(bool from_renderer) { |
| 176 | CancelRequestInternal(net::ERR_ABORTED, from_renderer); |
| 177 | } |
| 178 | |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 179 | void ResourceLoader::CancelAndIgnore() { |
| 180 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 181 | info->set_was_ignored_by_handler(true); |
| 182 | CancelRequest(false); |
| 183 | } |
| 184 | |
[email protected] | af3d496 | 2012-10-19 10:57:26 | [diff] [blame] | 185 | void ResourceLoader::CancelWithError(int error_code) { |
| 186 | CancelRequestInternal(error_code, false); |
| 187 | } |
| 188 | |
[email protected] | f820d9c5 | 2013-12-18 18:42:36 | [diff] [blame] | 189 | void ResourceLoader::MarkAsTransferring() { |
[email protected] | 6c1e0521 | 2014-07-31 00:59:40 | [diff] [blame] | 190 | CHECK(IsResourceTypeFrame(GetRequestInfo()->GetResourceType())) |
[email protected] | 46c70476 | 2013-12-18 18:10:24 | [diff] [blame] | 191 | << "Can only transfer for navigations"; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 192 | is_transferring_ = true; |
michaeln | bfea6ec | 2014-12-09 23:16:13 | [diff] [blame] | 193 | |
| 194 | int child_id = GetRequestInfo()->GetChildID(); |
| 195 | AppCacheInterceptor::PrepareForCrossSiteTransfer(request(), child_id); |
| 196 | ServiceWorkerRequestHandler* handler = |
| 197 | ServiceWorkerRequestHandler::GetHandler(request()); |
| 198 | if (handler) |
| 199 | handler->PrepareForCrossSiteTransfer(child_id); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 200 | } |
| 201 | |
[email protected] | 1f291cd | 2013-09-25 22:05:14 | [diff] [blame] | 202 | void ResourceLoader::CompleteTransfer() { |
[email protected] | 17b674e0 | 2014-05-10 04:30:02 | [diff] [blame] | 203 | // Although CrossSiteResourceHandler defers at OnResponseStarted |
| 204 | // (DEFERRED_READ), it may be seeing a replay of events via |
mmenke | 664a8fd | 2015-06-13 00:08:51 | [diff] [blame] | 205 | // MimeTypeResourceHandler, and so the request itself is actually deferred |
| 206 | // at a later read stage. |
[email protected] | 17b674e0 | 2014-05-10 04:30:02 | [diff] [blame] | 207 | DCHECK(DEFERRED_READ == deferred_stage_ || |
| 208 | DEFERRED_RESPONSE_COMPLETE == deferred_stage_); |
michaeln | bfea6ec | 2014-12-09 23:16:13 | [diff] [blame] | 209 | DCHECK(is_transferring_); |
| 210 | |
| 211 | // In some cases, a process transfer doesn't really happen and the |
| 212 | // request is resumed in the original process. Real transfers to a new process |
| 213 | // are completed via ResourceDispatcherHostImpl::UpdateRequestForTransfer. |
| 214 | int child_id = GetRequestInfo()->GetChildID(); |
| 215 | AppCacheInterceptor::MaybeCompleteCrossSiteTransferInOldProcess( |
| 216 | request(), child_id); |
| 217 | ServiceWorkerRequestHandler* handler = |
| 218 | ServiceWorkerRequestHandler::GetHandler(request()); |
| 219 | if (handler) |
| 220 | handler->MaybeCompleteCrossSiteTransferInOldProcess(child_id); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 221 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 222 | is_transferring_ = false; |
[email protected] | fc6c187 | 2013-10-03 01:22:35 | [diff] [blame] | 223 | GetRequestInfo()->cross_site_handler()->ResumeResponse(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | ResourceRequestInfoImpl* ResourceLoader::GetRequestInfo() { |
| 227 | return ResourceRequestInfoImpl::ForRequest(request_.get()); |
| 228 | } |
| 229 | |
| 230 | void ResourceLoader::ClearLoginDelegate() { |
| 231 | login_delegate_ = NULL; |
| 232 | } |
| 233 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 234 | void ResourceLoader::OnReceivedRedirect(net::URLRequest* unused, |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 235 | const net::RedirectInfo& redirect_info, |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 236 | bool* defer) { |
| 237 | DCHECK_EQ(request_.get(), unused); |
| 238 | |
mostynb | 0e79091bb | 2015-06-15 16:18:25 | [diff] [blame] | 239 | DVLOG(1) << "OnReceivedRedirect: " << request_->url().spec(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 240 | DCHECK(request_->status().is_success()); |
| 241 | |
| 242 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 243 | |
[email protected] | 2d09a70 | 2014-01-19 23:41:24 | [diff] [blame] | 244 | if (info->GetProcessType() != PROCESS_TYPE_PLUGIN && |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 245 | !ChildProcessSecurityPolicyImpl::GetInstance()-> |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 246 | CanRequestURL(info->GetChildID(), redirect_info.new_url)) { |
mostynb | 0e79091bb | 2015-06-15 16:18:25 | [diff] [blame] | 247 | DVLOG(1) << "Denied unauthorized request for " |
| 248 | << redirect_info.new_url.possibly_invalid_spec(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 249 | |
| 250 | // Tell the renderer that this request was disallowed. |
| 251 | Cancel(); |
| 252 | return; |
| 253 | } |
| 254 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 255 | delegate_->DidReceiveRedirect(this, redirect_info.new_url); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 256 | |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 257 | if (delegate_->HandleExternalProtocol(this, redirect_info.new_url)) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 258 | // The request is complete so we can remove it. |
[email protected] | 2756a8e | 2012-09-07 18:24:29 | [diff] [blame] | 259 | CancelAndIgnore(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 260 | return; |
| 261 | } |
| 262 | |
| 263 | scoped_refptr<ResourceResponse> response(new ResourceResponse()); |
[email protected] | 3821f7a | 2014-08-13 16:40:08 | [diff] [blame] | 264 | PopulateResourceResponse(info, request_.get(), response.get()); |
[email protected] | cba2464 | 2014-08-15 20:49:59 | [diff] [blame] | 265 | if (!handler_->OnRequestRedirected(redirect_info, response.get(), defer)) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 266 | Cancel(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 267 | } else if (*defer) { |
| 268 | deferred_stage_ = DEFERRED_REDIRECT; // Follow redirect when resumed. |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 269 | } |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void ResourceLoader::OnAuthRequired(net::URLRequest* unused, |
| 273 | net::AuthChallengeInfo* auth_info) { |
| 274 | DCHECK_EQ(request_.get(), unused); |
| 275 | |
baranovich | 1b32ffb | 2015-01-15 14:44:52 | [diff] [blame] | 276 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 277 | if (info->do_not_prompt_for_login()) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 278 | request_->CancelAuth(); |
| 279 | return; |
| 280 | } |
| 281 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 282 | // Create a login dialog on the UI thread to get authentication data, or pull |
| 283 | // from cache and continue on the IO thread. |
| 284 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 285 | DCHECK(!login_delegate_.get()) |
| 286 | << "OnAuthRequired called with login_delegate pending"; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 287 | login_delegate_ = delegate_->CreateLoginDelegate(this, auth_info); |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 288 | if (!login_delegate_.get()) |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 289 | request_->CancelAuth(); |
| 290 | } |
| 291 | |
| 292 | void ResourceLoader::OnCertificateRequested( |
| 293 | net::URLRequest* unused, |
| 294 | net::SSLCertRequestInfo* cert_info) { |
| 295 | DCHECK_EQ(request_.get(), unused); |
| 296 | |
[email protected] | 294084d | 2014-01-06 22:22:02 | [diff] [blame] | 297 | if (request_->load_flags() & net::LOAD_PREFETCH) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 298 | request_->Cancel(); |
| 299 | return; |
| 300 | } |
| 301 | |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 302 | DCHECK(!ssl_client_auth_handler_) |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 303 | << "OnCertificateRequested called with ssl_client_auth_handler pending"; |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 304 | ssl_client_auth_handler_.reset(new SSLClientAuthHandler( |
| 305 | GetRequestInfo()->GetContext()->CreateClientCertStore(), request_.get(), |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 306 | cert_info, this)); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 307 | ssl_client_auth_handler_->SelectCertificate(); |
| 308 | } |
| 309 | |
| 310 | void ResourceLoader::OnSSLCertificateError(net::URLRequest* request, |
| 311 | const net::SSLInfo& ssl_info, |
| 312 | bool fatal) { |
| 313 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 314 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 315 | SSLManager::OnSSLCertificateError( |
clamy | 0d32d6d | 2015-11-24 11:16:26 | [diff] [blame] | 316 | weak_ptr_factory_.GetWeakPtr(), info->GetResourceType(), request_->url(), |
clamy | b830cae | 2015-12-02 14:09:30 | [diff] [blame] | 317 | info->GetWebContentsGetterForRequest(), ssl_info, fatal); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 318 | } |
| 319 | |
[email protected] | 5584eab | 2014-01-09 22:16:15 | [diff] [blame] | 320 | void ResourceLoader::OnBeforeNetworkStart(net::URLRequest* unused, |
| 321 | bool* defer) { |
| 322 | DCHECK_EQ(request_.get(), unused); |
| 323 | |
| 324 | // Give the handler a chance to delay the URLRequest from using the network. |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 325 | if (!handler_->OnBeforeNetworkStart(request_->url(), defer)) { |
[email protected] | 5584eab | 2014-01-09 22:16:15 | [diff] [blame] | 326 | Cancel(); |
| 327 | return; |
| 328 | } else if (*defer) { |
| 329 | deferred_stage_ = DEFERRED_NETWORK_START; |
| 330 | } |
| 331 | } |
| 332 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 333 | void ResourceLoader::OnResponseStarted(net::URLRequest* unused) { |
| 334 | DCHECK_EQ(request_.get(), unused); |
| 335 | |
mostynb | 0e79091bb | 2015-06-15 16:18:25 | [diff] [blame] | 336 | DVLOG(1) << "OnResponseStarted: " << request_->url().spec(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 337 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 338 | if (!request_->status().is_success()) { |
| 339 | ResponseCompleted(); |
| 340 | return; |
| 341 | } |
| 342 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 343 | CompleteResponseStarted(); |
| 344 | |
| 345 | if (is_deferred()) |
| 346 | return; |
| 347 | |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 348 | if (request_->status().is_success()) |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 349 | StartReading(false); // Read the first chunk. |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 350 | else |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 351 | ResponseCompleted(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | void ResourceLoader::OnReadCompleted(net::URLRequest* unused, int bytes_read) { |
| 355 | DCHECK_EQ(request_.get(), unused); |
mostynb | 0e79091bb | 2015-06-15 16:18:25 | [diff] [blame] | 356 | DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\"" |
| 357 | << " bytes_read = " << bytes_read; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 358 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 359 | // bytes_read == -1 always implies an error. |
| 360 | if (bytes_read == -1 || !request_->status().is_success()) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 361 | ResponseCompleted(); |
| 362 | return; |
| 363 | } |
| 364 | |
[email protected] | 218d9d37 | 2014-04-16 18:34:55 | [diff] [blame] | 365 | CompleteRead(bytes_read); |
| 366 | |
[email protected] | d1222a6 | 2014-04-14 20:27:32 | [diff] [blame] | 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. |
| 371 | // |
| 372 | // Note: if bytes_read is 0 (EOF) and the handler defers, resumption will call |
[email protected] | 17b674e0 | 2014-05-10 04:30:02 | [diff] [blame] | 373 | // ResponseCompleted(). |
[email protected] | 218d9d37 | 2014-04-16 18:34:55 | [diff] [blame] | 374 | if (is_deferred() || !request_->status().is_success()) |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 375 | return; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 376 | |
[email protected] | d1222a6 | 2014-04-14 20:27:32 | [diff] [blame] | 377 | if (bytes_read > 0) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 378 | StartReading(true); // Read the next chunk. |
| 379 | } else { |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 380 | // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. |
| 381 | tracked_objects::ScopedTracker tracking_profile( |
| 382 | FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 ResponseCompleted()")); |
vadimt | 2df40b37 | 2014-11-27 02:01:41 | [diff] [blame] | 383 | |
[email protected] | d1222a6 | 2014-04-14 20:27:32 | [diff] [blame] | 384 | // URLRequest reported an EOF. Call ResponseCompleted. |
| 385 | DCHECK_EQ(0, bytes_read); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 386 | ResponseCompleted(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 387 | } |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 388 | } |
| 389 | |
davidben | 21163ec | 2014-10-01 23:05:23 | [diff] [blame] | 390 | void ResourceLoader::CancelSSLRequest(int error, |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 391 | const net::SSLInfo* ssl_info) { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 392 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 393 | |
| 394 | // The request can be NULL if it was cancelled by the renderer (as the |
| 395 | // request of the user navigating to a new page from the location bar). |
| 396 | if (!request_->is_pending()) |
| 397 | return; |
| 398 | DVLOG(1) << "CancelSSLRequest() url: " << request_->url().spec(); |
| 399 | |
| 400 | if (ssl_info) { |
| 401 | request_->CancelWithSSLError(error, *ssl_info); |
| 402 | } else { |
| 403 | request_->CancelWithError(error); |
| 404 | } |
| 405 | } |
| 406 | |
davidben | 21163ec | 2014-10-01 23:05:23 | [diff] [blame] | 407 | void ResourceLoader::ContinueSSLRequest() { |
mostynb | 4c27d04 | 2015-03-18 21:47:47 | [diff] [blame] | 408 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 409 | |
| 410 | DVLOG(1) << "ContinueSSLRequest() url: " << request_->url().spec(); |
| 411 | |
| 412 | request_->ContinueDespiteLastError(); |
| 413 | } |
| 414 | |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 415 | void ResourceLoader::ContinueWithCertificate(net::X509Certificate* cert) { |
| 416 | DCHECK(ssl_client_auth_handler_); |
| 417 | ssl_client_auth_handler_.reset(); |
svaldez | 7872fd0 | 2015-11-19 21:10:54 | [diff] [blame] | 418 | if (!cert) { |
| 419 | request_->ContinueWithCertificate(nullptr, nullptr); |
| 420 | return; |
| 421 | } |
| 422 | scoped_refptr<net::SSLPrivateKey> private_key = |
| 423 | net::FetchClientCertPrivateKey(cert); |
| 424 | request_->ContinueWithCertificate(cert, private_key.get()); |
davidben | 3b8455ae7 | 2015-03-11 19:42:19 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | void ResourceLoader::CancelCertificateSelection() { |
| 428 | DCHECK(ssl_client_auth_handler_); |
| 429 | ssl_client_auth_handler_.reset(); |
| 430 | request_->CancelWithError(net::ERR_SSL_CLIENT_AUTH_CERT_NEEDED); |
| 431 | } |
| 432 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 433 | void ResourceLoader::Resume() { |
| 434 | DCHECK(!is_transferring_); |
| 435 | |
| 436 | DeferredStage stage = deferred_stage_; |
| 437 | deferred_stage_ = DEFERRED_NONE; |
| 438 | switch (stage) { |
| 439 | case DEFERRED_NONE: |
| 440 | NOTREACHED(); |
| 441 | break; |
| 442 | case DEFERRED_START: |
| 443 | StartRequestInternal(); |
| 444 | break; |
[email protected] | 5584eab | 2014-01-09 22:16:15 | [diff] [blame] | 445 | case DEFERRED_NETWORK_START: |
| 446 | request_->ResumeNetworkStart(); |
| 447 | break; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 448 | case DEFERRED_REDIRECT: |
| 449 | request_->FollowDeferredRedirect(); |
| 450 | break; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 451 | case DEFERRED_READ: |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 452 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 453 | FROM_HERE, base::Bind(&ResourceLoader::ResumeReading, |
| 454 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 455 | break; |
[email protected] | 17b674e0 | 2014-05-10 04:30:02 | [diff] [blame] | 456 | case DEFERRED_RESPONSE_COMPLETE: |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 457 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 458 | FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted, |
| 459 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 17b674e0 | 2014-05-10 04:30:02 | [diff] [blame] | 460 | break; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 461 | case DEFERRED_FINISH: |
| 462 | // Delay self-destruction since we don't know how we were reached. |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 463 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 464 | FROM_HERE, base::Bind(&ResourceLoader::CallDidFinishLoading, |
| 465 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 466 | break; |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | void ResourceLoader::Cancel() { |
| 471 | CancelRequest(false); |
| 472 | } |
| 473 | |
| 474 | void ResourceLoader::StartRequestInternal() { |
| 475 | DCHECK(!request_->is_pending()); |
[email protected] | 4c409211 | 2014-02-26 23:29:12 | [diff] [blame] | 476 | |
| 477 | if (!request_->status().is_success()) { |
| 478 | return; |
| 479 | } |
| 480 | |
mmenke | e0b2bd62 | 2015-06-24 20:23:10 | [diff] [blame] | 481 | started_request_ = true; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 482 | request_->Start(); |
| 483 | |
| 484 | delegate_->DidStartRequest(this); |
| 485 | } |
| 486 | |
| 487 | void ResourceLoader::CancelRequestInternal(int error, bool from_renderer) { |
mostynb | 0e79091bb | 2015-06-15 16:18:25 | [diff] [blame] | 488 | DVLOG(1) << "CancelRequestInternal: " << request_->url().spec(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 489 | |
| 490 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 491 | |
| 492 | // WebKit will send us a cancel for downloads since it no longer handles |
| 493 | // them. In this case, ignore the cancel since we handle downloads in the |
| 494 | // browser. |
[email protected] | 7098382 | 2013-11-27 14:33:59 | [diff] [blame] | 495 | if (from_renderer && (info->IsDownload() || info->is_stream())) |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 496 | return; |
| 497 | |
[email protected] | 146b8b2 | 2013-11-20 03:59:18 | [diff] [blame] | 498 | if (from_renderer && info->detachable_handler()) { |
| 499 | // TODO(davidben): Fix Blink handling of prefetches so they are not |
| 500 | // cancelled on navigate away and end up in the local cache. |
| 501 | info->detachable_handler()->Detach(); |
| 502 | return; |
| 503 | } |
| 504 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 505 | // TODO(darin): Perhaps we should really be looking to see if the status is |
| 506 | // IO_PENDING? |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 507 | bool was_pending = request_->is_pending(); |
| 508 | |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 509 | if (login_delegate_.get()) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 510 | login_delegate_->OnRequestCancelled(); |
| 511 | login_delegate_ = NULL; |
| 512 | } |
davidben | 6cd57dd | 2014-12-12 19:23:57 | [diff] [blame] | 513 | ssl_client_auth_handler_.reset(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 514 | |
mmenke | e0b2bd62 | 2015-06-24 20:23:10 | [diff] [blame] | 515 | if (!started_request_) { |
| 516 | times_cancelled_before_request_start_++; |
| 517 | } else { |
| 518 | times_cancelled_after_request_start_++; |
| 519 | } |
| 520 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 521 | request_->CancelWithError(error); |
| 522 | |
| 523 | if (!was_pending) { |
| 524 | // If the request isn't in flight, then we won't get an asynchronous |
| 525 | // notification from the request, so we have to signal ourselves to finish |
| 526 | // this request. |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 527 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 528 | FROM_HERE, base::Bind(&ResourceLoader::ResponseCompleted, |
| 529 | weak_ptr_factory_.GetWeakPtr())); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 533 | void ResourceLoader::CompleteResponseStarted() { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 534 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 535 | scoped_refptr<ResourceResponse> response(new ResourceResponse()); |
[email protected] | 3821f7a | 2014-08-13 16:40:08 | [diff] [blame] | 536 | PopulateResourceResponse(info, request_.get(), response.get()); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 537 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 538 | delegate_->DidReceiveResponse(this); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 539 | |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 540 | // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 541 | tracked_objects::ScopedTracker tracking_profile( |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 542 | FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseStarted()")); |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 543 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 544 | bool defer = false; |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 545 | if (!handler_->OnResponseStarted(response.get(), &defer)) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 546 | Cancel(); |
| 547 | } else if (defer) { |
[email protected] | 5e318348 | 2013-08-09 11:05:19 | [diff] [blame] | 548 | read_deferral_start_time_ = base::TimeTicks::Now(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 549 | deferred_stage_ = DEFERRED_READ; // Read first chunk when resumed. |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 550 | } |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 551 | } |
| 552 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 553 | void ResourceLoader::StartReading(bool is_continuation) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 554 | int bytes_read = 0; |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 555 | ReadMore(&bytes_read); |
| 556 | |
| 557 | // If IO is pending, wait for the URLRequest to call OnReadCompleted. |
| 558 | if (request_->status().is_io_pending()) |
| 559 | return; |
| 560 | |
| 561 | if (!is_continuation || bytes_read <= 0) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 562 | OnReadCompleted(request_.get(), bytes_read); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 563 | } else { |
| 564 | // Else, trigger OnReadCompleted asynchronously to avoid starving the IO |
| 565 | // thread in case the URLRequest can provide data synchronously. |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 566 | base::ThreadTaskRunnerHandle::Get()->PostTask( |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 567 | FROM_HERE, |
[email protected] | 5c1d3e5 | 2012-08-01 05:14:27 | [diff] [blame] | 568 | base::Bind(&ResourceLoader::OnReadCompleted, |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 569 | weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read)); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | void ResourceLoader::ResumeReading() { |
| 574 | DCHECK(!is_deferred()); |
| 575 | |
[email protected] | 5e318348 | 2013-08-09 11:05:19 | [diff] [blame] | 576 | if (!read_deferral_start_time_.is_null()) { |
| 577 | UMA_HISTOGRAM_TIMES("Net.ResourceLoader.ReadDeferral", |
| 578 | base::TimeTicks::Now() - read_deferral_start_time_); |
| 579 | read_deferral_start_time_ = base::TimeTicks(); |
| 580 | } |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 581 | if (request_->status().is_success()) { |
| 582 | StartReading(false); // Read the next chunk (OK to complete synchronously). |
| 583 | } else { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 584 | ResponseCompleted(); |
| 585 | } |
| 586 | } |
| 587 | |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 588 | void ResourceLoader::ReadMore(int* bytes_read) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 589 | DCHECK(!is_deferred()); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 590 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 591 | // Make sure we track the buffer in at least one place. This ensures it gets |
| 592 | // deleted even in the case the request has already finished its job and |
| 593 | // doesn't use the buffer. |
| 594 | scoped_refptr<net::IOBuffer> buf; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 595 | int buf_size; |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 596 | { |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 597 | // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. |
vadimt | 394300b0 | 2014-12-02 04:38:41 | [diff] [blame] | 598 | tracked_objects::ScopedTracker tracking_profile2( |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 599 | FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnWillRead()")); |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 600 | |
| 601 | if (!handler_->OnWillRead(&buf, &buf_size, -1)) { |
| 602 | Cancel(); |
| 603 | return; |
| 604 | } |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 605 | } |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 606 | |
dcheng | 67769df5 | 2014-08-26 19:43:51 | [diff] [blame] | 607 | DCHECK(buf.get()); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 608 | DCHECK(buf_size > 0); |
| 609 | |
[email protected] | ef5306e | 2013-10-15 19:38:18 | [diff] [blame] | 610 | request_->Read(buf.get(), buf_size, bytes_read); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 611 | |
| 612 | // No need to check the return value here as we'll detect errors by |
| 613 | // inspecting the URLRequest's status. |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 614 | } |
| 615 | |
[email protected] | 218d9d37 | 2014-04-16 18:34:55 | [diff] [blame] | 616 | void ResourceLoader::CompleteRead(int bytes_read) { |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 617 | DCHECK(bytes_read >= 0); |
| 618 | DCHECK(request_->status().is_success()); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 619 | |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 620 | // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 621 | tracked_objects::ScopedTracker tracking_profile( |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 622 | FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnReadCompleted()")); |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 623 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 624 | bool defer = false; |
[email protected] | d22e800e | 2014-05-28 21:55:57 | [diff] [blame] | 625 | if (!handler_->OnReadCompleted(bytes_read, &defer)) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 626 | Cancel(); |
[email protected] | 926360f | 2012-06-29 04:45:02 | [diff] [blame] | 627 | } else if (defer) { |
[email protected] | 17b674e0 | 2014-05-10 04:30:02 | [diff] [blame] | 628 | deferred_stage_ = |
| 629 | bytes_read > 0 ? DEFERRED_READ : DEFERRED_RESPONSE_COMPLETE; |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 630 | } |
[email protected] | 218d9d37 | 2014-04-16 18:34:55 | [diff] [blame] | 631 | |
| 632 | // Note: the request may still have been cancelled while OnReadCompleted |
| 633 | // returns true if OnReadCompleted caused request to get cancelled |
| 634 | // out-of-band. (In AwResourceDispatcherHostDelegate::DownloadStarting, for |
| 635 | // instance.) |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void ResourceLoader::ResponseCompleted() { |
mostynb | 0e79091bb | 2015-06-15 16:18:25 | [diff] [blame] | 639 | DVLOG(1) << "ResponseCompleted: " << request_->url().spec(); |
[email protected] | 3ed8472 | 2013-11-01 17:17:07 | [diff] [blame] | 640 | RecordHistograms(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 641 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 642 | |
| 643 | std::string security_info; |
| 644 | const net::SSLInfo& ssl_info = request_->ssl_info(); |
[email protected] | fc72bb1 | 2013-06-02 21:13:46 | [diff] [blame] | 645 | if (ssl_info.cert.get() != NULL) { |
estark | 294fbd9 | 2015-07-31 18:36:24 | [diff] [blame] | 646 | SSLStatus ssl_status; |
| 647 | GetSSLStatusForRequest(request_->url(), ssl_info, info->GetChildID(), |
| 648 | &ssl_status); |
[email protected] | 0bbd63b | 2013-11-29 00:02:12 | [diff] [blame] | 649 | |
estark | 294fbd9 | 2015-07-31 18:36:24 | [diff] [blame] | 650 | security_info = SerializeSecurityInfo(ssl_status); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 651 | } |
| 652 | |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 653 | bool defer = false; |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 654 | { |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 655 | // TODO(darin): Remove ScopedTracker below once crbug.com/475761 is fixed. |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 656 | tracked_objects::ScopedTracker tracking_profile( |
pkasting | 941842c | 2015-04-11 01:51:30 | [diff] [blame] | 657 | FROM_HERE_WITH_EXPLICIT_FUNCTION("475761 OnResponseCompleted()")); |
vadimt | 81ef0df | 2014-11-18 19:42:31 | [diff] [blame] | 658 | |
| 659 | handler_->OnResponseCompleted(request_->status(), security_info, &defer); |
| 660 | } |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 661 | if (defer) { |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 662 | // The handler is not ready to die yet. We will call DidFinishLoading when |
| 663 | // we resume. |
| 664 | deferred_stage_ = DEFERRED_FINISH; |
[email protected] | 3780874a | 2013-11-18 05:49:03 | [diff] [blame] | 665 | } else { |
| 666 | // This will result in our destruction. |
| 667 | CallDidFinishLoading(); |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 668 | } |
| 669 | } |
| 670 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 671 | void ResourceLoader::CallDidFinishLoading() { |
| 672 | delegate_->DidFinishLoading(this); |
| 673 | } |
| 674 | |
[email protected] | 3ed8472 | 2013-11-01 17:17:07 | [diff] [blame] | 675 | void ResourceLoader::RecordHistograms() { |
bnc | 3d8d8d3 | 2015-10-28 15:45:56 | [diff] [blame] | 676 | if (request_->response_info().network_accessed) { |
| 677 | UMA_HISTOGRAM_ENUMERATION("Net.HttpResponseInfo.ConnectionInfo", |
| 678 | request_->response_info().connection_info, |
| 679 | net::HttpResponseInfo::NUM_OF_CONNECTION_INFOS); |
| 680 | } |
| 681 | |
[email protected] | 3ed8472 | 2013-11-01 17:17:07 | [diff] [blame] | 682 | ResourceRequestInfoImpl* info = GetRequestInfo(); |
| 683 | |
[email protected] | 6c1e0521 | 2014-07-31 00:59:40 | [diff] [blame] | 684 | if (info->GetResourceType() == RESOURCE_TYPE_PREFETCH) { |
[email protected] | 3ed8472 | 2013-11-01 17:17:07 | [diff] [blame] | 685 | PrefetchStatus status = STATUS_UNDEFINED; |
| 686 | TimeDelta total_time = base::TimeTicks::Now() - request_->creation_time(); |
| 687 | |
| 688 | switch (request_->status().status()) { |
| 689 | case net::URLRequestStatus::SUCCESS: |
| 690 | if (request_->was_cached()) { |
| 691 | status = STATUS_SUCCESS_FROM_CACHE; |
| 692 | UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentPrefetchingFromCache", |
| 693 | total_time); |
| 694 | } else { |
| 695 | status = STATUS_SUCCESS_FROM_NETWORK; |
| 696 | UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeSpentPrefetchingFromNetwork", |
| 697 | total_time); |
| 698 | } |
| 699 | break; |
| 700 | case net::URLRequestStatus::CANCELED: |
| 701 | status = STATUS_CANCELED; |
| 702 | UMA_HISTOGRAM_TIMES("Net.Prefetch.TimeBeforeCancel", total_time); |
| 703 | break; |
| 704 | case net::URLRequestStatus::IO_PENDING: |
| 705 | case net::URLRequestStatus::FAILED: |
| 706 | status = STATUS_UNDEFINED; |
| 707 | break; |
| 708 | } |
| 709 | |
| 710 | UMA_HISTOGRAM_ENUMERATION("Net.Prefetch.Pattern", status, STATUS_MAX); |
| 711 | } |
| 712 | } |
| 713 | |
[email protected] | 56f0b08 | 2012-06-14 07:12:32 | [diff] [blame] | 714 | } // namespace content |