Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / http / disk_based_cert_cache.h
blob347d2f0bb1dde960042f96af08acdbc6b6068db2
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.
5 #ifndef NET_HTTP_DISK_BASED_CERT_CACHE_H
6 #define NET_HTTP_DISK_BASED_CERT_CACHE_H
8 #include <string>
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 {
18 class Backend;
19 } // namespace disk_cache
21 namespace net {
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 {
27 public:
28 typedef base::Callback<void(const X509Certificate::OSCertHandle cert_handle)>
29 GetCallback;
30 typedef base::Callback<void(const std::string&)> SetCallback;
32 // Initializes a new DiskBasedCertCache that will access the disk cache via
33 // |backend|.
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
41 // inside |cb|.
42 void GetCertificate(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 SetCertificate(const X509Certificate::OSCertHandle cert_handle,
48 const SetCallback& cb);
50 // Returns the number of in-memory MRU cache hits that have occurred
51 // on SetCertificate and GetCertificate operations. Intended for test purposes
52 // only.
53 size_t mem_cache_hits_for_testing() const { return mem_cache_hits_; }
55 // Returns the number of in-memory MRU cache misses that have occurred
56 // on SetCertificate and GetCertificate operations. Intended for test purposes
57 // only.
58 size_t mem_cache_misses_for_testing() const { return mem_cache_misses_; }
60 private:
61 class ReadWorker;
62 class WriteWorker;
64 // A functor used to free an OSCertHandle. Used by the MRUCertCache.
65 struct CertFree {
66 void operator()(X509Certificate::OSCertHandle cert_handle);
69 // An in-memory cache that is used to prevent redundantly reading
70 // from disk.
71 typedef base::MRUCacheBase<std::string,
72 X509Certificate::OSCertHandle,
73 CertFree> MRUCertCache;
75 // ReadWorkerMap and WriteWorkerMap map cache keys to their
76 // corresponding Workers.
77 typedef base::hash_map<std::string, ReadWorker*> ReadWorkerMap;
78 typedef base::hash_map<std::string, WriteWorker*> WriteWorkerMap;
80 // FinishedReadOperation and FinishedWriteOperation are used to remove
81 // workers from their respective worker maps, and perform other necessary
82 // cleanup. They are called from the workers via callback.
83 void FinishedReadOperation(const std::string& key,
84 X509Certificate::OSCertHandle cert_handle);
85 void FinishedWriteOperation(const std::string& key,
86 X509Certificate::OSCertHandle cert_handle);
88 disk_cache::Backend* backend_;
90 ReadWorkerMap read_worker_map_;
91 WriteWorkerMap write_worker_map_;
92 MRUCertCache mru_cert_cache_;
94 int mem_cache_hits_;
95 int mem_cache_misses_;
97 base::WeakPtrFactory<DiskBasedCertCache> weak_factory_;
98 DISALLOW_COPY_AND_ASSIGN(DiskBasedCertCache);
101 } // namespace net
103 #endif // NET_HTTP_DISK_BASED_CERT_CACHE_H