Imported Upstream version 1.34.0
[platform/upstream/grpc.git] / src / core / ext / xds / certificate_provider_registry.cc
1 //
2 //
3 // Copyright 2020 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 <grpc/support/port_platform.h>
20
21 #include "absl/container/inlined_vector.h"
22
23 #include "src/core/ext/xds/certificate_provider_registry.h"
24
25 namespace grpc_core {
26
27 namespace {
28
29 class RegistryState {
30  public:
31   void RegisterCertificateProviderFactory(
32       std::unique_ptr<CertificateProviderFactory> factory) {
33     gpr_log(GPR_DEBUG, "registering certificate provider factory for \"%s\"",
34             factory->name());
35     for (size_t i = 0; i < factories_.size(); ++i) {
36       GPR_ASSERT(strcmp(factories_[i]->name(), factory->name()) != 0);
37     }
38     factories_.push_back(std::move(factory));
39   }
40
41   CertificateProviderFactory* LookupCertificateProviderFactory(
42       absl::string_view name) const {
43     for (size_t i = 0; i < factories_.size(); ++i) {
44       if (name == factories_[i]->name()) {
45         return factories_[i].get();
46       }
47     }
48     return nullptr;
49   }
50
51  private:
52   // We currently support 3 factories without doing additional
53   // allocation.  This number could be raised if there is a case where
54   // more factories are needed and the additional allocations are
55   // hurting performance (which is unlikely, since these allocations
56   // only occur at gRPC initialization time).
57   absl::InlinedVector<std::unique_ptr<CertificateProviderFactory>, 3>
58       factories_;
59 };
60
61 static RegistryState* g_state = nullptr;
62
63 }  // namespace
64
65 //
66 // CertificateProviderRegistry
67 //
68
69 CertificateProviderFactory*
70 CertificateProviderRegistry::LookupCertificateProviderFactory(
71     absl::string_view name) {
72   GPR_ASSERT(g_state != nullptr);
73   return g_state->LookupCertificateProviderFactory(name);
74 }
75
76 void CertificateProviderRegistry::InitRegistry() {
77   if (g_state == nullptr) g_state = new RegistryState();
78 }
79
80 void CertificateProviderRegistry::ShutdownRegistry() {
81   delete g_state;
82   g_state = nullptr;
83 }
84
85 void CertificateProviderRegistry::RegisterCertificateProviderFactory(
86     std::unique_ptr<CertificateProviderFactory> factory) {
87   InitRegistry();
88   g_state->RegisterCertificateProviderFactory(std::move(factory));
89 }
90
91 }  // namespace grpc_core
92
93 //
94 // Plugin registration
95 //
96
97 void grpc_certificate_provider_registry_init() {
98   grpc_core::CertificateProviderRegistry::InitRegistry();
99 }
100
101 void grpc_certificate_provider_registry_shutdown() {
102   grpc_core::CertificateProviderRegistry::ShutdownRegistry();
103 }