转自:
http://www.iosart.com/firefox/xpcom/
About
This is a step-by-step tutorial on creating, building and registering an XPCOM component on Linux and MS Windows.
Download
The complete source code for this tutorial can be downloaded from here.
Creating the component
- Download the Gecko SDK for your platform.
- You can find it at http://ftp.mozilla.org/pub/mozilla.org/mozilla/releases/mozilla1.7/.
- You can choose a different Mozilla release if you like.
- Extract the SDK to a local directory.
- Create a GUID for your main interface.
- On Windows you can use the
guidgen
utility. - On Linux you can use the
uuidgen
utility.
- On Windows you can use the
- Create the interface definition file -
IMyComponent.idl
- Use the following template, add methods and attributes to the interface.
- Fill in the GUID you have generated.
#include "nsISupports.idl" [scriptable, uuid(_YOUR_INTERFACE_GUID_)] interface IMyComponent : nsISupports { long Add(in long a, in long b); };
- Generate the interface header and typelib files out of the interface definition file.
- Use the xpidl utility that comes with Gecko SDK.
- Substitute the "_DIR_" in the following commands with the full path to the
xpcom/idl
directory found under your Gecko SDK main directory. xpidl -m header -I_DIR_ IMyComponent.idl
will create theIMyComponent.h
header file.xpidl -m typelib -I_DIR_ IMyComponent.idl
will create theIMyComponent.xpt
typelib file.
- The interface header file
IMyComponent.h
contains templates for building your component header and implementation files. You can copy and paste the templates to create these files, changing only your component name. - Create your component header file -
MyComponent.h
.- Start by inserting double inclusion protection code (
#ifndef _MY_COMPONENT_H_ ....
). - Add
#include "IMyComponent.h"
to include the interface definition. - Create a GUID for your component.
- Add the following lines, which define your component name, contract ID, and GUID:
#define MY_COMPONENT_CONTRACTID "@mydomain.com/XPCOMSample/MyComponent;1" #define MY_COMPONENT_CLASSNAME "A Simple XPCOM Sample" #define MY_COMPONENT_CID _YOUR_COMPONENT_GUID_
- Copy the header template from
IMyComponent.h
(starting with/* Header file */
) into theMyComponent.h
file. - Replace all the instances of
_MYCLASS_
with the name of your component.
- Start by inserting double inclusion protection code (
- Create your component implementation file -
MyComponent.cpp
.- Add
#include "MyComponent.h"
to include your component definitions. - Copy the implementation template from
IMyComponent.h
(starting with/* Implementation file */
) into theMyComponent.cpp
file. - Replace all the instances of
_MYCLASS_
with the name of your component. - Add method implementation code.
- Add
- Create your module definitions file -
MyComponentModule.cpp
.- Add
#include "nsIGenericFactory.h"
to include Mozilla GenericFactory definitions. - Add
#include "MyComponent.h"
to include your component definitions. - Add
NS_GENERIC_FACTORY_CONSTRUCTOR(MyComponent)
to define the constructor for your component. - Add
to define the class name, contract ID and GUID of your component.static nsModuleComponentInfo components[] = { { MY_COMPONENT_CLASSNAME, MY_COMPONENT_CID, MY_COMPONENT_CONTRACTID, MyComponentConstructor, } };
- Add
NS_IMPL_NSGETMODULE("MyComponentsModule", components)
to export the above information to Mozilla.
- Add
- Create the makefiles and/or projects.
- You can use the templates provided with this sample to create your custom makefile.
- Alternatively, you can create a Visual C++ project with similar settings (on Windows).
Building the sample
- Download the sample code from here.
- Extract the sample to a local directory.
- Edit the makefile.
- The makefile is located in the src directory of the sample.
- It is named
Makefile
for Linux,MyComponent.mak
for Windows - Edit the makefile, changing the
GECKO_SDK_PATH
variable to point to your Gecko SDK directory. - Added: When using the included Visual Studio project:
- Open the project settings:
Project > Settings...
- Choose
Settings For: All Configurations
in the upper left corner. - Open the
C/C++
tab and choosePreprocessor
in theCategory
drop-down list. - Make sure that "Additional include directories" points to your Gecko SDK.
- Open the
Link
tab and chooseInput
in theCategory
drop-down list. - Make sure that the "Additional library path" points to your Gecko SDK.
- Open the project settings:
- Build the sample.
- Open a command shell (
cmd
on Windows;tcsh
,bash
,xterm
etc. on Linux). - Navigate to the sample src directory.
- On Linux issue a
make
command.MyComponent.so
file is created. - On Windows issue a
nmake /f MyComponent.mak
command.Debug/MyComponent.dll
is created. - Alternatively, on Windows, issue a
nmake /f "MyComponent.mak" CFG="MyComponent - Win32 Release"
command for a release build.Release/MyComponent.dll
is created. - Added: When using the included Visual Studio project:
- Select the wanted configuration (Debug/Release) in
Build > Set Active Configuration...
- Choose
Build > Build MyComponent.dll
- Select the wanted configuration (Debug/Release) in
- Open a command shell (
- Register the new component with Mozilla.
- Copy
MyComponent.so
/MyComponent.dll
andIMyComponent.xpt
file to the Mozilla components directory. - On Windows this directory is typically located at:
C:/Program Files/Mozilla Firefox/components
. - On Linux this directory is typically located at
~/firefox/components
(or ~/mozilla/components). - Run the
regxpcom
command supplied with the Gecko SDK to register the new component. - You might need to provide an additional parameter:
regxpcom -x _COMPONENTS_DIR_
where _COMPONENTS_DIR_ is the components directory. - Delete the
xpti.dat
andcompreg.dat
files from your Mozilla profile directory.
These files will be automatically rebuilt by Mozilla the next time it is restarted.
Added: Alternatively you can "touch" (either create or update) a file called.autoreg
in the main Mozilla/Firefox installation directory. - The profile directory is typically located at:
~/.mozilla/firefox/default.???
on Linux.C:/Documents and Settings/USERNAME/Application Data/Mozilla/Firefox/Profiles/default.???
on Windows.
- Copy
- Test the new component.
- Restart Mozilla.
- Open the
MyComponentTest.html
file provided with the sample and click the "Go" button. - If everything is OK you should see a dialog saying "3 + 4 = 7".
Links and Resources
- IBM developerWorks - XPCOM Overview [part 1] [part 2] [part 3] [part 4] [part 5]
- Creating XPCOM Components by Doug Turner.
- Creating Applications With Mozilla [chapter 8 - XPCOM]
Revision History
- June 3, 2005
- Modified the Windows makefile (MyComponent.mak) to work with the latest Gecko SDK. The old version of the sample can be found here.
- Added Visual Studio 6 project file to the sample.
- Added a tip about using the ".autoreg" file for registering new components.