1 // Copyright 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.
5 #ifndef NET_HTTP_DISK_BASED_CERT_CACHE_H
6 #define NET_HTTP_DISK_BASED_CERT_CACHE_H
10 #include "base/callback.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/containers/mru_cache.h"
13 #include "base/memory/weak_ptr.h"
14 #include "net/base/net_export.h"
15 #include "net/cert/x509_certificate.h"
17 namespace disk_cache
{
19 } // namespace disk_cache
23 // DiskBasedCertCache is used to store and retrieve X.509 certificates from the
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.
26 class NET_EXPORT_PRIVATE DiskBasedCertCache
{
28 typedef base::Callback
<void(const X509Certificate::OSCertHandle cert_handle
)>
30 typedef base::Callback
<void(const std::string
&)> SetCallback
;
32 // Initializes a new DiskBasedCertCache that will use |backend|, which has
33 // previously been initialized, to store the certificate in the cache.
34 explicit DiskBasedCertCache(disk_cache::Backend
* backend
);
35 ~DiskBasedCertCache();
37 // Fetches the certificate associated with |key|. If the certificate is
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
40 // a reference to the certificate need to use X509Certificate::DupOSCertHandle
42 void Get(const std::string
& key
, const GetCallback
& cb
);
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
46 // string, then |cert_handle| was not stored.
47 void Set(const X509Certificate::OSCertHandle cert_handle
,
48 const SetCallback
& cb
);
50 // Returns the number of in-memory MRU cache hits that have occured
51 // on Set and Get operations. Intended for test purposes only.
52 size_t mem_cache_hits_for_testing() const { return mem_cache_hits_
; }
54 // Returns the number of in-memory MRU cache misses that have occured
55 // on Set and Get operations. Intended for test purposes only.
56 size_t mem_cache_misses_for_testing() const { return mem_cache_misses_
; }
62 // A functor used to free an OSCertHandle. Used by the MRUCertCache.
64 void operator()(X509Certificate::OSCertHandle cert_handle
);
67 // An in-memory cache that is used to prevent redundant reads and writes
68 // to and from the disk cache.
69 typedef base::MRUCacheBase
<std::string
,
70 X509Certificate::OSCertHandle
,
71 CertFree
> MRUCertCache
;
73 // ReadWorkerMap and WriteWorkerMap map cache keys to their
74 // corresponding Workers.
75 typedef base::hash_map
<std::string
, ReadWorker
*> ReadWorkerMap
;
76 typedef base::hash_map
<std::string
, WriteWorker
*> WriteWorkerMap
;
78 // FinishedReadOperation and FinishedWriteOperation are used by callbacks
79 // given to the workers to signal the DiskBasedCertCache they have completed
81 void FinishedReadOperation(const std::string
& key
,
82 X509Certificate::OSCertHandle cert_handle
);
83 void FinishedWriteOperation(const std::string
& key
,
84 X509Certificate::OSCertHandle cert_handle
);
86 disk_cache::Backend
* backend_
;
88 ReadWorkerMap read_worker_map_
;
89 WriteWorkerMap write_worker_map_
;
90 MRUCertCache mru_cert_cache_
;
93 int mem_cache_misses_
;
95 base::WeakPtrFactory
<DiskBasedCertCache
> weak_factory_
;
96 DISALLOW_COPY_AND_ASSIGN(DiskBasedCertCache
);
101 #endif // NET_HTTP_DISK_BASED_CERT_CACHE_H