Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / net / http / http_cache_transaction.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/http/http_cache_transaction.h"
6
7 #include "build/build_config.h"
8
9 #if defined(OS_POSIX)
10 #include <unistd.h>
11 #endif
12
13 #include <algorithm>
14 #include <string>
15
16 #include "base/bind.h"
17 #include "base/compiler_specific.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/metrics/field_trial.h"
20 #include "base/metrics/histogram.h"
21 #include "base/metrics/sparse_histogram.h"
22 #include "base/rand_util.h"
23 #include "base/strings/string_number_conversions.h"
24 #include "base/strings/string_util.h"
25 #include "base/time/time.h"
26 #include "net/base/completion_callback.h"
27 #include "net/base/io_buffer.h"
28 #include "net/base/load_flags.h"
29 #include "net/base/load_timing_info.h"
30 #include "net/base/net_errors.h"
31 #include "net/base/net_log.h"
32 #include "net/base/upload_data_stream.h"
33 #include "net/cert/cert_status_flags.h"
34 #include "net/disk_cache/disk_cache.h"
35 #include "net/http/http_network_session.h"
36 #include "net/http/http_request_info.h"
37 #include "net/http/http_response_headers.h"
38 #include "net/http/http_transaction.h"
39 #include "net/http/http_util.h"
40 #include "net/http/partial_data.h"
41 #include "net/ssl/ssl_cert_request_info.h"
42 #include "net/ssl/ssl_config_service.h"
43
44 using base::Time;
45 using base::TimeDelta;
46 using base::TimeTicks;
47
48 namespace {
49
50 // From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
51 //      a "non-error response" is one with a 2xx (Successful) or 3xx
52 //      (Redirection) status code.
53 bool NonErrorResponse(int status_code) {
54   int status_code_range = status_code / 100;
55   return status_code_range == 2 || status_code_range == 3;
56 }
57
58 // Error codes that will be considered indicative of a page being offline/
59 // unreachable for LOAD_FROM_CACHE_IF_OFFLINE.
60 bool IsOfflineError(int error) {
61   return (error == net::ERR_NAME_NOT_RESOLVED ||
62           error == net::ERR_INTERNET_DISCONNECTED ||
63           error == net::ERR_ADDRESS_UNREACHABLE ||
64           error == net::ERR_CONNECTION_TIMED_OUT);
65 }
66
67 // Enum for UMA, indicating the status (with regard to offline mode) of
68 // a particular request.
69 enum RequestOfflineStatus {
70   // A cache transaction hit in cache (data was present and not stale)
71   // and returned it.
72   OFFLINE_STATUS_FRESH_CACHE,
73
74   // A network request was required for a cache entry, and it succeeded.
75   OFFLINE_STATUS_NETWORK_SUCCEEDED,
76
77   // A network request was required for a cache entry, and it failed with
78   // a non-offline error.
79   OFFLINE_STATUS_NETWORK_FAILED,
80
81   // A network request was required for a cache entry, it failed with an
82   // offline error, and we could serve stale data if
83   // LOAD_FROM_CACHE_IF_OFFLINE was set.
84   OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE,
85
86   // A network request was required for a cache entry, it failed with
87   // an offline error, and there was no servable data in cache (even
88   // stale data).
89   OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE,
90
91   OFFLINE_STATUS_MAX_ENTRIES
92 };
93
94 void RecordOfflineStatus(int load_flags, RequestOfflineStatus status) {
95   // Restrict to main frame to keep statistics close to
96   // "would have shown them something useful if offline mode was enabled".
97   if (load_flags & net::LOAD_MAIN_FRAME) {
98     UMA_HISTOGRAM_ENUMERATION("HttpCache.OfflineStatus", status,
99                               OFFLINE_STATUS_MAX_ENTRIES);
100   }
101 }
102
103 // TODO(rvargas): Remove once we get the data.
104 void RecordVaryHeaderHistogram(const net::HttpResponseInfo* response) {
105   enum VaryType {
106     VARY_NOT_PRESENT,
107     VARY_UA,
108     VARY_OTHER,
109     VARY_MAX
110   };
111   VaryType vary = VARY_NOT_PRESENT;
112   if (response->vary_data.is_valid()) {
113     vary = VARY_OTHER;
114     if (response->headers->HasHeaderValue("vary", "user-agent"))
115       vary = VARY_UA;
116   }
117   UMA_HISTOGRAM_ENUMERATION("HttpCache.Vary", vary, VARY_MAX);
118 }
119
120 }  // namespace
121
122 namespace net {
123
124 struct HeaderNameAndValue {
125   const char* name;
126   const char* value;
127 };
128
129 // If the request includes one of these request headers, then avoid caching
130 // to avoid getting confused.
131 static const HeaderNameAndValue kPassThroughHeaders[] = {
132   { "if-unmodified-since", NULL },  // causes unexpected 412s
133   { "if-match", NULL },             // causes unexpected 412s
134   { "if-range", NULL },
135   { NULL, NULL }
136 };
137
138 struct ValidationHeaderInfo {
139   const char* request_header_name;
140   const char* related_response_header_name;
141 };
142
143 static const ValidationHeaderInfo kValidationHeaders[] = {
144   { "if-modified-since", "last-modified" },
145   { "if-none-match", "etag" },
146 };
147
148 // If the request includes one of these request headers, then avoid reusing
149 // our cached copy if any.
150 static const HeaderNameAndValue kForceFetchHeaders[] = {
151   { "cache-control", "no-cache" },
152   { "pragma", "no-cache" },
153   { NULL, NULL }
154 };
155
156 // If the request includes one of these request headers, then force our
157 // cached copy (if any) to be revalidated before reusing it.
158 static const HeaderNameAndValue kForceValidateHeaders[] = {
159   { "cache-control", "max-age=0" },
160   { NULL, NULL }
161 };
162
163 static bool HeaderMatches(const HttpRequestHeaders& headers,
164                           const HeaderNameAndValue* search) {
165   for (; search->name; ++search) {
166     std::string header_value;
167     if (!headers.GetHeader(search->name, &header_value))
168       continue;
169
170     if (!search->value)
171       return true;
172
173     HttpUtil::ValuesIterator v(header_value.begin(), header_value.end(), ',');
174     while (v.GetNext()) {
175       if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value))
176         return true;
177     }
178   }
179   return false;
180 }
181
182 //-----------------------------------------------------------------------------
183
184 HttpCache::Transaction::Transaction(
185     RequestPriority priority,
186     HttpCache* cache)
187     : next_state_(STATE_NONE),
188       request_(NULL),
189       priority_(priority),
190       cache_(cache->GetWeakPtr()),
191       entry_(NULL),
192       new_entry_(NULL),
193       new_response_(NULL),
194       mode_(NONE),
195       target_state_(STATE_NONE),
196       reading_(false),
197       invalid_range_(false),
198       truncated_(false),
199       is_sparse_(false),
200       range_requested_(false),
201       handling_206_(false),
202       cache_pending_(false),
203       done_reading_(false),
204       vary_mismatch_(false),
205       couldnt_conditionalize_request_(false),
206       io_buf_len_(0),
207       read_offset_(0),
208       effective_load_flags_(0),
209       write_len_(0),
210       weak_factory_(this),
211       io_callback_(base::Bind(&Transaction::OnIOComplete,
212                               weak_factory_.GetWeakPtr())),
213       transaction_pattern_(PATTERN_UNDEFINED),
214       total_received_bytes_(0),
215       websocket_handshake_stream_base_create_helper_(NULL) {
216   COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders ==
217                  arraysize(kValidationHeaders),
218                  Invalid_number_of_validation_headers);
219 }
220
221 HttpCache::Transaction::~Transaction() {
222   // We may have to issue another IO, but we should never invoke the callback_
223   // after this point.
224   callback_.Reset();
225
226   if (cache_) {
227     if (entry_) {
228       bool cancel_request = reading_ && response_.headers;
229       if (cancel_request) {
230         if (partial_) {
231           entry_->disk_entry->CancelSparseIO();
232         } else {
233           cancel_request &= (response_.headers->response_code() == 200);
234         }
235       }
236
237       cache_->DoneWithEntry(entry_, this, cancel_request);
238     } else if (cache_pending_) {
239       cache_->RemovePendingTransaction(this);
240     }
241   }
242 }
243
244 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
245                                           const CompletionCallback& callback) {
246   DCHECK(buf);
247   DCHECK_GT(buf_len, 0);
248   DCHECK(!callback.is_null());
249   if (!cache_.get() || !entry_)
250     return ERR_UNEXPECTED;
251
252   // We don't need to track this operation for anything.
253   // It could be possible to check if there is something already written and
254   // avoid writing again (it should be the same, right?), but let's allow the
255   // caller to "update" the contents with something new.
256   return entry_->disk_entry->WriteData(kMetadataIndex, 0, buf, buf_len,
257                                        callback, true);
258 }
259
260 bool HttpCache::Transaction::AddTruncatedFlag() {
261   DCHECK(mode_ & WRITE || mode_ == NONE);
262
263   // Don't set the flag for sparse entries.
264   if (partial_.get() && !truncated_)
265     return true;
266
267   if (!CanResume(true))
268     return false;
269
270   // We may have received the whole resource already.
271   if (done_reading_)
272     return true;
273
274   truncated_ = true;
275   target_state_ = STATE_NONE;
276   next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE;
277   DoLoop(OK);
278   return true;
279 }
280
281 LoadState HttpCache::Transaction::GetWriterLoadState() const {
282   if (network_trans_.get())
283     return network_trans_->GetLoadState();
284   if (entry_ || !request_)
285     return LOAD_STATE_IDLE;
286   return LOAD_STATE_WAITING_FOR_CACHE;
287 }
288
289 const BoundNetLog& HttpCache::Transaction::net_log() const {
290   return net_log_;
291 }
292
293 int HttpCache::Transaction::Start(const HttpRequestInfo* request,
294                                   const CompletionCallback& callback,
295                                   const BoundNetLog& net_log) {
296   DCHECK(request);
297   DCHECK(!callback.is_null());
298
299   // Ensure that we only have one asynchronous call at a time.
300   DCHECK(callback_.is_null());
301   DCHECK(!reading_);
302   DCHECK(!network_trans_.get());
303   DCHECK(!entry_);
304
305   if (!cache_.get())
306     return ERR_UNEXPECTED;
307
308   SetRequest(net_log, request);
309
310   // We have to wait until the backend is initialized so we start the SM.
311   next_state_ = STATE_GET_BACKEND;
312   int rv = DoLoop(OK);
313
314   // Setting this here allows us to check for the existence of a callback_ to
315   // determine if we are still inside Start.
316   if (rv == ERR_IO_PENDING)
317     callback_ = callback;
318
319   return rv;
320 }
321
322 int HttpCache::Transaction::RestartIgnoringLastError(
323     const CompletionCallback& callback) {
324   DCHECK(!callback.is_null());
325
326   // Ensure that we only have one asynchronous call at a time.
327   DCHECK(callback_.is_null());
328
329   if (!cache_.get())
330     return ERR_UNEXPECTED;
331
332   int rv = RestartNetworkRequest();
333
334   if (rv == ERR_IO_PENDING)
335     callback_ = callback;
336
337   return rv;
338 }
339
340 int HttpCache::Transaction::RestartWithCertificate(
341     X509Certificate* client_cert,
342     const CompletionCallback& callback) {
343   DCHECK(!callback.is_null());
344
345   // Ensure that we only have one asynchronous call at a time.
346   DCHECK(callback_.is_null());
347
348   if (!cache_.get())
349     return ERR_UNEXPECTED;
350
351   int rv = RestartNetworkRequestWithCertificate(client_cert);
352
353   if (rv == ERR_IO_PENDING)
354     callback_ = callback;
355
356   return rv;
357 }
358
359 int HttpCache::Transaction::RestartWithAuth(
360     const AuthCredentials& credentials,
361     const CompletionCallback& callback) {
362   DCHECK(auth_response_.headers.get());
363   DCHECK(!callback.is_null());
364
365   // Ensure that we only have one asynchronous call at a time.
366   DCHECK(callback_.is_null());
367
368   if (!cache_.get())
369     return ERR_UNEXPECTED;
370
371   // Clear the intermediate response since we are going to start over.
372   auth_response_ = HttpResponseInfo();
373
374   int rv = RestartNetworkRequestWithAuth(credentials);
375
376   if (rv == ERR_IO_PENDING)
377     callback_ = callback;
378
379   return rv;
380 }
381
382 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
383   if (!network_trans_.get())
384     return false;
385   return network_trans_->IsReadyToRestartForAuth();
386 }
387
388 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
389                                  const CompletionCallback& callback) {
390   DCHECK(buf);
391   DCHECK_GT(buf_len, 0);
392   DCHECK(!callback.is_null());
393
394   DCHECK(callback_.is_null());
395
396   if (!cache_.get())
397     return ERR_UNEXPECTED;
398
399   // If we have an intermediate auth response at this point, then it means the
400   // user wishes to read the network response (the error page).  If there is a
401   // previous response in the cache then we should leave it intact.
402   if (auth_response_.headers.get() && mode_ != NONE) {
403     UpdateTransactionPattern(PATTERN_NOT_COVERED);
404     DCHECK(mode_ & WRITE);
405     DoneWritingToEntry(mode_ == READ_WRITE);
406     mode_ = NONE;
407   }
408
409   reading_ = true;
410   int rv;
411
412   switch (mode_) {
413     case READ_WRITE:
414       DCHECK(partial_.get());
415       if (!network_trans_.get()) {
416         // We are just reading from the cache, but we may be writing later.
417         rv = ReadFromEntry(buf, buf_len);
418         break;
419       }
420     case NONE:
421     case WRITE:
422       DCHECK(network_trans_.get());
423       rv = ReadFromNetwork(buf, buf_len);
424       break;
425     case READ:
426       rv = ReadFromEntry(buf, buf_len);
427       break;
428     default:
429       NOTREACHED();
430       rv = ERR_FAILED;
431   }
432
433   if (rv == ERR_IO_PENDING) {
434     DCHECK(callback_.is_null());
435     callback_ = callback;
436   }
437   return rv;
438 }
439
440 void HttpCache::Transaction::StopCaching() {
441   // We really don't know where we are now. Hopefully there is no operation in
442   // progress, but nothing really prevents this method to be called after we
443   // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this
444   // point because we need the state machine for that (and even if we are really
445   // free, that would be an asynchronous operation). In other words, keep the
446   // entry how it is (it will be marked as truncated at destruction), and let
447   // the next piece of code that executes know that we are now reading directly
448   // from the net.
449   // TODO(mmenke):  This doesn't release the lock on the cache entry, so a
450   //                future request for the resource will be blocked on this one.
451   //                Fix this.
452   if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() &&
453       !is_sparse_ && !range_requested_) {
454     mode_ = NONE;
455   }
456 }
457
458 bool HttpCache::Transaction::GetFullRequestHeaders(
459     HttpRequestHeaders* headers) const {
460   if (network_trans_)
461     return network_trans_->GetFullRequestHeaders(headers);
462
463   // TODO(ttuttle): Read headers from cache.
464   return false;
465 }
466
467 int64 HttpCache::Transaction::GetTotalReceivedBytes() const {
468   int64 total_received_bytes = total_received_bytes_;
469   if (network_trans_)
470     total_received_bytes += network_trans_->GetTotalReceivedBytes();
471   return total_received_bytes;
472 }
473
474 void HttpCache::Transaction::DoneReading() {
475   if (cache_.get() && entry_) {
476     DCHECK_NE(mode_, UPDATE);
477     if (mode_ & WRITE) {
478       DoneWritingToEntry(true);
479     } else if (mode_ & READ) {
480       // It is necessary to check mode_ & READ because it is possible
481       // for mode_ to be NONE and entry_ non-NULL with a write entry
482       // if StopCaching was called.
483       cache_->DoneReadingFromEntry(entry_, this);
484       entry_ = NULL;
485     }
486   }
487 }
488
489 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
490   // Null headers means we encountered an error or haven't a response yet
491   if (auth_response_.headers.get())
492     return &auth_response_;
493   return (response_.headers.get() || response_.ssl_info.cert.get() ||
494           response_.cert_request_info.get())
495              ? &response_
496              : NULL;
497 }
498
499 LoadState HttpCache::Transaction::GetLoadState() const {
500   LoadState state = GetWriterLoadState();
501   if (state != LOAD_STATE_WAITING_FOR_CACHE)
502     return state;
503
504   if (cache_.get())
505     return cache_->GetLoadStateForPendingTransaction(this);
506
507   return LOAD_STATE_IDLE;
508 }
509
510 UploadProgress HttpCache::Transaction::GetUploadProgress() const {
511   if (network_trans_.get())
512     return network_trans_->GetUploadProgress();
513   return final_upload_progress_;
514 }
515
516 void HttpCache::Transaction::SetQuicServerInfo(
517     QuicServerInfo* quic_server_info) {}
518
519 bool HttpCache::Transaction::GetLoadTimingInfo(
520     LoadTimingInfo* load_timing_info) const {
521   if (network_trans_)
522     return network_trans_->GetLoadTimingInfo(load_timing_info);
523
524   if (old_network_trans_load_timing_) {
525     *load_timing_info = *old_network_trans_load_timing_;
526     return true;
527   }
528
529   if (first_cache_access_since_.is_null())
530     return false;
531
532   // If the cache entry was opened, return that time.
533   load_timing_info->send_start = first_cache_access_since_;
534   // This time doesn't make much sense when reading from the cache, so just use
535   // the same time as send_start.
536   load_timing_info->send_end = first_cache_access_since_;
537   return true;
538 }
539
540 void HttpCache::Transaction::SetPriority(RequestPriority priority) {
541   priority_ = priority;
542   if (network_trans_)
543     network_trans_->SetPriority(priority_);
544 }
545
546 void HttpCache::Transaction::SetWebSocketHandshakeStreamCreateHelper(
547     WebSocketHandshakeStreamBase::CreateHelper* create_helper) {
548   websocket_handshake_stream_base_create_helper_ = create_helper;
549   if (network_trans_)
550     network_trans_->SetWebSocketHandshakeStreamCreateHelper(create_helper);
551 }
552
553 void HttpCache::Transaction::SetBeforeNetworkStartCallback(
554     const BeforeNetworkStartCallback& callback) {
555   DCHECK(!network_trans_);
556   before_network_start_callback_ = callback;
557 }
558
559 int HttpCache::Transaction::ResumeNetworkStart() {
560   if (network_trans_)
561     return network_trans_->ResumeNetworkStart();
562   return ERR_UNEXPECTED;
563 }
564
565 //-----------------------------------------------------------------------------
566
567 void HttpCache::Transaction::DoCallback(int rv) {
568   DCHECK(rv != ERR_IO_PENDING);
569   DCHECK(!callback_.is_null());
570
571   read_buf_ = NULL;  // Release the buffer before invoking the callback.
572
573   // Since Run may result in Read being called, clear callback_ up front.
574   CompletionCallback c = callback_;
575   callback_.Reset();
576   c.Run(rv);
577 }
578
579 int HttpCache::Transaction::HandleResult(int rv) {
580   DCHECK(rv != ERR_IO_PENDING);
581   if (!callback_.is_null())
582     DoCallback(rv);
583
584   return rv;
585 }
586
587 // A few common patterns: (Foo* means Foo -> FooComplete)
588 //
589 // Not-cached entry:
590 //   Start():
591 //   GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
592 //   SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
593 //   CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
594 //   PartialHeadersReceived
595 //
596 //   Read():
597 //   NetworkRead* -> CacheWriteData*
598 //
599 // Cached entry, no validation:
600 //   Start():
601 //   GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
602 //   -> BeginPartialCacheValidation() -> BeginCacheValidation()
603 //
604 //   Read():
605 //   CacheReadData*
606 //
607 // Cached entry, validation (304):
608 //   Start():
609 //   GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
610 //   -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
611 //   SendRequest* -> SuccessfulSendRequest -> UpdateCachedResponse ->
612 //   CacheWriteResponse* -> UpdateCachedResponseComplete ->
613 //   OverwriteCachedResponse -> PartialHeadersReceived
614 //
615 //   Read():
616 //   CacheReadData*
617 //
618 // Cached entry, validation and replace (200):
619 //   Start():
620 //   GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
621 //   -> BeginPartialCacheValidation() -> BeginCacheValidation() ->
622 //   SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
623 //   CacheWriteResponse* -> DoTruncateCachedData* -> TruncateCachedMetadata* ->
624 //   PartialHeadersReceived
625 //
626 //   Read():
627 //   NetworkRead* -> CacheWriteData*
628 //
629 // Sparse entry, partially cached, byte range request:
630 //   Start():
631 //   GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
632 //   -> BeginPartialCacheValidation() -> CacheQueryData* ->
633 //   ValidateEntryHeadersAndContinue() -> StartPartialCacheValidation ->
634 //   CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
635 //   SuccessfulSendRequest -> UpdateCachedResponse -> CacheWriteResponse* ->
636 //   UpdateCachedResponseComplete -> OverwriteCachedResponse ->
637 //   PartialHeadersReceived
638 //
639 //   Read() 1:
640 //   NetworkRead* -> CacheWriteData*
641 //
642 //   Read() 2:
643 //   NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
644 //   CompletePartialCacheValidation -> CacheReadData* ->
645 //
646 //   Read() 3:
647 //   CacheReadData* -> StartPartialCacheValidation ->
648 //   CompletePartialCacheValidation -> BeginCacheValidation() -> SendRequest* ->
649 //   SuccessfulSendRequest -> UpdateCachedResponse* -> OverwriteCachedResponse
650 //   -> PartialHeadersReceived -> NetworkRead* -> CacheWriteData*
651 //
652 int HttpCache::Transaction::DoLoop(int result) {
653   DCHECK(next_state_ != STATE_NONE);
654
655   int rv = result;
656   do {
657     State state = next_state_;
658     next_state_ = STATE_NONE;
659     switch (state) {
660       case STATE_GET_BACKEND:
661         DCHECK_EQ(OK, rv);
662         rv = DoGetBackend();
663         break;
664       case STATE_GET_BACKEND_COMPLETE:
665         rv = DoGetBackendComplete(rv);
666         break;
667       case STATE_SEND_REQUEST:
668         DCHECK_EQ(OK, rv);
669         rv = DoSendRequest();
670         break;
671       case STATE_SEND_REQUEST_COMPLETE:
672         rv = DoSendRequestComplete(rv);
673         break;
674       case STATE_SUCCESSFUL_SEND_REQUEST:
675         DCHECK_EQ(OK, rv);
676         rv = DoSuccessfulSendRequest();
677         break;
678       case STATE_NETWORK_READ:
679         DCHECK_EQ(OK, rv);
680         rv = DoNetworkRead();
681         break;
682       case STATE_NETWORK_READ_COMPLETE:
683         rv = DoNetworkReadComplete(rv);
684         break;
685       case STATE_INIT_ENTRY:
686         DCHECK_EQ(OK, rv);
687         rv = DoInitEntry();
688         break;
689       case STATE_OPEN_ENTRY:
690         DCHECK_EQ(OK, rv);
691         rv = DoOpenEntry();
692         break;
693       case STATE_OPEN_ENTRY_COMPLETE:
694         rv = DoOpenEntryComplete(rv);
695         break;
696       case STATE_CREATE_ENTRY:
697         DCHECK_EQ(OK, rv);
698         rv = DoCreateEntry();
699         break;
700       case STATE_CREATE_ENTRY_COMPLETE:
701         rv = DoCreateEntryComplete(rv);
702         break;
703       case STATE_DOOM_ENTRY:
704         DCHECK_EQ(OK, rv);
705         rv = DoDoomEntry();
706         break;
707       case STATE_DOOM_ENTRY_COMPLETE:
708         rv = DoDoomEntryComplete(rv);
709         break;
710       case STATE_ADD_TO_ENTRY:
711         DCHECK_EQ(OK, rv);
712         rv = DoAddToEntry();
713         break;
714       case STATE_ADD_TO_ENTRY_COMPLETE:
715         rv = DoAddToEntryComplete(rv);
716         break;
717       case STATE_START_PARTIAL_CACHE_VALIDATION:
718         DCHECK_EQ(OK, rv);
719         rv = DoStartPartialCacheValidation();
720         break;
721       case STATE_COMPLETE_PARTIAL_CACHE_VALIDATION:
722         rv = DoCompletePartialCacheValidation(rv);
723         break;
724       case STATE_UPDATE_CACHED_RESPONSE:
725         DCHECK_EQ(OK, rv);
726         rv = DoUpdateCachedResponse();
727         break;
728       case STATE_UPDATE_CACHED_RESPONSE_COMPLETE:
729         rv = DoUpdateCachedResponseComplete(rv);
730         break;
731       case STATE_OVERWRITE_CACHED_RESPONSE:
732         DCHECK_EQ(OK, rv);
733         rv = DoOverwriteCachedResponse();
734         break;
735       case STATE_TRUNCATE_CACHED_DATA:
736         DCHECK_EQ(OK, rv);
737         rv = DoTruncateCachedData();
738         break;
739       case STATE_TRUNCATE_CACHED_DATA_COMPLETE:
740         rv = DoTruncateCachedDataComplete(rv);
741         break;
742       case STATE_TRUNCATE_CACHED_METADATA:
743         DCHECK_EQ(OK, rv);
744         rv = DoTruncateCachedMetadata();
745         break;
746       case STATE_TRUNCATE_CACHED_METADATA_COMPLETE:
747         rv = DoTruncateCachedMetadataComplete(rv);
748         break;
749       case STATE_PARTIAL_HEADERS_RECEIVED:
750         DCHECK_EQ(OK, rv);
751         rv = DoPartialHeadersReceived();
752         break;
753       case STATE_CACHE_READ_RESPONSE:
754         DCHECK_EQ(OK, rv);
755         rv = DoCacheReadResponse();
756         break;
757       case STATE_CACHE_READ_RESPONSE_COMPLETE:
758         rv = DoCacheReadResponseComplete(rv);
759         break;
760       case STATE_CACHE_WRITE_RESPONSE:
761         DCHECK_EQ(OK, rv);
762         rv = DoCacheWriteResponse();
763         break;
764       case STATE_CACHE_WRITE_TRUNCATED_RESPONSE:
765         DCHECK_EQ(OK, rv);
766         rv = DoCacheWriteTruncatedResponse();
767         break;
768       case STATE_CACHE_WRITE_RESPONSE_COMPLETE:
769         rv = DoCacheWriteResponseComplete(rv);
770         break;
771       case STATE_CACHE_READ_METADATA:
772         DCHECK_EQ(OK, rv);
773         rv = DoCacheReadMetadata();
774         break;
775       case STATE_CACHE_READ_METADATA_COMPLETE:
776         rv = DoCacheReadMetadataComplete(rv);
777         break;
778       case STATE_CACHE_QUERY_DATA:
779         DCHECK_EQ(OK, rv);
780         rv = DoCacheQueryData();
781         break;
782       case STATE_CACHE_QUERY_DATA_COMPLETE:
783         rv = DoCacheQueryDataComplete(rv);
784         break;
785       case STATE_CACHE_READ_DATA:
786         DCHECK_EQ(OK, rv);
787         rv = DoCacheReadData();
788         break;
789       case STATE_CACHE_READ_DATA_COMPLETE:
790         rv = DoCacheReadDataComplete(rv);
791         break;
792       case STATE_CACHE_WRITE_DATA:
793         rv = DoCacheWriteData(rv);
794         break;
795       case STATE_CACHE_WRITE_DATA_COMPLETE:
796         rv = DoCacheWriteDataComplete(rv);
797         break;
798       default:
799         NOTREACHED() << "bad state";
800         rv = ERR_FAILED;
801         break;
802     }
803   } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
804
805   if (rv != ERR_IO_PENDING)
806     HandleResult(rv);
807
808   return rv;
809 }
810
811 int HttpCache::Transaction::DoGetBackend() {
812   cache_pending_ = true;
813   next_state_ = STATE_GET_BACKEND_COMPLETE;
814   net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND);
815   return cache_->GetBackendForTransaction(this);
816 }
817
818 int HttpCache::Transaction::DoGetBackendComplete(int result) {
819   DCHECK(result == OK || result == ERR_FAILED);
820   net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND,
821                                     result);
822   cache_pending_ = false;
823
824   if (!ShouldPassThrough()) {
825     cache_key_ = cache_->GenerateCacheKey(request_);
826
827     // Requested cache access mode.
828     if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
829       mode_ = READ;
830     } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
831       mode_ = WRITE;
832     } else {
833       mode_ = READ_WRITE;
834     }
835
836     // Downgrade to UPDATE if the request has been externally conditionalized.
837     if (external_validation_.initialized) {
838       if (mode_ & WRITE) {
839         // Strip off the READ_DATA bit (and maybe add back a READ_META bit
840         // in case READ was off).
841         mode_ = UPDATE;
842       } else {
843         mode_ = NONE;
844       }
845     }
846   }
847
848   // Use PUT and DELETE only to invalidate existing stored entries.
849   if ((request_->method == "PUT" || request_->method == "DELETE") &&
850       mode_ != READ_WRITE && mode_ != WRITE) {
851     mode_ = NONE;
852   }
853
854   // If must use cache, then we must fail.  This can happen for back/forward
855   // navigations to a page generated via a form post.
856   if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE)
857     return ERR_CACHE_MISS;
858
859   if (mode_ == NONE) {
860     if (partial_.get()) {
861       partial_->RestoreHeaders(&custom_request_->extra_headers);
862       partial_.reset();
863     }
864     next_state_ = STATE_SEND_REQUEST;
865   } else {
866     next_state_ = STATE_INIT_ENTRY;
867   }
868
869   // This is only set if we have something to do with the response.
870   range_requested_ = (partial_.get() != NULL);
871
872   return OK;
873 }
874
875 int HttpCache::Transaction::DoSendRequest() {
876   DCHECK(mode_ & WRITE || mode_ == NONE);
877   DCHECK(!network_trans_.get());
878
879   send_request_since_ = TimeTicks::Now();
880
881   // Create a network transaction.
882   int rv = cache_->network_layer_->CreateTransaction(priority_,
883                                                      &network_trans_);
884   if (rv != OK)
885     return rv;
886   network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
887
888   // Old load timing information, if any, is now obsolete.
889   old_network_trans_load_timing_.reset();
890
891   if (websocket_handshake_stream_base_create_helper_)
892     network_trans_->SetWebSocketHandshakeStreamCreateHelper(
893         websocket_handshake_stream_base_create_helper_);
894
895   next_state_ = STATE_SEND_REQUEST_COMPLETE;
896   rv = network_trans_->Start(request_, io_callback_, net_log_);
897   return rv;
898 }
899
900 int HttpCache::Transaction::DoSendRequestComplete(int result) {
901   if (!cache_.get())
902     return ERR_UNEXPECTED;
903
904   // If requested, and we have a readable cache entry, and we have
905   // an error indicating that we're offline as opposed to in contact
906   // with a bad server, read from cache anyway.
907   if (IsOfflineError(result)) {
908     if (mode_ == READ_WRITE && entry_ && !partial_) {
909       RecordOfflineStatus(effective_load_flags_,
910                           OFFLINE_STATUS_DATA_AVAILABLE_OFFLINE);
911       if (effective_load_flags_ & LOAD_FROM_CACHE_IF_OFFLINE) {
912         UpdateTransactionPattern(PATTERN_NOT_COVERED);
913         response_.server_data_unavailable = true;
914         return SetupEntryForRead();
915       }
916     } else {
917       RecordOfflineStatus(effective_load_flags_,
918                           OFFLINE_STATUS_DATA_UNAVAILABLE_OFFLINE);
919     }
920   } else {
921     RecordOfflineStatus(effective_load_flags_,
922                         (result == OK ? OFFLINE_STATUS_NETWORK_SUCCEEDED :
923                                         OFFLINE_STATUS_NETWORK_FAILED));
924   }
925
926   // If we tried to conditionalize the request and failed, we know
927   // we won't be reading from the cache after this point.
928   if (couldnt_conditionalize_request_)
929     mode_ = WRITE;
930
931   if (result == OK) {
932     next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
933     return OK;
934   }
935
936   // Do not record requests that have network errors or restarts.
937   UpdateTransactionPattern(PATTERN_NOT_COVERED);
938   if (IsCertificateError(result)) {
939     const HttpResponseInfo* response = network_trans_->GetResponseInfo();
940     // If we get a certificate error, then there is a certificate in ssl_info,
941     // so GetResponseInfo() should never return NULL here.
942     DCHECK(response);
943     response_.ssl_info = response->ssl_info;
944   } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
945     const HttpResponseInfo* response = network_trans_->GetResponseInfo();
946     DCHECK(response);
947     response_.cert_request_info = response->cert_request_info;
948   } else if (response_.was_cached) {
949     DoneWritingToEntry(true);
950   }
951   return result;
952 }
953
954 // We received the response headers and there is no error.
955 int HttpCache::Transaction::DoSuccessfulSendRequest() {
956   DCHECK(!new_response_);
957   const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
958   bool authentication_failure = false;
959
960   if (new_response->headers->response_code() == 401 ||
961       new_response->headers->response_code() == 407) {
962     auth_response_ = *new_response;
963     if (!reading_)
964       return OK;
965
966     // We initiated a second request the caller doesn't know about. We should be
967     // able to authenticate this request because we should have authenticated
968     // this URL moments ago.
969     if (IsReadyToRestartForAuth()) {
970       DCHECK(!response_.auth_challenge.get());
971       next_state_ = STATE_SEND_REQUEST_COMPLETE;
972       // In theory we should check to see if there are new cookies, but there
973       // is no way to do that from here.
974       return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
975     }
976
977     // We have to perform cleanup at this point so that at least the next
978     // request can succeed.
979     authentication_failure = true;
980     if (entry_)
981       DoomPartialEntry(false);
982     mode_ = NONE;
983     partial_.reset();
984   }
985
986   new_response_ = new_response;
987   if (authentication_failure ||
988       (!ValidatePartialResponse() && !auth_response_.headers.get())) {
989     // Something went wrong with this request and we have to restart it.
990     // If we have an authentication response, we are exposed to weird things
991     // hapenning if the user cancels the authentication before we receive
992     // the new response.
993     UpdateTransactionPattern(PATTERN_NOT_COVERED);
994     response_ = HttpResponseInfo();
995     ResetNetworkTransaction();
996     new_response_ = NULL;
997     next_state_ = STATE_SEND_REQUEST;
998     return OK;
999   }
1000
1001   if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) {
1002     // We have stored the full entry, but it changed and the server is
1003     // sending a range. We have to delete the old entry.
1004     UpdateTransactionPattern(PATTERN_NOT_COVERED);
1005     DoneWritingToEntry(false);
1006   }
1007
1008   if (mode_ == WRITE &&
1009       transaction_pattern_ != PATTERN_ENTRY_CANT_CONDITIONALIZE) {
1010     UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED);
1011   }
1012
1013   if (mode_ == WRITE &&
1014       (request_->method == "PUT" || request_->method == "DELETE")) {
1015     if (NonErrorResponse(new_response->headers->response_code())) {
1016       int ret = cache_->DoomEntry(cache_key_, NULL);
1017       DCHECK_EQ(OK, ret);
1018     }
1019     cache_->DoneWritingToEntry(entry_, true);
1020     entry_ = NULL;
1021     mode_ = NONE;
1022   }
1023
1024   if (request_->method == "POST" &&
1025       NonErrorResponse(new_response->headers->response_code())) {
1026     cache_->DoomMainEntryForUrl(request_->url);
1027   }
1028
1029   RecordVaryHeaderHistogram(new_response);
1030
1031   if (new_response_->headers->response_code() == 416 &&
1032       (request_->method == "GET" || request_->method == "POST")) {
1033     // If there is an ective entry it may be destroyed with this transaction.
1034     response_ = *new_response_;
1035     return OK;
1036   }
1037
1038   // Are we expecting a response to a conditional query?
1039   if (mode_ == READ_WRITE || mode_ == UPDATE) {
1040     if (new_response->headers->response_code() == 304 || handling_206_) {
1041       UpdateTransactionPattern(PATTERN_ENTRY_VALIDATED);
1042       next_state_ = STATE_UPDATE_CACHED_RESPONSE;
1043       return OK;
1044     }
1045     UpdateTransactionPattern(PATTERN_ENTRY_UPDATED);
1046     mode_ = WRITE;
1047   }
1048
1049   next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1050   return OK;
1051 }
1052
1053 int HttpCache::Transaction::DoNetworkRead() {
1054   next_state_ = STATE_NETWORK_READ_COMPLETE;
1055   return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1056 }
1057
1058 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
1059   DCHECK(mode_ & WRITE || mode_ == NONE);
1060
1061   if (!cache_.get())
1062     return ERR_UNEXPECTED;
1063
1064   // If there is an error or we aren't saving the data, we are done; just wait
1065   // until the destructor runs to see if we can keep the data.
1066   if (mode_ == NONE || result < 0)
1067     return result;
1068
1069   next_state_ = STATE_CACHE_WRITE_DATA;
1070   return result;
1071 }
1072
1073 int HttpCache::Transaction::DoInitEntry() {
1074   DCHECK(!new_entry_);
1075
1076   if (!cache_.get())
1077     return ERR_UNEXPECTED;
1078
1079   if (mode_ == WRITE) {
1080     next_state_ = STATE_DOOM_ENTRY;
1081     return OK;
1082   }
1083
1084   next_state_ = STATE_OPEN_ENTRY;
1085   return OK;
1086 }
1087
1088 int HttpCache::Transaction::DoOpenEntry() {
1089   DCHECK(!new_entry_);
1090   next_state_ = STATE_OPEN_ENTRY_COMPLETE;
1091   cache_pending_ = true;
1092   net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY);
1093   first_cache_access_since_ = TimeTicks::Now();
1094   return cache_->OpenEntry(cache_key_, &new_entry_, this);
1095 }
1096
1097 int HttpCache::Transaction::DoOpenEntryComplete(int result) {
1098   // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1099   // OK, otherwise the cache will end up with an active entry without any
1100   // transaction attached.
1101   net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result);
1102   cache_pending_ = false;
1103   if (result == OK) {
1104     next_state_ = STATE_ADD_TO_ENTRY;
1105     return OK;
1106   }
1107
1108   if (result == ERR_CACHE_RACE) {
1109     next_state_ = STATE_INIT_ENTRY;
1110     return OK;
1111   }
1112
1113   if (request_->method == "PUT" || request_->method == "DELETE") {
1114     DCHECK(mode_ == READ_WRITE || mode_ == WRITE);
1115     mode_ = NONE;
1116     next_state_ = STATE_SEND_REQUEST;
1117     return OK;
1118   }
1119
1120   if (mode_ == READ_WRITE) {
1121     mode_ = WRITE;
1122     next_state_ = STATE_CREATE_ENTRY;
1123     return OK;
1124   }
1125   if (mode_ == UPDATE) {
1126     // There is no cache entry to update; proceed without caching.
1127     mode_ = NONE;
1128     next_state_ = STATE_SEND_REQUEST;
1129     return OK;
1130   }
1131   if (cache_->mode() == PLAYBACK)
1132     DVLOG(1) << "Playback Cache Miss: " << request_->url;
1133
1134   // The entry does not exist, and we are not permitted to create a new entry,
1135   // so we must fail.
1136   return ERR_CACHE_MISS;
1137 }
1138
1139 int HttpCache::Transaction::DoCreateEntry() {
1140   DCHECK(!new_entry_);
1141   next_state_ = STATE_CREATE_ENTRY_COMPLETE;
1142   cache_pending_ = true;
1143   net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY);
1144   return cache_->CreateEntry(cache_key_, &new_entry_, this);
1145 }
1146
1147 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1148   // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1149   // OK, otherwise the cache will end up with an active entry without any
1150   // transaction attached.
1151   net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY,
1152                                     result);
1153   cache_pending_ = false;
1154   next_state_ = STATE_ADD_TO_ENTRY;
1155
1156   if (result == ERR_CACHE_RACE) {
1157     next_state_ = STATE_INIT_ENTRY;
1158     return OK;
1159   }
1160
1161   if (result == OK) {
1162     UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", false);
1163   } else {
1164     UMA_HISTOGRAM_BOOLEAN("HttpCache.OpenToCreateRace", true);
1165     // We have a race here: Maybe we failed to open the entry and decided to
1166     // create one, but by the time we called create, another transaction already
1167     // created the entry. If we want to eliminate this issue, we need an atomic
1168     // OpenOrCreate() method exposed by the disk cache.
1169     DLOG(WARNING) << "Unable to create cache entry";
1170     mode_ = NONE;
1171     if (partial_.get())
1172       partial_->RestoreHeaders(&custom_request_->extra_headers);
1173     next_state_ = STATE_SEND_REQUEST;
1174   }
1175   return OK;
1176 }
1177
1178 int HttpCache::Transaction::DoDoomEntry() {
1179   next_state_ = STATE_DOOM_ENTRY_COMPLETE;
1180   cache_pending_ = true;
1181   if (first_cache_access_since_.is_null())
1182     first_cache_access_since_ = TimeTicks::Now();
1183   net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY);
1184   return cache_->DoomEntry(cache_key_, this);
1185 }
1186
1187 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1188   net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result);
1189   next_state_ = STATE_CREATE_ENTRY;
1190   cache_pending_ = false;
1191   if (result == ERR_CACHE_RACE)
1192     next_state_ = STATE_INIT_ENTRY;
1193   return OK;
1194 }
1195
1196 int HttpCache::Transaction::DoAddToEntry() {
1197   DCHECK(new_entry_);
1198   cache_pending_ = true;
1199   next_state_ = STATE_ADD_TO_ENTRY_COMPLETE;
1200   net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY);
1201   DCHECK(entry_lock_waiting_since_.is_null());
1202   entry_lock_waiting_since_ = TimeTicks::Now();
1203   return cache_->AddTransactionToEntry(new_entry_, this);
1204 }
1205
1206 int HttpCache::Transaction::DoAddToEntryComplete(int result) {
1207   net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_ADD_TO_ENTRY,
1208                                     result);
1209   const TimeDelta entry_lock_wait =
1210       TimeTicks::Now() - entry_lock_waiting_since_;
1211   UMA_HISTOGRAM_TIMES("HttpCache.EntryLockWait", entry_lock_wait);
1212
1213   entry_lock_waiting_since_ = TimeTicks();
1214   DCHECK(new_entry_);
1215   cache_pending_ = false;
1216
1217   if (result == OK)
1218     entry_ = new_entry_;
1219
1220   // If there is a failure, the cache should have taken care of new_entry_.
1221   new_entry_ = NULL;
1222
1223   if (result == ERR_CACHE_RACE) {
1224     next_state_ = STATE_INIT_ENTRY;
1225     return OK;
1226   }
1227
1228   if (result != OK) {
1229     NOTREACHED();
1230     return result;
1231   }
1232
1233   if (mode_ == WRITE) {
1234     if (partial_.get())
1235       partial_->RestoreHeaders(&custom_request_->extra_headers);
1236     next_state_ = STATE_SEND_REQUEST;
1237   } else {
1238     // We have to read the headers from the cached entry.
1239     DCHECK(mode_ & READ_META);
1240     next_state_ = STATE_CACHE_READ_RESPONSE;
1241   }
1242   return OK;
1243 }
1244
1245 // We may end up here multiple times for a given request.
1246 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1247   if (mode_ == NONE)
1248     return OK;
1249
1250   next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION;
1251   return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1252 }
1253
1254 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1255   if (!result) {
1256     // This is the end of the request.
1257     if (mode_ & WRITE) {
1258       DoneWritingToEntry(true);
1259     } else {
1260       cache_->DoneReadingFromEntry(entry_, this);
1261       entry_ = NULL;
1262     }
1263     return result;
1264   }
1265
1266   if (result < 0)
1267     return result;
1268
1269   partial_->PrepareCacheValidation(entry_->disk_entry,
1270                                    &custom_request_->extra_headers);
1271
1272   if (reading_ && partial_->IsCurrentRangeCached()) {
1273     next_state_ = STATE_CACHE_READ_DATA;
1274     return OK;
1275   }
1276
1277   return BeginCacheValidation();
1278 }
1279
1280 // We received 304 or 206 and we want to update the cached response headers.
1281 int HttpCache::Transaction::DoUpdateCachedResponse() {
1282   next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1283   int rv = OK;
1284   // Update cached response based on headers in new_response.
1285   // TODO(wtc): should we update cached certificate (response_.ssl_info), too?
1286   response_.headers->Update(*new_response_->headers.get());
1287   response_.response_time = new_response_->response_time;
1288   response_.request_time = new_response_->request_time;
1289   response_.network_accessed = new_response_->network_accessed;
1290
1291   if (response_.headers->HasHeaderValue("cache-control", "no-store")) {
1292     if (!entry_->doomed) {
1293       int ret = cache_->DoomEntry(cache_key_, NULL);
1294       DCHECK_EQ(OK, ret);
1295     }
1296   } else {
1297     // If we are already reading, we already updated the headers for this
1298     // request; doing it again will change Content-Length.
1299     if (!reading_) {
1300       target_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE;
1301       next_state_ = STATE_CACHE_WRITE_RESPONSE;
1302       rv = OK;
1303     }
1304   }
1305   return rv;
1306 }
1307
1308 int HttpCache::Transaction::DoUpdateCachedResponseComplete(int result) {
1309   if (mode_ == UPDATE) {
1310     DCHECK(!handling_206_);
1311     // We got a "not modified" response and already updated the corresponding
1312     // cache entry above.
1313     //
1314     // By closing the cached entry now, we make sure that the 304 rather than
1315     // the cached 200 response, is what will be returned to the user.
1316     DoneWritingToEntry(true);
1317   } else if (entry_ && !handling_206_) {
1318     DCHECK_EQ(READ_WRITE, mode_);
1319     if (!partial_.get() || partial_->IsLastRange()) {
1320       cache_->ConvertWriterToReader(entry_);
1321       mode_ = READ;
1322     }
1323     // We no longer need the network transaction, so destroy it.
1324     final_upload_progress_ = network_trans_->GetUploadProgress();
1325     ResetNetworkTransaction();
1326   } else if (entry_ && handling_206_ && truncated_ &&
1327              partial_->initial_validation()) {
1328     // We just finished the validation of a truncated entry, and the server
1329     // is willing to resume the operation. Now we go back and start serving
1330     // the first part to the user.
1331     ResetNetworkTransaction();
1332     new_response_ = NULL;
1333     next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1334     partial_->SetRangeToStartDownload();
1335     return OK;
1336   }
1337   next_state_ = STATE_OVERWRITE_CACHED_RESPONSE;
1338   return OK;
1339 }
1340
1341 int HttpCache::Transaction::DoOverwriteCachedResponse() {
1342   if (mode_ & READ) {
1343     next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1344     return OK;
1345   }
1346
1347   // We change the value of Content-Length for partial content.
1348   if (handling_206_ && partial_.get())
1349     partial_->FixContentLength(new_response_->headers.get());
1350
1351   response_ = *new_response_;
1352
1353   if (handling_206_ && !CanResume(false)) {
1354     // There is no point in storing this resource because it will never be used.
1355     DoneWritingToEntry(false);
1356     if (partial_.get())
1357       partial_->FixResponseHeaders(response_.headers.get(), true);
1358     next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1359     return OK;
1360   }
1361
1362   target_state_ = STATE_TRUNCATE_CACHED_DATA;
1363   next_state_ = truncated_ ? STATE_CACHE_WRITE_TRUNCATED_RESPONSE :
1364                              STATE_CACHE_WRITE_RESPONSE;
1365   return OK;
1366 }
1367
1368 int HttpCache::Transaction::DoTruncateCachedData() {
1369   next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE;
1370   if (!entry_)
1371     return OK;
1372   if (net_log_.IsLogging())
1373     net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1374   // Truncate the stream.
1375   return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_);
1376 }
1377
1378 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) {
1379   if (entry_) {
1380     if (net_log_.IsLogging()) {
1381       net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1382                                         result);
1383     }
1384   }
1385
1386   next_state_ = STATE_TRUNCATE_CACHED_METADATA;
1387   return OK;
1388 }
1389
1390 int HttpCache::Transaction::DoTruncateCachedMetadata() {
1391   next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE;
1392   if (!entry_)
1393     return OK;
1394
1395   if (net_log_.IsLogging())
1396     net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1397   return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_);
1398 }
1399
1400 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) {
1401   if (entry_) {
1402     if (net_log_.IsLogging()) {
1403       net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1404                                         result);
1405     }
1406   }
1407
1408   next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1409   return OK;
1410 }
1411
1412 int HttpCache::Transaction::DoPartialHeadersReceived() {
1413   new_response_ = NULL;
1414   if (entry_ && !partial_.get() &&
1415       entry_->disk_entry->GetDataSize(kMetadataIndex))
1416     next_state_ = STATE_CACHE_READ_METADATA;
1417
1418   if (!partial_.get())
1419     return OK;
1420
1421   if (reading_) {
1422     if (network_trans_.get()) {
1423       next_state_ = STATE_NETWORK_READ;
1424     } else {
1425       next_state_ = STATE_CACHE_READ_DATA;
1426     }
1427   } else if (mode_ != NONE) {
1428     // We are about to return the headers for a byte-range request to the user,
1429     // so let's fix them.
1430     partial_->FixResponseHeaders(response_.headers.get(), true);
1431   }
1432   return OK;
1433 }
1434
1435 int HttpCache::Transaction::DoCacheReadResponse() {
1436   DCHECK(entry_);
1437   next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE;
1438
1439   io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex);
1440   read_buf_ = new IOBuffer(io_buf_len_);
1441
1442   net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1443   return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_.get(),
1444                                       io_buf_len_, io_callback_);
1445 }
1446
1447 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1448   net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1449   if (result != io_buf_len_ ||
1450       !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_,
1451                                     &response_, &truncated_)) {
1452     return OnCacheReadError(result, true);
1453   }
1454
1455   // Some resources may have slipped in as truncated when they're not.
1456   int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1457   if (response_.headers->GetContentLength() == current_size)
1458     truncated_ = false;
1459
1460   // We now have access to the cache entry.
1461   //
1462   //  o if we are a reader for the transaction, then we can start reading the
1463   //    cache entry.
1464   //
1465   //  o if we can read or write, then we should check if the cache entry needs
1466   //    to be validated and then issue a network request if needed or just read
1467   //    from the cache if the cache entry is already valid.
1468   //
1469   //  o if we are set to UPDATE, then we are handling an externally
1470   //    conditionalized request (if-modified-since / if-none-match). We check
1471   //    if the request headers define a validation request.
1472   //
1473   switch (mode_) {
1474     case READ:
1475       UpdateTransactionPattern(PATTERN_ENTRY_USED);
1476       result = BeginCacheRead();
1477       break;
1478     case READ_WRITE:
1479       result = BeginPartialCacheValidation();
1480       break;
1481     case UPDATE:
1482       result = BeginExternallyConditionalizedRequest();
1483       break;
1484     case WRITE:
1485     default:
1486       NOTREACHED();
1487       result = ERR_FAILED;
1488   }
1489   return result;
1490 }
1491
1492 int HttpCache::Transaction::DoCacheWriteResponse() {
1493   if (entry_) {
1494     if (net_log_.IsLogging())
1495       net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1496   }
1497   return WriteResponseInfoToEntry(false);
1498 }
1499
1500 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() {
1501   if (entry_) {
1502     if (net_log_.IsLogging())
1503       net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
1504   }
1505   return WriteResponseInfoToEntry(true);
1506 }
1507
1508 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1509   next_state_ = target_state_;
1510   target_state_ = STATE_NONE;
1511   if (!entry_)
1512     return OK;
1513   if (net_log_.IsLogging()) {
1514     net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO,
1515                                       result);
1516   }
1517
1518   // Balance the AddRef from WriteResponseInfoToEntry.
1519   if (result != io_buf_len_) {
1520     DLOG(ERROR) << "failed to write response info to cache";
1521     DoneWritingToEntry(false);
1522   }
1523   return OK;
1524 }
1525
1526 int HttpCache::Transaction::DoCacheReadMetadata() {
1527   DCHECK(entry_);
1528   DCHECK(!response_.metadata.get());
1529   next_state_ = STATE_CACHE_READ_METADATA_COMPLETE;
1530
1531   response_.metadata =
1532       new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1533
1534   net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO);
1535   return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1536                                       response_.metadata.get(),
1537                                       response_.metadata->size(),
1538                                       io_callback_);
1539 }
1540
1541 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1542   net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result);
1543   if (result != response_.metadata->size())
1544     return OnCacheReadError(result, false);
1545   return OK;
1546 }
1547
1548 int HttpCache::Transaction::DoCacheQueryData() {
1549   next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE;
1550   return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1551 }
1552
1553 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1554   if (result == ERR_NOT_IMPLEMENTED) {
1555     // Restart the request overwriting the cache entry.
1556     // TODO(pasko): remove this workaround as soon as the SimpleBackendImpl
1557     // supports Sparse IO.
1558     return DoRestartPartialRequest();
1559   }
1560   DCHECK_EQ(OK, result);
1561   if (!cache_.get())
1562     return ERR_UNEXPECTED;
1563
1564   return ValidateEntryHeadersAndContinue();
1565 }
1566
1567 int HttpCache::Transaction::DoCacheReadData() {
1568   DCHECK(entry_);
1569   next_state_ = STATE_CACHE_READ_DATA_COMPLETE;
1570
1571   if (net_log_.IsLogging())
1572     net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA);
1573   if (partial_.get()) {
1574     return partial_->CacheRead(entry_->disk_entry, read_buf_.get(), io_buf_len_,
1575                                io_callback_);
1576   }
1577
1578   return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_,
1579                                       read_buf_.get(), io_buf_len_,
1580                                       io_callback_);
1581 }
1582
1583 int HttpCache::Transaction::DoCacheReadDataComplete(int result) {
1584   if (net_log_.IsLogging()) {
1585     net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA,
1586                                       result);
1587   }
1588
1589   if (!cache_.get())
1590     return ERR_UNEXPECTED;
1591
1592   if (partial_.get()) {
1593     // Partial requests are confusing to report in histograms because they may
1594     // have multiple underlying requests.
1595     UpdateTransactionPattern(PATTERN_NOT_COVERED);
1596     return DoPartialCacheReadCompleted(result);
1597   }
1598
1599   if (result > 0) {
1600     read_offset_ += result;
1601   } else if (result == 0) {  // End of file.
1602     RecordHistograms();
1603     cache_->DoneReadingFromEntry(entry_, this);
1604     entry_ = NULL;
1605   } else {
1606     return OnCacheReadError(result, false);
1607   }
1608   return result;
1609 }
1610
1611 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) {
1612   next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE;
1613   write_len_ = num_bytes;
1614   if (entry_) {
1615     if (net_log_.IsLogging())
1616       net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA);
1617   }
1618
1619   return AppendResponseDataToEntry(read_buf_.get(), num_bytes, io_callback_);
1620 }
1621
1622 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) {
1623   if (entry_) {
1624     if (net_log_.IsLogging()) {
1625       net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA,
1626                                         result);
1627     }
1628   }
1629   // Balance the AddRef from DoCacheWriteData.
1630   if (!cache_.get())
1631     return ERR_UNEXPECTED;
1632
1633   if (result != write_len_) {
1634     DLOG(ERROR) << "failed to write response data to cache";
1635     DoneWritingToEntry(false);
1636
1637     // We want to ignore errors writing to disk and just keep reading from
1638     // the network.
1639     result = write_len_;
1640   } else if (!done_reading_ && entry_) {
1641     int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1642     int64 body_size = response_.headers->GetContentLength();
1643     if (body_size >= 0 && body_size <= current_size)
1644       done_reading_ = true;
1645   }
1646
1647   if (partial_.get()) {
1648     // This may be the last request.
1649     if (!(result == 0 && !truncated_ &&
1650           (partial_->IsLastRange() || mode_ == WRITE)))
1651       return DoPartialNetworkReadCompleted(result);
1652   }
1653
1654   if (result == 0) {
1655     // End of file. This may be the result of a connection problem so see if we
1656     // have to keep the entry around to be flagged as truncated later on.
1657     if (done_reading_ || !entry_ || partial_.get() ||
1658         response_.headers->GetContentLength() <= 0)
1659       DoneWritingToEntry(true);
1660   }
1661
1662   return result;
1663 }
1664
1665 //-----------------------------------------------------------------------------
1666
1667 void HttpCache::Transaction::SetRequest(const BoundNetLog& net_log,
1668                                         const HttpRequestInfo* request) {
1669   net_log_ = net_log;
1670   request_ = request;
1671   effective_load_flags_ = request_->load_flags;
1672
1673   switch (cache_->mode()) {
1674     case NORMAL:
1675       break;
1676     case RECORD:
1677       // When in record mode, we want to NEVER load from the cache.
1678       // The reason for this is beacuse we save the Set-Cookie headers
1679       // (intentionally).  If we read from the cache, we replay them
1680       // prematurely.
1681       effective_load_flags_ |= LOAD_BYPASS_CACHE;
1682       break;
1683     case PLAYBACK:
1684       // When in playback mode, we want to load exclusively from the cache.
1685       effective_load_flags_ |= LOAD_ONLY_FROM_CACHE;
1686       break;
1687     case DISABLE:
1688       effective_load_flags_ |= LOAD_DISABLE_CACHE;
1689       break;
1690   }
1691
1692   // Some headers imply load flags.  The order here is significant.
1693   //
1694   //   LOAD_DISABLE_CACHE   : no cache read or write
1695   //   LOAD_BYPASS_CACHE    : no cache read
1696   //   LOAD_VALIDATE_CACHE  : no cache read unless validation
1697   //
1698   // The former modes trump latter modes, so if we find a matching header we
1699   // can stop iterating kSpecialHeaders.
1700   //
1701   static const struct {
1702     const HeaderNameAndValue* search;
1703     int load_flag;
1704   } kSpecialHeaders[] = {
1705     { kPassThroughHeaders, LOAD_DISABLE_CACHE },
1706     { kForceFetchHeaders, LOAD_BYPASS_CACHE },
1707     { kForceValidateHeaders, LOAD_VALIDATE_CACHE },
1708   };
1709
1710   bool range_found = false;
1711   bool external_validation_error = false;
1712
1713   if (request_->extra_headers.HasHeader(HttpRequestHeaders::kRange))
1714     range_found = true;
1715
1716   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSpecialHeaders); ++i) {
1717     if (HeaderMatches(request_->extra_headers, kSpecialHeaders[i].search)) {
1718       effective_load_flags_ |= kSpecialHeaders[i].load_flag;
1719       break;
1720     }
1721   }
1722
1723   // Check for conditionalization headers which may correspond with a
1724   // cache validation request.
1725   for (size_t i = 0; i < arraysize(kValidationHeaders); ++i) {
1726     const ValidationHeaderInfo& info = kValidationHeaders[i];
1727     std::string validation_value;
1728     if (request_->extra_headers.GetHeader(
1729             info.request_header_name, &validation_value)) {
1730       if (!external_validation_.values[i].empty() ||
1731           validation_value.empty()) {
1732         external_validation_error = true;
1733       }
1734       external_validation_.values[i] = validation_value;
1735       external_validation_.initialized = true;
1736     }
1737   }
1738
1739   // We don't support ranges and validation headers.
1740   if (range_found && external_validation_.initialized) {
1741     LOG(WARNING) << "Byte ranges AND validation headers found.";
1742     effective_load_flags_ |= LOAD_DISABLE_CACHE;
1743   }
1744
1745   // If there is more than one validation header, we can't treat this request as
1746   // a cache validation, since we don't know for sure which header the server
1747   // will give us a response for (and they could be contradictory).
1748   if (external_validation_error) {
1749     LOG(WARNING) << "Multiple or malformed validation headers found.";
1750     effective_load_flags_ |= LOAD_DISABLE_CACHE;
1751   }
1752
1753   if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
1754     UpdateTransactionPattern(PATTERN_NOT_COVERED);
1755     partial_.reset(new PartialData);
1756     if (request_->method == "GET" && partial_->Init(request_->extra_headers)) {
1757       // We will be modifying the actual range requested to the server, so
1758       // let's remove the header here.
1759       custom_request_.reset(new HttpRequestInfo(*request_));
1760       custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
1761       request_ = custom_request_.get();
1762       partial_->SetHeaders(custom_request_->extra_headers);
1763     } else {
1764       // The range is invalid or we cannot handle it properly.
1765       VLOG(1) << "Invalid byte range found.";
1766       effective_load_flags_ |= LOAD_DISABLE_CACHE;
1767       partial_.reset(NULL);
1768     }
1769   }
1770 }
1771
1772 bool HttpCache::Transaction::ShouldPassThrough() {
1773   // We may have a null disk_cache if there is an error we cannot recover from,
1774   // like not enough disk space, or sharing violations.
1775   if (!cache_->disk_cache_.get())
1776     return true;
1777
1778   // When using the record/playback modes, we always use the cache
1779   // and we never pass through.
1780   if (cache_->mode() == RECORD || cache_->mode() == PLAYBACK)
1781     return false;
1782
1783   if (effective_load_flags_ & LOAD_DISABLE_CACHE)
1784     return true;
1785
1786   if (request_->method == "GET")
1787     return false;
1788
1789   if (request_->method == "POST" && request_->upload_data_stream &&
1790       request_->upload_data_stream->identifier()) {
1791     return false;
1792   }
1793
1794   if (request_->method == "PUT" && request_->upload_data_stream)
1795     return false;
1796
1797   if (request_->method == "DELETE")
1798     return false;
1799
1800   // TODO(darin): add support for caching HEAD responses
1801   return true;
1802 }
1803
1804 int HttpCache::Transaction::BeginCacheRead() {
1805   // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
1806   if (response_.headers->response_code() == 206 || partial_.get()) {
1807     NOTREACHED();
1808     return ERR_CACHE_MISS;
1809   }
1810
1811   // We don't have the whole resource.
1812   if (truncated_)
1813     return ERR_CACHE_MISS;
1814
1815   if (entry_->disk_entry->GetDataSize(kMetadataIndex))
1816     next_state_ = STATE_CACHE_READ_METADATA;
1817
1818   return OK;
1819 }
1820
1821 int HttpCache::Transaction::BeginCacheValidation() {
1822   DCHECK(mode_ == READ_WRITE);
1823
1824   bool skip_validation = !RequiresValidation();
1825
1826   if (truncated_) {
1827     // Truncated entries can cause partial gets, so we shouldn't record this
1828     // load in histograms.
1829     UpdateTransactionPattern(PATTERN_NOT_COVERED);
1830     skip_validation = !partial_->initial_validation();
1831   }
1832
1833   if (partial_.get() && (is_sparse_ || truncated_) &&
1834       (!partial_->IsCurrentRangeCached() || invalid_range_)) {
1835     // Force revalidation for sparse or truncated entries. Note that we don't
1836     // want to ignore the regular validation logic just because a byte range was
1837     // part of the request.
1838     skip_validation = false;
1839   }
1840
1841   if (skip_validation) {
1842     UpdateTransactionPattern(PATTERN_ENTRY_USED);
1843     RecordOfflineStatus(effective_load_flags_, OFFLINE_STATUS_FRESH_CACHE);
1844     return SetupEntryForRead();
1845   } else {
1846     // Make the network request conditional, to see if we may reuse our cached
1847     // response.  If we cannot do so, then we just resort to a normal fetch.
1848     // Our mode remains READ_WRITE for a conditional request.  Even if the
1849     // conditionalization fails, we don't switch to WRITE mode until we
1850     // know we won't be falling back to using the cache entry in the
1851     // LOAD_FROM_CACHE_IF_OFFLINE case.
1852     if (!ConditionalizeRequest()) {
1853       couldnt_conditionalize_request_ = true;
1854       UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
1855       if (partial_.get())
1856         return DoRestartPartialRequest();
1857
1858       DCHECK_NE(206, response_.headers->response_code());
1859     }
1860     next_state_ = STATE_SEND_REQUEST;
1861   }
1862   return OK;
1863 }
1864
1865 int HttpCache::Transaction::BeginPartialCacheValidation() {
1866   DCHECK(mode_ == READ_WRITE);
1867
1868   if (response_.headers->response_code() != 206 && !partial_.get() &&
1869       !truncated_) {
1870     return BeginCacheValidation();
1871   }
1872
1873   // Partial requests should not be recorded in histograms.
1874   UpdateTransactionPattern(PATTERN_NOT_COVERED);
1875   if (range_requested_) {
1876     next_state_ = STATE_CACHE_QUERY_DATA;
1877     return OK;
1878   }
1879   // The request is not for a range, but we have stored just ranges.
1880   partial_.reset(new PartialData());
1881   partial_->SetHeaders(request_->extra_headers);
1882   if (!custom_request_.get()) {
1883     custom_request_.reset(new HttpRequestInfo(*request_));
1884     request_ = custom_request_.get();
1885   }
1886
1887   return ValidateEntryHeadersAndContinue();
1888 }
1889
1890 // This should only be called once per request.
1891 int HttpCache::Transaction::ValidateEntryHeadersAndContinue() {
1892   DCHECK(mode_ == READ_WRITE);
1893
1894   if (!partial_->UpdateFromStoredHeaders(
1895           response_.headers.get(), entry_->disk_entry, truncated_)) {
1896     return DoRestartPartialRequest();
1897   }
1898
1899   if (response_.headers->response_code() == 206)
1900     is_sparse_ = true;
1901
1902   if (!partial_->IsRequestedRangeOK()) {
1903     // The stored data is fine, but the request may be invalid.
1904     invalid_range_ = true;
1905   }
1906
1907   next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
1908   return OK;
1909 }
1910
1911 int HttpCache::Transaction::BeginExternallyConditionalizedRequest() {
1912   DCHECK_EQ(UPDATE, mode_);
1913   DCHECK(external_validation_.initialized);
1914
1915   for (size_t i = 0;  i < arraysize(kValidationHeaders); i++) {
1916     if (external_validation_.values[i].empty())
1917       continue;
1918     // Retrieve either the cached response's "etag" or "last-modified" header.
1919     std::string validator;
1920     response_.headers->EnumerateHeader(
1921         NULL,
1922         kValidationHeaders[i].related_response_header_name,
1923         &validator);
1924
1925     if (response_.headers->response_code() != 200 || truncated_ ||
1926         validator.empty() || validator != external_validation_.values[i]) {
1927       // The externally conditionalized request is not a validation request
1928       // for our existing cache entry. Proceed with caching disabled.
1929       UpdateTransactionPattern(PATTERN_NOT_COVERED);
1930       DoneWritingToEntry(true);
1931     }
1932   }
1933
1934   next_state_ = STATE_SEND_REQUEST;
1935   return OK;
1936 }
1937
1938 int HttpCache::Transaction::RestartNetworkRequest() {
1939   DCHECK(mode_ & WRITE || mode_ == NONE);
1940   DCHECK(network_trans_.get());
1941   DCHECK_EQ(STATE_NONE, next_state_);
1942
1943   next_state_ = STATE_SEND_REQUEST_COMPLETE;
1944   int rv = network_trans_->RestartIgnoringLastError(io_callback_);
1945   if (rv != ERR_IO_PENDING)
1946     return DoLoop(rv);
1947   return rv;
1948 }
1949
1950 int HttpCache::Transaction::RestartNetworkRequestWithCertificate(
1951     X509Certificate* client_cert) {
1952   DCHECK(mode_ & WRITE || mode_ == NONE);
1953   DCHECK(network_trans_.get());
1954   DCHECK_EQ(STATE_NONE, next_state_);
1955
1956   next_state_ = STATE_SEND_REQUEST_COMPLETE;
1957   int rv = network_trans_->RestartWithCertificate(client_cert, io_callback_);
1958   if (rv != ERR_IO_PENDING)
1959     return DoLoop(rv);
1960   return rv;
1961 }
1962
1963 int HttpCache::Transaction::RestartNetworkRequestWithAuth(
1964     const AuthCredentials& credentials) {
1965   DCHECK(mode_ & WRITE || mode_ == NONE);
1966   DCHECK(network_trans_.get());
1967   DCHECK_EQ(STATE_NONE, next_state_);
1968
1969   next_state_ = STATE_SEND_REQUEST_COMPLETE;
1970   int rv = network_trans_->RestartWithAuth(credentials, io_callback_);
1971   if (rv != ERR_IO_PENDING)
1972     return DoLoop(rv);
1973   return rv;
1974 }
1975
1976 bool HttpCache::Transaction::RequiresValidation() {
1977   // TODO(darin): need to do more work here:
1978   //  - make sure we have a matching request method
1979   //  - watch out for cached responses that depend on authentication
1980
1981   // In playback mode, nothing requires validation.
1982   if (cache_->mode() == net::HttpCache::PLAYBACK)
1983     return false;
1984
1985   if (response_.vary_data.is_valid() &&
1986       !response_.vary_data.MatchesRequest(*request_,
1987                                           *response_.headers.get())) {
1988     vary_mismatch_ = true;
1989     return true;
1990   }
1991
1992   if (effective_load_flags_ & LOAD_PREFERRING_CACHE)
1993     return false;
1994
1995   if (effective_load_flags_ & LOAD_VALIDATE_CACHE)
1996     return true;
1997
1998   if (request_->method == "PUT" || request_->method == "DELETE")
1999     return true;
2000
2001   if (response_.headers->RequiresValidation(
2002           response_.request_time, response_.response_time, Time::Now())) {
2003     return true;
2004   }
2005
2006   return false;
2007 }
2008
2009 bool HttpCache::Transaction::ConditionalizeRequest() {
2010   DCHECK(response_.headers.get());
2011
2012   if (request_->method == "PUT" || request_->method == "DELETE")
2013     return false;
2014
2015   // This only makes sense for cached 200 or 206 responses.
2016   if (response_.headers->response_code() != 200 &&
2017       response_.headers->response_code() != 206) {
2018     return false;
2019   }
2020
2021   // We should have handled this case before.
2022   DCHECK(response_.headers->response_code() != 206 ||
2023          response_.headers->HasStrongValidators());
2024
2025   // Just use the first available ETag and/or Last-Modified header value.
2026   // TODO(darin): Or should we use the last?
2027
2028   std::string etag_value;
2029   if (response_.headers->GetHttpVersion() >= HttpVersion(1, 1))
2030     response_.headers->EnumerateHeader(NULL, "etag", &etag_value);
2031
2032   std::string last_modified_value;
2033   if (!vary_mismatch_) {
2034     response_.headers->EnumerateHeader(NULL, "last-modified",
2035                                        &last_modified_value);
2036   }
2037
2038   if (etag_value.empty() && last_modified_value.empty())
2039     return false;
2040
2041   if (!partial_.get()) {
2042     // Need to customize the request, so this forces us to allocate :(
2043     custom_request_.reset(new HttpRequestInfo(*request_));
2044     request_ = custom_request_.get();
2045   }
2046   DCHECK(custom_request_.get());
2047
2048   bool use_if_range = partial_.get() && !partial_->IsCurrentRangeCached() &&
2049                       !invalid_range_;
2050
2051   if (!etag_value.empty()) {
2052     if (use_if_range) {
2053       // We don't want to switch to WRITE mode if we don't have this block of a
2054       // byte-range request because we may have other parts cached.
2055       custom_request_->extra_headers.SetHeader(
2056           HttpRequestHeaders::kIfRange, etag_value);
2057     } else {
2058       custom_request_->extra_headers.SetHeader(
2059           HttpRequestHeaders::kIfNoneMatch, etag_value);
2060     }
2061     // For byte-range requests, make sure that we use only one way to validate
2062     // the request.
2063     if (partial_.get() && !partial_->IsCurrentRangeCached())
2064       return true;
2065   }
2066
2067   if (!last_modified_value.empty()) {
2068     if (use_if_range) {
2069       custom_request_->extra_headers.SetHeader(
2070           HttpRequestHeaders::kIfRange, last_modified_value);
2071     } else {
2072       custom_request_->extra_headers.SetHeader(
2073           HttpRequestHeaders::kIfModifiedSince, last_modified_value);
2074     }
2075   }
2076
2077   return true;
2078 }
2079
2080 // We just received some headers from the server. We may have asked for a range,
2081 // in which case partial_ has an object. This could be the first network request
2082 // we make to fulfill the original request, or we may be already reading (from
2083 // the net and / or the cache). If we are not expecting a certain response, we
2084 // just bypass the cache for this request (but again, maybe we are reading), and
2085 // delete partial_ (so we are not able to "fix" the headers that we return to
2086 // the user). This results in either a weird response for the caller (we don't
2087 // expect it after all), or maybe a range that was not exactly what it was asked
2088 // for.
2089 //
2090 // If the server is simply telling us that the resource has changed, we delete
2091 // the cached entry and restart the request as the caller intended (by returning
2092 // false from this method). However, we may not be able to do that at any point,
2093 // for instance if we already returned the headers to the user.
2094 //
2095 // WARNING: Whenever this code returns false, it has to make sure that the next
2096 // time it is called it will return true so that we don't keep retrying the
2097 // request.
2098 bool HttpCache::Transaction::ValidatePartialResponse() {
2099   const HttpResponseHeaders* headers = new_response_->headers.get();
2100   int response_code = headers->response_code();
2101   bool partial_response = (response_code == 206);
2102   handling_206_ = false;
2103
2104   if (!entry_ || request_->method != "GET")
2105     return true;
2106
2107   if (invalid_range_) {
2108     // We gave up trying to match this request with the stored data. If the
2109     // server is ok with the request, delete the entry, otherwise just ignore
2110     // this request
2111     DCHECK(!reading_);
2112     if (partial_response || response_code == 200) {
2113       DoomPartialEntry(true);
2114       mode_ = NONE;
2115     } else {
2116       if (response_code == 304)
2117         FailRangeRequest();
2118       IgnoreRangeRequest();
2119     }
2120     return true;
2121   }
2122
2123   if (!partial_.get()) {
2124     // We are not expecting 206 but we may have one.
2125     if (partial_response)
2126       IgnoreRangeRequest();
2127
2128     return true;
2129   }
2130
2131   // TODO(rvargas): Do we need to consider other results here?.
2132   bool failure = response_code == 200 || response_code == 416;
2133
2134   if (partial_->IsCurrentRangeCached()) {
2135     // We asked for "If-None-Match: " so a 206 means a new object.
2136     if (partial_response)
2137       failure = true;
2138
2139     if (response_code == 304 && partial_->ResponseHeadersOK(headers))
2140       return true;
2141   } else {
2142     // We asked for "If-Range: " so a 206 means just another range.
2143     if (partial_response && partial_->ResponseHeadersOK(headers)) {
2144       handling_206_ = true;
2145       return true;
2146     }
2147
2148     if (!reading_ && !is_sparse_ && !partial_response) {
2149       // See if we can ignore the fact that we issued a byte range request.
2150       // If the server sends 200, just store it. If it sends an error, redirect
2151       // or something else, we may store the response as long as we didn't have
2152       // anything already stored.
2153       if (response_code == 200 ||
2154           (!truncated_ && response_code != 304 && response_code != 416)) {
2155         // The server is sending something else, and we can save it.
2156         DCHECK((truncated_ && !partial_->IsLastRange()) || range_requested_);
2157         partial_.reset();
2158         truncated_ = false;
2159         return true;
2160       }
2161     }
2162
2163     // 304 is not expected here, but we'll spare the entry (unless it was
2164     // truncated).
2165     if (truncated_)
2166       failure = true;
2167   }
2168
2169   if (failure) {
2170     // We cannot truncate this entry, it has to be deleted.
2171     UpdateTransactionPattern(PATTERN_NOT_COVERED);
2172     DoomPartialEntry(false);
2173     mode_ = NONE;
2174     if (!reading_ && !partial_->IsLastRange()) {
2175       // We'll attempt to issue another network request, this time without us
2176       // messing up the headers.
2177       partial_->RestoreHeaders(&custom_request_->extra_headers);
2178       partial_.reset();
2179       truncated_ = false;
2180       return false;
2181     }
2182     LOG(WARNING) << "Failed to revalidate partial entry";
2183     partial_.reset();
2184     return true;
2185   }
2186
2187   IgnoreRangeRequest();
2188   return true;
2189 }
2190
2191 void HttpCache::Transaction::IgnoreRangeRequest() {
2192   // We have a problem. We may or may not be reading already (in which case we
2193   // returned the headers), but we'll just pretend that this request is not
2194   // using the cache and see what happens. Most likely this is the first
2195   // response from the server (it's not changing its mind midway, right?).
2196   UpdateTransactionPattern(PATTERN_NOT_COVERED);
2197   if (mode_ & WRITE)
2198     DoneWritingToEntry(mode_ != WRITE);
2199   else if (mode_ & READ && entry_)
2200     cache_->DoneReadingFromEntry(entry_, this);
2201
2202   partial_.reset(NULL);
2203   entry_ = NULL;
2204   mode_ = NONE;
2205 }
2206
2207 void HttpCache::Transaction::FailRangeRequest() {
2208   response_ = *new_response_;
2209   partial_->FixResponseHeaders(response_.headers.get(), false);
2210 }
2211
2212 int HttpCache::Transaction::SetupEntryForRead() {
2213   if (network_trans_)
2214     ResetNetworkTransaction();
2215   if (partial_.get()) {
2216     if (truncated_ || is_sparse_ || !invalid_range_) {
2217       // We are going to return the saved response headers to the caller, so
2218       // we may need to adjust them first.
2219       next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
2220       return OK;
2221     } else {
2222       partial_.reset();
2223     }
2224   }
2225   cache_->ConvertWriterToReader(entry_);
2226   mode_ = READ;
2227
2228   if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2229     next_state_ = STATE_CACHE_READ_METADATA;
2230   return OK;
2231 }
2232
2233
2234 int HttpCache::Transaction::ReadFromNetwork(IOBuffer* data, int data_len) {
2235   read_buf_ = data;
2236   io_buf_len_ = data_len;
2237   next_state_ = STATE_NETWORK_READ;
2238   return DoLoop(OK);
2239 }
2240
2241 int HttpCache::Transaction::ReadFromEntry(IOBuffer* data, int data_len) {
2242   read_buf_ = data;
2243   io_buf_len_ = data_len;
2244   next_state_ = STATE_CACHE_READ_DATA;
2245   return DoLoop(OK);
2246 }
2247
2248 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2249                                          IOBuffer* data, int data_len,
2250                                          const CompletionCallback& callback) {
2251   if (!entry_)
2252     return data_len;
2253
2254   int rv = 0;
2255   if (!partial_.get() || !data_len) {
2256     rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2257                                        true);
2258   } else {
2259     rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2260   }
2261   return rv;
2262 }
2263
2264 int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
2265   next_state_ = STATE_CACHE_WRITE_RESPONSE_COMPLETE;
2266   if (!entry_)
2267     return OK;
2268
2269   // Do not cache no-store content (unless we are record mode).  Do not cache
2270   // content with cert errors either.  This is to prevent not reporting net
2271   // errors when loading a resource from the cache.  When we load a page over
2272   // HTTPS with a cert error we show an SSL blocking page.  If the user clicks
2273   // proceed we reload the resource ignoring the errors.  The loaded resource
2274   // is then cached.  If that resource is subsequently loaded from the cache,
2275   // no net error is reported (even though the cert status contains the actual
2276   // errors) and no SSL blocking page is shown.  An alternative would be to
2277   // reverse-map the cert status to a net error and replay the net error.
2278   if ((cache_->mode() != RECORD &&
2279        response_.headers->HasHeaderValue("cache-control", "no-store")) ||
2280       net::IsCertStatusError(response_.ssl_info.cert_status)) {
2281     DoneWritingToEntry(false);
2282     if (net_log_.IsLogging())
2283       net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO);
2284     return OK;
2285   }
2286
2287   // When writing headers, we normally only write the non-transient
2288   // headers; when in record mode, record everything.
2289   bool skip_transient_headers = (cache_->mode() != RECORD);
2290
2291   if (truncated)
2292     DCHECK_EQ(200, response_.headers->response_code());
2293
2294   scoped_refptr<PickledIOBuffer> data(new PickledIOBuffer());
2295   response_.Persist(data->pickle(), skip_transient_headers, truncated);
2296   data->Done();
2297
2298   io_buf_len_ = data->pickle()->size();
2299   return entry_->disk_entry->WriteData(kResponseInfoIndex, 0, data.get(),
2300                                        io_buf_len_, io_callback_, true);
2301 }
2302
2303 int HttpCache::Transaction::AppendResponseDataToEntry(
2304     IOBuffer* data, int data_len, const CompletionCallback& callback) {
2305   if (!entry_ || !data_len)
2306     return data_len;
2307
2308   int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
2309   return WriteToEntry(kResponseContentIndex, current_size, data, data_len,
2310                       callback);
2311 }
2312
2313 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2314   if (!entry_)
2315     return;
2316
2317   RecordHistograms();
2318
2319   cache_->DoneWritingToEntry(entry_, success);
2320   entry_ = NULL;
2321   mode_ = NONE;  // switch to 'pass through' mode
2322 }
2323
2324 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2325   DLOG(ERROR) << "ReadData failed: " << result;
2326   const int result_for_histogram = std::max(0, -result);
2327   if (restart) {
2328     UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2329                                 result_for_histogram);
2330   } else {
2331     UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2332                                 result_for_histogram);
2333   }
2334
2335   // Avoid using this entry in the future.
2336   if (cache_.get())
2337     cache_->DoomActiveEntry(cache_key_);
2338
2339   if (restart) {
2340     DCHECK(!reading_);
2341     DCHECK(!network_trans_.get());
2342     cache_->DoneWithEntry(entry_, this, false);
2343     entry_ = NULL;
2344     is_sparse_ = false;
2345     partial_.reset();
2346     next_state_ = STATE_GET_BACKEND;
2347     return OK;
2348   }
2349
2350   return ERR_CACHE_READ_FAILURE;
2351 }
2352
2353 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2354   DVLOG(2) << "DoomPartialEntry";
2355   int rv = cache_->DoomEntry(cache_key_, NULL);
2356   DCHECK_EQ(OK, rv);
2357   cache_->DoneWithEntry(entry_, this, false);
2358   entry_ = NULL;
2359   is_sparse_ = false;
2360   if (delete_object)
2361     partial_.reset(NULL);
2362 }
2363
2364 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2365   partial_->OnNetworkReadCompleted(result);
2366
2367   if (result == 0) {
2368     // We need to move on to the next range.
2369     ResetNetworkTransaction();
2370     next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2371   }
2372   return result;
2373 }
2374
2375 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) {
2376   partial_->OnCacheReadCompleted(result);
2377
2378   if (result == 0 && mode_ == READ_WRITE) {
2379     // We need to move on to the next range.
2380     next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION;
2381   } else if (result < 0) {
2382     return OnCacheReadError(result, false);
2383   }
2384   return result;
2385 }
2386
2387 int HttpCache::Transaction::DoRestartPartialRequest() {
2388   // The stored data cannot be used. Get rid of it and restart this request.
2389   // We need to also reset the |truncated_| flag as a new entry is created.
2390   DoomPartialEntry(!range_requested_);
2391   mode_ = WRITE;
2392   truncated_ = false;
2393   next_state_ = STATE_INIT_ENTRY;
2394   return OK;
2395 }
2396
2397 void HttpCache::Transaction::ResetNetworkTransaction() {
2398   DCHECK(!old_network_trans_load_timing_);
2399   DCHECK(network_trans_);
2400   LoadTimingInfo load_timing;
2401   if (network_trans_->GetLoadTimingInfo(&load_timing))
2402     old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing));
2403   total_received_bytes_ += network_trans_->GetTotalReceivedBytes();
2404   network_trans_.reset();
2405 }
2406
2407 // Histogram data from the end of 2010 show the following distribution of
2408 // response headers:
2409 //
2410 //   Content-Length............... 87%
2411 //   Date......................... 98%
2412 //   Last-Modified................ 49%
2413 //   Etag......................... 19%
2414 //   Accept-Ranges: bytes......... 25%
2415 //   Accept-Ranges: none.......... 0.4%
2416 //   Strong Validator............. 50%
2417 //   Strong Validator + ranges.... 24%
2418 //   Strong Validator + CL........ 49%
2419 //
2420 bool HttpCache::Transaction::CanResume(bool has_data) {
2421   // Double check that there is something worth keeping.
2422   if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2423     return false;
2424
2425   if (request_->method != "GET")
2426     return false;
2427
2428   // Note that if this is a 206, content-length was already fixed after calling
2429   // PartialData::ResponseHeadersOK().
2430   if (response_.headers->GetContentLength() <= 0 ||
2431       response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2432       !response_.headers->HasStrongValidators()) {
2433     return false;
2434   }
2435
2436   return true;
2437 }
2438
2439 void HttpCache::Transaction::UpdateTransactionPattern(
2440     TransactionPattern new_transaction_pattern) {
2441   if (transaction_pattern_ == PATTERN_NOT_COVERED)
2442     return;
2443   DCHECK(transaction_pattern_ == PATTERN_UNDEFINED ||
2444          new_transaction_pattern == PATTERN_NOT_COVERED);
2445   transaction_pattern_ = new_transaction_pattern;
2446 }
2447
2448 void HttpCache::Transaction::RecordHistograms() {
2449   DCHECK_NE(PATTERN_UNDEFINED, transaction_pattern_);
2450   if (!cache_.get() || !cache_->GetCurrentBackend() ||
2451       cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2452       cache_->mode() != NORMAL || request_->method != "GET") {
2453     return;
2454   }
2455   UMA_HISTOGRAM_ENUMERATION(
2456       "HttpCache.Pattern", transaction_pattern_, PATTERN_MAX);
2457   if (transaction_pattern_ == PATTERN_NOT_COVERED)
2458     return;
2459   DCHECK(!range_requested_);
2460   DCHECK(!first_cache_access_since_.is_null());
2461
2462   TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2463
2464   UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2465
2466   bool did_send_request = !send_request_since_.is_null();
2467   DCHECK(
2468       (did_send_request &&
2469        (transaction_pattern_ == PATTERN_ENTRY_NOT_CACHED ||
2470         transaction_pattern_ == PATTERN_ENTRY_VALIDATED ||
2471         transaction_pattern_ == PATTERN_ENTRY_UPDATED ||
2472         transaction_pattern_ == PATTERN_ENTRY_CANT_CONDITIONALIZE)) ||
2473       (!did_send_request && transaction_pattern_ == PATTERN_ENTRY_USED));
2474
2475   if (!did_send_request) {
2476     DCHECK(transaction_pattern_ == PATTERN_ENTRY_USED);
2477     UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.Used", total_time);
2478     return;
2479   }
2480
2481   TimeDelta before_send_time = send_request_since_ - first_cache_access_since_;
2482   int before_send_percent =
2483       total_time.ToInternalValue() == 0 ? 0
2484                                         : before_send_time * 100 / total_time;
2485   DCHECK_LE(0, before_send_percent);
2486   DCHECK_GE(100, before_send_percent);
2487
2488   UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone.SentRequest", total_time);
2489   UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend", before_send_time);
2490   UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend", before_send_percent);
2491
2492   // TODO(gavinp): Remove or minimize these histograms, particularly the ones
2493   // below this comment after we have received initial data.
2494   switch (transaction_pattern_) {
2495     case PATTERN_ENTRY_CANT_CONDITIONALIZE: {
2496       UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.CantConditionalize",
2497                           before_send_time);
2498       UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.CantConditionalize",
2499                                before_send_percent);
2500       break;
2501     }
2502     case PATTERN_ENTRY_NOT_CACHED: {
2503       UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.NotCached", before_send_time);
2504       UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.NotCached",
2505                                before_send_percent);
2506       break;
2507     }
2508     case PATTERN_ENTRY_VALIDATED: {
2509       UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Validated", before_send_time);
2510       UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Validated",
2511                                before_send_percent);
2512       break;
2513     }
2514     case PATTERN_ENTRY_UPDATED: {
2515       UMA_HISTOGRAM_TIMES("HttpCache.BeforeSend.Updated", before_send_time);
2516       UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
2517                                before_send_percent);
2518       break;
2519     }
2520     default:
2521       NOTREACHED();
2522   }
2523 }
2524
2525 void HttpCache::Transaction::OnIOComplete(int result) {
2526   DoLoop(result);
2527 }
2528
2529 }  // namespace net