blob: 7852298ef9e0bd54b3335bca952c3004764f6d2c [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#pragma once
18
19#include <android/binder_ibinder.h>
Steven Moreland4d5ad492018-09-13 12:49:16 -070020#include "ibinder_internal.h"
Steven Moreland2e87adc2018-08-20 19:47:00 -070021
22#include <atomic>
Steven Moreland901c7452018-09-04 16:17:28 -070023#include <mutex>
24#include <vector>
Steven Moreland2e87adc2018-08-20 19:47:00 -070025
26#include <binder/Binder.h>
27#include <binder/IBinder.h>
28
29inline bool isUserCommand(transaction_code_t code) {
30 return code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION;
31}
32
33struct ABBinder;
34struct ABpBinder;
35
36struct AIBinder : public virtual ::android::RefBase {
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -080037 explicit AIBinder(const AIBinder_Class* clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -070038 virtual ~AIBinder();
39
Steven Moreland71cddc32018-08-30 23:39:22 -070040 bool associateClass(const AIBinder_Class* clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -070041 const AIBinder_Class* getClass() const { return mClazz; }
42
Steven Moreland2e87adc2018-08-20 19:47:00 -070043 virtual ::android::sp<::android::IBinder> getBinder() = 0;
44 virtual ABBinder* asABBinder() { return nullptr; }
45 virtual ABpBinder* asABpBinder() { return nullptr; }
46
Steven Moreland5ea54da2018-09-04 13:29:55 -070047 bool isRemote() const {
48 ::android::sp<::android::IBinder> binder = const_cast<AIBinder*>(this)->getBinder();
49 return binder->remoteBinder() != nullptr;
50 }
Steven Moreland2e87adc2018-08-20 19:47:00 -070051
Steven Moreland6cf66ac2018-11-02 18:14:54 -070052 private:
Steven Moreland2e87adc2018-08-20 19:47:00 -070053 // 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.
60struct ABBinder : public AIBinder, public ::android::BBinder {
Steven Moreland2e87adc2018-08-20 19:47:00 -070061 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 Moreland5d62e442018-09-13 15:01:02 -070069 ::android::status_t onTransact(uint32_t code, const ::android::Parcel& data,
70 ::android::Parcel* reply, binder_flags_t flags) override;
Steven Moreland2e87adc2018-08-20 19:47:00 -070071
Steven Moreland6cf66ac2018-11-02 18:14:54 -070072 private:
Steven Moreland71cddc32018-08-30 23:39:22 -070073 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 Moreland2e87adc2018-08-20 19:47:00 -070078 // 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 Moreland94968952018-09-05 14:42:59 -070084// 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 Moreland2e87adc2018-08-20 19:47:00 -070086struct ABpBinder : public AIBinder, public ::android::BpRefBase {
Steven Moreland94968952018-09-05 14:42:59 -070087 // 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 Moreland71cddc32018-08-30 23:39:22 -070091
Steven Moreland2e87adc2018-08-20 19:47:00 -070092 virtual ~ABpBinder();
93
Steven Moreland94968952018-09-05 14:42:59 -070094 void onLastStrongRef(const void* id) override;
95
Steven Moreland2e87adc2018-08-20 19:47:00 -070096 ::android::sp<::android::IBinder> getBinder() override { return remote(); }
97 ABpBinder* asABpBinder() override { return this; }
Steven Moreland71cddc32018-08-30 23:39:22 -070098
Steven Moreland6cf66ac2018-11-02 18:14:54 -070099 private:
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800100 explicit ABpBinder(const ::android::sp<::android::IBinder>& binder);
Steven Moreland2e87adc2018-08-20 19:47:00 -0700101};
102
103struct 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 Moreland6cf66ac2018-11-02 18:14:54 -0700113 private:
Steven Moreland2e87adc2018-08-20 19:47:00 -0700114 // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to
115 // one.
116 const ::android::String16 mInterfaceDescriptor;
117};
Steven Moreland901c7452018-09-04 16:17:28 -0700118
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.
125struct 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 Moreland6cf66ac2018-11-02 18:14:54 -0700131 : mWho(who), mCookie(cookie), mOnDied(onDied) {}
Steven Moreland901c7452018-09-04 16:17:28 -0700132
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 Moreland6cf66ac2018-11-02 18:14:54 -0700138 private:
Steven Moreland901c7452018-09-04 16:17:28 -0700139 ::android::wp<::android::IBinder> mWho;
140 void* mCookie;
141 const AIBinder_DeathRecipient_onBinderDied& mOnDied;
142 };
143
Chih-Hung Hsieh5ca1ea42018-12-20 15:42:22 -0800144 explicit AIBinder_DeathRecipient(AIBinder_DeathRecipient_onBinderDied onDied);
Steven Moreland901c7452018-09-04 16:17:28 -0700145 binder_status_t linkToDeath(AIBinder* binder, void* cookie);
146 binder_status_t unlinkToDeath(AIBinder* binder, void* cookie);
147
Steven Moreland6cf66ac2018-11-02 18:14:54 -0700148 private:
Steven Moreland901c7452018-09-04 16:17:28 -0700149 std::mutex mDeathRecipientsMutex;
150 std::vector<::android::sp<TransferDeathRecipient>> mDeathRecipients;
151 AIBinder_DeathRecipient_onBinderDied mOnDied;
152};