1.安装SDK
sudo apt-get install xulrunner-1.9-dev
2.新建nsIMyCom.idl
#include "nsISupports.idl"
[scrīptable, uuid(0a00c48b-e956-4812-a142-aac76cbdcb56)]
interface nsIMyCom : nsISupports
{
long Add(in long a, in long b);
};
3.新建nsMyCom.h
#ifndef _nsMyCom_h_
#define _nsMyCom_h_
#include "nsIClassInfoImpl.h"
#include "nsIMyCom.h"
#define NS_MYCOM_CID \
{0xdcc47191, 0x4c95, 0x415d, \
{0x97, 0xb4, 0xc6, 0xb9, 0x0f, 0xf0, 0x9e, 0xe8}}
#define NS_MYCOM_CONTRACTID "@lws.org/mycom;1"
#define NS_MYCOM_NAME "XPCOM TEST"
class nsMyCom : public nsIMyCom
{
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIMYCOM
nsMyCom();
private:
~nsMyCom();
protected:
/* additional members */
};
#endif
4.新建nsMyCom.cpp
#include "nsMyCom.h"
#include "nsMemory.h"
NS_IMPL_ISUPPORTS1_CI(nsMyCom, nsIMyCom)
nsMyCom::nsMyCom()
{
}
nsMyCom::~nsMyCom()
{
}
NS_IMETHODIMP nsMyCom::Add(PRInt32 a, PRInt32 b, PRInt32 *_retval)
{
*_retval = a + b;
return NS_OK;
}
5.新建nsMyComModule.cpp
#include "nsIGenericFactory.h"
#include "nsMyCom.h"
NS_GENERIC_FACTORY_CONSTRUCTOR(nsMyCom)
NS_DECL_CLASSINFO(nsMyCom)
static const nsModuleComponentInfo components[] =
{
{
NS_MYCOM_NAME,
NS_MYCOM_CID,
NS_MYCOM_CONTRACTID,
nsMyComConstructor
}
};
NS_IMPL_NSGETMODULE(nsMyComModule, components)
6.新建Makefile
CPP = g++
CPPFLAGS += -fno-rtti -fno-exceptions -shared
GECKO_SDK_PATH = /usr/lib/xulrunner-devel-1.9/sdk
XPIDL = $(GECKO_SDK_PATH)/../bin/xpidl
CPPHEADER = -m header
TYPELIB = -m typelib
REGDIR = /usr/lib/xulrunner-1.9
OUTDIR = $(REGDIR)/components
GECKO_CONFIG_INCLUDE = -include xpcom-config.h
GECKO_DEFINES = -DMOZILLA_INTERNAL_API
GECKO_INCLUDES = -I$(GECKO_SDK_PATH)/include \
-I/usr/include/nspr
GECKO_LDFLAGS = $(GECKO_SDK_PATH)/lib/libxpcomglue_s.a \
-L$(GECKO_SDK_PATH)/lib -L$(GECKO_SDK_PATH)/../bin -lmozjs -lxul -lxpcom -lsqlite3 \
-L/usr/lib/nspr -lnspr4
GECKO_IDL = -I$(GECKO_SDK_PATH)/idl
build: idl nsMyCom.o nsMyComModule.o
$(CPP) $(CPPFLAGS) -o libxpmycom.so $(GECKO_DEFINES) \
nsMyCom.o nsMyComModule.o $(GECKO_LDFLAGS)
idl: nsIMyCom.idl
$(XPIDL) $(GECKO_IDL) $(CPPHEADER) nsIMyCom.idl
$(XPIDL) $(GECKO_IDL) $(TYPELIB) nsIMyCom.idl
nsMyCom.o: nsMyCom.cpp
$(CPP) $(GECKO_CONFIG_INCLUDE) $(GECKO_DEFINES) \
$(GECKO_INCLUDES) -c nsMyCom.cpp -o nsMyCom.o
nsMyComModule.o: nsMyComModule.cpp
$(CPP) $(GECKO_CONFIG_INCLUDE) $(GECKO_DEFINES) \
$(GECKO_INCLUDES) -c nsMyComModule.cpp -o nsMyComModule.o
install:
cp nsIMyCom.xpt $(OUTDIR)/
cp libxpmycom.so $(OUTDIR)/
clean:
rm *.o
rm *.so
rm *.*~
rm *~
7.编译生成
make clean
make
sudo make install
8.新建测试网页test.html
<html>
<head>
<title>
测试XPCOM组件
</title>
</head>
<body>
<scrīpt>
function test()
{
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var mycom = Components.classes["@lws.org/mycom;1"].createInstance();
mycom = mycom.QueryInterface(Components.interfaces.nsIMyCom);
alert(mycom.Add(3,5));
}
</scrīpt>
<form name="form_test">
<input type="button" value="testxpcom" ōnClick = "test();">
</form>
</body>
</html>
9.测试
sudo touch /usr/lib/xulrunner-1.9/.autoreg
firefox test.html
10. 其它
将Makefile中CPPFLAG注释掉,可以帮助检查component编译的情况
使用:
export NSPR_LOG_MODULES=nsNativeModuleLoader:5
export NSPR_LOG_FILE=~/log
可以将firefox运行的日志记录下来,里面有components注册的信息