XPCOM is a cross-platform component object model similar to COM. It allows components to be implemented and used in multiple languages including JavaScript, Java, and Python in addition to C++. Interfaces are defined in XPIDL and XPCOM provides core classes for file, memory, and data management. Components can be globally used across applications and languages. Drawbacks include needing to access objects through interfaces and increased memory leak potential. Components are registered via modules to simplify retrieval. Factories instantiate components by their interfaces.
3. Introduction
XPCOM is a cross platform component object model, similar
to Microsoft COM.
It has multiple language bindings, allowing XPCOM
components to be used and implemented in JavaScript, Java,
and Python in addition to C++.
Interfaces in XPCOM are defined in a dialect of IDL
called XPIDL.
XPCOM itself provides a set of core components and
classes, e.g. file and memory management, threads, basic
data structures (strings, arrays, variants), etc.
4. Benefits
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
between windows.
XPCOM encourages modular programming - programming
logic can be encapsulated within a component.
5. Drawbacks
Objects must be accessed from their defined interfaces.
Javascript shortcuts such as the global window object cannot
be accessed.
The only exception to this rule, is if you set a magical
property wrappedJSObject on your component implemented in
JavaScript, the object this property refers to will be accessible
from the calling side. You can set ”this.wrappedJSObject =
this” in your component's constructor and avoid writing any
interfaces altogether.
It is easier to have memory leaks.
6. Concepts
Components:
A component is simply an object that implements interfaces, and
is registered with the component manager.
Interfaces:
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.
7. Concepts
Factories:
(Cont.)
Factories are objects used to instantiate components. Each
factory object implements the nsIFactory interface.
Modules:
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.
11. Working
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.
12. Working
II. Class Creation
(Cont.)
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
creation with JavaScript.