diff --git a/browser/components/safebrowsing/content/application.js b/browser/components/safebrowsing/content/application.js index c2c15afb7f3d..cf97cb3571d0 100644 --- a/browser/components/safebrowsing/content/application.js +++ b/browser/components/safebrowsing/content/application.js @@ -67,7 +67,6 @@ function PROT_Application() { TEST_PROT_ListManager(); TEST_PROT_PhishingWarden(); TEST_PROT_TRFetcher(); - TEST_G_ObjectSafeMap(); TEST_PROT_URLCanonicalizer(); TEST_G_Preferences(); TEST_G_Observer(); diff --git a/browser/components/safebrowsing/src/nsSafebrowsingApplication.js b/browser/components/safebrowsing/src/nsSafebrowsingApplication.js index 400fb9fda16a..207144f3f3fc 100644 --- a/browser/components/safebrowsing/src/nsSafebrowsingApplication.js +++ b/browser/components/safebrowsing/src/nsSafebrowsingApplication.js @@ -34,7 +34,6 @@ function Init() { modScope.PROT_XMLFetcher = jslib.PROT_XMLFetcher; modScope.BindToObject = jslib.BindToObject; modScope.G_Protocol4Parser = jslib.G_Protocol4Parser; - modScope.G_ObjectSafeMap = jslib.G_ObjectSafeMap; modScope.PROT_UrlCrypto = jslib.PROT_UrlCrypto; modScope.RequestBackoff = jslib.RequestBackoff; diff --git a/toolkit/components/url-classifier/content/moz/objectsafemap.js b/toolkit/components/url-classifier/content/moz/objectsafemap.js deleted file mode 100644 index 0c632e505ae1..000000000000 --- a/toolkit/components/url-classifier/content/moz/objectsafemap.js +++ /dev/null @@ -1,231 +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 ***** - - -// ObjectSafeMap is, shockingly, a Map with which it is safe to use -// objects as keys. It currently uses parallel arrays for storage, -// rendering it inefficient (linear) for large maps. We can always -// swap out the implementation if this becomes a problem. Note that -// this class uses strict equality to determine equivalent keys. -// -// Interface: -// -// insert(key, value) -// erase(key) // Returns true if key erased, false if not found -// find(key) // Returns undefined if key not found -// replace(otherMap) // Clones otherMap, replacing the current map -// forEach(func) -// size() // Returns number of items in the map -// -// TODO: should probably make it iterable by implementing getList(); - - -/** - * Create a new ObjectSafeMap. - * - * @param opt_name A string used to name the map - * - * @constructor - */ -function G_ObjectSafeMap(opt_name) { - this.debugZone = "objectsafemap"; - this.name_ = opt_name ? opt_name : "noname"; - this.keys_ = []; - this.values_ = []; -} - -/** - * Helper function to return the index of a key. - * - * @param key An key to find - * - * @returns Index in the keys array where the key is found, -1 if not - */ -G_ObjectSafeMap.prototype.indexOfKey_ = function(key) { - for (var i = 0; i < this.keys_.length; i++) - if (this.keys_[i] === key) - return i; - return -1; -} - -/** - * Add an item - * - * @param key An key to add (overwrites previous key) - * - * @param value The value to store at that key - */ -G_ObjectSafeMap.prototype.insert = function(key, value) { - if (key === null) - throw new Error("Can't use null as a key"); - if (value === undefined) - throw new Error("Can't store undefined values in this map"); - - var i = this.indexOfKey_(key); - if (i == -1) { - this.keys_.push(key); - this.values_.push(value); - } else { - this.keys_[i] = key; - this.values_[i] = value; - } - - G_Assert(this, this.keys_.length == this.values_.length, - "Different number of keys than values!"); -} - -/** - * Remove a key from the map - * - * @param key The key to remove - * - * @returns Boolean indicating if the key was removed - */ -G_ObjectSafeMap.prototype.erase = function(key) { - var keyLocation = this.indexOfKey_(key); - var keyFound = keyLocation != -1; - if (keyFound) { - this.keys_.splice(keyLocation, 1); - this.values_.splice(keyLocation, 1); - } - G_Assert(this, this.keys_.length == this.values_.length, - "Different number of keys than values!"); - - return keyFound; -} - -/** - * Look up a key in the map - * - * @param key The key to look up - * - * @returns The value at that key or undefined if it doesn't exist - */ -G_ObjectSafeMap.prototype.find = function(key) { - var keyLocation = this.indexOfKey_(key); - return keyLocation == -1 ? undefined : this.values_[keyLocation]; -} - -/** - * Replace one map with the content of another - * - * @param map input map that needs to be merged into our map - */ -G_ObjectSafeMap.prototype.replace = function(other) { - this.keys_ = []; - this.values_ = []; - for (var i = 0; i < other.keys_.length; i++) { - this.keys_.push(other.keys_[i]); - this.values_.push(other.values_[i]); - } - - G_Assert(this, this.keys_.length == this.values_.length, - "Different number of keys than values!"); -} - -/** - * Apply a function to each of the key value pairs. - * - * @param func Function to apply to the map's key value pairs - */ -G_ObjectSafeMap.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.keys_.length; i++) - func(this.keys_[i], this.values_[i]); -} - -/** - * @returns The number of keys in the map - */ -G_ObjectSafeMap.prototype.size = function() { - return this.keys_.length; -} - -#ifdef DEBUG -// Cheesey, yes, but it's what we've got -function TEST_G_ObjectSafeMap() { - if (G_GDEBUG) { - var z = "map UNITTEST"; - G_debugService.enableZone(z); - G_Debug(z, "Starting"); - - var m = new G_ObjectSafeMap(); - G_Assert(z, m.size() == 0, "Initial size not zero"); - - var o1 = new Object; - var v1 = "1"; - var o2 = new Object; - var v2 = "1"; - - G_Assert(z, m.find(o1) == undefined, "Found non-existent item"); - - m.insert(o1, v1); - m.insert(o2, v2); - - G_Assert(z, m.size() == 2, "Size not 2"); - G_Assert(z, m.find(o1) == "1", "Didn't find item 1"); - G_Assert(z, m.find(o2) == "1", "Didn't find item 1"); - - m.insert(o1, "2"); - - G_Assert(z, m.size() == 2, "Size not 2"); - G_Assert(z, m.find(o1) == "2", "Didn't find item 1"); - G_Assert(z, m.find(o2) == "1", "Didn't find item 1"); - - m.erase(o1); - - G_Assert(z, m.size() == 1, "Size not 1"); - G_Assert(z, m.find(o1) == undefined, "Found item1"); - G_Assert(z, m.find(o2) == "1", "Didn't find item 2"); - - m.erase(o1); - - G_Assert(z, m.size() == 1, "Size not 1"); - G_Assert(z, m.find(o1) == undefined, "Found item1"); - G_Assert(z, m.find(o2) == "1", "Didn't find item 2"); - - m.erase(o2); - - G_Assert(z, m.size() == 0, "Size not 0"); - G_Assert(z, m.find(o2) == undefined, "Found item2"); - - G_Debug(z, "PASSED"); - } -} -#endif diff --git a/toolkit/components/url-classifier/content/moz/preferences.js b/toolkit/components/url-classifier/content/moz/preferences.js index 831d08b69d46..637aa9f626a0 100644 --- a/toolkit/components/url-classifier/content/moz/preferences.js +++ b/toolkit/components/url-classifier/content/moz/preferences.js @@ -182,12 +182,17 @@ G_Preferences.prototype.clearPref = function(which) { * holding the preference name that changed */ G_Preferences.prototype.addObserver = function(which, callback) { - var observer = new G_PreferenceObserver(callback); // Need to store the observer we create so we can eventually unregister it if (!this.observers_[which]) - this.observers_[which] = new G_ObjectSafeMap(); - this.observers_[which].insert(callback, observer); - this.prefs_.addObserver(which, observer, false /* strong reference */); + this.observers_[which] = { callbacks: [], observers: [] }; + + /* only add an observer if the callback hasn't been registered yet */ + if (this.observers_[which].callbacks.indexOf(callback) == -1) { + var observer = new G_PreferenceObserver(callback); + this.observers_[which].callbacks.push(callback); + this.observers_[which].observers.push(observer); + this.prefs_.addObserver(which, observer, false /* strong reference */); + } } /** @@ -197,10 +202,11 @@ G_Preferences.prototype.addObserver = function(which, callback) { * @param callback Function to remove as an observer */ G_Preferences.prototype.removeObserver = function(which, callback) { - var observer = this.observers_[which].find(callback); - G_Assert(this, !!observer, "Tried to unregister a nonexistant observer"); + var ix = this.observers_[which].callbacks.indexOf(callback); + G_Assert(this, ix != -1, "Tried to unregister a nonexistant observer"); + this.observers_[which].callbacks.splice(ix, 1); + var observer = this.observers_[which].observers.splice(ix, 1)[0]; this.prefs_.removeObserver(which, observer); - this.observers_[which].erase(callback); } diff --git a/toolkit/components/url-classifier/src/nsUrlClassifierLib.js b/toolkit/components/url-classifier/src/nsUrlClassifierLib.js index 5312855d206d..104d0aa8bba8 100644 --- a/toolkit/components/url-classifier/src/nsUrlClassifierLib.js +++ b/toolkit/components/url-classifier/src/nsUrlClassifierLib.js @@ -48,7 +48,6 @@ const G_GDEBUG = false; #include ../content/moz/debug.js #include ../content/moz/alarm.js #include ../content/moz/cryptohasher.js -#include ../content/moz/objectsafemap.js #include ../content/moz/observer.js #include ../content/moz/protocol4.js