Viet-Trung Luu | c7f9e25 | 2016-03-08 08:00:24 -0800 | [diff] [blame^] | 1 | **TODO(vtl)**: Reorganize this to be properly structured. |
| 2 | |
| 3 | # Mojom IDL |
| 4 | |
| 5 | The Mojom IDL (interface definition language) is primarily used to describe |
| 6 | *interfaces* to be used on [message pipes](message_pipes.md). Below, we describe |
| 7 | practical aspects of the Mojom language. Elsewhere, we describe the [Mojom |
| 8 | protocol](mojom_protocol.md). (**TODO(vtl)**: Also, serialization format? |
| 9 | Versioning?) |
| 10 | |
| 11 | Text 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 |
| 13 | generator (**TODO(vtl)**: link?) may be used to generate code in a variety of |
| 14 | languages (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 |
| 16 | for the particular target language. |
| 17 | |
| 18 | ## Interfaces |
| 19 | |
| 20 | A Mojom *interface* is (typically) used to describe communication on a message |
| 21 | pipe. Typically, message pipes are created with a particular interface in mind, |
| 22 | with one endpoint designated the *client* (which sends *request* messages and |
| 23 | receives *response* messages) and the other designed that *server* or *impl* |
| 24 | (which receives request messages and sends response messages). |
| 25 | |
| 26 | For example, take the following Mojom interface declaration: |
| 27 | ```mojom |
| 28 | interface MyInterface { |
| 29 | Foo(int32 a, string b); |
| 30 | Bar() => (bool x, uint32 y); |
| 31 | Baz() => (); |
| 32 | }; |
| 33 | ``` |
| 34 | This specifies a Mojom interface in which the client may send three types of |
| 35 | messages, namely `Foo`, `Bar`, and `Baz` (see the note below about names in |
| 36 | Mojom). The first does not have a response message defined, whereas the latter |
| 37 | two do. Whenever the server receives a `Bar` or `Baz` message, it *must* |
| 38 | (eventually) send a (single) corresponding response message. |
| 39 | |
| 40 | The `Foo` request message contains two pieces of data: a signed (two's |
| 41 | complement) 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 | |
| 45 | The `Bar` request message contains no data, so on the wire it's just metadata |
| 46 | and an empty struct. It has a response message, containing a boolean value `x` |
| 47 | and an unsigned 32-bit integer `y`, which on the wire consists of metadata and a |
| 48 | struct with `x` and `y`. Each time the server receives a `Bar` message, it is |
| 49 | supposed to (eventually) respond by sending the response message. (Note: The |
| 50 | client may include as part of the request message's metadata an identifier for |
| 51 | the request; the response's metadata will then include this identifier, allowing |
| 52 | it to match responses to requests.) |
| 53 | |
| 54 | The `Baz` request message also contains no data. It requires a response, also |
| 55 | containing no data. Note that even though the response has no data, a response |
| 56 | message must nonetheless be sent, functioning as an "ack". (Thus this is |
| 57 | different from not having a response, as was the case for `Foo`.) |
| 58 | |
| 59 | ## Structs |
| 60 | |
| 61 | Mojom defines a way of serializing data structures (with the Mojom IDL providing |
| 62 | a way of specifying those data structures). A Mojom *struct* is the basic unit |
| 63 | of serialization. As we saw above, messages are basically just structs, with a |
| 64 | small amount of additional metadata. |
| 65 | |
| 66 | Here is a simple example of a struct declaration: |
| 67 | ```mojom |
| 68 | struct MyStruct { |
| 69 | int32 a; |
| 70 | string b; |
| 71 | }; |
| 72 | ``` |
| 73 | We will discuss in greater detail how structs are declared later. |
| 74 | |
| 75 | ### Names in Mojom |
| 76 | |
| 77 | Names in Mojom are not important. Except in affecting compatibility at the level |
| 78 | of source code (when generating bindings), names in a Mojom file may be changed |
| 79 | arbitrarily without any effect on the "meaning" of the Mojom file (subject to |
| 80 | basic language requirements, e.g., avoiding collisions with keywords and other |
| 81 | names). E.g., the following is completely equivalent to the interface discussed |
| 82 | above: |
| 83 | ```mojom |
| 84 | interface Something { |
| 85 | One(int32 an_integer, string a_string); |
| 86 | Two() => (bool a_boolean, uint32 an_unsigned); |
| 87 | Three() => (); |
| 88 | }; |
| 89 | ``` |
| 90 | The `Something` interface is compatible at a binary level with `MyInterface`. A |
| 91 | client using the `Something` interface may communicate with a server |
| 92 | implementing the `MyInterface` with no issues, and vice versa. |
| 93 | |
| 94 | The reason for this is that elements (messages, parameters, struct members, |
| 95 | etc.) are actually identified by *ordinal* value. They may be specified |
| 96 | explicitly (using `@123` notation; see below). If they are not specified |
| 97 | explicitly, they are automatically assigned. (The ordinal values for each |
| 98 | interface/struct/etc. must assign distinct values for each item, in a |
| 99 | consecutive range starting at 0.) |
| 100 | |
| 101 | Explicitly assigning ordinals allows Mojom files to be rearranged "physically" |
| 102 | without changing their meaning. E.g., perhaps one would write: |
| 103 | ```mojom |
| 104 | interface 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 | |
| 113 | Ordinals also tie into the versioning scheme (**TODO(vtl)**: link?), which |
| 114 | allows Mojom files to be evolved in a backwards-compatible way. We will not |
| 115 | discuss this matter further here. |
| 116 | |
| 117 | **TODO(vtl)**: Maybe mention exceptions to this in attributes (e.g., |
| 118 | `ServiceName`). |
| 119 | |
| 120 | ## Mojom files |
| 121 | |
| 122 | A 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 | |
| 129 | Additionally, C/C++-style comments are supported (i.e., single-line comments |
| 130 | starting with `//` or multi-line comments of the form `/* ... */`). |
| 131 | |
| 132 | As stated above, the order of struct/interface/union/enum/const declarations is |
| 133 | not important. This is required to allow "cyclic" structures to be defined. |
| 134 | Nonetheless, 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 | |
| 139 | const MyEnum kMyConst = kMyOtherConst; |
| 140 | const MyEnum kMyOtherConst = A_VALUE; |
| 141 | |
| 142 | enum MyEnum { |
| 143 | A_VALUE, |
| 144 | ANOTHER_VALUE, |
| 145 | }; |
| 146 | ``` |
| 147 | |
| 148 | ### Naming style |
| 149 | |
| 150 | There 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 | |
| 158 | Following this style is highly recommended, since code generators for various |
| 159 | languages will expect this style, in order to transform the names into a more |
| 160 | language-appropriate style. |
| 161 | |
| 162 | ### Module statement |
| 163 | |
| 164 | The Mojom *module* statement is just a way of logically grouping Mojom |
| 165 | declarations. For example: |
| 166 | ```mojom |
| 167 | module my_module; |
| 168 | ``` |
| 169 | Mojom modules are similar to C++ namespaces (and the standard C++ code generator |
| 170 | would put generated code into the `my_module` namespace), in that there is no |
| 171 | implication that the file contains the entirety of the "module" definiton; |
| 172 | multiple files may have the same module statement. (There is also no requirement |
| 173 | that the module name have anything to do with the file path containing the Mojom |
| 174 | file.) |
| 175 | |
| 176 | Mojom module names are hierarchical in that they can be composed of multiple |
| 177 | parts separated by `.`. For example: |
| 178 | ```mojom |
| 179 | module my_module.my_submodule; |
| 180 | |
| 181 | struct MyStruct { |
| 182 | }; |
| 183 | ``` |
| 184 | Name 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 |
| 187 | current module is just `my_module` then only the latter two do. |
| 188 | |
| 189 | ### Import declarations |
| 190 | |
| 191 | An *import* declaration makes the declarations from another Mojom file available |
| 192 | in the current Mojom file. Moreover, it operates transitively, in the sense that |
| 193 | it also makes the imports of the imported file available, etc. The "argument" to |
| 194 | the import statement is a path to a file. Tools that work with Mojom files are |
| 195 | typically provided with a search path for importing files (just as a C++ |
| 196 | compiler can be provided with an "include path"), for the purposes of resolving |
| 197 | these paths. (**TODO(vtl)**: This always includes the current Mojom file's path, |
| 198 | right? Is the current path the first path that's searched?) |
| 199 | |
| 200 | For example: |
| 201 | ```mojom |
| 202 | module my_module; |
| 203 | |
| 204 | import "path/to/another.mojom"; |
| 205 | import "path/to/yet/a/different.mojom"; |
| 206 | ``` |
| 207 | This makes the contents of the two mentioned Mojom files available, together |
| 208 | with whatever they import, transitively. (Note that names are resolved in the |
| 209 | way described in the previous section.) |
| 210 | |
| 211 | Import 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 |
| 213 | entirely 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 | |
| 219 | A Mojom *struct* declaration consists of a finite sequence of *field |
| 220 | declaration*, 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 |
| 222 | declared, then the default is the default value for the field type, typically 0, |
| 223 | null, or similar.) |
| 224 | |
| 225 | Additionally, a struct may contain enum and const declarations (**TODO(vtl)**: |
| 226 | why not struct/union/interface declarations?). While the order of the field |
| 227 | declarations (with respect to one another) is important, the ordering of the |
| 228 | enum/const declarations (with respect to both the field declarations and other |
| 229 | enum/const declarations) is not. (But as before, we recommend declaring things |
| 230 | before "use".) |
| 231 | |
| 232 | Here is an example with these elements: |
| 233 | ```mojom |
| 234 | struct 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, |
| 250 | similarly, `MyEnum` as `Foo.MyEnum`. This is required outside of the `Foo` |
| 251 | declaration.) |
| 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 |