| 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 |
| 11 #if defined(OS_POSIX) | 11 #if defined(OS_POSIX) |
| 12 #include <unistd.h> | 12 #include <unistd.h> |
| 13 #endif | 13 #endif |
| 14 | 14 |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/callback.h" | 16 #include "base/callback.h" |
| 17 #include "base/format_macros.h" | 17 #include "base/format_macros.h" |
| 18 #include "base/location.h" | 18 #include "base/location.h" |
| 19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
| 21 #include "base/pickle.h" | 21 #include "base/pickle.h" |
| 22 #include "base/stl_util.h" | 22 #include "base/stl_util.h" |
| 23 #include "base/string_number_conversions.h" | 23 #include "base/string_number_conversions.h" |
| 24 #include "base/string_util.h" | 24 #include "base/string_util.h" |
| 25 #include "base/stringprintf.h" | 25 #include "base/stringprintf.h" |
| 26 #include "net/base/completion_callback.h" | |
| 27 #include "net/base/io_buffer.h" | 26 #include "net/base/io_buffer.h" |
| 28 #include "net/base/load_flags.h" | 27 #include "net/base/load_flags.h" |
| 29 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 30 #include "net/disk_cache/disk_cache.h" | 29 #include "net/disk_cache/disk_cache.h" |
| 31 #include "net/http/disk_cache_based_ssl_host_info.h" | 30 #include "net/http/disk_cache_based_ssl_host_info.h" |
| 32 #include "net/http/http_cache_transaction.h" | 31 #include "net/http/http_cache_transaction.h" |
| 33 #include "net/http/http_network_layer.h" | 32 #include "net/http/http_network_layer.h" |
| 34 #include "net/http/http_network_session.h" | 33 #include "net/http/http_network_session.h" |
| 35 #include "net/http/http_request_info.h" | 34 #include "net/http/http_request_info.h" |
| 36 #include "net/http/http_response_headers.h" | 35 #include "net/http/http_response_headers.h" |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 WI_OPEN_ENTRY, | 136 WI_OPEN_ENTRY, |
| 138 WI_CREATE_ENTRY, | 137 WI_CREATE_ENTRY, |
| 139 WI_DOOM_ENTRY | 138 WI_DOOM_ENTRY |
| 140 }; | 139 }; |
| 141 | 140 |
| 142 // A work item encapsulates a single request to the backend with all the | 141 // A work item encapsulates a single request to the backend with all the |
| 143 // information needed to complete that request. | 142 // information needed to complete that request. |
| 144 class HttpCache::WorkItem { | 143 class HttpCache::WorkItem { |
| 145 public: | 144 public: |
| 146 WorkItem(WorkItemOperation operation, Transaction* trans, ActiveEntry** entry) | 145 WorkItem(WorkItemOperation operation, Transaction* trans, ActiveEntry** entry) |
| 147 : operation_(operation), | 146 : operation_(operation), trans_(trans), entry_(entry), callback_(NULL), |
| 148 trans_(trans), | |
| 149 entry_(entry), | |
| 150 backend_(NULL) {} | 147 backend_(NULL) {} |
| 151 WorkItem(WorkItemOperation operation, Transaction* trans, | 148 WorkItem(WorkItemOperation operation, Transaction* trans, |
| 152 const CompletionCallback& cb, disk_cache::Backend** backend) | 149 OldCompletionCallback* cb, disk_cache::Backend** backend) |
| 153 : operation_(operation), | 150 : operation_(operation), trans_(trans), entry_(NULL), callback_(cb), |
| 154 trans_(trans), | |
| 155 entry_(NULL), | |
| 156 callback_(cb), | |
| 157 backend_(backend) {} | 151 backend_(backend) {} |
| 158 ~WorkItem() {} | 152 ~WorkItem() {} |
| 159 | 153 |
| 160 // Calls back the transaction with the result of the operation. | 154 // Calls back the transaction with the result of the operation. |
| 161 void NotifyTransaction(int result, ActiveEntry* entry) { | 155 void NotifyTransaction(int result, ActiveEntry* entry) { |
| 162 DCHECK(!entry || entry->disk_entry); | 156 DCHECK(!entry || entry->disk_entry); |
| 163 if (entry_) | 157 if (entry_) |
| 164 *entry_ = entry; | 158 *entry_ = entry; |
| 165 if (trans_) | 159 if (trans_) |
| 166 trans_->io_callback()->Run(result); | 160 trans_->io_callback()->Run(result); |
| 167 } | 161 } |
| 168 | 162 |
| 169 // Notifies the caller about the operation completion. Returns true if the | 163 // Notifies the caller about the operation completion. Returns true if the |
| 170 // callback was invoked. | 164 // callback was invoked. |
| 171 bool DoCallback(int result, disk_cache::Backend* backend) { | 165 bool DoCallback(int result, disk_cache::Backend* backend) { |
| 172 if (backend_) | 166 if (backend_) |
| 173 *backend_ = backend; | 167 *backend_ = backend; |
| 174 if (!callback_.is_null()) { | 168 if (callback_) { |
| 175 callback_.Run(result); | 169 callback_->Run(result); |
| 176 return true; | 170 return true; |
| 177 } | 171 } |
| 178 return false; | 172 return false; |
| 179 } | 173 } |
| 180 | 174 |
| 181 void ClearCallback() { | |
| 182 callback_.Reset(); | |
| 183 } | |
| 184 | |
| 185 bool IsValid() const { | |
| 186 return trans_ || entry_ || !callback_.is_null(); | |
| 187 } | |
| 188 | |
| 189 WorkItemOperation operation() { return operation_; } | 175 WorkItemOperation operation() { return operation_; } |
| 190 void ClearTransaction() { trans_ = NULL; } | 176 void ClearTransaction() { trans_ = NULL; } |
| 191 void ClearEntry() { entry_ = NULL; } | 177 void ClearEntry() { entry_ = NULL; } |
| 178 void ClearCallback() { callback_ = NULL; } |
| 192 bool Matches(Transaction* trans) const { return trans == trans_; } | 179 bool Matches(Transaction* trans) const { return trans == trans_; } |
| 180 bool IsValid() const { return trans_ || entry_ || callback_; } |
| 193 | 181 |
| 194 private: | 182 private: |
| 195 WorkItemOperation operation_; | 183 WorkItemOperation operation_; |
| 196 Transaction* trans_; | 184 Transaction* trans_; |
| 197 ActiveEntry** entry_; | 185 ActiveEntry** entry_; |
| 198 CompletionCallback callback_; | 186 OldCompletionCallback* callback_; // User callback. |
| 199 disk_cache::Backend** backend_; | 187 disk_cache::Backend** backend_; |
| 200 }; | 188 }; |
| 201 | 189 |
| 202 //----------------------------------------------------------------------------- | 190 //----------------------------------------------------------------------------- |
| 203 | 191 |
| 204 // This class is a specialized type of OldCompletionCallback that allows us to | 192 // This class is a specialized type of OldCompletionCallback that allows us to |
| 205 // pass multiple arguments to the completion routine. | 193 // pass multiple arguments to the completion routine. |
| 206 class HttpCache::BackendCallback : public CallbackRunner<Tuple1<int> > { | 194 class HttpCache::BackendCallback : public CallbackRunner<Tuple1<int> > { |
| 207 public: | 195 public: |
| 208 BackendCallback(HttpCache* cache, PendingOp* pending_op) | 196 BackendCallback(HttpCache* cache, PendingOp* pending_op) |
| (...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 delete pending_op->callback; | 415 delete pending_op->callback; |
| 428 } | 416 } |
| 429 | 417 |
| 430 STLDeleteElements(&pending_op->pending_queue); | 418 STLDeleteElements(&pending_op->pending_queue); |
| 431 if (delete_pending_op) | 419 if (delete_pending_op) |
| 432 delete pending_op; | 420 delete pending_op; |
| 433 } | 421 } |
| 434 } | 422 } |
| 435 | 423 |
| 436 int HttpCache::GetBackend(disk_cache::Backend** backend, | 424 int HttpCache::GetBackend(disk_cache::Backend** backend, |
| 437 const CompletionCallback& callback) { | 425 OldCompletionCallback* callback) { |
| 438 DCHECK(!callback.is_null()); | 426 DCHECK(callback != NULL); |
| 439 | 427 |
| 440 if (disk_cache_.get()) { | 428 if (disk_cache_.get()) { |
| 441 *backend = disk_cache_.get(); | 429 *backend = disk_cache_.get(); |
| 442 return OK; | 430 return OK; |
| 443 } | 431 } |
| 444 | 432 |
| 445 return CreateBackend(backend, callback); | 433 return CreateBackend(backend, callback); |
| 446 } | 434 } |
| 447 | 435 |
| 448 disk_cache::Backend* HttpCache::GetCurrentBackend() const { | 436 disk_cache::Backend* HttpCache::GetCurrentBackend() const { |
| 449 return disk_cache_.get(); | 437 return disk_cache_.get(); |
| 450 } | 438 } |
| 451 | 439 |
| 452 // static | 440 // static |
| 453 bool HttpCache::ParseResponseInfo(const char* data, int len, | 441 bool HttpCache::ParseResponseInfo(const char* data, int len, |
| 454 HttpResponseInfo* response_info, | 442 HttpResponseInfo* response_info, |
| 455 bool* response_truncated) { | 443 bool* response_truncated) { |
| 456 Pickle pickle(data, len); | 444 Pickle pickle(data, len); |
| 457 return response_info->InitFromPickle(pickle, response_truncated); | 445 return response_info->InitFromPickle(pickle, response_truncated); |
| 458 } | 446 } |
| 459 | 447 |
| 460 void HttpCache::WriteMetadata(const GURL& url, | 448 void HttpCache::WriteMetadata(const GURL& url, |
| 461 base::Time expected_response_time, IOBuffer* buf, | 449 base::Time expected_response_time, IOBuffer* buf, |
| 462 int buf_len) { | 450 int buf_len) { |
| 463 if (!buf_len) | 451 if (!buf_len) |
| 464 return; | 452 return; |
| 465 | 453 |
| 466 // Do lazy initialization of disk cache if needed. | 454 // Do lazy initialization of disk cache if needed. |
| 467 if (!disk_cache_.get()) { | 455 if (!disk_cache_.get()) |
| 468 // We don't care about the result. | 456 CreateBackend(NULL, NULL); // We don't care about the result. |
| 469 CreateBackend(NULL, CompletionCallback()); | |
| 470 } | |
| 471 | 457 |
| 472 HttpCache::Transaction* trans = new HttpCache::Transaction(this); | 458 HttpCache::Transaction* trans = new HttpCache::Transaction(this); |
| 473 MetadataWriter* writer = new MetadataWriter(trans); | 459 MetadataWriter* writer = new MetadataWriter(trans); |
| 474 | 460 |
| 475 // The writer will self destruct when done. | 461 // The writer will self destruct when done. |
| 476 writer->Write(url, expected_response_time, buf, buf_len); | 462 writer->Write(url, expected_response_time, buf, buf_len); |
| 477 } | 463 } |
| 478 | 464 |
| 479 void HttpCache::CloseAllConnections() { | 465 void HttpCache::CloseAllConnections() { |
| 480 net::HttpNetworkLayer* network = | 466 net::HttpNetworkLayer* network = |
| (...skipping 18 matching lines...) Expand all Loading... |
| 499 | 485 |
| 500 HttpRequestInfo request_info; | 486 HttpRequestInfo request_info; |
| 501 request_info.url = url; | 487 request_info.url = url; |
| 502 request_info.method = http_method; | 488 request_info.method = http_method; |
| 503 std::string key = GenerateCacheKey(&request_info); | 489 std::string key = GenerateCacheKey(&request_info); |
| 504 disk_cache_->OnExternalCacheHit(key); | 490 disk_cache_->OnExternalCacheHit(key); |
| 505 } | 491 } |
| 506 | 492 |
| 507 int HttpCache::CreateTransaction(scoped_ptr<HttpTransaction>* trans) { | 493 int HttpCache::CreateTransaction(scoped_ptr<HttpTransaction>* trans) { |
| 508 // Do lazy initialization of disk cache if needed. | 494 // Do lazy initialization of disk cache if needed. |
| 509 if (!disk_cache_.get()) { | 495 if (!disk_cache_.get()) |
| 510 // We don't care about the result. | 496 CreateBackend(NULL, NULL); // We don't care about the result. |
| 511 CreateBackend(NULL, CompletionCallback()); | |
| 512 } | |
| 513 | 497 |
| 514 trans->reset(new HttpCache::Transaction(this)); | 498 trans->reset(new HttpCache::Transaction(this)); |
| 515 return OK; | 499 return OK; |
| 516 } | 500 } |
| 517 | 501 |
| 518 HttpCache* HttpCache::GetCache() { | 502 HttpCache* HttpCache::GetCache() { |
| 519 return this; | 503 return this; |
| 520 } | 504 } |
| 521 | 505 |
| 522 HttpNetworkSession* HttpCache::GetSession() { | 506 HttpNetworkSession* HttpCache::GetSession() { |
| 523 net::HttpNetworkLayer* network = | 507 net::HttpNetworkLayer* network = |
| 524 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); | 508 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); |
| 525 return network->GetSession(); | 509 return network->GetSession(); |
| 526 } | 510 } |
| 527 | 511 |
| 528 //----------------------------------------------------------------------------- | 512 //----------------------------------------------------------------------------- |
| 529 | 513 |
| 530 int HttpCache::CreateBackend(disk_cache::Backend** backend, | 514 int HttpCache::CreateBackend(disk_cache::Backend** backend, |
| 531 const CompletionCallback& callback) { | 515 OldCompletionCallback* callback) { |
| 532 if (!backend_factory_.get()) | 516 if (!backend_factory_.get()) |
| 533 return ERR_FAILED; | 517 return ERR_FAILED; |
| 534 | 518 |
| 535 building_backend_ = true; | 519 building_backend_ = true; |
| 536 | 520 |
| 537 scoped_ptr<WorkItem> item(new WorkItem(WI_CREATE_BACKEND, NULL, callback, | 521 scoped_ptr<WorkItem> item(new WorkItem(WI_CREATE_BACKEND, NULL, callback, |
| 538 backend)); | 522 backend)); |
| 539 | 523 |
| 540 // This is the only operation that we can do that is not related to any given | 524 // This is the only operation that we can do that is not related to any given |
| 541 // entry, so we use an empty key for it. | 525 // entry, so we use an empty key for it. |
| 542 PendingOp* pending_op = GetPendingOp(""); | 526 PendingOp* pending_op = GetPendingOp(""); |
| 543 if (pending_op->writer) { | 527 if (pending_op->writer) { |
| 544 if (!callback.is_null()) | 528 if (callback) |
| 545 pending_op->pending_queue.push_back(item.release()); | 529 pending_op->pending_queue.push_back(item.release()); |
| 546 return ERR_IO_PENDING; | 530 return ERR_IO_PENDING; |
| 547 } | 531 } |
| 548 | 532 |
| 549 DCHECK(pending_op->pending_queue.empty()); | 533 DCHECK(pending_op->pending_queue.empty()); |
| 550 | 534 |
| 551 pending_op->writer = item.release(); | 535 pending_op->writer = item.release(); |
| 552 BackendCallback* my_callback = new BackendCallback(this, pending_op); | 536 BackendCallback* my_callback = new BackendCallback(this, pending_op); |
| 553 pending_op->callback = my_callback; | 537 pending_op->callback = my_callback; |
| 554 | 538 |
| 555 int rv = backend_factory_->CreateBackend(net_log_, &pending_op->backend, | 539 int rv = backend_factory_->CreateBackend(net_log_, &pending_op->backend, |
| 556 my_callback); | 540 my_callback); |
| 557 if (rv != ERR_IO_PENDING) { | 541 if (rv != ERR_IO_PENDING) { |
| 558 pending_op->writer->ClearCallback(); | 542 pending_op->writer->ClearCallback(); |
| 559 my_callback->Run(rv); | 543 my_callback->Run(rv); |
| 560 } | 544 } |
| 561 | 545 |
| 562 return rv; | 546 return rv; |
| 563 } | 547 } |
| 564 | 548 |
| 565 int HttpCache::GetBackendForTransaction(Transaction* trans) { | 549 int HttpCache::GetBackendForTransaction(Transaction* trans) { |
| 566 if (disk_cache_.get()) | 550 if (disk_cache_.get()) |
| 567 return OK; | 551 return OK; |
| 568 | 552 |
| 569 if (!building_backend_) | 553 if (!building_backend_) |
| 570 return ERR_FAILED; | 554 return ERR_FAILED; |
| 571 | 555 |
| 572 WorkItem* item = new WorkItem(WI_CREATE_BACKEND, trans, | 556 WorkItem* item = new WorkItem(WI_CREATE_BACKEND, trans, NULL, NULL); |
| 573 CompletionCallback(), NULL); | |
| 574 PendingOp* pending_op = GetPendingOp(""); | 557 PendingOp* pending_op = GetPendingOp(""); |
| 575 DCHECK(pending_op->writer); | 558 DCHECK(pending_op->writer); |
| 576 pending_op->pending_queue.push_back(item); | 559 pending_op->pending_queue.push_back(item); |
| 577 return ERR_IO_PENDING; | 560 return ERR_IO_PENDING; |
| 578 } | 561 } |
| 579 | 562 |
| 580 // Generate a key that can be used inside the cache. | 563 // Generate a key that can be used inside the cache. |
| 581 std::string HttpCache::GenerateCacheKey(const HttpRequestInfo* request) { | 564 std::string HttpCache::GenerateCacheKey(const HttpRequestInfo* request) { |
| 582 // Strip out the reference, username, and password sections of the URL. | 565 // Strip out the reference, username, and password sections of the URL. |
| 583 std::string url = HttpUtil::SpecForRequest(request->url); | 566 std::string url = HttpUtil::SpecForRequest(request->url); |
| (...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1178 building_backend_ = false; | 1161 building_backend_ = false; |
| 1179 DeletePendingOp(pending_op); | 1162 DeletePendingOp(pending_op); |
| 1180 } | 1163 } |
| 1181 | 1164 |
| 1182 // The cache may be gone when we return from the callback. | 1165 // The cache may be gone when we return from the callback. |
| 1183 if (!item->DoCallback(result, backend)) | 1166 if (!item->DoCallback(result, backend)) |
| 1184 item->NotifyTransaction(result, NULL); | 1167 item->NotifyTransaction(result, NULL); |
| 1185 } | 1168 } |
| 1186 | 1169 |
| 1187 } // namespace net | 1170 } // namespace net |
| OLD | NEW |