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

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: Test fix. 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),
150 old_callback_(NULL),
147 backend_(NULL) {} 151 backend_(NULL) {}
148 WorkItem(WorkItemOperation operation, Transaction* trans, 152 WorkItem(WorkItemOperation operation, Transaction* trans,
149 OldCompletionCallback* cb, disk_cache::Backend** backend) 153 OldCompletionCallback* cb, disk_cache::Backend** backend)
150 : operation_(operation), trans_(trans), entry_(NULL), callback_(cb), 154 : operation_(operation),
155 trans_(trans),
156 entry_(NULL),
157 old_callback_(cb),
158 backend_(backend) {}
csilv 2011/12/06 00:56:08 I don't see where this constructor is used, can yo
James Hawkins 2011/12/06 17:25:12 Done.
159 WorkItem(WorkItemOperation operation, Transaction* trans,
160 const CompletionCallback& cb, disk_cache::Backend** backend)
161 : operation_(operation),
162 trans_(trans),
163 entry_(NULL),
164 callback_(cb),
165 old_callback_(NULL),
151 backend_(backend) {} 166 backend_(backend) {}
152 ~WorkItem() {} 167 ~WorkItem() {}
153 168
154 // Calls back the transaction with the result of the operation. 169 // Calls back the transaction with the result of the operation.
155 void NotifyTransaction(int result, ActiveEntry* entry) { 170 void NotifyTransaction(int result, ActiveEntry* entry) {
156 DCHECK(!entry || entry->disk_entry); 171 DCHECK(!entry || entry->disk_entry);
157 if (entry_) 172 if (entry_)
158 *entry_ = entry; 173 *entry_ = entry;
159 if (trans_) 174 if (trans_)
160 trans_->io_callback()->Run(result); 175 trans_->io_callback()->Run(result);
161 } 176 }
162 177
163 // Notifies the caller about the operation completion. Returns true if the 178 // Notifies the caller about the operation completion. Returns true if the
164 // callback was invoked. 179 // callback was invoked.
165 bool DoCallback(int result, disk_cache::Backend* backend) { 180 bool DoCallback(int result, disk_cache::Backend* backend) {
166 if (backend_) 181 if (backend_)
167 *backend_ = backend; 182 *backend_ = backend;
168 if (callback_) { 183 if (old_callback_) {
169 callback_->Run(result); 184 old_callback_->Run(result);
185 return true;
186 } else if (!callback_.is_null()) {
187 callback_.Run(result);
170 return true; 188 return true;
171 } 189 }
172 return false; 190 return false;
173 } 191 }
174 192
193 void ClearCallback() {
194 old_callback_ = NULL;
195 callback_.Reset();
196 }
197
198 bool IsValid() const {
199 return trans_ || entry_ || old_callback_ || !callback_.is_null();
200 }
201
175 WorkItemOperation operation() { return operation_; } 202 WorkItemOperation operation() { return operation_; }
176 void ClearTransaction() { trans_ = NULL; } 203 void ClearTransaction() { trans_ = NULL; }
177 void ClearEntry() { entry_ = NULL; } 204 void ClearEntry() { entry_ = NULL; }
178 void ClearCallback() { callback_ = NULL; }
179 bool Matches(Transaction* trans) const { return trans == trans_; } 205 bool Matches(Transaction* trans) const { return trans == trans_; }
180 bool IsValid() const { return trans_ || entry_ || callback_; }
181 206
182 private: 207 private:
183 WorkItemOperation operation_; 208 WorkItemOperation operation_;
184 Transaction* trans_; 209 Transaction* trans_;
185 ActiveEntry** entry_; 210 ActiveEntry** entry_;
186 OldCompletionCallback* callback_; // User callback. 211 CompletionCallback callback_;
212 OldCompletionCallback* old_callback_;
187 disk_cache::Backend** backend_; 213 disk_cache::Backend** backend_;
188 }; 214 };
189 215
190 //----------------------------------------------------------------------------- 216 //-----------------------------------------------------------------------------
191 217
192 // This class is a specialized type of OldCompletionCallback that allows us to 218 // This class is a specialized type of OldCompletionCallback that allows us to
193 // pass multiple arguments to the completion routine. 219 // pass multiple arguments to the completion routine.
194 class HttpCache::BackendCallback : public CallbackRunner<Tuple1<int> > { 220 class HttpCache::BackendCallback : public CallbackRunner<Tuple1<int> > {
195 public: 221 public:
196 BackendCallback(HttpCache* cache, PendingOp* pending_op) 222 BackendCallback(HttpCache* cache, PendingOp* pending_op)
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 delete pending_op->callback; 441 delete pending_op->callback;
416 } 442 }
417 443
418 STLDeleteElements(&pending_op->pending_queue); 444 STLDeleteElements(&pending_op->pending_queue);
419 if (delete_pending_op) 445 if (delete_pending_op)
420 delete pending_op; 446 delete pending_op;
421 } 447 }
422 } 448 }
423 449
424 int HttpCache::GetBackend(disk_cache::Backend** backend, 450 int HttpCache::GetBackend(disk_cache::Backend** backend,
425 OldCompletionCallback* callback) { 451 const CompletionCallback& callback) {
426 DCHECK(callback != NULL); 452 DCHECK(!callback.is_null());
427 453
428 if (disk_cache_.get()) { 454 if (disk_cache_.get()) {
429 *backend = disk_cache_.get(); 455 *backend = disk_cache_.get();
430 return OK; 456 return OK;
431 } 457 }
432 458
433 return CreateBackend(backend, callback); 459 return CreateBackend(backend, callback);
434 } 460 }
435 461
436 disk_cache::Backend* HttpCache::GetCurrentBackend() const { 462 disk_cache::Backend* HttpCache::GetCurrentBackend() const {
437 return disk_cache_.get(); 463 return disk_cache_.get();
438 } 464 }
439 465
440 // static 466 // static
441 bool HttpCache::ParseResponseInfo(const char* data, int len, 467 bool HttpCache::ParseResponseInfo(const char* data, int len,
442 HttpResponseInfo* response_info, 468 HttpResponseInfo* response_info,
443 bool* response_truncated) { 469 bool* response_truncated) {
444 Pickle pickle(data, len); 470 Pickle pickle(data, len);
445 return response_info->InitFromPickle(pickle, response_truncated); 471 return response_info->InitFromPickle(pickle, response_truncated);
446 } 472 }
447 473
448 void HttpCache::WriteMetadata(const GURL& url, 474 void HttpCache::WriteMetadata(const GURL& url,
449 base::Time expected_response_time, IOBuffer* buf, 475 base::Time expected_response_time, IOBuffer* buf,
450 int buf_len) { 476 int buf_len) {
451 if (!buf_len) 477 if (!buf_len)
452 return; 478 return;
453 479
454 // Do lazy initialization of disk cache if needed. 480 // Do lazy initialization of disk cache if needed.
455 if (!disk_cache_.get()) 481 if (!disk_cache_.get()) {
456 CreateBackend(NULL, NULL); // We don't care about the result. 482 // We don't care about the result.
483 CreateBackend(NULL, CompletionCallback());
484 }
457 485
458 HttpCache::Transaction* trans = new HttpCache::Transaction(this); 486 HttpCache::Transaction* trans = new HttpCache::Transaction(this);
459 MetadataWriter* writer = new MetadataWriter(trans); 487 MetadataWriter* writer = new MetadataWriter(trans);
460 488
461 // The writer will self destruct when done. 489 // The writer will self destruct when done.
462 writer->Write(url, expected_response_time, buf, buf_len); 490 writer->Write(url, expected_response_time, buf, buf_len);
463 } 491 }
464 492
465 void HttpCache::CloseAllConnections() { 493 void HttpCache::CloseAllConnections() {
466 net::HttpNetworkLayer* network = 494 net::HttpNetworkLayer* network =
(...skipping 18 matching lines...) Expand all
485 513
486 HttpRequestInfo request_info; 514 HttpRequestInfo request_info;
487 request_info.url = url; 515 request_info.url = url;
488 request_info.method = http_method; 516 request_info.method = http_method;
489 std::string key = GenerateCacheKey(&request_info); 517 std::string key = GenerateCacheKey(&request_info);
490 disk_cache_->OnExternalCacheHit(key); 518 disk_cache_->OnExternalCacheHit(key);
491 } 519 }
492 520
493 int HttpCache::CreateTransaction(scoped_ptr<HttpTransaction>* trans) { 521 int HttpCache::CreateTransaction(scoped_ptr<HttpTransaction>* trans) {
494 // Do lazy initialization of disk cache if needed. 522 // Do lazy initialization of disk cache if needed.
495 if (!disk_cache_.get()) 523 if (!disk_cache_.get()) {
496 CreateBackend(NULL, NULL); // We don't care about the result. 524 // We don't care about the result.
525 CreateBackend(NULL, CompletionCallback());
526 }
497 527
498 trans->reset(new HttpCache::Transaction(this)); 528 trans->reset(new HttpCache::Transaction(this));
499 return OK; 529 return OK;
500 } 530 }
501 531
502 HttpCache* HttpCache::GetCache() { 532 HttpCache* HttpCache::GetCache() {
503 return this; 533 return this;
504 } 534 }
505 535
506 HttpNetworkSession* HttpCache::GetSession() { 536 HttpNetworkSession* HttpCache::GetSession() {
507 net::HttpNetworkLayer* network = 537 net::HttpNetworkLayer* network =
508 static_cast<net::HttpNetworkLayer*>(network_layer_.get()); 538 static_cast<net::HttpNetworkLayer*>(network_layer_.get());
509 return network->GetSession(); 539 return network->GetSession();
510 } 540 }
511 541
512 //----------------------------------------------------------------------------- 542 //-----------------------------------------------------------------------------
513 543
514 int HttpCache::CreateBackend(disk_cache::Backend** backend, 544 int HttpCache::CreateBackend(disk_cache::Backend** backend,
515 OldCompletionCallback* callback) { 545 const CompletionCallback& callback) {
516 if (!backend_factory_.get()) 546 if (!backend_factory_.get())
517 return ERR_FAILED; 547 return ERR_FAILED;
518 548
519 building_backend_ = true; 549 building_backend_ = true;
520 550
521 scoped_ptr<WorkItem> item(new WorkItem(WI_CREATE_BACKEND, NULL, callback, 551 scoped_ptr<WorkItem> item(new WorkItem(WI_CREATE_BACKEND, NULL, callback,
522 backend)); 552 backend));
523 553
524 // This is the only operation that we can do that is not related to any given 554 // 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. 555 // entry, so we use an empty key for it.
526 PendingOp* pending_op = GetPendingOp(""); 556 PendingOp* pending_op = GetPendingOp("");
527 if (pending_op->writer) { 557 if (pending_op->writer) {
528 if (callback) 558 if (!callback.is_null())
529 pending_op->pending_queue.push_back(item.release()); 559 pending_op->pending_queue.push_back(item.release());
530 return ERR_IO_PENDING; 560 return ERR_IO_PENDING;
531 } 561 }
532 562
533 DCHECK(pending_op->pending_queue.empty()); 563 DCHECK(pending_op->pending_queue.empty());
534 564
535 pending_op->writer = item.release(); 565 pending_op->writer = item.release();
536 BackendCallback* my_callback = new BackendCallback(this, pending_op); 566 BackendCallback* my_callback = new BackendCallback(this, pending_op);
537 pending_op->callback = my_callback; 567 pending_op->callback = my_callback;
538 568
539 int rv = backend_factory_->CreateBackend(net_log_, &pending_op->backend, 569 int rv = backend_factory_->CreateBackend(net_log_, &pending_op->backend,
540 my_callback); 570 my_callback);
541 if (rv != ERR_IO_PENDING) { 571 if (rv != ERR_IO_PENDING) {
542 pending_op->writer->ClearCallback(); 572 pending_op->writer->ClearCallback();
543 my_callback->Run(rv); 573 my_callback->Run(rv);
544 } 574 }
545 575
546 return rv; 576 return rv;
547 } 577 }
548 578
549 int HttpCache::GetBackendForTransaction(Transaction* trans) { 579 int HttpCache::GetBackendForTransaction(Transaction* trans) {
550 if (disk_cache_.get()) 580 if (disk_cache_.get())
551 return OK; 581 return OK;
552 582
553 if (!building_backend_) 583 if (!building_backend_)
554 return ERR_FAILED; 584 return ERR_FAILED;
555 585
556 WorkItem* item = new WorkItem(WI_CREATE_BACKEND, trans, NULL, NULL); 586 WorkItem* item = new WorkItem(WI_CREATE_BACKEND, trans,
587 CompletionCallback(), NULL);
557 PendingOp* pending_op = GetPendingOp(""); 588 PendingOp* pending_op = GetPendingOp("");
558 DCHECK(pending_op->writer); 589 DCHECK(pending_op->writer);
559 pending_op->pending_queue.push_back(item); 590 pending_op->pending_queue.push_back(item);
560 return ERR_IO_PENDING; 591 return ERR_IO_PENDING;
561 } 592 }
562 593
563 // Generate a key that can be used inside the cache. 594 // Generate a key that can be used inside the cache.
564 std::string HttpCache::GenerateCacheKey(const HttpRequestInfo* request) { 595 std::string HttpCache::GenerateCacheKey(const HttpRequestInfo* request) {
565 // Strip out the reference, username, and password sections of the URL. 596 // Strip out the reference, username, and password sections of the URL.
566 std::string url = HttpUtil::SpecForRequest(request->url); 597 std::string url = HttpUtil::SpecForRequest(request->url);
(...skipping 594 matching lines...) Expand 10 before | Expand all | Expand 10 after
1161 building_backend_ = false; 1192 building_backend_ = false;
1162 DeletePendingOp(pending_op); 1193 DeletePendingOp(pending_op);
1163 } 1194 }
1164 1195
1165 // The cache may be gone when we return from the callback. 1196 // The cache may be gone when we return from the callback.
1166 if (!item->DoCallback(result, backend)) 1197 if (!item->DoCallback(result, backend))
1167 item->NotifyTransaction(result, NULL); 1198 item->NotifyTransaction(result, NULL);
1168 } 1199 }
1169 1200
1170 } // namespace net 1201 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698