CROSS PLATFORM COMPONENT OBJECT
A Brief Overview
XPCOM is a cross platform component object model, similar
to Microsoft COM.
It has multiple language bindings, allowing XPCOM
and Python in addition to C++.
Interfaces in XPCOM are defined in a dialect of IDL
XPCOM itself provides a set of core components and
classes, e.g. file and memory management, threads, basic
data structures (strings, arrays, variants), etc.
XPCOM components can be used by any programming
language Mozilla supports.
Objects implemented as XPCOM can be global to an
application, and are not dependent on the scope of any one
window. You can use an XPCOM service to share data
XPCOM encourages modular programming - programming
logic can be encapsulated within a component.
Objects must be accessed from their defined interfaces.
The only exception to this rule, is if you set a magical
property wrappedJSObject on your component implemented in
from the calling side. You can set ”this.wrappedJSObject =
this” in your component's constructor and avoid writing any
It is easier to have memory leaks.
A component is simply an object that implements interfaces, and
is registered with the component manager.
Interfaces are used to define what functions and attributes an
XPCOM component provides. nsISupports is an interface that all
classes must implement. It provides a function to request the
interfaces on a class.
Factories are objects used to instantiate components. Each
factory object implements the nsIFactory interface.
Multiple classes can be defined in a file. A module is an object
that is used to register, unregister and provide factories for
classes. It implements the nsIModule interface.
8. Example Code
9. Example Code
10. Example Code
I. Startup & Registration
During the startup process if the file compreg.dat is missing or
the file .autoreg exists, the application knows that it needs to
register components. It then loads files from each of the
components folders, including the components folder in each
extension directory. It then calls the function NSGetModule on
each file to get the object that implements the nsIModule
Interface. The module object is then used to register your
module. This is used to simplify the process of retrieving
the module later in class creation. The registerSelf function
is called to do the actual registering. When removing a
class, the unregisterSelf function is called.
II. Class Creation
The code calls getClassObject on your module.
This returns a class factory. The factory then has a method called
createInstance which is used to create the actual class object.
III. Accessing the component
You can now access your component by the code written.
Mozilla also provides a component construction process to easy