2008-07-09 00:47:32 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Observers.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Daniel Aquino.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2008
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Daniel Aquino <mr.danielaquino@gmail.com>
|
|
|
|
* Myk Melez <myk@mozilla.org>
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
let EXPORTED_SYMBOLS = ["Observers"];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
|
2008-12-27 03:49:31 +03:00
|
|
|
/**
|
|
|
|
* A service for adding, removing and notifying observers of notifications.
|
|
|
|
* Wraps the nsIObserverService interface.
|
|
|
|
*
|
|
|
|
* @version 0.2
|
|
|
|
*/
|
2008-07-09 00:47:32 +04:00
|
|
|
let Observers = {
|
2009-01-14 04:58:01 +03:00
|
|
|
/**
|
|
|
|
* Register the given callback as an observer of the given topic.
|
|
|
|
*
|
|
|
|
* @param topic {String}
|
|
|
|
* the topic to observe
|
|
|
|
*
|
|
|
|
* @param callback {Object}
|
|
|
|
* the callback; an Object that implements nsIObserver or a Function
|
|
|
|
* that gets called when the notification occurs
|
|
|
|
*
|
|
|
|
* @param thisObject {Object} [optional]
|
|
|
|
* the object to use as |this| when calling a Function callback
|
|
|
|
*
|
|
|
|
* @returns the observer
|
|
|
|
*/
|
2008-12-27 06:31:37 +03:00
|
|
|
add: function(topic, callback, thisObject) {
|
2008-12-29 16:42:11 +03:00
|
|
|
let observer = new Observer(topic, callback, thisObject);
|
|
|
|
this._cache.push(observer);
|
|
|
|
this._service.addObserver(observer, topic, true);
|
2008-12-27 06:31:37 +03:00
|
|
|
|
2008-07-09 00:47:32 +04:00
|
|
|
return observer;
|
|
|
|
},
|
|
|
|
|
2009-01-14 04:58:01 +03:00
|
|
|
/**
|
|
|
|
* Unregister the given callback as an observer of the given topic.
|
|
|
|
*
|
|
|
|
* @param topic {String}
|
|
|
|
* the topic being observed
|
|
|
|
*
|
|
|
|
* @param callback {Object}
|
|
|
|
* the callback doing the observing
|
|
|
|
*
|
|
|
|
* @param thisObject {Object} [optional]
|
|
|
|
* the object being used as |this| when calling a Function callback
|
|
|
|
*/
|
2008-12-27 06:31:37 +03:00
|
|
|
remove: function(topic, callback, thisObject) {
|
2008-12-29 16:42:11 +03:00
|
|
|
// This seems fairly inefficient, but I'm not sure how much better
|
|
|
|
// we can make it. We could index by topic, but we can't index by callback
|
|
|
|
// or thisObject, as far as I know, since the keys to JavaScript hashes
|
|
|
|
// (a.k.a. objects) can apparently only be primitive values.
|
|
|
|
let [observer] = this._cache.filter(function(v) v.topic == topic &&
|
|
|
|
v.callback == callback &&
|
|
|
|
v.thisObject == thisObject);
|
2008-09-02 05:58:02 +04:00
|
|
|
if (observer) {
|
2008-12-29 16:42:11 +03:00
|
|
|
this._service.removeObserver(observer, topic);
|
|
|
|
this._cache.splice(this._cache.indexOf(observer), 1);
|
2008-09-02 05:58:02 +04:00
|
|
|
}
|
2008-07-09 00:47:32 +04:00
|
|
|
},
|
|
|
|
|
2008-12-29 17:15:33 +03:00
|
|
|
/**
|
|
|
|
* Notify observers about something.
|
|
|
|
*
|
|
|
|
* @param topic {String}
|
|
|
|
* the topic to notify observers about
|
|
|
|
*
|
2009-01-14 04:58:01 +03:00
|
|
|
* @param subject {Object} [optional]
|
2008-12-29 17:15:33 +03:00
|
|
|
* some information about the topic; can be any JS object or primitive
|
|
|
|
*
|
2009-01-14 04:58:01 +03:00
|
|
|
* @param data {String} [optional] [deprecated]
|
|
|
|
* some more information about the topic; deprecated as the subject
|
|
|
|
* is sufficient to pass all needed information to the JS observers
|
|
|
|
* that this module targets; if you have multiple values to pass to
|
|
|
|
* the observer, wrap them in an object and pass them via the subject
|
|
|
|
* parameter (i.e.: { foo: 1, bar: "some string", baz: myObject })
|
2008-12-29 17:15:33 +03:00
|
|
|
*/
|
2008-12-27 03:49:31 +03:00
|
|
|
notify: function(topic, subject, data) {
|
|
|
|
subject = (typeof subject == "undefined") ? null : new Subject(subject);
|
|
|
|
data = (typeof data == "undefined") ? null : data;
|
2008-12-29 16:42:11 +03:00
|
|
|
this._service.notifyObservers(subject, topic, data);
|
2008-07-09 00:47:32 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_service: Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService),
|
|
|
|
|
2008-12-29 16:42:11 +03:00
|
|
|
/**
|
|
|
|
* A cache of observers that have been added.
|
|
|
|
*
|
|
|
|
* We use this to remove observers when a caller calls |remove|.
|
|
|
|
*
|
|
|
|
* XXX This might result in reference cycles, causing memory leaks,
|
|
|
|
* if we hold a reference to an observer that holds a reference to us.
|
|
|
|
* Could we fix that by making this an independent top-level object
|
|
|
|
* rather than a property of this object?
|
|
|
|
*/
|
|
|
|
_cache: []
|
2008-07-09 00:47:32 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-12-29 16:42:11 +03:00
|
|
|
function Observer(topic, callback, thisObject) {
|
|
|
|
this.topic = topic;
|
|
|
|
this.callback = callback;
|
|
|
|
this.thisObject = thisObject;
|
2008-07-09 00:47:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Observer.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
|
|
|
|
observe: function(subject, topic, data) {
|
2008-12-29 16:42:11 +03:00
|
|
|
// Extract the wrapped object for subjects that are one of our wrappers
|
|
|
|
// around a JS object. This way we support both wrapped subjects created
|
2008-07-09 00:47:32 +04:00
|
|
|
// using this module and those that are real XPCOM components.
|
2008-12-29 16:42:11 +03:00
|
|
|
if (subject && typeof subject == "object" &&
|
|
|
|
("wrappedJSObject" in subject) &&
|
|
|
|
("observersModuleSubjectWrapper" in subject.wrappedJSObject))
|
|
|
|
subject = subject.wrappedJSObject.object;
|
|
|
|
|
|
|
|
if (typeof this.callback == "function") {
|
|
|
|
if (this.thisObject)
|
|
|
|
this.callback.call(this.thisObject, subject, data);
|
2008-12-27 06:31:37 +03:00
|
|
|
else
|
2008-12-29 16:42:11 +03:00
|
|
|
this.callback(subject, data);
|
2008-12-28 03:52:56 +03:00
|
|
|
}
|
2008-12-29 16:42:11 +03:00
|
|
|
else // typeof this.callback == "object" (nsIObserver)
|
|
|
|
this.callback.observe(topic, subject, data);
|
2008-07-09 00:47:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function Subject(object) {
|
2008-12-29 16:42:11 +03:00
|
|
|
// Double-wrap the object and set a property identifying the wrappedJSObject
|
|
|
|
// as one of our wrappers to distinguish between subjects that are one of our
|
|
|
|
// wrappers (which we should unwrap when notifying our observers) and those
|
|
|
|
// that are real JS XPCOM components (which we should pass through unaltered).
|
|
|
|
this.wrappedJSObject = { observersModuleSubjectWrapper: true, object: object };
|
2008-07-09 00:47:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
Subject.prototype = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([]),
|
|
|
|
getHelperForLanguage: function() {},
|
|
|
|
getInterfaces: function() {}
|
|
|
|
};
|