Introduction

Previously we created a C++ binary XPCOM component called FirstXpcom and tested it in the JavaScript Shell. However, we haven't really done anything with it yet. Ideally we'd like to add our component to the browser and create some UI in order to allow the user to access its functionality.

We'll work in stages to create a simple UI for accessing FirstXpcom's functionality. Our goal will be to add a custom dialog box to the browser, accessible via the menu bar. This dialog box will allow the user to access the functionality in our XPCOM component via JavaScript that we'll write.

Creating the FirstXPCOM Chrome Extension

Generating the extension automatically

If you've gone through the process of creating an extension by hand already (e.g., writing install.rdf, creating the proper directory structure, etc.), you can choose to use a handy on-line wizard this time. Use the following values/options in the wizard:

Now create a file in your profile's extension folder (%Application Data%\Mozilla\Firefox\Profile\<development_profile>\extensions) named firstxpcomchrome@mozilla.org. This file should contain the full path to your unzipped extension (perhaps C:\temp\firstxpcomchrome), and must include a trailing slash. Here is one way to do it:

Restart Firefox and try out your new extension. By default you should have a new red menu item, Tools > Your localized menuitem (you should also see firstxpcomchrome listed in the Add-on manager, along with firstxpcom).

Review of XUL Overlays

The extension wizard generated our extension's generic structure and files, but also created a browser overlay and associated JavaScript file--these are what allow the custom menu item to be added. The files that interest us most are:

firstxpcomchrome/content/firefoxOverlay.xul

firstxpcomchrome/content/overlay.js

firstxpcomchrome/chrome.manifest

Together the firefoxOverlay.xul and chrome.manifest files provide a way to add the custom menu item to the browser's Tools menu. In chrome.manifest we see:

Here an ENTITY from the referenced DTD is used instead of a hard-coded string. Looking at the file firstxpcomchrome/locale/en-US/firstxpcomchrome.dtd we can see the mapping to English, which is what gets displayed at runtime:

<!ENTITY firstxpcomchrome.label "Your localized menuitem">

You'll notice too that the menu item appears red. This is caused by a custom CSS property defined in firstxpcomchrome/skin/overlay.css. You can see it being referenced in firefoxOverlay.xul

Let's begin with the final line, a load listener which insures that our code is run when the browser starts up. Our object's onLoad function takes care of general initialization tasks, including creating an instance of FirstXpcom that we'll use throughout the life of our extension:

We create an instance of firstxpcom and then QI (i.e., "query interface") it to IFirstXpcom. Now we can call its methods, for example, setting the name attribute. We do the same later in onMenuCommand:

Accessing our C++ XPCOM methods is as easy as calling any other JavaScript function.

The first part of onMenuCommand deals with displaying and using the dialog box we'll write, which we'll return to below. However, let's skip to the end of this function and discuss the following code:

// Use the Alerts Service to display the results to the user.
var alertsService = Components.classes["@mozilla.org/alerts-service;1"]
.getService(Components.interfaces.nsIAlertsService);
alertsService.showAlertNotification(null, this.firstxpcom.name, this.total,
false, "", null);

I chose to use the nsIAlertsService , which creates an animated pop-up over the task list, rather than displaying the info to the user with an alert(), for a number of reasons. First, I want to show that now that you know how to create an XPCOM component in C++, and also how to use it in JavaScript, you can use any of the hundreds of interfaces available in the Mozilla platform--the nsIAlertsService is no different from IFirstXpcom. I also wanted to draw your attention to another method of instantiating a component in JavaScript. Compare the following two code snippets:

In the former case we use createInstance, which gives us a new unique instance. In the latter, we use getService, which returns a shared instance of an existing component (i.e., a Singleton). Unlike IFirstXpcom, which can be created many times by different callers, the nsIAlertsService is a shared component, because only one pop-up message at a time can be shown to the user.

NOTE: Because this is JavaScript and not C++, there is no need to recompile or start/stop the browser when you make a change to your files. Using the Extension Developer's extension, you can simply reload all chrome: Tools > Extension Developer > Reload All Chrome

Creating the dialog

Now let's focus on the code to create and use the dialog box. We've seen XUL files a number of times used to define overlays. However, we haven't done any UI work with them yet. XUL does for chrome what HTML does for content, namely, it allows the developer to use a declarative XML syntax in order to define a UI, and then add functionality with JavaScript.

XUL is well documented elsewhere, so we won't attempt to cover it here. Instead, we'll jump into creating a simple dialog using a handful of XUL widgets. Experimenting with XUL is greatly simplified by using Mark Finkle's XUL Explorer application (itself written in XUL and running on XULRunner). In all, we'll use the following XUL:

This is a simple dialog, and creating a more elaborate one is left to the reader as an exercise. Now that the dialog's structure is complete, we need to add JavaScript to make it work with the rest of our extension.

Coding the dialog

Let's return to the code we skipped in onMenuCommand related to the dialog:

When the menu item is clicked, the dialog will be shown and the values the user enters returned. The showDialog function begins by packaging up in (we use inn because in is a keyword in JavaScript) and out variables. This allows us to pass multiple variables from/to the dialog. In this case, we pass the value of our component's name attribute to the dialog, so it can be displayed in a textbox for editing. The out variable will contain the updated values as entered in the dialog's textboxes (the code to do this will be discussed shortly).

We actually display the dialog using window.openDialog, which takes a URI to our dialog's XUL file, as well as a list of options (e.g., dialog is modal, resizable, etc.) and our params object, containing the inn and out variables. Execution will block on focus() until the user clicks OK or Cancel, or closes the window.

Here's the complete XUL file for our dialog, this time with the JavaScript added:

In the dialog element we've wired the onLoad and onOK functions. The onLoad function is used to extract the parameters passed in with the call to window.openDialog() and then set values in the UI.

The onOK function occurs when the user clicks the accept button (i.e., OK). When this happens the values from the textboxes are obtained and packaged up in the out parameter we sent in earlier. This is how we pass values back to the main window, and our extension code in firstxpcomchrome.