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" | |
16 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
17 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
18 #include "net/disk_cache/disk_cache.h" | 17 #include "net/disk_cache/disk_cache.h" |
19 #include "net/http/http_network_layer.h" | 18 #include "net/http/http_network_layer.h" |
20 #include "net/http/http_request_info.h" | 19 #include "net/http/http_request_info.h" |
21 #include "net/http/http_response_info.h" | 20 #include "net/http/http_response_info.h" |
22 #include "net/http/http_transaction.h" | 21 #include "net/http/http_transaction.h" |
23 #include "net/http/http_util.h" | 22 #include "net/http/http_util.h" |
24 | 23 |
25 using base::Time; | 24 using base::Time; |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 | 161 |
163 class HttpCache::Transaction : public HttpTransaction { | 162 class HttpCache::Transaction : public HttpTransaction { |
164 public: | 163 public: |
165 explicit Transaction(HttpCache* cache) | 164 explicit Transaction(HttpCache* cache) |
166 : request_(NULL), | 165 : request_(NULL), |
167 cache_(cache), | 166 cache_(cache), |
168 entry_(NULL), | 167 entry_(NULL), |
169 network_trans_(NULL), | 168 network_trans_(NULL), |
170 callback_(NULL), | 169 callback_(NULL), |
171 mode_(NONE), | 170 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, IOBuffer* data, int data_len); | 281 void WriteToEntry(int index, int offset, const char* 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(IOBuffer* data, int data_len); | 287 void AppendResponseDataToEntry(const char* 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 scoped_refptr<IOBuffer> read_buf_; | 311 char* 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; | 438 read_buf_ = buf->data(); |
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); |
445 rv = entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_, | 446 rv = entry_->disk_entry->ReadData(kResponseContentIndex, read_offset_, |
446 buf, buf_len, cache_read_callback_); | 447 buf->data(), buf_len, |
447 read_buf_ = buf; | 448 cache_read_callback_); |
| 449 read_buf_ = buf->data(); |
448 if (rv >= 0) { | 450 if (rv >= 0) { |
449 OnCacheReadCompleted(rv); | 451 OnCacheReadCompleted(rv); |
450 } else if (rv != ERR_IO_PENDING) { | 452 } else if (rv != ERR_IO_PENDING) { |
451 cache_read_callback_->Release(); | 453 cache_read_callback_->Release(); |
| 454 cache_read_callback_->ReleaseBuffer(); |
452 } | 455 } |
453 break; | 456 break; |
454 default: | 457 default: |
455 NOTREACHED(); | 458 NOTREACHED(); |
456 rv = ERR_FAILED; | 459 rv = ERR_FAILED; |
457 } | 460 } |
458 | 461 |
459 if (rv == ERR_IO_PENDING) | 462 if (rv == ERR_IO_PENDING) |
460 callback_ = callback; | 463 callback_ = callback; |
461 return rv; | 464 return rv; |
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
767 int HttpCache::Transaction::ReadResponseInfoFromEntry() { | 770 int HttpCache::Transaction::ReadResponseInfoFromEntry() { |
768 DCHECK(entry_); | 771 DCHECK(entry_); |
769 | 772 |
770 if (!HttpCache::ReadResponseInfo(entry_->disk_entry, &response_)) | 773 if (!HttpCache::ReadResponseInfo(entry_->disk_entry, &response_)) |
771 return ERR_FAILED; | 774 return ERR_FAILED; |
772 | 775 |
773 return OK; | 776 return OK; |
774 } | 777 } |
775 | 778 |
776 void HttpCache::Transaction::WriteToEntry(int index, int offset, | 779 void HttpCache::Transaction::WriteToEntry(int index, int offset, |
777 IOBuffer* data, int data_len) { | 780 const char* data, int data_len) { |
778 if (!entry_) | 781 if (!entry_) |
779 return; | 782 return; |
780 | 783 |
781 int rv = entry_->disk_entry->WriteData(index, offset, data, data_len, NULL, | 784 int rv = entry_->disk_entry->WriteData(index, offset, data, data_len, NULL, |
782 true); | 785 true); |
783 if (rv != data_len) { | 786 if (rv != data_len) { |
784 DLOG(ERROR) << "failed to write response data to cache"; | 787 DLOG(ERROR) << "failed to write response data to cache"; |
785 DoneWritingToEntry(false); | 788 DoneWritingToEntry(false); |
786 } | 789 } |
787 } | 790 } |
(...skipping 22 matching lines...) Expand all Loading... |
810 // headers; when in record mode, record everything. | 813 // headers; when in record mode, record everything. |
811 bool skip_transient_headers = (cache_->mode() != RECORD); | 814 bool skip_transient_headers = (cache_->mode() != RECORD); |
812 | 815 |
813 if (!HttpCache::WriteResponseInfo(entry_->disk_entry, &response_, | 816 if (!HttpCache::WriteResponseInfo(entry_->disk_entry, &response_, |
814 skip_transient_headers)) { | 817 skip_transient_headers)) { |
815 DLOG(ERROR) << "failed to write response info to cache"; | 818 DLOG(ERROR) << "failed to write response info to cache"; |
816 DoneWritingToEntry(false); | 819 DoneWritingToEntry(false); |
817 } | 820 } |
818 } | 821 } |
819 | 822 |
820 void HttpCache::Transaction::AppendResponseDataToEntry(IOBuffer* data, | 823 void HttpCache::Transaction::AppendResponseDataToEntry(const char* data, |
821 int data_len) { | 824 int data_len) { |
822 if (!entry_) | 825 if (!entry_) |
823 return; | 826 return; |
824 | 827 |
825 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); | 828 int current_size = entry_->disk_entry->GetDataSize(kResponseContentIndex); |
826 WriteToEntry(kResponseContentIndex, current_size, data, data_len); | 829 WriteToEntry(kResponseContentIndex, current_size, data, data_len); |
827 } | 830 } |
828 | 831 |
829 void HttpCache::Transaction::DoneWritingToEntry(bool success) { | 832 void HttpCache::Transaction::DoneWritingToEntry(bool success) { |
830 if (!entry_) | 833 if (!entry_) |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
899 AppendResponseDataToEntry(read_buf_, result); | 902 AppendResponseDataToEntry(read_buf_, result); |
900 } else if (result == 0) { // end of file | 903 } else if (result == 0) { // end of file |
901 DoneWritingToEntry(true); | 904 DoneWritingToEntry(true); |
902 } | 905 } |
903 HandleResult(result); | 906 HandleResult(result); |
904 } | 907 } |
905 | 908 |
906 void HttpCache::Transaction::OnCacheReadCompleted(int result) { | 909 void HttpCache::Transaction::OnCacheReadCompleted(int result) { |
907 DCHECK(cache_); | 910 DCHECK(cache_); |
908 cache_read_callback_->Release(); // Balance the AddRef() from Start(). | 911 cache_read_callback_->Release(); // Balance the AddRef() from Start(). |
| 912 cache_read_callback_->ReleaseBuffer(); |
909 | 913 |
910 if (result > 0) { | 914 if (result > 0) { |
911 read_offset_ += result; | 915 read_offset_ += result; |
912 } else if (result == 0) { // end of file | 916 } else if (result == 0) { // end of file |
913 cache_->DoneReadingFromEntry(entry_, this); | 917 cache_->DoneReadingFromEntry(entry_, this); |
914 entry_ = NULL; | 918 entry_ = NULL; |
915 } | 919 } |
916 HandleResult(result); | 920 HandleResult(result); |
917 } | 921 } |
918 | 922 |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
990 | 994 |
991 void HttpCache::Suspend(bool suspend) { | 995 void HttpCache::Suspend(bool suspend) { |
992 network_layer_->Suspend(suspend); | 996 network_layer_->Suspend(suspend); |
993 } | 997 } |
994 | 998 |
995 // static | 999 // static |
996 bool HttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry, | 1000 bool HttpCache::ReadResponseInfo(disk_cache::Entry* disk_entry, |
997 HttpResponseInfo* response_info) { | 1001 HttpResponseInfo* response_info) { |
998 int size = disk_entry->GetDataSize(kResponseInfoIndex); | 1002 int size = disk_entry->GetDataSize(kResponseInfoIndex); |
999 | 1003 |
1000 scoped_refptr<IOBuffer> buffer = new IOBuffer(size); | 1004 std::string data; |
1001 int rv = disk_entry->ReadData(kResponseInfoIndex, 0, buffer, size, NULL); | 1005 int rv = disk_entry->ReadData(kResponseInfoIndex, 0, |
| 1006 WriteInto(&data, size + 1), |
| 1007 size, NULL); |
1002 if (rv != size) { | 1008 if (rv != size) { |
1003 DLOG(ERROR) << "ReadData failed: " << rv; | 1009 DLOG(ERROR) << "ReadData failed: " << rv; |
1004 return false; | 1010 return false; |
1005 } | 1011 } |
1006 | 1012 |
1007 Pickle pickle(buffer->data(), size); | 1013 Pickle pickle(data.data(), static_cast<int>(data.size())); |
1008 void* iter = NULL; | 1014 void* iter = NULL; |
1009 | 1015 |
1010 // read flags and verify version | 1016 // read flags and verify version |
1011 int flags; | 1017 int flags; |
1012 if (!pickle.ReadInt(&iter, &flags)) | 1018 if (!pickle.ReadInt(&iter, &flags)) |
1013 return false; | 1019 return false; |
1014 int version = flags & RESPONSE_INFO_VERSION_MASK; | 1020 int version = flags & RESPONSE_INFO_VERSION_MASK; |
1015 if (version != RESPONSE_INFO_VERSION) { | 1021 if (version != RESPONSE_INFO_VERSION) { |
1016 DLOG(ERROR) << "unexpected response info version: " << version; | 1022 DLOG(ERROR) << "unexpected response info version: " << version; |
1017 return false; | 1023 return false; |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1095 if (response_info->ssl_info.cert) { | 1101 if (response_info->ssl_info.cert) { |
1096 response_info->ssl_info.cert->Persist(&pickle); | 1102 response_info->ssl_info.cert->Persist(&pickle); |
1097 pickle.WriteInt(response_info->ssl_info.cert_status); | 1103 pickle.WriteInt(response_info->ssl_info.cert_status); |
1098 } | 1104 } |
1099 if (response_info->ssl_info.security_bits != -1) | 1105 if (response_info->ssl_info.security_bits != -1) |
1100 pickle.WriteInt(response_info->ssl_info.security_bits); | 1106 pickle.WriteInt(response_info->ssl_info.security_bits); |
1101 | 1107 |
1102 if (response_info->vary_data.is_valid()) | 1108 if (response_info->vary_data.is_valid()) |
1103 response_info->vary_data.Persist(&pickle); | 1109 response_info->vary_data.Persist(&pickle); |
1104 | 1110 |
1105 scoped_refptr<WrappedIOBuffer> data = new WrappedIOBuffer( | 1111 const char* data = static_cast<const char*>(pickle.data()); |
1106 reinterpret_cast<const char*>(pickle.data())); | |
1107 int len = static_cast<int>(pickle.size()); | 1112 int len = static_cast<int>(pickle.size()); |
1108 | 1113 |
1109 return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL, | 1114 return disk_entry->WriteData(kResponseInfoIndex, 0, data, len, NULL, |
1110 true) == len; | 1115 true) == len; |
1111 } | 1116 } |
1112 | 1117 |
1113 void HttpCache::DoomEntry(const std::string& key) { | 1118 void HttpCache::DoomEntry(const std::string& key) { |
1114 // Need to abandon the ActiveEntry, but any transaction attached to the entry | 1119 // Need to abandon the ActiveEntry, but any transaction attached to the entry |
1115 // should not be impacted. Dooming an entry only means that it will no | 1120 // should not be impacted. Dooming an entry only means that it will no |
1116 // longer be returned by FindActiveEntry (and it will also be destroyed once | 1121 // 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... |
1372 | 1377 |
1373 entry->pending_queue.erase(entry->pending_queue.begin()); | 1378 entry->pending_queue.erase(entry->pending_queue.begin()); |
1374 | 1379 |
1375 AddTransactionToEntry(entry, next); | 1380 AddTransactionToEntry(entry, next); |
1376 } | 1381 } |
1377 | 1382 |
1378 //----------------------------------------------------------------------------- | 1383 //----------------------------------------------------------------------------- |
1379 | 1384 |
1380 } // namespace net | 1385 } // namespace net |
1381 | 1386 |
OLD | NEW |