Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: lib/requestNotifier.js

Issue 29329490: Issue 3222 - Use WeakMap to store data, remove bug 982561 work-around (Closed)
Patch Set: Removed unnecessary Services.jsm import Created Oct. 29, 2015, 6:56 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/requestNotifier.js
===================================================================
--- a/lib/requestNotifier.js
+++ b/lib/requestNotifier.js
@@ -14,61 +14,24 @@
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @fileOverview Stores Adblock Plus data to be attached to a window.
*/
-Cu.import("resource://gre/modules/Services.jsm");
-
let {Utils} = require("utils");
let {BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, ElemHideException} = require("filterClasses");
let nodeData = new WeakMap();
let windowStats = new WeakMap();
let windowSelection = new WeakMap();
let requestEntryMaxId = 0;
-let setEntry, hasEntry, getEntry;
-// Last issue(Bug 982561) preventing us from using WeakMap fixed for FF version 32
-if (Services.vc.compare(Utils.platformVersion, "32.0a1") >= 0)
-{
- setEntry = (map, key, value) => map.set(key, value);
- hasEntry = (map, key) => map.has(key);
- getEntry = (map, key) => map.get(key);
-}
-else
-{
- // Fall back to user data
- let dataSeed = Math.random();
- let nodeDataProp = "abpNodeData" + dataSeed;
- let windowStatsProp = "abpWindowStats" + dataSeed;
- let windowSelectionProp = "abpWindowSelection" + dataSeed;
- let getProp = function(map)
- {
- switch (map)
- {
- case nodeData:
- return nodeDataProp;
- case windowStats:
- return windowStatsProp;
- case windowSelection:
- return windowSelectionProp;
- default:
- return null;
- }
- };
-
- setEntry = (map, key, value) => key.setUserData(getProp(map), value, null);
- hasEntry = (map, key) => key.getUserData(getProp(map));
- getEntry = (map, key) => key.getUserData(getProp(map)) || undefined;
-}
-
/**
* List of notifiers in use - these notifiers need to receive notifications on
* new requests.
* @type RequestNotifier[]
*/
let activeNotifiers = [];
/**
@@ -160,17 +123,17 @@ RequestNotifier.prototype =
let walker = doc.createTreeWalker(doc, Ci.nsIDOMNodeFilter.SHOW_ELEMENT, null, false);
let process = function()
{
if (!this.listener)
return;
let node = walker.currentNode;
- let data = getEntry(nodeData, node);
+ let data = nodeData.get(node);
if (typeof data != "undefined")
for (let k in data)
this.notifyListener(wnd, node, data[k]);
if (walker.nextNode())
Utils.runAsync(process);
else
{
@@ -190,22 +153,22 @@ RequestNotifier.prototype =
// Process each node in a separate event to allow other events to process
this.eventsPosted++;
Utils.runAsync(process);
}
};
RequestNotifier.storeSelection = function(/**Window*/ wnd, /**String*/ selection)
{
- setEntry(windowSelection, wnd.document, selection);
+ windowSelection.set(wnd.document, selection);
};
RequestNotifier.getSelection = function(/**Window*/ wnd) /**String*/
{
- if (hasEntry(windowSelection, wnd.document))
- return getEntry(windowSelection, wnd.document);
+ if (windowSelection.has(wnd.document))
+ return windowSelection.get(wnd.document);
else
return null;
};
/**
* Attaches request data to a DOM node.
* @param {Node} node node to attach data to
* @param {Window} topWnd top-level window the node belongs to
@@ -221,18 +184,18 @@ RequestNotifier.addNodeData = function(/
}
/**
* Retrieves the statistics for a window.
* @result {Object} Object with the properties items, blocked, whitelisted, hidden, filters containing statistics for the window (might be null)
*/
RequestNotifier.getWindowStatistics = function(/**Window*/ wnd)
{
- if (hasEntry(windowStats, wnd.document))
- return getEntry(windowStats, wnd.document);
+ if (windowStats.has(wnd.document))
+ return windowStats.get(wnd.document);
else
return null;
}
/**
* Retrieves the request entry associated with a DOM node.
* @param {Node} node
* @param {Boolean} noParent if missing or false, the search will extend to the parent nodes until one is found that has data associated with it
@@ -240,17 +203,17 @@ RequestNotifier.getWindowStatistics = fu
* @param {String} [location] request location to be looking for
* @result {[Node, RequestEntry]}
* @static
*/
RequestNotifier.getDataForNode = function(node, noParent, type, location)
{
while (node)
{
- let data = getEntry(nodeData, node);
+ let data = nodeData.get(node);
if (typeof data != "undefined")
{
let entry = null;
// Look for matching entry
for (let k in data)
{
if ((!entry || entry.id < data[k].id) &&
(typeof type == "undefined" || data[k].type == type) &&
@@ -285,28 +248,28 @@ function RequestEntry(node, topWnd, cont
this.thirdParty = thirdParty;
this.location = location;
this.filter = filter;
this.id = ++requestEntryMaxId;
this.attachToNode(node);
// Update window statistics
- if (!hasEntry(windowStats, topWnd.document))
+ if (!windowStats.has(topWnd.document))
{
- setEntry(windowStats, topWnd.document, {
+ windowStats.set(topWnd.document, {
items: 0,
hidden: 0,
blocked: 0,
whitelisted: 0,
filters: {}
});
}
- let stats = getEntry(windowStats, topWnd.document);
+ let stats = windowStats.get(topWnd.document);
if (!filter || !(filter instanceof ElemHideBase))
stats.items++;
if (filter)
{
if (filter instanceof BlockingFilter)
stats.blocked++;
else if (filter instanceof WhitelistFilter || filter instanceof ElemHideException)
stats.whitelisted++;
@@ -357,19 +320,19 @@ RequestEntry.prototype =
*/
filter: null,
/**
* Attaches this request object to a DOM node.
*/
attachToNode: function(/**Node*/ node)
{
- let existingData = getEntry(nodeData, node);
+ let existingData = nodeData.get(node);
if (typeof existingData == "undefined")
{
existingData = {};
- setEntry(nodeData, node, existingData);
+ nodeData.set(node, existingData);
}
// Add this request to the node data
existingData[this.type + " " + this.location] = this;
}
};
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld