Ken Rockot | 686e413 | 2017-04-26 00:03:31 | [diff] [blame] | 1 | # Mojo |
[email protected] | 99e508a4 | 2013-12-04 01:15:09 | [diff] [blame] | 2 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 3 | [TOC] |
scheib | aad29cf | 2016-03-31 22:33:50 | [diff] [blame] | 4 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 5 | ## Getting Started With Mojo |
| 6 | |
| 7 | To get started using Mojo in applications which already support it (such as |
| 8 | Chrome), the fastest path forward will be to look at the bindings documentation |
| 9 | for your language of choice ([**C++**](#C_Bindings), |
| 10 | [**JavaScript**](#JavaScript-Bindings), or [**Java**](#Java-Bindings)) as well |
| 11 | as the documentation for the |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 12 | [**Mojom IDL and bindings generator**](/mojo/public/tools/bindings/README.md). |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 13 | |
| 14 | If you're looking for information on creating and/or connecting to services, see |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 15 | the top-level [Services documentation](/services/README.md). |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 16 | |
| 17 | For specific details regarding the conversion of old things to new things, check |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 18 | out [Converting Legacy Chrome IPC To Mojo](/ipc/README.md). |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 19 | |
| 20 | ## System Overview |
| 21 | |
| 22 | Mojo is a layered collection of runtime libraries providing a platform-agnostic |
| 23 | abstraction of common IPC primitives, a message IDL format, and a bindings |
| 24 | library with code generation for multiple target languages to facilitate |
| 25 | convenient message passing across arbitrary inter- and intra-process boundaries. |
| 26 | |
| 27 | The documentation here is segmented according to the different isolated layers |
| 28 | and libraries comprising the system. The basic hierarchy of features is as |
| 29 | follows: |
| 30 | |
Ken Rockot | 856f977 | 2017-04-11 19:41:39 | [diff] [blame] | 31 |  |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 32 | |
| 33 | ## Embedder Development Kit (EDK) |
| 34 | Every process to be interconnected via Mojo IPC is called a **Mojo embedder** |
| 35 | and needs to embed the |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 36 | [**Embedder Development Kit (EDK)**](/mojo/edk/embedder/README.md) library. The |
| 37 | EDK exposes the means for an embedder to physically connect one process to another |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 38 | using any supported native IPC primitive (*e.g.,* a UNIX domain socket or |
| 39 | Windows named pipe) on the host platform. |
| 40 | |
| 41 | Details regarding where and how an application process actually embeds and |
| 42 | configures the EDK are generaly hidden from the rest of the application code, |
| 43 | and applications instead use the public System and Bindings APIs to get things |
| 44 | done within processes that embed Mojo. |
| 45 | |
| 46 | ## C System API |
| 47 | Once the EDK is initialized within a process, the public |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 48 | [**C System API**](/mojo/public/c/system/README.md) is usable on any thread for |
| 49 | the remainder of the process's lifetime. This is a lightweight API with a |
| 50 | relatively small (and eventually stable) ABI. Typically this API is not used |
| 51 | directly, but it is the foundation upon which all remaining upper layers are |
| 52 | built. It exposes the fundamental capabilities to create and interact with |
| 53 | various types of Mojo handles including **message pipes**, **data pipes**, and |
| 54 | **shared buffers**. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 55 | |
| 56 | ## High-Level System APIs |
| 57 | |
| 58 | There is a relatively small, higher-level system API for each supported |
| 59 | language, built upon the low-level C API. Like the C API, direct usage of these |
| 60 | system APIs is rare compared to the bindings APIs, but it is sometimes desirable |
| 61 | or necessary. |
| 62 | |
| 63 | ### C++ |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 64 | The [**C++ System API**](/mojo/public/cpp/system/README.md) provides a layer of |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 65 | C++ helper classes and functions to make safe System API usage easier: |
| 66 | strongly-typed handle scopers, synchronous waiting operations, system handle |
| 67 | wrapping and unwrapping helpers, common handle operations, and utilities for |
| 68 | more easily watching handle state changes. |
| 69 | |
| 70 | ### JavaScript |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 71 | The [**JavaScript System API**](/third_party/blink/renderer/core/mojo/README.md) |
| 72 | exposes the Mojo primitives to JavaScript, covering all basic functionality of the |
Yuzhu Shen | e70d197 | 2017-06-02 16:35:15 | [diff] [blame] | 73 | low-level C API. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 74 | |
| 75 | ### Java |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 76 | The [**Java System API**](/mojo/public/java/system/README.md) provides helper |
| 77 | classes for working with Mojo primitives, covering all basic functionality of |
| 78 | the low-level C API. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 79 | |
| 80 | ## High-Level Bindings APIs |
| 81 | Typically developers do not use raw message pipe I/O directly, but instead |
Eric Roman | a13aa24 | 2017-11-10 22:37:11 | [diff] [blame] | 82 | define some set of interfaces which are used to generate code that resembles |
| 83 | an idiomatic method-calling interface in the target language of choice. This is |
| 84 | the bindings layer. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 85 | |
| 86 | ### Mojom IDL and Bindings Generator |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 87 | Interfaces are defined using the |
| 88 | [**Mojom IDL**](/mojo/public/tools/bindings/README.md), which can be fed to the |
| 89 | [**bindings generator**](/mojo/public/tools/bindings/README.md) to generate code |
| 90 | in various supported languages. Generated code manages serialization and |
| 91 | deserialization of messages between interface clients and implementations, |
| 92 | simplifying the code -- and ultimately hiding the message pipe -- on either side |
| 93 | of an interface connection. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 94 | |
| 95 | ### C++ Bindings |
| 96 | By far the most commonly used API defined by Mojo, the |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 97 | [**C++ Bindings API**](/mojo/public/cpp/bindings/README.md) exposes a robust set |
| 98 | of features for interacting with message pipes via generated C++ bindings code, |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 99 | including support for sets of related bindings endpoints, associated interfaces, |
| 100 | nested sync IPC, versioning, bad-message reporting, arbitrary message filter |
| 101 | injection, and convenient test facilities. |
| 102 | |
| 103 | ### JavaScript Bindings |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 104 | The [**JavaScript Bindings API**](/mojo/public/js/README.md) provides helper |
| 105 | classes for working with JavaScript code emitted by the bindings generator. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 106 | |
| 107 | ### Java Bindings |
Ken Rockot | 929282c | 2018-05-02 17:07:29 | [diff] [blame] | 108 | The [**Java Bindings API**](/mojo/public/java/bindings/README.md) provides |
| 109 | helper classes for working with Java code emitted by the bindings generator. |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 110 | |
| 111 | ## FAQ |
| 112 | |
| 113 | ### Why not protobuf? Why a new thing? |
| 114 | There are number of potentially decent answers to this question, but the |
| 115 | deal-breaker is that a useful IPC mechanism must support transfer of native |
| 116 | object handles (*e.g.* file descriptors) across process boundaries. Other |
| 117 | non-new IPC things that do support this capability (*e.g.* D-Bus) have their own |
| 118 | substantial deficiencies. |
| 119 | |
| 120 | ### Are message pipes expensive? |
| 121 | No. As an implementation detail, creating a message pipe is essentially |
| 122 | generating two random numbers and stuffing them into a hash table, along with a |
| 123 | few tiny heap allocations. |
| 124 | |
| 125 | ### So really, can I create like, thousands of them? |
| 126 | Yes! Nobody will mind. Create millions if you like. (OK but maybe don't.) |
| 127 | |
John Abd-El-Malek | d5cc4a4 | 2017-07-13 23:43:42 | [diff] [blame] | 128 | ### What are the performance characteristics of Mojo? |
| 129 | Compared to the old IPC in Chrome, making a Mojo call is about 1/3 faster and uses |
| 130 | 1/3 fewer context switches. The full data is [available here](https://docs.google.com/document/d/1n7qYjQ5iy8xAkQVMYGqjIy_AXu2_JJtMoAcOOupO_jQ/edit). |
| 131 | |
rockot | f59d2d6 | 2017-04-01 02:49:08 | [diff] [blame] | 132 | ### Can I use in-process message pipes? |
| 133 | Yes, and message pipe usage is identical regardless of whether the pipe actually |
| 134 | crosses a process boundary -- in fact this detail is intentionally obscured. |
| 135 | |
| 136 | Message pipes which don't cross a process boundary are efficient: sent messages |
| 137 | are never copied, and a write on one end will synchronously modify the message |
| 138 | queue on the other end. When working with generated C++ bindings, for example, |
| 139 | the net result is that an `InterfacePtr` on one thread sending a message to a |
| 140 | `Binding` on another thread (or even the same thread) is effectively a |
| 141 | `PostTask` to the `Binding`'s `TaskRunner` with the added -- but often small -- |
| 142 | costs of serialization, deserialization, validation, and some internal routing |
| 143 | logic. |
| 144 | |
| 145 | ### What about ____? |
| 146 | |
| 147 | Please post questions to |
| 148 | [`[email protected]`](https://groups.google.com/a/chromium.org/forum/#!forum/chromium-mojo)! |
| 149 | The list is quite responsive. |
| 150 | |