blob: 5d3ec8634275c5ee069193081d100065389f712b [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]7d791652010-12-01 16:34:492// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ef12d1e62012-03-21 20:55:055#ifndef BASE_MAC_SCOPED_AUTHORIZATIONREF_H_
6#define BASE_MAC_SCOPED_AUTHORIZATIONREF_H_
[email protected]7d791652010-12-01 16:34:497
8#include <Security/Authorization.h>
9
Avi Drissman70141bfa2022-12-30 20:52:5810#include <utility>
[email protected]7d791652010-12-01 16:34:4911
Avi Drissman70141bfa2022-12-30 20:52:5812#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]7d791652010-12-01 16:34:4917
Avi Drissmanefca4122022-01-05 23:59:3618namespace base::mac {
[email protected]ef12d1e62012-03-21 20:55:0519
Niels Möller031125412019-08-28 07:52:5920class BASE_EXPORT ScopedAuthorizationRef {
[email protected]7d791652010-12-01 16:34:4921 public:
Avi Drissman70141bfa2022-12-30 20:52:5822 explicit ScopedAuthorizationRef(AuthorizationRef authorization = nullptr)
23 : authorization_(authorization) {}
[email protected]7d791652010-12-01 16:34:4924
Peter Boström7319bbd2021-09-15 22:59:3825 ScopedAuthorizationRef(const ScopedAuthorizationRef&) = delete;
26 ScopedAuthorizationRef& operator=(const ScopedAuthorizationRef&) = delete;
27
Avi Drissman70141bfa2022-12-30 20:52:5828 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]a6e349a62011-06-27 16:26:2435 ~ScopedAuthorizationRef() {
[email protected]7d791652010-12-01 16:34:4936 if (authorization_) {
Niels Möller031125412019-08-28 07:52:5937 FreeInternal();
[email protected]7d791652010-12-01 16:34:4938 }
39 }
40
Avi Drissman70141bfa2022-12-30 20:52:5841 void reset(AuthorizationRef authorization = nullptr) {
[email protected]7d791652010-12-01 16:34:4942 if (authorization_ != authorization) {
43 if (authorization_) {
Niels Möller031125412019-08-28 07:52:5944 FreeInternal();
[email protected]7d791652010-12-01 16:34:4945 }
46 authorization_ = authorization;
47 }
48 }
49
50 bool operator==(AuthorizationRef that) const {
51 return authorization_ == that;
52 }
53
Peter Kasting134ef9af2024-12-28 02:30:0954 operator AuthorizationRef() const { return authorization_; }
[email protected]7d791652010-12-01 16:34:4955
Mark Rowe31692c62023-02-16 12:05:0156 explicit operator bool() const { return authorization_ != nullptr; }
57
Avi Drissman70141bfa2022-12-30 20:52:5858 // 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]7d791652010-12-01 16:34:4965
Peter Kasting134ef9af2024-12-28 02:30:0966 AuthorizationRef get() const { return authorization_; }
[email protected]7d791652010-12-01 16:34:4967
dcheng093de9b2016-04-04 21:25:5168 // 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 Cheng4455c9842022-01-13 23:26:3771 [[nodiscard]] AuthorizationRef release() {
[email protected]7d791652010-12-01 16:34:4972 AuthorizationRef temp = authorization_;
Avi Drissman70141bfa2022-12-30 20:52:5873 authorization_ = nullptr;
[email protected]7d791652010-12-01 16:34:4974 return temp;
75 }
76
77 private:
Niels Möller031125412019-08-28 07:52:5978 // 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]7d791652010-12-01 16:34:4985 AuthorizationRef authorization_;
[email protected]7d791652010-12-01 16:34:4986};
87
Avi Drissmanefca4122022-01-05 23:59:3688} // namespace base::mac
[email protected]ef12d1e62012-03-21 20:55:0589
90#endif // BASE_MAC_SCOPED_AUTHORIZATIONREF_H_