ab56d6073fe42f8aacd779b1cd7aaaf109c98ca9
[platform/upstream/grpc.git] / src / cpp / common / channel_filter.cc
1 /*
2  *
3  * Copyright 2016 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 <string.h>
20
21 #include "src/core/lib/channel/channel_stack.h"
22 #include "src/cpp/common/channel_filter.h"
23
24 #include <grpcpp/impl/codegen/slice.h>
25
26 namespace grpc {
27
28 // MetadataBatch
29
30 grpc_linked_mdelem* MetadataBatch::AddMetadata(const string& key,
31                                                const string& value) {
32   grpc_linked_mdelem* storage = new grpc_linked_mdelem;
33   storage->md = grpc_mdelem_from_slices(SliceFromCopiedString(key),
34                                         SliceFromCopiedString(value));
35   GRPC_LOG_IF_ERROR("MetadataBatch::AddMetadata",
36                     grpc_metadata_batch_link_head(batch_, storage));
37   return storage;
38 }
39
40 // ChannelData
41
42 void ChannelData::StartTransportOp(grpc_channel_element* elem,
43                                    TransportOp* op) {
44   grpc_channel_next_op(elem, op->op());
45 }
46
47 void ChannelData::GetInfo(grpc_channel_element* elem,
48                           const grpc_channel_info* channel_info) {
49   grpc_channel_next_get_info(elem, channel_info);
50 }
51
52 // CallData
53
54 void CallData::StartTransportStreamOpBatch(grpc_call_element* elem,
55                                            TransportStreamOpBatch* op) {
56   grpc_call_next_op(elem, op->op());
57 }
58
59 void CallData::SetPollsetOrPollsetSet(grpc_call_element* elem,
60                                       grpc_polling_entity* pollent) {
61   grpc_call_stack_ignore_set_pollset_or_pollset_set(elem, pollent);
62 }
63
64 // internal code used by RegisterChannelFilter()
65
66 namespace internal {
67
68 // Note: Implicitly initialized to nullptr due to static lifetime.
69 std::vector<FilterRecord>* channel_filters;
70
71 namespace {
72
73 bool MaybeAddFilter(grpc_channel_stack_builder* builder, void* arg) {
74   const FilterRecord& filter = *static_cast<FilterRecord*>(arg);
75   if (filter.include_filter) {
76     const grpc_channel_args* args =
77         grpc_channel_stack_builder_get_channel_arguments(builder);
78     if (!filter.include_filter(*args)) return true;
79   }
80   return grpc_channel_stack_builder_prepend_filter(builder, &filter.filter,
81                                                    nullptr, nullptr);
82 }
83
84 }  // namespace
85
86 void ChannelFilterPluginInit() {
87   for (size_t i = 0; i < channel_filters->size(); ++i) {
88     FilterRecord& filter = (*channel_filters)[i];
89     grpc_channel_init_register_stage(filter.stack_type, filter.priority,
90                                      MaybeAddFilter, &filter);
91   }
92 }
93
94 void ChannelFilterPluginShutdown() {}
95
96 }  // namespace internal
97
98 }  // namespace grpc