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 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame^] | 38 | bool associateClass(const AIBinder_Class* clazz); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 39 | const AIBinder_Class* getClass() const { return mClazz; } |
| 40 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 41 | virtual ::android::sp<::android::IBinder> getBinder() = 0; |
| 42 | virtual ABBinder* asABBinder() { return nullptr; } |
| 43 | virtual ABpBinder* asABpBinder() { return nullptr; } |
| 44 | |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame^] | 45 | bool isRemote() { return getBinder()->remoteBinder() != nullptr; } |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 46 | |
| 47 | private: |
| 48 | // AIBinder instance is instance of this class for a local object. In order to transact on a |
| 49 | // remote object, this also must be set for simplicity (although right now, only the |
| 50 | // interfaceDescriptor from it is used). |
| 51 | const AIBinder_Class* mClazz; |
| 52 | }; |
| 53 | |
| 54 | // This is a local AIBinder object with a known class. |
| 55 | struct ABBinder : public AIBinder, public ::android::BBinder { |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 56 | virtual ~ABBinder(); |
| 57 | |
| 58 | void* getUserData() { return mUserData; } |
| 59 | |
| 60 | ::android::sp<::android::IBinder> getBinder() override { return this; } |
| 61 | ABBinder* asABBinder() override { return this; } |
| 62 | |
| 63 | const ::android::String16& getInterfaceDescriptor() const override; |
| 64 | binder_status_t onTransact(uint32_t code, const ::android::Parcel& data, |
| 65 | ::android::Parcel* reply, binder_flags_t flags) override; |
| 66 | |
| 67 | private: |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame^] | 68 | ABBinder(const AIBinder_Class* clazz, void* userData); |
| 69 | |
| 70 | // only thing that should create an ABBinder |
| 71 | friend AIBinder* AIBinder_new(const AIBinder_Class*, void*); |
| 72 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 73 | // Can contain implementation if this is a local binder. This can still be nullptr for a local |
| 74 | // binder. If it is nullptr, the implication is the implementation state is entirely external to |
| 75 | // this object and the functionality provided in the AIBinder_Class is sufficient. |
| 76 | void* mUserData; |
| 77 | }; |
| 78 | |
| 79 | // This binder object may be remote or local (even though it is 'Bp'). It is not yet associated with |
| 80 | // a class. |
| 81 | struct ABpBinder : public AIBinder, public ::android::BpRefBase { |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame^] | 82 | static ::android::sp<AIBinder> fromBinder(const ::android::sp<::android::IBinder>& binder); |
| 83 | |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 84 | virtual ~ABpBinder(); |
| 85 | |
| 86 | ::android::sp<::android::IBinder> getBinder() override { return remote(); } |
| 87 | ABpBinder* asABpBinder() override { return this; } |
Steven Moreland | 71cddc3 | 2018-08-30 23:39:22 -0700 | [diff] [blame^] | 88 | |
| 89 | private: |
| 90 | ABpBinder(const ::android::sp<::android::IBinder>& binder); |
Steven Moreland | 2e87adc | 2018-08-20 19:47:00 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | struct AIBinder_Class { |
| 94 | AIBinder_Class(const char* interfaceDescriptor, AIBinder_Class_onCreate onCreate, |
| 95 | AIBinder_Class_onDestroy onDestroy, AIBinder_Class_onTransact onTransact); |
| 96 | |
| 97 | const ::android::String16& getInterfaceDescriptor() const { return mInterfaceDescriptor; } |
| 98 | |
| 99 | const AIBinder_Class_onCreate onCreate; |
| 100 | const AIBinder_Class_onDestroy onDestroy; |
| 101 | const AIBinder_Class_onTransact onTransact; |
| 102 | |
| 103 | private: |
| 104 | // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to |
| 105 | // one. |
| 106 | const ::android::String16 mInterfaceDescriptor; |
| 107 | }; |