blob: 430f4526565579d7abe2c1a9fbc5cd236ec5fcce [file] [log] [blame] [view]
Viet-Trung Luuc7f9e252016-03-08 08:00:24 -08001**TODO(vtl)**: Reorganize this to be properly structured.
2
3# Mojom IDL
4
5The Mojom IDL (interface definition language) is primarily used to describe
6*interfaces* to be used on [message pipes](message_pipes.md). Below, we describe
7practical aspects of the Mojom language. Elsewhere, we describe the [Mojom
8protocol](mojom_protocol.md). (**TODO(vtl)**: Also, serialization format?
9Versioning?)
10
11Text files written in Mojom IDL are given the `.mojom` extension by convention
12(and are usually referred to as Mojom/mojom/`.mojom` files). The Mojom bindings
13generator (**TODO(vtl)**: link?) may be used to generate code in a variety of
14languages (including C++, Dart, and Go) from a Mojom file. Such generated code
15"implements" the things specified in the Mojom file, in a way that's appropriate
16for the particular target language.
17
18## Interfaces
19
20A Mojom *interface* is (typically) used to describe communication on a message
21pipe. Typically, message pipes are created with a particular interface in mind,
22with one endpoint designated the *client* (which sends *request* messages and
23receives *response* messages) and the other designed that *server* or *impl*
24(which receives request messages and sends response messages).
25
26For example, take the following Mojom interface declaration:
27```mojom
28interface MyInterface {
29 Foo(int32 a, string b);
30 Bar() => (bool x, uint32 y);
31 Baz() => ();
32};
33```
34This specifies a Mojom interface in which the client may send three types of
35messages, namely `Foo`, `Bar`, and `Baz` (see the note below about names in
36Mojom). The first does not have a response message defined, whereas the latter
37two do. Whenever the server receives a `Bar` or `Baz` message, it *must*
38(eventually) send a (single) corresponding response message.
39
40The `Foo` request message contains two pieces of data: a signed (two's
41complement) 32-bit integer called `a` and a Unicode string called `b`. On the
42"wire", the message basically consists of metadata and a (serialized) *struct*
43(see below) containing `a` and `b`.
44
45The `Bar` request message contains no data, so on the wire it's just metadata
46and an empty struct. It has a response message, containing a boolean value `x`
47and an unsigned 32-bit integer `y`, which on the wire consists of metadata and a
48struct with `x` and `y`. Each time the server receives a `Bar` message, it is
49supposed to (eventually) respond by sending the response message. (Note: The
50client may include as part of the request message's metadata an identifier for
51the request; the response's metadata will then include this identifier, allowing
52it to match responses to requests.)
53
54The `Baz` request message also contains no data. It requires a response, also
55containing no data. Note that even though the response has no data, a response
56message must nonetheless be sent, functioning as an "ack". (Thus this is
57different from not having a response, as was the case for `Foo`.)
58
59## Structs
60
61Mojom defines a way of serializing data structures (with the Mojom IDL providing
62a way of specifying those data structures). A Mojom *struct* is the basic unit
63of serialization. As we saw above, messages are basically just structs, with a
64small amount of additional metadata.
65
66Here is a simple example of a struct declaration:
67```mojom
68struct MyStruct {
69 int32 a;
70 string b;
71};
72```
73We will discuss in greater detail how structs are declared later.
74
75### Names in Mojom
76
77Names in Mojom are not important. Except in affecting compatibility at the level
78of source code (when generating bindings), names in a Mojom file may be changed
79arbitrarily without any effect on the "meaning" of the Mojom file (subject to
80basic language requirements, e.g., avoiding collisions with keywords and other
81names). E.g., the following is completely equivalent to the interface discussed
82above:
83```mojom
84interface Something {
85 One(int32 an_integer, string a_string);
86 Two() => (bool a_boolean, uint32 an_unsigned);
87 Three() => ();
88};
89```
90The `Something` interface is compatible at a binary level with `MyInterface`. A
91client using the `Something` interface may communicate with a server
92implementing the `MyInterface` with no issues, and vice versa.
93
94The reason for this is that elements (messages, parameters, struct members,
95etc.) are actually identified by *ordinal* value. They may be specified
96explicitly (using `@123` notation; see below). If they are not specified
97explicitly, they are automatically assigned. (The ordinal values for each
98interface/struct/etc. must assign distinct values for each item, in a
99consecutive range starting at 0.)
100
101Explicitly assigning ordinals allows Mojom files to be rearranged "physically"
102without changing their meaning. E.g., perhaps one would write:
103```mojom
104interface MyInterface {
105 Bar@1() => (bool x@0, uint32 y@1);
106 Baz@2() => ();
107
108 // Please don't use this in new code!
109 FooDeprecated@0(int32 a@0, string b@1);
110};
111```
112
113Ordinals also tie into the versioning scheme (**TODO(vtl)**: link?), which
114allows Mojom files to be evolved in a backwards-compatible way. We will not
115discuss this matter further here.
116
117**TODO(vtl)**: Maybe mention exceptions to this in attributes (e.g.,
118`ServiceName`).
119
120## Mojom files
121
122A Mojom file consists of, in order:
123* an optional *module* declaration;
124* zero or more *import* statements (the order of these is not important); and
125* zero or more declarations of *struct*s, *interface*s, *union*s, *enum*s, or
126 *const*s (the order of these is not important).
127(These are all described further below.)
128
129Additionally, C/C++-style comments are supported (i.e., single-line comments
130starting with `//` or multi-line comments of the form `/* ... */`).
131
132As stated above, the order of struct/interface/union/enum/const declarations is
133not important. This is required to allow "cyclic" structures to be defined.
134Nonetheless, whenever possible, one should declare things before they are
135"used". For example, the following is valid but not recommended:
136```mojom
137// NOT recommended.
138
139const MyEnum kMyConst = kMyOtherConst;
140const MyEnum kMyOtherConst = A_VALUE;
141
142enum MyEnum {
143 A_VALUE,
144 ANOTHER_VALUE,
145};
146```
147
148### Naming style
149
150There is a standard style for naming things:
151* `StudlyCaps` (a.k.a. `CapitalizedCamelCase`) for: (struct, interface, union,
152 and enum) type names and message (a.k.a. function or method) names;
153* `unix_hacker_style` for field names (in structs and unions) and "parameter"
154 names;
155* `ALL_CAPS_UNIX_HACKER_STYLE` for enum value names; and
156* `kStudlyCaps` for const names.
157
158Following this style is highly recommended, since code generators for various
159languages will expect this style, in order to transform the names into a more
160language-appropriate style.
161
162### Module statement
163
164The Mojom *module* statement is just a way of logically grouping Mojom
165declarations. For example:
166```mojom
167module my_module;
168```
169Mojom modules are similar to C++ namespaces (and the standard C++ code generator
170would put generated code into the `my_module` namespace), in that there is no
171implication that the file contains the entirety of the "module" definiton;
172multiple files may have the same module statement. (There is also no requirement
173that the module name have anything to do with the file path containing the Mojom
174file.)
175
176Mojom module names are hierarchical in that they can be composed of multiple
177parts separated by `.`. For example:
178```mojom
179module my_module.my_submodule;
180
181struct MyStruct {
182};
183```
184Name look-up is similar to C++: E.g., if the current module is
185`my_module.my_submodule` then `MyStruct`, `my_submodule.MyStruct`, and
186`my_module.my_submodule.MyStruct` all refer to the above struct, whereas if the
187current module is just `my_module` then only the latter two do.
188
189### Import declarations
190
191An *import* declaration makes the declarations from another Mojom file available
192in the current Mojom file. Moreover, it operates transitively, in the sense that
193it also makes the imports of the imported file available, etc. The "argument" to
194the import statement is a path to a file. Tools that work with Mojom files are
195typically provided with a search path for importing files (just as a C++
196compiler can be provided with an "include path"), for the purposes of resolving
197these paths. (**TODO(vtl)**: This always includes the current Mojom file's path,
198right? Is the current path the first path that's searched?)
199
200For example:
201```mojom
202module my_module;
203
204import "path/to/another.mojom";
205import "path/to/yet/a/different.mojom";
206```
207This makes the contents of the two mentioned Mojom files available, together
208with whatever they import, transitively. (Note that names are resolved in the
209way described in the previous section.)
210
211Import cycles are not permitted (so, e.g., it would be an error if
212`path/to/another.mojom` imported the current Mojom file). However, it is
213entirely valid for Mojom files to be imported (transitively) multiple times
214(e.g., it is fine for `path/to/another.mojom` to also import
215`path/to/yet/a/different.mojom`).
216
217### Struct declarations
218
219A Mojom *struct* declaration consists of a finite sequence of *field
220declaration*, each of which consists of a *type*, a *name*, and optionally a
221*default value* (if applicable for the given type). (If no default value is
222declared, then the default is the default value for the field type, typically 0,
223null, or similar.)
224
225Additionally, a struct may contain enum and const declarations (**TODO(vtl)**:
226why not struct/union/interface declarations?). While the order of the field
227declarations (with respect to one another) is important, the ordering of the
228enum/const declarations (with respect to both the field declarations and other
229enum/const declarations) is not. (But as before, we recommend declaring things
230before "use".)
231
232Here is an example with these elements:
233```mojom
234struct Foo {
235 const int8 kSomeConstant = 123;
236
237 enum MyEnum {
238 A_VALUE,
239 ANOTHER_VALUE
240 };
241
242 int8 first_field = kSomeConstant;
243 uint32 second_field = 123;
244 MyEnum etc_etc = A_VALUE;
245 float a; // Default value is 0.
246 string? b; // Default value is null.
247};
248```
249(Note that `kSomeConstant` may be referred to as `Foo.kSomeConstant` and,
250similarly, `MyEnum` as `Foo.MyEnum`. This is required outside of the `Foo`
251declaration.)
252
253### Interface declarations
254
255**TODO(vtl)**
256
257### Union declarations
258
259**TODO(vtl)**
260
261### Enum declarations
262
263**TODO(vtl)**
264
265### Const declarations
266
267**TODO(vtl)**
268
269**TODO(vtl)**: Write/(re)organize the sections below.
270
271## Data types
272
273### Primitive types
274
275#### Standard types
276
277#### Enum types
278
279### "Pointer" types
280
281#### Nullability
282
283#### Strings
284
285#### Maps
286
287#### Structs
288
289#### Arrays
290
291#### Unions
292
293### Handle types
294
295#### Raw handle types
296
297#### Interface types
298
299#### Interface request types
300
301## Annotations
302
303## Pipelining