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

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

Issue 29338279: Issue 3499 - Use new messaging API in the content policy implementation (Closed)
Patch Set: More obvious logic in postProcessNodes Created March 16, 2016, 10:10 a.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 | « no previous file | lib/contentPolicy.js » ('j') | 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
(...skipping 19 matching lines...) Expand all
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 Promise.<string>
48 */ 49 */
49 let collapsedClass = null; 50 let collapsedClass = port.emitWithResponse("getCollapsedClass");
50 51
51 /** 52 /**
52 * Maps numerical content type IDs to strings. 53 * Maps numerical content type IDs to strings.
53 * @type Map.<number,string> 54 * @type Map.<number,string>
54 */ 55 */
55 let types = new Map(); 56 let types = new Map();
56 57
57 /** 58 /**
58 * Contains nodes stored by storeNodes() mapped by their IDs. 59 * Contains nodes stored by storeNodes() mapped by their IDs.
59 * @type Map.<string,DOMNode[]> 60 * @type Map.<string,DOMNode[]>
60 */ 61 */
61 let storedNodes = new Map(); 62 let storedNodes = new Map();
62 63
63 /** 64 /**
64 * Process-dependent prefix to be used for unique nodes identifiers returned 65 * Process-dependent prefix to be used for unique nodes identifiers returned
65 * by storeNodes(). 66 * by storeNodes().
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
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 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 scheduledNodes = [node]; 449 scheduledNodes = [node];
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 collapsedClass.then(cls =>
Thomas Greiner 2016/03/23 18:47:59 Detail: Anything wrong with calling it "collapsedC
Wladimir Palant 2016/03/24 07:27:26 They are not the same thing however. I would consi
462 collapsedClass = sendSyncMessage("AdblockPlus:GetCollapsedClass"); 460 {
461 let nodes = scheduledNodes;
462 scheduledNodes = null;
463 463
464 let nodes = scheduledNodes; 464 // Resolving class is async initially so the nodes might have already been
465 scheduledNodes = null; 465 // processed in the meantime.
466 if (!nodes)
467 return;
466 468
467 if (!collapsedClass) 469 for (let node of nodes)
468 return; 470 {
471 // adjust frameset's cols/rows for frames
472 let parentNode = node.parentNode;
473 if (parentNode && parentNode instanceof Ci.nsIDOMHTMLFrameSetElement)
474 {
475 let hasCols = (parentNode.cols && parentNode.cols.indexOf(",") > 0);
476 let hasRows = (parentNode.rows && parentNode.rows.indexOf(",") > 0);
477 if ((hasCols || hasRows) && !(hasCols && hasRows))
478 {
479 let index = -1;
480 for (let frame = node; frame; frame = frame.previousSibling)
481 if (frame instanceof Ci.nsIDOMHTMLFrameElement || frame instanceof C i.nsIDOMHTMLFrameSetElement)
482 index++;
469 483
470 for (let node of nodes) 484 let property = (hasCols ? "cols" : "rows");
471 { 485 let weights = parentNode[property].split(",");
472 // adjust frameset's cols/rows for frames 486 weights[index] = "0";
473 let parentNode = node.parentNode; 487 parentNode[property] = weights.join(",");
474 if (parentNode && parentNode instanceof Ci.nsIDOMHTMLFrameSetElement) 488 }
475 {
476 let hasCols = (parentNode.cols && parentNode.cols.indexOf(",") > 0);
477 let hasRows = (parentNode.rows && parentNode.rows.indexOf(",") > 0);
478 if ((hasCols || hasRows) && !(hasCols && hasRows))
479 {
480 let index = -1;
481 for (let frame = node; frame; frame = frame.previousSibling)
482 if (frame instanceof Ci.nsIDOMHTMLFrameElement || frame instanceof Ci. nsIDOMHTMLFrameSetElement)
483 index++;
484
485 let property = (hasCols ? "cols" : "rows");
486 let weights = parentNode[property].split(",");
487 weights[index] = "0";
488 parentNode[property] = weights.join(",");
489 } 489 }
490 else
491 node.classList.add(cls);
490 } 492 }
491 else 493 });
492 node.classList.add(collapsedClass);
493 }
494 } 494 }
OLDNEW
« no previous file with comments | « no previous file | lib/contentPolicy.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld