xref: /aosp_15_r20/external/grpc-grpc/include/grpcpp/support/client_interceptor.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPCPP_SUPPORT_CLIENT_INTERCEPTOR_H
20 #define GRPCPP_SUPPORT_CLIENT_INTERCEPTOR_H
21 
22 #include <memory>
23 #include <vector>
24 
25 #include <grpc/support/log.h>
26 #include <grpcpp/impl/rpc_method.h>
27 #include <grpcpp/support/interceptor.h>
28 #include <grpcpp/support/string_ref.h>
29 
30 namespace grpc {
31 
32 class Channel;
33 class ClientContext;
34 
35 namespace internal {
36 class InterceptorBatchMethodsImpl;
37 }
38 
39 namespace experimental {
40 class ClientRpcInfo;
41 
42 // A factory interface for creation of client interceptors. A vector of
43 // factories can be provided at channel creation which will be used to create a
44 // new vector of client interceptors per RPC. Client interceptor authors should
45 // create a subclass of ClientInterceptorFactorInterface which creates objects
46 // of their interceptors.
47 class ClientInterceptorFactoryInterface {
48  public:
~ClientInterceptorFactoryInterface()49   virtual ~ClientInterceptorFactoryInterface() {}
50   // Returns a pointer to an Interceptor object on successful creation, nullptr
51   // otherwise. If nullptr is returned, this server interceptor factory is
52   // ignored for the purposes of that RPC.
53   virtual Interceptor* CreateClientInterceptor(ClientRpcInfo* info) = 0;
54 };
55 }  // namespace experimental
56 
57 namespace internal {
58 extern experimental::ClientInterceptorFactoryInterface*
59     g_global_client_interceptor_factory;
60 
61 extern experimental::ClientInterceptorFactoryInterface*
62     g_global_client_stats_interceptor_factory;
63 }  // namespace internal
64 
65 /// ClientRpcInfo represents the state of a particular RPC as it
66 /// appears to an interceptor. It is created and owned by the library and
67 /// passed to the CreateClientInterceptor method of the application's
68 /// ClientInterceptorFactoryInterface implementation
69 namespace experimental {
70 class ClientRpcInfo {
71  public:
72   // TODO(yashykt): Stop default-constructing ClientRpcInfo and remove UNKNOWN
73   //                from the list of possible Types.
74   /// Type categorizes RPCs by unary or streaming type
75   enum class Type {
76     UNARY,
77     CLIENT_STREAMING,
78     SERVER_STREAMING,
79     BIDI_STREAMING,
80     UNKNOWN  // UNKNOWN is not API and will be removed later
81   };
82 
~ClientRpcInfo()83   ~ClientRpcInfo() {}
84 
85   // Delete copy constructor but allow default move constructor
86   ClientRpcInfo(const ClientRpcInfo&) = delete;
87   ClientRpcInfo(ClientRpcInfo&&) = default;
88 
89   // Getter methods
90 
91   /// Return the fully-specified method name
method()92   const char* method() const { return method_; }
93 
94   /// Return an identifying suffix for the client stub, or nullptr if one wasn't
95   /// specified.
suffix_for_stats()96   const char* suffix_for_stats() const { return suffix_for_stats_; }
97 
98   /// Return a pointer to the channel on which the RPC is being sent
channel()99   ChannelInterface* channel() { return channel_; }
100 
101   /// Return a pointer to the underlying ClientContext structure associated
102   /// with the RPC to support features that apply to it
client_context()103   grpc::ClientContext* client_context() { return ctx_; }
104 
105   /// Return the type of the RPC (unary or a streaming flavor)
type()106   Type type() const { return type_; }
107 
108  private:
109   static_assert(Type::UNARY ==
110                     static_cast<Type>(internal::RpcMethod::NORMAL_RPC),
111                 "violated expectation about Type enum");
112   static_assert(Type::CLIENT_STREAMING ==
113                     static_cast<Type>(internal::RpcMethod::CLIENT_STREAMING),
114                 "violated expectation about Type enum");
115   static_assert(Type::SERVER_STREAMING ==
116                     static_cast<Type>(internal::RpcMethod::SERVER_STREAMING),
117                 "violated expectation about Type enum");
118   static_assert(Type::BIDI_STREAMING ==
119                     static_cast<Type>(internal::RpcMethod::BIDI_STREAMING),
120                 "violated expectation about Type enum");
121 
122   // Default constructor should only be used by ClientContext
123   ClientRpcInfo() = default;
124 
125   // Constructor will only be called from ClientContext
ClientRpcInfo(grpc::ClientContext * ctx,internal::RpcMethod::RpcType type,const char * method,const char * suffix_for_stats,grpc::ChannelInterface * channel)126   ClientRpcInfo(grpc::ClientContext* ctx, internal::RpcMethod::RpcType type,
127                 const char* method, const char* suffix_for_stats,
128                 grpc::ChannelInterface* channel)
129       : ctx_(ctx),
130         type_(static_cast<Type>(type)),
131         method_(method),
132         suffix_for_stats_(suffix_for_stats),
133         channel_(channel) {}
134 
135   // Move assignment should only be used by ClientContext
136   // TODO(yashykt): Delete move assignment
137   ClientRpcInfo& operator=(ClientRpcInfo&&) = default;
138 
139   // Runs interceptor at pos \a pos.
RunInterceptor(experimental::InterceptorBatchMethods * interceptor_methods,size_t pos)140   void RunInterceptor(
141       experimental::InterceptorBatchMethods* interceptor_methods, size_t pos) {
142     GPR_ASSERT(pos < interceptors_.size());
143     interceptors_[pos]->Intercept(interceptor_methods);
144   }
145 
RegisterInterceptors(const std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> & creators,size_t interceptor_pos)146   void RegisterInterceptors(
147       const std::vector<std::unique_ptr<
148           experimental::ClientInterceptorFactoryInterface>>& creators,
149       size_t interceptor_pos) {
150     // TODO(yashykt): This calculation seems broken for the case where an
151     // interceptor factor returns nullptr.
152     size_t num_interceptors =
153         creators.size() +
154         (internal::g_global_client_stats_interceptor_factory != nullptr) +
155         (internal::g_global_client_interceptor_factory != nullptr);
156     if (interceptor_pos > num_interceptors) {
157       // No interceptors to register
158       return;
159     }
160     if (internal::g_global_client_stats_interceptor_factory != nullptr) {
161       interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
162           internal::g_global_client_stats_interceptor_factory
163               ->CreateClientInterceptor(this)));
164       --interceptor_pos;
165     }
166     // NOTE: The following is not a range-based for loop because it will only
167     //       iterate over a portion of the creators vector.
168     for (auto it = creators.begin() + interceptor_pos; it != creators.end();
169          ++it) {
170       auto* interceptor = (*it)->CreateClientInterceptor(this);
171       if (interceptor != nullptr) {
172         interceptors_.push_back(
173             std::unique_ptr<experimental::Interceptor>(interceptor));
174       }
175     }
176     if (internal::g_global_client_interceptor_factory != nullptr) {
177       interceptors_.push_back(std::unique_ptr<experimental::Interceptor>(
178           internal::g_global_client_interceptor_factory
179               ->CreateClientInterceptor(this)));
180     }
181   }
182 
183   grpc::ClientContext* ctx_ = nullptr;
184   // TODO(yashykt): make type_ const once move-assignment is deleted
185   Type type_{Type::UNKNOWN};
186   const char* method_ = nullptr;
187   const char* suffix_for_stats_ = nullptr;
188   grpc::ChannelInterface* channel_ = nullptr;
189   std::vector<std::unique_ptr<experimental::Interceptor>> interceptors_;
190   bool hijacked_ = false;
191   size_t hijacked_interceptor_ = 0;
192 
193   friend class internal::InterceptorBatchMethodsImpl;
194   friend class grpc::ClientContext;
195 };
196 
197 // PLEASE DO NOT USE THIS. ALWAYS PREFER PER CHANNEL INTERCEPTORS OVER A GLOBAL
198 // INTERCEPTOR. IF USAGE IS ABSOLUTELY NECESSARY, PLEASE READ THE SAFETY NOTES.
199 // Registers a global client interceptor factory object, which is used for all
200 // RPCs made in this process. The application is responsible for maintaining the
201 // life of the object while gRPC operations are in progress. The global
202 // interceptor factory should only be registered once at the start of the
203 // process before any gRPC operations have begun.
204 void RegisterGlobalClientInterceptorFactory(
205     ClientInterceptorFactoryInterface* factory);
206 
207 // For testing purposes only
208 void TestOnlyResetGlobalClientInterceptorFactory();
209 
210 }  // namespace experimental
211 }  // namespace grpc
212 
213 #endif  // GRPCPP_SUPPORT_CLIENT_INTERCEPTOR_H
214