Left: | ||
Right: |
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-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 |
(...skipping 19 matching lines...) Expand all Loading... | |
30 delete proto.Components; | 30 delete proto.Components; |
31 } | 31 } |
32 catch (e) | 32 catch (e) |
33 { | 33 { |
34 Cu.reportError(e); | 34 Cu.reportError(e); |
35 } | 35 } |
36 | 36 |
37 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); | 37 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
38 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | 38 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
39 | 39 |
40 let {port} = require("messaging"); | |
40 let {Utils} = require("utils"); | 41 let {Utils} = require("utils"); |
41 let {getFrames, isPrivate} = require("child/utils"); | 42 let {getFrames, isPrivate} = require("child/utils"); |
42 let {objectMouseEventHander} = require("child/objectTabs"); | 43 let {objectMouseEventHander} = require("child/objectTabs"); |
43 let {RequestNotifier} = require("child/requestNotifier"); | 44 let {RequestNotifier} = require("child/requestNotifier"); |
44 | 45 |
45 /** | 46 /** |
46 * Randomly generated class name, to be applied to collapsed nodes. | 47 * Randomly generated class name, to be applied to collapsed nodes. |
47 * @type string | 48 * @type string |
48 */ | 49 */ |
49 let collapsedClass = null; | 50 let collapsedClass = null; |
(...skipping 16 matching lines...) Expand all Loading... | |
66 * @type string | 67 * @type string |
67 */ | 68 */ |
68 let nodesIDPrefix = Services.appinfo.processID + " "; | 69 let nodesIDPrefix = Services.appinfo.processID + " "; |
69 | 70 |
70 /** | 71 /** |
71 * Counter used to generate unique nodes identifiers in storeNodes(). | 72 * Counter used to generate unique nodes identifiers in storeNodes(). |
72 * @type number | 73 * @type number |
73 */ | 74 */ |
74 let maxNodesID = 0; | 75 let maxNodesID = 0; |
75 | 76 |
76 addMessageListener("AdblockPlus:DeleteNodes", onDeleteNodes); | 77 port.on("deleteNodes", onDeleteNodes); |
77 addMessageListener("AdblockPlus:RefilterNodes", onRefilterNodes); | 78 port.on("refilterNodes", onRefilterNodes); |
78 | |
79 onShutdown.add(() => { | |
80 removeMessageListener("AdblockPlus:DeleteNodes", onDeleteNodes); | |
81 removeMessageListener("AdblockPlus:RefilterNodes", onRefilterNodes); | |
82 }); | |
83 | 79 |
84 /** | 80 /** |
85 * Processes parent's response to the ShouldAllow message. | 81 * Processes parent's response to the ShouldAllow message. |
86 * @param {nsIDOMWindow} window window that the request is associated with | 82 * @param {nsIDOMWindow} window window that the request is associated with |
87 * @param {nsIDOMElement} node DOM element that the request is associated with | 83 * @param {nsIDOMElement} node DOM element that the request is associated with |
88 * @param {Object|undefined} response object received as response | 84 * @param {Object|undefined} response object received as response |
89 * @return {Boolean} false if the request should be blocked | 85 * @return {Boolean} false if the request should be blocked |
90 */ | 86 */ |
91 function processPolicyResponse(window, node, response) | 87 function processPolicyResponse(window, node, response) |
92 { | 88 { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 /** | 125 /** |
130 * Checks whether a request should be allowed, hides it if necessary | 126 * Checks whether a request should be allowed, hides it if necessary |
131 * @param {nsIDOMWindow} window | 127 * @param {nsIDOMWindow} window |
132 * @param {nsIDOMElement} node | 128 * @param {nsIDOMElement} node |
133 * @param {String} contentType | 129 * @param {String} contentType |
134 * @param {String} location location of the request, filter key if contentType i s ELEMHIDE | 130 * @param {String} location location of the request, filter key if contentType i s ELEMHIDE |
135 * @return {Boolean} false if the request should be blocked | 131 * @return {Boolean} false if the request should be blocked |
136 */ | 132 */ |
137 let shouldAllow = exports.shouldAllow = function(window, node, contentType, loca tion) | 133 let shouldAllow = exports.shouldAllow = function(window, node, contentType, loca tion) |
138 { | 134 { |
139 return processPolicyResponse(window, node, sendSyncMessage("AdblockPlus:Should Allow", { | 135 return processPolicyResponse(window, node, port.emitSync("shouldAllow", { |
140 contentType, | 136 contentType, |
141 location, | 137 location, |
142 frames: getFrames(window), | 138 frames: getFrames(window), |
143 isPrivate: isPrivate(window) | 139 isPrivate: isPrivate(window) |
144 })); | 140 })); |
145 }; | 141 }; |
146 | 142 |
147 /** | 143 /** |
148 * Asynchronously checks whether a request should be allowed. | 144 * Asynchronously checks whether a request should be allowed. |
149 * @param {nsIDOMWindow} window | 145 * @param {nsIDOMWindow} window |
150 * @param {nsIDOMElement} node | 146 * @param {nsIDOMElement} node |
151 * @param {String} contentType | 147 * @param {String} contentType |
152 * @param {String} location location of the request, filter key if contentType i s ELEMHIDE | 148 * @param {String} location location of the request, filter key if contentType i s ELEMHIDE |
153 * @param {Function} callback callback to be called with a boolean value, if | 149 * @param {Function} callback callback to be called with a boolean value, if |
154 * false the request should be blocked | 150 * false the request should be blocked |
155 */ | 151 */ |
156 let shouldAllowAsync = exports.shouldAllowAsync = function(window, node, content Type, location, callback) | 152 let shouldAllowAsync = exports.shouldAllowAsync = function(window, node, content Type, location, callback) |
157 { | 153 { |
158 sendAsyncMessage("AdblockPlus:ShouldAllow", { | 154 port.emitWithResponse("shouldAllow", { |
159 contentType, | 155 contentType, |
160 location, | 156 location, |
161 frames: getFrames(window), | 157 frames: getFrames(window), |
162 isPrivate: isPrivate(window) | 158 isPrivate: isPrivate(window) |
163 }, response => callback(processPolicyResponse(window, node, response))); | 159 }).then(response => |
160 { | |
161 callback(processPolicyResponse(window, node, response)); | |
162 }); | |
164 }; | 163 }; |
165 | 164 |
166 /** | 165 /** |
167 * Stores nodes and generates a unique ID for them that can be used for | 166 * Stores nodes and generates a unique ID for them that can be used for |
168 * Policy.refilterNodes() later. It's important that Policy.deleteNodes() is | 167 * Policy.refilterNodes() later. It's important that Policy.deleteNodes() is |
169 * called later, otherwise the nodes will be leaked. | 168 * called later, otherwise the nodes will be leaked. |
170 * @param {DOMNode[]} nodes list of nodes to be stored | 169 * @param {DOMNode[]} nodes list of nodes to be stored |
171 * @return {string} unique ID for the nodes | 170 * @return {string} unique ID for the nodes |
172 */ | 171 */ |
173 let storeNodes = exports.storeNodes = function(nodes) | 172 let storeNodes = exports.storeNodes = function(nodes) |
174 { | 173 { |
175 let id = nodesIDPrefix + (++maxNodesID); | 174 let id = nodesIDPrefix + (++maxNodesID); |
176 storedNodes.set(id, nodes); | 175 storedNodes.set(id, nodes); |
177 return id; | 176 return id; |
178 }; | 177 }; |
179 | 178 |
180 /** | 179 /** |
181 * Called via message whenever Policy.deleteNodes() is called in the parent. | 180 * Called via message whenever Policy.deleteNodes() is called in the parent. |
182 */ | 181 */ |
183 function onDeleteNodes(message) | 182 function onDeleteNodes(id, sender) |
184 { | 183 { |
185 storedNodes.delete(message.data); | 184 storedNodes.delete(id); |
186 } | 185 } |
187 | 186 |
188 /** | 187 /** |
189 * Called via message whenever Policy.refilterNodes() is called in the parent. | 188 * Called via message whenever Policy.refilterNodes() is called in the parent. |
190 */ | 189 */ |
191 function onRefilterNodes(message) | 190 function onRefilterNodes({nodesID, entry}, sender) |
192 { | 191 { |
193 let {nodesID, entry} = message.data; | |
194 let nodes = storedNodes.get(nodesID); | 192 let nodes = storedNodes.get(nodesID); |
195 if (nodes) | 193 if (nodes) |
196 for (let node of nodes) | 194 for (let node of nodes) |
197 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) | 195 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) |
198 Utils.runAsync(refilterNode.bind(this, node, entry)); | 196 Utils.runAsync(refilterNode.bind(this, node, entry)); |
199 } | 197 } |
200 | 198 |
201 /** | 199 /** |
202 * Re-checks filters on an element. | 200 * Re-checks filters on an element. |
203 */ | 201 */ |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
452 Utils.runAsync(postProcessNodes); | 450 Utils.runAsync(postProcessNodes); |
453 } | 451 } |
454 } | 452 } |
455 | 453 |
456 /** | 454 /** |
457 * Processes nodes scheduled for post-processing (typically hides them). | 455 * Processes nodes scheduled for post-processing (typically hides them). |
458 */ | 456 */ |
459 function postProcessNodes() | 457 function postProcessNodes() |
460 { | 458 { |
461 if (!collapsedClass) | 459 if (!collapsedClass) |
462 collapsedClass = sendSyncMessage("AdblockPlus:GetCollapsedClass"); | 460 { |
461 port.emitWithResponse("getCollapsedClass").then(cls => | |
462 { | |
463 // We might have sent this message multiple times, ignore response if a | |
464 // previous response was already processed. | |
465 if (collapsedClass) | |
466 return; | |
467 | |
468 collapsedClass = cls; | |
469 postProcessNodes(); | |
470 }); | |
471 return; | |
472 } | |
463 | 473 |
464 let nodes = scheduledNodes; | 474 let nodes = scheduledNodes; |
465 scheduledNodes = null; | 475 scheduledNodes = null; |
Erik
2016/03/15 20:57:41
It appears that `postProcessNodes` is run twice no
Wladimir Palant
2016/03/16 10:13:00
No, we don't run this code multiple times (return
| |
466 | 476 |
467 if (!collapsedClass) | |
468 return; | |
469 | |
470 for (let node of nodes) | 477 for (let node of nodes) |
471 { | 478 { |
472 // adjust frameset's cols/rows for frames | 479 // adjust frameset's cols/rows for frames |
473 let parentNode = node.parentNode; | 480 let parentNode = node.parentNode; |
474 if (parentNode && parentNode instanceof Ci.nsIDOMHTMLFrameSetElement) | 481 if (parentNode && parentNode instanceof Ci.nsIDOMHTMLFrameSetElement) |
475 { | 482 { |
476 let hasCols = (parentNode.cols && parentNode.cols.indexOf(",") > 0); | 483 let hasCols = (parentNode.cols && parentNode.cols.indexOf(",") > 0); |
477 let hasRows = (parentNode.rows && parentNode.rows.indexOf(",") > 0); | 484 let hasRows = (parentNode.rows && parentNode.rows.indexOf(",") > 0); |
478 if ((hasCols || hasRows) && !(hasCols && hasRows)) | 485 if ((hasCols || hasRows) && !(hasCols && hasRows)) |
479 { | 486 { |
480 let index = -1; | 487 let index = -1; |
481 for (let frame = node; frame; frame = frame.previousSibling) | 488 for (let frame = node; frame; frame = frame.previousSibling) |
482 if (frame instanceof Ci.nsIDOMHTMLFrameElement || frame instanceof Ci. nsIDOMHTMLFrameSetElement) | 489 if (frame instanceof Ci.nsIDOMHTMLFrameElement || frame instanceof Ci. nsIDOMHTMLFrameSetElement) |
483 index++; | 490 index++; |
484 | 491 |
485 let property = (hasCols ? "cols" : "rows"); | 492 let property = (hasCols ? "cols" : "rows"); |
486 let weights = parentNode[property].split(","); | 493 let weights = parentNode[property].split(","); |
487 weights[index] = "0"; | 494 weights[index] = "0"; |
488 parentNode[property] = weights.join(","); | 495 parentNode[property] = weights.join(","); |
489 } | 496 } |
490 } | 497 } |
491 else | 498 else |
492 node.classList.add(collapsedClass); | 499 node.classList.add(collapsedClass); |
493 } | 500 } |
494 } | 501 } |
OLD | NEW |