diff --git a/browser/components/safebrowsing/content/js/eventregistrar.js b/browser/components/safebrowsing/content/js/eventregistrar.js deleted file mode 100644 index 2c3ca9344171..000000000000 --- a/browser/components/safebrowsing/content/js/eventregistrar.js +++ /dev/null @@ -1,172 +0,0 @@ -/* ***** 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 Google Safe Browsing. - * - * The Initial Developer of the Original Code is Google Inc. - * Portions created by the Initial Developer are Copyright (C) 2006 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Fritz Schneider (original author) - * - * 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 ***** */ - - -// This file implements an event registrar, an object with which you -// can register handlers for arbitrary programmer-defined -// events. Events are arbitrary strings and listeners are functions -// taking an object (stuffed with arguments) as a parameter. When you -// fire an event through the registrar, all listeners are invoked in -// an unspecified order. The firing function takes an object to be -// passed into each handler (it is _not_ copied, so be careful). We -// chose this calling convention so we don't have to change handler -// signatures when adding new information. -// -// Why not just use notifier/observers? Because passing data around -// with them requires either serialization or a new xpcom interface, -// both of which are a pain in the ass. -// -// Example: -// -// // Set up a listener -// this.handleTabload = function(e) { -// foo(e.url); -// bar(e.browser); -// }; -// -// // Set up the registrar -// var eventTypes = ["tabload", "tabunload", "tabswitch"]; -// var registrar = new EventRegistrar(eventTypes); -// var handler = BindToObject(this.handleTabload, this); -// -// // Register a listener -// registrar.registerListener("tabload", handler); -// -// // Fire an event and remove the listener -// var event = { "url": "http://www", "browser": browser }; -// registrar.fire("tabload", event); -// registrar.removeListener("tabload", handler); -// -// TODO: could add ability to cancel further handlers by having listeners -// return a boolean - -/** - * EventRegistrars are used to manage user-defined events. - * - * @constructor - * @param eventTypes {Array or Object} Array holding names of events or - * Object holding properties the values of which are - * names (strings) for which listeners can register - */ -function EventRegistrar(eventTypes) { - this.eventTypes = []; - this.listeners_ = {}; // Listener sets, index by event type - - if (eventTypes instanceof Array) { - var events = eventTypes; - } else if (typeof eventTypes == "object") { - var events = []; - for (var e in eventTypes) - events.push(eventTypes[e]); - } else { - throw new Error("Unrecognized init parameter to EventRegistrar"); - } - - for (var i = 0; i < events.length; i++) { - this.eventTypes.push(events[i]); // Copy in case caller mutates - this.listeners_[events[i]] = - new ListDictionary(events[i] + "Listeners"); - } -} - -/** - * Indicates whether the given event is one the registrar can handle. - * - * @param eventType {String} The name of the event to look up - * @returns {Boolean} false if the event type is not known or the - * event type string itself if it is - */ -EventRegistrar.prototype.isKnownEventType = function(eventType) { - for (var i=0; i < this.eventTypes.length; i++) - if (eventType == this.eventTypes[i]) - return eventType; - return false; -} - -/** - * Add an event type to listen for. - * @param eventType {String} The name of the event to add - */ -EventRegistrar.prototype.addEventType = function(eventType) { - if (this.isKnownEventType(eventType)) - throw new Error("Event type already known: " + eventType); - - this.eventTypes.push(eventType); - this.listeners_[eventType] = new ListDictionary(eventType + "Listeners"); -} - -/** - * Register to receive events of the type passed in. - * - * @param eventType {String} indicating the event type (one of this.eventTypes) - * @param listener {Function} to invoke when the event occurs. - */ -EventRegistrar.prototype.registerListener = function(eventType, listener) { - if (this.isKnownEventType(eventType) === false) - throw new Error("Unknown event type: " + eventType); - - this.listeners_[eventType].addMember(listener); -} - -/** - * Unregister a listener. - * - * @param eventType {String} One of EventRegistrar.eventTypes' members - * @param listener {Function} Function to remove as listener - */ -EventRegistrar.prototype.removeListener = function(eventType, listener) { - if (this.isKnownEventType(eventType) === false) - throw new Error("Unknown event type: " + eventType); - - this.listeners_[eventType].removeMember(listener); -} - -/** - * Invoke the handlers for the given eventType. - * - * @param eventType {String} The event to fire - * @param e {Object} Object containing the parameters of the event - */ -EventRegistrar.prototype.fire = function(eventType, e) { - if (this.isKnownEventType(eventType) === false) - throw new Error("Unknown event type: " + eventType); - - var invoke = function(listener) { - listener(e); - }; - - this.listeners_[eventType].forEach(invoke); -} diff --git a/browser/components/safebrowsing/content/js/listdictionary.js b/browser/components/safebrowsing/content/js/listdictionary.js deleted file mode 100644 index 8169c93722c8..000000000000 --- a/browser/components/safebrowsing/content/js/listdictionary.js +++ /dev/null @@ -1,117 +0,0 @@ -/* ***** 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 Google Safe Browsing. - * - * The Initial Developer of the Original Code is Google Inc. - * Portions created by the Initial Developer are Copyright (C) 2006 - * the Initial Developer. All Rights Reserved. - * - * Contributor(s): - * Fritz Schneider (original author) - * - * 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 ***** */ - - -// This file implements a Dictionary data structure using a list -// (array). We could instead use an object, but using a list enables -// us to have ordering guarantees for iterators. The interface it exposes -// is: -// -// addMember(item) -// removeMember(item) -// isMember(item) -// forEach(func) -// -// TODO: this class isn't really a Dictionary, it's more like a -// membership set (i.e., a set without union and whatnot). We -// should probably change the name to avoid confusion. - -/** - * Create a new Dictionary data structure. - * - * @constructor - * @param name A string used to name the dictionary - */ -function ListDictionary(name) { - this.name_ = name; - this.members_ = []; -} - -/** - * Look an item up. - * - * @param item An item to look up in the dictionary - * @returns Boolean indicating if the parameter is a member of the dictionary - */ -ListDictionary.prototype.isMember = function(item) { - for (var i=0; i < this.members_.length; i++) - if (this.members_[i] == item) - return true; - return false; -} - -/** - * Add an item - * - * @param item An item to add (does not check for dups) - */ -ListDictionary.prototype.addMember = function(item) { - this.members_.push(item); -} - -/** - * Remove an item - * - * @param item The item to remove (doesn't check for dups) - * @returns Boolean indicating if the item was removed - */ -ListDictionary.prototype.removeMember = function(item) { - for (var i=0; i < this.members_.length; i++) { - if (this.members_[i] == item) { - for (var j=i; j < this.members_.length; j++) - this.members_[j] = this.members_[j+1]; - - this.members_.length--; - return true; - } - } - return false; -} - -/** - * Apply a function to each of the members. Does NOT replace the members - * in the dictionary with results -- it just calls the function on each one. - * - * @param func Function to apply to the dictionary's members - */ -ListDictionary.prototype.forEach = function(func) { - if (typeof func != "function") - throw new Error("argument to forEach is not a function, it's a(n) " + - typeof func); - - for (var i=0; i < this.members_.length; i++) - func(this.members_[i]); -} diff --git a/browser/components/safebrowsing/content/moz/tabbedbrowserwatcher.js b/browser/components/safebrowsing/content/moz/tabbedbrowserwatcher.js index 570c438b43fc..2c83b9f4a17b 100644 --- a/browser/components/safebrowsing/content/moz/tabbedbrowserwatcher.js +++ b/browser/components/safebrowsing/content/moz/tabbedbrowserwatcher.js @@ -199,10 +199,12 @@ function G_TabbedBrowserWatcher(tabBrowser, name, opt_filterAboutBlank) { this.debugZone = "tabbedbrowserwatcher"; - this.registrar_ = new EventRegistrar(G_TabbedBrowserWatcher.events); this.tabBrowser_ = tabBrowser; this.filterAboutBlank_ = !!opt_filterAboutBlank; this.events = G_TabbedBrowserWatcher.events; // Convenience pointer + this.eventListeners_ = {}; + for (var e in this.events) + this.eventListeners_[this.events[e]] = []; // We need some way to tell if we've seen a browser before, so we // set a property on it with a probabilistically unique string. The @@ -289,7 +291,10 @@ G_TabbedBrowserWatcher.prototype.instrumentBrowser_ = function(browser) { */ G_TabbedBrowserWatcher.prototype.registerListener = function(eventType, listener) { - this.registrar_.registerListener(eventType, listener); + if (!(eventType in this.eventListeners_)) + throw new Error("Unknown event type: " + eventType); + + this.eventListeners_[eventType].push(listener); } /** @@ -300,7 +305,12 @@ G_TabbedBrowserWatcher.prototype.registerListener = function(eventType, */ G_TabbedBrowserWatcher.prototype.removeListener = function(eventType, listener) { - this.registrar_.removeListener(eventType, listener); + if (!(eventType in this.eventListeners_)) + throw new Error("Unknown event type: " + eventType); + + var ix = this.eventListeners_[eventType].indexOf(listener); + if (ix > -1) + this.eventListeners_[eventType].splice(ix, 1); } /** @@ -310,7 +320,10 @@ G_TabbedBrowserWatcher.prototype.removeListener = function(eventType, * @param e Object to pass to each listener (NOT copied -- be careful) */ G_TabbedBrowserWatcher.prototype.fire = function(eventType, e) { - this.registrar_.fire(eventType, e); + if (!(eventType in this.eventListeners_)) + throw new Error("Unknown event type: " + eventType); + + this.eventListeners_[eventType].forEach(function(listener) { listener(e); }); } /** diff --git a/browser/components/safebrowsing/src/nsSafebrowsingApplication.js b/browser/components/safebrowsing/src/nsSafebrowsingApplication.js index f538bbcbe075..77293929dfe4 100644 --- a/browser/components/safebrowsing/src/nsSafebrowsingApplication.js +++ b/browser/components/safebrowsing/src/nsSafebrowsingApplication.js @@ -10,8 +10,6 @@ Function.prototype.inherits = function(parentCtor) { this.prototype = new tempCtor(); } -#include ../content/js/eventregistrar.js -#include ../content/js/listdictionary.js #include ../content/moz/tabbedbrowserwatcher.js #include ../content/application.js