• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpcpp/ext/proto_server_reflection_plugin.h>
20 #include <grpcpp/impl/server_builder_plugin.h>
21 #include <grpcpp/impl/server_initializer.h>
22 #include <grpcpp/server_builder.h>
23 
24 #include <memory>
25 #include <string>
26 
27 #include "src/core/config/config_vars.h"
28 #include "src/cpp/ext/proto_server_reflection.h"
29 
30 namespace grpc {
31 namespace reflection {
32 
ProtoServerReflectionPlugin()33 ProtoServerReflectionPlugin::ProtoServerReflectionPlugin()
34     : backend_(std::make_shared<ProtoServerReflectionBackend>()),
35       reflection_service_v1alpha_(
36           std::make_shared<ProtoServerReflection>(backend_)),
37       reflection_service_v1_(
38           std::make_shared<ProtoServerReflectionV1>(backend_)) {}
39 
name()40 std::string ProtoServerReflectionPlugin::name() {
41   return "proto_server_reflection";
42 }
43 
InitServer(grpc::ServerInitializer * si)44 void ProtoServerReflectionPlugin::InitServer(grpc::ServerInitializer* si) {
45   // We cannot simply keep the plugin from being unregistered because this must
46   // happen at static initialization time, whereas flag configuration that
47   // controls this is not received until later.
48   if (!grpc_core::ConfigVars::Get().CppExperimentalDisableReflection()) {
49     si->RegisterService(reflection_service_v1_);
50     si->RegisterService(reflection_service_v1alpha_);
51   }
52 }
53 
Finish(grpc::ServerInitializer * si)54 void ProtoServerReflectionPlugin::Finish(grpc::ServerInitializer* si) {
55   backend_->SetServiceList(si->GetServiceList());
56 }
57 
ChangeArguments(const std::string &,void *)58 void ProtoServerReflectionPlugin::ChangeArguments(const std::string& /*name*/,
59                                                   void* /*value*/) {}
60 
has_sync_methods() const61 bool ProtoServerReflectionPlugin::has_sync_methods() const {
62   if (!grpc_core::ConfigVars::Get().CppExperimentalDisableReflection()) {
63     return (reflection_service_v1_ &&
64             reflection_service_v1_->has_synchronous_methods()) ||
65            (reflection_service_v1alpha_ &&
66             reflection_service_v1alpha_->has_synchronous_methods());
67   }
68   return false;
69 }
70 
has_async_methods() const71 bool ProtoServerReflectionPlugin::has_async_methods() const {
72   if (!grpc_core::ConfigVars::Get().CppExperimentalDisableReflection()) {
73     return (reflection_service_v1_ &&
74             reflection_service_v1_->has_async_methods()) ||
75            (reflection_service_v1alpha_ &&
76             reflection_service_v1alpha_->has_async_methods());
77   }
78   return false;
79 }
80 
CreateProtoReflection()81 static std::unique_ptr<grpc::ServerBuilderPlugin> CreateProtoReflection() {
82   return std::unique_ptr<grpc::ServerBuilderPlugin>(
83       new ProtoServerReflectionPlugin());
84 }
85 
InitProtoReflectionServerBuilderPlugin()86 void InitProtoReflectionServerBuilderPlugin() {
87   static struct Initialize {
88     Initialize() {
89       grpc::ServerBuilder::InternalAddPluginFactory(&CreateProtoReflection);
90     }
91   } initializer;
92 }
93 
94 // Force InitProtoReflectionServerBuilderPlugin() to be called at static
95 // initialization time.
96 struct StaticProtoReflectionPluginInitializer {
StaticProtoReflectionPluginInitializergrpc::reflection::StaticProtoReflectionPluginInitializer97   StaticProtoReflectionPluginInitializer() {
98     InitProtoReflectionServerBuilderPlugin();
99   }
100 } static_proto_reflection_plugin_initializer;
101 
102 }  // namespace reflection
103 }  // namespace grpc
104