Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * @addtogroup NdkBinder |
| 19 | * @{ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file binder_auto_utils.h |
| 24 | * @brief These objects provide a more C++-like thin interface to the . |
| 25 | */ |
| 26 | |
| 27 | #pragma once |
| 28 | |
| 29 | #include <android/binder_ibinder.h> |
| 30 | #include <android/binder_parcel.h> |
| 31 | #include <android/binder_status.h> |
| 32 | |
Steven Moreland | e97106a | 2018-10-08 17:53:48 -0700 | [diff] [blame] | 33 | #include <assert.h> |
Steven Moreland | 86ae9e7 | 2018-11-05 11:12:40 -0800 | [diff] [blame] | 34 | |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 35 | #include <unistd.h> |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 36 | #include <cstddef> |
| 37 | |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 38 | namespace ndk { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Represents one strong pointer to an AIBinder object. |
| 42 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 43 | class SpAIBinder { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 44 | public: |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 45 | /** |
| 46 | * Takes ownership of one strong refcount of binder. |
| 47 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 48 | explicit SpAIBinder(AIBinder* binder = nullptr) : mBinder(binder) {} |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 49 | |
| 50 | /** |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 51 | * Convenience operator for implicitly constructing an SpAIBinder from nullptr. This is not |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 52 | * explicit because it is not taking ownership of anything. |
| 53 | */ |
Chih-Hung Hsieh | 5ca1ea4 | 2018-12-20 15:42:22 -0800 | [diff] [blame] | 54 | SpAIBinder(std::nullptr_t) : SpAIBinder() {} // NOLINT(google-explicit-constructor) |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * This will delete the underlying object if it exists. See operator=. |
| 58 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 59 | SpAIBinder(const SpAIBinder& other) { *this = other; } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
| 62 | * This deletes the underlying object if it exists. See set. |
| 63 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 64 | ~SpAIBinder() { set(nullptr); } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * This takes ownership of a binder from another AIBinder object but it does not affect the |
| 68 | * ownership of that other object. |
| 69 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 70 | SpAIBinder& operator=(const SpAIBinder& other) { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 71 | AIBinder_incStrong(other.mBinder); |
| 72 | set(other.mBinder); |
| 73 | return *this; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Takes ownership of one strong refcount of binder |
| 78 | */ |
| 79 | void set(AIBinder* binder) { |
Steven Moreland | e97106a | 2018-10-08 17:53:48 -0700 | [diff] [blame] | 80 | AIBinder* old = *const_cast<AIBinder* volatile*>(&mBinder); |
| 81 | if (old != nullptr) AIBinder_decStrong(old); |
| 82 | if (old != *const_cast<AIBinder* volatile*>(&mBinder)) { |
| 83 | __assert(__FILE__, __LINE__, "Race detected."); |
| 84 | } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 85 | mBinder = binder; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * This returns the underlying binder object for transactions. If it is used to create another |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 90 | * SpAIBinder object, it should first be incremented. |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 91 | */ |
| 92 | AIBinder* get() const { return mBinder; } |
| 93 | |
| 94 | /** |
| 95 | * This allows the value in this class to be set from beneath it. If you call this method and |
| 96 | * then change the value of T*, you must take ownership of the value you are replacing and add |
| 97 | * ownership to the object that is put in here. |
| 98 | * |
| 99 | * Recommended use is like this: |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 100 | * SpAIBinder a; // will be nullptr |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 101 | * SomeInitFunction(a.getR()); // value is initialized with refcount |
| 102 | * |
| 103 | * Other usecases are discouraged. |
| 104 | * |
| 105 | */ |
| 106 | AIBinder** getR() { return &mBinder; } |
| 107 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 108 | private: |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 109 | AIBinder* mBinder = nullptr; |
| 110 | }; |
| 111 | |
Steven Moreland | 26c4df1 | 2019-01-30 12:29:53 -0800 | [diff] [blame^] | 112 | namespace impl { |
| 113 | |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 114 | /** |
| 115 | * This baseclass owns a single object, used to make various classes RAII. |
| 116 | */ |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 117 | template <typename T, typename R, R (*Destroy)(T), T DEFAULT> |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame] | 118 | class ScopedAResource { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 119 | public: |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 120 | /** |
| 121 | * Takes ownership of t. |
| 122 | */ |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 123 | explicit ScopedAResource(T t = DEFAULT) : mT(t) {} |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * This deletes the underlying object if it exists. See set. |
| 127 | */ |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 128 | ~ScopedAResource() { set(DEFAULT); } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 129 | |
| 130 | /** |
| 131 | * Takes ownership of t. |
| 132 | */ |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 133 | void set(T t) { |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 134 | Destroy(mT); |
| 135 | mT = t; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * This returns the underlying object to be modified but does not affect ownership. |
| 140 | */ |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 141 | T get() { return mT; } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 142 | |
| 143 | /** |
| 144 | * This returns the const underlying object but does not affect ownership. |
| 145 | */ |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 146 | const T get() const { return mT; } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 147 | |
| 148 | /** |
| 149 | * This allows the value in this class to be set from beneath it. If you call this method and |
| 150 | * then change the value of T*, you must take ownership of the value you are replacing and add |
| 151 | * ownership to the object that is put in here. |
| 152 | * |
| 153 | * Recommended use is like this: |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame] | 154 | * ScopedAResource<T> a; // will be nullptr |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 155 | * SomeInitFunction(a.getR()); // value is initialized with refcount |
| 156 | * |
| 157 | * Other usecases are discouraged. |
| 158 | * |
| 159 | */ |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 160 | T* getR() { return &mT; } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 161 | |
| 162 | // copy-constructing, or move/copy assignment is disallowed |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame] | 163 | ScopedAResource(const ScopedAResource&) = delete; |
| 164 | ScopedAResource& operator=(const ScopedAResource&) = delete; |
| 165 | ScopedAResource& operator=(ScopedAResource&&) = delete; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 166 | |
| 167 | // move-constructing is okay |
Steven Moreland | cd056e4 | 2018-12-11 11:31:12 -0800 | [diff] [blame] | 168 | ScopedAResource(ScopedAResource&& other) : mT(std::move(other.mT)) { other.mT = DEFAULT; } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 169 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 170 | private: |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 171 | T mT; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 172 | }; |
| 173 | |
Steven Moreland | 26c4df1 | 2019-01-30 12:29:53 -0800 | [diff] [blame^] | 174 | } // namespace impl |
| 175 | |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 176 | /** |
| 177 | * Convenience wrapper. See AParcel. |
| 178 | */ |
Steven Moreland | 26c4df1 | 2019-01-30 12:29:53 -0800 | [diff] [blame^] | 179 | class ScopedAParcel : public impl::ScopedAResource<AParcel*, void, AParcel_delete, nullptr> { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 180 | public: |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 181 | /** |
| 182 | * Takes ownership of a. |
| 183 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame] | 184 | explicit ScopedAParcel(AParcel* a = nullptr) : ScopedAResource(a) {} |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 185 | ~ScopedAParcel() {} |
| 186 | ScopedAParcel(ScopedAParcel&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
| 189 | /** |
| 190 | * Convenience wrapper. See AStatus. |
| 191 | */ |
Steven Moreland | 26c4df1 | 2019-01-30 12:29:53 -0800 | [diff] [blame^] | 192 | class ScopedAStatus : public impl::ScopedAResource<AStatus*, void, AStatus_delete, nullptr> { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 193 | public: |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Takes ownership of a. |
| 196 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame] | 197 | explicit ScopedAStatus(AStatus* a = nullptr) : ScopedAResource(a) {} |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 198 | ~ScopedAStatus() {} |
| 199 | ScopedAStatus(ScopedAStatus&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 200 | |
| 201 | /** |
| 202 | * See AStatus_isOk. |
| 203 | */ |
| 204 | bool isOk() { return get() != nullptr && AStatus_isOk(get()); } |
Steven Moreland | 9450a48 | 2018-11-15 15:17:53 -0800 | [diff] [blame] | 205 | |
| 206 | /** |
| 207 | * Convenience method for okay status. |
| 208 | */ |
| 209 | static ScopedAStatus ok() { return ScopedAStatus(AStatus_newOk()); } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | /** |
| 213 | * Convenience wrapper. See AIBinder_DeathRecipient. |
| 214 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 215 | class ScopedAIBinder_DeathRecipient |
Steven Moreland | 26c4df1 | 2019-01-30 12:29:53 -0800 | [diff] [blame^] | 216 | : public impl::ScopedAResource<AIBinder_DeathRecipient*, void, AIBinder_DeathRecipient_delete, |
| 217 | nullptr> { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 218 | public: |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Takes ownership of a. |
| 221 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame] | 222 | explicit ScopedAIBinder_DeathRecipient(AIBinder_DeathRecipient* a = nullptr) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 223 | : ScopedAResource(a) {} |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 224 | ~ScopedAIBinder_DeathRecipient() {} |
| 225 | ScopedAIBinder_DeathRecipient(ScopedAIBinder_DeathRecipient&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | /** |
| 229 | * Convenience wrapper. See AIBinder_Weak. |
| 230 | */ |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 231 | class ScopedAIBinder_Weak |
Steven Moreland | 26c4df1 | 2019-01-30 12:29:53 -0800 | [diff] [blame^] | 232 | : public impl::ScopedAResource<AIBinder_Weak*, void, AIBinder_Weak_delete, nullptr> { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 233 | public: |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 234 | /** |
| 235 | * Takes ownership of a. |
| 236 | */ |
Steven Moreland | 1630c19 | 2018-10-09 11:54:52 -0700 | [diff] [blame] | 237 | explicit ScopedAIBinder_Weak(AIBinder_Weak* a = nullptr) : ScopedAResource(a) {} |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 238 | ~ScopedAIBinder_Weak() {} |
| 239 | ScopedAIBinder_Weak(ScopedAIBinder_Weak&&) = default; |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 240 | |
| 241 | /** |
| 242 | * See AIBinder_Weak_promote. |
| 243 | */ |
Steven Moreland | 8b4f48a | 2018-10-08 14:30:42 -0700 | [diff] [blame] | 244 | SpAIBinder promote() { return SpAIBinder(AIBinder_Weak_promote(get())); } |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 245 | }; |
| 246 | |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 247 | /** |
| 248 | * Convenience wrapper for a file descriptor. |
| 249 | */ |
Steven Moreland | 26c4df1 | 2019-01-30 12:29:53 -0800 | [diff] [blame^] | 250 | class ScopedFileDescriptor : public impl::ScopedAResource<int, int, close, -1> { |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 251 | public: |
Steven Moreland | 063f236 | 2018-10-18 12:49:11 -0700 | [diff] [blame] | 252 | /** |
| 253 | * Takes ownership of a. |
| 254 | */ |
| 255 | explicit ScopedFileDescriptor(int a = -1) : ScopedAResource(a) {} |
| 256 | ~ScopedFileDescriptor() {} |
| 257 | ScopedFileDescriptor(ScopedFileDescriptor&&) = default; |
| 258 | }; |
| 259 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame] | 260 | } // namespace ndk |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 261 | |
Steven Moreland | 2648d20 | 2018-08-28 01:23:02 -0700 | [diff] [blame] | 262 | /** @} */ |