blob: f16cde61409bc83ed5d8bc679cd932bfb799633a [file] [log] [blame]
Steven Moreland2e87adc2018-08-20 19:47:00 -07001/*
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#include <android/binder_ibinder.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070018#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070019
20#include <android/binder_status.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070021#include "parcel_internal.h"
Steven Moreland5d62e442018-09-13 15:01:02 -070022#include "status_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070023
24#include <android-base/logging.h>
25
Steven Moreland901c7452018-09-04 16:17:28 -070026using DeathRecipient = ::android::IBinder::DeathRecipient;
27
Steven Moreland2e87adc2018-08-20 19:47:00 -070028using ::android::IBinder;
29using ::android::Parcel;
30using ::android::sp;
Steven Moreland5d62e442018-09-13 15:01:02 -070031using ::android::status_t;
Steven Moreland2e87adc2018-08-20 19:47:00 -070032using ::android::String16;
33using ::android::wp;
34
Steven Moreland71cddc32018-08-30 23:39:22 -070035namespace ABBinderTag {
36
37static const void* kId = "ABBinder";
38static void* kValue = static_cast<void*>(new bool{true});
Steven Moreland94968952018-09-05 14:42:59 -070039void clean(const void* /*id*/, void* /*obj*/, void* /*cookie*/){/* do nothing */};
Steven Moreland71cddc32018-08-30 23:39:22 -070040
41static void attach(const sp<IBinder>& binder) {
Steven Moreland94968952018-09-05 14:42:59 -070042 binder->attachObject(kId, kValue, nullptr /*cookie*/, clean);
Steven Moreland71cddc32018-08-30 23:39:22 -070043}
44static bool has(const sp<IBinder>& binder) {
45 return binder != nullptr && binder->findObject(kId) == kValue;
46}
47
48} // namespace ABBinderTag
49
Steven Moreland94968952018-09-05 14:42:59 -070050namespace ABpBinderTag {
51
52static std::mutex gLock;
53static const void* kId = "ABpBinder";
54struct Value {
55 wp<ABpBinder> binder;
56};
57void clean(const void* id, void* obj, void* cookie) {
58 CHECK(id == kId) << id << " " << obj << " " << cookie;
59
60 delete static_cast<Value*>(obj);
61};
62
63} // namespace ABpBinderTag
64
Steven Moreland2e87adc2018-08-20 19:47:00 -070065AIBinder::AIBinder(const AIBinder_Class* clazz) : mClazz(clazz) {}
66AIBinder::~AIBinder() {}
67
Steven Moreland71cddc32018-08-30 23:39:22 -070068bool AIBinder::associateClass(const AIBinder_Class* clazz) {
Steven Moreland2e87adc2018-08-20 19:47:00 -070069 using ::android::String8;
70
Steven Moreland71cddc32018-08-30 23:39:22 -070071 if (clazz == nullptr) return false;
72 if (mClazz == clazz) return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -070073
74 String8 newDescriptor(clazz->getInterfaceDescriptor());
75
76 if (mClazz != nullptr) {
77 String8 currentDescriptor(mClazz->getInterfaceDescriptor());
78 if (newDescriptor == currentDescriptor) {
79 LOG(ERROR) << __func__ << ": Class descriptors '" << currentDescriptor
80 << "' match during associateClass, but they are different class objects. "
81 "Class descriptor collision?";
Steven Moreland71cddc32018-08-30 23:39:22 -070082 } else {
83 LOG(ERROR) << __func__
84 << ": Class cannot be associated on object which already has a class. "
85 "Trying to associate to '"
86 << newDescriptor.c_str() << "' but already set to '"
87 << currentDescriptor.c_str() << "'.";
Steven Moreland2e87adc2018-08-20 19:47:00 -070088 }
89
Steven Moreland71cddc32018-08-30 23:39:22 -070090 // always a failure because we know mClazz != clazz
91 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 }
93
Steven Moreland71cddc32018-08-30 23:39:22 -070094 CHECK(asABpBinder() != nullptr); // ABBinder always has a descriptor
95
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 String8 descriptor(getBinder()->getInterfaceDescriptor());
97 if (descriptor != newDescriptor) {
98 LOG(ERROR) << __func__ << ": Expecting binder to have class '" << newDescriptor.c_str()
99 << "' but descriptor is actually '" << descriptor.c_str() << "'.";
Steven Moreland71cddc32018-08-30 23:39:22 -0700100 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700101 }
102
Steven Moreland71cddc32018-08-30 23:39:22 -0700103 // if this is a local object, it's not one known to libbinder_ndk
Steven Moreland2e87adc2018-08-20 19:47:00 -0700104 mClazz = clazz;
105
Steven Moreland71cddc32018-08-30 23:39:22 -0700106 return true;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700107}
108
109ABBinder::ABBinder(const AIBinder_Class* clazz, void* userData)
110 : AIBinder(clazz), BBinder(), mUserData(userData) {
111 CHECK(clazz != nullptr);
112}
113ABBinder::~ABBinder() {
114 getClass()->onDestroy(mUserData);
115}
116
117const String16& ABBinder::getInterfaceDescriptor() const {
118 return getClass()->getInterfaceDescriptor();
119}
120
Steven Moreland5d62e442018-09-13 15:01:02 -0700121status_t ABBinder::onTransact(transaction_code_t code, const Parcel& data, Parcel* reply,
122 binder_flags_t flags) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700123 if (isUserCommand(code)) {
124 if (!data.checkInterface(this)) {
Steven Moreland9d089af2018-09-18 08:58:09 -0700125 return STATUS_BAD_TYPE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700126 }
127
128 const AParcel in = AParcel::readOnly(this, &data);
129 AParcel out = AParcel(this, reply, false /*owns*/);
130
Steven Moreland5d62e442018-09-13 15:01:02 -0700131 binder_status_t status = getClass()->onTransact(this, code, &in, &out);
132 return PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700133 } else {
134 return BBinder::onTransact(code, data, reply, flags);
135 }
136}
137
Steven Moreland71cddc32018-08-30 23:39:22 -0700138ABpBinder::ABpBinder(const ::android::sp<::android::IBinder>& binder)
Steven Moreland2e87adc2018-08-20 19:47:00 -0700139 : AIBinder(nullptr /*clazz*/), BpRefBase(binder) {
140 CHECK(binder != nullptr);
141}
142ABpBinder::~ABpBinder() {}
143
Steven Moreland94968952018-09-05 14:42:59 -0700144void ABpBinder::onLastStrongRef(const void* id) {
145 {
146 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
147 // Since ABpBinder is OBJECT_LIFETIME_WEAK, we must remove this weak reference in order for
148 // the ABpBinder to be deleted. Since a strong reference to this ABpBinder object should no
149 // longer be able to exist at the time of this method call, there is no longer a need to
150 // recover it.
151
152 ABpBinderTag::Value* value =
153 static_cast<ABpBinderTag::Value*>(remote()->findObject(ABpBinderTag::kId));
154 if (value != nullptr) {
155 value->binder = nullptr;
156 }
157 }
158
159 BpRefBase::onLastStrongRef(id);
160}
161
162sp<AIBinder> ABpBinder::lookupOrCreateFromBinder(const ::android::sp<::android::IBinder>& binder) {
Steven Moreland71cddc32018-08-30 23:39:22 -0700163 if (binder == nullptr) {
164 return nullptr;
165 }
166 if (ABBinderTag::has(binder)) {
167 return static_cast<ABBinder*>(binder.get());
168 }
Steven Moreland94968952018-09-05 14:42:59 -0700169
170 // The following code ensures that for a given binder object (remote or local), if it is not an
171 // ABBinder then at most one ABpBinder object exists in a given process representing it.
172 std::lock_guard<std::mutex> lock(ABpBinderTag::gLock);
173
174 ABpBinderTag::Value* value =
175 static_cast<ABpBinderTag::Value*>(binder->findObject(ABpBinderTag::kId));
176 if (value == nullptr) {
177 value = new ABpBinderTag::Value;
178 binder->attachObject(ABpBinderTag::kId, static_cast<void*>(value), nullptr /*cookie*/,
179 ABpBinderTag::clean);
180 }
181
182 sp<ABpBinder> ret = value->binder.promote();
183 if (ret == nullptr) {
184 ret = new ABpBinder(binder);
185 value->binder = ret;
186 }
187
188 return ret;
Steven Moreland71cddc32018-08-30 23:39:22 -0700189}
190
Steven Moreland2e87adc2018-08-20 19:47:00 -0700191struct AIBinder_Weak {
192 wp<AIBinder> binder;
193};
194AIBinder_Weak* AIBinder_Weak_new(AIBinder* binder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700195 if (binder == nullptr) {
196 return nullptr;
197 }
198
Steven Moreland2e87adc2018-08-20 19:47:00 -0700199 return new AIBinder_Weak{wp<AIBinder>(binder)};
200}
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700201void AIBinder_Weak_delete(AIBinder_Weak** weakBinder) {
202 if (weakBinder == nullptr) {
203 return;
204 }
205
206 delete *weakBinder;
207 *weakBinder = nullptr;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700208}
209AIBinder* AIBinder_Weak_promote(AIBinder_Weak* weakBinder) {
Steven Moreland5ccb70f2018-09-04 16:30:21 -0700210 if (weakBinder == nullptr) {
211 return nullptr;
212 }
213
Steven Moreland2e87adc2018-08-20 19:47:00 -0700214 sp<AIBinder> binder = weakBinder->binder.promote();
215 AIBinder_incStrong(binder.get());
216 return binder.get();
217}
218
219AIBinder_Class::AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate,
220 AIBinder_Class_onDestroy onDestroy,
221 AIBinder_Class_onTransact onTransact)
222 : onCreate(onCreate),
223 onDestroy(onDestroy),
224 onTransact(onTransact),
225 mInterfaceDescriptor(interfaceDescriptor) {}
226
227AIBinder_Class* AIBinder_Class_define(const char* interfaceDescriptor,
228 AIBinder_Class_onCreate onCreate,
229 AIBinder_Class_onDestroy onDestroy,
230 AIBinder_Class_onTransact onTransact) {
231 if (interfaceDescriptor == nullptr || onCreate == nullptr || onDestroy == nullptr ||
232 onTransact == nullptr) {
233 return nullptr;
234 }
235
236 return new AIBinder_Class(interfaceDescriptor, onCreate, onDestroy, onTransact);
237}
238
Steven Moreland901c7452018-09-04 16:17:28 -0700239void AIBinder_DeathRecipient::TransferDeathRecipient::binderDied(const wp<IBinder>& who) {
240 CHECK(who == mWho);
241
242 mOnDied(mCookie);
243 mWho = nullptr;
244}
245
246AIBinder_DeathRecipient::AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied)
247 : mOnDied(onDied) {
248 CHECK(onDied != nullptr);
249}
250
251binder_status_t AIBinder_DeathRecipient::linkToDeath(AIBinder* binder, void* cookie) {
252 CHECK(binder != nullptr);
253
254 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
255
256 sp<TransferDeathRecipient> recipient =
257 new TransferDeathRecipient(binder->getBinder(), cookie, mOnDied);
258
Steven Moreland5d62e442018-09-13 15:01:02 -0700259 status_t status = binder->getBinder()->linkToDeath(recipient, cookie, 0 /*flags*/);
260 if (status != STATUS_OK) {
261 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700262 }
263
264 mDeathRecipients.push_back(recipient);
Steven Moreland5d62e442018-09-13 15:01:02 -0700265 return STATUS_OK;
Steven Moreland901c7452018-09-04 16:17:28 -0700266}
267
268binder_status_t AIBinder_DeathRecipient::unlinkToDeath(AIBinder* binder, void* cookie) {
269 CHECK(binder != nullptr);
270
271 std::lock_guard<std::mutex> l(mDeathRecipientsMutex);
272
273 for (auto it = mDeathRecipients.rbegin(); it != mDeathRecipients.rend(); ++it) {
274 sp<TransferDeathRecipient> recipient = *it;
275
Steven Moreland5d62e442018-09-13 15:01:02 -0700276 if (recipient->getCookie() == cookie && recipient->getWho() == binder->getBinder()) {
Steven Moreland901c7452018-09-04 16:17:28 -0700277 mDeathRecipients.erase(it.base() - 1);
278
Steven Moreland5d62e442018-09-13 15:01:02 -0700279 status_t status = binder->getBinder()->unlinkToDeath(recipient, cookie, 0 /*flags*/);
280 if (status != ::android::OK) {
Steven Moreland901c7452018-09-04 16:17:28 -0700281 LOG(ERROR) << __func__
282 << ": removed reference to death recipient but unlink failed.";
283 }
Steven Moreland5d62e442018-09-13 15:01:02 -0700284 return PruneStatusT(status);
Steven Moreland901c7452018-09-04 16:17:28 -0700285 }
286 }
287
Steven Moreland5d62e442018-09-13 15:01:02 -0700288 return STATUS_NAME_NOT_FOUND;
Steven Moreland901c7452018-09-04 16:17:28 -0700289}
290
291// start of C-API methods
292
Steven Moreland2e87adc2018-08-20 19:47:00 -0700293AIBinder* AIBinder_new(const AIBinder_Class* clazz, void* args) {
294 if (clazz == nullptr) {
295 LOG(ERROR) << __func__ << ": Must provide class to construct local binder.";
296 return nullptr;
297 }
298
299 void* userData = clazz->onCreate(args);
300
Steven Moreland71cddc32018-08-30 23:39:22 -0700301 sp<AIBinder> ret = new ABBinder(clazz, userData);
302 ABBinderTag::attach(ret->getBinder());
303
304 AIBinder_incStrong(ret.get());
305 return ret.get();
Steven Moreland2e87adc2018-08-20 19:47:00 -0700306}
307
Steven Moreland5ea54da2018-09-04 13:29:55 -0700308bool AIBinder_isRemote(const AIBinder* binder) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700309 if (binder == nullptr) {
310 return true;
311 }
312
313 return binder->isRemote();
314}
315
Steven Moreland65867d72018-09-04 14:22:26 -0700316bool AIBinder_isAlive(const AIBinder* binder) {
317 if (binder == nullptr) {
318 return false;
319 }
320
321 return const_cast<AIBinder*>(binder)->getBinder()->isBinderAlive();
322}
323
324binder_status_t AIBinder_ping(AIBinder* binder) {
325 if (binder == nullptr) {
Steven Moreland5d62e442018-09-13 15:01:02 -0700326 return STATUS_UNEXPECTED_NULL;
Steven Moreland65867d72018-09-04 14:22:26 -0700327 }
328
Steven Moreland5d62e442018-09-13 15:01:02 -0700329 return PruneStatusT(binder->getBinder()->pingBinder());
Steven Moreland65867d72018-09-04 14:22:26 -0700330}
331
Steven Moreland901c7452018-09-04 16:17:28 -0700332binder_status_t AIBinder_linkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
333 void* cookie) {
334 if (binder == nullptr || recipient == nullptr) {
335 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700336 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700337 }
338
Steven Moreland5d62e442018-09-13 15:01:02 -0700339 // returns binder_status_t
Steven Moreland901c7452018-09-04 16:17:28 -0700340 return recipient->linkToDeath(binder, cookie);
341}
342
343binder_status_t AIBinder_unlinkToDeath(AIBinder* binder, AIBinder_DeathRecipient* recipient,
344 void* cookie) {
345 if (binder == nullptr || recipient == nullptr) {
346 LOG(ERROR) << __func__ << ": Must provide binder and recipient.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700347 return STATUS_UNEXPECTED_NULL;
Steven Moreland901c7452018-09-04 16:17:28 -0700348 }
349
Steven Moreland5d62e442018-09-13 15:01:02 -0700350 // returns binder_status_t
Steven Moreland901c7452018-09-04 16:17:28 -0700351 return recipient->unlinkToDeath(binder, cookie);
352}
353
Steven Moreland2e87adc2018-08-20 19:47:00 -0700354void AIBinder_incStrong(AIBinder* binder) {
355 if (binder == nullptr) {
356 LOG(ERROR) << __func__ << ": on null binder";
357 return;
358 }
359
360 binder->incStrong(nullptr);
361}
362void AIBinder_decStrong(AIBinder* binder) {
363 if (binder == nullptr) {
364 LOG(ERROR) << __func__ << ": on null binder";
365 return;
366 }
367
368 binder->decStrong(nullptr);
369}
370int32_t AIBinder_debugGetRefCount(AIBinder* binder) {
371 if (binder == nullptr) {
372 LOG(ERROR) << __func__ << ": on null binder";
373 return -1;
374 }
375
376 return binder->getStrongCount();
377}
378
Steven Moreland71cddc32018-08-30 23:39:22 -0700379bool AIBinder_associateClass(AIBinder* binder, const AIBinder_Class* clazz) {
380 if (binder == nullptr) {
381 return false;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700382 }
383
Steven Moreland71cddc32018-08-30 23:39:22 -0700384 return binder->associateClass(clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700385}
386
387const AIBinder_Class* AIBinder_getClass(AIBinder* binder) {
388 if (binder == nullptr) {
389 return nullptr;
390 }
391
392 return binder->getClass();
393}
394
395void* AIBinder_getUserData(AIBinder* binder) {
396 if (binder == nullptr) {
397 return nullptr;
398 }
399
400 ABBinder* bBinder = binder->asABBinder();
401 if (bBinder == nullptr) {
402 return nullptr;
403 }
404
405 return bBinder->getUserData();
406}
407
408binder_status_t AIBinder_prepareTransaction(AIBinder* binder, AParcel** in) {
409 if (binder == nullptr || in == nullptr) {
410 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700411 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700412 }
413 const AIBinder_Class* clazz = binder->getClass();
414 if (clazz == nullptr) {
415 LOG(ERROR) << __func__
416 << ": Class must be defined for a remote binder transaction. See "
417 "AIBinder_associateClass.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700418 return STATUS_INVALID_OPERATION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700419 }
420
Steven Moreland3527c2e2018-09-05 17:07:14 -0700421 if (!binder->isRemote()) {
Steven Moreland74521c82018-09-07 14:50:40 -0700422 LOG(WARNING) << "A binder object at " << binder
423 << " is being transacted on, however, this object is in the same process as "
424 "its proxy. Transacting with this binder is expensive compared to just "
425 "calling the corresponding functionality in the same process.";
Steven Moreland3527c2e2018-09-05 17:07:14 -0700426 }
427
Steven Moreland2e87adc2018-08-20 19:47:00 -0700428 *in = new AParcel(binder);
Steven Morelandf18615b2018-09-14 11:43:06 -0700429 status_t status = (*in)->get()->writeInterfaceToken(clazz->getInterfaceDescriptor());
Steven Moreland5d62e442018-09-13 15:01:02 -0700430 binder_status_t ret = PruneStatusT(status);
431
432 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700433 delete *in;
434 *in = nullptr;
435 }
436
Steven Moreland5d62e442018-09-13 15:01:02 -0700437 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700438}
439
Steven Moreland2e87adc2018-08-20 19:47:00 -0700440binder_status_t AIBinder_transact(AIBinder* binder, transaction_code_t code, AParcel** in,
441 AParcel** out, binder_flags_t flags) {
442 if (in == nullptr) {
443 LOG(ERROR) << __func__ << ": requires non-null in parameter";
Steven Moreland5d62e442018-09-13 15:01:02 -0700444 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700445 }
446
Steven Morelandcaa776c2018-09-04 13:48:11 -0700447 using AutoParcelDestroyer = std::unique_ptr<AParcel*, void (*)(AParcel**)>;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700448 // This object is the input to the transaction. This function takes ownership of it and deletes
449 // it.
Steven Morelandcaa776c2018-09-04 13:48:11 -0700450 AutoParcelDestroyer forIn(in, AParcel_delete);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700451
452 if (!isUserCommand(code)) {
453 LOG(ERROR) << __func__ << ": Only user-defined transactions can be made from the NDK.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700454 return STATUS_UNKNOWN_TRANSACTION;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700455 }
456
457 if ((flags & ~FLAG_ONEWAY) != 0) {
458 LOG(ERROR) << __func__ << ": Unrecognized flags sent: " << flags;
Steven Moreland5d62e442018-09-13 15:01:02 -0700459 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700460 }
461
462 if (binder == nullptr || *in == nullptr || out == nullptr) {
463 LOG(ERROR) << __func__ << ": requires non-null parameters.";
Steven Moreland5d62e442018-09-13 15:01:02 -0700464 return STATUS_UNEXPECTED_NULL;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700465 }
466
467 if ((*in)->getBinder() != binder) {
468 LOG(ERROR) << __func__ << ": parcel is associated with binder object " << binder
469 << " but called with " << (*in)->getBinder();
Steven Moreland5d62e442018-09-13 15:01:02 -0700470 return STATUS_BAD_VALUE;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700471 }
472
473 *out = new AParcel(binder);
474
Steven Morelandf18615b2018-09-14 11:43:06 -0700475 status_t status = binder->getBinder()->transact(code, *(*in)->get(), (*out)->get(), flags);
Steven Moreland5d62e442018-09-13 15:01:02 -0700476 binder_status_t ret = PruneStatusT(status);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700477
Steven Moreland5d62e442018-09-13 15:01:02 -0700478 if (ret != STATUS_OK) {
Steven Moreland2e87adc2018-08-20 19:47:00 -0700479 delete *out;
480 *out = nullptr;
481 }
482
Steven Moreland5d62e442018-09-13 15:01:02 -0700483 return ret;
Steven Moreland2e87adc2018-08-20 19:47:00 -0700484}
Steven Moreland901c7452018-09-04 16:17:28 -0700485
486AIBinder_DeathRecipient* AIBinder_DeathRecipient_new(
487 AIBinder_DeathRecipient_onBinderDied onBinderDied) {
488 if (onBinderDied == nullptr) {
489 LOG(ERROR) << __func__ << ": requires non-null onBinderDied parameter.";
490 return nullptr;
491 }
492 return new AIBinder_DeathRecipient(onBinderDied);
493}
494
495void AIBinder_DeathRecipient_delete(AIBinder_DeathRecipient** recipient) {
496 if (recipient == nullptr) {
497 return;
498 }
499
500 delete *recipient;
501 *recipient = nullptr;
502}