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. 『转』Writing Well

    这是前辈Julie Zhuo的最新关于写作的文章,昨天写下-进行总结和阅读思考 这是一篇关于提笔写作的文章,首发在The looking glass...前辈每周都会回答一个读者的问题耶--This ...

  2. Thingworx 使用REST API获取JSON数据

    版本:7.4.1 1.URL规则 http://localhost/Thingworx/Things/[Things名称]/Services/[Service名称]?method=POST&A ...

  3. 蓝桥杯_left and throw

    思考了许久没有结果,最后,还是一位擅长搜索资源的学长帮我找到了一个不错的代码,这个代码极其精妙,再一次印证了一句话,没有做不到的,只有想不到的,当然这个代码我拿到手的时候是个没有注释的代码,我费尽周折 ...

  4. html+css居中问题

    一.行级元素水平居中对齐(父元素设置 text-align:center) <div style="width: 200px; height: 100px;border: 1px so ...

  5. Cookie和Session有什么区别

    1. 由于HTTP协议是无状态的协议,所以服务端需要记录用户的状态时,就需要用某种机制来识别具体的用户,这个机制就是Session.   典型的场景比如购物车,当你点击下单按钮时,由于HTTP协议无状 ...

  6. echarts图表属性说明

    参考博客: https://blog.csdn.net/luanpeng825485697/article/details/76691965

  7. django-2-路由配置及渲染方式

    <<<视图>>> (1)首先要注册创建好的app (2)配置路由 在app目录下新建一个urls.py模块 模块里面复制myproject目录下urls.py里面的 ...

  8. CSS隐藏overflow默认滚动条同时保留滚动效果

    主要应用于移动端场景,仿移动端滚动效果.对于隐藏滚动条,众所周知overflow:hidden,但是想要的滚动效果也没了. 所以对于移动端webkit内核提供一个伪类选择器: .element::-w ...

  9. phpexcel乱码问题

    php导出Excel乱码,只需在header函数前加入ob_end_clean();//清除缓冲区,避免乱码

  10. spring-boot-maven-plugin 插件的作用(转)

    OM 文件中添加了“org.springframework.boot:spring-boot-maven-plugin”插件.在添加了该插件之后,当运行“mvn package”进行打包时,会打包成一 ...