endpoint_binder_pool.cc
Go to the documentation of this file.
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 
16 
18 
19 #ifndef GRPC_NO_BINDER
20 
22 
23 #ifdef GPR_SUPPORT_BINDER_TRANSPORT
24 
25 #include <jni.h>
26 
28 
29 extern "C" {
30 // Adds endpoint binder to binder pool when Java notify us that the endpoint
31 // binder is ready. This is called from GrpcBinderConnection.java
32 JNIEXPORT void JNICALL
33 Java_io_grpc_binder_cpp_GrpcBinderConnection_notifyConnected__Ljava_lang_String_2Landroid_os_IBinder_2(
34  JNIEnv* jni_env, jobject, jstring conn_id_jstring, jobject ibinder) {
35  jboolean isCopy;
36  const char* conn_id = jni_env->GetStringUTFChars(conn_id_jstring, &isCopy);
37  gpr_log(GPR_INFO, "%s invoked with conn_id = %s", __func__, conn_id);
38  GPR_ASSERT(ibinder != nullptr);
39  grpc_binder::ndk_util::SpAIBinder aibinder =
40  grpc_binder::FromJavaBinder(jni_env, ibinder);
41  gpr_log(GPR_INFO, "%s got aibinder = %p", __func__, aibinder.get());
42  auto b = absl::make_unique<grpc_binder::BinderAndroid>(aibinder);
43  GPR_ASSERT(b != nullptr);
45  std::move(b));
46  if (isCopy == JNI_TRUE) {
47  jni_env->ReleaseStringUTFChars(conn_id_jstring, conn_id);
48  }
49 }
50 }
51 
52 #endif // GPR_SUPPORT_BINDER_TRANSPORT
53 
54 namespace grpc_binder {
55 
57  std::string conn_id,
58  std::function<void(std::unique_ptr<grpc_binder::Binder>)> cb) {
59  gpr_log(GPR_INFO, "EndpointBinder requested. conn_id = %s", conn_id.c_str());
60  std::unique_ptr<grpc_binder::Binder> b;
61  {
63  if (binder_map_.count(conn_id)) {
64  b = std::move(binder_map_[conn_id]);
65  binder_map_.erase(conn_id);
66  GPR_ASSERT(b != nullptr);
67  } else {
68  if (pending_requests_.count(conn_id) != 0) {
70  "Duplicate GetEndpointBinder requested. conn_id = %s",
71  conn_id.c_str());
72  return;
73  }
74  pending_requests_[conn_id] = std::move(cb);
75  return;
76  }
77  }
78  GPR_ASSERT(b != nullptr);
79  cb(std::move(b));
80 }
81 
83  std::string conn_id, std::unique_ptr<grpc_binder::Binder> b) {
84  gpr_log(GPR_INFO, "EndpointBinder added. conn_id = %s", conn_id.c_str());
85  GPR_ASSERT(b != nullptr);
86  // cb will be set in the following block if there is a pending callback
87  std::function<void(std::unique_ptr<grpc_binder::Binder>)> cb = nullptr;
88  {
90  if (binder_map_.count(conn_id) != 0) {
91  gpr_log(GPR_ERROR, "EndpointBinder already in the pool. conn_id = %s",
92  conn_id.c_str());
93  return;
94  }
95  if (pending_requests_.count(conn_id)) {
96  cb = std::move(pending_requests_[conn_id]);
97  pending_requests_.erase(conn_id);
98  } else {
99  binder_map_[conn_id] = std::move(b);
100  b = nullptr;
101  }
102  }
103  if (cb != nullptr) {
104  cb(std::move(b));
105  }
106 }
107 
109  static EndpointBinderPool* p = new EndpointBinderPool();
110  return p;
111 }
112 } // namespace grpc_binder
113 #endif
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
grpc_binder::EndpointBinderPool::m_
grpc_core::Mutex m_
Definition: endpoint_binder_pool.h:52
grpc_core::MutexLock
Definition: src/core/lib/gprpp/sync.h:88
grpc_binder::EndpointBinderPool::GetEndpointBinder
void GetEndpointBinder(std::string conn_id, std::function< void(std::unique_ptr< grpc_binder::Binder >)> cb)
Definition: endpoint_binder_pool.cc:56
grpc_binder
Definition: connection_id_generator.cc:45
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
jni_utils.h
binder_android.h
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
endpoint_binder_pool.h
grpc_binder::GetEndpointBinderPool
EndpointBinderPool * GetEndpointBinderPool()
Definition: endpoint_binder_pool.cc:108
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_binder::EndpointBinderPool::AddEndpointBinder
void AddEndpointBinder(std::string conn_id, std::unique_ptr< grpc_binder::Binder > b)
Definition: endpoint_binder_pool.cc:82
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc_binder::EndpointBinderPool
Definition: endpoint_binder_pool.h:36
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
cb
OPENSSL_EXPORT pem_password_cb * cb
Definition: pem.h:351
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:19