Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 writing_incomplete = cache_->IsTransactionWritingIncomplete( |
| 200 if (cancel_request) { | 201 entry_, this, request_->method); |
| 201 if (partial_) { | 202 if (writing_incomplete && 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, writing_incomplete); |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 // Access timestamp for histograms only if entry is already written, to avoid |
| 1208 // data race since cache thread can also access this. | |
| 1209 if (!cache_->IsWritingInProgress(entry_)) | |
| 1210 open_entry_last_used_ = entry_->disk_entry->GetLastUsed(); | |
| 1186 | 1211 |
| 1187 // TODO(jkarlin): We should either handle the case or DCHECK. | 1212 // TODO(jkarlin): We should either handle the case or DCHECK. |
| 1188 if (result != OK) { | 1213 if (result != OK) { |
| 1189 NOTREACHED(); | 1214 NOTREACHED(); |
| 1190 TransitionToState(STATE_NONE); | 1215 TransitionToState(STATE_FINISH_HEADERS); |
| 1191 return result; | 1216 return result; |
| 1192 } | 1217 } |
| 1193 | 1218 |
| 1194 if (mode_ == WRITE) { | 1219 if (mode_ == WRITE) { |
| 1195 if (partial_) | 1220 if (partial_) |
| 1196 partial_->RestoreHeaders(&custom_request_->extra_headers); | 1221 partial_->RestoreHeaders(&custom_request_->extra_headers); |
| 1197 TransitionToState(STATE_SEND_REQUEST); | 1222 TransitionToState(STATE_SEND_REQUEST); |
| 1198 } else { | 1223 } else { |
| 1199 // We have to read the headers from the cached entry. | 1224 // We have to read the headers from the cached entry. |
| 1200 DCHECK(mode_ & READ_META); | 1225 DCHECK(mode_ & READ_META); |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 1219 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { | 1244 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { |
| 1220 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadResponseComplete"); | 1245 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadResponseComplete"); |
| 1221 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, | 1246 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, |
| 1222 result); | 1247 result); |
| 1223 if (result != io_buf_len_ || | 1248 if (result != io_buf_len_ || |
| 1224 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, &response_, | 1249 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, &response_, |
| 1225 &truncated_)) { | 1250 &truncated_)) { |
| 1226 return OnCacheReadError(result, true); | 1251 return OnCacheReadError(result, true); |
| 1227 } | 1252 } |
| 1228 | 1253 |
| 1229 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 1254 if (!cache_->IsWritingInProgress(entry_)) { |
| 1230 int64_t full_response_length = response_.headers->GetContentLength(); | 1255 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
| 1256 int64_t full_response_length = response_.headers->GetContentLength(); | |
| 1231 | 1257 |
| 1232 // Some resources may have slipped in as truncated when they're not. | 1258 // Some resources may have slipped in as truncated when they're not. |
| 1233 if (full_response_length == current_size) | 1259 if (full_response_length == current_size) |
| 1234 truncated_ = false; | 1260 truncated_ = false; |
| 1235 | 1261 |
| 1236 // The state machine's handling of StopCaching unfortunately doesn't deal well | 1262 // 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 | 1263 // 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 | 1264 // 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 | 1265 // the following logic is put in place to defer such requests to the |
| 1240 // cache should not be storing multi gigabyte resources. See | 1266 // network. The cache should not be storing multi gigabyte resources. See |
| 1241 // http://crbug.com/89567. | 1267 // http://crbug.com/89567. |
| 1242 if ((truncated_ || response_.headers->response_code() == 206) && | 1268 if ((truncated_ || response_.headers->response_code() == 206) && |
| 1243 !range_requested_ && | 1269 !range_requested_ && |
| 1244 full_response_length > std::numeric_limits<int32_t>::max()) { | 1270 full_response_length > std::numeric_limits<int32_t>::max()) { |
| 1245 // Does not release the cache entry. If another transaction wants to use | 1271 // Does not release the cache entry. If another transaction wants to use |
| 1246 // this cache entry while this transaction is active, the second transaction | 1272 // this cache entry while this transaction is active, the second |
| 1247 // will fall back to the network after the timeout. | 1273 // transaction will fall back to the network after the timeout. |
| 1248 DCHECK(!partial_); | 1274 DCHECK(!partial_); |
| 1249 mode_ = NONE; | 1275 mode_ = NONE; |
| 1250 TransitionToState(STATE_SEND_REQUEST); | 1276 TransitionToState(STATE_SEND_REQUEST); |
| 1251 return OK; | 1277 return OK; |
| 1278 } | |
| 1252 } | 1279 } |
| 1253 | 1280 |
| 1254 if (response_.unused_since_prefetch != | 1281 if (response_.unused_since_prefetch != |
| 1255 !!(request_->load_flags & LOAD_PREFETCH)) { | 1282 !!(request_->load_flags & LOAD_PREFETCH)) { |
| 1256 // Either this is the first use of an entry since it was prefetched XOR | 1283 // 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 | 1284 // 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. | 1285 // valid for this transaction but the bit needs to be flipped in storage. |
| 1259 TransitionToState(STATE_TOGGLE_UNUSED_SINCE_PREFETCH); | 1286 TransitionToState(STATE_TOGGLE_UNUSED_SINCE_PREFETCH); |
| 1260 return OK; | 1287 return OK; |
| 1261 } | 1288 } |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1323 } | 1350 } |
| 1324 | 1351 |
| 1325 int HttpCache::Transaction::DoCacheQueryData() { | 1352 int HttpCache::Transaction::DoCacheQueryData() { |
| 1326 TransitionToState(STATE_CACHE_QUERY_DATA_COMPLETE); | 1353 TransitionToState(STATE_CACHE_QUERY_DATA_COMPLETE); |
| 1327 return entry_->disk_entry->ReadyForSparseIO(io_callback_); | 1354 return entry_->disk_entry->ReadyForSparseIO(io_callback_); |
| 1328 } | 1355 } |
| 1329 | 1356 |
| 1330 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { | 1357 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { |
| 1331 DCHECK_EQ(OK, result); | 1358 DCHECK_EQ(OK, result); |
| 1332 if (!cache_.get()) { | 1359 if (!cache_.get()) { |
| 1333 TransitionToState(STATE_NONE); | 1360 TransitionToState(STATE_FINISH_HEADERS); |
| 1334 return ERR_UNEXPECTED; | 1361 return ERR_UNEXPECTED; |
| 1335 } | 1362 } |
| 1336 | 1363 |
| 1337 return ValidateEntryHeadersAndContinue(); | 1364 return ValidateEntryHeadersAndContinue(); |
| 1338 } | 1365 } |
| 1339 | 1366 |
| 1340 // We may end up here multiple times for a given request. | 1367 // We may end up here multiple times for a given request. |
| 1341 int HttpCache::Transaction::DoStartPartialCacheValidation() { | 1368 int HttpCache::Transaction::DoStartPartialCacheValidation() { |
| 1342 if (mode_ == NONE) { | 1369 if (mode_ == NONE) { |
| 1343 TransitionToState(STATE_NONE); | 1370 TransitionToState(STATE_FINISH_HEADERS); |
| 1344 return OK; | 1371 return OK; |
| 1345 } | 1372 } |
| 1346 | 1373 |
| 1347 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); | 1374 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); |
| 1348 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); | 1375 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); |
| 1349 } | 1376 } |
| 1350 | 1377 |
| 1351 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { | 1378 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { |
| 1352 if (!result) { | 1379 if (!result) { |
| 1353 // This is the end of the request. | 1380 // This is the end of the request. |
| 1354 if (mode_ & WRITE) { | 1381 if (mode_ & WRITE) { |
| 1355 DoneWritingToEntry(true); | 1382 DoneWritingToEntry(true); |
| 1356 } else { | 1383 } else { |
| 1357 cache_->DoneReadingFromEntry(entry_, this); | 1384 cache_->DoneReadingFromEntry(entry_, this); |
| 1358 entry_ = NULL; | 1385 entry_ = NULL; |
| 1359 } | 1386 } |
| 1360 TransitionToState(STATE_NONE); | 1387 TransitionToState(STATE_FINISH_HEADERS); |
| 1361 return result; | 1388 return result; |
| 1362 } | 1389 } |
| 1363 | 1390 |
| 1364 if (result < 0) { | 1391 if (result < 0) { |
| 1365 TransitionToState(STATE_NONE); | 1392 TransitionToState(STATE_FINISH_HEADERS); |
| 1366 return result; | 1393 return result; |
| 1367 } | 1394 } |
| 1368 | 1395 |
| 1369 partial_->PrepareCacheValidation(entry_->disk_entry, | 1396 partial_->PrepareCacheValidation(entry_->disk_entry, |
| 1370 &custom_request_->extra_headers); | 1397 &custom_request_->extra_headers); |
| 1371 | 1398 |
| 1372 if (reading_ && partial_->IsCurrentRangeCached()) { | 1399 if (reading_ && partial_->IsCurrentRangeCached()) { |
| 1373 TransitionToState(STATE_CACHE_READ_DATA); | 1400 TransitionToState(STATE_CACHE_READ_DATA); |
| 1374 return OK; | 1401 return OK; |
| 1375 } | 1402 } |
| 1376 | 1403 |
| 1377 return BeginCacheValidation(); | 1404 return BeginCacheValidation(); |
| 1378 } | 1405 } |
| 1379 | 1406 |
| 1380 int HttpCache::Transaction::DoSendRequest() { | 1407 int HttpCache::Transaction::DoSendRequest() { |
| 1381 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequest"); | 1408 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequest"); |
| 1382 DCHECK(mode_ & WRITE || mode_ == NONE); | 1409 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 1383 DCHECK(!network_trans_.get()); | 1410 DCHECK(!network_trans_.get()); |
| 1384 | 1411 |
| 1385 send_request_since_ = TimeTicks::Now(); | 1412 send_request_since_ = TimeTicks::Now(); |
| 1386 | 1413 |
| 1387 // Create a network transaction. | 1414 // Create a network transaction. |
| 1388 int rv = | 1415 int rv = |
| 1389 cache_->network_layer_->CreateTransaction(priority_, &network_trans_); | 1416 cache_->network_layer_->CreateTransaction(priority_, &network_trans_); |
| 1390 if (rv != OK) { | 1417 if (rv != OK) { |
| 1391 TransitionToState(STATE_NONE); | 1418 TransitionToState(STATE_FINISH_HEADERS); |
| 1392 return rv; | 1419 return rv; |
| 1393 } | 1420 } |
| 1394 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_); | 1421 network_trans_->SetBeforeNetworkStartCallback(before_network_start_callback_); |
| 1395 network_trans_->SetBeforeHeadersSentCallback(before_headers_sent_callback_); | 1422 network_trans_->SetBeforeHeadersSentCallback(before_headers_sent_callback_); |
| 1396 | 1423 |
| 1397 // Old load timing information, if any, is now obsolete. | 1424 // Old load timing information, if any, is now obsolete. |
| 1398 old_network_trans_load_timing_.reset(); | 1425 old_network_trans_load_timing_.reset(); |
| 1399 old_remote_endpoint_ = IPEndPoint(); | 1426 old_remote_endpoint_ = IPEndPoint(); |
| 1400 | 1427 |
| 1401 if (websocket_handshake_stream_base_create_helper_) | 1428 if (websocket_handshake_stream_base_create_helper_) |
| 1402 network_trans_->SetWebSocketHandshakeStreamCreateHelper( | 1429 network_trans_->SetWebSocketHandshakeStreamCreateHelper( |
| 1403 websocket_handshake_stream_base_create_helper_); | 1430 websocket_handshake_stream_base_create_helper_); |
| 1404 | 1431 |
| 1405 TransitionToState(STATE_SEND_REQUEST_COMPLETE); | 1432 TransitionToState(STATE_SEND_REQUEST_COMPLETE); |
| 1406 rv = network_trans_->Start(request_, io_callback_, net_log_); | 1433 rv = network_trans_->Start(request_, io_callback_, net_log_); |
| 1407 return rv; | 1434 return rv; |
| 1408 } | 1435 } |
| 1409 | 1436 |
| 1410 int HttpCache::Transaction::DoSendRequestComplete(int result) { | 1437 int HttpCache::Transaction::DoSendRequestComplete(int result) { |
| 1411 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequestComplete"); | 1438 TRACE_EVENT0("io", "HttpCacheTransaction::DoSendRequestComplete"); |
| 1412 if (!cache_.get()) { | 1439 if (!cache_.get()) { |
| 1413 TransitionToState(STATE_NONE); | 1440 TransitionToState(STATE_FINISH_HEADERS); |
| 1414 return ERR_UNEXPECTED; | 1441 return ERR_UNEXPECTED; |
| 1415 } | 1442 } |
| 1416 | 1443 |
| 1417 // If we tried to conditionalize the request and failed, we know | 1444 // If we tried to conditionalize the request and failed, we know |
| 1418 // we won't be reading from the cache after this point. | 1445 // we won't be reading from the cache after this point. |
| 1419 if (couldnt_conditionalize_request_) | 1446 if (couldnt_conditionalize_request_) |
| 1420 mode_ = WRITE; | 1447 mode_ = WRITE; |
| 1421 | 1448 |
| 1422 if (result == OK) { | 1449 if (result == OK) { |
| 1423 TransitionToState(STATE_SUCCESSFUL_SEND_REQUEST); | 1450 TransitionToState(STATE_SUCCESSFUL_SEND_REQUEST); |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 1434 // so GetResponseInfo() should never return NULL here. | 1461 // so GetResponseInfo() should never return NULL here. |
| 1435 DCHECK(response); | 1462 DCHECK(response); |
| 1436 response_.ssl_info = response->ssl_info; | 1463 response_.ssl_info = response->ssl_info; |
| 1437 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { | 1464 } else if (result == ERR_SSL_CLIENT_AUTH_CERT_NEEDED) { |
| 1438 DCHECK(response); | 1465 DCHECK(response); |
| 1439 response_.cert_request_info = response->cert_request_info; | 1466 response_.cert_request_info = response->cert_request_info; |
| 1440 } else if (response_.was_cached) { | 1467 } else if (response_.was_cached) { |
| 1441 DoneWritingToEntry(true); | 1468 DoneWritingToEntry(true); |
| 1442 } | 1469 } |
| 1443 | 1470 |
| 1444 TransitionToState(STATE_NONE); | 1471 TransitionToState(STATE_FINISH_HEADERS); |
| 1445 return result; | 1472 return result; |
| 1446 } | 1473 } |
| 1447 | 1474 |
| 1448 // We received the response headers and there is no error. | 1475 // We received the response headers and there is no error. |
| 1449 int HttpCache::Transaction::DoSuccessfulSendRequest() { | 1476 int HttpCache::Transaction::DoSuccessfulSendRequest() { |
| 1450 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); | 1477 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); |
| 1451 DCHECK(!new_response_); | 1478 DCHECK(!new_response_); |
| 1452 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); | 1479 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); |
| 1453 | 1480 |
| 1454 if (new_response->headers->response_code() == 401 || | 1481 if (new_response->headers->response_code() == 401 || |
| 1455 new_response->headers->response_code() == 407) { | 1482 new_response->headers->response_code() == 407) { |
| 1456 SetAuthResponse(*new_response); | 1483 SetAuthResponse(*new_response); |
| 1457 if (!reading_) { | 1484 if (!reading_) { |
| 1458 TransitionToState(STATE_NONE); | 1485 TransitionToState(STATE_FINISH_HEADERS); |
| 1459 return OK; | 1486 return OK; |
| 1460 } | 1487 } |
| 1461 | 1488 |
| 1462 // We initiated a second request the caller doesn't know about. We should be | 1489 // 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 | 1490 // able to authenticate this request because we should have authenticated |
| 1464 // this URL moments ago. | 1491 // this URL moments ago. |
| 1465 if (IsReadyToRestartForAuth()) { | 1492 if (IsReadyToRestartForAuth()) { |
| 1466 DCHECK(!response_.auth_challenge.get()); | 1493 DCHECK(!response_.auth_challenge.get()); |
| 1467 TransitionToState(STATE_SEND_REQUEST_COMPLETE); | 1494 TransitionToState(STATE_SEND_REQUEST_COMPLETE); |
| 1468 // In theory we should check to see if there are new cookies, but there | 1495 // In theory we should check to see if there are new cookies, but there |
| 1469 // is no way to do that from here. | 1496 // is no way to do that from here. |
| 1470 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_); | 1497 return network_trans_->RestartWithAuth(AuthCredentials(), io_callback_); |
| 1471 } | 1498 } |
| 1472 | 1499 |
| 1473 // We have to perform cleanup at this point so that at least the next | 1500 // 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 | 1501 // 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 | 1502 // 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 | 1503 // fail again, and potentially loop. This can happen if the credentials |
| 1477 // expire while chrome is suspended. | 1504 // expire while chrome is suspended. |
| 1478 if (entry_) | 1505 if (entry_) |
| 1479 DoomPartialEntry(false); | 1506 DoomPartialEntry(false); |
| 1480 mode_ = NONE; | 1507 mode_ = NONE; |
| 1481 partial_.reset(); | 1508 partial_.reset(); |
| 1482 ResetNetworkTransaction(); | 1509 ResetNetworkTransaction(); |
| 1483 TransitionToState(STATE_NONE); | 1510 TransitionToState(STATE_FINISH_HEADERS); |
| 1484 return ERR_CACHE_AUTH_FAILURE_AFTER_READ; | 1511 return ERR_CACHE_AUTH_FAILURE_AFTER_READ; |
| 1485 } | 1512 } |
| 1486 | 1513 |
| 1487 new_response_ = new_response; | 1514 new_response_ = new_response; |
| 1488 if (!ValidatePartialResponse() && !auth_response_.headers.get()) { | 1515 if (!ValidatePartialResponse() && !auth_response_.headers.get()) { |
| 1489 // Something went wrong with this request and we have to restart it. | 1516 // 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 | 1517 // If we have an authentication response, we are exposed to weird things |
| 1491 // hapenning if the user cancels the authentication before we receive | 1518 // hapenning if the user cancels the authentication before we receive |
| 1492 // the new response. | 1519 // the new response. |
| 1493 net_log_.AddEvent(NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST); | 1520 net_log_.AddEvent(NetLogEventType::HTTP_CACHE_RE_SEND_PARTIAL_REQUEST); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 1511 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); | 1538 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); |
| 1512 } | 1539 } |
| 1513 | 1540 |
| 1514 // Invalidate any cached GET with a successful PUT or DELETE. | 1541 // Invalidate any cached GET with a successful PUT or DELETE. |
| 1515 if (mode_ == WRITE && | 1542 if (mode_ == WRITE && |
| 1516 (request_->method == "PUT" || request_->method == "DELETE")) { | 1543 (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) && |
| 1528 request_->method == "POST" && | 1555 request_->method == "POST" && |
| 1529 NonErrorResponse(new_response->headers->response_code())) { | 1556 NonErrorResponse(new_response->headers->response_code())) { |
| 1530 cache_->DoomMainEntryForUrl(request_->url); | 1557 cache_->DoomMainEntryForUrl(request_->url); |
| 1531 } | 1558 } |
| 1532 | 1559 |
| 1533 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); | 1560 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); |
| 1534 | 1561 |
| 1535 if (new_response_->headers->response_code() == 416 && | 1562 if (new_response_->headers->response_code() == 416 && |
| 1536 (request_->method == "GET" || request_->method == "POST")) { | 1563 (request_->method == "GET" || request_->method == "POST")) { |
| 1537 // If there is an active entry it may be destroyed with this transaction. | 1564 // If there is an active entry it may be destroyed with this transaction. |
| 1538 SetResponse(*new_response_); | 1565 SetResponse(*new_response_); |
| 1539 TransitionToState(STATE_NONE); | 1566 TransitionToState(STATE_FINISH_HEADERS); |
| 1540 return OK; | 1567 return OK; |
| 1541 } | 1568 } |
| 1542 | 1569 |
| 1543 // Are we expecting a response to a conditional query? | 1570 // Are we expecting a response to a conditional query? |
| 1544 if (mode_ == READ_WRITE || mode_ == UPDATE) { | 1571 if (mode_ == READ_WRITE || mode_ == UPDATE) { |
| 1545 if (new_response->headers->response_code() == 304 || handling_206_) { | 1572 if (new_response->headers->response_code() == 304 || handling_206_) { |
| 1546 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); | 1573 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); |
| 1547 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); | 1574 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); |
| 1548 return OK; | 1575 return OK; |
| 1549 } | 1576 } |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1617 DCHECK(!handling_206_); | 1644 DCHECK(!handling_206_); |
| 1618 // We got a "not modified" response and already updated the corresponding | 1645 // We got a "not modified" response and already updated the corresponding |
| 1619 // cache entry above. | 1646 // cache entry above. |
| 1620 // | 1647 // |
| 1621 // By closing the cached entry now, we make sure that the 304 rather than | 1648 // 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. | 1649 // the cached 200 response, is what will be returned to the user. |
| 1623 DoneWritingToEntry(true); | 1650 DoneWritingToEntry(true); |
| 1624 } else if (entry_ && !handling_206_) { | 1651 } else if (entry_ && !handling_206_) { |
| 1625 DCHECK_EQ(READ_WRITE, mode_); | 1652 DCHECK_EQ(READ_WRITE, mode_); |
| 1626 if (!partial_ || partial_->IsLastRange()) { | 1653 if (!partial_ || partial_->IsLastRange()) { |
| 1627 cache_->ConvertWriterToReader(entry_); | |
| 1628 mode_ = READ; | 1654 mode_ = READ; |
| 1629 } | 1655 } |
| 1630 // We no longer need the network transaction, so destroy it. | 1656 // We no longer need the network transaction, so destroy it. |
| 1631 ResetNetworkTransaction(); | 1657 ResetNetworkTransaction(); |
| 1632 } else if (entry_ && handling_206_ && truncated_ && | 1658 } else if (entry_ && handling_206_ && truncated_ && |
| 1633 partial_->initial_validation()) { | 1659 partial_->initial_validation()) { |
| 1634 // We just finished the validation of a truncated entry, and the server | 1660 // 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 | 1661 // is willing to resume the operation. Now we go back and start serving |
| 1636 // the first part to the user. | 1662 // the first part to the user. |
| 1637 ResetNetworkTransaction(); | 1663 ResetNetworkTransaction(); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 1655 if (handling_206_ && partial_) | 1681 if (handling_206_ && partial_) |
| 1656 partial_->FixContentLength(new_response_->headers.get()); | 1682 partial_->FixContentLength(new_response_->headers.get()); |
| 1657 | 1683 |
| 1658 SetResponse(*new_response_); | 1684 SetResponse(*new_response_); |
| 1659 | 1685 |
| 1660 if (request_->method == "HEAD") { | 1686 if (request_->method == "HEAD") { |
| 1661 // This response is replacing the cached one. | 1687 // This response is replacing the cached one. |
| 1662 DoneWritingToEntry(false); | 1688 DoneWritingToEntry(false); |
| 1663 mode_ = NONE; | 1689 mode_ = NONE; |
| 1664 new_response_ = NULL; | 1690 new_response_ = NULL; |
| 1665 TransitionToState(STATE_NONE); | 1691 TransitionToState(STATE_FINISH_HEADERS); |
| 1666 return OK; | 1692 return OK; |
| 1667 } | 1693 } |
| 1668 | 1694 |
| 1669 if (handling_206_ && !CanResume(false)) { | 1695 if (handling_206_ && !CanResume(false)) { |
| 1670 // There is no point in storing this resource because it will never be used. | 1696 // 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. | 1697 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries. |
| 1672 DoneWritingToEntry(false); | 1698 DoneWritingToEntry(false); |
| 1673 if (partial_) | 1699 if (partial_) |
| 1674 partial_->FixResponseHeaders(response_.headers.get(), true); | 1700 partial_->FixResponseHeaders(response_.headers.get(), true); |
| 1675 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); | 1701 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); |
| 1676 return OK; | 1702 return OK; |
| 1677 } | 1703 } |
| 1678 | 1704 |
| 1679 TransitionToState(STATE_CACHE_WRITE_RESPONSE); | 1705 TransitionToState(STATE_CACHE_WRITE_RESPONSE); |
| 1680 return OK; | 1706 return OK; |
| 1681 } | 1707 } |
| 1682 | 1708 |
| 1683 int HttpCache::Transaction::DoCacheWriteResponse() { | 1709 int HttpCache::Transaction::DoCacheWriteResponse() { |
| 1684 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponse"); | 1710 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponse"); |
| 1685 TransitionToState(STATE_CACHE_WRITE_RESPONSE_COMPLETE); | 1711 TransitionToState(STATE_CACHE_WRITE_RESPONSE_COMPLETE); |
| 1712 | |
| 1713 // Invalidate any current entry with a successful response if this transaction | |
| 1714 // cannot write to this entry. This transaction then continues to read from | |
| 1715 // the network without writing to the backend. | |
| 1716 if (entry_ && response_.headers && | |
| 1717 !cache_->CanTransactionWriteResponseHeaders( | |
| 1718 entry_, this, response_.headers->response_code())) { | |
| 1719 cache_->DoneWritingToEntry(entry_, false, this); | |
| 1720 entry_ = nullptr; | |
| 1721 mode_ = NONE; | |
| 1722 return OK; | |
| 1723 } | |
| 1724 | |
| 1686 return WriteResponseInfoToEntry(truncated_); | 1725 return WriteResponseInfoToEntry(truncated_); |
| 1687 } | 1726 } |
| 1688 | 1727 |
| 1689 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) { | 1728 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) { |
| 1690 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponseComplete"); | 1729 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheWriteResponseComplete"); |
| 1691 TransitionToState(STATE_TRUNCATE_CACHED_DATA); | 1730 TransitionToState(STATE_TRUNCATE_CACHED_DATA); |
| 1692 return OnWriteResponseInfoToEntryComplete(result); | 1731 return OnWriteResponseInfoToEntryComplete(result); |
| 1693 } | 1732 } |
| 1694 | 1733 |
| 1695 int HttpCache::Transaction::DoTruncateCachedData() { | 1734 int HttpCache::Transaction::DoTruncateCachedData() { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1737 } | 1776 } |
| 1738 | 1777 |
| 1739 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); | 1778 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); |
| 1740 return OK; | 1779 return OK; |
| 1741 } | 1780 } |
| 1742 | 1781 |
| 1743 int HttpCache::Transaction::DoPartialHeadersReceived() { | 1782 int HttpCache::Transaction::DoPartialHeadersReceived() { |
| 1744 new_response_ = NULL; | 1783 new_response_ = NULL; |
| 1745 | 1784 |
| 1746 if (!partial_) { | 1785 if (!partial_) { |
| 1747 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) | 1786 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) { |
| 1748 TransitionToState(STATE_CACHE_READ_METADATA); | 1787 TransitionToState(STATE_CACHE_READ_METADATA); |
| 1749 else | 1788 } else { |
| 1750 TransitionToState(STATE_NONE); | 1789 DCHECK(!reading_); |
| 1790 TransitionToState(STATE_FINISH_HEADERS); | |
| 1791 } | |
| 1751 return OK; | 1792 return OK; |
| 1752 } | 1793 } |
| 1753 | 1794 |
| 1754 if (reading_) { | 1795 if (reading_) { |
| 1755 if (network_trans_.get()) { | 1796 if (network_trans_.get()) { |
| 1756 TransitionToState(STATE_NETWORK_READ); | 1797 TransitionToState(STATE_NETWORK_READ); |
| 1757 } else { | 1798 } else { |
| 1758 TransitionToState(STATE_CACHE_READ_DATA); | 1799 TransitionToState(STATE_CACHE_READ_DATA); |
| 1759 } | 1800 } |
| 1760 } else if (mode_ != NONE) { | 1801 } else if (mode_ != NONE) { |
| 1761 // We are about to return the headers for a byte-range request to the user, | 1802 // We are about to return the headers for a byte-range request to the user, |
| 1762 // so let's fix them. | 1803 // so let's fix them. |
| 1763 partial_->FixResponseHeaders(response_.headers.get(), true); | 1804 partial_->FixResponseHeaders(response_.headers.get(), true); |
| 1764 TransitionToState(STATE_NONE); | 1805 TransitionToState(STATE_FINISH_HEADERS); |
| 1765 } else { | 1806 } else { |
| 1766 TransitionToState(STATE_NONE); | 1807 TransitionToState(STATE_FINISH_HEADERS); |
| 1767 } | 1808 } |
| 1768 return OK; | 1809 return OK; |
| 1769 } | 1810 } |
| 1770 | 1811 |
| 1812 int HttpCache::Transaction::DoHeadersPhaseCannotProceed() { | |
| 1813 // If its the Start state machine and it cannot proceed due to a cache | |
| 1814 // failure, restart this transaction. | |
| 1815 DCHECK(!reading_); | |
| 1816 TransitionToState(STATE_INIT_ENTRY); | |
| 1817 cache_entry_status_ = CacheEntryStatus::ENTRY_UNDEFINED; | |
| 1818 entry_ = nullptr; | |
| 1819 mode_ = original_mode_; | |
| 1820 if (network_trans_) | |
| 1821 network_trans_.reset(); | |
| 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 there is no response body to be written or read, it does not need to | |
| 1842 // wait. | |
| 1843 if (request_->method == "HEAD") | |
| 1844 return OK; | |
| 1845 | |
| 1846 // If the transaction needs to wait because another transaction is still | |
| 1847 // writing the response body, it will return ERR_IO_PENDING now and the | |
| 1848 // io_callback_ will be invoked when the wait is done. | |
| 1849 return cache_->DoneWithResponseHeaders(entry_, this); | |
| 1850 } | |
| 1851 | |
| 1852 int HttpCache::Transaction::DoFinishHeadersComplete(int rv) { | |
| 1853 if (rv == ERR_CACHE_RACE) { | |
| 1854 TransitionToState(STATE_HEADERS_PHASE_CANNOT_PROCEED); | |
| 1855 return OK; | |
| 1856 } | |
| 1857 | |
| 1858 TransitionToState(STATE_NONE); | |
| 1859 return rv; | |
| 1860 } | |
| 1861 | |
| 1771 int HttpCache::Transaction::DoCacheReadMetadata() { | 1862 int HttpCache::Transaction::DoCacheReadMetadata() { |
| 1772 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); | 1863 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); |
| 1773 DCHECK(entry_); | 1864 DCHECK(entry_); |
| 1774 DCHECK(!response_.metadata.get()); | 1865 DCHECK(!response_.metadata.get()); |
| 1775 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); | 1866 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); |
| 1776 | 1867 |
| 1777 response_.metadata = | 1868 response_.metadata = |
| 1778 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); | 1869 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); |
| 1779 | 1870 |
| 1780 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); | 1871 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); |
| 1781 return entry_->disk_entry->ReadData(kMetadataIndex, 0, | 1872 return entry_->disk_entry->ReadData(kMetadataIndex, 0, |
| 1782 response_.metadata.get(), | 1873 response_.metadata.get(), |
| 1783 response_.metadata->size(), | 1874 response_.metadata->size(), |
| 1784 io_callback_); | 1875 io_callback_); |
| 1785 } | 1876 } |
| 1786 | 1877 |
| 1787 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { | 1878 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { |
| 1788 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); | 1879 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); |
| 1789 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, | 1880 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, |
| 1790 result); | 1881 result); |
| 1791 if (result != response_.metadata->size()) | 1882 if (result != response_.metadata->size()) |
| 1792 return OnCacheReadError(result, false); | 1883 return OnCacheReadError(result, false); |
| 1793 TransitionToState(STATE_NONE); | 1884 |
| 1885 TransitionToState(STATE_FINISH_HEADERS); | |
| 1794 return OK; | 1886 return OK; |
| 1795 } | 1887 } |
| 1796 | 1888 |
| 1797 int HttpCache::Transaction::DoNetworkRead() { | 1889 int HttpCache::Transaction::DoNetworkRead() { |
| 1798 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); | 1890 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); |
| 1799 TransitionToState(STATE_NETWORK_READ_COMPLETE); | 1891 TransitionToState(STATE_NETWORK_READ_COMPLETE); |
| 1800 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); | 1892 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); |
| 1801 } | 1893 } |
| 1802 | 1894 |
| 1803 int HttpCache::Transaction::DoNetworkReadComplete(int result) { | 1895 int HttpCache::Transaction::DoNetworkReadComplete(int result) { |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2080 return false; | 2172 return false; |
| 2081 | 2173 |
| 2082 return true; | 2174 return true; |
| 2083 } | 2175 } |
| 2084 | 2176 |
| 2085 int HttpCache::Transaction::BeginCacheRead() { | 2177 int HttpCache::Transaction::BeginCacheRead() { |
| 2086 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges. | 2178 // We don't support any combination of LOAD_ONLY_FROM_CACHE and byte ranges. |
| 2087 // TODO(jkarlin): Either handle this case or DCHECK. | 2179 // TODO(jkarlin): Either handle this case or DCHECK. |
| 2088 if (response_.headers->response_code() == 206 || partial_) { | 2180 if (response_.headers->response_code() == 206 || partial_) { |
| 2089 NOTREACHED(); | 2181 NOTREACHED(); |
| 2090 TransitionToState(STATE_NONE); | 2182 TransitionToState(STATE_FINISH_HEADERS); |
| 2091 return ERR_CACHE_MISS; | 2183 return ERR_CACHE_MISS; |
| 2092 } | 2184 } |
| 2093 | 2185 |
| 2094 // We don't have the whole resource. | 2186 // We don't have the whole resource. |
| 2095 if (truncated_) { | 2187 if (truncated_) { |
| 2096 TransitionToState(STATE_NONE); | 2188 TransitionToState(STATE_FINISH_HEADERS); |
| 2097 return ERR_CACHE_MISS; | 2189 return ERR_CACHE_MISS; |
| 2098 } | 2190 } |
| 2099 | 2191 |
| 2100 if (RequiresValidation()) { | 2192 if (RequiresValidation()) { |
| 2101 TransitionToState(STATE_NONE); | 2193 TransitionToState(STATE_FINISH_HEADERS); |
| 2102 return ERR_CACHE_MISS; | 2194 return ERR_CACHE_MISS; |
| 2103 } | 2195 } |
| 2104 | 2196 |
| 2105 if (request_->method == "HEAD") | 2197 if (request_->method == "HEAD") |
| 2106 FixHeadersForHead(); | 2198 FixHeadersForHead(); |
| 2107 | 2199 |
| 2108 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) | 2200 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) |
| 2109 TransitionToState(STATE_CACHE_READ_METADATA); | 2201 TransitionToState(STATE_CACHE_READ_METADATA); |
| 2110 else | 2202 else |
| 2111 TransitionToState(STATE_NONE); | 2203 TransitionToState(STATE_FINISH_HEADERS); |
| 2112 | 2204 |
| 2113 return OK; | 2205 return OK; |
| 2114 } | 2206 } |
| 2115 | 2207 |
| 2116 int HttpCache::Transaction::BeginCacheValidation() { | 2208 int HttpCache::Transaction::BeginCacheValidation() { |
| 2117 DCHECK_EQ(mode_, READ_WRITE); | 2209 DCHECK_EQ(mode_, READ_WRITE); |
| 2118 | 2210 |
| 2119 bool skip_validation = !RequiresValidation(); | 2211 bool skip_validation = !RequiresValidation(); |
| 2120 | 2212 |
| 2121 if (request_->method == "HEAD" && | 2213 if (request_->method == "HEAD" && |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2561 if (partial_) { | 2653 if (partial_) { |
| 2562 if (truncated_ || is_sparse_ || !invalid_range_) { | 2654 if (truncated_ || is_sparse_ || !invalid_range_) { |
| 2563 // We are going to return the saved response headers to the caller, so | 2655 // We are going to return the saved response headers to the caller, so |
| 2564 // we may need to adjust them first. | 2656 // we may need to adjust them first. |
| 2565 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); | 2657 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); |
| 2566 return OK; | 2658 return OK; |
| 2567 } else { | 2659 } else { |
| 2568 partial_.reset(); | 2660 partial_.reset(); |
| 2569 } | 2661 } |
| 2570 } | 2662 } |
| 2571 cache_->ConvertWriterToReader(entry_); | 2663 |
| 2572 mode_ = READ; | 2664 mode_ = READ; |
| 2573 | 2665 |
| 2574 if (request_->method == "HEAD") | 2666 if (request_->method == "HEAD") |
| 2575 FixHeadersForHead(); | 2667 FixHeadersForHead(); |
| 2576 | 2668 |
| 2577 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) | 2669 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) |
| 2578 TransitionToState(STATE_CACHE_READ_METADATA); | 2670 TransitionToState(STATE_CACHE_READ_METADATA); |
| 2579 else | 2671 else |
| 2580 TransitionToState(STATE_NONE); | 2672 TransitionToState(STATE_FINISH_HEADERS); |
| 2581 return OK; | 2673 return OK; |
| 2582 } | 2674 } |
| 2583 | 2675 |
| 2584 int HttpCache::Transaction::WriteToEntry(int index, int offset, | 2676 int HttpCache::Transaction::WriteToEntry(int index, int offset, |
| 2585 IOBuffer* data, int data_len, | 2677 IOBuffer* data, int data_len, |
| 2586 const CompletionCallback& callback) { | 2678 const CompletionCallback& callback) { |
| 2587 if (!entry_) | 2679 if (!entry_) |
| 2588 return data_len; | 2680 return data_len; |
| 2589 | 2681 |
| 2590 int rv = 0; | 2682 int rv = 0; |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2649 } | 2741 } |
| 2650 return OK; | 2742 return OK; |
| 2651 } | 2743 } |
| 2652 | 2744 |
| 2653 void HttpCache::Transaction::DoneWritingToEntry(bool success) { | 2745 void HttpCache::Transaction::DoneWritingToEntry(bool success) { |
| 2654 if (!entry_) | 2746 if (!entry_) |
| 2655 return; | 2747 return; |
| 2656 | 2748 |
| 2657 RecordHistograms(); | 2749 RecordHistograms(); |
| 2658 | 2750 |
| 2659 cache_->DoneWritingToEntry(entry_, success); | 2751 cache_->DoneWritingToEntry(entry_, success, this); |
| 2660 entry_ = NULL; | 2752 entry_ = NULL; |
| 2661 mode_ = NONE; // switch to 'pass through' mode | 2753 mode_ = NONE; // switch to 'pass through' mode |
| 2662 } | 2754 } |
| 2663 | 2755 |
| 2664 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { | 2756 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { |
| 2665 DLOG(ERROR) << "ReadData failed: " << result; | 2757 DLOG(ERROR) << "ReadData failed: " << result; |
| 2666 const int result_for_histogram = std::max(0, -result); | 2758 const int result_for_histogram = std::max(0, -result); |
| 2667 if (restart) { | 2759 if (restart) { |
| 2668 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", | 2760 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", |
| 2669 result_for_histogram); | 2761 result_for_histogram); |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2860 bool validation_request = | 2952 bool validation_request = |
| 2861 cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED || | 2953 cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED || |
| 2862 cache_entry_status_ == CacheEntryStatus::ENTRY_UPDATED; | 2954 cache_entry_status_ == CacheEntryStatus::ENTRY_UPDATED; |
| 2863 | 2955 |
| 2864 bool stale_request = | 2956 bool stale_request = |
| 2865 validation_cause_ == VALIDATION_CAUSE_STALE && | 2957 validation_cause_ == VALIDATION_CAUSE_STALE && |
| 2866 (validation_request || | 2958 (validation_request || |
| 2867 cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE); | 2959 cache_entry_status_ == CacheEntryStatus::ENTRY_CANT_CONDITIONALIZE); |
| 2868 int64_t freshness_periods_since_last_used = 0; | 2960 int64_t freshness_periods_since_last_used = 0; |
| 2869 | 2961 |
| 2870 if (stale_request) { | 2962 if (stale_request && !open_entry_last_used_.is_null()) { |
| 2963 // Note that we are not able to capture those transactions' histograms which | |
| 2964 // when added to entry the response was being written by another transaction | |
| 2965 // because getting the last used timestamp might lead to a data race in that | |
| 2966 // case. | |
|
jkarlin
2017/04/20 15:05:31
Can you add a crbug link to the comment?
shivanisha
2017/04/21 23:06:01
done
| |
| 2967 | |
| 2871 // For stale entries, record how many freshness periods have elapsed since | 2968 // For stale entries, record how many freshness periods have elapsed since |
| 2872 // the entry was last used. | 2969 // the entry was last used. |
| 2873 DCHECK(!open_entry_last_used_.is_null()); | |
| 2874 DCHECK(!stale_entry_freshness_.is_zero()); | 2970 DCHECK(!stale_entry_freshness_.is_zero()); |
| 2875 base::TimeDelta time_since_use = base::Time::Now() - open_entry_last_used_; | 2971 base::TimeDelta time_since_use = base::Time::Now() - open_entry_last_used_; |
| 2876 freshness_periods_since_last_used = | 2972 freshness_periods_since_last_used = |
| 2877 (time_since_use * 1000) / stale_entry_freshness_; | 2973 (time_since_use * 1000) / stale_entry_freshness_; |
| 2878 | 2974 |
| 2879 if (validation_request) { | 2975 if (validation_request) { |
| 2880 int64_t age_in_freshness_periods = | 2976 int64_t age_in_freshness_periods = |
| 2881 (stale_entry_age_ * 100) / stale_entry_freshness_; | 2977 (stale_entry_age_ * 100) / stale_entry_freshness_; |
| 2882 if (cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED) { | 2978 if (cache_entry_status_ == CacheEntryStatus::ENTRY_VALIDATED) { |
| 2883 UMA_HISTOGRAM_COUNTS("HttpCache.StaleEntry.Validated.Age", | 2979 UMA_HISTOGRAM_COUNTS("HttpCache.StaleEntry.Validated.Age", |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3017 } | 3113 } |
| 3018 | 3114 |
| 3019 void HttpCache::Transaction::TransitionToState(State state) { | 3115 void HttpCache::Transaction::TransitionToState(State state) { |
| 3020 // Ensure that the state is only set once per Do* state. | 3116 // Ensure that the state is only set once per Do* state. |
| 3021 DCHECK(in_do_loop_); | 3117 DCHECK(in_do_loop_); |
| 3022 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; | 3118 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; |
| 3023 next_state_ = state; | 3119 next_state_ = state; |
| 3024 } | 3120 } |
| 3025 | 3121 |
| 3026 } // namespace net | 3122 } // namespace net |
| OLD | NEW |