MozJS D-Bus
From Extremeboredom wiki
Contents |
About
MozJS D-Bus is a poorly named library that makes it possible for Mozilla/Javascript-based applications and extensions to interoperable with D-Bus, which allows for deep integration with the Linux desktop.
This project is written by Eric Butler. Please contact me (email/xmpp) at eric@extremeboredom.net if you have any questions, criticisms, praises or would like to help!
API
See MozJS_D-Bus_API.
Downloading & Building
Source code is available from the mercurial repository.
$ hg clone http://eric.extremeboredom.net/hg/mozjs_dbus
Here's the basic idea of how to build the code:
$ cd recent-mozilla-trunk/extensions/ $ hg clone http://eric.extremeboredom.net/hg/mozjs_dbus $ cd .. $ ./configure --enable-dbus --enable-extensions=mozjs_dbus --enable-applications=firefox $ make
This will build a .xpi in dist/bin/xpi-stage, which can be loaded as a Firefox extension. There's will also be an application.ini file in dist/bin/xpi-stage/mozjs_dbus, which should work with xulrunner.
Usage & Examples
Once the extension has been loaded into your application, you can import the types into your current javascript scope by using:
Components.utils.import("resource://mozjs_dbus/DBUS.jsm");
Basic remote method calls
var bus = DBUS.getSessionBus();
var nf = bus.getObject("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications");
alert(nf.GetCapabilities());
Dealing with Signals
var bus = DBUS.getSessionBus();
var nf = bus.getObject("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications");
nf.connectToSignal('NotificationClosed', function (id, reason) {
alert('Notification Closed! ' + id + ' .. ' + reason);
});
nf.connectToSignal('ActionInvoked', function (id, action_key) {
alert('Action Invoked! ' + id + ' .. ' + action_key);
});
Custom Services
MozJS D-Bus supports creating new services using Javascript.
var testObj = new DBusObject();
var iface = testObj.defineInterface("net.extremeboredom.TestInterface");
iface.defineMethod('sum', 'ii', 'i', function(first, second) {
return first+second;
});
iface.defineMethod('hello', , 's', function() {
return "Hello World!";
});
iface.defineSignal('somethingHappen', 'ssi');
var bus = DBUS.getSessionBus();
var service = bus.requestService('net.extremeboredom.TestService');
service.exportObject('/Test', testObj);
Status & TODO
The following features are implemented:
- Basic remote method calls
- Signal handling
- XML Introspection
- Creating new services
- DICT types
The following features have not yet been implemented (please help!):
- Support for methods that return more than one value
- Automatically create a D-Bus service from an XPCOM service

