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 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 new_entry_(NULL), | 256 new_entry_(NULL), |
| 257 new_response_(NULL), | 257 new_response_(NULL), |
| 258 mode_(NONE), | 258 mode_(NONE), |
| 259 reading_(false), | 259 reading_(false), |
| 260 invalid_range_(false), | 260 invalid_range_(false), |
| 261 truncated_(false), | 261 truncated_(false), |
| 262 is_sparse_(false), | 262 is_sparse_(false), |
| 263 range_requested_(false), | 263 range_requested_(false), |
| 264 handling_206_(false), | 264 handling_206_(false), |
| 265 cache_pending_(false), | 265 cache_pending_(false), |
| 266 done_reading_(false), | 266 done_writing_(false), |
| 267 vary_mismatch_(false), | 267 vary_mismatch_(false), |
| 268 couldnt_conditionalize_request_(false), | 268 couldnt_conditionalize_request_(false), |
| 269 stopped_caching_(false), | |
| 269 bypass_lock_for_test_(false), | 270 bypass_lock_for_test_(false), |
| 270 fail_conditionalization_for_test_(false), | 271 fail_conditionalization_for_test_(false), |
| 271 io_buf_len_(0), | 272 io_buf_len_(0), |
| 272 read_offset_(0), | 273 read_offset_(0), |
| 273 effective_load_flags_(0), | 274 effective_load_flags_(0), |
| 274 write_len_(0), | 275 write_len_(0), |
| 275 transaction_pattern_(PATTERN_UNDEFINED), | 276 transaction_pattern_(PATTERN_UNDEFINED), |
| 276 total_received_bytes_(0), | 277 total_received_bytes_(0), |
| 277 websocket_handshake_stream_base_create_helper_(NULL), | 278 websocket_handshake_stream_base_create_helper_(NULL), |
| 278 weak_factory_(this) { | 279 weak_factory_(this) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 293 if (entry_) { | 294 if (entry_) { |
| 294 bool cancel_request = reading_ && response_.headers.get(); | 295 bool cancel_request = reading_ && response_.headers.get(); |
| 295 if (cancel_request) { | 296 if (cancel_request) { |
| 296 if (partial_) { | 297 if (partial_) { |
| 297 entry_->disk_entry->CancelSparseIO(); | 298 entry_->disk_entry->CancelSparseIO(); |
| 298 } else { | 299 } else { |
| 299 cancel_request &= (response_.headers->response_code() == 200); | 300 cancel_request &= (response_.headers->response_code() == 200); |
| 300 } | 301 } |
| 301 } | 302 } |
| 302 | 303 |
| 303 cache_->DoneWithEntry(entry_, this, cancel_request); | 304 DoneWithEntry(cancel_request); |
| 304 } else if (cache_pending_) { | 305 } else if (cache_pending_) { |
| 305 cache_->RemovePendingTransaction(this); | 306 cache_->RemovePendingTransaction(this); |
| 306 } | 307 } |
| 307 } | 308 } |
| 308 } | 309 } |
| 309 | 310 |
| 310 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, | 311 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, |
| 311 const CompletionCallback& callback) { | 312 const CompletionCallback& callback) { |
| 312 DCHECK(buf); | 313 DCHECK(buf); |
| 313 DCHECK_GT(buf_len, 0); | 314 DCHECK_GT(buf_len, 0); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 327 DCHECK(mode_ & WRITE || mode_ == NONE); | 328 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 328 | 329 |
| 329 // Don't set the flag for sparse entries. | 330 // Don't set the flag for sparse entries. |
| 330 if (is_sparse_) | 331 if (is_sparse_) |
| 331 return true; | 332 return true; |
| 332 | 333 |
| 333 if (!CanResume(true)) | 334 if (!CanResume(true)) |
| 334 return false; | 335 return false; |
| 335 | 336 |
| 336 // We may have received the whole resource already. | 337 // We may have received the whole resource already. |
| 337 if (done_reading_) | 338 if (done_writing_) |
| 338 return true; | 339 return true; |
| 339 | 340 |
| 340 truncated_ = true; | 341 truncated_ = true; |
| 341 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE; | 342 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE; |
| 342 DoLoop(OK); | 343 DoLoop(OK); |
| 343 return true; | 344 return true; |
| 344 } | 345 } |
| 345 | 346 |
| 346 LoadState HttpCache::Transaction::GetWriterLoadState() const { | 347 LoadState HttpCache::Transaction::GetWriterLoadState() const { |
| 347 if (network_trans_.get()) | 348 if (network_trans_.get()) |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 488 int rv = DoLoop(OK); | 489 int rv = DoLoop(OK); |
| 489 | 490 |
| 490 if (rv == ERR_IO_PENDING) { | 491 if (rv == ERR_IO_PENDING) { |
| 491 DCHECK(callback_.is_null()); | 492 DCHECK(callback_.is_null()); |
| 492 callback_ = callback; | 493 callback_ = callback; |
| 493 } | 494 } |
| 494 return rv; | 495 return rv; |
| 495 } | 496 } |
| 496 | 497 |
| 497 void HttpCache::Transaction::StopCaching() { | 498 void HttpCache::Transaction::StopCaching() { |
| 498 // We really don't know where we are now. Hopefully there is no operation in | |
| 499 // progress, but nothing really prevents this method to be called after we | |
| 500 // returned ERR_IO_PENDING. We cannot attempt to truncate the entry at this | |
| 501 // point because we need the state machine for that (and even if we are really | |
| 502 // free, that would be an asynchronous operation). In other words, keep the | |
| 503 // entry how it is (it will be marked as truncated at destruction), and let | |
| 504 // the next piece of code that executes know that we are now reading directly | |
| 505 // from the net. | |
| 506 // TODO(mmenke): This doesn't release the lock on the cache entry, so a | 499 // TODO(mmenke): This doesn't release the lock on the cache entry, so a |
| 507 // future request for the resource will be blocked on this one. | 500 // future request for the resource will be blocked on this one. |
| 508 // Fix this. | 501 // Fix this. |
| 509 if (cache_.get() && entry_ && (mode_ & WRITE) && network_trans_.get() && | 502 |
| 510 !is_sparse_ && !range_requested_) { | 503 stopped_caching_ = true; |
| 511 mode_ = NONE; | 504 UpdateTransactionPattern(PATTERN_NOT_COVERED); |
| 512 } | |
| 513 } | 505 } |
| 514 | 506 |
| 515 bool HttpCache::Transaction::GetFullRequestHeaders( | 507 bool HttpCache::Transaction::GetFullRequestHeaders( |
| 516 HttpRequestHeaders* headers) const { | 508 HttpRequestHeaders* headers) const { |
| 517 if (network_trans_) | 509 if (network_trans_) |
| 518 return network_trans_->GetFullRequestHeaders(headers); | 510 return network_trans_->GetFullRequestHeaders(headers); |
| 519 | 511 |
| 520 // TODO(ttuttle): Read headers from cache. | 512 // TODO(ttuttle): Read headers from cache. |
| 521 return false; | 513 return false; |
| 522 } | 514 } |
| 523 | 515 |
| 524 int64 HttpCache::Transaction::GetTotalReceivedBytes() const { | 516 int64 HttpCache::Transaction::GetTotalReceivedBytes() const { |
| 525 int64 total_received_bytes = total_received_bytes_; | 517 int64 total_received_bytes = total_received_bytes_; |
| 526 if (network_trans_) | 518 if (network_trans_) |
| 527 total_received_bytes += network_trans_->GetTotalReceivedBytes(); | 519 total_received_bytes += network_trans_->GetTotalReceivedBytes(); |
| 528 return total_received_bytes; | 520 return total_received_bytes; |
| 529 } | 521 } |
| 530 | 522 |
| 531 void HttpCache::Transaction::DoneReading() { | 523 void HttpCache::Transaction::DoneReading() { |
| 532 if (cache_.get() && entry_) { | 524 DoneWithRequest(); |
| 533 DCHECK_NE(mode_, UPDATE); | |
| 534 if (mode_ & WRITE) { | |
| 535 DoneWritingToEntry(true); | |
| 536 } else if (mode_ & READ) { | |
| 537 // It is necessary to check mode_ & READ because it is possible | |
| 538 // for mode_ to be NONE and entry_ non-NULL with a write entry | |
| 539 // if StopCaching was called. | |
| 540 cache_->DoneReadingFromEntry(entry_, this); | |
| 541 entry_ = NULL; | |
| 542 } | |
| 543 } | |
| 544 } | 525 } |
| 545 | 526 |
| 546 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const { | 527 const HttpResponseInfo* HttpCache::Transaction::GetResponseInfo() const { |
| 547 // Null headers means we encountered an error or haven't a response yet | 528 // Null headers means we encountered an error or haven't a response yet |
| 548 if (auth_response_.headers.get()) | 529 if (auth_response_.headers.get()) |
| 549 return &auth_response_; | 530 return &auth_response_; |
| 550 return &response_; | 531 return &response_; |
| 551 } | 532 } |
| 552 | 533 |
| 553 LoadState HttpCache::Transaction::GetLoadState() const { | 534 LoadState HttpCache::Transaction::GetLoadState() const { |
| (...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1344 // We may end up here multiple times for a given request. | 1325 // We may end up here multiple times for a given request. |
| 1345 int HttpCache::Transaction::DoStartPartialCacheValidation() { | 1326 int HttpCache::Transaction::DoStartPartialCacheValidation() { |
| 1346 if (mode_ == NONE) | 1327 if (mode_ == NONE) |
| 1347 return OK; | 1328 return OK; |
| 1348 | 1329 |
| 1349 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION; | 1330 next_state_ = STATE_COMPLETE_PARTIAL_CACHE_VALIDATION; |
| 1350 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); | 1331 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); |
| 1351 } | 1332 } |
| 1352 | 1333 |
| 1353 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { | 1334 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { |
| 1354 if (!result) { | 1335 // This is the end of the request. |
| 1355 // This is the end of the request. | 1336 if (!result) |
| 1356 if (mode_ & WRITE) { | 1337 return DoneWithRequest(); |
| 1357 DoneWritingToEntry(true); | |
| 1358 } else { | |
| 1359 cache_->DoneReadingFromEntry(entry_, this); | |
| 1360 entry_ = NULL; | |
| 1361 } | |
| 1362 return result; | |
| 1363 } | |
| 1364 | 1338 |
| 1365 if (result < 0) | 1339 if (result < 0) |
| 1366 return result; | 1340 return result; |
| 1367 | 1341 |
| 1368 partial_->PrepareCacheValidation(entry_->disk_entry, | 1342 partial_->PrepareCacheValidation(entry_->disk_entry, |
| 1369 &custom_request_->extra_headers); | 1343 &custom_request_->extra_headers); |
| 1370 | 1344 |
| 1371 if (reading_ && partial_->IsCurrentRangeCached()) { | 1345 if (reading_ && partial_->IsCurrentRangeCached()) { |
| 1372 next_state_ = STATE_CACHE_READ_DATA; | 1346 next_state_ = STATE_CACHE_READ_DATA; |
| 1373 return OK; | 1347 return OK; |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1499 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED); | 1473 UpdateTransactionPattern(PATTERN_ENTRY_NOT_CACHED); |
| 1500 } | 1474 } |
| 1501 | 1475 |
| 1502 // Invalidate any cached GET with a successful PUT or DELETE. | 1476 // Invalidate any cached GET with a successful PUT or DELETE. |
| 1503 if (mode_ == WRITE && | 1477 if (mode_ == WRITE && |
| 1504 (request_->method == "PUT" || request_->method == "DELETE")) { | 1478 (request_->method == "PUT" || request_->method == "DELETE")) { |
| 1505 if (NonErrorResponse(new_response->headers->response_code())) { | 1479 if (NonErrorResponse(new_response->headers->response_code())) { |
| 1506 int ret = cache_->DoomEntry(cache_key_, NULL); | 1480 int ret = cache_->DoomEntry(cache_key_, NULL); |
| 1507 DCHECK_EQ(OK, ret); | 1481 DCHECK_EQ(OK, ret); |
| 1508 } | 1482 } |
| 1509 cache_->DoneWritingToEntry(entry_, true); | 1483 DoneWritingToEntry(true); |
| 1510 entry_ = NULL; | |
| 1511 mode_ = NONE; | |
| 1512 } | 1484 } |
| 1513 | 1485 |
| 1514 // Invalidate any cached GET with a successful POST. | 1486 // Invalidate any cached GET with a successful POST. |
| 1515 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && | 1487 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && |
| 1516 request_->method == "POST" && | 1488 request_->method == "POST" && |
| 1517 NonErrorResponse(new_response->headers->response_code())) { | 1489 NonErrorResponse(new_response->headers->response_code())) { |
| 1518 cache_->DoomMainEntryForUrl(request_->url); | 1490 cache_->DoomMainEntryForUrl(request_->url); |
| 1519 } | 1491 } |
| 1520 | 1492 |
| 1521 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); | 1493 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1636 | 1608 |
| 1637 // We change the value of Content-Length for partial content. | 1609 // We change the value of Content-Length for partial content. |
| 1638 if (handling_206_ && partial_) | 1610 if (handling_206_ && partial_) |
| 1639 partial_->FixContentLength(new_response_->headers.get()); | 1611 partial_->FixContentLength(new_response_->headers.get()); |
| 1640 | 1612 |
| 1641 response_ = *new_response_; | 1613 response_ = *new_response_; |
| 1642 | 1614 |
| 1643 if (request_->method == "HEAD") { | 1615 if (request_->method == "HEAD") { |
| 1644 // This response is replacing the cached one. | 1616 // This response is replacing the cached one. |
| 1645 DoneWritingToEntry(false); | 1617 DoneWritingToEntry(false); |
| 1646 mode_ = NONE; | |
| 1647 new_response_ = NULL; | 1618 new_response_ = NULL; |
| 1648 return OK; | 1619 return OK; |
| 1649 } | 1620 } |
| 1650 | 1621 |
| 1651 if (handling_206_ && !CanResume(false)) { | 1622 if (handling_206_ && !CanResume(false)) { |
| 1652 // There is no point in storing this resource because it will never be used. | 1623 // There is no point in storing this resource because it will never be used. |
| 1653 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries. | 1624 // This may change if we support LOAD_ONLY_FROM_CACHE with sparse entries. |
| 1654 DoneWritingToEntry(false); | 1625 DoneWritingToEntry(false); |
| 1655 if (partial_) | 1626 if (partial_) |
| 1656 partial_->FixResponseHeaders(response_.headers.get(), true); | 1627 partial_->FixResponseHeaders(response_.headers.get(), true); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1769 next_state_ = STATE_NETWORK_READ_COMPLETE; | 1740 next_state_ = STATE_NETWORK_READ_COMPLETE; |
| 1770 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); | 1741 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); |
| 1771 } | 1742 } |
| 1772 | 1743 |
| 1773 int HttpCache::Transaction::DoNetworkReadComplete(int result) { | 1744 int HttpCache::Transaction::DoNetworkReadComplete(int result) { |
| 1774 DCHECK(mode_ & WRITE || mode_ == NONE); | 1745 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 1775 | 1746 |
| 1776 if (!cache_.get()) | 1747 if (!cache_.get()) |
| 1777 return ERR_UNEXPECTED; | 1748 return ERR_UNEXPECTED; |
| 1778 | 1749 |
| 1779 // If there is an error or we aren't saving the data, we are done; just wait | 1750 if (result < 0) |
| 1780 // until the destructor runs to see if we can keep the data. | 1751 return result; |
| 1781 if (mode_ == NONE || result < 0) | 1752 |
| 1753 // Skip writing if StopCaching() has been called. | |
| 1754 // | |
| 1755 // For partial requests the transaction still needs to update its state. If | |
| 1756 // the network transaction being read was conditionalized, the cache | |
| 1757 // transaction may need to issue additional network or cache reads to fulfil | |
| 1758 // the original request. | |
| 1759 if (stopped_caching_ && partial_) | |
| 1760 return DoPartialNetworkReadCompleted(result); | |
| 1761 | |
| 1762 if (mode_ == NONE || stopped_caching_) | |
| 1782 return result; | 1763 return result; |
| 1783 | 1764 |
| 1784 next_state_ = STATE_CACHE_WRITE_DATA; | 1765 next_state_ = STATE_CACHE_WRITE_DATA; |
| 1785 return result; | 1766 return result; |
| 1786 } | 1767 } |
| 1787 | 1768 |
| 1788 int HttpCache::Transaction::DoCacheReadData() { | 1769 int HttpCache::Transaction::DoCacheReadData() { |
| 1789 if (request_->method == "HEAD") | 1770 if (request_->method == "HEAD") |
| 1790 return 0; | 1771 return 0; |
| 1791 | 1772 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1842 | 1823 |
| 1843 if (!entry_ || !num_bytes) | 1824 if (!entry_ || !num_bytes) |
| 1844 return num_bytes; | 1825 return num_bytes; |
| 1845 | 1826 |
| 1846 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 1827 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
| 1847 return WriteToEntry(kResponseContentIndex, current_size, read_buf_.get(), | 1828 return WriteToEntry(kResponseContentIndex, current_size, read_buf_.get(), |
| 1848 num_bytes, io_callback_); | 1829 num_bytes, io_callback_); |
| 1849 } | 1830 } |
| 1850 | 1831 |
| 1851 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) { | 1832 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) { |
| 1852 if (entry_) { | 1833 if (entry_ && net_log_.IsCapturing()) |
| 1853 if (net_log_.IsCapturing()) { | 1834 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA, |
| 1854 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA, | 1835 result); |
| 1855 result); | |
| 1856 } | |
| 1857 } | |
| 1858 if (!cache_.get()) | 1836 if (!cache_.get()) |
| 1859 return ERR_UNEXPECTED; | 1837 return ERR_UNEXPECTED; |
| 1860 | 1838 |
| 1861 if (result != write_len_) { | 1839 if (result != write_len_) { |
| 1862 DLOG(ERROR) << "failed to write response data to cache"; | 1840 DLOG(ERROR) << "failed to write response data to cache"; |
| 1863 DoneWritingToEntry(false); | 1841 DoneWritingToEntry(false); |
| 1864 | 1842 |
| 1865 // We want to ignore errors writing to disk and just keep reading from | 1843 // We want to ignore errors writing to disk and just keep reading from |
| 1866 // the network. | 1844 // the network. |
| 1867 result = write_len_; | 1845 result = write_len_; |
| 1868 } else if (!done_reading_ && entry_) { | 1846 } else if (!done_writing_ && entry_) { |
| 1869 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 1847 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
| 1870 int64 body_size = response_.headers->GetContentLength(); | 1848 int64 body_size = response_.headers->GetContentLength(); |
| 1871 if (body_size >= 0 && body_size <= current_size) | 1849 if (body_size >= 0 && body_size <= current_size) |
| 1872 done_reading_ = true; | 1850 done_writing_ = true; |
| 1873 } | 1851 } |
| 1874 | 1852 |
| 1875 if (partial_) { | 1853 if (partial_) { |
| 1876 // This may be the last request. | 1854 // This may be the last request. |
| 1877 if (result != 0 || truncated_ || | 1855 if (result != 0 || truncated_ || |
| 1878 !(partial_->IsLastRange() || mode_ == WRITE)) { | 1856 !(partial_->IsLastRange() || mode_ == WRITE)) { |
| 1879 return DoPartialNetworkReadCompleted(result); | 1857 return DoPartialNetworkReadCompleted(result); |
| 1880 } | 1858 } |
| 1881 } | 1859 } |
| 1882 | 1860 |
| 1883 if (result == 0) { | 1861 if (result == 0) { |
| 1884 // End of file. This may be the result of a connection problem so see if we | 1862 // End of file. This may be the result of a connection problem so see if we |
| 1885 // have to keep the entry around to be flagged as truncated later on. | 1863 // have to keep the entry around to be flagged as truncated later on. |
| 1886 if (done_reading_ || !entry_ || partial_ || | 1864 if (done_writing_ || !entry_ || partial_ || |
| 1887 response_.headers->GetContentLength() <= 0) { | 1865 response_.headers->GetContentLength() <= 0) |
| 1888 DoneWritingToEntry(true); | 1866 DoneWritingToEntry(true); |
| 1889 } | |
| 1890 } | 1867 } |
| 1891 | 1868 |
| 1892 return result; | 1869 return result; |
| 1893 } | 1870 } |
| 1894 | 1871 |
| 1895 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() { | 1872 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() { |
| 1896 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE; | 1873 next_state_ = STATE_CACHE_WRITE_TRUNCATED_RESPONSE_COMPLETE; |
| 1897 return WriteResponseInfoToEntry(true); | 1874 return WriteResponseInfoToEntry(true); |
| 1898 } | 1875 } |
| 1899 | 1876 |
| (...skipping 766 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2666 result); | 2643 result); |
| 2667 } | 2644 } |
| 2668 | 2645 |
| 2669 if (result != io_buf_len_) { | 2646 if (result != io_buf_len_) { |
| 2670 DLOG(ERROR) << "failed to write response info to cache"; | 2647 DLOG(ERROR) << "failed to write response info to cache"; |
| 2671 DoneWritingToEntry(false); | 2648 DoneWritingToEntry(false); |
| 2672 } | 2649 } |
| 2673 return OK; | 2650 return OK; |
| 2674 } | 2651 } |
| 2675 | 2652 |
| 2653 int HttpCache::Transaction::DoneWithRequest() { | |
| 2654 if (!cache_ || !entry_) | |
| 2655 return OK; | |
| 2656 | |
| 2657 DCHECK_EQ(STATE_NONE, next_state_); | |
|
hubbe
2015/07/16 15:56:12
Shouldn't these DCHECKs go first?
asanka
2015/07/16 17:57:00
Moved up.
| |
| 2658 DCHECK_NE(mode_, UPDATE); | |
| 2659 | |
| 2660 if (!(mode_ & WRITE)) { | |
| 2661 cache_->DoneReadingFromEntry(entry_, this); | |
| 2662 entry_ = NULL; | |
| 2663 return OK; | |
| 2664 } | |
| 2665 | |
| 2666 if (stopped_caching_ && !is_sparse_ && !done_writing_ && !entry_->doomed) { | |
| 2667 if (CanResume(true)) { | |
| 2668 truncated_ = true; | |
| 2669 return DoCacheWriteTruncatedResponse(); | |
| 2670 } | |
| 2671 int ret = cache_->DoomEntry(cache_key_, nullptr); | |
| 2672 DCHECK_EQ(OK, ret); | |
| 2673 } | |
| 2674 | |
| 2675 DoneWritingToEntry(true); | |
| 2676 return OK; | |
| 2677 } | |
| 2678 | |
| 2676 void HttpCache::Transaction::DoneWritingToEntry(bool success) { | 2679 void HttpCache::Transaction::DoneWritingToEntry(bool success) { |
| 2677 if (!entry_) | 2680 if (!entry_) |
| 2678 return; | 2681 return; |
| 2679 | 2682 |
| 2680 RecordHistograms(); | 2683 RecordHistograms(); |
| 2681 | 2684 |
| 2682 cache_->DoneWritingToEntry(entry_, success); | 2685 cache_->DoneWritingToEntry(entry_, success); |
| 2683 entry_ = NULL; | 2686 entry_ = NULL; |
| 2684 mode_ = NONE; // switch to 'pass through' mode | 2687 mode_ = NONE; // switch to 'pass through' mode |
| 2685 } | 2688 } |
| 2686 | 2689 |
| 2690 void HttpCache::Transaction::DoneWithEntry(bool cancel) { | |
| 2691 cache_->DoneWithEntry(entry_, this, cancel); | |
| 2692 entry_ = nullptr; | |
| 2693 is_sparse_ = false; | |
| 2694 truncated_ = false; | |
| 2695 } | |
| 2696 | |
| 2687 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { | 2697 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { |
| 2688 DLOG(ERROR) << "ReadData failed: " << result; | 2698 DLOG(ERROR) << "ReadData failed: " << result; |
| 2689 const int result_for_histogram = std::max(0, -result); | 2699 const int result_for_histogram = std::max(0, -result); |
| 2690 if (restart) { | 2700 if (restart) { |
| 2691 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", | 2701 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", |
| 2692 result_for_histogram); | 2702 result_for_histogram); |
| 2693 } else { | 2703 } else { |
| 2694 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable", | 2704 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorNonRestartable", |
| 2695 result_for_histogram); | 2705 result_for_histogram); |
| 2696 } | 2706 } |
| 2697 | 2707 |
| 2698 // Avoid using this entry in the future. | 2708 // Avoid using this entry in the future. |
| 2699 if (cache_.get()) | 2709 if (cache_.get()) |
| 2700 cache_->DoomActiveEntry(cache_key_); | 2710 cache_->DoomActiveEntry(cache_key_); |
| 2701 | 2711 |
| 2702 if (restart) { | 2712 if (restart) { |
| 2703 DCHECK(!reading_); | 2713 DCHECK(!reading_); |
| 2704 DCHECK(!network_trans_.get()); | 2714 DCHECK(!network_trans_.get()); |
| 2705 cache_->DoneWithEntry(entry_, this, false); | 2715 DoneWithEntry(false); |
| 2706 entry_ = NULL; | |
| 2707 is_sparse_ = false; | |
| 2708 partial_.reset(); | 2716 partial_.reset(); |
| 2709 next_state_ = STATE_GET_BACKEND; | 2717 next_state_ = STATE_GET_BACKEND; |
| 2710 return OK; | 2718 return OK; |
| 2711 } | 2719 } |
| 2712 | 2720 |
| 2713 return ERR_CACHE_READ_FAILURE; | 2721 return ERR_CACHE_READ_FAILURE; |
| 2714 } | 2722 } |
| 2715 | 2723 |
| 2716 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) { | 2724 void HttpCache::Transaction::OnAddToEntryTimeout(base::TimeTicks start_time) { |
| 2717 if (entry_lock_waiting_since_ != start_time) | 2725 if (entry_lock_waiting_since_ != start_time) |
| 2718 return; | 2726 return; |
| 2719 | 2727 |
| 2720 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE); | 2728 DCHECK_EQ(next_state_, STATE_ADD_TO_ENTRY_COMPLETE); |
| 2721 | 2729 |
| 2722 if (!cache_) | 2730 if (!cache_) |
| 2723 return; | 2731 return; |
| 2724 | 2732 |
| 2725 cache_->RemovePendingTransaction(this); | 2733 cache_->RemovePendingTransaction(this); |
| 2726 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT); | 2734 OnIOComplete(ERR_CACHE_LOCK_TIMEOUT); |
| 2727 } | 2735 } |
| 2728 | 2736 |
| 2729 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) { | 2737 void HttpCache::Transaction::DoomPartialEntry(bool delete_object) { |
| 2730 DVLOG(2) << "DoomPartialEntry"; | 2738 DVLOG(2) << "DoomPartialEntry"; |
| 2731 int rv = cache_->DoomEntry(cache_key_, NULL); | 2739 int rv = cache_->DoomEntry(cache_key_, NULL); |
| 2732 DCHECK_EQ(OK, rv); | 2740 DCHECK_EQ(OK, rv); |
| 2733 cache_->DoneWithEntry(entry_, this, false); | 2741 DoneWithEntry(false); |
| 2734 entry_ = NULL; | |
| 2735 is_sparse_ = false; | |
| 2736 truncated_ = false; | |
| 2737 if (delete_object) | 2742 if (delete_object) |
| 2738 partial_.reset(NULL); | 2743 partial_.reset(); |
| 2739 } | 2744 } |
| 2740 | 2745 |
| 2741 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) { | 2746 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) { |
| 2742 partial_->OnNetworkReadCompleted(result); | 2747 partial_->OnNetworkReadCompleted(result); |
| 2743 | 2748 |
| 2744 if (result == 0) { | 2749 if (result == 0) { |
| 2745 // We need to move on to the next range. | 2750 // We need to move on to the next range. |
| 2746 ResetNetworkTransaction(); | 2751 ResetNetworkTransaction(); |
| 2747 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | 2752 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; |
| 2748 } | 2753 } |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2918 default: | 2923 default: |
| 2919 NOTREACHED(); | 2924 NOTREACHED(); |
| 2920 } | 2925 } |
| 2921 } | 2926 } |
| 2922 | 2927 |
| 2923 void HttpCache::Transaction::OnIOComplete(int result) { | 2928 void HttpCache::Transaction::OnIOComplete(int result) { |
| 2924 DoLoop(result); | 2929 DoLoop(result); |
| 2925 } | 2930 } |
| 2926 | 2931 |
| 2927 } // namespace net | 2932 } // namespace net |
| OLD | NEW |