JS编写xpcom的实现

很久之前的文档,拿过来分享。不过现在 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);
}

(哈哈,一个包含多个接口的组件完成了。)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值