Home | History | Annotate | Download | only in crypto
      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
      3 // found in the LICENSE file.
      4 
      5 #ifndef CRYPTO_SCOPED_NSS_TYPES_H_
      6 #define CRYPTO_SCOPED_NSS_TYPES_H_
      7 #pragma once
      8 
      9 #include <nss.h>
     10 #include <pk11pub.h>
     11 
     12 #include "base/memory/scoped_ptr.h"
     13 
     14 namespace crypto {
     15 
     16 template <typename Type, void (*Destroyer)(Type*)>
     17 struct NSSDestroyer {
     18   void operator()(Type* ptr) const {
     19     if (ptr)
     20       Destroyer(ptr);
     21   }
     22 };
     23 
     24 template <typename Type, void (*Destroyer)(Type*, PRBool), PRBool freeit>
     25 struct NSSDestroyer1 {
     26   void operator()(Type* ptr) const {
     27     if (ptr)
     28       Destroyer(ptr, freeit);
     29   }
     30 };
     31 
     32 // Define some convenient scopers around NSS pointers.
     33 typedef scoped_ptr_malloc<
     34     PK11Context, NSSDestroyer1<PK11Context,
     35                                PK11_DestroyContext,
     36                                PR_TRUE> > ScopedPK11Context;
     37 typedef scoped_ptr_malloc<
     38     PK11SlotInfo, NSSDestroyer<PK11SlotInfo, PK11_FreeSlot> > ScopedPK11Slot;
     39 typedef scoped_ptr_malloc<
     40     PK11SymKey, NSSDestroyer<PK11SymKey, PK11_FreeSymKey> > ScopedPK11SymKey;
     41 typedef scoped_ptr_malloc<
     42     SECAlgorithmID, NSSDestroyer1<SECAlgorithmID,
     43                                   SECOID_DestroyAlgorithmID,
     44                                   PR_TRUE> > ScopedSECAlgorithmID;
     45 typedef scoped_ptr_malloc<
     46     SECItem, NSSDestroyer1<SECItem,
     47                            SECITEM_FreeItem,
     48                            PR_TRUE> > ScopedSECItem;
     49 
     50 }  // namespace crypto
     51 
     52 #endif  // CRYPTO_SCOPED_NSS_TYPES_H_
     53