I came across these in the code and I am wondering what they mean.
const { components, Ci, Cr, Cu, CC } = require("chrome");
This seems to indicate that the C stands for chrome. But that’s all I can read into that. What’s the background?
I came across these in the code and I am wondering what they mean.
const { components, Ci, Cr, Cu, CC } = require("chrome");
This seems to indicate that the C stands for chrome. But that’s all I can read into that. What’s the background?
These are structures to interface with XPCOM. Ci
is a shorthand for Components.interfaces
, Cr
for Components.results
, Cu
for Components.utils
and CC
for Components.Constructor
. I don’t think documentation for them has made it to https://firefox-source-docs.mozilla.org yet, so you can find the old MDN content at https://github.com/mdn/archived-content/tree/main/files/en-us/mozilla/tech/xpcom/language_bindings
There is some information about XPCOM here but it does indeed not seem to include an answer to my question.
To maybe copy a bit from the components.interfaces docs in that repo:
Components.interfaces
is a read-only object whose properties implement thensIJSIID
interface. Each object represents one of the XPCOM interfaces – that some component or another might or might not implement. It reflects only those interfaces which have been designated in their.idl
description asscriptable
, that is the interfaces which XPConnect is capable of reflecting into JavaScript.Properties of the
Components.interfaces
object are used where XPCOM methods expect a parameter of typensID
. This includesnsISupports.QueryInterface()
, the optional parameter accepted bynsIJSCID.getService()
,nsIJSCID.createInstance()
when called from JavaScript, andnsIClassInfo.getInterfaces()
.The properties of the
Components.interfaces
object can be enumerated using a for…in loop.
So what happens for example here:
const imageTools = Cc["@mozilla.org/image/tools;1"].getService(Ci.imgITools);
seems to be that Cc creates a new XPCOM object which is then used via the interface Ci.imgITools.
Is that about right?
getService
creates a reference to a service, which is a singleton-like structure in XPCOM. To get a separate instance you’d use createInstance
as mentioned in your quote. The interface you pass in is the interface the XPCOM wrappers will expose, the underlying service/component may also implement other interfaces that will not be available (but you can use QueryInterface
to add them).