OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/pickle.h" | 11 #include "base/pickle.h" |
12 #include "base/ref_counted.h" | 12 #include "base/ref_counted.h" |
13 #include "base/string_util.h" | 13 #include "base/string_util.h" |
14 #include "base/time.h" | 14 #include "base/time.h" |
| 15 #include "net/base/io_buffer.h" |
15 #include "net/base/load_flags.h" | 16 #include "net/base/load_flags.h" |
16 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
17 #include "net/disk_cache/disk_cache.h" | 18 #include "net/disk_cache/disk_cache.h" |
18 #include "net/http/http_network_layer.h" | 19 #include "net/http/http_network_layer.h" |
19 #include "net/http/http_request_info.h" | 20 #include "net/http/http_request_info.h" |
20 #include "net/http/http_response_info.h" | 21 #include "net/http/http_response_info.h" |
21 #include "net/http/http_transaction.h" | 22 #include "net/http/http_transaction.h" |
22 #include "net/http/http_util.h" | 23 #include "net/http/http_util.h" |
23 | 24 |
24 using base::Time; | 25 using base::Time; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 | 162 |
162 class HttpCache::Transaction : public HttpTransaction { | 163 class HttpCache::Transaction : public HttpTransaction { |
163 public: | 164 public: |
164 explicit Transaction(HttpCache* cache) | 165 explicit Transaction(HttpCache* cache) |
165 : request_(NULL), | 166 : request_(NULL), |
166 cache_(cache), | 167 cache_(cache), |
167 entry_(NULL), | 168 entry_(NULL), |
168 network_trans_(NULL), | 169 network_trans_(NULL), |
169 callback_(NULL), | 170 callback_(NULL), |
170 mode_(NONE), | 171 mode_(NONE), |
171 read_buf_(NULL), | |
172 read_offset_(0), | 172 read_offset_(0), |
173 effective_load_flags_(0), | 173 effective_load_flags_(0), |
174 final_upload_progress_(0), | 174 final_upload_progress_(0), |
175 ALLOW_THIS_IN_INITIALIZER_LIST( | 175 ALLOW_THIS_IN_INITIALIZER_LIST( |
176 network_info_callback_(this, &Transaction::OnNetworkInfoAvailable)), | 176 network_info_callback_(this, &Transaction::OnNetworkInfoAvailable)), |
177 ALLOW_THIS_IN_INITIALIZER_LIST( | 177 ALLOW_THIS_IN_INITIALIZER_LIST( |
178 network_read_callback_(this, &Transaction::OnNetworkReadCompleted)), | 178 network_read_callback_(this, &Transaction::OnNetworkReadCompleted)), |
179 ALLOW_THIS_IN_INITIALIZER_LIST( | 179 ALLOW_THIS_IN_INITIALIZER_LIST( |
180 cache_read_callback_(new CancelableCompletionCallback<Transaction>( | 180 cache_read_callback_(new CancelableCompletionCallback<Transaction>( |
181 this, &Transaction::OnCacheReadCompleted))) { | 181 this, &Transaction::OnCacheReadCompleted))) { |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 // Called to make the request conditional (to ask the server if the cached | 271 // Called to make the request conditional (to ask the server if the cached |
272 // copy is valid). Returns true if able to make the request conditional. | 272 // copy is valid). Returns true if able to make the request conditional. |
273 bool ConditionalizeRequest(); | 273 bool ConditionalizeRequest(); |
274 | 274 |
275 // Called to populate response_ from the cache entry. | 275 // Called to populate response_ from the cache entry. |
276 int ReadResponseInfoFromEntry(); | 276 int ReadResponseInfoFromEntry(); |
277 | 277 |
278 // Called to write data to the cache entry. If the write fails, then the | 278 // Called to write data to the cache entry. If the write fails, then the |
279 // cache entry is destroyed. Future calls to this function will just do | 279 // cache entry is destroyed. Future calls to this function will just do |
280 // nothing without side-effect. | 280 // nothing without side-effect. |
281 void WriteToEntry(int index, int offset, const char* data, int data_len); | 281 void WriteToEntry(int index, int offset, IOBuffer* data, int data_len); |
282 | 282 |
283 // Called to write response_ to the cache entry. | 283 // Called to write response_ to the cache entry. |
284 void WriteResponseInfoToEntry(); | 284 void WriteResponseInfoToEntry(); |
285 | 285 |
286 // Called to append response data to the cache entry. | 286 // Called to append response data to the cache entry. |
287 void AppendResponseDataToEntry(const char* data, int data_len); | 287 void AppendResponseDataToEntry(IOBuffer* data, int data_len); |
288 | 288 |
289 // Called when we are done writing to the cache entry. | 289 // Called when we are done writing to the cache entry. |
290 void DoneWritingToEntry(bool success); | 290 void DoneWritingToEntry(bool success); |
291 | 291 |
292 // Called to signal completion of the network transaction's Start method: | 292 // Called to signal completion of the network transaction's Start method: |
293 void OnNetworkInfoAvailable(int result); | 293 void OnNetworkInfoAvailable(int result); |
294 | 294 |
295 // Called to signal completion of the network transaction's Read method: | 295 // Called to signal completion of the network transaction's Read method: |
296 void OnNetworkReadCompleted(int result); | 296 void OnNetworkReadCompleted(int result); |
297 | 297 |
298 // Called to signal completion of the cache's ReadData method: | 298 // Called to signal completion of the cache's ReadData method: |
299 void OnCacheReadCompleted(int result); | 299 void OnCacheReadCompleted(int result); |
300 | 300 |
301 const HttpRequestInfo* request_; | 301 const HttpRequestInfo* request_; |
302 scoped_ptr<HttpRequestInfo> custom_request_; | 302 scoped_ptr<HttpRequestInfo> custom_request_; |
303 HttpCache* cache_; | 303 HttpCache* cache_; |
304 HttpCache::ActiveEntry* entry_; | 304 HttpCache::ActiveEntry* entry_; |
305 scoped_ptr<HttpTransaction> network_trans_; | 305 scoped_ptr<HttpTransaction> network_trans_; |
306 CompletionCallback* callback_; // consumer's callback | 306 CompletionCallback* callback_; // consumer's callback |
307 HttpResponseInfo response_; | 307 HttpResponseInfo response_; |
308 HttpResponseInfo auth_response_; | 308 HttpResponseInfo auth_response_; |
309 std::string cache_key_; | 309 std::string cache_key_; |
310 Mode mode_; | 310 Mode mode_; |
311 char* read_buf_; | 311 scoped_refptr<IOBuffer> read_buf_; |
312 int read_offset_; | 312 int read_offset_; |
313 int effective_load_flags_; | 313 int effective_load_flags_; |
314 uint64 final_upload_progress_; | 314 uint64 final_upload_progress_; |
315 CompletionCallbackImpl<Transaction> network_info_callback_; | 315 CompletionCallbackImpl<Transaction> network_info_callback_; |
316 CompletionCallbackImpl<Transaction> network_read_callback_; | 316 CompletionCallbackImpl<Transaction> network_read_callback_; |
317 scoped_refptr<CancelableCompletionCallback<Transaction> > cache_read_callback_
; | 317 scoped_refptr<CancelableCompletionCallback<Transaction> > cache_read_callback_
; |
318 }; | 318 }; |
319 | 319 |
320 HttpCache::Transaction::~Transaction() { | 320 HttpCache::Transaction::~Transaction() { |
321 if (entry_) { | 321 if (entry_) { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
428 mode_ = NONE; | 428 mode_ = NONE; |
429 } | 429 } |
430 | 430 |
431 int rv; | 431 int rv; |
432 | 432 |
433 switch (mode_) { | 433 switch (mode_) { |
434 case NONE: | 434 case NONE: |
435 case WRITE: | 435 case WRITE: |
436 DCHECK(network_trans_.get()); | 436 DCHECK(network_trans_.get()); |
437 rv = network_trans_->Read(buf, buf_len, &network_read_callback_); | 437 rv = network_trans_->Read(buf, buf_len, &network_read_callback_); |
438 read_buf_ = buf->data(); | 438 read_buf_ = buf; |
439 if (rv >= 0) | 439 if (rv >= 0) |
440 OnNetworkReadCompleted(rv); | 440 OnNetworkReadCompleted(rv); |
441 break; | 441 break; |
442 case READ: | 442 case READ: |
443 DCHECK(entry_); | 443 DCHECK(entry_); |
444 cache_read_callback_->AddRef(); // Balanced in OnCacheReadCompleted. | 444 cache_read_callback_->AddRef(); // Balanced in OnCacheReadCompleted. |
445 cache_read_callback_->UseBuffer(buf); | |
446 rv = entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_, | 445 rv = entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_, |
447 buf->data(), buf_len, | 446 buf, buf_len, cache_read_callback_); |
448 cache_read_callback_); | 447 read_buf_ = buf; |
449 read_buf_ = buf->data(); | |
450 if (rv >= 0) { | 448 if (rv >= 0) { |
451 OnCacheReadCompleted(rv); | 449 OnCacheReadCompleted(rv); |
452 } else if (rv != ERR_IO_PENDING) { | 450 } else if (rv != ERR_IO_PENDING) { |
453 cache_read_callback_->Release(); | 451 cache_read_callback_->Release(); |
454 cache_read_callback_->ReleaseBuffer(); | |
455 } | 452 } |
456 break; | 453 break; |
457 default: | 454 default: |
458 NOTREACHED(); | 455 NOTREACHED(); |
459 rv = ERR_FAILED; | 456 rv = ERR_FAILED; |
460 } | 457 } |
461 | 458 |
462 if (rv == ERR_IO_PENDING) | 459 if (rv == ERR_IO_PENDING) |
463 callback_ = callback; | 460 callback_ = callback; |
464 return rv; | 461 return rv; |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 int HttpCache::Transaction::ReadResponseInfoFromEntry() { | 767 int HttpCache::Transaction::ReadResponseInfoFromEntry() { |
771 DCHECK(entry_); | 768 DCHECK(entry_); |
772 | 769 |
773 if (!HttpCache::ReadResponseInfo(entry_->disk_entry, &response_)) | 770 if (!HttpCache::ReadResponseInfo(entry_->disk_entry, &response_)) |
774 return ERR_FAILED; | 771 return ERR_FAILED; |
775 | 772 |
776 return OK; | 773 return OK; |
777 } | 774 } |
778 | 775 |
779 void HttpCache::Transaction::WriteToEntry(int index, int offset, | 776 void HttpCache::Transaction::WriteToEntry(int index, int offset, |
780 const char* data, int data_len) { | 777 IOBuffer* data, int data_len) { |
781 if (!entry_) | 778 if (!entry_) |
782 return; | 779 return; |
783 | 780 |
784 int rv = entry_->disk_entry->WriteData(index, offset, data, data_len, NULL, | 781 int rv = entry_->disk_entry->WriteData(index, offset, data, data_len, NULL, |
785 true); | 782 true); |
786 if (rv != data_len) { | 783 if (rv != data_len) { |
787 DLOG(ERROR) << "failed to write response data to cache"; | 784 DLOG(ERROR) << "failed to write response data to cache"; |
788 DoneWritingToEntry(false); | 785 DoneWritingToEntry(false); |
789 } | 786 } |
790 } | 787 } |
(...skipping 22 matching lines...) Expand all Loading... |
813 // headers; when in record mode, record everything. | 810 // headers; when in record mode, record everything. |
814 bool skip_transient_headers = (cache_->mode() != RECORD); | 811 bool skip_transient_headers = (cache_->mode() != RECORD); |
815 | 812 |
816 if (!HttpCache::WriteResponseInfo(entry_->disk_entry, &response_, | 813 if (!HttpCache::WriteResponseInfo(entry_->disk_entry, &response_, |
817 skip_transient_headers)) { | 814 skip_transient_headers)) { |
818 DLOG(ERROR) << "failed to write response info to cache"; | 815 DLOG(ERROR) << "failed to write response info to cache"; |
819 DoneWritingToEntry(false); | 816 DoneWritingToEntry(false); |
820 } | 817 } |
821 } | 818 } |
822 | 819 |
823 void HttpCache::Transaction::AppendResponseDataToEntry(const char* data, | 820 void HttpCache::Transaction::AppendResponseDataToEntry(IOBuffer* data, |
824 int data_len) { | 821 int data_len) { |
825 if (!entry_) | 822 if (!entry_) |
826 return; | 823 return; |
827 | 824 |
828 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 825 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
829 WriteToEntry(kResponseContentIndex, current_size, data, data_len); | 826 WriteToEntry(kResponseContentIndex, current_size, data, data_len); |
830 } | 827 } |
831 | 828 |
832 void HttpCache::Transaction::DoneWritingToEntry(bool success) { | 829 void HttpCache::Transaction::DoneWritingToEntry(bool success) { |
833 if (!entry_) | 830 if (!entry_) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
902 AppendResponseDataToEntry(read_buf_, result); | 899 AppendResponseDataToEntry(read_buf_, result); |
903 } else if (result == 0) { // end of file | 900 } else if (result == 0) { // end of file |
904 DoneWritingToEntry(true); | 901 DoneWritingToEntry(true); |
905 } | 902 } |
906 HandleResult(result); | 903 HandleResult(result); |
907 } | 904 } |
908 | 905 |
909 void HttpCache::Transaction::OnCacheReadCompleted(int result) { | 906 void HttpCache::Transaction::OnCacheReadCompleted(int result) { |
910 DCHECK(cache_); | 907 DCHECK(cache_); |
911 cache_read_callback_->Release(); // Balance the AddRef() from Start(). | 908 cache_read_callback_->Release(); // Balance the AddRef() from Start(). |
912 cache_read_callback_->ReleaseBuffer(); | |
913 | 909 |
914 if (result > 0) { | 910 if (result > 0) { |
915 read_offset_ += result; | 911 read_offset_ += result; |
916 } else if (result == 0) { // end of file | 912 } else if (result == 0) { // end of file |
917 cache_->DoneReadingFromEntry(entry_, this); | 913 cache_->DoneReadingFromEntry(entry_, this); |
918 entry_ = NULL; | 914 entry_ = NULL; |
919 } | 915 } |
920 HandleResult(result); | 916 HandleResult(result); |
921 } | 917 } |
922 | 918 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
994 | 990 |
995 void HttpCache::Suspend(bool suspend) { | 991 void HttpCache::Suspend(bool suspend) { |
996 network_layer_->Suspend(suspend); | 992 network_layer_->Suspend(suspend); |
997 } | 993 } |
998 | 994 |
999 // static | 995 // static |
1000 bool HttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry, | 996 bool HttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry, |
1001 HttpResponseInfo* response_info) { | 997 HttpResponseInfo* response_info) { |
1002 int size = disk_entry->GetDataSize(kResponseInfoIndex); | 998 int size = disk_entry->GetDataSize(kResponseInfoIndex); |
1003 | 999 |
1004 std::string data; | 1000 scoped_refptr<IOBuffer> buffer = new IOBuffer(size); |
1005 int rv = disk_entry->ReadData(kResponseInfoIndex, 0, | 1001 int rv = disk_entry->ReadData(kResponseInfoIndex, 0, buffer, size, NULL); |
1006 WriteInto(&data, size + 1), | |
1007 size, NULL); | |
1008 if (rv != size) { | 1002 if (rv != size) { |
1009 DLOG(ERROR) << "ReadData failed: " << rv; | 1003 DLOG(ERROR) << "ReadData failed: " << rv; |
1010 return false; | 1004 return false; |
1011 } | 1005 } |
1012 | 1006 |
1013 Pickle pickle(data.data(), static_cast<int>(data.size())); | 1007 Pickle pickle(buffer->data(), size); |
1014 void* iter = NULL; | 1008 void* iter = NULL; |
1015 | 1009 |
1016 // read flags and verify version | 1010 // read flags and verify version |
1017 int flags; | 1011 int flags; |
1018 if (!pickle.ReadInt(&iter, &flags)) | 1012 if (!pickle.ReadInt(&iter, &flags)) |
1019 return false; | 1013 return false; |
1020 int version = flags & RESPONSE_INFO_VERSION_MASK; | 1014 int version = flags & RESPONSE_INFO_VERSION_MASK; |
1021 if (version != RESPONSE_INFO_VERSION) { | 1015 if (version != RESPONSE_INFO_VERSION) { |
1022 DLOG(ERROR) << "unexpected response info version: " << version; | 1016 DLOG(ERROR) << "unexpected response info version: " << version; |
1023 return false; | 1017 return false; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1101 if (response_info->ssl_info.cert) { | 1095 if (response_info->ssl_info.cert) { |
1102 response_info->ssl_info.cert->Persist(&pickle); | 1096 response_info->ssl_info.cert->Persist(&pickle); |
1103 pickle.WriteInt(response_info->ssl_info.cert_status); | 1097 pickle.WriteInt(response_info->ssl_info.cert_status); |
1104 } | 1098 } |
1105 if (response_info->ssl_info.security_bits != -1) | 1099 if (response_info->ssl_info.security_bits != -1) |
1106 pickle.WriteInt(response_info->ssl_info.security_bits); | 1100 pickle.WriteInt(response_info->ssl_info.security_bits); |
1107 | 1101 |
1108 if (response_info->vary_data.is_valid()) | 1102 if (response_info->vary_data.is_valid()) |
1109 response_info->vary_data.Persist(&pickle); | 1103 response_info->vary_data.Persist(&pickle); |
1110 | 1104 |
1111 const char* data = static_cast<const char*>(pickle.data()); | 1105 scoped_refptr<WrappedIOBuffer> data = new WrappedIOBuffer( |
| 1106 reinterpret_cast<const char*>(pickle.data())); |
1112 int len = static_cast<int>(pickle.size()); | 1107 int len = static_cast<int>(pickle.size()); |
1113 | 1108 |
1114 return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL, | 1109 return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL, |
1115 true) == len; | 1110 true) == len; |
1116 } | 1111 } |
1117 | 1112 |
1118 void HttpCache::DoomEntry(const std::string& key) { | 1113 void HttpCache::DoomEntry(const std::string& key) { |
1119 // Need to abandon the ActiveEntry, but any transaction attached to the entry | 1114 // Need to abandon the ActiveEntry, but any transaction attached to the entry |
1120 // should not be impacted. Dooming an entry only means that it will no | 1115 // should not be impacted. Dooming an entry only means that it will no |
1121 // longer be returned by FindActiveEntry (and it will also be destroyed once | 1116 // longer be returned by FindActiveEntry (and it will also be destroyed once |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1377 | 1372 |
1378 entry->pending_queue.erase(entry->pending_queue.begin()); | 1373 entry->pending_queue.erase(entry->pending_queue.begin()); |
1379 | 1374 |
1380 AddTransactionToEntry(entry, next); | 1375 AddTransactionToEntry(entry, next); |
1381 } | 1376 } |
1382 | 1377 |
1383 //----------------------------------------------------------------------------- | 1378 //----------------------------------------------------------------------------- |
1384 | 1379 |
1385 } // namespace net | 1380 } // namespace net |
1386 | 1381 |
OLD | NEW |