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