Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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. | |
| 4 | |
| 5 #ifndef IOS_WEB_NET_CERT_VERIFICATION_CACHE_H_ | |
| 6 #define IOS_WEB_NET_CERT_VERIFICATION_CACHE_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "net/cert/x509_certificate.h" | |
| 12 | |
| 13 namespace web { | |
| 14 | |
| 15 // Allows caching cert verification data by providing |Get|, |Set| and |reset| | |
| 16 // API. Cache key used by |Get| and |Set| is cert-host pair. Cached value is a | |
| 17 // template param which can be any object which holds cert verification data. | |
| 18 // This class is not threadsafe and provides infinite cache size. | |
| 19 // Usage example: | |
| 20 // | |
| 21 // scoped_refptr<net::X509Certificate> valid_cert = ... | |
| 22 // scoped_refptr<net::X509Certificate> invalid_cert = ... | |
| 23 // | |
| 24 // // Store a bool which indicates whether cert is valid or not. | |
| 25 // CertVerificationCache<bool> cert_validness_state; | |
| 26 // cert_validness_state.Set(valid_cert, "www.example.com", true); | |
| 27 // cert_validness_state.Set(invalid_cert, "www.example.com", false); | |
| 28 // | |
| 29 // // Check cert's validness state. | |
| 30 // bool is_valid = cert_validness_state.Get(valid_cert, "www.example.com"); | |
| 31 // | |
| 32 // // Clear the cache. | |
| 33 // cert_validness_state.reset(); | |
| 34 // | |
| 35 template <typename ValueType> | |
| 36 class CertVerificationCache { | |
|
stuartmorgan
2015/10/08 16:52:39
Per Ryan's earlier comments, can this be replaced
Eugene But (OOO till 7-30)
2015/10/09 16:32:36
Done.
| |
| 37 public: | |
| 38 CertVerificationCache() {} | |
| 39 ~CertVerificationCache() {} | |
| 40 | |
| 41 // Retrieves |value| for the given cert-host pair. |cert| cannot be null. | |
| 42 // Returns true on success. | |
| 43 bool Get(const scoped_refptr<net::X509Certificate>& cert, | |
| 44 const std::string& host, | |
| 45 ValueType* value) const; | |
| 46 | |
| 47 // Adds |value| for the given cert-host pair. |cert| cannot be null. | |
| 48 void Set(const scoped_refptr<net::X509Certificate>& cert, | |
| 49 const std::string& host, | |
| 50 const ValueType& value); | |
| 51 | |
| 52 // Clears the cache. | |
| 53 void reset() { map_.clear(); } | |
| 54 | |
| 55 private: | |
| 56 struct KeyType { | |
| 57 KeyType(const scoped_refptr<net::X509Certificate>& cert, | |
| 58 const std::string& host) | |
| 59 : cert(cert), host(host) {} | |
| 60 | |
| 61 bool operator<(const KeyType& other) const { | |
| 62 if (host != other.host) | |
| 63 return host < other.host; | |
| 64 return cert_comparator(cert, other.cert); | |
| 65 } | |
| 66 | |
| 67 scoped_refptr<net::X509Certificate> cert; | |
| 68 std::string host; | |
| 69 | |
| 70 private: | |
| 71 net::X509Certificate::LessThan cert_comparator; | |
| 72 }; | |
| 73 | |
| 74 std::map<KeyType, ValueType> map_; | |
| 75 }; | |
| 76 | |
| 77 template <typename ValueType> | |
| 78 bool CertVerificationCache<ValueType>::Get( | |
| 79 const scoped_refptr<net::X509Certificate>& cert, | |
| 80 const std::string& host, | |
| 81 ValueType* value) const { | |
| 82 auto it = map_.find(KeyType(cert, host)); | |
| 83 if (it == map_.end()) | |
| 84 return false; | |
| 85 | |
| 86 *value = it->second; | |
| 87 return true; | |
| 88 } | |
| 89 | |
| 90 template <typename ValueType> | |
| 91 void CertVerificationCache<ValueType>::Set( | |
| 92 const scoped_refptr<net::X509Certificate>& cert, | |
| 93 const std::string& host, | |
| 94 const ValueType& value) { | |
| 95 DCHECK(cert); | |
| 96 map_[KeyType(cert, host)] = value; | |
| 97 } | |
| 98 | |
| 99 } // namespace web | |
| 100 | |
| 101 #endif // IOS_WEB_NET_CERT_VERIFICATION_CACHE_H_ | |
| OLD | NEW |