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_CORE_EXT_TRANSPORT_BINDER_WIRE_FORMAT_BINDER_H
16 #define GRPC_CORE_EXT_TRANSPORT_BINDER_WIRE_FORMAT_BINDER_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <cstdint>
21 #include <functional>
22 #include <memory>
23 #include <string>
24 
25 #include "absl/status/status.h"
26 #include "absl/strings/string_view.h"
27 
28 #include "src/core/ext/transport/binder/wire_format/binder_constants.h"
29 #include "src/core/lib/gprpp/orphanable.h"
30 
31 namespace grpc_binder {
32 
33 class HasRawBinder {
34  public:
35   virtual ~HasRawBinder() = default;
36   virtual void* GetRawBinder() = 0;
37 };
38 
39 class Binder;
40 
41 // TODO(waynetu): We might need other methods as well.
42 // TODO(waynetu): Find a better way to express the returned status than
43 // binder_status_t.
44 class WritableParcel {
45  public:
46   virtual ~WritableParcel() = default;
47   virtual int32_t GetDataSize() const = 0;
48   virtual absl::Status WriteInt32(int32_t data) = 0;
49   virtual absl::Status WriteInt64(int64_t data) = 0;
50   virtual absl::Status WriteBinder(HasRawBinder* binder) = 0;
51   virtual absl::Status WriteString(absl::string_view s) = 0;
52   virtual absl::Status WriteByteArray(const int8_t* buffer, int32_t length) = 0;
53 
WriteByteArrayWithLength(absl::string_view buffer)54   absl::Status WriteByteArrayWithLength(absl::string_view buffer) {
55     absl::Status status = WriteInt32(buffer.length());
56     if (!status.ok()) return status;
57     if (buffer.empty()) return absl::OkStatus();
58     return WriteByteArray(reinterpret_cast<const int8_t*>(buffer.data()),
59                           buffer.length());
60   }
61 };
62 
63 // TODO(waynetu): We might need other methods as well.
64 // TODO(waynetu): Find a better way to express the returned status than
65 // binder_status_t.
66 class ReadableParcel {
67  public:
68   virtual ~ReadableParcel() = default;
69   virtual int32_t GetDataSize() const = 0;
70   virtual absl::Status ReadInt32(int32_t* data) = 0;
71   virtual absl::Status ReadInt64(int64_t* data) = 0;
72   virtual absl::Status ReadBinder(std::unique_ptr<Binder>* data) = 0;
73   virtual absl::Status ReadByteArray(std::string* data) = 0;
74   virtual absl::Status ReadString(std::string* str) = 0;
75 };
76 
77 class TransactionReceiver : public HasRawBinder {
78  public:
79   using OnTransactCb =
80       std::function<absl::Status(transaction_code_t, ReadableParcel*, int uid)>;
81 
82   ~TransactionReceiver() override = default;
83 };
84 
85 class WireReader;
86 
87 class Binder : public HasRawBinder {
88  public:
89   ~Binder() override = default;
90 
91   virtual void Initialize() = 0;
92   virtual absl::Status PrepareTransaction() = 0;
93   virtual absl::Status Transact(BinderTransportTxCode tx_code) = 0;
94 
95   virtual WritableParcel* GetWritableParcel() const = 0;
96 
97   // TODO(waynetu): Can we decouple the receiver from the binder?
98   virtual std::unique_ptr<TransactionReceiver> ConstructTxReceiver(
99       grpc_core::RefCountedPtr<WireReader> wire_reader_ref,
100       TransactionReceiver::OnTransactCb transact_cb) const = 0;
101 };
102 
103 }  // namespace grpc_binder
104 
105 #endif  // GRPC_CORE_EXT_TRANSPORT_BINDER_WIRE_FORMAT_BINDER_H
106