| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #include "net/url_request/url_request.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/callback.h" | |
| 10 #include "base/compiler_specific.h" | |
| 11 #include "base/debug/stack_trace.h" | |
| 12 #include "base/lazy_instance.h" | |
| 13 #include "base/memory/singleton.h" | |
| 14 #include "base/message_loop/message_loop.h" | |
| 15 #include "base/profiler/scoped_tracker.h" | |
| 16 #include "base/stl_util.h" | |
| 17 #include "base/strings/utf_string_conversions.h" | |
| 18 #include "base/synchronization/lock.h" | |
| 19 #include "base/values.h" | |
| 20 #include "net/base/auth.h" | |
| 21 #include "net/base/chunked_upload_data_stream.h" | |
| 22 #include "net/base/host_port_pair.h" | |
| 23 #include "net/base/load_flags.h" | |
| 24 #include "net/base/load_timing_info.h" | |
| 25 #include "net/base/net_errors.h" | |
| 26 #include "net/base/net_log.h" | |
| 27 #include "net/base/network_change_notifier.h" | |
| 28 #include "net/base/network_delegate.h" | |
| 29 #include "net/base/upload_data_stream.h" | |
| 30 #include "net/http/http_response_headers.h" | |
| 31 #include "net/http/http_util.h" | |
| 32 #include "net/ssl/ssl_cert_request_info.h" | |
| 33 #include "net/url_request/redirect_info.h" | |
| 34 #include "net/url_request/url_request_context.h" | |
| 35 #include "net/url_request/url_request_error_job.h" | |
| 36 #include "net/url_request/url_request_job.h" | |
| 37 #include "net/url_request/url_request_job_manager.h" | |
| 38 #include "net/url_request/url_request_netlog_params.h" | |
| 39 #include "net/url_request/url_request_redirect_job.h" | |
| 40 | |
| 41 using base::Time; | |
| 42 using std::string; | |
| 43 | |
| 44 namespace net { | |
| 45 | |
| 46 namespace { | |
| 47 | |
| 48 // Max number of http redirects to follow. Same number as gecko. | |
| 49 const int kMaxRedirects = 20; | |
| 50 | |
| 51 // Discard headers which have meaning in POST (Content-Length, Content-Type, | |
| 52 // Origin). | |
| 53 void StripPostSpecificHeaders(HttpRequestHeaders* headers) { | |
| 54 // These are headers that may be attached to a POST. | |
| 55 headers->RemoveHeader(HttpRequestHeaders::kContentLength); | |
| 56 headers->RemoveHeader(HttpRequestHeaders::kContentType); | |
| 57 headers->RemoveHeader(HttpRequestHeaders::kOrigin); | |
| 58 } | |
| 59 | |
| 60 // TODO(battre): Delete this, see http://crbug.com/89321: | |
| 61 // This counter keeps track of the identifiers used for URL requests so far. | |
| 62 // 0 is reserved to represent an invalid ID. | |
| 63 uint64 g_next_url_request_identifier = 1; | |
| 64 | |
| 65 // This lock protects g_next_url_request_identifier. | |
| 66 base::LazyInstance<base::Lock>::Leaky | |
| 67 g_next_url_request_identifier_lock = LAZY_INSTANCE_INITIALIZER; | |
| 68 | |
| 69 // Returns an prior unused identifier for URL requests. | |
| 70 uint64 GenerateURLRequestIdentifier() { | |
| 71 base::AutoLock lock(g_next_url_request_identifier_lock.Get()); | |
| 72 return g_next_url_request_identifier++; | |
| 73 } | |
| 74 | |
| 75 // True once the first URLRequest was started. | |
| 76 bool g_url_requests_started = false; | |
| 77 | |
| 78 // True if cookies are accepted by default. | |
| 79 bool g_default_can_use_cookies = true; | |
| 80 | |
| 81 // When the URLRequest first assempts load timing information, it has the times | |
| 82 // at which each event occurred. The API requires the time which the request | |
| 83 // was blocked on each phase. This function handles the conversion. | |
| 84 // | |
| 85 // In the case of reusing a SPDY session, old proxy results may have been | |
| 86 // reused, so proxy resolution times may be before the request was started. | |
| 87 // | |
| 88 // Due to preconnect and late binding, it is also possible for the connection | |
| 89 // attempt to start before a request has been started, or proxy resolution | |
| 90 // completed. | |
| 91 // | |
| 92 // This functions fixes both those cases. | |
| 93 void ConvertRealLoadTimesToBlockingTimes( | |
| 94 net::LoadTimingInfo* load_timing_info) { | |
| 95 DCHECK(!load_timing_info->request_start.is_null()); | |
| 96 | |
| 97 // Earliest time possible for the request to be blocking on connect events. | |
| 98 base::TimeTicks block_on_connect = load_timing_info->request_start; | |
| 99 | |
| 100 if (!load_timing_info->proxy_resolve_start.is_null()) { | |
| 101 DCHECK(!load_timing_info->proxy_resolve_end.is_null()); | |
| 102 | |
| 103 // Make sure the proxy times are after request start. | |
| 104 if (load_timing_info->proxy_resolve_start < load_timing_info->request_start) | |
| 105 load_timing_info->proxy_resolve_start = load_timing_info->request_start; | |
| 106 if (load_timing_info->proxy_resolve_end < load_timing_info->request_start) | |
| 107 load_timing_info->proxy_resolve_end = load_timing_info->request_start; | |
| 108 | |
| 109 // Connect times must also be after the proxy times. | |
| 110 block_on_connect = load_timing_info->proxy_resolve_end; | |
| 111 } | |
| 112 | |
| 113 // Make sure connection times are after start and proxy times. | |
| 114 | |
| 115 net::LoadTimingInfo::ConnectTiming* connect_timing = | |
| 116 &load_timing_info->connect_timing; | |
| 117 if (!connect_timing->dns_start.is_null()) { | |
| 118 DCHECK(!connect_timing->dns_end.is_null()); | |
| 119 if (connect_timing->dns_start < block_on_connect) | |
| 120 connect_timing->dns_start = block_on_connect; | |
| 121 if (connect_timing->dns_end < block_on_connect) | |
| 122 connect_timing->dns_end = block_on_connect; | |
| 123 } | |
| 124 | |
| 125 if (!connect_timing->connect_start.is_null()) { | |
| 126 DCHECK(!connect_timing->connect_end.is_null()); | |
| 127 if (connect_timing->connect_start < block_on_connect) | |
| 128 connect_timing->connect_start = block_on_connect; | |
| 129 if (connect_timing->connect_end < block_on_connect) | |
| 130 connect_timing->connect_end = block_on_connect; | |
| 131 } | |
| 132 | |
| 133 if (!connect_timing->ssl_start.is_null()) { | |
| 134 DCHECK(!connect_timing->ssl_end.is_null()); | |
| 135 if (connect_timing->ssl_start < block_on_connect) | |
| 136 connect_timing->ssl_start = block_on_connect; | |
| 137 if (connect_timing->ssl_end < block_on_connect) | |
| 138 connect_timing->ssl_end = block_on_connect; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 } // namespace | |
| 143 | |
| 144 /////////////////////////////////////////////////////////////////////////////// | |
| 145 // URLRequest::Delegate | |
| 146 | |
| 147 void URLRequest::Delegate::OnReceivedRedirect(URLRequest* request, | |
| 148 const RedirectInfo& redirect_info, | |
| 149 bool* defer_redirect) { | |
| 150 } | |
| 151 | |
| 152 void URLRequest::Delegate::OnAuthRequired(URLRequest* request, | |
| 153 AuthChallengeInfo* auth_info) { | |
| 154 request->CancelAuth(); | |
| 155 } | |
| 156 | |
| 157 void URLRequest::Delegate::OnCertificateRequested( | |
| 158 URLRequest* request, | |
| 159 SSLCertRequestInfo* cert_request_info) { | |
| 160 request->Cancel(); | |
| 161 } | |
| 162 | |
| 163 void URLRequest::Delegate::OnSSLCertificateError(URLRequest* request, | |
| 164 const SSLInfo& ssl_info, | |
| 165 bool is_hsts_ok) { | |
| 166 request->Cancel(); | |
| 167 } | |
| 168 | |
| 169 void URLRequest::Delegate::OnBeforeNetworkStart(URLRequest* request, | |
| 170 bool* defer) { | |
| 171 } | |
| 172 | |
| 173 /////////////////////////////////////////////////////////////////////////////// | |
| 174 // URLRequest | |
| 175 | |
| 176 URLRequest::~URLRequest() { | |
| 177 Cancel(); | |
| 178 | |
| 179 if (network_delegate_) { | |
| 180 network_delegate_->NotifyURLRequestDestroyed(this); | |
| 181 if (job_.get()) | |
| 182 job_->NotifyURLRequestDestroyed(); | |
| 183 } | |
| 184 | |
| 185 if (job_.get()) | |
| 186 OrphanJob(); | |
| 187 | |
| 188 int deleted = context_->url_requests()->erase(this); | |
| 189 CHECK_EQ(1, deleted); | |
| 190 | |
| 191 int net_error = OK; | |
| 192 // Log error only on failure, not cancellation, as even successful requests | |
| 193 // are "cancelled" on destruction. | |
| 194 if (status_.status() == URLRequestStatus::FAILED) | |
| 195 net_error = status_.error(); | |
| 196 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_REQUEST_ALIVE, net_error); | |
| 197 } | |
| 198 | |
| 199 void URLRequest::EnableChunkedUpload() { | |
| 200 DCHECK(!upload_data_stream_ || upload_data_stream_->is_chunked()); | |
| 201 if (!upload_data_stream_) { | |
| 202 upload_chunked_data_stream_ = new ChunkedUploadDataStream(0); | |
| 203 upload_data_stream_.reset(upload_chunked_data_stream_); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 void URLRequest::AppendChunkToUpload(const char* bytes, | |
| 208 int bytes_len, | |
| 209 bool is_last_chunk) { | |
| 210 DCHECK(upload_data_stream_); | |
| 211 DCHECK(upload_data_stream_->is_chunked()); | |
| 212 DCHECK_GT(bytes_len, 0); | |
| 213 upload_chunked_data_stream_->AppendData(bytes, bytes_len, is_last_chunk); | |
| 214 } | |
| 215 | |
| 216 void URLRequest::set_upload(scoped_ptr<UploadDataStream> upload) { | |
| 217 DCHECK(!upload->is_chunked()); | |
| 218 upload_data_stream_ = upload.Pass(); | |
| 219 } | |
| 220 | |
| 221 const UploadDataStream* URLRequest::get_upload() const { | |
| 222 return upload_data_stream_.get(); | |
| 223 } | |
| 224 | |
| 225 bool URLRequest::has_upload() const { | |
| 226 return upload_data_stream_.get() != NULL; | |
| 227 } | |
| 228 | |
| 229 void URLRequest::SetExtraRequestHeaderByName(const string& name, | |
| 230 const string& value, | |
| 231 bool overwrite) { | |
| 232 DCHECK(!is_pending_ || is_redirecting_); | |
| 233 if (overwrite) { | |
| 234 extra_request_headers_.SetHeader(name, value); | |
| 235 } else { | |
| 236 extra_request_headers_.SetHeaderIfMissing(name, value); | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 void URLRequest::RemoveRequestHeaderByName(const string& name) { | |
| 241 DCHECK(!is_pending_ || is_redirecting_); | |
| 242 extra_request_headers_.RemoveHeader(name); | |
| 243 } | |
| 244 | |
| 245 void URLRequest::SetExtraRequestHeaders( | |
| 246 const HttpRequestHeaders& headers) { | |
| 247 DCHECK(!is_pending_); | |
| 248 extra_request_headers_ = headers; | |
| 249 | |
| 250 // NOTE: This method will likely become non-trivial once the other setters | |
| 251 // for request headers are implemented. | |
| 252 } | |
| 253 | |
| 254 bool URLRequest::GetFullRequestHeaders(HttpRequestHeaders* headers) const { | |
| 255 if (!job_.get()) | |
| 256 return false; | |
| 257 | |
| 258 return job_->GetFullRequestHeaders(headers); | |
| 259 } | |
| 260 | |
| 261 int64 URLRequest::GetTotalReceivedBytes() const { | |
| 262 if (!job_.get()) | |
| 263 return 0; | |
| 264 | |
| 265 return job_->GetTotalReceivedBytes(); | |
| 266 } | |
| 267 | |
| 268 LoadStateWithParam URLRequest::GetLoadState() const { | |
| 269 // TODO(pkasting): Remove ScopedTracker below once crbug.com/455952 is | |
| 270 // fixed. | |
| 271 tracked_objects::ScopedTracker tracking_profile( | |
| 272 FROM_HERE_WITH_EXPLICIT_FUNCTION("455952 URLRequest::GetLoadState")); | |
| 273 // The !blocked_by_.empty() check allows |this| to report it's blocked on a | |
| 274 // delegate before it has been started. | |
| 275 if (calling_delegate_ || !blocked_by_.empty()) { | |
| 276 return LoadStateWithParam( | |
| 277 LOAD_STATE_WAITING_FOR_DELEGATE, | |
| 278 use_blocked_by_as_load_param_ ? base::UTF8ToUTF16(blocked_by_) : | |
| 279 base::string16()); | |
| 280 } | |
| 281 return LoadStateWithParam(job_.get() ? job_->GetLoadState() : LOAD_STATE_IDLE, | |
| 282 base::string16()); | |
| 283 } | |
| 284 | |
| 285 base::Value* URLRequest::GetStateAsValue() const { | |
| 286 base::DictionaryValue* dict = new base::DictionaryValue(); | |
| 287 dict->SetString("url", original_url().possibly_invalid_spec()); | |
| 288 | |
| 289 if (url_chain_.size() > 1) { | |
| 290 base::ListValue* list = new base::ListValue(); | |
| 291 for (std::vector<GURL>::const_iterator url = url_chain_.begin(); | |
| 292 url != url_chain_.end(); ++url) { | |
| 293 list->AppendString(url->possibly_invalid_spec()); | |
| 294 } | |
| 295 dict->Set("url_chain", list); | |
| 296 } | |
| 297 | |
| 298 dict->SetInteger("load_flags", load_flags_); | |
| 299 | |
| 300 LoadStateWithParam load_state = GetLoadState(); | |
| 301 dict->SetInteger("load_state", load_state.state); | |
| 302 if (!load_state.param.empty()) | |
| 303 dict->SetString("load_state_param", load_state.param); | |
| 304 if (!blocked_by_.empty()) | |
| 305 dict->SetString("delegate_info", blocked_by_); | |
| 306 | |
| 307 dict->SetString("method", method_); | |
| 308 dict->SetBoolean("has_upload", has_upload()); | |
| 309 dict->SetBoolean("is_pending", is_pending_); | |
| 310 | |
| 311 // Add the status of the request. The status should always be IO_PENDING, and | |
| 312 // the error should always be OK, unless something is holding onto a request | |
| 313 // that has finished or a request was leaked. Neither of these should happen. | |
| 314 switch (status_.status()) { | |
| 315 case URLRequestStatus::SUCCESS: | |
| 316 dict->SetString("status", "SUCCESS"); | |
| 317 break; | |
| 318 case URLRequestStatus::IO_PENDING: | |
| 319 dict->SetString("status", "IO_PENDING"); | |
| 320 break; | |
| 321 case URLRequestStatus::CANCELED: | |
| 322 dict->SetString("status", "CANCELED"); | |
| 323 break; | |
| 324 case URLRequestStatus::FAILED: | |
| 325 dict->SetString("status", "FAILED"); | |
| 326 break; | |
| 327 } | |
| 328 if (status_.error() != OK) | |
| 329 dict->SetInteger("net_error", status_.error()); | |
| 330 return dict; | |
| 331 } | |
| 332 | |
| 333 void URLRequest::LogBlockedBy(const char* blocked_by) { | |
| 334 DCHECK(blocked_by); | |
| 335 DCHECK_GT(strlen(blocked_by), 0u); | |
| 336 | |
| 337 // Only log information to NetLog during startup and certain deferring calls | |
| 338 // to delegates. For all reads but the first, do nothing. | |
| 339 if (!calling_delegate_ && !response_info_.request_time.is_null()) | |
| 340 return; | |
| 341 | |
| 342 LogUnblocked(); | |
| 343 blocked_by_ = blocked_by; | |
| 344 use_blocked_by_as_load_param_ = false; | |
| 345 | |
| 346 net_log_.BeginEvent( | |
| 347 NetLog::TYPE_DELEGATE_INFO, | |
| 348 NetLog::StringCallback("delegate_info", &blocked_by_)); | |
| 349 } | |
| 350 | |
| 351 void URLRequest::LogAndReportBlockedBy(const char* source) { | |
| 352 LogBlockedBy(source); | |
| 353 use_blocked_by_as_load_param_ = true; | |
| 354 } | |
| 355 | |
| 356 void URLRequest::LogUnblocked() { | |
| 357 if (blocked_by_.empty()) | |
| 358 return; | |
| 359 | |
| 360 net_log_.EndEvent(NetLog::TYPE_DELEGATE_INFO); | |
| 361 blocked_by_.clear(); | |
| 362 } | |
| 363 | |
| 364 UploadProgress URLRequest::GetUploadProgress() const { | |
| 365 if (!job_.get()) { | |
| 366 // We haven't started or the request was cancelled | |
| 367 return UploadProgress(); | |
| 368 } | |
| 369 if (final_upload_progress_.position()) { | |
| 370 // The first job completed and none of the subsequent series of | |
| 371 // GETs when following redirects will upload anything, so we return the | |
| 372 // cached results from the initial job, the POST. | |
| 373 return final_upload_progress_; | |
| 374 } | |
| 375 return job_->GetUploadProgress(); | |
| 376 } | |
| 377 | |
| 378 void URLRequest::GetResponseHeaderByName(const string& name, string* value) { | |
| 379 DCHECK(value); | |
| 380 if (response_info_.headers.get()) { | |
| 381 response_info_.headers->GetNormalizedHeader(name, value); | |
| 382 } else { | |
| 383 value->clear(); | |
| 384 } | |
| 385 } | |
| 386 | |
| 387 HostPortPair URLRequest::GetSocketAddress() const { | |
| 388 DCHECK(job_.get()); | |
| 389 return job_->GetSocketAddress(); | |
| 390 } | |
| 391 | |
| 392 HttpResponseHeaders* URLRequest::response_headers() const { | |
| 393 return response_info_.headers.get(); | |
| 394 } | |
| 395 | |
| 396 void URLRequest::GetLoadTimingInfo(LoadTimingInfo* load_timing_info) const { | |
| 397 *load_timing_info = load_timing_info_; | |
| 398 } | |
| 399 | |
| 400 bool URLRequest::GetResponseCookies(ResponseCookies* cookies) { | |
| 401 DCHECK(job_.get()); | |
| 402 return job_->GetResponseCookies(cookies); | |
| 403 } | |
| 404 | |
| 405 void URLRequest::GetMimeType(string* mime_type) const { | |
| 406 DCHECK(job_.get()); | |
| 407 job_->GetMimeType(mime_type); | |
| 408 } | |
| 409 | |
| 410 void URLRequest::GetCharset(string* charset) const { | |
| 411 DCHECK(job_.get()); | |
| 412 job_->GetCharset(charset); | |
| 413 } | |
| 414 | |
| 415 int URLRequest::GetResponseCode() const { | |
| 416 DCHECK(job_.get()); | |
| 417 return job_->GetResponseCode(); | |
| 418 } | |
| 419 | |
| 420 void URLRequest::SetLoadFlags(int flags) { | |
| 421 if ((load_flags_ & LOAD_IGNORE_LIMITS) != (flags & LOAD_IGNORE_LIMITS)) { | |
| 422 DCHECK(!job_.get()); | |
| 423 DCHECK(flags & LOAD_IGNORE_LIMITS); | |
| 424 DCHECK_EQ(priority_, MAXIMUM_PRIORITY); | |
| 425 } | |
| 426 load_flags_ = flags; | |
| 427 | |
| 428 // This should be a no-op given the above DCHECKs, but do this | |
| 429 // anyway for release mode. | |
| 430 if ((load_flags_ & LOAD_IGNORE_LIMITS) != 0) | |
| 431 SetPriority(MAXIMUM_PRIORITY); | |
| 432 } | |
| 433 | |
| 434 // static | |
| 435 void URLRequest::SetDefaultCookiePolicyToBlock() { | |
| 436 CHECK(!g_url_requests_started); | |
| 437 g_default_can_use_cookies = false; | |
| 438 } | |
| 439 | |
| 440 // static | |
| 441 bool URLRequest::IsHandledProtocol(const std::string& scheme) { | |
| 442 return URLRequestJobManager::SupportsScheme(scheme); | |
| 443 } | |
| 444 | |
| 445 // static | |
| 446 bool URLRequest::IsHandledURL(const GURL& url) { | |
| 447 if (!url.is_valid()) { | |
| 448 // We handle error cases. | |
| 449 return true; | |
| 450 } | |
| 451 | |
| 452 return IsHandledProtocol(url.scheme()); | |
| 453 } | |
| 454 | |
| 455 void URLRequest::set_first_party_for_cookies( | |
| 456 const GURL& first_party_for_cookies) { | |
| 457 DCHECK(!is_pending_); | |
| 458 first_party_for_cookies_ = first_party_for_cookies; | |
| 459 } | |
| 460 | |
| 461 void URLRequest::set_first_party_url_policy( | |
| 462 FirstPartyURLPolicy first_party_url_policy) { | |
| 463 DCHECK(!is_pending_); | |
| 464 first_party_url_policy_ = first_party_url_policy; | |
| 465 } | |
| 466 | |
| 467 void URLRequest::set_method(const std::string& method) { | |
| 468 DCHECK(!is_pending_); | |
| 469 method_ = method; | |
| 470 } | |
| 471 | |
| 472 void URLRequest::SetReferrer(const std::string& referrer) { | |
| 473 DCHECK(!is_pending_); | |
| 474 GURL referrer_url(referrer); | |
| 475 if (referrer_url.is_valid()) { | |
| 476 referrer_ = referrer_url.GetAsReferrer().spec(); | |
| 477 } else { | |
| 478 referrer_ = referrer; | |
| 479 } | |
| 480 } | |
| 481 | |
| 482 void URLRequest::set_referrer_policy(ReferrerPolicy referrer_policy) { | |
| 483 DCHECK(!is_pending_); | |
| 484 referrer_policy_ = referrer_policy; | |
| 485 } | |
| 486 | |
| 487 void URLRequest::set_delegate(Delegate* delegate) { | |
| 488 delegate_ = delegate; | |
| 489 } | |
| 490 | |
| 491 void URLRequest::Start() { | |
| 492 // Some values can be NULL, but the job factory must not be. | |
| 493 DCHECK(context_->job_factory()); | |
| 494 | |
| 495 // Anything that sets |blocked_by_| before start should have cleaned up after | |
| 496 // itself. | |
| 497 DCHECK(blocked_by_.empty()); | |
| 498 | |
| 499 g_url_requests_started = true; | |
| 500 response_info_.request_time = base::Time::Now(); | |
| 501 | |
| 502 load_timing_info_ = LoadTimingInfo(); | |
| 503 load_timing_info_.request_start_time = response_info_.request_time; | |
| 504 load_timing_info_.request_start = base::TimeTicks::Now(); | |
| 505 | |
| 506 // Only notify the delegate for the initial request. | |
| 507 if (network_delegate_) { | |
| 508 OnCallToDelegate(); | |
| 509 int error = network_delegate_->NotifyBeforeURLRequest( | |
| 510 this, before_request_callback_, &delegate_redirect_url_); | |
| 511 // If ERR_IO_PENDING is returned, the delegate will invoke | |
| 512 // |before_request_callback_| later. | |
| 513 if (error != ERR_IO_PENDING) | |
| 514 BeforeRequestComplete(error); | |
| 515 return; | |
| 516 } | |
| 517 | |
| 518 StartJob(URLRequestJobManager::GetInstance()->CreateJob( | |
| 519 this, network_delegate_)); | |
| 520 } | |
| 521 | |
| 522 /////////////////////////////////////////////////////////////////////////////// | |
| 523 | |
| 524 URLRequest::URLRequest(const GURL& url, | |
| 525 RequestPriority priority, | |
| 526 Delegate* delegate, | |
| 527 const URLRequestContext* context, | |
| 528 CookieStore* cookie_store, | |
| 529 NetworkDelegate* network_delegate) | |
| 530 : context_(context), | |
| 531 network_delegate_(network_delegate ? network_delegate | |
| 532 : context->network_delegate()), | |
| 533 net_log_(BoundNetLog::Make(context->net_log(), | |
| 534 NetLog::SOURCE_URL_REQUEST)), | |
| 535 url_chain_(1, url), | |
| 536 method_("GET"), | |
| 537 referrer_policy_(CLEAR_REFERRER_ON_TRANSITION_FROM_SECURE_TO_INSECURE), | |
| 538 first_party_url_policy_(NEVER_CHANGE_FIRST_PARTY_URL), | |
| 539 load_flags_(LOAD_NORMAL), | |
| 540 delegate_(delegate), | |
| 541 is_pending_(false), | |
| 542 is_redirecting_(false), | |
| 543 redirect_limit_(kMaxRedirects), | |
| 544 priority_(priority), | |
| 545 identifier_(GenerateURLRequestIdentifier()), | |
| 546 calling_delegate_(false), | |
| 547 use_blocked_by_as_load_param_(false), | |
| 548 before_request_callback_(base::Bind(&URLRequest::BeforeRequestComplete, | |
| 549 base::Unretained(this))), | |
| 550 has_notified_completion_(false), | |
| 551 received_response_content_length_(0), | |
| 552 creation_time_(base::TimeTicks::Now()), | |
| 553 notified_before_network_start_(false), | |
| 554 cookie_store_(cookie_store ? cookie_store : context->cookie_store()) { | |
| 555 // Sanity check out environment. | |
| 556 DCHECK(base::MessageLoop::current()) | |
| 557 << "The current base::MessageLoop must exist"; | |
| 558 | |
| 559 context->url_requests()->insert(this); | |
| 560 net_log_.BeginEvent(NetLog::TYPE_REQUEST_ALIVE); | |
| 561 } | |
| 562 | |
| 563 void URLRequest::BeforeRequestComplete(int error) { | |
| 564 DCHECK(!job_.get()); | |
| 565 DCHECK_NE(ERR_IO_PENDING, error); | |
| 566 | |
| 567 // Check that there are no callbacks to already canceled requests. | |
| 568 DCHECK_NE(URLRequestStatus::CANCELED, status_.status()); | |
| 569 | |
| 570 OnCallToDelegateComplete(); | |
| 571 | |
| 572 if (error != OK) { | |
| 573 std::string source("delegate"); | |
| 574 net_log_.AddEvent(NetLog::TYPE_CANCELLED, | |
| 575 NetLog::StringCallback("source", &source)); | |
| 576 StartJob(new URLRequestErrorJob(this, network_delegate_, error)); | |
| 577 } else if (!delegate_redirect_url_.is_empty()) { | |
| 578 GURL new_url; | |
| 579 new_url.Swap(&delegate_redirect_url_); | |
| 580 | |
| 581 URLRequestRedirectJob* job = new URLRequestRedirectJob( | |
| 582 this, network_delegate_, new_url, | |
| 583 // Use status code 307 to preserve the method, so POST requests work. | |
| 584 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT, "Delegate"); | |
| 585 StartJob(job); | |
| 586 } else { | |
| 587 StartJob(URLRequestJobManager::GetInstance()->CreateJob( | |
| 588 this, network_delegate_)); | |
| 589 } | |
| 590 } | |
| 591 | |
| 592 void URLRequest::StartJob(URLRequestJob* job) { | |
| 593 DCHECK(!is_pending_); | |
| 594 DCHECK(!job_.get()); | |
| 595 | |
| 596 net_log_.BeginEvent( | |
| 597 NetLog::TYPE_URL_REQUEST_START_JOB, | |
| 598 base::Bind(&NetLogURLRequestStartCallback, | |
| 599 &url(), &method_, load_flags_, priority_, | |
| 600 upload_data_stream_ ? upload_data_stream_->identifier() : -1)); | |
| 601 | |
| 602 job_ = job; | |
| 603 job_->SetExtraRequestHeaders(extra_request_headers_); | |
| 604 job_->SetPriority(priority_); | |
| 605 | |
| 606 if (upload_data_stream_.get()) | |
| 607 job_->SetUpload(upload_data_stream_.get()); | |
| 608 | |
| 609 is_pending_ = true; | |
| 610 is_redirecting_ = false; | |
| 611 | |
| 612 response_info_.was_cached = false; | |
| 613 | |
| 614 if (GURL(referrer_) != URLRequestJob::ComputeReferrerForRedirect( | |
| 615 referrer_policy_, referrer_, url())) { | |
| 616 if (!network_delegate_ || | |
| 617 !network_delegate_->CancelURLRequestWithPolicyViolatingReferrerHeader( | |
| 618 *this, url(), GURL(referrer_))) { | |
| 619 referrer_.clear(); | |
| 620 } else { | |
| 621 // We need to clear the referrer anyway to avoid an infinite recursion | |
| 622 // when starting the error job. | |
| 623 referrer_.clear(); | |
| 624 std::string source("delegate"); | |
| 625 net_log_.AddEvent(NetLog::TYPE_CANCELLED, | |
| 626 NetLog::StringCallback("source", &source)); | |
| 627 RestartWithJob(new URLRequestErrorJob( | |
| 628 this, network_delegate_, ERR_BLOCKED_BY_CLIENT)); | |
| 629 return; | |
| 630 } | |
| 631 } | |
| 632 | |
| 633 // Don't allow errors to be sent from within Start(). | |
| 634 // TODO(brettw) this may cause NotifyDone to be sent synchronously, | |
| 635 // we probably don't want this: they should be sent asynchronously so | |
| 636 // the caller does not get reentered. | |
| 637 job_->Start(); | |
| 638 } | |
| 639 | |
| 640 void URLRequest::Restart() { | |
| 641 // Should only be called if the original job didn't make any progress. | |
| 642 DCHECK(job_.get() && !job_->has_response_started()); | |
| 643 RestartWithJob( | |
| 644 URLRequestJobManager::GetInstance()->CreateJob(this, network_delegate_)); | |
| 645 } | |
| 646 | |
| 647 void URLRequest::RestartWithJob(URLRequestJob *job) { | |
| 648 DCHECK(job->request() == this); | |
| 649 PrepareToRestart(); | |
| 650 StartJob(job); | |
| 651 } | |
| 652 | |
| 653 void URLRequest::Cancel() { | |
| 654 DoCancel(ERR_ABORTED, SSLInfo()); | |
| 655 } | |
| 656 | |
| 657 void URLRequest::CancelWithError(int error) { | |
| 658 DoCancel(error, SSLInfo()); | |
| 659 } | |
| 660 | |
| 661 void URLRequest::CancelWithSSLError(int error, const SSLInfo& ssl_info) { | |
| 662 // This should only be called on a started request. | |
| 663 if (!is_pending_ || !job_.get() || job_->has_response_started()) { | |
| 664 NOTREACHED(); | |
| 665 return; | |
| 666 } | |
| 667 DoCancel(error, ssl_info); | |
| 668 } | |
| 669 | |
| 670 void URLRequest::DoCancel(int error, const SSLInfo& ssl_info) { | |
| 671 DCHECK(error < 0); | |
| 672 // If cancelled while calling a delegate, clear delegate info. | |
| 673 if (calling_delegate_) { | |
| 674 LogUnblocked(); | |
| 675 OnCallToDelegateComplete(); | |
| 676 } | |
| 677 | |
| 678 // If the URL request already has an error status, then canceling is a no-op. | |
| 679 // Plus, we don't want to change the error status once it has been set. | |
| 680 if (status_.is_success()) { | |
| 681 status_.set_status(URLRequestStatus::CANCELED); | |
| 682 status_.set_error(error); | |
| 683 response_info_.ssl_info = ssl_info; | |
| 684 | |
| 685 // If the request hasn't already been completed, log a cancellation event. | |
| 686 if (!has_notified_completion_) { | |
| 687 // Don't log an error code on ERR_ABORTED, since that's redundant. | |
| 688 net_log_.AddEventWithNetErrorCode(NetLog::TYPE_CANCELLED, | |
| 689 error == ERR_ABORTED ? OK : error); | |
| 690 } | |
| 691 } | |
| 692 | |
| 693 if (is_pending_ && job_.get()) | |
| 694 job_->Kill(); | |
| 695 | |
| 696 // We need to notify about the end of this job here synchronously. The | |
| 697 // Job sends an asynchronous notification but by the time this is processed, | |
| 698 // our |context_| is NULL. | |
| 699 NotifyRequestCompleted(); | |
| 700 | |
| 701 // The Job will call our NotifyDone method asynchronously. This is done so | |
| 702 // that the Delegate implementation can call Cancel without having to worry | |
| 703 // about being called recursively. | |
| 704 } | |
| 705 | |
| 706 bool URLRequest::Read(IOBuffer* dest, int dest_size, int* bytes_read) { | |
| 707 DCHECK(job_.get()); | |
| 708 DCHECK(bytes_read); | |
| 709 *bytes_read = 0; | |
| 710 | |
| 711 // If this is the first read, end the delegate call that may have started in | |
| 712 // OnResponseStarted. | |
| 713 OnCallToDelegateComplete(); | |
| 714 | |
| 715 // This handles a cancel that happens while paused. | |
| 716 // TODO(ahendrickson): DCHECK() that it is not done after | |
| 717 // http://crbug.com/115705 is fixed. | |
| 718 if (job_->is_done()) | |
| 719 return false; | |
| 720 | |
| 721 if (dest_size == 0) { | |
| 722 // Caller is not too bright. I guess we've done what they asked. | |
| 723 return true; | |
| 724 } | |
| 725 | |
| 726 // Once the request fails or is cancelled, read will just return 0 bytes | |
| 727 // to indicate end of stream. | |
| 728 if (!status_.is_success()) { | |
| 729 return true; | |
| 730 } | |
| 731 | |
| 732 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | |
| 733 tracked_objects::ScopedTracker tracking_profile1( | |
| 734 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read1")); | |
| 735 | |
| 736 bool rv = job_->Read(dest, dest_size, bytes_read); | |
| 737 // If rv is false, the status cannot be success. | |
| 738 DCHECK(rv || status_.status() != URLRequestStatus::SUCCESS); | |
| 739 | |
| 740 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | |
| 741 tracked_objects::ScopedTracker tracking_profile2( | |
| 742 FROM_HERE_WITH_EXPLICIT_FUNCTION("423948 URLRequest::Read2")); | |
| 743 | |
| 744 if (rv && *bytes_read <= 0 && status_.is_success()) | |
| 745 NotifyRequestCompleted(); | |
| 746 return rv; | |
| 747 } | |
| 748 | |
| 749 void URLRequest::StopCaching() { | |
| 750 DCHECK(job_.get()); | |
| 751 job_->StopCaching(); | |
| 752 } | |
| 753 | |
| 754 void URLRequest::NotifyReceivedRedirect(const RedirectInfo& redirect_info, | |
| 755 bool* defer_redirect) { | |
| 756 is_redirecting_ = true; | |
| 757 | |
| 758 // TODO(davidben): Pass the full RedirectInfo down to MaybeInterceptRedirect? | |
| 759 URLRequestJob* job = | |
| 760 URLRequestJobManager::GetInstance()->MaybeInterceptRedirect( | |
| 761 this, network_delegate_, redirect_info.new_url); | |
| 762 if (job) { | |
| 763 RestartWithJob(job); | |
| 764 } else if (delegate_) { | |
| 765 OnCallToDelegate(); | |
| 766 | |
| 767 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | |
| 768 tracked_objects::ScopedTracker tracking_profile( | |
| 769 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 770 "423948 URLRequest::Delegate::OnReceivedRedirect")); | |
| 771 delegate_->OnReceivedRedirect(this, redirect_info, defer_redirect); | |
| 772 // |this| may be have been destroyed here. | |
| 773 } | |
| 774 } | |
| 775 | |
| 776 void URLRequest::NotifyBeforeNetworkStart(bool* defer) { | |
| 777 if (delegate_ && !notified_before_network_start_) { | |
| 778 OnCallToDelegate(); | |
| 779 { | |
| 780 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is | |
| 781 // fixed. | |
| 782 tracked_objects::ScopedTracker tracking_profile( | |
| 783 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 784 "423948 URLRequest::Delegate::OnBeforeNetworkStart")); | |
| 785 delegate_->OnBeforeNetworkStart(this, defer); | |
| 786 } | |
| 787 if (!*defer) | |
| 788 OnCallToDelegateComplete(); | |
| 789 notified_before_network_start_ = true; | |
| 790 } | |
| 791 } | |
| 792 | |
| 793 void URLRequest::ResumeNetworkStart() { | |
| 794 DCHECK(job_.get()); | |
| 795 DCHECK(notified_before_network_start_); | |
| 796 | |
| 797 OnCallToDelegateComplete(); | |
| 798 job_->ResumeNetworkStart(); | |
| 799 } | |
| 800 | |
| 801 void URLRequest::NotifyResponseStarted() { | |
| 802 int net_error = OK; | |
| 803 if (!status_.is_success()) | |
| 804 net_error = status_.error(); | |
| 805 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_URL_REQUEST_START_JOB, | |
| 806 net_error); | |
| 807 | |
| 808 URLRequestJob* job = | |
| 809 URLRequestJobManager::GetInstance()->MaybeInterceptResponse( | |
| 810 this, network_delegate_); | |
| 811 if (job) { | |
| 812 RestartWithJob(job); | |
| 813 } else { | |
| 814 if (delegate_) { | |
| 815 // In some cases (e.g. an event was canceled), we might have sent the | |
| 816 // completion event and receive a NotifyResponseStarted() later. | |
| 817 if (!has_notified_completion_ && status_.is_success()) { | |
| 818 if (network_delegate_) | |
| 819 network_delegate_->NotifyResponseStarted(this); | |
| 820 } | |
| 821 | |
| 822 // Notify in case the entire URL Request has been finished. | |
| 823 if (!has_notified_completion_ && !status_.is_success()) | |
| 824 NotifyRequestCompleted(); | |
| 825 | |
| 826 OnCallToDelegate(); | |
| 827 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is | |
| 828 // fixed. | |
| 829 tracked_objects::ScopedTracker tracking_profile( | |
| 830 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 831 "423948 URLRequest::Delegate::OnResponseStarted")); | |
| 832 delegate_->OnResponseStarted(this); | |
| 833 // Nothing may appear below this line as OnResponseStarted may delete | |
| 834 // |this|. | |
| 835 } | |
| 836 } | |
| 837 } | |
| 838 | |
| 839 void URLRequest::FollowDeferredRedirect() { | |
| 840 CHECK(job_.get()); | |
| 841 CHECK(status_.is_success()); | |
| 842 | |
| 843 job_->FollowDeferredRedirect(); | |
| 844 } | |
| 845 | |
| 846 void URLRequest::SetAuth(const AuthCredentials& credentials) { | |
| 847 DCHECK(job_.get()); | |
| 848 DCHECK(job_->NeedsAuth()); | |
| 849 | |
| 850 job_->SetAuth(credentials); | |
| 851 } | |
| 852 | |
| 853 void URLRequest::CancelAuth() { | |
| 854 DCHECK(job_.get()); | |
| 855 DCHECK(job_->NeedsAuth()); | |
| 856 | |
| 857 job_->CancelAuth(); | |
| 858 } | |
| 859 | |
| 860 void URLRequest::ContinueWithCertificate(X509Certificate* client_cert) { | |
| 861 DCHECK(job_.get()); | |
| 862 | |
| 863 job_->ContinueWithCertificate(client_cert); | |
| 864 } | |
| 865 | |
| 866 void URLRequest::ContinueDespiteLastError() { | |
| 867 DCHECK(job_.get()); | |
| 868 | |
| 869 job_->ContinueDespiteLastError(); | |
| 870 } | |
| 871 | |
| 872 void URLRequest::PrepareToRestart() { | |
| 873 DCHECK(job_.get()); | |
| 874 | |
| 875 // Close the current URL_REQUEST_START_JOB, since we will be starting a new | |
| 876 // one. | |
| 877 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_START_JOB); | |
| 878 | |
| 879 OrphanJob(); | |
| 880 | |
| 881 response_info_ = HttpResponseInfo(); | |
| 882 response_info_.request_time = base::Time::Now(); | |
| 883 | |
| 884 load_timing_info_ = LoadTimingInfo(); | |
| 885 load_timing_info_.request_start_time = response_info_.request_time; | |
| 886 load_timing_info_.request_start = base::TimeTicks::Now(); | |
| 887 | |
| 888 status_ = URLRequestStatus(); | |
| 889 is_pending_ = false; | |
| 890 } | |
| 891 | |
| 892 void URLRequest::OrphanJob() { | |
| 893 // When calling this function, please check that URLRequestHttpJob is | |
| 894 // not in between calling NetworkDelegate::NotifyHeadersReceived receiving | |
| 895 // the call back. This is currently guaranteed by the following strategies: | |
| 896 // - OrphanJob is called on JobRestart, in this case the URLRequestJob cannot | |
| 897 // be receiving any headers at that time. | |
| 898 // - OrphanJob is called in ~URLRequest, in this case | |
| 899 // NetworkDelegate::NotifyURLRequestDestroyed notifies the NetworkDelegate | |
| 900 // that the callback becomes invalid. | |
| 901 job_->Kill(); | |
| 902 job_->DetachRequest(); // ensures that the job will not call us again | |
| 903 job_ = NULL; | |
| 904 } | |
| 905 | |
| 906 int URLRequest::Redirect(const RedirectInfo& redirect_info) { | |
| 907 // Matches call in NotifyReceivedRedirect. | |
| 908 OnCallToDelegateComplete(); | |
| 909 if (net_log_.IsLogging()) { | |
| 910 net_log_.AddEvent( | |
| 911 NetLog::TYPE_URL_REQUEST_REDIRECTED, | |
| 912 NetLog::StringCallback("location", | |
| 913 &redirect_info.new_url.possibly_invalid_spec())); | |
| 914 } | |
| 915 | |
| 916 // TODO(davidben): Pass the full RedirectInfo to the NetworkDelegate. | |
| 917 if (network_delegate_) | |
| 918 network_delegate_->NotifyBeforeRedirect(this, redirect_info.new_url); | |
| 919 | |
| 920 if (redirect_limit_ <= 0) { | |
| 921 DVLOG(1) << "disallowing redirect: exceeds limit"; | |
| 922 return ERR_TOO_MANY_REDIRECTS; | |
| 923 } | |
| 924 | |
| 925 if (!redirect_info.new_url.is_valid()) | |
| 926 return ERR_INVALID_URL; | |
| 927 | |
| 928 if (!job_->IsSafeRedirect(redirect_info.new_url)) { | |
| 929 DVLOG(1) << "disallowing redirect: unsafe protocol"; | |
| 930 return ERR_UNSAFE_REDIRECT; | |
| 931 } | |
| 932 | |
| 933 if (!final_upload_progress_.position()) | |
| 934 final_upload_progress_ = job_->GetUploadProgress(); | |
| 935 PrepareToRestart(); | |
| 936 | |
| 937 if (redirect_info.new_method != method_) { | |
| 938 // TODO(davidben): This logic still needs to be replicated at the consumers. | |
| 939 if (method_ == "POST") { | |
| 940 // If being switched from POST, must remove headers that were specific to | |
| 941 // the POST and don't have meaning in other methods. For example the | |
| 942 // inclusion of a multipart Content-Type header in GET can cause problems | |
| 943 // with some servers: | |
| 944 // http://code.google.com/p/chromium/issues/detail?id=843 | |
| 945 StripPostSpecificHeaders(&extra_request_headers_); | |
| 946 } | |
| 947 upload_data_stream_.reset(); | |
| 948 method_ = redirect_info.new_method; | |
| 949 } | |
| 950 | |
| 951 referrer_ = redirect_info.new_referrer; | |
| 952 first_party_for_cookies_ = redirect_info.new_first_party_for_cookies; | |
| 953 | |
| 954 url_chain_.push_back(redirect_info.new_url); | |
| 955 --redirect_limit_; | |
| 956 | |
| 957 Start(); | |
| 958 return OK; | |
| 959 } | |
| 960 | |
| 961 const URLRequestContext* URLRequest::context() const { | |
| 962 return context_; | |
| 963 } | |
| 964 | |
| 965 int64 URLRequest::GetExpectedContentSize() const { | |
| 966 int64 expected_content_size = -1; | |
| 967 if (job_.get()) | |
| 968 expected_content_size = job_->expected_content_size(); | |
| 969 | |
| 970 return expected_content_size; | |
| 971 } | |
| 972 | |
| 973 void URLRequest::SetPriority(RequestPriority priority) { | |
| 974 DCHECK_GE(priority, MINIMUM_PRIORITY); | |
| 975 DCHECK_LE(priority, MAXIMUM_PRIORITY); | |
| 976 | |
| 977 if ((load_flags_ & LOAD_IGNORE_LIMITS) && (priority != MAXIMUM_PRIORITY)) { | |
| 978 NOTREACHED(); | |
| 979 // Maintain the invariant that requests with IGNORE_LIMITS set | |
| 980 // have MAXIMUM_PRIORITY for release mode. | |
| 981 return; | |
| 982 } | |
| 983 | |
| 984 if (priority_ == priority) | |
| 985 return; | |
| 986 | |
| 987 priority_ = priority; | |
| 988 if (job_.get()) { | |
| 989 net_log_.AddEvent(NetLog::TYPE_URL_REQUEST_SET_PRIORITY, | |
| 990 NetLog::IntegerCallback("priority", priority_)); | |
| 991 job_->SetPriority(priority_); | |
| 992 } | |
| 993 } | |
| 994 | |
| 995 bool URLRequest::GetHSTSRedirect(GURL* redirect_url) const { | |
| 996 const GURL& url = this->url(); | |
| 997 if (!url.SchemeIs("http")) | |
| 998 return false; | |
| 999 TransportSecurityState* state = context()->transport_security_state(); | |
| 1000 if (state && state->ShouldUpgradeToSSL(url.host())) { | |
| 1001 url::Replacements<char> replacements; | |
| 1002 const char kNewScheme[] = "https"; | |
| 1003 replacements.SetScheme(kNewScheme, url::Component(0, strlen(kNewScheme))); | |
| 1004 *redirect_url = url.ReplaceComponents(replacements); | |
| 1005 return true; | |
| 1006 } | |
| 1007 return false; | |
| 1008 } | |
| 1009 | |
| 1010 void URLRequest::NotifyAuthRequired(AuthChallengeInfo* auth_info) { | |
| 1011 NetworkDelegate::AuthRequiredResponse rv = | |
| 1012 NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION; | |
| 1013 auth_info_ = auth_info; | |
| 1014 if (network_delegate_) { | |
| 1015 OnCallToDelegate(); | |
| 1016 rv = network_delegate_->NotifyAuthRequired( | |
| 1017 this, | |
| 1018 *auth_info, | |
| 1019 base::Bind(&URLRequest::NotifyAuthRequiredComplete, | |
| 1020 base::Unretained(this)), | |
| 1021 &auth_credentials_); | |
| 1022 if (rv == NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING) | |
| 1023 return; | |
| 1024 } | |
| 1025 | |
| 1026 NotifyAuthRequiredComplete(rv); | |
| 1027 } | |
| 1028 | |
| 1029 void URLRequest::NotifyAuthRequiredComplete( | |
| 1030 NetworkDelegate::AuthRequiredResponse result) { | |
| 1031 OnCallToDelegateComplete(); | |
| 1032 | |
| 1033 // Check that there are no callbacks to already canceled requests. | |
| 1034 DCHECK_NE(URLRequestStatus::CANCELED, status_.status()); | |
| 1035 | |
| 1036 // NotifyAuthRequired may be called multiple times, such as | |
| 1037 // when an authentication attempt fails. Clear out the data | |
| 1038 // so it can be reset on another round. | |
| 1039 AuthCredentials credentials = auth_credentials_; | |
| 1040 auth_credentials_ = AuthCredentials(); | |
| 1041 scoped_refptr<AuthChallengeInfo> auth_info; | |
| 1042 auth_info.swap(auth_info_); | |
| 1043 | |
| 1044 switch (result) { | |
| 1045 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION: | |
| 1046 // Defer to the URLRequest::Delegate, since the NetworkDelegate | |
| 1047 // didn't take an action. | |
| 1048 if (delegate_) { | |
| 1049 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is | |
| 1050 // fixed. | |
| 1051 tracked_objects::ScopedTracker tracking_profile( | |
| 1052 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 1053 "423948 URLRequest::Delegate::OnAuthRequired")); | |
| 1054 delegate_->OnAuthRequired(this, auth_info.get()); | |
| 1055 } | |
| 1056 break; | |
| 1057 | |
| 1058 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH: | |
| 1059 SetAuth(credentials); | |
| 1060 break; | |
| 1061 | |
| 1062 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_CANCEL_AUTH: | |
| 1063 CancelAuth(); | |
| 1064 break; | |
| 1065 | |
| 1066 case NetworkDelegate::AUTH_REQUIRED_RESPONSE_IO_PENDING: | |
| 1067 NOTREACHED(); | |
| 1068 break; | |
| 1069 } | |
| 1070 } | |
| 1071 | |
| 1072 void URLRequest::NotifyCertificateRequested( | |
| 1073 SSLCertRequestInfo* cert_request_info) { | |
| 1074 if (delegate_) { | |
| 1075 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | |
| 1076 tracked_objects::ScopedTracker tracking_profile( | |
| 1077 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 1078 "423948 URLRequest::Delegate::OnCertificateRequested")); | |
| 1079 delegate_->OnCertificateRequested(this, cert_request_info); | |
| 1080 } | |
| 1081 } | |
| 1082 | |
| 1083 void URLRequest::NotifySSLCertificateError(const SSLInfo& ssl_info, | |
| 1084 bool fatal) { | |
| 1085 if (delegate_) { | |
| 1086 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | |
| 1087 tracked_objects::ScopedTracker tracking_profile( | |
| 1088 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 1089 "423948 URLRequest::Delegate::OnSSLCertificateError")); | |
| 1090 delegate_->OnSSLCertificateError(this, ssl_info, fatal); | |
| 1091 } | |
| 1092 } | |
| 1093 | |
| 1094 bool URLRequest::CanGetCookies(const CookieList& cookie_list) const { | |
| 1095 DCHECK(!(load_flags_ & LOAD_DO_NOT_SEND_COOKIES)); | |
| 1096 if (network_delegate_) { | |
| 1097 return network_delegate_->CanGetCookies(*this, cookie_list); | |
| 1098 } | |
| 1099 return g_default_can_use_cookies; | |
| 1100 } | |
| 1101 | |
| 1102 bool URLRequest::CanSetCookie(const std::string& cookie_line, | |
| 1103 CookieOptions* options) const { | |
| 1104 DCHECK(!(load_flags_ & LOAD_DO_NOT_SAVE_COOKIES)); | |
| 1105 if (network_delegate_) { | |
| 1106 return network_delegate_->CanSetCookie(*this, cookie_line, options); | |
| 1107 } | |
| 1108 return g_default_can_use_cookies; | |
| 1109 } | |
| 1110 | |
| 1111 bool URLRequest::CanEnablePrivacyMode() const { | |
| 1112 if (network_delegate_) { | |
| 1113 return network_delegate_->CanEnablePrivacyMode(url(), | |
| 1114 first_party_for_cookies_); | |
| 1115 } | |
| 1116 return !g_default_can_use_cookies; | |
| 1117 } | |
| 1118 | |
| 1119 | |
| 1120 void URLRequest::NotifyReadCompleted(int bytes_read) { | |
| 1121 // Notify in case the entire URL Request has been finished. | |
| 1122 if (bytes_read <= 0) | |
| 1123 NotifyRequestCompleted(); | |
| 1124 | |
| 1125 // Notify NetworkChangeNotifier that we just received network data. | |
| 1126 // This is to identify cases where the NetworkChangeNotifier thinks we | |
| 1127 // are off-line but we are still receiving network data (crbug.com/124069), | |
| 1128 // and to get rough network connection measurements. | |
| 1129 if (bytes_read > 0 && !was_cached()) | |
| 1130 NetworkChangeNotifier::NotifyDataReceived(*this, bytes_read); | |
| 1131 | |
| 1132 if (delegate_) { | |
| 1133 // TODO(vadimt): Remove ScopedTracker below once crbug.com/423948 is fixed. | |
| 1134 tracked_objects::ScopedTracker tracking_profile( | |
| 1135 FROM_HERE_WITH_EXPLICIT_FUNCTION( | |
| 1136 "423948 URLRequest::Delegate::OnReadCompleted")); | |
| 1137 delegate_->OnReadCompleted(this, bytes_read); | |
| 1138 } | |
| 1139 | |
| 1140 // Nothing below this line as OnReadCompleted may delete |this|. | |
| 1141 } | |
| 1142 | |
| 1143 void URLRequest::OnHeadersComplete() { | |
| 1144 // Cache load timing information now, as information will be lost once the | |
| 1145 // socket is closed and the ClientSocketHandle is Reset, which will happen | |
| 1146 // once the body is complete. The start times should already be populated. | |
| 1147 if (job_.get()) { | |
| 1148 // Keep a copy of the two times the URLRequest sets. | |
| 1149 base::TimeTicks request_start = load_timing_info_.request_start; | |
| 1150 base::Time request_start_time = load_timing_info_.request_start_time; | |
| 1151 | |
| 1152 // Clear load times. Shouldn't be neded, but gives the GetLoadTimingInfo a | |
| 1153 // consistent place to start from. | |
| 1154 load_timing_info_ = LoadTimingInfo(); | |
| 1155 job_->GetLoadTimingInfo(&load_timing_info_); | |
| 1156 | |
| 1157 load_timing_info_.request_start = request_start; | |
| 1158 load_timing_info_.request_start_time = request_start_time; | |
| 1159 | |
| 1160 ConvertRealLoadTimesToBlockingTimes(&load_timing_info_); | |
| 1161 } | |
| 1162 } | |
| 1163 | |
| 1164 void URLRequest::NotifyRequestCompleted() { | |
| 1165 // TODO(battre): Get rid of this check, according to willchan it should | |
| 1166 // not be needed. | |
| 1167 if (has_notified_completion_) | |
| 1168 return; | |
| 1169 | |
| 1170 is_pending_ = false; | |
| 1171 is_redirecting_ = false; | |
| 1172 has_notified_completion_ = true; | |
| 1173 if (network_delegate_) | |
| 1174 network_delegate_->NotifyCompleted(this, job_.get() != NULL); | |
| 1175 } | |
| 1176 | |
| 1177 void URLRequest::OnCallToDelegate() { | |
| 1178 DCHECK(!calling_delegate_); | |
| 1179 DCHECK(blocked_by_.empty()); | |
| 1180 calling_delegate_ = true; | |
| 1181 net_log_.BeginEvent(NetLog::TYPE_URL_REQUEST_DELEGATE); | |
| 1182 } | |
| 1183 | |
| 1184 void URLRequest::OnCallToDelegateComplete() { | |
| 1185 // This should have been cleared before resuming the request. | |
| 1186 DCHECK(blocked_by_.empty()); | |
| 1187 if (!calling_delegate_) | |
| 1188 return; | |
| 1189 calling_delegate_ = false; | |
| 1190 net_log_.EndEvent(NetLog::TYPE_URL_REQUEST_DELEGATE); | |
| 1191 } | |
| 1192 | |
| 1193 void URLRequest::set_stack_trace(const base::debug::StackTrace& stack_trace) { | |
| 1194 base::debug::StackTrace* stack_trace_copy = | |
| 1195 new base::debug::StackTrace(NULL, 0); | |
| 1196 *stack_trace_copy = stack_trace; | |
| 1197 stack_trace_.reset(stack_trace_copy); | |
| 1198 } | |
| 1199 | |
| 1200 const base::debug::StackTrace* URLRequest::stack_trace() const { | |
| 1201 return stack_trace_.get(); | |
| 1202 } | |
| 1203 | |
| 1204 } // namespace net | |
| OLD | NEW |