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> |
11 #endif | 11 #endif |
12 | 12 |
13 #include <string> | 13 #include <string> |
14 | 14 |
15 #include "base/bind.h" | 15 #include "base/bind.h" |
16 #include "base/compiler_specific.h" | 16 #include "base/compiler_specific.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/metrics/field_trial.h" | 18 #include "base/metrics/field_trial.h" |
19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
20 #include "base/rand_util.h" | 20 #include "base/rand_util.h" |
21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
22 #include "base/string_util.h" | 22 #include "base/string_util.h" |
23 #include "base/time.h" | 23 #include "base/time.h" |
24 #include "net/base/completion_callback.h" | 24 #include "net/base/completion_callback.h" |
25 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
26 #include "net/base/load_flags.h" | 26 #include "net/base/load_flags.h" |
| 27 #include "net/base/load_timing_info.h" |
27 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
28 #include "net/base/net_log.h" | 29 #include "net/base/net_log.h" |
29 #include "net/base/upload_data_stream.h" | 30 #include "net/base/upload_data_stream.h" |
30 #include "net/cert/cert_status_flags.h" | 31 #include "net/cert/cert_status_flags.h" |
31 #include "net/disk_cache/disk_cache.h" | 32 #include "net/disk_cache/disk_cache.h" |
32 #include "net/http/http_network_session.h" | 33 #include "net/http/http_network_session.h" |
33 #include "net/http/http_request_info.h" | 34 #include "net/http/http_request_info.h" |
34 #include "net/http/http_response_headers.h" | 35 #include "net/http/http_response_headers.h" |
35 #include "net/http/http_transaction.h" | 36 #include "net/http/http_transaction.h" |
36 #include "net/http/http_transaction_delegate.h" | 37 #include "net/http/http_transaction_delegate.h" |
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 UploadProgress HttpCache::Transaction::GetUploadProgress() const { | 482 UploadProgress HttpCache::Transaction::GetUploadProgress() const { |
482 if (network_trans_.get()) | 483 if (network_trans_.get()) |
483 return network_trans_->GetUploadProgress(); | 484 return network_trans_->GetUploadProgress(); |
484 return final_upload_progress_; | 485 return final_upload_progress_; |
485 } | 486 } |
486 | 487 |
487 bool HttpCache::Transaction::GetLoadTimingInfo( | 488 bool HttpCache::Transaction::GetLoadTimingInfo( |
488 LoadTimingInfo* load_timing_info) const { | 489 LoadTimingInfo* load_timing_info) const { |
489 if (network_trans_) | 490 if (network_trans_) |
490 return network_trans_->GetLoadTimingInfo(load_timing_info); | 491 return network_trans_->GetLoadTimingInfo(load_timing_info); |
491 // Don't modify |load_timing_info| when reading from the cache instead of the | 492 |
492 // network. | 493 if (old_network_trans_load_timing_) { |
493 return false; | 494 *load_timing_info = *old_network_trans_load_timing_; |
| 495 return true; |
| 496 } |
| 497 |
| 498 if (first_cache_access_since_.is_null()) |
| 499 return false; |
| 500 |
| 501 // If the cache entry was opened, return that time. |
| 502 load_timing_info->send_start = first_cache_access_since_; |
| 503 // This time doesn't make much sense when reading from the cache, so just use |
| 504 // the same time as send_start. |
| 505 load_timing_info->send_end = first_cache_access_since_; |
| 506 return true; |
494 } | 507 } |
495 | 508 |
496 void HttpCache::Transaction::SetPriority(RequestPriority priority) { | 509 void HttpCache::Transaction::SetPriority(RequestPriority priority) { |
497 priority_ = priority; | 510 priority_ = priority; |
498 if (network_trans_) | 511 if (network_trans_) |
499 network_trans_->SetPriority(priority_); | 512 network_trans_->SetPriority(priority_); |
500 } | 513 } |
501 | 514 |
502 //----------------------------------------------------------------------------- | 515 //----------------------------------------------------------------------------- |
503 | 516 |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
819 DCHECK(!network_trans_.get()); | 832 DCHECK(!network_trans_.get()); |
820 | 833 |
821 send_request_since_ = TimeTicks::Now(); | 834 send_request_since_ = TimeTicks::Now(); |
822 | 835 |
823 // Create a network transaction. | 836 // Create a network transaction. |
824 int rv = cache_->network_layer_->CreateTransaction( | 837 int rv = cache_->network_layer_->CreateTransaction( |
825 priority_, &network_trans_, NULL); | 838 priority_, &network_trans_, NULL); |
826 if (rv != OK) | 839 if (rv != OK) |
827 return rv; | 840 return rv; |
828 | 841 |
| 842 // Old load timing information, if any, is now obsolete. |
| 843 old_network_trans_load_timing_.reset(); |
| 844 |
829 ReportNetworkActionStart(); | 845 ReportNetworkActionStart(); |
830 next_state_ = STATE_SEND_REQUEST_COMPLETE; | 846 next_state_ = STATE_SEND_REQUEST_COMPLETE; |
831 rv = network_trans_->Start(request_, io_callback_, net_log_); | 847 rv = network_trans_->Start(request_, io_callback_, net_log_); |
832 return rv; | 848 return rv; |
833 } | 849 } |
834 | 850 |
835 int HttpCache::Transaction::DoSendRequestComplete(int result) { | 851 int HttpCache::Transaction::DoSendRequestComplete(int result) { |
836 ReportNetworkActionFinish(); | 852 ReportNetworkActionFinish(); |
837 | 853 |
838 if (!cache_) | 854 if (!cache_) |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
900 } | 916 } |
901 | 917 |
902 new_response_ = new_response; | 918 new_response_ = new_response; |
903 if (!ValidatePartialResponse() && !auth_response_.headers) { | 919 if (!ValidatePartialResponse() && !auth_response_.headers) { |
904 // Something went wrong with this request and we have to restart it. | 920 // Something went wrong with this request and we have to restart it. |
905 // If we have an authentication response, we are exposed to weird things | 921 // If we have an authentication response, we are exposed to weird things |
906 // hapenning if the user cancels the authentication before we receive | 922 // hapenning if the user cancels the authentication before we receive |
907 // the new response. | 923 // the new response. |
908 UpdateTransactionPattern(PATTERN_NOT_COVERED); | 924 UpdateTransactionPattern(PATTERN_NOT_COVERED); |
909 response_ = HttpResponseInfo(); | 925 response_ = HttpResponseInfo(); |
910 network_trans_.reset(); | 926 ResetNetworkTransaction(); |
911 new_response_ = NULL; | 927 new_response_ = NULL; |
912 next_state_ = STATE_SEND_REQUEST; | 928 next_state_ = STATE_SEND_REQUEST; |
913 return OK; | 929 return OK; |
914 } | 930 } |
915 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) { | 931 if (handling_206_ && mode_ == READ_WRITE && !truncated_ && !is_sparse_) { |
916 // We have stored the full entry, but it changed and the server is | 932 // We have stored the full entry, but it changed and the server is |
917 // sending a range. We have to delete the old entry. | 933 // sending a range. We have to delete the old entry. |
918 UpdateTransactionPattern(PATTERN_NOT_COVERED); | 934 UpdateTransactionPattern(PATTERN_NOT_COVERED); |
919 DoneWritingToEntry(false); | 935 DoneWritingToEntry(false); |
920 } | 936 } |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1252 // the cached 200 response, is what will be returned to the user. | 1268 // the cached 200 response, is what will be returned to the user. |
1253 DoneWritingToEntry(true); | 1269 DoneWritingToEntry(true); |
1254 } else if (entry_ && !handling_206_) { | 1270 } else if (entry_ && !handling_206_) { |
1255 DCHECK_EQ(READ_WRITE, mode_); | 1271 DCHECK_EQ(READ_WRITE, mode_); |
1256 if (!partial_.get() || partial_->IsLastRange()) { | 1272 if (!partial_.get() || partial_->IsLastRange()) { |
1257 cache_->ConvertWriterToReader(entry_); | 1273 cache_->ConvertWriterToReader(entry_); |
1258 mode_ = READ; | 1274 mode_ = READ; |
1259 } | 1275 } |
1260 // We no longer need the network transaction, so destroy it. | 1276 // We no longer need the network transaction, so destroy it. |
1261 final_upload_progress_ = network_trans_->GetUploadProgress(); | 1277 final_upload_progress_ = network_trans_->GetUploadProgress(); |
1262 network_trans_.reset(); | 1278 ResetNetworkTransaction(); |
1263 } else if (entry_ && handling_206_ && truncated_ && | 1279 } else if (entry_ && handling_206_ && truncated_ && |
1264 partial_->initial_validation()) { | 1280 partial_->initial_validation()) { |
1265 // We just finished the validation of a truncated entry, and the server | 1281 // We just finished the validation of a truncated entry, and the server |
1266 // is willing to resume the operation. Now we go back and start serving | 1282 // is willing to resume the operation. Now we go back and start serving |
1267 // the first part to the user. | 1283 // the first part to the user. |
1268 network_trans_.reset(); | 1284 ResetNetworkTransaction(); |
1269 new_response_ = NULL; | 1285 new_response_ = NULL; |
1270 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | 1286 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; |
1271 partial_->SetRangeToStartDownload(); | 1287 partial_->SetRangeToStartDownload(); |
1272 return OK; | 1288 return OK; |
1273 } | 1289 } |
1274 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE; | 1290 next_state_ = STATE_OVERWRITE_CACHED_RESPONSE; |
1275 return OK; | 1291 return OK; |
1276 } | 1292 } |
1277 | 1293 |
1278 int HttpCache::Transaction::DoOverwriteCachedResponse() { | 1294 int HttpCache::Transaction::DoOverwriteCachedResponse() { |
(...skipping 884 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2163 entry_ = NULL; | 2179 entry_ = NULL; |
2164 mode_ = NONE; | 2180 mode_ = NONE; |
2165 } | 2181 } |
2166 | 2182 |
2167 void HttpCache::Transaction::FailRangeRequest() { | 2183 void HttpCache::Transaction::FailRangeRequest() { |
2168 response_ = *new_response_; | 2184 response_ = *new_response_; |
2169 partial_->FixResponseHeaders(response_.headers, false); | 2185 partial_->FixResponseHeaders(response_.headers, false); |
2170 } | 2186 } |
2171 | 2187 |
2172 int HttpCache::Transaction::SetupEntryForRead() { | 2188 int HttpCache::Transaction::SetupEntryForRead() { |
2173 network_trans_.reset(); | 2189 if (network_trans_) |
| 2190 ResetNetworkTransaction(); |
2174 if (partial_.get()) { | 2191 if (partial_.get()) { |
2175 if (truncated_ || is_sparse_ || !invalid_range_) { | 2192 if (truncated_ || is_sparse_ || !invalid_range_) { |
2176 // We are going to return the saved response headers to the caller, so | 2193 // We are going to return the saved response headers to the caller, so |
2177 // we may need to adjust them first. | 2194 // we may need to adjust them first. |
2178 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; | 2195 next_state_ = STATE_PARTIAL_HEADERS_RECEIVED; |
2179 return OK; | 2196 return OK; |
2180 } else { | 2197 } else { |
2181 partial_.reset(); | 2198 partial_.reset(); |
2182 } | 2199 } |
2183 } | 2200 } |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2312 is_sparse_ = false; | 2329 is_sparse_ = false; |
2313 if (delete_object) | 2330 if (delete_object) |
2314 partial_.reset(NULL); | 2331 partial_.reset(NULL); |
2315 } | 2332 } |
2316 | 2333 |
2317 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) { | 2334 int HttpCache::Transaction::DoPartialNetworkReadCompleted(int result) { |
2318 partial_->OnNetworkReadCompleted(result); | 2335 partial_->OnNetworkReadCompleted(result); |
2319 | 2336 |
2320 if (result == 0) { | 2337 if (result == 0) { |
2321 // We need to move on to the next range. | 2338 // We need to move on to the next range. |
2322 network_trans_.reset(); | 2339 ResetNetworkTransaction(); |
2323 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; | 2340 next_state_ = STATE_START_PARTIAL_CACHE_VALIDATION; |
2324 } | 2341 } |
2325 return result; | 2342 return result; |
2326 } | 2343 } |
2327 | 2344 |
2328 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) { | 2345 int HttpCache::Transaction::DoPartialCacheReadCompleted(int result) { |
2329 partial_->OnCacheReadCompleted(result); | 2346 partial_->OnCacheReadCompleted(result); |
2330 | 2347 |
2331 if (result == 0 && mode_ == READ_WRITE) { | 2348 if (result == 0 && mode_ == READ_WRITE) { |
2332 // We need to move on to the next range. | 2349 // We need to move on to the next range. |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2554 } | 2571 } |
2555 } | 2572 } |
2556 | 2573 |
2557 int HttpCache::Transaction::ResetCacheIOStart(int return_value) { | 2574 int HttpCache::Transaction::ResetCacheIOStart(int return_value) { |
2558 DCHECK(cache_io_start_.is_null()); | 2575 DCHECK(cache_io_start_.is_null()); |
2559 if (return_value == ERR_IO_PENDING) | 2576 if (return_value == ERR_IO_PENDING) |
2560 cache_io_start_ = base::TimeTicks::Now(); | 2577 cache_io_start_ = base::TimeTicks::Now(); |
2561 return return_value; | 2578 return return_value; |
2562 } | 2579 } |
2563 | 2580 |
| 2581 void HttpCache::Transaction::ResetNetworkTransaction() { |
| 2582 DCHECK(!old_network_trans_load_timing_); |
| 2583 DCHECK(network_trans_); |
| 2584 LoadTimingInfo load_timing; |
| 2585 if (network_trans_->GetLoadTimingInfo(&load_timing)) |
| 2586 old_network_trans_load_timing_.reset(new LoadTimingInfo(load_timing)); |
| 2587 network_trans_.reset(); |
| 2588 } |
| 2589 |
2564 } // namespace net | 2590 } // namespace net |
OLD | NEW |