MozillaZine

Using observers

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 16:37, 22 December 2005
Asqueella (Talk | contribs)
(References - fix xulplanet link)
<-- Previous diff
Revision as of 21:53, 29 December 2005
Asqueella (Talk | contribs)
(Instantiate and register)
Next diff -->
Line 32: Line 32:
==Instantiate and register== ==Instantiate and register==
Do the following in your init function (probably an onload handler) for each window: Do the following in your init function (probably an onload handler) for each window:
-<pre>+ var observer = new myObserver();
-var observer = new myObserver();+Note: <code>myObserver</code>'s <code>register</code> method is called once in the constructor, so no need to call it manually.
-observer.register();+
-</pre>+
-Note: myObserver's register method is called once in the constructor, this example will register your observer twice.+
-To do things cleanly, you should also unregister your observer when the window is closed (probably an onunload handler) (actually, you must!).+You '''must''' also unregister your observer when the window is closed (probably in an <code>onunload</code> handler or earlier). Failing to do so will leak the window and cause various errors.
==Notify all observers== ==Notify all observers==

Revision as of 21:53, 29 December 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

An extension overlaying the Firefox browser window will have one "instance" for each window, that is completely separate from all other windows—you cannot share variables. There are several ways to implement cross-window communication—probably the most convenient one is using observers (see also XulPlanet article).

There are two interfaces in the Mozilla platform related to observers system: nsIObserverService which is available as a service, and nsIObserver, which is what you'll need to implement.

Contents

Observer object

Each observer implements nsIObserver interface. It may register for one or more topics. For each notification, its observe function is called with the subject (can be null), the topic and additional data. All three parameters can be freely used by the code invoking the notification, see "Notify all observers" below.

In the snippet below we also include register and unregister convenience functions, which are not a part of nsIObserver interface. Including these functions in the observer object is a matter of taste.

function myObserver()
{
  this.register();
}
myObserver.prototype = {
  observe: function(subject, topic, data) {
     // Do your stuff here.
  },
  register: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                          .getService(Components.interfaces.nsIObserverService);
    observerService.addObserver(this, "myTopicID", false);
  },
  unregister: function() {
    var observerService = Components.classes["@mozilla.org/observer-service;1"]
                            .getService(Components.interfaces.nsIObserverService);
    observerService.removeObserver(this, "myTopicID");
  }
}

Instantiate and register

Do the following in your init function (probably an onload handler) for each window:

var observer = new myObserver();

Note: myObserver's register method is called once in the constructor, so no need to call it manually.

You must also unregister your observer when the window is closed (probably in an onunload handler or earlier). Failing to do so will leak the window and cause various errors.

Notify all observers

You can send a notifications to all observers using nsIObserverService.notifyObservers():

Components.classes["@mozilla.org/observer-service;1"]
         .getService(Components.interfaces.nsIObserverService)
         .notifyObservers(null, "myTopicID", "someAdditionalInformationPassedAs'Data'Parameter");

References