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

Delta Between Two Patch Sets: lib/child/contentPolicy.js

Issue 29329754: Issue 3251 - Delegate processing of element hiding hits to shouldAllowAsync() so that hits show up (Closed)
Left Patch Set: Reversed logic and improved JSDoc comments Created Nov. 6, 2015, 11:26 a.m.
Right Patch Set: Rebased Created Nov. 12, 2015, 3:04 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | lib/child/elemHide.js » ('j') | lib/child/elemHide.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 */ 48 */
49 let collapsedClass = null; 49 let collapsedClass = null;
50 50
51 /** 51 /**
52 * Maps numerical content type IDs to strings. 52 * Maps numerical content type IDs to strings.
53 * @type Map.<number,string> 53 * @type Map.<number,string>
54 */ 54 */
55 let types = new Map(); 55 let types = new Map();
56 56
57 /** 57 /**
58 * Processes parent's response to the ShouldAllow message.
59 * @param {nsIDOMWindow} window window that the request is associated with
60 * @param {nsIDOMElement} node DOM element that the request is associated with
61 * @param {Object|undefined} response object received as response
62 * @return {Boolean} false if the request should be blocked
63 */
64 function processPolicyResponse(window, node, response)
65 {
66 if (typeof response == "undefined")
67 return true;
68
69 let {allow, collapse, hits} = response;
70 let isObject = false;
71 for (let {frameIndex, contentType, docDomain, thirdParty, location, filter} of hits)
72 {
73 if (contentType == "OBJECT")
74 isObject = true;
75
76 let context = node;
77 if (typeof frameIndex == "number")
78 {
79 context = window;
80 for (let i = 0; i < frameIndex; i++)
81 context = context.parent;
82 context = context.document;
83 }
84 RequestNotifier.addNodeData(context, window.top, contentType, docDomain, thi rdParty, location, filter);
85 }
86
87 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE)
88 {
89 // Track mouse events for objects
90 if (allow && isObject)
91 {
92 node.addEventListener("mouseover", objectMouseEventHander, true);
93 node.addEventListener("mouseout", objectMouseEventHander, true);
94 }
95
96 if (collapse)
97 schedulePostProcess(node);
98 }
99 return allow;
100 }
101
102 /**
58 * Checks whether a request should be allowed, hides it if necessary 103 * Checks whether a request should be allowed, hides it if necessary
59 * @param wnd {nsIDOMWindow} 104 * @param {nsIDOMWindow} window
60 * @param node {nsIDOMElement} 105 * @param {nsIDOMElement} node
61 * @param contentType {String} 106 * @param {String} contentType
62 * @param location {String} location of the request, filter key if contentType i s ELEMHIDE 107 * @param {String} location location of the request, filter key if contentType i s ELEMHIDE
63 * @param [callback] {Function} If present, the request will be sent
64 * asynchronously and callback called with the
65 * response
66 * @return {Boolean} false if the request should be blocked 108 * @return {Boolean} false if the request should be blocked
67 */ 109 */
68 let shouldAllow = exports.shouldAllow = function(window, node, contentType, loca tion, callback) 110 let shouldAllow = exports.shouldAllow = function(window, node, contentType, loca tion)
69 { 111 {
70 function processResponse(response) 112 return processPolicyResponse(window, node, sendSyncMessage("AdblockPlus:Should Allow", {
71 { 113 contentType,
72 if (typeof response == "undefined") 114 location,
73 return true;
74
75 let {allow, collapse, hits} = response;
76 for (let {frameIndex, contentType, docDomain, thirdParty, location, filter} of hits)
77 {
78 let context = node;
79 if (typeof frameIndex == "number")
80 {
81 context = window;
82 for (let i = 0; i < frameIndex; i++)
83 context = context.parent;
84 context = context.document;
85 }
86 RequestNotifier.addNodeData(context, window.top, contentType, docDomain, t hirdParty, location, filter);
87 }
88
89 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE)
90 {
91 // Track mouse events for objects
92 if (allow && contentType == "OBJECT")
93 {
94 node.addEventListener("mouseover", objectMouseEventHander, true);
95 node.addEventListener("mouseout", objectMouseEventHander, true);
96 }
97
98 if (collapse)
99 schedulePostProcess(node);
100 }
101 return allow;
102 }
103
104 let data = {
105 contentType: contentType,
106 location: location,
107 frames: getFrames(window), 115 frames: getFrames(window),
108 isPrivate: isPrivate(window) 116 isPrivate: isPrivate(window)
109 }; 117 }));
110 if (typeof callback == "function") 118 };
111 { 119
112 sendAsyncMessage("AdblockPlus:ShouldAllow", data, (data) => { 120 /**
113 callback(processResponse(data)); 121 * Asynchronously checks whether a request should be allowed.
114 }); 122 * @param {nsIDOMWindow} window
115 } 123 * @param {nsIDOMElement} node
116 else 124 * @param {String} contentType
117 return processResponse(sendSyncMessage("AdblockPlus:ShouldAllow", data)); 125 * @param {String} location location of the request, filter key if contentType i s ELEMHIDE
126 * @param {Function} callback callback to be called with a boolean value, if
127 * false the request should be blocked
128 */
129 let shouldAllowAsync = exports.shouldAllowAsync = function(window, node, content Type, location, callback)
130 {
131 sendAsyncMessage("AdblockPlus:ShouldAllow", {
132 contentType,
133 location,
134 frames: getFrames(window),
135 isPrivate: isPrivate(window)
136 }, response => callback(processPolicyResponse(window, node, response)));
118 }; 137 };
119 138
120 /** 139 /**
121 * Actual nsIContentPolicy and nsIChannelEventSink implementation 140 * Actual nsIContentPolicy and nsIChannelEventSink implementation
122 * @class 141 * @class
123 */ 142 */
124 var PolicyImplementation = 143 var PolicyImplementation =
125 { 144 {
126 classDescription: "Adblock Plus content policy", 145 classDescription: "Adblock Plus content policy",
127 classID: Components.ID("cfeaabe6-1dd1-11b2-a0c6-cb5c268894c9"), 146 classID: Components.ID("cfeaabe6-1dd1-11b2-a0c6-cb5c268894c9"),
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 // Special treatment for pop-up windows - this will close the window 289 // Special treatment for pop-up windows - this will close the window
271 // rather than preventing the redirect. Note that we might not have 290 // rather than preventing the redirect. Note that we might not have
272 // seen the original channel yet because the redirect happened before 291 // seen the original channel yet because the redirect happened before
273 // the async code in observe() had a chance to run. 292 // the async code in observe() had a chance to run.
274 this.observe(wnd, "content-document-global-created", null, oldChannel. URI.spec); 293 this.observe(wnd, "content-document-global-created", null, oldChannel. URI.spec);
275 this.observe(wnd, "content-document-global-created", null, newChannel. URI.spec); 294 this.observe(wnd, "content-document-global-created", null, newChannel. URI.spec);
276 } 295 }
277 return; 296 return;
278 } 297 }
279 298
280 shouldAllow(wnd, wnd.document, types.get(contentType), newChannel.URI.spec , function(allow) 299 shouldAllowAsync(wnd, wnd.document, types.get(contentType), newChannel.URI .spec, function(allow)
281 { 300 {
282 callback.onRedirectVerifyCallback(allow ? Cr.NS_OK : Cr.NS_BINDING_ABORT ED); 301 callback.onRedirectVerifyCallback(allow ? Cr.NS_OK : Cr.NS_BINDING_ABORT ED);
283 }); 302 });
284 async = true; 303 async = true;
285 } 304 }
286 catch (e) 305 catch (e)
287 { 306 {
288 // We shouldn't throw exceptions here - this will prevent the redirect. 307 // We shouldn't throw exceptions here - this will prevent the redirect.
289 Cu.reportError(e); 308 Cu.reportError(e);
290 } 309 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 Utils.runAsync(postProcessNodes); 346 Utils.runAsync(postProcessNodes);
328 } 347 }
329 } 348 }
330 349
331 /** 350 /**
332 * Processes nodes scheduled for post-processing (typically hides them). 351 * Processes nodes scheduled for post-processing (typically hides them).
333 */ 352 */
334 function postProcessNodes() 353 function postProcessNodes()
335 { 354 {
336 if (!collapsedClass) 355 if (!collapsedClass)
337 { 356 collapsedClass = sendSyncMessage("AdblockPlus:GetCollapsedClass");
338 let response = sendSyncMessage("AdblockPlus:GetCollapsedClass");
339 if (response.length)
340 collapsedClass = response[0];
341
342 if (!collapsedClass)
343 {
344 Utils.runAsync(postProcessNodes);
345 return;
346 }
347 }
348 357
349 let nodes = scheduledNodes; 358 let nodes = scheduledNodes;
350 scheduledNodes = null; 359 scheduledNodes = null;
360
361 if (!collapsedClass)
362 return;
351 363
352 for (let node of nodes) 364 for (let node of nodes)
353 { 365 {
354 // adjust frameset's cols/rows for frames 366 // adjust frameset's cols/rows for frames
355 let parentNode = node.parentNode; 367 let parentNode = node.parentNode;
356 if (parentNode && parentNode instanceof Ci.nsIDOMHTMLFrameSetElement) 368 if (parentNode && parentNode instanceof Ci.nsIDOMHTMLFrameSetElement)
357 { 369 {
358 let hasCols = (parentNode.cols && parentNode.cols.indexOf(",") > 0); 370 let hasCols = (parentNode.cols && parentNode.cols.indexOf(",") > 0);
359 let hasRows = (parentNode.rows && parentNode.rows.indexOf(",") > 0); 371 let hasRows = (parentNode.rows && parentNode.rows.indexOf(",") > 0);
360 if ((hasCols || hasRows) && !(hasCols && hasRows)) 372 if ((hasCols || hasRows) && !(hasCols && hasRows))
361 { 373 {
362 let index = -1; 374 let index = -1;
363 for (let frame = node; frame; frame = frame.previousSibling) 375 for (let frame = node; frame; frame = frame.previousSibling)
364 if (frame instanceof Ci.nsIDOMHTMLFrameElement || frame instanceof Ci. nsIDOMHTMLFrameSetElement) 376 if (frame instanceof Ci.nsIDOMHTMLFrameElement || frame instanceof Ci. nsIDOMHTMLFrameSetElement)
365 index++; 377 index++;
366 378
367 let property = (hasCols ? "cols" : "rows"); 379 let property = (hasCols ? "cols" : "rows");
368 let weights = parentNode[property].split(","); 380 let weights = parentNode[property].split(",");
369 weights[index] = "0"; 381 weights[index] = "0";
370 parentNode[property] = weights.join(","); 382 parentNode[property] = weights.join(",");
371 } 383 }
372 } 384 }
373 else 385 else
374 node.classList.add(collapsedClass); 386 node.classList.add(collapsedClass);
375 } 387 }
376 } 388 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld