Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 1 | # Mojo C++ Bindings API |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 2 | This document is a subset of the [Mojo documentation](/mojo/README.md). |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 3 | |
| 4 | [TOC] |
| 5 | |
| 6 | ## Overview |
| 7 | The Mojo C++ Bindings API leverages the |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 8 | [C++ System API](/mojo/public/cpp/system/README.md) to provide a more natural |
| 9 | set of primitives for communicating over Mojo message pipes. Combined with |
| 10 | generated code from the |
| 11 | [Mojom IDL and bindings generator](/mojo/public/tools/bindings/README.md), users |
| 12 | can easily connect interface clients and implementations across arbitrary intra- |
| 13 | and inter-process bounaries. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 14 | |
| 15 | This document provides a detailed guide to bindings API usage with example code |
| 16 | snippets. For a detailed API references please consult the headers in |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 17 | [//mojo/public/cpp/bindings](https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/README.md). |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 18 | |
Oksana Zhuravlova | 70c47af | 2018-07-06 22:48:24 | [diff] [blame] | 19 | For a simplified guide targeted at Chromium developers, see [this |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 20 | link](/docs/mojo_and_services.md). |
Erik Chen | 3fdc02bc | 2018-06-28 03:13:48 | [diff] [blame] | 21 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 22 | ## Getting Started |
| 23 | |
| 24 | When a Mojom IDL file is processed by the bindings generator, C++ code is |
| 25 | emitted in a series of `.h` and `.cc` files with names based on the input |
| 26 | `.mojom` file. Suppose we create the following Mojom file at |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 27 | `//services/db/public/mojom/db.mojom`: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 28 | |
| 29 | ``` |
| 30 | module db.mojom; |
| 31 | |
| 32 | interface Table { |
| 33 | AddRow(int32 key, string data); |
| 34 | }; |
| 35 | |
| 36 | interface Database { |
| 37 | CreateTable(Table& table); |
| 38 | }; |
| 39 | ``` |
| 40 | |
| 41 | And a GN target to generate the bindings in |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 42 | `//services/db/public/mojom/BUILD.gn`: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 43 | |
| 44 | ``` |
| 45 | import("//mojo/public/tools/bindings/mojom.gni") |
| 46 | |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 47 | mojom("mojom") { |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 48 | sources = [ |
| 49 | "db.mojom", |
| 50 | ] |
| 51 | } |
| 52 | ``` |
| 53 | |
David 'Digit' Turner | 743836f8 | 2017-10-27 13:04:15 | [diff] [blame] | 54 | Ensure that any target that needs this interface depends on it, e.g. with a line like: |
| 55 | |
| 56 | ``` |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 57 | deps += [ '//services/db/public/mojom' ] |
David 'Digit' Turner | 743836f8 | 2017-10-27 13:04:15 | [diff] [blame] | 58 | ``` |
| 59 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 60 | If we then build this target: |
| 61 | |
| 62 | ``` |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 63 | ninja -C out/r services/db/public/mojom |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 64 | ``` |
| 65 | |
| 66 | This will produce several generated source files, some of which are relevant to |
| 67 | C++ bindings. Two of these files are: |
| 68 | |
| 69 | ``` |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 70 | out/gen/services/db/public/mojom/db.mojom.cc |
| 71 | out/gen/services/db/public/mojom/db.mojom.h |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 72 | ``` |
| 73 | |
| 74 | You can include the above generated header in your sources in order to use the |
| 75 | definitions therein: |
| 76 | |
| 77 | ``` cpp |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 78 | #include "services/business/public/mojom/factory.mojom.h" |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 79 | |
| 80 | class TableImpl : public db::mojom::Table { |
| 81 | // ... |
| 82 | }; |
| 83 | ``` |
| 84 | |
| 85 | This document covers the different kinds of definitions generated by Mojom IDL |
| 86 | for C++ consumers and how they can effectively be used to communicate across |
| 87 | message pipes. |
| 88 | |
| 89 | *** note |
| 90 | **NOTE:** Using C++ bindings from within Blink code is typically subject to |
| 91 | special constraints which require the use of a different generated header. |
| 92 | For details, see [Blink Type Mapping](#Blink-Type-Mapping). |
| 93 | *** |
| 94 | |
| 95 | ## Interfaces |
| 96 | |
| 97 | Mojom IDL interfaces are translated to corresponding C++ (pure virtual) class |
| 98 | interface definitions in the generated header, consisting of a single generated |
| 99 | method signature for each request message on the interface. Internally there is |
| 100 | also generated code for serialization and deserialization of messages, but this |
| 101 | detail is hidden from bindings consumers. |
| 102 | |
| 103 | ### Basic Usage |
| 104 | |
| 105 | Let's consider a new `//sample/logger.mojom` to define a simple logging |
| 106 | interface which clients can use to log simple string messages: |
| 107 | |
| 108 | ``` cpp |
| 109 | module sample.mojom; |
| 110 | |
| 111 | interface Logger { |
| 112 | Log(string message); |
| 113 | }; |
| 114 | ``` |
| 115 | |
| 116 | Running this through the bindings generator will produce a `logging.mojom.h` |
| 117 | with the following definitions (modulo unimportant details): |
| 118 | |
| 119 | ``` cpp |
| 120 | namespace sample { |
| 121 | namespace mojom { |
| 122 | |
| 123 | class Logger { |
| 124 | virtual ~Logger() {} |
| 125 | |
| 126 | virtual void Log(const std::string& message) = 0; |
| 127 | }; |
| 128 | |
| 129 | using LoggerPtr = mojo::InterfacePtr<Logger>; |
| 130 | using LoggerRequest = mojo::InterfaceRequest<Logger>; |
| 131 | |
| 132 | } // namespace mojom |
| 133 | } // namespace sample |
| 134 | ``` |
| 135 | |
| 136 | Makes sense. Let's take a closer look at those type aliases at the end. |
| 137 | |
| 138 | ### InterfacePtr and InterfaceRequest |
| 139 | |
| 140 | You will notice the type aliases for `LoggerPtr` and |
| 141 | `LoggerRequest` are using two of the most fundamental template types in the C++ |
| 142 | bindings library: **`InterfacePtr<T>`** and **`InterfaceRequest<T>`**. |
| 143 | |
| 144 | In the world of Mojo bindings libraries these are effectively strongly-typed |
| 145 | message pipe endpoints. If an `InterfacePtr<T>` is bound to a message pipe |
| 146 | endpoint, it can be dereferenced to make calls on an opaque `T` interface. These |
| 147 | calls immediately serialize their arguments (using generated code) and write a |
| 148 | corresponding message to the pipe. |
| 149 | |
| 150 | An `InterfaceRequest<T>` is essentially just a typed container to hold the other |
| 151 | end of an `InterfacePtr<T>`'s pipe -- the receiving end -- until it can be |
| 152 | routed to some implementation which will **bind** it. The `InterfaceRequest<T>` |
| 153 | doesn't actually *do* anything other than hold onto a pipe endpoint and carry |
| 154 | useful compile-time type information. |
| 155 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 156 |  |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 157 | |
| 158 | So how do we create a strongly-typed message pipe? |
| 159 | |
| 160 | ### Creating Interface Pipes |
| 161 | |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 162 | One way to do this is by manually creating a pipe and wrapping each end with a |
| 163 | strongly-typed object: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 164 | |
| 165 | ``` cpp |
| 166 | #include "sample/logger.mojom.h" |
| 167 | |
| 168 | mojo::MessagePipe pipe; |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 169 | sample::mojom::LoggerPtr logger( |
| 170 | sample::mojom::LoggerPtrInfo(std::move(pipe.handle0), 0)); |
| 171 | sample::mojom::LoggerRequest request(std::move(pipe.handle1)); |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 172 | ``` |
| 173 | |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 174 | That's pretty verbose, but the C++ Bindings library provides a more convenient |
| 175 | way to accomplish the same thing. [interface_request.h](https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/interface_request.h) |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 176 | defines a `MakeRequest` function: |
| 177 | |
| 178 | ``` cpp |
| 179 | sample::mojom::LoggerPtr logger; |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 180 | auto request = mojo::MakeRequest(&logger); |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 181 | ``` |
| 182 | |
Ken Rockot | f4d8a94 | 2017-05-13 00:10:37 | [diff] [blame] | 183 | This second snippet is equivalent to the first one. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 184 | |
| 185 | *** note |
| 186 | **NOTE:** In the first example above you may notice usage of the `LoggerPtrInfo` |
| 187 | type, which is a generated alias for `mojo::InterfacePtrInfo<Logger>`. This is |
| 188 | similar to an `InterfaceRequest<T>` in that it merely holds onto a pipe handle |
| 189 | and cannot actually read or write messages on the pipe. Both this type and |
Sam McNally | d482b4b | 2017-07-17 03:45:03 | [diff] [blame] | 190 | `InterfaceRequest<T>` are safe to move freely from sequence to sequence, whereas |
| 191 | a bound `InterfacePtr<T>` is bound to a single sequence. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 192 | |
| 193 | An `InterfacePtr<T>` may be unbound by calling its `PassInterface()` method, |
| 194 | which returns a new `InterfacePtrInfo<T>`. Conversely, an `InterfacePtr<T>` may |
| 195 | bind (and thus take ownership of) an `InterfacePtrInfo<T>` so that interface |
| 196 | calls can be made on the pipe. |
| 197 | |
Sam McNally | d482b4b | 2017-07-17 03:45:03 | [diff] [blame] | 198 | The sequence-bound nature of `InterfacePtr<T>` is necessary to support safe |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 199 | dispatch of its [message responses](#Receiving-Responses) and |
| 200 | [connection error notifications](#Connection-Errors). |
| 201 | *** |
| 202 | |
| 203 | Once the `LoggerPtr` is bound we can immediately begin calling `Logger` |
| 204 | interface methods on it, which will immediately write messages into the pipe. |
| 205 | These messages will stay queued on the receiving end of the pipe until someone |
| 206 | binds to it and starts reading them. |
| 207 | |
| 208 | ``` cpp |
| 209 | logger->Log("Hello!"); |
| 210 | ``` |
| 211 | |
| 212 | This actually writes a `Log` message to the pipe. |
| 213 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 214 |  |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 215 | |
| 216 | But as mentioned above, `InterfaceRequest` *doesn't actually do anything*, so |
| 217 | that message will just sit on the pipe forever. We need a way to read messages |
| 218 | off the other end of the pipe and dispatch them. We have to |
| 219 | **bind the interface request**. |
| 220 | |
| 221 | ### Binding an Interface Request |
| 222 | |
| 223 | There are many different helper classes in the bindings library for binding the |
| 224 | receiving end of a message pipe. The most primitive among them is the aptly |
| 225 | named `mojo::Binding<T>`. A `mojo::Binding<T>` bridges an implementation of `T` |
| 226 | with a single bound message pipe endpoint (via a `mojo::InterfaceRequest<T>`), |
| 227 | which it continuously watches for readability. |
| 228 | |
| 229 | Any time the bound pipe becomes readable, the `Binding` will schedule a task to |
| 230 | read, deserialize (using generated code), and dispatch all available messages to |
| 231 | the bound `T` implementation. Below is a sample implementation of the `Logger` |
| 232 | interface. Notice that the implementation itself owns a `mojo::Binding`. This is |
| 233 | a common pattern, since a bound implementation must outlive any `mojo::Binding` |
| 234 | which binds it. |
| 235 | |
| 236 | ``` cpp |
| 237 | #include "base/logging.h" |
| 238 | #include "base/macros.h" |
| 239 | #include "sample/logger.mojom.h" |
| 240 | |
| 241 | class LoggerImpl : public sample::mojom::Logger { |
| 242 | public: |
| 243 | // NOTE: A common pattern for interface implementations which have one |
| 244 | // instance per client is to take an InterfaceRequest in the constructor. |
| 245 | |
| 246 | explicit LoggerImpl(sample::mojom::LoggerRequest request) |
| 247 | : binding_(this, std::move(request)) {} |
| 248 | ~Logger() override {} |
| 249 | |
| 250 | // sample::mojom::Logger: |
| 251 | void Log(const std::string& message) override { |
| 252 | LOG(ERROR) << "[Logger] " << message; |
| 253 | } |
| 254 | |
| 255 | private: |
| 256 | mojo::Binding<sample::mojom::Logger> binding_; |
| 257 | |
| 258 | DISALLOW_COPY_AND_ASSIGN(LoggerImpl); |
| 259 | }; |
| 260 | ``` |
| 261 | |
| 262 | Now we can construct a `LoggerImpl` over our pending `LoggerRequest`, and the |
| 263 | previously queued `Log` message will be dispatched ASAP on the `LoggerImpl`'s |
Sam McNally | d482b4b | 2017-07-17 03:45:03 | [diff] [blame] | 264 | sequence: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 265 | |
| 266 | ``` cpp |
| 267 | LoggerImpl impl(std::move(request)); |
| 268 | ``` |
| 269 | |
| 270 | The diagram below illustrates the following sequence of events, all set in |
| 271 | motion by the above line of code: |
| 272 | |
| 273 | 1. The `LoggerImpl` constructor is called, passing the `LoggerRequest` along |
| 274 | to the `Binding`. |
| 275 | 2. The `Binding` takes ownership of the `LoggerRequest`'s pipe endpoint and |
| 276 | begins watching it for readability. The pipe is readable immediately, so a |
| 277 | task is scheduled to read the pending `Log` message from the pipe ASAP. |
| 278 | 3. The `Log` message is read and deserialized, causing the `Binding` to invoke |
| 279 | the `Logger::Log` implementation on its bound `LoggerImpl`. |
| 280 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 281 |  |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 282 | |
| 283 | As a result, our implementation will eventually log the client's `"Hello!"` |
| 284 | message via `LOG(ERROR)`. |
| 285 | |
| 286 | *** note |
| 287 | **NOTE:** Messages will only be read and dispatched from a pipe as long as the |
| 288 | object which binds it (*i.e.* the `mojo::Binding` in the above example) remains |
| 289 | alive. |
| 290 | *** |
| 291 | |
| 292 | ### Receiving Responses |
| 293 | |
| 294 | Some Mojom interface methods expect a response. Suppose we modify our `Logger` |
| 295 | interface so that the last logged line can be queried like so: |
| 296 | |
| 297 | ``` cpp |
| 298 | module sample.mojom; |
| 299 | |
| 300 | interface Logger { |
| 301 | Log(string message); |
| 302 | GetTail() => (string message); |
| 303 | }; |
| 304 | ``` |
| 305 | |
| 306 | The generated C++ interface will now look like: |
| 307 | |
| 308 | ``` cpp |
| 309 | namespace sample { |
| 310 | namespace mojom { |
| 311 | |
| 312 | class Logger { |
| 313 | public: |
| 314 | virtual ~Logger() {} |
| 315 | |
| 316 | virtual void Log(const std::string& message) = 0; |
| 317 | |
David 'Digit' Turner | 743836f8 | 2017-10-27 13:04:15 | [diff] [blame] | 318 | using GetTailCallback = base::OnceCallback<void(const std::string& message)>; |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 319 | |
David 'Digit' Turner | 743836f8 | 2017-10-27 13:04:15 | [diff] [blame] | 320 | virtual void GetTail(GetTailCallback callback) = 0; |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | } // namespace mojom |
| 324 | } // namespace sample |
| 325 | ``` |
| 326 | |
| 327 | As before, both clients and implementations of this interface use the same |
| 328 | signature for the `GetTail` method: implementations use the `callback` argument |
| 329 | to *respond* to the request, while clients pass a `callback` argument to |
Andrew Moylan | 9216d377 | 2018-12-19 04:52:29 | [diff] [blame] | 330 | asynchronously `receive` the response. A client's `callback` runs on the same |
| 331 | sequence on which they invoked `GetTail` (the sequence to which their `logger` |
| 332 | is bound). Here's an updated implementation: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 333 | |
| 334 | ```cpp |
| 335 | class LoggerImpl : public sample::mojom::Logger { |
| 336 | public: |
| 337 | // NOTE: A common pattern for interface implementations which have one |
| 338 | // instance per client is to take an InterfaceRequest in the constructor. |
| 339 | |
| 340 | explicit LoggerImpl(sample::mojom::LoggerRequest request) |
| 341 | : binding_(this, std::move(request)) {} |
| 342 | ~Logger() override {} |
| 343 | |
| 344 | // sample::mojom::Logger: |
| 345 | void Log(const std::string& message) override { |
| 346 | LOG(ERROR) << "[Logger] " << message; |
| 347 | lines_.push_back(message); |
| 348 | } |
| 349 | |
David 'Digit' Turner | 743836f8 | 2017-10-27 13:04:15 | [diff] [blame] | 350 | void GetTail(GetTailCallback callback) override { |
| 351 | std::move(callback).Run(lines_.back()); |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | private: |
| 355 | mojo::Binding<sample::mojom::Logger> binding_; |
| 356 | std::vector<std::string> lines_; |
| 357 | |
| 358 | DISALLOW_COPY_AND_ASSIGN(LoggerImpl); |
| 359 | }; |
| 360 | ``` |
| 361 | |
| 362 | And an updated client call: |
| 363 | |
| 364 | ``` cpp |
| 365 | void OnGetTail(const std::string& message) { |
| 366 | LOG(ERROR) << "Tail was: " << message; |
| 367 | } |
| 368 | |
David 'Digit' Turner | 743836f8 | 2017-10-27 13:04:15 | [diff] [blame] | 369 | logger->GetTail(base::BindOnce(&OnGetTail)); |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 370 | ``` |
| 371 | |
| 372 | Behind the scenes, the implementation-side callback is actually serializing the |
| 373 | response arguments and writing them onto the pipe for delivery back to the |
| 374 | client. Meanwhile the client-side callback is invoked by some internal logic |
| 375 | which watches the pipe for an incoming response message, reads and deserializes |
| 376 | it once it arrives, and then invokes the callback with the deserialized |
| 377 | parameters. |
| 378 | |
| 379 | ### Connection Errors |
| 380 | |
Yuzhu Shen | 92e791aa | 2017-06-20 20:39:31 | [diff] [blame] | 381 | If a pipe is disconnected, both endpoints will be able to observe the connection |
| 382 | error (unless the disconnection is caused by closing/destroying an endpoint, in |
| 383 | which case that endpoint won't get such a notification). If there are remaining |
| 384 | incoming messages for an endpoint on disconnection, the connection error won't |
| 385 | be triggered until the messages are drained. |
| 386 | |
| 387 | Pipe disconnecition may be caused by: |
| 388 | * Mojo system-level causes: process terminated, resource exhausted, etc. |
| 389 | * The bindings close the pipe due to a validation error when processing a |
| 390 | received message. |
| 391 | * The peer endpoint is closed. For example, the remote side is a bound |
| 392 | `mojo::InterfacePtr<T>` and it is destroyed. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 393 | |
| 394 | Regardless of the underlying cause, when a connection error is encountered on |
| 395 | a binding endpoint, that endpoint's **connection error handler** (if set) is |
| 396 | invoked. This handler is a simple `base::Closure` and may only be invoked |
| 397 | *once* as long as the endpoint is bound to the same pipe. Typically clients and |
| 398 | implementations use this handler to do some kind of cleanup or -- particuarly if |
| 399 | the error was unexpected -- create a new pipe and attempt to establish a new |
| 400 | connection with it. |
| 401 | |
| 402 | All message pipe-binding C++ objects (*e.g.*, `mojo::Binding<T>`, |
| 403 | `mojo::InterfacePtr<T>`, *etc.*) support setting their connection error handler |
| 404 | via a `set_connection_error_handler` method. |
| 405 | |
| 406 | We can set up another end-to-end `Logger` example to demonstrate error handler |
| 407 | invocation: |
| 408 | |
| 409 | ``` cpp |
| 410 | sample::mojom::LoggerPtr logger; |
| 411 | LoggerImpl impl(mojo::MakeRequest(&logger)); |
jameswest | 14ae013 | 2017-06-12 22:52:00 | [diff] [blame] | 412 | impl.set_connection_error_handler(base::BindOnce([] { LOG(ERROR) << "Bye."; })); |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 413 | logger->Log("OK cool"); |
| 414 | logger.reset(); // Closes the client end. |
| 415 | ``` |
| 416 | |
| 417 | As long as `impl` stays alive here, it will eventually receive the `Log` message |
| 418 | followed immediately by an invocation of the bound callback which outputs |
| 419 | `"Bye."`. Like all other bindings callbacks, a connection error handler will |
| 420 | **never** be invoked once its corresponding binding object has been destroyed. |
| 421 | |
| 422 | In fact, suppose instead that `LoggerImpl` had set up the following error |
| 423 | handler within its constructor: |
| 424 | |
| 425 | ``` cpp |
| 426 | LoggerImpl::LoggerImpl(sample::mojom::LoggerRequest request) |
| 427 | : binding_(this, std::move(request)) { |
| 428 | binding_.set_connection_error_handler( |
jameswest | 14ae013 | 2017-06-12 22:52:00 | [diff] [blame] | 429 | base::BindOnce(&LoggerImpl::OnError, base::Unretained(this))); |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | void LoggerImpl::OnError() { |
| 433 | LOG(ERROR) << "Client disconnected! Purging log lines."; |
| 434 | lines_.clear(); |
| 435 | } |
| 436 | ``` |
| 437 | |
| 438 | The use of `base::Unretained` is *safe* because the error handler will never be |
| 439 | invoked beyond the lifetime of `binding_`, and `this` owns `binding_`. |
| 440 | |
Yuzhu Shen | 7afd726 | 2017-11-16 22:30:26 | [diff] [blame] | 441 | ### A Note About Endpoint Lifetime and Callbacks |
| 442 | Once a `mojo::InterfacePtr<T>` is destroyed, it is guaranteed that pending |
| 443 | callbacks as well as the connection error handler (if registered) won't be |
| 444 | called. |
| 445 | |
| 446 | Once a `mojo::Binding<T>` is destroyed, it is guaranteed that no more method |
| 447 | calls are dispatched to the implementation and the connection error handler (if |
| 448 | registered) won't be called. |
| 449 | |
John Abd-El-Malek | a915485 | 2017-12-21 23:39:48 | [diff] [blame] | 450 | ### Best practices for dealing with process crashes and callbacks |
| 451 | A common situation when calling mojo interface methods that take a callback is |
| 452 | that the caller wants to know if the other endpoint is torn down (e.g. because |
| 453 | of a crash). In that case, the consumer usually wants to know if the response |
| 454 | callback won't be run. There are different solutions for this problem, depending |
| 455 | on how the `InterfacePtr<T>` is held: |
| 456 | 1. The consumer owns the `InterfacePtr<T>`: `set_connection_error_handler` |
| 457 | should be used. |
| 458 | 2. The consumer doesn't own the `InterfacePtr<T>`: there are two helpers |
| 459 | depending on the behavior that the caller wants. If the caller wants to |
| 460 | ensure that an error handler is run, then |
| 461 | [**`mojo::WrapCallbackWithDropHandler`**](https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/callback_helpers.h?l=46) |
| 462 | should be used. If the caller wants the callback to always be run, then |
| 463 | [**`mojo::WrapCallbackWithDefaultInvokeIfNotRun`**](https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/callback_helpers.h?l=40) |
| 464 | helper should be used. With both of these helpers, usual callback care should |
| 465 | be followed to ensure that the callbacks don't run after the consumer is |
| 466 | destructed (e.g. because the owner of the `InterfacePtr<T>` outlives the |
| 467 | consumer). This includes using |
| 468 | [**`base::WeakPtr`**](https://cs.chromium.org/chromium/src/base/memory/weak_ptr.h?l=5) |
| 469 | or |
| 470 | [**`base::RefCounted`**](https://cs.chromium.org/chromium/src/base/memory/ref_counted.h?l=246). |
| 471 | It should also be noted that with these helpers, the callbacks could be run |
| 472 | synchronously while the InterfacePtr<T> is reset or destroyed. |
| 473 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 474 | ### A Note About Ordering |
| 475 | |
| 476 | As mentioned in the previous section, closing one end of a pipe will eventually |
| 477 | trigger a connection error on the other end. However it's important to note that |
| 478 | this event is itself ordered with respect to any other event (*e.g.* writing a |
| 479 | message) on the pipe. |
| 480 | |
| 481 | This means that it's safe to write something contrived like: |
| 482 | |
| 483 | ``` cpp |
| 484 | void GoBindALogger(sample::mojom::LoggerRequest request) { |
| 485 | LoggerImpl impl(std::move(request)); |
| 486 | base::RunLoop loop; |
| 487 | impl.set_connection_error_handler(loop.QuitClosure()); |
| 488 | loop.Run(); |
| 489 | } |
| 490 | |
| 491 | void LogSomething() { |
| 492 | sample::mojom::LoggerPtr logger; |
| 493 | bg_thread->task_runner()->PostTask( |
| 494 | FROM_HERE, base::BindOnce(&GoBindALogger, mojo::MakeRequest(&logger))); |
| 495 | logger->Log("OK Computer"); |
| 496 | } |
| 497 | ``` |
| 498 | |
| 499 | When `logger` goes out of scope it immediately closes its end of the message |
| 500 | pipe, but the impl-side won't notice this until it receives the sent `Log` |
| 501 | message. Thus the `impl` above will first log our message and *then* see a |
| 502 | connection error and break out of the run loop. |
| 503 | |
Sasha Bermeister | 995adc6 | 2017-12-07 02:36:43 | [diff] [blame] | 504 | ## Types |
| 505 | |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 506 | ### Enums |
| 507 | |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 508 | [Mojom enums](/mojo/public/tools/bindings/README.md#Enumeration-Types) translate |
| 509 | directly to equivalent strongly-typed C++11 enum classes with `int32_t` as the |
| 510 | underlying type. The typename and value names are identical between Mojom and |
| 511 | C++. Mojo also always defines a special enumerator `kMaxValue` that shares the |
| 512 | value of the highest enumerator: this makes it easy to record Mojo enums in |
| 513 | histograms and interoperate with legacy IPC. |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 514 | |
| 515 | For example, consider the following Mojom definition: |
| 516 | |
| 517 | ```cpp |
| 518 | module business.mojom; |
| 519 | |
| 520 | enum Department { |
| 521 | kEngineering, |
Andrew Moylan | 341cece7 | 2017-06-22 22:03:02 | [diff] [blame] | 522 | kMarketing, |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 523 | kSales, |
| 524 | }; |
| 525 | ``` |
| 526 | |
| 527 | This translates to the following C++ definition: |
| 528 | |
| 529 | ```cpp |
| 530 | namespace business { |
| 531 | namespace mojom { |
| 532 | |
| 533 | enum class Department : int32_t { |
| 534 | kEngineering, |
| 535 | kMarketing, |
| 536 | kSales, |
Daniel Cheng | cda1df5b | 2018-03-30 21:30:16 | [diff] [blame] | 537 | kMaxValue = kSales, |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 538 | }; |
| 539 | |
| 540 | } // namespace mojom |
| 541 | } // namespace business |
| 542 | ``` |
| 543 | |
| 544 | ### Structs |
| 545 | |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 546 | [Mojom structs](mojo/public/tools/bindings/README.md#Structs) can be used to |
| 547 | define logical groupings of fields into a new composite type. Every Mojom struct |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 548 | elicits the generation of an identically named, representative C++ class, with |
| 549 | identically named public fields of corresponding C++ types, and several helpful |
| 550 | public methods. |
| 551 | |
| 552 | For example, consider the following Mojom struct: |
| 553 | |
| 554 | ```cpp |
| 555 | module business.mojom; |
| 556 | |
| 557 | struct Employee { |
| 558 | int64 id; |
| 559 | string username; |
| 560 | Department department; |
| 561 | }; |
| 562 | ``` |
| 563 | |
| 564 | This would generate a C++ class like so: |
| 565 | |
| 566 | ```cpp |
| 567 | namespace business { |
| 568 | namespace mojom { |
| 569 | |
| 570 | class Employee; |
| 571 | |
| 572 | using EmployeePtr = mojo::StructPtr<Employee>; |
| 573 | |
| 574 | class Employee { |
| 575 | public: |
| 576 | // Default constructor - applies default values, potentially ones specified |
| 577 | // explicitly within the Mojom. |
| 578 | Employee(); |
| 579 | |
| 580 | // Value constructor - an explicit argument for every field in the struct, in |
| 581 | // lexical Mojom definition order. |
| 582 | Employee(int64_t id, const std::string& username, Department department); |
| 583 | |
| 584 | // Creates a new copy of this struct value |
| 585 | EmployeePtr Clone(); |
| 586 | |
| 587 | // Tests for equality with another struct value of the same type. |
| 588 | bool Equals(const Employee& other); |
| 589 | |
| 590 | // Equivalent public fields with names identical to the Mojom. |
| 591 | int64_t id; |
| 592 | std::string username; |
| 593 | Department department; |
| 594 | }; |
| 595 | |
| 596 | } // namespace mojom |
| 597 | } // namespace business |
| 598 | ``` |
| 599 | |
| 600 | Note when used as a message parameter or as a field within another Mojom struct, |
| 601 | a `struct` type is wrapped by the move-only `mojo::StructPtr` helper, which is |
| 602 | roughly equivalent to a `std::unique_ptr` with some additional utility methods. |
| 603 | This allows struct values to be nullable and struct types to be potentially |
| 604 | self-referential. |
| 605 | |
| 606 | Every genereated struct class has a static `New()` method which returns a new |
| 607 | `mojo::StructPtr<T>` wrapping a new instance of the class constructed by |
| 608 | forwarding the arguments from `New`. For example: |
| 609 | |
| 610 | ```cpp |
| 611 | mojom::EmployeePtr e1 = mojom::Employee::New(); |
| 612 | e1->id = 42; |
| 613 | e1->username = "mojo"; |
| 614 | e1->department = mojom::Department::kEngineering; |
| 615 | ``` |
| 616 | |
| 617 | is equivalent to |
| 618 | |
| 619 | ```cpp |
| 620 | auto e1 = mojom::Employee::New(42, "mojo", mojom::Department::kEngineering); |
| 621 | ``` |
| 622 | |
| 623 | Now if we define an interface like: |
| 624 | |
| 625 | ```cpp |
| 626 | interface EmployeeManager { |
| 627 | AddEmployee(Employee e); |
| 628 | }; |
| 629 | ``` |
| 630 | |
| 631 | We'll get this C++ interface to implement: |
| 632 | |
| 633 | ```cpp |
| 634 | class EmployeeManager { |
| 635 | public: |
| 636 | virtual ~EmployeManager() {} |
| 637 | |
| 638 | virtual void AddEmployee(EmployeePtr e) = 0; |
| 639 | }; |
| 640 | ``` |
| 641 | |
| 642 | And we can send this message from C++ code as follows: |
| 643 | |
| 644 | ```cpp |
| 645 | mojom::EmployeManagerPtr manager = ...; |
| 646 | manager->AddEmployee( |
| 647 | Employee::New(42, "mojo", mojom::Department::kEngineering)); |
| 648 | |
| 649 | // or |
| 650 | auto e = Employee::New(42, "mojo", mojom::Department::kEngineering); |
| 651 | manager->AddEmployee(std::move(e)); |
| 652 | ``` |
| 653 | |
| 654 | ### Unions |
| 655 | |
| 656 | Similarly to [structs](#Structs), tagged unions generate an identically named, |
| 657 | representative C++ class which is typically wrapped in a `mojo::StructPtr<T>`. |
| 658 | |
| 659 | Unlike structs, all generated union fields are private and must be retrieved and |
Oksana Zhuravlova | a4da21f | 2019-03-20 20:41:58 | [diff] [blame] | 660 | manipulated using accessors. A field `foo` is accessible by `get_foo()` and |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 661 | settable by `set_foo()`. There is also a boolean `is_foo()` for each field which |
| 662 | indicates whether the union is currently taking on the value of field `foo` in |
| 663 | exclusion to all other union fields. |
| 664 | |
| 665 | Finally, every generated union class also has a nested `Tag` enum class which |
| 666 | enumerates all of the named union fields. A Mojom union value's current type can |
| 667 | be determined by calling the `which()` method which returns a `Tag`. |
| 668 | |
| 669 | For example, consider the following Mojom definitions: |
| 670 | |
| 671 | ```cpp |
| 672 | union Value { |
| 673 | int64 int_value; |
Tom McKee | 1a5032f | 2018-08-02 17:14:55 | [diff] [blame] | 674 | float float_value; |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 675 | string string_value; |
| 676 | }; |
| 677 | |
| 678 | interface Dictionary { |
| 679 | AddValue(string key, Value value); |
| 680 | }; |
| 681 | ``` |
| 682 | |
Tom McKee | 1a5032f | 2018-08-02 17:14:55 | [diff] [blame] | 683 | This generates the following C++ interface: |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 684 | |
| 685 | ```cpp |
| 686 | class Value { |
| 687 | public: |
Tom McKee | 1a5032f | 2018-08-02 17:14:55 | [diff] [blame] | 688 | ~Value() {} |
| 689 | }; |
| 690 | |
| 691 | class Dictionary { |
| 692 | public: |
| 693 | virtual ~Dictionary() {} |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 694 | |
| 695 | virtual void AddValue(const std::string& key, ValuePtr value) = 0; |
| 696 | }; |
| 697 | ``` |
| 698 | |
| 699 | And we can use it like so: |
| 700 | |
| 701 | ```cpp |
| 702 | ValuePtr value = Value::New(); |
| 703 | value->set_int_value(42); |
| 704 | CHECK(value->is_int_value()); |
| 705 | CHECK_EQ(value->which(), Value::Tag::INT_VALUE); |
| 706 | |
| 707 | value->set_float_value(42); |
| 708 | CHECK(value->is_float_value()); |
| 709 | CHECK_EQ(value->which(), Value::Tag::FLOAT_VALUE); |
| 710 | |
| 711 | value->set_string_value("bananas"); |
| 712 | CHECK(value->is_string_value()); |
| 713 | CHECK_EQ(value->which(), Value::Tag::STRING_VALUE); |
| 714 | ``` |
| 715 | |
| 716 | Finally, note that if a union value is not currently occupied by a given field, |
| 717 | attempts to access that field will DCHECK: |
| 718 | |
| 719 | ```cpp |
| 720 | ValuePtr value = Value::New(); |
| 721 | value->set_int_value(42); |
| 722 | LOG(INFO) << "Value is " << value->string_value(); // DCHECK! |
| 723 | ``` |
| 724 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 725 | ### Sending Interfaces Over Interfaces |
| 726 | |
Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 727 | We know how to create interface pipes and use their Ptr and Request endpoints |
| 728 | in some interesting ways. This still doesn't add up to interesting IPC! The |
| 729 | bread and butter of Mojo IPC is the ability to transfer interface endpoints |
| 730 | across other interfaces, so let's take a look at how to accomplish that. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 731 | |
| 732 | #### Sending Interface Requests |
| 733 | |
| 734 | Consider a new example Mojom in `//sample/db.mojom`: |
| 735 | |
| 736 | ``` cpp |
| 737 | module db.mojom; |
| 738 | |
| 739 | interface Table { |
| 740 | void AddRow(int32 key, string data); |
| 741 | }; |
| 742 | |
| 743 | interface Database { |
| 744 | AddTable(Table& table); |
| 745 | }; |
| 746 | ``` |
| 747 | |
| 748 | As noted in the |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 749 | [Mojom IDL documentation](/mojo/public/tools/bindings/README.md#Primitive-Types), |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 750 | the `Table&` syntax denotes a `Table` interface request. This corresponds |
| 751 | precisely to the `InterfaceRequest<T>` type discussed in the sections above, and |
| 752 | in fact the generated code for these interfaces is approximately: |
| 753 | |
| 754 | ``` cpp |
| 755 | namespace db { |
| 756 | namespace mojom { |
| 757 | |
| 758 | class Table { |
| 759 | public: |
| 760 | virtual ~Table() {} |
| 761 | |
| 762 | virtual void AddRow(int32_t key, const std::string& data) = 0; |
| 763 | } |
| 764 | |
| 765 | using TablePtr = mojo::InterfacePtr<Table>; |
| 766 | using TableRequest = mojo::InterfaceRequest<Table>; |
| 767 | |
| 768 | class Database { |
| 769 | public: |
| 770 | virtual ~Database() {} |
| 771 | |
| 772 | virtual void AddTable(TableRequest table); |
| 773 | }; |
| 774 | |
| 775 | using DatabasePtr = mojo::InterfacePtr<Database>; |
| 776 | using DatabaseRequest = mojo::InterfaceRequest<Database>; |
| 777 | |
| 778 | } // namespace mojom |
| 779 | } // namespace db |
| 780 | ``` |
| 781 | |
| 782 | We can put this all together now with an implementation of `Table` and |
| 783 | `Database`: |
| 784 | |
| 785 | ``` cpp |
| 786 | #include "sample/db.mojom.h" |
| 787 | |
| 788 | class TableImpl : public db::mojom:Table { |
| 789 | public: |
| 790 | explicit TableImpl(db::mojom::TableRequest request) |
| 791 | : binding_(this, std::move(request)) {} |
| 792 | ~TableImpl() override {} |
| 793 | |
| 794 | // db::mojom::Table: |
| 795 | void AddRow(int32_t key, const std::string& data) override { |
| 796 | rows_.insert({key, data}); |
| 797 | } |
| 798 | |
| 799 | private: |
| 800 | mojo::Binding<db::mojom::Table> binding_; |
| 801 | std::map<int32_t, std::string> rows_; |
| 802 | }; |
| 803 | |
| 804 | class DatabaseImpl : public db::mojom::Database { |
| 805 | public: |
| 806 | explicit DatabaseImpl(db::mojom::DatabaseRequest request) |
| 807 | : binding_(this, std::move(request)) {} |
| 808 | ~DatabaseImpl() override {} |
| 809 | |
| 810 | // db::mojom::Database: |
| 811 | void AddTable(db::mojom::TableRequest table) { |
Jeremy Roman | cf9ae2f | 2017-08-24 17:06:37 | [diff] [blame] | 812 | tables_.emplace_back(std::make_unique<TableImpl>(std::move(table))); |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | private: |
| 816 | mojo::Binding<db::mojom::Database> binding_; |
| 817 | std::vector<std::unique_ptr<TableImpl>> tables_; |
| 818 | }; |
| 819 | ``` |
| 820 | |
| 821 | Pretty straightforward. The `Table&` Mojom paramter to `AddTable` translates to |
| 822 | a C++ `db::mojom::TableRequest`, aliased from |
| 823 | `mojo::InterfaceRequest<db::mojom::Table>`, which we know is just a |
| 824 | strongly-typed message pipe handle. When `DatabaseImpl` gets an `AddTable` call, |
| 825 | it constructs a new `TableImpl` and binds it to the received `TableRequest`. |
| 826 | |
| 827 | Let's see how this can be used. |
| 828 | |
| 829 | ``` cpp |
| 830 | db::mojom::DatabasePtr database; |
| 831 | DatabaseImpl db_impl(mojo::MakeRequest(&database)); |
| 832 | |
| 833 | db::mojom::TablePtr table1, table2; |
| 834 | database->AddTable(mojo::MakeRequest(&table1)); |
| 835 | database->AddTable(mojo::MakeRequest(&table2)); |
| 836 | |
| 837 | table1->AddRow(1, "hiiiiiiii"); |
| 838 | table2->AddRow(2, "heyyyyyy"); |
| 839 | ``` |
| 840 | |
| 841 | Notice that we can again start using the new `Table` pipes immediately, even |
| 842 | while their `TableRequest` endpoints are still in transit. |
| 843 | |
| 844 | #### Sending InterfacePtrs |
| 845 | |
| 846 | Of course we can also send `InterfacePtr`s: |
| 847 | |
| 848 | ``` cpp |
| 849 | interface TableListener { |
| 850 | OnRowAdded(int32 key, string data); |
| 851 | }; |
| 852 | |
| 853 | interface Table { |
| 854 | AddRow(int32 key, string data); |
| 855 | |
| 856 | AddListener(TableListener listener); |
| 857 | }; |
| 858 | ``` |
| 859 | |
| 860 | This would generate a `Table::AddListener` signature like so: |
| 861 | |
| 862 | ``` cpp |
| 863 | virtual void AddListener(TableListenerPtr listener) = 0; |
| 864 | ``` |
| 865 | |
| 866 | and this could be used like so: |
| 867 | |
| 868 | ``` cpp |
| 869 | db::mojom::TableListenerPtr listener; |
| 870 | TableListenerImpl impl(mojo::MakeRequest(&listener)); |
| 871 | table->AddListener(std::move(listener)); |
| 872 | ``` |
| 873 | |
| 874 | ## Other Interface Binding Types |
| 875 | |
| 876 | The [Interfaces](#Interfaces) section above covers basic usage of the most |
| 877 | common bindings object types: `InterfacePtr`, `InterfaceRequest`, and `Binding`. |
| 878 | While these types are probably the most commonly used in practice, there are |
| 879 | several other ways of binding both client- and implementation-side interface |
| 880 | pipes. |
| 881 | |
| 882 | ### Strong Bindings |
| 883 | |
| 884 | A **strong binding** exists as a standalone object which owns its interface |
| 885 | implementation and automatically cleans itself up when its bound interface |
| 886 | endpoint detects an error. The |
Austin Tankiang | 670438aa | 2018-02-05 00:18:36 | [diff] [blame] | 887 | [**`MakeStrongBinding`**](https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/strong_binding.h) |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 888 | function is used to create such a binding. |
| 889 | . |
| 890 | |
| 891 | ``` cpp |
| 892 | class LoggerImpl : public sample::mojom::Logger { |
| 893 | public: |
| 894 | LoggerImpl() {} |
| 895 | ~LoggerImpl() override {} |
| 896 | |
| 897 | // sample::mojom::Logger: |
| 898 | void Log(const std::string& message) override { |
| 899 | LOG(ERROR) << "[Logger] " << message; |
| 900 | } |
| 901 | |
| 902 | private: |
| 903 | // NOTE: This doesn't own any Binding object! |
| 904 | }; |
| 905 | |
| 906 | db::mojom::LoggerPtr logger; |
Jeremy Roman | cf9ae2f | 2017-08-24 17:06:37 | [diff] [blame] | 907 | mojo::MakeStrongBinding(std::make_unique<LoggerImpl>(), |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 908 | mojo::MakeRequest(&logger)); |
| 909 | |
| 910 | logger->Log("NOM NOM NOM MESSAGES"); |
| 911 | ``` |
| 912 | |
| 913 | Now as long as `logger` remains open somewhere in the system, the bound |
scottmg | 6613920 | 2017-05-04 18:56:35 | [diff] [blame] | 914 | `LoggerImpl` on the other end will remain alive. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 915 | |
| 916 | ### Binding Sets |
| 917 | |
| 918 | Sometimes it's useful to share a single implementation instance with multiple |
| 919 | clients. [**`BindingSet`**](https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/binding_set.h) |
| 920 | makes this easy. Consider the Mojom: |
| 921 | |
| 922 | ``` cpp |
| 923 | module system.mojom; |
| 924 | |
| 925 | interface Logger { |
| 926 | Log(string message); |
| 927 | }; |
| 928 | |
| 929 | interface LoggerProvider { |
| 930 | GetLogger(Logger& logger); |
| 931 | }; |
| 932 | ``` |
| 933 | |
| 934 | We can use `BindingSet` to bind multiple `Logger` requests to a single |
| 935 | implementation instance: |
| 936 | |
| 937 | ``` cpp |
| 938 | class LogManager : public system::mojom::LoggerProvider, |
| 939 | public system::mojom::Logger { |
| 940 | public: |
| 941 | explicit LogManager(system::mojom::LoggerProviderRequest request) |
| 942 | : provider_binding_(this, std::move(request)) {} |
| 943 | ~LogManager() {} |
| 944 | |
| 945 | // system::mojom::LoggerProvider: |
| 946 | void GetLogger(LoggerRequest request) override { |
| 947 | logger_bindings_.AddBinding(this, std::move(request)); |
| 948 | } |
| 949 | |
| 950 | // system::mojom::Logger: |
| 951 | void Log(const std::string& message) override { |
| 952 | LOG(ERROR) << "[Logger] " << message; |
| 953 | } |
| 954 | |
| 955 | private: |
| 956 | mojo::Binding<system::mojom::LoggerProvider> provider_binding_; |
| 957 | mojo::BindingSet<system::mojom::Logger> logger_bindings_; |
| 958 | }; |
| 959 | |
| 960 | ``` |
| 961 | |
| 962 | |
| 963 | ### InterfacePtr Sets |
| 964 | |
| 965 | Similar to the `BindingSet` above, sometimes it's useful to maintain a set of |
| 966 | `InterfacePtr`s for *e.g.* a set of clients observing some event. |
| 967 | [**`InterfacePtrSet`**](https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/interface_ptr_set.h) |
| 968 | is here to help. Take the Mojom: |
| 969 | |
| 970 | ``` cpp |
| 971 | module db.mojom; |
| 972 | |
| 973 | interface TableListener { |
| 974 | OnRowAdded(int32 key, string data); |
| 975 | }; |
| 976 | |
| 977 | interface Table { |
| 978 | AddRow(int32 key, string data); |
| 979 | AddListener(TableListener listener); |
| 980 | }; |
| 981 | ``` |
| 982 | |
| 983 | An implementation of `Table` might look something like like this: |
| 984 | |
| 985 | ``` cpp |
| 986 | class TableImpl : public db::mojom::Table { |
| 987 | public: |
| 988 | TableImpl() {} |
| 989 | ~TableImpl() override {} |
| 990 | |
| 991 | // db::mojom::Table: |
| 992 | void AddRow(int32_t key, const std::string& data) override { |
| 993 | rows_.insert({key, data}); |
| 994 | listeners_.ForEach([key, &data](db::mojom::TableListener* listener) { |
| 995 | listener->OnRowAdded(key, data); |
| 996 | }); |
| 997 | } |
| 998 | |
| 999 | void AddListener(db::mojom::TableListenerPtr listener) { |
| 1000 | listeners_.AddPtr(std::move(listener)); |
| 1001 | } |
| 1002 | |
| 1003 | private: |
| 1004 | mojo::InterfacePtrSet<db::mojom::Table> listeners_; |
| 1005 | std::map<int32_t, std::string> rows_; |
| 1006 | }; |
| 1007 | ``` |
| 1008 | |
| 1009 | ## Associated Interfaces |
| 1010 | |
Chase Phillips | 3f76e15 | 2018-07-18 20:45:26 | [diff] [blame] | 1011 | Associated interfaces are interfaces which: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1012 | |
Chase Phillips | 3f76e15 | 2018-07-18 20:45:26 | [diff] [blame] | 1013 | * enable running multiple interfaces over a single message pipe while |
| 1014 | preserving message ordering. |
| 1015 | * make it possible for the bindings to access a single message pipe from |
| 1016 | multiple sequences. |
| 1017 | |
| 1018 | ### Mojom |
| 1019 | |
| 1020 | A new keyword `associated` is introduced for interface pointer/request |
| 1021 | fields. For example: |
| 1022 | |
| 1023 | ``` cpp |
| 1024 | interface Bar {}; |
| 1025 | |
| 1026 | struct Qux { |
| 1027 | associated Bar bar3; |
| 1028 | }; |
| 1029 | |
| 1030 | interface Foo { |
| 1031 | // Uses associated interface pointer. |
| 1032 | SetBar(associated Bar bar1); |
| 1033 | // Uses associated interface request. |
| 1034 | GetBar(associated Bar& bar2); |
| 1035 | // Passes a struct with associated interface pointer. |
| 1036 | PassQux(Qux qux); |
| 1037 | // Uses associated interface pointer in callback. |
| 1038 | AsyncGetBar() => (associated Bar bar4); |
| 1039 | }; |
| 1040 | ``` |
| 1041 | |
| 1042 | It means the interface impl/client will communicate using the same |
| 1043 | message pipe over which the associated interface pointer/request is |
| 1044 | passed. |
| 1045 | |
| 1046 | ### Using associated interfaces in C++ |
| 1047 | |
| 1048 | When generating C++ bindings, the associated interface pointer of `Bar` is |
| 1049 | mapped to `BarAssociatedPtrInfo` (which is an alias of |
| 1050 | `mojo::AssociatedInterfacePtrInfo<Bar>`); associated interface request to |
| 1051 | `BarAssociatedRequest` (which is an alias of |
| 1052 | `mojo::AssociatedInterfaceRequest<Bar>`). |
| 1053 | |
| 1054 | ``` cpp |
| 1055 | // In mojom: |
| 1056 | interface Foo { |
| 1057 | ... |
| 1058 | SetBar(associated Bar bar1); |
| 1059 | GetBar(associated Bar& bar2); |
| 1060 | ... |
| 1061 | }; |
| 1062 | |
| 1063 | // In C++: |
| 1064 | class Foo { |
| 1065 | ... |
| 1066 | virtual void SetBar(BarAssociatedPtrInfo bar1) = 0; |
| 1067 | virtual void GetBar(BarAssociatedRequest bar2) = 0; |
| 1068 | ... |
| 1069 | }; |
| 1070 | ``` |
| 1071 | |
| 1072 | #### Passing associated interface requests |
| 1073 | |
| 1074 | Assume you have already got an `InterfacePtr<Foo> foo_ptr`, and you would like |
| 1075 | to call `GetBar()` on it. You can do: |
| 1076 | |
| 1077 | ``` cpp |
| 1078 | BarAssociatedPtrInfo bar_ptr_info; |
| 1079 | BarAssociatedRequest bar_request = MakeRequest(&bar_ptr_info); |
| 1080 | foo_ptr->GetBar(std::move(bar_request)); |
| 1081 | |
| 1082 | // BarAssociatedPtr is an alias of AssociatedInterfacePtr<Bar>. |
| 1083 | BarAssociatedPtr bar_ptr; |
| 1084 | bar_ptr.Bind(std::move(bar_ptr_info)); |
| 1085 | bar_ptr->DoSomething(); |
| 1086 | ``` |
| 1087 | |
| 1088 | First, the code creates an associated interface of type `Bar`. It looks very |
| 1089 | similar to what you would do to setup a non-associated interface. An |
| 1090 | important difference is that one of the two associated endpoints (either |
| 1091 | `bar_request` or `bar_ptr_info`) must be sent over another interface. That is |
| 1092 | how the interface is associated with an existing message pipe. |
| 1093 | |
| 1094 | It should be noted that you cannot call `bar_ptr->DoSomething()` before passing |
| 1095 | `bar_request`. This is required by the FIFO-ness guarantee: at the receiver |
| 1096 | side, when the message of `DoSomething` call arrives, we want to dispatch it to |
| 1097 | the corresponding `AssociatedBinding<Bar>` before processing any subsequent |
| 1098 | messages. If `bar_request` is in a subsequent message, message dispatching gets |
| 1099 | into a deadlock. On the other hand, as soon as `bar_request` is sent, `bar_ptr` |
| 1100 | is usable. There is no need to wait until `bar_request` is bound to an |
| 1101 | implementation at the remote side. |
| 1102 | |
| 1103 | A `MakeRequest` overload which takes an `AssociatedInterfacePtr` pointer |
| 1104 | (instead of an `AssociatedInterfacePtrInfo` pointer) is provided to make the |
| 1105 | code a little shorter. The following code achieves the same purpose: |
| 1106 | |
| 1107 | ``` cpp |
| 1108 | BarAssociatedPtr bar_ptr; |
| 1109 | foo_ptr->GetBar(MakeRequest(&bar_ptr)); |
| 1110 | bar_ptr->DoSomething(); |
| 1111 | ``` |
| 1112 | |
| 1113 | The implementation of `Foo` looks like this: |
| 1114 | |
| 1115 | ``` cpp |
| 1116 | class FooImpl : public Foo { |
| 1117 | ... |
| 1118 | void GetBar(BarAssociatedRequest bar2) override { |
| 1119 | bar_binding_.Bind(std::move(bar2)); |
| 1120 | ... |
| 1121 | } |
| 1122 | ... |
| 1123 | |
| 1124 | Binding<Foo> foo_binding_; |
| 1125 | AssociatedBinding<Bar> bar_binding_; |
| 1126 | }; |
| 1127 | ``` |
| 1128 | |
| 1129 | In this example, `bar_binding_`'s lifespan is tied to that of `FooImpl`. But you |
| 1130 | don't have to do that. You can, for example, pass `bar2` to another sequence to |
| 1131 | bind to an `AssociatedBinding<Bar>` there. |
| 1132 | |
| 1133 | When the underlying message pipe is disconnected (e.g., `foo_ptr` or |
| 1134 | `foo_binding_` is destroyed), all associated interface endpoints (e.g., |
| 1135 | `bar_ptr` and `bar_binding_`) will receive a connection error. |
| 1136 | |
| 1137 | #### Passing associated interface pointers |
| 1138 | |
| 1139 | Similarly, assume you have already got an `InterfacePtr<Foo> foo_ptr`, and you |
| 1140 | would like to call `SetBar()` on it. You can do: |
| 1141 | |
| 1142 | ``` cpp |
Kenichi Ishibashi | 89e8bc9 | 2018-08-07 00:37:48 | [diff] [blame] | 1143 | AssociatedBinding<Bar> bar_binding(some_bar_impl); |
Chase Phillips | 3f76e15 | 2018-07-18 20:45:26 | [diff] [blame] | 1144 | BarAssociatedPtrInfo bar_ptr_info; |
| 1145 | BarAssociatedRequest bar_request = MakeRequest(&bar_ptr_info); |
| 1146 | foo_ptr->SetBar(std::move(bar_ptr_info)); |
| 1147 | bar_binding.Bind(std::move(bar_request)); |
| 1148 | ``` |
| 1149 | |
| 1150 | The following code achieves the same purpose: |
| 1151 | |
| 1152 | ``` cpp |
Kenichi Ishibashi | 89e8bc9 | 2018-08-07 00:37:48 | [diff] [blame] | 1153 | AssociatedBinding<Bar> bar_binding(some_bar_impl); |
Chase Phillips | 3f76e15 | 2018-07-18 20:45:26 | [diff] [blame] | 1154 | BarAssociatedPtrInfo bar_ptr_info; |
| 1155 | bar_binding.Bind(&bar_ptr_info); |
| 1156 | foo_ptr->SetBar(std::move(bar_ptr_info)); |
| 1157 | ``` |
| 1158 | |
| 1159 | ### Performance considerations |
| 1160 | |
| 1161 | When using associated interfaces on different sequences than the master sequence |
| 1162 | (where the master interface lives): |
| 1163 | |
| 1164 | * Sending messages: send happens directly on the calling sequence. So there |
| 1165 | isn't sequence hopping. |
| 1166 | * Receiving messages: associated interfaces bound on a different sequence from |
| 1167 | the master interface incur an extra sequence hop during dispatch. |
| 1168 | |
| 1169 | Therefore, performance-wise associated interfaces are better suited for |
| 1170 | scenarios where message receiving happens on the master sequence. |
| 1171 | |
| 1172 | ### Testing |
| 1173 | |
| 1174 | Associated interfaces need to be associated with a master interface before |
| 1175 | they can be used. This means one end of the associated interface must be sent |
| 1176 | over one end of the master interface, or over one end of another associated |
| 1177 | interface which itself already has a master interface. |
| 1178 | |
| 1179 | If you want to test an associated interface endpoint without first |
Adithya Srinivasan | 4b6c608 | 2018-11-14 16:56:46 | [diff] [blame] | 1180 | associating it, you can use `mojo::MakeRequestAssociatedWithDedicatedPipe`. This |
| 1181 | will create working associated interface endpoints which are not actually |
| 1182 | associated with anything else. |
Chase Phillips | 3f76e15 | 2018-07-18 20:45:26 | [diff] [blame] | 1183 | |
| 1184 | ### Read more |
| 1185 | |
| 1186 | * [Design: Mojo Associated Interfaces](https://docs.google.com/document/d/1nq3J_HbS-gvVfIoEhcVyxm1uY-9G_7lhD-4Kyxb1WIY/edit) |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1187 | |
| 1188 | ## Synchronous Calls |
| 1189 | |
Oksana Zhuravlova | 50bac90 | 2019-01-15 00:17:59 | [diff] [blame] | 1190 | ### Think carefully before you decide to use sync calls |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1191 | |
Oksana Zhuravlova | 50bac90 | 2019-01-15 00:17:59 | [diff] [blame] | 1192 | Although sync calls are convenient, you should avoid them whenever they |
| 1193 | are not absolutely necessary: |
| 1194 | |
| 1195 | * Sync calls hurt parallelism and therefore hurt performance. |
| 1196 | * Re-entrancy changes message order and produces call stacks that you |
| 1197 | probably never think about while you are coding. It has always been a |
| 1198 | huge pain. |
| 1199 | * Sync calls may lead to deadlocks. |
| 1200 | |
| 1201 | ### Mojom changes |
| 1202 | |
| 1203 | A new attribute `[Sync]` (or `[Sync=true]`) is introduced for methods. |
| 1204 | For example: |
| 1205 | |
| 1206 | ``` cpp |
| 1207 | interface Foo { |
| 1208 | [Sync] |
| 1209 | SomeSyncCall() => (Bar result); |
| 1210 | }; |
| 1211 | ``` |
| 1212 | |
| 1213 | It indicates that when `SomeSyncCall()` is called, the control flow of |
| 1214 | the calling thread is blocked until the response is received. |
| 1215 | |
| 1216 | It is not allowed to use this attribute with functions that don’t have |
| 1217 | responses. If you just need to wait until the service side finishes |
| 1218 | processing the call, you can use an empty response parameter list: |
| 1219 | |
| 1220 | ``` cpp |
| 1221 | [Sync] |
| 1222 | SomeSyncCallWithNoResult() => (); |
| 1223 | ``` |
| 1224 | |
| 1225 | ### Generated bindings (C++) |
| 1226 | |
| 1227 | The generated C++ interface of the Foo interface above is: |
| 1228 | |
| 1229 | ``` cpp |
| 1230 | class Foo { |
| 1231 | public: |
| 1232 | // The service side implements this signature. The client side can |
| 1233 | // also use this signature if it wants to call the method asynchronously. |
| 1234 | virtual void SomeSyncCall(SomeSyncCallCallback callback) = 0; |
| 1235 | |
| 1236 | // The client side uses this signature to call the method synchronously. |
| 1237 | virtual bool SomeSyncCall(BarPtr* result); |
| 1238 | }; |
| 1239 | ``` |
| 1240 | |
| 1241 | As you can see, the client side and the service side use different |
| 1242 | signatures. At the client side, response is mapped to output parameters |
| 1243 | and the boolean return value indicates whether the operation is |
| 1244 | successful. (Returning false usually means a connection error has |
| 1245 | occurred.) |
| 1246 | |
| 1247 | At the service side, a signature with callback is used. The reason is |
| 1248 | that in some cases the implementation may need to do some asynchronous |
| 1249 | work which the sync method’s result depends on. |
| 1250 | |
| 1251 | *** note |
| 1252 | **NOTE:** you can also use the signature with callback at the client side to |
| 1253 | call the method asynchronously. |
| 1254 | *** |
| 1255 | |
| 1256 | ### Re-entrancy |
| 1257 | |
| 1258 | What happens on the calling thread while waiting for the response of a |
| 1259 | sync method call? It continues to process incoming sync request messages |
| 1260 | (i.e., sync method calls); block other messages, including async |
| 1261 | messages and sync response messages that don’t match the ongoing sync |
| 1262 | call. |
| 1263 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 1264 |  |
Oksana Zhuravlova | 50bac90 | 2019-01-15 00:17:59 | [diff] [blame] | 1265 | |
| 1266 | Please note that sync response messages that don’t match the ongoing |
| 1267 | sync call cannot re-enter. That is because they correspond to sync calls |
| 1268 | down in the call stack. Therefore, they need to be queued and processed |
| 1269 | while the stack unwinds. |
| 1270 | |
| 1271 | ### Avoid deadlocks |
| 1272 | |
| 1273 | Please note that the re-entrancy behavior doesn’t prevent deadlocks |
| 1274 | involving async calls. You need to avoid call sequences such as: |
| 1275 | |
Ken Rockot | ab03512 | 2019-02-06 00:35:24 | [diff] [blame] | 1276 |  |
Oksana Zhuravlova | 50bac90 | 2019-01-15 00:17:59 | [diff] [blame] | 1277 | |
| 1278 | ### Read more |
| 1279 | |
| 1280 | * [Design Proposal: Mojo Sync Methods]( |
| 1281 | https://docs.google.com/document/d/1dixzFzZQW8e3ldjdM8Adbo8klXDDE4pVekwo5aLgUsE) |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1282 | |
| 1283 | ## Type Mapping |
| 1284 | |
| 1285 | In many instances you might prefer that your generated C++ bindings use a more |
| 1286 | natural type to represent certain Mojom types in your interface methods. For one |
| 1287 | example consider a Mojom struct such as the `Rect` below: |
| 1288 | |
| 1289 | ``` cpp |
| 1290 | module gfx.mojom; |
| 1291 | |
| 1292 | struct Rect { |
| 1293 | int32 x; |
| 1294 | int32 y; |
| 1295 | int32 width; |
| 1296 | int32 height; |
| 1297 | }; |
| 1298 | |
| 1299 | interface Canvas { |
| 1300 | void FillRect(Rect rect); |
| 1301 | }; |
| 1302 | ``` |
| 1303 | |
| 1304 | The `Canvas` Mojom interface would normally generate a C++ interface like: |
| 1305 | |
| 1306 | ``` cpp |
| 1307 | class Canvas { |
| 1308 | public: |
| 1309 | virtual void FillRect(RectPtr rect) = 0; |
| 1310 | }; |
| 1311 | ``` |
| 1312 | |
| 1313 | However, the Chromium tree already defines a native |
| 1314 | [`gfx::Rect`](https://cs.chromium.org/chromium/src/ui/gfx/geometry/rect.h) which |
| 1315 | is equivalent in meaning but which also has useful helper methods. Instead of |
| 1316 | manually converting between a `gfx::Rect` and the Mojom-generated `RectPtr` at |
| 1317 | every message boundary, wouldn't it be nice if the Mojom bindings generator |
| 1318 | could instead generate: |
| 1319 | |
| 1320 | ``` cpp |
| 1321 | class Canvas { |
| 1322 | public: |
| 1323 | virtual void FillRect(const gfx::Rect& rect) = 0; |
| 1324 | } |
| 1325 | ``` |
| 1326 | |
| 1327 | The correct answer is, "Yes! That would be nice!" And fortunately, it can! |
| 1328 | |
| 1329 | ### Global Configuration |
| 1330 | |
| 1331 | While this feature is quite powerful, it introduces some unavoidable complexity |
| 1332 | into build system. This stems from the fact that type-mapping is an inherently |
| 1333 | viral concept: if `gfx::mojom::Rect` is mapped to `gfx::Rect` anywhere, the |
| 1334 | mapping needs to apply *everywhere*. |
| 1335 | |
| 1336 | For this reason we have a few global typemap configurations defined in |
sky | 0b887ae | 2017-05-18 03:26:03 | [diff] [blame] | 1337 | [chromium_bindings_configuration.gni](https://cs.chromium.org/chromium/src/mojo/public/tools/bindings/chromium_bindings_configuration.gni) |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1338 | and |
sky | 0b887ae | 2017-05-18 03:26:03 | [diff] [blame] | 1339 | [blink_bindings_configuration.gni](https://cs.chromium.org/chromium/src/mojo/public/tools/bindings/blink_bindings_configuration.gni). These configure the two supported [variants](#Variants) of Mojom generated |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1340 | bindings in the repository. Read more on this in the sections that follow. |
| 1341 | |
| 1342 | For now, let's take a look at how to express the mapping from `gfx::mojom::Rect` |
| 1343 | to `gfx::Rect`. |
| 1344 | |
| 1345 | ### Defining `StructTraits` |
| 1346 | |
| 1347 | In order to teach generated bindings code how to serialize an arbitrary native |
| 1348 | type `T` as an arbitrary Mojom type `mojom::U`, we need to define an appropriate |
| 1349 | specialization of the |
| 1350 | [`mojo::StructTraits`](https://cs.chromium.org/chromium/src/mojo/public/cpp/bindings/struct_traits.h) |
| 1351 | template. |
| 1352 | |
| 1353 | A valid specialization of `StructTraits` MUST define the following static |
| 1354 | methods: |
| 1355 | |
| 1356 | * A single static accessor for every field of the Mojom struct, with the exact |
| 1357 | same name as the struct field. These accessors must all take a const ref to |
| 1358 | an object of the native type, and must return a value compatible with the |
| 1359 | Mojom struct field's type. This is used to safely and consistently extract |
| 1360 | data from the native type during message serialization without incurring extra |
| 1361 | copying costs. |
| 1362 | |
| 1363 | * A single static `Read` method which initializes an instance of the the native |
| 1364 | type given a serialized representation of the Mojom struct. The `Read` method |
| 1365 | must return a `bool` to indicate whether the incoming data is accepted |
| 1366 | (`true`) or rejected (`false`). |
| 1367 | |
| 1368 | There are other methods a `StructTraits` specialization may define to satisfy |
| 1369 | some less common requirements. See |
| 1370 | [Advanced StructTraits Usage](#Advanced-StructTraits-Usage) for details. |
| 1371 | |
| 1372 | In order to define the mapping for `gfx::Rect`, we want the following |
| 1373 | `StructTraits` specialization, which we'll define in |
Stephen Nusko | 0ea37f3d | 2019-02-17 01:45:19 | [diff] [blame] | 1374 | `//ui/gfx/geometry/mojo/geometry_mojom_traits.h`: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1375 | |
| 1376 | ``` cpp |
Stephen Nusko | 0ea37f3d | 2019-02-17 01:45:19 | [diff] [blame] | 1377 | #include "mojo/public/cpp/bindings/mojom_traits.h" |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1378 | #include "ui/gfx/geometry/rect.h" |
| 1379 | #include "ui/gfx/geometry/mojo/geometry.mojom.h" |
| 1380 | |
| 1381 | namespace mojo { |
| 1382 | |
| 1383 | template <> |
| 1384 | class StructTraits<gfx::mojom::RectDataView, gfx::Rect> { |
| 1385 | public: |
| 1386 | static int32_t x(const gfx::Rect& r) { return r.x(); } |
| 1387 | static int32_t y(const gfx::Rect& r) { return r.y(); } |
| 1388 | static int32_t width(const gfx::Rect& r) { return r.width(); } |
| 1389 | static int32_t height(const gfx::Rect& r) { return r.height(); } |
| 1390 | |
| 1391 | static bool Read(gfx::mojom::RectDataView data, gfx::Rect* out_rect); |
| 1392 | }; |
| 1393 | |
| 1394 | } // namespace mojo |
| 1395 | ``` |
| 1396 | |
Stephen Nusko | 0ea37f3d | 2019-02-17 01:45:19 | [diff] [blame] | 1397 | And in `//ui/gfx/geometry/mojo/geometry_mojom_traits.cc`: |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1398 | |
| 1399 | ``` cpp |
Stephen Nusko | 0ea37f3d | 2019-02-17 01:45:19 | [diff] [blame] | 1400 | #include "ui/gfx/geometry/mojo/geometry_mojom_traits.h" |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1401 | |
| 1402 | namespace mojo { |
| 1403 | |
| 1404 | // static |
| 1405 | template <> |
| 1406 | bool StructTraits<gfx::mojom::RectDataView, gfx::Rect>::Read( |
| 1407 | gfx::mojom::RectDataView data, |
| 1408 | gfx::Rect* out_rect) { |
| 1409 | if (data.width() < 0 || data.height() < 0) |
| 1410 | return false; |
| 1411 | |
| 1412 | out_rect->SetRect(data.x(), data.y(), data.width(), data.height()); |
| 1413 | return true; |
| 1414 | }; |
| 1415 | |
| 1416 | } // namespace mojo |
| 1417 | ``` |
| 1418 | |
| 1419 | Note that the `Read()` method returns `false` if either the incoming `width` or |
| 1420 | `height` fields are negative. This acts as a validation step during |
| 1421 | deserialization: if a client sends a `gfx::Rect` with a negative width or |
| 1422 | height, its message will be rejected and the pipe will be closed. In this way, |
| 1423 | type mapping can serve to enable custom validation logic in addition to making |
| 1424 | callsites and interface implemention more convenient. |
| 1425 | |
Oksana Zhuravlova | 4b59467 | 2018-11-06 21:58:25 | [diff] [blame] | 1426 | When the struct fields have non-primitive types, e.g. string or array, |
| 1427 | returning a read-only view of the data in the accessor is recommended to |
| 1428 | avoid copying. It is safe because the input object is guaranteed to |
| 1429 | outlive the usage of the result returned by the accessor method. |
| 1430 | |
| 1431 | The following example uses `StringPiece` to return a view of the GURL's |
| 1432 | data (`//url/mojom/url_gurl_mojom_traits.h`): |
| 1433 | |
| 1434 | ``` cpp |
| 1435 | #include "base/strings/string_piece.h" |
| 1436 | #include "url/gurl.h" |
| 1437 | #include "url/mojom/url.mojom.h" |
| 1438 | #include "url/url_constants.h" |
| 1439 | |
| 1440 | namespace mojo { |
| 1441 | |
| 1442 | template <> |
| 1443 | struct StructTraits<url::mojom::UrlDataView, GURL> { |
| 1444 | static base::StringPiece url(const GURL& r) { |
| 1445 | if (r.possibly_invalid_spec().length() > url::kMaxURLChars || |
| 1446 | !r.is_valid()) { |
| 1447 | return base::StringPiece(); |
| 1448 | } |
| 1449 | return base::StringPiece(r.possibly_invalid_spec().c_str(), |
| 1450 | r.possibly_invalid_spec().length()); |
| 1451 | } |
| 1452 | } // namespace mojo |
| 1453 | ``` |
| 1454 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1455 | ### Enabling a New Type Mapping |
| 1456 | |
| 1457 | We've defined the `StructTraits` necessary, but we still need to teach the |
| 1458 | bindings generator (and hence the build system) about the mapping. To do this we |
| 1459 | must create a **typemap** file, which uses familiar GN syntax to describe the |
| 1460 | new type mapping. |
| 1461 | |
| 1462 | Let's place this `geometry.typemap` file alongside our Mojom file: |
| 1463 | |
| 1464 | ``` |
| 1465 | mojom = "//ui/gfx/geometry/mojo/geometry.mojom" |
Oksana Zhuravlova | 7e09e66 | 2018-11-29 21:11:25 | [diff] [blame] | 1466 | os_whitelist = [ "android" ] |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1467 | public_headers = [ "//ui/gfx/geometry/rect.h" ] |
Stephen Nusko | 0ea37f3d | 2019-02-17 01:45:19 | [diff] [blame] | 1468 | traits_headers = [ "//ui/gfx/geometry/mojo/geometry_mojom_traits.h" ] |
Ken Rockot | db79117e | 2018-01-17 20:36:08 | [diff] [blame] | 1469 | sources = [ |
Stephen Nusko | 0ea37f3d | 2019-02-17 01:45:19 | [diff] [blame] | 1470 | "//ui/gfx/geometry/mojo/geometry_mojom_traits.cc", |
| 1471 | "//ui/gfx/geometry/mojo/geometry_mojom_traits.h", |
Ken Rockot | db79117e | 2018-01-17 20:36:08 | [diff] [blame] | 1472 | ] |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1473 | public_deps = [ "//ui/gfx/geometry" ] |
| 1474 | type_mappings = [ |
| 1475 | "gfx.mojom.Rect=gfx::Rect", |
| 1476 | ] |
| 1477 | ``` |
| 1478 | |
| 1479 | Let's look at each of the variables above: |
| 1480 | |
| 1481 | * `mojom`: Specifies the `mojom` file to which the typemap applies. Many |
| 1482 | typemaps may apply to the same `mojom` file, but any given typemap may only |
| 1483 | apply to a single `mojom` file. |
Oksana Zhuravlova | 7e09e66 | 2018-11-29 21:11:25 | [diff] [blame] | 1484 | * `os_whitelist`: Optional list of specific platforms this typemap |
| 1485 | should be constrained to. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1486 | * `public_headers`: Additional headers required by any code which would depend |
| 1487 | on the Mojom definition of `gfx.mojom.Rect` now that the typemap is applied. |
| 1488 | Any headers required for the native target type definition should be listed |
| 1489 | here. |
| 1490 | * `traits_headers`: Headers which contain the relevant `StructTraits` |
| 1491 | specialization(s) for any type mappings described by this file. |
Stephen Nusko | 0ea37f3d | 2019-02-17 01:45:19 | [diff] [blame] | 1492 | * `sources`: Any implementation sources needed for the `StructTraits` |
| 1493 | definition. These sources are compiled directly into the generated C++ |
| 1494 | bindings target for a `mojom` file applying this typemap. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1495 | * `public_deps`: Target dependencies exposed by the `public_headers` and |
| 1496 | `traits_headers`. |
| 1497 | * `deps`: Target dependencies exposed by `sources` but not already covered by |
| 1498 | `public_deps`. |
| 1499 | * `type_mappings`: A list of type mappings to be applied for this typemap. The |
| 1500 | strings in this list are of the format `"MojomType=CppType"`, where |
| 1501 | `MojomType` must be a fully qualified Mojom typename and `CppType` must be a |
| 1502 | fully qualified C++ typename. Additional attributes may be specified in square |
| 1503 | brackets following the `CppType`: |
| 1504 | * `move_only`: The `CppType` is move-only and should be passed by value |
| 1505 | in any generated method signatures. Note that `move_only` is transitive, |
| 1506 | so containers of `MojomType` will translate to containers of `CppType` |
| 1507 | also passed by value. |
| 1508 | * `copyable_pass_by_value`: Forces values of type `CppType` to be passed by |
| 1509 | value without moving them. Unlike `move_only`, this is not transitive. |
| 1510 | * `nullable_is_same_type`: By default a non-nullable `MojomType` will be |
| 1511 | mapped to `CppType` while a nullable `MojomType?` will be mapped to |
| 1512 | `base::Optional<CppType>`. If this attribute is set, the `base::Optional` |
| 1513 | wrapper is omitted for nullable `MojomType?` values, but the |
| 1514 | `StructTraits` definition for this type mapping must define additional |
| 1515 | `IsNull` and `SetToNull` methods. See |
| 1516 | [Specializing Nullability](#Specializing-Nullability) below. |
Ken Rockot | c9c268a9 | 2018-01-31 21:04:40 | [diff] [blame] | 1517 | * `force_serialize`: The typemap is incompatible with lazy serialization |
| 1518 | (e.g. consider a typemap to a `base::StringPiece`, where retaining a |
| 1519 | copy is unsafe). Any messages carrying the type will be forced down the |
| 1520 | eager serailization path. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1521 | |
| 1522 | |
| 1523 | Now that we have the typemap file we need to add it to a local list of typemaps |
| 1524 | that can be added to the global configuration. We create a new |
| 1525 | `//ui/gfx/typemaps.gni` file with the following contents: |
| 1526 | |
| 1527 | ``` |
| 1528 | typemaps = [ |
| 1529 | "//ui/gfx/geometry/mojo/geometry.typemap", |
| 1530 | ] |
| 1531 | ``` |
| 1532 | |
| 1533 | And finally we can reference this file in the global default (Chromium) bindings |
| 1534 | configuration by adding it to `_typemap_imports` in |
sky | 0b887ae | 2017-05-18 03:26:03 | [diff] [blame] | 1535 | [chromium_bindings_configuration.gni](https://cs.chromium.org/chromium/src/mojo/public/tools/bindings/chromium_bindings_configuration.gni): |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1536 | |
| 1537 | ``` |
| 1538 | _typemap_imports = [ |
| 1539 | ..., |
| 1540 | "//ui/gfx/typemaps.gni", |
| 1541 | ..., |
| 1542 | ] |
| 1543 | ``` |
| 1544 | |
| 1545 | ### StructTraits Reference |
| 1546 | |
| 1547 | Each of a `StructTraits` specialization's static getter methods -- one per |
| 1548 | struct field -- must return a type which can be used as a data source for the |
| 1549 | field during serialization. This is a quick reference mapping Mojom field type |
| 1550 | to valid getter return types: |
| 1551 | |
| 1552 | | Mojom Field Type | C++ Getter Return Type | |
| 1553 | |------------------------------|------------------------| |
| 1554 | | `bool` | `bool` |
| 1555 | | `int8` | `int8_t` |
| 1556 | | `uint8` | `uint8_t` |
| 1557 | | `int16` | `int16_t` |
| 1558 | | `uint16` | `uint16_t` |
| 1559 | | `int32` | `int32_t` |
| 1560 | | `uint32` | `uint32_t` |
| 1561 | | `int64` | `int64_t` |
| 1562 | | `uint64` | `uint64_t` |
| 1563 | | `float` | `float` |
| 1564 | | `double` | `double` |
| 1565 | | `handle` | `mojo::ScopedHandle` |
| 1566 | | `handle<message_pipe>` | `mojo::ScopedMessagePipeHandle` |
| 1567 | | `handle<data_pipe_consumer>` | `mojo::ScopedDataPipeConsumerHandle` |
| 1568 | | `handle<data_pipe_producer>` | `mojo::ScopedDataPipeProducerHandle` |
| 1569 | | `handle<shared_buffer>` | `mojo::ScopedSharedBufferHandle` |
| 1570 | | `FooInterface` | `FooInterfacePtr` |
| 1571 | | `FooInterface&` | `FooInterfaceRequest` |
| 1572 | | `associated FooInterface` | `FooAssociatedInterfacePtr` |
| 1573 | | `associated FooInterface&` | `FooAssociatedInterfaceRequest` |
| 1574 | | `string` | Value or reference to any type `T` that has a `mojo::StringTraits` specialization defined. By default this includes `std::string`, `base::StringPiece`, and `WTF::String` (Blink). |
| 1575 | | `array<T>` | Value or reference to any type `T` that has a `mojo::ArrayTraits` specialization defined. By default this includes `std::vector<T>`, `mojo::CArray<T>`, and `WTF::Vector<T>` (Blink). |
| 1576 | | `map<K, V>` | Value or reference to any type `T` that has a `mojo::MapTraits` specialization defined. By default this includes `std::map<T>`, `mojo::unordered_map<T>`, and `WTF::HashMap<T>` (Blink). |
| 1577 | | `FooEnum` | Value of any type that has an appropriate `EnumTraits` specialization defined. By default this inlcudes only the generated `FooEnum` type. |
| 1578 | | `FooStruct` | Value or reference to any type that has an appropriate `StructTraits` specialization defined. By default this includes only the generated `FooStructPtr` type. |
| 1579 | | `FooUnion` | Value of reference to any type that has an appropriate `UnionTraits` specialization defined. By default this includes only the generated `FooUnionPtr` type. |
| 1580 | |
| 1581 | ### Using Generated DataView Types |
| 1582 | |
| 1583 | Static `Read` methods on `StructTraits` specializations get a generated |
| 1584 | `FooDataView` argument (such as the `RectDataView` in the example above) which |
| 1585 | exposes a direct view of the serialized Mojom structure within an incoming |
| 1586 | message's contents. In order to make this as easy to work with as possible, the |
| 1587 | generated `FooDataView` types have a generated method corresponding to every |
| 1588 | struct field: |
| 1589 | |
| 1590 | * For POD field types (*e.g.* bools, floats, integers) these are simple accessor |
| 1591 | methods with names identical to the field name. Hence in the `Rect` example we |
| 1592 | can access things like `data.x()` and `data.width()`. The return types |
| 1593 | correspond exactly to the mappings listed in the table above, under |
| 1594 | [StructTraits Reference](#StructTraits-Reference). |
| 1595 | |
| 1596 | * For handle and interface types (*e.g* `handle` or `FooInterface&`) these |
| 1597 | are named `TakeFieldName` (for a field named `field_name`) and they return an |
| 1598 | appropriate move-only handle type by value. The return types correspond |
| 1599 | exactly to the mappings listed in the table above, under |
| 1600 | [StructTraits Reference](#StructTraits-Reference). |
| 1601 | |
| 1602 | * For all other field types (*e.g.*, enums, strings, arrays, maps, structs) |
| 1603 | these are named `ReadFieldName` (for a field named `field_name`) and they |
| 1604 | return a `bool` (to indicate success or failure in reading). On success they |
| 1605 | fill their output argument with the deserialized field value. The output |
| 1606 | argument may be a pointer to any type with an appropriate `StructTraits` |
| 1607 | specialization defined, as mentioned in the table above, under |
| 1608 | [StructTraits Reference](#StructTraits-Reference). |
| 1609 | |
| 1610 | An example would be useful here. Suppose we introduced a new Mojom struct: |
| 1611 | |
| 1612 | ``` cpp |
| 1613 | struct RectPair { |
| 1614 | Rect left; |
| 1615 | Rect right; |
| 1616 | }; |
| 1617 | ``` |
| 1618 | |
| 1619 | and a corresponding C++ type: |
| 1620 | |
| 1621 | ``` cpp |
| 1622 | class RectPair { |
| 1623 | public: |
| 1624 | RectPair() {} |
| 1625 | |
| 1626 | const gfx::Rect& left() const { return left_; } |
| 1627 | const gfx::Rect& right() const { return right_; } |
| 1628 | |
| 1629 | void Set(const gfx::Rect& left, const gfx::Rect& right) { |
| 1630 | left_ = left; |
| 1631 | right_ = right; |
| 1632 | } |
| 1633 | |
| 1634 | // ... some other stuff |
| 1635 | |
| 1636 | private: |
| 1637 | gfx::Rect left_; |
| 1638 | gfx::Rect right_; |
| 1639 | }; |
| 1640 | ``` |
| 1641 | |
| 1642 | Our traits to map `gfx::mojom::RectPair` to `gfx::RectPair` might look like |
| 1643 | this: |
| 1644 | |
| 1645 | ``` cpp |
| 1646 | namespace mojo { |
| 1647 | |
| 1648 | template <> |
| 1649 | class StructTraits |
| 1650 | public: |
| 1651 | static const gfx::Rect& left(const gfx::RectPair& pair) { |
| 1652 | return pair.left(); |
| 1653 | } |
| 1654 | |
| 1655 | static const gfx::Rect& right(const gfx::RectPair& pair) { |
| 1656 | return pair.right(); |
| 1657 | } |
| 1658 | |
| 1659 | static bool Read(gfx::mojom::RectPairDataView data, gfx::RectPair* out_pair) { |
| 1660 | gfx::Rect left, right; |
| 1661 | if (!data.ReadLeft(&left) || !data.ReadRight(&right)) |
| 1662 | return false; |
| 1663 | out_pair->Set(left, right); |
| 1664 | return true; |
| 1665 | } |
| 1666 | } // namespace mojo |
| 1667 | ``` |
| 1668 | |
| 1669 | Generated `ReadFoo` methods always convert `multi_word_field_name` fields to |
| 1670 | `ReadMultiWordFieldName` methods. |
| 1671 | |
Andrew Moylan | 341cece7 | 2017-06-22 22:03:02 | [diff] [blame] | 1672 | <a name="Blink-Type-Mapping"></a> |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1673 | ### Variants |
| 1674 | |
| 1675 | By now you may have noticed that additional C++ sources are generated when a |
| 1676 | Mojom is processed. These exist due to type mapping, and the source files we |
| 1677 | refer to throughout this docuemnt (namely `foo.mojom.cc` and `foo.mojom.h`) are |
| 1678 | really only one **variant** (the *default* or *chromium* variant) of the C++ |
| 1679 | bindings for a given Mojom file. |
| 1680 | |
| 1681 | The only other variant currently defined in the tree is the *blink* variant, |
| 1682 | which produces a few additional files: |
| 1683 | |
| 1684 | ``` |
| 1685 | out/gen/sample/db.mojom-blink.cc |
| 1686 | out/gen/sample/db.mojom-blink.h |
| 1687 | ``` |
| 1688 | |
| 1689 | These files mirror the definitions in the default variant but with different |
| 1690 | C++ types in place of certain builtin field and parameter types. For example, |
| 1691 | Mojom strings are represented by `WTF::String` instead of `std::string`. To |
| 1692 | avoid symbol collisions, the variant's symbols are nested in an extra inner |
| 1693 | namespace, so Blink consumer of the interface might write something like: |
| 1694 | |
| 1695 | ``` |
| 1696 | #include "sample/db.mojom-blink.h" |
| 1697 | |
| 1698 | class TableImpl : public db::mojom::blink::Table { |
| 1699 | public: |
| 1700 | void AddRow(int32_t key, const WTF::String& data) override { |
| 1701 | // ... |
| 1702 | } |
| 1703 | }; |
| 1704 | ``` |
| 1705 | |
| 1706 | In addition to using different C++ types for builtin strings, arrays, and maps, |
| 1707 | the global typemap configuration for default and "blink" variants are completely |
| 1708 | separate. To add a typemap for the Blink configuration, you can modify |
| 1709 | [blink_bindings_configuration.gni](https://cs.chromium.org/chromium/src/mojo/public/tools/bindings/blink_bindings_configuration.gni). |
| 1710 | |
| 1711 | All variants share some definitions which are unaffected by differences in the |
| 1712 | type mapping configuration (enums, for example). These definitions are generated |
| 1713 | in *shared* sources: |
| 1714 | |
| 1715 | ``` |
| 1716 | out/gen/sample/db.mojom-shared.cc |
| 1717 | out/gen/sample/db.mojom-shared.h |
| 1718 | out/gen/sample/db.mojom-shared-internal.h |
| 1719 | ``` |
| 1720 | |
| 1721 | Including either variant's header (`db.mojom.h` or `db.mojom-blink.h`) |
Andrew Moylan | 341cece7 | 2017-06-22 22:03:02 | [diff] [blame] | 1722 | implicitly includes the shared header, but may wish to include *only* the shared |
| 1723 | header in some instances. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1724 | |
| 1725 | Finally, note that for `mojom` GN targets, there is implicitly a corresponding |
| 1726 | `mojom_{variant}` target defined for any supported bindings configuration. So |
| 1727 | for example if you've defined in `//sample/BUILD.gn`: |
| 1728 | |
| 1729 | ``` |
| 1730 | import("mojo/public/tools/bindings/mojom.gni") |
| 1731 | |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 1732 | mojom("mojom") { |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1733 | sources = [ |
| 1734 | "db.mojom", |
| 1735 | ] |
| 1736 | } |
| 1737 | ``` |
| 1738 | |
| 1739 | Code in Blink which wishes to use the generated Blink-variant definitions must |
Oksana Zhuravlova | d5fea16d | 2018-08-15 00:02:17 | [diff] [blame] | 1740 | depend on `"//sample:mojom_blink"`. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1741 | |
| 1742 | ## Versioning Considerations |
| 1743 | |
| 1744 | For general documentation of versioning in the Mojom IDL see |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 1745 | [Versioning](/mojo/public/tools/bindings/README.md#Versiwoning). |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1746 | |
| 1747 | This section briefly discusses some C++-specific considerations relevant to |
| 1748 | versioned Mojom types. |
| 1749 | |
| 1750 | ### Querying Interface Versions |
| 1751 | |
| 1752 | `InterfacePtr` defines the following methods to query or assert remote interface |
| 1753 | version: |
| 1754 | |
| 1755 | ```cpp |
| 1756 | void QueryVersion(const base::Callback<void(uint32_t)>& callback); |
| 1757 | ``` |
| 1758 | |
| 1759 | This queries the remote endpoint for the version number of its binding. When a |
| 1760 | response is received `callback` is invoked with the remote version number. Note |
| 1761 | that this value is cached by the `InterfacePtr` instance to avoid redundant |
| 1762 | queries. |
| 1763 | |
| 1764 | ```cpp |
| 1765 | void RequireVersion(uint32_t version); |
| 1766 | ``` |
| 1767 | |
| 1768 | Informs the remote endpoint that a minimum version of `version` is required by |
| 1769 | the client. If the remote endpoint cannot support that version, it will close |
| 1770 | its end of the pipe immediately, preventing any other requests from being |
| 1771 | received. |
| 1772 | |
| 1773 | ### Versioned Enums |
| 1774 | |
| 1775 | For convenience, every extensible enum has a generated helper function to |
| 1776 | determine whether a received enum value is known by the implementation's current |
| 1777 | version of the enum definition. For example: |
| 1778 | |
| 1779 | ```cpp |
| 1780 | [Extensible] |
| 1781 | enum Department { |
| 1782 | SALES, |
| 1783 | DEV, |
| 1784 | RESEARCH, |
| 1785 | }; |
| 1786 | ``` |
| 1787 | |
| 1788 | generates the function in the same namespace as the generated C++ enum type: |
| 1789 | |
| 1790 | ```cpp |
| 1791 | inline bool IsKnownEnumValue(Department value); |
| 1792 | ``` |
| 1793 | |
Sasha Bermeister | 995adc6 | 2017-12-07 02:36:43 | [diff] [blame] | 1794 | ### Using Mojo Bindings in Chrome |
| 1795 | |
Oksana Zhuravlova | 87b225a | 2019-03-07 01:08:03 | [diff] [blame] | 1796 | See [Converting Legacy Chrome IPC To Mojo](/docs/mojo_ipc_conversion.md). |
Sasha Bermeister | 995adc6 | 2017-12-07 02:36:43 | [diff] [blame] | 1797 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 1798 | ### Additional Documentation |
| 1799 | |
| 1800 | [Calling Mojo From Blink](https://www.chromium.org/developers/design-documents/mojo/calling-mojo-from-blink) |
| 1801 | : A brief overview of what it looks like to use Mojom C++ bindings from |
| 1802 | within Blink code. |