create_channel_binder.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef GRPCPP_CREATE_CHANNEL_BINDER_H
  15. #define GRPCPP_CREATE_CHANNEL_BINDER_H
  16. #include <grpc/support/port_platform.h>
  17. #ifdef GPR_ANDROID
  18. #include <jni.h>
  19. #include <memory>
  20. #include "y_absl/strings/string_view.h"
  21. #include <grpcpp/channel.h>
  22. #include <grpcpp/security/binder_security_policy.h>
  23. #include <grpcpp/support/channel_arguments.h>
  24. namespace grpc {
  25. namespace experimental {
  26. /// EXPERIMENTAL Create a new \a Channel based on binder transport. The package
  27. /// name and class name will be used identify the specific application component
  28. /// to connect to.
  29. ///
  30. /// \param jni_env_void Pointer to a JNIEnv structure
  31. /// \param context The context that we will use to invoke \a bindService See
  32. /// https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.ServiceConnection,%20int)
  33. /// for detail.
  34. /// \param package_name Package name of the component to be connected to
  35. /// \param class_name Class name of the component to be connected to
  36. /// \param security_policy Used for checking if remote component is allowed to
  37. /// connect
  38. std::shared_ptr<grpc::Channel> CreateBinderChannel(
  39. void* jni_env_void, jobject context, y_absl::string_view package_name,
  40. y_absl::string_view class_name,
  41. std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
  42. security_policy);
  43. /// EXPERIMENTAL Create a new \a Channel based on binder transport. The package
  44. /// name and class name will be used identify the specific application component
  45. /// to connect to.
  46. ///
  47. /// \param jni_env_void Pointer to a JNIEnv structure
  48. /// \param context The context that we will use to invoke \a bindService See
  49. /// https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.ServiceConnection,%20int)
  50. /// for detail.
  51. /// \param package_name Package name of the component to be connected to
  52. /// \param class_name Class name of the component to be connected to
  53. /// \param security_policy Used for checking if remote component is allowed to
  54. /// connect
  55. /// \param args Options for channel creation.
  56. std::shared_ptr<grpc::Channel> CreateCustomBinderChannel(
  57. void* jni_env_void, jobject context, y_absl::string_view package_name,
  58. y_absl::string_view class_name,
  59. std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy,
  60. const ChannelArguments& args);
  61. /// EXPERIMENTAL Create a new \a Channel based on binder transport.
  62. ///
  63. /// \param jni_env_void Pointer to a JNIEnv structure
  64. /// \param context The context that we will use to invoke \a bindService See
  65. /// https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.ServiceConnection,%20int)
  66. /// for detail.
  67. /// \param uri An URI that can be parsed as an `Intent` with
  68. /// https://developer.android.com/reference/android/content/Intent#parseUri(java.lang.String,%20int)
  69. /// \param security_policy Used for checking if remote component is allowed to
  70. /// connect
  71. std::shared_ptr<grpc::Channel> CreateBinderChannel(
  72. void* jni_env_void, jobject context, y_absl::string_view uri,
  73. std::shared_ptr<grpc::experimental::binder::SecurityPolicy>
  74. security_policy);
  75. /// EXPERIMENTAL Create a new \a Channel based on binder transport.
  76. ///
  77. /// \param jni_env_void Pointer to a JNIEnv structure
  78. /// \param context The context that we will use to invoke \a bindService See
  79. /// https://developer.android.com/reference/android/content/Context#bindService(android.content.Intent,%20android.content.ServiceConnection,%20int)
  80. /// for detail.
  81. /// \param uri An URI that can be parsed as an `Intent` with
  82. /// https://developer.android.com/reference/android/content/Intent#parseUri(java.lang.String,%20int)
  83. /// \param security_policy Used for checking if remote component is allowed to
  84. /// connect
  85. /// \param args Options for channel creation.
  86. std::shared_ptr<grpc::Channel> CreateCustomBinderChannel(
  87. void* jni_env_void, jobject context, y_absl::string_view uri,
  88. std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy,
  89. const ChannelArguments& args);
  90. /// EXPERIMENTAL Finds internal binder transport Java code. To create channels
  91. /// in threads created in native code, it is required to call this function
  92. /// once beforehand in a thread that is not created in native code.
  93. /// See
  94. /// https://developer.android.com/training/articles/perf-jni#faq:-why-didnt-findclass-find-my-class
  95. /// for details of this limitation.
  96. /// Returns true when the initialization is successful.
  97. bool InitializeBinderChannelJavaClass(void* jni_env_void);
  98. /// EXPERIMENTAL Alternative version of `InitializeBinderChannelJavaClass(void*
  99. /// jni_env_void)`. This version used a user-specified function to find the
  100. /// required internal Java class. When a class is found, the `class_finder`
  101. /// function should return a local reference to the class (jclass type). The
  102. /// returned jclass will then be used to create global reference for gRPC to use
  103. /// it later. After that, gRPC will DeleteLocalRef the returned local reference.
  104. bool InitializeBinderChannelJavaClass(
  105. void* jni_env_void, std::function<void*(TString)> class_finder);
  106. } // namespace experimental
  107. } // namespace grpc
  108. #endif
  109. #endif // GRPCPP_CREATE_CHANNEL_BINDER_H