Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CRYPTO_SCOPED_CAPI_TYPES_H_ | 5 #ifndef CRYPTO_SCOPED_CAPI_TYPES_H_ |
| 6 #define CRYPTO_SCOPED_CAPI_TYPES_H_ | 6 #define CRYPTO_SCOPED_CAPI_TYPES_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <windows.h> | 9 #include <windows.h> |
| 10 #include <wincrypt.h> | 10 #include <wincrypt.h> |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 // scoped_ptr-like class for the CryptoAPI cryptography and certificate | 44 // scoped_ptr-like class for the CryptoAPI cryptography and certificate |
| 45 // handles. Because these handles are defined as integer types, and not | 45 // handles. Because these handles are defined as integer types, and not |
| 46 // pointers, the existing scoped classes, such as scoped_ptr_malloc, are | 46 // pointers, the existing scoped classes, such as scoped_ptr_malloc, are |
| 47 // insufficient. The semantics are the same as scoped_ptr. | 47 // insufficient. The semantics are the same as scoped_ptr. |
| 48 template <class CAPIHandle, typename FreeProc> | 48 template <class CAPIHandle, typename FreeProc> |
| 49 class ScopedCAPIHandle { | 49 class ScopedCAPIHandle { |
| 50 public: | 50 public: |
| 51 explicit ScopedCAPIHandle(CAPIHandle handle = NULL) : handle_(handle) {} | 51 explicit ScopedCAPIHandle(CAPIHandle handle = NULL) : handle_(handle) {} |
| 52 | 52 |
| 53 ~ScopedCAPIHandle() { | 53 ~ScopedCAPIHandle() { |
| 54 free_(handle_); | 54 reset(); |
|
wtc
2011/08/31 21:15:28
Just to make sure I understand this correctly: thi
| |
| 55 } | 55 } |
| 56 | 56 |
| 57 void reset(CAPIHandle handle = NULL) { | 57 void reset(CAPIHandle handle = NULL) { |
| 58 if (handle_ != handle) { | 58 if (handle_ != handle) { |
| 59 free_(handle_); | 59 FreeProc free_proc; |
| 60 free_proc(handle_); | |
| 60 handle_ = handle; | 61 handle_ = handle; |
| 61 } | 62 } |
| 62 } | 63 } |
| 63 | 64 |
| 64 operator CAPIHandle() const { return handle_; } | 65 operator CAPIHandle() const { return handle_; } |
| 65 CAPIHandle get() const { return handle_; } | 66 CAPIHandle get() const { return handle_; } |
| 66 | 67 |
| 67 CAPIHandle* receive() { | 68 CAPIHandle* receive() { |
| 68 CHECK(handle_ == NULL); | 69 CHECK(handle_ == NULL); |
| 69 return &handle_; | 70 return &handle_; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 84 } | 85 } |
| 85 | 86 |
| 86 CAPIHandle release() { | 87 CAPIHandle release() { |
| 87 CAPIHandle tmp = handle_; | 88 CAPIHandle tmp = handle_; |
| 88 handle_ = NULL; | 89 handle_ = NULL; |
| 89 return tmp; | 90 return tmp; |
| 90 } | 91 } |
| 91 | 92 |
| 92 private: | 93 private: |
| 93 CAPIHandle handle_; | 94 CAPIHandle handle_; |
| 94 static const FreeProc free_; | |
| 95 | 95 |
| 96 DISALLOW_COPY_AND_ASSIGN(ScopedCAPIHandle); | 96 DISALLOW_COPY_AND_ASSIGN(ScopedCAPIHandle); |
| 97 }; | 97 }; |
| 98 | 98 |
| 99 template<class CH, typename FP> | |
| 100 const FP ScopedCAPIHandle<CH, FP>::free_ = FP(); | |
| 101 | |
| 102 template<class CH, typename FP> inline | 99 template<class CH, typename FP> inline |
| 103 bool operator==(CH h, const ScopedCAPIHandle<CH, FP>& b) { | 100 bool operator==(CH h, const ScopedCAPIHandle<CH, FP>& b) { |
| 104 return h == b.get(); | 101 return h == b.get(); |
| 105 } | 102 } |
| 106 | 103 |
| 107 template<class CH, typename FP> inline | 104 template<class CH, typename FP> inline |
| 108 bool operator!=(CH h, const ScopedCAPIHandle<CH, FP>& b) { | 105 bool operator!=(CH h, const ScopedCAPIHandle<CH, FP>& b) { |
| 109 return h != b.get(); | 106 return h != b.get(); |
| 110 } | 107 } |
| 111 | 108 |
| 112 typedef ScopedCAPIHandle< | 109 typedef ScopedCAPIHandle< |
| 113 HCRYPTPROV, | 110 HCRYPTPROV, |
| 114 CAPIDestroyerWithFlags<HCRYPTPROV, | 111 CAPIDestroyerWithFlags<HCRYPTPROV, |
| 115 CryptReleaseContext, 0> > ScopedHCRYPTPROV; | 112 CryptReleaseContext, 0> > ScopedHCRYPTPROV; |
| 116 | 113 |
| 117 typedef ScopedCAPIHandle< | 114 typedef ScopedCAPIHandle< |
| 118 HCRYPTKEY, CAPIDestroyer<HCRYPTKEY, CryptDestroyKey> > ScopedHCRYPTKEY; | 115 HCRYPTKEY, CAPIDestroyer<HCRYPTKEY, CryptDestroyKey> > ScopedHCRYPTKEY; |
| 119 | 116 |
| 120 typedef ScopedCAPIHandle< | 117 typedef ScopedCAPIHandle< |
| 121 HCRYPTHASH, CAPIDestroyer<HCRYPTHASH, CryptDestroyHash> > ScopedHCRYPTHASH; | 118 HCRYPTHASH, CAPIDestroyer<HCRYPTHASH, CryptDestroyHash> > ScopedHCRYPTHASH; |
| 122 | 119 |
| 123 } // namespace crypto | 120 } // namespace crypto |
| 124 | 121 |
| 125 #endif // CRYPTO_SCOPED_CAPI_TYPES_H_ | 122 #endif // CRYPTO_SCOPED_CAPI_TYPES_H_ |
| OLD | NEW |