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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: Josh's feedback on HttpCache::Transaction addressed Created 3 years, 8 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
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" // 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 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 163
164 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache) 164 HttpCache::Transaction::Transaction(RequestPriority priority, HttpCache* cache)
165 : next_state_(STATE_NONE), 165 : next_state_(STATE_NONE),
166 request_(NULL), 166 request_(NULL),
167 priority_(priority), 167 priority_(priority),
168 cache_(cache->GetWeakPtr()), 168 cache_(cache->GetWeakPtr()),
169 entry_(NULL), 169 entry_(NULL),
170 new_entry_(NULL), 170 new_entry_(NULL),
171 new_response_(NULL), 171 new_response_(NULL),
172 mode_(NONE), 172 mode_(NONE),
173 original_mode_(NONE),
173 reading_(false), 174 reading_(false),
174 invalid_range_(false), 175 invalid_range_(false),
175 truncated_(false), 176 truncated_(false),
176 is_sparse_(false), 177 is_sparse_(false),
177 range_requested_(false), 178 range_requested_(false),
178 handling_206_(false), 179 handling_206_(false),
179 cache_pending_(false), 180 cache_pending_(false),
180 done_reading_(false), 181 done_reading_(false),
181 vary_mismatch_(false), 182 vary_mismatch_(false),
182 couldnt_conditionalize_request_(false), 183 couldnt_conditionalize_request_(false),
183 bypass_lock_for_test_(false), 184 bypass_lock_for_test_(false),
184 fail_conditionalization_for_test_(false), 185 fail_conditionalization_for_test_(false),
186 validating_cannot_proceed_(false),
185 io_buf_len_(0), 187 io_buf_len_(0),
186 read_offset_(0), 188 read_offset_(0),
187 effective_load_flags_(0), 189 effective_load_flags_(0),
188 write_len_(0), 190 write_len_(0),
189 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED), 191 cache_entry_status_(CacheEntryStatus::ENTRY_UNDEFINED),
190 validation_cause_(VALIDATION_CAUSE_UNDEFINED), 192 validation_cause_(VALIDATION_CAUSE_UNDEFINED),
191 total_received_bytes_(0), 193 total_received_bytes_(0),
192 total_sent_bytes_(0), 194 total_sent_bytes_(0),
193 websocket_handshake_stream_base_create_helper_(NULL), 195 websocket_handshake_stream_base_create_helper_(NULL),
194 in_do_loop_(false), 196 in_do_loop_(false),
195 weak_factory_(this) { 197 weak_factory_(this) {
196 TRACE_EVENT0("io", "HttpCacheTransaction::Transaction"); 198 TRACE_EVENT0("io", "HttpCacheTransaction::Transaction");
197 static_assert(HttpCache::Transaction::kNumValidationHeaders == 199 static_assert(HttpCache::Transaction::kNumValidationHeaders ==
198 arraysize(kValidationHeaders), 200 arraysize(kValidationHeaders),
199 "invalid number of validation headers"); 201 "invalid number of validation headers");
200 202
201 io_callback_ = base::Bind(&Transaction::OnIOComplete, 203 io_callback_ = base::Bind(&Transaction::OnIOComplete,
202 weak_factory_.GetWeakPtr()); 204 weak_factory_.GetWeakPtr());
203 } 205 }
204 206
205 HttpCache::Transaction::~Transaction() { 207 HttpCache::Transaction::~Transaction() {
206 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction"); 208 TRACE_EVENT0("io", "HttpCacheTransaction::~Transaction");
207 // We may have to issue another IO, but we should never invoke the callback_ 209 // We may have to issue another IO, but we should never invoke the callback_
208 // after this point. 210 // after this point.
209 callback_.Reset(); 211 callback_.Reset();
210 212
211 if (cache_) { 213 if (cache_) {
212 if (entry_) { 214 if (entry_) {
213 bool cancel_request = reading_ && response_.headers.get(); 215 bool cancel_response =
214 if (cancel_request) { 216 cache_->IsCancelResponseBody(entry_, this, request_->method);
215 if (partial_) { 217 if (cancel_response && partial_)
216 entry_->disk_entry->CancelSparseIO(); 218 entry_->disk_entry->CancelSparseIO();
217 } else {
218 cancel_request &= (response_.headers->response_code() == 200);
219 }
220 }
221 219
222 cache_->DoneWithEntry(entry_, this, cancel_request); 220 cache_->DoneWithEntry(entry_, this, cancel_response);
223 } else if (cache_pending_) { 221 } else if (cache_pending_) {
224 cache_->RemovePendingTransaction(this); 222 cache_->RemovePendingTransaction(this);
225 } 223 }
226 } 224 }
227 } 225 }
228 226
229 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len, 227 int HttpCache::Transaction::WriteMetadata(IOBuffer* buf, int buf_len,
230 const CompletionCallback& callback) { 228 const CompletionCallback& callback) {
231 DCHECK(buf); 229 DCHECK(buf);
232 DCHECK_GT(buf_len, 0); 230 DCHECK_GT(buf_len, 0);
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
367 } 365 }
368 366
369 bool HttpCache::Transaction::IsReadyToRestartForAuth() { 367 bool HttpCache::Transaction::IsReadyToRestartForAuth() {
370 if (!network_trans_.get()) 368 if (!network_trans_.get())
371 return false; 369 return false;
372 return network_trans_->IsReadyToRestartForAuth(); 370 return network_trans_->IsReadyToRestartForAuth();
373 } 371 }
374 372
375 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len, 373 int HttpCache::Transaction::Read(IOBuffer* buf, int buf_len,
376 const CompletionCallback& callback) { 374 const CompletionCallback& callback) {
375 // TODO(shivanisha): A lot of tests invoke Read on a HEAD request which
376 // should not be allowed in the Read() method. Fix those tests.
377 DCHECK_EQ(next_state_, STATE_NONE); 377 DCHECK_EQ(next_state_, STATE_NONE);
378 DCHECK(buf); 378 DCHECK(buf);
379 DCHECK_GT(buf_len, 0); 379 DCHECK_GT(buf_len, 0);
380 DCHECK(!callback.is_null()); 380 DCHECK(!callback.is_null());
381 381
382 DCHECK(callback_.is_null()); 382 DCHECK(callback_.is_null());
383 383
384 if (!cache_.get()) 384 if (!cache_.get())
385 return ERR_UNEXPECTED; 385 return ERR_UNEXPECTED;
386 386
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 ConnectionAttempts* out) const { 572 ConnectionAttempts* out) const {
573 ConnectionAttempts new_connection_attempts; 573 ConnectionAttempts new_connection_attempts;
574 if (network_trans_) 574 if (network_trans_)
575 network_trans_->GetConnectionAttempts(&new_connection_attempts); 575 network_trans_->GetConnectionAttempts(&new_connection_attempts);
576 576
577 out->swap(new_connection_attempts); 577 out->swap(new_connection_attempts);
578 out->insert(out->begin(), old_connection_attempts_.begin(), 578 out->insert(out->begin(), old_connection_attempts_.begin(),
579 old_connection_attempts_.end()); 579 old_connection_attempts_.end());
580 } 580 }
581 581
582 void HttpCache::Transaction::SetValidatingCannotProceed() {
583 validating_cannot_proceed_ = true;
584 entry_ = nullptr;
585 }
586
582 size_t HttpCache::Transaction::EstimateMemoryUsage() const { 587 size_t HttpCache::Transaction::EstimateMemoryUsage() const {
583 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108. 588 // TODO(xunjieli): Consider improving the coverage. crbug.com/669108.
584 return 0; 589 return 0;
585 } 590 }
586 591
587 //----------------------------------------------------------------------------- 592 //-----------------------------------------------------------------------------
588 593
589 // A few common patterns: (Foo* means Foo -> FooComplete) 594 // A few common patterns: (Foo* means Foo -> FooComplete)
590 // 595 //
591 // 1. Not-cached entry: 596 // 1. Not-cached entry:
592 // Start(): 597 // Start():
593 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* -> 598 // GetBackend* -> InitEntry -> OpenEntry* -> CreateEntry* -> AddToEntry* ->
594 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse -> 599 // SendRequest* -> SuccessfulSendRequest -> OverwriteCachedResponse ->
595 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* -> 600 // CacheWriteResponse* -> TruncateCachedData* -> TruncateCachedMetadata* ->
596 // PartialHeadersReceived 601 // PartialHeadersReceived
597 // 602 //
598 // Read(): 603 // Read():
599 // NetworkRead* -> CacheWriteData* 604 // NetworkRead* -> CacheWriteData*
600 // 605 //
601 // 2. Cached entry, no validation: 606 // 2. Cached entry, no validation:
602 // Start(): 607 // Start():
603 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 608 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
604 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 609 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
605 // BeginCacheValidation() -> SetupEntryForRead() 610 // BeginCacheValidation() -> SetupEntryForRead() -> WaitBeforeRead*
jkarlin 2017/03/30 17:04:54 Need to update these comments for the new state na
shivanisha 2017/03/31 17:47:06 Updated the comments to rename and include the sta
606 // 611 //
607 // Read(): 612 // Read():
608 // CacheReadData* 613 // CacheReadData*
609 // 614 //
610 // 3. Cached entry, validation (304): 615 // 3. Cached entry, validation (304):
611 // Start(): 616 // Start():
612 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 617 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
613 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 618 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
614 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 619 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
615 // UpdateCachedResponse -> CacheWriteUpdatedResponse* -> 620 // UpdateCachedResponse -> CacheWriteUpdatedResponse* ->
616 // UpdateCachedResponseComplete -> OverwriteCachedResponse -> 621 // UpdateCachedResponseComplete -> OverwriteCachedResponse ->
617 // PartialHeadersReceived 622 // PartialHeadersReceived -> WaitBeforeRead*
618 // 623 //
619 // Read(): 624 // Read():
620 // CacheReadData* 625 // CacheReadData*
621 // 626 //
622 // 4. Cached entry, validation and replace (200): 627 // 4. Cached entry, validation and replace (200):
623 // Start(): 628 // Start():
624 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse* 629 // GetBackend* -> InitEntry -> OpenEntry* -> AddToEntry* -> CacheReadResponse*
625 // -> CacheDispatchValidation -> BeginPartialCacheValidation() -> 630 // -> CacheDispatchValidation -> BeginPartialCacheValidation() ->
626 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest -> 631 // BeginCacheValidation() -> SendRequest* -> SuccessfulSendRequest ->
627 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* -> 632 // OverwriteCachedResponse -> CacheWriteResponse* -> DoTruncateCachedData* ->
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 // 717 //
713 // 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:
714 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between 719 // Like examples 2-4, only CacheToggleUnusedSincePrefetch* is inserted between
715 // CacheReadResponse* and CacheDispatchValidation. 720 // CacheReadResponse* and CacheDispatchValidation.
716 int HttpCache::Transaction::DoLoop(int result) { 721 int HttpCache::Transaction::DoLoop(int result) {
717 DCHECK_NE(STATE_UNSET, next_state_); 722 DCHECK_NE(STATE_UNSET, next_state_);
718 DCHECK_NE(STATE_NONE, next_state_); 723 DCHECK_NE(STATE_NONE, next_state_);
719 DCHECK(!in_do_loop_); 724 DCHECK(!in_do_loop_);
720 725
721 int rv = result; 726 int rv = result;
727 State state = next_state_;
722 do { 728 do {
723 State state = next_state_; 729 state = next_state_;
724 next_state_ = STATE_UNSET; 730 next_state_ = STATE_UNSET;
725 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true); 731 base::AutoReset<bool> scoped_in_do_loop(&in_do_loop_, true);
726 732
727 switch (state) { 733 switch (state) {
728 case STATE_GET_BACKEND: 734 case STATE_GET_BACKEND:
729 DCHECK_EQ(OK, rv); 735 DCHECK_EQ(OK, rv);
730 rv = DoGetBackend(); 736 rv = DoGetBackend();
731 break; 737 break;
732 case STATE_GET_BACKEND_COMPLETE: 738 case STATE_GET_BACKEND_COMPLETE:
733 rv = DoGetBackendComplete(rv); 739 rv = DoGetBackendComplete(rv);
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
850 DCHECK_EQ(OK, rv); 856 DCHECK_EQ(OK, rv);
851 rv = DoPartialHeadersReceived(); 857 rv = DoPartialHeadersReceived();
852 break; 858 break;
853 case STATE_CACHE_READ_METADATA: 859 case STATE_CACHE_READ_METADATA:
854 DCHECK_EQ(OK, rv); 860 DCHECK_EQ(OK, rv);
855 rv = DoCacheReadMetadata(); 861 rv = DoCacheReadMetadata();
856 break; 862 break;
857 case STATE_CACHE_READ_METADATA_COMPLETE: 863 case STATE_CACHE_READ_METADATA_COMPLETE:
858 rv = DoCacheReadMetadataComplete(rv); 864 rv = DoCacheReadMetadataComplete(rv);
859 break; 865 break;
866 case STATE_FINISH_HEADERS:
867 DCHECK_EQ(OK, rv);
868 rv = DoFinishHeaders();
869 break;
870 case STATE_FINISH_HEADERS_COMPLETE:
871 rv = DoFinishHeadersComplete(rv);
872 break;
860 case STATE_NETWORK_READ: 873 case STATE_NETWORK_READ:
861 DCHECK_EQ(OK, rv); 874 DCHECK_EQ(OK, rv);
862 rv = DoNetworkRead(); 875 rv = DoNetworkRead();
863 break; 876 break;
864 case STATE_NETWORK_READ_COMPLETE: 877 case STATE_NETWORK_READ_COMPLETE:
865 rv = DoNetworkReadComplete(rv); 878 rv = DoNetworkReadComplete(rv);
866 break; 879 break;
867 case STATE_CACHE_READ_DATA: 880 case STATE_CACHE_READ_DATA:
868 DCHECK_EQ(OK, rv); 881 DCHECK_EQ(OK, rv);
869 rv = DoCacheReadData(); 882 rv = DoCacheReadData();
(...skipping 16 matching lines...) Expand all
886 break; 899 break;
887 default: 900 default:
888 NOTREACHED() << "bad state " << state; 901 NOTREACHED() << "bad state " << state;
889 rv = ERR_FAILED; 902 rv = ERR_FAILED;
890 break; 903 break;
891 } 904 }
892 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state; 905 DCHECK(next_state_ != STATE_UNSET) << "Previous state was " << state;
893 906
894 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); 907 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
895 908
909 // Assert Start() state machine's allowed last state in successful cases when
910 // caching is happening.
911 DCHECK(reading_ || rv != OK || !entry_ ||
912 state == STATE_FINISH_HEADERS_COMPLETE);
913
896 if (rv != ERR_IO_PENDING && !callback_.is_null()) { 914 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
897 read_buf_ = NULL; // Release the buffer before invoking the callback. 915 read_buf_ = nullptr; // Release the buffer before invoking the callback.
898 base::ResetAndReturn(&callback_).Run(rv); 916 base::ResetAndReturn(&callback_).Run(rv);
899 } 917 }
900 918
901 return rv; 919 return rv;
902 } 920 }
903 921
904 int HttpCache::Transaction::DoGetBackend() { 922 int HttpCache::Transaction::DoGetBackend() {
905 cache_pending_ = true; 923 cache_pending_ = true;
906 TransitionToState(STATE_GET_BACKEND_COMPLETE); 924 TransitionToState(STATE_GET_BACKEND_COMPLETE);
907 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); 925 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND);
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 partial_.reset(); 988 partial_.reset();
971 } 989 }
972 TransitionToState(STATE_SEND_REQUEST); 990 TransitionToState(STATE_SEND_REQUEST);
973 } else { 991 } else {
974 TransitionToState(STATE_INIT_ENTRY); 992 TransitionToState(STATE_INIT_ENTRY);
975 } 993 }
976 994
977 // This is only set if we have something to do with the response. 995 // This is only set if we have something to do with the response.
978 range_requested_ = (partial_.get() != NULL); 996 range_requested_ = (partial_.get() != NULL);
979 997
998 // mode_ may change later, save the initial mode in case we need to restart
999 // this request.
1000 original_mode_ = mode_;
980 return OK; 1001 return OK;
981 } 1002 }
982 1003
983 int HttpCache::Transaction::DoInitEntry() { 1004 int HttpCache::Transaction::DoInitEntry() {
984 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); 1005 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry");
985 DCHECK(!new_entry_); 1006 DCHECK(!new_entry_);
986 1007
987 if (!cache_.get()) { 1008 if (!cache_.get()) {
988 TransitionToState(STATE_NONE); 1009 TransitionToState(STATE_NONE);
989 return ERR_UNEXPECTED; 1010 return ERR_UNEXPECTED;
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
1347 TransitionToState(STATE_NONE); 1368 TransitionToState(STATE_NONE);
1348 return ERR_UNEXPECTED; 1369 return ERR_UNEXPECTED;
1349 } 1370 }
1350 1371
1351 return ValidateEntryHeadersAndContinue(); 1372 return ValidateEntryHeadersAndContinue();
1352 } 1373 }
1353 1374
1354 // We may end up here multiple times for a given request. 1375 // We may end up here multiple times for a given request.
1355 int HttpCache::Transaction::DoStartPartialCacheValidation() { 1376 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1356 if (mode_ == NONE) { 1377 if (mode_ == NONE) {
1357 TransitionToState(STATE_NONE); 1378 TransitionToState(STATE_FINISH_HEADERS);
1358 return OK; 1379 return OK;
1359 } 1380 }
1360 1381
1361 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); 1382 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION);
1362 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); 1383 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1363 } 1384 }
1364 1385
1365 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { 1386 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1366 if (!result) { 1387 if (!result) {
1367 // This is the end of the request. 1388 // This is the end of the request.
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1462 // We received the response headers and there is no error. 1483 // We received the response headers and there is no error.
1463 int HttpCache::Transaction::DoSuccessfulSendRequest() { 1484 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1464 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); 1485 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest");
1465 DCHECK(!new_response_); 1486 DCHECK(!new_response_);
1466 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); 1487 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1467 1488
1468 if (new_response->headers->response_code() == 401 || 1489 if (new_response->headers->response_code() == 401 ||
1469 new_response->headers->response_code() == 407) { 1490 new_response->headers->response_code() == 407) {
1470 SetAuthResponse(*new_response); 1491 SetAuthResponse(*new_response);
1471 if (!reading_) { 1492 if (!reading_) {
1472 TransitionToState(STATE_NONE); 1493 TransitionToState(STATE_FINISH_HEADERS);
1473 return OK; 1494 return OK;
1474 } 1495 }
1475 1496
1476 // We initiated a second request the caller doesn't know about. We should be 1497 // We initiated a second request the caller doesn't know about. We should be
1477 // able to authenticate this request because we should have authenticated 1498 // able to authenticate this request because we should have authenticated
1478 // this URL moments ago. 1499 // this URL moments ago.
1479 if (IsReadyToRestartForAuth()) { 1500 if (IsReadyToRestartForAuth()) {
1480 DCHECK(!response_.auth_challenge.get()); 1501 DCHECK(!response_.auth_challenge.get());
1481 TransitionToState(STATE_SEND_REQUEST_COMPLETE); 1502 TransitionToState(STATE_SEND_REQUEST_COMPLETE);
1482 // In theory we should check to see if there are new cookies, but there 1503 // In theory we should check to see if there are new cookies, but there
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
1525 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE); 1546 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE);
1526 } 1547 }
1527 1548
1528 // Invalidate any cached GET with a successful PUT or DELETE. 1549 // Invalidate any cached GET with a successful PUT or DELETE.
1529 if (mode_ == WRITE && 1550 if (mode_ == WRITE &&
1530 (request_->method == "PUT" || request_->method == "DELETE")) { 1551 (request_->method == "PUT" || request_->method == "DELETE")) {
1531 if (NonErrorResponse(new_response->headers->response_code())) { 1552 if (NonErrorResponse(new_response->headers->response_code())) {
1532 int ret = cache_->DoomEntry(cache_key_, NULL); 1553 int ret = cache_->DoomEntry(cache_key_, NULL);
1533 DCHECK_EQ(OK, ret); 1554 DCHECK_EQ(OK, ret);
1534 } 1555 }
1535 cache_->DoneWritingToEntry(entry_, true); 1556 cache_->DoneWritingToEntry(entry_, true, this);
1536 entry_ = NULL; 1557 entry_ = NULL;
1537 mode_ = NONE; 1558 mode_ = NONE;
1538 } 1559 }
1539 1560
1540 // Invalidate any cached GET with a successful POST. 1561 // Invalidate any cached GET with a successful POST.
1541 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && 1562 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1542 request_->method == "POST" && 1563 request_->method == "POST" &&
1543 NonErrorResponse(new_response->headers->response_code())) { 1564 NonErrorResponse(new_response->headers->response_code())) {
1544 cache_->DoomMainEntryForUrl(request_->url); 1565 cache_->DoomMainEntryForUrl(request_->url);
1545 } 1566 }
1546 1567
1547 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); 1568 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1548 1569
1549 if (new_response_->headers->response_code() == 416 && 1570 if (new_response_->headers->response_code() == 416 &&
1550 (request_->method == "GET" || request_->method == "POST")) { 1571 (request_->method == "GET" || request_->method == "POST")) {
1551 // If there is an active entry it may be destroyed with this transaction. 1572 // If there is an active entry it may be destroyed with this transaction.
1552 SetResponse(*new_response_); 1573 SetResponse(*new_response_);
1553 TransitionToState(STATE_NONE); 1574 TransitionToState(STATE_FINISH_HEADERS);
1554 return OK; 1575 return OK;
1555 } 1576 }
1556 1577
1557 // Are we expecting a response to a conditional query? 1578 // Are we expecting a response to a conditional query?
1558 if (mode_ == READ_WRITE || mode_ == UPDATE) { 1579 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1559 if (new_response->headers->response_code() == 304 || handling_206_) { 1580 if (new_response->headers->response_code() == 304 || handling_206_) {
1560 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); 1581 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED);
1561 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); 1582 TransitionToState(STATE_UPDATE_CACHED_RESPONSE);
1562 return OK; 1583 return OK;
1563 } 1584 }
1564 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); 1585 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED);
1565 mode_ = WRITE; 1586 mode_ = WRITE;
1566 } 1587 }
1567 1588
1568 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); 1589 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE);
1590
1591 if (!entry_)
1592 return OK;
1593
1594 // Invalidate any current entry with a successful response if this transaction
1595 // cannot write to this entry.
1596 if (new_response->headers->response_code() != 304 &&
1597 (entry_->writer || !entry_->readers.empty())) {
1598 DCHECK_EQ(entry_->headers_transaction, this);
1599 cache_->DoneResponseHeaders(entry_, this, false);
1600 entry_ = nullptr;
1601 mode_ = NONE;
1602 return OK;
1603 }
1604
1569 return OK; 1605 return OK;
1570 } 1606 }
1571 1607
1572 // We received 304 or 206 and we want to update the cached response headers. 1608 // We received 304 or 206 and we want to update the cached response headers.
1573 int HttpCache::Transaction::DoUpdateCachedResponse() { 1609 int HttpCache::Transaction::DoUpdateCachedResponse() {
1574 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); 1610 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse");
1575 int rv = OK; 1611 int rv = OK;
1576 // Update the cached response based on the headers and properties of 1612 // Update the cached response based on the headers and properties of
1577 // new_response_. 1613 // new_response_.
1578 response_.headers->Update(*new_response_->headers.get()); 1614 response_.headers->Update(*new_response_->headers.get());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1631 DCHECK(!handling_206_); 1667 DCHECK(!handling_206_);
1632 // We got a "not modified" response and already updated the corresponding 1668 // We got a "not modified" response and already updated the corresponding
1633 // cache entry above. 1669 // cache entry above.
1634 // 1670 //
1635 // By closing the cached entry now, we make sure that the 304 rather than 1671 // By closing the cached entry now, we make sure that the 304 rather than
1636 // the cached 200 response, is what will be returned to the user. 1672 // the cached 200 response, is what will be returned to the user.
1637 DoneWritingToEntry(true); 1673 DoneWritingToEntry(true);
1638 } else if (entry_ && !handling_206_) { 1674 } else if (entry_ && !handling_206_) {
1639 DCHECK_EQ(READ_WRITE, mode_); 1675 DCHECK_EQ(READ_WRITE, mode_);
1640 if (!partial_ || partial_->IsLastRange()) { 1676 if (!partial_ || partial_->IsLastRange()) {
1641 cache_->ConvertWriterToReader(entry_);
1642 mode_ = READ; 1677 mode_ = READ;
1643 } 1678 }
1644 // We no longer need the network transaction, so destroy it. 1679 // We no longer need the network transaction, so destroy it.
1645 ResetNetworkTransaction(); 1680 ResetNetworkTransaction();
1646 } else if (entry_ && handling_206_ && truncated_ && 1681 } else if (entry_ && handling_206_ && truncated_ &&
1647 partial_->initial_validation()) { 1682 partial_->initial_validation()) {
1648 // We just finished the validation of a truncated entry, and the server 1683 // We just finished the validation of a truncated entry, and the server
1649 // is willing to resume the operation. Now we go back and start serving 1684 // is willing to resume the operation. Now we go back and start serving
1650 // the first part to the user. 1685 // the first part to the user.
1651 ResetNetworkTransaction(); 1686 ResetNetworkTransaction();
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1751 } 1786 }
1752 1787
1753 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1788 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1754 return OK; 1789 return OK;
1755 } 1790 }
1756 1791
1757 int HttpCache::Transaction::DoPartialHeadersReceived() { 1792 int HttpCache::Transaction::DoPartialHeadersReceived() {
1758 new_response_ = NULL; 1793 new_response_ = NULL;
1759 1794
1760 if (!partial_) { 1795 if (!partial_) {
1761 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) 1796 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) {
1762 TransitionToState(STATE_CACHE_READ_METADATA); 1797 TransitionToState(STATE_CACHE_READ_METADATA);
1763 else 1798 } else {
1764 TransitionToState(STATE_NONE); 1799 DCHECK(!reading_);
1800 TransitionToState(STATE_FINISH_HEADERS);
1801 }
1765 return OK; 1802 return OK;
1766 } 1803 }
1767 1804
1768 if (reading_) { 1805 if (reading_) {
1769 if (network_trans_.get()) { 1806 if (network_trans_.get()) {
1770 TransitionToState(STATE_NETWORK_READ); 1807 TransitionToState(STATE_NETWORK_READ);
1771 } else { 1808 } else {
1772 TransitionToState(STATE_CACHE_READ_DATA); 1809 TransitionToState(STATE_CACHE_READ_DATA);
1773 } 1810 }
1774 } else if (mode_ != NONE) { 1811 } else if (mode_ != NONE) {
1775 // We are about to return the headers for a byte-range request to the user, 1812 // We are about to return the headers for a byte-range request to the user,
1776 // so let's fix them. 1813 // so let's fix them.
1777 partial_->FixResponseHeaders(response_.headers.get(), true); 1814 partial_->FixResponseHeaders(response_.headers.get(), true);
1778 TransitionToState(STATE_NONE); 1815 TransitionToState(STATE_FINISH_HEADERS);
1779 } else { 1816 } else {
1780 TransitionToState(STATE_NONE); 1817 TransitionToState(STATE_FINISH_HEADERS);
1781 } 1818 }
1782 return OK; 1819 return OK;
1783 } 1820 }
1784 1821
1822 int HttpCache::Transaction::DoFinishHeaders() {
1823 if (!entry_) {
1824 TransitionToState(STATE_NONE);
1825 return OK;
1826 }
1827
1828 TransitionToState(STATE_FINISH_HEADERS_COMPLETE);
1829
1830 // If it was an auth failure or 416, this transaction should continue to be
1831 // headers_transaction till consumer takes an action, so no need to do
1832 // anything now.
1833 if (auth_response_.headers.get() ||
jkarlin 2017/03/30 17:04:54 Do we need this check? I'd rather we went through
shivanisha 2017/03/31 17:47:06 This check is to do with the fact that this transa
1834 (new_response_ && new_response_->headers &&
1835 new_response_->headers->response_code() == 416))
1836 return OK;
1837
1838 // If there is no response body to be written or read, it does not need to
1839 // wait.
1840 if (request_->method == "HEAD")
1841 return OK;
jkarlin 2017/03/30 17:04:54 Do we need this check? I'd rather we went through
shivanisha 2017/03/31 17:47:06 We need this check because these requests should n
1842
1843 return cache_->DoneResponseHeaders(entry_, this, true);
1844
1845 return OK;
1846 }
1847
1848 int HttpCache::Transaction::DoFinishHeadersComplete(int rv) {
1849 if (rv == ERR_CACHE_RACE) {
1850 RestartAfterValidationStarted();
1851 return OK;
1852 }
1853
1854 TransitionToState(STATE_NONE);
1855 return rv;
1856 }
1857
1858 void HttpCache::Transaction::RestartAfterValidationStarted() {
1859 DCHECK(!reading_);
1860 next_state_ = STATE_INIT_ENTRY;
1861 cache_entry_status_ = CacheEntryStatus::ENTRY_UNDEFINED;
1862 entry_ = nullptr;
1863 mode_ = original_mode_;
1864 if (network_trans_)
1865 network_trans_.reset();
1866 }
1867
1868 void HttpCache::Transaction::ConvertToReadMode() {
1869 mode_ = READ;
jkarlin 2017/03/30 17:04:54 DCHECK that we're in a safe state to call ConvertT
shivanisha 2017/03/31 17:47:06 Added DCHECK(!reading_)
1870 if (network_trans_)
1871 ResetNetworkTransaction();
1872 }
1873
1785 int HttpCache::Transaction::DoCacheReadMetadata() { 1874 int HttpCache::Transaction::DoCacheReadMetadata() {
1786 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); 1875 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata");
1787 DCHECK(entry_); 1876 DCHECK(entry_);
1788 DCHECK(!response_.metadata.get()); 1877 DCHECK(!response_.metadata.get());
1789 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); 1878 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE);
1790 1879
1791 response_.metadata = 1880 response_.metadata =
1792 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); 1881 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1793 1882
1794 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); 1883 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO);
1795 return entry_->disk_entry->ReadData(kMetadataIndex, 0, 1884 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1796 response_.metadata.get(), 1885 response_.metadata.get(),
1797 response_.metadata->size(), 1886 response_.metadata->size(),
1798 io_callback_); 1887 io_callback_);
1799 } 1888 }
1800 1889
1801 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { 1890 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1802 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); 1891 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete");
1803 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, 1892 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO,
1804 result); 1893 result);
1805 if (result != response_.metadata->size()) 1894 if (result != response_.metadata->size())
1806 return OnCacheReadError(result, false); 1895 return OnCacheReadError(result, false);
1807 TransitionToState(STATE_NONE); 1896
1897 TransitionToState(STATE_FINISH_HEADERS);
1808 return OK; 1898 return OK;
1809 } 1899 }
1810 1900
1811 int HttpCache::Transaction::DoNetworkRead() { 1901 int HttpCache::Transaction::DoNetworkRead() {
1812 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); 1902 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead");
1813 TransitionToState(STATE_NETWORK_READ_COMPLETE); 1903 TransitionToState(STATE_NETWORK_READ_COMPLETE);
1814 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); 1904 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1815 } 1905 }
1816 1906
1817 int HttpCache::Transaction::DoNetworkReadComplete(int result) { 1907 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
2115 TransitionToState(STATE_NONE); 2205 TransitionToState(STATE_NONE);
2116 return ERR_CACHE_MISS; 2206 return ERR_CACHE_MISS;
2117 } 2207 }
2118 2208
2119 if (request_->method == "HEAD") 2209 if (request_->method == "HEAD")
2120 FixHeadersForHead(); 2210 FixHeadersForHead();
2121 2211
2122 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2212 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2123 TransitionToState(STATE_CACHE_READ_METADATA); 2213 TransitionToState(STATE_CACHE_READ_METADATA);
2124 else 2214 else
2125 TransitionToState(STATE_NONE); 2215 TransitionToState(STATE_FINISH_HEADERS);
2126 2216
2127 return OK; 2217 return OK;
2128 } 2218 }
2129 2219
2130 int HttpCache::Transaction::BeginCacheValidation() { 2220 int HttpCache::Transaction::BeginCacheValidation() {
2131 DCHECK_EQ(mode_, READ_WRITE); 2221 DCHECK_EQ(mode_, READ_WRITE);
2132 2222
2133 ValidationType required_validation = RequiresValidation(); 2223 ValidationType required_validation = RequiresValidation();
2134 2224
2135 bool skip_validation = (required_validation == VALIDATION_NONE); 2225 bool skip_validation = (required_validation == VALIDATION_NONE);
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
2628 if (partial_) { 2718 if (partial_) {
2629 if (truncated_ || is_sparse_ || !invalid_range_) { 2719 if (truncated_ || is_sparse_ || !invalid_range_) {
2630 // We are going to return the saved response headers to the caller, so 2720 // We are going to return the saved response headers to the caller, so
2631 // we may need to adjust them first. 2721 // we may need to adjust them first.
2632 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 2722 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
2633 return OK; 2723 return OK;
2634 } else { 2724 } else {
2635 partial_.reset(); 2725 partial_.reset();
2636 } 2726 }
2637 } 2727 }
2638 cache_->ConvertWriterToReader(entry_); 2728
2639 mode_ = READ; 2729 mode_ = READ;
2640 2730
2641 if (request_->method == "HEAD") 2731 if (request_->method == "HEAD")
2642 FixHeadersForHead(); 2732 FixHeadersForHead();
2643 2733
2644 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2734 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2645 TransitionToState(STATE_CACHE_READ_METADATA); 2735 TransitionToState(STATE_CACHE_READ_METADATA);
2646 else 2736 else
2647 TransitionToState(STATE_NONE); 2737 TransitionToState(STATE_FINISH_HEADERS);
2648 return OK; 2738 return OK;
2649 } 2739 }
2650 2740
2651 int HttpCache::Transaction::WriteToEntry(int index, int offset, 2741 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2652 IOBuffer* data, int data_len, 2742 IOBuffer* data, int data_len,
2653 const CompletionCallback& callback) { 2743 const CompletionCallback& callback) {
2654 if (!entry_) 2744 if (!entry_)
2655 return data_len; 2745 return data_len;
2656 2746
2657 int rv = 0; 2747 int rv = 0;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2716 } 2806 }
2717 return OK; 2807 return OK;
2718 } 2808 }
2719 2809
2720 void HttpCache::Transaction::DoneWritingToEntry(bool success) { 2810 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2721 if (!entry_) 2811 if (!entry_)
2722 return; 2812 return;
2723 2813
2724 RecordHistograms(); 2814 RecordHistograms();
2725 2815
2726 cache_->DoneWritingToEntry(entry_, success); 2816 cache_->DoneWritingToEntry(entry_, success, this);
2727 entry_ = NULL; 2817 entry_ = NULL;
2728 mode_ = NONE; // switch to 'pass through' mode 2818 mode_ = NONE; // switch to 'pass through' mode
2729 } 2819 }
2730 2820
2731 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { 2821 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2732 DLOG(ERROR) << "ReadData failed: " << result; 2822 DLOG(ERROR) << "ReadData failed: " << result;
2733 const int result_for_histogram = std::max(0, -result); 2823 const int result_for_histogram = std::max(0, -result);
2734 if (restart) { 2824 if (restart) {
2735 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", 2825 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2736 result_for_histogram); 2826 result_for_histogram);
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
3073 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated", 3163 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
3074 before_send_sample); 3164 before_send_sample);
3075 break; 3165 break;
3076 } 3166 }
3077 default: 3167 default:
3078 NOTREACHED(); 3168 NOTREACHED();
3079 } 3169 }
3080 } 3170 }
3081 3171
3082 void HttpCache::Transaction::OnIOComplete(int result) { 3172 void HttpCache::Transaction::OnIOComplete(int result) {
3173 // If its the Start state machine and it cannot proceed due to failure by
3174 // another transaction in writing the response, restart this transaction.
3175 if (validating_cannot_proceed_) {
3176 DCHECK(!reading_);
3177 validating_cannot_proceed_ = false;
3178 RestartAfterValidationStarted();
3179 }
3180
3083 DoLoop(result); 3181 DoLoop(result);
3084 } 3182 }
3085 3183
3086 void HttpCache::Transaction::TransitionToState(State state) { 3184 void HttpCache::Transaction::TransitionToState(State state) {
3087 // Ensure that the state is only set once per Do* state. 3185 // Ensure that the state is only set once per Do* state.
3088 DCHECK(in_do_loop_); 3186 DCHECK(in_do_loop_);
3089 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; 3187 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state;
3090 next_state_ = state; 3188 next_state_ = state;
3091 } 3189 }
3092 3190
3093 } // namespace net 3191 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698