Skip to content
codebutler edited this page Sep 13, 2010 · 10 revisions

MozJS D-Bus

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 API.

Downloading & Building

Source code is available from the git repository. Here’s the basic idea of how to build the code:

(First Download the mozilla source code)
$ cd mozilla/extensions/
$ git clone git://github.com/codebutler/mozjs-dbus.git mozjs_dbus
$ cd ..
$ ./configure --enable-dbus --enable-extensions=mozjs_dbus --enable-application=xulrunner
$ make

This will build a .xpi in dist/xpi-stage, which can be loaded
as a Firefox extension. There’s will also be an application.ini file in
dist/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