1 // Copyright 2021 gRPC authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_WIRE_FORMAT_BINDER_ANDROID_H 16 #define GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_WIRE_FORMAT_BINDER_ANDROID_H 17 18 #include <grpc/support/port_platform.h> 19 20 #ifdef GPR_SUPPORT_BINDER_TRANSPORT 21 22 #include <jni.h> 23 24 #include <memory> 25 26 #include "absl/memory/memory.h" 27 28 #include "src/core/ext/transport/binder/utils/binder_auto_utils.h" 29 #include "src/core/ext/transport/binder/utils/ndk_binder.h" 30 #include "src/core/ext/transport/binder/wire_format/binder.h" 31 #include "src/core/ext/transport/binder/wire_format/wire_reader.h" 32 33 namespace grpc_binder { 34 35 ndk_util::SpAIBinder FromJavaBinder(JNIEnv* jni_env, jobject binder); 36 37 class BinderAndroid; 38 39 class WritableParcelAndroid final : public WritableParcel { 40 public: 41 WritableParcelAndroid() = default; WritableParcelAndroid(ndk_util::AParcel * parcel)42 explicit WritableParcelAndroid(ndk_util::AParcel* parcel) : parcel_(parcel) {} 43 ~WritableParcelAndroid() override = default; 44 45 int32_t GetDataSize() const override; 46 absl::Status WriteInt32(int32_t data) override; 47 absl::Status WriteInt64(int64_t data) override; 48 absl::Status WriteBinder(HasRawBinder* binder) override; 49 absl::Status WriteString(absl::string_view s) override; 50 absl::Status WriteByteArray(const int8_t* buffer, int32_t length) override; 51 52 private: 53 ndk_util::AParcel* parcel_ = nullptr; 54 55 friend class BinderAndroid; 56 }; 57 58 class ReadableParcelAndroid final : public ReadableParcel { 59 public: 60 ReadableParcelAndroid() = default; 61 // TODO(waynetu): Get rid of the const_cast. ReadableParcelAndroid(const ndk_util::AParcel * parcel)62 explicit ReadableParcelAndroid(const ndk_util::AParcel* parcel) 63 : parcel_(parcel) {} 64 ~ReadableParcelAndroid() override = default; 65 66 int32_t GetDataSize() const override; 67 absl::Status ReadInt32(int32_t* data) override; 68 absl::Status ReadInt64(int64_t* data) override; 69 absl::Status ReadBinder(std::unique_ptr<Binder>* data) override; 70 absl::Status ReadByteArray(std::string* data) override; 71 absl::Status ReadString(std::string* str) override; 72 73 private: 74 const ndk_util::AParcel* parcel_ = nullptr; 75 76 friend class BinderAndroid; 77 }; 78 79 class BinderAndroid final : public Binder { 80 public: BinderAndroid(ndk_util::SpAIBinder binder)81 explicit BinderAndroid(ndk_util::SpAIBinder binder) 82 : binder_(binder), 83 input_parcel_(std::make_unique<WritableParcelAndroid>()) {} 84 ~BinderAndroid() override = default; 85 GetRawBinder()86 void* GetRawBinder() override { return binder_.get(); } 87 88 void Initialize() override; 89 absl::Status PrepareTransaction() override; 90 absl::Status Transact(BinderTransportTxCode tx_code) override; 91 GetWritableParcel()92 WritableParcel* GetWritableParcel() const override { 93 return input_parcel_.get(); 94 } 95 96 std::unique_ptr<TransactionReceiver> ConstructTxReceiver( 97 grpc_core::RefCountedPtr<WireReader> wire_reader_ref, 98 TransactionReceiver::OnTransactCb transact_cb) const override; 99 100 private: 101 ndk_util::SpAIBinder binder_; 102 std::unique_ptr<WritableParcelAndroid> input_parcel_; 103 }; 104 105 class TransactionReceiverAndroid final : public TransactionReceiver { 106 public: 107 TransactionReceiverAndroid( 108 grpc_core::RefCountedPtr<WireReader> wire_reader_ref, 109 OnTransactCb transaction_cb); 110 ~TransactionReceiverAndroid() override; GetRawBinder()111 void* GetRawBinder() override { return binder_; } 112 113 private: 114 ndk_util::AIBinder* binder_; 115 OnTransactCb transact_cb_; 116 }; 117 118 } // namespace grpc_binder 119 120 #endif // GPR_SUPPORT_BINDER_TRANSPORT 121 122 #endif // GRPC_SRC_CORE_EXT_TRANSPORT_BINDER_WIRE_FORMAT_BINDER_ANDROID_H 123