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> |
| 20 | #include "AIBinder_internal.h" |
| 21 | |
| 22 | #include <atomic> |
| 23 | |
| 24 | #include <binder/Binder.h> |
| 25 | #include <binder/IBinder.h> |
| 26 | |
| 27 | inline bool isUserCommand(transaction_code_t code) { |
| 28 | return code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION; |
| 29 | } |
| 30 | |
| 31 | struct ABBinder; |
| 32 | struct ABpBinder; |
| 33 | |
| 34 | struct AIBinder : public virtual ::android::RefBase { |
| 35 | AIBinder(const AIBinder_Class* clazz); |
| 36 | virtual ~AIBinder(); |
| 37 | |
| 38 | // This returns an AIBinder object with this class associated. If the class is already |
| 39 | // associated, 'this' will be returned. If there is a local AIBinder implementation, that will |
| 40 | // be returned. If this is a remote object, the class will be associated and this will be ready |
| 41 | // to be used for transactions. |
| 42 | ::android::sp<AIBinder> associateClass(const AIBinder_Class* clazz); |
| 43 | const AIBinder_Class* getClass() const { return mClazz; } |
| 44 | |
| 45 | // This does not create the binder if it does not exist in the process. |
| 46 | virtual ::android::sp<::android::IBinder> getBinder() = 0; |
| 47 | virtual ABBinder* asABBinder() { return nullptr; } |
| 48 | virtual ABpBinder* asABpBinder() { return nullptr; } |
| 49 | |
| 50 | bool isRemote() { |
| 51 | auto binder = getBinder(); |
| 52 | // if the binder is nullptr, then it is a local object which hasn't been sent out of process |
| 53 | // yet. |
| 54 | return binder != nullptr && binder->remoteBinder() != nullptr; |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | // AIBinder instance is instance of this class for a local object. In order to transact on a |
| 59 | // remote object, this also must be set for simplicity (although right now, only the |
| 60 | // interfaceDescriptor from it is used). |
| 61 | const AIBinder_Class* mClazz; |
| 62 | }; |
| 63 | |
| 64 | // This is a local AIBinder object with a known class. |
| 65 | struct ABBinder : public AIBinder, public ::android::BBinder { |
| 66 | ABBinder(const AIBinder_Class* clazz, void* userData); |
| 67 | virtual ~ABBinder(); |
| 68 | |
| 69 | void* getUserData() { return mUserData; } |
| 70 | |
| 71 | ::android::sp<::android::IBinder> getBinder() override { return this; } |
| 72 | ABBinder* asABBinder() override { return this; } |
| 73 | |
| 74 | const ::android::String16& getInterfaceDescriptor() const override; |
| 75 | binder_status_t onTransact(uint32_t code, const ::android::Parcel& data, |
| 76 | ::android::Parcel* reply, binder_flags_t flags) override; |
| 77 | |
| 78 | private: |
| 79 | // Can contain implementation if this is a local binder. This can still be nullptr for a local |
| 80 | // binder. If it is nullptr, the implication is the implementation state is entirely external to |
| 81 | // this object and the functionality provided in the AIBinder_Class is sufficient. |
| 82 | void* mUserData; |
| 83 | }; |
| 84 | |
| 85 | // This binder object may be remote or local (even though it is 'Bp'). It is not yet associated with |
| 86 | // a class. |
| 87 | struct ABpBinder : public AIBinder, public ::android::BpRefBase { |
| 88 | ABpBinder(::android::sp<::android::IBinder> binder); |
| 89 | virtual ~ABpBinder(); |
| 90 | |
| 91 | ::android::sp<::android::IBinder> getBinder() override { return remote(); } |
| 92 | ABpBinder* asABpBinder() override { return this; } |
| 93 | }; |
| 94 | |
| 95 | struct AIBinder_Class { |
| 96 | AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, |
| 97 | AIBinder_Class_onDestroy onDestroy, AIBinder_Class_onTransact onTransact); |
| 98 | |
| 99 | const ::android::String16& getInterfaceDescriptor() const { return mInterfaceDescriptor; } |
| 100 | |
| 101 | const AIBinder_Class_onCreate onCreate; |
| 102 | const AIBinder_Class_onDestroy onDestroy; |
| 103 | const AIBinder_Class_onTransact onTransact; |
| 104 | |
| 105 | private: |
| 106 | // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to |
| 107 | // one. |
| 108 | const ::android::String16 mInterfaceDescriptor; |
| 109 | }; |