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