OLD | NEW |
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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 /** | 18 /** |
19 * @fileOverview Stores Adblock Plus data to be attached to a window. | 19 * @fileOverview Stores Adblock Plus data to be attached to a window. |
20 */ | 20 */ |
21 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | 21 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
22 | 22 |
23 let {Utils} = require("utils"); | 23 let {Utils} = require("utils"); |
| 24 let {Flasher} = require("child/flasher"); |
24 | 25 |
25 let nodeData = new WeakMap(); | 26 let nodeData = new WeakMap(); |
26 let windowStats = new WeakMap(); | 27 let windowStats = new WeakMap(); |
27 let requestEntryMaxId = 0; | 28 let requestEntryMaxId = 0; |
28 | 29 |
29 /** | 30 /** |
30 * Active RequestNotifier instances by their ID | 31 * Active RequestNotifier instances by their ID |
31 * @type Map.<number,RequestNotifier> | 32 * @type Map.<number,RequestNotifier> |
32 */ | 33 */ |
33 let notifiers = new Map(); | 34 let notifiers = new Map(); |
34 | 35 |
35 addMessageListener("AdblockPlus:StartWindowScan", onStartScan); | 36 addMessageListener("AdblockPlus:StartWindowScan", onStartScan); |
36 addMessageListener("AdblockPlus:ShutdownNotifier", onNotifierShutdown); | 37 addMessageListener("AdblockPlus:ShutdownNotifier", onNotifierShutdown); |
| 38 addMessageListener("AdblockPlus:FlashNodes", onFlashNodes); |
37 | 39 |
38 onShutdown.add(() => { | 40 onShutdown.add(() => { |
39 removeMessageListener("AdblockPlus:StartWindowScan", onStartScan); | 41 removeMessageListener("AdblockPlus:StartWindowScan", onStartScan); |
40 removeMessageListener("AdblockPlus:ShutdownNotifier", onNotifierShutdown); | 42 removeMessageListener("AdblockPlus:ShutdownNotifier", onNotifierShutdown); |
| 43 removeMessageListener("AdblockPlus:FlashNodes", onFlashNodes); |
41 }); | 44 }); |
42 | 45 |
43 function onStartScan(message) | 46 function onStartScan(message) |
44 { | 47 { |
45 let {notifierID, outerWindowID} = message.data; | 48 let {notifierID, outerWindowID} = message.data; |
46 let window = Services.wm.getOuterWindowWithId(outerWindowID); | 49 let window = Services.wm.getOuterWindowWithId(outerWindowID); |
47 if (window) | 50 if (window) |
48 new RequestNotifier(window, notifierID); | 51 new RequestNotifier(window, notifierID); |
49 } | 52 } |
50 | 53 |
51 function onNotifierShutdown(message) | 54 function onNotifierShutdown(message) |
52 { | 55 { |
53 let notifier = notifiers.get(message.data); | 56 let notifier = notifiers.get(message.data); |
54 if (notifier) | 57 if (notifier) |
55 notifier.shutdown(); | 58 notifier.shutdown(); |
56 } | 59 } |
57 | 60 |
| 61 function onFlashNodes(message) |
| 62 { |
| 63 let {notifierID, requests, scrollToItem} = message.data; |
| 64 let notifier = notifiers.get(notifierID); |
| 65 if (notifier) |
| 66 notifier.flashNodes(requests, scrollToItem); |
| 67 } |
| 68 |
58 /** | 69 /** |
59 * Creates a notifier object for a particular window. After creation the window | 70 * Creates a notifier object for a particular window. After creation the window |
60 * will first be scanned for previously saved requests. Once that scan is | 71 * will first be scanned for previously saved requests. Once that scan is |
61 * complete only new requests for this window will be reported. | 72 * complete only new requests for this window will be reported. |
62 * @param {Window} window window to attach the notifier to | 73 * @param {Window} window window to attach the notifier to |
63 * @param {Integer} notifierID Parent notifier ID to be messaged | 74 * @param {Integer} notifierID Parent notifier ID to be messaged |
64 */ | 75 */ |
65 function RequestNotifier(window, notifierID) | 76 function RequestNotifier(window, notifierID) |
66 { | 77 { |
67 this.window = window; | 78 this.window = window; |
68 this.id = notifierID; | 79 this.id = notifierID; |
69 notifiers.set(this.id, this); | 80 notifiers.set(this.id, this); |
| 81 this.nodes = new Map(); |
70 this.startScan(window); | 82 this.startScan(window); |
71 } | 83 } |
72 exports.RequestNotifier = RequestNotifier; | 84 exports.RequestNotifier = RequestNotifier; |
73 | 85 |
74 RequestNotifier.prototype = | 86 RequestNotifier.prototype = |
75 { | 87 { |
76 /** | 88 /** |
77 * Parent notifier ID to be messaged | 89 * Parent notifier ID to be messaged |
78 * @type Integer | 90 * @type Integer |
79 */ | 91 */ |
80 id: null, | 92 id: null, |
81 | 93 |
82 /** | 94 /** |
83 * The window this notifier is associated with. | 95 * The window this notifier is associated with. |
84 * @type Window | 96 * @type Window |
85 */ | 97 */ |
86 window: null, | 98 window: null, |
87 | 99 |
88 /** | 100 /** |
| 101 * Nodes associated with a particular request ID. |
| 102 * @type Map.<number,Node> |
| 103 */ |
| 104 nodes: null, |
| 105 |
| 106 /** |
89 * Shuts down the notifier once it is no longer used. The listener | 107 * Shuts down the notifier once it is no longer used. The listener |
90 * will no longer be called after that. | 108 * will no longer be called after that. |
91 */ | 109 */ |
92 shutdown: function() | 110 shutdown: function() |
93 { | 111 { |
94 delete this.window; | 112 delete this.window; |
| 113 delete this.nodes; |
| 114 this.stopFlashing(); |
95 notifiers.delete(this.id); | 115 notifiers.delete(this.id); |
96 }, | 116 }, |
97 | 117 |
98 /** | 118 /** |
99 * Notifies the parent about a new request. | 119 * Notifies the parent about a new request. |
| 120 * @param {Node} node DOM node that the request is associated with |
100 * @param {Object} entry | 121 * @param {Object} entry |
101 */ | 122 */ |
102 notifyListener: function(entry) | 123 notifyListener: function(node, entry) |
103 { | 124 { |
| 125 if (this.nodes) |
| 126 this.nodes.set(entry.id, node); |
104 sendAsyncMessage("AdblockPlus:FoundNodeData", { | 127 sendAsyncMessage("AdblockPlus:FoundNodeData", { |
105 notifierID: this.id, | 128 notifierID: this.id, |
106 data: entry | 129 data: entry |
107 }); | 130 }); |
108 }, | 131 }, |
109 | 132 |
110 onComplete: function() | 133 onComplete: function() |
111 { | 134 { |
112 sendAsyncMessage("AdblockPlus:ScanComplete", this.id); | 135 sendAsyncMessage("AdblockPlus:ScanComplete", this.id); |
113 }, | 136 }, |
(...skipping 16 matching lines...) Expand all Loading... |
130 let process = function() | 153 let process = function() |
131 { | 154 { |
132 // Don't do anything if the notifier was shut down already. | 155 // Don't do anything if the notifier was shut down already. |
133 if (!this.window) | 156 if (!this.window) |
134 return; | 157 return; |
135 | 158 |
136 let node = walker.currentNode; | 159 let node = walker.currentNode; |
137 let data = nodeData.get(node); | 160 let data = nodeData.get(node); |
138 if (typeof data != "undefined") | 161 if (typeof data != "undefined") |
139 for (let k in data) | 162 for (let k in data) |
140 this.notifyListener(data[k]); | 163 this.notifyListener(node, data[k]); |
141 | 164 |
142 if (walker.nextNode()) | 165 if (walker.nextNode()) |
143 Utils.runAsync(process); | 166 Utils.runAsync(process); |
144 else | 167 else |
145 { | 168 { |
146 // Done with the current window, start the scan for its frames | 169 // Done with the current window, start the scan for its frames |
147 for (let i = 0; i < wnd.frames.length; i++) | 170 for (let i = 0; i < wnd.frames.length; i++) |
148 this.startScan(wnd.frames[i]); | 171 this.startScan(wnd.frames[i]); |
149 | 172 |
150 this.eventsPosted--; | 173 this.eventsPosted--; |
151 if (!this.eventsPosted) | 174 if (!this.eventsPosted) |
152 { | 175 { |
153 this.scanComplete = true; | 176 this.scanComplete = true; |
154 this.onComplete(); | 177 this.onComplete(); |
155 } | 178 } |
156 } | 179 } |
157 }.bind(this); | 180 }.bind(this); |
158 | 181 |
159 // Process each node in a separate event to allow other events to process | 182 // Process each node in a separate event to allow other events to process |
160 this.eventsPosted++; | 183 this.eventsPosted++; |
161 Utils.runAsync(process); | 184 Utils.runAsync(process); |
| 185 }, |
| 186 |
| 187 /** |
| 188 * Makes the nodes associated with the given requests blink. |
| 189 * @param {number[]} requests list of request IDs that were previously |
| 190 * reported by this notifier. |
| 191 * @param {boolean} scrollToItem if true, scroll to first node |
| 192 */ |
| 193 flashNodes: function(requests, scrollToItem) |
| 194 { |
| 195 this.stopFlashing(); |
| 196 |
| 197 let nodes = []; |
| 198 for (let id of requests) |
| 199 { |
| 200 if (!this.nodes.has(id)) |
| 201 continue; |
| 202 |
| 203 let node = this.nodes.get(id); |
| 204 if (Cu.isDeadWrapper(node)) |
| 205 this.nodes.delete(node); |
| 206 else if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) |
| 207 nodes.push(node); |
| 208 } |
| 209 if (nodes.length) |
| 210 this.flasher = new Flasher(nodes, scrollToItem); |
| 211 }, |
| 212 |
| 213 /** |
| 214 * Stops flashing nodes after a previous flashNodes() call. |
| 215 */ |
| 216 stopFlashing: function() |
| 217 { |
| 218 if (this.flasher) |
| 219 this.flasher.stop(); |
| 220 this.flasher = null; |
162 } | 221 } |
163 }; | 222 }; |
164 | 223 |
165 /** | 224 /** |
166 * Attaches request data to a DOM node. | 225 * Attaches request data to a DOM node. |
167 * @param {Node} node node to attach data to | 226 * @param {Node} node node to attach data to |
168 * @param {Window} topWnd top-level window the node belongs to | 227 * @param {Window} topWnd top-level window the node belongs to |
169 * @param {String} contentType request type, e.g. "IMAGE" | 228 * @param {String} contentType request type, e.g. "IMAGE" |
170 * @param {String} docDomain domain of the document that initiated the request | 229 * @param {String} docDomain domain of the document that initiated the request |
171 * @param {Boolean} thirdParty will be true if a third-party server has been re
quested | 230 * @param {Boolean} thirdParty will be true if a third-party server has been re
quested |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 276 |
218 if (filter.text in stats.filters) | 277 if (filter.text in stats.filters) |
219 stats.filters[filter.text]++; | 278 stats.filters[filter.text]++; |
220 else | 279 else |
221 stats.filters[filter.text] = 1; | 280 stats.filters[filter.text] = 1; |
222 } | 281 } |
223 | 282 |
224 // Notify listeners | 283 // Notify listeners |
225 for (let notifier of notifiers.values()) | 284 for (let notifier of notifiers.values()) |
226 if (!notifier.window || notifier.window == topWnd) | 285 if (!notifier.window || notifier.window == topWnd) |
227 notifier.notifyListener(entry); | 286 notifier.notifyListener(node, entry); |
228 } | 287 } |
OLD | NEW |