Variants

Let's assume we have a mojom file such as this:
 
module example.mojom;
 
interface Foo {
  SendData(string param1, array<int32> param2);
};
 
Given the following GN mojom definition the compiler will generate two targets: example and example_blink
 
mojom("example") {
  sources = [ "example.mojom" ]
  use_new_wrapper_types = true

}
 
The target "example" will generate Chromium-style C++ bindings using STL types:
 
// example.mojom.h

class Example {
  virtual void SendArray(const std::string& param1, const std::vector<int32_t>& param2) = 0;
}

 
The target "example_blink" will generate Blink-style C++ bindings using WTF types:
 
// example.mojom-blink.h

class Example {
  virtual void SendArray(const WTF::String& param1, const WTF::Vector<int32_t>& param2) = 0;
}

 
Thanks to these separate sets of bindings no work is necessary to convert types between Blink-style code and Chromium-style code. It is handled automatically during message serialization and deserialization.
 
Converting WTF::Function to base::Callback
 
Mojo methods that return a value take an instance of base::Callback. To solve this problem use WTF::convertToBaseCallback to turn a WTF::Function into a base::Callback that accepts the same arguments. Do not try to use base::Bind directly as it doesn't understand Oilpan. For example,
 
#include "wtf/Functional.h"

void MyObject::ReverseString(const String& string)
{
  m_service->ReverseString(
      string,
      ConvertToBaseCallback(WTF::Bind(&MyObject::onStringReversed, wrapPersistent(this))));
}

 
Mojo is not traced by Oilpan so one must be careful. If an interface pointer is owned by an managed object the message pipe will be closed during lazy sweeping. This means that messages may be received after the object has become unreachable and pointers to other managed objects are possibly invalid. To avoid this all managed objects bound with WTF::bind must be either Persistent or WeakPersistent references. This is done by wrapping pointers passed to WTF::bind with either wrapPersistent() or wrapWeakPersistent(this).
 
WeakPersistent references should be used to avoid cycles whenever a callback is expected to have the same lifetime as the object. The most common example of this is the this pointer bound to the callback passed to InterfacePtr::set_connection_error_callback.
 
Persistent references should be used at any other time when the interface pointer is owned by the bound object. This is because if no other reference to the object exists it will be garbage collected before the response is received, canceling the request. Only if this behavior is desired should a WeakPersistent be used instead.
 
If response callbacks are bound with wrapPersistent() then a connection error handler that closes the message pipe should be attached to the interface pointer. Otherwise callbacks will not be destroyed when the pipe is closed and the object will leak.
 
// m_service is a member of MyObject. This code should be run as soon as m_foo is bound to a message pipe.
m_service.set_connection_error_handler(
    ConvertToBaseCallback(WTF::Bind(&MyObject::OnConnectionError,
                                    wrapWeakPersistent(this))));

void MyObject::OnConnectionError()
{
  m_service.reset();
}

 
Implementing Mojo interfaces in Blink
 
Only a mojo::Binding or mojo::BindingSet should be used when implementing a Mojo interface in an Oilpan managed object. The object must then have a pre-finalizer to close any open pipes when the object is about to be swept as lazy sweeping means that it may be invalid long before the destructor is called. This requires setup in both the object header and implementation.
 
// MyObject.h

class MyObject : public GarbageCollected
               , public example::blink::Example
{
  USING_PRE_FINALIZER(MyObject, dispose);
 public:
  MyObject();

void dispose();

// Implementation of example::blink::Example.

private:
  mojo::Binding<example::blink::Example> m_binding;
};

// MyObject.cpp

MyObject::MyObject() : m_binding(this)
{
  ThreadState::current()->registerPreFinalizer(this);
}

void MyObject::dispose()
{
  m_binding.Close();
}

Calling Mojo from Blink的更多相关文章

  1. [Chromium文档转载,第005章]Calling Mojo from Blink

    For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Calling Mojo from Blink Variants Let's as ...

  2. Mojo C++ Bindings API

    This document is a subset of the Mojo documentation. Contents Overview Getting Started Interfaces Ba ...

  3. [Chromium文档转载,第002章]Mojo C++ Bindings API

    Mojo C++ Bindings API This document is a subset of the Mojo documentation. Contents Overview Getting ...

  4. Deno下一代Nodejs?Deno初体验

    前言 Ryan Dahl之父发布了新的项目Deno,很多IT媒体都使用了标题“下一代Nodejs”,首先我们看一下Deno的特性: 1.支持typescript (nodejs目前也支持). 2.无p ...

  5. Converting Legacy Chrome IPC To Mojo

    Converting Legacy Chrome IPC To Mojo Looking for Mojo Documentation? Contents Overview Deciding What ...

  6. [Chromium文档转载,第006章]Chrome IPC To Mojo IPC Cheat Sheet

    For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Chrome IPC To Mojo IPC Cheat Sheet 目录 1 O ...

  7. [Chromium文档转载,第001章] Mojo Migration Guide

        For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Mojo Migration Guide 目录 1 Summary 2 H ...

  8. Mojo C++ System API

    This document is a subset of the Mojo documentation. Contents Overview Scoped, Typed Handles Message ...

  9. Mojo Associated Interfaces

    Mojo Associated Interfaces yzshen@chromium.org 02/22/2017 Background Before associated interfaces ar ...

随机推荐

  1. 如何使用pgpool failover_stream.sh自己控制选择指定的master节点

    集群架构: h236:master h237:standby sync h238:standby sync h239:stadnby async h240:standby async h241:sta ...

  2. HDU 1176 免费馅饼【动态规划】

    解题思路:用a[i][j]表示在第i秒在地点j的掉落馅饼的数量,设整个馅饼掉落的时间持续为timemax,即为矩阵的高度,一共0到10个地点,为矩阵的长度,如图,即可构成数塔,因为考虑到在地点0的时候 ...

  3. 前端精选文摘:css之BFC 神奇背后的原理(转载)

    一.BFC是什么? 在解释 BFC 是什么之前,需要先介绍 Box.Formatting Context的概念. Box: CSS布局的基本单位 Box 是 CSS 布局的对象和基本单位, 直观点来说 ...

  4. 百度api使用说明

    .初始化地图,并设置地图中心点 复制代码 https://www.cnblogs.com/zqzjs/p/5293698.html var map = new BMap.Map("allma ...

  5. webpack——react

    这里记录了webpack在react中的简单运用 npx create-react-app 项目名 创建一个新项目 npm run build serve -s build

  6. 在用HTML+css写页面中遇到的问题

    一.清除浮动. (1)verflow:hidden;这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很 了解.一提到清除浮动,我们就会 ...

  7. 为什么Arduino独占鳌头并站稳脚跟?

    出处: http://bbs.dfrobot.com.cn/thread-793-1-1.html 为什么Arduino独占鳌头并站稳脚跟? 每个月,我都会在<Make>杂志上发表几篇社论 ...

  8. HDU 5533 Dancing Stars on Me( 有趣的计算几何 )

    链接:传送门 题意:给出 n 个点,判断能不能构成一个正 n 边形,这 n 个点坐标是整数 思路:这道题关键就在与这 n 个点坐标是正整数!!!可以简单的分析,如果 n != 4,那一定就不能构成正 ...

  9. vue 删除某个元素和删除某些元素

    今天做项目使用前端vue框架,需要循环遍历去删除一些数组元素.开始思想局限,一直纠结如何去循环删除,犹豫循环删除数组值下标会发生变化,并不是一种好的方法. 方法一:使用forEach 和 splice ...

  10. SQL的运算符优先级

    注: 1.乘除的优先级高于加减: 2.同一优先级运算符从左向右执行: 3.括号内的运算先执行.