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

Side by Side Diff: lib/requestNotifier.js

Issue 29329654: Issue 3222 - Split up requestNotifier module into a parent and child part (Closed)
Patch Set: Created Nov. 2, 2015, 7:48 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-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 21
22 let {Utils} = require("utils");
23
24 let nodeData = new WeakMap();
25 let windowStats = new WeakMap();
26 let windowSelection = new WeakMap(); 22 let windowSelection = new WeakMap();
27 let requestNotifierMaxId = 0; 23 let requestNotifierMaxId = 0;
28 let requestEntryMaxId = 0;
29 24
30 /** 25 /**
31 * Active RequestNotifier instances by their ID 26 * Active RequestNotifier instances by their ID
32 * @type Map 27 * @type Map
33 */ 28 */
34 let notifiers = new Map(); 29 let notifiers = new Map();
35 30
31 let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"]
32 .getService(Ci.nsIMessageListenerManager)
33 .QueryInterface(Ci.nsIMessageBroadcaster);
34 messageManager.addMessageListener("AdblockPlus:FoundNodeData", onNodeData);
35 messageManager.addMessageListener("AdblockPlus:ScanComplete", onScanComplete);
36
37 onShutdown.add(() => {
38 messageManager.removeMessageListener("AdblockPlus:FoundNodeData", onNodeData);
39 messageManager.removeMessageListener("AdblockPlus:ScanComplete", onScanComplet e);
40 });
41
42 function onNodeData(message)
43 {
44 let {notifierID, data} = message.data;
45 let notifier = notifiers.get(notifierID);
46 if (notifier)
47 notifier.notifyListener(data);
48 }
49
50 function onScanComplete(message)
51 {
52 let notifier = notifiers.get(message.data);
53 if (notifier)
54 notifier.onComplete();
55 }
56
36 /** 57 /**
37 * Creates a notifier object for a particular window. After creation the window 58 * Creates a notifier object for a particular window. After creation the window
38 * will first be scanned for previously saved requests. Once that scan is 59 * will first be scanned for previously saved requests. Once that scan is
39 * complete only new requests for this window will be reported. 60 * complete only new requests for this window will be reported.
40 * @param {Window} wnd window to attach the notifier to 61 * @param {Integer} outerWindowID ID of the window to attach the notifier to
41 * @param {Function} listener listener to be called whenever a new request is f ound 62 * @param {Function} listener listener to be called whenever a new request is f ound
42 * @param {Object} [listenerObj] "this" pointer to be used when calling the lis tener 63 * @param {Object} [listenerObj] "this" pointer to be used when calling the lis tener
43 */ 64 */
44 function RequestNotifier(wnd, listener, listenerObj) 65 function RequestNotifier(outerWindowID, listener, listenerObj)
45 { 66 {
46 this.window = wnd; 67 this.outerWindowID = outerWindowID;
47 this.listener = listener; 68 this.listener = listener;
48 this.listenerObj = listenerObj || null; 69 this.listenerObj = listenerObj || null;
49 this.id = ++requestNotifierMaxId; 70 this.id = ++requestNotifierMaxId;
50 notifiers.set(this.id, this); 71 notifiers.set(this.id, this);
51 if (wnd) 72
52 this.startScan(wnd); 73 messageManager.broadcastAsyncMessage("AdblockPlus:StartWindowScan", {
53 else 74 notifierID: this.id,
54 this.scanComplete = true; 75 outerWindowID: this.outerWindowID
76 });
55 } 77 }
56 exports.RequestNotifier = RequestNotifier; 78 exports.RequestNotifier = RequestNotifier;
57 79
58 RequestNotifier.prototype = 80 RequestNotifier.prototype =
59 { 81 {
60 /** 82 /**
61 * The unique ID of this notifier. 83 * The unique ID of this notifier.
62 * @type Integer 84 * @type Integer
63 */ 85 */
64 id: null, 86 id: null,
65 87
66 /** 88 /**
67 * The window this notifier is associated with. 89 * The ID of the window this notifier is associated with.
68 * @type Window 90 * @type Integer
69 */ 91 */
70 window: null, 92 outerWindowID: null,
71 93
72 /** 94 /**
73 * The listener to be called when a new request is found. 95 * The listener to be called when a new request is found.
74 * @type Function 96 * @type Function
75 */ 97 */
76 listener: null, 98 listener: null,
77 99
78 /** 100 /**
79 * "this" pointer to be used when calling the listener. 101 * "this" pointer to be used when calling the listener.
80 * @type Object 102 * @type Object
81 */ 103 */
82 listenerObj: null, 104 listenerObj: null,
83 105
84 /** 106 /**
85 * Will be set to true once the initial window scan is complete. 107 * Will be set to true once the initial window scan is complete.
86 * @type Boolean 108 * @type Boolean
87 */ 109 */
88 scanComplete: false, 110 scanComplete: false,
89 111
90 /** 112 /**
91 * Shuts down the notifier once it is no longer used. The listener 113 * Shuts down the notifier once it is no longer used. The listener
92 * will no longer be called after that. 114 * will no longer be called after that.
93 */ 115 */
94 shutdown: function() 116 shutdown: function()
95 { 117 {
96 delete this.window;
97 delete this.listener;
98 delete this.listenerObj;
99
100 notifiers.delete(this.id); 118 notifiers.delete(this.id);
119 messageManager.broadcastAsyncMessage("AdblockPlus:ShutdownNotifier", this.id );
101 }, 120 },
102 121
103 /** 122 /**
104 * Notifies listener about a new request. 123 * Notifies listener about a new request.
105 * @param {Window} wnd
106 * @param {Node} node
107 * @param {Object} entry 124 * @param {Object} entry
108 */ 125 */
109 notifyListener: function(wnd, node, entry) 126 notifyListener: function(entry)
110 { 127 {
111 this.listener.call(this.listenerObj, wnd, node, entry, this.scanComplete); 128 this.listener.call(this.listenerObj, entry, this.scanComplete);
112 }, 129 },
113 130
114 /** 131 onComplete: function()
115 * Number of currently posted scan events (will be 0 when the scan finishes
116 * running).
117 */
118 eventsPosted: 0,
119
120 /**
121 * Starts the initial scan of the window (will recurse into frames).
122 * @param {Window} wnd the window to be scanned
123 */
124 startScan: function(wnd)
125 { 132 {
126 let doc = wnd.document; 133 this.scanComplete = true;
127 let walker = doc.createTreeWalker(doc, Ci.nsIDOMNodeFilter.SHOW_ELEMENT, nul l, false); 134 this.notifyListener(null);
128
129 let process = function()
130 {
131 if (!this.listener)
132 return;
133
134 let node = walker.currentNode;
135 let data = nodeData.get(node);
136 if (typeof data != "undefined")
137 for (let k in data)
138 this.notifyListener(wnd, node, data[k]);
139
140 if (walker.nextNode())
141 Utils.runAsync(process);
142 else
143 {
144 // Done with the current window, start the scan for its frames
145 for (let i = 0; i < wnd.frames.length; i++)
146 this.startScan(wnd.frames[i]);
147
148 this.eventsPosted--;
149 if (!this.eventsPosted)
150 {
151 this.scanComplete = true;
152 this.notifyListener(wnd, null, null);
153 }
154 }
155 }.bind(this);
156
157 // Process each node in a separate event to allow other events to process
158 this.eventsPosted++;
159 Utils.runAsync(process);
160 } 135 }
161 }; 136 };
162 137
163 RequestNotifier.storeSelection = function(/**Window*/ wnd, /**String*/ selection ) 138 RequestNotifier.storeSelection = function(/**Window*/ wnd, /**String*/ selection )
164 { 139 {
165 windowSelection.set(wnd.document, selection); 140 windowSelection.set(wnd.document, selection);
166 }; 141 };
167 RequestNotifier.getSelection = function(/**Window*/ wnd) /**String*/ 142 RequestNotifier.getSelection = function(/**Window*/ wnd) /**String*/
168 { 143 {
169 if (windowSelection.has(wnd.document)) 144 if (windowSelection.has(wnd.document))
170 return windowSelection.get(wnd.document); 145 return windowSelection.get(wnd.document);
171 else 146 else
172 return null; 147 return null;
173 }; 148 };
174 149
175 /** 150 /**
176 * Attaches request data to a DOM node.
177 * @param {Node} node node to attach data to
178 * @param {Window} topWnd top-level window the node belongs to
179 * @param {String} contentType request type, e.g. "IMAGE"
180 * @param {String} docDomain domain of the document that initiated the request
181 * @param {Boolean} thirdParty will be true if a third-party server has been re quested
182 * @param {String} location the address that has been requested
183 * @param {Filter} filter filter applied to the request or null if none
184 */
185 RequestNotifier.addNodeData = function(/**Node*/ node, /**Window*/ topWnd, /**St ring*/ contentType, /**String*/ docDomain, /**Boolean*/ thirdParty, /**String*/ location, /**Filter*/ filter)
186 {
187 let entry = {
188 id: ++requestEntryMaxId,
189 type: contentType,
190 docDomain, thirdParty, location, filter
191 }
192
193 let existingData = nodeData.get(node);
194 if (typeof existingData == "undefined")
195 {
196 existingData = {};
197 nodeData.set(node, existingData);
198 }
199
200 // Add this request to the node data
201 existingData[contentType + " " + location] = entry;
202
203 // Update window statistics
204 if (!windowStats.has(topWnd.document))
205 {
206 windowStats.set(topWnd.document, {
207 items: 0,
208 hidden: 0,
209 blocked: 0,
210 whitelisted: 0,
211 filters: {}
212 });
213 }
214
215 let stats = windowStats.get(topWnd.document);
216 let filterType = (filter ? filter.type : null);
217 if (filterType != "elemhide" && filterType != "elemhideexception" && filterTyp e != "cssproperty")
218 stats.items++;
219 if (filter)
220 {
221 if (filterType == "blocking")
222 stats.blocked++;
223 else if (filterType == "whitelist" || filterType == "elemhideexception")
224 stats.whitelisted++;
225 else if (filterType == "elemhide" || filterType == "cssproperty")
226 stats.hidden++;
227
228 if (filter.text in stats.filters)
229 stats.filters[filter.text]++;
230 else
231 stats.filters[filter.text] = 1;
232 }
233
234 // Notify listeners
235 for (let notifier of notifiers.values())
236 if (!notifier.window || notifier.window == topWnd)
237 notifier.notifyListener(topWnd, node, entry);
238 }
239
240 /**
241 * Retrieves the statistics for a window. 151 * Retrieves the statistics for a window.
242 * @result {Object} Object with the properties items, blocked, whitelisted, hidd en, filters containing statistics for the window (might be null) 152 * @result {Object} Object with the properties items, blocked, whitelisted, hidd en, filters containing statistics for the window (might be null)
243 */ 153 */
244 RequestNotifier.getWindowStatistics = function(/**Window*/ wnd) 154 RequestNotifier.getWindowStatistics = function(/**Window*/ wnd)
245 { 155 {
246 if (windowStats.has(wnd.document)) 156 if (windowStats.has(wnd.document))
247 return windowStats.get(wnd.document); 157 return windowStats.get(wnd.document);
248 else 158 else
249 return null; 159 return null;
250 } 160 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 node = node.parentNode; 197 node = node.parentNode;
288 } 198 }
289 else 199 else
290 { 200 {
291 node = null; 201 node = null;
292 } 202 }
293 } 203 }
294 204
295 return null; 205 return null;
296 }; 206 };
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