Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: net/http/http_cache.cc

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

Powered by Google App Engine
This is Rietveld 408576698