| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/http/http_cache_transaction.h" | 5 #include "net/http/http_cache_transaction.h" |
| 6 | 6 |
| 7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
| 8 | 8 |
| 9 #if defined(OS_POSIX) | 9 #if defined(OS_POSIX) |
| 10 #include <unistd.h> | 10 #include <unistd.h> |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 #include "net/base/load_flags.h" | 25 #include "net/base/load_flags.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/base/net_log.h" | 27 #include "net/base/net_log.h" |
| 28 #include "net/base/ssl_cert_request_info.h" | 28 #include "net/base/ssl_cert_request_info.h" |
| 29 #include "net/base/ssl_config_service.h" | 29 #include "net/base/ssl_config_service.h" |
| 30 #include "net/disk_cache/disk_cache.h" | 30 #include "net/disk_cache/disk_cache.h" |
| 31 #include "net/http/http_network_session.h" | 31 #include "net/http/http_network_session.h" |
| 32 #include "net/http/http_request_info.h" | 32 #include "net/http/http_request_info.h" |
| 33 #include "net/http/http_response_headers.h" | 33 #include "net/http/http_response_headers.h" |
| 34 #include "net/http/http_transaction.h" | 34 #include "net/http/http_transaction.h" |
| 35 #include "net/http/http_transaction_delegate.h" |
| 35 #include "net/http/http_util.h" | 36 #include "net/http/http_util.h" |
| 36 #include "net/http/partial_data.h" | 37 #include "net/http/partial_data.h" |
| 37 | 38 |
| 38 using base::Time; | 39 using base::Time; |
| 39 using base::TimeDelta; | 40 using base::TimeDelta; |
| 40 using base::TimeTicks; | 41 using base::TimeTicks; |
| 41 | 42 |
| 42 namespace net { | 43 namespace net { |
| 43 | 44 |
| 44 struct HeaderNameAndValue { | 45 struct HeaderNameAndValue { |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 while (v.GetNext()) { | 95 while (v.GetNext()) { |
| 95 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value)) | 96 if (LowerCaseEqualsASCII(v.value_begin(), v.value_end(), search->value)) |
| 96 return true; | 97 return true; |
| 97 } | 98 } |
| 98 } | 99 } |
| 99 return false; | 100 return false; |
| 100 } | 101 } |
| 101 | 102 |
| 102 //----------------------------------------------------------------------------- | 103 //----------------------------------------------------------------------------- |
| 103 | 104 |
| 104 HttpCache::Transaction::Transaction(HttpCache* cache) | 105 HttpCache::Transaction::Transaction(HttpCache* cache, |
| 106 HttpTransactionDelegate* delegate) |
| 105 : next_state_(STATE_NONE), | 107 : next_state_(STATE_NONE), |
| 106 request_(NULL), | 108 request_(NULL), |
| 107 cache_(cache->AsWeakPtr()), | 109 cache_(cache->AsWeakPtr()), |
| 108 entry_(NULL), | 110 entry_(NULL), |
| 109 new_entry_(NULL), | 111 new_entry_(NULL), |
| 110 network_trans_(NULL), | 112 network_trans_(NULL), |
| 111 new_response_(NULL), | 113 new_response_(NULL), |
| 112 mode_(NONE), | 114 mode_(NONE), |
| 113 target_state_(STATE_NONE), | 115 target_state_(STATE_NONE), |
| 114 reading_(false), | 116 reading_(false), |
| 115 invalid_range_(false), | 117 invalid_range_(false), |
| 116 truncated_(false), | 118 truncated_(false), |
| 117 is_sparse_(false), | 119 is_sparse_(false), |
| 118 range_requested_(false), | 120 range_requested_(false), |
| 119 handling_206_(false), | 121 handling_206_(false), |
| 120 cache_pending_(false), | 122 cache_pending_(false), |
| 121 done_reading_(false), | 123 done_reading_(false), |
| 122 read_offset_(0), | 124 read_offset_(0), |
| 123 effective_load_flags_(0), | 125 effective_load_flags_(0), |
| 124 write_len_(0), | 126 write_len_(0), |
| 125 final_upload_progress_(0), | 127 final_upload_progress_(0), |
| 126 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), | 128 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)), |
| 127 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( | 129 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( |
| 128 base::Bind(&Transaction::OnIOComplete, | 130 base::Bind(&Transaction::OnIOComplete, |
| 129 weak_factory_.GetWeakPtr()))) { | 131 weak_factory_.GetWeakPtr()))), |
| 132 delegate_(delegate) { |
| 130 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders == | 133 COMPILE_ASSERT(HttpCache::Transaction::kNumValidationHeaders == |
| 131 arraysize(kValidationHeaders), | 134 arraysize(kValidationHeaders), |
| 132 Invalid_number_of_validation_headers); | 135 Invalid_number_of_validation_headers); |
| 133 } | 136 } |
| 134 | 137 |
| 135 HttpCache::Transaction::~Transaction() { | 138 HttpCache::Transaction::~Transaction() { |
| 136 // We may have to issue another IO, but we should never invoke the callback_ | 139 // We may have to issue another IO, but we should never invoke the callback_ |
| 137 // after this point. | 140 // after this point. |
| 138 callback_.Reset(); | 141 callback_.Reset(); |
| 139 | 142 |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 if (rv != ERR_IO_PENDING) | 650 if (rv != ERR_IO_PENDING) |
| 648 HandleResult(rv); | 651 HandleResult(rv); |
| 649 | 652 |
| 650 return rv; | 653 return rv; |
| 651 } | 654 } |
| 652 | 655 |
| 653 int HttpCache::Transaction::DoGetBackend() { | 656 int HttpCache::Transaction::DoGetBackend() { |
| 654 cache_pending_ = true; | 657 cache_pending_ = true; |
| 655 next_state_ = STATE_GET_BACKEND_COMPLETE; | 658 next_state_ = STATE_GET_BACKEND_COMPLETE; |
| 656 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND); | 659 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_GET_BACKEND); |
| 660 ReportCacheActionStart(); |
| 657 return cache_->GetBackendForTransaction(this); | 661 return cache_->GetBackendForTransaction(this); |
| 658 } | 662 } |
| 659 | 663 |
| 660 int HttpCache::Transaction::DoGetBackendComplete(int result) { | 664 int HttpCache::Transaction::DoGetBackendComplete(int result) { |
| 661 DCHECK(result == OK || result == ERR_FAILED); | 665 DCHECK(result == OK || result == ERR_FAILED); |
| 666 ReportCacheActionFinish(); |
| 662 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND, | 667 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_GET_BACKEND, |
| 663 result); | 668 result); |
| 664 cache_pending_ = false; | 669 cache_pending_ = false; |
| 665 | 670 |
| 666 if (!ShouldPassThrough()) { | 671 if (!ShouldPassThrough()) { |
| 667 cache_key_ = cache_->GenerateCacheKey(request_); | 672 cache_key_ = cache_->GenerateCacheKey(request_); |
| 668 | 673 |
| 669 // Requested cache access mode. | 674 // Requested cache access mode. |
| 670 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { | 675 if (effective_load_flags_ & LOAD_ONLY_FROM_CACHE) { |
| 671 mode_ = READ; | 676 mode_ = READ; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 712 range_requested_ = (partial_.get() != NULL); | 717 range_requested_ = (partial_.get() != NULL); |
| 713 | 718 |
| 714 return OK; | 719 return OK; |
| 715 } | 720 } |
| 716 | 721 |
| 717 int HttpCache::Transaction::DoSendRequest() { | 722 int HttpCache::Transaction::DoSendRequest() { |
| 718 DCHECK(mode_ & WRITE || mode_ == NONE); | 723 DCHECK(mode_ & WRITE || mode_ == NONE); |
| 719 DCHECK(!network_trans_.get()); | 724 DCHECK(!network_trans_.get()); |
| 720 | 725 |
| 721 // Create a network transaction. | 726 // Create a network transaction. |
| 722 int rv = cache_->network_layer_->CreateTransaction(&network_trans_); | 727 int rv = cache_->network_layer_->CreateTransaction(&network_trans_, NULL); |
| 723 if (rv != OK) | 728 if (rv != OK) |
| 724 return rv; | 729 return rv; |
| 725 | 730 |
| 726 next_state_ = STATE_SEND_REQUEST_COMPLETE; | 731 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
| 727 rv = network_trans_->Start(request_, io_callback_, net_log_); | 732 rv = network_trans_->Start(request_, io_callback_, net_log_); |
| 728 return rv; | 733 return rv; |
| 729 } | 734 } |
| 730 | 735 |
| 731 int HttpCache::Transaction::DoSendRequestComplete(int result) { | 736 int HttpCache::Transaction::DoSendRequestComplete(int result) { |
| 732 if (!cache_) | 737 if (!cache_) |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 840 | 845 |
| 841 next_state_ = STATE_OPEN_ENTRY; | 846 next_state_ = STATE_OPEN_ENTRY; |
| 842 return OK; | 847 return OK; |
| 843 } | 848 } |
| 844 | 849 |
| 845 int HttpCache::Transaction::DoOpenEntry() { | 850 int HttpCache::Transaction::DoOpenEntry() { |
| 846 DCHECK(!new_entry_); | 851 DCHECK(!new_entry_); |
| 847 next_state_ = STATE_OPEN_ENTRY_COMPLETE; | 852 next_state_ = STATE_OPEN_ENTRY_COMPLETE; |
| 848 cache_pending_ = true; | 853 cache_pending_ = true; |
| 849 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY); | 854 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY); |
| 855 ReportCacheActionStart(); |
| 850 return cache_->OpenEntry(cache_key_, &new_entry_, this); | 856 return cache_->OpenEntry(cache_key_, &new_entry_, this); |
| 851 } | 857 } |
| 852 | 858 |
| 853 int HttpCache::Transaction::DoOpenEntryComplete(int result) { | 859 int HttpCache::Transaction::DoOpenEntryComplete(int result) { |
| 854 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is | 860 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is |
| 855 // OK, otherwise the cache will end up with an active entry without any | 861 // OK, otherwise the cache will end up with an active entry without any |
| 856 // transaction attached. | 862 // transaction attached. |
| 863 ReportCacheActionFinish(); |
| 857 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result); | 864 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_OPEN_ENTRY, result); |
| 858 cache_pending_ = false; | 865 cache_pending_ = false; |
| 859 if (result == OK) { | 866 if (result == OK) { |
| 860 next_state_ = STATE_ADD_TO_ENTRY; | 867 next_state_ = STATE_ADD_TO_ENTRY; |
| 861 return OK; | 868 return OK; |
| 862 } | 869 } |
| 863 | 870 |
| 864 if (result == ERR_CACHE_RACE) { | 871 if (result == ERR_CACHE_RACE) { |
| 865 next_state_ = STATE_INIT_ENTRY; | 872 next_state_ = STATE_INIT_ENTRY; |
| 866 return OK; | 873 return OK; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 890 // The entry does not exist, and we are not permitted to create a new entry, | 897 // The entry does not exist, and we are not permitted to create a new entry, |
| 891 // so we must fail. | 898 // so we must fail. |
| 892 return ERR_CACHE_MISS; | 899 return ERR_CACHE_MISS; |
| 893 } | 900 } |
| 894 | 901 |
| 895 int HttpCache::Transaction::DoCreateEntry() { | 902 int HttpCache::Transaction::DoCreateEntry() { |
| 896 DCHECK(!new_entry_); | 903 DCHECK(!new_entry_); |
| 897 next_state_ = STATE_CREATE_ENTRY_COMPLETE; | 904 next_state_ = STATE_CREATE_ENTRY_COMPLETE; |
| 898 cache_pending_ = true; | 905 cache_pending_ = true; |
| 899 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY); | 906 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY); |
| 907 ReportCacheActionStart(); |
| 900 return cache_->CreateEntry(cache_key_, &new_entry_, this); | 908 return cache_->CreateEntry(cache_key_, &new_entry_, this); |
| 901 } | 909 } |
| 902 | 910 |
| 903 int HttpCache::Transaction::DoCreateEntryComplete(int result) { | 911 int HttpCache::Transaction::DoCreateEntryComplete(int result) { |
| 904 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is | 912 // It is important that we go to STATE_ADD_TO_ENTRY whenever the result is |
| 905 // OK, otherwise the cache will end up with an active entry without any | 913 // OK, otherwise the cache will end up with an active entry without any |
| 906 // transaction attached. | 914 // transaction attached. |
| 915 ReportCacheActionFinish(); |
| 907 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY, | 916 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_CREATE_ENTRY, |
| 908 result); | 917 result); |
| 909 cache_pending_ = false; | 918 cache_pending_ = false; |
| 910 next_state_ = STATE_ADD_TO_ENTRY; | 919 next_state_ = STATE_ADD_TO_ENTRY; |
| 911 | 920 |
| 912 if (result == ERR_CACHE_RACE) { | 921 if (result == ERR_CACHE_RACE) { |
| 913 next_state_ = STATE_INIT_ENTRY; | 922 next_state_ = STATE_INIT_ENTRY; |
| 914 return OK; | 923 return OK; |
| 915 } | 924 } |
| 916 | 925 |
| 917 if (result != OK) { | 926 if (result != OK) { |
| 918 // We have a race here: Maybe we failed to open the entry and decided to | 927 // We have a race here: Maybe we failed to open the entry and decided to |
| 919 // create one, but by the time we called create, another transaction already | 928 // create one, but by the time we called create, another transaction already |
| 920 // created the entry. If we want to eliminate this issue, we need an atomic | 929 // created the entry. If we want to eliminate this issue, we need an atomic |
| 921 // OpenOrCreate() method exposed by the disk cache. | 930 // OpenOrCreate() method exposed by the disk cache. |
| 922 DLOG(WARNING) << "Unable to create cache entry"; | 931 DLOG(WARNING) << "Unable to create cache entry"; |
| 923 mode_ = NONE; | 932 mode_ = NONE; |
| 924 if (partial_.get()) | 933 if (partial_.get()) |
| 925 partial_->RestoreHeaders(&custom_request_->extra_headers); | 934 partial_->RestoreHeaders(&custom_request_->extra_headers); |
| 926 next_state_ = STATE_SEND_REQUEST; | 935 next_state_ = STATE_SEND_REQUEST; |
| 927 } | 936 } |
| 928 return OK; | 937 return OK; |
| 929 } | 938 } |
| 930 | 939 |
| 931 int HttpCache::Transaction::DoDoomEntry() { | 940 int HttpCache::Transaction::DoDoomEntry() { |
| 932 next_state_ = STATE_DOOM_ENTRY_COMPLETE; | 941 next_state_ = STATE_DOOM_ENTRY_COMPLETE; |
| 933 cache_pending_ = true; | 942 cache_pending_ = true; |
| 934 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY); | 943 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY); |
| 944 ReportCacheActionStart(); |
| 935 return cache_->DoomEntry(cache_key_, this); | 945 return cache_->DoomEntry(cache_key_, this); |
| 936 } | 946 } |
| 937 | 947 |
| 938 int HttpCache::Transaction::DoDoomEntryComplete(int result) { | 948 int HttpCache::Transaction::DoDoomEntryComplete(int result) { |
| 949 ReportCacheActionFinish(); |
| 939 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result); | 950 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_DOOM_ENTRY, result); |
| 940 next_state_ = STATE_CREATE_ENTRY; | 951 next_state_ = STATE_CREATE_ENTRY; |
| 941 cache_pending_ = false; | 952 cache_pending_ = false; |
| 942 if (result == ERR_CACHE_RACE) | 953 if (result == ERR_CACHE_RACE) |
| 943 next_state_ = STATE_INIT_ENTRY; | 954 next_state_ = STATE_INIT_ENTRY; |
| 944 | 955 |
| 945 return OK; | 956 return OK; |
| 946 } | 957 } |
| 947 | 958 |
| 948 int HttpCache::Transaction::DoAddToEntry() { | 959 int HttpCache::Transaction::DoAddToEntry() { |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1130 STATE_CACHE_WRITE_RESPONSE; | 1141 STATE_CACHE_WRITE_RESPONSE; |
| 1131 return OK; | 1142 return OK; |
| 1132 } | 1143 } |
| 1133 | 1144 |
| 1134 int HttpCache::Transaction::DoTruncateCachedData() { | 1145 int HttpCache::Transaction::DoTruncateCachedData() { |
| 1135 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE; | 1146 next_state_ = STATE_TRUNCATE_CACHED_DATA_COMPLETE; |
| 1136 if (!entry_) | 1147 if (!entry_) |
| 1137 return OK; | 1148 return OK; |
| 1138 if (net_log_.IsLoggingAllEvents()) | 1149 if (net_log_.IsLoggingAllEvents()) |
| 1139 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA); | 1150 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA); |
| 1140 | 1151 ReportCacheActionStart(); |
| 1141 // Truncate the stream. | 1152 // Truncate the stream. |
| 1142 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_); | 1153 return WriteToEntry(kResponseContentIndex, 0, NULL, 0, io_callback_); |
| 1143 } | 1154 } |
| 1144 | 1155 |
| 1145 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) { | 1156 int HttpCache::Transaction::DoTruncateCachedDataComplete(int result) { |
| 1146 if (net_log_.IsLoggingAllEvents() && entry_) { | 1157 if (entry_) { |
| 1147 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA, | 1158 ReportCacheActionFinish(); |
| 1148 result); | 1159 if (net_log_.IsLoggingAllEvents()) { |
| 1160 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA, |
| 1161 result); |
| 1162 } |
| 1149 } | 1163 } |
| 1150 | 1164 |
| 1151 next_state_ = STATE_TRUNCATE_CACHED_METADATA; | 1165 next_state_ = STATE_TRUNCATE_CACHED_METADATA; |
| 1152 return OK; | 1166 return OK; |
| 1153 } | 1167 } |
| 1154 | 1168 |
| 1155 int HttpCache::Transaction::DoTruncateCachedMetadata() { | 1169 int HttpCache::Transaction::DoTruncateCachedMetadata() { |
| 1156 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE; | 1170 next_state_ = STATE_TRUNCATE_CACHED_METADATA_COMPLETE; |
| 1157 if (!entry_) | 1171 if (!entry_) |
| 1158 return OK; | 1172 return OK; |
| 1159 | 1173 |
| 1160 if (net_log_.IsLoggingAllEvents()) | 1174 if (net_log_.IsLoggingAllEvents()) |
| 1161 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); | 1175 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); |
| 1176 ReportCacheActionStart(); |
| 1162 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_); | 1177 return WriteToEntry(kMetadataIndex, 0, NULL, 0, io_callback_); |
| 1163 } | 1178 } |
| 1164 | 1179 |
| 1165 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) { | 1180 int HttpCache::Transaction::DoTruncateCachedMetadataComplete(int result) { |
| 1166 if (net_log_.IsLoggingAllEvents() && entry_) { | 1181 if (entry_) { |
| 1167 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO, | 1182 ReportCacheActionFinish(); |
| 1168 result); | 1183 if (net_log_.IsLoggingAllEvents()) { |
| 1184 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO, |
| 1185 result); |
| 1186 } |
| 1169 } | 1187 } |
| 1170 | 1188 |
| 1171 // If this response is a redirect, then we can stop writing now. (We don't | 1189 // If this response is a redirect, then we can stop writing now. (We don't |
| 1172 // need to cache the response body of a redirect.) | 1190 // need to cache the response body of a redirect.) |
| 1173 if (response_.headers->IsRedirect(NULL)) | 1191 if (response_.headers->IsRedirect(NULL)) |
| 1174 DoneWritingToEntry(true); | 1192 DoneWritingToEntry(true); |
| 1175 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; | 1193 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; |
| 1176 return OK; | 1194 return OK; |
| 1177 } | 1195 } |
| 1178 | 1196 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 1200 } | 1218 } |
| 1201 | 1219 |
| 1202 int HttpCache::Transaction::DoCacheReadResponse() { | 1220 int HttpCache::Transaction::DoCacheReadResponse() { |
| 1203 DCHECK(entry_); | 1221 DCHECK(entry_); |
| 1204 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE; | 1222 next_state_ = STATE_CACHE_READ_RESPONSE_COMPLETE; |
| 1205 | 1223 |
| 1206 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex); | 1224 io_buf_len_ = entry_->disk_entry->GetDataSize(kResponseInfoIndex); |
| 1207 read_buf_ = new IOBuffer(io_buf_len_); | 1225 read_buf_ = new IOBuffer(io_buf_len_); |
| 1208 | 1226 |
| 1209 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); | 1227 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); |
| 1228 ReportCacheActionStart(); |
| 1210 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_, | 1229 return entry_->disk_entry->ReadData(kResponseInfoIndex, 0, read_buf_, |
| 1211 io_buf_len_, io_callback_); | 1230 io_buf_len_, io_callback_); |
| 1212 } | 1231 } |
| 1213 | 1232 |
| 1214 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { | 1233 int HttpCache::Transaction::DoCacheReadResponseComplete(int result) { |
| 1234 ReportCacheActionFinish(); |
| 1215 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); | 1235 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); |
| 1216 if (result != io_buf_len_ || | 1236 if (result != io_buf_len_ || |
| 1217 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, | 1237 !HttpCache::ParseResponseInfo(read_buf_->data(), io_buf_len_, |
| 1218 &response_, &truncated_)) { | 1238 &response_, &truncated_)) { |
| 1219 return OnCacheReadError(result, true); | 1239 return OnCacheReadError(result, true); |
| 1220 } | 1240 } |
| 1221 | 1241 |
| 1222 // Some resources may have slipped in as truncated when they're not. | 1242 // Some resources may have slipped in as truncated when they're not. |
| 1223 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 1243 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
| 1224 if (response_.headers->GetContentLength() == current_size) | 1244 if (response_.headers->GetContentLength() == current_size) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 1249 break; | 1269 break; |
| 1250 case WRITE: | 1270 case WRITE: |
| 1251 default: | 1271 default: |
| 1252 NOTREACHED(); | 1272 NOTREACHED(); |
| 1253 result = ERR_FAILED; | 1273 result = ERR_FAILED; |
| 1254 } | 1274 } |
| 1255 return result; | 1275 return result; |
| 1256 } | 1276 } |
| 1257 | 1277 |
| 1258 int HttpCache::Transaction::DoCacheWriteResponse() { | 1278 int HttpCache::Transaction::DoCacheWriteResponse() { |
| 1259 if (net_log_.IsLoggingAllEvents() && entry_) | 1279 if (entry_) { |
| 1260 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); | 1280 if (net_log_.IsLoggingAllEvents()) |
| 1281 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); |
| 1282 ReportCacheActionStart(); |
| 1283 } |
| 1261 return WriteResponseInfoToEntry(false); | 1284 return WriteResponseInfoToEntry(false); |
| 1262 } | 1285 } |
| 1263 | 1286 |
| 1264 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() { | 1287 int HttpCache::Transaction::DoCacheWriteTruncatedResponse() { |
| 1265 if (net_log_.IsLoggingAllEvents() && entry_) | 1288 if (entry_) { |
| 1266 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); | 1289 if (net_log_.IsLoggingAllEvents() && entry_) |
| 1290 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_INFO); |
| 1291 ReportCacheActionStart(); |
| 1292 } |
| 1267 return WriteResponseInfoToEntry(true); | 1293 return WriteResponseInfoToEntry(true); |
| 1268 } | 1294 } |
| 1269 | 1295 |
| 1270 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) { | 1296 int HttpCache::Transaction::DoCacheWriteResponseComplete(int result) { |
| 1271 next_state_ = target_state_; | 1297 next_state_ = target_state_; |
| 1272 target_state_ = STATE_NONE; | 1298 target_state_ = STATE_NONE; |
| 1273 if (!entry_) | 1299 if (!entry_) |
| 1274 return OK; | 1300 return OK; |
| 1301 ReportCacheActionFinish(); |
| 1275 if (net_log_.IsLoggingAllEvents()) { | 1302 if (net_log_.IsLoggingAllEvents()) { |
| 1276 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO, | 1303 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_INFO, |
| 1277 result); | 1304 result); |
| 1278 } | 1305 } |
| 1279 | 1306 |
| 1280 // Balance the AddRef from WriteResponseInfoToEntry. | 1307 // Balance the AddRef from WriteResponseInfoToEntry. |
| 1281 if (result != io_buf_len_) { | 1308 if (result != io_buf_len_) { |
| 1282 DLOG(ERROR) << "failed to write response info to cache"; | 1309 DLOG(ERROR) << "failed to write response info to cache"; |
| 1283 DoneWritingToEntry(false); | 1310 DoneWritingToEntry(false); |
| 1284 } | 1311 } |
| 1285 return OK; | 1312 return OK; |
| 1286 } | 1313 } |
| 1287 | 1314 |
| 1288 int HttpCache::Transaction::DoCacheReadMetadata() { | 1315 int HttpCache::Transaction::DoCacheReadMetadata() { |
| 1289 DCHECK(entry_); | 1316 DCHECK(entry_); |
| 1290 DCHECK(!response_.metadata); | 1317 DCHECK(!response_.metadata); |
| 1291 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE; | 1318 next_state_ = STATE_CACHE_READ_METADATA_COMPLETE; |
| 1292 | 1319 |
| 1293 response_.metadata = | 1320 response_.metadata = |
| 1294 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); | 1321 new IOBufferWithSize(entry_->disk_entry->GetDataSize(kMetadataIndex)); |
| 1295 | 1322 |
| 1296 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); | 1323 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_INFO); |
| 1324 ReportCacheActionStart(); |
| 1297 return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata, | 1325 return entry_->disk_entry->ReadData(kMetadataIndex, 0, response_.metadata, |
| 1298 response_.metadata->size(), | 1326 response_.metadata->size(), |
| 1299 io_callback_); | 1327 io_callback_); |
| 1300 } | 1328 } |
| 1301 | 1329 |
| 1302 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { | 1330 int HttpCache::Transaction::DoCacheReadMetadataComplete(int result) { |
| 1331 ReportCacheActionFinish(); |
| 1303 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); | 1332 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_INFO, result); |
| 1304 if (result != response_.metadata->size()) | 1333 if (result != response_.metadata->size()) |
| 1305 return OnCacheReadError(result, false); | 1334 return OnCacheReadError(result, false); |
| 1306 | 1335 |
| 1307 return OK; | 1336 return OK; |
| 1308 } | 1337 } |
| 1309 | 1338 |
| 1310 int HttpCache::Transaction::DoCacheQueryData() { | 1339 int HttpCache::Transaction::DoCacheQueryData() { |
| 1311 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE; | 1340 next_state_ = STATE_CACHE_QUERY_DATA_COMPLETE; |
| 1312 | 1341 |
| 1313 // Balanced in DoCacheQueryDataComplete. | 1342 // Balanced in DoCacheQueryDataComplete. |
| 1314 return entry_->disk_entry->ReadyForSparseIO(io_callback_); | 1343 return entry_->disk_entry->ReadyForSparseIO(io_callback_); |
| 1315 } | 1344 } |
| 1316 | 1345 |
| 1317 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { | 1346 int HttpCache::Transaction::DoCacheQueryDataComplete(int result) { |
| 1318 DCHECK_EQ(OK, result); | 1347 DCHECK_EQ(OK, result); |
| 1319 if (!cache_) | 1348 if (!cache_) |
| 1320 return ERR_UNEXPECTED; | 1349 return ERR_UNEXPECTED; |
| 1321 | 1350 |
| 1322 return ValidateEntryHeadersAndContinue(); | 1351 return ValidateEntryHeadersAndContinue(); |
| 1323 } | 1352 } |
| 1324 | 1353 |
| 1325 int HttpCache::Transaction::DoCacheReadData() { | 1354 int HttpCache::Transaction::DoCacheReadData() { |
| 1326 DCHECK(entry_); | 1355 DCHECK(entry_); |
| 1327 next_state_ = STATE_CACHE_READ_DATA_COMPLETE; | 1356 next_state_ = STATE_CACHE_READ_DATA_COMPLETE; |
| 1328 | 1357 |
| 1329 if (net_log_.IsLoggingAllEvents()) | 1358 if (net_log_.IsLoggingAllEvents()) |
| 1330 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA); | 1359 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_READ_DATA); |
| 1360 ReportCacheActionStart(); |
| 1331 if (partial_.get()) { | 1361 if (partial_.get()) { |
| 1332 return partial_->CacheRead(entry_->disk_entry, read_buf_, io_buf_len_, | 1362 return partial_->CacheRead(entry_->disk_entry, read_buf_, io_buf_len_, |
| 1333 io_callback_); | 1363 io_callback_); |
| 1334 } | 1364 } |
| 1335 | 1365 |
| 1336 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_, | 1366 return entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_, |
| 1337 read_buf_, io_buf_len_, io_callback_); | 1367 read_buf_, io_buf_len_, io_callback_); |
| 1338 } | 1368 } |
| 1339 | 1369 |
| 1340 int HttpCache::Transaction::DoCacheReadDataComplete(int result) { | 1370 int HttpCache::Transaction::DoCacheReadDataComplete(int result) { |
| 1371 ReportCacheActionFinish(); |
| 1341 if (net_log_.IsLoggingAllEvents()) { | 1372 if (net_log_.IsLoggingAllEvents()) { |
| 1342 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA, | 1373 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_READ_DATA, |
| 1343 result); | 1374 result); |
| 1344 } | 1375 } |
| 1345 | 1376 |
| 1346 if (!cache_) | 1377 if (!cache_) |
| 1347 return ERR_UNEXPECTED; | 1378 return ERR_UNEXPECTED; |
| 1348 | 1379 |
| 1349 if (partial_.get()) | 1380 if (partial_.get()) |
| 1350 return DoPartialCacheReadCompleted(result); | 1381 return DoPartialCacheReadCompleted(result); |
| 1351 | 1382 |
| 1352 if (result > 0) { | 1383 if (result > 0) { |
| 1353 read_offset_ += result; | 1384 read_offset_ += result; |
| 1354 } else if (result == 0) { // End of file. | 1385 } else if (result == 0) { // End of file. |
| 1355 cache_->DoneReadingFromEntry(entry_, this); | 1386 cache_->DoneReadingFromEntry(entry_, this); |
| 1356 entry_ = NULL; | 1387 entry_ = NULL; |
| 1357 } else { | 1388 } else { |
| 1358 return OnCacheReadError(result, false); | 1389 return OnCacheReadError(result, false); |
| 1359 } | 1390 } |
| 1360 return result; | 1391 return result; |
| 1361 } | 1392 } |
| 1362 | 1393 |
| 1363 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) { | 1394 int HttpCache::Transaction::DoCacheWriteData(int num_bytes) { |
| 1364 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE; | 1395 next_state_ = STATE_CACHE_WRITE_DATA_COMPLETE; |
| 1365 write_len_ = num_bytes; | 1396 write_len_ = num_bytes; |
| 1366 if (net_log_.IsLoggingAllEvents() && entry_) | 1397 if (entry_) { |
| 1367 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA); | 1398 if (net_log_.IsLoggingAllEvents()) |
| 1399 net_log_.BeginEvent(NetLog::TYPE_HTTP_CACHE_WRITE_DATA); |
| 1400 ReportCacheActionStart(); |
| 1401 } |
| 1368 | 1402 |
| 1369 return AppendResponseDataToEntry(read_buf_, num_bytes, io_callback_); | 1403 return AppendResponseDataToEntry(read_buf_, num_bytes, io_callback_); |
| 1370 } | 1404 } |
| 1371 | 1405 |
| 1372 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) { | 1406 int HttpCache::Transaction::DoCacheWriteDataComplete(int result) { |
| 1373 if (net_log_.IsLoggingAllEvents() && entry_) { | 1407 if (entry_) { |
| 1374 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA, | 1408 ReportCacheActionFinish(); |
| 1375 result); | 1409 if (net_log_.IsLoggingAllEvents()) { |
| 1410 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_HTTP_CACHE_WRITE_DATA, |
| 1411 result); |
| 1412 } |
| 1376 } | 1413 } |
| 1377 // Balance the AddRef from DoCacheWriteData. | 1414 // Balance the AddRef from DoCacheWriteData. |
| 1378 if (!cache_) | 1415 if (!cache_) |
| 1379 return ERR_UNEXPECTED; | 1416 return ERR_UNEXPECTED; |
| 1380 | 1417 |
| 1381 if (result != write_len_) { | 1418 if (result != write_len_) { |
| 1382 DLOG(ERROR) << "failed to write response data to cache"; | 1419 DLOG(ERROR) << "failed to write response data to cache"; |
| 1383 DoneWritingToEntry(false); | 1420 DoneWritingToEntry(false); |
| 1384 | 1421 |
| 1385 // We want to ignore errors writing to disk and just keep reading from | 1422 // We want to ignore errors writing to disk and just keep reading from |
| (...skipping 719 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2105 !response_.headers->HasStrongValidators()) | 2142 !response_.headers->HasStrongValidators()) |
| 2106 return false; | 2143 return false; |
| 2107 | 2144 |
| 2108 return true; | 2145 return true; |
| 2109 } | 2146 } |
| 2110 | 2147 |
| 2111 void HttpCache::Transaction::OnIOComplete(int result) { | 2148 void HttpCache::Transaction::OnIOComplete(int result) { |
| 2112 DoLoop(result); | 2149 DoLoop(result); |
| 2113 } | 2150 } |
| 2114 | 2151 |
| 2152 void HttpCache::Transaction::ReportCacheActionStart() { |
| 2153 if (delegate_) |
| 2154 delegate_->OnCacheActionStart(); |
| 2155 } |
| 2156 |
| 2157 void HttpCache::Transaction::ReportCacheActionFinish() { |
| 2158 if (delegate_) |
| 2159 delegate_->OnCacheActionFinish(); |
| 2160 } |
| 2161 |
| 2115 } // namespace net | 2162 } // namespace net |
| OLD | NEW |