Chapter 5. Scripting Mozilla- P4
Figure 5-4. How XPConnect fits into the application model
In Mozilla, XPConnect is the bridge between JavaScript and XPCOM
components. The XPConnect technology wraps natively compiled
components with JavaScript objects. XPCOM, Mozilla's own cross-platform
component technology, is the framework on top of which these scriptable
components are built. Using JavaScript and XPConnect, you can create
instances of these components and use their methods and properties as you
do any regular JavaScript object, as described here. You can access any or
all of the functionality in Mozilla in this way.
Chapter 8 describes more about the XPConnect technology and how it
connects components to the interface. It also describes the components
themselves and their interfaces, the XPCOM technology, and how you can
create your own XPCOM components.
5.4.1.1. Creating XPCOM objects in script
Example 5-10
demonstrates the creation and use of an XPCOM component
in JavaScript. In this example, the script instantiates the filepicker
object and then uses it to display a file picker dialog with all of the file filters
selected. To run this example, add the function to your xfly.js file and call it
from an event handler on the "New" menu item you added in Example 3-5
.
Example 5-10. Scriptable component example
// chooseApp: Open file picker and prompt user for
application.
chooseApp: function( ) {
var nsIFilePicker =
Components.interfaces.nsIFilePicker;
var fp =
<title>Sound Service Play Example</title>
<script>
function play() {
netscape.security.PrivilegeManager.enablePrivilege(
"UniversalXPConnect");
var sample =
Components.classes["@mozilla.org/sound;1"].createIn
stance();
sample =
sample.QueryInterface(Components.interfaces.nsISoun
d);
const SND_NETWORK_STD_CID =
"@mozilla.org/network/standard-url;1";
const SND_I_URL = "nsIURL";
const SND_URL = new
C.Constructor(SND_NETWORK_STD_CID, SND_I_URL);
var url = new SND_URL();
url.spec =
'http://jslib.mozdev.org/test.wav';
sample.play(url);
}
</script>
</head>
<form name="form">
<input type="button" value="Play Sound"
onclick="play();">
</form>
As in Example 5-10
the contract ID ldap-connection;1, instantiates an object from the
nsILDAPConnection interface, and then calls a method on that object:
var connection = Components.classes
["@mozilla.org/network/ldap-
connection;1"].
createInstance(Components.interfaces.nsILDAPConnect
ion);
connection.init(queryURL.host, queryURL.port,
null,
generateGetTargetsBoundCallback( ));
These two common processes -- getting a component and selecting one of its
interfaces to assign to an object -- can also be separated into two different
statements:
// get the ldap connection component
var connection = Components.classes
["@mozilla.org/network/ldap-
connection;1"];
// create an object from the nsILDAPConnection
interface;
connection.createInstance(Components.interfaces.nsI
LDAPConnection);
// call the init( ) method on that object
connection.init(queryURL.host, queryURL.port, null,
generateGetTargetsBoundCallback(
));
Mozilla constantly uses these processes. Wherever functionality is organized
into XPCOM objects (and most of it is), these two statements bring that
functionality into JavaScript as high-level and user-friendly JavaScript
these interfaces are intended to provide a general-purpose library for Mozilla
application developers. To understand what JSLib does, consider the
following short snippet from the JSLib source file jslib/io/file.js,
which implements a close( ) function for open file objects and provides
a handy way to clean up things when you finish editing a file in the
filesystem.