1 // 2 // Copyright 2016 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_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H 18 #define GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H 19 20 #include <grpc/support/port_platform.h> 21 22 #include <map> 23 24 #include "absl/strings/string_view.h" 25 26 #include "src/core/ext/filters/client_channel/service_config.h" 27 #include "src/core/ext/filters/client_channel/service_config_parser.h" 28 #include "src/core/lib/channel/context.h" 29 #include "src/core/lib/gprpp/ref_counted_ptr.h" 30 31 namespace grpc_core { 32 33 /// When a service config is applied to a call in the client_channel_filter, 34 /// we create an instance of this object on the arena. A pointer to this 35 /// object is also stored in the call_context, so that future filters can 36 /// easily access method and global parameters for the call. 37 class ServiceConfigCallData { 38 public: ServiceConfigCallData(RefCountedPtr<ServiceConfig> service_config,const ServiceConfigParser::ParsedConfigVector * method_configs,std::map<const char *,absl::string_view> call_attributes,grpc_call_context_element * call_context)39 ServiceConfigCallData( 40 RefCountedPtr<ServiceConfig> service_config, 41 const ServiceConfigParser::ParsedConfigVector* method_configs, 42 std::map<const char*, absl::string_view> call_attributes, 43 grpc_call_context_element* call_context) 44 : service_config_(std::move(service_config)), 45 method_configs_(method_configs), 46 call_attributes_(std::move(call_attributes)) { 47 call_context[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].value = this; 48 call_context[GRPC_CONTEXT_SERVICE_CONFIG_CALL_DATA].destroy = Destroy; 49 } 50 ServiceConfigCallData(RefCountedPtr<ServiceConfig> service_config,const ServiceConfigParser::ParsedConfigVector * method_configs,grpc_call_context_element * call_context)51 ServiceConfigCallData( 52 RefCountedPtr<ServiceConfig> service_config, 53 const ServiceConfigParser::ParsedConfigVector* method_configs, 54 grpc_call_context_element* call_context) 55 : ServiceConfigCallData(std::move(service_config), method_configs, {}, 56 call_context) {} 57 service_config()58 ServiceConfig* service_config() { return service_config_.get(); } 59 GetMethodParsedConfig(size_t index)60 ServiceConfigParser::ParsedConfig* GetMethodParsedConfig(size_t index) const { 61 return method_configs_ != nullptr ? (*method_configs_)[index].get() 62 : nullptr; 63 } 64 GetGlobalParsedConfig(size_t index)65 ServiceConfigParser::ParsedConfig* GetGlobalParsedConfig(size_t index) const { 66 return service_config_->GetGlobalParsedConfig(index); 67 } 68 call_attributes()69 const std::map<const char*, absl::string_view>& call_attributes() const { 70 return call_attributes_; 71 } 72 73 private: Destroy(void * ptr)74 static void Destroy(void* ptr) { 75 ServiceConfigCallData* self = static_cast<ServiceConfigCallData*>(ptr); 76 self->~ServiceConfigCallData(); 77 } 78 79 RefCountedPtr<ServiceConfig> service_config_; 80 const ServiceConfigParser::ParsedConfigVector* method_configs_ = nullptr; 81 std::map<const char*, absl::string_view> call_attributes_; 82 }; 83 84 } // namespace grpc_core 85 86 #endif /* GRPC_CORE_EXT_FILTERS_CLIENT_CHANNEL_SERVICE_CONFIG_CALL_DATA_H */ 87