| OLD | NEW |
| 1 # Mojo C++ Bindings API | 1 # Mojo C++ Bindings API |
| 2 This document is a subset of the [Mojo documentation](/mojo). | 2 This document is a subset of the [Mojo documentation](/mojo). |
| 3 | 3 |
| 4 [TOC] | 4 [TOC] |
| 5 | 5 |
| 6 ## Overview | 6 ## Overview |
| 7 The Mojo C++ Bindings API leverages the | 7 The Mojo C++ Bindings API leverages the |
| 8 [C++ System API](/mojo/public/cpp/system) to provide a more natural set of | 8 [C++ System API](/mojo/public/cpp/system) to provide a more natural set of |
| 9 primitives for communicating over Mojo message pipes. Combined with generated | 9 primitives for communicating over Mojo message pipes. Combined with generated |
| 10 code from the [Mojom IDL and bindings generator](/mojo/public/tools/bindings), | 10 code from the [Mojom IDL and bindings generator](/mojo/public/tools/bindings), |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 All message pipe-binding C++ objects (*e.g.*, `mojo::Binding<T>`, | 382 All message pipe-binding C++ objects (*e.g.*, `mojo::Binding<T>`, |
| 383 `mojo::InterfacePtr<T>`, *etc.*) support setting their connection error handler | 383 `mojo::InterfacePtr<T>`, *etc.*) support setting their connection error handler |
| 384 via a `set_connection_error_handler` method. | 384 via a `set_connection_error_handler` method. |
| 385 | 385 |
| 386 We can set up another end-to-end `Logger` example to demonstrate error handler | 386 We can set up another end-to-end `Logger` example to demonstrate error handler |
| 387 invocation: | 387 invocation: |
| 388 | 388 |
| 389 ``` cpp | 389 ``` cpp |
| 390 sample::mojom::LoggerPtr logger; | 390 sample::mojom::LoggerPtr logger; |
| 391 LoggerImpl impl(mojo::MakeRequest(&logger)); | 391 LoggerImpl impl(mojo::MakeRequest(&logger)); |
| 392 impl.set_connection_error_handler(base::Bind([] { LOG(ERROR) << "Bye."; })); | 392 impl.set_connection_error_handler(base::BindOnce([] { LOG(ERROR) << "Bye."; })); |
| 393 logger->Log("OK cool"); | 393 logger->Log("OK cool"); |
| 394 logger.reset(); // Closes the client end. | 394 logger.reset(); // Closes the client end. |
| 395 ``` | 395 ``` |
| 396 | 396 |
| 397 As long as `impl` stays alive here, it will eventually receive the `Log` message | 397 As long as `impl` stays alive here, it will eventually receive the `Log` message |
| 398 followed immediately by an invocation of the bound callback which outputs | 398 followed immediately by an invocation of the bound callback which outputs |
| 399 `"Bye."`. Like all other bindings callbacks, a connection error handler will | 399 `"Bye."`. Like all other bindings callbacks, a connection error handler will |
| 400 **never** be invoked once its corresponding binding object has been destroyed. | 400 **never** be invoked once its corresponding binding object has been destroyed. |
| 401 | 401 |
| 402 In fact, suppose instead that `LoggerImpl` had set up the following error | 402 In fact, suppose instead that `LoggerImpl` had set up the following error |
| 403 handler within its constructor: | 403 handler within its constructor: |
| 404 | 404 |
| 405 ``` cpp | 405 ``` cpp |
| 406 LoggerImpl::LoggerImpl(sample::mojom::LoggerRequest request) | 406 LoggerImpl::LoggerImpl(sample::mojom::LoggerRequest request) |
| 407 : binding_(this, std::move(request)) { | 407 : binding_(this, std::move(request)) { |
| 408 binding_.set_connection_error_handler( | 408 binding_.set_connection_error_handler( |
| 409 base::Bind(&LoggerImpl::OnError, base::Unretained(this))); | 409 base::BindOnce(&LoggerImpl::OnError, base::Unretained(this))); |
| 410 } | 410 } |
| 411 | 411 |
| 412 void LoggerImpl::OnError() { | 412 void LoggerImpl::OnError() { |
| 413 LOG(ERROR) << "Client disconnected! Purging log lines."; | 413 LOG(ERROR) << "Client disconnected! Purging log lines."; |
| 414 lines_.clear(); | 414 lines_.clear(); |
| 415 } | 415 } |
| 416 ``` | 416 ``` |
| 417 | 417 |
| 418 The use of `base::Unretained` is *safe* because the error handler will never be | 418 The use of `base::Unretained` is *safe* because the error handler will never be |
| 419 invoked beyond the lifetime of `binding_`, and `this` owns `binding_`. | 419 invoked beyond the lifetime of `binding_`, and `this` owns `binding_`. |
| (...skipping 1002 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1422 | 1422 |
| 1423 ```cpp | 1423 ```cpp |
| 1424 inline bool IsKnownEnumValue(Department value); | 1424 inline bool IsKnownEnumValue(Department value); |
| 1425 ``` | 1425 ``` |
| 1426 | 1426 |
| 1427 ### Additional Documentation | 1427 ### Additional Documentation |
| 1428 | 1428 |
| 1429 [Calling Mojo From Blink](https://www.chromium.org/developers/design-documents/m
ojo/calling-mojo-from-blink) | 1429 [Calling Mojo From Blink](https://www.chromium.org/developers/design-documents/m
ojo/calling-mojo-from-blink) |
| 1430 : A brief overview of what it looks like to use Mojom C++ bindings from | 1430 : A brief overview of what it looks like to use Mojom C++ bindings from |
| 1431 within Blink code. | 1431 within Blink code. |
| OLD | NEW |