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