1*635a8641SAndroid Build Coastguard Worker# Mojom IDL and Bindings Generator 2*635a8641SAndroid Build Coastguard WorkerThis document is a subset of the [Mojo documentation](/mojo/README.md). 3*635a8641SAndroid Build Coastguard Worker 4*635a8641SAndroid Build Coastguard Worker[TOC] 5*635a8641SAndroid Build Coastguard Worker 6*635a8641SAndroid Build Coastguard Worker## Overview 7*635a8641SAndroid Build Coastguard Worker 8*635a8641SAndroid Build Coastguard WorkerMojom is the IDL for Mojo bindings interfaces. Given a `.mojom` file, the 9*635a8641SAndroid Build Coastguard Worker[bindings 10*635a8641SAndroid Build Coastguard Workergenerator](https://cs.chromium.org/chromium/src/mojo/public/tools/bindings/) 11*635a8641SAndroid Build Coastguard Workeroutputs bindings for all supported languages: **C++**, **JavaScript**, and 12*635a8641SAndroid Build Coastguard Worker**Java**. 13*635a8641SAndroid Build Coastguard Worker 14*635a8641SAndroid Build Coastguard WorkerFor a trivial example consider the following hypothetical Mojom file we write to 15*635a8641SAndroid Build Coastguard Worker`//services/widget/public/interfaces/frobinator.mojom`: 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard Worker``` 18*635a8641SAndroid Build Coastguard Workermodule widget.mojom; 19*635a8641SAndroid Build Coastguard Worker 20*635a8641SAndroid Build Coastguard Workerinterface Frobinator { 21*635a8641SAndroid Build Coastguard Worker Frobinate(); 22*635a8641SAndroid Build Coastguard Worker}; 23*635a8641SAndroid Build Coastguard Worker``` 24*635a8641SAndroid Build Coastguard Worker 25*635a8641SAndroid Build Coastguard WorkerThis defines a single [interface](#Interfaces) named `Frobinator` in a 26*635a8641SAndroid Build Coastguard Worker[module](#Modules) named `widget.mojom` (and thus fully qualified in Mojom as 27*635a8641SAndroid Build Coastguard Worker`widget.mojom.Frobinator`.) Note that many interfaces and/or other types of 28*635a8641SAndroid Build Coastguard Workerdefinitions may be included in a single Mojom file. 29*635a8641SAndroid Build Coastguard Worker 30*635a8641SAndroid Build Coastguard WorkerIf we add a corresponding GN target to 31*635a8641SAndroid Build Coastguard Worker`//services/widget/public/interfaces/BUILD.gn`: 32*635a8641SAndroid Build Coastguard Worker 33*635a8641SAndroid Build Coastguard Worker``` 34*635a8641SAndroid Build Coastguard Workerimport("mojo/public/tools/bindings/mojom.gni") 35*635a8641SAndroid Build Coastguard Worker 36*635a8641SAndroid Build Coastguard Workermojom("interfaces") { 37*635a8641SAndroid Build Coastguard Worker sources = [ 38*635a8641SAndroid Build Coastguard Worker "frobinator.mojom", 39*635a8641SAndroid Build Coastguard Worker ] 40*635a8641SAndroid Build Coastguard Worker} 41*635a8641SAndroid Build Coastguard Worker``` 42*635a8641SAndroid Build Coastguard Worker 43*635a8641SAndroid Build Coastguard Workerand then build this target: 44*635a8641SAndroid Build Coastguard Worker 45*635a8641SAndroid Build Coastguard Worker``` 46*635a8641SAndroid Build Coastguard Workerninja -C out/r services/widget/public/interfaces 47*635a8641SAndroid Build Coastguard Worker``` 48*635a8641SAndroid Build Coastguard Worker 49*635a8641SAndroid Build Coastguard Workerwe'll find several generated sources in our output directory: 50*635a8641SAndroid Build Coastguard Worker 51*635a8641SAndroid Build Coastguard Worker``` 52*635a8641SAndroid Build Coastguard Workerout/r/gen/services/widget/public/interfaces/frobinator.mojom.cc 53*635a8641SAndroid Build Coastguard Workerout/r/gen/services/widget/public/interfaces/frobinator.mojom.h 54*635a8641SAndroid Build Coastguard Workerout/r/gen/services/widget/public/interfaces/frobinator.mojom.js 55*635a8641SAndroid Build Coastguard Workerout/r/gen/services/widget/public/interfaces/frobinator.mojom.srcjar 56*635a8641SAndroid Build Coastguard Worker... 57*635a8641SAndroid Build Coastguard Worker``` 58*635a8641SAndroid Build Coastguard Worker 59*635a8641SAndroid Build Coastguard WorkerEach of these generated source modules includes a set of definitions 60*635a8641SAndroid Build Coastguard Workerrepresenting the Mojom contents within the target language. For more details 61*635a8641SAndroid Build Coastguard Workerregarding the generated outputs please see 62*635a8641SAndroid Build Coastguard Worker[documentation for individual target languages](#Generated-Code-For-Target-Languages). 63*635a8641SAndroid Build Coastguard Worker 64*635a8641SAndroid Build Coastguard Worker## Mojom Syntax 65*635a8641SAndroid Build Coastguard Worker 66*635a8641SAndroid Build Coastguard WorkerMojom IDL allows developers to define **structs**, **unions**, **interfaces**, 67*635a8641SAndroid Build Coastguard Worker**constants**, and **enums**, all within the context of a **module**. These 68*635a8641SAndroid Build Coastguard Workerdefinitions are used to generate code in the supported target languages at build 69*635a8641SAndroid Build Coastguard Workertime. 70*635a8641SAndroid Build Coastguard Worker 71*635a8641SAndroid Build Coastguard WorkerMojom files may **import** other Mojom files in order to reference their 72*635a8641SAndroid Build Coastguard Workerdefinitions. 73*635a8641SAndroid Build Coastguard Worker 74*635a8641SAndroid Build Coastguard Worker### Primitive Types 75*635a8641SAndroid Build Coastguard WorkerMojom supports a few basic data types which may be composed into structs or used 76*635a8641SAndroid Build Coastguard Workerfor message parameters. 77*635a8641SAndroid Build Coastguard Worker 78*635a8641SAndroid Build Coastguard Worker| Type | Description 79*635a8641SAndroid Build Coastguard Worker|-------------------------------|-------------------------------------------------------| 80*635a8641SAndroid Build Coastguard Worker| `bool` | Boolean type (`true` or `false`.) 81*635a8641SAndroid Build Coastguard Worker| `int8`, `uint8` | Signed or unsigned 8-bit integer. 82*635a8641SAndroid Build Coastguard Worker| `int16`, `uint16` | Signed or unsigned 16-bit integer. 83*635a8641SAndroid Build Coastguard Worker| `int32`, `uint32` | Signed or unsigned 32-bit integer. 84*635a8641SAndroid Build Coastguard Worker| `int64`, `uint64` | Signed or unsigned 64-bit integer. 85*635a8641SAndroid Build Coastguard Worker| `float`, `double` | 32- or 64-bit floating point number. 86*635a8641SAndroid Build Coastguard Worker| `string` | UTF-8 encoded string. 87*635a8641SAndroid Build Coastguard Worker| `array<T>` | Array of any Mojom type *T*; for example, `array<uint8>` or `array<array<string>>`. 88*635a8641SAndroid Build Coastguard Worker| `array<T, N>` | Fixed-length array of any Mojom type *T*. The parameter *N* must be an integral constant. 89*635a8641SAndroid Build Coastguard Worker| `map<S, T>` | Associated array maping values of type *S* to values of type *T*. *S* may be a `string`, `enum`, or numeric type. 90*635a8641SAndroid Build Coastguard Worker| `handle` | Generic Mojo handle. May be any type of handle, including a wrapped native platform handle. 91*635a8641SAndroid Build Coastguard Worker| `handle<message_pipe>` | Generic message pipe handle. 92*635a8641SAndroid Build Coastguard Worker| `handle<shared_buffer>` | Shared buffer handle. 93*635a8641SAndroid Build Coastguard Worker| `handle<data_pipe_producer>` | Data pipe producer handle. 94*635a8641SAndroid Build Coastguard Worker| `handle<data_pipe_consumer>` | Data pipe consumer handle. 95*635a8641SAndroid Build Coastguard Worker| *`InterfaceType`* | Any user-defined Mojom interface type. This is sugar for a strongly-typed message pipe handle which should eventually be used to make outgoing calls on the interface. 96*635a8641SAndroid Build Coastguard Worker| *`InterfaceType&`* | An interface request for any user-defined Mojom interface type. This is sugar for a more strongly-typed message pipe handle which is expected to receive request messages and should therefore eventually be bound to an implementation of the interface. 97*635a8641SAndroid Build Coastguard Worker| *`associated InterfaceType`* | An associated interface handle. See [Associated Interfaces](#Associated-Interfaces) 98*635a8641SAndroid Build Coastguard Worker| *`associated InterfaceType&`* | An associated interface request. See [Associated Interfaces](#Associated-Interfaces) 99*635a8641SAndroid Build Coastguard Worker| *T*? | An optional (nullable) value. Primitive numeric types (integers, floats, booleans, and enums) are not nullable. All other types are nullable. 100*635a8641SAndroid Build Coastguard Worker 101*635a8641SAndroid Build Coastguard Worker### Modules 102*635a8641SAndroid Build Coastguard Worker 103*635a8641SAndroid Build Coastguard WorkerEvery Mojom file may optionally specify a single **module** to which it belongs. 104*635a8641SAndroid Build Coastguard Worker 105*635a8641SAndroid Build Coastguard WorkerThis is used strictly for aggregaging all defined symbols therein within a 106*635a8641SAndroid Build Coastguard Workercommon Mojom namespace. The specific impact this has on generated binidngs code 107*635a8641SAndroid Build Coastguard Workervaries for each target language. For example, if the following Mojom is used to 108*635a8641SAndroid Build Coastguard Workergenerate bindings: 109*635a8641SAndroid Build Coastguard Worker 110*635a8641SAndroid Build Coastguard Worker``` 111*635a8641SAndroid Build Coastguard Workermodule business.stuff; 112*635a8641SAndroid Build Coastguard Worker 113*635a8641SAndroid Build Coastguard Workerinterface MoneyGenerator { 114*635a8641SAndroid Build Coastguard Worker GenerateMoney(); 115*635a8641SAndroid Build Coastguard Worker}; 116*635a8641SAndroid Build Coastguard Worker``` 117*635a8641SAndroid Build Coastguard Worker 118*635a8641SAndroid Build Coastguard WorkerGenerated C++ bindings will define a class interface `MoneyGenerator` in the 119*635a8641SAndroid Build Coastguard Worker`business::stuff` namespace, while Java bindings will define an interface 120*635a8641SAndroid Build Coastguard Worker`MoneyGenerator` in the `org.chromium.business.stuff` package. JavaScript 121*635a8641SAndroid Build Coastguard Workerbindings at this time are unaffected by module declarations. 122*635a8641SAndroid Build Coastguard Worker 123*635a8641SAndroid Build Coastguard Worker**NOTE:** By convention in the Chromium codebase, **all** Mojom files should 124*635a8641SAndroid Build Coastguard Workerdeclare a module name with at least (and preferrably exactly) one top-level name 125*635a8641SAndroid Build Coastguard Workeras well as an inner `mojom` module suffix. *e.g.*, `chrome.mojom`, 126*635a8641SAndroid Build Coastguard Worker`business.mojom`, *etc.* 127*635a8641SAndroid Build Coastguard Worker 128*635a8641SAndroid Build Coastguard WorkerThis convention makes it easy to tell which symbols are generated by Mojom when 129*635a8641SAndroid Build Coastguard Workerreading non-Mojom code, and it also avoids namespace collisions in the fairly 130*635a8641SAndroid Build Coastguard Workercommon scenario where you have a real C++ or Java `Foo` along with a 131*635a8641SAndroid Build Coastguard Workercorresponding Mojom `Foo` for its serialized representation. 132*635a8641SAndroid Build Coastguard Worker 133*635a8641SAndroid Build Coastguard Worker### Imports 134*635a8641SAndroid Build Coastguard Worker 135*635a8641SAndroid Build Coastguard WorkerIf your Mojom references definitions from other Mojom files, you must **import** 136*635a8641SAndroid Build Coastguard Workerthose files. Import syntax is as follows: 137*635a8641SAndroid Build Coastguard Worker 138*635a8641SAndroid Build Coastguard Worker``` 139*635a8641SAndroid Build Coastguard Workerimport "services/widget/public/interfaces/frobinator.mojom"; 140*635a8641SAndroid Build Coastguard Worker``` 141*635a8641SAndroid Build Coastguard Worker 142*635a8641SAndroid Build Coastguard WorkerImport paths are always relative to the top-level directory. 143*635a8641SAndroid Build Coastguard Worker 144*635a8641SAndroid Build Coastguard WorkerNote that circular imports are **not** supported. 145*635a8641SAndroid Build Coastguard Worker 146*635a8641SAndroid Build Coastguard Worker### Structs 147*635a8641SAndroid Build Coastguard Worker 148*635a8641SAndroid Build Coastguard WorkerStructs are defined using the **struct** keyword, and they provide a way to 149*635a8641SAndroid Build Coastguard Workergroup related fields together: 150*635a8641SAndroid Build Coastguard Worker 151*635a8641SAndroid Build Coastguard Worker``` cpp 152*635a8641SAndroid Build Coastguard Workerstruct StringPair { 153*635a8641SAndroid Build Coastguard Worker string first; 154*635a8641SAndroid Build Coastguard Worker string second; 155*635a8641SAndroid Build Coastguard Worker}; 156*635a8641SAndroid Build Coastguard Worker``` 157*635a8641SAndroid Build Coastguard Worker 158*635a8641SAndroid Build Coastguard WorkerStruct fields may be comprised of any of the types listed above in the 159*635a8641SAndroid Build Coastguard Worker[Primitive Types](#Primitive-Types) section. 160*635a8641SAndroid Build Coastguard Worker 161*635a8641SAndroid Build Coastguard WorkerDefault values may be specified as long as they are constant: 162*635a8641SAndroid Build Coastguard Worker 163*635a8641SAndroid Build Coastguard Worker``` cpp 164*635a8641SAndroid Build Coastguard Workerstruct Request { 165*635a8641SAndroid Build Coastguard Worker int32 id = -1; 166*635a8641SAndroid Build Coastguard Worker string details; 167*635a8641SAndroid Build Coastguard Worker}; 168*635a8641SAndroid Build Coastguard Worker``` 169*635a8641SAndroid Build Coastguard Worker 170*635a8641SAndroid Build Coastguard WorkerWhat follows is a fairly 171*635a8641SAndroid Build Coastguard Workercomprehensive example using the supported field types: 172*635a8641SAndroid Build Coastguard Worker 173*635a8641SAndroid Build Coastguard Worker``` cpp 174*635a8641SAndroid Build Coastguard Workerstruct StringPair { 175*635a8641SAndroid Build Coastguard Worker string first; 176*635a8641SAndroid Build Coastguard Worker string second; 177*635a8641SAndroid Build Coastguard Worker}; 178*635a8641SAndroid Build Coastguard Worker 179*635a8641SAndroid Build Coastguard Workerenum AnEnum { 180*635a8641SAndroid Build Coastguard Worker YES, 181*635a8641SAndroid Build Coastguard Worker NO 182*635a8641SAndroid Build Coastguard Worker}; 183*635a8641SAndroid Build Coastguard Worker 184*635a8641SAndroid Build Coastguard Workerinterface SampleInterface { 185*635a8641SAndroid Build Coastguard Worker DoStuff(); 186*635a8641SAndroid Build Coastguard Worker}; 187*635a8641SAndroid Build Coastguard Worker 188*635a8641SAndroid Build Coastguard Workerstruct AllTheThings { 189*635a8641SAndroid Build Coastguard Worker // Note that these types can never be marked nullable! 190*635a8641SAndroid Build Coastguard Worker bool boolean_value; 191*635a8641SAndroid Build Coastguard Worker int8 signed_8bit_value = 42; 192*635a8641SAndroid Build Coastguard Worker uint8 unsigned_8bit_value; 193*635a8641SAndroid Build Coastguard Worker int16 signed_16bit_value; 194*635a8641SAndroid Build Coastguard Worker uint16 unsigned_16bit_value; 195*635a8641SAndroid Build Coastguard Worker int32 signed_32bit_value; 196*635a8641SAndroid Build Coastguard Worker uint32 unsigned_32bit_value; 197*635a8641SAndroid Build Coastguard Worker int64 signed_64bit_value; 198*635a8641SAndroid Build Coastguard Worker uint64 unsigned_64bit_value; 199*635a8641SAndroid Build Coastguard Worker float float_value_32bit; 200*635a8641SAndroid Build Coastguard Worker double float_value_64bit; 201*635a8641SAndroid Build Coastguard Worker AnEnum enum_value = AnEnum.YES; 202*635a8641SAndroid Build Coastguard Worker 203*635a8641SAndroid Build Coastguard Worker // Strings may be nullable. 204*635a8641SAndroid Build Coastguard Worker string? maybe_a_string_maybe_not; 205*635a8641SAndroid Build Coastguard Worker 206*635a8641SAndroid Build Coastguard Worker // Structs may contain other structs. These may also be nullable. 207*635a8641SAndroid Build Coastguard Worker StringPair some_strings; 208*635a8641SAndroid Build Coastguard Worker StringPair? maybe_some_more_strings; 209*635a8641SAndroid Build Coastguard Worker 210*635a8641SAndroid Build Coastguard Worker // In fact structs can also be nested, though in practice you must always make 211*635a8641SAndroid Build Coastguard Worker // such fields nullable -- otherwise messages would need to be infinitely long 212*635a8641SAndroid Build Coastguard Worker // in order to pass validation! 213*635a8641SAndroid Build Coastguard Worker AllTheThings? more_things; 214*635a8641SAndroid Build Coastguard Worker 215*635a8641SAndroid Build Coastguard Worker // Arrays may be templated over any Mojom type, and are always nullable: 216*635a8641SAndroid Build Coastguard Worker array<int32> numbers; 217*635a8641SAndroid Build Coastguard Worker array<int32>? maybe_more_numbers; 218*635a8641SAndroid Build Coastguard Worker 219*635a8641SAndroid Build Coastguard Worker // Arrays of arrays of arrays... are fine. 220*635a8641SAndroid Build Coastguard Worker array<array<array<AnEnum>>> this_works_but_really_plz_stop; 221*635a8641SAndroid Build Coastguard Worker 222*635a8641SAndroid Build Coastguard Worker // The element type may be nullable if it's a type which is allowed to be 223*635a8641SAndroid Build Coastguard Worker // nullable. 224*635a8641SAndroid Build Coastguard Worker array<AllTheThings?> more_maybe_things; 225*635a8641SAndroid Build Coastguard Worker 226*635a8641SAndroid Build Coastguard Worker // Fixed-size arrays get some extra validation on the receiving end to ensure 227*635a8641SAndroid Build Coastguard Worker // that the correct number of elements is always received. 228*635a8641SAndroid Build Coastguard Worker array<uint64, 2> uuid; 229*635a8641SAndroid Build Coastguard Worker 230*635a8641SAndroid Build Coastguard Worker // Maps follow many of the same rules as arrays. Key types may be any 231*635a8641SAndroid Build Coastguard Worker // non-handle, non-collection type, and value types may be any supported 232*635a8641SAndroid Build Coastguard Worker // struct field type. Maps may also be nullable. 233*635a8641SAndroid Build Coastguard Worker map<string, int32> one_map; 234*635a8641SAndroid Build Coastguard Worker map<AnEnum, string>? maybe_another_map; 235*635a8641SAndroid Build Coastguard Worker map<StringPair, AllTheThings?>? maybe_a_pretty_weird_but_valid_map; 236*635a8641SAndroid Build Coastguard Worker map<StringPair, map<int32, array<map<string, string>?>?>?> ridiculous; 237*635a8641SAndroid Build Coastguard Worker 238*635a8641SAndroid Build Coastguard Worker // And finally, all handle types are valid as struct fields and may be 239*635a8641SAndroid Build Coastguard Worker // nullable. Note that interfaces and interface requests (the "Foo" and 240*635a8641SAndroid Build Coastguard Worker // "Foo&" type syntax respectively) are just strongly-typed message pipe 241*635a8641SAndroid Build Coastguard Worker // handles. 242*635a8641SAndroid Build Coastguard Worker handle generic_handle; 243*635a8641SAndroid Build Coastguard Worker handle<data_pipe_consumer> reader; 244*635a8641SAndroid Build Coastguard Worker handle<data_pipe_producer>? maybe_writer; 245*635a8641SAndroid Build Coastguard Worker handle<shared_buffer> dumping_ground; 246*635a8641SAndroid Build Coastguard Worker handle<message_pipe> raw_message_pipe; 247*635a8641SAndroid Build Coastguard Worker SampleInterface? maybe_a_sample_interface_client_pipe; 248*635a8641SAndroid Build Coastguard Worker SampleInterface& non_nullable_sample_interface_request; 249*635a8641SAndroid Build Coastguard Worker SampleInterface&? nullable_sample_interface_request; 250*635a8641SAndroid Build Coastguard Worker associated SampleInterface associated_interface_client; 251*635a8641SAndroid Build Coastguard Worker associated SampleInterface& associated_interface_request; 252*635a8641SAndroid Build Coastguard Worker associated SampleInterface&? maybe_another_associated_request; 253*635a8641SAndroid Build Coastguard Worker}; 254*635a8641SAndroid Build Coastguard Worker``` 255*635a8641SAndroid Build Coastguard Worker 256*635a8641SAndroid Build Coastguard WorkerFor details on how all of these different types translate to usable generated 257*635a8641SAndroid Build Coastguard Workercode, see 258*635a8641SAndroid Build Coastguard Worker[documentation for individual target languages](#Generated-Code-For-Target-Languages). 259*635a8641SAndroid Build Coastguard Worker 260*635a8641SAndroid Build Coastguard Worker### Unions 261*635a8641SAndroid Build Coastguard Worker 262*635a8641SAndroid Build Coastguard WorkerMojom supports tagged unions using the **union** keyword. A union is a 263*635a8641SAndroid Build Coastguard Workercollection of fields which may taken the value of any single one of those fields 264*635a8641SAndroid Build Coastguard Workerat a time. Thus they provide a way to represent a variant value type while 265*635a8641SAndroid Build Coastguard Workerminimizing storage requirements. 266*635a8641SAndroid Build Coastguard Worker 267*635a8641SAndroid Build Coastguard WorkerUnion fields may be of any type supported by [struct](#Structs) fields. For 268*635a8641SAndroid Build Coastguard Workerexample: 269*635a8641SAndroid Build Coastguard Worker 270*635a8641SAndroid Build Coastguard Worker```cpp 271*635a8641SAndroid Build Coastguard Workerunion ExampleUnion { 272*635a8641SAndroid Build Coastguard Worker string str; 273*635a8641SAndroid Build Coastguard Worker StringPair pair; 274*635a8641SAndroid Build Coastguard Worker int64 id; 275*635a8641SAndroid Build Coastguard Worker array<uint64, 2> guid; 276*635a8641SAndroid Build Coastguard Worker SampleInterface iface; 277*635a8641SAndroid Build Coastguard Worker}; 278*635a8641SAndroid Build Coastguard Worker``` 279*635a8641SAndroid Build Coastguard Worker 280*635a8641SAndroid Build Coastguard WorkerFor details on how unions like this translate to generated bindings code, see 281*635a8641SAndroid Build Coastguard Worker[documentation for individual target languages](#Generated-Code-For-Target-Languages). 282*635a8641SAndroid Build Coastguard Worker 283*635a8641SAndroid Build Coastguard Worker### Enumeration Types 284*635a8641SAndroid Build Coastguard Worker 285*635a8641SAndroid Build Coastguard WorkerEnumeration types may be defined using the **enum** keyword either directly 286*635a8641SAndroid Build Coastguard Workerwithin a module or within the namespace of some struct or interface: 287*635a8641SAndroid Build Coastguard Worker 288*635a8641SAndroid Build Coastguard Worker``` 289*635a8641SAndroid Build Coastguard Workermodule business.mojom; 290*635a8641SAndroid Build Coastguard Worker 291*635a8641SAndroid Build Coastguard Workerenum Department { 292*635a8641SAndroid Build Coastguard Worker SALES = 0, 293*635a8641SAndroid Build Coastguard Worker DEV, 294*635a8641SAndroid Build Coastguard Worker}; 295*635a8641SAndroid Build Coastguard Worker 296*635a8641SAndroid Build Coastguard Workerstruct Employee { 297*635a8641SAndroid Build Coastguard Worker enum Type { 298*635a8641SAndroid Build Coastguard Worker FULL_TIME, 299*635a8641SAndroid Build Coastguard Worker PART_TIME, 300*635a8641SAndroid Build Coastguard Worker }; 301*635a8641SAndroid Build Coastguard Worker 302*635a8641SAndroid Build Coastguard Worker Type type; 303*635a8641SAndroid Build Coastguard Worker // ... 304*635a8641SAndroid Build Coastguard Worker}; 305*635a8641SAndroid Build Coastguard Worker``` 306*635a8641SAndroid Build Coastguard Worker 307*635a8641SAndroid Build Coastguard WorkerThat that similar to C-style enums, individual values may be explicitly assigned 308*635a8641SAndroid Build Coastguard Workerwithin an enum definition. By default values are based at zero and incremenet by 309*635a8641SAndroid Build Coastguard Worker1 sequentially. 310*635a8641SAndroid Build Coastguard Worker 311*635a8641SAndroid Build Coastguard WorkerThe effect of nested definitions on generated bindings varies depending on the 312*635a8641SAndroid Build Coastguard Workertarget language. See [documentation for individual target languages](#Generated-Code-For-Target-Languages) 313*635a8641SAndroid Build Coastguard Worker 314*635a8641SAndroid Build Coastguard Worker### Constants 315*635a8641SAndroid Build Coastguard Worker 316*635a8641SAndroid Build Coastguard WorkerConstants may be defined using the **const** keyword either directly within a 317*635a8641SAndroid Build Coastguard Workermodule or within the namespace of some struct or interface: 318*635a8641SAndroid Build Coastguard Worker 319*635a8641SAndroid Build Coastguard Worker``` 320*635a8641SAndroid Build Coastguard Workermodule business.mojom; 321*635a8641SAndroid Build Coastguard Worker 322*635a8641SAndroid Build Coastguard Workerconst string kServiceName = "business"; 323*635a8641SAndroid Build Coastguard Worker 324*635a8641SAndroid Build Coastguard Workerstruct Employee { 325*635a8641SAndroid Build Coastguard Worker const uint64 kInvalidId = 0; 326*635a8641SAndroid Build Coastguard Worker 327*635a8641SAndroid Build Coastguard Worker enum Type { 328*635a8641SAndroid Build Coastguard Worker FULL_TIME, 329*635a8641SAndroid Build Coastguard Worker PART_TIME, 330*635a8641SAndroid Build Coastguard Worker }; 331*635a8641SAndroid Build Coastguard Worker 332*635a8641SAndroid Build Coastguard Worker uint64 id = kInvalidId; 333*635a8641SAndroid Build Coastguard Worker Type type; 334*635a8641SAndroid Build Coastguard Worker}; 335*635a8641SAndroid Build Coastguard Worker``` 336*635a8641SAndroid Build Coastguard Worker 337*635a8641SAndroid Build Coastguard WorkerThe effect of nested definitions on generated bindings varies depending on the 338*635a8641SAndroid Build Coastguard Workertarget language. See [documentation for individual target languages](#Generated-Code-For-Target-Languages) 339*635a8641SAndroid Build Coastguard Worker 340*635a8641SAndroid Build Coastguard Worker### Interfaces 341*635a8641SAndroid Build Coastguard Worker 342*635a8641SAndroid Build Coastguard WorkerAn **interface** is a logical bundle of parameterized request messages. Each 343*635a8641SAndroid Build Coastguard Workerrequest message may optionally define a parameterized response message. Here's 344*635a8641SAndroid Build Coastguard Workersyntax to define an interface `Foo` with various kinds of requests: 345*635a8641SAndroid Build Coastguard Worker 346*635a8641SAndroid Build Coastguard Worker``` 347*635a8641SAndroid Build Coastguard Workerinterface Foo { 348*635a8641SAndroid Build Coastguard Worker // A request which takes no arguments and expects no response. 349*635a8641SAndroid Build Coastguard Worker MyMessage(); 350*635a8641SAndroid Build Coastguard Worker 351*635a8641SAndroid Build Coastguard Worker // A request which has some arguments and expects no response. 352*635a8641SAndroid Build Coastguard Worker MyOtherMessage(string name, array<uint8> bytes); 353*635a8641SAndroid Build Coastguard Worker 354*635a8641SAndroid Build Coastguard Worker // A request which expects a single-argument response. 355*635a8641SAndroid Build Coastguard Worker MyMessageWithResponse(string command) => (bool success); 356*635a8641SAndroid Build Coastguard Worker 357*635a8641SAndroid Build Coastguard Worker // A request which expects a response with multiple arguments. 358*635a8641SAndroid Build Coastguard Worker MyMessageWithMoarResponse(string a, string b) => (int8 c, int8 d); 359*635a8641SAndroid Build Coastguard Worker}; 360*635a8641SAndroid Build Coastguard Worker``` 361*635a8641SAndroid Build Coastguard Worker 362*635a8641SAndroid Build Coastguard WorkerAnything which is a valid struct field type (see [Structs](#Structs)) is also a 363*635a8641SAndroid Build Coastguard Workervalid request or response argument type. The type notation is the same for both. 364*635a8641SAndroid Build Coastguard Worker 365*635a8641SAndroid Build Coastguard Worker### Attributes 366*635a8641SAndroid Build Coastguard Worker 367*635a8641SAndroid Build Coastguard WorkerMojom definitions may have their meaning altered by **attributes**, specified 368*635a8641SAndroid Build Coastguard Workerwith a syntax similar to Java or C# attributes. There are a handle of 369*635a8641SAndroid Build Coastguard Workerinteresting attributes supported today. 370*635a8641SAndroid Build Coastguard Worker 371*635a8641SAndroid Build Coastguard Worker**`[Sync]`** 372*635a8641SAndroid Build Coastguard Worker: The `Sync` attribute may be specified for any interface method which expects 373*635a8641SAndroid Build Coastguard Worker a response. This makes it so that callers of the method can wait 374*635a8641SAndroid Build Coastguard Worker synchronously for a response. See 375*635a8641SAndroid Build Coastguard Worker [Synchronous Calls](/mojo/public/cpp/bindings/README.md#Synchronous-Calls) 376*635a8641SAndroid Build Coastguard Worker in the C++ bindings documentation. Note that sync calls are not currently 377*635a8641SAndroid Build Coastguard Worker supported in other target languages. 378*635a8641SAndroid Build Coastguard Worker 379*635a8641SAndroid Build Coastguard Worker**`[Extensible]`** 380*635a8641SAndroid Build Coastguard Worker: The `Extensible` attribute may be specified for any enum definition. This 381*635a8641SAndroid Build Coastguard Worker essentially disables builtin range validation when receiving values of the 382*635a8641SAndroid Build Coastguard Worker enum type in a message, allowing older bindings to tolerate unrecognized 383*635a8641SAndroid Build Coastguard Worker values from newer versions of the enum. 384*635a8641SAndroid Build Coastguard Worker 385*635a8641SAndroid Build Coastguard Worker**`[Native]`** 386*635a8641SAndroid Build Coastguard Worker: The `Native` attribute may be specified for an empty struct declaration to 387*635a8641SAndroid Build Coastguard Worker provide a nominal bridge between Mojo IPC and legacy `IPC::ParamTraits` or 388*635a8641SAndroid Build Coastguard Worker `IPC_STRUCT_TRAITS*` macros. 389*635a8641SAndroid Build Coastguard Worker See [Using Legacy IPC Traits](/ipc/README.md#Using-Legacy-IPC-Traits) for 390*635a8641SAndroid Build Coastguard Worker more details. Note support for this attribute is strictly limited to C++ 391*635a8641SAndroid Build Coastguard Worker bindings generation. 392*635a8641SAndroid Build Coastguard Worker 393*635a8641SAndroid Build Coastguard Worker**`[MinVersion=N]`** 394*635a8641SAndroid Build Coastguard Worker: The `MinVersion` attribute is used to specify the version at which a given 395*635a8641SAndroid Build Coastguard Worker field, enum value, interface method, or method parameter was introduced. 396*635a8641SAndroid Build Coastguard Worker See [Versioning](#Versioning) for more details. 397*635a8641SAndroid Build Coastguard Worker 398*635a8641SAndroid Build Coastguard Worker**`[Uuid=<UUID>]`** 399*635a8641SAndroid Build Coastguard Worker: Specifies a UUID to be associated with a given interface. The UUID is 400*635a8641SAndroid Build Coastguard Worker intended to remain stable across all changes to the interface definition, 401*635a8641SAndroid Build Coastguard Worker including name changes. The value given for this attribute should be a 402*635a8641SAndroid Build Coastguard Worker standard UUID string representation as specified by RFC 4122. New UUIDs can 403*635a8641SAndroid Build Coastguard Worker be generated with common tools such as `uuidgen`. 404*635a8641SAndroid Build Coastguard Worker 405*635a8641SAndroid Build Coastguard Worker**`[EnableIf=value]`** 406*635a8641SAndroid Build Coastguard Worker: The `EnableIf` attribute is used to conditionally enable definitions when 407*635a8641SAndroid Build Coastguard Worker the mojom is parsed. If the `mojom` target in the GN file does not include 408*635a8641SAndroid Build Coastguard Worker the matching `value` in the list of `enabled_features`, the definition 409*635a8641SAndroid Build Coastguard Worker will be disabled. This is useful for mojom definitions that only make 410*635a8641SAndroid Build Coastguard Worker sense on one platform. Note that the `EnableIf` attribute can only be set 411*635a8641SAndroid Build Coastguard Worker once per definition. 412*635a8641SAndroid Build Coastguard Worker 413*635a8641SAndroid Build Coastguard Worker## Generated Code For Target Languages 414*635a8641SAndroid Build Coastguard Worker 415*635a8641SAndroid Build Coastguard WorkerWhen the bindings generator successfully processes an input Mojom file, it emits 416*635a8641SAndroid Build Coastguard Workercorresponding code for each supported target language. For more details on how 417*635a8641SAndroid Build Coastguard WorkerMojom concepts translate to a given target langauge, please refer to the 418*635a8641SAndroid Build Coastguard Workerbindings API documentation for that language: 419*635a8641SAndroid Build Coastguard Worker 420*635a8641SAndroid Build Coastguard Worker* [C++ Bindings](/mojo/public/cpp/bindings/README.md) 421*635a8641SAndroid Build Coastguard Worker* [JavaScript Bindings](/mojo/public/js/README.md) 422*635a8641SAndroid Build Coastguard Worker* [Java Bindings](/mojo/public/java/bindings/README.md) 423*635a8641SAndroid Build Coastguard Worker 424*635a8641SAndroid Build Coastguard Worker## Message Validation 425*635a8641SAndroid Build Coastguard Worker 426*635a8641SAndroid Build Coastguard WorkerRegardless of target language, all interface messages are validated during 427*635a8641SAndroid Build Coastguard Workerdeserialization before they are dispatched to a receiving implementation of the 428*635a8641SAndroid Build Coastguard Workerinterface. This helps to ensure consitent validation across interfaces without 429*635a8641SAndroid Build Coastguard Workerleaving the burden to developers and security reviewers every time a new message 430*635a8641SAndroid Build Coastguard Workeris added. 431*635a8641SAndroid Build Coastguard Worker 432*635a8641SAndroid Build Coastguard WorkerIf a message fails validation, it is never dispatched. Instead a **connection 433*635a8641SAndroid Build Coastguard Workererror** is raised on the binding object (see 434*635a8641SAndroid Build Coastguard Worker[C++ Connection Errors](/mojo/public/cpp/bindings/README.md#Connection-Errors), 435*635a8641SAndroid Build Coastguard Worker[Java Connection Errors](/mojo/public/java/bindings/README.md#Connection-Errors), 436*635a8641SAndroid Build Coastguard Workeror 437*635a8641SAndroid Build Coastguard Worker[JavaScript Connection Errors](/mojo/public/js/README.md#Connection-Errors) for 438*635a8641SAndroid Build Coastguard Workerdetails.) 439*635a8641SAndroid Build Coastguard Worker 440*635a8641SAndroid Build Coastguard WorkerSome baseline level of validation is done automatically for primitive Mojom 441*635a8641SAndroid Build Coastguard Workertypes. 442*635a8641SAndroid Build Coastguard Worker 443*635a8641SAndroid Build Coastguard Worker### Non-Nullable Objects 444*635a8641SAndroid Build Coastguard Worker 445*635a8641SAndroid Build Coastguard WorkerMojom fields or parameter values (*e.g.*, structs, interfaces, arrays, *etc.*) 446*635a8641SAndroid Build Coastguard Workermay be marked nullable in Mojom definitions (see 447*635a8641SAndroid Build Coastguard Worker[Primitive Types](#Primitive-Types).) If a field or parameter is **not** marked 448*635a8641SAndroid Build Coastguard Workernullable but a message is received with a null value in its place, that message 449*635a8641SAndroid Build Coastguard Workerwill fail validation. 450*635a8641SAndroid Build Coastguard Worker 451*635a8641SAndroid Build Coastguard Worker### Enums 452*635a8641SAndroid Build Coastguard Worker 453*635a8641SAndroid Build Coastguard WorkerEnums declared in Mojom are automatically validated against the range of legal 454*635a8641SAndroid Build Coastguard Workervalues. For example if a Mojom declares the enum: 455*635a8641SAndroid Build Coastguard Worker 456*635a8641SAndroid Build Coastguard Worker``` cpp 457*635a8641SAndroid Build Coastguard Workerenum AdvancedBoolean { 458*635a8641SAndroid Build Coastguard Worker TRUE = 0, 459*635a8641SAndroid Build Coastguard Worker FALSE = 1, 460*635a8641SAndroid Build Coastguard Worker FILE_NOT_FOUND = 2, 461*635a8641SAndroid Build Coastguard Worker}; 462*635a8641SAndroid Build Coastguard Worker``` 463*635a8641SAndroid Build Coastguard Worker 464*635a8641SAndroid Build Coastguard Workerand a message is received with the integral value 3 (or anything other than 0, 465*635a8641SAndroid Build Coastguard Worker1, or 2) in place of some `AdvancedBoolean` field or parameter, the message will 466*635a8641SAndroid Build Coastguard Workerfail validation. 467*635a8641SAndroid Build Coastguard Worker 468*635a8641SAndroid Build Coastguard Worker*** note 469*635a8641SAndroid Build Coastguard WorkerNOTE: It's possible to avoid this type of validation error by explicitly marking 470*635a8641SAndroid Build Coastguard Workeran enum as [Extensible](#Attributes) if you anticipate your enum being exchanged 471*635a8641SAndroid Build Coastguard Workerbetween two different versions of the binding interface. See 472*635a8641SAndroid Build Coastguard Worker[Versioning](#Versioning). 473*635a8641SAndroid Build Coastguard Worker*** 474*635a8641SAndroid Build Coastguard Worker 475*635a8641SAndroid Build Coastguard Worker### Other failures 476*635a8641SAndroid Build Coastguard Worker 477*635a8641SAndroid Build Coastguard WorkerThere are a host of internal validation errors that may occur when a malformed 478*635a8641SAndroid Build Coastguard Workermessage is received, but developers should not be concerned with these 479*635a8641SAndroid Build Coastguard Workerspecifically; in general they can only result from internal bindings bugs, 480*635a8641SAndroid Build Coastguard Workercompromised processes, or some remote endpoint making a dubious effort to 481*635a8641SAndroid Build Coastguard Workermanually encode their own bindings messages. 482*635a8641SAndroid Build Coastguard Worker 483*635a8641SAndroid Build Coastguard Worker### Custom Validation 484*635a8641SAndroid Build Coastguard Worker 485*635a8641SAndroid Build Coastguard WorkerIt's also possible for developers to define custom validation logic for specific 486*635a8641SAndroid Build Coastguard WorkerMojom struct types by exploiting the 487*635a8641SAndroid Build Coastguard Worker[type mapping](/mojo/public/cpp/bindings/README.md#Type-Mapping) system for C++ 488*635a8641SAndroid Build Coastguard Workerbindings. Messages rejected by custom validation logic trigger the same 489*635a8641SAndroid Build Coastguard Workervalidation failure behavior as the built-in type validation routines. 490*635a8641SAndroid Build Coastguard Worker 491*635a8641SAndroid Build Coastguard Worker## Associated Interfaces 492*635a8641SAndroid Build Coastguard Worker 493*635a8641SAndroid Build Coastguard WorkerAs mentioned in the [Primitive Types](#Primitive-Types) section above, interface 494*635a8641SAndroid Build Coastguard Workerand interface request fields and parameters may be marked as `associated`. This 495*635a8641SAndroid Build Coastguard Workeressentially means that they are piggy-backed on some other interface's message 496*635a8641SAndroid Build Coastguard Workerpipe. 497*635a8641SAndroid Build Coastguard Worker 498*635a8641SAndroid Build Coastguard WorkerBecause individual interface message pipes operate independently there can be no 499*635a8641SAndroid Build Coastguard Workerrelative ordering guarantees among them. Associated interfaces are useful when 500*635a8641SAndroid Build Coastguard Workerone interface needs to guarantee strict FIFO ordering with respect to one or 501*635a8641SAndroid Build Coastguard Workermore other interfaces, as they allow interfaces to share a single pipe. 502*635a8641SAndroid Build Coastguard Worker 503*635a8641SAndroid Build Coastguard WorkerCurrenly associated interfaces are only supported in generated C++ bindings. 504*635a8641SAndroid Build Coastguard WorkerSee the documentation for 505*635a8641SAndroid Build Coastguard Worker[C++ Associated Interfaces](/mojo/public/cpp/bindings/README.md#Associated-Interfaces). 506*635a8641SAndroid Build Coastguard Worker 507*635a8641SAndroid Build Coastguard Worker## Versioning 508*635a8641SAndroid Build Coastguard Worker 509*635a8641SAndroid Build Coastguard Worker### Overview 510*635a8641SAndroid Build Coastguard Worker 511*635a8641SAndroid Build Coastguard Worker*** note 512*635a8641SAndroid Build Coastguard Worker**NOTE:** You don't need to worry about versioning if you don't care about 513*635a8641SAndroid Build Coastguard Workerbackwards compatibility. Specifically, all parts of Chrome are updated 514*635a8641SAndroid Build Coastguard Workeratomically today and there is not yet any possibility of any two Chrome 515*635a8641SAndroid Build Coastguard Workerprocesses communicating with two different versions of any given Mojom 516*635a8641SAndroid Build Coastguard Workerinterface. 517*635a8641SAndroid Build Coastguard Worker*** 518*635a8641SAndroid Build Coastguard Worker 519*635a8641SAndroid Build Coastguard WorkerServices extend their interfaces to support new features over time, and clients 520*635a8641SAndroid Build Coastguard Workerwant to use those new features when they are available. If services and clients 521*635a8641SAndroid Build Coastguard Workerare not updated at the same time, it's important for them to be able to 522*635a8641SAndroid Build Coastguard Workercommunicate with each other using different snapshots (versions) of their 523*635a8641SAndroid Build Coastguard Workerinterfaces. 524*635a8641SAndroid Build Coastguard Worker 525*635a8641SAndroid Build Coastguard WorkerThis document shows how to extend Mojom interfaces in a backwards-compatible 526*635a8641SAndroid Build Coastguard Workerway. Changing interfaces in a non-backwards-compatible way is not discussed, 527*635a8641SAndroid Build Coastguard Workerbecause in that case communication between different interface versions is 528*635a8641SAndroid Build Coastguard Workerimpossible anyway. 529*635a8641SAndroid Build Coastguard Worker 530*635a8641SAndroid Build Coastguard Worker### Versioned Structs 531*635a8641SAndroid Build Coastguard Worker 532*635a8641SAndroid Build Coastguard WorkerYou can use the `MinVersion` [attribute](#Attributes) to indicate from which 533*635a8641SAndroid Build Coastguard Workerversion a struct field is introduced. Assume you have the following struct: 534*635a8641SAndroid Build Coastguard Worker 535*635a8641SAndroid Build Coastguard Worker``` cpp 536*635a8641SAndroid Build Coastguard Workerstruct Employee { 537*635a8641SAndroid Build Coastguard Worker uint64 employee_id; 538*635a8641SAndroid Build Coastguard Worker string name; 539*635a8641SAndroid Build Coastguard Worker}; 540*635a8641SAndroid Build Coastguard Worker``` 541*635a8641SAndroid Build Coastguard Worker 542*635a8641SAndroid Build Coastguard Workerand you would like to add a birthday field. You can do: 543*635a8641SAndroid Build Coastguard Worker 544*635a8641SAndroid Build Coastguard Worker``` cpp 545*635a8641SAndroid Build Coastguard Workerstruct Employee { 546*635a8641SAndroid Build Coastguard Worker uint64 employee_id; 547*635a8641SAndroid Build Coastguard Worker string name; 548*635a8641SAndroid Build Coastguard Worker [MinVersion=1] Date? birthday; 549*635a8641SAndroid Build Coastguard Worker}; 550*635a8641SAndroid Build Coastguard Worker``` 551*635a8641SAndroid Build Coastguard Worker 552*635a8641SAndroid Build Coastguard WorkerBy default, fields belong to version 0. New fields must be appended to the 553*635a8641SAndroid Build Coastguard Workerstruct definition (*i.e*., existing fields must not change **ordinal value**) 554*635a8641SAndroid Build Coastguard Workerwith the `MinVersion` attribute set to a number greater than any previous 555*635a8641SAndroid Build Coastguard Workerexisting versions. 556*635a8641SAndroid Build Coastguard Worker 557*635a8641SAndroid Build Coastguard Worker**Ordinal value** refers to the relative positional layout of a struct's fields 558*635a8641SAndroid Build Coastguard Worker(and an interface's methods) when encoded in a message. Implicitly, ordinal 559*635a8641SAndroid Build Coastguard Workernumbers are assigned to fields according to lexical position. In the example 560*635a8641SAndroid Build Coastguard Workerabove, `employee_id` has an ordinal value of 0 and `name` has an ordinal value 561*635a8641SAndroid Build Coastguard Workerof 1. 562*635a8641SAndroid Build Coastguard Worker 563*635a8641SAndroid Build Coastguard WorkerOrdinal values can be specified explicitly using `**@**` notation, subject to 564*635a8641SAndroid Build Coastguard Workerthe following hard constraints: 565*635a8641SAndroid Build Coastguard Worker 566*635a8641SAndroid Build Coastguard Worker* For any given struct or interface, if any field or method explicitly specifies 567*635a8641SAndroid Build Coastguard Worker an ordinal value, all fields or methods must explicitly specify an ordinal 568*635a8641SAndroid Build Coastguard Worker value. 569*635a8641SAndroid Build Coastguard Worker* For an *N*-field struct or *N*-method interface, the set of explicitly 570*635a8641SAndroid Build Coastguard Worker assigned ordinal values must be limited to the range *[0, N-1]*. 571*635a8641SAndroid Build Coastguard Worker 572*635a8641SAndroid Build Coastguard WorkerYou may reorder fields, but you must ensure that the ordinal values of existing 573*635a8641SAndroid Build Coastguard Workerfields remain unchanged. For example, the following struct remains 574*635a8641SAndroid Build Coastguard Workerbackwards-compatible: 575*635a8641SAndroid Build Coastguard Worker 576*635a8641SAndroid Build Coastguard Worker``` cpp 577*635a8641SAndroid Build Coastguard Workerstruct Employee { 578*635a8641SAndroid Build Coastguard Worker uint64 employee_id@0; 579*635a8641SAndroid Build Coastguard Worker [MinVersion=1] Date? birthday@2; 580*635a8641SAndroid Build Coastguard Worker string name@1; 581*635a8641SAndroid Build Coastguard Worker}; 582*635a8641SAndroid Build Coastguard Worker``` 583*635a8641SAndroid Build Coastguard Worker 584*635a8641SAndroid Build Coastguard Worker*** note 585*635a8641SAndroid Build Coastguard Worker**NOTE:** Newly added fields of Mojo object or handle types MUST be nullable. 586*635a8641SAndroid Build Coastguard WorkerSee [Primitive Types](#Primitive-Types). 587*635a8641SAndroid Build Coastguard Worker*** 588*635a8641SAndroid Build Coastguard Worker 589*635a8641SAndroid Build Coastguard Worker### Versioned Interfaces 590*635a8641SAndroid Build Coastguard Worker 591*635a8641SAndroid Build Coastguard WorkerThere are two dimensions on which an interface can be extended 592*635a8641SAndroid Build Coastguard Worker 593*635a8641SAndroid Build Coastguard Worker**Appending New Parameters To Existing Methods** 594*635a8641SAndroid Build Coastguard Worker: Parameter lists are treated as structs internally, so all the rules of 595*635a8641SAndroid Build Coastguard Worker versioned structs apply to method parameter lists. The only difference is 596*635a8641SAndroid Build Coastguard Worker that the version number is scoped to the whole interface rather than to any 597*635a8641SAndroid Build Coastguard Worker individual parameter list. 598*635a8641SAndroid Build Coastguard Worker 599*635a8641SAndroid Build Coastguard Worker Please note that adding a response to a message which did not previously 600*635a8641SAndroid Build Coastguard Worker expect a response is a not a backwards-compatible change. 601*635a8641SAndroid Build Coastguard Worker 602*635a8641SAndroid Build Coastguard Worker**Appending New Methods** 603*635a8641SAndroid Build Coastguard Worker: Similarly, you can reorder methods with explicit ordinal values as long as 604*635a8641SAndroid Build Coastguard Worker the ordinal values of existing methods are unchanged. 605*635a8641SAndroid Build Coastguard Worker 606*635a8641SAndroid Build Coastguard WorkerFor example: 607*635a8641SAndroid Build Coastguard Worker 608*635a8641SAndroid Build Coastguard Worker``` cpp 609*635a8641SAndroid Build Coastguard Worker// Old version: 610*635a8641SAndroid Build Coastguard Workerinterface HumanResourceDatabase { 611*635a8641SAndroid Build Coastguard Worker AddEmployee(Employee employee) => (bool success); 612*635a8641SAndroid Build Coastguard Worker QueryEmployee(uint64 id) => (Employee? employee); 613*635a8641SAndroid Build Coastguard Worker}; 614*635a8641SAndroid Build Coastguard Worker 615*635a8641SAndroid Build Coastguard Worker// New version: 616*635a8641SAndroid Build Coastguard Workerinterface HumanResourceDatabase { 617*635a8641SAndroid Build Coastguard Worker AddEmployee(Employee employee) => (bool success); 618*635a8641SAndroid Build Coastguard Worker 619*635a8641SAndroid Build Coastguard Worker QueryEmployee(uint64 id, [MinVersion=1] bool retrieve_finger_print) 620*635a8641SAndroid Build Coastguard Worker => (Employee? employee, 621*635a8641SAndroid Build Coastguard Worker [MinVersion=1] array<uint8>? finger_print); 622*635a8641SAndroid Build Coastguard Worker 623*635a8641SAndroid Build Coastguard Worker [MinVersion=1] 624*635a8641SAndroid Build Coastguard Worker AttachFingerPrint(uint64 id, array<uint8> finger_print) 625*635a8641SAndroid Build Coastguard Worker => (bool success); 626*635a8641SAndroid Build Coastguard Worker}; 627*635a8641SAndroid Build Coastguard Worker``` 628*635a8641SAndroid Build Coastguard Worker 629*635a8641SAndroid Build Coastguard WorkerSimilar to [versioned structs](#Versioned-Structs), when you pass the parameter 630*635a8641SAndroid Build Coastguard Workerlist of a request or response method to a destination using an older version of 631*635a8641SAndroid Build Coastguard Workeran interface, unrecognized fields are silently discarded. However, if the method 632*635a8641SAndroid Build Coastguard Workercall itself is not recognized, it is considered a validation error and the 633*635a8641SAndroid Build Coastguard Workerreceiver will close its end of the interface pipe. For example, if a client on 634*635a8641SAndroid Build Coastguard Workerversion 1 of the above interface sends an `AttachFingerPrint` request to an 635*635a8641SAndroid Build Coastguard Workerimplementation of version 0, the client will be disconnected. 636*635a8641SAndroid Build Coastguard Worker 637*635a8641SAndroid Build Coastguard WorkerBindings target languages that support versioning expose means to query or 638*635a8641SAndroid Build Coastguard Workerassert the remote version from a client handle (*e.g.*, an 639*635a8641SAndroid Build Coastguard Worker`InterfacePtr<T>` in C++ bindings.) 640*635a8641SAndroid Build Coastguard Worker 641*635a8641SAndroid Build Coastguard WorkerSee 642*635a8641SAndroid Build Coastguard Worker[C++ Versioning Considerations](/mojo/public/cpp/bindings/README.md#Versioning-Considerations) 643*635a8641SAndroid Build Coastguard Workerand 644*635a8641SAndroid Build Coastguard Worker[Java Versioning Considerations](/mojo/public/java/bindings/README.md#Versioning-Considerations) 645*635a8641SAndroid Build Coastguard Worker 646*635a8641SAndroid Build Coastguard Worker### Versioned Enums 647*635a8641SAndroid Build Coastguard Worker 648*635a8641SAndroid Build Coastguard Worker**By default, enums are non-extensible**, which means that generated message 649*635a8641SAndroid Build Coastguard Workervalidation code does not expect to see new values in the future. When an unknown 650*635a8641SAndroid Build Coastguard Workervalue is seen for a non-extensible enum field or parameter, a validation error 651*635a8641SAndroid Build Coastguard Workeris raised. 652*635a8641SAndroid Build Coastguard Worker 653*635a8641SAndroid Build Coastguard WorkerIf you want an enum to be extensible in the future, you can apply the 654*635a8641SAndroid Build Coastguard Worker`[Extensible]` [attribute](#Attributes): 655*635a8641SAndroid Build Coastguard Worker 656*635a8641SAndroid Build Coastguard Worker``` cpp 657*635a8641SAndroid Build Coastguard Worker[Extensible] 658*635a8641SAndroid Build Coastguard Workerenum Department { 659*635a8641SAndroid Build Coastguard Worker SALES, 660*635a8641SAndroid Build Coastguard Worker DEV, 661*635a8641SAndroid Build Coastguard Worker}; 662*635a8641SAndroid Build Coastguard Worker``` 663*635a8641SAndroid Build Coastguard Worker 664*635a8641SAndroid Build Coastguard WorkerAnd later you can extend this enum without breaking backwards compatibility: 665*635a8641SAndroid Build Coastguard Worker 666*635a8641SAndroid Build Coastguard Worker``` cpp 667*635a8641SAndroid Build Coastguard Worker[Extensible] 668*635a8641SAndroid Build Coastguard Workerenum Department { 669*635a8641SAndroid Build Coastguard Worker SALES, 670*635a8641SAndroid Build Coastguard Worker DEV, 671*635a8641SAndroid Build Coastguard Worker [MinVersion=1] RESEARCH, 672*635a8641SAndroid Build Coastguard Worker}; 673*635a8641SAndroid Build Coastguard Worker``` 674*635a8641SAndroid Build Coastguard Worker 675*635a8641SAndroid Build Coastguard Worker*** note 676*635a8641SAndroid Build Coastguard Worker**NOTE:** For versioned enum definitions, the use of a `[MinVersion]` attribute 677*635a8641SAndroid Build Coastguard Workeris strictly for documentation purposes. It has no impact on the generated code. 678*635a8641SAndroid Build Coastguard Worker*** 679*635a8641SAndroid Build Coastguard Worker 680*635a8641SAndroid Build Coastguard WorkerWith extensible enums, bound interface implementations may receive unknown enum 681*635a8641SAndroid Build Coastguard Workervalues and will need to deal with them gracefully. See 682*635a8641SAndroid Build Coastguard Worker[C++ Versioning Considerations](/mojo/public/cpp/bindings/README.md#Versioning-Considerations) 683*635a8641SAndroid Build Coastguard Workerfor details. 684*635a8641SAndroid Build Coastguard Worker 685*635a8641SAndroid Build Coastguard Worker## Grammar Reference 686*635a8641SAndroid Build Coastguard Worker 687*635a8641SAndroid Build Coastguard WorkerBelow is the (BNF-ish) context-free grammar of the Mojom language: 688*635a8641SAndroid Build Coastguard Worker 689*635a8641SAndroid Build Coastguard Worker``` 690*635a8641SAndroid Build Coastguard WorkerMojomFile = StatementList 691*635a8641SAndroid Build Coastguard WorkerStatementList = Statement StatementList | Statement 692*635a8641SAndroid Build Coastguard WorkerStatement = ModuleStatement | ImportStatement | Definition 693*635a8641SAndroid Build Coastguard Worker 694*635a8641SAndroid Build Coastguard WorkerModuleStatement = AttributeSection "module" Identifier ";" 695*635a8641SAndroid Build Coastguard WorkerImportStatement = "import" StringLiteral ";" 696*635a8641SAndroid Build Coastguard WorkerDefinition = Struct Union Interface Enum Const 697*635a8641SAndroid Build Coastguard Worker 698*635a8641SAndroid Build Coastguard WorkerAttributeSection = "[" AttributeList "]" 699*635a8641SAndroid Build Coastguard WorkerAttributeList = <empty> | NonEmptyAttributeList 700*635a8641SAndroid Build Coastguard WorkerNonEmptyAttributeList = Attribute 701*635a8641SAndroid Build Coastguard Worker | Attribute "," NonEmptyAttributeList 702*635a8641SAndroid Build Coastguard WorkerAttribute = Name 703*635a8641SAndroid Build Coastguard Worker | Name "=" Name 704*635a8641SAndroid Build Coastguard Worker | Name "=" Literal 705*635a8641SAndroid Build Coastguard Worker 706*635a8641SAndroid Build Coastguard WorkerStruct = AttributeSection "struct" Name "{" StructBody "}" ";" 707*635a8641SAndroid Build Coastguard Worker | AttributeSection "struct" Name ";" 708*635a8641SAndroid Build Coastguard WorkerStructBody = <empty> 709*635a8641SAndroid Build Coastguard Worker | StructBody Const 710*635a8641SAndroid Build Coastguard Worker | StructBody Enum 711*635a8641SAndroid Build Coastguard Worker | StructBody StructField 712*635a8641SAndroid Build Coastguard WorkerStructField = AttributeSection TypeSpec Name Orginal Default ";" 713*635a8641SAndroid Build Coastguard Worker 714*635a8641SAndroid Build Coastguard WorkerUnion = AttributeSection "union" Name "{" UnionBody "}" ";" 715*635a8641SAndroid Build Coastguard WorkerUnionBody = <empty> | UnionBody UnionField 716*635a8641SAndroid Build Coastguard WorkerUnionField = AttributeSection TypeSpec Name Ordinal ";" 717*635a8641SAndroid Build Coastguard Worker 718*635a8641SAndroid Build Coastguard WorkerInterface = AttributeSection "interface" Name "{" InterfaceBody "}" ";" 719*635a8641SAndroid Build Coastguard WorkerInterfaceBody = <empty> 720*635a8641SAndroid Build Coastguard Worker | InterfaceBody Const 721*635a8641SAndroid Build Coastguard Worker | InterfaceBody Enum 722*635a8641SAndroid Build Coastguard Worker | InterfaceBody Method 723*635a8641SAndroid Build Coastguard WorkerMethod = AttributeSection Name Ordinal "(" ParamterList ")" Response ";" 724*635a8641SAndroid Build Coastguard WorkerParameterList = <empty> | NonEmptyParameterList 725*635a8641SAndroid Build Coastguard WorkerNonEmptyParameterList = Parameter 726*635a8641SAndroid Build Coastguard Worker | Parameter "," NonEmptyParameterList 727*635a8641SAndroid Build Coastguard WorkerParameter = AttributeSection TypeSpec Name Ordinal 728*635a8641SAndroid Build Coastguard WorkerResponse = <empty> | "=>" "(" ParameterList ")" 729*635a8641SAndroid Build Coastguard Worker 730*635a8641SAndroid Build Coastguard WorkerTypeSpec = TypeName "?" | TypeName 731*635a8641SAndroid Build Coastguard WorkerTypeName = BasicTypeName 732*635a8641SAndroid Build Coastguard Worker | Array 733*635a8641SAndroid Build Coastguard Worker | FixedArray 734*635a8641SAndroid Build Coastguard Worker | Map 735*635a8641SAndroid Build Coastguard Worker | InterfaceRequest 736*635a8641SAndroid Build Coastguard WorkerBasicTypeName = Identifier | "associated" Identifier | HandleType | NumericType 737*635a8641SAndroid Build Coastguard WorkerNumericType = "bool" | "int8" | "uint8" | "int16" | "uint16" | "int32" 738*635a8641SAndroid Build Coastguard Worker | "uint32" | "int64" | "uint64" | "float" | "double" 739*635a8641SAndroid Build Coastguard WorkerHandleType = "handle" | "handle" "<" SpecificHandleType ">" 740*635a8641SAndroid Build Coastguard WorkerSpecificHandleType = "message_pipe" 741*635a8641SAndroid Build Coastguard Worker | "shared_buffer" 742*635a8641SAndroid Build Coastguard Worker | "data_pipe_consumer" 743*635a8641SAndroid Build Coastguard Worker | "data_pipe_producer" 744*635a8641SAndroid Build Coastguard WorkerArray = "array" "<" TypeSpec ">" 745*635a8641SAndroid Build Coastguard WorkerFixedArray = "array" "<" TypeSpec "," IntConstDec ">" 746*635a8641SAndroid Build Coastguard WorkerMap = "map" "<" Identifier "," TypeSpec ">" 747*635a8641SAndroid Build Coastguard WorkerInterfaceRequest = Identifier "&" | "associated" Identifier "&" 748*635a8641SAndroid Build Coastguard Worker 749*635a8641SAndroid Build Coastguard WorkerOrdinal = <empty> | OrdinalValue 750*635a8641SAndroid Build Coastguard Worker 751*635a8641SAndroid Build Coastguard WorkerDefault = <empty> | "=" Constant 752*635a8641SAndroid Build Coastguard Worker 753*635a8641SAndroid Build Coastguard WorkerEnum = AttributeSection "enum" Name "{" NonEmptyEnumValueList "}" ";" 754*635a8641SAndroid Build Coastguard Worker | AttributeSection "enum" Name "{" NonEmptyEnumValueList "," "}" ";" 755*635a8641SAndroid Build Coastguard WorkerNonEmptyEnumValueList = EnumValue | NonEmptyEnumValueList "," EnumValue 756*635a8641SAndroid Build Coastguard WorkerEnumValue = AttributeSection Name 757*635a8641SAndroid Build Coastguard Worker | AttributeSection Name "=" Integer 758*635a8641SAndroid Build Coastguard Worker | AttributeSection Name "=" Identifier 759*635a8641SAndroid Build Coastguard Worker 760*635a8641SAndroid Build Coastguard WorkerConst = "const" TypeSpec Name "=" Constant ";" 761*635a8641SAndroid Build Coastguard Worker 762*635a8641SAndroid Build Coastguard WorkerConstant = Literal | Identifier ";" 763*635a8641SAndroid Build Coastguard Worker 764*635a8641SAndroid Build Coastguard WorkerIdentifier = Name | Name "." Identifier 765*635a8641SAndroid Build Coastguard Worker 766*635a8641SAndroid Build Coastguard WorkerLiteral = Integer | Float | "true" | "false" | "default" | StringLiteral 767*635a8641SAndroid Build Coastguard Worker 768*635a8641SAndroid Build Coastguard WorkerInteger = IntConst | "+" IntConst | "-" IntConst 769*635a8641SAndroid Build Coastguard WorkerIntConst = IntConstDec | IntConstHex 770*635a8641SAndroid Build Coastguard Worker 771*635a8641SAndroid Build Coastguard WorkerFloat = FloatConst | "+" FloatConst | "-" FloatConst 772*635a8641SAndroid Build Coastguard Worker 773*635a8641SAndroid Build Coastguard Worker; The rules below are for tokens matched strictly according to the given regexes 774*635a8641SAndroid Build Coastguard Worker 775*635a8641SAndroid Build Coastguard WorkerIdentifier = /[a-zA-Z_][0-9a-zA-Z_]*/ 776*635a8641SAndroid Build Coastguard WorkerIntConstDec = /0|(1-9[0-9]*)/ 777*635a8641SAndroid Build Coastguard WorkerIntConstHex = /0[xX][0-9a-fA-F]+/ 778*635a8641SAndroid Build Coastguard WorkerOrdinalValue = /@(0|(1-9[0-9]*))/ 779*635a8641SAndroid Build Coastguard WorkerFloatConst = ... # Imagine it's close enough to C-style float syntax. 780*635a8641SAndroid Build Coastguard WorkerStringLiteral = ... # Imagine it's close enough to C-style string literals, including escapes. 781*635a8641SAndroid Build Coastguard Worker``` 782*635a8641SAndroid Build Coastguard Worker 783*635a8641SAndroid Build Coastguard Worker## Additional Documentation 784*635a8641SAndroid Build Coastguard Worker 785*635a8641SAndroid Build Coastguard Worker[Mojom Message Format](https://docs.google.com/document/d/13pv9cFh5YKuBggDBQ1-AL8VReF-IYpFOFpRfvWFrwio/edit) 786*635a8641SAndroid Build Coastguard Worker: Describes the wire format used by Mojo bindings interfaces over message 787*635a8641SAndroid Build Coastguard Worker pipes. 788*635a8641SAndroid Build Coastguard Worker 789*635a8641SAndroid Build Coastguard Worker[Input Format of Mojom Message Validation Tests](https://docs.google.com/document/d/1-y-2IYctyX2NPaLxJjpJfzVNWCC2SR2MJAD9MpIytHQ/edit) 790*635a8641SAndroid Build Coastguard Worker: Describes a text format used to facilitate bindings message validation 791*635a8641SAndroid Build Coastguard Worker tests. 792