17 #ifndef GRPC_NO_BINDER
19 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
23 #include "absl/memory/memory.h"
24 #include "absl/strings/str_cat.h"
34 struct BinderUserData {
47 void* f_onCreate_userdata(
void*
data) {
48 auto*
args =
static_cast<OnCreateArgs*
>(
data);
49 return new BinderUserData(
args->wire_reader_ref,
args->callback);
52 void f_onDestroy_delete(
void*
data) {
53 auto* user_data =
static_cast<BinderUserData*
>(
data);
57 void* f_onCreate_noop(
void* ) {
return nullptr; }
58 void f_onDestroy_noop(
void* ) {}
61 ndk_util::binder_status_t f_onTransact(ndk_util::AIBinder* binder,
63 const ndk_util::AParcel*
in,
64 ndk_util::AParcel* ) {
69 static_cast<BinderUserData*
>(ndk_util::AIBinder_getUserData(binder));
72 std::unique_ptr<ReadableParcel>
output =
73 absl::make_unique<ReadableParcelAndroid>(
in);
76 (*callback)(
code,
output.get(), ndk_util::AIBinder_getCallingUid());
78 return ndk_util::STATUS_OK;
81 return ndk_util::STATUS_UNKNOWN_ERROR;
90 if (
length <= 0)
return false;
93 str->resize(
static_cast<size_t>(
length) - 1);
98 ndk_util::binder_status_t AParcelReadString(
const ndk_util::AParcel* parcel,
100 void* stringData =
static_cast<void*
>(
str);
101 return ndk_util::AParcel_readString(parcel, stringData, StdStringAllocator);
104 template <
typename T>
105 bool StdVectorAllocator(
void* vectorData,
int32_t length,
T** outBuffer) {
106 if (
length < 0)
return false;
108 std::vector<T>* vec =
static_cast<std::vector<T>*
>(vectorData);
109 if (
static_cast<size_t>(
length) > vec->max_size())
return false;
111 vec->resize(
static_cast<size_t>(
length));
112 *outBuffer = vec->data();
116 ndk_util::binder_status_t AParcelReadVector(
const ndk_util::AParcel* parcel,
117 std::vector<uint8_t>* vec) {
118 void* vectorData =
static_cast<void*
>(vec);
119 return ndk_util::AParcel_readByteArray(parcel, vectorData,
120 StdVectorAllocator<int8_t>);
125 ndk_util::SpAIBinder FromJavaBinder(JNIEnv* jni_env, jobject binder) {
126 return ndk_util::SpAIBinder(
127 ndk_util::AIBinder_fromJavaBinder(jni_env, binder));
130 TransactionReceiverAndroid::TransactionReceiverAndroid(
132 OnTransactCb transact_cb)
133 : transact_cb_(transact_cb) {
138 ndk_util::AIBinder_Class* aibinder_class = ndk_util::AIBinder_Class_define(
139 "", f_onCreate_userdata, f_onDestroy_delete,
142 ndk_util::AIBinder_Class_disableInterfaceTokenHeader(aibinder_class);
151 args.wire_reader_ref = wire_reader_ref;
152 args.callback = &transact_cb_;
153 binder_ = ndk_util::AIBinder_new(aibinder_class, &
args);
157 ndk_util::AIBinder_associateClass(binder_, aibinder_class)));
160 TransactionReceiverAndroid::~TransactionReceiverAndroid() {
162 ndk_util::AIBinder_decStrong(binder_);
167 ndk_util::binder_status_t f_onTransact_noop(ndk_util::AIBinder* ,
169 const ndk_util::AParcel* ,
170 ndk_util::AParcel* ) {
174 void AssociateWithNoopClass(ndk_util::AIBinder* binder) {
176 ndk_util::AIBinder_Class* aibinder_class = ndk_util::AIBinder_Class_define(
177 "", f_onCreate_noop, f_onDestroy_noop, f_onTransact_noop);
179 ndk_util::AIBinder_Class_disableInterfaceTokenHeader(aibinder_class);
183 ndk_util::AIBinder_associateClass(binder, aibinder_class)));
189 ndk_util::AIBinder* binder = binder_.get();
190 AssociateWithNoopClass(binder);
194 ndk_util::AIBinder* binder = binder_.get();
195 return ndk_util::AIBinder_prepareTransaction(
196 binder, &input_parcel_->parcel_) == ndk_util::STATUS_OK
199 "ndk_util::AIBinder_prepareTransaction failed");
203 ndk_util::AIBinder* binder = binder_.get();
205 ndk_util::AParcel* unused_output_parcel;
207 (ndk_util::AIBinder_transact(
209 &input_parcel_->parcel_, &unused_output_parcel,
210 ndk_util::FLAG_ONEWAY) == ndk_util::STATUS_OK)
213 ndk_util::AParcel_delete(unused_output_parcel);
217 std::unique_ptr<TransactionReceiver> BinderAndroid::ConstructTxReceiver(
220 return absl::make_unique<TransactionReceiverAndroid>(wire_reader_ref,
224 int32_t WritableParcelAndroid::GetDataSize()
const {
225 return ndk_util::AParcel_getDataSize(parcel_);
229 return ndk_util::AParcel_writeInt32(parcel_,
data) == ndk_util::STATUS_OK
235 return ndk_util::AParcel_writeInt64(parcel_,
data) == ndk_util::STATUS_OK
240 absl::Status WritableParcelAndroid::WriteBinder(HasRawBinder* binder) {
241 return ndk_util::AParcel_writeStrongBinder(
242 parcel_,
reinterpret_cast<ndk_util::AIBinder*
>(
243 binder->GetRawBinder())) == ndk_util::STATUS_OK
249 return ndk_util::AParcel_writeString(parcel_,
s.data(),
s.length()) ==
257 return ndk_util::AParcel_writeByteArray(parcel_,
buffer,
length) ==
263 int32_t ReadableParcelAndroid::GetDataSize()
const {
264 return ndk_util::AParcel_getDataSize(parcel_);
268 return ndk_util::AParcel_readInt32(parcel_,
data) == ndk_util::STATUS_OK
274 return ndk_util::AParcel_readInt64(parcel_,
data) == ndk_util::STATUS_OK
279 absl::Status ReadableParcelAndroid::ReadBinder(std::unique_ptr<Binder>*
data) {
280 ndk_util::AIBinder* binder;
281 if (AParcel_readStrongBinder(parcel_, &binder) != ndk_util::STATUS_OK) {
285 *
data = absl::make_unique<BinderAndroid>(ndk_util::SpAIBinder(binder));
290 std::vector<uint8_t> vec;
291 if (AParcelReadVector(parcel_, &vec) == ndk_util::STATUS_OK) {
292 data->resize(vec.size());
294 memcpy(&((*
data)[0]), vec.data(), vec.size());
302 return AParcelReadString(parcel_,
str) == ndk_util::STATUS_OK
309 #endif // GPR_SUPPORT_BINDER_TRANSPORT