1*635a8641SAndroid Build Coastguard Worker# Mojo 2*635a8641SAndroid Build Coastguard Worker 3*635a8641SAndroid Build Coastguard Worker[TOC] 4*635a8641SAndroid Build Coastguard Worker 5*635a8641SAndroid Build Coastguard Worker## Getting Started With Mojo 6*635a8641SAndroid Build Coastguard Worker 7*635a8641SAndroid Build Coastguard WorkerTo get started using Mojo in applications which already support it (such as 8*635a8641SAndroid Build Coastguard WorkerChrome), the fastest path forward will be to look at the bindings documentation 9*635a8641SAndroid Build Coastguard Workerfor your language of choice ([**C++**](#C_Bindings), 10*635a8641SAndroid Build Coastguard Worker[**JavaScript**](#JavaScript-Bindings), or [**Java**](#Java-Bindings)) as well 11*635a8641SAndroid Build Coastguard Workeras the documentation for the 12*635a8641SAndroid Build Coastguard Worker[**Mojom IDL and bindings generator**](/mojo/public/tools/bindings/README.md). 13*635a8641SAndroid Build Coastguard Worker 14*635a8641SAndroid Build Coastguard WorkerIf you're looking for information on creating and/or connecting to services, see 15*635a8641SAndroid Build Coastguard Workerthe top-level [Services documentation](/services/README.md). 16*635a8641SAndroid Build Coastguard Worker 17*635a8641SAndroid Build Coastguard WorkerFor specific details regarding the conversion of old things to new things, check 18*635a8641SAndroid Build Coastguard Workerout [Converting Legacy Chrome IPC To Mojo](/ipc/README.md). 19*635a8641SAndroid Build Coastguard Worker 20*635a8641SAndroid Build Coastguard Worker## System Overview 21*635a8641SAndroid Build Coastguard Worker 22*635a8641SAndroid Build Coastguard WorkerMojo is a collection of runtime libraries providing a platform-agnostic 23*635a8641SAndroid Build Coastguard Workerabstraction of common IPC primitives, a message IDL format, and a bindings 24*635a8641SAndroid Build Coastguard Workerlibrary with code generation for multiple target languages to facilitate 25*635a8641SAndroid Build Coastguard Workerconvenient message passing across arbitrary inter- and intra-process boundaries. 26*635a8641SAndroid Build Coastguard Worker 27*635a8641SAndroid Build Coastguard WorkerThe documentation here is segmented according to the different libraries 28*635a8641SAndroid Build Coastguard Workercomprising Mojo. The basic hierarchy of features is as follows: 29*635a8641SAndroid Build Coastguard Worker 30*635a8641SAndroid Build Coastguard Worker 31*635a8641SAndroid Build Coastguard Worker 32*635a8641SAndroid Build Coastguard Worker## Mojo Core 33*635a8641SAndroid Build Coastguard WorkerIn order to use any of the more interesting high-level support libraries like 34*635a8641SAndroid Build Coastguard Workerthe System APIs or Bindings APIs, a process must first initialize Mojo Core. 35*635a8641SAndroid Build Coastguard WorkerThis is a one-time initialization which remains active for the remainder of the 36*635a8641SAndroid Build Coastguard Workerprocess's lifetime. There are two ways to initialize Mojo Core: via the Embedder 37*635a8641SAndroid Build Coastguard WorkerAPI, or through a dynamically linked library. 38*635a8641SAndroid Build Coastguard Worker 39*635a8641SAndroid Build Coastguard Worker### Embedding 40*635a8641SAndroid Build Coastguard WorkerMany processes to be interconnected via Mojo are **embedders**, meaning that 41*635a8641SAndroid Build Coastguard Workerthey statically link against the `//mojo/core/embedder` target and initialize 42*635a8641SAndroid Build Coastguard WorkerMojo support within each process by calling `mojo::core::Init()`. See 43*635a8641SAndroid Build Coastguard Worker[**Mojo Core Embedder API**](/mojo/core/embedder/README.md) for more details. 44*635a8641SAndroid Build Coastguard Worker 45*635a8641SAndroid Build Coastguard WorkerThis is a reasonable option when you can guarantee that all interconnected 46*635a8641SAndroid Build Coastguard Workerprocess binaries are linking against precisely the same revision of Mojo Core. 47*635a8641SAndroid Build Coastguard WorkerTo support other scenarios, use dynamic linking. 48*635a8641SAndroid Build Coastguard Worker 49*635a8641SAndroid Build Coastguard Worker## Dynamic Linking 50*635a8641SAndroid Build Coastguard WorkerOn some platforms, it's also possible for applications to rely on a 51*635a8641SAndroid Build Coastguard Workerdynamically-linked Mojo Core library (`libmojo_core.so` or `mojo_core.dll`) 52*635a8641SAndroid Build Coastguard Workerinstead of statically linking against Mojo Core. 53*635a8641SAndroid Build Coastguard Worker 54*635a8641SAndroid Build Coastguard WorkerIn order to take advantage of this mechanism, the corresponding library must be 55*635a8641SAndroid Build Coastguard Workerpresent in either: 56*635a8641SAndroid Build Coastguard Worker 57*635a8641SAndroid Build Coastguard Worker - The working directory of the application 58*635a8641SAndroid Build Coastguard Worker - A directory named by the `MOJO_CORE_LIBRARY_PATH` environment variable 59*635a8641SAndroid Build Coastguard Worker - A directory named explicitly by the application at runtime 60*635a8641SAndroid Build Coastguard Worker 61*635a8641SAndroid Build Coastguard WorkerInstead of calling `mojo::core::Init()` as embedders do, an application using 62*635a8641SAndroid Build Coastguard Workerdynamic Mojo Core instead calls `MojoInitialize()` from the C System API. This 63*635a8641SAndroid Build Coastguard Workercall will attempt to locate (see above) and load a Mojo Core library to support 64*635a8641SAndroid Build Coastguard Workersubsequent Mojo API usage within the process. 65*635a8641SAndroid Build Coastguard Worker 66*635a8641SAndroid Build Coastguard WorkerNote that the Mojo Core shared library presents a stable, forward-compatible C 67*635a8641SAndroid Build Coastguard WorkerABI which can support all current and future versions of the higher-level, 68*635a8641SAndroid Build Coastguard Workerpublic (and not binary-stable) System and Bindings APIs. 69*635a8641SAndroid Build Coastguard Worker 70*635a8641SAndroid Build Coastguard Worker## C System API 71*635a8641SAndroid Build Coastguard WorkerOnce Mojo is initialized within a process, the public 72*635a8641SAndroid Build Coastguard Worker[**C System API**](/mojo/public/c/system/README.md) is usable on any thread for 73*635a8641SAndroid Build Coastguard Workerthe remainder of the process's lifetime. This is a lightweight API with a 74*635a8641SAndroid Build Coastguard Workerrelatively small, stable, forward-compatible ABI, comprising the total public 75*635a8641SAndroid Build Coastguard WorkerAPI surface of the Mojo Core library. 76*635a8641SAndroid Build Coastguard Worker 77*635a8641SAndroid Build Coastguard WorkerThis API is rarely used directly, but it is the foundation upon which all 78*635a8641SAndroid Build Coastguard Workerhigher-level Mojo APIs are built. It exposes the fundamental capabilities to 79*635a8641SAndroid Build Coastguard Workercreate and interact Mojo primitives like **message pipes**, **data pipes**, and 80*635a8641SAndroid Build Coastguard Worker**shared buffers**, as well as APIs to help bootstrap connections among 81*635a8641SAndroid Build Coastguard Workerprocesses. 82*635a8641SAndroid Build Coastguard Worker 83*635a8641SAndroid Build Coastguard Worker## Platform Support API 84*635a8641SAndroid Build Coastguard WorkerMojo provides a small collection of abstractions around platform-specific IPC 85*635a8641SAndroid Build Coastguard Workerprimitives to facilitate bootstrapping Mojo IPC between two processes. See the 86*635a8641SAndroid Build Coastguard Worker[Platform API](/mojo/public/cpp/platform/README.md) documentation for details. 87*635a8641SAndroid Build Coastguard Worker 88*635a8641SAndroid Build Coastguard Worker## High-Level System APIs 89*635a8641SAndroid Build Coastguard WorkerThere is a relatively small, higher-level system API for each supported 90*635a8641SAndroid Build Coastguard Workerlanguage, built upon the low-level C API. Like the C API, direct usage of these 91*635a8641SAndroid Build Coastguard Workersystem APIs is rare compared to the bindings APIs, but it is sometimes desirable 92*635a8641SAndroid Build Coastguard Workeror necessary. 93*635a8641SAndroid Build Coastguard Worker 94*635a8641SAndroid Build Coastguard Worker### C++ 95*635a8641SAndroid Build Coastguard WorkerThe [**C++ System API**](/mojo/public/cpp/system/README.md) provides a layer of 96*635a8641SAndroid Build Coastguard WorkerC++ helper classes and functions to make safe System API usage easier: 97*635a8641SAndroid Build Coastguard Workerstrongly-typed handle scopers, synchronous waiting operations, system handle 98*635a8641SAndroid Build Coastguard Workerwrapping and unwrapping helpers, common handle operations, and utilities for 99*635a8641SAndroid Build Coastguard Workermore easily watching handle state changes. 100*635a8641SAndroid Build Coastguard Worker 101*635a8641SAndroid Build Coastguard Worker### JavaScript 102*635a8641SAndroid Build Coastguard WorkerThe [**JavaScript System API**](/third_party/blink/renderer/core/mojo/README.md) 103*635a8641SAndroid Build Coastguard Workerexposes the Mojo primitives to JavaScript, covering all basic functionality of the 104*635a8641SAndroid Build Coastguard Workerlow-level C API. 105*635a8641SAndroid Build Coastguard Worker 106*635a8641SAndroid Build Coastguard Worker### Java 107*635a8641SAndroid Build Coastguard WorkerThe [**Java System API**](/mojo/public/java/system/README.md) provides helper 108*635a8641SAndroid Build Coastguard Workerclasses for working with Mojo primitives, covering all basic functionality of 109*635a8641SAndroid Build Coastguard Workerthe low-level C API. 110*635a8641SAndroid Build Coastguard Worker 111*635a8641SAndroid Build Coastguard Worker## High-Level Bindings APIs 112*635a8641SAndroid Build Coastguard WorkerTypically developers do not use raw message pipe I/O directly, but instead 113*635a8641SAndroid Build Coastguard Workerdefine some set of interfaces which are used to generate code that resembles 114*635a8641SAndroid Build Coastguard Workeran idiomatic method-calling interface in the target language of choice. This is 115*635a8641SAndroid Build Coastguard Workerthe bindings layer. 116*635a8641SAndroid Build Coastguard Worker 117*635a8641SAndroid Build Coastguard Worker### Mojom IDL and Bindings Generator 118*635a8641SAndroid Build Coastguard WorkerInterfaces are defined using the 119*635a8641SAndroid Build Coastguard Worker[**Mojom IDL**](/mojo/public/tools/bindings/README.md), which can be fed to the 120*635a8641SAndroid Build Coastguard Worker[**bindings generator**](/mojo/public/tools/bindings/README.md) to generate code 121*635a8641SAndroid Build Coastguard Workerin various supported languages. Generated code manages serialization and 122*635a8641SAndroid Build Coastguard Workerdeserialization of messages between interface clients and implementations, 123*635a8641SAndroid Build Coastguard Workersimplifying the code -- and ultimately hiding the message pipe -- on either side 124*635a8641SAndroid Build Coastguard Workerof an interface connection. 125*635a8641SAndroid Build Coastguard Worker 126*635a8641SAndroid Build Coastguard Worker### C++ Bindings 127*635a8641SAndroid Build Coastguard WorkerBy far the most commonly used API defined by Mojo, the 128*635a8641SAndroid Build Coastguard Worker[**C++ Bindings API**](/mojo/public/cpp/bindings/README.md) exposes a robust set 129*635a8641SAndroid Build Coastguard Workerof features for interacting with message pipes via generated C++ bindings code, 130*635a8641SAndroid Build Coastguard Workerincluding support for sets of related bindings endpoints, associated interfaces, 131*635a8641SAndroid Build Coastguard Workernested sync IPC, versioning, bad-message reporting, arbitrary message filter 132*635a8641SAndroid Build Coastguard Workerinjection, and convenient test facilities. 133*635a8641SAndroid Build Coastguard Worker 134*635a8641SAndroid Build Coastguard Worker### JavaScript Bindings 135*635a8641SAndroid Build Coastguard WorkerThe [**JavaScript Bindings API**](/mojo/public/js/README.md) provides helper 136*635a8641SAndroid Build Coastguard Workerclasses for working with JavaScript code emitted by the bindings generator. 137*635a8641SAndroid Build Coastguard Worker 138*635a8641SAndroid Build Coastguard Worker### Java Bindings 139*635a8641SAndroid Build Coastguard WorkerThe [**Java Bindings API**](/mojo/public/java/bindings/README.md) provides 140*635a8641SAndroid Build Coastguard Workerhelper classes for working with Java code emitted by the bindings generator. 141*635a8641SAndroid Build Coastguard Worker 142*635a8641SAndroid Build Coastguard Worker## FAQ 143*635a8641SAndroid Build Coastguard Worker 144*635a8641SAndroid Build Coastguard Worker### Why not protobuf? Why a new thing? 145*635a8641SAndroid Build Coastguard WorkerThere are number of potentially decent answers to this question, but the 146*635a8641SAndroid Build Coastguard Workerdeal-breaker is that a useful IPC mechanism must support transfer of native 147*635a8641SAndroid Build Coastguard Workerobject handles (*e.g.* file descriptors) across process boundaries. Other 148*635a8641SAndroid Build Coastguard Workernon-new IPC things that do support this capability (*e.g.* D-Bus) have their own 149*635a8641SAndroid Build Coastguard Workersubstantial deficiencies. 150*635a8641SAndroid Build Coastguard Worker 151*635a8641SAndroid Build Coastguard Worker### Are message pipes expensive? 152*635a8641SAndroid Build Coastguard WorkerNo. As an implementation detail, creating a message pipe is essentially 153*635a8641SAndroid Build Coastguard Workergenerating two random numbers and stuffing them into a hash table, along with a 154*635a8641SAndroid Build Coastguard Workerfew tiny heap allocations. 155*635a8641SAndroid Build Coastguard Worker 156*635a8641SAndroid Build Coastguard Worker### So really, can I create like, thousands of them? 157*635a8641SAndroid Build Coastguard WorkerYes! Nobody will mind. Create millions if you like. (OK but maybe don't.) 158*635a8641SAndroid Build Coastguard Worker 159*635a8641SAndroid Build Coastguard Worker### What are the performance characteristics of Mojo? 160*635a8641SAndroid Build Coastguard WorkerCompared to the old IPC in Chrome, making a Mojo call is about 1/3 faster and uses 161*635a8641SAndroid Build Coastguard Worker1/3 fewer context switches. The full data is [available here](https://docs.google.com/document/d/1n7qYjQ5iy8xAkQVMYGqjIy_AXu2_JJtMoAcOOupO_jQ/edit). 162*635a8641SAndroid Build Coastguard Worker 163*635a8641SAndroid Build Coastguard Worker### Can I use in-process message pipes? 164*635a8641SAndroid Build Coastguard WorkerYes, and message pipe usage is identical regardless of whether the pipe actually 165*635a8641SAndroid Build Coastguard Workercrosses a process boundary -- in fact this detail is intentionally obscured. 166*635a8641SAndroid Build Coastguard Worker 167*635a8641SAndroid Build Coastguard WorkerMessage pipes which don't cross a process boundary are efficient: sent messages 168*635a8641SAndroid Build Coastguard Workerare never copied, and a write on one end will synchronously modify the message 169*635a8641SAndroid Build Coastguard Workerqueue on the other end. When working with generated C++ bindings, for example, 170*635a8641SAndroid Build Coastguard Workerthe net result is that an `InterfacePtr` on one thread sending a message to a 171*635a8641SAndroid Build Coastguard Worker`Binding` on another thread (or even the same thread) is effectively a 172*635a8641SAndroid Build Coastguard Worker`PostTask` to the `Binding`'s `TaskRunner` with the added -- but often small -- 173*635a8641SAndroid Build Coastguard Workercosts of serialization, deserialization, validation, and some internal routing 174*635a8641SAndroid Build Coastguard Workerlogic. 175*635a8641SAndroid Build Coastguard Worker 176*635a8641SAndroid Build Coastguard Worker### What about ____? 177*635a8641SAndroid Build Coastguard Worker 178*635a8641SAndroid Build Coastguard WorkerPlease post questions to 179*635a8641SAndroid Build Coastguard Worker[`[email protected]`](https://groups.google.com/a/chromium.org/forum/#!forum/chromium-mojo)! 180*635a8641SAndroid Build Coastguard WorkerThe list is quite responsive. 181*635a8641SAndroid Build Coastguard Worker 182