1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //     http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef GRPC_SRC_CORE_LIB_RESOLVER_RESOLVER_REGISTRY_H
18 #define GRPC_SRC_CORE_LIB_RESOLVER_RESOLVER_REGISTRY_H
19 
20 #include <grpc/support/port_platform.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 
27 #include "absl/strings/string_view.h"
28 
29 #include "src/core/lib/channel/channel_args.h"
30 #include "src/core/lib/gprpp/orphanable.h"
31 #include "src/core/lib/iomgr/iomgr_fwd.h"
32 #include "src/core/lib/resolver/resolver.h"
33 #include "src/core/lib/resolver/resolver_factory.h"
34 #include "src/core/lib/uri/uri_parser.h"
35 
36 namespace grpc_core {
37 
38 class ResolverRegistry {
39  private:
40   // Forward declaration needed to use this in Builder.
41   struct State {
42     std::map<absl::string_view, std::unique_ptr<ResolverFactory>> factories;
43     std::string default_prefix;
44   };
45 
46  public:
47   /// Methods used to create and populate the ResolverRegistry.
48   /// NOT THREAD SAFE -- to be used only during global gRPC
49   /// initialization and shutdown.
50   class Builder {
51    public:
52     Builder();
53 
54     /// Sets the default URI prefix to \a default_prefix.
55     void SetDefaultPrefix(std::string default_prefix);
56 
57     /// Registers a resolver factory.  The factory will be used to create a
58     /// resolver for any URI whose scheme matches that of the factory.
59     void RegisterResolverFactory(std::unique_ptr<ResolverFactory> factory);
60 
61     /// Returns true iff scheme already has a registered factory.
62     bool HasResolverFactory(absl::string_view scheme) const;
63 
64     /// Wipe everything in the registry and reset to empty.
65     void Reset();
66 
67     ResolverRegistry Build();
68 
69    private:
70     ResolverRegistry::State state_;
71   };
72 
73   ResolverRegistry(const ResolverRegistry&) = delete;
74   ResolverRegistry& operator=(const ResolverRegistry&) = delete;
75   ResolverRegistry(ResolverRegistry&&) noexcept;
76   ResolverRegistry& operator=(ResolverRegistry&&) noexcept;
77 
78   /// Checks whether the user input \a target is valid to create a resolver.
79   bool IsValidTarget(absl::string_view target) const;
80 
81   /// Creates a resolver given \a target.
82   /// First tries to parse \a target as a URI. If this succeeds, tries
83   /// to locate a registered resolver factory based on the URI scheme.
84   /// If parsing fails or there is no factory for the URI's scheme,
85   /// prepends default_prefix to target and tries again.
86   /// If a resolver factory is found, uses it to instantiate a resolver and
87   /// returns it; otherwise, returns nullptr.
88   /// \a args, \a pollset_set, and \a work_serializer are passed to the
89   /// factory's \a CreateResolver() method. \a args are the channel args to be
90   /// included in resolver results. \a pollset_set is used to drive I/O in the
91   /// name resolution process. \a work_serializer is the work_serializer under
92   /// which all resolver calls will be run. \a result_handler is used to return
93   /// results from the resolver.
94   OrphanablePtr<Resolver> CreateResolver(
95       absl::string_view target, const ChannelArgs& args,
96       grpc_pollset_set* pollset_set,
97       std::shared_ptr<WorkSerializer> work_serializer,
98       std::unique_ptr<Resolver::ResultHandler> result_handler) const;
99 
100   /// Returns the default authority to pass from a client for \a target.
101   std::string GetDefaultAuthority(absl::string_view target) const;
102 
103   /// Returns \a target with the default prefix prepended, if needed.
104   std::string AddDefaultPrefixIfNeeded(absl::string_view target) const;
105 
106   /// Returns the resolver factory for \a scheme.
107   /// Caller does NOT own the return value.
108   ResolverFactory* LookupResolverFactory(absl::string_view scheme) const;
109 
110  private:
ResolverRegistry(State state)111   explicit ResolverRegistry(State state) : state_(std::move(state)) {}
112 
113   // TODO(ctiller): fix callers such that the canonical_target argument can be
114   // removed, and replaced with uri.ToString().
115   ResolverFactory* FindResolverFactory(absl::string_view target, URI* uri,
116                                        std::string* canonical_target) const;
117 
118   State state_;
119 };
120 
121 }  // namespace grpc_core
122 
123 #endif  // GRPC_SRC_CORE_LIB_RESOLVER_RESOLVER_REGISTRY_H
124