XPCOMUtils.jsm

  • Revision slug: Mozilla/JavaScript_code_modules/XPCOMUtils.jsm
  • Revision title: XPCOMUtils.jsm
  • Revision id: 339701
  • Created:
  • Creator: teoli
  • Is current revision? No
  • Comment Marked generateCI as requiring 7.0 in the overview as well; 6 words addedMoved From JavaScript_code_modules/XPCOMUtils.jsm to Mozilla/JavaScript_code_modules/XPCOMUtils.jsm

Revision Content

{{ gecko_minversion_header("1.9") }}

The XPCOMUtils.jsm JavaScript code module offers utility routines for JavaScript components loaded by the JavaScript component loader.

To use this, you first need to import the code module into your JavaScript scope:

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

Using XPCOMUtils

Exposing a JavaScript class as a component using these utility methods requires four key steps:

  1. Import XPCOMUtils.jsm, as explained previously.
  2. Declare the class (or multiple classes) implementing the component(s).
  3. Create an array of component constructors.
  4. Define the NSGetFactory() or NSGetModule() entry point.

Pseudocode

This section provides some pseudocode that demonstrates how to put together a JavaScript class based on the steps listed above.

Constructor

The constructor is a simple method that handles any required initialization tasks.

function MyComponent() {
  // initialize the component here
} 

Class declaration

Declare the class prototype, using a form similar to this.

MyComponent.prototype = {
  // properties required for XPCOM registration:
  classDescription: "unique text description",
  classID:          Components.ID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"),
  contractID:       "@example.com/xxx;1",

  // [optional] custom factory (an object implementing nsIFactory). If not
  // provided, the default factory is used, which returns
  // |(new MyComponent()).QueryInterface(iid)| in its createInstance().
  _xpcom_factory: { ... },

  // [optional] an array of categories to register this component in.
  _xpcom_categories: [{
    // Each object in the array specifies the parameters to pass to
    // nsICategoryManager.addCategoryEntry(). 'true' is passed for
    // both aPersist and aReplace params.
    category: "some-category",
    // optional, defaults to the object's classDescription
    entry: "entry name",
    // optional, defaults to the object's contractID (unless
    // 'service' is specified)
    value: "...",
    // optional, defaults to false. When set to true, and only if 'value'
    // is not specified, the concatenation of the string "service," and the
    // object's contractID is passed as aValue parameter of addCategoryEntry.
    service: true,
    // optional array of applications' IDs in the form:
    // [ "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}", ... ]
    // If this is defined, the component is registered in this
    // category only on the specified applications.
    apps: [ ... ]
  }],

  // QueryInterface implementation, e.g. using the generateQI helper
  QueryInterface: XPCOMUtils.generateQI(
    [Components.interfaces.nsIObserver,
     Components.interfaces.nsIMyInterface,
     "nsIFoo",
     "nsIBar" ]),

  // [optional] classInfo implementation, e.g. using the generateCI helper.
  // Will be automatically returned from QueryInterface if that was
  // generated with the generateQI helper.
  classInfo: XPCOMUtils.generateCI(
    {classID: Components.ID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"),
     contractID: "@example.com/xxx;1",
     classDescription: "unique text description",
     interfaces: [Components.interfaces.nsIObserver,
                  Components.interfaces.nsIMyInterface,
                  "nsIFoo",
                  "nsIBar"],
     flags: Ci.nsIClassInfo.SINGLETON}),

  // ...component implementation...
};

{{ gecko_callout_heading("2.0") }}

The ability to register the component in a category only on specific applications by adding the apps field to a category entry was added in Gecko 2.0 {{ geckoRelease("2.0") }}.

Notice that the QueryInterface() method implemented by the component simply calls the generateQI() method provided by the XPCOMUtils code module.

Create an array of component constructors

You need to create an array that lists the constructors for each component. This array can of course have just one entry:

var components = [MyComponent];

Here, we're calling the array components.

Create the NSGetFactory() or NSGetModule() entry point

Finally, you need to implement the NSGetModule() entry point so Gecko can start up your component:

// "components" is the array created in the previous section
if ("generateNSGetFactory" in XPCOMUtils)
  var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);  // Gecko 2.0+
else
  var NSGetModule = XPCOMUtils.generateNSGetModule(components);    // Gecko 1.9.x

Method overview

function defineLazyGetter(aObject, aName, aLambda); {{ gecko_minversion_inline("1.9.2") }}
function defineLazyModuleGetter(aObject, aName, aResource, [optional] aSymbol); {{ gecko_minversion_inline("5.0") }}
function defineLazyServiceGetter(aObject, aName, aContract, aInterfaceName); {{ gecko_minversion_inline("1.9.2") }}
nsIModule generateModule(componentsArray, postRegister, preUnregister); {{ obsolete_inline("2.0") }}
function generateNSGetFactory(componentsArray); {{ gecko_minversion_inline("2.0") }}
function generateNSGetModule(componentsArray, postRegister, preUnregister); {{ obsolete_inline("2.0") }}
function generateCI(classInfo); {{ gecko_minversion_inline("7.0") }}
function generateQI(interfaces);
void importRelative(that, path); {{ gecko_minversion_inline("6.0") }}

Attributes

Attribute Type Description
categoryManager {{ interface("nsICategoryManager") }} Returns a reference to {{ interface("nsICategoryManager") }}.

Methods

{{ method_gecko_minversion("defineLazyGetter", "1.9.2") }}

Defines a function on a specified object that acts as a getter which will be created the first time it's used.

function defineLazyGetter(
  aObject,
  aName,
  aLambda
); 
Parameters
aObject
The object into which to add the new lazy getter function.
aName
The name of the getter function to create.
aLambda
A function that returns the value the getter should return. This function is called exactly once.  this will reference aObject during execution of the function.

{{ method_gecko_minversion("defineLazyModuleGetter", "5.0") }}

Defines a getter on a specified object for a module. The module will not be imported until first use.

function defineLazyModuleGetter(
  aObject,
  aName,
  aResource,
  aSymbol
); 
Parameters
aObject
The object to define the lazy getter on.
aName
The name of the getter to define on aObject for the module.
aResource
The URL used to obtain the module.
aSymbol
The name of the symbol exported by the module. This parameter is optional and defaults to aName.

{{ method_gecko_minversion("defineLazyServiceGetter", "1.9.2") }}

Defines a function on a specified object which acts as a getter for a service. The service isn't obtained until the first time it's used.

function defineLazyServiceGetter(
  aObject,
  aName,
  aContract,
  aInterfaceName
);
Parameters
aObject
The object into which to add the new lazy service getter function.
aName
The name of the getter function to create.
aContract
The contract to use to obtain the service.
aInterfaceName
The name of the interface to query the service to.

{{ method_obsolete_gecko("generateModule", "2.0") }}

Generates a module implementation.

nsIModule generateModule(
  componentsArray,
  postRegister,
  preUnregister
); 
Parameters
componentsArray
An array of component constructors.
postRegister
An optional function that is called after the module is registered. See Post-registration callback.
preUnregister
An optional function that's called before the module is unregistered. See Pre-unregistration callback.
Return value

An {{ interface("nsIModule") }} that implements the module.

{{ method_gecko_minversion("generateNSGetFactory", "2.0") }}

Generates the NSGetFactory() function along with the factory definition.

Function generateNSGetFactory(
  componentsArray
); 
Parameters
componentsArray
An array of component constructors.
Return value

A function that will return the factory for the components and can be assigned to NSGetFactory global variable.

{{ method_obsolete_gecko("generateNSGetModule", "2.0") }}

Generates the NSGetModule() function along with the module definition. You can use this instead of generateModule() to create both the module and the NSGetModule() function at the same time.

nsIModule generateNSGetModule(
  componentsArray,
  postRegister,
  preUnregister
); 
Parameters
componentsArray
An array of component constructors.
postRegister
An optional function that is called after the module is registered. See Post-registration callback.
preUnregister
An optional function that's called before the module is unregistered. See Pre-unregistration callback.
Return value

An {{ interface("nsIModule") }} that implements the module.

{{ method_gecko_minversion("generateCI", "7.0") }}

Generates an {{ interface("nsIClassInfo") }} implementation for a component. The returned object should be assigned to the classInfo property of a JS object, the QueryInterface() function generated by generateQI will return it automatically then.

function generateCI(
  classInfo
); 
Parameters
classInfo
An object containing the optional properties interfaces, contractID, classDescription, classID, flags. This parameter should not be the component itself because that would cause a memory leak.
Return value

An {{ interface("nsIClassInfo") }} implementation returning the values of the properties from the classInfo parameter in its various properties.

Exceptions thrown

This method throws an exception with the message "In generateCI, don't use a component for generating classInfo" if classInfo parameter is an XPCOM component.

generateQI()

Generates a QueryInterface() function implementation. You need to assign the returned function to the QueryInterface property of a JavaScript object.

When the generated method is invoked on that object, it checks to see if the specified IID is listed in the array specified by the interfaces parameter; if it is, this (that is, the object itself) is returned. Otherwise, null is returned.

function generateQI(
  interfaces
); 
Parameters
interfaces
An array of interfaces implemented by the component.
Return value

A QueryInterface() function implementation.

{{ method_gecko_minversion("importRelative", "6.0") }}

Imports a JavaScript code module given the calling JavaScript code module's global object (you should specify this) and a path relative to that module. This lets modules bundled in a package load one another without having to hard-code full paths.

void importRelative(
  that,
  path
); 
Parameters
that
The JavaScript Object of the calling JavaScript code module's global scope. You should simply pass this.
path
The relative path of the JavaScript code module to import.
Remarks

This lets an extension bundle its own JavaScript code modules within itself and have them load one another. For example, if an extension named "MyExtension" bundles foo.jsm and bar.jsm, and foo.jsm needs to load bar.jsm, it can do so like this:

Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.importRelative(this, "bar.jsm"); 

Post-registration callback

The post-registration callback called by generateModule() should have the following signature:

postRegister(
  nsIComponentManager compMgr,
  nsIFile fileSpec,
  componentsArray
);
Parameters
compMgr
An {{ interface("nsIComponentManager") }} instance to use for managing the component.
fileSpec
An {{ interface("nsIFile") }} instance for... what?
componentsArray
An array of the components, as passed to generateModule().

The function doesn't need to return a value.

Pre-unregistration callback

The pre-unregistration callback passed to generateModule() should have the following signature:

preUnregister(
  nsIComponentManager compMgr,
  nsIFile fileSpec,
  componentsArray
); 
Parameters
compMgr
The {{ interface("nsIComponentManager") }} instance to use for managing the component.
fileSpec
An {{ interface("nsIFile") }} instance for... what?
componentsArray
The array of components passed to generateModule().

This function doesn't need to return a value.

See also

{{ languages( { "es": "es/XPCOMUtils.jsm", "ja": "ja/XPCOMUtils.jsm" } ) }}

Revision Source

<p>{{ gecko_minversion_header("1.9") }}</p>
<p>The <span style="font-family: monospace;">XPCOMUtils</span><code>.jsm</code> JavaScript code module offers utility routines for JavaScript components loaded by the JavaScript component loader.</p>
<p>To use this, you first need to import the code module into your JavaScript scope:</p>
<pre>Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
</pre>
<h2 id="Using_XPCOMUtils">Using XPCOMUtils</h2>
<p>Exposing a JavaScript class as a component using these utility methods requires four key steps:</p>
<ol> <li>Import <code>XPCOMUtils.jsm</code>, as explained previously.</li> <li>Declare the class (or multiple classes) implementing the component(s).</li> <li>Create an array of component constructors.</li> <li>Define the <code>NSGetFactory()</code> or <code>NSGetModule()</code> entry point.</li>
</ol>
<h3 id="Pseudocode">Pseudocode</h3>
<p>This section provides some pseudocode that demonstrates how to put together a JavaScript class based on the steps listed above.</p>
<h4 id="Constructor">Constructor</h4>
<p>The constructor is a simple method that handles any required initialization tasks.</p>
<pre class="brush: js">function MyComponent() {
  // initialize the component here
} 
</pre>
<h4 id="Class_declaration">Class declaration</h4>
<p>Declare the class prototype, using a form similar to this.</p>
<pre class="brush: js">MyComponent.prototype = {
  // properties required for XPCOM registration:
  classDescription: "unique text description",
  classID:          Components.ID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"),
  contractID:       "@example.com/xxx;1",

  // [optional] custom factory (an object implementing nsIFactory). If not
  // provided, the default factory is used, which returns
  // |(new MyComponent()).QueryInterface(iid)| in its createInstance().
  _xpcom_factory: { ... },

  // [optional] an array of categories to register this component in.
  _xpcom_categories: [{
    // Each object in the array specifies the parameters to pass to
    // nsICategoryManager.addCategoryEntry(). 'true' is passed for
    // both aPersist and aReplace params.
    category: "some-category",
    // optional, defaults to the object's classDescription
    entry: "entry name",
    // optional, defaults to the object's contractID (unless
    // 'service' is specified)
    value: "...",
    // optional, defaults to false. When set to true, and only if 'value'
    // is not specified, the concatenation of the string "service," and the
    // object's contractID is passed as aValue parameter of addCategoryEntry.
    service: true,
    // optional array of applications' IDs in the form:
    // [ "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}", ... ]
    // If this is defined, the component is registered in this
    // category only on the specified applications.
    apps: [ ... ]
  }],

  // QueryInterface implementation, e.g. using the generateQI helper
  QueryInterface: XPCOMUtils.generateQI(
    [Components.interfaces.nsIObserver,
     Components.interfaces.nsIMyInterface,
     "nsIFoo",
     "nsIBar" ]),

  // [optional] classInfo implementation, e.g. using the generateCI helper.
  // Will be automatically returned from QueryInterface if that was
  // generated with the generateQI helper.
  classInfo: XPCOMUtils.generateCI(
    {classID: Components.ID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"),
     contractID: "@example.com/xxx;1",
     classDescription: "unique text description",
     interfaces: [Components.interfaces.nsIObserver,
                  Components.interfaces.nsIMyInterface,
                  "nsIFoo",
                  "nsIBar"],
     flags: Ci.nsIClassInfo.SINGLETON}),

  // ...component implementation...
};
</pre>
<div class="geckoVersionNote"> <p>{{ gecko_callout_heading("2.0") }}</p> <p>The ability to register the component in a category only on specific applications by adding the apps field to a category entry was added in Gecko 2.0 {{ geckoRelease("2.0") }}.</p>
</div>
<p>Notice that the <code>QueryInterface()</code> method implemented by the component simply calls the <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateQI()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateQI()"><code>generateQI()</code></a> method provided by the XPCOMUtils code module.</p>
<h4 id="Create_an_array_of_component_constructors">Create an array of component constructors</h4>
<p>You need to create an array that lists the constructors for each component. This array can of course have just one entry:</p>
<pre>var components = [MyComponent];
</pre>
<p>Here, we're calling the array <code>components</code>.</p>
<h4 id="Create_the_NSGetFactory()_or_NSGetModule() entry_point">Create the NSGetFactory() or NSGetModule() entry point</h4>
<p>Finally, you need to implement the <code>NSGetModule()</code> entry point so Gecko can start up your component:</p>
<pre>// "components" is the array created in the previous section
if ("generateNSGetFactory" in XPCOMUtils)
  var NSGetFactory = XPCOMUtils.generateNSGetFactory(components);  // Gecko 2.0+
else
  var NSGetModule = XPCOMUtils.generateNSGetModule(components);    // Gecko 1.9.x</pre>
<h2 id="Method_overview">Method overview</h2>
<table class="standard-table"> <tbody> <tr> <td><code>function <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#defineLazyGetter()" title="en/JavaScript code modules/XPCOMUtils.jsm#defineLazyGetter()">defineLazyGetter</a>(aObject, aName, aLambda);</code> {{ gecko_minversion_inline("1.9.2") }}</td> </tr> <tr> <td><code>function <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#defineLazyModuleGetter()" title="en/JavaScript code modules/XPCOMUtils.jsm#defineLazyModuleGetter()">defineLazyModuleGetter</a>(aObject, aName, aResource, [optional] aSymbol);</code> {{ gecko_minversion_inline("5.0") }}</td> </tr> <tr> <td><code>function <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#defineLazyServiceGetter()" title="en/JavaScript code modules/XPCOMUtils.jsm#defineLazyServiceGetter()">defineLazyServiceGetter</a>(aObject, aName, aContract, aInterfaceName);</code> {{ gecko_minversion_inline("1.9.2") }}</td> </tr> <tr> <td><code>nsIModule <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateModule()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateModule()">generateModule</a>(componentsArray, postRegister, preUnregister);</code> {{ obsolete_inline("2.0") }}</td> </tr> <tr> <td><code>function <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateNSGetFactory()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateNSGetFactory()">generateNSGetFactory</a>(componentsArray);</code> {{ gecko_minversion_inline("2.0") }}</td> </tr> <tr> <td><code>function <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateNSGetModule()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateNSGetModule()">generateNSGetModule</a>(componentsArray, postRegister, preUnregister);</code> {{ obsolete_inline("2.0") }}</td> </tr> <tr> <td><code>function <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateCI()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateCI()">generateCI</a>(classInfo); </code>{{ gecko_minversion_inline("7.0") }}</td> </tr> <tr> <td><code>function <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateQI()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateQI()">generateQI</a>(interfaces);</code></td> </tr> <tr> <td><code>void <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#importRelative()" title="en/JavaScript code modules/XPCOMUtils.jsm#importRelative()">importRelative</a>(that, path);</code> {{ gecko_minversion_inline("6.0") }}</td> </tr> </tbody>
</table>
<h2 id="Attributes">Attributes</h2>
<table class="standard-table"> <tbody> <tr> <td class="header">Attribute</td> <td class="header">Type</td> <td class="header">Description</td> </tr> <tr> <td><code>categoryManager</code></td> <td>{{ interface("nsICategoryManager") }}</td> <td>Returns a reference to {{ interface("nsICategoryManager") }}.</td> </tr> </tbody>
</table>
<h2 id="Methods">Methods</h2>
<p>{{ method_gecko_minversion("defineLazyGetter", "1.9.2") }}</p>
<p>Defines a function on a specified object that acts as a getter which will be created the first time it's used.</p>
<pre><code>function defineLazyGetter(
  aObject,
  aName,
  aLambda
);</code> </pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>aObject<br> </code></dt> <dd>The object into which to add the new lazy getter function.</dd> <dt><code>aName</code></dt> <dd>The name of the getter function to create.</dd> <dt><code>aLambda</code></dt> <dd>A function that returns the value the getter should return. This function is called exactly once.  <code>this</code> will reference <code>aObject</code> during execution of the function.</dd>
</dl>
<p>{{ method_gecko_minversion("defineLazyModuleGetter", "5.0") }}</p>
<p>Defines a getter on a specified object for a module. The module will not be imported until first use.</p>
<pre><code>function defineLazyModuleGetter(
  aObject,
  aName,
  aResource,
  aSymbol
);</code> </pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>aObject</code></dt> <dd>The object to define the lazy getter on.</dd> <dt><code>aName</code></dt> <dd>The name of the getter to define on <code>aObject</code> for the module.</dd> <dt><code>aResource</code></dt> <dd>The URL used to obtain the module.</dd> <dt><code>aSymbol</code></dt> <dd>The name of the symbol exported by the module. This parameter is optional and defaults to <code>aName</code>.</dd>
</dl>
<p>{{ method_gecko_minversion("defineLazyServiceGetter", "1.9.2") }}</p>
<p>Defines a function on a specified object which acts as a getter for a service. The service isn't obtained until the first time it's used.</p>
<pre>function defineLazyServiceGetter(
  aObject,
  aName,
  aContract,
  aInterfaceName
);
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>aObject</code></dt> <dd>The object into which to add the new lazy service getter function.</dd> <dt><code>aName</code></dt> <dd>The name of the getter function to create.</dd> <dt><code>aContract</code></dt> <dd>The contract to use to obtain the service.</dd> <dt><code>aInterfaceName</code></dt> <dd>The name of the interface to query the service to.</dd>
</dl>
<p>{{ method_obsolete_gecko("generateModule", "2.0") }}</p>
<p>Generates a module implementation.</p>
<pre>nsIModule generateModule(
  componentsArray,
  postRegister,
  preUnregister
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>componentsArray</code></dt> <dd>An array of component constructors.</dd> <dt><code>postRegister</code></dt> <dd>An optional function that is called after the module is registered. See Post-registration callback.</dd> <dt><code>preUnregister</code></dt> <dd>An optional function that's called before the module is unregistered. See Pre-unregistration callback.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>An {{ interface("nsIModule") }} that implements the module.</p>
<p>{{ method_gecko_minversion("generateNSGetFactory", "2.0") }}</p>
<p>Generates the <code>NSGetFactory()</code> function along with the factory definition.</p>
<pre>Function generateNSGetFactory(
  componentsArray
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>componentsArray</code></dt> <dd>An array of component constructors.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>A function that will return the factory for the components and can be assigned to <code>NSGetFactory</code> global variable.</p>
<p>{{ method_obsolete_gecko("generateNSGetModule", "2.0") }}</p>
<p>Generates the <code>NSGetModule()</code> function along with the module definition. You can use this instead of <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateModule()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateModule()"><code>generateModule()</code></a> to create both the module and the <code>NSGetModule()</code> function at the same time.</p>
<pre>nsIModule generateNSGetModule(
  componentsArray,
  postRegister,
  preUnregister
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>componentsArray</code></dt> <dd>An array of component constructors.</dd> <dt><code>postRegister</code></dt> <dd>An optional function that is called after the module is registered. See Post-registration callback.</dd> <dt><code>preUnregister</code></dt> <dd>An optional function that's called before the module is unregistered. See Pre-unregistration callback.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>An {{ interface("nsIModule") }} that implements the module.</p>
<p>{{ method_gecko_minversion("generateCI", "7.0") }}</p>
<p>Generates an {{ interface("nsIClassInfo") }} implementation for a component. The returned object should be assigned to the <code>classInfo</code> property of a JS object, the <code>QueryInterface()</code> function generated by <code>generateQI</code> will return it automatically then.</p>
<pre>function generateCI(
  classInfo
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt>classInfo</dt> <dd>An object containing the optional properties <code>interfaces</code>, <code>contractID</code>, <code>classDescription</code>, <code>classID</code>, <code>flags</code>. This parameter should not be the component itself because that would cause a memory leak.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>An {{ interface("nsIClassInfo") }} implementation returning the values of the properties from the <code>classInfo</code> parameter in its various properties.</p>
<h6 id="Exceptions_thrown">Exceptions thrown</h6>
<p>This method throws an exception with the message "In generateCI, don't use a component for generating classInfo" if <code>classInfo</code> parameter is an XPCOM component.</p>
<h3 id="generateQI()">generateQI()</h3>
<p>Generates a <code>QueryInterface()</code> function implementation. You need to assign the returned function to the <code>QueryInterface</code> property of a JavaScript object.</p>
<p>When the generated method is invoked on that object, it checks to see if the specified IID is listed in the array specified by the <code>interfaces</code> parameter; if it is, <code>this</code> (that is, the object itself) is returned. Otherwise, <code>null</code> is returned.</p>
<pre>function generateQI(
  interfaces
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt>interfaces</dt> <dd>An array of interfaces implemented by the component.</dd>
</dl>
<h6 id="Return_value">Return value</h6>
<p>A <code>QueryInterface()</code> function implementation.</p>
<p>{{ method_gecko_minversion("importRelative", "6.0") }}</p>
<p>Imports a JavaScript code module given the calling JavaScript code module's global object (you should specify <code>this</code>) and a path relative to that module. This lets modules bundled in a package load one another without having to hard-code full paths.</p>
<pre>void importRelative(
  that,
  path
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>that</code></dt> <dd>The JavaScript Object of the calling JavaScript code module's global scope. You should simply pass <code>this</code>.</dd> <dt><code>path</code></dt> <dd>The relative path of the JavaScript code module to import.</dd>
</dl>
<h6 id="Remarks">Remarks</h6>
<p>This lets an extension bundle its own JavaScript code modules within itself and have them load one another. For example, if an extension named "MyExtension" bundles <code>foo.jsm</code> and <code>bar.jsm</code>, and foo.jsm needs to load <code>bar.jsm</code>, it can do so like this:</p>
<pre class="deki-transform">Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
XPCOMUtils.importRelative(this, "bar.jsm"); </pre>
<h2 id="Post-registration_callback">Post-registration callback</h2>
<p>The post-registration callback called by <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateModule()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateModule()"><code>generateModule()</code></a> should have the following signature:</p>
<pre>postRegister(
  nsIComponentManager compMgr,
  nsIFile fileSpec,
  componentsArray
);
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>compMgr</code></dt> <dd>An {{ interface("nsIComponentManager") }} instance to use for managing the component.</dd> <dt><code>fileSpec</code></dt> <dd>An {{ interface("nsIFile") }} instance for... what?</dd> <dt><code>componentsArray</code></dt> <dd>An array of the components, as passed to <code>generateModule()</code>.</dd>
</dl>
<p>The function doesn't need to return a value.</p>
<h2 id="Pre-unregistration_callback">Pre-unregistration callback</h2>
<p>The pre-unregistration callback passed to <a href="/en/JavaScript_code_modules/XPCOMUtils.jsm#generateModule()" title="en/JavaScript code modules/XPCOMUtils.jsm#generateModule()"><code>generateModule()</code></a> should have the following signature:</p>
<pre>preUnregister(
  nsIComponentManager compMgr,
  nsIFile fileSpec,
  componentsArray
); 
</pre>
<h6 id="Parameters">Parameters</h6>
<dl> <dt><code>compMgr</code></dt> <dd>The {{ interface("nsIComponentManager") }} instance to use for managing the component.</dd> <dt><code>fileSpec</code></dt> <dd>An {{ interface("nsIFile") }} instance for... what?</dd> <dt><code>componentsArray</code></dt> <dd>The array of components passed to <code>generateModule()</code>.</dd>
</dl>
<p>This function doesn't need to return a value.</p>
<h2 id="See_also">See also</h2>
<ul> <li><a class="internal" href="/en/JavaScript_code_modules/Using" title="en/JavaScript code modules/Using JavaScript code
    modules">Using JavaScript code modules</a></li> <li><a class="internal" href="/en/JavaScript_code_modules" title="en/JavaScript code
    modules">JavaScript code modules</a></li>
</ul>
<p>{{ languages( { "es": "es/XPCOMUtils.jsm", "ja": "ja/XPCOMUtils.jsm" } ) }}</p>
Revert to this revision