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

Side by Side Diff: lib/child/requestNotifier.js

Issue 29329779: Issue 3258 - Blockable items: restore item flashing functionality (Closed)
Patch Set: Better handling of dead objects Created Nov. 5, 2015, 11:06 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
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
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} wnd window to attach the notifier to 73 * @param {Window} wnd window to attach the notifier to
63 * @param {Integer} id Parent notifier ID to be messaged 74 * @param {Integer} id 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 15 matching lines...) Expand all
129 152
130 let process = function() 153 let process = function()
131 { 154 {
132 if (!this.window) 155 if (!this.window)
133 return; 156 return;
134 157
135 let node = walker.currentNode; 158 let node = walker.currentNode;
136 let data = nodeData.get(node); 159 let data = nodeData.get(node);
137 if (typeof data != "undefined") 160 if (typeof data != "undefined")
138 for (let k in data) 161 for (let k in data)
139 this.notifyListener(data[k]); 162 this.notifyListener(node, data[k]);
140 163
141 if (walker.nextNode()) 164 if (walker.nextNode())
142 Utils.runAsync(process); 165 Utils.runAsync(process);
143 else 166 else
144 { 167 {
145 // Done with the current window, start the scan for its frames 168 // Done with the current window, start the scan for its frames
146 for (let i = 0; i < wnd.frames.length; i++) 169 for (let i = 0; i < wnd.frames.length; i++)
147 this.startScan(wnd.frames[i]); 170 this.startScan(wnd.frames[i]);
148 171
149 this.eventsPosted--; 172 this.eventsPosted--;
150 if (!this.eventsPosted) 173 if (!this.eventsPosted)
151 { 174 {
152 this.scanComplete = true; 175 this.scanComplete = true;
153 this.onComplete(); 176 this.onComplete();
154 } 177 }
155 } 178 }
156 }.bind(this); 179 }.bind(this);
157 180
158 // Process each node in a separate event to allow other events to process 181 // Process each node in a separate event to allow other events to process
159 this.eventsPosted++; 182 this.eventsPosted++;
160 Utils.runAsync(process); 183 Utils.runAsync(process);
184 },
185
186 /**
187 * Makes the nodes associated with the given requests blink.
188 * @param {number[]} requests list of request IDs that were previously
189 * reported by this notifier.
190 * @param {Boolean} scrollToItem if true, scroll to first node
191 */
192 flashNodes: function(requests, scrollToItem)
193 {
194 this.stopFlashing();
195
196 let nodes = [];
197 for (let id of requests)
198 {
199 if (!this.nodes.has(id))
200 continue;
201
202 let node = this.nodes.get(id);
203 if (Cu.isDeadWrapper(node))
204 this.nodes.delete(node);
205 else if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE)
206 nodes.push(node);
207 }
208 if (nodes.length)
209 this.flasher = new Flasher(nodes, scrollToItem);
210 },
211
212 /**
213 * Stops flashing nodes after a previous flashNodes() call.
214 */
215 stopFlashing: function()
216 {
217 if (this.flasher)
218 this.flasher.stop();
219 this.flasher = null;
161 } 220 }
162 }; 221 };
163 222
164 /** 223 /**
165 * Attaches request data to a DOM node. 224 * Attaches request data to a DOM node.
166 * @param {Node} node node to attach data to 225 * @param {Node} node node to attach data to
167 * @param {Window} topWnd top-level window the node belongs to 226 * @param {Window} topWnd top-level window the node belongs to
168 * @param {String} contentType request type, e.g. "IMAGE" 227 * @param {String} contentType request type, e.g. "IMAGE"
169 * @param {String} docDomain domain of the document that initiated the request 228 * @param {String} docDomain domain of the document that initiated the request
170 * @param {Boolean} thirdParty will be true if a third-party server has been re quested 229 * @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
216 275
217 if (filter.text in stats.filters) 276 if (filter.text in stats.filters)
218 stats.filters[filter.text]++; 277 stats.filters[filter.text]++;
219 else 278 else
220 stats.filters[filter.text] = 1; 279 stats.filters[filter.text] = 1;
221 } 280 }
222 281
223 // Notify listeners 282 // Notify listeners
224 for (let notifier of notifiers.values()) 283 for (let notifier of notifiers.values())
225 if (!notifier.window || notifier.window == topWnd) 284 if (!notifier.window || notifier.window == topWnd)
226 notifier.notifyListener(entry); 285 notifier.notifyListener(node, entry);
227 } 286 }
OLDNEW

Powered by Google App Engine
This is Rietveld