| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef NET_HTTP_DISK_BASED_CERT_CACHE_H | |
| 6 #define NET_HTTP_DISK_BASED_CERT_CACHE_H | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <string> | |
| 11 | |
| 12 #include "base/callback.h" | |
| 13 #include "base/containers/hash_tables.h" | |
| 14 #include "base/containers/mru_cache.h" | |
| 15 #include "base/macros.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "net/base/net_export.h" | |
| 18 #include "net/cert/x509_certificate.h" | |
| 19 | |
| 20 namespace disk_cache { | |
| 21 class Backend; | |
| 22 } // namespace disk_cache | |
| 23 | |
| 24 namespace net { | |
| 25 | |
| 26 // DiskBasedCertCache is used to store and retrieve X.509 certificates from the | |
| 27 // cache. Each individual certificate is stored separately from its certificate | |
| 28 // chain. No more than one copy (per certificate) will be stored on disk. | |
| 29 class NET_EXPORT_PRIVATE DiskBasedCertCache { | |
| 30 public: | |
| 31 typedef base::Callback<void(const X509Certificate::OSCertHandle cert_handle)> | |
| 32 GetCallback; | |
| 33 typedef base::Callback<void(const std::string&)> SetCallback; | |
| 34 | |
| 35 // Initializes a new DiskBasedCertCache that will access the disk cache via | |
| 36 // |backend|. | |
| 37 explicit DiskBasedCertCache(disk_cache::Backend* backend); | |
| 38 ~DiskBasedCertCache(); | |
| 39 | |
| 40 // Fetches the certificate associated with |key|. If the certificate is | |
| 41 // found within the cache, |cb| will be called with the certificate. | |
| 42 // Otherwise, |cb| will be called with NULL. Callers that wish to store | |
| 43 // a reference to the certificate need to use X509Certificate::DupOSCertHandle | |
| 44 // inside |cb|. | |
| 45 void GetCertificate(const std::string& key, const GetCallback& cb); | |
| 46 | |
| 47 // Stores |cert_handle| in the cache. If |cert_handle| is successfully stored, | |
| 48 // |cb| will be called with the key. If |cb| is called with an empty | |
| 49 // string, then |cert_handle| was not stored. | |
| 50 void SetCertificate(const X509Certificate::OSCertHandle cert_handle, | |
| 51 const SetCallback& cb); | |
| 52 | |
| 53 // Returns the number of in-memory MRU cache hits that have occurred | |
| 54 // on SetCertificate and GetCertificate operations. Intended for test purposes | |
| 55 // only. | |
| 56 size_t mem_cache_hits_for_testing() const { return mem_cache_hits_; } | |
| 57 | |
| 58 // Returns the number of in-memory MRU cache misses that have occurred | |
| 59 // on SetCertificate and GetCertificate operations. Intended for test purposes | |
| 60 // only. | |
| 61 size_t mem_cache_misses_for_testing() const { return mem_cache_misses_; } | |
| 62 | |
| 63 private: | |
| 64 class ReadWorker; | |
| 65 class WriteWorker; | |
| 66 | |
| 67 // A functor used to free an OSCertHandle. Used by the MRUCertCache. | |
| 68 struct CertFree { | |
| 69 void operator()(X509Certificate::OSCertHandle cert_handle); | |
| 70 }; | |
| 71 | |
| 72 // An in-memory cache that is used to prevent redundantly reading | |
| 73 // from disk. | |
| 74 typedef base::MRUCacheBase<std::string, | |
| 75 X509Certificate::OSCertHandle, | |
| 76 CertFree> MRUCertCache; | |
| 77 | |
| 78 // ReadWorkerMap and WriteWorkerMap map cache keys to their | |
| 79 // corresponding Workers. | |
| 80 typedef base::hash_map<std::string, ReadWorker*> ReadWorkerMap; | |
| 81 typedef base::hash_map<std::string, WriteWorker*> WriteWorkerMap; | |
| 82 | |
| 83 // FinishedReadOperation and FinishedWriteOperation are used to remove | |
| 84 // workers from their respective worker maps, and perform other necessary | |
| 85 // cleanup. They are called from the workers via callback. | |
| 86 void FinishedReadOperation(const std::string& key, | |
| 87 X509Certificate::OSCertHandle cert_handle); | |
| 88 void FinishedWriteOperation(const std::string& key, | |
| 89 X509Certificate::OSCertHandle cert_handle); | |
| 90 | |
| 91 disk_cache::Backend* backend_; | |
| 92 | |
| 93 ReadWorkerMap read_worker_map_; | |
| 94 WriteWorkerMap write_worker_map_; | |
| 95 MRUCertCache mru_cert_cache_; | |
| 96 | |
| 97 int mem_cache_hits_; | |
| 98 int mem_cache_misses_; | |
| 99 | |
| 100 base::WeakPtrFactory<DiskBasedCertCache> weak_factory_; | |
| 101 DISALLOW_COPY_AND_ASSIGN(DiskBasedCertCache); | |
| 102 }; | |
| 103 | |
| 104 } // namespace net | |
| 105 | |
| 106 #endif // NET_HTTP_DISK_BASED_CERT_CACHE_H | |
| OLD | NEW |