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

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

Issue 12310075: Cache failover to LOAD_PREFERRING_CACHE if network response suggests offline. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Removed vestigial reference to new content flag. Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/http/http_cache_transaction.h" 5 #include "net/http/http_cache_transaction.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #if defined(OS_POSIX) 9 #if defined(OS_POSIX)
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 target_state_(STATE_NONE), 133 target_state_(STATE_NONE),
134 reading_(false), 134 reading_(false),
135 invalid_range_(false), 135 invalid_range_(false),
136 truncated_(false), 136 truncated_(false),
137 is_sparse_(false), 137 is_sparse_(false),
138 range_requested_(false), 138 range_requested_(false),
139 handling_206_(false), 139 handling_206_(false),
140 cache_pending_(false), 140 cache_pending_(false),
141 done_reading_(false), 141 done_reading_(false),
142 vary_mismatch_(false), 142 vary_mismatch_(false),
143 couldnt_conditionalize_request_(false),
143 io_buf_len_(0), 144 io_buf_len_(0),
144 read_offset_(0), 145 read_offset_(0),
145 effective_load_flags_(0), 146 effective_load_flags_(0),
146 write_len_(0), 147 write_len_(0),
147 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), 148 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)),
148 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( 149 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_(
149 base::Bind(&Transaction::OnIOComplete, 150 base::Bind(&Transaction::OnIOComplete,
150 weak_factory_.GetWeakPtr()))), 151 weak_factory_.GetWeakPtr()))),
151 transaction_pattern_(PATTERN_UNDEFINED), 152 transaction_pattern_(PATTERN_UNDEFINED),
152 defer_cache_sensitivity_delay_(false), 153 defer_cache_sensitivity_delay_(false),
(...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after
803 rv = network_trans_->Start(request_, io_callback_, net_log_); 804 rv = network_trans_->Start(request_, io_callback_, net_log_);
804 return rv; 805 return rv;
805 } 806 }
806 807
807 int HttpCache::Transaction::DoSendRequestComplete(int result) { 808 int HttpCache::Transaction::DoSendRequestComplete(int result) {
808 ReportNetworkActionFinish(); 809 ReportNetworkActionFinish();
809 810
810 if (!cache_) 811 if (!cache_)
811 return ERR_UNEXPECTED; 812 return ERR_UNEXPECTED;
812 813
814 // If requested, and we have a readable cache entry, and we have
815 // an error indicating that we're offline as opposed to in contact
816 // with a bad server, read from cache anyway.
817 if (effective_load_flags_ & LOAD_CACHE_RETURN_IF_OFFLINE &&
rvargas (doing something else) 2013/03/05 02:58:19 nit: single space before &... and add () around th
Randy Smith (Not in Mondays) 2013/03/05 23:16:14 Whoops; done.
818 mode_ == READ_WRITE && entry_ && !partial_ && result != OK) {
rvargas (doing something else) 2013/03/05 02:58:19 nit: move result != OK to the beginning of this li
rvargas (doing something else) 2013/03/05 02:58:19 it would be nice to make this work with byte range
Randy Smith (Not in Mondays) 2013/03/05 23:16:14 Done.
Randy Smith (Not in Mondays) 2013/03/05 23:16:14 I'm happy to take a swing at that, but I'm not com
rvargas (doing something else) 2013/03/06 03:11:48 I don't think we need to do that on the first vers
Randy Smith (Not in Mondays) 2013/03/06 22:55:55 Ok, sounds good.
819 switch (result) {
rvargas (doing something else) 2013/03/05 02:58:19 if (IsOffline(result)) ?
Randy Smith (Not in Mondays) 2013/03/05 23:16:14 Done. Note that I took the opportunity to get rid
820 // Errors that mean we didn't have a connection to the host.
821 // Go ahead and read from the cache anyway.
822 case ERR_NAME_NOT_RESOLVED:
823 case ERR_INTERNET_DISCONNECTED:
824 case ERR_ADDRESS_UNREACHABLE:
825 case ERR_CONNECTION_TIMED_OUT:
826 network_trans_.reset();
827 UpdateTransactionPattern(PATTERN_NOT_COVERED);
828
829 cache_->ConvertWriterToReader(entry_);
rvargas (doing something else) 2013/03/05 02:58:19 maybe extract this and 1800-1818 into a new method
Randy Smith (Not in Mondays) 2013/03/05 23:16:14 Say more? I don't follow the suggestion, probably
rvargas (doing something else) 2013/03/06 03:11:48 I just meant something like: if (effective_flags.
Randy Smith (Not in Mondays) 2013/03/06 22:55:55 Ah, gotcha. I'm not used to thinking of things th
830 mode_ = READ;
831 response_.was_cache_override = true;
832
833 // If there is metadata, read it. Otherwise, we're done.
834 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
835 next_state_ = STATE_CACHE_READ_METADATA;
836 return OK;
837 default:
838 break;
839 }
840 }
841
842 // If we tried to conditionalize the request and failed, we know
843 // we won't be reading from the cache after this point.
844 if (couldnt_conditionalize_request_)
845 mode_ = WRITE;
846
813 if (result == OK) { 847 if (result == OK) {
814 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST; 848 next_state_ = STATE_SUCCESSFUL_SEND_REQUEST;
815 return OK; 849 return OK;
816 } 850 }
817 851
818 // Do not record requests that have network errors or restarts. 852 // Do not record requests that have network errors or restarts.
819 UpdateTransactionPattern(PATTERN_NOT_COVERED); 853 UpdateTransactionPattern(PATTERN_NOT_COVERED);
820 if (IsCertificateError(result)) { 854 if (IsCertificateError(result)) {
821 const HttpResponseInfo* response = network_trans_->GetResponseInfo(); 855 const HttpResponseInfo* response = network_trans_->GetResponseInfo();
822 // If we get a certificate error, then there is a certificate in ssl_info, 856 // If we get a certificate error, then there is a certificate in ssl_info,
(...skipping 942 matching lines...) Expand 10 before | Expand all | Expand 10 after
1765 UpdateTransactionPattern(PATTERN_ENTRY_USED); 1799 UpdateTransactionPattern(PATTERN_ENTRY_USED);
1766 if (partial_.get()) { 1800 if (partial_.get()) {
1767 if (truncated_ || is_sparse_ || !invalid_range_) { 1801 if (truncated_ || is_sparse_ || !invalid_range_) {
1768 // We are going to return the saved response headers to the caller, so 1802 // We are going to return the saved response headers to the caller, so
1769 // we may need to adjust them first. 1803 // we may need to adjust them first.
1770 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; 1804 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED;
1771 return OK; 1805 return OK;
1772 } else { 1806 } else {
1773 partial_.reset(); 1807 partial_.reset();
1774 } 1808 }
1809 } else {
1810 // Override possible flag set in RequiresValidation(); we're
1811 // validating and did not override from cache.
1812 response_.was_cache_override = false;
rvargas (doing something else) 2013/03/05 02:58:19 remove? (see other comments)
Randy Smith (Not in Mondays) 2013/03/05 23:16:14 As noted in other comment, I don't yet follow.
1775 } 1813 }
1776 cache_->ConvertWriterToReader(entry_); 1814 cache_->ConvertWriterToReader(entry_);
1777 mode_ = READ; 1815 mode_ = READ;
1778 1816
1779 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 1817 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
1780 next_state_ = STATE_CACHE_READ_METADATA; 1818 next_state_ = STATE_CACHE_READ_METADATA;
1781 } else { 1819 } else {
1782 // Make the network request conditional, to see if we may reuse our cached 1820 // Make the network request conditional, to see if we may reuse our cached
1783 // response. If we cannot do so, then we just resort to a normal fetch. 1821 // response. If we cannot do so, then we just resort to a normal fetch.
1784 // Our mode remains READ_WRITE for a conditional request. We'll switch to 1822 // Our mode remains READ_WRITE for a conditional request. We'll switch to
1785 // either READ or WRITE mode once we hear back from the server. 1823 // either READ or WRITE mode once we hear back from the server.
1786 if (!ConditionalizeRequest()) { 1824 if (!ConditionalizeRequest()) {
1825 couldnt_conditionalize_request_ = true;
1787 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE); 1826 UpdateTransactionPattern(PATTERN_ENTRY_CANT_CONDITIONALIZE);
1788 if (partial_.get()) 1827 if (partial_.get())
1789 return DoRestartPartialRequest(); 1828 return DoRestartPartialRequest();
1790 1829
1791 DCHECK_NE(206, response_.headers->response_code()); 1830 DCHECK_NE(206, response_.headers->response_code());
1792 mode_ = WRITE; 1831 // We don't want to switch mode to WRITE here as in offline
rvargas (doing something else) 2013/03/05 02:58:19 update the comment at 1822 instead of adding a new
Randy Smith (Not in Mondays) 2013/03/05 23:16:14 Done. Note that I changed the comment to only ref
1832 // mode we may still need to read from the cache. Instead
1833 // we record and test on whether we could conditionalize the
1834 // request.
1793 } 1835 }
1794 next_state_ = STATE_SEND_REQUEST; 1836 next_state_ = STATE_SEND_REQUEST;
1795 } 1837 }
1796 return OK; 1838 return OK;
1797 } 1839 }
1798 1840
1799 int HttpCache::Transaction::BeginPartialCacheValidation() { 1841 int HttpCache::Transaction::BeginPartialCacheValidation() {
1800 DCHECK(mode_ == READ_WRITE); 1842 DCHECK(mode_ == READ_WRITE);
1801 1843
1802 if (response_.headers->response_code() != 206 && !partial_.get() && 1844 if (response_.headers->response_code() != 206 && !partial_.get() &&
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1918 // In playback mode, nothing requires validation. 1960 // In playback mode, nothing requires validation.
1919 if (cache_->mode() == net::HttpCache::PLAYBACK) 1961 if (cache_->mode() == net::HttpCache::PLAYBACK)
1920 return false; 1962 return false;
1921 1963
1922 if (response_.vary_data.is_valid() && 1964 if (response_.vary_data.is_valid() &&
1923 !response_.vary_data.MatchesRequest(*request_, *response_.headers)) { 1965 !response_.vary_data.MatchesRequest(*request_, *response_.headers)) {
1924 vary_mismatch_ = true; 1966 vary_mismatch_ = true;
1925 return true; 1967 return true;
1926 } 1968 }
1927 1969
1928 if (effective_load_flags_ & LOAD_PREFERRING_CACHE) 1970 if (effective_load_flags_ & LOAD_PREFERRING_CACHE) {
1971 response_.was_cache_override = true;
rvargas (doing something else) 2013/03/05 02:58:19 Feels wrong to do this, especially on this method.
Randy Smith (Not in Mondays) 2013/03/05 23:16:14 ETA: One of your later comments made it clear your
1929 return false; 1972 return false;
1973 }
1930 1974
1931 if (effective_load_flags_ & LOAD_VALIDATE_CACHE) 1975 if (effective_load_flags_ & LOAD_VALIDATE_CACHE)
1932 return true; 1976 return true;
1933 1977
1934 if (request_->method == "PUT" || request_->method == "DELETE") 1978 if (request_->method == "PUT" || request_->method == "DELETE")
1935 return true; 1979 return true;
1936 1980
1937 if (response_.headers->RequiresValidation( 1981 if (response_.headers->RequiresValidation(
1938 response_.request_time, response_.response_time, Time::Now())) { 1982 response_.request_time, response_.response_time, Time::Now())) {
1939 return true; 1983 return true;
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after
2573 entry_path += " -> "; 2617 entry_path += " -> ";
2574 entry_path += state_names[*it]; 2618 entry_path += state_names[*it];
2575 } 2619 }
2576 LOG(WARNING) << "Path state transitions for " << cache_key_ 2620 LOG(WARNING) << "Path state transitions for " << cache_key_
2577 << ": " << entry_path; 2621 << ": " << entry_path;
2578 } 2622 }
2579 2623
2580 #endif 2624 #endif
2581 2625
2582 } // namespace net 2626 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698