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