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

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

Issue 2721933002: HttpCache::Transaction layer allowing parallel validation (Closed)
Patch Set: HttpCache::IsCancelResponseBody added. 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*
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_WAIT_BEFORE_READ:
867 DCHECK_EQ(OK, rv);
868 rv = DoWaitBeforeRead();
869 break;
870 case STATE_WAIT_BEFORE_READ_COMPLETE:
871 rv = DoWaitBeforeReadComplete(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_WAIT_BEFORE_READ_COMPLETE);
913
914 if (!reading_ && rv == OK) {
915 DCHECK(!entry_ || state == STATE_WAIT_BEFORE_READ_COMPLETE);
916 }
917
896 if (rv != ERR_IO_PENDING && !callback_.is_null()) { 918 if (rv != ERR_IO_PENDING && !callback_.is_null()) {
897 read_buf_ = NULL; // Release the buffer before invoking the callback. 919 read_buf_ = nullptr; // Release the buffer before invoking the callback.
898 base::ResetAndReturn(&callback_).Run(rv); 920 base::ResetAndReturn(&callback_).Run(rv);
899 } 921 }
900 922
901 return rv; 923 return rv;
902 } 924 }
903 925
904 int HttpCache::Transaction::DoGetBackend() { 926 int HttpCache::Transaction::DoGetBackend() {
905 cache_pending_ = true; 927 cache_pending_ = true;
906 TransitionToState(STATE_GET_BACKEND_COMPLETE); 928 TransitionToState(STATE_GET_BACKEND_COMPLETE);
907 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND); 929 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_GET_BACKEND);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
969 partial_->RestoreHeaders(&custom_request_->extra_headers); 991 partial_->RestoreHeaders(&custom_request_->extra_headers);
970 partial_.reset(); 992 partial_.reset();
971 } 993 }
972 TransitionToState(STATE_SEND_REQUEST); 994 TransitionToState(STATE_SEND_REQUEST);
973 } else { 995 } else {
974 TransitionToState(STATE_INIT_ENTRY); 996 TransitionToState(STATE_INIT_ENTRY);
975 } 997 }
976 998
977 // This is only set if we have something to do with the response. 999 // This is only set if we have something to do with the response.
978 range_requested_ = (partial_.get() != NULL); 1000 range_requested_ = (partial_.get() != NULL);
979 1001 original_mode_ = mode_;
jkarlin 2017/03/30 14:25:26 There should be a newline above this as it doesn't
shivanisha 2017/03/30 16:38:41 Done. Also added a comment on why we need original
980 return OK; 1002 return OK;
981 } 1003 }
982 1004
983 int HttpCache::Transaction::DoInitEntry() { 1005 int HttpCache::Transaction::DoInitEntry() {
984 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry"); 1006 TRACE_EVENT0("io", "HttpCacheTransaction::DoInitEntry");
985 DCHECK(!new_entry_); 1007 DCHECK(!new_entry_);
986 1008
987 if (!cache_.get()) { 1009 if (!cache_.get()) {
988 TransitionToState(STATE_NONE); 1010 TransitionToState(STATE_NONE);
989 return ERR_UNEXPECTED; 1011 return ERR_UNEXPECTED;
(...skipping 357 matching lines...) Expand 10 before | Expand all | Expand 10 after
1347 TransitionToState(STATE_NONE); 1369 TransitionToState(STATE_NONE);
1348 return ERR_UNEXPECTED; 1370 return ERR_UNEXPECTED;
1349 } 1371 }
1350 1372
1351 return ValidateEntryHeadersAndContinue(); 1373 return ValidateEntryHeadersAndContinue();
1352 } 1374 }
1353 1375
1354 // We may end up here multiple times for a given request. 1376 // We may end up here multiple times for a given request.
1355 int HttpCache::Transaction::DoStartPartialCacheValidation() { 1377 int HttpCache::Transaction::DoStartPartialCacheValidation() {
1356 if (mode_ == NONE) { 1378 if (mode_ == NONE) {
1357 TransitionToState(STATE_NONE); 1379 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
jkarlin 2017/03/30 14:25:26 Is this use of reading_ really a check to see if t
shivanisha 2017/03/30 16:38:41 Done.
1358 return OK; 1380 return OK;
1359 } 1381 }
1360 1382
1361 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION); 1383 TransitionToState(STATE_COMPLETE_PARTIAL_CACHE_VALIDATION);
1362 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_); 1384 return partial_->ShouldValidateCache(entry_->disk_entry, io_callback_);
1363 } 1385 }
1364 1386
1365 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) { 1387 int HttpCache::Transaction::DoCompletePartialCacheValidation(int result) {
1366 if (!result) { 1388 if (!result) {
1367 // This is the end of the request. 1389 // 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. 1484 // We received the response headers and there is no error.
1463 int HttpCache::Transaction::DoSuccessfulSendRequest() { 1485 int HttpCache::Transaction::DoSuccessfulSendRequest() {
1464 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest"); 1486 TRACE_EVENT0("io", "HttpCacheTransaction::DoSuccessfulSendRequest");
1465 DCHECK(!new_response_); 1487 DCHECK(!new_response_);
1466 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo(); 1488 const HttpResponseInfo* new_response = network_trans_->GetResponseInfo();
1467 1489
1468 if (new_response->headers->response_code() == 401 || 1490 if (new_response->headers->response_code() == 401 ||
1469 new_response->headers->response_code() == 407) { 1491 new_response->headers->response_code() == 407) {
1470 SetAuthResponse(*new_response); 1492 SetAuthResponse(*new_response);
1471 if (!reading_) { 1493 if (!reading_) {
1472 TransitionToState(STATE_NONE); 1494 TransitionToState(STATE_WAIT_BEFORE_READ);
1473 return OK; 1495 return OK;
1474 } 1496 }
1475 1497
1476 // We initiated a second request the caller doesn't know about. We should be 1498 // 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 1499 // able to authenticate this request because we should have authenticated
1478 // this URL moments ago. 1500 // this URL moments ago.
1479 if (IsReadyToRestartForAuth()) { 1501 if (IsReadyToRestartForAuth()) {
1480 DCHECK(!response_.auth_challenge.get()); 1502 DCHECK(!response_.auth_challenge.get());
1481 TransitionToState(STATE_SEND_REQUEST_COMPLETE); 1503 TransitionToState(STATE_SEND_REQUEST_COMPLETE);
1482 // In theory we should check to see if there are new cookies, but there 1504 // 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); 1547 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_NOT_IN_CACHE);
1526 } 1548 }
1527 1549
1528 // Invalidate any cached GET with a successful PUT or DELETE. 1550 // Invalidate any cached GET with a successful PUT or DELETE.
1529 if (mode_ == WRITE && 1551 if (mode_ == WRITE &&
1530 (request_->method == "PUT" || request_->method == "DELETE")) { 1552 (request_->method == "PUT" || request_->method == "DELETE")) {
1531 if (NonErrorResponse(new_response->headers->response_code())) { 1553 if (NonErrorResponse(new_response->headers->response_code())) {
1532 int ret = cache_->DoomEntry(cache_key_, NULL); 1554 int ret = cache_->DoomEntry(cache_key_, NULL);
1533 DCHECK_EQ(OK, ret); 1555 DCHECK_EQ(OK, ret);
1534 } 1556 }
1535 cache_->DoneWritingToEntry(entry_, true); 1557 cache_->DoneWritingToEntry(entry_, true, this);
1536 entry_ = NULL; 1558 entry_ = NULL;
1537 mode_ = NONE; 1559 mode_ = NONE;
1538 } 1560 }
1539 1561
1540 // Invalidate any cached GET with a successful POST. 1562 // Invalidate any cached GET with a successful POST.
1541 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) && 1563 if (!(effective_load_flags_ & LOAD_DISABLE_CACHE) &&
1542 request_->method == "POST" && 1564 request_->method == "POST" &&
1543 NonErrorResponse(new_response->headers->response_code())) { 1565 NonErrorResponse(new_response->headers->response_code())) {
1544 cache_->DoomMainEntryForUrl(request_->url); 1566 cache_->DoomMainEntryForUrl(request_->url);
1545 } 1567 }
1546 1568
1547 RecordNoStoreHeaderHistogram(request_->load_flags, new_response); 1569 RecordNoStoreHeaderHistogram(request_->load_flags, new_response);
1548 1570
1549 if (new_response_->headers->response_code() == 416 && 1571 if (new_response_->headers->response_code() == 416 &&
1550 (request_->method == "GET" || request_->method == "POST")) { 1572 (request_->method == "GET" || request_->method == "POST")) {
1551 // If there is an active entry it may be destroyed with this transaction. 1573 // If there is an active entry it may be destroyed with this transaction.
1552 SetResponse(*new_response_); 1574 SetResponse(*new_response_);
1553 TransitionToState(STATE_NONE); 1575 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
1554 return OK; 1576 return OK;
1555 } 1577 }
1556 1578
1557 // Are we expecting a response to a conditional query? 1579 // Are we expecting a response to a conditional query?
1558 if (mode_ == READ_WRITE || mode_ == UPDATE) { 1580 if (mode_ == READ_WRITE || mode_ == UPDATE) {
1559 if (new_response->headers->response_code() == 304 || handling_206_) { 1581 if (new_response->headers->response_code() == 304 || handling_206_) {
1560 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED); 1582 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_VALIDATED);
1561 TransitionToState(STATE_UPDATE_CACHED_RESPONSE); 1583 TransitionToState(STATE_UPDATE_CACHED_RESPONSE);
1562 return OK; 1584 return OK;
1563 } 1585 }
1564 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED); 1586 UpdateCacheEntryStatus(CacheEntryStatus::ENTRY_UPDATED);
1565 mode_ = WRITE; 1587 mode_ = WRITE;
1566 } 1588 }
1567 1589
1568 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE); 1590 TransitionToState(STATE_OVERWRITE_CACHED_RESPONSE);
1591
1592 if (!entry_)
1593 return OK;
1594
1595 // Invalidate any current entry with a successful response if this transaction
1596 // cannot write to this entry.
1597 if (new_response->headers->response_code() != 304 &&
1598 (entry_->writer || !entry_->readers.empty())) {
1599 DCHECK_EQ(entry_->headers_transaction, this);
1600 cache_->DoneResponseHeaders(entry_, this, false);
1601 entry_ = nullptr;
1602 mode_ = NONE;
1603 return OK;
1604 }
1605
1569 return OK; 1606 return OK;
1570 } 1607 }
1571 1608
1572 // We received 304 or 206 and we want to update the cached response headers. 1609 // We received 304 or 206 and we want to update the cached response headers.
1573 int HttpCache::Transaction::DoUpdateCachedResponse() { 1610 int HttpCache::Transaction::DoUpdateCachedResponse() {
1574 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse"); 1611 TRACE_EVENT0("io", "HttpCacheTransaction::DoUpdateCachedResponse");
1575 int rv = OK; 1612 int rv = OK;
1576 // Update the cached response based on the headers and properties of 1613 // Update the cached response based on the headers and properties of
1577 // new_response_. 1614 // new_response_.
1578 response_.headers->Update(*new_response_->headers.get()); 1615 response_.headers->Update(*new_response_->headers.get());
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1631 DCHECK(!handling_206_); 1668 DCHECK(!handling_206_);
1632 // We got a "not modified" response and already updated the corresponding 1669 // We got a "not modified" response and already updated the corresponding
1633 // cache entry above. 1670 // cache entry above.
1634 // 1671 //
1635 // By closing the cached entry now, we make sure that the 304 rather than 1672 // 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. 1673 // the cached 200 response, is what will be returned to the user.
1637 DoneWritingToEntry(true); 1674 DoneWritingToEntry(true);
1638 } else if (entry_ && !handling_206_) { 1675 } else if (entry_ && !handling_206_) {
1639 DCHECK_EQ(READ_WRITE, mode_); 1676 DCHECK_EQ(READ_WRITE, mode_);
1640 if (!partial_ || partial_->IsLastRange()) { 1677 if (!partial_ || partial_->IsLastRange()) {
1641 cache_->ConvertWriterToReader(entry_);
1642 mode_ = READ; 1678 mode_ = READ;
1643 } 1679 }
1644 // We no longer need the network transaction, so destroy it. 1680 // We no longer need the network transaction, so destroy it.
1645 ResetNetworkTransaction(); 1681 ResetNetworkTransaction();
1646 } else if (entry_ && handling_206_ && truncated_ && 1682 } else if (entry_ && handling_206_ && truncated_ &&
1647 partial_->initial_validation()) { 1683 partial_->initial_validation()) {
1648 // We just finished the validation of a truncated entry, and the server 1684 // 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 1685 // is willing to resume the operation. Now we go back and start serving
1650 // the first part to the user. 1686 // the first part to the user.
1651 ResetNetworkTransaction(); 1687 ResetNetworkTransaction();
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
1751 } 1787 }
1752 1788
1753 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 1789 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
1754 return OK; 1790 return OK;
1755 } 1791 }
1756 1792
1757 int HttpCache::Transaction::DoPartialHeadersReceived() { 1793 int HttpCache::Transaction::DoPartialHeadersReceived() {
1758 new_response_ = NULL; 1794 new_response_ = NULL;
1759 1795
1760 if (!partial_) { 1796 if (!partial_) {
1761 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) 1797 if (entry_ && entry_->disk_entry->GetDataSize(kMetadataIndex)) {
1762 TransitionToState(STATE_CACHE_READ_METADATA); 1798 TransitionToState(STATE_CACHE_READ_METADATA);
1763 else 1799 } else {
1764 TransitionToState(STATE_NONE); 1800 DCHECK(!reading_);
1801 TransitionToState(STATE_WAIT_BEFORE_READ);
1802 }
1765 return OK; 1803 return OK;
1766 } 1804 }
1767 1805
1768 if (reading_) { 1806 if (reading_) {
1769 if (network_trans_.get()) { 1807 if (network_trans_.get()) {
1770 TransitionToState(STATE_NETWORK_READ); 1808 TransitionToState(STATE_NETWORK_READ);
1771 } else { 1809 } else {
1772 TransitionToState(STATE_CACHE_READ_DATA); 1810 TransitionToState(STATE_CACHE_READ_DATA);
1773 } 1811 }
1774 } else if (mode_ != NONE) { 1812 } else if (mode_ != NONE) {
1775 // We are about to return the headers for a byte-range request to the user, 1813 // We are about to return the headers for a byte-range request to the user,
1776 // so let's fix them. 1814 // so let's fix them.
1777 partial_->FixResponseHeaders(response_.headers.get(), true); 1815 partial_->FixResponseHeaders(response_.headers.get(), true);
1778 TransitionToState(STATE_NONE); 1816 TransitionToState(STATE_WAIT_BEFORE_READ);
1779 } else { 1817 } else {
1780 TransitionToState(STATE_NONE); 1818 TransitionToState(STATE_WAIT_BEFORE_READ);
1781 } 1819 }
1782 return OK; 1820 return OK;
1783 } 1821 }
1784 1822
1823 int HttpCache::Transaction::DoWaitBeforeRead() {
1824 if (!entry_) {
1825 TransitionToState(STATE_NONE);
1826 return OK;
1827 }
1828
1829 // A transaction comes here between the completion of headers phase and the
1830 // start of response body phase, thus it should never be the active writer at
1831 // this point.
1832 DCHECK_NE(entry_->writer, this);
jkarlin 2017/03/30 14:25:26 The Transaction should be unaware of its role in t
shivanisha 2017/03/30 16:38:41 Removed , also because now we are allowing writers
1833
1834 TransitionToState(STATE_WAIT_BEFORE_READ_COMPLETE);
1835
1836 // If it was an auth failure or 416, this transaction should continue to be
1837 // headers_transaction till consumer takes an action, so no need to do
1838 // anything now.
1839 if (auth_response_.headers.get() ||
1840 (new_response_ && new_response_->headers &&
1841 new_response_->headers->response_code() == 416))
1842 return OK;
1843
1844 // If there is no response body to be written or read, it does not need to
1845 // wait.
1846 if (request_->method == "HEAD")
1847 return OK;
1848
1849 // Proceed only if |this| completed the headers phase. For read-only
1850 // transactions since they do not have a headers phase, they would have
1851 // already been added to entry_->readers, so do nothing.
1852 if (entry_->headers_transaction == this)
jkarlin 2017/03/30 14:25:26 The Transaction should be unaware of its role in t
shivanisha 2017/03/30 16:38:41 Done
shivanisha 2017/03/30 16:45:59 We cannot check mode to be read-only to imply that
1853 return cache_->DoneResponseHeaders(entry_, this, true);
1854
1855 return OK;
1856 }
1857
1858 int HttpCache::Transaction::DoWaitBeforeReadComplete(int rv) {
1859 if (rv == ERR_CACHE_RACE) {
1860 RestartAfterValidationStarted();
1861 return OK;
1862 }
1863
1864 TransitionToState(STATE_NONE);
1865 if (rv != OK)
1866 return rv;
1867
jkarlin 2017/03/30 14:25:26 It looks like at this point all paths lead to retu
shivanisha 2017/03/30 16:45:59 Done. Added a function ConvertToReadMode to be inv
1868 // If successful then this must be either be the writer or a reader as the
1869 // response must have completely been written to the cache.
1870 if (entry_->writer == this)
1871 return OK;
1872
1873 // If it was an auth failure or 416, this transaction should continue to be
1874 // headers_transaction, so no need to do anything now.
1875 if (auth_response_.headers.get() ||
1876 (new_response_ && new_response_->headers &&
1877 new_response_->headers->response_code() == 416)) {
1878 DCHECK_EQ(entry_->headers_transaction, this);
1879 return OK;
1880 }
1881
1882 mode_ = READ;
1883 if (network_trans_)
1884 ResetNetworkTransaction();
1885 return OK;
1886 }
1887
1888 void HttpCache::Transaction::RestartAfterValidationStarted() {
1889 DCHECK(!reading_);
1890 next_state_ = STATE_INIT_ENTRY;
1891 cache_entry_status_ = CacheEntryStatus::ENTRY_UNDEFINED;
1892 entry_ = nullptr;
1893 mode_ = original_mode_;
1894 if (network_trans_)
1895 network_trans_.reset();
1896 }
1897
1785 int HttpCache::Transaction::DoCacheReadMetadata() { 1898 int HttpCache::Transaction::DoCacheReadMetadata() {
1786 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata"); 1899 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadata");
1787 DCHECK(entry_); 1900 DCHECK(entry_);
1788 DCHECK(!response_.metadata.get()); 1901 DCHECK(!response_.metadata.get());
1789 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE); 1902 TransitionToState(STATE_CACHE_READ_METADATA_COMPLETE);
1790 1903
1791 response_.metadata = 1904 response_.metadata =
1792 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); 1905 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex));
1793 1906
1794 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO); 1907 net_log_.BeginEvent(NetLogEventType::HTTP_CACHE_READ_INFO);
1795 return entry_->disk_entry->ReadData(kMetadataIndex, 0, 1908 return entry_->disk_entry->ReadData(kMetadataIndex, 0,
1796 response_.metadata.get(), 1909 response_.metadata.get(),
1797 response_.metadata->size(), 1910 response_.metadata->size(),
1798 io_callback_); 1911 io_callback_);
1799 } 1912 }
1800 1913
1801 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { 1914 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) {
1802 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete"); 1915 TRACE_EVENT0("io", "HttpCacheTransaction::DoCacheReadMetadataComplete");
1803 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO, 1916 net_log_.EndEventWithNetErrorCode(NetLogEventType::HTTP_CACHE_READ_INFO,
1804 result); 1917 result);
1805 if (result != response_.metadata->size()) 1918 if (result != response_.metadata->size())
1806 return OnCacheReadError(result, false); 1919 return OnCacheReadError(result, false);
1807 TransitionToState(STATE_NONE); 1920
1921 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
1808 return OK; 1922 return OK;
1809 } 1923 }
1810 1924
1811 int HttpCache::Transaction::DoNetworkRead() { 1925 int HttpCache::Transaction::DoNetworkRead() {
1812 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead"); 1926 TRACE_EVENT0("io", "HttpCacheTransaction::DoNetworkRead");
1813 TransitionToState(STATE_NETWORK_READ_COMPLETE); 1927 TransitionToState(STATE_NETWORK_READ_COMPLETE);
1814 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_); 1928 return network_trans_->Read(read_buf_.get(), io_buf_len_, io_callback_);
1815 } 1929 }
1816 1930
1817 int HttpCache::Transaction::DoNetworkReadComplete(int result) { 1931 int HttpCache::Transaction::DoNetworkReadComplete(int result) {
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
2115 TransitionToState(STATE_NONE); 2229 TransitionToState(STATE_NONE);
2116 return ERR_CACHE_MISS; 2230 return ERR_CACHE_MISS;
2117 } 2231 }
2118 2232
2119 if (request_->method == "HEAD") 2233 if (request_->method == "HEAD")
2120 FixHeadersForHead(); 2234 FixHeadersForHead();
2121 2235
2122 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2236 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2123 TransitionToState(STATE_CACHE_READ_METADATA); 2237 TransitionToState(STATE_CACHE_READ_METADATA);
2124 else 2238 else
2125 TransitionToState(STATE_NONE); 2239 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
2126 2240
2127 return OK; 2241 return OK;
2128 } 2242 }
2129 2243
2130 int HttpCache::Transaction::BeginCacheValidation() { 2244 int HttpCache::Transaction::BeginCacheValidation() {
2131 DCHECK_EQ(mode_, READ_WRITE); 2245 DCHECK_EQ(mode_, READ_WRITE);
2132 2246
2133 ValidationType required_validation = RequiresValidation(); 2247 ValidationType required_validation = RequiresValidation();
2134 2248
2135 bool skip_validation = (required_validation == VALIDATION_NONE); 2249 bool skip_validation = (required_validation == VALIDATION_NONE);
(...skipping 492 matching lines...) Expand 10 before | Expand all | Expand 10 after
2628 if (partial_) { 2742 if (partial_) {
2629 if (truncated_ || is_sparse_ || !invalid_range_) { 2743 if (truncated_ || is_sparse_ || !invalid_range_) {
2630 // We are going to return the saved response headers to the caller, so 2744 // We are going to return the saved response headers to the caller, so
2631 // we may need to adjust them first. 2745 // we may need to adjust them first.
2632 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED); 2746 TransitionToState(STATE_PARTIAL_HEADERS_RECEIVED);
2633 return OK; 2747 return OK;
2634 } else { 2748 } else {
2635 partial_.reset(); 2749 partial_.reset();
2636 } 2750 }
2637 } 2751 }
2638 cache_->ConvertWriterToReader(entry_); 2752
2639 mode_ = READ; 2753 mode_ = READ;
2640 2754
2641 if (request_->method == "HEAD") 2755 if (request_->method == "HEAD")
2642 FixHeadersForHead(); 2756 FixHeadersForHead();
2643 2757
2644 if (entry_->disk_entry->GetDataSize(kMetadataIndex)) 2758 if (entry_->disk_entry->GetDataSize(kMetadataIndex))
2645 TransitionToState(STATE_CACHE_READ_METADATA); 2759 TransitionToState(STATE_CACHE_READ_METADATA);
2646 else 2760 else
2647 TransitionToState(STATE_NONE); 2761 TransitionToState(reading_ ? STATE_NONE : STATE_WAIT_BEFORE_READ);
2648 return OK; 2762 return OK;
2649 } 2763 }
2650 2764
2651 int HttpCache::Transaction::WriteToEntry(int index, int offset, 2765 int HttpCache::Transaction::WriteToEntry(int index, int offset,
2652 IOBuffer* data, int data_len, 2766 IOBuffer* data, int data_len,
2653 const CompletionCallback& callback) { 2767 const CompletionCallback& callback) {
2654 if (!entry_) 2768 if (!entry_)
2655 return data_len; 2769 return data_len;
2656 2770
2657 int rv = 0; 2771 int rv = 0;
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
2716 } 2830 }
2717 return OK; 2831 return OK;
2718 } 2832 }
2719 2833
2720 void HttpCache::Transaction::DoneWritingToEntry(bool success) { 2834 void HttpCache::Transaction::DoneWritingToEntry(bool success) {
2721 if (!entry_) 2835 if (!entry_)
2722 return; 2836 return;
2723 2837
2724 RecordHistograms(); 2838 RecordHistograms();
2725 2839
2726 cache_->DoneWritingToEntry(entry_, success); 2840 cache_->DoneWritingToEntry(entry_, success, this);
2727 entry_ = NULL; 2841 entry_ = NULL;
2728 mode_ = NONE; // switch to 'pass through' mode 2842 mode_ = NONE; // switch to 'pass through' mode
2729 } 2843 }
2730 2844
2731 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) { 2845 int HttpCache::Transaction::OnCacheReadError(int result, bool restart) {
2732 DLOG(ERROR) << "ReadData failed: " << result; 2846 DLOG(ERROR) << "ReadData failed: " << result;
2733 const int result_for_histogram = std::max(0, -result); 2847 const int result_for_histogram = std::max(0, -result);
2734 if (restart) { 2848 if (restart) {
2735 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable", 2849 UMA_HISTOGRAM_SPARSE_SLOWLY("HttpCache.ReadErrorRestartable",
2736 result_for_histogram); 2850 result_for_histogram);
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
3073 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated", 3187 UMA_HISTOGRAM_PERCENTAGE("HttpCache.PercentBeforeSend.Updated",
3074 before_send_sample); 3188 before_send_sample);
3075 break; 3189 break;
3076 } 3190 }
3077 default: 3191 default:
3078 NOTREACHED(); 3192 NOTREACHED();
3079 } 3193 }
3080 } 3194 }
3081 3195
3082 void HttpCache::Transaction::OnIOComplete(int result) { 3196 void HttpCache::Transaction::OnIOComplete(int result) {
3197 // If its the Start state machine and it cannot proceed due to failure by
3198 // another transaction in writing the response, restart this transaction.
3199 if (!reading_ && validating_cannot_proceed_) {
jkarlin 2017/03/30 14:25:26 Same as above, let's not use reading_ to infer our
3200 validating_cannot_proceed_ = false;
3201 RestartAfterValidationStarted();
3202 }
3203
3083 DoLoop(result); 3204 DoLoop(result);
3084 } 3205 }
3085 3206
3086 void HttpCache::Transaction::TransitionToState(State state) { 3207 void HttpCache::Transaction::TransitionToState(State state) {
3087 // Ensure that the state is only set once per Do* state. 3208 // Ensure that the state is only set once per Do* state.
3088 DCHECK(in_do_loop_); 3209 DCHECK(in_do_loop_);
3089 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state; 3210 DCHECK_EQ(STATE_UNSET, next_state_) << "Next state is " << state;
3090 next_state_ = state; 3211 next_state_ = state;
3091 } 3212 }
3092 3213
3093 } // namespace net 3214 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698