2014-06-24 22:12:07 -07:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
|
2009-08-31 09:32:05 -07:00
|
|
|
* vim: sw=2 ts=2 sts=2 et filetype=javascript
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-05-15 16:27:40 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utilities for JavaScript components loaded by the JS component
|
|
|
|
* loader.
|
|
|
|
*
|
|
|
|
* Import into a JS component using
|
2007-07-14 14:43:35 -07:00
|
|
|
* 'Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");'
|
2007-05-15 16:27:40 -07:00
|
|
|
*
|
2007-06-20 21:53:35 -07:00
|
|
|
* Exposing a JS 'class' as a component using these utility methods consists
|
|
|
|
* of several steps:
|
|
|
|
* 0. Import XPCOMUtils, as described above.
|
|
|
|
* 1. Declare the 'class' (or multiple classes) implementing the component(s):
|
|
|
|
* function MyComponent() {
|
|
|
|
* // constructor
|
|
|
|
* }
|
|
|
|
* MyComponent.prototype = {
|
|
|
|
* // properties required for XPCOM registration:
|
|
|
|
* classID: Components.ID("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"),
|
|
|
|
*
|
|
|
|
* // [optional] custom factory (an object implementing nsIFactory). If not
|
|
|
|
* // provided, the default factory is used, which returns
|
2007-07-18 13:19:05 -07:00
|
|
|
* // |(new MyComponent()).QueryInterface(iid)| in its createInstance().
|
2007-08-23 17:21:10 -07:00
|
|
|
* _xpcom_factory: { ... },
|
|
|
|
*
|
2010-07-14 10:09:42 -07:00
|
|
|
* // QueryInterface implementation, e.g. using the generateQI helper
|
|
|
|
* QueryInterface: XPCOMUtils.generateQI(
|
|
|
|
* [Components.interfaces.nsIObserver,
|
|
|
|
* Components.interfaces.nsIMyInterface,
|
|
|
|
* "nsIFoo",
|
|
|
|
* "nsIBar" ]),
|
|
|
|
*
|
2011-05-25 01:46:51 -07:00
|
|
|
* // [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}),
|
|
|
|
*
|
2010-07-14 10:09:42 -07:00
|
|
|
* // The following properties were used prior to Mozilla 2, but are no
|
|
|
|
* // longer supported. They may still be included for compatibility with
|
|
|
|
* // prior versions of XPCOMUtils. In Mozilla 2, this information is
|
|
|
|
* // included in the .manifest file which registers this JS component.
|
|
|
|
* classDescription: "unique text description",
|
|
|
|
* contractID: "@example.com/xxx;1",
|
|
|
|
*
|
2007-08-23 17:21:10 -07:00
|
|
|
* // [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.
|
2010-05-22 01:41:00 -07:00
|
|
|
* service: true,
|
|
|
|
* // optional, it can be an array of applications' IDs in the form:
|
|
|
|
* // [ "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}", ... ]
|
|
|
|
* // If defined the component will be registered in this category only for
|
|
|
|
* // the provided applications.
|
|
|
|
* apps: [...]
|
2007-08-23 17:21:10 -07:00
|
|
|
* }],
|
2007-06-20 21:53:35 -07:00
|
|
|
*
|
|
|
|
* // ...component implementation...
|
|
|
|
* };
|
|
|
|
*
|
|
|
|
* 2. Create an array of component constructors (like the one
|
|
|
|
* created in step 1):
|
|
|
|
* var components = [MyComponent];
|
|
|
|
*
|
2010-07-14 10:09:42 -07:00
|
|
|
* 3. Define the NSGetFactory entry point:
|
2012-10-31 09:13:28 -07:00
|
|
|
* this.NSGetFactory = XPCOMUtils.generateNSGetFactory(components);
|
2007-05-15 16:27:40 -07:00
|
|
|
*/
|
|
|
|
|
2007-06-20 21:53:35 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.EXPORTED_SYMBOLS = [ "XPCOMUtils" ];
|
2007-05-15 16:27:40 -07:00
|
|
|
|
2009-08-31 09:32:05 -07:00
|
|
|
const Cc = Components.classes;
|
2007-06-20 21:53:35 -07:00
|
|
|
const Ci = Components.interfaces;
|
2007-09-18 15:11:31 -07:00
|
|
|
const Cr = Components.results;
|
2011-03-30 10:40:47 -07:00
|
|
|
const Cu = Components.utils;
|
2007-06-20 21:53:35 -07:00
|
|
|
|
2012-10-31 09:13:28 -07:00
|
|
|
this.XPCOMUtils = {
|
2007-06-20 21:53:35 -07:00
|
|
|
/**
|
|
|
|
* Generate a QueryInterface implementation. The returned function must be
|
|
|
|
* assigned to the 'QueryInterface' property of a JS object. When invoked on
|
|
|
|
* that object, it checks if the given iid is listed in the |interfaces|
|
|
|
|
* param, and if it is, returns |this| (the object it was called on).
|
2011-05-25 01:46:51 -07:00
|
|
|
* If the JS object has a classInfo property it'll be returned for the
|
|
|
|
* nsIClassInfo IID, generateCI can be used to generate the classInfo
|
|
|
|
* property.
|
2007-06-20 21:53:35 -07:00
|
|
|
*/
|
2009-12-15 23:25:38 -08:00
|
|
|
generateQI: function XPCU_generateQI(interfaces) {
|
2009-06-15 16:06:20 -07:00
|
|
|
/* Note that Ci[Ci.x] == Ci.x for all x */
|
2014-07-16 22:45:29 -07:00
|
|
|
let a = [];
|
|
|
|
if (interfaces) {
|
|
|
|
for (let i = 0; i < interfaces.length; i++) {
|
|
|
|
let iface = interfaces[i];
|
|
|
|
if (Ci[iface]) {
|
|
|
|
a.push(Ci[iface].name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return makeQI(a);
|
2007-06-20 21:53:35 -07:00
|
|
|
},
|
2007-05-15 16:27:40 -07:00
|
|
|
|
2011-05-25 01:46:51 -07:00
|
|
|
/**
|
|
|
|
* Generate a ClassInfo implementation for a component. The returned object
|
|
|
|
* must be assigned to the 'classInfo' property of a JS object. The first and
|
|
|
|
* only argument should be an object that contains a number of optional
|
|
|
|
* properties: "interfaces", "contractID", "classDescription", "classID" and
|
|
|
|
* "flags". The values of the properties will be returned as the values of the
|
|
|
|
* various properties of the nsIClassInfo implementation.
|
|
|
|
*/
|
|
|
|
generateCI: function XPCU_generateCI(classInfo)
|
|
|
|
{
|
|
|
|
if (QueryInterface in classInfo)
|
|
|
|
throw Error("In generateCI, don't use a component for generating classInfo");
|
|
|
|
/* Note that Ci[Ci.x] == Ci.x for all x */
|
2014-07-16 22:45:29 -07:00
|
|
|
let _interfaces = [];
|
|
|
|
for (let i = 0; i < classInfo.interfaces.length; i++) {
|
|
|
|
let iface = classInfo.interfaces[i];
|
|
|
|
if (Ci[iface]) {
|
|
|
|
_interfaces.push(Ci[iface]);
|
|
|
|
}
|
|
|
|
}
|
2011-05-25 01:46:51 -07:00
|
|
|
return {
|
|
|
|
getInterfaces: function XPCU_getInterfaces(countRef) {
|
|
|
|
countRef.value = _interfaces.length;
|
|
|
|
return _interfaces;
|
|
|
|
},
|
|
|
|
getHelperForLanguage: function XPCU_getHelperForLanguage(language) null,
|
|
|
|
contractID: classInfo.contractID,
|
|
|
|
classDescription: classInfo.classDescription,
|
|
|
|
classID: classInfo.classID,
|
|
|
|
implementationLanguage: Ci.nsIProgrammingLanguage.JAVASCRIPT,
|
|
|
|
flags: classInfo.flags,
|
|
|
|
QueryInterface: this.generateQI([Ci.nsIClassInfo])
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
/**
|
2010-06-21 11:29:32 -07:00
|
|
|
* Generate a NSGetFactory function given an array of components.
|
2007-05-15 16:27:40 -07:00
|
|
|
*/
|
2010-06-21 11:29:32 -07:00
|
|
|
generateNSGetFactory: function XPCU_generateNSGetFactory(componentsArray) {
|
|
|
|
let classes = {};
|
2014-07-16 22:45:29 -07:00
|
|
|
for (let i = 0; i < componentsArray.length; i++) {
|
|
|
|
let component = componentsArray[i];
|
2010-06-27 18:05:47 -07:00
|
|
|
if (!(component.prototype.classID instanceof Components.ID))
|
|
|
|
throw Error("In generateNSGetFactory, classID missing or incorrect for component " + component);
|
|
|
|
|
2010-06-21 11:29:32 -07:00
|
|
|
classes[component.prototype.classID] = this._getFactory(component);
|
2007-06-20 21:53:35 -07:00
|
|
|
}
|
2010-06-21 11:29:32 -07:00
|
|
|
return function NSGetFactory(cid) {
|
|
|
|
let cidstring = cid.toString();
|
|
|
|
if (cidstring in classes)
|
|
|
|
return classes[cidstring];
|
|
|
|
throw Cr.NS_ERROR_FACTORY_NOT_REGISTERED;
|
2010-05-22 01:41:00 -07:00
|
|
|
}
|
2007-05-15 16:27:40 -07:00
|
|
|
},
|
|
|
|
|
2009-08-31 09:32:05 -07:00
|
|
|
/**
|
|
|
|
* Defines a getter on a specified object that will be created upon first use.
|
|
|
|
*
|
|
|
|
* @param aObject
|
|
|
|
* The object to define the lazy getter on.
|
|
|
|
* @param aName
|
|
|
|
* The name of the getter to define on aObject.
|
|
|
|
* @param aLambda
|
|
|
|
* A function that returns what the getter should return. This will
|
|
|
|
* only ever be called once.
|
|
|
|
*/
|
|
|
|
defineLazyGetter: function XPCU_defineLazyGetter(aObject, aName, aLambda)
|
|
|
|
{
|
2012-09-25 08:04:25 -07:00
|
|
|
Object.defineProperty(aObject, aName, {
|
|
|
|
get: function () {
|
|
|
|
delete aObject[aName];
|
|
|
|
return aObject[aName] = aLambda.apply(aObject);
|
|
|
|
},
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true
|
2009-08-31 09:32:05 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defines a getter on a specified object for a service. The service will not
|
|
|
|
* be obtained until first use.
|
|
|
|
*
|
|
|
|
* @param aObject
|
|
|
|
* The object to define the lazy getter on.
|
|
|
|
* @param aName
|
|
|
|
* The name of the getter to define on aObject for the service.
|
|
|
|
* @param aContract
|
|
|
|
* The contract used to obtain the service.
|
|
|
|
* @param aInterfaceName
|
|
|
|
* The name of the interface to query the service to.
|
|
|
|
*/
|
|
|
|
defineLazyServiceGetter: function XPCU_defineLazyServiceGetter(aObject, aName,
|
|
|
|
aContract,
|
|
|
|
aInterfaceName)
|
|
|
|
{
|
|
|
|
this.defineLazyGetter(aObject, aName, function XPCU_serviceLambda() {
|
2010-06-23 12:19:18 -07:00
|
|
|
return Cc[aContract].getService(Ci[aInterfaceName]);
|
2009-08-31 09:32:05 -07:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2011-03-30 10:40:47 -07:00
|
|
|
/**
|
|
|
|
* Defines a getter on a specified object for a module. The module will not
|
|
|
|
* be imported until first use.
|
|
|
|
*
|
|
|
|
* @param aObject
|
|
|
|
* The object to define the lazy getter on.
|
|
|
|
* @param aName
|
|
|
|
* The name of the getter to define on aObject for the module.
|
|
|
|
* @param aResource
|
|
|
|
* The URL used to obtain the module.
|
|
|
|
* @param aSymbol
|
|
|
|
* The name of the symbol exported by the module.
|
|
|
|
* This parameter is optional and defaults to aName.
|
|
|
|
*/
|
|
|
|
defineLazyModuleGetter: function XPCU_defineLazyModuleGetter(aObject, aName,
|
|
|
|
aResource,
|
|
|
|
aSymbol)
|
|
|
|
{
|
|
|
|
this.defineLazyGetter(aObject, aName, function XPCU_moduleLambda() {
|
|
|
|
var temp = {};
|
2014-05-19 10:12:49 -07:00
|
|
|
try {
|
|
|
|
Cu.import(aResource, temp);
|
|
|
|
} catch (ex) {
|
2014-05-21 16:48:19 -07:00
|
|
|
Cu.reportError("Failed to load module " + aResource + ".");
|
2014-05-19 10:12:49 -07:00
|
|
|
throw ex;
|
|
|
|
}
|
2011-03-30 10:40:47 -07:00
|
|
|
return temp[aSymbol || aName];
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2007-05-15 16:27:40 -07:00
|
|
|
/**
|
|
|
|
* Convenience access to category manager
|
|
|
|
*/
|
|
|
|
get categoryManager() {
|
|
|
|
return Components.classes["@mozilla.org/categorymanager;1"]
|
2007-06-20 21:53:35 -07:00
|
|
|
.getService(Ci.nsICategoryManager);
|
|
|
|
},
|
|
|
|
|
2010-02-09 17:05:31 -08:00
|
|
|
/**
|
|
|
|
* Helper which iterates over a nsISimpleEnumerator.
|
|
|
|
* @param e The nsISimpleEnumerator to iterate over.
|
|
|
|
* @param i The expected interface for each element.
|
|
|
|
*/
|
|
|
|
IterSimpleEnumerator: function XPCU_IterSimpleEnumerator(e, i)
|
|
|
|
{
|
|
|
|
while (e.hasMoreElements())
|
|
|
|
yield e.getNext().QueryInterface(i);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper which iterates over a string enumerator.
|
|
|
|
* @param e The string enumerator (nsIUTF8StringEnumerator or
|
|
|
|
* nsIStringEnumerator) over which to iterate.
|
|
|
|
*/
|
|
|
|
IterStringEnumerator: function XPCU_IterStringEnumerator(e)
|
|
|
|
{
|
|
|
|
while (e.hasMore())
|
|
|
|
yield e.getNext();
|
|
|
|
},
|
|
|
|
|
2007-06-20 21:53:35 -07:00
|
|
|
/**
|
|
|
|
* Returns an nsIFactory for |component|.
|
|
|
|
*/
|
2009-12-15 23:25:38 -08:00
|
|
|
_getFactory: function XPCOMUtils__getFactory(component) {
|
2007-06-20 21:53:35 -07:00
|
|
|
var factory = component.prototype._xpcom_factory;
|
|
|
|
if (!factory) {
|
|
|
|
factory = {
|
|
|
|
createInstance: function(outer, iid) {
|
2007-09-18 15:11:31 -07:00
|
|
|
if (outer)
|
|
|
|
throw Cr.NS_ERROR_NO_AGGREGATION;
|
2007-06-20 21:53:35 -07:00
|
|
|
return (new component()).QueryInterface(iid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return factory;
|
2011-04-13 15:10:13 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allows you to fake a relative import. Expects the global object from the
|
|
|
|
* module that's calling us, and the relative filename that we wish to import.
|
|
|
|
*/
|
2014-04-22 15:37:26 -07:00
|
|
|
importRelative: function XPCOMUtils__importRelative(that, path, scope) {
|
2011-04-13 15:10:13 -07:00
|
|
|
if (!("__URI__" in that))
|
|
|
|
throw Error("importRelative may only be used from a JSM, and its first argument "+
|
|
|
|
"must be that JSM's global object (hint: use this)");
|
|
|
|
let uri = that.__URI__;
|
|
|
|
let i = uri.lastIndexOf("/");
|
2014-04-22 15:37:26 -07:00
|
|
|
Components.utils.import(uri.substring(0, i+1) + path, scope || that);
|
2011-04-13 15:10:13 -07:00
|
|
|
},
|
2012-02-07 01:17:42 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* generates a singleton nsIFactory implementation that can be used as
|
|
|
|
* the _xpcom_factory of the component.
|
|
|
|
* @param aServiceConstructor
|
|
|
|
* Constructor function of the component.
|
|
|
|
*/
|
|
|
|
generateSingletonFactory:
|
|
|
|
function XPCOMUtils_generateSingletonFactory(aServiceConstructor) {
|
|
|
|
return {
|
|
|
|
_instance: null,
|
|
|
|
createInstance: function XPCU_SF_createInstance(aOuter, aIID) {
|
|
|
|
if (aOuter !== null) {
|
|
|
|
throw Cr.NS_ERROR_NO_AGGREGATION;
|
|
|
|
}
|
|
|
|
if (this._instance === null) {
|
|
|
|
this._instance = new aServiceConstructor();
|
|
|
|
}
|
|
|
|
return this._instance.QueryInterface(aIID);
|
|
|
|
},
|
|
|
|
lockFactory: function XPCU_SF_lockFactory(aDoLock) {
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
},
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory])
|
|
|
|
};
|
|
|
|
},
|
2007-05-15 16:27:40 -07:00
|
|
|
};
|
2007-06-20 21:53:35 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper for XPCOMUtils.generateQI to avoid leaks - see bug 381651#c1
|
|
|
|
*/
|
|
|
|
function makeQI(interfaceNames) {
|
|
|
|
return function XPCOMUtils_QueryInterface(iid) {
|
|
|
|
if (iid.equals(Ci.nsISupports))
|
|
|
|
return this;
|
2011-05-25 01:46:51 -07:00
|
|
|
if (iid.equals(Ci.nsIClassInfo) && "classInfo" in this)
|
|
|
|
return this.classInfo;
|
2014-07-16 22:45:29 -07:00
|
|
|
for (let i = 0; i < interfaceNames.length; i++) {
|
|
|
|
if (Ci[interfaceNames[i]].equals(iid))
|
2007-06-20 21:53:35 -07:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2007-09-18 15:11:31 -07:00
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
2007-06-20 21:53:35 -07:00
|
|
|
};
|
|
|
|
}
|