OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This file contains structs used for constructing type descriptors for | |
6 // generated mojom types. A type descriptor for a mojom struct is a table | |
7 // describing the byte-offsets of all the pointers and handles in the struct, | |
8 // and has references to the type descriptorss that further describe the | |
9 // pointers. The table is used for doing all computations for the struct -- | |
10 // determining serialized size, encoding and decoding recursively, etc. A type | |
11 // descriptor is generated for each struct, union, array and map. Mojom maps are | |
12 // just mojom structs with two mojom arrays. We denote it as a separate type, | |
13 // but it is described the same way as a mojom struct. | |
14 // | |
15 // The user is not expected to construct type descriptors -- a bindings | |
16 // generator will do this when it generates bindings for a mojom file. | |
17 // | |
18 // A type descriptor for a mojom struct is a |MojomTypeDescriptorStruct| | |
19 // containing an array of entries of types of |MojomTypeDescriptorStructEntry|. | |
20 // Similarly, unions are described with |MojomTypeDescriptorUnion| with entries | |
21 // of |MojomTypeDescriptorUnionEntry|. Arrays are described with | |
22 // |MojomTypeDescriptorArray|. | |
23 | |
24 #ifndef MOJO_PUBLIC_C_INCLUDE_MOJO_BINDINGS_INTERNAL_TYPE_DESCRIPTOR_H_ | |
25 #define MOJO_PUBLIC_C_INCLUDE_MOJO_BINDINGS_INTERNAL_TYPE_DESCRIPTOR_H_ | |
26 | |
27 #include <mojo/bindings/buffer.h> | |
28 #include <mojo/bindings/internal/util.h> | |
29 #include <mojo/bindings/validation.h> | |
30 #include <mojo/macros.h> | |
31 #include <mojo/system/handle.h> | |
32 #include <stdbool.h> | |
33 #include <stddef.h> | |
34 #include <stdint.h> | |
35 | |
36 MOJO_BEGIN_EXTERN_C | |
37 | |
38 // This enum is used in a type descriptor entry for the |elem_type| field, and | |
39 // indicates which type the accompanying |elem_descriptor| is describing (if it | |
40 // is describing a reference type), or to indicate that it's a handle type. | |
41 // Values that correspond to an |elem_descriptor|'s pointer type: | |
42 // - If MOJOM_TYPE_DESCRIPTOR_TYPE_STRUCT_PTR, then |elem_descriptor| points to | |
43 // a |MojomTypeDescriptorStruct|. | |
44 // - If MOJOM_TYPE_DESCRIPTOR_TYPE_MAP_PTR, then |elem_descriptor| points to | |
45 // a |MojomTypeDescriptorStruct|. | |
46 // - If MOJOM_TYPE_DESCRIPTOR_TYPE_UNION or | |
47 // MOJOM_TYPE_DESCRIPTOR_TYPE_UNION_PTR, then |elem_descriptor| points to a | |
48 // |MojomTypeDescriptorUnion|. | |
49 // - If MOJOM_TYPE_DESCRIPTOR_TYPE_ARRAY_PTR, then |elem_descriptor| points to a | |
50 // |MojomTypeDescriptorArray|. | |
51 // - For any other value, |elem_descriptor| is NULL. | |
52 enum MojomTypeDescriptorType { | |
53 // Note: A map is a mojom struct with 2 mojom arrays, so we don't have a | |
54 // separate descriptor type for it. | |
55 MOJOM_TYPE_DESCRIPTOR_TYPE_STRUCT_PTR = 0, | |
56 MOJOM_TYPE_DESCRIPTOR_TYPE_MAP_PTR = 1, | |
57 MOJOM_TYPE_DESCRIPTOR_TYPE_ARRAY_PTR = 2, | |
58 // A MOJOM_TYPE_DESCRIPTOR_TYPE_UNION_PTR only occurs inside a | |
59 // |MojomTypeDescriptorUnion|, since union fields inside unions are encoded as | |
60 // pointers to an out-of-line union. | |
61 MOJOM_TYPE_DESCRIPTOR_TYPE_UNION_PTR = 3, | |
62 // A union that is not inside a union is inlined, and described by | |
63 // MOJOM_TYPE_DESCRIPTOR_TYPE_UNION. | |
64 MOJOM_TYPE_DESCRIPTOR_TYPE_UNION = 4, | |
65 MOJOM_TYPE_DESCRIPTOR_TYPE_HANDLE = 5, | |
66 MOJOM_TYPE_DESCRIPTOR_TYPE_INTERFACE = 6, | |
67 // This is only used in an array descriptor, and serves as a way to terminate | |
68 // a chain of array descriptors; the last entry in the chain always contains a | |
69 // plain-old-data type. | |
70 MOJOM_TYPE_DESCRIPTOR_TYPE_POD = 7, | |
71 }; | |
72 | |
73 struct MojomTypeDescriptorStructVersion { | |
74 uint32_t version; | |
75 uint32_t num_bytes; | |
76 }; | |
77 | |
78 // Mojom structs are described using this struct. | |
79 struct MojomTypeDescriptorStruct { | |
80 uint32_t num_versions; | |
81 // Increasing ordered by version. | |
82 struct MojomTypeDescriptorStructVersion* versions; | |
83 // |entries| is an array of |num_entries|, each describing a field of | |
84 // reference or handle type. | |
85 uint32_t num_entries; | |
86 const struct MojomTypeDescriptorStructEntry* entries; | |
87 }; | |
88 | |
89 // This struct is used to describe each entry in a mojom struct. Each entry | |
90 // indicates whether it is describing a reference type, or a handle type through | |
91 // the field |elem_type|. |elem_descriptor| points to the type descriptor | |
92 // describing the field, if the type has one (handles don't have type | |
93 // descriptors). |offset| indicates the starting byte offset of the element | |
94 // within the struct. | |
95 struct MojomTypeDescriptorStructEntry { | |
96 // See comments for |MojomTypeDescriptorType| on possible values and | |
97 // corresponding behaviour with |elem_descriptor|. | |
98 enum MojomTypeDescriptorType elem_type; | |
99 // Use |elem_type| to decide which type |elem_descriptor| should be casted to. | |
100 const void* elem_descriptor; | |
101 // |offset| does not account for the struct header. Offset 0 always refers to | |
102 // the first element. | |
103 uint32_t offset; | |
104 // Corresponds to the '[MinVersion]' attribute in mojom IDL. This determines | |
105 // if this field should be ignored if its min_version < version of the struct | |
106 // we are dealing with. | |
107 uint32_t min_version; | |
108 // Is this field nullable? | |
109 bool nullable; | |
110 }; | |
111 | |
112 // Mojom unions are described using this struct. | |
113 struct MojomTypeDescriptorUnion { | |
114 // Number of fields in the union. | |
115 uint32_t num_fields; | |
116 // Number of elements in the |entries| array below. | |
117 uint32_t num_entries; | |
118 // |entries| only includes entries for pointer and handle types. | |
119 const struct MojomTypeDescriptorUnionEntry* entries; | |
120 }; | |
121 | |
122 // Like |MojomTypeDescriptorStructEntry|, this variant is used to construct a | |
123 // type descriptor for a union. Instead of an offset, it describes a union field | |
124 // by its |tag|. | |
125 struct MojomTypeDescriptorUnionEntry { | |
126 // See comments for |MojomTypeDescriptorType| on possible values and | |
127 // corresponding behaviour with |elem_descriptor|. | |
128 enum MojomTypeDescriptorType elem_type; | |
129 const void* elem_descriptor; | |
130 // The tag of the union field. | |
131 uint32_t tag; | |
132 bool nullable; | |
133 }; | |
134 | |
135 // Describes a mojom array. To describe an array, we don't need a table of | |
136 // entries, since arrays can only describe 1 type. However, that one type can | |
137 // recursively be an array (e.g, array<array<int>>), in which case a chain of | |
138 // array entries are constructed. | |
139 struct MojomTypeDescriptorArray { | |
140 enum MojomTypeDescriptorType elem_type; | |
141 const void* elem_descriptor; | |
142 // How many elements is this array expected to hold? | |
143 // 0 means unspecified. | |
144 uint32_t num_elements; | |
145 // Size of each element, in bits (since bools are 1-bit in size). This is | |
146 // needed to validate the size of an array. | |
147 uint32_t elem_num_bits; | |
148 bool nullable; | |
149 }; | |
150 | |
151 // This describes a mojom string. | |
152 // A mojom string is a mojom array of chars without a fixed-sized. | |
153 extern const struct MojomTypeDescriptorArray g_mojom_string_type_description; | |
154 | |
155 // Returns true if |type| is a "pointer" type: an array or a struct, whose data | |
156 // is referenced by a pointer (or an offset, if serialized). | |
157 // Since unions being pointer types depends on their container, we don't include | |
158 // them here. | |
159 bool MojomType_IsPointer(enum MojomTypeDescriptorType type); | |
160 | |
161 // This helper function, depending on |type|, calls the appropriate | |
162 // *_ComputeSerializedSize(|type_desc|, |data|). | |
163 size_t MojomType_DispatchComputeSerializedSize( | |
164 enum MojomTypeDescriptorType type, | |
165 const void* type_desc, | |
166 bool nullable, | |
167 const void* data); | |
168 | |
169 // This helper function, depending on |in_elem_type|, calls the appropriate | |
170 // *_EncodePointersAndHandles(...). If |in_elem_type| describes a pointer, it | |
171 // first encodes the pointer before calling the associated | |
172 // *_EncodePointersAndHandles(...). If |in_elem_type| describes a handle, it | |
173 // encodes the handle into |inout_handles|. | |
174 void MojomType_DispatchEncodePointersAndHandles( | |
175 enum MojomTypeDescriptorType in_elem_type, | |
176 const void* in_type_desc, | |
177 bool in_nullable, | |
178 void* inout_buf, | |
179 uint32_t in_buf_size, | |
180 struct MojomHandleBuffer* inout_handles_buffer); | |
181 | |
182 // This helper function, depending on |in_elem_type|, calls the appropriate | |
183 // *_DecodePointersAndHandles(...). If |in_elem_type| describes a pointer, it | |
184 // first decodes the offset into a pointer before calling the associated | |
185 // *_DecodePointersAndHandles(...). If |in_elem_type| describes a handle, it | |
186 // decodes the handle by looking up it up in |inout_handles|. | |
187 void MojomType_DispatchDecodePointersAndHandles( | |
188 enum MojomTypeDescriptorType in_elem_type, | |
189 const void* in_type_desc, | |
190 bool in_nullable, | |
191 void* inout_buf, | |
192 uint32_t in_buf_size, | |
193 MojoHandle* inout_handles, | |
194 uint32_t in_num_handles); | |
195 | |
196 MojomValidationResult MojomType_DispatchValidate( | |
197 enum MojomTypeDescriptorType in_elem_type, | |
198 const void* in_type_desc, | |
199 bool in_nullable, | |
200 const void* in_buf, | |
201 uint32_t in_buf_size, | |
202 uint32_t in_num_handles, | |
203 struct MojomValidationContext* inout_context); | |
204 | |
205 // This helper function, depending on |in_elem_type|, calls the appropriate | |
206 // *_DeepCopy(...). The result of that call is then assigned to |out_data|. If | |
207 // |in_type_type| describes a pointer to a union, it allocates space for the | |
208 // new union before dispatching a call to MojomUnion_DeepCopy. Returns false if | |
209 // the copy failed due to insufficient space in the buffer. | |
210 bool MojomType_DispatchDeepCopy(struct MojomBuffer* buffer, | |
211 enum MojomTypeDescriptorType in_elem_type, | |
212 const void* in_type_desc, | |
213 const void* in_data, | |
214 void* out_data); | |
215 | |
216 MOJO_END_EXTERN_C | |
217 | |
218 #endif // MOJO_PUBLIC_C_INCLUDE_MOJO_BINDINGS_INTERNAL_TYPE_DESCRIPTOR_H_ | |
OLD | NEW |