1 /*
2  *
3  * Copyright 2015 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 #ifndef GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H
20 #define GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <string>
25 
26 #include <grpc/grpc.h>
27 
28 #include "src/core/lib/surface/channel_stack_type.h"
29 
30 // Channel args are intentionally immutable, to avoid the need for locking.
31 
32 /** Copy the arguments in \a src into a new instance */
33 grpc_channel_args* grpc_channel_args_copy(const grpc_channel_args* src);
34 
35 /** Copy the arguments in \a src into a new instance, stably sorting keys */
36 grpc_channel_args* grpc_channel_args_normalize(const grpc_channel_args* src);
37 
38 /** Copy the arguments in \a src and append \a to_add. If \a to_add is NULL, it
39  * is equivalent to calling \a grpc_channel_args_copy. */
40 grpc_channel_args* grpc_channel_args_copy_and_add(const grpc_channel_args* src,
41                                                   const grpc_arg* to_add,
copy_arg(const grpc_arg * src)42                                                   size_t num_to_add);
43 
44 /** Remove any channel args prefixed with 'grpc.internal.'
45  *  These are used for internal implementation details and are not intended to
46  *  be exposed to users.
47  *  Returns a new channel args instance.
48  *  Does not take ownership of \a src.
49  *  Should be called by any public API that receives channel args. */
50 grpc_channel_args* grpc_channel_args_remove_grpc_internal(
51     const grpc_channel_args* src);
52 
53 /** Copies the arguments in \a src except for those whose keys are in
54     \a to_remove. */
55 grpc_channel_args* grpc_channel_args_copy_and_remove(
56     const grpc_channel_args* src, const char** to_remove, size_t num_to_remove);
57 
58 /** Copies the arguments from \a src except for those whose keys are in
59     \a to_remove and appends the arguments in \a to_add. */
60 grpc_channel_args* grpc_channel_args_copy_and_add_and_remove(
61     const grpc_channel_args* src, const char** to_remove, size_t num_to_remove,
grpc_channel_args_copy_and_add(const grpc_channel_args * src,const grpc_arg * to_add,size_t num_to_add)62     const grpc_arg* to_add, size_t num_to_add);
63 
64 /** Perform the union of \a a and \a b, prioritizing \a a entries */
65 grpc_channel_args* grpc_channel_args_union(const grpc_channel_args* a,
66                                            const grpc_channel_args* b);
67 
68 /** Destroy arguments created by \a grpc_channel_args_copy */
grpc_channel_args_copy_and_remove(const grpc_channel_args * src,const char ** to_remove,size_t num_to_remove)69 void grpc_channel_args_destroy(grpc_channel_args* a);
70 inline void grpc_channel_args_destroy(const grpc_channel_args* a) {
71   grpc_channel_args_destroy(const_cast<grpc_channel_args*>(a));
72 }
73 
74 int grpc_channel_args_compare(const grpc_channel_args* a,
75                               const grpc_channel_args* b);
grpc_channel_args_remove_grpc_internal(const grpc_channel_args * src)76 
77 /** Returns the value of argument \a name from \a args, or NULL if not found. */
78 const grpc_arg* grpc_channel_args_find(const grpc_channel_args* args,
79                                        const char* name);
80 
81 bool grpc_channel_args_want_minimal_stack(const grpc_channel_args* args);
82 
83 typedef struct grpc_integer_options {
84   int default_value;  // Return this if value is outside of expected bounds.
85   int min_value;
86   int max_value;
87 } grpc_integer_options;
88 
89 /** Returns the value of \a arg, subject to the constraints in \a options. */
90 int grpc_channel_arg_get_integer(const grpc_arg* arg,
91                                  const grpc_integer_options options);
should_remove_arg(const grpc_arg * arg,const char ** to_remove,size_t num_to_remove)92 /** Similar to the above, but needs to find the arg from \a args by the name
93  * first. */
94 int grpc_channel_args_find_integer(const grpc_channel_args* args,
95                                    const char* name,
96                                    const grpc_integer_options options);
97 
98 /** Returns the value of \a arg if \a arg is of type GRPC_ARG_STRING.
99     Otherwise, emits a warning log, and returns nullptr.
grpc_channel_args_copy_and_add_and_remove(const grpc_channel_args * src,const char ** to_remove,size_t num_to_remove,const grpc_arg * to_add,size_t num_to_add)100     If arg is nullptr, returns nullptr, and does not emit a warning. */
101 char* grpc_channel_arg_get_string(const grpc_arg* arg);
102 /** Similar to the above, but needs to find the arg from \a args by the name
103  * first. */
104 char* grpc_channel_args_find_string(const grpc_channel_args* args,
105                                     const char* name);
106 /** If \a arg is of type GRPC_ARG_INTEGER, returns true if it's non-zero.
107  * Returns \a default_value if \a arg is of other types. */
108 bool grpc_channel_arg_get_bool(const grpc_arg* arg, bool default_value);
109 /** Similar to the above, but needs to find the arg from \a args by the name
110  * first. */
111 bool grpc_channel_args_find_bool(const grpc_channel_args* args,
112                                  const char* name, bool default_value);
113 
114 template <typename T>
115 T* grpc_channel_args_find_pointer(const grpc_channel_args* args,
116                                   const char* name) {
117   const grpc_arg* arg = grpc_channel_args_find(args, name);
118   if (arg == nullptr || arg->type != GRPC_ARG_POINTER) return nullptr;
119   return static_cast<T*>(arg->value.pointer.p);
120 }
121 
122 // Helpers for creating channel args.
123 grpc_arg grpc_channel_arg_string_create(char* name, char* value);
124 grpc_arg grpc_channel_arg_integer_create(char* name, int value);
125 grpc_arg grpc_channel_arg_pointer_create(char* name, void* value,
126                                          const grpc_arg_pointer_vtable* vtable);
127 
128 // Returns a string representing channel args in human-readable form.
129 std::string grpc_channel_args_string(const grpc_channel_args* args);
130 
131 // Takes ownership of the old_args
132 typedef grpc_channel_args* (*grpc_channel_args_client_channel_creation_mutator)(
133     const char* target, grpc_channel_args* old_args,
134     grpc_channel_stack_type type);
135 
136 // Should be called only once globaly before grpc is init'ed.
137 void grpc_channel_args_set_client_channel_creation_mutator(
138     grpc_channel_args_client_channel_creation_mutator cb);
grpc_channel_args_copy(const grpc_channel_args * src)139 // This will be called at the creation of each channel.
140 grpc_channel_args_client_channel_creation_mutator
141 grpc_channel_args_get_client_channel_creation_mutator();
142 
grpc_channel_args_union(const grpc_channel_args * a,const grpc_channel_args * b)143 #endif /* GRPC_CORE_LIB_CHANNEL_CHANNEL_ARGS_H */
144