ffos (gecko):如何在gecko 中编写XPCOM组件(一)--javascript 实现方式
记录与分享,鉴于gecko中编写XPCOM组件的文档都极度过时(基本是基于2006年版的变体),我搬砖尝试好久才写出两个组件。老文档代码部分参考价值不高,下面将自己写xpcom组件的成果分享如下,
一篇写js实现的,一篇写c++实现的。
ffos (gecko):如何在gecko 中编写XPCOM组件(一)--javascript 实现方式
ffos (gecko):如何在gecko 中编写XPCOM组件(二)--C++ 实现方式
xpcom refference links XPCM相关供有需要的搬砖同道参考,有错误的地方,欢迎指正。
关于XPCOM的扫盲材料可参考MDN,或者
说明:
开发环境,ubuntu + FFOS 源码。
uuid 用uuidgen 命令生成。
涉及到的目录及文件及其作用原理:
ffos 源码中:
一.组件编写
1.js 组件最终目录结构:
gecko/dom/simplejs/
simplejs
├── moz.build
├── nsISimplejs.idl
├── nsIUseComponent.idl
├── Simplejs.js
├── Simplejs.manifest
├── UseComponent.js
└── UseComponent.manifest
2.组件实现:
(1)nsISimplejs.idl 接口定义文件
#include "nsISupports.idl"
[scriptable, uuid(ce32e3ff-36f8-425f-94be-d85b26e634ee)]
interface nsISimpleComponent : nsISupports
{attribute string yourName;void write();void changejs(in string aValue);
};
(2)Simplejs.js nsISimplejs.idl的实现文件
"use strict";const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cr = Components.results;Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Services.jsm");function SimpleComponentImpl() {debug("yahaha,constructor SimpleComponentImpl constructor in simplecomponent.js################");
}//dump("yahaha,dump,im SimpleComponentImpl in simplecomponent.js######################### before prototype");SimpleComponentImpl.prototype = {contractID : "@mozilla.org/simplejs;1",classID: Components.ID("{cc587afa-0696-469f-9eff-9dac0dd727fe}"),QueryInterface: XPCOMUtils.generateQI([Ci.nsISimpleComponent]),get yourName(){ return this.mName; },set yourName(aName){ return this.mName = aName;},write: function(){ dump("yahaha,SimpleComponentImpl.write()");dump("Hello " + this.mName + "\n");},changejs: function(aValue){ this.mName = aValue;dump("Hello " + this.mName + "\n");},mName: "Anonymous!",
};//the implement of interfaces in .idl//dump("yahaha,dump,in simplecomponent.js###############before generateNSGetFactory");
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SimpleComponentImpl]);//get factory
//dump("yahaha SimpleComponent.js ooooooooooooooooooooooooooooooooooooooooo");
(3)Simplejs.manifest 组件注册清单文件
# Simplejs.js
component {cc587afa-0696-469f-9eff-9dac0dd727fe} Simplejs.js
contract @mozilla.org/simplejs;1 {cc587afa-0696-469f-9eff-9dac0dd727fe}
category profile-after-change simplecomponent @mozilla.org/simplejs;1
(4)在gecko/b2g/installer/package-manifest.in
dom_simplejs.xpt是moz.build 指定生成的xpt模块。见moz.build部分。
添加:
@RESPATH@/components/dom_simplejs.xpt
@RESPATH@/components/Simplejs.js
@RESPATH@/components/Simplejs.manifest
(5)moz.build
编译文件。写法如下:
# vim: set filetype=python:XPIDL_MODULE = 'dom_simplejs'XPIDL_SOURCES += ['
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
