You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

create_channel_binder.h 6.4 kB

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