Visit Mozilla.org

Fragmenty kodu:JS XPCOM

z Mozilla Developer Center, polskiego centrum programistów Mozilli.

UWAGA: Tłumaczenie tej strony nie zostało zakończone.
Może być ona niekompletna lub wymagać korekty.
Chcesz pomóc? | Dokończ tłumaczenie | Sprawdź ortografię | Więcej takich stron...

Here are a few useful snippets of code for dealing with XPCOM components in JavaScript.

Spis treści

[edytuj] Contract IDs

A contract ID allows is a unique name for an XPCOM object. They are used to create or access a well known object in XPCOM.

[edytuj] Interfaces

Every XPCOM object implements one or more interfaces. An interface is simply a list of constants and methods that can be called on the object, an example is nsIFile. Every XPCOM object must implement the nsISupports interface.

[edytuj] Accessing XPCOM components from JavaScript

XPCOM objects are either created as new instances (each creation gives you a completely new object) or as services (each access gives you the same object, often called a singleton). Whether you must create a new instance or access as a service depends on the object. In order to get an XPCOM object you need to know the contract ID of the object and the interface that you wish to use on it.

[edytuj] Creating an instance of a component

var component = Components.classes["@mozilla.org/file/local;1"]
                          .createInstance(Components.interfaces.nsIFile);

This creates a new instance of the object with contract ID @mozilla.org/file/local;1 and allows you to call methods from the nsIFile interface on it.

[edytuj] Getting an XPCOM service

var preferences = Components.classes["@mozilla.org/preferences-service;1"]
                            .getService(Components.interfaces.nsIPrefService);

You can then call any methods in the nsIPrefService interface on the preferences object.

[edytuj] Getting a different interface for a component

Some components implement more than one interface. Sometimes JavaScript is clever enough to know all the interfaces available on a component, but in most cases you will have to explicitly check for an interface. With the preferences service from the previous example we can do the following:

preferences = preferences.QueryInterface(Components.interfaces.nsIPrefBranch2);

This allows you to use the methods in the nsIPrefBranch2 interface.

[edytuj] Implementing XPCOM components in JavaScript

Here is a simple stub of a JavaScript XPCOM component. In order to use it you must do the following:

  • Replace the 3 strings at the top of the initModule with your own.
  • Change the test in the QueryInterface method to work for any interfaces you are implementing.
  • Add methods to the prototype that appear in the interfaces you are implementing.
  • Add any initialisation code you want into the ExampleComponent constructor.
function ExampleComponent()
{
	// Add any initialisation for your component here.
}

ExampleComponent.prototype = {
QueryInterface: function(iid)
{
	if (iid.equals(Components.interfaces.myInterface)
		|| iid.equals(Ci.nsISupports))
	{
		return this;
	}
	else
	{
		throw Components.results.NS_ERROR_NO_INTERFACE;
	}
}
};

var initModule =
{
	ServiceCID: Components.ID("{examplee-xamp-leex-ampl-eexampleexam}"),  // Insert a guid in the quotes
	ServiceContractID: "@example.com/example;1",                          // Insert a contract ID in the quotes
	ServiceName: "",                                                      // Insert your own name in the quotes
	
	registerSelf: function (compMgr, fileSpec, location, type)
	{
		compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
		compMgr.registerFactoryLocation(this.ServiceCID,this.ServiceName,this.ServiceContractID,
			fileSpec,location,type);
	},

	unregisterSelf: function (compMgr, fileSpec, location)
	{
		compMgr = compMgr.QueryInterface(Ci.nsIComponentRegistrar);
		compMgr.unregisterFactoryLocation(this.ServiceCID,fileSpec);
	},

	getClassObject: function (compMgr, cid, iid)
	{
		if (!cid.equals(this.ServiceCID))
			throw Components.results.NS_ERROR_NO_INTERFACE
		if (!iid.equals(Components.interfaces.nsIFactory))
			throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
		return this.instanceFactory;
	},

	canUnload: function(compMgr)
	{
		return true;
	},

	instanceFactory:
	{
		createInstance: function (outer, iid)
		{
			if (outer != null)
				throw Components.results.NS_ERROR_NO_AGGREGATION;
			return new ExampleComponent().QueryInterface(iid);
		}
	}
}; //Module

function NSGetModule(compMgr, fileSpec)
{
	return initModule;
}