xref: /aosp_15_r20/external/grpc-grpc/src/cpp/ext/proto_server_reflection_plugin.cc (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 #include <memory>
20 #include <string>
21 
22 #include <grpcpp/ext/proto_server_reflection_plugin.h>
23 #include <grpcpp/impl/server_builder_plugin.h>
24 #include <grpcpp/impl/server_initializer.h>
25 #include <grpcpp/server_builder.h>
26 
27 #include "src/cpp/ext/proto_server_reflection.h"
28 
29 namespace grpc {
30 namespace reflection {
31 
ProtoServerReflectionPlugin()32 ProtoServerReflectionPlugin::ProtoServerReflectionPlugin()
33     : backend_(std::make_shared<ProtoServerReflectionBackend>()),
34       reflection_service_v1alpha_(
35           std::make_shared<ProtoServerReflection>(backend_)),
36       reflection_service_v1_(
37           std::make_shared<ProtoServerReflectionV1>(backend_)) {}
38 
name()39 std::string ProtoServerReflectionPlugin::name() {
40   return "proto_server_reflection";
41 }
42 
InitServer(grpc::ServerInitializer * si)43 void ProtoServerReflectionPlugin::InitServer(grpc::ServerInitializer* si) {
44   si->RegisterService(reflection_service_v1_);
45   si->RegisterService(reflection_service_v1alpha_);
46 }
47 
Finish(grpc::ServerInitializer * si)48 void ProtoServerReflectionPlugin::Finish(grpc::ServerInitializer* si) {
49   backend_->SetServiceList(si->GetServiceList());
50 }
51 
ChangeArguments(const std::string &,void *)52 void ProtoServerReflectionPlugin::ChangeArguments(const std::string& /*name*/,
53                                                   void* /*value*/) {}
54 
has_sync_methods() const55 bool ProtoServerReflectionPlugin::has_sync_methods() const {
56   return (reflection_service_v1_ &&
57           reflection_service_v1_->has_synchronous_methods()) ||
58          (reflection_service_v1alpha_ &&
59           reflection_service_v1alpha_->has_synchronous_methods());
60 }
61 
has_async_methods() const62 bool ProtoServerReflectionPlugin::has_async_methods() const {
63   return (reflection_service_v1_ &&
64           reflection_service_v1_->has_async_methods()) ||
65          (reflection_service_v1alpha_ &&
66           reflection_service_v1alpha_->has_async_methods());
67 }
68 
CreateProtoReflection()69 static std::unique_ptr<grpc::ServerBuilderPlugin> CreateProtoReflection() {
70   return std::unique_ptr<grpc::ServerBuilderPlugin>(
71       new ProtoServerReflectionPlugin());
72 }
73 
InitProtoReflectionServerBuilderPlugin()74 void InitProtoReflectionServerBuilderPlugin() {
75   static struct Initialize {
76     Initialize() {
77       grpc::ServerBuilder::InternalAddPluginFactory(&CreateProtoReflection);
78     }
79   } initializer;
80 }
81 
82 // Force InitProtoReflectionServerBuilderPlugin() to be called at static
83 // initialization time.
84 struct StaticProtoReflectionPluginInitializer {
StaticProtoReflectionPluginInitializergrpc::reflection::StaticProtoReflectionPluginInitializer85   StaticProtoReflectionPluginInitializer() {
86     InitProtoReflectionServerBuilderPlugin();
87   }
88 } static_proto_reflection_plugin_initializer;
89 
90 }  // namespace reflection
91 }  // namespace grpc
92