Changes

Jump to: navigation, search

Real World Mozilla First XPCOM Component

390 bytes added, 13:25, 18 November 2008
nsISupports
= Writing FirstXpcom =
For this walkthrough we will use the Mozilla build system to create our component. It is also possible to us use the '''Gecko SDK''' ([http://developer.mozilla.org/en/docs/How_to_build_a_binary_XPCOM_component_using_Visual_Studio instructions are here]). NOTE: this assumes that you have already done a successful objdir-Firefox build.
== Creating Directories ==
=== nsISupports ===
[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 define 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. 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).
[scriptable, uuid(78af1749-014a-47aa-baec-2669670b7601)]
 
More information about generating UUIDs in different forms is available [http://developer.mozilla.org/En/Generating_GUIDs here].
=== Attributes and Methods ===
ac_add_options --enable-extensions=default,'''firstxpcom'''
Now you can (BUT DON'T, see below for quick way!!)re-build your tree (also known as "rebuilding world") by doing:
$ cd mozilla
Re-run make.
''As an aside, and while you are thinking about Makefiles, you might take a moment to read [https://bugzilla.mozilla.org/show_bug.cgi?id=371201 bug 371201]. This is a bug that was identified while the author was trying to debug his Makefile.in files. Ted Mielczarek (luser ted on IRC) was finally able to spot the problem--a trailing space on XPI_NAME. I share this anecdote as a way to introduce https://bugzilla.mozilla.org, to emphasize the necessity of the community, and to show the kind of problems that one can have writing files for the build system.--UPDATE: this bug has now been fixed by Ted (March 26, 2007).''
== Examining IFirstXpcom.h ==
19f3ef5e-759f-49a4-88e3-ed27f9c83011
Now [http://developer.mozilla.org/En/Generating_GUIDs write a define ] to make it easier to pass this Class ID aroundin C++:
#define FIRSTXPCOM_CID \
{
/* member initializers and constructor code */
'''mName.Assign(LNS_LITERAL_STRING("FirstXpcom Component"));'''
}
$ cd $(objdir)/dist/bin
$ export MOZ_DEBUG_BREAK=warn
$ firefox.exe -Profilemanager --no-remote
== Accessing FirstXpcom from the JavaScript Shell ==
var obj = Components.classes[cid].createInstance()
* Next, take the the nsISupports object returned above and query it (i.e., see if it supports your interface type and if so, change it to that interface) to IFirstXpcom, often referred to as QI (e.g., ''"...you need to QI it to IFirstXpcom..."'').
obj = obj.QueryInterface(Components.interfaces.IFirstXpcom)
When you run this code, also notice how your C++ '''printf''' statements are causing messages to stdout in your console window (you may need to scrollback through all the other messages to find them).
 
= Resources =
 
* [[Makefile.in Template for In-Tree Extensions]]
1
edit

Navigation menu