For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎

Calling Mojo from Blink

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();
}

[Chromium文档转载,第005章]Calling Mojo from Blink的更多相关文章

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

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

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

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

  3. [Chromium文档转载,第003章]Proposal: Mojo Synchronous Methods

    Proposal: Mojo Synchronous Methods yzshen@chromium.org 02/02/2016 Overview Currently there are quite ...

  4. [Chromium文档转载,第007章]JNI on Chromium for Android

    Overview JNI (Java Native Interface) is the mechanism that enables Java code to call native function ...

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

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

  6. [Chromium文档转载,第004章]Mojo Synchronous Calls

    For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Synchronous Calls Think carefully before ...

  7. 用R创建Word和PowerPoint文档--转载

    https://www.jianshu.com/p/7df62865c3ed Rapp --简书 Microsoft的Office软件在办公软件领域占有绝对的主导地位,几乎每个职场人士都必须掌握Wor ...

  8. java实现支付宝接口--文档..转载

    //实现java支付宝很简单,只要从支付宝官方下载   http://help.alipay.com/support/index_sh.htm下载程序,配置一下参数就OK了:   1.先到http:/ ...

  9. iOS开发主要参考文档(转载)

    Objective-C,语言的系统详细资料.这是做iOS开发的前题与基础.https://developer.apple.com/library/ios/#documentation/Cocoa/Co ...

随机推荐

  1. hadoop-05-mysql修改密码

    hadoop-05-mysql修改密码 su root 1,service mysqld start 2,vi /var/log/mysqld.log #在这里面查找密码 3, mysql -uroo ...

  2. maven3+eclipse搭建webAPP企业级实战《一》

    想做企业级web系统:环境搭建不可缺少GO 1:新建 2:next : 3:选择webAPP next: 填完finish 初始项目结构: watermark/2/text/aHR0cDovL2Jsb ...

  3. 提高FPGA速度的quartus编译选项

    Turning on some optimizations in Quartus II may help increase it. Here are some you may want to try: ...

  4. Django是什么

    Django是什么 Django是什么? 是基于python语言的优秀的web开发框架.很多有名的网站比如youtube就是用django开发的. Python写的开源Web应用框架, 快速搭建blo ...

  5. springBoot 打war包 程序包com.sun.istack.internal不存在的问题

    使用的是 idea - Lifecycle-package 的方式打包(maven) 确认  <packaging>war</packaging> 修改启动类: (原启动类) ...

  6. 一键解决 500、502和504 Internal Privoxy Error 问题(图文详解)

    最近获得一个SS帐号,手机,其他电脑都能上,但是在我的电脑上就是500 或 502  或 504,如下所示. 502 Read from server failed: Unknown error Th ...

  7. HBase框架基础(四)

    * HBase框架基础(四) 上一节我们介绍了如何使用HBase搞一些MapReduce小程序,其主要作用呢是可以做一些数据清洗和分析或者导入数据的工作,这一节我们来介绍如何使用HBase与其他框架进 ...

  8. ubuntu12.04

    最近越来越觉得必须用Linux了,于是装了15.04,好不习惯的感觉,思维还是10.10的时代. 尝试做种http://jingyan.baidu.com/article/a681b0dedad55c ...

  9. 基于jquery 的find()函数和children()函数的区别

    element.find(selector)  返回匹配element集合中每个元素的后代,参数selector是必须的,可以通过选择器对元素进行过滤,筛选出符合条件的元素.如果想选中所有的后代元素, ...

  10. DNS Prefetching

    For Developers‎ > ‎Design Documents‎ > ‎ DNS Prefetching 目录 1 Problem 2 Solution 3 Architectur ...