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 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
162 | 162 |
163 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache) | 163 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache) |
164 : next_state_(STATE_NONE), | 164 : next_state_(STATE_NONE), |
165 request_(NULL), | 165 request_(NULL), |
166 priority_(priority), | 166 priority_(priority), |
167 cache_(cache->GetWeakPtr()), | 167 cache_(cache->GetWeakPtr()), |
168 entry_(NULL), | 168 entry_(NULL), |
169 new_entry_(NULL), | 169 new_entry_(NULL), |
170 new_response_(NULL), | 170 new_response_(NULL), |
171 mode_(NONE), | 171 mode_(NONE), |
172 original_mode_(NONE), | |
172 reading_(false), | 173 reading_(false), |
173 invalid_range_(false), | 174 invalid_range_(false), |
174 truncated_(false), | 175 truncated_(false), |
175 is_sparse_(false), | 176 is_sparse_(false), |
176 range_requested_(false), | 177 range_requested_(false), |
177 handling_206_(false), | 178 handling_206_(false), |
178 cache_pending_(false), | 179 cache_pending_(false), |
179 done_reading_(false), | 180 done_reading_(false), |
180 vary_mismatch_(false), | 181 vary_mismatch_(false), |
181 couldnt_conditionalize_request_(false), | 182 couldnt_conditionalize_request_(false), |
182 bypass_lock_for_test_(false), | 183 bypass_lock_for_test_(false), |
183 fail_conditionalization_for_test_(false), | 184 fail_conditionalization_for_test_(false), |
185 validating_cannot_proceed_(false), | |
184 io_buf_len_(0), | 186 io_buf_len_(0), |
185 read_offset_(0), | 187 read_offset_(0), |
186 effective_load_flags_(0), | 188 effective_load_flags_(0), |
187 write_len_(0), | 189 write_len_(0), |
188 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), | 190 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), |
189 validation_cause_(VALIDATION_CAUSE_UNDEFINED), | 191 validation_cause_(VALIDATION_CAUSE_UNDEFINED), |
190 total_received_bytes_(0), | 192 total_received_bytes_(0), |
191 total_sent_bytes_(0), | 193 total_sent_bytes_(0), |
192 websocket_handshake_stream_base_create_helper_(NULL), | 194 websocket_handshake_stream_base_create_helper_(NULL), |
193 weak_factory_(this) { | 195 weak_factory_(this) { |
(...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 ConnectionAttempts* out) const { | 572 ConnectionAttempts* out) const { |
571 ConnectionAttempts new_connection_attempts; | 573 ConnectionAttempts new_connection_attempts; |
572 if (network_trans_) | 574 if (network_trans_) |
573 network_trans_->GetConnectionAttempts(&new_connection_attempts); | 575 network_trans_->GetConnectionAttempts(&new_connection_attempts); |
574 | 576 |
575 out->swap(new_connection_attempts); | 577 out->swap(new_connection_attempts); |
576 out->insert(out->begin(), old_connection_attempts_.begin(), | 578 out->insert(out->begin(), old_connection_attempts_.begin(), |
577 old_connection_attempts_.end()); | 579 old_connection_attempts_.end()); |
578 } | 580 } |
579 | 581 |
582 void HttpCache::Transaction::SetValidatingCannotProceed() { | |
583 validating_cannot_proceed_ = true; | |
584 entry_ = nullptr; | |
585 } | |
586 | |
580 size_t HttpCache::Transaction::EstimateMemoryUsage() const { | 587 size_t HttpCache::Transaction::EstimateMemoryUsage() const { |
581 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. | 588 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. |
582 return 0; | 589 return 0; |
583 } | 590 } |
584 | 591 |
585 //----------------------------------------------------------------------------- | 592 //----------------------------------------------------------------------------- |
586 | 593 |
587 // A few common patterns: (Foo* means Foo -> FooComplete) | 594 // A few common patterns: (Foo* means Foo -> FooComplete) |
588 // | 595 // |
589 // 1. Not-cached entry: | 596 // 1. Not-cached entry: |
590 // Start(): | 597 // Start(): |
591 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> | 598 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> |
592 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> | 599 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> |
593 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> | 600 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> |
594 // PartialHeadersReceived | 601 // PartialHeadersReceived |
595 // | 602 // |
596 // Read(): | 603 // Read(): |
597 // NetworkRead* -> CacheWriteData* | 604 // NetworkRead* -> CacheWriteData* |
598 // | 605 // |
599 // 2. Cached entry, no validation: | 606 // 2. Cached entry, no validation: |
600 // Start(): | 607 // Start(): |
601 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 608 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
602 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> | 609 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
603 // BeginCacheValidation() -> SetupEntryForRead() | 610 // BeginCacheValidation() -> SetupEntryForRead() -> WaitBeforeRead* |
604 // | 611 // |
605 // Read(): | 612 // Read(): |
606 // CacheReadData* | 613 // CacheReadData* |
607 // | 614 // |
608 // 3. Cached entry, validation (304): | 615 // 3. Cached entry, validation (304): |
609 // Start(): | 616 // Start(): |
610 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 617 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
611 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> | 618 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
612 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> | 619 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
613 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> | 620 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> |
614 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> | 621 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> |
615 // PartialHeadersReceived | 622 // PartialHeadersReceived -> WaitBeforeRead* |
616 // | 623 // |
617 // Read(): | 624 // Read(): |
618 // CacheReadData* | 625 // CacheReadData* |
619 // | 626 // |
620 // 4. Cached entry, validation and replace (200): | 627 // 4. Cached entry, validation and replace (200): |
621 // Start(): | 628 // Start(): |
622 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* | 629 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* |
623 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> | 630 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> |
624 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> | 631 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> |
625 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> | 632 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
705 // BeginPartialCacheValidation() -> BeginCacheValidation() -> | 712 // BeginPartialCacheValidation() -> BeginCacheValidation() -> |
706 // SetupEntryForRead() | 713 // SetupEntryForRead() |
707 // | 714 // |
708 // Read(): | 715 // Read(): |
709 // CacheReadData* | 716 // CacheReadData* |
710 // | 717 // |
711 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: | 718 // 14. Cached entry more than 5 minutes old, unused_since_prefetch is true: |
712 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between | 719 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between |
713 // CacheReadResponse* and CacheDispatchValidation. | 720 // CacheReadResponse* and CacheDispatchValidation. |
714 int HttpCache::Transaction::DoLoop(int result) { | 721 int HttpCache::Transaction::DoLoop(int result) { |
722 // If its the start state machine and it cannot proceed due to failure by | |
723 // another transaction in writing the response, restart this transaction. | |
724 if (!reading_ && validating_cannot_proceed_) { | |
725 validating_cannot_proceed_ = false; | |
726 RestartAfterValidationStarted(); | |
727 } | |
728 | |
729 int rv = DoLoopImpl(result); | |
730 | |
731 // If its the end of the Start() state machine, the transaction might have to | |
732 // wait before it can read from the cache if there is another transaction | |
733 // writing to the cache currently. This is being done to enable parallelizing | |
734 // the validation phase of a transaction while another transaction is still | |
735 // writing the response to the cache. | |
736 // Checking reading_ will make sure that the control is still in the start | |
737 // state machine and also that we do not enter the wait state for | |
738 // partial requests that have already started reading and re-entered the | |
739 // start() state machine. | |
740 if (!reading_ && rv == OK) { | |
741 next_state_ = STATE_WAIT_BEFORE_READ; | |
742 rv = DoLoopImpl(rv); | |
743 } | |
744 | |
745 if (rv != ERR_IO_PENDING && !callback_.is_null()) { | |
746 read_buf_ = nullptr; // Release the buffer before invoking the callback. | |
747 base::ResetAndReturn(&callback_).Run(rv); | |
748 } | |
749 | |
750 return rv; | |
751 } | |
752 | |
753 int HttpCache::Transaction::DoLoopImpl(int result) { | |
715 DCHECK(next_state_ != STATE_NONE); | 754 DCHECK(next_state_ != STATE_NONE); |
716 | 755 |
717 int rv = result; | 756 int rv = result; |
718 do { | 757 do { |
719 State state = next_state_; | 758 State state = next_state_; |
720 next_state_ = STATE_NONE; | 759 next_state_ = STATE_NONE; |
721 switch (state) { | 760 switch (state) { |
722 case STATE_GET_BACKEND: | 761 case STATE_GET_BACKEND: |
723 DCHECK_EQ(OK, rv); | 762 DCHECK_EQ(OK, rv); |
724 rv = DoGetBackend(); | 763 rv = DoGetBackend(); |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
844 DCHECK_EQ(OK, rv); | 883 DCHECK_EQ(OK, rv); |
845 rv = DoPartialHeadersReceived(); | 884 rv = DoPartialHeadersReceived(); |
846 break; | 885 break; |
847 case STATE_CACHE_READ_METADATA: | 886 case STATE_CACHE_READ_METADATA: |
848 DCHECK_EQ(OK, rv); | 887 DCHECK_EQ(OK, rv); |
849 rv = DoCacheReadMetadata(); | 888 rv = DoCacheReadMetadata(); |
850 break; | 889 break; |
851 case STATE_CACHE_READ_METADATA_COMPLETE: | 890 case STATE_CACHE_READ_METADATA_COMPLETE: |
852 rv = DoCacheReadMetadataComplete(rv); | 891 rv = DoCacheReadMetadataComplete(rv); |
853 break; | 892 break; |
893 case STATE_WAIT_BEFORE_READ: | |
894 DCHECK_EQ(OK, rv); | |
895 rv = DoWaitBeforeRead(); | |
896 break; | |
897 case STATE_WAIT_BEFORE_READ_COMPLETE: | |
898 rv = DoWaitBeforeReadComplete(rv); | |
899 break; | |
854 case STATE_NETWORK_READ: | 900 case STATE_NETWORK_READ: |
855 DCHECK_EQ(OK, rv); | 901 DCHECK_EQ(OK, rv); |
856 rv = DoNetworkRead(); | 902 rv = DoNetworkRead(); |
857 break; | 903 break; |
858 case STATE_NETWORK_READ_COMPLETE: | 904 case STATE_NETWORK_READ_COMPLETE: |
859 rv = DoNetworkReadComplete(rv); | 905 rv = DoNetworkReadComplete(rv); |
860 break; | 906 break; |
861 case STATE_CACHE_READ_DATA: | 907 case STATE_CACHE_READ_DATA: |
862 DCHECK_EQ(OK, rv); | 908 DCHECK_EQ(OK, rv); |
863 rv = DoCacheReadData(); | 909 rv = DoCacheReadData(); |
(...skipping 14 matching lines...) Expand all Loading... | |
878 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE: | 924 case STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE: |
879 rv = DoCacheWriteTruncatedResponseComplete(rv); | 925 rv = DoCacheWriteTruncatedResponseComplete(rv); |
880 break; | 926 break; |
881 default: | 927 default: |
882 NOTREACHED() << "bad state"; | 928 NOTREACHED() << "bad state"; |
883 rv = ERR_FAILED; | 929 rv = ERR_FAILED; |
884 break; | 930 break; |
885 } | 931 } |
886 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 932 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
887 | 933 |
888 if (rv != ERR_IO_PENDING && !callback_.is_null()) { | |
889 read_buf_ = NULL; // Release the buffer before invoking the callback. | |
890 base::ResetAndReturn(&callback_).Run(rv); | |
891 } | |
892 | |
893 return rv; | 934 return rv; |
894 } | 935 } |
895 | 936 |
896 int HttpCache::Transaction::DoGetBackend() { | 937 int HttpCache::Transaction::DoGetBackend() { |
897 cache_pending_ = true; | 938 cache_pending_ = true; |
898 next_state_ = STATE_GET_BACKEND_COMPLETE; | 939 next_state_ = STATE_GET_BACKEND_COMPLETE; |
899 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); | 940 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); |
900 return cache_->GetBackendForTransaction(this); | 941 return cache_->GetBackendForTransaction(this); |
901 } | 942 } |
902 | 943 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
958 partial_->RestoreHeaders(&custom_request_->extra_headers); | 999 partial_->RestoreHeaders(&custom_request_->extra_headers); |
959 partial_.reset(); | 1000 partial_.reset(); |
960 } | 1001 } |
961 next_state_ = STATE_SEND_REQUEST; | 1002 next_state_ = STATE_SEND_REQUEST; |
962 } else { | 1003 } else { |
963 next_state_ = STATE_INIT_ENTRY; | 1004 next_state_ = STATE_INIT_ENTRY; |
964 } | 1005 } |
965 | 1006 |
966 // This is only set if we have something to do with the response. | 1007 // This is only set if we have something to do with the response. |
967 range_requested_ = (partial_.get() != NULL); | 1008 range_requested_ = (partial_.get() != NULL); |
968 | 1009 original_mode_ = mode_; |
969 return OK; | 1010 return OK; |
970 } | 1011 } |
971 | 1012 |
972 int HttpCache::Transaction::DoInitEntry() { | 1013 int HttpCache::Transaction::DoInitEntry() { |
973 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); | 1014 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); |
974 DCHECK(!new_entry_); | 1015 DCHECK(!new_entry_); |
975 | 1016 |
976 if (!cache_.get()) | 1017 if (!cache_.get()) |
977 return ERR_UNEXPECTED; | 1018 return ERR_UNEXPECTED; |
978 | 1019 |
(...skipping 511 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1490 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); | 1531 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); |
1491 } | 1532 } |
1492 | 1533 |
1493 // Invalidate any cached GET with a successful PUT or DELETE. | 1534 // Invalidate any cached GET with a successful PUT or DELETE. |
1494 if (mode_ == WRITE && | 1535 if (mode_ == WRITE && |
1495 (request_->method == "PUT" || request_->method == "DELETE")) { | 1536 (request_->method == "PUT" || request_->method == "DELETE")) { |
1496 if (NonErrorResponse(new_response->headers->response_code())) { | 1537 if (NonErrorResponse(new_response->headers->response_code())) { |
1497 int ret = cache_->DoomEntry(cache_key_, NULL); | 1538 int ret = cache_->DoomEntry(cache_key_, NULL); |
1498 DCHECK_EQ(OK, ret); | 1539 DCHECK_EQ(OK, ret); |
1499 } | 1540 } |
1500 cache_->DoneWritingToEntry(entry_, true); | 1541 cache_->DoneWritingToEntry(entry_, true, this); |
1501 entry_ = NULL; | 1542 entry_ = NULL; |
1502 mode_ = NONE; | 1543 mode_ = NONE; |
1503 } | 1544 } |
1504 | 1545 |
1505 // Invalidate any cached GET with a successful POST. | 1546 // Invalidate any cached GET with a successful POST. |
1506 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && | 1547 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && |
1507 request_->method == "POST" && | 1548 request_->method == "POST" && |
1508 NonErrorResponse(new_response->headers->response_code())) { | 1549 NonErrorResponse(new_response->headers->response_code())) { |
1509 cache_->DoomMainEntryForUrl(request_->url); | 1550 cache_->DoomMainEntryForUrl(request_->url); |
1510 } | 1551 } |
(...skipping 12 matching lines...) Expand all Loading... | |
1523 if (new_response->headers->response_code() == 304 || handling_206_) { | 1564 if (new_response->headers->response_code() == 304 || handling_206_) { |
1524 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); | 1565 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); |
1525 next_state_ = STATE_UPDATE_CACHED_RESPONSE; | 1566 next_state_ = STATE_UPDATE_CACHED_RESPONSE; |
1526 return OK; | 1567 return OK; |
1527 } | 1568 } |
1528 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); | 1569 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); |
1529 mode_ = WRITE; | 1570 mode_ = WRITE; |
1530 } | 1571 } |
1531 | 1572 |
1532 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE; | 1573 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE; |
1574 | |
1575 if (!entry_) | |
1576 return OK; | |
1577 | |
1578 // Invalidate any current entry with a successful response if this transaction | |
1579 // cannot write to this entry. | |
1580 if (new_response->headers->response_code() != 304 && | |
1581 (entry_->writer || !entry_->readers.empty())) { | |
1582 DCHECK_EQ(entry_->headers_transaction, this); | |
1583 cache_->DoneResponseHeaders(entry_, this, false); | |
1584 entry_ = nullptr; | |
1585 mode_ = NONE; | |
1586 return OK; | |
1587 } | |
1588 | |
1533 return OK; | 1589 return OK; |
1534 } | 1590 } |
1535 | 1591 |
1536 // We received 304 or 206 and we want to update the cached response headers. | 1592 // We received 304 or 206 and we want to update the cached response headers. |
1537 int HttpCache::Transaction::DoUpdateCachedResponse() { | 1593 int HttpCache::Transaction::DoUpdateCachedResponse() { |
1538 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); | 1594 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); |
1539 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE; | 1595 next_state_ = STATE_UPDATE_CACHED_RESPONSE_COMPLETE; |
1540 int rv = OK; | 1596 int rv = OK; |
1541 // Update the cached response based on the headers and properties of | 1597 // Update the cached response based on the headers and properties of |
1542 // new_response_. | 1598 // new_response_. |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1592 DCHECK(!handling_206_); | 1648 DCHECK(!handling_206_); |
1593 // We got a "not modified" response and already updated the corresponding | 1649 // We got a "not modified" response and already updated the corresponding |
1594 // cache entry above. | 1650 // cache entry above. |
1595 // | 1651 // |
1596 // By closing the cached entry now, we make sure that the 304 rather than | 1652 // By closing the cached entry now, we make sure that the 304 rather than |
1597 // the cached 200 response, is what will be returned to the user. | 1653 // the cached 200 response, is what will be returned to the user. |
1598 DoneWritingToEntry(true); | 1654 DoneWritingToEntry(true); |
1599 } else if (entry_ && !handling_206_) { | 1655 } else if (entry_ && !handling_206_) { |
1600 DCHECK_EQ(READ_WRITE, mode_); | 1656 DCHECK_EQ(READ_WRITE, mode_); |
1601 if (!partial_ || partial_->IsLastRange()) { | 1657 if (!partial_ || partial_->IsLastRange()) { |
1602 cache_->ConvertWriterToReader(entry_); | |
1603 mode_ = READ; | 1658 mode_ = READ; |
1604 } | 1659 } |
1605 // We no longer need the network transaction, so destroy it. | 1660 // We no longer need the network transaction, so destroy it. |
1606 ResetNetworkTransaction(); | 1661 ResetNetworkTransaction(); |
1607 } else if (entry_ && handling_206_ && truncated_ && | 1662 } else if (entry_ && handling_206_ && truncated_ && |
1608 partial_->initial_validation()) { | 1663 partial_->initial_validation()) { |
1609 // We just finished the validation of a truncated entry, and the server | 1664 // We just finished the validation of a truncated entry, and the server |
1610 // is willing to resume the operation. Now we go back and start serving | 1665 // is willing to resume the operation. Now we go back and start serving |
1611 // the first part to the user. | 1666 // the first part to the user. |
1612 ResetNetworkTransaction(); | 1667 ResetNetworkTransaction(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1729 next_state_ = STATE_CACHE_READ_DATA; | 1784 next_state_ = STATE_CACHE_READ_DATA; |
1730 } | 1785 } |
1731 } else if (mode_ != NONE) { | 1786 } else if (mode_ != NONE) { |
1732 // We are about to return the headers for a byte-range request to the user, | 1787 // We are about to return the headers for a byte-range request to the user, |
1733 // so let's fix them. | 1788 // so let's fix them. |
1734 partial_->FixResponseHeaders(response_.headers.get(), true); | 1789 partial_->FixResponseHeaders(response_.headers.get(), true); |
1735 } | 1790 } |
1736 return OK; | 1791 return OK; |
1737 } | 1792 } |
1738 | 1793 |
1794 int HttpCache::Transaction::DoWaitBeforeRead() { | |
1795 if (!entry_) | |
1796 return OK; | |
1797 | |
1798 next_state_ = STATE_WAIT_BEFORE_READ_COMPLETE; | |
1799 | |
1800 // If |this| is the writer or there is no response body to be written or read, | |
1801 // it does not need to wait. | |
1802 if (entry_->writer == this || request_->method == "HEAD") | |
Randy Smith (Not in Mondays)
2017/03/17 18:13:06
I'm a bit concerned about races here. The transit
shivanisha
2017/03/20 18:14:49
Actually, after the state simplification since a t
| |
1803 return OK; | |
1804 | |
1805 // If |this| is in readers, then no need to wait. | |
Randy Smith (Not in Mondays)
2017/03/17 18:13:06
This comment doesn't seem to fit the following lin
shivanisha
2017/03/20 18:14:49
Expanded the comment to be more clear. The if cond
| |
1806 if (entry_->headers_transaction == this) | |
Randy Smith (Not in Mondays)
2017/03/17 18:13:05
When does this happen? Naively, I think of WAIT_B
shivanisha
2017/03/20 18:14:49
At this point a transaction should always be a hea
| |
1807 return cache_->DoneResponseHeaders(entry_, this, true); | |
1808 | |
1809 return OK; | |
1810 } | |
1811 | |
1812 int HttpCache::Transaction::DoWaitBeforeReadComplete(int rv) { | |
1813 if (rv == ERR_CACHE_RACE) { | |
1814 RestartAfterValidationStarted(); | |
1815 return OK; | |
1816 } | |
1817 | |
1818 if (rv != OK) | |
1819 return rv; | |
1820 | |
1821 // If successful then this must be either be the writer or a reader as the | |
1822 // response must have completely been written to the cache. | |
1823 if (entry_->writer == this) | |
1824 return OK; | |
1825 | |
1826 mode_ = READ; | |
1827 if (network_trans_) | |
1828 ResetNetworkTransaction(); | |
1829 return OK; | |
1830 } | |
1831 | |
1832 void HttpCache::Transaction::RestartAfterValidationStarted() { | |
shivanisha
2017/03/16 15:43:26
DCHECK(!reading_);
shivanisha
2017/03/20 18:14:49
done
| |
1833 next_state_ = STATE_INIT_ENTRY; | |
1834 cache_entry_status_ = CacheEntryStatus::ENTRY_UNDEFINED; | |
1835 entry_ = nullptr; | |
1836 mode_ = original_mode_; | |
1837 if (network_trans_) | |
1838 network_trans_.reset(); | |
1839 } | |
1840 | |
1739 int HttpCache::Transaction::DoCacheReadMetadata() { | 1841 int HttpCache::Transaction::DoCacheReadMetadata() { |
1740 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); | 1842 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); |
1741 DCHECK(entry_); | 1843 DCHECK(entry_); |
1742 DCHECK(!response_.metadata.get()); | 1844 DCHECK(!response_.metadata.get()); |
1743 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE; | 1845 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE; |
1744 | 1846 |
1745 response_.metadata = | 1847 response_.metadata = |
1746 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); | 1848 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); |
1747 | 1849 |
1748 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); | 1850 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); |
(...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2558 if (partial_) { | 2660 if (partial_) { |
2559 if (truncated_ || is_sparse_ || !invalid_range_) { | 2661 if (truncated_ || is_sparse_ || !invalid_range_) { |
2560 // We are going to return the saved response headers to the caller, so | 2662 // We are going to return the saved response headers to the caller, so |
2561 // we may need to adjust them first. | 2663 // we may need to adjust them first. |
2562 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; | 2664 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; |
2563 return OK; | 2665 return OK; |
2564 } else { | 2666 } else { |
2565 partial_.reset(); | 2667 partial_.reset(); |
2566 } | 2668 } |
2567 } | 2669 } |
2568 cache_->ConvertWriterToReader(entry_); | 2670 |
2671 // This might or might not be able to add it as an active reader based on | |
2672 // whether this is the active writer or not. If not, it will be added to a | |
2673 // wait queue in DoWaitBeforeRead. | |
2569 mode_ = READ; | 2674 mode_ = READ; |
2570 | 2675 |
2571 if (request_->method == "HEAD") | 2676 if (request_->method == "HEAD") |
2572 FixHeadersForHead(); | 2677 FixHeadersForHead(); |
2573 | 2678 |
2574 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) | 2679 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) |
2575 next_state_ = STATE_CACHE_READ_METADATA; | 2680 next_state_ = STATE_CACHE_READ_METADATA; |
2576 return OK; | 2681 return OK; |
2577 } | 2682 } |
2578 | 2683 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2644 } | 2749 } |
2645 return OK; | 2750 return OK; |
2646 } | 2751 } |
2647 | 2752 |
2648 void HttpCache::Transaction::DoneWritingToEntry(bool success) { | 2753 void HttpCache::Transaction::DoneWritingToEntry(bool success) { |
2649 if (!entry_) | 2754 if (!entry_) |
2650 return; | 2755 return; |
2651 | 2756 |
2652 RecordHistograms(); | 2757 RecordHistograms(); |
2653 | 2758 |
2654 cache_->DoneWritingToEntry(entry_, success); | 2759 cache_->DoneWritingToEntry(entry_, success, this); |
2655 entry_ = NULL; | 2760 entry_ = NULL; |
2656 mode_ = NONE; // switch to 'pass through' mode | 2761 mode_ = NONE; // switch to 'pass through' mode |
2657 } | 2762 } |
2658 | 2763 |
2659 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { | 2764 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { |
2660 DLOG(ERROR) << "ReadData failed: " << result; | 2765 DLOG(ERROR) << "ReadData failed: " << result; |
2661 const int result_for_histogram = std::max(0, -result); | 2766 const int result_for_histogram = std::max(0, -result); |
2662 if (restart) { | 2767 if (restart) { |
2663 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", | 2768 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", |
2664 result_for_histogram); | 2769 result_for_histogram); |
(...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
3000 default: | 3105 default: |
3001 NOTREACHED(); | 3106 NOTREACHED(); |
3002 } | 3107 } |
3003 } | 3108 } |
3004 | 3109 |
3005 void HttpCache::Transaction::OnIOComplete(int result) { | 3110 void HttpCache::Transaction::OnIOComplete(int result) { |
3006 DoLoop(result); | 3111 DoLoop(result); |
3007 } | 3112 } |
3008 | 3113 |
3009 } // namespace net | 3114 } // namespace net |
OLD | NEW |