1 // Copyright (c) 2012 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 BASE_MAC_SCOPED_AUTHORIZATIONREF_H_ 6 #define BASE_MAC_SCOPED_AUTHORIZATIONREF_H_ 7 8 #include <Security/Authorization.h> 9 10 #include "base/compiler_specific.h" 11 #include "base/macros.h" 12 13 // ScopedAuthorizationRef maintains ownership of an AuthorizationRef. It is 14 // patterned after the scoped_ptr interface. 15 16 namespace base { 17 namespace mac { 18 19 class ScopedAuthorizationRef { 20 public: 21 explicit ScopedAuthorizationRef(AuthorizationRef authorization = NULL) authorization_(authorization)22 : authorization_(authorization) { 23 } 24 ~ScopedAuthorizationRef()25 ~ScopedAuthorizationRef() { 26 if (authorization_) { 27 AuthorizationFree(authorization_, kAuthorizationFlagDestroyRights); 28 } 29 } 30 31 void reset(AuthorizationRef authorization = NULL) { 32 if (authorization_ != authorization) { 33 if (authorization_) { 34 AuthorizationFree(authorization_, kAuthorizationFlagDestroyRights); 35 } 36 authorization_ = authorization; 37 } 38 } 39 40 bool operator==(AuthorizationRef that) const { 41 return authorization_ == that; 42 } 43 44 bool operator!=(AuthorizationRef that) const { 45 return authorization_ != that; 46 } 47 AuthorizationRef()48 operator AuthorizationRef() const { 49 return authorization_; 50 } 51 get_pointer()52 AuthorizationRef* get_pointer() { return &authorization_; } 53 get()54 AuthorizationRef get() const { 55 return authorization_; 56 } 57 swap(ScopedAuthorizationRef & that)58 void swap(ScopedAuthorizationRef& that) { 59 AuthorizationRef temp = that.authorization_; 60 that.authorization_ = authorization_; 61 authorization_ = temp; 62 } 63 64 // ScopedAuthorizationRef::release() is like std::unique_ptr<>::release. It is 65 // NOT a wrapper for AuthorizationFree(). To force a ScopedAuthorizationRef 66 // object to call AuthorizationFree(), use ScopedAuthorizationRef::reset(). release()67 AuthorizationRef release() WARN_UNUSED_RESULT { 68 AuthorizationRef temp = authorization_; 69 authorization_ = NULL; 70 return temp; 71 } 72 73 private: 74 AuthorizationRef authorization_; 75 76 DISALLOW_COPY_AND_ASSIGN(ScopedAuthorizationRef); 77 }; 78 79 } // namespace mac 80 } // namespace base 81 82 #endif // BASE_MAC_SCOPED_AUTHORIZATIONREF_H_ 83