Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: net/http/http_cache_transaction.cc

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: DoneReadingFromEntry replaced with DoneWithEntry Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/http/http_cache_transaction.h" 5 #include "net/http/http_cache_transaction.h"
6 6
7 #include "build/build_config.h" // For OS_POSIX 7 #include "build/build_config.h" // For OS_POSIX
8 8
9 #if defined(OS_POSIX) 9 #if defined(OS_POSIX)
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 } 189 }
190 190
191 HttpCache::Transaction::~Transaction() { 191 HttpCache::Transaction::~Transaction() {
192 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction"); 192 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction");
193 // We may have to issue another IO, but we should never invoke the callback_ 193 // We may have to issue another IO, but we should never invoke the callback_
194 // after this point. 194 // after this point.
195 callback_.Reset(); 195 callback_.Reset();
196 196
197 if (cache_) { 197 if (cache_) {
198 if (entry_) { 198 if (entry_) {
199 bool cancel_request = reading_ && response_.headers.get(); 199 cache_->DoneWithEntry(entry_, this, true, partial_ != nullptr);
jkarlin 2017/05/30 14:42:38 Yay for this cleanup!
200 if (cancel_request) {
201 if (partial_) {
202 entry_->disk_entry->CancelSparseIO();
203 } else {
204 cancel_request &= (response_.headers->response_code() == 200);
205 }
206 }
207
208 cache_->DoneWithEntry(entry_, this, cancel_request);
209 } else if (cache_pending_) { 200 } else if (cache_pending_) {
210 cache_->RemovePendingTransaction(this); 201 cache_->RemovePendingTransaction(this);
211 } 202 }
212 } 203 }
213 } 204 }
214 205
215 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, 206 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
216 const CompletionCallback& callback) { 207 const CompletionCallback& callback) {
217 DCHECK(buf); 208 DCHECK(buf);
218 DCHECK_GT(buf_len, 0); 209 DCHECK_GT(buf_len, 0);
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 435
445 void HttpCache::Transaction::DoneReading() { 436 void HttpCache::Transaction::DoneReading() {
446 if (cache_.get() && entry_) { 437 if (cache_.get() && entry_) {
447 DCHECK_NE(mode_, UPDATE); 438 DCHECK_NE(mode_, UPDATE);
448 if (mode_ & WRITE) { 439 if (mode_ & WRITE) {
449 DoneWritingToEntry(true); 440 DoneWritingToEntry(true);
450 } else if (mode_ & READ) { 441 } else if (mode_ & READ) {
451 // It is necessary to check mode_ & READ because it is possible 442 // It is necessary to check mode_ & READ because it is possible
452 // for mode_ to be NONE and entry_ non-NULL with a write entry 443 // for mode_ to be NONE and entry_ non-NULL with a write entry
453 // if StopCaching was called. 444 // if StopCaching was called.
454 cache_->DoneReadingFromEntry(entry_, this); 445 cache_->DoneWithEntry(entry_, this, false, partial_ != nullptr);
455 entry_ = NULL; 446 entry_ = NULL;
456 } 447 }
457 } 448 }
458 } 449 }
459 450
460 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const { 451 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const {
461 // Null headers means we encountered an error or haven't a response yet 452 // Null headers means we encountered an error or haven't a response yet
462 if (auth_response_.headers.get()) { 453 if (auth_response_.headers.get()) {
463 DCHECK_EQ(cache_entry_status_, auth_response_.cache_entry_status) 454 DCHECK_EQ(cache_entry_status_, auth_response_.cache_entry_status)
464 << "These must be in sync via SetResponse and SetAuthResponse."; 455 << "These must be in sync via SetResponse and SetAuthResponse.";
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 ConnectionAttempts* out) const { 549 ConnectionAttempts* out) const {
559 ConnectionAttempts new_connection_attempts; 550 ConnectionAttempts new_connection_attempts;
560 if (network_trans_) 551 if (network_trans_)
561 network_trans_->GetConnectionAttempts(&new_connection_attempts); 552 network_trans_->GetConnectionAttempts(&new_connection_attempts);
562 553
563 out->swap(new_connection_attempts); 554 out->swap(new_connection_attempts);
564 out->insert(out->begin(), old_connection_attempts_.begin(), 555 out->insert(out->begin(), old_connection_attempts_.begin(),
565 old_connection_attempts_.end()); 556 old_connection_attempts_.end());
566 } 557 }
567 558
559 void HttpCache::Transaction::SetValidatingCannotProceed() {
560 DCHECK(!reading_);
561 next_state_ = STATE_HEADERS_PHASE_CANNOT_PROCEED;
562 entry_ = nullptr;
563 }
564
568 size_t HttpCache::Transaction::EstimateMemoryUsage() const { 565 size_t HttpCache::Transaction::EstimateMemoryUsage() const {
569 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. 566 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108.
570 return 0; 567 return 0;
571 } 568 }
572 569
573 //----------------------------------------------------------------------------- 570 //-----------------------------------------------------------------------------
574 571
575 // A few common patterns: (Foo* means Foo -> FooComplete) 572 // A few common patterns: (Foo* means Foo -> FooComplete)
576 // 573 //
577 // 1. Not-cached entry: 574 // 1. Not-cached entry:
578 // Start(): 575 // Start():
579 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> 576 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
580 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> 577 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
581 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> 578 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
582 // PartialHeadersReceived 579 // PartialHeadersReceived -> FinishHeaders*
583 // 580 //
584 // Read(): 581 // Read():
585 // NetworkRead* -> CacheWriteData* 582 // NetworkRead* -> CacheWriteData*
586 // 583 //
587 // 2. Cached entry, no validation: 584 // 2. Cached entry, no validation:
588 // Start(): 585 // Start():
589 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 586 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
590 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 587 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
591 // BeginCacheValidation() -> SetupEntryForRead() 588 // BeginCacheValidation() -> SetupEntryForRead() -> FinishHeaders*
592 // 589 //
593 // Read(): 590 // Read():
594 // CacheReadData* 591 // CacheReadData*
595 // 592 //
596 // 3. Cached entry, validation (304): 593 // 3. Cached entry, validation (304):
597 // Start(): 594 // Start():
598 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 595 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
599 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 596 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
600 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 597 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
601 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> 598 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
602 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> 599 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
603 // PartialHeadersReceived 600 // PartialHeadersReceived -> FinishHeaders*
604 // 601 //
605 // Read(): 602 // Read():
606 // CacheReadData* 603 // CacheReadData*
607 // 604 //
608 // 4. Cached entry, validation and replace (200): 605 // 4. Cached entry, validation and replace (200):
609 // Start(): 606 // Start():
610 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 607 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
611 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 608 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
612 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 609 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
613 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> 610 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
614 // TruncateCachedMetadata* -> PartialHeadersReceived 611 // TruncateCachedMetadata* -> PartialHeadersReceived -> FinishHeaders*
615 // 612 //
616 // Read(): 613 // Read():
617 // NetworkRead* -> CacheWriteData* 614 // NetworkRead* -> CacheWriteData*
618 // 615 //
619 // 5. Sparse entry, partially cached, byte range request: 616 // 5. Sparse entry, partially cached, byte range request:
620 // Start(): 617 // Start():
621 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 618 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
622 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 619 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
623 // CacheQueryData* -> ValidateEntryHeadersAndContinue() -> 620 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
624 // StartPartialCacheValidation -> CompletePartialCacheValidation -> 621 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
625 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 622 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
626 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> 623 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
627 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> 624 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
628 // PartialHeadersReceived 625 // PartialHeadersReceived -> FinishHeaders*
629 // 626 //
630 // Read() 1: 627 // Read() 1:
631 // NetworkRead* -> CacheWriteData* 628 // NetworkRead* -> CacheWriteData*
632 // 629 //
633 // Read() 2: 630 // Read() 2:
634 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation -> 631 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
635 // CompletePartialCacheValidation -> CacheReadData* -> 632 // CompletePartialCacheValidation -> CacheReadData* ->
636 // 633 //
637 // Read() 3: 634 // Read() 3:
638 // CacheReadData* -> StartPartialCacheValidation -> 635 // CacheReadData* -> StartPartialCacheValidation ->
(...skipping 20 matching lines...) Expand all
659 // Read(): 656 // Read():
660 // CacheReadData (returns 0) 657 // CacheReadData (returns 0)
661 // 658 //
662 // 9. HEAD. Cached entry, validation and replace (200): 659 // 9. HEAD. Cached entry, validation and replace (200):
663 // Pass through. The request dooms the old entry, as a HEAD won't be stored by 660 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
664 // itself. 661 // itself.
665 // Start(): 662 // Start():
666 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 663 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
667 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 664 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
668 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 665 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
669 // OverwriteCachedResponse 666 // OverwriteCachedResponse -> FinishHeaders*
670 // 667 //
671 // 10. HEAD. Sparse entry, partially cached: 668 // 10. HEAD. Sparse entry, partially cached:
672 // Serve the request from the cache, as long as it doesn't require 669 // Serve the request from the cache, as long as it doesn't require
673 // revalidation. Ignore missing ranges when deciding to revalidate. If the 670 // revalidation. Ignore missing ranges when deciding to revalidate. If the
674 // entry requires revalidation, ignore the whole request and go to full pass 671 // entry requires revalidation, ignore the whole request and go to full pass
675 // through (the result of the HEAD request will NOT update the entry). 672 // through (the result of the HEAD request will NOT update the entry).
676 // 673 //
677 // Start(): Basically the same as example 7, as we never create a partial_ 674 // Start(): Basically the same as example 7, as we never create a partial_
678 // object for this request. 675 // object for this request.
679 // 676 //
680 // 11. Prefetch, not-cached entry: 677 // 11. Prefetch, not-cached entry:
681 // The same as example 1. The "unused_since_prefetch" bit is stored as true in 678 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
682 // UpdateCachedResponse. 679 // UpdateCachedResponse.
683 // 680 //
684 // 12. Prefetch, cached entry: 681 // 12. Prefetch, cached entry:
685 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between 682 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
686 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch 683 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
687 // bit is unset. 684 // bit is unset.
688 // 685 //
689 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true: 686 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
690 // Skip validation, similar to example 2. 687 // Skip validation, similar to example 2.
691 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 688 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
692 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation -> 689 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
693 // BeginPartialCacheValidation() -> BeginCacheValidation() -> 690 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
694 // SetupEntryForRead() 691 // SetupEntryForRead() -> FinishHeaders*
695 // 692 //
696 // Read(): 693 // Read():
697 // CacheReadData* 694 // CacheReadData*
698 // 695 //
699 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: 696 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
700 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between 697 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
701 // CacheReadResponse* and CacheDispatchValidation. 698 // CacheReadResponse* and CacheDispatchValidation.
702 int HttpCache::Transaction::DoLoop(int result) { 699 int HttpCache::Transaction::DoLoop(int result) {
703 DCHECK_NE(STATE_UNSET, next_state_); 700 DCHECK_NE(STATE_UNSET, next_state_);
704 DCHECK_NE(STATE_NONE, next_state_); 701 DCHECK_NE(STATE_NONE, next_state_);
705 DCHECK(!in_do_loop_); 702 DCHECK(!in_do_loop_);
706 703
707 int rv = result; 704 int rv = result;
705 State state = next_state_;
708 do { 706 do {
709 State state = next_state_; 707 state = next_state_;
710 next_state_ = STATE_UNSET; 708 next_state_ = STATE_UNSET;
711 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true); 709 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true);
712 710
713 switch (state) { 711 switch (state) {
714 case STATE_GET_BACKEND: 712 case STATE_GET_BACKEND:
715 DCHECK_EQ(OK, rv); 713 DCHECK_EQ(OK, rv);
716 rv = DoGetBackend(); 714 rv = DoGetBackend();
717 break; 715 break;
718 case STATE_GET_BACKEND_COMPLETE: 716 case STATE_GET_BACKEND_COMPLETE:
719 rv = DoGetBackendComplete(rv); 717 rv = DoGetBackendComplete(rv);
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 DCHECK_EQ(OK, rv); 834 DCHECK_EQ(OK, rv);
837 rv = DoPartialHeadersReceived(); 835 rv = DoPartialHeadersReceived();
838 break; 836 break;
839 case STATE_CACHE_READ_METADATA: 837 case STATE_CACHE_READ_METADATA:
840 DCHECK_EQ(OK, rv); 838 DCHECK_EQ(OK, rv);
841 rv = DoCacheReadMetadata(); 839 rv = DoCacheReadMetadata();
842 break; 840 break;
843 case STATE_CACHE_READ_METADATA_COMPLETE: 841 case STATE_CACHE_READ_METADATA_COMPLETE:
844 rv = DoCacheReadMetadataComplete(rv); 842 rv = DoCacheReadMetadataComplete(rv);
845 break; 843 break;
844 case STATE_HEADERS_PHASE_CANNOT_PROCEED:
845 rv = DoHeadersPhaseCannotProceed();
846 break;
847 case STATE_FINISH_HEADERS:
848 rv = DoFinishHeaders(rv);
849 break;
850 case STATE_FINISH_HEADERS_COMPLETE:
851 rv = DoFinishHeadersComplete(rv);
852 break;
846 case STATE_NETWORK_READ: 853 case STATE_NETWORK_READ:
847 DCHECK_EQ(OK, rv); 854 DCHECK_EQ(OK, rv);
848 rv = DoNetworkRead(); 855 rv = DoNetworkRead();
849 break; 856 break;
850 case STATE_NETWORK_READ_COMPLETE: 857 case STATE_NETWORK_READ_COMPLETE:
851 rv = DoNetworkReadComplete(rv); 858 rv = DoNetworkReadComplete(rv);
852 break; 859 break;
853 case STATE_CACHE_READ_DATA: 860 case STATE_CACHE_READ_DATA:
854 DCHECK_EQ(OK, rv); 861 DCHECK_EQ(OK, rv);
855 rv = DoCacheReadData(); 862 rv = DoCacheReadData();
(...skipping 16 matching lines...) Expand all
872 break; 879 break;
873 default: 880 default:
874 NOTREACHED() << "bad state " << state; 881 NOTREACHED() << "bad state " << state;
875 rv = ERR_FAILED; 882 rv = ERR_FAILED;
876 break; 883 break;
877 } 884 }
878 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state; 885 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state;
879 886
880 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); 887 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
881 888
889 // Assert Start() state machine's allowed last state in successful cases when
890 // caching is happening.
891 DCHECK(reading_ || rv != OK || !entry_ ||
892 state == STATE_FINISH_HEADERS_COMPLETE);
893
882 if (rv != ERR_IO_PENDING && !callback_.is_null()) { 894 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
883 read_buf_ = NULL; // Release the buffer before invoking the callback. 895 read_buf_ = nullptr; // Release the buffer before invoking the callback.
884 base::ResetAndReturn(&callback_).Run(rv); 896 base::ResetAndReturn(&callback_).Run(rv);
885 } 897 }
886 898
887 return rv; 899 return rv;
888 } 900 }
889 901
890 int HttpCache::Transaction::DoGetBackend() { 902 int HttpCache::Transaction::DoGetBackend() {
891 cache_pending_ = true; 903 cache_pending_ = true;
892 TransitionToState(STATE_GET_BACKEND_COMPLETE); 904 TransitionToState(STATE_GET_BACKEND_COMPLETE);
893 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); 905 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND);
894 return cache_->GetBackendForTransaction(this); 906 return cache_->GetBackendForTransaction(this);
895 } 907 }
896 908
897 int HttpCache::Transaction::DoGetBackendComplete(int result) { 909 int HttpCache::Transaction::DoGetBackendComplete(int result) {
898 DCHECK(result == OK || result == ERR_FAILED); 910 DCHECK(result == OK || result == ERR_FAILED);
899 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_GET_BACKEND, 911 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_GET_BACKEND,
900 result); 912 result);
901 cache_pending_ = false; 913 cache_pending_ = false;
902 914
903 if (!ShouldPassThrough()) { 915 if (!ShouldPassThrough()) {
904 cache_key_ = cache_->GenerateCacheKey(request_); 916 cache_key_ = cache_->GenerateCacheKey(request_);
905 917
906 // Requested cache access mode. 918 // Requested cache access mode.
907 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { 919 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
908 if (effective_load_flags_ & LOAD_BYPASS_CACHE) { 920 if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
909 // The client has asked for nonsense. 921 // The client has asked for nonsense.
910 TransitionToState(STATE_NONE); 922 TransitionToState(STATE_FINISH_HEADERS);
911 return ERR_CACHE_MISS; 923 return ERR_CACHE_MISS;
912 } 924 }
913 mode_ = READ; 925 mode_ = READ;
914 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) { 926 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
915 mode_ = WRITE; 927 mode_ = WRITE;
916 } else { 928 } else {
917 mode_ = READ_WRITE; 929 mode_ = READ_WRITE;
918 } 930 }
919 931
920 // Downgrade to UPDATE if the request has been externally conditionalized. 932 // Downgrade to UPDATE if the request has been externally conditionalized.
921 if (external_validation_.initialized) { 933 if (external_validation_.initialized) {
922 if (mode_ & WRITE) { 934 if (mode_ & WRITE) {
923 // Strip off the READ_DATA bit (and maybe add back a READ_META bit 935 // Strip off the READ_DATA bit (and maybe add back a READ_META bit
924 // in case READ was off). 936 // in case READ was off).
925 mode_ = UPDATE; 937 mode_ = UPDATE;
926 } else { 938 } else {
927 mode_ = NONE; 939 mode_ = NONE;
928 } 940 }
929 } 941 }
930 } 942 }
931 943
932 // Use PUT and DELETE only to invalidate existing stored entries. 944 // Use PUT and DELETE only to invalidate existing stored entries.
933 if ((request_->method == "PUT" || request_->method == "DELETE") && 945 if ((method_ == "PUT" || method_ == "DELETE") && mode_ != READ_WRITE &&
934 mode_ != READ_WRITE && mode_ != WRITE) { 946 mode_ != WRITE) {
935 mode_ = NONE; 947 mode_ = NONE;
936 } 948 }
937 949
938 // Note that if mode_ == UPDATE (which is tied to external_validation_), the 950 // Note that if mode_ == UPDATE (which is tied to external_validation_), the
939 // transaction behaves the same for GET and HEAD requests at this point: if it 951 // transaction behaves the same for GET and HEAD requests at this point: if it
940 // was not modified, the entry is updated and a response is not returned from 952 // was not modified, the entry is updated and a response is not returned from
941 // the cache. If we receive 200, it doesn't matter if there was a validation 953 // the cache. If we receive 200, it doesn't matter if there was a validation
942 // header or not. 954 // header or not.
943 if (request_->method == "HEAD" && mode_ == WRITE) 955 if (method_ == "HEAD" && mode_ == WRITE)
944 mode_ = NONE; 956 mode_ = NONE;
945 957
946 // If must use cache, then we must fail. This can happen for back/forward 958 // If must use cache, then we must fail. This can happen for back/forward
947 // navigations to a page generated via a form post. 959 // navigations to a page generated via a form post.
948 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { 960 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
949 TransitionToState(STATE_NONE); 961 TransitionToState(STATE_FINISH_HEADERS);
950 return ERR_CACHE_MISS; 962 return ERR_CACHE_MISS;
951 } 963 }
952 964
953 if (mode_ == NONE) { 965 if (mode_ == NONE) {
954 if (partial_) { 966 if (partial_) {
955 partial_->RestoreHeaders(&custom_request_->extra_headers); 967 partial_->RestoreHeaders(&custom_request_->extra_headers);
956 partial_.reset(); 968 partial_.reset();
957 } 969 }
958 TransitionToState(STATE_SEND_REQUEST); 970 TransitionToState(STATE_SEND_REQUEST);
959 } else { 971 } else {
960 TransitionToState(STATE_INIT_ENTRY); 972 TransitionToState(STATE_INIT_ENTRY);
961 } 973 }
962 974
963 // This is only set if we have something to do with the response. 975 // This is only set if we have something to do with the response.
964 range_requested_ = (partial_.get() != NULL); 976 range_requested_ = (partial_.get() != NULL);
965 977
978 // mode_ may change later, save the initial mode in case we need to restart
979 // this request.
980 restart_info_.mode = mode_;
966 return OK; 981 return OK;
967 } 982 }
968 983
969 int HttpCache::Transaction::DoInitEntry() { 984 int HttpCache::Transaction::DoInitEntry() {
970 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); 985 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry");
971 DCHECK(!new_entry_); 986 DCHECK(!new_entry_);
972 987
973 if (!cache_.get()) { 988 if (!cache_.get()) {
974 TransitionToState(STATE_NONE); 989 TransitionToState(STATE_FINISH_HEADERS);
975 return ERR_UNEXPECTED; 990 return ERR_UNEXPECTED;
976 } 991 }
977 992
978 if (mode_ == WRITE) { 993 if (mode_ == WRITE) {
979 TransitionToState(STATE_DOOM_ENTRY); 994 TransitionToState(STATE_DOOM_ENTRY);
980 return OK; 995 return OK;
981 } 996 }
982 997
983 TransitionToState(STATE_OPEN_ENTRY); 998 TransitionToState(STATE_OPEN_ENTRY);
984 return OK; 999 return OK;
(...skipping 16 matching lines...) Expand all
1001 // transaction attached. 1016 // transaction attached.
1002 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_OPEN_ENTRY, 1017 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_OPEN_ENTRY,
1003 result); 1018 result);
1004 cache_pending_ = false; 1019 cache_pending_ = false;
1005 if (result == OK) { 1020 if (result == OK) {
1006 TransitionToState(STATE_ADD_TO_ENTRY); 1021 TransitionToState(STATE_ADD_TO_ENTRY);
1007 return OK; 1022 return OK;
1008 } 1023 }
1009 1024
1010 if (result == ERR_CACHE_RACE) { 1025 if (result == ERR_CACHE_RACE) {
1011 TransitionToState(STATE_INIT_ENTRY); 1026 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED);
1012 return OK; 1027 return OK;
1013 } 1028 }
1014 1029
1015 if (request_->method == "PUT" || request_->method == "DELETE" || 1030 if (method_ == "PUT" || method_ == "DELETE" ||
1016 (request_->method == "HEAD" && mode_ == READ_WRITE)) { 1031 (method_ == "HEAD" && mode_ == READ_WRITE)) {
1017 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD"); 1032 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || method_ == "HEAD");
1018 mode_ = NONE; 1033 mode_ = NONE;
1019 TransitionToState(STATE_SEND_REQUEST); 1034 TransitionToState(STATE_SEND_REQUEST);
1020 return OK; 1035 return OK;
1021 } 1036 }
1022 1037
1023 if (mode_ == READ_WRITE) { 1038 if (mode_ == READ_WRITE) {
1024 mode_ = WRITE; 1039 mode_ = WRITE;
1025 TransitionToState(STATE_CREATE_ENTRY); 1040 TransitionToState(STATE_CREATE_ENTRY);
1026 return OK; 1041 return OK;
1027 } 1042 }
1028 if (mode_ == UPDATE) { 1043 if (mode_ == UPDATE) {
1029 // There is no cache entry to update; proceed without caching. 1044 // There is no cache entry to update; proceed without caching.
1030 mode_ = NONE; 1045 mode_ = NONE;
1031 TransitionToState(STATE_SEND_REQUEST); 1046 TransitionToState(STATE_SEND_REQUEST);
1032 return OK; 1047 return OK;
1033 } 1048 }
1034 1049
1035 // The entry does not exist, and we are not permitted to create a new entry, 1050 // The entry does not exist, and we are not permitted to create a new entry,
1036 // so we must fail. 1051 // so we must fail.
1037 TransitionToState(STATE_NONE); 1052 TransitionToState(STATE_FINISH_HEADERS);
1038 return ERR_CACHE_MISS; 1053 return ERR_CACHE_MISS;
1039 } 1054 }
1040 1055
1041 int HttpCache::Transaction::DoDoomEntry() { 1056 int HttpCache::Transaction::DoDoomEntry() {
1042 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntry"); 1057 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntry");
1043 TransitionToState(STATE_DOOM_ENTRY_COMPLETE); 1058 TransitionToState(STATE_DOOM_ENTRY_COMPLETE);
1044 cache_pending_ = true; 1059 cache_pending_ = true;
1045 if (first_cache_access_since_.is_null()) 1060 if (first_cache_access_since_.is_null())
1046 first_cache_access_since_ = TimeTicks::Now(); 1061 first_cache_access_since_ = TimeTicks::Now();
1047 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_DOOM_ENTRY); 1062 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_DOOM_ENTRY);
1048 return cache_->DoomEntry(cache_key_, this); 1063 return cache_->DoomEntry(cache_key_, this);
1049 } 1064 }
1050 1065
1051 int HttpCache::Transaction::DoDoomEntryComplete(int result) { 1066 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1052 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntryComplete"); 1067 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntryComplete");
1053 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_DOOM_ENTRY, 1068 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_DOOM_ENTRY,
1054 result); 1069 result);
1055 cache_pending_ = false; 1070 cache_pending_ = false;
1056 TransitionToState(result == ERR_CACHE_RACE ? STATE_INIT_ENTRY 1071 TransitionToState(result == ERR_CACHE_RACE
1057 : STATE_CREATE_ENTRY); 1072 ? STATE_HEADERS_PHASE_CANNOT_PROCEED
1073 : STATE_CREATE_ENTRY);
1058 return OK; 1074 return OK;
1059 } 1075 }
1060 1076
1061 int HttpCache::Transaction::DoCreateEntry() { 1077 int HttpCache::Transaction::DoCreateEntry() {
1062 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntry"); 1078 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntry");
1063 DCHECK(!new_entry_); 1079 DCHECK(!new_entry_);
1064 TransitionToState(STATE_CREATE_ENTRY_COMPLETE); 1080 TransitionToState(STATE_CREATE_ENTRY_COMPLETE);
1065 cache_pending_ = true; 1081 cache_pending_ = true;
1066 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_CREATE_ENTRY); 1082 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_CREATE_ENTRY);
1067 return cache_->CreateEntry(cache_key_, &new_entry_, this); 1083 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1068 } 1084 }
1069 1085
1070 int HttpCache::Transaction::DoCreateEntryComplete(int result) { 1086 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1071 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntryComplete"); 1087 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntryComplete");
1072 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is 1088 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is
1073 // OK, otherwise the cache will end up with an active entry without any 1089 // OK, otherwise the cache will end up with an active entry without any
1074 // transaction attached. 1090 // transaction attached.
1075 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_CREATE_ENTRY, 1091 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_CREATE_ENTRY,
1076 result); 1092 result);
1077 cache_pending_ = false; 1093 cache_pending_ = false;
1078 switch (result) { 1094 switch (result) {
1079 case OK: 1095 case OK:
1080 TransitionToState(STATE_ADD_TO_ENTRY); 1096 TransitionToState(STATE_ADD_TO_ENTRY);
1081 break; 1097 break;
1082 1098
1083 case ERR_CACHE_RACE: 1099 case ERR_CACHE_RACE:
1084 TransitionToState(STATE_INIT_ENTRY); 1100 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED);
1085 break; 1101 break;
1086 1102
1087 default: 1103 default:
1088 // We have a race here: Maybe we failed to open the entry and decided to 1104 // We have a race here: Maybe we failed to open the entry and decided to
1089 // create one, but by the time we called create, another transaction 1105 // create one, but by the time we called create, another transaction
1090 // already created the entry. If we want to eliminate this issue, we 1106 // already created the entry. If we want to eliminate this issue, we
1091 // need an atomic OpenOrCreate() method exposed by the disk cache. 1107 // need an atomic OpenOrCreate() method exposed by the disk cache.
1092 DLOG(WARNING) << "Unable to create cache entry"; 1108 DLOG(WARNING) << "Unable to create cache entry";
1093 mode_ = NONE; 1109 mode_ = NONE;
1094 if (partial_) 1110 if (partial_)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 DCHECK(new_entry_); 1171 DCHECK(new_entry_);
1156 cache_pending_ = false; 1172 cache_pending_ = false;
1157 1173
1158 if (result == OK) 1174 if (result == OK)
1159 entry_ = new_entry_; 1175 entry_ = new_entry_;
1160 1176
1161 // If there is a failure, the cache should have taken care of new_entry_. 1177 // If there is a failure, the cache should have taken care of new_entry_.
1162 new_entry_ = NULL; 1178 new_entry_ = NULL;
1163 1179
1164 if (result == ERR_CACHE_RACE) { 1180 if (result == ERR_CACHE_RACE) {
1165 TransitionToState(STATE_INIT_ENTRY); 1181 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED);
1166 return OK; 1182 return OK;
1167 } 1183 }
1168 1184
1169 if (result == ERR_CACHE_LOCK_TIMEOUT) { 1185 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1170 if (mode_ == READ) { 1186 if (mode_ == READ) {
1171 TransitionToState(STATE_NONE); 1187 TransitionToState(STATE_FINISH_HEADERS);
1172 return ERR_CACHE_MISS; 1188 return ERR_CACHE_MISS;
1173 } 1189 }
1174 1190
1175 // The cache is busy, bypass it for this transaction. 1191 // The cache is busy, bypass it for this transaction.
1176 mode_ = NONE; 1192 mode_ = NONE;
1177 TransitionToState(STATE_SEND_REQUEST); 1193 TransitionToState(STATE_SEND_REQUEST);
1178 if (partial_) { 1194 if (partial_) {
1179 partial_->RestoreHeaders(&custom_request_->extra_headers); 1195 partial_->RestoreHeaders(&custom_request_->extra_headers);
1180 partial_.reset(); 1196 partial_.reset();
1181 } 1197 }
1182 return OK; 1198 return OK;
1183 } 1199 }
1184 1200
1185 open_entry_last_used_ = entry_->disk_entry->GetLastUsed(); 1201 // TODO(crbug.com/713354) Access timestamp for histograms only if entry is
1202 // already written, to avoid data race since cache thread can also access
1203 // this.
1204 if (!cache_->IsWritingInProgress(entry_))
1205 open_entry_last_used_ = entry_->disk_entry->GetLastUsed();
1186 1206
1187 // TODO(jkarlin): We should either handle the case or DCHECK. 1207 // TODO(jkarlin): We should either handle the case or DCHECK.
1188 if (result != OK) { 1208 if (result != OK) {
1189 NOTREACHED(); 1209 NOTREACHED();
1190 TransitionToState(STATE_NONE); 1210 TransitionToState(STATE_FINISH_HEADERS);
1191 return result; 1211 return result;
1192 } 1212 }
1193 1213
1194 if (mode_ == WRITE) { 1214 if (mode_ == WRITE) {
1195 if (partial_) 1215 if (partial_)
1196 partial_->RestoreHeaders(&custom_request_->extra_headers); 1216 partial_->RestoreHeaders(&custom_request_->extra_headers);
1197 TransitionToState(STATE_SEND_REQUEST); 1217 TransitionToState(STATE_SEND_REQUEST);
1198 } else { 1218 } else {
1199 // We have to read the headers from the cached entry. 1219 // We have to read the headers from the cached entry.
1200 DCHECK(mode_ & READ_META); 1220 DCHECK(mode_ & READ_META);
(...skipping 18 matching lines...) Expand all
1219 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { 1239 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) {
1220 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadResponseComplete"); 1240 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadResponseComplete");
1221 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, 1241 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO,
1222 result); 1242 result);
1223 if (result != io_buf_len_ || 1243 if (result != io_buf_len_ ||
1224 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, &response_, 1244 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, &response_,
1225 &truncated_)) { 1245 &truncated_)) {
1226 return OnCacheReadError(result, true); 1246 return OnCacheReadError(result, true);
1227 } 1247 }
1228 1248
1229 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); 1249 // TODO(crbug.com/713354) Only get data size if there is no other transaction
1230 int64_t full_response_length = response_.headers->GetContentLength(); 1250 // currently writing the response body due to the data race mentioned in the
1251 // associated bug.
1252 if (!cache_->IsWritingInProgress(entry_)) {
1253 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex);
1254 int64_t full_response_length = response_.headers->GetContentLength();
1231 1255
1232 // Some resources may have slipped in as truncated when they're not. 1256 // Some resources may have slipped in as truncated when they're not.
1233 if (full_response_length == current_size) 1257 if (full_response_length == current_size)
1234 truncated_ = false; 1258 truncated_ = false;
1235 1259
1236 // The state machine's handling of StopCaching unfortunately doesn't deal well 1260 // The state machine's handling of StopCaching unfortunately doesn't deal
1237 // with resources that are larger than 2GB when there is a truncated or sparse 1261 // well with resources that are larger than 2GB when there is a truncated or
1238 // cache entry. While the state machine is reworked to resolve this, the 1262 // sparse cache entry. While the state machine is reworked to resolve this,
1239 // following logic is put in place to defer such requests to the network. The 1263 // the following logic is put in place to defer such requests to the
1240 // cache should not be storing multi gigabyte resources. See 1264 // network. The cache should not be storing multi gigabyte resources. See
1241 // http://crbug.com/89567. 1265 // http://crbug.com/89567.
1242 if ((truncated_ || response_.headers->response_code() == 206) && 1266 if ((truncated_ || response_.headers->response_code() == 206) &&
1243 !range_requested_ && 1267 !range_requested_ &&
1244 full_response_length > std::numeric_limits<int32_t>::max()) { 1268 full_response_length > std::numeric_limits<int32_t>::max()) {
1245 // Does not release the cache entry. If another transaction wants to use 1269 DCHECK(!partial_);
1246 // this cache entry while this transaction is active, the second transaction 1270
1247 // will fall back to the network after the timeout. 1271 // Doom the entry so that no other transaction gets added to this entry
1248 DCHECK(!partial_); 1272 // and avoid a race of not being able to check this condition because
1249 mode_ = NONE; 1273 // writing is in progress.
1250 TransitionToState(STATE_SEND_REQUEST); 1274 cache_->DoneWritingToEntry(entry_, false, this);
1251 return OK; 1275 entry_ = nullptr;
1276 mode_ = NONE;
1277 TransitionToState(STATE_SEND_REQUEST);
1278 return OK;
1279 }
1252 } 1280 }
1253 1281
1254 if (response_.unused_since_prefetch != 1282 if (response_.unused_since_prefetch !=
1255 !!(request_->load_flags & LOAD_PREFETCH)) { 1283 !!(request_->load_flags & LOAD_PREFETCH)) {
1256 // Either this is the first use of an entry since it was prefetched XOR 1284 // Either this is the first use of an entry since it was prefetched XOR
1257 // this is a prefetch. The value of response.unused_since_prefetch is 1285 // this is a prefetch. The value of response.unused_since_prefetch is
1258 // valid for this transaction but the bit needs to be flipped in storage. 1286 // valid for this transaction but the bit needs to be flipped in storage.
1259 TransitionToState(STATE_TOGGLE_UNUSED_SINCE_PREFETCH); 1287 TransitionToState(STATE_TOGGLE_UNUSED_SINCE_PREFETCH);
1260 return OK; 1288 return OK;
1261 } 1289 }
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
1323 } 1351 }
1324 1352
1325 int HttpCache::Transaction::DoCacheQueryData() { 1353 int HttpCache::Transaction::DoCacheQueryData() {
1326 TransitionToState(STATE_CACHE_QUERY_DATA_COMPLETE); 1354 TransitionToState(STATE_CACHE_QUERY_DATA_COMPLETE);
1327 return entry_->disk_entry->ReadyForSparseIO(io_callback_); 1355 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1328 } 1356 }
1329 1357
1330 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { 1358 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1331 DCHECK_EQ(OK, result); 1359 DCHECK_EQ(OK, result);
1332 if (!cache_.get()) { 1360 if (!cache_.get()) {
1333 TransitionToState(STATE_NONE); 1361 TransitionToState(STATE_FINISH_HEADERS);
1334 return ERR_UNEXPECTED; 1362 return ERR_UNEXPECTED;
1335 } 1363 }
1336 1364
1337 return ValidateEntryHeadersAndContinue(); 1365 return ValidateEntryHeadersAndContinue();
1338 } 1366 }
1339 1367
1340 // We may end up here multiple times for a given request. 1368 // We may end up here multiple times for a given request.
1341 int HttpCache::Transaction::DoStartPartialCacheValidation() { 1369 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1342 if (mode_ == NONE) { 1370 if (mode_ == NONE) {
1343 TransitionToState(STATE_NONE); 1371 TransitionToState(STATE_FINISH_HEADERS);
1344 return OK; 1372 return OK;
1345 } 1373 }
1346 1374
1347 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); 1375 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION);
1348 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); 1376 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1349 } 1377 }
1350 1378
1351 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { 1379 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1352 if (!result) { 1380 if (!result) {
1353 // This is the end of the request. 1381 // This is the end of the request.
1354 if (mode_ & WRITE) { 1382 if (mode_ & WRITE) {
1355 DoneWritingToEntry(true); 1383 DoneWritingToEntry(true);
1356 } else { 1384 } else {
1357 cache_->DoneReadingFromEntry(entry_, this); 1385 cache_->DoneWithEntry(entry_, this, false, partial_ != nullptr);
1358 entry_ = NULL; 1386 entry_ = NULL;
1359 } 1387 }
1360 TransitionToState(STATE_NONE); 1388 TransitionToState(STATE_FINISH_HEADERS);
1361 return result; 1389 return result;
1362 } 1390 }
1363 1391
1364 if (result < 0) { 1392 if (result < 0) {
1365 TransitionToState(STATE_NONE); 1393 TransitionToState(STATE_FINISH_HEADERS);
1366 return result; 1394 return result;
1367 } 1395 }
1368 1396
1369 partial_->PrepareCacheValidation(entry_->disk_entry, 1397 partial_->PrepareCacheValidation(entry_->disk_entry,
1370 &custom_request_->extra_headers); 1398 &custom_request_->extra_headers);
1371 1399
1372 if (reading_ && partial_->IsCurrentRangeCached()) { 1400 if (reading_ && partial_->IsCurrentRangeCached()) {
1373 TransitionToState(STATE_CACHE_READ_DATA); 1401 TransitionToState(STATE_CACHE_READ_DATA);
1374 return OK; 1402 return OK;
1375 } 1403 }
1376 1404
1377 return BeginCacheValidation(); 1405 return BeginCacheValidation();
1378 } 1406 }
1379 1407
1380 int HttpCache::Transaction::DoSendRequest() { 1408 int HttpCache::Transaction::DoSendRequest() {
1381 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequest"); 1409 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequest");
1382 DCHECK(mode_ & WRITE || mode_ == NONE); 1410 DCHECK(mode_ & WRITE || mode_ == NONE);
1383 DCHECK(!network_trans_.get()); 1411 DCHECK(!network_trans_.get());
1384 1412
1385 send_request_since_ = TimeTicks::Now(); 1413 send_request_since_ = TimeTicks::Now();
1386 1414
1387 // Create a network transaction. 1415 // Create a network transaction.
1388 int rv = 1416 int rv =
1389 cache_->network_layer_->CreateTransaction(priority_, &network_trans_); 1417 cache_->network_layer_->CreateTransaction(priority_, &network_trans_);
1390 if (rv != OK) { 1418 if (rv != OK) {
1391 TransitionToState(STATE_NONE); 1419 TransitionToState(STATE_FINISH_HEADERS);
1392 return rv; 1420 return rv;
1393 } 1421 }
1394 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_); 1422 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1395 network_trans_->SetBeforeHeadersSentCallback(before_headers_sent_callback_); 1423 network_trans_->SetBeforeHeadersSentCallback(before_headers_sent_callback_);
1396 1424
1397 // Old load timing information, if any, is now obsolete. 1425 // Old load timing information, if any, is now obsolete.
1398 old_network_trans_load_timing_.reset(); 1426 old_network_trans_load_timing_.reset();
1399 old_remote_endpoint_ = IPEndPoint(); 1427 old_remote_endpoint_ = IPEndPoint();
1400 1428
1401 if (websocket_handshake_stream_base_create_helper_) 1429 if (websocket_handshake_stream_base_create_helper_)
1402 network_trans_->SetWebSocketHandshakeStreamCreateHelper( 1430 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1403 websocket_handshake_stream_base_create_helper_); 1431 websocket_handshake_stream_base_create_helper_);
1404 1432
1405 TransitionToState(STATE_SEND_REQUEST_COMPLETE); 1433 TransitionToState(STATE_SEND_REQUEST_COMPLETE);
1406 rv = network_trans_->Start(request_, io_callback_, net_log_); 1434 rv = network_trans_->Start(request_, io_callback_, net_log_);
1407 return rv; 1435 return rv;
1408 } 1436 }
1409 1437
1410 int HttpCache::Transaction::DoSendRequestComplete(int result) { 1438 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1411 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequestComplete"); 1439 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequestComplete");
1412 if (!cache_.get()) { 1440 if (!cache_.get()) {
1413 TransitionToState(STATE_NONE); 1441 TransitionToState(STATE_FINISH_HEADERS);
1414 return ERR_UNEXPECTED; 1442 return ERR_UNEXPECTED;
1415 } 1443 }
1416 1444
1417 // If we tried to conditionalize the request and failed, we know 1445 // If we tried to conditionalize the request and failed, we know
1418 // we won't be reading from the cache after this point. 1446 // we won't be reading from the cache after this point.
1419 if (couldnt_conditionalize_request_) 1447 if (couldnt_conditionalize_request_)
1420 mode_ = WRITE; 1448 mode_ = WRITE;
1421 1449
1422 if (result == OK) { 1450 if (result == OK) {
1423 TransitionToState(STATE_SUCCESSFUL_SEND_REQUEST); 1451 TransitionToState(STATE_SUCCESSFUL_SEND_REQUEST);
(...skipping 10 matching lines...) Expand all
1434 // so GetResponseInfo() should never return NULL here. 1462 // so GetResponseInfo() should never return NULL here.
1435 DCHECK(response); 1463 DCHECK(response);
1436 response_.ssl_info = response->ssl_info; 1464 response_.ssl_info = response->ssl_info;
1437 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { 1465 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1438 DCHECK(response); 1466 DCHECK(response);
1439 response_.cert_request_info = response->cert_request_info; 1467 response_.cert_request_info = response->cert_request_info;
1440 } else if (response_.was_cached) { 1468 } else if (response_.was_cached) {
1441 DoneWritingToEntry(true); 1469 DoneWritingToEntry(true);
1442 } 1470 }
1443 1471
1444 TransitionToState(STATE_NONE); 1472 TransitionToState(STATE_FINISH_HEADERS);
1445 return result; 1473 return result;
1446 } 1474 }
1447 1475
1448 // We received the response headers and there is no error. 1476 // We received the response headers and there is no error.
1449 int HttpCache::Transaction::DoSuccessfulSendRequest() { 1477 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1450 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); 1478 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest");
1451 DCHECK(!new_response_); 1479 DCHECK(!new_response_);
1452 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); 1480 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1453 1481
1454 if (new_response->headers->response_code() == 401 || 1482 if (new_response->headers->response_code() == 401 ||
1455 new_response->headers->response_code() == 407) { 1483 new_response->headers->response_code() == 407) {
1456 SetAuthResponse(*new_response); 1484 SetAuthResponse(*new_response);
1457 if (!reading_) { 1485 if (!reading_) {
1458 TransitionToState(STATE_NONE); 1486 TransitionToState(STATE_FINISH_HEADERS);
1459 return OK; 1487 return OK;
1460 } 1488 }
1461 1489
1462 // We initiated a second request the caller doesn't know about. We should be 1490 // We initiated a second request the caller doesn't know about. We should be
1463 // able to authenticate this request because we should have authenticated 1491 // able to authenticate this request because we should have authenticated
1464 // this URL moments ago. 1492 // this URL moments ago.
1465 if (IsReadyToRestartForAuth()) { 1493 if (IsReadyToRestartForAuth()) {
1466 DCHECK(!response_.auth_challenge.get()); 1494 DCHECK(!response_.auth_challenge.get());
1467 TransitionToState(STATE_SEND_REQUEST_COMPLETE); 1495 TransitionToState(STATE_SEND_REQUEST_COMPLETE);
1468 // In theory we should check to see if there are new cookies, but there 1496 // In theory we should check to see if there are new cookies, but there
1469 // is no way to do that from here. 1497 // is no way to do that from here.
1470 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_); 1498 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
1471 } 1499 }
1472 1500
1473 // We have to perform cleanup at this point so that at least the next 1501 // We have to perform cleanup at this point so that at least the next
1474 // request can succeed. We do not retry at this point, because data 1502 // request can succeed. We do not retry at this point, because data
1475 // has been read and we have no way to gather credentials. We would 1503 // has been read and we have no way to gather credentials. We would
1476 // fail again, and potentially loop. This can happen if the credentials 1504 // fail again, and potentially loop. This can happen if the credentials
1477 // expire while chrome is suspended. 1505 // expire while chrome is suspended.
1478 if (entry_) 1506 if (entry_)
1479 DoomPartialEntry(false); 1507 DoomPartialEntry(false);
1480 mode_ = NONE; 1508 mode_ = NONE;
1481 partial_.reset(); 1509 partial_.reset();
1482 ResetNetworkTransaction(); 1510 ResetNetworkTransaction();
1483 TransitionToState(STATE_NONE); 1511 TransitionToState(STATE_FINISH_HEADERS);
1484 return ERR_CACHE_AUTH_FAILURE_AFTER_READ; 1512 return ERR_CACHE_AUTH_FAILURE_AFTER_READ;
1485 } 1513 }
1486 1514
1487 new_response_ = new_response; 1515 new_response_ = new_response;
1488 if (!ValidatePartialResponse() && !auth_response_.headers.get()) { 1516 if (!ValidatePartialResponse() && !auth_response_.headers.get()) {
1489 // Something went wrong with this request and we have to restart it. 1517 // Something went wrong with this request and we have to restart it.
1490 // If we have an authentication response, we are exposed to weird things 1518 // If we have an authentication response, we are exposed to weird things
1491 // hapenning if the user cancels the authentication before we receive 1519 // hapenning if the user cancels the authentication before we receive
1492 // the new response. 1520 // the new response.
1493 net_log_.AddEvent(NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST); 1521 net_log_.AddEvent(NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST);
(...skipping 11 matching lines...) Expand all
1505 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); 1533 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER);
1506 DoneWritingToEntry(false); 1534 DoneWritingToEntry(false);
1507 } 1535 }
1508 1536
1509 if (mode_ == WRITE && 1537 if (mode_ == WRITE &&
1510 cache_entry_status_ != CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE) { 1538 cache_entry_status_ != CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE) {
1511 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); 1539 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE);
1512 } 1540 }
1513 1541
1514 // Invalidate any cached GET with a successful PUT or DELETE. 1542 // Invalidate any cached GET with a successful PUT or DELETE.
1515 if (mode_ == WRITE && 1543 if (mode_ == WRITE && (method_ == "PUT" || method_ == "DELETE")) {
1516 (request_->method == "PUT" || request_->method == "DELETE")) {
1517 if (NonErrorResponse(new_response->headers->response_code())) { 1544 if (NonErrorResponse(new_response->headers->response_code())) {
1518 int ret = cache_->DoomEntry(cache_key_, NULL); 1545 int ret = cache_->DoomEntry(cache_key_, NULL);
1519 DCHECK_EQ(OK, ret); 1546 DCHECK_EQ(OK, ret);
1520 } 1547 }
1521 cache_->DoneWritingToEntry(entry_, true); 1548 cache_->DoneWritingToEntry(entry_, true, this);
1522 entry_ = NULL; 1549 entry_ = NULL;
1523 mode_ = NONE; 1550 mode_ = NONE;
1524 } 1551 }
1525 1552
1526 // Invalidate any cached GET with a successful POST. 1553 // Invalidate any cached GET with a successful POST.
1527 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && 1554 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && method_ == "POST" &&
1528 request_->method == "POST" &&
1529 NonErrorResponse(new_response->headers->response_code())) { 1555 NonErrorResponse(new_response->headers->response_code())) {
1530 cache_->DoomMainEntryForUrl(request_->url); 1556 cache_->DoomMainEntryForUrl(request_->url);
1531 } 1557 }
1532 1558
1533 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); 1559 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1534 1560
1535 if (new_response_->headers->response_code() == 416 && 1561 if (new_response_->headers->response_code() == 416 &&
1536 (request_->method == "GET" || request_->method == "POST")) { 1562 (method_ == "GET" || method_ == "POST")) {
1537 // If there is an active entry it may be destroyed with this transaction. 1563 // If there is an active entry it may be destroyed with this transaction.
1538 SetResponse(*new_response_); 1564 SetResponse(*new_response_);
1539 TransitionToState(STATE_NONE); 1565 TransitionToState(STATE_FINISH_HEADERS);
1540 return OK; 1566 return OK;
1541 } 1567 }
1542 1568
1543 // Are we expecting a response to a conditional query? 1569 // Are we expecting a response to a conditional query?
1544 if (mode_ == READ_WRITE || mode_ == UPDATE) { 1570 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1545 if (new_response->headers->response_code() == 304 || handling_206_) { 1571 if (new_response->headers->response_code() == 304 || handling_206_) {
1546 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); 1572 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED);
1547 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); 1573 TransitionToState(STATE_UPDATE_CACHED_RESPONSE);
1548 return OK; 1574 return OK;
1549 } 1575 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
1617 DCHECK(!handling_206_); 1643 DCHECK(!handling_206_);
1618 // We got a "not modified" response and already updated the corresponding 1644 // We got a "not modified" response and already updated the corresponding
1619 // cache entry above. 1645 // cache entry above.
1620 // 1646 //
1621 // By closing the cached entry now, we make sure that the 304 rather than 1647 // By closing the cached entry now, we make sure that the 304 rather than
1622 // the cached 200 response, is what will be returned to the user. 1648 // the cached 200 response, is what will be returned to the user.
1623 DoneWritingToEntry(true); 1649 DoneWritingToEntry(true);
1624 } else if (entry_ && !handling_206_) { 1650 } else if (entry_ && !handling_206_) {
1625 DCHECK_EQ(READ_WRITE, mode_); 1651 DCHECK_EQ(READ_WRITE, mode_);
1626 if (!partial_ || partial_->IsLastRange()) { 1652 if (!partial_ || partial_->IsLastRange()) {
1627 cache_->ConvertWriterToReader(entry_);
1628 mode_ = READ; 1653 mode_ = READ;
1629 } 1654 }
1630 // We no longer need the network transaction, so destroy it. 1655 // We no longer need the network transaction, so destroy it.
1631 ResetNetworkTransaction(); 1656 ResetNetworkTransaction();
1632 } else if (entry_ && handling_206_ && truncated_ && 1657 } else if (entry_ && handling_206_ && truncated_ &&
1633 partial_->initial_validation()) { 1658 partial_->initial_validation()) {
1634 // We just finished the validation of a truncated entry, and the server 1659 // We just finished the validation of a truncated entry, and the server
1635 // is willing to resume the operation. Now we go back and start serving 1660 // is willing to resume the operation. Now we go back and start serving
1636 // the first part to the user. 1661 // the first part to the user.
1637 ResetNetworkTransaction(); 1662 ResetNetworkTransaction();
(...skipping 12 matching lines...) Expand all
1650 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1675 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1651 return OK; 1676 return OK;
1652 } 1677 }
1653 1678
1654 // We change the value of Content-Length for partial content. 1679 // We change the value of Content-Length for partial content.
1655 if (handling_206_ && partial_) 1680 if (handling_206_ && partial_)
1656 partial_->FixContentLength(new_response_->headers.get()); 1681 partial_->FixContentLength(new_response_->headers.get());
1657 1682
1658 SetResponse(*new_response_); 1683 SetResponse(*new_response_);
1659 1684
1660 if (request_->method == "HEAD") { 1685 if (method_ == "HEAD") {
1661 // This response is replacing the cached one. 1686 // This response is replacing the cached one.
1662 DoneWritingToEntry(false); 1687 DoneWritingToEntry(false);
1663 mode_ = NONE; 1688 mode_ = NONE;
1664 new_response_ = NULL; 1689 new_response_ = NULL;
1665 TransitionToState(STATE_NONE); 1690 TransitionToState(STATE_FINISH_HEADERS);
1666 return OK; 1691 return OK;
1667 } 1692 }
1668 1693
1669 if (handling_206_ && !CanResume(false)) { 1694 if (handling_206_ && !CanResume(false)) {
1670 // There is no point in storing this resource because it will never be used. 1695 // There is no point in storing this resource because it will never be used.
1671 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries. 1696 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1672 DoneWritingToEntry(false); 1697 DoneWritingToEntry(false);
1673 if (partial_) 1698 if (partial_)
1674 partial_->FixResponseHeaders(response_.headers.get(), true); 1699 partial_->FixResponseHeaders(response_.headers.get(), true);
1675 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1700 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1676 return OK; 1701 return OK;
1677 } 1702 }
1678 1703
1679 TransitionToState(STATE_CACHE_WRITE_RESPONSE); 1704 TransitionToState(STATE_CACHE_WRITE_RESPONSE);
1680 return OK; 1705 return OK;
1681 } 1706 }
1682 1707
1683 int HttpCache::Transaction::DoCacheWriteResponse() { 1708 int HttpCache::Transaction::DoCacheWriteResponse() {
1684 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponse"); 1709 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponse");
1685 TransitionToState(STATE_CACHE_WRITE_RESPONSE_COMPLETE); 1710 TransitionToState(STATE_CACHE_WRITE_RESPONSE_COMPLETE);
1711
1712 // Invalidate any current entry with a successful response if this transaction
1713 // cannot write to this entry. This transaction then continues to read from
1714 // the network without writing to the backend.
1715 bool is_match = response_.headers->response_code() == 304;
1716 if (entry_ && response_.headers &&
1717 !cache_->CanTransactionWriteResponseHeaders(entry_, this, is_match)) {
1718 cache_->DoneWritingToEntry(entry_, false, this);
1719 entry_ = nullptr;
1720 mode_ = NONE;
1721 return OK;
1722 }
1723
1686 return WriteResponseInfoToEntry(truncated_); 1724 return WriteResponseInfoToEntry(truncated_);
1687 } 1725 }
1688 1726
1689 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) { 1727 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1690 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponseComplete"); 1728 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponseComplete");
1691 TransitionToState(STATE_TRUNCATE_CACHED_DATA); 1729 TransitionToState(STATE_TRUNCATE_CACHED_DATA);
1692 return OnWriteResponseInfoToEntryComplete(result); 1730 return OnWriteResponseInfoToEntryComplete(result);
1693 } 1731 }
1694 1732
1695 int HttpCache::Transaction::DoTruncateCachedData() { 1733 int HttpCache::Transaction::DoTruncateCachedData() {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1737 } 1775 }
1738 1776
1739 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1777 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1740 return OK; 1778 return OK;
1741 } 1779 }
1742 1780
1743 int HttpCache::Transaction::DoPartialHeadersReceived() { 1781 int HttpCache::Transaction::DoPartialHeadersReceived() {
1744 new_response_ = NULL; 1782 new_response_ = NULL;
1745 1783
1746 if (!partial_) { 1784 if (!partial_) {
1747 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) 1785 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) {
1748 TransitionToState(STATE_CACHE_READ_METADATA); 1786 TransitionToState(STATE_CACHE_READ_METADATA);
1749 else 1787 } else {
1750 TransitionToState(STATE_NONE); 1788 DCHECK(!reading_);
1789 TransitionToState(STATE_FINISH_HEADERS);
1790 }
1751 return OK; 1791 return OK;
1752 } 1792 }
1753 1793
1754 if (reading_) { 1794 if (reading_) {
1755 if (network_trans_.get()) { 1795 if (network_trans_.get()) {
1756 TransitionToState(STATE_NETWORK_READ); 1796 TransitionToState(STATE_NETWORK_READ);
1757 } else { 1797 } else {
1758 TransitionToState(STATE_CACHE_READ_DATA); 1798 TransitionToState(STATE_CACHE_READ_DATA);
1759 } 1799 }
1760 } else if (mode_ != NONE) { 1800 } else if (mode_ != NONE) {
1761 // We are about to return the headers for a byte-range request to the user, 1801 // We are about to return the headers for a byte-range request to the user,
1762 // so let's fix them. 1802 // so let's fix them.
1763 partial_->FixResponseHeaders(response_.headers.get(), true); 1803 partial_->FixResponseHeaders(response_.headers.get(), true);
1764 TransitionToState(STATE_NONE); 1804 TransitionToState(STATE_FINISH_HEADERS);
1765 } else { 1805 } else {
1766 TransitionToState(STATE_NONE); 1806 TransitionToState(STATE_FINISH_HEADERS);
1767 } 1807 }
1768 return OK; 1808 return OK;
1769 } 1809 }
1770 1810
1811 int HttpCache::Transaction::DoHeadersPhaseCannotProceed() {
1812 // If its the Start state machine and it cannot proceed due to a cache
1813 // failure, restart this transaction.
1814 DCHECK(!reading_);
1815 TransitionToState(STATE_INIT_ENTRY);
1816 cache_entry_status_ = restart_info_.cache_entry_status;
1817 entry_ = nullptr;
1818 mode_ = restart_info_.mode;
1819 if (network_trans_)
1820 network_trans_.reset();
1821
1822 return OK;
1823 }
1824
1825 int HttpCache::Transaction::DoFinishHeaders(int result) {
1826 if (!entry_ || result != OK) {
1827 TransitionToState(STATE_NONE);
1828 return result;
1829 }
1830
1831 TransitionToState(STATE_FINISH_HEADERS_COMPLETE);
1832
1833 // If it was an auth failure or 416, this transaction should continue to be
1834 // headers_transaction till consumer takes an action, so no need to do
1835 // anything now.
1836 if (auth_response_.headers.get() ||
1837 (new_response_ && new_response_->headers &&
1838 new_response_->headers->response_code() == 416))
1839 return OK;
1840
1841 // If the transaction needs to wait because another transaction is still
1842 // writing the response body, it will return ERR_IO_PENDING now and the
1843 // io_callback_ will be invoked when the wait is done.
1844 return cache_->DoneWithResponseHeaders(entry_, this, partial_ != nullptr);
1845 }
1846
1847 int HttpCache::Transaction::DoFinishHeadersComplete(int rv) {
1848 if (rv == ERR_CACHE_RACE) {
1849 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED);
1850 return OK;
1851 }
1852
1853 TransitionToState(STATE_NONE);
1854 return rv;
1855 }
1856
1771 int HttpCache::Transaction::DoCacheReadMetadata() { 1857 int HttpCache::Transaction::DoCacheReadMetadata() {
1772 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); 1858 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata");
1773 DCHECK(entry_); 1859 DCHECK(entry_);
1774 DCHECK(!response_.metadata.get()); 1860 DCHECK(!response_.metadata.get());
1775 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); 1861 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE);
1776 1862
1777 response_.metadata = 1863 response_.metadata =
1778 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); 1864 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1779 1865
1780 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); 1866 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO);
1781 return entry_->disk_entry->ReadData(kMetadataIndex, 0, 1867 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1782 response_.metadata.get(), 1868 response_.metadata.get(),
1783 response_.metadata->size(), 1869 response_.metadata->size(),
1784 io_callback_); 1870 io_callback_);
1785 } 1871 }
1786 1872
1787 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { 1873 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1788 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); 1874 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete");
1789 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, 1875 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO,
1790 result); 1876 result);
1791 if (result != response_.metadata->size()) 1877 if (result != response_.metadata->size())
1792 return OnCacheReadError(result, false); 1878 return OnCacheReadError(result, false);
1793 TransitionToState(STATE_NONE); 1879
1880 TransitionToState(STATE_FINISH_HEADERS);
1794 return OK; 1881 return OK;
1795 } 1882 }
1796 1883
1797 int HttpCache::Transaction::DoNetworkRead() { 1884 int HttpCache::Transaction::DoNetworkRead() {
1798 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); 1885 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead");
1799 TransitionToState(STATE_NETWORK_READ_COMPLETE); 1886 TransitionToState(STATE_NETWORK_READ_COMPLETE);
1800 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); 1887 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1801 } 1888 }
1802 1889
1803 int HttpCache::Transaction::DoNetworkReadComplete(int result) { 1890 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
(...skipping 12 matching lines...) Expand all
1816 return result; 1903 return result;
1817 } 1904 }
1818 1905
1819 TransitionToState(STATE_CACHE_WRITE_DATA); 1906 TransitionToState(STATE_CACHE_WRITE_DATA);
1820 return result; 1907 return result;
1821 } 1908 }
1822 1909
1823 int HttpCache::Transaction::DoCacheReadData() { 1910 int HttpCache::Transaction::DoCacheReadData() {
1824 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadData"); 1911 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadData");
1825 1912
1826 if (request_->method == "HEAD") { 1913 if (method_ == "HEAD") {
1827 TransitionToState(STATE_NONE); 1914 TransitionToState(STATE_NONE);
1828 return 0; 1915 return 0;
1829 } 1916 }
1830 1917
1831 DCHECK(entry_); 1918 DCHECK(entry_);
1832 TransitionToState(STATE_CACHE_READ_DATA_COMPLETE); 1919 TransitionToState(STATE_CACHE_READ_DATA_COMPLETE);
1833 1920
1834 if (net_log_.IsCapturing()) 1921 if (net_log_.IsCapturing())
1835 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_DATA); 1922 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_DATA);
1836 if (partial_) { 1923 if (partial_) {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
1954 return OnWriteResponseInfoToEntryComplete(result); 2041 return OnWriteResponseInfoToEntryComplete(result);
1955 } 2042 }
1956 2043
1957 //----------------------------------------------------------------------------- 2044 //-----------------------------------------------------------------------------
1958 2045
1959 void HttpCache::Transaction::SetRequest(const NetLogWithSource& net_log, 2046 void HttpCache::Transaction::SetRequest(const NetLogWithSource& net_log,
1960 const HttpRequestInfo* request) { 2047 const HttpRequestInfo* request) {
1961 net_log_ = net_log; 2048 net_log_ = net_log;
1962 request_ = request; 2049 request_ = request;
1963 effective_load_flags_ = request_->load_flags; 2050 effective_load_flags_ = request_->load_flags;
2051 method_ = request_->method;
1964 2052
1965 if (cache_->mode() == DISABLE) 2053 if (cache_->mode() == DISABLE)
1966 effective_load_flags_ |= LOAD_DISABLE_CACHE; 2054 effective_load_flags_ |= LOAD_DISABLE_CACHE;
1967 2055
1968 // Some headers imply load flags. The order here is significant. 2056 // Some headers imply load flags. The order here is significant.
1969 // 2057 //
1970 // LOAD_DISABLE_CACHE : no cache read or write 2058 // LOAD_DISABLE_CACHE : no cache read or write
1971 // LOAD_BYPASS_CACHE : no cache read 2059 // LOAD_BYPASS_CACHE : no cache read
1972 // LOAD_VALIDATE_CACHE : no cache read unless validation 2060 // LOAD_VALIDATE_CACHE : no cache read unless validation
1973 // 2061 //
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
2033 // a cache validation, since we don't know for sure which header the server 2121 // a cache validation, since we don't know for sure which header the server
2034 // will give us a response for (and they could be contradictory). 2122 // will give us a response for (and they could be contradictory).
2035 if (external_validation_error) { 2123 if (external_validation_error) {
2036 LOG(WARNING) << "Multiple or malformed validation headers found."; 2124 LOG(WARNING) << "Multiple or malformed validation headers found.";
2037 effective_load_flags_ |= LOAD_DISABLE_CACHE; 2125 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2038 } 2126 }
2039 2127
2040 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) { 2128 if (range_found && !(effective_load_flags_ & LOAD_DISABLE_CACHE)) {
2041 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); 2129 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER);
2042 partial_.reset(new PartialData); 2130 partial_.reset(new PartialData);
2043 if (request_->method == "GET" && partial_->Init(request_->extra_headers)) { 2131 if (method_ == "GET" && partial_->Init(request_->extra_headers)) {
2044 // We will be modifying the actual range requested to the server, so 2132 // We will be modifying the actual range requested to the server, so
2045 // let's remove the header here. 2133 // let's remove the header here.
2046 custom_request_.reset(new HttpRequestInfo(*request_)); 2134 custom_request_.reset(new HttpRequestInfo(*request_));
2047 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange); 2135 custom_request_->extra_headers.RemoveHeader(HttpRequestHeaders::kRange);
2048 request_ = custom_request_.get(); 2136 request_ = custom_request_.get();
2049 partial_->SetHeaders(custom_request_->extra_headers); 2137 partial_->SetHeaders(custom_request_->extra_headers);
2050 } else { 2138 } else {
2051 // The range is invalid or we cannot handle it properly. 2139 // The range is invalid or we cannot handle it properly.
2052 VLOG(1) << "Invalid byte range found."; 2140 VLOG(1) << "Invalid byte range found.";
2053 effective_load_flags_ |= LOAD_DISABLE_CACHE; 2141 effective_load_flags_ |= LOAD_DISABLE_CACHE;
2054 partial_.reset(NULL); 2142 partial_.reset(NULL);
2055 } 2143 }
2056 } 2144 }
2145 restart_info_.cache_entry_status = cache_entry_status_;
2057 } 2146 }
2058 2147
2059 bool HttpCache::Transaction::ShouldPassThrough() { 2148 bool HttpCache::Transaction::ShouldPassThrough() {
2060 // We may have a null disk_cache if there is an error we cannot recover from, 2149 // We may have a null disk_cache if there is an error we cannot recover from,
2061 // like not enough disk space, or sharing violations. 2150 // like not enough disk space, or sharing violations.
2062 if (!cache_->disk_cache_.get()) 2151 if (!cache_->disk_cache_.get())
2063 return true; 2152 return true;
2064 2153
2065 if (effective_load_flags_ & LOAD_DISABLE_CACHE) 2154 if (effective_load_flags_ & LOAD_DISABLE_CACHE)
2066 return true; 2155 return true;
2067 2156
2068 if (request_->method == "GET" || request_->method == "HEAD") 2157 if (method_ == "GET" || method_ == "HEAD")
2069 return false; 2158 return false;
2070 2159
2071 if (request_->method == "POST" && request_->upload_data_stream && 2160 if (method_ == "POST" && request_->upload_data_stream &&
2072 request_->upload_data_stream->identifier()) { 2161 request_->upload_data_stream->identifier()) {
2073 return false; 2162 return false;
2074 } 2163 }
2075 2164
2076 if (request_->method == "PUT" && request_->upload_data_stream) 2165 if (method_ == "PUT" && request_->upload_data_stream)
2077 return false; 2166 return false;
2078 2167
2079 if (request_->method == "DELETE") 2168 if (method_ == "DELETE")
2080 return false; 2169 return false;
2081 2170
2082 return true; 2171 return true;
2083 } 2172 }
2084 2173
2085 int HttpCache::Transaction::BeginCacheRead() { 2174 int HttpCache::Transaction::BeginCacheRead() {
2086 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges. 2175 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2087 // TODO(jkarlin): Either handle this case or DCHECK. 2176 // TODO(jkarlin): Either handle this case or DCHECK.
2088 if (response_.headers->response_code() == 206 || partial_) { 2177 if (response_.headers->response_code() == 206 || partial_) {
2089 NOTREACHED(); 2178 NOTREACHED();
2090 TransitionToState(STATE_NONE); 2179 TransitionToState(STATE_FINISH_HEADERS);
2091 return ERR_CACHE_MISS; 2180 return ERR_CACHE_MISS;
2092 } 2181 }
2093 2182
2094 // We don't have the whole resource. 2183 // We don't have the whole resource.
2095 if (truncated_) { 2184 if (truncated_) {
2096 TransitionToState(STATE_NONE); 2185 TransitionToState(STATE_FINISH_HEADERS);
2097 return ERR_CACHE_MISS; 2186 return ERR_CACHE_MISS;
2098 } 2187 }
2099 2188
2100 if (RequiresValidation()) { 2189 if (RequiresValidation()) {
2101 TransitionToState(STATE_NONE); 2190 TransitionToState(STATE_FINISH_HEADERS);
2102 return ERR_CACHE_MISS; 2191 return ERR_CACHE_MISS;
2103 } 2192 }
2104 2193
2105 if (request_->method == "HEAD") 2194 if (method_ == "HEAD")
2106 FixHeadersForHead(); 2195 FixHeadersForHead();
2107 2196
2108 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2197 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2109 TransitionToState(STATE_CACHE_READ_METADATA); 2198 TransitionToState(STATE_CACHE_READ_METADATA);
2110 else 2199 else
2111 TransitionToState(STATE_NONE); 2200 TransitionToState(STATE_FINISH_HEADERS);
2112 2201
2113 return OK; 2202 return OK;
2114 } 2203 }
2115 2204
2116 int HttpCache::Transaction::BeginCacheValidation() { 2205 int HttpCache::Transaction::BeginCacheValidation() {
2117 DCHECK_EQ(mode_, READ_WRITE); 2206 DCHECK_EQ(mode_, READ_WRITE);
2118 2207
2119 bool skip_validation = !RequiresValidation(); 2208 bool skip_validation = !RequiresValidation();
2120 2209
2121 if (request_->method == "HEAD" && 2210 if (method_ == "HEAD" &&
2122 (truncated_ || response_.headers->response_code() == 206)) { 2211 (truncated_ || response_.headers->response_code() == 206)) {
2123 DCHECK(!partial_); 2212 DCHECK(!partial_);
2124 if (skip_validation) 2213 if (skip_validation)
2125 return SetupEntryForRead(); 2214 return SetupEntryForRead();
2126 2215
2127 // Bail out! 2216 // Bail out!
2128 TransitionToState(STATE_SEND_REQUEST); 2217 TransitionToState(STATE_SEND_REQUEST);
2129 mode_ = NONE; 2218 mode_ = NONE;
2130 return OK; 2219 return OK;
2131 } 2220 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
2169 } 2258 }
2170 2259
2171 int HttpCache::Transaction::BeginPartialCacheValidation() { 2260 int HttpCache::Transaction::BeginPartialCacheValidation() {
2172 DCHECK_EQ(mode_, READ_WRITE); 2261 DCHECK_EQ(mode_, READ_WRITE);
2173 2262
2174 if (response_.headers->response_code() != 206 && !partial_ && !truncated_) 2263 if (response_.headers->response_code() != 206 && !partial_ && !truncated_)
2175 return BeginCacheValidation(); 2264 return BeginCacheValidation();
2176 2265
2177 // Partial requests should not be recorded in histograms. 2266 // Partial requests should not be recorded in histograms.
2178 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); 2267 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER);
2179 if (request_->method == "HEAD") 2268 if (method_ == "HEAD")
2180 return BeginCacheValidation(); 2269 return BeginCacheValidation();
2181 2270
2182 if (!range_requested_) { 2271 if (!range_requested_) {
2183 // The request is not for a range, but we have stored just ranges. 2272 // The request is not for a range, but we have stored just ranges.
2184 2273
2185 partial_.reset(new PartialData()); 2274 partial_.reset(new PartialData());
2186 partial_->SetHeaders(request_->extra_headers); 2275 partial_->SetHeaders(request_->extra_headers);
2187 if (!custom_request_.get()) { 2276 if (!custom_request_.get()) {
2188 custom_request_.reset(new HttpRequestInfo(*request_)); 2277 custom_request_.reset(new HttpRequestInfo(*request_));
2189 request_ = custom_request_.get(); 2278 request_ = custom_request_.get();
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
2307 // The first use of a resource after prefetch within a short window skips 2396 // The first use of a resource after prefetch within a short window skips
2308 // validation. 2397 // validation.
2309 return false; 2398 return false;
2310 } 2399 }
2311 2400
2312 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) { 2401 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) {
2313 validation_cause_ = VALIDATION_CAUSE_VALIDATE_FLAG; 2402 validation_cause_ = VALIDATION_CAUSE_VALIDATE_FLAG;
2314 return true; 2403 return true;
2315 } 2404 }
2316 2405
2317 if (request_->method == "PUT" || request_->method == "DELETE") 2406 if (method_ == "PUT" || method_ == "DELETE")
2318 return true; 2407 return true;
2319 2408
2320 bool validation_required_by_headers = response_.headers->RequiresValidation( 2409 bool validation_required_by_headers = response_.headers->RequiresValidation(
2321 response_.request_time, response_.response_time, cache_->clock_->Now()); 2410 response_.request_time, response_.response_time, cache_->clock_->Now());
2322 2411
2323 if (validation_required_by_headers) { 2412 if (validation_required_by_headers) {
2324 HttpResponseHeaders::FreshnessLifetimes lifetimes = 2413 HttpResponseHeaders::FreshnessLifetimes lifetimes =
2325 response_.headers->GetFreshnessLifetimes(response_.response_time); 2414 response_.headers->GetFreshnessLifetimes(response_.response_time);
2326 if (lifetimes.freshness == base::TimeDelta()) { 2415 if (lifetimes.freshness == base::TimeDelta()) {
2327 validation_cause_ = VALIDATION_CAUSE_ZERO_FRESHNESS; 2416 validation_cause_ = VALIDATION_CAUSE_ZERO_FRESHNESS;
2328 } else { 2417 } else {
2329 validation_cause_ = VALIDATION_CAUSE_STALE; 2418 validation_cause_ = VALIDATION_CAUSE_STALE;
2330 stale_entry_freshness_ = lifetimes.freshness; 2419 stale_entry_freshness_ = lifetimes.freshness;
2331 stale_entry_age_ = response_.headers->GetCurrentAge( 2420 stale_entry_age_ = response_.headers->GetCurrentAge(
2332 response_.request_time, response_.response_time, 2421 response_.request_time, response_.response_time,
2333 cache_->clock_->Now()); 2422 cache_->clock_->Now());
2334 } 2423 }
2335 } 2424 }
2336 2425
2337 return validation_required_by_headers; 2426 return validation_required_by_headers;
2338 } 2427 }
2339 2428
2340 bool HttpCache::Transaction::ConditionalizeRequest() { 2429 bool HttpCache::Transaction::ConditionalizeRequest() {
2341 DCHECK(response_.headers.get()); 2430 DCHECK(response_.headers.get());
2342 2431
2343 if (request_->method == "PUT" || request_->method == "DELETE") 2432 if (method_ == "PUT" || method_ == "DELETE")
2344 return false; 2433 return false;
2345 2434
2346 // This only makes sense for cached 200 or 206 responses. 2435 // This only makes sense for cached 200 or 206 responses.
2347 if (response_.headers->response_code() != 200 && 2436 if (response_.headers->response_code() != 200 &&
2348 response_.headers->response_code() != 206) { 2437 response_.headers->response_code() != 206) {
2349 return false; 2438 return false;
2350 } 2439 }
2351 2440
2352 if (fail_conditionalization_for_test_) 2441 if (fail_conditionalization_for_test_)
2353 return false; 2442 return false;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
2427 // 2516 //
2428 // WARNING: Whenever this code returns false, it has to make sure that the next 2517 // WARNING: Whenever this code returns false, it has to make sure that the next
2429 // time it is called it will return true so that we don't keep retrying the 2518 // time it is called it will return true so that we don't keep retrying the
2430 // request. 2519 // request.
2431 bool HttpCache::Transaction::ValidatePartialResponse() { 2520 bool HttpCache::Transaction::ValidatePartialResponse() {
2432 const HttpResponseHeaders* headers = new_response_->headers.get(); 2521 const HttpResponseHeaders* headers = new_response_->headers.get();
2433 int response_code = headers->response_code(); 2522 int response_code = headers->response_code();
2434 bool partial_response = (response_code == 206); 2523 bool partial_response = (response_code == 206);
2435 handling_206_ = false; 2524 handling_206_ = false;
2436 2525
2437 if (!entry_ || request_->method != "GET") 2526 if (!entry_ || method_ != "GET")
2438 return true; 2527 return true;
2439 2528
2440 if (invalid_range_) { 2529 if (invalid_range_) {
2441 // We gave up trying to match this request with the stored data. If the 2530 // We gave up trying to match this request with the stored data. If the
2442 // server is ok with the request, delete the entry, otherwise just ignore 2531 // server is ok with the request, delete the entry, otherwise just ignore
2443 // this request 2532 // this request
2444 DCHECK(!reading_); 2533 DCHECK(!reading_);
2445 if (partial_response || response_code == 200) { 2534 if (partial_response || response_code == 200) {
2446 DoomPartialEntry(true); 2535 DoomPartialEntry(true);
2447 mode_ = NONE; 2536 mode_ = NONE;
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
2534 2623
2535 void HttpCache::Transaction::IgnoreRangeRequest() { 2624 void HttpCache::Transaction::IgnoreRangeRequest() {
2536 // We have a problem. We may or may not be reading already (in which case we 2625 // We have a problem. We may or may not be reading already (in which case we
2537 // returned the headers), but we'll just pretend that this request is not 2626 // returned the headers), but we'll just pretend that this request is not
2538 // using the cache and see what happens. Most likely this is the first 2627 // using the cache and see what happens. Most likely this is the first
2539 // response from the server (it's not changing its mind midway, right?). 2628 // response from the server (it's not changing its mind midway, right?).
2540 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER); 2629 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_OTHER);
2541 if (mode_ & WRITE) 2630 if (mode_ & WRITE)
2542 DoneWritingToEntry(mode_ != WRITE); 2631 DoneWritingToEntry(mode_ != WRITE);
2543 else if (mode_ & READ && entry_) 2632 else if (mode_ & READ && entry_)
2544 cache_->DoneReadingFromEntry(entry_, this); 2633 cache_->DoneWithEntry(entry_, this, false, partial_ != nullptr);
2545 2634
2546 partial_.reset(NULL); 2635 partial_.reset(NULL);
2547 entry_ = NULL; 2636 entry_ = NULL;
2548 mode_ = NONE; 2637 mode_ = NONE;
2549 } 2638 }
2550 2639
2551 void HttpCache::Transaction::FixHeadersForHead() { 2640 void HttpCache::Transaction::FixHeadersForHead() {
2552 if (response_.headers->response_code() == 206) { 2641 if (response_.headers->response_code() == 206) {
2553 response_.headers->RemoveHeader("Content-Range"); 2642 response_.headers->RemoveHeader("Content-Range");
2554 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK"); 2643 response_.headers->ReplaceStatusLine("HTTP/1.1 200 OK");
2555 } 2644 }
2556 } 2645 }
2557 2646
2558 int HttpCache::Transaction::SetupEntryForRead() { 2647 int HttpCache::Transaction::SetupEntryForRead() {
2559 if (network_trans_) 2648 if (network_trans_)
2560 ResetNetworkTransaction(); 2649 ResetNetworkTransaction();
2561 if (partial_) { 2650 if (partial_) {
2562 if (truncated_ || is_sparse_ || !invalid_range_) { 2651 if (truncated_ || is_sparse_ || !invalid_range_) {
2563 // We are going to return the saved response headers to the caller, so 2652 // We are going to return the saved response headers to the caller, so
2564 // we may need to adjust them first. 2653 // we may need to adjust them first.
2565 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 2654 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
2566 return OK; 2655 return OK;
2567 } else { 2656 } else {
2568 partial_.reset(); 2657 partial_.reset();
2569 } 2658 }
2570 } 2659 }
2571 cache_->ConvertWriterToReader(entry_); 2660
2572 mode_ = READ; 2661 mode_ = READ;
2573 2662
2574 if (request_->method == "HEAD") 2663 if (method_ == "HEAD")
2575 FixHeadersForHead(); 2664 FixHeadersForHead();
2576 2665
2577 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2666 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2578 TransitionToState(STATE_CACHE_READ_METADATA); 2667 TransitionToState(STATE_CACHE_READ_METADATA);
2579 else 2668 else
2580 TransitionToState(STATE_NONE); 2669 TransitionToState(STATE_FINISH_HEADERS);
2581 return OK; 2670 return OK;
2582 } 2671 }
2583 2672
2584 int HttpCache::Transaction::WriteToEntry(int index, int offset, 2673 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2585 IOBuffer* data, int data_len, 2674 IOBuffer* data, int data_len,
2586 const CompletionCallback& callback) { 2675 const CompletionCallback& callback) {
2587 if (!entry_) 2676 if (!entry_)
2588 return data_len; 2677 return data_len;
2589 2678
2590 int rv = 0; 2679 int rv = 0;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2649 } 2738 }
2650 return OK; 2739 return OK;
2651 } 2740 }
2652 2741
2653 void HttpCache::Transaction::DoneWritingToEntry(bool success) { 2742 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2654 if (!entry_) 2743 if (!entry_)
2655 return; 2744 return;
2656 2745
2657 RecordHistograms(); 2746 RecordHistograms();
2658 2747
2659 cache_->DoneWritingToEntry(entry_, success); 2748 cache_->DoneWritingToEntry(entry_, success, this);
2660 entry_ = NULL; 2749 entry_ = NULL;
2661 mode_ = NONE; // switch to 'pass through' mode 2750 mode_ = NONE; // switch to 'pass through' mode
2662 } 2751 }
2663 2752
2664 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { 2753 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2665 DLOG(ERROR) << "ReadData failed: " << result; 2754 DLOG(ERROR) << "ReadData failed: " << result;
2666 const int result_for_histogram = std::max(0, -result); 2755 const int result_for_histogram = std::max(0, -result);
2667 if (restart) { 2756 if (restart) {
2668 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", 2757 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2669 result_for_histogram); 2758 result_for_histogram);
2670 } else { 2759 } else {
2671 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable", 2760 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable",
2672 result_for_histogram); 2761 result_for_histogram);
2673 } 2762 }
2674 2763
2675 // Avoid using this entry in the future. 2764 // Avoid using this entry in the future.
2676 if (cache_.get()) 2765 if (cache_.get())
2677 cache_->DoomActiveEntry(cache_key_); 2766 cache_->DoomActiveEntry(cache_key_);
2678 2767
2679 if (restart) { 2768 if (restart) {
2680 DCHECK(!reading_); 2769 DCHECK(!reading_);
2681 DCHECK(!network_trans_.get()); 2770 DCHECK(!network_trans_.get());
2682 cache_->DoneWithEntry(entry_, this, false); 2771 cache_->DoneWithEntry(entry_, this, false, partial_ != nullptr);
2683 entry_ = NULL; 2772 entry_ = NULL;
2684 is_sparse_ = false; 2773 is_sparse_ = false;
2685 partial_.reset(); 2774 partial_.reset();
2686 TransitionToState(STATE_GET_BACKEND); 2775 TransitionToState(STATE_GET_BACKEND);
2687 return OK; 2776 return OK;
2688 } 2777 }
2689 2778
2690 TransitionToState(STATE_NONE); 2779 TransitionToState(STATE_NONE);
2691 return ERR_CACHE_READ_FAILURE; 2780 return ERR_CACHE_READ_FAILURE;
2692 } 2781 }
2693 2782
2694 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) { 2783 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) {
2695 if (entry_lock_waiting_since_ != start_time) 2784 if (entry_lock_waiting_since_ != start_time)
2696 return; 2785 return;
2697 2786
2698 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE); 2787 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE);
2699 2788
2700 if (!cache_) 2789 if (!cache_)
2701 return; 2790 return;
2702 2791
2703 cache_->RemovePendingTransaction(this); 2792 cache_->RemovePendingTransaction(this);
2704 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT); 2793 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT);
2705 } 2794 }
2706 2795
2707 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) { 2796 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) {
2708 DVLOG(2) << "DoomPartialEntry"; 2797 DVLOG(2) << "DoomPartialEntry";
2709 int rv = cache_->DoomEntry(cache_key_, NULL); 2798 int rv = cache_->DoomEntry(cache_key_, NULL);
2710 DCHECK_EQ(OK, rv); 2799 DCHECK_EQ(OK, rv);
2711 cache_->DoneWithEntry(entry_, this, false); 2800 cache_->DoneWithEntry(entry_, this, false, partial_ != nullptr);
2712 entry_ = NULL; 2801 entry_ = NULL;
2713 is_sparse_ = false; 2802 is_sparse_ = false;
2714 truncated_ = false; 2803 truncated_ = false;
2715 if (delete_object) 2804 if (delete_object)
2716 partial_.reset(NULL); 2805 partial_.reset(NULL);
2717 } 2806 }
2718 2807
2719 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) { 2808 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) {
2720 partial_->OnNetworkReadCompleted(result); 2809 partial_->OnNetworkReadCompleted(result);
2721 2810
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
2797 // Accept-Ranges: none.......... 0.4% 2886 // Accept-Ranges: none.......... 0.4%
2798 // Strong Validator............. 50% 2887 // Strong Validator............. 50%
2799 // Strong Validator + ranges.... 24% 2888 // Strong Validator + ranges.... 24%
2800 // Strong Validator + CL........ 49% 2889 // Strong Validator + CL........ 49%
2801 // 2890 //
2802 bool HttpCache::Transaction::CanResume(bool has_data) { 2891 bool HttpCache::Transaction::CanResume(bool has_data) {
2803 // Double check that there is something worth keeping. 2892 // Double check that there is something worth keeping.
2804 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex)) 2893 if (has_data && !entry_->disk_entry->GetDataSize(kResponseContentIndex))
2805 return false; 2894 return false;
2806 2895
2807 if (request_->method != "GET") 2896 if (method_ != "GET")
2808 return false; 2897 return false;
2809 2898
2810 // Note that if this is a 206, content-length was already fixed after calling 2899 // Note that if this is a 206, content-length was already fixed after calling
2811 // PartialData::ResponseHeadersOK(). 2900 // PartialData::ResponseHeadersOK().
2812 if (response_.headers->GetContentLength() <= 0 || 2901 if (response_.headers->GetContentLength() <= 0 ||
2813 response_.headers->HasHeaderValue("Accept-Ranges", "none") || 2902 response_.headers->HasHeaderValue("Accept-Ranges", "none") ||
2814 !response_.headers->HasStrongValidators()) { 2903 !response_.headers->HasStrongValidators()) {
2815 return false; 2904 return false;
2816 } 2905 }
2817 2906
(...skipping 28 matching lines...) Expand all
2846 response_.cache_entry_status = cache_entry_status_; 2935 response_.cache_entry_status = cache_entry_status_;
2847 if (auth_response_.headers.get()) { 2936 if (auth_response_.headers.get()) {
2848 auth_response_.cache_entry_status = cache_entry_status_; 2937 auth_response_.cache_entry_status = cache_entry_status_;
2849 } 2938 }
2850 } 2939 }
2851 2940
2852 void HttpCache::Transaction::RecordHistograms() { 2941 void HttpCache::Transaction::RecordHistograms() {
2853 DCHECK_NE(CacheEntryStatus::ENTRY_UNDEFINED, cache_entry_status_); 2942 DCHECK_NE(CacheEntryStatus::ENTRY_UNDEFINED, cache_entry_status_);
2854 if (!cache_.get() || !cache_->GetCurrentBackend() || 2943 if (!cache_.get() || !cache_->GetCurrentBackend() ||
2855 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE || 2944 cache_->GetCurrentBackend()->GetCacheType() != DISK_CACHE ||
2856 cache_->mode() != NORMAL || request_->method != "GET") { 2945 cache_->mode() != NORMAL || method_ != "GET") {
2857 return; 2946 return;
2858 } 2947 }
2859 2948
2860 bool validation_request = 2949 bool validation_request =
2861 cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED || 2950 cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED ||
2862 cache_entry_status_ == CacheEntryStatus::ENTRY_UPDATED; 2951 cache_entry_status_ == CacheEntryStatus::ENTRY_UPDATED;
2863 2952
2864 bool stale_request = 2953 bool stale_request =
2865 validation_cause_ == VALIDATION_CAUSE_STALE && 2954 validation_cause_ == VALIDATION_CAUSE_STALE &&
2866 (validation_request || 2955 (validation_request ||
2867 cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE); 2956 cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE);
2868 int64_t freshness_periods_since_last_used = 0; 2957 int64_t freshness_periods_since_last_used = 0;
2869 2958
2870 if (stale_request) { 2959 if (stale_request && !open_entry_last_used_.is_null()) {
2960 // Note that we are not able to capture those transactions' histograms which
2961 // when added to entry, the response was being written by another
2962 // transaction because getting the last used timestamp might lead to a data
2963 // race in that case. TODO(crbug.com/713354).
2964
2871 // For stale entries, record how many freshness periods have elapsed since 2965 // For stale entries, record how many freshness periods have elapsed since
2872 // the entry was last used. 2966 // the entry was last used.
2873 DCHECK(!open_entry_last_used_.is_null());
2874 DCHECK(!stale_entry_freshness_.is_zero()); 2967 DCHECK(!stale_entry_freshness_.is_zero());
2875 base::TimeDelta time_since_use = base::Time::Now() - open_entry_last_used_; 2968 base::TimeDelta time_since_use = base::Time::Now() - open_entry_last_used_;
2876 freshness_periods_since_last_used = 2969 freshness_periods_since_last_used =
2877 (time_since_use * 1000) / stale_entry_freshness_; 2970 (time_since_use * 1000) / stale_entry_freshness_;
2878 2971
2879 if (validation_request) { 2972 if (validation_request) {
2880 int64_t age_in_freshness_periods = 2973 int64_t age_in_freshness_periods =
2881 (stale_entry_age_ * 100) / stale_entry_freshness_; 2974 (stale_entry_age_ * 100) / stale_entry_freshness_;
2882 if (cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED) { 2975 if (cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED) {
2883 UMA_HISTOGRAM_COUNTS("HttpCache.StaleEntry.Validated.Age", 2976 UMA_HISTOGRAM_COUNTS("HttpCache.StaleEntry.Validated.Age",
(...skipping 12 matching lines...) Expand all
2896 } 2989 }
2897 } 2990 }
2898 2991
2899 std::string mime_type; 2992 std::string mime_type;
2900 HttpResponseHeaders* response_headers = GetResponseInfo()->headers.get(); 2993 HttpResponseHeaders* response_headers = GetResponseInfo()->headers.get();
2901 if (response_headers && response_headers->GetMimeType(&mime_type)) { 2994 if (response_headers && response_headers->GetMimeType(&mime_type)) {
2902 // Record the cache pattern by resource type. The type is inferred by 2995 // Record the cache pattern by resource type. The type is inferred by
2903 // response header mime type, which could be incorrect, so this is just an 2996 // response header mime type, which could be incorrect, so this is just an
2904 // estimate. 2997 // estimate.
2905 if (mime_type == "text/html" && 2998 if (mime_type == "text/html" &&
2906 (request_->load_flags & LOAD_MAIN_FRAME_DEPRECATED)) { 2999 (effective_load_flags_ & LOAD_MAIN_FRAME_DEPRECATED)) {
2907 CACHE_STATUS_HISTOGRAMS(".MainFrameHTML"); 3000 CACHE_STATUS_HISTOGRAMS(".MainFrameHTML");
2908 } else if (mime_type == "text/html") { 3001 } else if (mime_type == "text/html") {
2909 CACHE_STATUS_HISTOGRAMS(".NonMainFrameHTML"); 3002 CACHE_STATUS_HISTOGRAMS(".NonMainFrameHTML");
2910 } else if (mime_type == "text/css") { 3003 } else if (mime_type == "text/css") {
2911 CACHE_STATUS_HISTOGRAMS(".CSS"); 3004 CACHE_STATUS_HISTOGRAMS(".CSS");
2912 } else if (base::StartsWith(mime_type, "image/", 3005 } else if (base::StartsWith(mime_type, "image/",
2913 base::CompareCase::SENSITIVE)) { 3006 base::CompareCase::SENSITIVE)) {
2914 int64_t content_length = response_headers->GetContentLength(); 3007 int64_t content_length = response_headers->GetContentLength();
2915 if (content_length >= 0 && content_length < 100) { 3008 if (content_length >= 0 && content_length < 100) {
2916 CACHE_STATUS_HISTOGRAMS(".TinyImage"); 3009 CACHE_STATUS_HISTOGRAMS(".TinyImage");
(...skipping 19 matching lines...) Expand all
2936 3029
2937 CACHE_STATUS_HISTOGRAMS(""); 3030 CACHE_STATUS_HISTOGRAMS("");
2938 3031
2939 if (cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE) { 3032 if (cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE) {
2940 UMA_HISTOGRAM_ENUMERATION("HttpCache.CantConditionalizeCause", 3033 UMA_HISTOGRAM_ENUMERATION("HttpCache.CantConditionalizeCause",
2941 validation_cause_, VALIDATION_CAUSE_MAX); 3034 validation_cause_, VALIDATION_CAUSE_MAX);
2942 } 3035 }
2943 3036
2944 if (cache_entry_status_ == CacheEntryStatus::ENTRY_OTHER) 3037 if (cache_entry_status_ == CacheEntryStatus::ENTRY_OTHER)
2945 return; 3038 return;
2946 DCHECK(!range_requested_); 3039
3040 DCHECK(!range_requested_) << "Cache entry status " << cache_entry_status_;
2947 DCHECK(!first_cache_access_since_.is_null()); 3041 DCHECK(!first_cache_access_since_.is_null());
2948 3042
2949 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_; 3043 TimeDelta total_time = base::TimeTicks::Now() - first_cache_access_since_;
2950 3044
2951 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time); 3045 UMA_HISTOGRAM_TIMES("HttpCache.AccessToDone", total_time);
2952 3046
2953 bool did_send_request = !send_request_since_.is_null(); 3047 bool did_send_request = !send_request_since_.is_null();
2954 DCHECK( 3048 DCHECK(
2955 (did_send_request && 3049 (did_send_request &&
2956 (cache_entry_status_ == CacheEntryStatus::ENTRY_NOT_IN_CACHE || 3050 (cache_entry_status_ == CacheEntryStatus::ENTRY_NOT_IN_CACHE ||
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
3017 } 3111 }
3018 3112
3019 void HttpCache::Transaction::TransitionToState(State state) { 3113 void HttpCache::Transaction::TransitionToState(State state) {
3020 // Ensure that the state is only set once per Do* state. 3114 // Ensure that the state is only set once per Do* state.
3021 DCHECK(in_do_loop_); 3115 DCHECK(in_do_loop_);
3022 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; 3116 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state;
3023 next_state_ = state; 3117 next_state_ = state;
3024 } 3118 }
3025 3119
3026 } // namespace net 3120 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698