在移植gecko的时候发现一些问题,比如gecko连alert对话框这种基本的控件都没有默认实现,包括b2g,在firefox os(那个ui叫gaia)中是纯粹地用html实现的。起初觉得把gaia的的代码,拷过来点就是了,后来发现gaia写得有点复杂,会产生很多依赖,还不如直接用native的控件来实现。做法是参考android的firefox,用js处理部分逻辑,gui相关部分用java,就会产生js调java的需求。我是要js调c++,做法差不多。
android中有一个叫AndroidBridge的类,把它注册为一个service,js就调用这个类的handleGeckoMessage()方法。这里简单介绍下操作步骤
1. widget/YOUR_OS/nsINativeBridge.idl
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsISupports.idl"
[scriptable, uuid(0e93d8f4-5392-48bc-9f4d-3a151773b14c)]
interface nsINativeBridge : nsISupports
{
AString handleGeckoMessage(in AString message);
};