Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 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/disk_based_cert_cache.h" | 5 #include "net/http/disk_based_cert_cache.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/metrics/histogram.h" | |
| 12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "net/base/io_buffer.h" | 15 #include "net/base/io_buffer.h" |
| 15 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 16 #include "net/disk_cache/disk_cache.h" | 17 #include "net/disk_cache/disk_cache.h" |
| 17 | 18 |
| 18 namespace net { | 19 namespace net { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // TODO(brandonsalmon): change this number to improve performance. | 23 // TODO(brandonsalmon): change this number to improve performance. |
| 23 const size_t kMemoryCacheMaxSize = 30; | 24 const size_t kMemoryCacheMaxSize = 30; |
| 24 | 25 |
| 25 // Used to obtain a unique cache key for a certificate in the form of | 26 // Used to obtain a unique cache key for a certificate in the form of |
| 26 // "cert:<hash>". | 27 // "cert:<hash>". |
| 27 std::string GetCacheKeyToCert(const X509Certificate::OSCertHandle cert_handle) { | 28 std::string GetCacheKeyForCert( |
| 29 const X509Certificate::OSCertHandle cert_handle) { | |
| 28 SHA1HashValue fingerprint = | 30 SHA1HashValue fingerprint = |
| 29 X509Certificate::CalculateFingerprint(cert_handle); | 31 X509Certificate::CalculateFingerprint(cert_handle); |
| 30 | 32 |
| 31 return "cert:" + | 33 return "cert:" + |
| 32 base::HexEncode(fingerprint.data, arraysize(fingerprint.data)); | 34 base::HexEncode(fingerprint.data, arraysize(fingerprint.data)); |
| 33 } | 35 } |
| 34 | 36 |
| 37 void RecordMemCacheHit(bool hit) { | |
| 38 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.MemCacheHit", hit); | |
| 39 } | |
| 40 | |
| 41 void RecordDiskCacheHit(bool hit) { | |
| 42 UMA_HISTOGRAM_BOOLEAN("DiskBasedCertCache.DiskCacheHit", hit); | |
| 43 } | |
| 44 | |
| 35 } // namespace | 45 } // namespace |
| 36 | 46 |
| 37 // WriteWorkers represent pending Set jobs in the DiskBasedCertCache. Each | 47 // WriteWorkers represent pending Set jobs in the DiskBasedCertCache. Each |
| 38 // certificate requested to be cached is assigned a Writeworker on a one-to-one | 48 // certificate requested to be cached is assigned a Writeworker on a one-to-one |
| 39 // basis. The same certificate should not have multiple WriteWorkers at the same | 49 // basis. The same certificate should not have multiple WriteWorkers at the same |
| 40 // time; instead, add a user callback to the existing WriteWorker. | 50 // time; instead, add a user callback to the existing WriteWorker. |
| 41 class DiskBasedCertCache::WriteWorker { | 51 class DiskBasedCertCache::WriteWorker { |
| 42 public: | 52 public: |
| 43 // |backend| is the backend to store |certificate| in, using | 53 // |backend| is the backend to store |certificate| in, using |
| 44 // |key| as the key for the disk_cache::Entry. | 54 // |key| as the key for the disk_cache::Entry. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 canceled_(false), | 126 canceled_(false), |
| 117 entry_(NULL), | 127 entry_(NULL), |
| 118 state_(STATE_NONE), | 128 state_(STATE_NONE), |
| 119 io_buf_len_(0), | 129 io_buf_len_(0), |
| 120 cleanup_callback_(cleanup_callback), | 130 cleanup_callback_(cleanup_callback), |
| 121 io_callback_( | 131 io_callback_( |
| 122 base::Bind(&WriteWorker::OnIOComplete, base::Unretained(this))) { | 132 base::Bind(&WriteWorker::OnIOComplete, base::Unretained(this))) { |
| 123 } | 133 } |
| 124 | 134 |
| 125 DiskBasedCertCache::WriteWorker::~WriteWorker() { | 135 DiskBasedCertCache::WriteWorker::~WriteWorker() { |
| 126 X509Certificate::FreeOSCertHandle(cert_handle_); | 136 if (cert_handle_) |
| 137 X509Certificate::FreeOSCertHandle(cert_handle_); | |
| 127 if (entry_) | 138 if (entry_) |
| 128 entry_->Close(); | 139 entry_->Close(); |
| 129 } | 140 } |
| 130 | 141 |
| 131 void DiskBasedCertCache::WriteWorker::Start() { | 142 void DiskBasedCertCache::WriteWorker::Start() { |
| 132 DCHECK_EQ(STATE_NONE, state_); | 143 DCHECK_EQ(STATE_NONE, state_); |
| 133 state_ = STATE_CREATE; | 144 state_ = STATE_CREATE; |
| 134 int rv = DoLoop(OK); | 145 int rv = DoLoop(OK); |
| 135 | 146 |
| 136 if (rv == ERR_IO_PENDING) | 147 if (rv == ERR_IO_PENDING) |
| (...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 421 | 432 |
| 422 return rv; | 433 return rv; |
| 423 } | 434 } |
| 424 | 435 |
| 425 int DiskBasedCertCache::ReadWorker::DoOpen() { | 436 int DiskBasedCertCache::ReadWorker::DoOpen() { |
| 426 state_ = STATE_OPEN_COMPLETE; | 437 state_ = STATE_OPEN_COMPLETE; |
| 427 return backend_->OpenEntry(key_, &entry_, io_callback_); | 438 return backend_->OpenEntry(key_, &entry_, io_callback_); |
| 428 } | 439 } |
| 429 | 440 |
| 430 int DiskBasedCertCache::ReadWorker::DoOpenComplete(int rv) { | 441 int DiskBasedCertCache::ReadWorker::DoOpenComplete(int rv) { |
| 431 if (rv < 0) | 442 if (rv < 0) { |
| 443 // Errors other than CACHE_MISS are not recorded as either a hit | |
|
wtc
2014/07/09 21:07:36
Nit: I prefer either the original "cache miss" or
| |
| 444 // or a miss. | |
| 445 if (rv == ERR_CACHE_MISS) | |
| 446 RecordDiskCacheHit(false); | |
| 432 return rv; | 447 return rv; |
| 448 } | |
| 449 RecordDiskCacheHit(true); | |
| 433 | 450 |
| 434 state_ = STATE_READ; | 451 state_ = STATE_READ; |
| 435 return OK; | 452 return OK; |
| 436 } | 453 } |
| 437 | 454 |
| 438 int DiskBasedCertCache::ReadWorker::DoRead() { | 455 int DiskBasedCertCache::ReadWorker::DoRead() { |
| 439 state_ = STATE_READ_COMPLETE; | 456 state_ = STATE_READ_COMPLETE; |
| 440 io_buf_len_ = entry_->GetDataSize(0 /* index */); | 457 io_buf_len_ = entry_->GetDataSize(0 /* index */); |
| 441 buffer_ = new IOBuffer(io_buf_len_); | 458 buffer_ = new IOBuffer(io_buf_len_); |
| 442 return entry_->ReadData( | 459 return entry_->ReadData( |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 } | 516 } |
| 500 | 517 |
| 501 void DiskBasedCertCache::Get(const std::string& key, const GetCallback& cb) { | 518 void DiskBasedCertCache::Get(const std::string& key, const GetCallback& cb) { |
| 502 DCHECK(!key.empty()); | 519 DCHECK(!key.empty()); |
| 503 | 520 |
| 504 // If the handle is already in the MRU cache, just return that (via callback). | 521 // If the handle is already in the MRU cache, just return that (via callback). |
| 505 // Note, this will also bring the cert_handle to the front of the recency | 522 // Note, this will also bring the cert_handle to the front of the recency |
| 506 // list in the MRU cache. | 523 // list in the MRU cache. |
| 507 MRUCertCache::iterator mru_it = mru_cert_cache_.Get(key); | 524 MRUCertCache::iterator mru_it = mru_cert_cache_.Get(key); |
| 508 if (mru_it != mru_cert_cache_.end()) { | 525 if (mru_it != mru_cert_cache_.end()) { |
| 526 RecordMemCacheHit(true); | |
| 509 ++mem_cache_hits_; | 527 ++mem_cache_hits_; |
| 510 cb.Run(mru_it->second); | 528 cb.Run(mru_it->second); |
| 511 return; | 529 return; |
| 512 } | 530 } |
| 531 RecordMemCacheHit(false); | |
| 513 ++mem_cache_misses_; | 532 ++mem_cache_misses_; |
| 514 | 533 |
| 515 ReadWorkerMap::iterator it = read_worker_map_.find(key); | 534 ReadWorkerMap::iterator it = read_worker_map_.find(key); |
| 516 | 535 |
| 517 if (it == read_worker_map_.end()) { | 536 if (it == read_worker_map_.end()) { |
| 518 ReadWorker* worker = | 537 ReadWorker* worker = |
| 519 new ReadWorker(backend_, | 538 new ReadWorker(backend_, |
| 520 key, | 539 key, |
| 521 base::Bind(&DiskBasedCertCache::FinishedReadOperation, | 540 base::Bind(&DiskBasedCertCache::FinishedReadOperation, |
| 522 weak_factory_.GetWeakPtr(), | 541 weak_factory_.GetWeakPtr(), |
| 523 key)); | 542 key)); |
| 524 read_worker_map_[key] = worker; | 543 read_worker_map_[key] = worker; |
| 525 worker->AddCallback(cb); | 544 worker->AddCallback(cb); |
| 526 worker->Start(); | 545 worker->Start(); |
| 527 } else { | 546 } else { |
| 528 it->second->AddCallback(cb); | 547 it->second->AddCallback(cb); |
| 529 } | 548 } |
| 530 } | 549 } |
| 531 | 550 |
| 532 void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle, | 551 void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle, |
| 533 const SetCallback& cb) { | 552 const SetCallback& cb) { |
| 534 DCHECK(!cb.is_null()); | 553 DCHECK(!cb.is_null()); |
| 535 DCHECK(cert_handle); | 554 DCHECK(cert_handle); |
| 536 std::string key = GetCacheKeyToCert(cert_handle); | 555 std::string key = GetCacheKeyForCert(cert_handle); |
| 537 | 556 |
| 538 WriteWorkerMap::iterator it = write_worker_map_.find(key); | 557 WriteWorkerMap::iterator it = write_worker_map_.find(key); |
| 539 | 558 |
| 540 if (it == write_worker_map_.end()) { | 559 if (it == write_worker_map_.end()) { |
| 541 WriteWorker* worker = | 560 WriteWorker* worker = |
| 542 new WriteWorker(backend_, | 561 new WriteWorker(backend_, |
| 543 key, | 562 key, |
| 544 cert_handle, | 563 cert_handle, |
| 545 base::Bind(&DiskBasedCertCache::FinishedWriteOperation, | 564 base::Bind(&DiskBasedCertCache::FinishedWriteOperation, |
| 546 weak_factory_.GetWeakPtr(), | 565 weak_factory_.GetWeakPtr(), |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 564 | 583 |
| 565 void DiskBasedCertCache::FinishedWriteOperation( | 584 void DiskBasedCertCache::FinishedWriteOperation( |
| 566 const std::string& key, | 585 const std::string& key, |
| 567 X509Certificate::OSCertHandle cert_handle) { | 586 X509Certificate::OSCertHandle cert_handle) { |
| 568 write_worker_map_.erase(key); | 587 write_worker_map_.erase(key); |
| 569 if (!key.empty()) | 588 if (!key.empty()) |
| 570 mru_cert_cache_.Put(key, X509Certificate::DupOSCertHandle(cert_handle)); | 589 mru_cert_cache_.Put(key, X509Certificate::DupOSCertHandle(cert_handle)); |
| 571 } | 590 } |
| 572 | 591 |
| 573 } // namespace net | 592 } // namespace net |
| OLD | NEW |