Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -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 | #pragma once |
| 18 | |
| 19 | #include <android/binder_ibinder.h> |
Steven Moreland | 4d5ad49 | 2018-09-13 12:49:16 -0700 | [diff] [blame] | 20 | #include "ibinder_internal.h" |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 21 | |
| 22 | #include <atomic> |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 23 | #include <mutex> |
| 24 | #include <vector> |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 25 | |
| 26 | #include <binder/Binder.h> |
| 27 | #include <binder/IBinder.h> |
| 28 | |
| 29 | inline bool isUserCommand(transaction_code_t code) { |
| 30 | return code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION; |
| 31 | } |
| 32 | |
| 33 | struct ABBinder; |
| 34 | struct ABpBinder; |
| 35 | |
| 36 | struct AIBinder : public virtual ::android::RefBase { |
| 37 | AIBinder(const AIBinder_Class* clazz); |
| 38 | virtual ~AIBinder(); |
| 39 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 40 | bool associateClass(const AIBinder_Class* clazz); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 41 | const AIBinder_Class* getClass() const { return mClazz; } |
| 42 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 43 | virtual ::android::sp<::android::IBinder> getBinder() = 0; |
| 44 | virtual ABBinder* asABBinder() { return nullptr; } |
| 45 | virtual ABpBinder* asABpBinder() { return nullptr; } |
| 46 | |
Steven Moreland | 5ea54da | 2018-09-04 13:29:55 -0700 | [diff] [blame] | 47 | bool isRemote() const { |
| 48 | ::android::sp<::android::IBinder> binder = const_cast<AIBinder*>(this)->getBinder(); |
| 49 | return binder->remoteBinder() != nullptr; |
| 50 | } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 51 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame^] | 52 | private: |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 53 | // AIBinder instance is instance of this class for a local object. In order to transact on a |
| 54 | // remote object, this also must be set for simplicity (although right now, only the |
| 55 | // interfaceDescriptor from it is used). |
| 56 | const AIBinder_Class* mClazz; |
| 57 | }; |
| 58 | |
| 59 | // This is a local AIBinder object with a known class. |
| 60 | struct ABBinder : public AIBinder, public ::android::BBinder { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 61 | virtual ~ABBinder(); |
| 62 | |
| 63 | void* getUserData() { return mUserData; } |
| 64 | |
| 65 | ::android::sp<::android::IBinder> getBinder() override { return this; } |
| 66 | ABBinder* asABBinder() override { return this; } |
| 67 | |
| 68 | const ::android::String16& getInterfaceDescriptor() const override; |
Steven Moreland | 5d62e44 | 2018-09-13 15:01:02 -0700 | [diff] [blame] | 69 | ::android::status_t onTransact(uint32_t code, const ::android::Parcel& data, |
| 70 | ::android::Parcel* reply, binder_flags_t flags) override; |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 71 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame^] | 72 | private: |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 73 | ABBinder(const AIBinder_Class* clazz, void* userData); |
| 74 | |
| 75 | // only thing that should create an ABBinder |
| 76 | friend AIBinder* AIBinder_new(const AIBinder_Class*, void*); |
| 77 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 78 | // Can contain implementation if this is a local binder. This can still be nullptr for a local |
| 79 | // binder. If it is nullptr, the implication is the implementation state is entirely external to |
| 80 | // this object and the functionality provided in the AIBinder_Class is sufficient. |
| 81 | void* mUserData; |
| 82 | }; |
| 83 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 84 | // This binder object may be remote or local (even though it is 'Bp'). The implication if it is |
| 85 | // local is that it is an IBinder object created outside of the domain of libbinder_ndk. |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 86 | struct ABpBinder : public AIBinder, public ::android::BpRefBase { |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 87 | // Looks up to see if this object has or is an existing ABBinder or ABpBinder object, otherwise |
| 88 | // it creates an ABpBinder object. |
| 89 | static ::android::sp<AIBinder> lookupOrCreateFromBinder( |
| 90 | const ::android::sp<::android::IBinder>& binder); |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 91 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 92 | virtual ~ABpBinder(); |
| 93 | |
Steven Moreland | 9496895 | 2018-09-05 14:42:59 -0700 | [diff] [blame] | 94 | void onLastStrongRef(const void* id) override; |
| 95 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 96 | ::android::sp<::android::IBinder> getBinder() override { return remote(); } |
| 97 | ABpBinder* asABpBinder() override { return this; } |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 98 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame^] | 99 | private: |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame] | 100 | ABpBinder(const ::android::sp<::android::IBinder>& binder); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | struct AIBinder_Class { |
| 104 | AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, |
| 105 | AIBinder_Class_onDestroy onDestroy, AIBinder_Class_onTransact onTransact); |
| 106 | |
| 107 | const ::android::String16& getInterfaceDescriptor() const { return mInterfaceDescriptor; } |
| 108 | |
| 109 | const AIBinder_Class_onCreate onCreate; |
| 110 | const AIBinder_Class_onDestroy onDestroy; |
| 111 | const AIBinder_Class_onTransact onTransact; |
| 112 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame^] | 113 | private: |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 114 | // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to |
| 115 | // one. |
| 116 | const ::android::String16 mInterfaceDescriptor; |
| 117 | }; |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 118 | |
| 119 | // Ownership is like this (when linked to death): |
| 120 | // |
| 121 | // AIBinder_DeathRecipient -sp-> TransferDeathRecipient <-wp-> IBinder |
| 122 | // |
| 123 | // When the AIBinder_DeathRecipient is dropped, so are the actual underlying death recipients. When |
| 124 | // the IBinder dies, only a wp to it is kept. |
| 125 | struct AIBinder_DeathRecipient { |
| 126 | // One of these is created for every linkToDeath. This is to be able to recover data when a |
| 127 | // binderDied receipt only gives us information about the IBinder. |
| 128 | struct TransferDeathRecipient : ::android::IBinder::DeathRecipient { |
| 129 | TransferDeathRecipient(const ::android::wp<::android::IBinder>& who, void* cookie, |
| 130 | const AIBinder_DeathRecipient_onBinderDied& onDied) |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame^] | 131 | : mWho(who), mCookie(cookie), mOnDied(onDied) {} |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 132 | |
| 133 | void binderDied(const ::android::wp<::android::IBinder>& who) override; |
| 134 | |
| 135 | const ::android::wp<::android::IBinder>& getWho() { return mWho; } |
| 136 | void* getCookie() { return mCookie; } |
| 137 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame^] | 138 | private: |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 139 | ::android::wp<::android::IBinder> mWho; |
| 140 | void* mCookie; |
| 141 | const AIBinder_DeathRecipient_onBinderDied& mOnDied; |
| 142 | }; |
| 143 | |
| 144 | AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied); |
| 145 | binder_status_t linkToDeath(AIBinder* binder, void* cookie); |
| 146 | binder_status_t unlinkToDeath(AIBinder* binder, void* cookie); |
| 147 | |
Steven Moreland | 6cf66ac | 2018-11-02 18:14:54 -0700 | [diff] [blame^] | 148 | private: |
Steven Moreland | 901c745 | 2018-09-04 16:17:28 -0700 | [diff] [blame] | 149 | std::mutex mDeathRecipientsMutex; |
| 150 | std::vector<::android::sp<TransferDeathRecipient>> mDeathRecipients; |
| 151 | AIBinder_DeathRecipient_onBinderDied mOnDied; |
| 152 | }; |