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

Side by Side Diff: lib/requestNotifier.js

Issue 29338417: Issue 3499 - Use the new messaging API for the request notifier (Closed)
Patch Set: Created March 16, 2016, 12:33 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 | « lib/child/requestNotifier.js ('k') | 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 21
22 let {Utils} = require("utils"); 22 let {port} = require("messaging");
23 23
24 let windowSelection = new WeakMap();
25 let requestNotifierMaxId = 0; 24 let requestNotifierMaxId = 0;
26 25
27 let windowStatsMaxResponseID = 0;
28 let windowStatsCallbacks = new Map();
29
30 let windowDataMaxResponseID = 0;
31 let windowDataCallbacks = new Map();
32
33 /** 26 /**
34 * Active RequestNotifier instances by their ID 27 * Active RequestNotifier instances by their ID
35 * @type Map.<number,RequestNotifier> 28 * @type Map.<number,RequestNotifier>
36 */ 29 */
37 let notifiers = new Map(); 30 let notifiers = new Map();
38 31
39 let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] 32 port.on("foundNodeData", ({notifierID, data}, sender) =>
40 .getService(Ci.nsIMessageListenerManager)
41 .QueryInterface(Ci.nsIMessageBroadcaster);
42
43 Utils.addChildMessageListener("AdblockPlus:FoundNodeData", onNodeData);
44 Utils.addChildMessageListener("AdblockPlus:ScanComplete", onScanComplete);
45 Utils.addChildMessageListener("AdblockPlus:NotifierResponse", onNotifierResponse );
46 Utils.addChildMessageListener("AdblockPlus:RetrieveWindowStatsResponse", onWindo wStatsReceived);
47 Utils.addChildMessageListener("AdblockPlus:RetrieveWindowDataResponse", onWindow DataReceived);
48
49 function onNodeData({notifierID, data})
50 { 33 {
51 let notifier = notifiers.get(notifierID); 34 let notifier = notifiers.get(notifierID);
52 if (notifier) 35 if (notifier)
53 notifier.notifyListener(data); 36 notifier.notifyListener(data);
54 } 37 });
55 38
56 function onScanComplete(notifierID) 39 port.on("scanComplete", (notifierID, sender) =>
57 { 40 {
58 let notifier = notifiers.get(notifierID); 41 let notifier = notifiers.get(notifierID);
59 if (notifier) 42 if (notifier)
60 notifier.onComplete(); 43 notifier.onComplete();
61 } 44 });
62
63 function onNotifierResponse({notifierID, responseID, response})
64 {
65 let notifier = notifiers.get(notifierID);
66 if (notifier)
67 notifier.onResponse(responseID, response);
68 }
69
70 function onWindowStatsReceived({responseID, stats})
71 {
72 let callback = windowStatsCallbacks.get(responseID);
73 windowStatsCallbacks.delete(responseID);
74 if (typeof callback == "function")
75 callback(stats);
76 }
77
78 function onWindowDataReceived({responseID, data})
79 {
80 let callback = windowDataCallbacks.get(responseID);
81 windowDataCallbacks.delete(responseID);
82 if (typeof callback == "function")
83 callback(data);
84 }
85 45
86 /** 46 /**
87 * Creates a notifier object for a particular window. After creation the window 47 * Creates a notifier object for a particular window. After creation the window
88 * will first be scanned for previously saved requests. Once that scan is 48 * will first be scanned for previously saved requests. Once that scan is
89 * complete only new requests for this window will be reported. 49 * complete only new requests for this window will be reported.
90 * @param {Integer} outerWindowID ID of the window to attach the notifier to 50 * @param {Integer} outerWindowID ID of the window to attach the notifier to
91 * @param {Function} listener listener to be called whenever a new request is f ound 51 * @param {Function} listener listener to be called whenever a new request is f ound
92 * @param {Object} [listenerObj] "this" pointer to be used when calling the lis tener 52 * @param {Object} [listenerObj] "this" pointer to be used when calling the lis tener
93 */ 53 */
94 function RequestNotifier(outerWindowID, listener, listenerObj) 54 function RequestNotifier(outerWindowID, listener, listenerObj)
95 { 55 {
96 this.listener = listener; 56 this.listener = listener;
97 this.listenerObj = listenerObj || null; 57 this.listenerObj = listenerObj || null;
98 this.id = ++requestNotifierMaxId; 58 this.id = ++requestNotifierMaxId;
99 notifiers.set(this.id, this); 59 notifiers.set(this.id, this);
100 this._callbacks = new Map();
101 60
102 messageManager.broadcastAsyncMessage("AdblockPlus:StartWindowScan", { 61 port.emit("startWindowScan", {
103 notifierID: this.id, 62 notifierID: this.id,
104 outerWindowID: outerWindowID 63 outerWindowID: outerWindowID
105 }); 64 });
106 } 65 }
107 exports.RequestNotifier = RequestNotifier; 66 exports.RequestNotifier = RequestNotifier;
108 67
109 RequestNotifier.prototype = 68 RequestNotifier.prototype =
110 { 69 {
111 /** 70 /**
112 * The unique ID of this notifier. 71 * The unique ID of this notifier.
(...skipping 19 matching lines...) Expand all
132 */ 91 */
133 scanComplete: false, 92 scanComplete: false,
134 93
135 /** 94 /**
136 * Shuts down the notifier once it is no longer used. The listener 95 * Shuts down the notifier once it is no longer used. The listener
137 * will no longer be called after that. 96 * will no longer be called after that.
138 */ 97 */
139 shutdown: function() 98 shutdown: function()
140 { 99 {
141 notifiers.delete(this.id); 100 notifiers.delete(this.id);
142 messageManager.broadcastAsyncMessage("AdblockPlus:ShutdownNotifier", this.id ); 101 port.emit("shutdownNotifier", this.id);
143 }, 102 },
144 103
145 /** 104 /**
146 * Notifies listener about a new request. 105 * Notifies listener about a new request.
147 * @param {Object} entry 106 * @param {Object} entry
148 */ 107 */
149 notifyListener: function(entry) 108 notifyListener: function(entry)
150 { 109 {
151 this.listener.call(this.listenerObj, entry, this.scanComplete); 110 this.listener.call(this.listenerObj, entry, this.scanComplete);
152 }, 111 },
153 112
154 onComplete: function() 113 onComplete: function()
155 { 114 {
156 this.scanComplete = true; 115 this.scanComplete = true;
157 this.notifyListener(null); 116 this.notifyListener(null);
158 }, 117 },
159 118
160 /** 119 /**
161 * Makes the nodes associated with the given requests blink. 120 * Makes the nodes associated with the given requests blink.
162 * @param {number[]} requests list of request IDs that were previously 121 * @param {number[]} requests list of request IDs that were previously
163 * reported by this notifier. 122 * reported by this notifier.
164 * @param {Boolean} scrollToItem if true, scroll to first node 123 * @param {Boolean} scrollToItem if true, scroll to first node
165 */ 124 */
166 flashNodes: function(requests, scrollToItem) 125 flashNodes: function(requests, scrollToItem)
167 { 126 {
168 if (!requests) 127 if (!requests)
169 requests = []; 128 requests = [];
170 129
171 messageManager.broadcastAsyncMessage("AdblockPlus:FlashNodes", { 130 port.emit("flashNodes", {
172 notifierID: this.id, 131 notifierID: this.id,
173 requests, 132 requests,
174 scrollToItem 133 scrollToItem
175 }); 134 });
176 }, 135 },
177 136
178 _maxResponseID: 0,
179 _callbacks: null,
180
181 /** 137 /**
182 * Attempts to calculate the size of the nodes associated with the requests. 138 * Attempts to calculate the size of the nodes associated with the requests.
183 * @param {number[]} requests list of request IDs that were previously 139 * @param {number[]} requests list of request IDs that were previously
184 * reported by this notifier. 140 * reported by this notifier.
185 * @param {Function} callback function to be called with two parameters (x,y) 141 * @param {Function} callback function to be called with two parameters (x,y)
186 */ 142 */
187 retrieveNodeSize: function(requests, callback) 143 retrieveNodeSize: function(requests, callback)
188 { 144 {
189 if (!requests) 145 if (!requests)
190 requests = []; 146 requests = [];
191 147
192 let id = ++this._maxResponseID; 148 port.emitWithResponse("retrieveNodeSize", {
193 this._callbacks.set(id, callback);
194
195 messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveNodeSize", {
196 notifierID: this.id, 149 notifierID: this.id,
197 responseID: id, 150 requests
198 requests, 151 }).then(callback);
199 });
200 },
201
202 onResponse: function(responseID, response)
203 {
204 let callback = this._callbacks.get(responseID);
205 this._callbacks.delete(responseID);
206 if (typeof callback == "function")
207 callback(response);
208 }, 152 },
209 153
210 /** 154 /**
211 * Stores the nodes associated with the requests and generates a unique ID 155 * Stores the nodes associated with the requests and generates a unique ID
212 * for them that can be used with Policy.refilterNodes(). Note that 156 * for them that can be used with Policy.refilterNodes(). Note that
213 * Policy.deleteNodes() always has to be called to release the memory. 157 * Policy.deleteNodes() always has to be called to release the memory.
214 * @param {number[]} requests list of request IDs that were previously 158 * @param {number[]} requests list of request IDs that were previously
215 * reported by this notifier. 159 * reported by this notifier.
216 * @param {Function} callback function to be called with the nodes ID. 160 * @param {Function} callback function to be called with the nodes ID.
217 */ 161 */
218 storeNodesForEntries: function(requests, callback) 162 storeNodesForEntries: function(requests, callback)
219 { 163 {
220 if (!requests) 164 if (!requests)
221 requests = []; 165 requests = [];
222 166
223 let id = ++this._maxResponseID; 167 port.emitWithResponse("storeNodesForEntries", {
224 this._callbacks.set(id, callback);
225
226 messageManager.broadcastAsyncMessage("AdblockPlus:StoreNodesForEntries", {
227 notifierID: this.id, 168 notifierID: this.id,
228 responseID: id, 169 requests
229 requests, 170 }).then(callback);
230 });
231 } 171 }
232 }; 172 };
233 173
234 /** 174 /**
235 * Associates a piece of data with a particular window. 175 * Associates a piece of data with a particular window.
236 * @param {number} outerWindowID the ID of the window 176 * @param {number} outerWindowID the ID of the window
237 * @static 177 * @static
238 */ 178 */
239 RequestNotifier.storeWindowData = function(outerWindowID, data) 179 RequestNotifier.storeWindowData = function(outerWindowID, data)
240 { 180 {
241 messageManager.broadcastAsyncMessage("AdblockPlus:StoreWindowData", { 181 port.emit("storeWindowData", {
242 outerWindowID, 182 outerWindowID,
243 data 183 data
244 }); 184 });
245 }; 185 };
246 186
247 /** 187 /**
248 * Retrieves a piece of data previously associated with the window by calling 188 * Retrieves a piece of data previously associated with the window by calling
249 * storeWindowData. 189 * storeWindowData.
250 * @param {number} outerWindowID the ID of the window 190 * @param {number} outerWindowID the ID of the window
251 * @param {Function} callback function to be called with the data. 191 * @param {Function} callback function to be called with the data.
252 * @static 192 * @static
253 */ 193 */
254 RequestNotifier.retrieveWindowData = function(outerWindowID, callback) 194 RequestNotifier.retrieveWindowData = function(outerWindowID, callback)
255 { 195 {
256 let id = ++windowDataMaxResponseID; 196 port.emitWithResponse("retrieveWindowData", outerWindowID).then(callback);
257 windowDataCallbacks.set(id, callback);
258
259 messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveWindowData", {
260 outerWindowID,
261 responseID: id
262 });
263 }; 197 };
264 198
265 /** 199 /**
266 * Retrieves the statistics for a window. 200 * Retrieves the statistics for a window.
267 * @param {number} outerWindowID the ID of the window 201 * @param {number} outerWindowID the ID of the window
268 * @param {Function} callback the callback to be called with the resulting 202 * @param {Function} callback the callback to be called with the resulting
269 * object (object properties will be items, blocked, 203 * object (object properties will be items, blocked,
270 * whitelisted, hidden, filters) or null. 204 * whitelisted, hidden, filters) or null.
271 */ 205 */
272 RequestNotifier.getWindowStatistics = function(outerWindowID, callback) 206 RequestNotifier.getWindowStatistics = function(outerWindowID, callback)
273 { 207 {
274 let id = ++windowStatsMaxResponseID; 208 port.emitWithResponse("retrieveWindowStats", outerWindowID).then(callback);
275 windowStatsCallbacks.set(id, callback); 209 };
276
277 messageManager.broadcastAsyncMessage("AdblockPlus:RetrieveWindowStats", {
278 responseID: id,
279 outerWindowID
280 });
281 }
OLDNEW
« no previous file with comments | « lib/child/requestNotifier.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld