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