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: Improved comments Created Nov. 12, 2015, 12:40 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;
47 this.listener = listener; 67 this.listener = listener;
48 this.listenerObj = listenerObj || null; 68 this.listenerObj = listenerObj || null;
49 this.id = ++requestNotifierMaxId; 69 this.id = ++requestNotifierMaxId;
50 notifiers.set(this.id, this); 70 notifiers.set(this.id, this);
51 if (wnd) 71
52 this.startScan(wnd); 72 messageManager.broadcastAsyncMessage("AdblockPlus:StartWindowScan", {
53 else 73 notifierID: this.id,
54 this.scanComplete = true; 74 outerWindowID: outerWindowID
75 });
55 } 76 }
56 exports.RequestNotifier = RequestNotifier; 77 exports.RequestNotifier = RequestNotifier;
57 78
58 RequestNotifier.prototype = 79 RequestNotifier.prototype =
59 { 80 {
60 /** 81 /**
61 * The unique ID of this notifier. 82 * The unique ID of this notifier.
62 * @type Integer 83 * @type Integer
63 */ 84 */
64 id: null, 85 id: null,
65 86
66 /** 87 /**
67 * The window this notifier is associated with.
68 * @type Window
69 */
70 window: null,
71
72 /**
73 * The listener to be called when a new request is found. 88 * The listener to be called when a new request is found.
74 * @type Function 89 * @type Function
75 */ 90 */
76 listener: null, 91 listener: null,
77 92
78 /** 93 /**
79 * "this" pointer to be used when calling the listener. 94 * "this" pointer to be used when calling the listener.
80 * @type Object 95 * @type Object
81 */ 96 */
82 listenerObj: null, 97 listenerObj: null,
83 98
84 /** 99 /**
85 * Will be set to true once the initial window scan is complete. 100 * Will be set to true once the initial window scan is complete.
86 * @type Boolean 101 * @type Boolean
87 */ 102 */
88 scanComplete: false, 103 scanComplete: false,
89 104
90 /** 105 /**
91 * Shuts down the notifier once it is no longer used. The listener 106 * Shuts down the notifier once it is no longer used. The listener
92 * will no longer be called after that. 107 * will no longer be called after that.
93 */ 108 */
94 shutdown: function() 109 shutdown: function()
95 { 110 {
96 delete this.window;
97 delete this.listener;
98 delete this.listenerObj;
99
100 notifiers.delete(this.id); 111 notifiers.delete(this.id);
112 messageManager.broadcastAsyncMessage("AdblockPlus:ShutdownNotifier", this.id );
101 }, 113 },
102 114
103 /** 115 /**
104 * Notifies listener about a new request. 116 * Notifies listener about a new request.
105 * @param {Window} wnd
106 * @param {Node} node
107 * @param {Object} entry 117 * @param {Object} entry
108 */ 118 */
109 notifyListener: function(wnd, node, entry) 119 notifyListener: function(entry)
110 { 120 {
111 this.listener.call(this.listenerObj, wnd, node, entry, this.scanComplete); 121 this.listener.call(this.listenerObj, entry, this.scanComplete);
112 }, 122 },
113 123
114 /** 124 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 { 125 {
126 let doc = wnd.document; 126 this.scanComplete = true;
127 let walker = doc.createTreeWalker(doc, Ci.nsIDOMNodeFilter.SHOW_ELEMENT, nul l, false); 127 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 } 128 }
161 }; 129 };
162 130
163 RequestNotifier.storeSelection = function(/**Window*/ wnd, /**String*/ selection ) 131 RequestNotifier.storeSelection = function(/**Window*/ wnd, /**String*/ selection )
164 { 132 {
165 windowSelection.set(wnd.document, selection); 133 windowSelection.set(wnd.document, selection);
166 }; 134 };
167 RequestNotifier.getSelection = function(/**Window*/ wnd) /**String*/ 135 RequestNotifier.getSelection = function(/**Window*/ wnd) /**String*/
168 { 136 {
169 if (windowSelection.has(wnd.document)) 137 if (windowSelection.has(wnd.document))
170 return windowSelection.get(wnd.document); 138 return windowSelection.get(wnd.document);
171 else 139 else
172 return null; 140 return null;
173 }; 141 };
174 142
175 /** 143 /**
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. 144 * 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) 145 * @result {Object} Object with the properties items, blocked, whitelisted, hidd en, filters containing statistics for the window (might be null)
243 */ 146 */
244 RequestNotifier.getWindowStatistics = function(/**Window*/ wnd) 147 RequestNotifier.getWindowStatistics = function(/**Window*/ wnd)
245 { 148 {
246 if (windowStats.has(wnd.document)) 149 if (windowStats.has(wnd.document))
247 return windowStats.get(wnd.document); 150 return windowStats.get(wnd.document);
248 else 151 else
249 return null; 152 return null;
250 } 153 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 node = node.parentNode; 190 node = node.parentNode;
288 } 191 }
289 else 192 else
290 { 193 {
291 node = null; 194 node = null;
292 } 195 }
293 } 196 }
294 197
295 return null; 198 return null;
296 }; 199 };
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