blob: ed3b37b9f26dd506453ffc20e4c9d79ef3d64645 [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>
20#include "AIBinder_internal.h"
21
22#include <atomic>
23
24#include <binder/Binder.h>
25#include <binder/IBinder.h>
26
27inline bool isUserCommand(transaction_code_t code) {
28 return code >= FIRST_CALL_TRANSACTION && code <= LAST_CALL_TRANSACTION;
29}
30
31struct ABBinder;
32struct ABpBinder;
33
34struct AIBinder : public virtual ::android::RefBase {
35 AIBinder(const AIBinder_Class* clazz);
36 virtual ~AIBinder();
37
Steven Moreland71cddc32018-08-30 23:39:22 -070038 bool associateClass(const AIBinder_Class* clazz);
Steven Moreland2e87adc2018-08-20 19:47:00 -070039 const AIBinder_Class* getClass() const { return mClazz; }
40
Steven Moreland2e87adc2018-08-20 19:47:00 -070041 virtual ::android::sp<::android::IBinder> getBinder() = 0;
42 virtual ABBinder* asABBinder() { return nullptr; }
43 virtual ABpBinder* asABpBinder() { return nullptr; }
44
Steven Moreland71cddc32018-08-30 23:39:22 -070045 bool isRemote() { return getBinder()->remoteBinder() != nullptr; }
Steven Moreland2e87adc2018-08-20 19:47:00 -070046
47private:
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.
55struct ABBinder : public AIBinder, public ::android::BBinder {
Steven Moreland2e87adc2018-08-20 19:47:00 -070056 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
67private:
Steven Moreland71cddc32018-08-30 23:39:22 -070068 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 Moreland2e87adc2018-08-20 19:47:00 -070073 // 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.
81struct ABpBinder : public AIBinder, public ::android::BpRefBase {
Steven Moreland71cddc32018-08-30 23:39:22 -070082 static ::android::sp<AIBinder> fromBinder(const ::android::sp<::android::IBinder>& binder);
83
Steven Moreland2e87adc2018-08-20 19:47:00 -070084 virtual ~ABpBinder();
85
86 ::android::sp<::android::IBinder> getBinder() override { return remote(); }
87 ABpBinder* asABpBinder() override { return this; }
Steven Moreland71cddc32018-08-30 23:39:22 -070088
89private:
90 ABpBinder(const ::android::sp<::android::IBinder>& binder);
Steven Moreland2e87adc2018-08-20 19:47:00 -070091};
92
93struct 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
103private:
104 // This must be a String16 since BBinder virtual getInterfaceDescriptor returns a reference to
105 // one.
106 const ::android::String16 mInterfaceDescriptor;
107};