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_RESOLVER_RESOLVER_REGISTRY_H 18 #define GRPC_SRC_CORE_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 #include "src/core/lib/channel/channel_args.h" 29 #include "src/core/lib/iomgr/iomgr_fwd.h" 30 #include "src/core/resolver/resolver.h" 31 #include "src/core/resolver/resolver_factory.h" 32 #include "src/core/util/orphanable.h" 33 #include "src/core/util/uri.h" 34 35 namespace grpc_core { 36 37 class ResolverRegistry final { 38 private: 39 // Forward declaration needed to use this in Builder. 40 struct State { 41 std::map<absl::string_view, std::unique_ptr<ResolverFactory>> factories; 42 std::string default_prefix; 43 }; 44 45 public: 46 /// Methods used to create and populate the ResolverRegistry. 47 /// NOT THREAD SAFE -- to be used only during global gRPC 48 /// initialization and shutdown. 49 class Builder final { 50 public: 51 Builder(); 52 53 /// Sets the default URI prefix to \a default_prefix. 54 void SetDefaultPrefix(std::string default_prefix); 55 56 /// Registers a resolver factory. The factory will be used to create a 57 /// resolver for any URI whose scheme matches that of the factory. 58 void RegisterResolverFactory(std::unique_ptr<ResolverFactory> factory); 59 60 /// Returns true iff scheme already has a registered factory. 61 bool HasResolverFactory(absl::string_view scheme) const; 62 63 /// Wipe everything in the registry and reset to empty. 64 void Reset(); 65 66 ResolverRegistry Build(); 67 68 private: 69 ResolverRegistry::State state_; 70 }; 71 72 ResolverRegistry(const ResolverRegistry&) = delete; 73 ResolverRegistry& operator=(const ResolverRegistry&) = delete; 74 ResolverRegistry(ResolverRegistry&&) noexcept; 75 ResolverRegistry& operator=(ResolverRegistry&&) noexcept; 76 77 /// Checks whether the user input \a target is valid to create a resolver. 78 bool IsValidTarget(absl::string_view target) const; 79 80 /// Creates a resolver given \a target. 81 /// First tries to parse \a target as a URI. If this succeeds, tries 82 /// to locate a registered resolver factory based on the URI scheme. 83 /// If parsing fails or there is no factory for the URI's scheme, 84 /// prepends default_prefix to target and tries again. 85 /// If a resolver factory is found, uses it to instantiate a resolver and 86 /// returns it; otherwise, returns nullptr. 87 /// \a args, \a pollset_set, and \a work_serializer are passed to the 88 /// factory's \a CreateResolver() method. \a args are the channel args to be 89 /// included in resolver results. \a pollset_set is used to drive I/O in the 90 /// name resolution process. \a work_serializer is the work_serializer under 91 /// which all resolver calls will be run. \a result_handler is used to return 92 /// results from the resolver. 93 OrphanablePtr<Resolver> CreateResolver( 94 absl::string_view target, const ChannelArgs& args, 95 grpc_pollset_set* pollset_set, 96 std::shared_ptr<WorkSerializer> work_serializer, 97 std::unique_ptr<Resolver::ResultHandler> result_handler) const; 98 99 /// Returns the default authority to pass from a client for \a target. 100 std::string GetDefaultAuthority(absl::string_view target) const; 101 102 /// Returns \a target with the default prefix prepended, if needed. 103 std::string AddDefaultPrefixIfNeeded(absl::string_view target) const; 104 105 /// Returns the resolver factory for \a scheme. 106 /// Caller does NOT own the return value. 107 ResolverFactory* LookupResolverFactory(absl::string_view scheme) const; 108 109 private: ResolverRegistry(State state)110 explicit ResolverRegistry(State state) : state_(std::move(state)) {} 111 112 // TODO(ctiller): fix callers such that the canonical_target argument can be 113 // removed, and replaced with uri.ToString(). 114 ResolverFactory* FindResolverFactory(absl::string_view target, URI* uri, 115 std::string* canonical_target) const; 116 117 State state_; 118 }; 119 120 } // namespace grpc_core 121 122 #endif // GRPC_SRC_CORE_RESOLVER_RESOLVER_REGISTRY_H 123