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

Side by Side Diff: lib/requestNotifier.js

Issue 29329505: Issue 3222 - Replace the RequestEntry class by a plain object (Closed)
Patch Set: Created Oct. 29, 2015, 6:52 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 delete this.listener; 100 delete this.listener;
101 delete this.listenerObj; 101 delete this.listenerObj;
102 102
103 notifiers.delete(this.id); 103 notifiers.delete(this.id);
104 }, 104 },
105 105
106 /** 106 /**
107 * Notifies listener about a new request. 107 * Notifies listener about a new request.
108 * @param {Window} wnd 108 * @param {Window} wnd
109 * @param {Node} node 109 * @param {Node} node
110 * @param {RequestEntry} entry 110 * @param {Object} entry
111 */ 111 */
112 notifyListener: function(wnd, node, entry) 112 notifyListener: function(wnd, node, entry)
113 { 113 {
114 this.listener.call(this.listenerObj, wnd, node, entry, this.scanComplete); 114 this.listener.call(this.listenerObj, wnd, node, entry, this.scanComplete);
115 }, 115 },
116 116
117 /** 117 /**
118 * Number of currently posted scan events (will be 0 when the scan finishes 118 * Number of currently posted scan events (will be 0 when the scan finishes
119 * running). 119 * running).
120 */ 120 */
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 * @param {Node} node node to attach data to 180 * @param {Node} node node to attach data to
181 * @param {Window} topWnd top-level window the node belongs to 181 * @param {Window} topWnd top-level window the node belongs to
182 * @param {String} contentType request type, e.g. "IMAGE" 182 * @param {String} contentType request type, e.g. "IMAGE"
183 * @param {String} docDomain domain of the document that initiated the request 183 * @param {String} docDomain domain of the document that initiated the request
184 * @param {Boolean} thirdParty will be true if a third-party server has been re quested 184 * @param {Boolean} thirdParty will be true if a third-party server has been re quested
185 * @param {String} location the address that has been requested 185 * @param {String} location the address that has been requested
186 * @param {Filter} filter filter applied to the request or null if none 186 * @param {Filter} filter filter applied to the request or null if none
187 */ 187 */
188 RequestNotifier.addNodeData = function(/**Node*/ node, /**Window*/ topWnd, /**St ring*/ contentType, /**String*/ docDomain, /**Boolean*/ thirdParty, /**String*/ location, /**Filter*/ filter) 188 RequestNotifier.addNodeData = function(/**Node*/ node, /**Window*/ topWnd, /**St ring*/ contentType, /**String*/ docDomain, /**Boolean*/ thirdParty, /**String*/ location, /**Filter*/ filter)
189 { 189 {
190 return new RequestEntry(node, topWnd, contentType, docDomain, thirdParty, loca tion, filter); 190 let entry = {
191 id: ++requestEntryMaxId,
192 type: contentType,
193 docDomain, thirdParty, location, filter
194 }
195
196 let existingData = nodeData.get(node);
197 if (typeof existingData == "undefined")
198 {
199 existingData = {};
200 nodeData.set(node, existingData);
201 }
202
203 // Add this request to the node data
204 existingData[contentType + " " + location] = entry;
205
206 // Update window statistics
207 if (!windowStats.has(topWnd.document))
208 {
209 windowStats.set(topWnd.document, {
210 items: 0,
211 hidden: 0,
212 blocked: 0,
213 whitelisted: 0,
214 filters: {}
215 });
216 }
217
218 let stats = windowStats.get(topWnd.document);
219 if (!filter || !(filter instanceof ElemHideBase))
220 stats.items++;
221 if (filter)
222 {
223 if (filter instanceof BlockingFilter)
224 stats.blocked++;
225 else if (filter instanceof WhitelistFilter || filter instanceof ElemHideExce ption)
226 stats.whitelisted++;
227 else if (filter instanceof ElemHideFilter)
228 stats.hidden++;
229
230 if (filter.text in stats.filters)
231 stats.filters[filter.text]++;
232 else
233 stats.filters[filter.text] = 1;
234 }
235
236 // Notify listeners
237 for (let notifier of notifiers.values())
238 if (!notifier.window || notifier.window == topWnd)
239 notifier.notifyListener(topWnd, node, entry);
191 } 240 }
192 241
193 /** 242 /**
194 * Retrieves the statistics for a window. 243 * Retrieves the statistics for a window.
195 * @result {Object} Object with the properties items, blocked, whitelisted, hidd en, filters containing statistics for the window (might be null) 244 * @result {Object} Object with the properties items, blocked, whitelisted, hidd en, filters containing statistics for the window (might be null)
196 */ 245 */
197 RequestNotifier.getWindowStatistics = function(/**Window*/ wnd) 246 RequestNotifier.getWindowStatistics = function(/**Window*/ wnd)
198 { 247 {
199 if (windowStats.has(wnd.document)) 248 if (windowStats.has(wnd.document))
200 return windowStats.get(wnd.document); 249 return windowStats.get(wnd.document);
201 else 250 else
202 return null; 251 return null;
203 } 252 }
204 253
205 /** 254 /**
206 * Retrieves the request entry associated with a DOM node. 255 * Retrieves the request data associated with a DOM node.
207 * @param {Node} node 256 * @param {Node} node
208 * @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 257 * @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
209 * @param {Integer} [type] request type to be looking for 258 * @param {Integer} [type] request type to be looking for
210 * @param {String} [location] request location to be looking for 259 * @param {String} [location] request location to be looking for
211 * @result {[Node, RequestEntry]} 260 * @result {[Node, Object]}
212 * @static 261 * @static
213 */ 262 */
214 RequestNotifier.getDataForNode = function(node, noParent, type, location) 263 RequestNotifier.getDataForNode = function(node, noParent, type, location)
215 { 264 {
216 while (node) 265 while (node)
217 { 266 {
218 let data = nodeData.get(node); 267 let data = nodeData.get(node);
219 if (typeof data != "undefined") 268 if (typeof data != "undefined")
220 { 269 {
221 let entry = null; 270 let entry = null;
(...skipping 18 matching lines...) Expand all
240 node = node.parentNode; 289 node = node.parentNode;
241 } 290 }
242 else 291 else
243 { 292 {
244 node = null; 293 node = null;
245 } 294 }
246 } 295 }
247 296
248 return null; 297 return null;
249 }; 298 };
250
251 function RequestEntry(node, topWnd, contentType, docDomain, thirdParty, location , filter)
252 {
253 this.type = contentType;
254 this.docDomain = docDomain;
255 this.thirdParty = thirdParty;
256 this.location = location;
257 this.filter = filter;
258 this.id = ++requestEntryMaxId;
259
260 this.attachToNode(node);
261
262 // Update window statistics
263 if (!windowStats.has(topWnd.document))
264 {
265 windowStats.set(topWnd.document, {
266 items: 0,
267 hidden: 0,
268 blocked: 0,
269 whitelisted: 0,
270 filters: {}
271 });
272 }
273
274 let stats = windowStats.get(topWnd.document);
275 if (!filter || !(filter instanceof ElemHideBase))
276 stats.items++;
277 if (filter)
278 {
279 if (filter instanceof BlockingFilter)
280 stats.blocked++;
281 else if (filter instanceof WhitelistFilter || filter instanceof ElemHideExce ption)
282 stats.whitelisted++;
283 else if (filter instanceof ElemHideFilter)
284 stats.hidden++;
285
286 if (filter.text in stats.filters)
287 stats.filters[filter.text]++;
288 else
289 stats.filters[filter.text] = 1;
290 }
291
292 // Notify listeners
293 for (let notifier of notifiers.values())
294 if (!notifier.window || notifier.window == topWnd)
295 notifier.notifyListener(topWnd, node, this);
296 }
297 RequestEntry.prototype =
298 {
299 /**
300 * id of request (used to determine last entry attached to a node)
301 * @type integer
302 */
303 id: 0,
304 /**
305 * Content type of the request, e.g. "IMAGE"
306 * @type String
307 */
308 type: null,
309 /**
310 * Domain name of the requesting document
311 * @type String
312 */
313 docDomain: null,
314 /**
315 * True if the request goes to a different domain than the domain of the conta ining document
316 * @type Boolean
317 */
318 thirdParty: false,
319 /**
320 * Address being requested
321 * @type String
322 */
323 location: null,
324 /**
325 * Filter that was applied to this request (if any)
326 * @type Filter
327 */
328 filter: null,
329
330 /**
331 * Attaches this request object to a DOM node.
332 */
333 attachToNode: function(/**Node*/ node)
334 {
335 let existingData = nodeData.get(node);
336 if (typeof existingData == "undefined")
337 {
338 existingData = {};
339 nodeData.set(node, existingData);
340 }
341
342 // Add this request to the node data
343 existingData[this.type + " " + this.location] = this;
344 }
345 };
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld