很久之前的文档,拿过来分享。不过现在 Gecko2.0 了,所以有些细节可能有变动,目前还没有对 Gecko2.0 进行研究。待续。
目的:领会JS实现XPCOM的思想。
JS实现的xpcom需要和xpt放在一个文件夹下,即components文件夹下。
(同理,c++实现的xpcom 即 dll文件 需要和xpt放在同一个文件夹下,也是components文件夹下)。
文档网址:
http://developer.mozilla.org/en/How_to_Build_an_XPCOM_Component_in_Javascript
编译命令设置:
{sdk_dir}/bin/xpidl -m typelib -w -v -I {sdk_dir}/idl -e HelloWorld.xpt HelloWorld.idl
编译命令举例:
D:/sourcecode/xulrunner-sdk/xulrunner-sdk-1.9/seamonkey-xulrunner-sdk/bin/xpidl -m typelib -w -v -I D:/sourcecode/xulrunner-sdk/xulrunner-sdk-1.9/seamonkey-xulrunner-sdk/idl -e D:/log/IMService.xpt D:/IMService.idl
编译中遇到的问题:
1.编码问题:xpidl是不识别UTF-8的。有点晕。
2.路径中不能有空格。
在一个组件中添加多个接口的实例:
IMService.idl
#include "nsISupports.idl"
[scriptable, uuid(EED9537B-E056-4f0d-834A-6AEDF8E5B14A)]
interface ceopIIMService: nsISupports {
};
[scriptable, uuid(3CBED3C4-A0D8-11DD-AF3E-E43F56D89593)]
interface ceopIIMAccountService: nsISupports {
};
IMService.js
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
// utils
const Cc=Components.classes;
const Ci=Components.interfaces;
/***********************************************************
class definition
***********************************************************/
// class constructor
function IMService() { }
// class definition
IMService.prototype = {
// properties required for XPCOM registration:
classDescription: "CE IMService",
classID: Components.ID("{AD3F34D6-9688-11DD-B0C5-F7CC56D89593}"),
contractID: "@c.cn/IMService;1",
QueryInterface: XPCOMUtils.generateQI([Ci.ceopIIMService, Ci.ceopIIMAccountService ]),
.....
};
var components = [IMService];
function NSGetModule(compMgr, fileSpec) {
return XPCOMUtils.generateModule(components);
}
(哈哈,一个包含多个接口的组件完成了。)