Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | ef12d1e6 | 2012-03-21 20:55:05 | [diff] [blame] | 5 | #ifndef BASE_MAC_SCOPED_AUTHORIZATIONREF_H_ |
6 | #define BASE_MAC_SCOPED_AUTHORIZATIONREF_H_ | ||||
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 7 | |
8 | #include <Security/Authorization.h> | ||||
9 | |||||
Avi Drissman | 70141bfa | 2022-12-30 20:52:58 | [diff] [blame] | 10 | #include <utility> |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 11 | |
Avi Drissman | 70141bfa | 2022-12-30 20:52:58 | [diff] [blame] | 12 | #include "base/base_export.h" |
13 | #include "base/check.h" | ||||
14 | |||||
15 | // `ScopedAuthorizationRef` maintains ownership of an `AuthorizationRef`. It is | ||||
16 | // patterned after the `unique_ptr` interface. | ||||
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 17 | |
Avi Drissman | efca412 | 2022-01-05 23:59:36 | [diff] [blame] | 18 | namespace base::mac { |
[email protected] | ef12d1e6 | 2012-03-21 20:55:05 | [diff] [blame] | 19 | |
Niels Möller | 03112541 | 2019-08-28 07:52:59 | [diff] [blame] | 20 | class BASE_EXPORT ScopedAuthorizationRef { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 21 | public: |
Avi Drissman | 70141bfa | 2022-12-30 20:52:58 | [diff] [blame] | 22 | explicit ScopedAuthorizationRef(AuthorizationRef authorization = nullptr) |
23 | : authorization_(authorization) {} | ||||
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 24 | |
Peter Boström | 7319bbd | 2021-09-15 22:59:38 | [diff] [blame] | 25 | ScopedAuthorizationRef(const ScopedAuthorizationRef&) = delete; |
26 | ScopedAuthorizationRef& operator=(const ScopedAuthorizationRef&) = delete; | ||||
27 | |||||
Avi Drissman | 70141bfa | 2022-12-30 20:52:58 | [diff] [blame] | 28 | ScopedAuthorizationRef(ScopedAuthorizationRef&& that) |
29 | : authorization_(std::exchange(that.authorization_, nullptr)) {} | ||||
30 | ScopedAuthorizationRef& operator=(ScopedAuthorizationRef&& that) { | ||||
31 | authorization_ = std::exchange(that.authorization_, nullptr); | ||||
32 | return *this; | ||||
33 | } | ||||
34 | |||||
[email protected] | a6e349a6 | 2011-06-27 16:26:24 | [diff] [blame] | 35 | ~ScopedAuthorizationRef() { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 36 | if (authorization_) { |
Niels Möller | 03112541 | 2019-08-28 07:52:59 | [diff] [blame] | 37 | FreeInternal(); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 38 | } |
39 | } | ||||
40 | |||||
Avi Drissman | 70141bfa | 2022-12-30 20:52:58 | [diff] [blame] | 41 | void reset(AuthorizationRef authorization = nullptr) { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 42 | if (authorization_ != authorization) { |
43 | if (authorization_) { | ||||
Niels Möller | 03112541 | 2019-08-28 07:52:59 | [diff] [blame] | 44 | FreeInternal(); |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 45 | } |
46 | authorization_ = authorization; | ||||
47 | } | ||||
48 | } | ||||
49 | |||||
50 | bool operator==(AuthorizationRef that) const { | ||||
51 | return authorization_ == that; | ||||
52 | } | ||||
53 | |||||
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 54 | operator AuthorizationRef() const { return authorization_; } |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 55 | |
Mark Rowe | 31692c6 | 2023-02-16 12:05:01 | [diff] [blame] | 56 | explicit operator bool() const { return authorization_ != nullptr; } |
57 | |||||
Avi Drissman | 70141bfa | 2022-12-30 20:52:58 | [diff] [blame] | 58 | // This is to be used only to take ownership of objects that are created |
59 | // by pass-by-pointer create functions. To enforce this, require that the | ||||
60 | // object be reset to NULL before this may be used. | ||||
61 | [[nodiscard]] AuthorizationRef* InitializeInto() { | ||||
62 | DCHECK(!authorization_); | ||||
63 | return &authorization_; | ||||
64 | } | ||||
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 65 | |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 66 | AuthorizationRef get() const { return authorization_; } |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 67 | |
dcheng | 093de9b | 2016-04-04 21:25:51 | [diff] [blame] | 68 | // ScopedAuthorizationRef::release() is like std::unique_ptr<>::release. It is |
69 | // NOT a wrapper for AuthorizationFree(). To force a ScopedAuthorizationRef | ||||
70 | // object to call AuthorizationFree(), use ScopedAuthorizationRef::reset(). | ||||
Daniel Cheng | 4455c984 | 2022-01-13 23:26:37 | [diff] [blame] | 71 | [[nodiscard]] AuthorizationRef release() { |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 72 | AuthorizationRef temp = authorization_; |
Avi Drissman | 70141bfa | 2022-12-30 20:52:58 | [diff] [blame] | 73 | authorization_ = nullptr; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 74 | return temp; |
75 | } | ||||
76 | |||||
77 | private: | ||||
Niels Möller | 03112541 | 2019-08-28 07:52:59 | [diff] [blame] | 78 | // Calling AuthorizationFree, defined in Security.framework, from an inline |
79 | // function, results in link errors when linking dynamically with | ||||
80 | // libbase.dylib. So wrap the call in an un-inlined method. This method | ||||
81 | // doesn't check if |authorization_| is null; that check should be in the | ||||
82 | // inlined callers. | ||||
83 | void FreeInternal(); | ||||
84 | |||||
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 85 | AuthorizationRef authorization_; |
[email protected] | 7d79165 | 2010-12-01 16:34:49 | [diff] [blame] | 86 | }; |
87 | |||||
Avi Drissman | efca412 | 2022-01-05 23:59:36 | [diff] [blame] | 88 | } // namespace base::mac |
[email protected] | ef12d1e6 | 2012-03-21 20:55:05 | [diff] [blame] | 89 | |
90 | #endif // BASE_MAC_SCOPED_AUTHORIZATIONREF_H_ |