| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 10 |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 callback_(cb), | 156 callback_(cb), |
| 157 backend_(backend) {} | 157 backend_(backend) {} |
| 158 ~WorkItem() {} | 158 ~WorkItem() {} |
| 159 | 159 |
| 160 // Calls back the transaction with the result of the operation. | 160 // Calls back the transaction with the result of the operation. |
| 161 void NotifyTransaction(int result, ActiveEntry* entry) { | 161 void NotifyTransaction(int result, ActiveEntry* entry) { |
| 162 DCHECK(!entry || entry->disk_entry); | 162 DCHECK(!entry || entry->disk_entry); |
| 163 if (entry_) | 163 if (entry_) |
| 164 *entry_ = entry; | 164 *entry_ = entry; |
| 165 if (trans_) | 165 if (trans_) |
| 166 trans_->io_callback()->Run(result); | 166 trans_->io_callback().Run(result); |
| 167 } | 167 } |
| 168 | 168 |
| 169 // Notifies the caller about the operation completion. Returns true if the | 169 // Notifies the caller about the operation completion. Returns true if the |
| 170 // callback was invoked. | 170 // callback was invoked. |
| 171 bool DoCallback(int result, disk_cache::Backend* backend) { | 171 bool DoCallback(int result, disk_cache::Backend* backend) { |
| 172 if (backend_) | 172 if (backend_) |
| 173 *backend_ = backend; | 173 *backend_ = backend; |
| 174 if (!callback_.is_null()) { | 174 if (!callback_.is_null()) { |
| 175 callback_.Run(result); | 175 callback_.Run(result); |
| 176 return true; | 176 return true; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 DISALLOW_COPY_AND_ASSIGN(BackendCallback); | 224 DISALLOW_COPY_AND_ASSIGN(BackendCallback); |
| 225 }; | 225 }; |
| 226 | 226 |
| 227 //----------------------------------------------------------------------------- | 227 //----------------------------------------------------------------------------- |
| 228 | 228 |
| 229 // This class encapsulates a transaction whose only purpose is to write metadata | 229 // This class encapsulates a transaction whose only purpose is to write metadata |
| 230 // to a given entry. | 230 // to a given entry. |
| 231 class HttpCache::MetadataWriter { | 231 class HttpCache::MetadataWriter { |
| 232 public: | 232 public: |
| 233 explicit MetadataWriter(HttpCache::Transaction* trans) | 233 explicit MetadataWriter(HttpCache::Transaction* trans) |
| 234 : transaction_(trans), | 234 : transaction_(trans) { |
| 235 ALLOW_THIS_IN_INITIALIZER_LIST( | 235 } |
| 236 callback_(this, &MetadataWriter::OnIOComplete)) {} | 236 |
| 237 ~MetadataWriter() {} | 237 ~MetadataWriter() {} |
| 238 | 238 |
| 239 // Implementes the bulk of HttpCache::WriteMetadata. | 239 // Implements the bulk of HttpCache::WriteMetadata. |
| 240 void Write(const GURL& url, base::Time expected_response_time, IOBuffer* buf, | 240 void Write(const GURL& url, base::Time expected_response_time, IOBuffer* buf, |
| 241 int buf_len); | 241 int buf_len); |
| 242 | 242 |
| 243 private: | 243 private: |
| 244 void VerifyResponse(int result); | 244 void VerifyResponse(int result); |
| 245 void SelfDestroy(); | 245 void SelfDestroy(); |
| 246 void OnIOComplete(int result); | 246 void OnIOComplete(int result); |
| 247 | 247 |
| 248 scoped_ptr<HttpCache::Transaction> transaction_; | 248 scoped_ptr<HttpCache::Transaction> transaction_; |
| 249 bool verified_; | 249 bool verified_; |
| 250 scoped_refptr<IOBuffer> buf_; | 250 scoped_refptr<IOBuffer> buf_; |
| 251 int buf_len_; | 251 int buf_len_; |
| 252 base::Time expected_response_time_; | 252 base::Time expected_response_time_; |
| 253 OldCompletionCallbackImpl<MetadataWriter> callback_; | |
| 254 HttpRequestInfo request_info_; | 253 HttpRequestInfo request_info_; |
| 255 DISALLOW_COPY_AND_ASSIGN(MetadataWriter); | 254 DISALLOW_COPY_AND_ASSIGN(MetadataWriter); |
| 256 }; | 255 }; |
| 257 | 256 |
| 258 void HttpCache::MetadataWriter::Write(const GURL& url, | 257 void HttpCache::MetadataWriter::Write(const GURL& url, |
| 259 base::Time expected_response_time, | 258 base::Time expected_response_time, |
| 260 IOBuffer* buf, int buf_len) { | 259 IOBuffer* buf, int buf_len) { |
| 261 DCHECK_GT(buf_len, 0); | 260 DCHECK_GT(buf_len, 0); |
| 262 DCHECK(buf); | 261 DCHECK(buf); |
| 263 DCHECK(buf->data()); | 262 DCHECK(buf->data()); |
| 264 request_info_.url = url; | 263 request_info_.url = url; |
| 265 request_info_.method = "GET"; | 264 request_info_.method = "GET"; |
| 266 request_info_.load_flags = LOAD_ONLY_FROM_CACHE; | 265 request_info_.load_flags = LOAD_ONLY_FROM_CACHE; |
| 267 | 266 |
| 268 expected_response_time_ = expected_response_time; | 267 expected_response_time_ = expected_response_time; |
| 269 buf_ = buf; | 268 buf_ = buf; |
| 270 buf_len_ = buf_len; | 269 buf_len_ = buf_len; |
| 271 verified_ = false; | 270 verified_ = false; |
| 272 | 271 |
| 273 int rv = transaction_->Start(&request_info_, &callback_, BoundNetLog()); | 272 int rv = transaction_->Start( |
| 273 &request_info_, |
| 274 base::Bind(&MetadataWriter::OnIOComplete, base::Unretained(this)), |
| 275 BoundNetLog()); |
| 274 if (rv != ERR_IO_PENDING) | 276 if (rv != ERR_IO_PENDING) |
| 275 VerifyResponse(rv); | 277 VerifyResponse(rv); |
| 276 } | 278 } |
| 277 | 279 |
| 278 void HttpCache::MetadataWriter::VerifyResponse(int result) { | 280 void HttpCache::MetadataWriter::VerifyResponse(int result) { |
| 279 verified_ = true; | 281 verified_ = true; |
| 280 if (result != OK) | 282 if (result != OK) |
| 281 return SelfDestroy(); | 283 return SelfDestroy(); |
| 282 | 284 |
| 283 const HttpResponseInfo* response_info = transaction_->GetResponseInfo(); | 285 const HttpResponseInfo* response_info = transaction_->GetResponseInfo(); |
| (...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 900 TransactionList pending_queue; | 902 TransactionList pending_queue; |
| 901 pending_queue.swap(entry->pending_queue); | 903 pending_queue.swap(entry->pending_queue); |
| 902 | 904 |
| 903 entry->disk_entry->Doom(); | 905 entry->disk_entry->Doom(); |
| 904 DestroyEntry(entry); | 906 DestroyEntry(entry); |
| 905 | 907 |
| 906 // We need to do something about these pending entries, which now need to | 908 // We need to do something about these pending entries, which now need to |
| 907 // be added to a new entry. | 909 // be added to a new entry. |
| 908 while (!pending_queue.empty()) { | 910 while (!pending_queue.empty()) { |
| 909 // ERR_CACHE_RACE causes the transaction to restart the whole process. | 911 // ERR_CACHE_RACE causes the transaction to restart the whole process. |
| 910 pending_queue.front()->io_callback()->Run(ERR_CACHE_RACE); | 912 pending_queue.front()->io_callback().Run(ERR_CACHE_RACE); |
| 911 pending_queue.pop_front(); | 913 pending_queue.pop_front(); |
| 912 } | 914 } |
| 913 } | 915 } |
| 914 } | 916 } |
| 915 | 917 |
| 916 void HttpCache::DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans) { | 918 void HttpCache::DoneReadingFromEntry(ActiveEntry* entry, Transaction* trans) { |
| 917 DCHECK(!entry->writer); | 919 DCHECK(!entry->writer); |
| 918 | 920 |
| 919 TransactionList::iterator it = | 921 TransactionList::iterator it = |
| 920 std::find(entry->readers.begin(), entry->readers.end(), trans); | 922 std::find(entry->readers.begin(), entry->readers.end(), trans); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1044 | 1046 |
| 1045 // Promote next transaction from the pending queue. | 1047 // Promote next transaction from the pending queue. |
| 1046 Transaction* next = entry->pending_queue.front(); | 1048 Transaction* next = entry->pending_queue.front(); |
| 1047 if ((next->mode() & Transaction::WRITE) && !entry->readers.empty()) | 1049 if ((next->mode() & Transaction::WRITE) && !entry->readers.empty()) |
| 1048 return; // Have to wait. | 1050 return; // Have to wait. |
| 1049 | 1051 |
| 1050 entry->pending_queue.erase(entry->pending_queue.begin()); | 1052 entry->pending_queue.erase(entry->pending_queue.begin()); |
| 1051 | 1053 |
| 1052 int rv = AddTransactionToEntry(entry, next); | 1054 int rv = AddTransactionToEntry(entry, next); |
| 1053 if (rv != ERR_IO_PENDING) { | 1055 if (rv != ERR_IO_PENDING) { |
| 1054 next->io_callback()->Run(rv); | 1056 next->io_callback().Run(rv); |
| 1055 } | 1057 } |
| 1056 } | 1058 } |
| 1057 | 1059 |
| 1058 void HttpCache::OnIOComplete(int result, PendingOp* pending_op) { | 1060 void HttpCache::OnIOComplete(int result, PendingOp* pending_op) { |
| 1059 WorkItemOperation op = pending_op->writer->operation(); | 1061 WorkItemOperation op = pending_op->writer->operation(); |
| 1060 | 1062 |
| 1061 // Completing the creation of the backend is simpler than the other cases. | 1063 // Completing the creation of the backend is simpler than the other cases. |
| 1062 if (op == WI_CREATE_BACKEND) | 1064 if (op == WI_CREATE_BACKEND) |
| 1063 return OnBackendCreated(result, pending_op); | 1065 return OnBackendCreated(result, pending_op); |
| 1064 | 1066 |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1179 building_backend_ = false; | 1181 building_backend_ = false; |
| 1180 DeletePendingOp(pending_op); | 1182 DeletePendingOp(pending_op); |
| 1181 } | 1183 } |
| 1182 | 1184 |
| 1183 // The cache may be gone when we return from the callback. | 1185 // The cache may be gone when we return from the callback. |
| 1184 if (!item->DoCallback(result, backend)) | 1186 if (!item->DoCallback(result, backend)) |
| 1185 item->NotifyTransaction(result, NULL); | 1187 item->NotifyTransaction(result, NULL); |
| 1186 } | 1188 } |
| 1187 | 1189 |
| 1188 } // namespace net | 1190 } // namespace net |
| OLD | NEW |