OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ | 5 #ifndef NET_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ |
6 #define NET_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ | 6 #define NET_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/containers/mru_cache.h" | 11 #include "base/containers/mru_cache.h" |
12 #include "base/memory/ref_counted.h" | |
13 #include "net/quic/core/crypto/proof_source.h" | 12 #include "net/quic/core/crypto/proof_source.h" |
14 #include "net/quic/platform/api/quic_export.h" | 13 #include "net/quic/platform/api/quic_export.h" |
15 | 14 |
16 namespace net { | 15 namespace net { |
17 | 16 |
18 // QuicCompressedCertsCache is a cache to track most recently compressed certs. | 17 // QuicCompressedCertsCache is a cache to track most recently compressed certs. |
19 class QUIC_EXPORT_PRIVATE QuicCompressedCertsCache { | 18 class QUIC_EXPORT_PRIVATE QuicCompressedCertsCache { |
20 public: | 19 public: |
21 explicit QuicCompressedCertsCache(int64_t max_num_certs); | 20 explicit QuicCompressedCertsCache(int64_t max_num_certs); |
22 ~QuicCompressedCertsCache(); | 21 ~QuicCompressedCertsCache(); |
23 | 22 |
24 // Returns the pointer to the cached compressed cert if | 23 // Returns the pointer to the cached compressed cert if |
25 // |chain, client_common_set_hashes, client_cached_cert_hashes| hits cache. | 24 // |chain, client_common_set_hashes, client_cached_cert_hashes| hits cache. |
26 // Otherwise, return nullptr. | 25 // Otherwise, return nullptr. |
27 // Returned pointer might become invalid on the next call to Insert(). | 26 // Returned pointer might become invalid on the next call to Insert(). |
28 const std::string* GetCompressedCert( | 27 const std::string* GetCompressedCert( |
29 const scoped_refptr<ProofSource::Chain>& chain, | 28 const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
30 const std::string& client_common_set_hashes, | 29 const std::string& client_common_set_hashes, |
31 const std::string& client_cached_cert_hashes); | 30 const std::string& client_cached_cert_hashes); |
32 | 31 |
33 // Inserts the specified | 32 // Inserts the specified |
34 // |chain, client_common_set_hashes, | 33 // |chain, client_common_set_hashes, |
35 // client_cached_cert_hashes, compressed_cert| tuple to the cache. | 34 // client_cached_cert_hashes, compressed_cert| tuple to the cache. |
36 // If the insertion causes the cache to become overfull, entries will | 35 // If the insertion causes the cache to become overfull, entries will |
37 // be deleted in an LRU order to make room. | 36 // be deleted in an LRU order to make room. |
38 void Insert(const scoped_refptr<ProofSource::Chain>& chain, | 37 void Insert(const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
39 const std::string& client_common_set_hashes, | 38 const std::string& client_common_set_hashes, |
40 const std::string& client_cached_cert_hashes, | 39 const std::string& client_cached_cert_hashes, |
41 const std::string& compressed_cert); | 40 const std::string& compressed_cert); |
42 | 41 |
43 // Returns max number of cache entries the cache can carry. | 42 // Returns max number of cache entries the cache can carry. |
44 size_t MaxSize(); | 43 size_t MaxSize(); |
45 | 44 |
46 // Returns current number of cache entries in the cache. | 45 // Returns current number of cache entries in the cache. |
47 size_t Size(); | 46 size_t Size(); |
48 | 47 |
49 // Default size of the QuicCompressedCertsCache per server side investigation. | 48 // Default size of the QuicCompressedCertsCache per server side investigation. |
50 static const size_t kQuicCompressedCertsCacheSize = 225; | 49 static const size_t kQuicCompressedCertsCacheSize = 225; |
51 | 50 |
52 private: | 51 private: |
53 // A wrapper of the tuple: | 52 // A wrapper of the tuple: |
54 // |chain, client_common_set_hashes, client_cached_cert_hashes| | 53 // |chain, client_common_set_hashes, client_cached_cert_hashes| |
55 // to identify uncompressed representation of certs. | 54 // to identify uncompressed representation of certs. |
56 struct UncompressedCerts { | 55 struct UncompressedCerts { |
57 UncompressedCerts(); | 56 UncompressedCerts(); |
58 UncompressedCerts(const scoped_refptr<ProofSource::Chain>& chain, | 57 UncompressedCerts( |
59 const std::string* client_common_set_hashes, | 58 const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
60 const std::string* client_cached_cert_hashes); | 59 const std::string* client_common_set_hashes, |
| 60 const std::string* client_cached_cert_hashes); |
61 ~UncompressedCerts(); | 61 ~UncompressedCerts(); |
62 | 62 |
63 const scoped_refptr<ProofSource::Chain> chain; | 63 const QuicReferenceCountedPointer<ProofSource::Chain> chain; |
64 const std::string* client_common_set_hashes; | 64 const std::string* client_common_set_hashes; |
65 const std::string* client_cached_cert_hashes; | 65 const std::string* client_cached_cert_hashes; |
66 }; | 66 }; |
67 | 67 |
68 // Certs stored by QuicCompressedCertsCache where uncompressed certs data is | 68 // Certs stored by QuicCompressedCertsCache where uncompressed certs data is |
69 // used to identify the uncompressed representation of certs and | 69 // used to identify the uncompressed representation of certs and |
70 // |compressed_cert| is the cached compressed representation. | 70 // |compressed_cert| is the cached compressed representation. |
71 class CachedCerts { | 71 class CachedCerts { |
72 public: | 72 public: |
73 CachedCerts(); | 73 CachedCerts(); |
74 CachedCerts(const UncompressedCerts& uncompressed_certs, | 74 CachedCerts(const UncompressedCerts& uncompressed_certs, |
75 const std::string& compressed_cert); | 75 const std::string& compressed_cert); |
76 CachedCerts(const CachedCerts& other); | 76 CachedCerts(const CachedCerts& other); |
77 | 77 |
78 ~CachedCerts(); | 78 ~CachedCerts(); |
79 | 79 |
80 // Returns true if the |uncompressed_certs| matches uncompressed | 80 // Returns true if the |uncompressed_certs| matches uncompressed |
81 // representation of this cert. | 81 // representation of this cert. |
82 bool MatchesUncompressedCerts( | 82 bool MatchesUncompressedCerts( |
83 const UncompressedCerts& uncompressed_certs) const; | 83 const UncompressedCerts& uncompressed_certs) const; |
84 | 84 |
85 const std::string* compressed_cert() const; | 85 const std::string* compressed_cert() const; |
86 | 86 |
87 private: | 87 private: |
88 // Uncompressed certs data. | 88 // Uncompressed certs data. |
89 scoped_refptr<ProofSource::Chain> chain_; | 89 QuicReferenceCountedPointer<ProofSource::Chain> chain_; |
90 const std::string client_common_set_hashes_; | 90 const std::string client_common_set_hashes_; |
91 const std::string client_cached_cert_hashes_; | 91 const std::string client_cached_cert_hashes_; |
92 | 92 |
93 // Cached compressed representation derived from uncompressed certs. | 93 // Cached compressed representation derived from uncompressed certs. |
94 const std::string compressed_cert_; | 94 const std::string compressed_cert_; |
95 }; | 95 }; |
96 | 96 |
97 // Computes a uint64_t hash for |uncompressed_certs|. | 97 // Computes a uint64_t hash for |uncompressed_certs|. |
98 uint64_t ComputeUncompressedCertsHash( | 98 uint64_t ComputeUncompressedCertsHash( |
99 const UncompressedCerts& uncompressed_certs); | 99 const UncompressedCerts& uncompressed_certs); |
100 | 100 |
101 // Key is a unit64_t hash for UncompressedCerts. Stored associated value is | 101 // Key is a unit64_t hash for UncompressedCerts. Stored associated value is |
102 // CachedCerts which has both original uncompressed certs data and the | 102 // CachedCerts which has both original uncompressed certs data and the |
103 // compressed representation of the certs. | 103 // compressed representation of the certs. |
104 base::MRUCache<uint64_t, CachedCerts> certs_cache_; | 104 base::MRUCache<uint64_t, CachedCerts> certs_cache_; |
105 }; | 105 }; |
106 | 106 |
107 } // namespace net | 107 } // namespace net |
108 | 108 |
109 #endif // NET_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ | 109 #endif // NET_QUIC_CORE_CRYPTO_QUIC_COMPRESSED_CERTS_CACHE_H_ |
OLD | NEW |