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