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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Rebased till refs/heads/master@{#462134} Created 3 years, 8 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 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 149
150 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache) 150 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
151 : next_state_(STATE_NONE), 151 : next_state_(STATE_NONE),
152 request_(NULL), 152 request_(NULL),
153 priority_(priority), 153 priority_(priority),
154 cache_(cache->GetWeakPtr()), 154 cache_(cache->GetWeakPtr()),
155 entry_(NULL), 155 entry_(NULL),
156 new_entry_(NULL), 156 new_entry_(NULL),
157 new_response_(NULL), 157 new_response_(NULL),
158 mode_(NONE), 158 mode_(NONE),
159 original_mode_(NONE),
159 reading_(false), 160 reading_(false),
160 invalid_range_(false), 161 invalid_range_(false),
161 truncated_(false), 162 truncated_(false),
162 is_sparse_(false), 163 is_sparse_(false),
163 range_requested_(false), 164 range_requested_(false),
164 handling_206_(false), 165 handling_206_(false),
165 cache_pending_(false), 166 cache_pending_(false),
166 done_reading_(false), 167 done_reading_(false),
167 vary_mismatch_(false), 168 vary_mismatch_(false),
168 couldnt_conditionalize_request_(false), 169 couldnt_conditionalize_request_(false),
(...skipping 20 matching lines...) Expand all
189 } 190 }
190 191
191 HttpCache::Transaction::~Transaction() { 192 HttpCache::Transaction::~Transaction() {
192 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction"); 193 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction");
193 // We may have to issue another IO, but we should never invoke the callback_ 194 // We may have to issue another IO, but we should never invoke the callback_
194 // after this point. 195 // after this point.
195 callback_.Reset(); 196 callback_.Reset();
196 197
197 if (cache_) { 198 if (cache_) {
198 if (entry_) { 199 if (entry_) {
199 bool cancel_request = reading_ && response_.headers.get(); 200 bool cancel_response = cache_->CanTransactionWriteResponseBody(
jkarlin 2017/04/06 17:37:14 nit: s/cancel_response/writing_incomplete/
shivanisha 2017/04/07 21:11:16 done
200 if (cancel_request) { 201 entry_, this, request_->method);
201 if (partial_) { 202 if (cancel_response && partial_)
202 entry_->disk_entry->CancelSparseIO(); 203 entry_->disk_entry->CancelSparseIO();
203 } else {
204 cancel_request &= (response_.headers->response_code() == 200);
205 }
206 }
207 204
208 cache_->DoneWithEntry(entry_, this, cancel_request); 205 cache_->DoneWithEntry(entry_, this, cancel_response);
209 } else if (cache_pending_) { 206 } else if (cache_pending_) {
210 cache_->RemovePendingTransaction(this); 207 cache_->RemovePendingTransaction(this);
211 } 208 }
212 } 209 }
213 } 210 }
214 211
215 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, 212 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
216 const CompletionCallback& callback) { 213 const CompletionCallback& callback) {
217 DCHECK(buf); 214 DCHECK(buf);
218 DCHECK_GT(buf_len, 0); 215 DCHECK_GT(buf_len, 0);
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after
558 ConnectionAttempts* out) const { 555 ConnectionAttempts* out) const {
559 ConnectionAttempts new_connection_attempts; 556 ConnectionAttempts new_connection_attempts;
560 if (network_trans_) 557 if (network_trans_)
561 network_trans_->GetConnectionAttempts(&new_connection_attempts); 558 network_trans_->GetConnectionAttempts(&new_connection_attempts);
562 559
563 out->swap(new_connection_attempts); 560 out->swap(new_connection_attempts);
564 out->insert(out->begin(), old_connection_attempts_.begin(), 561 out->insert(out->begin(), old_connection_attempts_.begin(),
565 old_connection_attempts_.end()); 562 old_connection_attempts_.end());
566 } 563 }
567 564
565 void HttpCache::Transaction::SetValidatingCannotProceed() {
566 DCHECK(!reading_);
567 next_state_ = STATE_HEADERS_PHASE_CANNOT_PROCEED;
568 entry_ = nullptr;
569 }
570
568 size_t HttpCache::Transaction::EstimateMemoryUsage() const { 571 size_t HttpCache::Transaction::EstimateMemoryUsage() const {
569 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. 572 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108.
570 return 0; 573 return 0;
571 } 574 }
572 575
573 //----------------------------------------------------------------------------- 576 //-----------------------------------------------------------------------------
574 577
575 // A few common patterns: (Foo* means Foo -> FooComplete) 578 // A few common patterns: (Foo* means Foo -> FooComplete)
576 // 579 //
577 // 1. Not-cached entry: 580 // 1. Not-cached entry:
578 // Start(): 581 // Start():
579 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> 582 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
580 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> 583 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
581 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> 584 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
582 // PartialHeadersReceived 585 // PartialHeadersReceived -> FinishHeaders*
583 // 586 //
584 // Read(): 587 // Read():
585 // NetworkRead* -> CacheWriteData* 588 // NetworkRead* -> CacheWriteData*
586 // 589 //
587 // 2. Cached entry, no validation: 590 // 2. Cached entry, no validation:
588 // Start(): 591 // Start():
589 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 592 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
590 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 593 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
591 // BeginCacheValidation() -> SetupEntryForRead() 594 // BeginCacheValidation() -> SetupEntryForRead() -> FinishHeaders*
592 // 595 //
593 // Read(): 596 // Read():
594 // CacheReadData* 597 // CacheReadData*
595 // 598 //
596 // 3. Cached entry, validation (304): 599 // 3. Cached entry, validation (304):
597 // Start(): 600 // Start():
598 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 601 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
599 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 602 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
600 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 603 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
601 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> 604 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
602 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> 605 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
603 // PartialHeadersReceived 606 // PartialHeadersReceived -> FinishHeaders*
604 // 607 //
605 // Read(): 608 // Read():
606 // CacheReadData* 609 // CacheReadData*
607 // 610 //
608 // 4. Cached entry, validation and replace (200): 611 // 4. Cached entry, validation and replace (200):
609 // Start(): 612 // Start():
610 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 613 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
611 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 614 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
612 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 615 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
613 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> 616 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
614 // TruncateCachedMetadata* -> PartialHeadersReceived 617 // TruncateCachedMetadata* -> PartialHeadersReceived -> FinishHeaders*
615 // 618 //
616 // Read(): 619 // Read():
617 // NetworkRead* -> CacheWriteData* 620 // NetworkRead* -> CacheWriteData*
618 // 621 //
619 // 5. Sparse entry, partially cached, byte range request: 622 // 5. Sparse entry, partially cached, byte range request:
620 // Start(): 623 // Start():
621 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 624 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
622 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 625 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
623 // CacheQueryData* -> ValidateEntryHeadersAndContinue() -> 626 // CacheQueryData* -> ValidateEntryHeadersAndContinue() ->
624 // StartPartialCacheValidation -> CompletePartialCacheValidation -> 627 // StartPartialCacheValidation -> CompletePartialCacheValidation ->
625 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 628 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
626 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> 629 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
627 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> 630 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
628 // PartialHeadersReceived 631 // PartialHeadersReceived -> FinishHeaders*
629 // 632 //
630 // Read() 1: 633 // Read() 1:
631 // NetworkRead* -> CacheWriteData* 634 // NetworkRead* -> CacheWriteData*
632 // 635 //
633 // Read() 2: 636 // Read() 2:
634 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation -> 637 // NetworkRead* -> CacheWriteData* -> StartPartialCacheValidation ->
635 // CompletePartialCacheValidation -> CacheReadData* -> 638 // CompletePartialCacheValidation -> CacheReadData* ->
636 // 639 //
637 // Read() 3: 640 // Read() 3:
638 // CacheReadData* -> StartPartialCacheValidation -> 641 // CacheReadData* -> StartPartialCacheValidation ->
(...skipping 20 matching lines...) Expand all
659 // Read(): 662 // Read():
660 // CacheReadData (returns 0) 663 // CacheReadData (returns 0)
661 // 664 //
662 // 9. HEAD. Cached entry, validation and replace (200): 665 // 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 666 // Pass through. The request dooms the old entry, as a HEAD won't be stored by
664 // itself. 667 // itself.
665 // Start(): 668 // Start():
666 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 669 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
667 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 670 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
668 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 671 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
669 // OverwriteCachedResponse 672 // OverwriteCachedResponse -> FinishHeaders*
670 // 673 //
671 // 10. HEAD. Sparse entry, partially cached: 674 // 10. HEAD. Sparse entry, partially cached:
672 // Serve the request from the cache, as long as it doesn't require 675 // Serve the request from the cache, as long as it doesn't require
673 // revalidation. Ignore missing ranges when deciding to revalidate. If the 676 // revalidation. Ignore missing ranges when deciding to revalidate. If the
674 // entry requires revalidation, ignore the whole request and go to full pass 677 // 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). 678 // through (the result of the HEAD request will NOT update the entry).
676 // 679 //
677 // Start(): Basically the same as example 7, as we never create a partial_ 680 // Start(): Basically the same as example 7, as we never create a partial_
678 // object for this request. 681 // object for this request.
679 // 682 //
680 // 11. Prefetch, not-cached entry: 683 // 11. Prefetch, not-cached entry:
681 // The same as example 1. The "unused_since_prefetch" bit is stored as true in 684 // The same as example 1. The "unused_since_prefetch" bit is stored as true in
682 // UpdateCachedResponse. 685 // UpdateCachedResponse.
683 // 686 //
684 // 12. Prefetch, cached entry: 687 // 12. Prefetch, cached entry:
685 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between 688 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
686 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch 689 // CacheReadResponse* and CacheDispatchValidation if the unused_since_prefetch
687 // bit is unset. 690 // bit is unset.
688 // 691 //
689 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true: 692 // 13. Cached entry less than 5 minutes old, unused_since_prefetch is true:
690 // Skip validation, similar to example 2. 693 // Skip validation, similar to example 2.
691 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 694 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
692 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation -> 695 // -> CacheToggleUnusedSincePrefetch* -> CacheDispatchValidation ->
693 // BeginPartialCacheValidation() -> BeginCacheValidation() -> 696 // BeginPartialCacheValidation() -> BeginCacheValidation() ->
694 // SetupEntryForRead() 697 // SetupEntryForRead() -> FinishHeaders*
695 // 698 //
696 // Read(): 699 // Read():
697 // CacheReadData* 700 // CacheReadData*
698 // 701 //
699 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: 702 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true:
700 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between 703 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
701 // CacheReadResponse* and CacheDispatchValidation. 704 // CacheReadResponse* and CacheDispatchValidation.
702 int HttpCache::Transaction::DoLoop(int result) { 705 int HttpCache::Transaction::DoLoop(int result) {
703 DCHECK_NE(STATE_UNSET, next_state_); 706 DCHECK_NE(STATE_UNSET, next_state_);
704 DCHECK_NE(STATE_NONE, next_state_); 707 DCHECK_NE(STATE_NONE, next_state_);
705 DCHECK(!in_do_loop_); 708 DCHECK(!in_do_loop_);
706 709
707 int rv = result; 710 int rv = result;
711 State state = next_state_;
708 do { 712 do {
709 State state = next_state_; 713 state = next_state_;
710 next_state_ = STATE_UNSET; 714 next_state_ = STATE_UNSET;
711 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true); 715 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true);
712 716
713 switch (state) { 717 switch (state) {
714 case STATE_GET_BACKEND: 718 case STATE_GET_BACKEND:
715 DCHECK_EQ(OK, rv); 719 DCHECK_EQ(OK, rv);
716 rv = DoGetBackend(); 720 rv = DoGetBackend();
717 break; 721 break;
718 case STATE_GET_BACKEND_COMPLETE: 722 case STATE_GET_BACKEND_COMPLETE:
719 rv = DoGetBackendComplete(rv); 723 rv = DoGetBackendComplete(rv);
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
836 DCHECK_EQ(OK, rv); 840 DCHECK_EQ(OK, rv);
837 rv = DoPartialHeadersReceived(); 841 rv = DoPartialHeadersReceived();
838 break; 842 break;
839 case STATE_CACHE_READ_METADATA: 843 case STATE_CACHE_READ_METADATA:
840 DCHECK_EQ(OK, rv); 844 DCHECK_EQ(OK, rv);
841 rv = DoCacheReadMetadata(); 845 rv = DoCacheReadMetadata();
842 break; 846 break;
843 case STATE_CACHE_READ_METADATA_COMPLETE: 847 case STATE_CACHE_READ_METADATA_COMPLETE:
844 rv = DoCacheReadMetadataComplete(rv); 848 rv = DoCacheReadMetadataComplete(rv);
845 break; 849 break;
850 case STATE_HEADERS_PHASE_CANNOT_PROCEED:
851 rv = DoHeadersPhaseCannotProceed();
852 break;
853 case STATE_FINISH_HEADERS:
854 rv = DoFinishHeaders(rv);
855 break;
856 case STATE_FINISH_HEADERS_COMPLETE:
857 rv = DoFinishHeadersComplete(rv);
858 break;
846 case STATE_NETWORK_READ: 859 case STATE_NETWORK_READ:
847 DCHECK_EQ(OK, rv); 860 DCHECK_EQ(OK, rv);
848 rv = DoNetworkRead(); 861 rv = DoNetworkRead();
849 break; 862 break;
850 case STATE_NETWORK_READ_COMPLETE: 863 case STATE_NETWORK_READ_COMPLETE:
851 rv = DoNetworkReadComplete(rv); 864 rv = DoNetworkReadComplete(rv);
852 break; 865 break;
853 case STATE_CACHE_READ_DATA: 866 case STATE_CACHE_READ_DATA:
854 DCHECK_EQ(OK, rv); 867 DCHECK_EQ(OK, rv);
855 rv = DoCacheReadData(); 868 rv = DoCacheReadData();
(...skipping 16 matching lines...) Expand all
872 break; 885 break;
873 default: 886 default:
874 NOTREACHED() << "bad state " << state; 887 NOTREACHED() << "bad state " << state;
875 rv = ERR_FAILED; 888 rv = ERR_FAILED;
876 break; 889 break;
877 } 890 }
878 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state; 891 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state;
879 892
880 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); 893 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
881 894
895 // Assert Start() state machine's allowed last state in successful cases when
896 // caching is happening.
897 DCHECK(reading_ || rv != OK || !entry_ ||
898 state == STATE_FINISH_HEADERS_COMPLETE);
899
882 if (rv != ERR_IO_PENDING && !callback_.is_null()) { 900 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
883 read_buf_ = NULL; // Release the buffer before invoking the callback. 901 read_buf_ = nullptr; // Release the buffer before invoking the callback.
884 base::ResetAndReturn(&callback_).Run(rv); 902 base::ResetAndReturn(&callback_).Run(rv);
885 } 903 }
886 904
887 return rv; 905 return rv;
888 } 906 }
889 907
890 int HttpCache::Transaction::DoGetBackend() { 908 int HttpCache::Transaction::DoGetBackend() {
891 cache_pending_ = true; 909 cache_pending_ = true;
892 TransitionToState(STATE_GET_BACKEND_COMPLETE); 910 TransitionToState(STATE_GET_BACKEND_COMPLETE);
893 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); 911 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND);
894 return cache_->GetBackendForTransaction(this); 912 return cache_->GetBackendForTransaction(this);
895 } 913 }
896 914
897 int HttpCache::Transaction::DoGetBackendComplete(int result) { 915 int HttpCache::Transaction::DoGetBackendComplete(int result) {
898 DCHECK(result == OK || result == ERR_FAILED); 916 DCHECK(result == OK || result == ERR_FAILED);
899 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_GET_BACKEND, 917 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_GET_BACKEND,
900 result); 918 result);
901 cache_pending_ = false; 919 cache_pending_ = false;
902 920
903 if (!ShouldPassThrough()) { 921 if (!ShouldPassThrough()) {
904 cache_key_ = cache_->GenerateCacheKey(request_); 922 cache_key_ = cache_->GenerateCacheKey(request_);
905 923
906 // Requested cache access mode. 924 // Requested cache access mode.
907 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { 925 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
908 if (effective_load_flags_ & LOAD_BYPASS_CACHE) { 926 if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
909 // The client has asked for nonsense. 927 // The client has asked for nonsense.
910 TransitionToState(STATE_NONE); 928 TransitionToState(STATE_FINISH_HEADERS);
911 return ERR_CACHE_MISS; 929 return ERR_CACHE_MISS;
912 } 930 }
913 mode_ = READ; 931 mode_ = READ;
914 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) { 932 } else if (effective_load_flags_ & LOAD_BYPASS_CACHE) {
915 mode_ = WRITE; 933 mode_ = WRITE;
916 } else { 934 } else {
917 mode_ = READ_WRITE; 935 mode_ = READ_WRITE;
918 } 936 }
919 937
920 // Downgrade to UPDATE if the request has been externally conditionalized. 938 // Downgrade to UPDATE if the request has been externally conditionalized.
(...skipping 18 matching lines...) Expand all
939 // transaction behaves the same for GET and HEAD requests at this point: if it 957 // 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 958 // 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 959 // the cache. If we receive 200, it doesn't matter if there was a validation
942 // header or not. 960 // header or not.
943 if (request_->method == "HEAD" && mode_ == WRITE) 961 if (request_->method == "HEAD" && mode_ == WRITE)
944 mode_ = NONE; 962 mode_ = NONE;
945 963
946 // If must use cache, then we must fail. This can happen for back/forward 964 // If must use cache, then we must fail. This can happen for back/forward
947 // navigations to a page generated via a form post. 965 // navigations to a page generated via a form post.
948 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { 966 if (!(mode_ & READ) && effective_load_flags_ & LOAD_ONLY_FROM_CACHE) {
949 TransitionToState(STATE_NONE); 967 TransitionToState(STATE_FINISH_HEADERS);
950 return ERR_CACHE_MISS; 968 return ERR_CACHE_MISS;
951 } 969 }
952 970
953 if (mode_ == NONE) { 971 if (mode_ == NONE) {
954 if (partial_) { 972 if (partial_) {
955 partial_->RestoreHeaders(&custom_request_->extra_headers); 973 partial_->RestoreHeaders(&custom_request_->extra_headers);
956 partial_.reset(); 974 partial_.reset();
957 } 975 }
958 TransitionToState(STATE_SEND_REQUEST); 976 TransitionToState(STATE_SEND_REQUEST);
959 } else { 977 } else {
960 TransitionToState(STATE_INIT_ENTRY); 978 TransitionToState(STATE_INIT_ENTRY);
961 } 979 }
962 980
963 // This is only set if we have something to do with the response. 981 // This is only set if we have something to do with the response.
964 range_requested_ = (partial_.get() != NULL); 982 range_requested_ = (partial_.get() != NULL);
965 983
984 // mode_ may change later, save the initial mode in case we need to restart
985 // this request.
986 original_mode_ = mode_;
966 return OK; 987 return OK;
967 } 988 }
968 989
969 int HttpCache::Transaction::DoInitEntry() { 990 int HttpCache::Transaction::DoInitEntry() {
970 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); 991 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry");
971 DCHECK(!new_entry_); 992 DCHECK(!new_entry_);
972 993
973 if (!cache_.get()) { 994 if (!cache_.get()) {
974 TransitionToState(STATE_NONE); 995 TransitionToState(STATE_FINISH_HEADERS);
975 return ERR_UNEXPECTED; 996 return ERR_UNEXPECTED;
976 } 997 }
977 998
978 if (mode_ == WRITE) { 999 if (mode_ == WRITE) {
979 TransitionToState(STATE_DOOM_ENTRY); 1000 TransitionToState(STATE_DOOM_ENTRY);
980 return OK; 1001 return OK;
981 } 1002 }
982 1003
983 TransitionToState(STATE_OPEN_ENTRY); 1004 TransitionToState(STATE_OPEN_ENTRY);
984 return OK; 1005 return OK;
(...skipping 16 matching lines...) Expand all
1001 // transaction attached. 1022 // transaction attached.
1002 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_OPEN_ENTRY, 1023 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_OPEN_ENTRY,
1003 result); 1024 result);
1004 cache_pending_ = false; 1025 cache_pending_ = false;
1005 if (result == OK) { 1026 if (result == OK) {
1006 TransitionToState(STATE_ADD_TO_ENTRY); 1027 TransitionToState(STATE_ADD_TO_ENTRY);
1007 return OK; 1028 return OK;
1008 } 1029 }
1009 1030
1010 if (result == ERR_CACHE_RACE) { 1031 if (result == ERR_CACHE_RACE) {
1011 TransitionToState(STATE_INIT_ENTRY); 1032 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED);
1012 return OK; 1033 return OK;
1013 } 1034 }
1014 1035
1015 if (request_->method == "PUT" || request_->method == "DELETE" || 1036 if (request_->method == "PUT" || request_->method == "DELETE" ||
1016 (request_->method == "HEAD" && mode_ == READ_WRITE)) { 1037 (request_->method == "HEAD" && mode_ == READ_WRITE)) {
1017 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD"); 1038 DCHECK(mode_ == READ_WRITE || mode_ == WRITE || request_->method == "HEAD");
1018 mode_ = NONE; 1039 mode_ = NONE;
1019 TransitionToState(STATE_SEND_REQUEST); 1040 TransitionToState(STATE_SEND_REQUEST);
1020 return OK; 1041 return OK;
1021 } 1042 }
1022 1043
1023 if (mode_ == READ_WRITE) { 1044 if (mode_ == READ_WRITE) {
1024 mode_ = WRITE; 1045 mode_ = WRITE;
1025 TransitionToState(STATE_CREATE_ENTRY); 1046 TransitionToState(STATE_CREATE_ENTRY);
1026 return OK; 1047 return OK;
1027 } 1048 }
1028 if (mode_ == UPDATE) { 1049 if (mode_ == UPDATE) {
1029 // There is no cache entry to update; proceed without caching. 1050 // There is no cache entry to update; proceed without caching.
1030 mode_ = NONE; 1051 mode_ = NONE;
1031 TransitionToState(STATE_SEND_REQUEST); 1052 TransitionToState(STATE_SEND_REQUEST);
1032 return OK; 1053 return OK;
1033 } 1054 }
1034 1055
1035 // The entry does not exist, and we are not permitted to create a new entry, 1056 // The entry does not exist, and we are not permitted to create a new entry,
1036 // so we must fail. 1057 // so we must fail.
1037 TransitionToState(STATE_NONE); 1058 TransitionToState(STATE_FINISH_HEADERS);
1038 return ERR_CACHE_MISS; 1059 return ERR_CACHE_MISS;
1039 } 1060 }
1040 1061
1041 int HttpCache::Transaction::DoDoomEntry() { 1062 int HttpCache::Transaction::DoDoomEntry() {
1042 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntry"); 1063 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntry");
1043 TransitionToState(STATE_DOOM_ENTRY_COMPLETE); 1064 TransitionToState(STATE_DOOM_ENTRY_COMPLETE);
1044 cache_pending_ = true; 1065 cache_pending_ = true;
1045 if (first_cache_access_since_.is_null()) 1066 if (first_cache_access_since_.is_null())
1046 first_cache_access_since_ = TimeTicks::Now(); 1067 first_cache_access_since_ = TimeTicks::Now();
1047 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_DOOM_ENTRY); 1068 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_DOOM_ENTRY);
1048 return cache_->DoomEntry(cache_key_, this); 1069 return cache_->DoomEntry(cache_key_, this);
1049 } 1070 }
1050 1071
1051 int HttpCache::Transaction::DoDoomEntryComplete(int result) { 1072 int HttpCache::Transaction::DoDoomEntryComplete(int result) {
1052 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntryComplete"); 1073 TRACE_EVENT0("io", "HttpCacheTransaction::DoDoomEntryComplete");
1053 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_DOOM_ENTRY, 1074 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_DOOM_ENTRY,
1054 result); 1075 result);
1055 cache_pending_ = false; 1076 cache_pending_ = false;
1056 TransitionToState(result == ERR_CACHE_RACE ? STATE_INIT_ENTRY 1077 TransitionToState(result == ERR_CACHE_RACE
1057 : STATE_CREATE_ENTRY); 1078 ? STATE_HEADERS_PHASE_CANNOT_PROCEED
1079 : STATE_CREATE_ENTRY);
1058 return OK; 1080 return OK;
1059 } 1081 }
1060 1082
1061 int HttpCache::Transaction::DoCreateEntry() { 1083 int HttpCache::Transaction::DoCreateEntry() {
1062 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntry"); 1084 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntry");
1063 DCHECK(!new_entry_); 1085 DCHECK(!new_entry_);
1064 TransitionToState(STATE_CREATE_ENTRY_COMPLETE); 1086 TransitionToState(STATE_CREATE_ENTRY_COMPLETE);
1065 cache_pending_ = true; 1087 cache_pending_ = true;
1066 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_CREATE_ENTRY); 1088 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_CREATE_ENTRY);
1067 return cache_->CreateEntry(cache_key_, &new_entry_, this); 1089 return cache_->CreateEntry(cache_key_, &new_entry_, this);
1068 } 1090 }
1069 1091
1070 int HttpCache::Transaction::DoCreateEntryComplete(int result) { 1092 int HttpCache::Transaction::DoCreateEntryComplete(int result) {
1071 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntryComplete"); 1093 TRACE_EVENT0("io", "HttpCacheTransaction::DoCreateEntryComplete");
1072 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is 1094 // 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 1095 // OK, otherwise the cache will end up with an active entry without any
1074 // transaction attached. 1096 // transaction attached.
1075 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_CREATE_ENTRY, 1097 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_CREATE_ENTRY,
1076 result); 1098 result);
1077 cache_pending_ = false; 1099 cache_pending_ = false;
1078 switch (result) { 1100 switch (result) {
1079 case OK: 1101 case OK:
1080 TransitionToState(STATE_ADD_TO_ENTRY); 1102 TransitionToState(STATE_ADD_TO_ENTRY);
1081 break; 1103 break;
1082 1104
1083 case ERR_CACHE_RACE: 1105 case ERR_CACHE_RACE:
1084 TransitionToState(STATE_INIT_ENTRY); 1106 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED);
1085 break; 1107 break;
1086 1108
1087 default: 1109 default:
1088 // We have a race here: Maybe we failed to open the entry and decided to 1110 // 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 1111 // create one, but by the time we called create, another transaction
1090 // already created the entry. If we want to eliminate this issue, we 1112 // already created the entry. If we want to eliminate this issue, we
1091 // need an atomic OpenOrCreate() method exposed by the disk cache. 1113 // need an atomic OpenOrCreate() method exposed by the disk cache.
1092 DLOG(WARNING) << "Unable to create cache entry"; 1114 DLOG(WARNING) << "Unable to create cache entry";
1093 mode_ = NONE; 1115 mode_ = NONE;
1094 if (partial_) 1116 if (partial_)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
1155 DCHECK(new_entry_); 1177 DCHECK(new_entry_);
1156 cache_pending_ = false; 1178 cache_pending_ = false;
1157 1179
1158 if (result == OK) 1180 if (result == OK)
1159 entry_ = new_entry_; 1181 entry_ = new_entry_;
1160 1182
1161 // If there is a failure, the cache should have taken care of new_entry_. 1183 // If there is a failure, the cache should have taken care of new_entry_.
1162 new_entry_ = NULL; 1184 new_entry_ = NULL;
1163 1185
1164 if (result == ERR_CACHE_RACE) { 1186 if (result == ERR_CACHE_RACE) {
1165 TransitionToState(STATE_INIT_ENTRY); 1187 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED);
1166 return OK; 1188 return OK;
1167 } 1189 }
1168 1190
1169 if (result == ERR_CACHE_LOCK_TIMEOUT) { 1191 if (result == ERR_CACHE_LOCK_TIMEOUT) {
1170 if (mode_ == READ) { 1192 if (mode_ == READ) {
1171 TransitionToState(STATE_NONE); 1193 TransitionToState(STATE_FINISH_HEADERS);
1172 return ERR_CACHE_MISS; 1194 return ERR_CACHE_MISS;
1173 } 1195 }
1174 1196
1175 // The cache is busy, bypass it for this transaction. 1197 // The cache is busy, bypass it for this transaction.
1176 mode_ = NONE; 1198 mode_ = NONE;
1177 TransitionToState(STATE_SEND_REQUEST); 1199 TransitionToState(STATE_SEND_REQUEST);
1178 if (partial_) { 1200 if (partial_) {
1179 partial_->RestoreHeaders(&custom_request_->extra_headers); 1201 partial_->RestoreHeaders(&custom_request_->extra_headers);
1180 partial_.reset(); 1202 partial_.reset();
1181 } 1203 }
1182 return OK; 1204 return OK;
1183 } 1205 }
1184 1206
1185 open_entry_last_used_ = entry_->disk_entry->GetLastUsed(); 1207 open_entry_last_used_ = entry_->disk_entry->GetLastUsed();
1186 1208
1187 // TODO(jkarlin): We should either handle the case or DCHECK. 1209 // TODO(jkarlin): We should either handle the case or DCHECK.
1188 if (result != OK) { 1210 if (result != OK) {
1189 NOTREACHED(); 1211 NOTREACHED();
1190 TransitionToState(STATE_NONE); 1212 TransitionToState(STATE_FINISH_HEADERS);
1191 return result; 1213 return result;
1192 } 1214 }
1193 1215
1194 if (mode_ == WRITE) { 1216 if (mode_ == WRITE) {
1195 if (partial_) 1217 if (partial_)
1196 partial_->RestoreHeaders(&custom_request_->extra_headers); 1218 partial_->RestoreHeaders(&custom_request_->extra_headers);
1197 TransitionToState(STATE_SEND_REQUEST); 1219 TransitionToState(STATE_SEND_REQUEST);
1198 } else { 1220 } else {
1199 // We have to read the headers from the cached entry. 1221 // We have to read the headers from the cached entry.
1200 DCHECK(mode_ & READ_META); 1222 DCHECK(mode_ & READ_META);
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
1323 } 1345 }
1324 1346
1325 int HttpCache::Transaction::DoCacheQueryData() { 1347 int HttpCache::Transaction::DoCacheQueryData() {
1326 TransitionToState(STATE_CACHE_QUERY_DATA_COMPLETE); 1348 TransitionToState(STATE_CACHE_QUERY_DATA_COMPLETE);
1327 return entry_->disk_entry->ReadyForSparseIO(io_callback_); 1349 return entry_->disk_entry->ReadyForSparseIO(io_callback_);
1328 } 1350 }
1329 1351
1330 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { 1352 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) {
1331 DCHECK_EQ(OK, result); 1353 DCHECK_EQ(OK, result);
1332 if (!cache_.get()) { 1354 if (!cache_.get()) {
1333 TransitionToState(STATE_NONE); 1355 TransitionToState(STATE_FINISH_HEADERS);
1334 return ERR_UNEXPECTED; 1356 return ERR_UNEXPECTED;
1335 } 1357 }
1336 1358
1337 return ValidateEntryHeadersAndContinue(); 1359 return ValidateEntryHeadersAndContinue();
1338 } 1360 }
1339 1361
1340 // We may end up here multiple times for a given request. 1362 // We may end up here multiple times for a given request.
1341 int HttpCache::Transaction::DoStartPartialCacheValidation() { 1363 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1342 if (mode_ == NONE) { 1364 if (mode_ == NONE) {
1343 TransitionToState(STATE_NONE); 1365 TransitionToState(STATE_FINISH_HEADERS);
1344 return OK; 1366 return OK;
1345 } 1367 }
1346 1368
1347 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); 1369 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION);
1348 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); 1370 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1349 } 1371 }
1350 1372
1351 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { 1373 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1352 if (!result) { 1374 if (!result) {
1353 // This is the end of the request. 1375 // This is the end of the request.
1354 if (mode_ & WRITE) { 1376 if (mode_ & WRITE) {
1355 DoneWritingToEntry(true); 1377 DoneWritingToEntry(true);
1356 } else { 1378 } else {
1357 cache_->DoneReadingFromEntry(entry_, this); 1379 cache_->DoneReadingFromEntry(entry_, this);
1358 entry_ = NULL; 1380 entry_ = NULL;
1359 } 1381 }
1360 TransitionToState(STATE_NONE); 1382 TransitionToState(STATE_FINISH_HEADERS);
1361 return result; 1383 return result;
1362 } 1384 }
1363 1385
1364 if (result < 0) { 1386 if (result < 0) {
1365 TransitionToState(STATE_NONE); 1387 TransitionToState(STATE_FINISH_HEADERS);
1366 return result; 1388 return result;
1367 } 1389 }
1368 1390
1369 partial_->PrepareCacheValidation(entry_->disk_entry, 1391 partial_->PrepareCacheValidation(entry_->disk_entry,
1370 &custom_request_->extra_headers); 1392 &custom_request_->extra_headers);
1371 1393
1372 if (reading_ && partial_->IsCurrentRangeCached()) { 1394 if (reading_ && partial_->IsCurrentRangeCached()) {
1373 TransitionToState(STATE_CACHE_READ_DATA); 1395 TransitionToState(STATE_CACHE_READ_DATA);
1374 return OK; 1396 return OK;
1375 } 1397 }
1376 1398
1377 return BeginCacheValidation(); 1399 return BeginCacheValidation();
1378 } 1400 }
1379 1401
1380 int HttpCache::Transaction::DoSendRequest() { 1402 int HttpCache::Transaction::DoSendRequest() {
1381 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequest"); 1403 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequest");
1382 DCHECK(mode_ & WRITE || mode_ == NONE); 1404 DCHECK(mode_ & WRITE || mode_ == NONE);
1383 DCHECK(!network_trans_.get()); 1405 DCHECK(!network_trans_.get());
1384 1406
1385 send_request_since_ = TimeTicks::Now(); 1407 send_request_since_ = TimeTicks::Now();
1386 1408
1387 // Create a network transaction. 1409 // Create a network transaction.
1388 int rv = 1410 int rv =
1389 cache_->network_layer_->CreateTransaction(priority_, &network_trans_); 1411 cache_->network_layer_->CreateTransaction(priority_, &network_trans_);
1390 if (rv != OK) { 1412 if (rv != OK) {
1391 TransitionToState(STATE_NONE); 1413 TransitionToState(STATE_FINISH_HEADERS);
1392 return rv; 1414 return rv;
1393 } 1415 }
1394 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_); 1416 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_);
1395 network_trans_->SetBeforeHeadersSentCallback(before_headers_sent_callback_); 1417 network_trans_->SetBeforeHeadersSentCallback(before_headers_sent_callback_);
1396 1418
1397 // Old load timing information, if any, is now obsolete. 1419 // Old load timing information, if any, is now obsolete.
1398 old_network_trans_load_timing_.reset(); 1420 old_network_trans_load_timing_.reset();
1399 old_remote_endpoint_ = IPEndPoint(); 1421 old_remote_endpoint_ = IPEndPoint();
1400 1422
1401 if (websocket_handshake_stream_base_create_helper_) 1423 if (websocket_handshake_stream_base_create_helper_)
1402 network_trans_->SetWebSocketHandshakeStreamCreateHelper( 1424 network_trans_->SetWebSocketHandshakeStreamCreateHelper(
1403 websocket_handshake_stream_base_create_helper_); 1425 websocket_handshake_stream_base_create_helper_);
1404 1426
1405 TransitionToState(STATE_SEND_REQUEST_COMPLETE); 1427 TransitionToState(STATE_SEND_REQUEST_COMPLETE);
1406 rv = network_trans_->Start(request_, io_callback_, net_log_); 1428 rv = network_trans_->Start(request_, io_callback_, net_log_);
1407 return rv; 1429 return rv;
1408 } 1430 }
1409 1431
1410 int HttpCache::Transaction::DoSendRequestComplete(int result) { 1432 int HttpCache::Transaction::DoSendRequestComplete(int result) {
1411 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequestComplete"); 1433 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequestComplete");
1412 if (!cache_.get()) { 1434 if (!cache_.get()) {
1413 TransitionToState(STATE_NONE); 1435 TransitionToState(STATE_FINISH_HEADERS);
1414 return ERR_UNEXPECTED; 1436 return ERR_UNEXPECTED;
1415 } 1437 }
1416 1438
1417 // If we tried to conditionalize the request and failed, we know 1439 // If we tried to conditionalize the request and failed, we know
1418 // we won't be reading from the cache after this point. 1440 // we won't be reading from the cache after this point.
1419 if (couldnt_conditionalize_request_) 1441 if (couldnt_conditionalize_request_)
1420 mode_ = WRITE; 1442 mode_ = WRITE;
1421 1443
1422 if (result == OK) { 1444 if (result == OK) {
1423 TransitionToState(STATE_SUCCESSFUL_SEND_REQUEST); 1445 TransitionToState(STATE_SUCCESSFUL_SEND_REQUEST);
(...skipping 10 matching lines...) Expand all
1434 // so GetResponseInfo() should never return NULL here. 1456 // so GetResponseInfo() should never return NULL here.
1435 DCHECK(response); 1457 DCHECK(response);
1436 response_.ssl_info = response->ssl_info; 1458 response_.ssl_info = response->ssl_info;
1437 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { 1459 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) {
1438 DCHECK(response); 1460 DCHECK(response);
1439 response_.cert_request_info = response->cert_request_info; 1461 response_.cert_request_info = response->cert_request_info;
1440 } else if (response_.was_cached) { 1462 } else if (response_.was_cached) {
1441 DoneWritingToEntry(true); 1463 DoneWritingToEntry(true);
1442 } 1464 }
1443 1465
1444 TransitionToState(STATE_NONE); 1466 TransitionToState(STATE_FINISH_HEADERS);
1445 return result; 1467 return result;
1446 } 1468 }
1447 1469
1448 // We received the response headers and there is no error. 1470 // We received the response headers and there is no error.
1449 int HttpCache::Transaction::DoSuccessfulSendRequest() { 1471 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1450 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); 1472 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest");
1451 DCHECK(!new_response_); 1473 DCHECK(!new_response_);
1452 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); 1474 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1453 1475
1454 if (new_response->headers->response_code() == 401 || 1476 if (new_response->headers->response_code() == 401 ||
1455 new_response->headers->response_code() == 407) { 1477 new_response->headers->response_code() == 407) {
1456 SetAuthResponse(*new_response); 1478 SetAuthResponse(*new_response);
1457 if (!reading_) { 1479 if (!reading_) {
1458 TransitionToState(STATE_NONE); 1480 TransitionToState(STATE_FINISH_HEADERS);
1459 return OK; 1481 return OK;
1460 } 1482 }
1461 1483
1462 // We initiated a second request the caller doesn't know about. We should be 1484 // 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 1485 // able to authenticate this request because we should have authenticated
1464 // this URL moments ago. 1486 // this URL moments ago.
1465 if (IsReadyToRestartForAuth()) { 1487 if (IsReadyToRestartForAuth()) {
1466 DCHECK(!response_.auth_challenge.get()); 1488 DCHECK(!response_.auth_challenge.get());
1467 TransitionToState(STATE_SEND_REQUEST_COMPLETE); 1489 TransitionToState(STATE_SEND_REQUEST_COMPLETE);
1468 // In theory we should check to see if there are new cookies, but there 1490 // In theory we should check to see if there are new cookies, but there
1469 // is no way to do that from here. 1491 // is no way to do that from here.
1470 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_); 1492 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_);
1471 } 1493 }
1472 1494
1473 // We have to perform cleanup at this point so that at least the next 1495 // 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 1496 // 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 1497 // 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 1498 // fail again, and potentially loop. This can happen if the credentials
1477 // expire while chrome is suspended. 1499 // expire while chrome is suspended.
1478 if (entry_) 1500 if (entry_)
1479 DoomPartialEntry(false); 1501 DoomPartialEntry(false);
1480 mode_ = NONE; 1502 mode_ = NONE;
1481 partial_.reset(); 1503 partial_.reset();
1482 ResetNetworkTransaction(); 1504 ResetNetworkTransaction();
1483 TransitionToState(STATE_NONE); 1505 TransitionToState(STATE_FINISH_HEADERS);
1484 return ERR_CACHE_AUTH_FAILURE_AFTER_READ; 1506 return ERR_CACHE_AUTH_FAILURE_AFTER_READ;
1485 } 1507 }
1486 1508
1487 new_response_ = new_response; 1509 new_response_ = new_response;
1488 if (!ValidatePartialResponse() && !auth_response_.headers.get()) { 1510 if (!ValidatePartialResponse() && !auth_response_.headers.get()) {
1489 // Something went wrong with this request and we have to restart it. 1511 // 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 1512 // If we have an authentication response, we are exposed to weird things
1491 // hapenning if the user cancels the authentication before we receive 1513 // hapenning if the user cancels the authentication before we receive
1492 // the new response. 1514 // the new response.
1493 net_log_.AddEvent(NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST); 1515 net_log_.AddEvent(NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST);
(...skipping 17 matching lines...) Expand all
1511 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); 1533 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE);
1512 } 1534 }
1513 1535
1514 // Invalidate any cached GET with a successful PUT or DELETE. 1536 // Invalidate any cached GET with a successful PUT or DELETE.
1515 if (mode_ == WRITE && 1537 if (mode_ == WRITE &&
1516 (request_->method == "PUT" || request_->method == "DELETE")) { 1538 (request_->method == "PUT" || request_->method == "DELETE")) {
1517 if (NonErrorResponse(new_response->headers->response_code())) { 1539 if (NonErrorResponse(new_response->headers->response_code())) {
1518 int ret = cache_->DoomEntry(cache_key_, NULL); 1540 int ret = cache_->DoomEntry(cache_key_, NULL);
1519 DCHECK_EQ(OK, ret); 1541 DCHECK_EQ(OK, ret);
1520 } 1542 }
1521 cache_->DoneWritingToEntry(entry_, true); 1543 cache_->DoneWritingToEntry(entry_, true, this);
1522 entry_ = NULL; 1544 entry_ = NULL;
1523 mode_ = NONE; 1545 mode_ = NONE;
1524 } 1546 }
1525 1547
1526 // Invalidate any cached GET with a successful POST. 1548 // Invalidate any cached GET with a successful POST.
1527 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && 1549 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1528 request_->method == "POST" && 1550 request_->method == "POST" &&
1529 NonErrorResponse(new_response->headers->response_code())) { 1551 NonErrorResponse(new_response->headers->response_code())) {
1530 cache_->DoomMainEntryForUrl(request_->url); 1552 cache_->DoomMainEntryForUrl(request_->url);
1531 } 1553 }
1532 1554
1533 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); 1555 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1534 1556
1535 if (new_response_->headers->response_code() == 416 && 1557 if (new_response_->headers->response_code() == 416 &&
1536 (request_->method == "GET" || request_->method == "POST")) { 1558 (request_->method == "GET" || request_->method == "POST")) {
1537 // If there is an active entry it may be destroyed with this transaction. 1559 // If there is an active entry it may be destroyed with this transaction.
1538 SetResponse(*new_response_); 1560 SetResponse(*new_response_);
1539 TransitionToState(STATE_NONE); 1561 TransitionToState(STATE_FINISH_HEADERS);
1540 return OK; 1562 return OK;
1541 } 1563 }
1542 1564
1543 // Are we expecting a response to a conditional query? 1565 // Are we expecting a response to a conditional query?
1544 if (mode_ == READ_WRITE || mode_ == UPDATE) { 1566 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1545 if (new_response->headers->response_code() == 304 || handling_206_) { 1567 if (new_response->headers->response_code() == 304 || handling_206_) {
1546 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); 1568 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED);
1547 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); 1569 TransitionToState(STATE_UPDATE_CACHED_RESPONSE);
1548 return OK; 1570 return OK;
1549 } 1571 }
1550 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); 1572 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED);
1551 mode_ = WRITE; 1573 mode_ = WRITE;
1552 } 1574 }
1553 1575
1554 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); 1576 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE);
1577
jkarlin 2017/04/06 17:37:14 nit: remove newline
shivanisha 2017/04/07 21:11:17 done
1555 return OK; 1578 return OK;
1556 } 1579 }
1557 1580
1558 // We received 304 or 206 and we want to update the cached response headers. 1581 // We received 304 or 206 and we want to update the cached response headers.
1559 int HttpCache::Transaction::DoUpdateCachedResponse() { 1582 int HttpCache::Transaction::DoUpdateCachedResponse() {
1560 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); 1583 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse");
1561 int rv = OK; 1584 int rv = OK;
1562 // Update the cached response based on the headers and properties of 1585 // Update the cached response based on the headers and properties of
1563 // new_response_. 1586 // new_response_.
1564 response_.headers->Update(*new_response_->headers.get()); 1587 response_.headers->Update(*new_response_->headers.get());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1617 DCHECK(!handling_206_); 1640 DCHECK(!handling_206_);
1618 // We got a "not modified" response and already updated the corresponding 1641 // We got a "not modified" response and already updated the corresponding
1619 // cache entry above. 1642 // cache entry above.
1620 // 1643 //
1621 // By closing the cached entry now, we make sure that the 304 rather than 1644 // 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. 1645 // the cached 200 response, is what will be returned to the user.
1623 DoneWritingToEntry(true); 1646 DoneWritingToEntry(true);
1624 } else if (entry_ && !handling_206_) { 1647 } else if (entry_ && !handling_206_) {
1625 DCHECK_EQ(READ_WRITE, mode_); 1648 DCHECK_EQ(READ_WRITE, mode_);
1626 if (!partial_ || partial_->IsLastRange()) { 1649 if (!partial_ || partial_->IsLastRange()) {
1627 cache_->ConvertWriterToReader(entry_);
1628 mode_ = READ; 1650 mode_ = READ;
1629 } 1651 }
1630 // We no longer need the network transaction, so destroy it. 1652 // We no longer need the network transaction, so destroy it.
1631 ResetNetworkTransaction(); 1653 ResetNetworkTransaction();
1632 } else if (entry_ && handling_206_ && truncated_ && 1654 } else if (entry_ && handling_206_ && truncated_ &&
1633 partial_->initial_validation()) { 1655 partial_->initial_validation()) {
1634 // We just finished the validation of a truncated entry, and the server 1656 // 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 1657 // is willing to resume the operation. Now we go back and start serving
1636 // the first part to the user. 1658 // the first part to the user.
1637 ResetNetworkTransaction(); 1659 ResetNetworkTransaction();
(...skipping 17 matching lines...) Expand all
1655 if (handling_206_ && partial_) 1677 if (handling_206_ && partial_)
1656 partial_->FixContentLength(new_response_->headers.get()); 1678 partial_->FixContentLength(new_response_->headers.get());
1657 1679
1658 SetResponse(*new_response_); 1680 SetResponse(*new_response_);
1659 1681
1660 if (request_->method == "HEAD") { 1682 if (request_->method == "HEAD") {
1661 // This response is replacing the cached one. 1683 // This response is replacing the cached one.
1662 DoneWritingToEntry(false); 1684 DoneWritingToEntry(false);
1663 mode_ = NONE; 1685 mode_ = NONE;
1664 new_response_ = NULL; 1686 new_response_ = NULL;
1665 TransitionToState(STATE_NONE); 1687 TransitionToState(STATE_FINISH_HEADERS);
1666 return OK; 1688 return OK;
1667 } 1689 }
1668 1690
1669 if (handling_206_ && !CanResume(false)) { 1691 if (handling_206_ && !CanResume(false)) {
1670 // There is no point in storing this resource because it will never be used. 1692 // 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. 1693 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries.
1672 DoneWritingToEntry(false); 1694 DoneWritingToEntry(false);
1673 if (partial_) 1695 if (partial_)
1674 partial_->FixResponseHeaders(response_.headers.get(), true); 1696 partial_->FixResponseHeaders(response_.headers.get(), true);
1675 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1697 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1676 return OK; 1698 return OK;
1677 } 1699 }
1678 1700
1679 TransitionToState(STATE_CACHE_WRITE_RESPONSE); 1701 TransitionToState(STATE_CACHE_WRITE_RESPONSE);
1680 return OK; 1702 return OK;
1681 } 1703 }
1682 1704
1683 int HttpCache::Transaction::DoCacheWriteResponse() { 1705 int HttpCache::Transaction::DoCacheWriteResponse() {
1684 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponse"); 1706 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponse");
1685 TransitionToState(STATE_CACHE_WRITE_RESPONSE_COMPLETE); 1707 TransitionToState(STATE_CACHE_WRITE_RESPONSE_COMPLETE);
1708
1709 // Invalidate any current entry with a successful response if this transaction
1710 // cannot write to this entry.
jkarlin 2017/04/06 17:37:14 Also mention that the transaction continues on wit
shivanisha 2017/04/07 21:11:16 done
1711 if (entry_ && response_.headers &&
1712 !cache_->CanTransactionWriteResponseHeaders(
1713 entry_, this, response_.headers->response_code())) {
1714 cache_->DoneWritingToEntry(entry_, false, this);
1715 entry_ = nullptr;
1716 mode_ = NONE;
1717 return OK;
1718 }
1719
1686 return WriteResponseInfoToEntry(truncated_); 1720 return WriteResponseInfoToEntry(truncated_);
1687 } 1721 }
1688 1722
1689 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) { 1723 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) {
1690 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponseComplete"); 1724 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponseComplete");
1691 TransitionToState(STATE_TRUNCATE_CACHED_DATA); 1725 TransitionToState(STATE_TRUNCATE_CACHED_DATA);
1692 return OnWriteResponseInfoToEntryComplete(result); 1726 return OnWriteResponseInfoToEntryComplete(result);
1693 } 1727 }
1694 1728
1695 int HttpCache::Transaction::DoTruncateCachedData() { 1729 int HttpCache::Transaction::DoTruncateCachedData() {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1737 } 1771 }
1738 1772
1739 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1773 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1740 return OK; 1774 return OK;
1741 } 1775 }
1742 1776
1743 int HttpCache::Transaction::DoPartialHeadersReceived() { 1777 int HttpCache::Transaction::DoPartialHeadersReceived() {
1744 new_response_ = NULL; 1778 new_response_ = NULL;
1745 1779
1746 if (!partial_) { 1780 if (!partial_) {
1747 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) 1781 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) {
1748 TransitionToState(STATE_CACHE_READ_METADATA); 1782 TransitionToState(STATE_CACHE_READ_METADATA);
1749 else 1783 } else {
1750 TransitionToState(STATE_NONE); 1784 DCHECK(!reading_);
1785 TransitionToState(STATE_FINISH_HEADERS);
1786 }
1751 return OK; 1787 return OK;
1752 } 1788 }
1753 1789
1754 if (reading_) { 1790 if (reading_) {
1755 if (network_trans_.get()) { 1791 if (network_trans_.get()) {
1756 TransitionToState(STATE_NETWORK_READ); 1792 TransitionToState(STATE_NETWORK_READ);
1757 } else { 1793 } else {
1758 TransitionToState(STATE_CACHE_READ_DATA); 1794 TransitionToState(STATE_CACHE_READ_DATA);
1759 } 1795 }
1760 } else if (mode_ != NONE) { 1796 } else if (mode_ != NONE) {
1761 // We are about to return the headers for a byte-range request to the user, 1797 // We are about to return the headers for a byte-range request to the user,
1762 // so let's fix them. 1798 // so let's fix them.
1763 partial_->FixResponseHeaders(response_.headers.get(), true); 1799 partial_->FixResponseHeaders(response_.headers.get(), true);
1764 TransitionToState(STATE_NONE); 1800 TransitionToState(STATE_FINISH_HEADERS);
1765 } else { 1801 } else {
1766 TransitionToState(STATE_NONE); 1802 TransitionToState(STATE_FINISH_HEADERS);
1767 } 1803 }
1768 return OK; 1804 return OK;
1769 } 1805 }
1770 1806
1807 int HttpCache::Transaction::DoHeadersPhaseCannotProceed() {
1808 // If its the Start state machine and it cannot proceed due to a failure,
1809 // restart this transaction.
1810 DCHECK(!reading_);
1811 TransitionToState(STATE_INIT_ENTRY);
1812 cache_entry_status_ = CacheEntryStatus::ENTRY_UNDEFINED;
1813 entry_ = nullptr;
1814 mode_ = original_mode_;
1815 if (network_trans_)
1816 network_trans_.reset();
1817 return OK;
1818 }
1819
1820 int HttpCache::Transaction::DoFinishHeaders(int result) {
1821 if (!entry_ || result != OK) {
1822 TransitionToState(STATE_NONE);
1823 return result;
1824 }
1825
1826 TransitionToState(STATE_FINISH_HEADERS_COMPLETE);
1827
1828 // If it was an auth failure or 416, this transaction should continue to be
1829 // headers_transaction till consumer takes an action, so no need to do
1830 // anything now.
1831 if (auth_response_.headers.get() ||
1832 (new_response_ && new_response_->headers &&
1833 new_response_->headers->response_code() == 416))
1834 return OK;
1835
1836 // If there is no response body to be written or read, it does not need to
1837 // wait.
1838 if (request_->method == "HEAD")
1839 return OK;
1840
1841 return cache_->DoneWithResponseHeaders(entry_, this);
jkarlin 2017/04/06 17:37:14 Since you're not passing a callback it's non-obvio
shivanisha 2017/04/07 21:11:17 done
1842 }
1843
1844 int HttpCache::Transaction::DoFinishHeadersComplete(int rv) {
1845 if (rv == ERR_CACHE_RACE) {
1846 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED);
1847 return OK;
1848 }
1849
1850 TransitionToState(STATE_NONE);
1851 return rv;
1852 }
1853
1771 int HttpCache::Transaction::DoCacheReadMetadata() { 1854 int HttpCache::Transaction::DoCacheReadMetadata() {
1772 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); 1855 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata");
1773 DCHECK(entry_); 1856 DCHECK(entry_);
1774 DCHECK(!response_.metadata.get()); 1857 DCHECK(!response_.metadata.get());
1775 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); 1858 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE);
1776 1859
1777 response_.metadata = 1860 response_.metadata =
1778 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); 1861 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1779 1862
1780 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); 1863 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO);
1781 return entry_->disk_entry->ReadData(kMetadataIndex, 0, 1864 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1782 response_.metadata.get(), 1865 response_.metadata.get(),
1783 response_.metadata->size(), 1866 response_.metadata->size(),
1784 io_callback_); 1867 io_callback_);
1785 } 1868 }
1786 1869
1787 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { 1870 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1788 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); 1871 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete");
1789 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, 1872 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO,
1790 result); 1873 result);
1791 if (result != response_.metadata->size()) 1874 if (result != response_.metadata->size())
1792 return OnCacheReadError(result, false); 1875 return OnCacheReadError(result, false);
1793 TransitionToState(STATE_NONE); 1876
1877 TransitionToState(STATE_FINISH_HEADERS);
1794 return OK; 1878 return OK;
1795 } 1879 }
1796 1880
1797 int HttpCache::Transaction::DoNetworkRead() { 1881 int HttpCache::Transaction::DoNetworkRead() {
1798 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); 1882 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead");
1799 TransitionToState(STATE_NETWORK_READ_COMPLETE); 1883 TransitionToState(STATE_NETWORK_READ_COMPLETE);
1800 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); 1884 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1801 } 1885 }
1802 1886
1803 int HttpCache::Transaction::DoNetworkReadComplete(int result) { 1887 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
2080 return false; 2164 return false;
2081 2165
2082 return true; 2166 return true;
2083 } 2167 }
2084 2168
2085 int HttpCache::Transaction::BeginCacheRead() { 2169 int HttpCache::Transaction::BeginCacheRead() {
2086 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges. 2170 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges.
2087 // TODO(jkarlin): Either handle this case or DCHECK. 2171 // TODO(jkarlin): Either handle this case or DCHECK.
2088 if (response_.headers->response_code() == 206 || partial_) { 2172 if (response_.headers->response_code() == 206 || partial_) {
2089 NOTREACHED(); 2173 NOTREACHED();
2090 TransitionToState(STATE_NONE); 2174 TransitionToState(STATE_FINISH_HEADERS);
2091 return ERR_CACHE_MISS; 2175 return ERR_CACHE_MISS;
2092 } 2176 }
2093 2177
2094 // We don't have the whole resource. 2178 // We don't have the whole resource.
2095 if (truncated_) { 2179 if (truncated_) {
2096 TransitionToState(STATE_NONE); 2180 TransitionToState(STATE_FINISH_HEADERS);
2097 return ERR_CACHE_MISS; 2181 return ERR_CACHE_MISS;
2098 } 2182 }
2099 2183
2100 if (RequiresValidation()) { 2184 if (RequiresValidation()) {
2101 TransitionToState(STATE_NONE); 2185 TransitionToState(STATE_FINISH_HEADERS);
2102 return ERR_CACHE_MISS; 2186 return ERR_CACHE_MISS;
2103 } 2187 }
2104 2188
2105 if (request_->method == "HEAD") 2189 if (request_->method == "HEAD")
2106 FixHeadersForHead(); 2190 FixHeadersForHead();
2107 2191
2108 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2192 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2109 TransitionToState(STATE_CACHE_READ_METADATA); 2193 TransitionToState(STATE_CACHE_READ_METADATA);
2110 else 2194 else
2111 TransitionToState(STATE_NONE); 2195 TransitionToState(STATE_FINISH_HEADERS);
2112 2196
2113 return OK; 2197 return OK;
2114 } 2198 }
2115 2199
2116 int HttpCache::Transaction::BeginCacheValidation() { 2200 int HttpCache::Transaction::BeginCacheValidation() {
2117 DCHECK_EQ(mode_, READ_WRITE); 2201 DCHECK_EQ(mode_, READ_WRITE);
2118 2202
2119 bool skip_validation = !RequiresValidation(); 2203 bool skip_validation = !RequiresValidation();
2120 2204
2121 if (request_->method == "HEAD" && 2205 if (request_->method == "HEAD" &&
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
2561 if (partial_) { 2645 if (partial_) {
2562 if (truncated_ || is_sparse_ || !invalid_range_) { 2646 if (truncated_ || is_sparse_ || !invalid_range_) {
2563 // We are going to return the saved response headers to the caller, so 2647 // We are going to return the saved response headers to the caller, so
2564 // we may need to adjust them first. 2648 // we may need to adjust them first.
2565 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 2649 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
2566 return OK; 2650 return OK;
2567 } else { 2651 } else {
2568 partial_.reset(); 2652 partial_.reset();
2569 } 2653 }
2570 } 2654 }
2571 cache_->ConvertWriterToReader(entry_); 2655
2572 mode_ = READ; 2656 mode_ = READ;
2573 2657
2574 if (request_->method == "HEAD") 2658 if (request_->method == "HEAD")
2575 FixHeadersForHead(); 2659 FixHeadersForHead();
2576 2660
2577 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2661 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2578 TransitionToState(STATE_CACHE_READ_METADATA); 2662 TransitionToState(STATE_CACHE_READ_METADATA);
2579 else 2663 else
2580 TransitionToState(STATE_NONE); 2664 TransitionToState(STATE_FINISH_HEADERS);
2581 return OK; 2665 return OK;
2582 } 2666 }
2583 2667
2584 int HttpCache::Transaction::WriteToEntry(int index, int offset, 2668 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2585 IOBuffer* data, int data_len, 2669 IOBuffer* data, int data_len,
2586 const CompletionCallback& callback) { 2670 const CompletionCallback& callback) {
2587 if (!entry_) 2671 if (!entry_)
2588 return data_len; 2672 return data_len;
2589 2673
2674 DCHECK(
2675 cache_->CanTransactionWriteResponseBody(entry_, this, request_->method));
2676
2590 int rv = 0; 2677 int rv = 0;
2591 if (!partial_ || !data_len) { 2678 if (!partial_ || !data_len) {
2592 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback, 2679 rv = entry_->disk_entry->WriteData(index, offset, data, data_len, callback,
2593 true); 2680 true);
2594 } else { 2681 } else {
2595 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback); 2682 rv = partial_->CacheWrite(entry_->disk_entry, data, data_len, callback);
2596 } 2683 }
2597 return rv; 2684 return rv;
2598 } 2685 }
2599 2686
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
2649 } 2736 }
2650 return OK; 2737 return OK;
2651 } 2738 }
2652 2739
2653 void HttpCache::Transaction::DoneWritingToEntry(bool success) { 2740 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2654 if (!entry_) 2741 if (!entry_)
2655 return; 2742 return;
2656 2743
2657 RecordHistograms(); 2744 RecordHistograms();
2658 2745
2659 cache_->DoneWritingToEntry(entry_, success); 2746 cache_->DoneWritingToEntry(entry_, success, this);
2660 entry_ = NULL; 2747 entry_ = NULL;
2661 mode_ = NONE; // switch to 'pass through' mode 2748 mode_ = NONE; // switch to 'pass through' mode
2662 } 2749 }
2663 2750
2664 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { 2751 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2665 DLOG(ERROR) << "ReadData failed: " << result; 2752 DLOG(ERROR) << "ReadData failed: " << result;
2666 const int result_for_histogram = std::max(0, -result); 2753 const int result_for_histogram = std::max(0, -result);
2667 if (restart) { 2754 if (restart) {
2668 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", 2755 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2669 result_for_histogram); 2756 result_for_histogram);
(...skipping 347 matching lines...) Expand 10 before | Expand all | Expand 10 after
3017 } 3104 }
3018 3105
3019 void HttpCache::Transaction::TransitionToState(State state) { 3106 void HttpCache::Transaction::TransitionToState(State state) {
3020 // Ensure that the state is only set once per Do* state. 3107 // Ensure that the state is only set once per Do* state.
3021 DCHECK(in_do_loop_); 3108 DCHECK(in_do_loop_);
3022 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; 3109 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state;
3023 next_state_ = state; 3110 next_state_ = state;
3024 } 3111 }
3025 3112
3026 } // namespace net 3113 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698