Fix a crash in the LegacyRenderWidgetHostHWND destruction code path which occurs...
[chromium-blink-merge.git] / base / mac / scoped_authorizationref.h
blob6413f2e41649849c6cb7153b43e29a2b34a8037e
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.
5 #ifndef BASE_MAC_SCOPED_AUTHORIZATIONREF_H_
6 #define BASE_MAC_SCOPED_AUTHORIZATIONREF_H_
8 #include <Security/Authorization.h>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
13 // ScopedAuthorizationRef maintains ownership of an AuthorizationRef. It is
14 // patterned after the scoped_ptr interface.
16 namespace base {
17 namespace mac {
19 class ScopedAuthorizationRef {
20 public:
21 explicit ScopedAuthorizationRef(AuthorizationRef authorization = NULL)
22 : authorization_(authorization) {
25 ~ScopedAuthorizationRef() {
26 if (authorization_) {
27 AuthorizationFree(authorization_, kAuthorizationFlagDestroyRights);
31 void reset(AuthorizationRef authorization = NULL) {
32 if (authorization_ != authorization) {
33 if (authorization_) {
34 AuthorizationFree(authorization_, kAuthorizationFlagDestroyRights);
36 authorization_ = authorization;
40 bool operator==(AuthorizationRef that) const {
41 return authorization_ == that;
44 bool operator!=(AuthorizationRef that) const {
45 return authorization_ != that;
48 operator AuthorizationRef() const {
49 return authorization_;
52 AuthorizationRef* operator&() {
53 return &authorization_;
56 AuthorizationRef get() const {
57 return authorization_;
60 void swap(ScopedAuthorizationRef& that) {
61 AuthorizationRef temp = that.authorization_;
62 that.authorization_ = authorization_;
63 authorization_ = temp;
66 // ScopedAuthorizationRef::release() is like scoped_ptr<>::release. It is
67 // NOT a wrapper for AuthorizationFree(). To force a
68 // ScopedAuthorizationRef object to call AuthorizationFree(), use
69 // ScopedAuthorizationRef::reset().
70 AuthorizationRef release() WARN_UNUSED_RESULT {
71 AuthorizationRef temp = authorization_;
72 authorization_ = NULL;
73 return temp;
76 private:
77 AuthorizationRef authorization_;
79 DISALLOW_COPY_AND_ASSIGN(ScopedAuthorizationRef);
82 } // namespace mac
83 } // namespace base
85 #endif // BASE_MAC_SCOPED_AUTHORIZATIONREF_H_