LEFT | RIGHT |
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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 let {notifierID, responseID, requests} = message.data; | 73 let {notifierID, responseID, requests} = message.data; |
74 let notifier = notifiers.get(notifierID); | 74 let notifier = notifiers.get(notifierID); |
75 if (notifier) | 75 if (notifier) |
76 notifier.retrieveNodeSize(requests, responseID); | 76 notifier.retrieveNodeSize(requests, responseID); |
77 } | 77 } |
78 | 78 |
79 /** | 79 /** |
80 * Creates a notifier object for a particular window. After creation the window | 80 * Creates a notifier object for a particular window. After creation the window |
81 * will first be scanned for previously saved requests. Once that scan is | 81 * will first be scanned for previously saved requests. Once that scan is |
82 * complete only new requests for this window will be reported. | 82 * complete only new requests for this window will be reported. |
83 * @param {Window} wnd window to attach the notifier to | 83 * @param {Window} window window to attach the notifier to |
84 * @param {Integer} id Parent notifier ID to be messaged | 84 * @param {Integer} notifierID Parent notifier ID to be messaged |
85 */ | 85 */ |
86 function RequestNotifier(window, notifierID) | 86 function RequestNotifier(window, notifierID) |
87 { | 87 { |
88 this.window = window; | 88 this.window = window; |
89 this.id = notifierID; | 89 this.id = notifierID; |
90 notifiers.set(this.id, this); | 90 notifiers.set(this.id, this); |
91 this.nodes = new Map(); | 91 this.nodes = new Map(); |
92 this.startScan(window); | 92 this.startScan(window); |
93 } | 93 } |
94 exports.RequestNotifier = RequestNotifier; | 94 exports.RequestNotifier = RequestNotifier; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
155 * Starts the initial scan of the window (will recurse into frames). | 155 * Starts the initial scan of the window (will recurse into frames). |
156 * @param {Window} wnd the window to be scanned | 156 * @param {Window} wnd the window to be scanned |
157 */ | 157 */ |
158 startScan: function(wnd) | 158 startScan: function(wnd) |
159 { | 159 { |
160 let doc = wnd.document; | 160 let doc = wnd.document; |
161 let walker = doc.createTreeWalker(doc, Ci.nsIDOMNodeFilter.SHOW_ELEMENT, nul
l, false); | 161 let walker = doc.createTreeWalker(doc, Ci.nsIDOMNodeFilter.SHOW_ELEMENT, nul
l, false); |
162 | 162 |
163 let process = function() | 163 let process = function() |
164 { | 164 { |
| 165 // Don't do anything if the notifier was shut down already. |
165 if (!this.window) | 166 if (!this.window) |
166 return; | 167 return; |
167 | 168 |
168 let node = walker.currentNode; | 169 let node = walker.currentNode; |
169 let data = nodeData.get(node); | 170 let data = nodeData.get(node); |
170 if (typeof data != "undefined") | 171 if (typeof data != "undefined") |
171 for (let k in data) | 172 for (let k in data) |
172 this.notifyListener(node, data[k]); | 173 this.notifyListener(node, data[k]); |
173 | 174 |
174 if (walker.nextNode()) | 175 if (walker.nextNode()) |
(...skipping 15 matching lines...) Expand all Loading... |
190 | 191 |
191 // Process each node in a separate event to allow other events to process | 192 // Process each node in a separate event to allow other events to process |
192 this.eventsPosted++; | 193 this.eventsPosted++; |
193 Utils.runAsync(process); | 194 Utils.runAsync(process); |
194 }, | 195 }, |
195 | 196 |
196 /** | 197 /** |
197 * Makes the nodes associated with the given requests blink. | 198 * Makes the nodes associated with the given requests blink. |
198 * @param {number[]} requests list of request IDs that were previously | 199 * @param {number[]} requests list of request IDs that were previously |
199 * reported by this notifier. | 200 * reported by this notifier. |
200 * @param {Boolean} scrollToItem if true, scroll to first node | 201 * @param {boolean} scrollToItem if true, scroll to first node |
201 */ | 202 */ |
202 flashNodes: function(requests, scrollToItem) | 203 flashNodes: function(requests, scrollToItem) |
203 { | 204 { |
204 this.stopFlashing(); | 205 this.stopFlashing(); |
205 | 206 |
206 let nodes = []; | 207 let nodes = []; |
207 for (let id of requests) | 208 for (let id of requests) |
208 { | 209 { |
| 210 if (!this.nodes.has(id)) |
| 211 continue; |
| 212 |
209 let node = this.nodes.get(id); | 213 let node = this.nodes.get(id); |
210 if (typeof node != "undefined" && node.nodeType == Ci.nsIDOMNode.ELEMENT_N
ODE) | 214 if (Cu.isDeadWrapper(node)) |
| 215 this.nodes.delete(node); |
| 216 else if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) |
211 nodes.push(node); | 217 nodes.push(node); |
212 } | 218 } |
213 if (nodes.length) | 219 if (nodes.length) |
214 this.flasher = new Flasher(nodes, scrollToItem); | 220 this.flasher = new Flasher(nodes, scrollToItem); |
215 }, | 221 }, |
216 | 222 |
217 /** | 223 /** |
218 * Stops flashing nodes after a previous flashNodes() call. | 224 * Stops flashing nodes after a previous flashNodes() call. |
219 */ | 225 */ |
220 stopFlashing: function() | 226 stopFlashing: function() |
221 { | 227 { |
222 if (this.flasher) | 228 if (this.flasher) |
223 this.flasher.stop(); | 229 this.flasher.stop(); |
224 this.flasher = null; | 230 this.flasher = null; |
225 }, | 231 }, |
226 | 232 |
227 /** | 233 /** |
228 * Attempts to calculate the size of the nodes associated with the requests, | 234 * Attempts to calculate the size of the nodes associated with the requests, |
229 * Sends message to parent when complete. | 235 * Sends message to parent when complete. |
230 * @param {number[]} requests list of request IDs that were previously | 236 * @param {number[]} requests list of request IDs that were previously |
231 * reported by this notifier. | 237 * reported by this notifier. |
232 * @param {number} responseID ID to be sent with the response. | 238 * @param {number} responseID ID to be sent with the response. |
233 */ | 239 */ |
234 retrieveNodeSize: function(requests, responseID) | 240 retrieveNodeSize: function(requests, responseID) |
235 { | 241 { |
236 function getNodeSize(node) | 242 function getNodeSize(node) |
237 { | 243 { |
238 if (typeof node != "undefined" && !Cu.isDeadWrapper(node)) | 244 if (node instanceof Ci.nsIDOMHTMLImageElement && (node.naturalWidth || nod
e.naturalHeight)) |
239 { | 245 return [node.naturalWidth, node.naturalHeight]; |
240 if (node instanceof Ci.nsIDOMHTMLImageElement && (node.naturalWidth || n
ode.naturalHeight)) | 246 else if (node instanceof Ci.nsIDOMHTMLElement && (node.offsetWidth || node
.offsetHeight)) |
241 return [node.naturalWidth, node.naturalHeight]; | 247 return [node.offsetWidth, node.offsetHeight]; |
242 else if (node instanceof Ci.nsIDOMHTMLElement && (node.offsetWidth || no
de.offsetHeight)) | 248 else |
243 return [node.offsetWidth, node.offsetHeight]; | 249 return null; |
244 } | |
245 return null; | |
246 } | 250 } |
247 | 251 |
248 let size = null; | 252 let size = null; |
249 for (let id of requests) | 253 for (let id of requests) |
250 { | 254 { |
251 size = getNodeSize(this.nodes.get(id)); | 255 if (!this.nodes.has(id)) |
252 if (size) | 256 continue; |
253 break; | 257 |
| 258 let node = this.nodes.get(id); |
| 259 if (Cu.isDeadWrapper(node)) |
| 260 this.nodes.delete(node); |
| 261 else |
| 262 { |
| 263 size = getNodeSize(node); |
| 264 if (size) |
| 265 break; |
| 266 } |
254 } | 267 } |
255 sendAsyncMessage("AdblockPlus:RetrieveNodeSizeResponse", { | 268 sendAsyncMessage("AdblockPlus:RetrieveNodeSizeResponse", { |
256 notifierID: this.id, | 269 notifierID: this.id, |
257 responseID, | 270 responseID, |
258 size | 271 size |
259 }); | 272 }); |
260 } | 273 } |
261 }; | 274 }; |
262 | 275 |
263 /** | 276 /** |
264 * Attaches request data to a DOM node. | 277 * Attaches request data to a DOM node. |
265 * @param {Node} node node to attach data to | 278 * @param {Node} node node to attach data to |
266 * @param {Window} topWnd top-level window the node belongs to | 279 * @param {Window} topWnd top-level window the node belongs to |
267 * @param {String} contentType request type, e.g. "IMAGE" | 280 * @param {Object} hitData |
268 * @param {String} docDomain domain of the document that initiated the request | 281 * @param {String} hitData.contentType request type, e.g. "IMAGE" |
269 * @param {Boolean} thirdParty will be true if a third-party server has been re
quested | 282 * @param {String} hitData.docDomain domain of the document that initiated the
request |
270 * @param {String} location the address that has been requested | 283 * @param {Boolean} hitData.thirdParty will be true if a third-party server has
been requested |
271 * @param {Filter} filter filter applied to the request or null if none | 284 * @param {String} hitData.location the address that has been requested |
272 */ | 285 * @param {String} hitData.filter filter applied to the request or null if non
e |
273 RequestNotifier.addNodeData = function(node, topWnd, contentType, docDomain, thi
rdParty, location, filter) | 286 * @param {String} hitData.filterType type of filter applied to the request |
| 287 */ |
| 288 RequestNotifier.addNodeData = function(node, topWnd, {contentType, docDomain, th
irdParty, location, filter, filterType}) |
274 { | 289 { |
275 let entry = { | 290 let entry = { |
276 id: ++requestEntryMaxId, | 291 id: ++requestEntryMaxId, |
277 type: contentType, | 292 type: contentType, |
278 docDomain, thirdParty, location, filter | 293 docDomain, thirdParty, location, filter |
279 }; | 294 }; |
280 | 295 |
281 let existingData = nodeData.get(node); | 296 let existingData = nodeData.get(node); |
282 if (typeof existingData == "undefined") | 297 if (typeof existingData == "undefined") |
283 { | 298 { |
(...skipping 10 matching lines...) Expand all Loading... |
294 windowStats.set(topWnd.document, { | 309 windowStats.set(topWnd.document, { |
295 items: 0, | 310 items: 0, |
296 hidden: 0, | 311 hidden: 0, |
297 blocked: 0, | 312 blocked: 0, |
298 whitelisted: 0, | 313 whitelisted: 0, |
299 filters: {} | 314 filters: {} |
300 }); | 315 }); |
301 } | 316 } |
302 | 317 |
303 let stats = windowStats.get(topWnd.document); | 318 let stats = windowStats.get(topWnd.document); |
304 let filterType = (filter ? filter.type : null); | |
305 if (filterType != "elemhide" && filterType != "elemhideexception" && filterTyp
e != "cssproperty") | 319 if (filterType != "elemhide" && filterType != "elemhideexception" && filterTyp
e != "cssproperty") |
306 stats.items++; | 320 stats.items++; |
307 if (filter) | 321 if (filter) |
308 { | 322 { |
309 if (filterType == "blocking") | 323 if (filterType == "blocking") |
310 stats.blocked++; | 324 stats.blocked++; |
311 else if (filterType == "whitelist" || filterType == "elemhideexception") | 325 else if (filterType == "whitelist" || filterType == "elemhideexception") |
312 stats.whitelisted++; | 326 stats.whitelisted++; |
313 else if (filterType == "elemhide" || filterType == "cssproperty") | 327 else if (filterType == "elemhide" || filterType == "cssproperty") |
314 stats.hidden++; | 328 stats.hidden++; |
315 | 329 |
316 if (filter.text in stats.filters) | 330 if (filter in stats.filters) |
317 stats.filters[filter.text]++; | 331 stats.filters[filter]++; |
318 else | 332 else |
319 stats.filters[filter.text] = 1; | 333 stats.filters[filter] = 1; |
320 } | 334 } |
321 | 335 |
322 // Notify listeners | 336 // Notify listeners |
323 for (let notifier of notifiers.values()) | 337 for (let notifier of notifiers.values()) |
324 if (!notifier.window || notifier.window == topWnd) | 338 if (!notifier.window || notifier.window == topWnd) |
325 notifier.notifyListener(node, entry); | 339 notifier.notifyListener(node, entry); |
326 } | 340 } |
LEFT | RIGHT |