| 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 #include "net/quic/core/crypto/quic_compressed_certs_cache.h" | 5 #include "net/quic/core/crypto/quic_compressed_certs_cache.h" |
| 6 | 6 |
| 7 using std::string; | 7 using std::string; |
| 8 | 8 |
| 9 namespace net { | 9 namespace net { |
| 10 | 10 |
| 11 namespace { | 11 namespace { |
| 12 | 12 |
| 13 // Inline helper function for extending a 64-bit |seed| in-place with a 64-bit | 13 // Inline helper function for extending a 64-bit |seed| in-place with a 64-bit |
| 14 // |value|. Based on Boost's hash_combine function. | 14 // |value|. Based on Boost's hash_combine function. |
| 15 inline void hash_combine(uint64_t* seed, const uint64_t& val) { | 15 inline void hash_combine(uint64_t* seed, const uint64_t& val) { |
| 16 (*seed) ^= val + 0x9e3779b9 + ((*seed) << 6) + ((*seed) >> 2); | 16 (*seed) ^= val + 0x9e3779b9 + ((*seed) << 6) + ((*seed) >> 2); |
| 17 } | 17 } |
| 18 | 18 |
| 19 } // namespace | 19 } // namespace |
| 20 | 20 |
| 21 QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts() {} | 21 QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts() |
| 22 : chain(nullptr) {} |
| 22 | 23 |
| 23 QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts( | 24 QuicCompressedCertsCache::UncompressedCerts::UncompressedCerts( |
| 24 const scoped_refptr<ProofSource::Chain>& chain, | 25 const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
| 25 const string* client_common_set_hashes, | 26 const string* client_common_set_hashes, |
| 26 const string* client_cached_cert_hashes) | 27 const string* client_cached_cert_hashes) |
| 27 : chain(chain), | 28 : chain(chain), |
| 28 client_common_set_hashes(client_common_set_hashes), | 29 client_common_set_hashes(client_common_set_hashes), |
| 29 client_cached_cert_hashes(client_cached_cert_hashes) {} | 30 client_cached_cert_hashes(client_cached_cert_hashes) {} |
| 30 | 31 |
| 31 QuicCompressedCertsCache::UncompressedCerts::~UncompressedCerts() {} | 32 QuicCompressedCertsCache::UncompressedCerts::~UncompressedCerts() {} |
| 32 | 33 |
| 33 QuicCompressedCertsCache::CachedCerts::CachedCerts() {} | 34 QuicCompressedCertsCache::CachedCerts::CachedCerts() {} |
| 34 | 35 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 60 | 61 |
| 61 QuicCompressedCertsCache::QuicCompressedCertsCache(int64_t max_num_certs) | 62 QuicCompressedCertsCache::QuicCompressedCertsCache(int64_t max_num_certs) |
| 62 : certs_cache_(max_num_certs) {} | 63 : certs_cache_(max_num_certs) {} |
| 63 | 64 |
| 64 QuicCompressedCertsCache::~QuicCompressedCertsCache() { | 65 QuicCompressedCertsCache::~QuicCompressedCertsCache() { |
| 65 // Underlying cache must be cleared before destruction. | 66 // Underlying cache must be cleared before destruction. |
| 66 certs_cache_.Clear(); | 67 certs_cache_.Clear(); |
| 67 } | 68 } |
| 68 | 69 |
| 69 const string* QuicCompressedCertsCache::GetCompressedCert( | 70 const string* QuicCompressedCertsCache::GetCompressedCert( |
| 70 const scoped_refptr<ProofSource::Chain>& chain, | 71 const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
| 71 const string& client_common_set_hashes, | 72 const string& client_common_set_hashes, |
| 72 const string& client_cached_cert_hashes) { | 73 const string& client_cached_cert_hashes) { |
| 73 UncompressedCerts uncompressed_certs(chain, &client_common_set_hashes, | 74 UncompressedCerts uncompressed_certs(chain, &client_common_set_hashes, |
| 74 &client_cached_cert_hashes); | 75 &client_cached_cert_hashes); |
| 75 | 76 |
| 76 uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs); | 77 uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs); |
| 77 | 78 |
| 78 auto cached_it = certs_cache_.Get(key); | 79 auto cached_it = certs_cache_.Get(key); |
| 79 | 80 |
| 80 if (cached_it != certs_cache_.end()) { | 81 if (cached_it != certs_cache_.end()) { |
| 81 const CachedCerts& cached_value = cached_it->second; | 82 const CachedCerts& cached_value = cached_it->second; |
| 82 if (cached_value.MatchesUncompressedCerts(uncompressed_certs)) { | 83 if (cached_value.MatchesUncompressedCerts(uncompressed_certs)) { |
| 83 return cached_value.compressed_cert(); | 84 return cached_value.compressed_cert(); |
| 84 } | 85 } |
| 85 } | 86 } |
| 86 return nullptr; | 87 return nullptr; |
| 87 } | 88 } |
| 88 | 89 |
| 89 void QuicCompressedCertsCache::Insert( | 90 void QuicCompressedCertsCache::Insert( |
| 90 const scoped_refptr<ProofSource::Chain>& chain, | 91 const QuicReferenceCountedPointer<ProofSource::Chain>& chain, |
| 91 const string& client_common_set_hashes, | 92 const string& client_common_set_hashes, |
| 92 const string& client_cached_cert_hashes, | 93 const string& client_cached_cert_hashes, |
| 93 const string& compressed_cert) { | 94 const string& compressed_cert) { |
| 94 UncompressedCerts uncompressed_certs(chain, &client_common_set_hashes, | 95 UncompressedCerts uncompressed_certs(chain, &client_common_set_hashes, |
| 95 &client_cached_cert_hashes); | 96 &client_cached_cert_hashes); |
| 96 | 97 |
| 97 uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs); | 98 uint64_t key = ComputeUncompressedCertsHash(uncompressed_certs); |
| 98 | 99 |
| 99 // Insert one unit to the cache. | 100 // Insert one unit to the cache. |
| 100 certs_cache_.Put(key, CachedCerts(uncompressed_certs, compressed_cert)); | 101 certs_cache_.Put(key, CachedCerts(uncompressed_certs, compressed_cert)); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 115 uint64_t h = | 116 uint64_t h = |
| 116 std::hash<string>()(*uncompressed_certs.client_cached_cert_hashes); | 117 std::hash<string>()(*uncompressed_certs.client_cached_cert_hashes); |
| 117 hash_combine(&hash, h); | 118 hash_combine(&hash, h); |
| 118 | 119 |
| 119 hash_combine(&hash, | 120 hash_combine(&hash, |
| 120 reinterpret_cast<uint64_t>(uncompressed_certs.chain.get())); | 121 reinterpret_cast<uint64_t>(uncompressed_certs.chain.get())); |
| 121 return hash; | 122 return hash; |
| 122 } | 123 } |
| 123 | 124 |
| 124 } // namespace net | 125 } // namespace net |
| OLD | NEW |