Mojo Core Embedder API
This document is a subset of the Mojo documentation.
Overview
The Mojo Core Embedder API enables process to initialize and use Mojo for IPC, using an implementation of Mojo Core that is statically linked into the application. See the note about dynamic linking here for more information about an alternative approach to Mojo Core initialization.
NOTE: Unless you are introducing a new binary entry point into the system (e.g., a new executable with a new main()
definition), you probably don't need to know anything about the Embedder API. Most processes defined in the Chrome repo today already fully initialize Mojo Core so that all other public Mojo APIs just work out of the box.
Basic Initialization
As an embedder, initializing Mojo Core requires a single call to mojo::core::Init
:
#include "mojo/core/embedder/embedder.h" int main(int argc, char** argv) { mojo::core::Init(); // Now you can create message pipes, write messages, etc return 0; }
This enables local API calls to work, so message pipes etc can be created and used. In some cases (particuarly many unit testing scenarios) this is sufficient, but to support any actual multiprocess communication (e.g. sending or accepting Mojo invitations), a second IPC initialization step is required.
IPC Initialization
Internal Mojo IPC implementation requires a background TaskRunner
on which it can watch for inbound I/O from other processes. This is configured using a ScopedIPCSupport
object, which keeps IPC support alive through the extent of its lifetime.
Typically an application will create a dedicated background thread and give its TaskRunner
to Mojo. Note that in Chromium, we use the existing “IO thread” in the browser process and content child processes. In general, any thread used for Mojo IPC support must be running a base::MessageLoop::TYPE_IO
loop.
#include "base/threading/thread.h" #include "mojo/core/embedder/embedder.h" #include "mojo/core/embedder/scoped_ipc_support.h" int main(int argc, char** argv) { mojo::core::Init(); base::Thread ipc_thread("ipc!"); ipc_thread.StartWithOptions( base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); // As long as this object is alive, all Mojo API surface relevant to IPC // connections is usable, and message pipes which span a process boundary will // continue to function. mojo::core::ScopedIPCSupport ipc_support( ipc_thread.task_runner(), mojo::core::ScopedIPCSupport::ShutdownPolicy::CLEAN); return 0; }
This process is now fully prepared to use Mojo IPC!
Note that all existing process types in Chromium already perform this setup very early during startup.
Connecting Two Processes
Once IPC is initialized, you can bootstrap connections to other processes by using the public Invitations API.
posted on 2019-01-25 17:24 huangguanyuan 阅读(358) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~