| OLD | NEW |
| 1 // Copyright (c) 2006-2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2010 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 "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 | 8 |
| 9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| (...skipping 660 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 671 } | 671 } |
| 672 | 672 |
| 673 next_state_ = STATE_OPEN_ENTRY; | 673 next_state_ = STATE_OPEN_ENTRY; |
| 674 return OK; | 674 return OK; |
| 675 } | 675 } |
| 676 | 676 |
| 677 int HttpCache::Transaction::DoOpenEntry() { | 677 int HttpCache::Transaction::DoOpenEntry() { |
| 678 DCHECK(!new_entry_); | 678 DCHECK(!new_entry_); |
| 679 next_state_ = STATE_OPEN_ENTRY_COMPLETE; | 679 next_state_ = STATE_OPEN_ENTRY_COMPLETE; |
| 680 cache_pending_ = true; | 680 cache_pending_ = true; |
| 681 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY); | 681 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, NULL); |
| 682 return cache_->OpenEntry(cache_key_, &new_entry_, this); | 682 return cache_->OpenEntry(cache_key_, &new_entry_, this); |
| 683 } | 683 } |
| 684 | 684 |
| 685 int HttpCache::Transaction::DoOpenEntryComplete(int result) { | 685 int HttpCache::Transaction::DoOpenEntryComplete(int result) { |
| 686 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is | 686 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is |
| 687 // OK, otherwise the cache will end up with an active entry without any | 687 // OK, otherwise the cache will end up with an active entry without any |
| 688 // transaction attached. | 688 // transaction attached. |
| 689 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY); | 689 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, NULL); |
| 690 cache_pending_ = false; | 690 cache_pending_ = false; |
| 691 if (result == OK) { | 691 if (result == OK) { |
| 692 next_state_ = STATE_ADD_TO_ENTRY; | 692 next_state_ = STATE_ADD_TO_ENTRY; |
| 693 return OK; | 693 return OK; |
| 694 } | 694 } |
| 695 | 695 |
| 696 if (result == ERR_CACHE_RACE) { | 696 if (result == ERR_CACHE_RACE) { |
| 697 next_state_ = STATE_INIT_ENTRY; | 697 next_state_ = STATE_INIT_ENTRY; |
| 698 return OK; | 698 return OK; |
| 699 } | 699 } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 714 | 714 |
| 715 // The entry does not exist, and we are not permitted to create a new entry, | 715 // The entry does not exist, and we are not permitted to create a new entry, |
| 716 // so we must fail. | 716 // so we must fail. |
| 717 return ERR_CACHE_MISS; | 717 return ERR_CACHE_MISS; |
| 718 } | 718 } |
| 719 | 719 |
| 720 int HttpCache::Transaction::DoCreateEntry() { | 720 int HttpCache::Transaction::DoCreateEntry() { |
| 721 DCHECK(!new_entry_); | 721 DCHECK(!new_entry_); |
| 722 next_state_ = STATE_CREATE_ENTRY_COMPLETE; | 722 next_state_ = STATE_CREATE_ENTRY_COMPLETE; |
| 723 cache_pending_ = true; | 723 cache_pending_ = true; |
| 724 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY); | 724 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY, NULL); |
| 725 return cache_->CreateEntry(cache_key_, &new_entry_, this); | 725 return cache_->CreateEntry(cache_key_, &new_entry_, this); |
| 726 } | 726 } |
| 727 | 727 |
| 728 int HttpCache::Transaction::DoCreateEntryComplete(int result) { | 728 int HttpCache::Transaction::DoCreateEntryComplete(int result) { |
| 729 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is | 729 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is |
| 730 // OK, otherwise the cache will end up with an active entry without any | 730 // OK, otherwise the cache will end up with an active entry without any |
| 731 // transaction attached. | 731 // transaction attached. |
| 732 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY); | 732 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY, NULL); |
| 733 cache_pending_ = false; | 733 cache_pending_ = false; |
| 734 next_state_ = STATE_ADD_TO_ENTRY; | 734 next_state_ = STATE_ADD_TO_ENTRY; |
| 735 | 735 |
| 736 if (result == ERR_CACHE_RACE) { | 736 if (result == ERR_CACHE_RACE) { |
| 737 next_state_ = STATE_INIT_ENTRY; | 737 next_state_ = STATE_INIT_ENTRY; |
| 738 return OK; | 738 return OK; |
| 739 } | 739 } |
| 740 | 740 |
| 741 if (result != OK) { | 741 if (result != OK) { |
| 742 // We have a race here: Maybe we failed to open the entry and decided to | 742 // We have a race here: Maybe we failed to open the entry and decided to |
| 743 // create one, but by the time we called create, another transaction already | 743 // create one, but by the time we called create, another transaction already |
| 744 // created the entry. If we want to eliminate this issue, we need an atomic | 744 // created the entry. If we want to eliminate this issue, we need an atomic |
| 745 // OpenOrCreate() method exposed by the disk cache. | 745 // OpenOrCreate() method exposed by the disk cache. |
| 746 DLOG(WARNING) << "Unable to create cache entry"; | 746 DLOG(WARNING) << "Unable to create cache entry"; |
| 747 mode_ = NONE; | 747 mode_ = NONE; |
| 748 if (partial_.get()) | 748 if (partial_.get()) |
| 749 partial_->RestoreHeaders(&custom_request_->extra_headers); | 749 partial_->RestoreHeaders(&custom_request_->extra_headers); |
| 750 next_state_ = STATE_SEND_REQUEST; | 750 next_state_ = STATE_SEND_REQUEST; |
| 751 } | 751 } |
| 752 return OK; | 752 return OK; |
| 753 } | 753 } |
| 754 | 754 |
| 755 int HttpCache::Transaction::DoDoomEntry() { | 755 int HttpCache::Transaction::DoDoomEntry() { |
| 756 next_state_ = STATE_DOOM_ENTRY_COMPLETE; | 756 next_state_ = STATE_DOOM_ENTRY_COMPLETE; |
| 757 cache_pending_ = true; | 757 cache_pending_ = true; |
| 758 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY); | 758 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, NULL); |
| 759 return cache_->DoomEntry(cache_key_, this); | 759 return cache_->DoomEntry(cache_key_, this); |
| 760 } | 760 } |
| 761 | 761 |
| 762 int HttpCache::Transaction::DoDoomEntryComplete(int result) { | 762 int HttpCache::Transaction::DoDoomEntryComplete(int result) { |
| 763 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY); | 763 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, NULL); |
| 764 next_state_ = STATE_CREATE_ENTRY; | 764 next_state_ = STATE_CREATE_ENTRY; |
| 765 cache_pending_ = false; | 765 cache_pending_ = false; |
| 766 if (result == ERR_CACHE_RACE) | 766 if (result == ERR_CACHE_RACE) |
| 767 next_state_ = STATE_INIT_ENTRY; | 767 next_state_ = STATE_INIT_ENTRY; |
| 768 | 768 |
| 769 return OK; | 769 return OK; |
| 770 } | 770 } |
| 771 | 771 |
| 772 int HttpCache::Transaction::DoAddToEntry() { | 772 int HttpCache::Transaction::DoAddToEntry() { |
| 773 DCHECK(new_entry_); | 773 DCHECK(new_entry_); |
| 774 cache_pending_ = true; | 774 cache_pending_ = true; |
| 775 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE; | 775 next_state_ = STATE_ADD_TO_ENTRY_COMPLETE; |
| 776 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WAITING); | 776 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WAITING, NULL); |
| 777 return cache_->AddTransactionToEntry(new_entry_, this); | 777 return cache_->AddTransactionToEntry(new_entry_, this); |
| 778 } | 778 } |
| 779 | 779 |
| 780 int HttpCache::Transaction::DoAddToEntryComplete(int result) { | 780 int HttpCache::Transaction::DoAddToEntryComplete(int result) { |
| 781 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WAITING); | 781 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_WAITING, NULL); |
| 782 DCHECK(new_entry_); | 782 DCHECK(new_entry_); |
| 783 cache_pending_ = false; | 783 cache_pending_ = false; |
| 784 | 784 |
| 785 if (result == ERR_CACHE_RACE) { | 785 if (result == ERR_CACHE_RACE) { |
| 786 new_entry_ = NULL; | 786 new_entry_ = NULL; |
| 787 next_state_ = STATE_INIT_ENTRY; | 787 next_state_ = STATE_INIT_ENTRY; |
| 788 return OK; | 788 return OK; |
| 789 } | 789 } |
| 790 | 790 |
| 791 if (result != OK) { | 791 if (result != OK) { |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 951 return OK; | 951 return OK; |
| 952 } | 952 } |
| 953 | 953 |
| 954 int HttpCache::Transaction::DoCacheReadResponse() { | 954 int HttpCache::Transaction::DoCacheReadResponse() { |
| 955 DCHECK(entry_); | 955 DCHECK(entry_); |
| 956 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE; | 956 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE; |
| 957 | 957 |
| 958 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex); | 958 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex); |
| 959 read_buf_ = new IOBuffer(io_buf_len_); | 959 read_buf_ = new IOBuffer(io_buf_len_); |
| 960 | 960 |
| 961 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); | 961 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO, NULL); |
| 962 cache_callback_->AddRef(); // Balanced in DoCacheReadResponseComplete. | 962 cache_callback_->AddRef(); // Balanced in DoCacheReadResponseComplete. |
| 963 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_, | 963 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_, |
| 964 io_buf_len_, cache_callback_); | 964 io_buf_len_, cache_callback_); |
| 965 } | 965 } |
| 966 | 966 |
| 967 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { | 967 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { |
| 968 cache_callback_->Release(); // Balance the AddRef from DoCacheReadResponse. | 968 cache_callback_->Release(); // Balance the AddRef from DoCacheReadResponse. |
| 969 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); | 969 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO, NULL); |
| 970 if (result != io_buf_len_ || | 970 if (result != io_buf_len_ || |
| 971 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, | 971 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, |
| 972 &response_, &truncated_)) { | 972 &response_, &truncated_)) { |
| 973 DLOG(ERROR) << "ReadData failed: " << result; | 973 DLOG(ERROR) << "ReadData failed: " << result; |
| 974 return ERR_CACHE_READ_FAILURE; | 974 return ERR_CACHE_READ_FAILURE; |
| 975 } | 975 } |
| 976 | 976 |
| 977 // We now have access to the cache entry. | 977 // We now have access to the cache entry. |
| 978 // | 978 // |
| 979 // o if we are a reader for the transaction, then we can start reading the | 979 // o if we are a reader for the transaction, then we can start reading the |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1029 } | 1029 } |
| 1030 | 1030 |
| 1031 int HttpCache::Transaction::DoCacheReadMetadata() { | 1031 int HttpCache::Transaction::DoCacheReadMetadata() { |
| 1032 DCHECK(entry_); | 1032 DCHECK(entry_); |
| 1033 DCHECK(!response_.metadata); | 1033 DCHECK(!response_.metadata); |
| 1034 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE; | 1034 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE; |
| 1035 | 1035 |
| 1036 response_.metadata = | 1036 response_.metadata = |
| 1037 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); | 1037 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); |
| 1038 | 1038 |
| 1039 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); | 1039 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO, NULL); |
| 1040 cache_callback_->AddRef(); // Balanced in DoCacheReadMetadataComplete. | 1040 cache_callback_->AddRef(); // Balanced in DoCacheReadMetadataComplete. |
| 1041 return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata, | 1041 return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata, |
| 1042 response_.metadata->size(), | 1042 response_.metadata->size(), |
| 1043 cache_callback_); | 1043 cache_callback_); |
| 1044 } | 1044 } |
| 1045 | 1045 |
| 1046 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { | 1046 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { |
| 1047 cache_callback_->Release(); // Balance the AddRef from DoCacheReadMetadata. | 1047 cache_callback_->Release(); // Balance the AddRef from DoCacheReadMetadata. |
| 1048 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); | 1048 net_log_.EndEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO, NULL); |
| 1049 if (result != response_.metadata->size()) { | 1049 if (result != response_.metadata->size()) { |
| 1050 DLOG(ERROR) << "ReadData failed: " << result; | 1050 DLOG(ERROR) << "ReadData failed: " << result; |
| 1051 return ERR_CACHE_READ_FAILURE; | 1051 return ERR_CACHE_READ_FAILURE; |
| 1052 } | 1052 } |
| 1053 | 1053 |
| 1054 return OK; | 1054 return OK; |
| 1055 } | 1055 } |
| 1056 | 1056 |
| 1057 int HttpCache::Transaction::DoCacheQueryData() { | 1057 int HttpCache::Transaction::DoCacheQueryData() { |
| 1058 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE; | 1058 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE; |
| (...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1800 // |value| goes from 0 to 63. Actually, the max value should be 47 (0x2f) | 1800 // |value| goes from 0 to 63. Actually, the max value should be 47 (0x2f) |
| 1801 // but we'll see. | 1801 // but we'll see. |
| 1802 UMA_HISTOGRAM_ENUMERATION("HttpCache.ResponseHeaders", value, 65); | 1802 UMA_HISTOGRAM_ENUMERATION("HttpCache.ResponseHeaders", value, 65); |
| 1803 } | 1803 } |
| 1804 | 1804 |
| 1805 void HttpCache::Transaction::OnIOComplete(int result) { | 1805 void HttpCache::Transaction::OnIOComplete(int result) { |
| 1806 DoLoop(result); | 1806 DoLoop(result); |
| 1807 } | 1807 } |
| 1808 | 1808 |
| 1809 } // namespace net | 1809 } // namespace net |
| OLD | NEW |