[Presentation API] Plumbing of |onstatechange| from the Mojo service to the public...
[chromium-blink-merge.git] / net / http / http_cache_transaction.cc
blob7b29dceb42e29944cfd419e9879f1ea2f37be5ce
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/http/http_cache_transaction.h"
7 #include "build/build_config.h"
9 #if defined(OS_POSIX)
10 #include <unistd.h>
11 #endif
13 #include <algorithm>
14 #include <string>
16 #include "base/bind.h"
17 #include "base/compiler_specific.h"
18 #include "base/format_macros.h"
19 #include "base/memory/ref_counted.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/metrics/field_trial.h"
22 #include "base/metrics/histogram.h"
23 #include "base/metrics/sparse_histogram.h"
24 #include "base/profiler/scoped_tracker.h"
25 #include "base/rand_util.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/strings/string_piece.h"
28 #include "base/strings/string_util.h"
29 #include "base/strings/stringprintf.h"
30 #include "base/time/clock.h"
31 #include "base/time/time.h"
32 #include "base/values.h"
33 #include "net/base/completion_callback.h"
34 #include "net/base/io_buffer.h"
35 #include "net/base/load_flags.h"
36 #include "net/base/load_timing_info.h"
37 #include "net/base/net_errors.h"
38 #include "net/base/net_log.h"
39 #include "net/base/upload_data_stream.h"
40 #include "net/cert/cert_status_flags.h"
41 #include "net/disk_cache/disk_cache.h"
42 #include "net/http/disk_based_cert_cache.h"
43 #include "net/http/http_network_session.h"
44 #include "net/http/http_request_info.h"
45 #include "net/http/http_response_headers.h"
46 #include "net/http/http_transaction.h"
47 #include "net/http/http_util.h"
48 #include "net/http/partial_data.h"
49 #include "net/ssl/ssl_cert_request_info.h"
50 #include "net/ssl/ssl_config_service.h"
52 using base::Time;
53 using base::TimeDelta;
54 using base::TimeTicks;
56 namespace {
58 // TODO(ricea): Move this to HttpResponseHeaders once it is standardised.
59 static const char kFreshnessHeader[] = "Resource-Freshness";
61 // Stores data relevant to the statistics of writing and reading entire
62 // certificate chains using DiskBasedCertCache. |num_pending_ops| is the number
63 // of certificates in the chain that have pending operations in the
64 // DiskBasedCertCache. |start_time| is the time that the read and write
65 // commands began being issued to the DiskBasedCertCache.
66 // TODO(brandonsalmon): Remove this when it is no longer necessary to
67 // collect data.
68 class SharedChainData : public base::RefCounted<SharedChainData> {
69 public:
70 SharedChainData(int num_ops, TimeTicks start)
71 : num_pending_ops(num_ops), start_time(start) {}
73 int num_pending_ops;
74 TimeTicks start_time;
76 private:
77 friend class base::RefCounted<SharedChainData>;
78 ~SharedChainData() {}
79 DISALLOW_COPY_AND_ASSIGN(SharedChainData);
82 // Used to obtain a cache entry key for an OSCertHandle.
83 // TODO(brandonsalmon): Remove this when cache keys are stored
84 // and no longer have to be recomputed to retrieve the OSCertHandle
85 // from the disk.
86 std::string GetCacheKeyForCert(net::X509Certificate::OSCertHandle cert_handle) {
87 net::SHA1HashValue fingerprint =
88 net::X509Certificate::CalculateFingerprint(cert_handle);
90 return "cert:" +
91 base::HexEncode(fingerprint.data, arraysize(fingerprint.data));
94 // |dist_from_root| indicates the position of the read certificate in the
95 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
96 // whether or not the read certificate was the leaf of the chain.
97 // |shared_chain_data| contains data shared by each certificate in
98 // the chain.
99 void OnCertReadIOComplete(
100 int dist_from_root,
101 bool is_leaf,
102 const scoped_refptr<SharedChainData>& shared_chain_data,
103 net::X509Certificate::OSCertHandle cert_handle) {
104 // If |num_pending_ops| is one, this was the last pending read operation
105 // for this chain of certificates. The total time used to read the chain
106 // can be calculated by subtracting the starting time from Now().
107 shared_chain_data->num_pending_ops--;
108 if (!shared_chain_data->num_pending_ops) {
109 const TimeDelta read_chain_wait =
110 TimeTicks::Now() - shared_chain_data->start_time;
111 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainReadTime",
112 read_chain_wait,
113 base::TimeDelta::FromMilliseconds(1),
114 base::TimeDelta::FromMinutes(10),
115 50);
118 bool success = (cert_handle != NULL);
119 if (is_leaf)
120 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoReadSuccessLeaf", success);
122 if (success)
123 UMA_HISTOGRAM_CUSTOM_COUNTS(
124 "DiskBasedCertCache.CertIoReadSuccess", dist_from_root, 0, 10, 7);
125 else
126 UMA_HISTOGRAM_CUSTOM_COUNTS(
127 "DiskBasedCertCache.CertIoReadFailure", dist_from_root, 0, 10, 7);
130 // |dist_from_root| indicates the position of the written certificate in the
131 // certificate chain, 0 indicating it is the root. |is_leaf| indicates
132 // whether or not the written certificate was the leaf of the chain.
133 // |shared_chain_data| contains data shared by each certificate in
134 // the chain.
135 void OnCertWriteIOComplete(
136 int dist_from_root,
137 bool is_leaf,
138 const scoped_refptr<SharedChainData>& shared_chain_data,
139 const std::string& key) {
140 // If |num_pending_ops| is one, this was the last pending write operation
141 // for this chain of certificates. The total time used to write the chain
142 // can be calculated by subtracting the starting time from Now().
143 shared_chain_data->num_pending_ops--;
144 if (!shared_chain_data->num_pending_ops) {
145 const TimeDelta write_chain_wait =
146 TimeTicks::Now() - shared_chain_data->start_time;
147 UMA_HISTOGRAM_CUSTOM_TIMES("DiskBasedCertCache.ChainWriteTime",
148 write_chain_wait,
149 base::TimeDelta::FromMilliseconds(1),
150 base::TimeDelta::FromMinutes(10),
151 50);
154 bool success = !key.empty();
155 if (is_leaf)
156 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.CertIoWriteSuccessLeaf", success);
158 if (success)
159 UMA_HISTOGRAM_CUSTOM_COUNTS(
160 "DiskBasedCertCache.CertIoWriteSuccess", dist_from_root, 0, 10, 7);
161 else
162 UMA_HISTOGRAM_CUSTOM_COUNTS(
163 "DiskBasedCertCache.CertIoWriteFailure", dist_from_root, 0, 10, 7);
166 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
167 // a "non-error response" is one with a 2xx (Successful) or 3xx
168 // (Redirection) status code.
169 bool NonErrorResponse(int status_code) {
170 int status_code_range = status_code / 100;
171 return status_code_range == 2 || status_code_range == 3;
174 // Error codes that will be considered indicative of a page being offline/
175 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
176 bool IsOfflineError(int error) {
177 return (error == net::ERR_NAME_NOT_RESOLVED ||
178 error == net::ERR_INTERNET_DISCONNECTED ||
179 error == net::ERR_ADDRESS_UNREACHABLE ||
180 error == net::ERR_CONNECTION_TIMED_OUT);
183 // Enum for UMA, indicating the status (with regard to offline mode) of
184 // a particular request.
185 enum RequestOfflineStatus {
186 // A cache transaction hit in cache (data was present and not stale)
187 // and returned it.
188 OFFLINE_STATUS_FRESH_CACHE,
190 // A network request was required for a cache entry, and it succeeded.
191 OFFLINE_STATUS_NETWORK_SUCCEEDED,
193 // A network request was required for a cache entry, and it failed with
194 // a non-offline error.
195 OFFLINE_STATUS_NETWORK_FAILED,
197 // A network request was required for a cache entry, it failed with an
198 // offline error, and we could serve stale data if
199 // LOAD_FROM_CACHE_IF_OFFLINE was set.
200 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE,
202 // A network request was required for a cache entry, it failed with
203 // an offline error, and there was no servable data in cache (even
204 // stale data).
205 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE,
207 OFFLINE_STATUS_MAX_ENTRIES
210 void RecordOfflineStatus(int load_flags, RequestOfflineStatus status) {
211 // Restrict to main frame to keep statistics close to
212 // "would have shown them something useful if offline mode was enabled".
213 if (load_flags & net::LOAD_MAIN_FRAME) {
214 UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status,
215 OFFLINE_STATUS_MAX_ENTRIES);
219 void RecordNoStoreHeaderHistogram(int load_flags,
220 const net::HttpResponseInfo* response) {
221 if (load_flags & net::LOAD_MAIN_FRAME) {
222 UMA_HISTOGRAM_BOOLEAN(
223 "Net.MainFrameNoStore",
224 response->headers->HasHeaderValue("cache-control", "no-store"));
228 base::Value* NetLogAsyncRevalidationInfoCallback(
229 const net::NetLog::Source& source,
230 const net::HttpRequestInfo* request,
231 net::NetLog::LogLevel log_level) {
232 base::DictionaryValue* dict = new base::DictionaryValue();
233 source.AddToEventParameters(dict);
235 dict->SetString("url", request->url.possibly_invalid_spec());
236 dict->SetString("method", request->method);
237 return dict;
240 enum ExternallyConditionalizedType {
241 EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION,
242 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE,
243 EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS,
244 EXTERNALLY_CONDITIONALIZED_MAX
247 } // namespace
249 namespace net {
251 struct HeaderNameAndValue {
252 const char* name;
253 const char* value;
256 // If the request includes one of these request headers, then avoid caching
257 // to avoid getting confused.
258 static const HeaderNameAndValue kPassThroughHeaders[] = {
259 { "if-unmodified-since", NULL }, // causes unexpected 412s
260 { "if-match", NULL }, // causes unexpected 412s
261 { "if-range", NULL },
262 { NULL, NULL }
265 struct ValidationHeaderInfo {
266 const char* request_header_name;
267 const char* related_response_header_name;
270 static const ValidationHeaderInfo kValidationHeaders[] = {
271 { "if-modified-since", "last-modified" },
272 { "if-none-match", "etag" },
275 // If the request includes one of these request headers, then avoid reusing
276 // our cached copy if any.
277 static const HeaderNameAndValue kForceFetchHeaders[] = {
278 { "cache-control", "no-cache" },
279 { "pragma", "no-cache" },
280 { NULL, NULL }
283 // If the request includes one of these request headers, then force our
284 // cached copy (if any) to be revalidated before reusing it.
285 static const HeaderNameAndValue kForceValidateHeaders[] = {
286 { "cache-control", "max-age=0" },
287 { NULL, NULL }
290 static bool HeaderMatches(const HttpRequestHeaders& headers,
291 const HeaderNameAndValue* search) {
292 for (; search->name; ++search) {
293 std::string header_value;
294 if (!headers.GetHeader(search->name, &header_value))
295 continue;
297 if (!search->value)
298 return true;
300 HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
301 while (v.GetNext()) {
302 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value))
303 return true;
306 return false;
309 //-----------------------------------------------------------------------------
311 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
312 : next_state_(STATE_NONE),
313 request_(NULL),
314 priority_(priority),
315 cache_(cache->GetWeakPtr()),
316 entry_(NULL),
317 new_entry_(NULL),
318 new_response_(NULL),
319 mode_(NONE),
320 target_state_(STATE_NONE),
321 reading_(false),
322 invalid_range_(false),
323 truncated_(false),
324 is_sparse_(false),
325 range_requested_(false),
326 handling_206_(false),
327 cache_pending_(false),
328 done_reading_(false),
329 vary_mismatch_(false),
330 couldnt_conditionalize_request_(false),
331 bypass_lock_for_test_(false),
332 fail_conditionalization_for_test_(false),
333 io_buf_len_(0),
334 read_offset_(0),
335 effective_load_flags_(0),
336 write_len_(0),
337 transaction_pattern_(PATTERN_UNDEFINED),
338 total_received_bytes_(0),
339 websocket_handshake_stream_base_create_helper_(NULL),
340 weak_factory_(this) {
341 static_assert(HttpCache::Transaction::kNumValidationHeaders ==
342 arraysize(kValidationHeaders),
343 "invalid number of validation headers");
345 io_callback_ = base::Bind(&Transaction::OnIOComplete,
346 weak_factory_.GetWeakPtr());
349 HttpCache::Transaction::~Transaction() {
350 // We may have to issue another IO, but we should never invoke the callback_
351 // after this point.
352 callback_.Reset();
354 if (cache_) {
355 if (entry_) {
356 bool cancel_request = reading_ && response_.headers.get();
357 if (cancel_request) {
358 if (partial_) {
359 entry_->disk_entry->CancelSparseIO();
360 } else {
361 cancel_request &= (response_.headers->response_code() == 200);
365 cache_->DoneWithEntry(entry_, this, cancel_request);
366 } else if (cache_pending_) {
367 cache_->RemovePendingTransaction(this);
372 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
373 const CompletionCallback& callback) {
374 DCHECK(buf);
375 DCHECK_GT(buf_len, 0);
376 DCHECK(!callback.is_null());
377 if (!cache_.get() || !entry_)
378 return ERR_UNEXPECTED;
380 // We don't need to track this operation for anything.
381 // It could be possible to check if there is something already written and
382 // avoid writing again (it should be the same, right?), but let's allow the
383 // caller to "update" the contents with something new.
384 return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
385 callback, true);
388 bool HttpCache::Transaction::AddTruncatedFlag() {
389 DCHECK(mode_ & WRITE || mode_ == NONE);
391 // Don't set the flag for sparse entries.
392 if (partial_.get() && !truncated_)
393 return true;
395 if (!CanResume(true))
396 return false;
398 // We may have received the whole resource already.
399 if (done_reading_)
400 return true;
402 truncated_ = true;
403 target_state_ = STATE_NONE;
404 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
405 DoLoop(OK);
406 return true;
409 LoadState HttpCache::Transaction::GetWriterLoadState() const {
410 if (network_trans_.get())
411 return network_trans_->GetLoadState();
412 if (entry_ || !request_)
413 return LOAD_STATE_IDLE;
414 return LOAD_STATE_WAITING_FOR_CACHE;
417 const BoundNetLog& HttpCache::Transaction::net_log() const {
418 return net_log_;
421 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
422 const CompletionCallback& callback,
423 const BoundNetLog& net_log) {
424 DCHECK(request);
425 DCHECK(!callback.is_null());
427 // Ensure that we only have one asynchronous call at a time.
428 DCHECK(callback_.is_null());
429 DCHECK(!reading_);
430 DCHECK(!network_trans_.get());
431 DCHECK(!entry_);
433 if (!cache_.get())
434 return ERR_UNEXPECTED;
436 SetRequest(net_log, request);
438 // We have to wait until the backend is initialized so we start the SM.
439 next_state_ = STATE_GET_BACKEND;
440 int rv = DoLoop(OK);
442 // Setting this here allows us to check for the existence of a callback_ to
443 // determine if we are still inside Start.
444 if (rv == ERR_IO_PENDING) {
445 callback_ = tracked_objects::ScopedTracker::TrackCallback(
446 FROM_HERE_WITH_EXPLICIT_FUNCTION(
447 "422516 HttpCache::Transaction::Start"),
448 callback);
451 return rv;
454 int HttpCache::Transaction::RestartIgnoringLastError(
455 const CompletionCallback& callback) {
456 DCHECK(!callback.is_null());
458 // Ensure that we only have one asynchronous call at a time.
459 DCHECK(callback_.is_null());
461 if (!cache_.get())
462 return ERR_UNEXPECTED;
464 int rv = RestartNetworkRequest();
466 if (rv == ERR_IO_PENDING) {
467 callback_ = tracked_objects::ScopedTracker::TrackCallback(
468 FROM_HERE_WITH_EXPLICIT_FUNCTION(
469 "422516 HttpCache::Transaction::RestartIgnoringLastError"),
470 callback);
473 return rv;
476 int HttpCache::Transaction::RestartWithCertificate(
477 X509Certificate* client_cert,
478 const CompletionCallback& callback) {
479 DCHECK(!callback.is_null());
481 // Ensure that we only have one asynchronous call at a time.
482 DCHECK(callback_.is_null());
484 if (!cache_.get())
485 return ERR_UNEXPECTED;
487 int rv = RestartNetworkRequestWithCertificate(client_cert);
489 if (rv == ERR_IO_PENDING) {
490 callback_ = tracked_objects::ScopedTracker::TrackCallback(
491 FROM_HERE_WITH_EXPLICIT_FUNCTION(
492 "422516 HttpCache::Transaction::RestartWithCertificate"),
493 callback);
496 return rv;
499 int HttpCache::Transaction::RestartWithAuth(
500 const AuthCredentials& credentials,
501 const CompletionCallback& callback) {
502 DCHECK(auth_response_.headers.get());
503 DCHECK(!callback.is_null());
505 // Ensure that we only have one asynchronous call at a time.
506 DCHECK(callback_.is_null());
508 if (!cache_.get())
509 return ERR_UNEXPECTED;
511 // Clear the intermediate response since we are going to start over.
512 auth_response_ = HttpResponseInfo();
514 int rv = RestartNetworkRequestWithAuth(credentials);
516 if (rv == ERR_IO_PENDING) {
517 callback_ = tracked_objects::ScopedTracker::TrackCallback(
518 FROM_HERE_WITH_EXPLICIT_FUNCTION(
519 "422516 HttpCache::Transaction::RestartWithAuth"),
520 callback);
523 return rv;
526 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
527 if (!network_trans_.get())
528 return false;
529 return network_trans_->IsReadyToRestartForAuth();
532 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
533 const CompletionCallback& callback) {
534 DCHECK(buf);
535 DCHECK_GT(buf_len, 0);
536 DCHECK(!callback.is_null());
538 DCHECK(callback_.is_null());
540 if (!cache_.get())
541 return ERR_UNEXPECTED;
543 // If we have an intermediate auth response at this point, then it means the
544 // user wishes to read the network response (the error page). If there is a
545 // previous response in the cache then we should leave it intact.
546 if (auth_response_.headers.get() && mode_ != NONE) {
547 UpdateTransactionPattern(PATTERN_NOT_COVERED);
548 DCHECK(mode_ & WRITE);
549 DoneWritingToEntry(mode_ == READ_WRITE);
550 mode_ = NONE;
553 reading_ = true;
554 int rv;
556 switch (mode_) {
557 case READ_WRITE:
558 DCHECK(partial_.get());
559 if (!network_trans_.get()) {
560 // We are just reading from the cache, but we may be writing later.
561 rv = ReadFromEntry(buf, buf_len);
562 break;
564 case NONE:
565 case WRITE:
566 DCHECK(network_trans_.get());
567 rv = ReadFromNetwork(buf, buf_len);
568 break;
569 case READ:
570 rv = ReadFromEntry(buf, buf_len);
571 break;
572 default:
573 NOTREACHED();
574 rv = ERR_FAILED;
577 if (rv == ERR_IO_PENDING) {
578 DCHECK(callback_.is_null());
579 callback_ = tracked_objects::ScopedTracker::TrackCallback(
580 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 HttpCache::Transaction::Read"),
581 callback);
583 return rv;
586 void HttpCache::Transaction::StopCaching() {
587 // We really don't know where we are now. Hopefully there is no operation in
588 // progress, but nothing really prevents this method to be called after we
589 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
590 // point because we need the state machine for that (and even if we are really
591 // free, that would be an asynchronous operation). In other words, keep the
592 // entry how it is (it will be marked as truncated at destruction), and let
593 // the next piece of code that executes know that we are now reading directly
594 // from the net.
595 // TODO(mmenke): This doesn't release the lock on the cache entry, so a
596 // future request for the resource will be blocked on this one.
597 // Fix this.
598 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
599 !is_sparse_ && !range_requested_) {
600 mode_ = NONE;
604 bool HttpCache::Transaction::GetFullRequestHeaders(
605 HttpRequestHeaders* headers) const {
606 if (network_trans_)
607 return network_trans_->GetFullRequestHeaders(headers);
609 // TODO(ttuttle): Read headers from cache.
610 return false;
613 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
614 int64 total_received_bytes = total_received_bytes_;
615 if (network_trans_)
616 total_received_bytes += network_trans_->GetTotalReceivedBytes();
617 return total_received_bytes;
620 void HttpCache::Transaction::DoneReading() {
621 if (cache_.get() && entry_) {
622 DCHECK_NE(mode_, UPDATE);
623 if (mode_ & WRITE) {
624 DoneWritingToEntry(true);
625 } else if (mode_ & READ) {
626 // It is necessary to check mode_ & READ because it is possible
627 // for mode_ to be NONE and entry_ non-NULL with a write entry
628 // if StopCaching was called.
629 cache_->DoneReadingFromEntry(entry_, this);
630 entry_ = NULL;
635 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
636 // Null headers means we encountered an error or haven't a response yet
637 if (auth_response_.headers.get())
638 return &auth_response_;
639 return (response_.headers.get() || response_.ssl_info.cert.get() ||
640 response_.cert_request_info.get())
641 ? &response_
642 : NULL;
645 LoadState HttpCache::Transaction::GetLoadState() const {
646 LoadState state = GetWriterLoadState();
647 if (state != LOAD_STATE_WAITING_FOR_CACHE)
648 return state;
650 if (cache_.get())
651 return cache_->GetLoadStateForPendingTransaction(this);
653 return LOAD_STATE_IDLE;
656 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
657 if (network_trans_.get())
658 return network_trans_->GetUploadProgress();
659 return final_upload_progress_;
662 void HttpCache::Transaction::SetQuicServerInfo(
663 QuicServerInfo* quic_server_info) {}
665 bool HttpCache::Transaction::GetLoadTimingInfo(
666 LoadTimingInfo* load_timing_info) const {
667 if (network_trans_)
668 return network_trans_->GetLoadTimingInfo(load_timing_info);
670 if (old_network_trans_load_timing_) {
671 *load_timing_info = *old_network_trans_load_timing_;
672 return true;
675 if (first_cache_access_since_.is_null())
676 return false;
678 // If the cache entry was opened, return that time.
679 load_timing_info->send_start = first_cache_access_since_;
680 // This time doesn't make much sense when reading from the cache, so just use
681 // the same time as send_start.
682 load_timing_info->send_end = first_cache_access_since_;
683 return true;
686 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
687 priority_ = priority;
688 if (network_trans_)
689 network_trans_->SetPriority(priority_);
692 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
693 WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
694 websocket_handshake_stream_base_create_helper_ = create_helper;
695 if (network_trans_)
696 network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
699 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
700 const BeforeNetworkStartCallback& callback) {
701 DCHECK(!network_trans_);
702 before_network_start_callback_ = callback;
705 void HttpCache::Transaction::SetBeforeProxyHeadersSentCallback(
706 const BeforeProxyHeadersSentCallback& callback) {
707 DCHECK(!network_trans_);
708 before_proxy_headers_sent_callback_ = callback;
711 int HttpCache::Transaction::ResumeNetworkStart() {
712 if (network_trans_)
713 return network_trans_->ResumeNetworkStart();
714 return ERR_UNEXPECTED;
717 //-----------------------------------------------------------------------------
719 void HttpCache::Transaction::DoCallback(int rv) {
720 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
721 tracked_objects::ScopedTracker tracking_profile(
722 FROM_HERE_WITH_EXPLICIT_FUNCTION(
723 "422516 HttpCache::Transaction::DoCallback"));
725 DCHECK(rv != ERR_IO_PENDING);
726 DCHECK(!callback_.is_null());
728 read_buf_ = NULL; // Release the buffer before invoking the callback.
730 // Since Run may result in Read being called, clear callback_ up front.
731 CompletionCallback c = callback_;
732 callback_.Reset();
733 c.Run(rv);
736 int HttpCache::Transaction::HandleResult(int rv) {
737 DCHECK(rv != ERR_IO_PENDING);
738 if (!callback_.is_null())
739 DoCallback(rv);
741 return rv;
744 // A few common patterns: (Foo* means Foo -> FooComplete)
746 // 1. Not-cached entry:
747 // Start():
748 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
749 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
750 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
751 // PartialHeadersReceived
753 // Read():
754 // NetworkRead* -> CacheWriteData*
756 // 2. Cached entry, no validation:
757 // Start():
758 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
759 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
760 // BeginCacheValidation() -> SetupEntryForRead()
762 // Read():
763 // CacheReadData*
765 // 3. Cached entry, validation (304):
766 // Start():
767 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
768 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
769 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
770 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
771 // -> OverwriteCachedResponse -> PartialHeadersReceived
773 // Read():
774 // CacheReadData*
776 // 4. Cached entry, validation and replace (200):
777 // Start():
778 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
779 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
780 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
781 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
782 // TruncateCachedMetadata* -> PartialHeadersReceived
784 // Read():
785 // NetworkRead* -> CacheWriteData*
787 // 5. Sparse entry, partially cached, byte range request:
788 // Start():
789 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
790 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
791 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
792 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
793 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
794 // UpdateCachedResponse -> CacheWriteResponse* -> UpdateCachedResponseComplete
795 // -> OverwriteCachedResponse -> PartialHeadersReceived
797 // Read() 1:
798 // NetworkRead* -> CacheWriteData*
800 // Read() 2:
801 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
802 // CompletePartialCacheValidation -> CacheReadData* ->
804 // Read() 3:
805 // CacheReadData* -> StartPartialCacheValidation ->
806 // CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
807 // SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
808 // -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
810 // 6. HEAD. Not-cached entry:
811 // Pass through. Don't save a HEAD by itself.
812 // Start():
813 // GetBackend* -> InitEntry -> OpenEntry* -> SendRequest*
815 // 7. HEAD. Cached entry, no validation:
816 // Start():
817 // The same flow as for a GET request (example #2)
819 // Read():
820 // CacheReadData (returns 0)
822 // 8. HEAD. Cached entry, validation (304):
823 // The request updates the stored headers.
824 // Start(): Same as for a GET request (example #3)
826 // Read():
827 // CacheReadData (returns 0)
829 // 9. HEAD. Cached entry, validation and replace (200):
830 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
831 // itself.
832 // Start():
833 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
834 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
835 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
836 // OverwriteCachedResponse
838 // 10. HEAD. Sparse entry, partially cached:
839 // Serve the request from the cache, as long as it doesn't require
840 // revalidation. Ignore missing ranges when deciding to revalidate. If the
841 // entry requires revalidation, ignore the whole request and go to full pass
842 // through (the result of the HEAD request will NOT update the entry).
844 // Start(): Basically the same as example 7, as we never create a partial_
845 // object for this request.
847 // 11. Prefetch, not-cached entry:
848 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
849 // UpdateCachedResponse.
851 // 12. Prefetch, cached entry:
852 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
853 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
854 // bit is unset.
856 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
857 // Skip validation, similar to example 2.
858 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
859 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
860 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
861 // SetupEntryForRead()
863 // Read():
864 // CacheReadData*
866 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
867 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
868 // CacheReadResponse* and CacheDispatchValidation.
869 int HttpCache::Transaction::DoLoop(int result) {
870 DCHECK(next_state_ != STATE_NONE);
872 int rv = result;
873 do {
874 State state = next_state_;
875 next_state_ = STATE_NONE;
876 switch (state) {
877 case STATE_GET_BACKEND:
878 DCHECK_EQ(OK, rv);
879 rv = DoGetBackend();
880 break;
881 case STATE_GET_BACKEND_COMPLETE:
882 rv = DoGetBackendComplete(rv);
883 break;
884 case STATE_SEND_REQUEST:
885 DCHECK_EQ(OK, rv);
886 rv = DoSendRequest();
887 break;
888 case STATE_SEND_REQUEST_COMPLETE:
889 rv = DoSendRequestComplete(rv);
890 break;
891 case STATE_SUCCESSFUL_SEND_REQUEST:
892 DCHECK_EQ(OK, rv);
893 rv = DoSuccessfulSendRequest();
894 break;
895 case STATE_NETWORK_READ:
896 DCHECK_EQ(OK, rv);
897 rv = DoNetworkRead();
898 break;
899 case STATE_NETWORK_READ_COMPLETE:
900 rv = DoNetworkReadComplete(rv);
901 break;
902 case STATE_INIT_ENTRY:
903 DCHECK_EQ(OK, rv);
904 rv = DoInitEntry();
905 break;
906 case STATE_OPEN_ENTRY:
907 DCHECK_EQ(OK, rv);
908 rv = DoOpenEntry();
909 break;
910 case STATE_OPEN_ENTRY_COMPLETE:
911 rv = DoOpenEntryComplete(rv);
912 break;
913 case STATE_CREATE_ENTRY:
914 DCHECK_EQ(OK, rv);
915 rv = DoCreateEntry();
916 break;
917 case STATE_CREATE_ENTRY_COMPLETE:
918 rv = DoCreateEntryComplete(rv);
919 break;
920 case STATE_DOOM_ENTRY:
921 DCHECK_EQ(OK, rv);
922 rv = DoDoomEntry();
923 break;
924 case STATE_DOOM_ENTRY_COMPLETE:
925 rv = DoDoomEntryComplete(rv);
926 break;
927 case STATE_ADD_TO_ENTRY:
928 DCHECK_EQ(OK, rv);
929 rv = DoAddToEntry();
930 break;
931 case STATE_ADD_TO_ENTRY_COMPLETE:
932 rv = DoAddToEntryComplete(rv);
933 break;
934 case STATE_START_PARTIAL_CACHE_VALIDATION:
935 DCHECK_EQ(OK, rv);
936 rv = DoStartPartialCacheValidation();
937 break;
938 case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
939 rv = DoCompletePartialCacheValidation(rv);
940 break;
941 case STATE_UPDATE_CACHED_RESPONSE:
942 DCHECK_EQ(OK, rv);
943 rv = DoUpdateCachedResponse();
944 break;
945 case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
946 rv = DoUpdateCachedResponseComplete(rv);
947 break;
948 case STATE_OVERWRITE_CACHED_RESPONSE:
949 DCHECK_EQ(OK, rv);
950 rv = DoOverwriteCachedResponse();
951 break;
952 case STATE_TRUNCATE_CACHED_DATA:
953 DCHECK_EQ(OK, rv);
954 rv = DoTruncateCachedData();
955 break;
956 case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
957 rv = DoTruncateCachedDataComplete(rv);
958 break;
959 case STATE_TRUNCATE_CACHED_METADATA:
960 DCHECK_EQ(OK, rv);
961 rv = DoTruncateCachedMetadata();
962 break;
963 case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
964 rv = DoTruncateCachedMetadataComplete(rv);
965 break;
966 case STATE_PARTIAL_HEADERS_RECEIVED:
967 DCHECK_EQ(OK, rv);
968 rv = DoPartialHeadersReceived();
969 break;
970 case STATE_CACHE_READ_RESPONSE:
971 DCHECK_EQ(OK, rv);
972 rv = DoCacheReadResponse();
973 break;
974 case STATE_CACHE_READ_RESPONSE_COMPLETE:
975 rv = DoCacheReadResponseComplete(rv);
976 break;
977 case STATE_CACHE_DISPATCH_VALIDATION:
978 DCHECK_EQ(OK, rv);
979 rv = DoCacheDispatchValidation();
980 break;
981 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH:
982 DCHECK_EQ(OK, rv);
983 rv = DoCacheToggleUnusedSincePrefetch();
984 break;
985 case STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE:
986 rv = DoCacheToggleUnusedSincePrefetchComplete(rv);
987 break;
988 case STATE_CACHE_WRITE_RESPONSE:
989 DCHECK_EQ(OK, rv);
990 rv = DoCacheWriteResponse();
991 break;
992 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
993 DCHECK_EQ(OK, rv);
994 rv = DoCacheWriteTruncatedResponse();
995 break;
996 case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
997 rv = DoCacheWriteResponseComplete(rv);
998 break;
999 case STATE_CACHE_READ_METADATA:
1000 DCHECK_EQ(OK, rv);
1001 rv = DoCacheReadMetadata();
1002 break;
1003 case STATE_CACHE_READ_METADATA_COMPLETE:
1004 rv = DoCacheReadMetadataComplete(rv);
1005 break;
1006 case STATE_CACHE_QUERY_DATA:
1007 DCHECK_EQ(OK, rv);
1008 rv = DoCacheQueryData();
1009 break;
1010 case STATE_CACHE_QUERY_DATA_COMPLETE:
1011 rv = DoCacheQueryDataComplete(rv);
1012 break;
1013 case STATE_CACHE_READ_DATA:
1014 DCHECK_EQ(OK, rv);
1015 rv = DoCacheReadData();
1016 break;
1017 case STATE_CACHE_READ_DATA_COMPLETE:
1018 rv = DoCacheReadDataComplete(rv);
1019 break;
1020 case STATE_CACHE_WRITE_DATA:
1021 rv = DoCacheWriteData(rv);
1022 break;
1023 case STATE_CACHE_WRITE_DATA_COMPLETE:
1024 rv = DoCacheWriteDataComplete(rv);
1025 break;
1026 default:
1027 NOTREACHED() << "bad state";
1028 rv = ERR_FAILED;
1029 break;
1031 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
1033 if (rv != ERR_IO_PENDING)
1034 HandleResult(rv);
1036 return rv;
1039 int HttpCache::Transaction::DoGetBackend() {
1040 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1041 tracked_objects::ScopedTracker tracking_profile(
1042 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1043 "422516 HttpCache::Transaction::DoGetBackend"));
1045 cache_pending_ = true;
1046 next_state_ = STATE_GET_BACKEND_COMPLETE;
1047 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
1048 return cache_->GetBackendForTransaction(this);
1051 int HttpCache::Transaction::DoGetBackendComplete(int result) {
1052 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1053 tracked_objects::ScopedTracker tracking_profile(
1054 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1055 "422516 HttpCache::Transaction::DoGetBackendComplete"));
1057 DCHECK(result == OK || result == ERR_FAILED);
1058 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
1059 result);
1060 cache_pending_ = false;
1062 if (!ShouldPassThrough()) {
1063 cache_key_ = cache_->GenerateCacheKey(request_);
1065 // Requested cache access mode.
1066 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
1067 mode_ = READ;
1068 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
1069 mode_ = WRITE;
1070 } else {
1071 mode_ = READ_WRITE;
1074 // Downgrade to UPDATE if the request has been externally conditionalized.
1075 if (external_validation_.initialized) {
1076 if (mode_ & WRITE) {
1077 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
1078 // in case READ was off).
1079 mode_ = UPDATE;
1080 } else {
1081 mode_ = NONE;
1086 // Use PUT and DELETE only to invalidate existing stored entries.
1087 if ((request_->method == "PUT" || request_->method == "DELETE") &&
1088 mode_ != READ_WRITE && mode_ != WRITE) {
1089 mode_ = NONE;
1092 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
1093 // transaction behaves the same for GET and HEAD requests at this point: if it
1094 // was not modified, the entry is updated and a response is not returned from
1095 // the cache. If we receive 200, it doesn't matter if there was a validation
1096 // header or not.
1097 if (request_->method == "HEAD" && mode_ == WRITE)
1098 mode_ = NONE;
1100 // If must use cache, then we must fail. This can happen for back/forward
1101 // navigations to a page generated via a form post.
1102 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
1103 return ERR_CACHE_MISS;
1105 if (mode_ == NONE) {
1106 if (partial_.get()) {
1107 partial_->RestoreHeaders(&custom_request_->extra_headers);
1108 partial_.reset();
1110 next_state_ = STATE_SEND_REQUEST;
1111 } else {
1112 next_state_ = STATE_INIT_ENTRY;
1115 // This is only set if we have something to do with the response.
1116 range_requested_ = (partial_.get() != NULL);
1118 return OK;
1121 int HttpCache::Transaction::DoSendRequest() {
1122 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1123 tracked_objects::ScopedTracker tracking_profile(
1124 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1125 "422516 HttpCache::Transaction::DoSendRequest"));
1127 DCHECK(mode_ & WRITE || mode_ == NONE);
1128 DCHECK(!network_trans_.get());
1130 send_request_since_ = TimeTicks::Now();
1132 // Create a network transaction.
1133 int rv = cache_->network_layer_->CreateTransaction(priority_,
1134 &network_trans_);
1135 if (rv != OK)
1136 return rv;
1137 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1138 network_trans_->SetBeforeProxyHeadersSentCallback(
1139 before_proxy_headers_sent_callback_);
1141 // Old load timing information, if any, is now obsolete.
1142 old_network_trans_load_timing_.reset();
1144 if (websocket_handshake_stream_base_create_helper_)
1145 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1146 websocket_handshake_stream_base_create_helper_);
1148 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1149 rv = network_trans_->Start(request_, io_callback_, net_log_);
1150 return rv;
1153 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1154 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1155 tracked_objects::ScopedTracker tracking_profile(
1156 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1157 "422516 HttpCache::Transaction::DoSendRequestComplete"));
1159 if (!cache_.get())
1160 return ERR_UNEXPECTED;
1162 // If requested, and we have a readable cache entry, and we have
1163 // an error indicating that we're offline as opposed to in contact
1164 // with a bad server, read from cache anyway.
1165 if (IsOfflineError(result)) {
1166 if (mode_ == READ_WRITE && entry_ && !partial_) {
1167 RecordOfflineStatus(effective_load_flags_,
1168 OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE);
1169 if (effective_load_flags_ & LOAD_FROM_CACHE_IF_OFFLINE) {
1170 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1171 response_.server_data_unavailable = true;
1172 return SetupEntryForRead();
1174 } else {
1175 RecordOfflineStatus(effective_load_flags_,
1176 OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE);
1178 } else {
1179 RecordOfflineStatus(effective_load_flags_,
1180 (result == OK ? OFFLINE_STATUS_NETWORK_SUCCEEDED :
1181 OFFLINE_STATUS_NETWORK_FAILED));
1184 // If we tried to conditionalize the request and failed, we know
1185 // we won't be reading from the cache after this point.
1186 if (couldnt_conditionalize_request_)
1187 mode_ = WRITE;
1189 if (result == OK) {
1190 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
1191 return OK;
1194 // Do not record requests that have network errors or restarts.
1195 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1196 if (IsCertificateError(result)) {
1197 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1198 // If we get a certificate error, then there is a certificate in ssl_info,
1199 // so GetResponseInfo() should never return NULL here.
1200 DCHECK(response);
1201 response_.ssl_info = response->ssl_info;
1202 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1203 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
1204 DCHECK(response);
1205 response_.cert_request_info = response->cert_request_info;
1206 } else if (response_.was_cached) {
1207 DoneWritingToEntry(true);
1209 return result;
1212 // We received the response headers and there is no error.
1213 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1214 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1215 tracked_objects::ScopedTracker tracking_profile(
1216 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1217 "422516 HttpCache::Transaction::DoSuccessfulSendRequest"));
1219 DCHECK(!new_response_);
1220 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1221 bool authentication_failure = false;
1223 if (new_response->headers->response_code() == 401 ||
1224 new_response->headers->response_code() == 407) {
1225 auth_response_ = *new_response;
1226 if (!reading_)
1227 return OK;
1229 // We initiated a second request the caller doesn't know about. We should be
1230 // able to authenticate this request because we should have authenticated
1231 // this URL moments ago.
1232 if (IsReadyToRestartForAuth()) {
1233 DCHECK(!response_.auth_challenge.get());
1234 next_state_ = STATE_SEND_REQUEST_COMPLETE;
1235 // In theory we should check to see if there are new cookies, but there
1236 // is no way to do that from here.
1237 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
1240 // We have to perform cleanup at this point so that at least the next
1241 // request can succeed.
1242 authentication_failure = true;
1243 if (entry_)
1244 DoomPartialEntry(false);
1245 mode_ = NONE;
1246 partial_.reset();
1249 new_response_ = new_response;
1250 if (authentication_failure ||
1251 (!ValidatePartialResponse() && !auth_response_.headers.get())) {
1252 // Something went wrong with this request and we have to restart it.
1253 // If we have an authentication response, we are exposed to weird things
1254 // hapenning if the user cancels the authentication before we receive
1255 // the new response.
1256 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RE_SEND_PARTIAL_REQUEST);
1257 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1258 response_ = HttpResponseInfo();
1259 ResetNetworkTransaction();
1260 new_response_ = NULL;
1261 next_state_ = STATE_SEND_REQUEST;
1262 return OK;
1265 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1266 // We have stored the full entry, but it changed and the server is
1267 // sending a range. We have to delete the old entry.
1268 UpdateTransactionPattern(PATTERN_NOT_COVERED);
1269 DoneWritingToEntry(false);
1272 if (mode_ == WRITE &&
1273 transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1274 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1277 // Invalidate any cached GET with a successful PUT or DELETE.
1278 if (mode_ == WRITE &&
1279 (request_->method == "PUT" || request_->method == "DELETE")) {
1280 if (NonErrorResponse(new_response->headers->response_code())) {
1281 int ret = cache_->DoomEntry(cache_key_, NULL);
1282 DCHECK_EQ(OK, ret);
1284 cache_->DoneWritingToEntry(entry_, true);
1285 entry_ = NULL;
1286 mode_ = NONE;
1289 // Invalidate any cached GET with a successful POST.
1290 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1291 request_->method == "POST" &&
1292 NonErrorResponse(new_response->headers->response_code())) {
1293 cache_->DoomMainEntryForUrl(request_->url);
1296 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1298 if (new_response_->headers->response_code() == 416 &&
1299 (request_->method == "GET" || request_->method == "POST")) {
1300 // If there is an active entry it may be destroyed with this transaction.
1301 response_ = *new_response_;
1302 return OK;
1305 // Are we expecting a response to a conditional query?
1306 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1307 if (new_response->headers->response_code() == 304 || handling_206_) {
1308 UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1309 next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1310 return OK;
1312 UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1313 mode_ = WRITE;
1316 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1317 return OK;
1320 int HttpCache::Transaction::DoNetworkRead() {
1321 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1322 tracked_objects::ScopedTracker tracking_profile(
1323 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1324 "422516 HttpCache::Transaction::DoNetworkRead"));
1326 next_state_ = STATE_NETWORK_READ_COMPLETE;
1327 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1330 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1331 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1332 tracked_objects::ScopedTracker tracking_profile(
1333 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1334 "422516 HttpCache::Transaction::DoNetworkReadComplete"));
1336 DCHECK(mode_ & WRITE || mode_ == NONE);
1338 if (!cache_.get())
1339 return ERR_UNEXPECTED;
1341 // If there is an error or we aren't saving the data, we are done; just wait
1342 // until the destructor runs to see if we can keep the data.
1343 if (mode_ == NONE || result < 0)
1344 return result;
1346 next_state_ = STATE_CACHE_WRITE_DATA;
1347 return result;
1350 int HttpCache::Transaction::DoInitEntry() {
1351 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1352 tracked_objects::ScopedTracker tracking_profile(
1353 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1354 "422516 HttpCache::Transaction::DoInitEntry"));
1356 DCHECK(!new_entry_);
1358 if (!cache_.get())
1359 return ERR_UNEXPECTED;
1361 if (mode_ == WRITE) {
1362 next_state_ = STATE_DOOM_ENTRY;
1363 return OK;
1366 next_state_ = STATE_OPEN_ENTRY;
1367 return OK;
1370 int HttpCache::Transaction::DoOpenEntry() {
1371 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1372 tracked_objects::ScopedTracker tracking_profile(
1373 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1374 "422516 HttpCache::Transaction::DoOpenEntry"));
1376 DCHECK(!new_entry_);
1377 next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1378 cache_pending_ = true;
1379 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1380 first_cache_access_since_ = TimeTicks::Now();
1381 return cache_->OpenEntry(cache_key_, &new_entry_, this);
1384 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1385 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1386 tracked_objects::ScopedTracker tracking_profile(
1387 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1388 "422516 HttpCache::Transaction::DoOpenEntryComplete"));
1390 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1391 // OK, otherwise the cache will end up with an active entry without any
1392 // transaction attached.
1393 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1394 cache_pending_ = false;
1395 if (result == OK) {
1396 next_state_ = STATE_ADD_TO_ENTRY;
1397 return OK;
1400 if (result == ERR_CACHE_RACE) {
1401 next_state_ = STATE_INIT_ENTRY;
1402 return OK;
1405 if (request_->method == "PUT" || request_->method == "DELETE" ||
1406 (request_->method == "HEAD" && mode_ == READ_WRITE)) {
1407 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD");
1408 mode_ = NONE;
1409 next_state_ = STATE_SEND_REQUEST;
1410 return OK;
1413 if (mode_ == READ_WRITE) {
1414 mode_ = WRITE;
1415 next_state_ = STATE_CREATE_ENTRY;
1416 return OK;
1418 if (mode_ == UPDATE) {
1419 // There is no cache entry to update; proceed without caching.
1420 mode_ = NONE;
1421 next_state_ = STATE_SEND_REQUEST;
1422 return OK;
1425 // The entry does not exist, and we are not permitted to create a new entry,
1426 // so we must fail.
1427 return ERR_CACHE_MISS;
1430 int HttpCache::Transaction::DoCreateEntry() {
1431 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1432 tracked_objects::ScopedTracker tracking_profile(
1433 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1434 "422516 HttpCache::Transaction::DoCreateEntry"));
1436 DCHECK(!new_entry_);
1437 next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1438 cache_pending_ = true;
1439 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1440 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1443 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1444 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1445 tracked_objects::ScopedTracker tracking_profile(
1446 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1447 "422516 HttpCache::Transaction::DoCreateEntryComplete"));
1449 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1450 // OK, otherwise the cache will end up with an active entry without any
1451 // transaction attached.
1452 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1453 result);
1454 cache_pending_ = false;
1455 next_state_ = STATE_ADD_TO_ENTRY;
1457 if (result == ERR_CACHE_RACE) {
1458 next_state_ = STATE_INIT_ENTRY;
1459 return OK;
1462 if (result != OK) {
1463 // We have a race here: Maybe we failed to open the entry and decided to
1464 // create one, but by the time we called create, another transaction already
1465 // created the entry. If we want to eliminate this issue, we need an atomic
1466 // OpenOrCreate() method exposed by the disk cache.
1467 DLOG(WARNING) << "Unable to create cache entry";
1468 mode_ = NONE;
1469 if (partial_.get())
1470 partial_->RestoreHeaders(&custom_request_->extra_headers);
1471 next_state_ = STATE_SEND_REQUEST;
1473 return OK;
1476 int HttpCache::Transaction::DoDoomEntry() {
1477 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1478 tracked_objects::ScopedTracker tracking_profile(
1479 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1480 "422516 HttpCache::Transaction::DoDoomEntry"));
1482 next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1483 cache_pending_ = true;
1484 if (first_cache_access_since_.is_null())
1485 first_cache_access_since_ = TimeTicks::Now();
1486 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1487 return cache_->DoomEntry(cache_key_, this);
1490 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1491 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1492 tracked_objects::ScopedTracker tracking_profile(
1493 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1494 "422516 HttpCache::Transaction::DoDoomEntryComplete"));
1496 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1497 next_state_ = STATE_CREATE_ENTRY;
1498 cache_pending_ = false;
1499 if (result == ERR_CACHE_RACE)
1500 next_state_ = STATE_INIT_ENTRY;
1501 return OK;
1504 int HttpCache::Transaction::DoAddToEntry() {
1505 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1506 tracked_objects::ScopedTracker tracking_profile(
1507 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1508 "422516 HttpCache::Transaction::DoAddToEntry"));
1510 DCHECK(new_entry_);
1511 cache_pending_ = true;
1512 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1513 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1514 DCHECK(entry_lock_waiting_since_.is_null());
1515 entry_lock_waiting_since_ = TimeTicks::Now();
1516 int rv = cache_->AddTransactionToEntry(new_entry_, this);
1517 if (rv == ERR_IO_PENDING) {
1518 if (bypass_lock_for_test_) {
1519 OnAddToEntryTimeout(entry_lock_waiting_since_);
1520 } else {
1521 int timeout_milliseconds = 20 * 1000;
1522 if (partial_ && new_entry_->writer &&
1523 new_entry_->writer->range_requested_) {
1524 // Quickly timeout and bypass the cache if we're a range request and
1525 // we're blocked by the reader/writer lock. Doing so eliminates a long
1526 // running issue, http://crbug.com/31014, where two of the same media
1527 // resources could not be played back simultaneously due to one locking
1528 // the cache entry until the entire video was downloaded.
1530 // Bypassing the cache is not ideal, as we are now ignoring the cache
1531 // entirely for all range requests to a resource beyond the first. This
1532 // is however a much more succinct solution than the alternatives, which
1533 // would require somewhat significant changes to the http caching logic.
1535 // Allow some timeout slack for the entry addition to complete in case
1536 // the writer lock is imminently released; we want to avoid skipping
1537 // the cache if at all possible. See http://crbug.com/408765
1538 timeout_milliseconds = 25;
1540 base::MessageLoop::current()->PostDelayedTask(
1541 FROM_HERE,
1542 base::Bind(&HttpCache::Transaction::OnAddToEntryTimeout,
1543 weak_factory_.GetWeakPtr(), entry_lock_waiting_since_),
1544 TimeDelta::FromMilliseconds(timeout_milliseconds));
1547 return rv;
1550 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1551 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1552 tracked_objects::ScopedTracker tracking_profile(
1553 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1554 "422516 HttpCache::Transaction::DoAddToEntryComplete"));
1556 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1557 result);
1558 const TimeDelta entry_lock_wait =
1559 TimeTicks::Now() - entry_lock_waiting_since_;
1560 UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1562 entry_lock_waiting_since_ = TimeTicks();
1563 DCHECK(new_entry_);
1564 cache_pending_ = false;
1566 if (result == OK)
1567 entry_ = new_entry_;
1569 // If there is a failure, the cache should have taken care of new_entry_.
1570 new_entry_ = NULL;
1572 if (result == ERR_CACHE_RACE) {
1573 next_state_ = STATE_INIT_ENTRY;
1574 return OK;
1577 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1578 // The cache is busy, bypass it for this transaction.
1579 mode_ = NONE;
1580 next_state_ = STATE_SEND_REQUEST;
1581 if (partial_) {
1582 partial_->RestoreHeaders(&custom_request_->extra_headers);
1583 partial_.reset();
1585 return OK;
1588 if (result != OK) {
1589 NOTREACHED();
1590 return result;
1593 if (mode_ == WRITE) {
1594 if (partial_.get())
1595 partial_->RestoreHeaders(&custom_request_->extra_headers);
1596 next_state_ = STATE_SEND_REQUEST;
1597 } else {
1598 // We have to read the headers from the cached entry.
1599 DCHECK(mode_ & READ_META);
1600 next_state_ = STATE_CACHE_READ_RESPONSE;
1602 return OK;
1605 // We may end up here multiple times for a given request.
1606 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1607 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1608 tracked_objects::ScopedTracker tracking_profile(
1609 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1610 "422516 HttpCache::Transaction::DoStartPartialCacheValidation"));
1612 if (mode_ == NONE)
1613 return OK;
1615 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1616 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1619 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1620 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1621 tracked_objects::ScopedTracker tracking_profile(
1622 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1623 "422516 HttpCache::Transaction::DoCompletePartialCacheValidation"));
1625 if (!result) {
1626 // This is the end of the request.
1627 if (mode_ & WRITE) {
1628 DoneWritingToEntry(true);
1629 } else {
1630 cache_->DoneReadingFromEntry(entry_, this);
1631 entry_ = NULL;
1633 return result;
1636 if (result < 0)
1637 return result;
1639 partial_->PrepareCacheValidation(entry_->disk_entry,
1640 &custom_request_->extra_headers);
1642 if (reading_ && partial_->IsCurrentRangeCached()) {
1643 next_state_ = STATE_CACHE_READ_DATA;
1644 return OK;
1647 return BeginCacheValidation();
1650 // We received 304 or 206 and we want to update the cached response headers.
1651 int HttpCache::Transaction::DoUpdateCachedResponse() {
1652 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1653 tracked_objects::ScopedTracker tracking_profile(
1654 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1655 "422516 HttpCache::Transaction::DoUpdateCachedResponse"));
1657 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1658 int rv = OK;
1659 // Update cached response based on headers in new_response.
1660 // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1661 response_.headers->Update(*new_response_->headers.get());
1662 response_.response_time = new_response_->response_time;
1663 response_.request_time = new_response_->request_time;
1664 response_.network_accessed = new_response_->network_accessed;
1665 response_.unused_since_prefetch = new_response_->unused_since_prefetch;
1666 if (new_response_->vary_data.is_valid()) {
1667 response_.vary_data = new_response_->vary_data;
1668 } else if (response_.vary_data.is_valid()) {
1669 // There is a vary header in the stored response but not in the current one.
1670 // Update the data with the new request headers.
1671 HttpVaryData new_vary_data;
1672 new_vary_data.Init(*request_, *response_.headers.get());
1673 response_.vary_data = new_vary_data;
1676 if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1677 if (!entry_->doomed) {
1678 int ret = cache_->DoomEntry(cache_key_, NULL);
1679 DCHECK_EQ(OK, ret);
1681 } else {
1682 // If we are already reading, we already updated the headers for this
1683 // request; doing it again will change Content-Length.
1684 if (!reading_) {
1685 target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1686 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1687 rv = OK;
1690 return rv;
1693 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1694 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1695 tracked_objects::ScopedTracker tracking_profile(
1696 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1697 "422516 HttpCache::Transaction::DoUpdateCachedResponseComplete"));
1699 if (mode_ == UPDATE) {
1700 DCHECK(!handling_206_);
1701 // We got a "not modified" response and already updated the corresponding
1702 // cache entry above.
1704 // By closing the cached entry now, we make sure that the 304 rather than
1705 // the cached 200 response, is what will be returned to the user.
1706 DoneWritingToEntry(true);
1707 } else if (entry_ && !handling_206_) {
1708 DCHECK_EQ(READ_WRITE, mode_);
1709 if (!partial_.get() || partial_->IsLastRange()) {
1710 cache_->ConvertWriterToReader(entry_);
1711 mode_ = READ;
1713 // We no longer need the network transaction, so destroy it.
1714 final_upload_progress_ = network_trans_->GetUploadProgress();
1715 ResetNetworkTransaction();
1716 } else if (entry_ && handling_206_ && truncated_ &&
1717 partial_->initial_validation()) {
1718 // We just finished the validation of a truncated entry, and the server
1719 // is willing to resume the operation. Now we go back and start serving
1720 // the first part to the user.
1721 ResetNetworkTransaction();
1722 new_response_ = NULL;
1723 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1724 partial_->SetRangeToStartDownload();
1725 return OK;
1727 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1728 return OK;
1731 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1732 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1733 tracked_objects::ScopedTracker tracking_profile(
1734 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1735 "422516 HttpCache::Transaction::DoOverwriteCachedResponse"));
1737 if (mode_ & READ) {
1738 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1739 return OK;
1742 // We change the value of Content-Length for partial content.
1743 if (handling_206_ && partial_.get())
1744 partial_->FixContentLength(new_response_->headers.get());
1746 response_ = *new_response_;
1748 if (request_->method == "HEAD") {
1749 // This response is replacing the cached one.
1750 DoneWritingToEntry(false);
1751 mode_ = NONE;
1752 new_response_ = NULL;
1753 return OK;
1756 if (handling_206_ && !CanResume(false)) {
1757 // There is no point in storing this resource because it will never be used.
1758 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1759 DoneWritingToEntry(false);
1760 if (partial_.get())
1761 partial_->FixResponseHeaders(response_.headers.get(), true);
1762 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1763 return OK;
1766 target_state_ = STATE_TRUNCATE_CACHED_DATA;
1767 next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1768 STATE_CACHE_WRITE_RESPONSE;
1769 return OK;
1772 int HttpCache::Transaction::DoTruncateCachedData() {
1773 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1774 tracked_objects::ScopedTracker tracking_profile(
1775 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1776 "422516 HttpCache::Transaction::DoTruncateCachedData"));
1778 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1779 if (!entry_)
1780 return OK;
1781 if (net_log_.IsLogging())
1782 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1783 // Truncate the stream.
1784 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1787 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1788 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1789 tracked_objects::ScopedTracker tracking_profile(
1790 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1791 "422516 HttpCache::Transaction::DoTruncateCachedDataComplete"));
1793 if (entry_) {
1794 if (net_log_.IsLogging()) {
1795 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1796 result);
1800 next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1801 return OK;
1804 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1805 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1806 tracked_objects::ScopedTracker tracking_profile(
1807 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1808 "422516 HttpCache::Transaction::DoTruncateCachedMetadata"));
1810 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1811 if (!entry_)
1812 return OK;
1814 if (net_log_.IsLogging())
1815 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1816 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1819 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1820 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1821 tracked_objects::ScopedTracker tracking_profile(
1822 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1823 "422516 HttpCache::Transaction::DoTruncateCachedMetadataComplete"));
1825 if (entry_) {
1826 if (net_log_.IsLogging()) {
1827 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1828 result);
1832 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1833 return OK;
1836 int HttpCache::Transaction::DoPartialHeadersReceived() {
1837 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1838 tracked_objects::ScopedTracker tracking_profile(
1839 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1840 "422516 HttpCache::Transaction::DoPartialHeadersReceived"));
1842 new_response_ = NULL;
1843 if (entry_ && !partial_.get() &&
1844 entry_->disk_entry->GetDataSize(kMetadataIndex))
1845 next_state_ = STATE_CACHE_READ_METADATA;
1847 if (!partial_.get())
1848 return OK;
1850 if (reading_) {
1851 if (network_trans_.get()) {
1852 next_state_ = STATE_NETWORK_READ;
1853 } else {
1854 next_state_ = STATE_CACHE_READ_DATA;
1856 } else if (mode_ != NONE) {
1857 // We are about to return the headers for a byte-range request to the user,
1858 // so let's fix them.
1859 partial_->FixResponseHeaders(response_.headers.get(), true);
1861 return OK;
1864 int HttpCache::Transaction::DoCacheReadResponse() {
1865 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1866 tracked_objects::ScopedTracker tracking_profile(
1867 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1868 "422516 HttpCache::Transaction::DoCacheReadResponse"));
1870 DCHECK(entry_);
1871 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1873 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1874 read_buf_ = new IOBuffer(io_buf_len_);
1876 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1877 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1878 io_buf_len_, io_callback_);
1881 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1882 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1883 tracked_objects::ScopedTracker tracking_profile(
1884 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1885 "422516 HttpCache::Transaction::DoCacheReadResponseComplete"));
1887 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1888 if (result != io_buf_len_ ||
1889 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1890 &response_, &truncated_)) {
1891 return OnCacheReadError(result, true);
1894 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
1895 if (cache_->cert_cache() && response_.ssl_info.is_valid())
1896 ReadCertChain();
1898 // Some resources may have slipped in as truncated when they're not.
1899 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1900 if (response_.headers->GetContentLength() == current_size)
1901 truncated_ = false;
1903 if ((response_.unused_since_prefetch &&
1904 !(request_->load_flags & LOAD_PREFETCH)) ||
1905 (!response_.unused_since_prefetch &&
1906 (request_->load_flags & LOAD_PREFETCH))) {
1907 // Either this is the first use of an entry since it was prefetched or
1908 // this is a prefetch. The value of response.unused_since_prefetch is valid
1909 // for this transaction but the bit needs to be flipped in storage.
1910 next_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH;
1911 return OK;
1914 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1915 return OK;
1918 int HttpCache::Transaction::DoCacheDispatchValidation() {
1919 // We now have access to the cache entry.
1921 // o if we are a reader for the transaction, then we can start reading the
1922 // cache entry.
1924 // o if we can read or write, then we should check if the cache entry needs
1925 // to be validated and then issue a network request if needed or just read
1926 // from the cache if the cache entry is already valid.
1928 // o if we are set to UPDATE, then we are handling an externally
1929 // conditionalized request (if-modified-since / if-none-match). We check
1930 // if the request headers define a validation request.
1932 int result = ERR_FAILED;
1933 switch (mode_) {
1934 case READ:
1935 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1936 result = BeginCacheRead();
1937 break;
1938 case READ_WRITE:
1939 result = BeginPartialCacheValidation();
1940 break;
1941 case UPDATE:
1942 result = BeginExternallyConditionalizedRequest();
1943 break;
1944 case WRITE:
1945 default:
1946 NOTREACHED();
1948 return result;
1951 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetch() {
1952 // Write back the toggled value for the next use of this entry.
1953 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1955 // TODO(jkarlin): If DoUpdateCachedResponse is also called for this
1956 // transaction then metadata will be written to cache twice. If prefetching
1957 // becomes more common, consider combining the writes.
1958 target_state_ = STATE_TOGGLE_UNUSED_SINCE_PREFETCH_COMPLETE;
1959 next_state_ = STATE_CACHE_WRITE_RESPONSE;
1960 return OK;
1963 int HttpCache::Transaction::DoCacheToggleUnusedSincePrefetchComplete(
1964 int result) {
1965 // Restore the original value for this transaction.
1966 response_.unused_since_prefetch = !response_.unused_since_prefetch;
1967 next_state_ = STATE_CACHE_DISPATCH_VALIDATION;
1968 return OK;
1971 int HttpCache::Transaction::DoCacheWriteResponse() {
1972 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1973 tracked_objects::ScopedTracker tracking_profile(
1974 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1975 "422516 HttpCache::Transaction::DoCacheWriteResponse"));
1977 if (entry_) {
1978 if (net_log_.IsLogging())
1979 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1981 return WriteResponseInfoToEntry(false);
1984 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1985 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1986 tracked_objects::ScopedTracker tracking_profile(
1987 FROM_HERE_WITH_EXPLICIT_FUNCTION(
1988 "422516 HttpCache::Transaction::DoCacheWriteTruncatedResponse"));
1990 if (entry_) {
1991 if (net_log_.IsLogging())
1992 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1994 return WriteResponseInfoToEntry(true);
1997 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1998 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
1999 tracked_objects::ScopedTracker tracking_profile(
2000 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2001 "422516 HttpCache::Transaction::DoCacheWriteResponseComplete"));
2003 next_state_ = target_state_;
2004 target_state_ = STATE_NONE;
2005 if (!entry_)
2006 return OK;
2007 if (net_log_.IsLogging()) {
2008 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
2009 result);
2012 // Balance the AddRef from WriteResponseInfoToEntry.
2013 if (result != io_buf_len_) {
2014 DLOG(ERROR) << "failed to write response info to cache";
2015 DoneWritingToEntry(false);
2017 return OK;
2020 int HttpCache::Transaction::DoCacheReadMetadata() {
2021 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2022 tracked_objects::ScopedTracker tracking_profile(
2023 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2024 "422516 HttpCache::Transaction::DoCacheReadMetadata"));
2026 DCHECK(entry_);
2027 DCHECK(!response_.metadata.get());
2028 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
2030 response_.metadata =
2031 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
2033 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
2034 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
2035 response_.metadata.get(),
2036 response_.metadata->size(),
2037 io_callback_);
2040 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
2041 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2042 tracked_objects::ScopedTracker tracking_profile(
2043 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2044 "422516 HttpCache::Transaction::DoCacheReadMetadataComplete"));
2046 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
2047 if (result != response_.metadata->size())
2048 return OnCacheReadError(result, false);
2049 return OK;
2052 int HttpCache::Transaction::DoCacheQueryData() {
2053 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2054 tracked_objects::ScopedTracker tracking_profile(
2055 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2056 "422516 HttpCache::Transaction::DoCacheQueryData"));
2058 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
2059 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
2062 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
2063 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2064 tracked_objects::ScopedTracker tracking_profile(
2065 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2066 "422516 HttpCache::Transaction::DoCacheQueryDataComplete"));
2068 DCHECK_EQ(OK, result);
2069 if (!cache_.get())
2070 return ERR_UNEXPECTED;
2072 return ValidateEntryHeadersAndContinue();
2075 int HttpCache::Transaction::DoCacheReadData() {
2076 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2077 tracked_objects::ScopedTracker tracking_profile(
2078 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2079 "422516 HttpCache::Transaction::DoCacheReadData"));
2081 DCHECK(entry_);
2082 next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
2084 if (net_log_.IsLogging())
2085 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
2086 if (partial_.get()) {
2087 return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
2088 io_callback_);
2091 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
2092 read_buf_.get(), io_buf_len_,
2093 io_callback_);
2096 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
2097 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2098 tracked_objects::ScopedTracker tracking_profile(
2099 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2100 "422516 HttpCache::Transaction::DoCacheReadDataComplete"));
2102 if (net_log_.IsLogging()) {
2103 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
2104 result);
2107 if (!cache_.get())
2108 return ERR_UNEXPECTED;
2110 if (partial_.get()) {
2111 // Partial requests are confusing to report in histograms because they may
2112 // have multiple underlying requests.
2113 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2114 return DoPartialCacheReadCompleted(result);
2117 if (result > 0) {
2118 read_offset_ += result;
2119 } else if (result == 0) { // End of file.
2120 RecordHistograms();
2121 cache_->DoneReadingFromEntry(entry_, this);
2122 entry_ = NULL;
2123 } else {
2124 return OnCacheReadError(result, false);
2126 return result;
2129 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
2130 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2131 tracked_objects::ScopedTracker tracking_profile(
2132 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2133 "422516 HttpCache::Transaction::DoCacheWriteData"));
2135 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
2136 write_len_ = num_bytes;
2137 if (entry_) {
2138 if (net_log_.IsLogging())
2139 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
2142 return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
2145 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
2146 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
2147 tracked_objects::ScopedTracker tracking_profile(
2148 FROM_HERE_WITH_EXPLICIT_FUNCTION(
2149 "422516 HttpCache::Transaction::DoCacheWriteDataComplete"));
2151 if (entry_) {
2152 if (net_log_.IsLogging()) {
2153 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
2154 result);
2157 // Balance the AddRef from DoCacheWriteData.
2158 if (!cache_.get())
2159 return ERR_UNEXPECTED;
2161 if (result != write_len_) {
2162 DLOG(ERROR) << "failed to write response data to cache";
2163 DoneWritingToEntry(false);
2165 // We want to ignore errors writing to disk and just keep reading from
2166 // the network.
2167 result = write_len_;
2168 } else if (!done_reading_ && entry_) {
2169 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2170 int64 body_size = response_.headers->GetContentLength();
2171 if (body_size >= 0 && body_size <= current_size)
2172 done_reading_ = true;
2175 if (partial_.get()) {
2176 // This may be the last request.
2177 if (!(result == 0 && !truncated_ &&
2178 (partial_->IsLastRange() || mode_ == WRITE)))
2179 return DoPartialNetworkReadCompleted(result);
2182 if (result == 0) {
2183 // End of file. This may be the result of a connection problem so see if we
2184 // have to keep the entry around to be flagged as truncated later on.
2185 if (done_reading_ || !entry_ || partial_.get() ||
2186 response_.headers->GetContentLength() <= 0)
2187 DoneWritingToEntry(true);
2190 return result;
2193 //-----------------------------------------------------------------------------
2195 void HttpCache::Transaction::ReadCertChain() {
2196 std::string key =
2197 GetCacheKeyForCert(response_.ssl_info.cert->os_cert_handle());
2198 const X509Certificate::OSCertHandles& intermediates =
2199 response_.ssl_info.cert->GetIntermediateCertificates();
2200 int dist_from_root = intermediates.size();
2202 scoped_refptr<SharedChainData> shared_chain_data(
2203 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
2204 cache_->cert_cache()->GetCertificate(key,
2205 base::Bind(&OnCertReadIOComplete,
2206 dist_from_root,
2207 true /* is leaf */,
2208 shared_chain_data));
2210 for (X509Certificate::OSCertHandles::const_iterator it =
2211 intermediates.begin();
2212 it != intermediates.end();
2213 ++it) {
2214 --dist_from_root;
2215 key = GetCacheKeyForCert(*it);
2216 cache_->cert_cache()->GetCertificate(key,
2217 base::Bind(&OnCertReadIOComplete,
2218 dist_from_root,
2219 false /* is not leaf */,
2220 shared_chain_data));
2222 DCHECK_EQ(0, dist_from_root);
2225 void HttpCache::Transaction::WriteCertChain() {
2226 const X509Certificate::OSCertHandles& intermediates =
2227 response_.ssl_info.cert->GetIntermediateCertificates();
2228 int dist_from_root = intermediates.size();
2230 scoped_refptr<SharedChainData> shared_chain_data(
2231 new SharedChainData(intermediates.size() + 1, TimeTicks::Now()));
2232 cache_->cert_cache()->SetCertificate(
2233 response_.ssl_info.cert->os_cert_handle(),
2234 base::Bind(&OnCertWriteIOComplete,
2235 dist_from_root,
2236 true /* is leaf */,
2237 shared_chain_data));
2238 for (X509Certificate::OSCertHandles::const_iterator it =
2239 intermediates.begin();
2240 it != intermediates.end();
2241 ++it) {
2242 --dist_from_root;
2243 cache_->cert_cache()->SetCertificate(*it,
2244 base::Bind(&OnCertWriteIOComplete,
2245 dist_from_root,
2246 false /* is not leaf */,
2247 shared_chain_data));
2249 DCHECK_EQ(0, dist_from_root);
2252 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
2253 const HttpRequestInfo* request) {
2254 net_log_ = net_log;
2255 request_ = request;
2256 effective_load_flags_ = request_->load_flags;
2258 if (cache_->mode() == DISABLE)
2259 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2261 // Some headers imply load flags. The order here is significant.
2263 // LOAD_DISABLE_CACHE : no cache read or write
2264 // LOAD_BYPASS_CACHE : no cache read
2265 // LOAD_VALIDATE_CACHE : no cache read unless validation
2267 // The former modes trump latter modes, so if we find a matching header we
2268 // can stop iterating kSpecialHeaders.
2270 static const struct {
2271 const HeaderNameAndValue* search;
2272 int load_flag;
2273 } kSpecialHeaders[] = {
2274 { kPassThroughHeaders, LOAD_DISABLE_CACHE },
2275 { kForceFetchHeaders, LOAD_BYPASS_CACHE },
2276 { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
2279 bool range_found = false;
2280 bool external_validation_error = false;
2281 bool special_headers = false;
2283 if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
2284 range_found = true;
2286 for (size_t i = 0; i < arraysize(kSpecialHeaders); ++i) {
2287 if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
2288 effective_load_flags_ |= kSpecialHeaders[i].load_flag;
2289 special_headers = true;
2290 break;
2294 // Check for conditionalization headers which may correspond with a
2295 // cache validation request.
2296 for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
2297 const ValidationHeaderInfo& info = kValidationHeaders[i];
2298 std::string validation_value;
2299 if (request_->extra_headers.GetHeader(
2300 info.request_header_name, &validation_value)) {
2301 if (!external_validation_.values[i].empty() ||
2302 validation_value.empty()) {
2303 external_validation_error = true;
2305 external_validation_.values[i] = validation_value;
2306 external_validation_.initialized = true;
2310 if (range_found || special_headers || external_validation_.initialized) {
2311 // Log the headers before request_ is modified.
2312 std::string empty;
2313 net_log_.AddEvent(
2314 NetLog::TYPE_HTTP_CACHE_CALLER_REQUEST_HEADERS,
2315 base::Bind(&HttpRequestHeaders::NetLogCallback,
2316 base::Unretained(&request_->extra_headers), &empty));
2319 // We don't support ranges and validation headers.
2320 if (range_found && external_validation_.initialized) {
2321 LOG(WARNING) << "Byte ranges AND validation headers found.";
2322 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2325 // If there is more than one validation header, we can't treat this request as
2326 // a cache validation, since we don't know for sure which header the server
2327 // will give us a response for (and they could be contradictory).
2328 if (external_validation_error) {
2329 LOG(WARNING) << "Multiple or malformed validation headers found.";
2330 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2333 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
2334 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2335 partial_.reset(new PartialData);
2336 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
2337 // We will be modifying the actual range requested to the server, so
2338 // let's remove the header here.
2339 custom_request_.reset(new HttpRequestInfo(*request_));
2340 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
2341 request_ = custom_request_.get();
2342 partial_->SetHeaders(custom_request_->extra_headers);
2343 } else {
2344 // The range is invalid or we cannot handle it properly.
2345 VLOG(1) << "Invalid byte range found.";
2346 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2347 partial_.reset(NULL);
2352 bool HttpCache::Transaction::ShouldPassThrough() {
2353 // We may have a null disk_cache if there is an error we cannot recover from,
2354 // like not enough disk space, or sharing violations.
2355 if (!cache_->disk_cache_.get())
2356 return true;
2358 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
2359 return true;
2361 if (request_->method == "GET" || request_->method == "HEAD")
2362 return false;
2364 if (request_->method == "POST" && request_->upload_data_stream &&
2365 request_->upload_data_stream->identifier()) {
2366 return false;
2369 if (request_->method == "PUT" && request_->upload_data_stream)
2370 return false;
2372 if (request_->method == "DELETE")
2373 return false;
2375 return true;
2378 int HttpCache::Transaction::BeginCacheRead() {
2379 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2380 if (response_.headers->response_code() == 206 || partial_.get()) {
2381 NOTREACHED();
2382 return ERR_CACHE_MISS;
2385 if (request_->method == "HEAD")
2386 FixHeadersForHead();
2388 // We don't have the whole resource.
2389 if (truncated_)
2390 return ERR_CACHE_MISS;
2392 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2393 next_state_ = STATE_CACHE_READ_METADATA;
2395 return OK;
2398 int HttpCache::Transaction::BeginCacheValidation() {
2399 DCHECK(mode_ == READ_WRITE);
2401 ValidationType required_validation = RequiresValidation();
2403 bool skip_validation = (required_validation == VALIDATION_NONE);
2405 if (required_validation == VALIDATION_ASYNCHRONOUS &&
2406 !(request_->method == "GET" && (truncated_ || partial_)) && cache_ &&
2407 cache_->use_stale_while_revalidate()) {
2408 TriggerAsyncValidation();
2409 skip_validation = true;
2412 if (request_->method == "HEAD" &&
2413 (truncated_ || response_.headers->response_code() == 206)) {
2414 DCHECK(!partial_);
2415 if (skip_validation)
2416 return SetupEntryForRead();
2418 // Bail out!
2419 next_state_ = STATE_SEND_REQUEST;
2420 mode_ = NONE;
2421 return OK;
2424 if (truncated_) {
2425 // Truncated entries can cause partial gets, so we shouldn't record this
2426 // load in histograms.
2427 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2428 skip_validation = !partial_->initial_validation();
2431 if (partial_.get() && (is_sparse_ || truncated_) &&
2432 (!partial_->IsCurrentRangeCached() || invalid_range_)) {
2433 // Force revalidation for sparse or truncated entries. Note that we don't
2434 // want to ignore the regular validation logic just because a byte range was
2435 // part of the request.
2436 skip_validation = false;
2439 if (skip_validation) {
2440 // TODO(ricea): Is this pattern okay for asynchronous revalidations?
2441 UpdateTransactionPattern(PATTERN_ENTRY_USED);
2442 RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE);
2443 return SetupEntryForRead();
2444 } else {
2445 // Make the network request conditional, to see if we may reuse our cached
2446 // response. If we cannot do so, then we just resort to a normal fetch.
2447 // Our mode remains READ_WRITE for a conditional request. Even if the
2448 // conditionalization fails, we don't switch to WRITE mode until we
2449 // know we won't be falling back to using the cache entry in the
2450 // LOAD_FROM_CACHE_IF_OFFLINE case.
2451 if (!ConditionalizeRequest()) {
2452 couldnt_conditionalize_request_ = true;
2453 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
2454 if (partial_.get())
2455 return DoRestartPartialRequest();
2457 DCHECK_NE(206, response_.headers->response_code());
2459 next_state_ = STATE_SEND_REQUEST;
2461 return OK;
2464 int HttpCache::Transaction::BeginPartialCacheValidation() {
2465 DCHECK(mode_ == READ_WRITE);
2467 if (response_.headers->response_code() != 206 && !partial_.get() &&
2468 !truncated_) {
2469 return BeginCacheValidation();
2472 // Partial requests should not be recorded in histograms.
2473 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2474 if (range_requested_) {
2475 next_state_ = STATE_CACHE_QUERY_DATA;
2476 return OK;
2479 // The request is not for a range, but we have stored just ranges.
2481 if (request_->method == "HEAD")
2482 return BeginCacheValidation();
2484 partial_.reset(new PartialData());
2485 partial_->SetHeaders(request_->extra_headers);
2486 if (!custom_request_.get()) {
2487 custom_request_.reset(new HttpRequestInfo(*request_));
2488 request_ = custom_request_.get();
2491 return ValidateEntryHeadersAndContinue();
2494 // This should only be called once per request.
2495 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
2496 DCHECK(mode_ == READ_WRITE);
2498 if (!partial_->UpdateFromStoredHeaders(
2499 response_.headers.get(), entry_->disk_entry, truncated_)) {
2500 return DoRestartPartialRequest();
2503 if (response_.headers->response_code() == 206)
2504 is_sparse_ = true;
2506 if (!partial_->IsRequestedRangeOK()) {
2507 // The stored data is fine, but the request may be invalid.
2508 invalid_range_ = true;
2511 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2512 return OK;
2515 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
2516 DCHECK_EQ(UPDATE, mode_);
2517 DCHECK(external_validation_.initialized);
2519 for (size_t i = 0; i < arraysize(kValidationHeaders); i++) {
2520 if (external_validation_.values[i].empty())
2521 continue;
2522 // Retrieve either the cached response's "etag" or "last-modified" header.
2523 std::string validator;
2524 response_.headers->EnumerateHeader(
2525 NULL,
2526 kValidationHeaders[i].related_response_header_name,
2527 &validator);
2529 if (response_.headers->response_code() != 200 || truncated_ ||
2530 validator.empty() || validator != external_validation_.values[i]) {
2531 // The externally conditionalized request is not a validation request
2532 // for our existing cache entry. Proceed with caching disabled.
2533 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2534 DoneWritingToEntry(true);
2538 // TODO(ricea): This calculation is expensive to perform just to collect
2539 // statistics. Either remove it or use the result, depending on the result of
2540 // the experiment.
2541 ExternallyConditionalizedType type =
2542 EXTERNALLY_CONDITIONALIZED_CACHE_USABLE;
2543 if (mode_ == NONE)
2544 type = EXTERNALLY_CONDITIONALIZED_MISMATCHED_VALIDATORS;
2545 else if (RequiresValidation())
2546 type = EXTERNALLY_CONDITIONALIZED_CACHE_REQUIRES_VALIDATION;
2548 // TODO(ricea): Add CACHE_USABLE_STALE once stale-while-revalidate CL landed.
2549 // TODO(ricea): Either remove this histogram or make it permanent by M40.
2550 UMA_HISTOGRAM_ENUMERATION("HttpCache.ExternallyConditionalized",
2551 type,
2552 EXTERNALLY_CONDITIONALIZED_MAX);
2554 next_state_ = STATE_SEND_REQUEST;
2555 return OK;
2558 int HttpCache::Transaction::RestartNetworkRequest() {
2559 DCHECK(mode_ & WRITE || mode_ == NONE);
2560 DCHECK(network_trans_.get());
2561 DCHECK_EQ(STATE_NONE, next_state_);
2563 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2564 int rv = network_trans_->RestartIgnoringLastError(io_callback_);
2565 if (rv != ERR_IO_PENDING)
2566 return DoLoop(rv);
2567 return rv;
2570 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
2571 X509Certificate* client_cert) {
2572 DCHECK(mode_ & WRITE || mode_ == NONE);
2573 DCHECK(network_trans_.get());
2574 DCHECK_EQ(STATE_NONE, next_state_);
2576 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2577 int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
2578 if (rv != ERR_IO_PENDING)
2579 return DoLoop(rv);
2580 return rv;
2583 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
2584 const AuthCredentials& credentials) {
2585 DCHECK(mode_ & WRITE || mode_ == NONE);
2586 DCHECK(network_trans_.get());
2587 DCHECK_EQ(STATE_NONE, next_state_);
2589 next_state_ = STATE_SEND_REQUEST_COMPLETE;
2590 int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
2591 if (rv != ERR_IO_PENDING)
2592 return DoLoop(rv);
2593 return rv;
2596 ValidationType HttpCache::Transaction::RequiresValidation() {
2597 // TODO(darin): need to do more work here:
2598 // - make sure we have a matching request method
2599 // - watch out for cached responses that depend on authentication
2601 if (response_.vary_data.is_valid() &&
2602 !response_.vary_data.MatchesRequest(*request_,
2603 *response_.headers.get())) {
2604 vary_mismatch_ = true;
2605 return VALIDATION_SYNCHRONOUS;
2608 if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
2609 return VALIDATION_NONE;
2611 if (response_.unused_since_prefetch &&
2612 !(effective_load_flags_ & LOAD_PREFETCH) &&
2613 response_.headers->GetCurrentAge(
2614 response_.request_time, response_.response_time,
2615 cache_->clock_->Now()) < TimeDelta::FromMinutes(kPrefetchReuseMins)) {
2616 // The first use of a resource after prefetch within a short window skips
2617 // validation.
2618 return VALIDATION_NONE;
2621 if (effective_load_flags_ & (LOAD_VALIDATE_CACHE | LOAD_ASYNC_REVALIDATION))
2622 return VALIDATION_SYNCHRONOUS;
2624 if (request_->method == "PUT" || request_->method == "DELETE")
2625 return VALIDATION_SYNCHRONOUS;
2627 ValidationType validation_required_by_headers =
2628 response_.headers->RequiresValidation(response_.request_time,
2629 response_.response_time,
2630 cache_->clock_->Now());
2632 if (validation_required_by_headers == VALIDATION_ASYNCHRONOUS) {
2633 // Asynchronous revalidation is only supported for GET and HEAD methods.
2634 if (request_->method != "GET" && request_->method != "HEAD")
2635 return VALIDATION_SYNCHRONOUS;
2638 return validation_required_by_headers;
2641 bool HttpCache::Transaction::ConditionalizeRequest() {
2642 DCHECK(response_.headers.get());
2644 if (request_->method == "PUT" || request_->method == "DELETE")
2645 return false;
2647 // This only makes sense for cached 200 or 206 responses.
2648 if (response_.headers->response_code() != 200 &&
2649 response_.headers->response_code() != 206) {
2650 return false;
2653 if (fail_conditionalization_for_test_)
2654 return false;
2656 DCHECK(response_.headers->response_code() != 206 ||
2657 response_.headers->HasStrongValidators());
2659 // Just use the first available ETag and/or Last-Modified header value.
2660 // TODO(darin): Or should we use the last?
2662 std::string etag_value;
2663 if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2664 response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2666 std::string last_modified_value;
2667 if (!vary_mismatch_) {
2668 response_.headers->EnumerateHeader(NULL, "last-modified",
2669 &last_modified_value);
2672 if (etag_value.empty() && last_modified_value.empty())
2673 return false;
2675 if (!partial_.get()) {
2676 // Need to customize the request, so this forces us to allocate :(
2677 custom_request_.reset(new HttpRequestInfo(*request_));
2678 request_ = custom_request_.get();
2680 DCHECK(custom_request_.get());
2682 bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2683 !invalid_range_;
2685 if (!use_if_range) {
2686 // stale-while-revalidate is not useful when we only have a partial response
2687 // cached, so don't set the header in that case.
2688 HttpResponseHeaders::FreshnessLifetimes lifetimes =
2689 response_.headers->GetFreshnessLifetimes(response_.response_time);
2690 if (lifetimes.staleness > TimeDelta()) {
2691 TimeDelta current_age = response_.headers->GetCurrentAge(
2692 response_.request_time, response_.response_time,
2693 cache_->clock_->Now());
2695 custom_request_->extra_headers.SetHeader(
2696 kFreshnessHeader,
2697 base::StringPrintf("max-age=%" PRId64
2698 ",stale-while-revalidate=%" PRId64 ",age=%" PRId64,
2699 lifetimes.freshness.InSeconds(),
2700 lifetimes.staleness.InSeconds(),
2701 current_age.InSeconds()));
2705 if (!etag_value.empty()) {
2706 if (use_if_range) {
2707 // We don't want to switch to WRITE mode if we don't have this block of a
2708 // byte-range request because we may have other parts cached.
2709 custom_request_->extra_headers.SetHeader(
2710 HttpRequestHeaders::kIfRange, etag_value);
2711 } else {
2712 custom_request_->extra_headers.SetHeader(
2713 HttpRequestHeaders::kIfNoneMatch, etag_value);
2715 // For byte-range requests, make sure that we use only one way to validate
2716 // the request.
2717 if (partial_.get() && !partial_->IsCurrentRangeCached())
2718 return true;
2721 if (!last_modified_value.empty()) {
2722 if (use_if_range) {
2723 custom_request_->extra_headers.SetHeader(
2724 HttpRequestHeaders::kIfRange, last_modified_value);
2725 } else {
2726 custom_request_->extra_headers.SetHeader(
2727 HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2731 return true;
2734 // We just received some headers from the server. We may have asked for a range,
2735 // in which case partial_ has an object. This could be the first network request
2736 // we make to fulfill the original request, or we may be already reading (from
2737 // the net and / or the cache). If we are not expecting a certain response, we
2738 // just bypass the cache for this request (but again, maybe we are reading), and
2739 // delete partial_ (so we are not able to "fix" the headers that we return to
2740 // the user). This results in either a weird response for the caller (we don't
2741 // expect it after all), or maybe a range that was not exactly what it was asked
2742 // for.
2744 // If the server is simply telling us that the resource has changed, we delete
2745 // the cached entry and restart the request as the caller intended (by returning
2746 // false from this method). However, we may not be able to do that at any point,
2747 // for instance if we already returned the headers to the user.
2749 // WARNING: Whenever this code returns false, it has to make sure that the next
2750 // time it is called it will return true so that we don't keep retrying the
2751 // request.
2752 bool HttpCache::Transaction::ValidatePartialResponse() {
2753 const HttpResponseHeaders* headers = new_response_->headers.get();
2754 int response_code = headers->response_code();
2755 bool partial_response = (response_code == 206);
2756 handling_206_ = false;
2758 if (!entry_ || request_->method != "GET")
2759 return true;
2761 if (invalid_range_) {
2762 // We gave up trying to match this request with the stored data. If the
2763 // server is ok with the request, delete the entry, otherwise just ignore
2764 // this request
2765 DCHECK(!reading_);
2766 if (partial_response || response_code == 200) {
2767 DoomPartialEntry(true);
2768 mode_ = NONE;
2769 } else {
2770 if (response_code == 304)
2771 FailRangeRequest();
2772 IgnoreRangeRequest();
2774 return true;
2777 if (!partial_.get()) {
2778 // We are not expecting 206 but we may have one.
2779 if (partial_response)
2780 IgnoreRangeRequest();
2782 return true;
2785 // TODO(rvargas): Do we need to consider other results here?.
2786 bool failure = response_code == 200 || response_code == 416;
2788 if (partial_->IsCurrentRangeCached()) {
2789 // We asked for "If-None-Match: " so a 206 means a new object.
2790 if (partial_response)
2791 failure = true;
2793 if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2794 return true;
2795 } else {
2796 // We asked for "If-Range: " so a 206 means just another range.
2797 if (partial_response) {
2798 if (partial_->ResponseHeadersOK(headers)) {
2799 handling_206_ = true;
2800 return true;
2801 } else {
2802 failure = true;
2806 if (!reading_ && !is_sparse_ && !partial_response) {
2807 // See if we can ignore the fact that we issued a byte range request.
2808 // If the server sends 200, just store it. If it sends an error, redirect
2809 // or something else, we may store the response as long as we didn't have
2810 // anything already stored.
2811 if (response_code == 200 ||
2812 (!truncated_ && response_code != 304 && response_code != 416)) {
2813 // The server is sending something else, and we can save it.
2814 DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2815 partial_.reset();
2816 truncated_ = false;
2817 return true;
2821 // 304 is not expected here, but we'll spare the entry (unless it was
2822 // truncated).
2823 if (truncated_)
2824 failure = true;
2827 if (failure) {
2828 // We cannot truncate this entry, it has to be deleted.
2829 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2830 mode_ = NONE;
2831 if (is_sparse_ || truncated_) {
2832 // There was something cached to start with, either sparsed data (206), or
2833 // a truncated 200, which means that we probably modified the request,
2834 // adding a byte range or modifying the range requested by the caller.
2835 if (!reading_ && !partial_->IsLastRange()) {
2836 // We have not returned anything to the caller yet so it should be safe
2837 // to issue another network request, this time without us messing up the
2838 // headers.
2839 ResetPartialState(true);
2840 return false;
2842 LOG(WARNING) << "Failed to revalidate partial entry";
2844 DoomPartialEntry(true);
2845 return true;
2848 IgnoreRangeRequest();
2849 return true;
2852 void HttpCache::Transaction::IgnoreRangeRequest() {
2853 // We have a problem. We may or may not be reading already (in which case we
2854 // returned the headers), but we'll just pretend that this request is not
2855 // using the cache and see what happens. Most likely this is the first
2856 // response from the server (it's not changing its mind midway, right?).
2857 UpdateTransactionPattern(PATTERN_NOT_COVERED);
2858 if (mode_ & WRITE)
2859 DoneWritingToEntry(mode_ != WRITE);
2860 else if (mode_ & READ && entry_)
2861 cache_->DoneReadingFromEntry(entry_, this);
2863 partial_.reset(NULL);
2864 entry_ = NULL;
2865 mode_ = NONE;
2868 void HttpCache::Transaction::FixHeadersForHead() {
2869 if (response_.headers->response_code() == 206) {
2870 response_.headers->RemoveHeader("Content-Range");
2871 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK");
2875 void HttpCache::Transaction::TriggerAsyncValidation() {
2876 DCHECK(!request_->upload_data_stream);
2877 BoundNetLog async_revalidation_net_log(
2878 BoundNetLog::Make(net_log_.net_log(), NetLog::SOURCE_ASYNC_REVALIDATION));
2879 net_log_.AddEvent(
2880 NetLog::TYPE_HTTP_CACHE_VALIDATE_RESOURCE_ASYNC,
2881 async_revalidation_net_log.source().ToEventParametersCallback());
2882 async_revalidation_net_log.BeginEvent(
2883 NetLog::TYPE_ASYNC_REVALIDATION,
2884 base::Bind(
2885 &NetLogAsyncRevalidationInfoCallback, net_log_.source(), request_));
2886 base::MessageLoop::current()->PostTask(
2887 FROM_HERE,
2888 base::Bind(&HttpCache::PerformAsyncValidation,
2889 cache_, // cache_ is a weak pointer.
2890 *request_,
2891 async_revalidation_net_log));
2894 void HttpCache::Transaction::FailRangeRequest() {
2895 response_ = *new_response_;
2896 partial_->FixResponseHeaders(response_.headers.get(), false);
2899 int HttpCache::Transaction::SetupEntryForRead() {
2900 if (network_trans_)
2901 ResetNetworkTransaction();
2902 if (partial_.get()) {
2903 if (truncated_ || is_sparse_ || !invalid_range_) {
2904 // We are going to return the saved response headers to the caller, so
2905 // we may need to adjust them first.
2906 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2907 return OK;
2908 } else {
2909 partial_.reset();
2912 cache_->ConvertWriterToReader(entry_);
2913 mode_ = READ;
2915 if (request_->method == "HEAD")
2916 FixHeadersForHead();
2918 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2919 next_state_ = STATE_CACHE_READ_METADATA;
2920 return OK;
2924 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2925 read_buf_ = data;
2926 io_buf_len_ = data_len;
2927 next_state_ = STATE_NETWORK_READ;
2928 return DoLoop(OK);
2931 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2932 if (request_->method == "HEAD")
2933 return 0;
2935 read_buf_ = data;
2936 io_buf_len_ = data_len;
2937 next_state_ = STATE_CACHE_READ_DATA;
2938 return DoLoop(OK);
2941 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2942 IOBuffer* data, int data_len,
2943 const CompletionCallback& callback) {
2944 if (!entry_)
2945 return data_len;
2947 int rv = 0;
2948 if (!partial_.get() || !data_len) {
2949 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2950 true);
2951 } else {
2952 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2954 return rv;
2957 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2958 next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2959 if (!entry_)
2960 return OK;
2962 // Do not cache no-store content. Do not cache content with cert errors
2963 // either. This is to prevent not reporting net errors when loading a
2964 // resource from the cache. When we load a page over HTTPS with a cert error
2965 // we show an SSL blocking page. If the user clicks proceed we reload the
2966 // resource ignoring the errors. The loaded resource is then cached. If that
2967 // resource is subsequently loaded from the cache, no net error is reported
2968 // (even though the cert status contains the actual errors) and no SSL
2969 // blocking page is shown. An alternative would be to reverse-map the cert
2970 // status to a net error and replay the net error.
2971 if ((response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2972 net::IsCertStatusError(response_.ssl_info.cert_status)) {
2973 DoneWritingToEntry(false);
2974 if (net_log_.IsLogging())
2975 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2976 return OK;
2979 // cert_cache() will be null if the CertCacheTrial field trial is disabled.
2980 if (cache_->cert_cache() && response_.ssl_info.is_valid())
2981 WriteCertChain();
2983 if (truncated)
2984 DCHECK_EQ(200, response_.headers->response_code());
2986 // When writing headers, we normally only write the non-transient headers.
2987 bool skip_transient_headers = true;
2988 scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2989 response_.Persist(data->pickle(), skip_transient_headers, truncated);
2990 data->Done();
2992 io_buf_len_ = data->pickle()->size();
2993 return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2994 io_buf_len_, io_callback_, true);
2997 int HttpCache::Transaction::AppendResponseDataToEntry(
2998 IOBuffer* data, int data_len, const CompletionCallback& callback) {
2999 if (!entry_ || !data_len)
3000 return data_len;
3002 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
3003 return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
3004 callback);
3007 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
3008 if (!entry_)
3009 return;
3011 RecordHistograms();
3013 cache_->DoneWritingToEntry(entry_, success);
3014 entry_ = NULL;
3015 mode_ = NONE; // switch to 'pass through' mode
3018 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
3019 DLOG(ERROR) << "ReadData failed: " << result;
3020 const int result_for_histogram = std::max(0, -result);
3021 if (restart) {
3022 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
3023 result_for_histogram);
3024 } else {
3025 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
3026 result_for_histogram);
3029 // Avoid using this entry in the future.
3030 if (cache_.get())
3031 cache_->DoomActiveEntry(cache_key_);
3033 if (restart) {
3034 DCHECK(!reading_);
3035 DCHECK(!network_trans_.get());
3036 cache_->DoneWithEntry(entry_, this, false);
3037 entry_ = NULL;
3038 is_sparse_ = false;
3039 partial_.reset();
3040 next_state_ = STATE_GET_BACKEND;
3041 return OK;
3044 return ERR_CACHE_READ_FAILURE;
3047 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) {
3048 if (entry_lock_waiting_since_ != start_time)
3049 return;
3051 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE);
3053 if (!cache_)
3054 return;
3056 cache_->RemovePendingTransaction(this);
3057 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT);
3060 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
3061 DVLOG(2) << "DoomPartialEntry";
3062 int rv = cache_->DoomEntry(cache_key_, NULL);
3063 DCHECK_EQ(OK, rv);
3064 cache_->DoneWithEntry(entry_, this, false);
3065 entry_ = NULL;
3066 is_sparse_ = false;
3067 truncated_ = false;
3068 if (delete_object)
3069 partial_.reset(NULL);
3072 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
3073 partial_->OnNetworkReadCompleted(result);
3075 if (result == 0) {
3076 // We need to move on to the next range.
3077 ResetNetworkTransaction();
3078 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
3080 return result;
3083 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
3084 partial_->OnCacheReadCompleted(result);
3086 if (result == 0 && mode_ == READ_WRITE) {
3087 // We need to move on to the next range.
3088 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
3089 } else if (result < 0) {
3090 return OnCacheReadError(result, false);
3092 return result;
3095 int HttpCache::Transaction::DoRestartPartialRequest() {
3096 // The stored data cannot be used. Get rid of it and restart this request.
3097 net_log_.AddEvent(NetLog::TYPE_HTTP_CACHE_RESTART_PARTIAL_REQUEST);
3099 // WRITE + Doom + STATE_INIT_ENTRY == STATE_CREATE_ENTRY (without an attempt
3100 // to Doom the entry again).
3101 mode_ = WRITE;
3102 ResetPartialState(!range_requested_);
3103 next_state_ = STATE_CREATE_ENTRY;
3104 return OK;
3107 void HttpCache::Transaction::ResetPartialState(bool delete_object) {
3108 partial_->RestoreHeaders(&custom_request_->extra_headers);
3109 DoomPartialEntry(delete_object);
3111 if (!delete_object) {
3112 // The simplest way to re-initialize partial_ is to create a new object.
3113 partial_.reset(new PartialData());
3114 if (partial_->Init(request_->extra_headers))
3115 partial_->SetHeaders(custom_request_->extra_headers);
3116 else
3117 partial_.reset();
3121 void HttpCache::Transaction::ResetNetworkTransaction() {
3122 DCHECK(!old_network_trans_load_timing_);
3123 DCHECK(network_trans_);
3124 LoadTimingInfo load_timing;
3125 if (network_trans_->GetLoadTimingInfo(&load_timing))
3126 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
3127 total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
3128 network_trans_.reset();
3131 // Histogram data from the end of 2010 show the following distribution of
3132 // response headers:
3134 // Content-Length............... 87%
3135 // Date......................... 98%
3136 // Last-Modified................ 49%
3137 // Etag......................... 19%
3138 // Accept-Ranges: bytes......... 25%
3139 // Accept-Ranges: none.......... 0.4%
3140 // Strong Validator............. 50%
3141 // Strong Validator + ranges.... 24%
3142 // Strong Validator + CL........ 49%
3144 bool HttpCache::Transaction::CanResume(bool has_data) {
3145 // Double check that there is something worth keeping.
3146 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
3147 return false;
3149 if (request_->method != "GET")
3150 return false;
3152 // Note that if this is a 206, content-length was already fixed after calling
3153 // PartialData::ResponseHeadersOK().
3154 if (response_.headers->GetContentLength() <= 0 ||
3155 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
3156 !response_.headers->HasStrongValidators()) {
3157 return false;
3160 return true;
3163 void HttpCache::Transaction::UpdateTransactionPattern(
3164 TransactionPattern new_transaction_pattern) {
3165 if (transaction_pattern_ == PATTERN_NOT_COVERED)
3166 return;
3167 DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
3168 new_transaction_pattern == PATTERN_NOT_COVERED);
3169 transaction_pattern_ = new_transaction_pattern;
3172 void HttpCache::Transaction::RecordHistograms() {
3173 DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
3174 if (!cache_.get() || !cache_->GetCurrentBackend() ||
3175 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
3176 cache_->mode() != NORMAL || request_->method != "GET") {
3177 return;
3179 UMA_HISTOGRAM_ENUMERATION(
3180 "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
3181 if (transaction_pattern_ == PATTERN_NOT_COVERED)
3182 return;
3183 DCHECK(!range_requested_);
3184 DCHECK(!first_cache_access_since_.is_null());
3186 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
3188 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
3190 bool did_send_request = !send_request_since_.is_null();
3191 DCHECK(
3192 (did_send_request &&
3193 (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
3194 transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
3195 transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
3196 transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
3197 (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
3199 if (!did_send_request) {
3200 DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
3201 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
3202 return;
3205 TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
3206 int64 before_send_percent = (total_time.ToInternalValue() == 0) ?
3207 0 : before_send_time * 100 / total_time;
3208 DCHECK_GE(before_send_percent, 0);
3209 DCHECK_LE(before_send_percent, 100);
3210 base::HistogramBase::Sample before_send_sample =
3211 static_cast<base::HistogramBase::Sample>(before_send_percent);
3213 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
3214 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
3215 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_sample);
3217 // TODO(gavinp): Remove or minimize these histograms, particularly the ones
3218 // below this comment after we have received initial data.
3219 switch (transaction_pattern_) {
3220 case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
3221 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
3222 before_send_time);
3223 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
3224 before_send_sample);
3225 break;
3227 case PATTERN_ENTRY_NOT_CACHED: {
3228 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
3229 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
3230 before_send_sample);
3231 break;
3233 case PATTERN_ENTRY_VALIDATED: {
3234 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
3235 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
3236 before_send_sample);
3237 break;
3239 case PATTERN_ENTRY_UPDATED: {
3240 UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
3241 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
3242 before_send_sample);
3243 break;
3245 default:
3246 NOTREACHED();
3250 void HttpCache::Transaction::OnIOComplete(int result) {
3251 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
3252 tracked_objects::ScopedTracker tracking_profile(
3253 FROM_HERE_WITH_EXPLICIT_FUNCTION("422516 Transaction::OnIOComplete"));
3255 DoLoop(result);
3258 } // namespace net