Changes

Jump to: navigation, search

Real World Mozilla First XPCOM Component

109 bytes added, 18:49, 27 February 2007
Defining an Interface
What does this interface say? To begin, notice the use of '''nsISupports'''. This is the fundamental interface that all XPCOM components must implement. What does it do? How is it defined?
=== nsISupports (defined [http://developer.mozilla.org/en/docs/nsISupports here]) is the base interface for all XPCOM components (i.e., it is possible to pass any XPCOM component around as an nsISupports object). The methods in nsISupports define basic bookkeeping for an interface's lifetime (it also defines a way to check at runtime if a component implements a given interface, but more on that later).===
[http://developer.mozilla.org/en/docs/nsISupports nsISupports] is the base interface for all XPCOM components (i.e., it is possible to pass any XPCOM component around as an nsISupports object). The [http://developer.mozilla.org/en/docs/nsISupports methods] in nsISupports define basic bookkeeping for an interface's lifetime (they also defines a way to check at runtime if a component implements a given interface, but more on that later). Components need to keep track of how many clients hold references to them via an interface. We call this This is known as reference counting on an interface, and it is used to determine when a component can be safely unloaded so that it doesn't leak (i.e., no one holds a reference any more, but the interface is still in memory).
The members of nsISupports (i.e., QueryInterface, AddRef, and Release) provide the basic means for getting the right interface from an object, incrementing the reference count, and releasing objects once they are not being used.
So the first line above says, "include the interface for nsISupports (defined in nsISupports.idl) because I'll need it", and the fourth line says, "I'm a new interface called IFirstXpcom, but I'm also nsISupports because I inherit from it."
 
=== UUID ===
What does line three mean? This says that our component is '''scriptable''', and can be used or implemented in scripting languages, JavaScript for example (see http://developer.mozilla.org/en/docs/Interfaces:About_Scriptable_Interfaces).
[scriptable, uuid(78af1749-014a-47aa-baec-2669670b7601)]
 
=== Attributes and Methods ===
Next comes the body of your interface. Our interface defines one attribute and one method. An attribute is a value you can '''Get''' and '''Set''' (NOTE: you can specify attributes that are Get only, that is read-only). We have an attribute called '''name''' of type '''AString''' (a unicode, or two-byte string class. For more details about strings in Mozilla, see http://developer.mozilla.org/en/docs/XPCOM_string_guide).

Navigation menu