Bug 664116 - AllTabs.jsm needs to remove event listeners from browser windows. r=dietrich

--HG--
extra : rebase_source : 0bfd0ecd99512c058611d3118bc7ae482918f005
This commit is contained in:
Dão Gottwald 2011-06-16 21:18:39 +02:00
parent b4c5b0ece8
commit d9aef92f21

View File

@ -74,11 +74,11 @@ let AllTabs = {
*/ */
register: function register(eventName, callback) { register: function register(eventName, callback) {
// Either add additional callbacks or create the first entry // Either add additional callbacks or create the first entry
let listeners = eventListeners[eventName]; let listeners = eventListeners[events[eventName]];
if (listeners) if (listeners)
listeners.push(callback); listeners.push(callback);
else else
eventListeners[eventName] = [callback]; eventListeners[events[eventName]] = [callback];
}, },
/** /**
@ -93,7 +93,7 @@ let AllTabs = {
*/ */
unregister: function unregister(eventName, callback) { unregister: function unregister(eventName, callback) {
// Nothing to remove for this event // Nothing to remove for this event
let listeners = eventListeners[eventName]; let listeners = eventListeners[events[eventName]];
if (!listeners) if (!listeners)
return; return;
@ -114,31 +114,50 @@ __defineGetter__("browserWindows", function browserWindows() {
return browserWindows; return browserWindows;
}); });
let events = ["attrModified", "close", "move", "open", "select", "pinned", "unpinned"]; let events = {
attrModified: "TabAttrModified",
close: "TabClose",
move: "TabMove",
open: "TabOpen",
select: "TabSelect",
pinned: "TabPinned",
unpinned: "TabUnpinned"
};
let eventListeners = {}; let eventListeners = {};
function registerBrowserWindow(browserWindow) { function registerBrowserWindow(browserWindow) {
events.forEach(function(eventName) { for each (let event in events)
let tabEvent = "Tab" + eventName[0].toUpperCase() + eventName.slice(1); browserWindow.addEventListener(event, tabEventListener, true);
browserWindow.addEventListener(tabEvent, function(event) {
// Make sure we've gotten listeners before trying to call
let listeners = eventListeners[eventName];
if (!listeners)
return;
let tab = event.target; browserWindow.addEventListener("unload", unregisterBrowserWindow, false);
}
// Make a copy of the listeners, so it can't change as we call back function unregisterBrowserWindow(unloadEvent) {
listeners.slice().forEach(function(callback) { let browserWindow = unloadEvent.currentTarget;
try {
callback(tab, event); for each (let event in events)
} browserWindow.removeEventListener(event, tabEventListener, true);
// Don't let failing callbacks stop us but report the failure
catch(ex) { browserWindow.removeEventListener("unload", unregisterBrowserWindow, false);
Cu.reportError(ex); }
}
}); function tabEventListener(event) {
}, true); // Make sure we've gotten listeners before trying to call
let listeners = eventListeners[event.type];
if (!listeners)
return;
let tab = event.target;
// Make a copy of the listeners, so it can't change as we call back
listeners.slice().forEach(function (callback) {
try {
callback(tab, event);
}
// Don't let failing callbacks stop us but report the failure
catch (ex) {
Cu.reportError(ex);
}
}); });
} }