Mojo Core Embedder API

本文档详细介绍了如何使用Mojo Core Embedder API初始化Mojo进行进程间通信(IPC)。通过静态链接到应用程序的Mojo Core实现,可以启用本地API调用,创建消息管道等。对于实际的多进程通信,需要额外的IPC初始化步骤,包括配置ScopedIPCSupport对象以维持IPC支持。文章还展示了如何在Chromium中设置专用的IO线程来支持Mojo IPC。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

转载于:https://www.cnblogs.com/huangguanyuan/p/10320735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值