michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: # file, You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: michael@0: // A couple of classes to simplify creating observers. michael@0: // michael@0: // // Example1: michael@0: // michael@0: // function doSomething() { ... } michael@0: // var observer = new G_ObserverWrapper(topic, doSomething); michael@0: // someObj.addObserver(topic, observer); michael@0: // michael@0: // // Example2: michael@0: // michael@0: // function doSomething() { ... } michael@0: // new G_ObserverServiceObserver("profile-after-change", michael@0: // doSomething, michael@0: // true /* run only once */); michael@0: michael@0: michael@0: /** michael@0: * This class abstracts the admittedly simple boilerplate required of michael@0: * an nsIObserver. It saves you the trouble of implementing the michael@0: * indirection of your own observe() function. michael@0: * michael@0: * @param topic String containing the topic the observer will filter for michael@0: * michael@0: * @param observeFunction Reference to the function to call when the michael@0: * observer fires michael@0: * michael@0: * @constructor michael@0: */ michael@0: function G_ObserverWrapper(topic, observeFunction) { michael@0: this.debugZone = "observer"; michael@0: this.topic_ = topic; michael@0: this.observeFunction_ = observeFunction; michael@0: } michael@0: michael@0: /** michael@0: * XPCOM michael@0: */ michael@0: G_ObserverWrapper.prototype.QueryInterface = function(iid) { michael@0: if (iid.equals(Ci.nsISupports) || iid.equals(Ci.nsIObserver)) michael@0: return this; michael@0: throw Components.results.NS_ERROR_NO_INTERFACE; michael@0: } michael@0: michael@0: /** michael@0: * Invoked by the thingy being observed michael@0: */ michael@0: G_ObserverWrapper.prototype.observe = function(subject, topic, data) { michael@0: if (topic == this.topic_) michael@0: this.observeFunction_(subject, topic, data); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * This class abstracts the admittedly simple boilerplate required of michael@0: * observing an observerservice topic. It implements the indirection michael@0: * required, and automatically registers to hear the topic. michael@0: * michael@0: * @param topic String containing the topic the observer will filter for michael@0: * michael@0: * @param observeFunction Reference to the function to call when the michael@0: * observer fires michael@0: * michael@0: * @param opt_onlyOnce Boolean indicating if the observer should unregister michael@0: * after it has fired michael@0: * michael@0: * @constructor michael@0: */ michael@0: function G_ObserverServiceObserver(topic, observeFunction, opt_onlyOnce) { michael@0: this.debugZone = "observerserviceobserver"; michael@0: this.topic_ = topic; michael@0: this.observeFunction_ = observeFunction; michael@0: this.onlyOnce_ = !!opt_onlyOnce; michael@0: michael@0: this.observer_ = new G_ObserverWrapper(this.topic_, michael@0: BindToObject(this.observe_, this)); michael@0: this.observerService_ = Cc["@mozilla.org/observer-service;1"] michael@0: .getService(Ci.nsIObserverService); michael@0: this.observerService_.addObserver(this.observer_, this.topic_, false); michael@0: } michael@0: michael@0: /** michael@0: * Unregister the observer from the observerservice michael@0: */ michael@0: G_ObserverServiceObserver.prototype.unregister = function() { michael@0: this.observerService_.removeObserver(this.observer_, this.topic_); michael@0: this.observerService_ = null; michael@0: } michael@0: michael@0: /** michael@0: * Invoked by the observerservice michael@0: */ michael@0: G_ObserverServiceObserver.prototype.observe_ = function(subject, topic, data) { michael@0: this.observeFunction_(subject, topic, data); michael@0: if (this.onlyOnce_) michael@0: this.unregister(); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: function TEST_G_Observer() { michael@0: if (G_GDEBUG) { michael@0: michael@0: var z = "observer UNITTEST"; michael@0: G_debugService.enableZone(z); michael@0: michael@0: G_Debug(z, "Starting"); michael@0: michael@0: var regularObserverRan = 0; michael@0: var observerServiceObserverRan = 0; michael@0: michael@0: function regularObserver() { michael@0: regularObserverRan++; michael@0: }; michael@0: michael@0: function observerServiceObserver() { michael@0: observerServiceObserverRan++; michael@0: }; michael@0: michael@0: var service = Cc["@mozilla.org/observer-service;1"] michael@0: .getService(Ci.nsIObserverService); michael@0: var topic = "google-observer-test"; michael@0: michael@0: var o1 = new G_ObserverWrapper(topic, regularObserver); michael@0: service.addObserver(o1, topic, false); michael@0: michael@0: new G_ObserverServiceObserver(topic, michael@0: observerServiceObserver, true /* once */); michael@0: michael@0: // Notifications happen synchronously, so this is easy michael@0: service.notifyObservers(null, topic, null); michael@0: service.notifyObservers(null, topic, null); michael@0: michael@0: G_Assert(z, regularObserverRan == 2, "Regular observer broken"); michael@0: G_Assert(z, observerServiceObserverRan == 1, "ObsServObs broken"); michael@0: michael@0: service.removeObserver(o1, topic); michael@0: G_Debug(z, "PASSED"); michael@0: } michael@0: } michael@0: #endif