OLD | NEW |
1 # Mojom IDL | 1 # Mojom IDL |
2 | 2 |
3 The Mojom IDL (interface definition language) is primarily used to describe | 3 The Mojom IDL (interface definition language) is primarily used to describe |
4 *interfaces* to be used on [message pipes](message_pipes.md). Below, we describe | 4 *interfaces* to be used on [message pipes](message_pipes.md). Below, we describe |
5 practical aspects of the Mojom language. Elsewhere, we describe the [Mojom | 5 practical aspects of the Mojom language. Elsewhere, we describe the [Mojom |
6 protocol](mojom_protocol.md). (**TODO(vtl)**: Also, serialization format? | 6 protocol](mojom_protocol.md). (**TODO(vtl)**: Also, serialization format? |
7 Versioning?) | 7 Versioning?) |
8 | 8 |
9 Text files written in Mojom IDL are given the `.mojom` extension by convention | 9 Text files written in Mojom IDL are given the `.mojom` extension by convention |
10 (and are usually referred to as Mojom/mojom/`.mojom` files). The Mojom bindings | 10 (and are usually referred to as Mojom/mojom/`.mojom` files). The Mojom bindings |
11 generator (**TODO(vtl)**: link?) may be used to generate code in a variety of | 11 generator (**TODO(vtl)**: link?) may be used to generate code in a variety of |
12 languages (including C++, Dart, and Go) from a Mojom file. Such generated code | 12 languages (including C++, Dart, and Go) from a Mojom file. Such generated code |
13 "implements" the things specified in the Mojom file, in a way that's appropriate | 13 "implements" the things specified in the Mojom file, in a way that's appropriate |
14 for the particular target language. | 14 for the particular target language. |
15 | 15 |
16 ## Interfaces | 16 ## Interfaces |
17 | 17 |
18 A Mojom *interface* is (typically) used to describe communication on a message | 18 A Mojom *interface* is (typically) used to describe communication on a message |
19 pipe. Typically, message pipes are created with a particular interface in mind, | 19 pipe. Typically, message pipes are created with a particular interface in mind, |
20 with one endpoint designated the *client* (which sends *request* messages and | 20 with one endpoint designated the *client* (which sends *request* messages and |
21 receives *response* messages) and the other designed that *server* or *impl* | 21 receives *response* messages) and the other designed that *server* or *impl* |
22 (which receives request messages and sends response messages). | 22 (which receives request messages and sends response messages). |
23 | 23 |
24 For example, take the following Mojom interface definition: | 24 For example, take the following Mojom interface declaration: |
25 ```mojom | 25 ```mojom |
26 interface MyInterface { | 26 interface MyInterface { |
27 Foo(int32 a, string b); | 27 Foo(int32 a, string b); |
28 Bar() => (bool x, uint32 y); | 28 Bar() => (bool x, uint32 y); |
29 Baz() => (); | 29 Baz() => (); |
30 }; | 30 }; |
31 ``` | 31 ``` |
32 This specifies a Mojom interface in which the client may send three types of | 32 This specifies a Mojom interface in which the client may send three types of |
33 messages, namely `Foo`, `Bar`, and `Baz` (see the note below about names in | 33 messages, namely `Foo`, `Bar`, and `Baz` (see the note below about names in |
34 Mojom). The first does not have a response message defined, whereas the latter | 34 Mojom). The first does not have a response message defined, whereas the latter |
(...skipping 12 matching lines...) Expand all Loading... |
47 supposed to (eventually) respond by sending the response message. (Note: The | 47 supposed to (eventually) respond by sending the response message. (Note: The |
48 client may include as part of the request message's metadata an identifier for | 48 client may include as part of the request message's metadata an identifier for |
49 the request; the response's metadata will then include this identifier, allowing | 49 the request; the response's metadata will then include this identifier, allowing |
50 it to match responses to requests.) | 50 it to match responses to requests.) |
51 | 51 |
52 The `Baz` request message also contains no data. It requires a response, also | 52 The `Baz` request message also contains no data. It requires a response, also |
53 containing no data. Note that even though the response has no data, a response | 53 containing no data. Note that even though the response has no data, a response |
54 message must nonetheless be sent, functioning as an "ack". (Thus this is | 54 message must nonetheless be sent, functioning as an "ack". (Thus this is |
55 different from not having a response, as was the case for `Foo`.) | 55 different from not having a response, as was the case for `Foo`.) |
56 | 56 |
| 57 ## Structs |
| 58 |
| 59 Mojom defines a way of serializing data structures (with the Mojom IDL providing |
| 60 a way of specifying those data structures). A Mojom *struct* is the basic unit |
| 61 of serialization. As we saw above, messages are basically just structs, with a |
| 62 small amount of additional metadata. |
| 63 |
| 64 Here is a simple example of a struct declaration: |
| 65 ```mojom |
| 66 struct MyStruct { |
| 67 int32 a; |
| 68 string b; |
| 69 }; |
| 70 ``` |
| 71 We will discuss in greater detail how structs are declared later. |
| 72 |
57 ### Names in Mojom | 73 ### Names in Mojom |
58 | 74 |
59 Names in Mojom are not important. Except in affecting compatibility at level of | 75 Names in Mojom are not important. Except in affecting compatibility at the level |
60 source code (when generating bindings), names in a Mojom file may be changed | 76 of source code (when generating bindings), names in a Mojom file may be changed |
61 arbitrarily without any effect on the "meaning" of the Mojom file (subject to | 77 arbitrarily without any effect on the "meaning" of the Mojom file (subject to |
62 basic language requirements, e.g., avoiding collisions with keywords and other | 78 basic language requirements, e.g., avoiding collisions with keywords and other |
63 names). E.g., the following is completely equivalent to the interface discussed | 79 names). E.g., the following is completely equivalent to the interface discussed |
64 above: | 80 above: |
65 ```mojom | 81 ```mojom |
66 interface Something { | 82 interface Something { |
67 One(int32 an_integer, string a_string); | 83 One(int32 an_integer, string a_string); |
68 Two() => (bool a_boolean, uint32 an_unsigned); | 84 Two() => (bool a_boolean, uint32 an_unsigned); |
69 Three() => (); | 85 Three() => (); |
70 }; | 86 }; |
(...skipping 21 matching lines...) Expand all Loading... |
92 }; | 108 }; |
93 ``` | 109 ``` |
94 | 110 |
95 Ordinals also tie into the versioning scheme (**TODO(vtl)**: link?), which | 111 Ordinals also tie into the versioning scheme (**TODO(vtl)**: link?), which |
96 allows Mojom files to be evolved in a backwards-compatible way. We will not | 112 allows Mojom files to be evolved in a backwards-compatible way. We will not |
97 discuss this matter further here. | 113 discuss this matter further here. |
98 | 114 |
99 **TODO(vtl)**: Maybe mention exceptions to this in attributes (e.g., | 115 **TODO(vtl)**: Maybe mention exceptions to this in attributes (e.g., |
100 `ServiceName`). | 116 `ServiceName`). |
101 | 117 |
| 118 ## Mojom files |
| 119 |
| 120 A Mojom file consists of, in order: |
| 121 * an optional *module* declaration; |
| 122 * zero or more *import* statements (the order of these is not important); and |
| 123 * zero or more declarations of *struct*s, *interface*s, *union*s, *enum*s, or |
| 124 *const*s (the order of these is not important). |
| 125 (These are all described further below.) |
| 126 |
| 127 Additionally, C/C++-style comments are supported (i.e., single-line comments |
| 128 starting with `//` or multi-line comments of the form `/* ... */`). |
| 129 |
| 130 As stated above, the order of struct/interface/union/enum/const declarations is |
| 131 not important. This is required to allow "cyclic" structures to be defined. |
| 132 Nonetheless, whenever possible, one should declare things before they are |
| 133 "used". For example, the following is valid but not recommended: |
| 134 ```mojom |
| 135 // NOT recommended. |
| 136 |
| 137 const MyEnum kMyConst = kMyOtherConst; |
| 138 const MyEnum kMyOtherConst = A_VALUE; |
| 139 |
| 140 enum MyEnum { |
| 141 A_VALUE, |
| 142 ANOTHER_VALUE, |
| 143 }; |
| 144 ``` |
| 145 |
| 146 ### Naming style |
| 147 |
| 148 There is a standard style for naming things: |
| 149 * `StudlyCaps` (a.k.a. `CapitalizedCamelCase`) for: (struct, interface, union, |
| 150 and enum) type names and message (a.k.a. function or method) names; |
| 151 * `unix_hacker_style` for field names (in structs and unions) and "parameter" |
| 152 names; |
| 153 * `ALL_CAPS_UNIX_HACKER_STYLE` for enum value names; and |
| 154 * `kStudlyCaps` for const names. |
| 155 |
| 156 Following this style is highly recommended, since code generators for various |
| 157 languages will expect this style, in order to transform the names into a more |
| 158 language-appropriate style. |
| 159 |
| 160 ### Module statement |
| 161 |
| 162 **TODO(vtl)** |
| 163 |
| 164 ### Import declarations |
| 165 |
| 166 **TODO(vtl)** |
| 167 |
| 168 ### Struct declarations |
| 169 |
| 170 A Mojom struct declaration consists of a finite sequence of *field declaration*, |
| 171 each of which consists of a *type*, a *name*, and optionally a *default value* |
| 172 (if applicable for the given type). (If no default value is declared, then the |
| 173 default is the default value for the field type, typically 0, null, or similar.) |
| 174 |
| 175 Additionally, a struct may contain enum and const declarations (**TODO(vtl)**: |
| 176 why not struct/union/interface declarations?). While the order of the field |
| 177 declarations (with respect to one another) is important, the ordering of the |
| 178 enum/const declarations (with respect to both the field declarations and other |
| 179 enum/const declarations) is not. (But as before, we recommend declaring things |
| 180 before "use".) |
| 181 |
| 182 Here is an example with these elements: |
| 183 ```mojom |
| 184 struct Foo { |
| 185 const int8 kSomeConstant = 123; |
| 186 |
| 187 enum MyEnum { |
| 188 A_VALUE, |
| 189 ANOTHER_VALUE |
| 190 }; |
| 191 |
| 192 int8 first_field = kSomeConstant; |
| 193 uint32 second_field = 123; |
| 194 MyEnum etc_etc = A_VALUE; |
| 195 float a; // Default value is 0. |
| 196 string? b; // Default value is null. |
| 197 }; |
| 198 ``` |
| 199 (Note that `kSomeConstant` may be referred to as `Foo.kSomeConstant` and, |
| 200 similarly, `MyEnum` as `Foo.MyEnum`. This is required outside of the `Foo` |
| 201 declaration.) |
| 202 |
| 203 ### Interface declarations |
| 204 |
| 205 **TODO(vtl)** |
| 206 |
| 207 ### Union declarations |
| 208 |
| 209 **TODO(vtl)** |
| 210 |
| 211 ### Enum declarations |
| 212 |
| 213 **TODO(vtl)** |
| 214 |
| 215 ### Const declarations |
| 216 |
| 217 **TODO(vtl)** |
102 | 218 |
103 **TODO(vtl)**: Write/(re)organize the sections below. | 219 **TODO(vtl)**: Write/(re)organize the sections below. |
104 | 220 |
105 ## Structs | |
106 | |
107 ## Modules | |
108 | |
109 ## Data types | 221 ## Data types |
110 | 222 |
111 ### Primitive types | 223 ### Primitive types |
112 | 224 |
113 ### Enums | 225 #### Standard types |
114 | 226 |
115 ### Strings | 227 #### Enum types |
116 | 228 |
117 ### Nullability | 229 ### "Pointer" types |
118 | 230 |
119 ### Structs | 231 #### Nullability |
120 | 232 |
121 ### Arrays | 233 #### Strings |
122 | 234 |
123 ### Maps | 235 #### Maps |
124 | 236 |
125 ### Unions | 237 #### Structs |
126 | 238 |
127 ### Handle values | 239 #### Arrays |
128 | 240 |
129 ### Interface values | 241 #### Unions |
130 | 242 |
131 ### Interface requests | 243 ### Handle types |
132 | 244 |
133 ## Consts | 245 #### Raw handle types |
| 246 |
| 247 #### Interface types |
| 248 |
| 249 #### Interface request types |
134 | 250 |
135 ## Annotations | 251 ## Annotations |
136 | 252 |
137 ## Pipelining | 253 ## Pipelining |
OLD | NEW |