create_channel_binder.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 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 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, jobject context, absl::string_view package_name,
  40. 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 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,
  58. absl::string_view class_name,
  59. std::shared_ptr<grpc::experimental::binder::SecurityPolicy> security_policy,
  60. const ChannelArguments& args);
  61. /// EXPERIMENTAL Finds internal binder transport Java code. To create channels
  62. /// in threads created in native code, it is required to call this function
  63. /// once beforehand in a thread that is not created in native code.
  64. /// See
  65. /// https://developer.android.com/training/articles/perf-jni#faq:-why-didnt-findclass-find-my-class
  66. /// for details of this limitation.
  67. /// Returns true when the initialization is successful.
  68. bool InitializeBinderChannelJavaClass(void* jni_env_void);
  69. /// EXPERIMENTAL Alternative version of `InitializeBinderChannelJavaClass(void*
  70. /// jni_env_void)`. This version used a user-specified function to find the
  71. /// required internal Java class. When a class is found, the `class_finder`
  72. /// function should return a local reference to the class (jclass type). The
  73. /// returned jclass will then be used to create global reference for gRPC to use
  74. /// it later. After that, gRPC will DeleteLocalRef the returned local reference.
  75. bool InitializeBinderChannelJavaClass(
  76. void* jni_env_void, std::function<void*(std::string)> class_finder);
  77. } // namespace experimental
  78. } // namespace grpc
  79. #endif
  80. #endif // GRPCPP_CREATE_CHANNEL_BINDER_H