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" |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 return OK; | 200 return OK; |
| 201 } | 201 } |
| 202 | 202 |
| 203 int DiskBasedCertCache::WriteWorker::DoOpen() { | 203 int DiskBasedCertCache::WriteWorker::DoOpen() { |
| 204 state_ = STATE_OPEN_COMPLETE; | 204 state_ = STATE_OPEN_COMPLETE; |
| 205 return backend_->OpenEntry(key_, &entry_, io_callback_); | 205 return backend_->OpenEntry(key_, &entry_, io_callback_); |
| 206 } | 206 } |
| 207 | 207 |
| 208 int DiskBasedCertCache::WriteWorker::DoOpenComplete(int rv) { | 208 int DiskBasedCertCache::WriteWorker::DoOpenComplete(int rv) { |
| 209 if (rv < 0) { | 209 if (rv < 0) { |
| 210 state_ = STATE_NONE; | |
| 211 return rv; | 210 return rv; |
| 212 } | 211 } |
|
wtc
2014/07/01 02:11:31
Nit: omit the curly braces in these simple if stat
| |
| 213 state_ = STATE_WRITE; | 212 state_ = STATE_WRITE; |
| 214 return OK; | 213 return OK; |
| 215 } | 214 } |
| 216 | 215 |
| 217 int DiskBasedCertCache::WriteWorker::DoWrite() { | 216 int DiskBasedCertCache::WriteWorker::DoWrite() { |
| 218 std::string write_data; | 217 std::string write_data; |
| 219 bool encoded = X509Certificate::GetDEREncoded(cert_handle_, &write_data); | 218 bool encoded = X509Certificate::GetDEREncoded(cert_handle_, &write_data); |
| 220 | 219 |
| 221 if (!encoded) { | 220 if (!encoded) { |
| 222 state_ = STATE_NONE; | |
| 223 return ERR_FAILED; | 221 return ERR_FAILED; |
| 224 } | 222 } |
| 225 | 223 |
| 226 buffer_ = new IOBuffer(write_data.size()); | 224 buffer_ = new IOBuffer(write_data.size()); |
| 227 io_buf_len_ = write_data.size(); | 225 io_buf_len_ = write_data.size(); |
| 228 memcpy(buffer_->data(), write_data.data(), io_buf_len_); | 226 memcpy(buffer_->data(), write_data.data(), io_buf_len_); |
| 229 | 227 |
| 230 state_ = STATE_WRITE_COMPLETE; | 228 state_ = STATE_WRITE_COMPLETE; |
| 231 | 229 |
| 232 return entry_->WriteData(0 /* index */, | 230 return entry_->WriteData(0 /* index */, |
| 233 0 /* offset */, | 231 0 /* offset */, |
| 234 buffer_, | 232 buffer_, |
| 235 write_data.size(), | 233 write_data.size(), |
| 236 io_callback_, | 234 io_callback_, |
| 237 true /* truncate */); | 235 true /* truncate */); |
| 238 } | 236 } |
| 239 | 237 |
| 240 int DiskBasedCertCache::WriteWorker::DoWriteComplete(int rv) { | 238 int DiskBasedCertCache::WriteWorker::DoWriteComplete(int rv) { |
| 241 state_ = STATE_NONE; | |
| 242 if (rv < io_buf_len_) | 239 if (rv < io_buf_len_) |
| 243 return ERR_FAILED; | 240 return ERR_FAILED; |
| 244 | 241 |
| 245 return OK; | 242 return OK; |
| 246 } | 243 } |
| 247 | 244 |
| 248 void DiskBasedCertCache::WriteWorker::RunCallbacks(int rv) { | 245 void DiskBasedCertCache::WriteWorker::RunCallbacks(int rv) { |
| 249 std::string key; | 246 std::string key; |
| 250 if (rv >= 0) | 247 if (rv >= 0) |
| 251 key = key_; | 248 key = key_; |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 280 // at the same time; instead, call AddCallback to add a user_callback_ to | 277 // at the same time; instead, call AddCallback to add a user_callback_ to |
| 281 // the the existing ReadWorker. | 278 // the the existing ReadWorker. |
| 282 class DiskBasedCertCache::ReadWorker { | 279 class DiskBasedCertCache::ReadWorker { |
| 283 public: | 280 public: |
| 284 // |backend| is the backend to read |certificate| from, using | 281 // |backend| is the backend to read |certificate| from, using |
| 285 // |key| as the key for the disk_cache::Entry. | 282 // |key| as the key for the disk_cache::Entry. |
| 286 // |cleanup_callback| is called to clean up this ReadWorker, | 283 // |cleanup_callback| is called to clean up this ReadWorker, |
| 287 // regardless of success or failure. | 284 // regardless of success or failure. |
| 288 ReadWorker(disk_cache::Backend* backend, | 285 ReadWorker(disk_cache::Backend* backend, |
| 289 const std::string& key, | 286 const std::string& key, |
| 290 const base::Closure& cleanup_callback); | 287 const GetCallback& cleanup_callback); |
| 291 | 288 |
| 292 ~ReadWorker(); | 289 ~ReadWorker(); |
| 293 | 290 |
| 294 // Reads the given certificate from the cache. On completion, will invoke all | 291 // Reads the given certificate from the cache. On completion, will invoke all |
| 295 // user callbacks. | 292 // user callbacks. |
| 296 void Start(); | 293 void Start(); |
| 297 | 294 |
| 298 // Adds a callback to the set of callbacks to be run when this | 295 // Adds a callback to the set of callbacks to be run when this |
| 299 // ReadWorker finishes processing. | 296 // ReadWorker finishes processing. |
| 300 void AddCallback(const GetCallback& user_callback); | 297 void AddCallback(const GetCallback& user_callback); |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 328 X509Certificate::OSCertHandle cert_handle_; | 325 X509Certificate::OSCertHandle cert_handle_; |
| 329 std::string key_; | 326 std::string key_; |
| 330 bool canceled_; | 327 bool canceled_; |
| 331 | 328 |
| 332 disk_cache::Entry* entry_; | 329 disk_cache::Entry* entry_; |
| 333 | 330 |
| 334 State state_; | 331 State state_; |
| 335 scoped_refptr<IOBuffer> buffer_; | 332 scoped_refptr<IOBuffer> buffer_; |
| 336 int io_buf_len_; | 333 int io_buf_len_; |
| 337 | 334 |
| 338 base::Closure cleanup_callback_; | 335 GetCallback cleanup_callback_; |
| 339 std::vector<GetCallback> user_callbacks_; | 336 std::vector<GetCallback> user_callbacks_; |
| 340 CompletionCallback io_callback_; | 337 CompletionCallback io_callback_; |
| 341 }; | 338 }; |
| 342 | 339 |
| 343 DiskBasedCertCache::ReadWorker::ReadWorker( | 340 DiskBasedCertCache::ReadWorker::ReadWorker(disk_cache::Backend* backend, |
| 344 disk_cache::Backend* backend, | 341 const std::string& key, |
| 345 const std::string& key, | 342 const GetCallback& cleanup_callback) |
| 346 const base::Closure& cleanup_callback) | |
| 347 : backend_(backend), | 343 : backend_(backend), |
| 348 cert_handle_(NULL), | 344 cert_handle_(NULL), |
| 349 key_(key), | 345 key_(key), |
| 350 canceled_(false), | 346 canceled_(false), |
| 351 entry_(NULL), | 347 entry_(NULL), |
| 352 state_(STATE_NONE), | 348 state_(STATE_NONE), |
| 353 io_buf_len_(0), | 349 io_buf_len_(0), |
| 354 cleanup_callback_(cleanup_callback), | 350 cleanup_callback_(cleanup_callback), |
| 355 io_callback_( | 351 io_callback_( |
| 356 base::Bind(&ReadWorker::OnIOComplete, base::Unretained(this))) { | 352 base::Bind(&ReadWorker::OnIOComplete, base::Unretained(this))) { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 return rv; | 408 return rv; |
| 413 } | 409 } |
| 414 | 410 |
| 415 int DiskBasedCertCache::ReadWorker::DoOpen() { | 411 int DiskBasedCertCache::ReadWorker::DoOpen() { |
| 416 state_ = STATE_OPEN_COMPLETE; | 412 state_ = STATE_OPEN_COMPLETE; |
| 417 return backend_->OpenEntry(key_, &entry_, io_callback_); | 413 return backend_->OpenEntry(key_, &entry_, io_callback_); |
| 418 } | 414 } |
| 419 | 415 |
| 420 int DiskBasedCertCache::ReadWorker::DoOpenComplete(int rv) { | 416 int DiskBasedCertCache::ReadWorker::DoOpenComplete(int rv) { |
| 421 if (rv < 0) { | 417 if (rv < 0) { |
| 422 state_ = STATE_NONE; | |
| 423 return rv; | 418 return rv; |
| 424 } | 419 } |
| 425 state_ = STATE_READ; | 420 state_ = STATE_READ; |
| 426 return OK; | 421 return OK; |
| 427 } | 422 } |
| 428 | 423 |
| 429 int DiskBasedCertCache::ReadWorker::DoRead() { | 424 int DiskBasedCertCache::ReadWorker::DoRead() { |
| 430 state_ = STATE_READ_COMPLETE; | 425 state_ = STATE_READ_COMPLETE; |
| 431 io_buf_len_ = entry_->GetDataSize(0 /* index */); | 426 io_buf_len_ = entry_->GetDataSize(0 /* index */); |
| 432 buffer_ = new IOBuffer(io_buf_len_); | 427 buffer_ = new IOBuffer(io_buf_len_); |
| 433 return entry_->ReadData( | 428 return entry_->ReadData( |
| 434 0 /* index */, 0 /* offset */, buffer_, io_buf_len_, io_callback_); | 429 0 /* index */, 0 /* offset */, buffer_, io_buf_len_, io_callback_); |
| 435 } | 430 } |
| 436 | 431 |
| 437 int DiskBasedCertCache::ReadWorker::DoReadComplete(int rv) { | 432 int DiskBasedCertCache::ReadWorker::DoReadComplete(int rv) { |
| 438 state_ = STATE_NONE; | |
| 439 if (rv < io_buf_len_) | 433 if (rv < io_buf_len_) |
| 440 return ERR_FAILED; | 434 return ERR_FAILED; |
| 441 | 435 |
| 442 cert_handle_ = X509Certificate::CreateOSCertHandleFromBytes(buffer_->data(), | 436 cert_handle_ = X509Certificate::CreateOSCertHandleFromBytes(buffer_->data(), |
| 443 io_buf_len_); | 437 io_buf_len_); |
| 444 if (!cert_handle_) | 438 if (!cert_handle_) |
| 445 return ERR_FAILED; | 439 return ERR_FAILED; |
| 446 | 440 |
| 447 return OK; | 441 return OK; |
| 448 } | 442 } |
| 449 | 443 |
| 450 void DiskBasedCertCache::ReadWorker::RunCallbacks() { | 444 void DiskBasedCertCache::ReadWorker::RunCallbacks() { |
| 451 for (std::vector<GetCallback>::const_iterator it = user_callbacks_.begin(); | 445 for (std::vector<GetCallback>::const_iterator it = user_callbacks_.begin(); |
| 452 it != user_callbacks_.end(); | 446 it != user_callbacks_.end(); |
| 453 ++it) { | 447 ++it) { |
| 454 it->Run(cert_handle_); | 448 it->Run(cert_handle_); |
| 455 } | 449 } |
| 456 user_callbacks_.clear(); | 450 user_callbacks_.clear(); |
| 457 } | 451 } |
| 458 | 452 |
| 459 void DiskBasedCertCache::ReadWorker::Finish(int rv) { | 453 void DiskBasedCertCache::ReadWorker::Finish(int rv) { |
| 460 cleanup_callback_.Run(); | 454 cleanup_callback_.Run(cert_handle_); |
| 461 cleanup_callback_.Reset(); | 455 cleanup_callback_.Reset(); |
| 462 RunCallbacks(); | 456 RunCallbacks(); |
| 463 delete this; | 457 delete this; |
| 464 } | 458 } |
| 465 | 459 |
| 466 void DiskBasedCertCache::ReadWorker::Cancel() { | 460 void DiskBasedCertCache::ReadWorker::Cancel() { |
| 467 canceled_ = true; | 461 canceled_ = true; |
| 468 } | 462 } |
| 469 | 463 |
| 470 DiskBasedCertCache::ReadWorker::~ReadWorker() { | 464 DiskBasedCertCache::ReadWorker::~ReadWorker() { |
| 471 if (entry_) | 465 if (entry_) |
| 472 entry_->Close(); | 466 entry_->Close(); |
| 473 if (cert_handle_) | 467 if (cert_handle_) |
| 474 X509Certificate::FreeOSCertHandle(cert_handle_); | 468 X509Certificate::FreeOSCertHandle(cert_handle_); |
| 475 } | 469 } |
| 476 | 470 |
| 477 DiskBasedCertCache::DiskBasedCertCache(disk_cache::Backend* backend) | 471 DiskBasedCertCache::DiskBasedCertCache(disk_cache::Backend* backend) |
| 478 : backend_(backend), weak_factory_(this) { | 472 : backend_(backend), mru_cert_cache_(30), weak_factory_(this) { |
|
wtc
2014/07/01 02:11:31
Define a constant with the value 30 near the top o
| |
| 479 DCHECK(backend_); | 473 DCHECK(backend_); |
| 480 } | 474 } |
| 481 | 475 |
| 482 DiskBasedCertCache::~DiskBasedCertCache() { | 476 DiskBasedCertCache::~DiskBasedCertCache() { |
| 483 for (WriteWorkerMap::iterator it = write_worker_map_.begin(); | 477 for (WriteWorkerMap::iterator it = write_worker_map_.begin(); |
| 484 it != write_worker_map_.end(); | 478 it != write_worker_map_.end(); |
| 485 ++it) { | 479 ++it) { |
| 486 it->second->Cancel(); | 480 it->second->Cancel(); |
| 487 } | 481 } |
| 488 for (ReadWorkerMap::iterator it = read_worker_map_.begin(); | 482 for (ReadWorkerMap::iterator it = read_worker_map_.begin(); |
| 489 it != read_worker_map_.end(); | 483 it != read_worker_map_.end(); |
| 490 ++it) { | 484 ++it) { |
| 491 it->second->Cancel(); | 485 it->second->Cancel(); |
| 492 } | 486 } |
| 493 } | 487 } |
| 494 | 488 |
| 495 void DiskBasedCertCache::Get(const std::string& key, const GetCallback& cb) { | 489 void DiskBasedCertCache::Get(const std::string& key, const GetCallback& cb) { |
| 496 DCHECK(!key.empty()); | 490 DCHECK(!key.empty()); |
| 497 | 491 |
| 492 // If the handle is already in the MRU cache, just return that (via callback). | |
| 493 // Note, this will also bring the cert_handle to the front | |
| 494 // of the regency list in the MRU cache. | |
|
wtc
2014/07/01 02:11:31
1. Typo: regency => recency
2. Move some of the w
| |
| 495 MRUCertCache::iterator mru_it = mru_cert_cache_.Get(key); | |
| 496 if (mru_it != mru_cert_cache_.end()) { | |
| 497 cb.Run(mru_it->second); | |
| 498 return; | |
| 499 } | |
| 500 | |
| 498 ReadWorkerMap::iterator it = read_worker_map_.find(key); | 501 ReadWorkerMap::iterator it = read_worker_map_.find(key); |
| 499 | 502 |
| 500 if (it == read_worker_map_.end()) { | 503 if (it == read_worker_map_.end()) { |
| 501 ReadWorker* worker = | 504 ReadWorker* worker = |
| 502 new ReadWorker(backend_, | 505 new ReadWorker(backend_, |
| 503 key, | 506 key, |
| 504 base::Bind(&DiskBasedCertCache::FinishedReadOperation, | 507 base::Bind(&DiskBasedCertCache::FinishedReadOperation, |
| 505 weak_factory_.GetWeakPtr(), | 508 weak_factory_.GetWeakPtr(), |
| 506 key)); | 509 key)); |
| 507 read_worker_map_[key] = worker; | 510 read_worker_map_[key] = worker; |
| 508 worker->AddCallback(cb); | 511 worker->AddCallback(cb); |
| 509 worker->Start(); | 512 worker->Start(); |
| 510 } else { | 513 } else { |
| 511 it->second->AddCallback(cb); | 514 it->second->AddCallback(cb); |
| 512 } | 515 } |
| 513 } | 516 } |
| 514 | 517 |
| 515 void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle, | 518 void DiskBasedCertCache::Set(const X509Certificate::OSCertHandle cert_handle, |
| 516 const SetCallback& cb) { | 519 const SetCallback& cb) { |
| 517 DCHECK(!cb.is_null()); | 520 DCHECK(!cb.is_null()); |
| 518 DCHECK(cert_handle); | 521 DCHECK(cert_handle); |
| 519 std::string key = GetCacheKeyToCert(cert_handle); | 522 std::string key = GetCacheKeyToCert(cert_handle); |
| 520 | 523 |
| 524 // If |cert_handle| already exists in the MRU cache, there is no need to | |
| 525 // re-write it to the disk cache. This will also bring |cert_handle| | |
| 526 // to the front of the regency list in the MRU cache. | |
|
wtc
2014/07/01 02:11:31
Typo: regency => recency
| |
| 527 if (mru_cert_cache_.Get(key) != mru_cert_cache_.end()) { | |
| 528 cb.Run(key); | |
| 529 return; | |
| 530 } | |
| 531 | |
| 532 mru_cert_cache_.Put(key, X509Certificate::DupOSCertHandle(cert_handle)); | |
|
wtc
2014/07/01 02:11:31
A comment that explains why it is advantageous and
brandonsalmon
2014/07/01 18:19:29
I no longer think this is that advantageous. (Ther
| |
| 521 WriteWorkerMap::iterator it = write_worker_map_.find(key); | 533 WriteWorkerMap::iterator it = write_worker_map_.find(key); |
| 522 | 534 |
| 523 if (it == write_worker_map_.end()) { | 535 if (it == write_worker_map_.end()) { |
| 524 WriteWorker* worker = | 536 WriteWorker* worker = |
| 525 new WriteWorker(backend_, | 537 new WriteWorker(backend_, |
| 526 key, | 538 key, |
| 527 cert_handle, | 539 cert_handle, |
| 528 base::Bind(&DiskBasedCertCache::FinishedWriteOperation, | 540 base::Bind(&DiskBasedCertCache::FinishedWriteOperation, |
| 529 weak_factory_.GetWeakPtr(), | 541 weak_factory_.GetWeakPtr(), |
| 530 key)); | 542 key)); |
| 531 write_worker_map_[key] = worker; | 543 write_worker_map_[key] = worker; |
| 532 worker->AddCallback(cb); | 544 worker->AddCallback(cb); |
| 533 worker->Start(); | 545 worker->Start(); |
| 534 } else { | 546 } else { |
| 535 it->second->AddCallback(cb); | 547 it->second->AddCallback(cb); |
| 536 } | 548 } |
| 537 } | 549 } |
| 538 | 550 |
| 539 void DiskBasedCertCache::FinishedWriteOperation(const std::string& key) { | 551 void DiskBasedCertCache::FinishedWriteOperation(const std::string& key) { |
| 540 write_worker_map_.erase(key); | 552 write_worker_map_.erase(key); |
| 541 } | 553 } |
| 542 | 554 |
| 543 void DiskBasedCertCache::FinishedReadOperation(const std::string& key) { | 555 void DiskBasedCertCache::FinishedReadOperation( |
| 556 const std::string& key, | |
| 557 X509Certificate::OSCertHandle cert_handle) { | |
| 558 if (cert_handle) | |
| 559 mru_cert_cache_.Put(key, X509Certificate::DupOSCertHandle(cert_handle)); | |
| 544 read_worker_map_.erase(key); | 560 read_worker_map_.erase(key); |
| 545 } | 561 } |
| 546 | 562 |
| 547 } // namespace net | 563 } // namespace net |
| OLD | NEW |