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 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 173 mode_(NONE), | 173 mode_(NONE), |
| 174 original_mode_(NONE), | 174 original_mode_(NONE), |
| 175 reading_(false), | 175 reading_(false), |
| 176 invalid_range_(false), | 176 invalid_range_(false), |
| 177 truncated_(false), | 177 truncated_(false), |
| 178 is_sparse_(false), | 178 is_sparse_(false), |
| 179 range_requested_(false), | 179 range_requested_(false), |
| 180 handling_206_(false), | 180 handling_206_(false), |
| 181 cache_pending_(false), | 181 cache_pending_(false), |
| 182 done_reading_(false), | 182 done_reading_(false), |
| 183 done_headers_(false), | |
| 183 vary_mismatch_(false), | 184 vary_mismatch_(false), |
| 184 couldnt_conditionalize_request_(false), | 185 couldnt_conditionalize_request_(false), |
| 185 bypass_lock_for_test_(false), | 186 bypass_lock_for_test_(false), |
| 186 fail_conditionalization_for_test_(false), | 187 fail_conditionalization_for_test_(false), |
| 187 validating_cannot_proceed_(false), | 188 validating_cannot_proceed_(false), |
| 188 io_buf_len_(0), | 189 io_buf_len_(0), |
| 189 read_offset_(0), | 190 read_offset_(0), |
| 190 effective_load_flags_(0), | 191 effective_load_flags_(0), |
| 191 write_len_(0), | 192 write_len_(0), |
| 192 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), | 193 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), |
| (...skipping 936 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1129 case ERR_CACHE_RACE: | 1130 case ERR_CACHE_RACE: |
| 1130 TransitionToState(STATE_INIT_ENTRY); | 1131 TransitionToState(STATE_INIT_ENTRY); |
| 1131 break; | 1132 break; |
| 1132 | 1133 |
| 1133 default: | 1134 default: |
| 1134 // We have a race here: Maybe we failed to open the entry and decided to | 1135 // We have a race here: Maybe we failed to open the entry and decided to |
| 1135 // create one, but by the time we called create, another transaction | 1136 // create one, but by the time we called create, another transaction |
| 1136 // already created the entry. If we want to eliminate this issue, we | 1137 // already created the entry. If we want to eliminate this issue, we |
| 1137 // need an atomic OpenOrCreate() method exposed by the disk cache. | 1138 // need an atomic OpenOrCreate() method exposed by the disk cache. |
| 1138 DLOG(WARNING) << "Unable to create cache entry"; | 1139 DLOG(WARNING) << "Unable to create cache entry"; |
| 1139 mode_ = NONE; | 1140 mode_ = NONE; |
|
Randy Smith (Not in Mondays)
2017/04/04 21:54:03
I presume this means we're transitioning into a mo
shivanisha
2017/04/07 23:24:13
done
| |
| 1140 if (partial_) | 1141 if (done_headers_) { |
| 1141 partial_->RestoreHeaders(&custom_request_->extra_headers); | 1142 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); |
| 1142 TransitionToState(STATE_SEND_REQUEST); | 1143 } else { |
| 1144 if (partial_) | |
| 1145 partial_->RestoreHeaders(&custom_request_->extra_headers); | |
| 1146 TransitionToState(STATE_SEND_REQUEST); | |
| 1147 } | |
| 1143 } | 1148 } |
| 1144 return OK; | 1149 return OK; |
| 1145 } | 1150 } |
| 1146 | 1151 |
| 1147 int HttpCache::Transaction::DoAddToEntry() { | 1152 int HttpCache::Transaction::DoAddToEntry() { |
| 1148 TRACE_EVENT0("io", "HttpCacheTransaction::DoAddToEntry"); | 1153 TRACE_EVENT0("io", "HttpCacheTransaction::DoAddToEntry"); |
| 1149 DCHECK(new_entry_); | 1154 DCHECK(new_entry_); |
| 1150 cache_pending_ = true; | 1155 cache_pending_ = true; |
| 1151 TransitionToState(STATE_ADD_TO_ENTRY_COMPLETE); | 1156 TransitionToState(STATE_ADD_TO_ENTRY_COMPLETE); |
| 1152 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY); | 1157 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_ADD_TO_ENTRY); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1200 entry_lock_waiting_since_ = TimeTicks(); | 1205 entry_lock_waiting_since_ = TimeTicks(); |
| 1201 DCHECK(new_entry_); | 1206 DCHECK(new_entry_); |
| 1202 cache_pending_ = false; | 1207 cache_pending_ = false; |
| 1203 | 1208 |
| 1204 if (result == OK) | 1209 if (result == OK) |
| 1205 entry_ = new_entry_; | 1210 entry_ = new_entry_; |
| 1206 | 1211 |
| 1207 // If there is a failure, the cache should have taken care of new_entry_. | 1212 // If there is a failure, the cache should have taken care of new_entry_. |
| 1208 new_entry_ = NULL; | 1213 new_entry_ = NULL; |
| 1209 | 1214 |
| 1215 // If this entry creation was a result of validation not matching, then this | |
| 1216 // transaction should be the headers_transaction. | |
| 1217 if (done_headers_) { | |
| 1218 DCHECK_EQ(result, OK); | |
| 1219 DCHECK_EQ(entry_->headers_transaction, this); | |
| 1220 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); | |
| 1221 return OK; | |
| 1222 } | |
| 1223 | |
| 1210 if (result == ERR_CACHE_RACE) { | 1224 if (result == ERR_CACHE_RACE) { |
| 1211 TransitionToState(STATE_INIT_ENTRY); | 1225 TransitionToState(STATE_INIT_ENTRY); |
| 1212 return OK; | 1226 return OK; |
| 1213 } | 1227 } |
| 1214 | 1228 |
| 1215 if (result == ERR_CACHE_LOCK_TIMEOUT) { | 1229 if (result == ERR_CACHE_LOCK_TIMEOUT) { |
| 1216 if (mode_ == READ) { | 1230 if (mode_ == READ) { |
| 1217 TransitionToState(STATE_NONE); | 1231 TransitionToState(STATE_NONE); |
| 1218 return ERR_CACHE_MISS; | 1232 return ERR_CACHE_MISS; |
| 1219 } | 1233 } |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1596 if (mode_ == READ_WRITE || mode_ == UPDATE) { | 1610 if (mode_ == READ_WRITE || mode_ == UPDATE) { |
| 1597 if (new_response->headers->response_code() == 304 || handling_206_) { | 1611 if (new_response->headers->response_code() == 304 || handling_206_) { |
| 1598 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); | 1612 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); |
| 1599 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); | 1613 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); |
| 1600 return OK; | 1614 return OK; |
| 1601 } | 1615 } |
| 1602 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); | 1616 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); |
| 1603 mode_ = WRITE; | 1617 mode_ = WRITE; |
| 1604 } | 1618 } |
| 1605 | 1619 |
| 1606 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); | 1620 if (!entry_) { |
|
Randy Smith (Not in Mondays)
2017/04/04 21:54:03
Could this conditional be more succinctly implemen
shivanisha
2017/04/07 23:24:13
N/A now that this code is implemented in DoCacheWr
| |
| 1607 | 1621 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); |
| 1608 if (!entry_) | |
| 1609 return OK; | 1622 return OK; |
| 1623 } | |
| 1610 | 1624 |
| 1611 // Invalidate any current entry with a successful response if this transaction | 1625 // Invalidate any current entry with a successful response if this transaction |
| 1612 // cannot write to this entry. | 1626 // cannot write to this entry. |
| 1613 if (new_response->headers->response_code() != 304 && | 1627 if (new_response->headers->response_code() != 304 && |
| 1614 (entry_->writer || !entry_->readers.empty())) { | 1628 (entry_->writer || !entry_->readers.empty())) { |
| 1615 DCHECK_EQ(entry_->headers_transaction, this); | 1629 DCHECK_EQ(entry_->headers_transaction, this); |
| 1616 cache_->DoneResponseHeaders(entry_, this, false); | 1630 cache_->DoneResponseHeaders(entry_, this, false); |
| 1617 entry_ = nullptr; | 1631 entry_ = nullptr; |
| 1618 mode_ = NONE; | 1632 TransitionToState(STATE_CREATE_ENTRY); |
|
Randy Smith (Not in Mondays)
2017/04/04 21:54:03
As I understand the logic that follows on from thi
shivanisha
2017/04/07 23:24:13
Note that the state OVERWRITE_CACHED_RESPONSE bein
| |
| 1633 done_headers_ = true; | |
| 1619 return OK; | 1634 return OK; |
| 1620 } | 1635 } |
| 1621 | 1636 |
| 1637 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); | |
| 1622 return OK; | 1638 return OK; |
| 1623 } | 1639 } |
| 1624 | 1640 |
| 1625 // We received 304 or 206 and we want to update the cached response headers. | 1641 // We received 304 or 206 and we want to update the cached response headers. |
| 1626 int HttpCache::Transaction::DoUpdateCachedResponse() { | 1642 int HttpCache::Transaction::DoUpdateCachedResponse() { |
| 1627 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); | 1643 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); |
| 1628 int rv = OK; | 1644 int rv = OK; |
| 1629 // Update the cached response based on the headers and properties of | 1645 // Update the cached response based on the headers and properties of |
| 1630 // new_response_. | 1646 // new_response_. |
| 1631 response_.headers->Update(*new_response_->headers.get()); | 1647 response_.headers->Update(*new_response_->headers.get()); |
| (...skipping 1599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3231 } | 3247 } |
| 3232 | 3248 |
| 3233 void HttpCache::Transaction::TransitionToState(State state) { | 3249 void HttpCache::Transaction::TransitionToState(State state) { |
| 3234 // Ensure that the state is only set once per Do* state. | 3250 // Ensure that the state is only set once per Do* state. |
| 3235 DCHECK(in_do_loop_); | 3251 DCHECK(in_do_loop_); |
| 3236 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; | 3252 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; |
| 3237 next_state_ = state; | 3253 next_state_ = state; |
| 3238 } | 3254 } |
| 3239 | 3255 |
| 3240 } // namespace net | 3256 } // namespace net |
| OLD | NEW |