123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- "use strict";
- module.metadata = {
- "stability": "unstable"
- };
- const { Cc, Ci, Cr, Cm, components: { classesByID } } = require('chrome');
- const { registerFactory, unregisterFactory, isCIDRegistered } =
- Cm.QueryInterface(Ci.nsIComponentRegistrar);
- const { merge } = require('../util/object');
- const { Class, extend, mix } = require('../core/heritage');
- const { uuid } = require('../util/uuid');
- const Unknown = new function() {
- function hasInterface(component, iid) {
- return component && component.interfaces &&
- ( component.interfaces.some(function(id) iid.equals(Ci[id])) ||
- component.implements.some(function($) hasInterface($, iid)) ||
- hasInterface(Object.getPrototypeOf(component), iid));
- }
- return Class({
- /**
- * The `QueryInterface` method provides runtime type discovery used by XPCOM.
- * This method return queried instance of `this` if given `iid` is listed in
- * the `interfaces` property or in equivalent properties of objects in it's
- * prototype chain. In addition it will look up in the prototypes under
- * `implements` array property, this ways compositions made via `Class`
- * utility will carry interfaces implemented by composition components.
- */
- QueryInterface: function QueryInterface(iid) {
-
-
-
-
- if (iid && !hasInterface(this, iid))
- throw Cr.NS_ERROR_NO_INTERFACE;
- return this;
- },
-
- interfaces: Object.freeze([ 'nsISupports' ])
- });
- }
- exports.Unknown = Unknown;
- const Factory = Class({
- extends: Unknown,
- interfaces: [ 'nsIFactory' ],
-
- get id() { throw Error('Factory must implement `id` property') },
-
- contract: null,
-
- description: 'Jetpack generated factory',
-
- lockFactory: function lockFactory(lock) undefined,
- /**
- * If property is `true` XPCOM service / factory will be registered
- * automatically on creation.
- */
- register: true,
- /**
- * If property is `true` XPCOM factory will be unregistered prior to add-on
- * unload.
- */
- unregister: true,
- /**
- * Method is called on `Service.new(options)` passing given `options` to
- * it. Options is expected to have `component` property holding XPCOM
- * component implementation typically decedent of `Unknown` or any custom
- * implementation with a `new` method and optional `register`, `unregister`
- * flags. Unless `register` is `false` Service / Factory will be
- * automatically registered. Unless `unregister` is `false` component will
- * be automatically unregistered on add-on unload.
- */
- initialize: function initialize(options) {
- merge(this, {
- id: 'id' in options ? options.id : uuid(),
- register: 'register' in options ? options.register : this.register,
- unregister: 'unregister' in options ? options.unregister : this.unregister,
- contract: 'contract' in options ? options.contract : null,
- Component: options.Component
- });
-
- if (this.register)
- register(this);
- },
-
- createInstance: function createInstance(outer, iid) {
- try {
- if (outer)
- throw Cr.NS_ERROR_NO_AGGREGATION;
- return this.create().QueryInterface(iid);
- }
- catch (error) {
- throw error instanceof Ci.nsIException ? error : Cr.NS_ERROR_FAILURE;
- }
- },
- create: function create() this.Component()
- });
- exports.Factory = Factory;
- // Exemplar for creating services that implement `nsIFactory` interface, that
- // can be registered into runtime via call to `register`. This services return
- // enclosed `component` on `getService`.
- const Service = Class({
- extends: Factory,
- initialize: function initialize(options) {
- this.component = options.Component();
- Factory.prototype.initialize.call(this, options);
- },
- description: 'Jetpack generated service',
-
- create: function create() this.component
- });
- exports.Service = Service;
- function isRegistered({ id }) isCIDRegistered(id)
- exports.isRegistered = isRegistered;
- /**
- * Registers given `component` object to be used to instantiate a particular
- * class identified by `component.id`, and creates an association of class
- * name and `component.contract` with the class.
- */
- function register(factory) {
- if (!(factory instanceof Factory)) {
- throw new Error("xpcom.register() expect a Factory instance.\n" +
- "Please refactor your code to new xpcom module if you" +
- " are repacking an addon from SDK <= 1.5:\n" +
- "https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/api-utils/xpcom.html");
- }
- registerFactory(factory.id, factory.description, factory.contract, factory);
- if (factory.unregister)
- require('../system/unload').when(unregister.bind(null, factory));
- }
- exports.register = register;
- function unregister(factory) {
- if (isRegistered(factory))
- unregisterFactory(factory.id, factory);
- }
- exports.unregister = unregister;
- function autoRegister(path) {
-
-
-
-
-
-
-
- var runtime = require("../system/runtime");
- var osDirName = runtime.OS + "_" + runtime.XPCOMABI;
- var platformVersion = require("../system/xul-app").platformVersion.substring(0, 5);
- var file = Cc['@mozilla.org/file/local;1']
- .createInstance(Ci.nsILocalFile);
- file.initWithPath(path);
- file.append(osDirName);
- file.append(platformVersion);
- if (!(file.exists() && file.isDirectory()))
- throw new Error("component not available for OS/ABI " +
- osDirName + " and platform " + platformVersion);
- Cm.QueryInterface(Ci.nsIComponentRegistrar);
- Cm.autoRegister(file);
- }
- exports.autoRegister = autoRegister;
- function factoryByID(id) classesByID[id] || null
- exports.factoryByID = factoryByID;
- /**
- * Returns factory registered with a given `contract` or `null` if not found.
- * In contrast to `Cc[contract]` that does ignores new factory registration
- * with a given `contract` this will return a factory currently associated
- * with a `contract`.
- */
- function factoryByContract(contract) factoryByID(Cm.contractIDToCID(contract))
- exports.factoryByContract = factoryByContract;
|