OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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.h" | 5 #include "net/http/http_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 truncated_(false), | 183 truncated_(false), |
184 read_offset_(0), | 184 read_offset_(0), |
185 effective_load_flags_(0), | 185 effective_load_flags_(0), |
186 final_upload_progress_(0), | 186 final_upload_progress_(0), |
187 ALLOW_THIS_IN_INITIALIZER_LIST( | 187 ALLOW_THIS_IN_INITIALIZER_LIST( |
188 network_info_callback_(this, &Transaction::OnNetworkInfoAvailable)), | 188 network_info_callback_(this, &Transaction::OnNetworkInfoAvailable)), |
189 ALLOW_THIS_IN_INITIALIZER_LIST( | 189 ALLOW_THIS_IN_INITIALIZER_LIST( |
190 network_read_callback_(this, &Transaction::OnNetworkReadCompleted)), | 190 network_read_callback_(this, &Transaction::OnNetworkReadCompleted)), |
191 ALLOW_THIS_IN_INITIALIZER_LIST( | 191 ALLOW_THIS_IN_INITIALIZER_LIST( |
192 cache_read_callback_(new CancelableCompletionCallback<Transaction>( | 192 cache_read_callback_(new CancelableCompletionCallback<Transaction>( |
193 this, &Transaction::OnCacheReadCompleted))) { | 193 this, &Transaction::OnCacheReadCompleted))), |
| 194 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 195 entry_ready_callback_(new CancelableCompletionCallback<Transaction>( |
| 196 this, &Transaction::OnCacheEntryReady))) { |
194 } | 197 } |
195 | 198 |
196 // Clean up the transaction. | 199 // Clean up the transaction. |
197 virtual ~Transaction(); | 200 virtual ~Transaction(); |
198 | 201 |
199 // HttpTransaction methods: | 202 // HttpTransaction methods: |
200 virtual int Start(const HttpRequestInfo*, CompletionCallback*, LoadLog*); | 203 virtual int Start(const HttpRequestInfo*, CompletionCallback*, LoadLog*); |
201 virtual int RestartIgnoringLastError(CompletionCallback*); | 204 virtual int RestartIgnoringLastError(CompletionCallback*); |
202 virtual int RestartWithCertificate(X509Certificate* client_cert, | 205 virtual int RestartWithCertificate(X509Certificate* client_cert, |
203 CompletionCallback* callback); | 206 CompletionCallback* callback); |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 // Called to begin reading from the cache. Returns network error code. | 276 // Called to begin reading from the cache. Returns network error code. |
274 int BeginCacheRead(); | 277 int BeginCacheRead(); |
275 | 278 |
276 // Called to begin validating the cache entry. Returns network error code. | 279 // Called to begin validating the cache entry. Returns network error code. |
277 int BeginCacheValidation(); | 280 int BeginCacheValidation(); |
278 | 281 |
279 // Called to begin validating an entry that stores partial content. Returns | 282 // Called to begin validating an entry that stores partial content. Returns |
280 // a network error code. | 283 // a network error code. |
281 int BeginPartialCacheValidation(); | 284 int BeginPartialCacheValidation(); |
282 | 285 |
| 286 // Validates the entry headers against the requested range and continues with |
| 287 // the validation of the rest of the entry. Returns a network error code. |
| 288 int ValidateEntryHeadersAndContinue(bool byte_range_requested); |
| 289 |
283 // Performs the cache validation for the next chunk of data stored by the | 290 // Performs the cache validation for the next chunk of data stored by the |
284 // cache. If this chunk is not currently stored, starts the network request | 291 // cache. If this chunk is not currently stored, starts the network request |
285 // to fetch it. Returns a network error code. | 292 // to fetch it. Returns a network error code. |
286 int ContinuePartialCacheValidation(); | 293 int ContinuePartialCacheValidation(); |
287 | 294 |
288 // Called to start requests which were given an "if-modified-since" or | 295 // Called to start requests which were given an "if-modified-since" or |
289 // "if-none-match" validation header by the caller (NOT when the request was | 296 // "if-none-match" validation header by the caller (NOT when the request was |
290 // conditionalized internally in response to LOAD_VALIDATE_CACHE). | 297 // conditionalized internally in response to LOAD_VALIDATE_CACHE). |
291 // Returns a network error code. | 298 // Returns a network error code. |
292 int BeginExternallyConditionalizedRequest(); | 299 int BeginExternallyConditionalizedRequest(); |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 | 374 |
368 // Called to signal completion of the network transaction's Start method: | 375 // Called to signal completion of the network transaction's Start method: |
369 void OnNetworkInfoAvailable(int result); | 376 void OnNetworkInfoAvailable(int result); |
370 | 377 |
371 // Called to signal completion of the network transaction's Read method: | 378 // Called to signal completion of the network transaction's Read method: |
372 void OnNetworkReadCompleted(int result); | 379 void OnNetworkReadCompleted(int result); |
373 | 380 |
374 // Called to signal completion of the cache's ReadData method: | 381 // Called to signal completion of the cache's ReadData method: |
375 void OnCacheReadCompleted(int result); | 382 void OnCacheReadCompleted(int result); |
376 | 383 |
| 384 // Called to signal completion of the cache entry's ReadyForSparseIO method: |
| 385 void OnCacheEntryReady(int result); |
| 386 |
377 scoped_refptr<LoadLog> load_log_; | 387 scoped_refptr<LoadLog> load_log_; |
378 const HttpRequestInfo* request_; | 388 const HttpRequestInfo* request_; |
379 scoped_ptr<HttpRequestInfo> custom_request_; | 389 scoped_ptr<HttpRequestInfo> custom_request_; |
380 // If extra_headers specified a "if-modified-since" or "if-none-match", | 390 // If extra_headers specified a "if-modified-since" or "if-none-match", |
381 // |external_validation_| contains the value of that header. | 391 // |external_validation_| contains the value of that header. |
382 ValidationHeader external_validation_; | 392 ValidationHeader external_validation_; |
383 base::WeakPtr<HttpCache> cache_; | 393 base::WeakPtr<HttpCache> cache_; |
384 HttpCache::ActiveEntry* entry_; | 394 HttpCache::ActiveEntry* entry_; |
385 scoped_ptr<HttpTransaction> network_trans_; | 395 scoped_ptr<HttpTransaction> network_trans_; |
386 CompletionCallback* callback_; // Consumer's callback. | 396 CompletionCallback* callback_; // Consumer's callback. |
387 HttpResponseInfo response_; | 397 HttpResponseInfo response_; |
388 HttpResponseInfo auth_response_; | 398 HttpResponseInfo auth_response_; |
389 std::string cache_key_; | 399 std::string cache_key_; |
390 Mode mode_; | 400 Mode mode_; |
391 bool reading_; // We are already reading. | 401 bool reading_; // We are already reading. |
392 bool invalid_range_; // We may bypass the cache for this request. | 402 bool invalid_range_; // We may bypass the cache for this request. |
393 bool enable_range_support_; | 403 bool enable_range_support_; |
394 bool truncated_; // We don't have all the response data. | 404 bool truncated_; // We don't have all the response data. |
395 scoped_refptr<IOBuffer> read_buf_; | 405 scoped_refptr<IOBuffer> read_buf_; |
396 int read_buf_len_; | 406 int read_buf_len_; |
397 int read_offset_; | 407 int read_offset_; |
398 int effective_load_flags_; | 408 int effective_load_flags_; |
399 scoped_ptr<PartialData> partial_; // We are dealing with range requests. | 409 scoped_ptr<PartialData> partial_; // We are dealing with range requests. |
400 uint64 final_upload_progress_; | 410 uint64 final_upload_progress_; |
401 CompletionCallbackImpl<Transaction> network_info_callback_; | 411 CompletionCallbackImpl<Transaction> network_info_callback_; |
402 CompletionCallbackImpl<Transaction> network_read_callback_; | 412 CompletionCallbackImpl<Transaction> network_read_callback_; |
403 scoped_refptr<CancelableCompletionCallback<Transaction> > | 413 scoped_refptr<CancelableCompletionCallback<Transaction> > |
404 cache_read_callback_; | 414 cache_read_callback_; |
| 415 scoped_refptr<CancelableCompletionCallback<Transaction> > |
| 416 entry_ready_callback_; |
405 }; | 417 }; |
406 | 418 |
407 HttpCache::Transaction::~Transaction() { | 419 HttpCache::Transaction::~Transaction() { |
408 if (cache_) { | 420 if (cache_) { |
409 if (entry_) { | 421 if (entry_) { |
410 bool cancel_request = reading_ && enable_range_support_; | 422 bool cancel_request = reading_ && enable_range_support_; |
411 if (cancel_request && !partial_.get()) | 423 if (cancel_request) { |
412 cancel_request &= (response_.headers->response_code() == 200); | 424 if (partial_.get()) { |
| 425 entry_->disk_entry->CancelSparseIO(); |
| 426 } else { |
| 427 cancel_request &= (response_.headers->response_code() == 200); |
| 428 } |
| 429 } |
413 | 430 |
414 cache_->DoneWithEntry(entry_, this, cancel_request); | 431 cache_->DoneWithEntry(entry_, this, cancel_request); |
415 } else { | 432 } else { |
416 cache_->RemovePendingTransaction(this); | 433 cache_->RemovePendingTransaction(this); |
417 } | 434 } |
418 } | 435 } |
419 | 436 |
420 // If there is an outstanding callback, mark it as cancelled so running it | 437 // If there is an outstanding callback, mark it as cancelled so running it |
421 // does nothing. | 438 // does nothing. |
422 cache_read_callback_->Cancel(); | 439 cache_read_callback_->Cancel(); |
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
950 } | 967 } |
951 | 968 |
952 if (response_.headers->response_code() != 206 && !partial_.get() && | 969 if (response_.headers->response_code() != 206 && !partial_.get() && |
953 !truncated_) | 970 !truncated_) |
954 return BeginCacheValidation(); | 971 return BeginCacheValidation(); |
955 | 972 |
956 if (!enable_range_support_) | 973 if (!enable_range_support_) |
957 return BeginCacheValidation(); | 974 return BeginCacheValidation(); |
958 | 975 |
959 bool byte_range_requested = partial_.get() != NULL; | 976 bool byte_range_requested = partial_.get() != NULL; |
960 if (!byte_range_requested) { | 977 if (byte_range_requested) { |
| 978 if (OK != entry_->disk_entry->ReadyForSparseIO(entry_ready_callback_)) |
| 979 return ERR_IO_PENDING; |
| 980 } else { |
961 // The request is not for a range, but we have stored just ranges. | 981 // The request is not for a range, but we have stored just ranges. |
962 partial_.reset(new PartialData()); | 982 partial_.reset(new PartialData()); |
963 if (!custom_request_.get()) { | 983 if (!custom_request_.get()) { |
964 custom_request_.reset(new HttpRequestInfo(*request_)); | 984 custom_request_.reset(new HttpRequestInfo(*request_)); |
965 request_ = custom_request_.get(); | 985 request_ = custom_request_.get(); |
966 } | 986 } |
967 } | 987 } |
968 | 988 |
| 989 return ValidateEntryHeadersAndContinue(byte_range_requested); |
| 990 } |
| 991 |
| 992 int HttpCache::Transaction::ValidateEntryHeadersAndContinue( |
| 993 bool byte_range_requested) { |
| 994 DCHECK(mode_ == READ_WRITE); |
| 995 |
969 if (!partial_->UpdateFromStoredHeaders(response_.headers, entry_->disk_entry, | 996 if (!partial_->UpdateFromStoredHeaders(response_.headers, entry_->disk_entry, |
970 truncated_)) { | 997 truncated_)) { |
971 // The stored data cannot be used. Get rid of it and restart this request. | 998 // The stored data cannot be used. Get rid of it and restart this request. |
972 // We need to also reset the |truncated_| flag as a new entry is created. | 999 // We need to also reset the |truncated_| flag as a new entry is created. |
973 DoomPartialEntry(!byte_range_requested); | 1000 DoomPartialEntry(!byte_range_requested); |
974 mode_ = WRITE; | 1001 mode_ = WRITE; |
975 truncated_ = false; | 1002 truncated_ = false; |
976 return AddToEntry(); | 1003 return AddToEntry(); |
977 } | 1004 } |
978 | 1005 |
(...skipping 595 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1574 } | 1601 } |
1575 | 1602 |
1576 void HttpCache::Transaction::OnNetworkReadCompleted(int result) { | 1603 void HttpCache::Transaction::OnNetworkReadCompleted(int result) { |
1577 DoNetworkReadCompleted(result); | 1604 DoNetworkReadCompleted(result); |
1578 } | 1605 } |
1579 | 1606 |
1580 void HttpCache::Transaction::OnCacheReadCompleted(int result) { | 1607 void HttpCache::Transaction::OnCacheReadCompleted(int result) { |
1581 DoCacheReadCompleted(result); | 1608 DoCacheReadCompleted(result); |
1582 } | 1609 } |
1583 | 1610 |
| 1611 void HttpCache::Transaction::OnCacheEntryReady(int result) { |
| 1612 DCHECK_EQ(OK, result); |
| 1613 ValidateEntryHeadersAndContinue(true); |
| 1614 } |
| 1615 |
1584 //----------------------------------------------------------------------------- | 1616 //----------------------------------------------------------------------------- |
1585 | 1617 |
1586 HttpCache::HttpCache(HostResolver* host_resolver, | 1618 HttpCache::HttpCache(HostResolver* host_resolver, |
1587 ProxyService* proxy_service, | 1619 ProxyService* proxy_service, |
1588 SSLConfigService* ssl_config_service, | 1620 SSLConfigService* ssl_config_service, |
1589 const std::wstring& cache_dir, | 1621 const std::wstring& cache_dir, |
1590 int cache_size) | 1622 int cache_size) |
1591 : disk_cache_dir_(cache_dir), | 1623 : disk_cache_dir_(cache_dir), |
1592 mode_(NORMAL), | 1624 mode_(NORMAL), |
1593 type_(DISK_CACHE), | 1625 type_(DISK_CACHE), |
(...skipping 535 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2129 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); | 2161 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); |
2130 HttpNetworkSession* session = network->GetSession(); | 2162 HttpNetworkSession* session = network->GetSession(); |
2131 if (session) { | 2163 if (session) { |
2132 session->tcp_socket_pool()->CloseIdleSockets(); | 2164 session->tcp_socket_pool()->CloseIdleSockets(); |
2133 } | 2165 } |
2134 } | 2166 } |
2135 | 2167 |
2136 //----------------------------------------------------------------------------- | 2168 //----------------------------------------------------------------------------- |
2137 | 2169 |
2138 } // namespace net | 2170 } // namespace net |
OLD | NEW |