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

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

Issue 29329751: Issue 3251 - Add shouldAllowAsync() function for non-urgent policy checks (Closed)
Patch Set: Added shouldAllowAsync() as a separate function and improved JSDoc comments Created Nov. 12, 2015, 2:58 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 | « no previous file | 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
(...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 * Checks whether a request should be allowed, hides it if necessary 58 * Processes parent's response to the ShouldAllow message.
59 * @param wnd {nsIDOMWindow} 59 * @param {nsIDOMWindow} window window that the request is associated with
60 * @param node {nsIDOMElement} 60 * @param {nsIDOMElement} node DOM element that the request is associated with
61 * @param contentType {String} 61 * @param {Object|undefined} response object received as response
62 * @param location {String}
63 * @return {Boolean} false if the request should be blocked 62 * @return {Boolean} false if the request should be blocked
64 */ 63 */
65 function shouldAllow(window, node, contentType, location) 64 function processPolicyResponse(window, node, response)
66 { 65 {
67 let response = sendSyncMessage("AdblockPlus:ShouldAllow", {
68 contentType: contentType,
69 location: location,
70 frames: getFrames(window),
71 isPrivate: isPrivate(window)
72 });
73 if (typeof response == "undefined") 66 if (typeof response == "undefined")
74 return true; 67 return true;
75 68
76 let {allow, collapse, hits} = response; 69 let {allow, collapse, hits} = response;
70 let isObject = false;
77 for (let {frameIndex, contentType, docDomain, thirdParty, location, filter} of hits) 71 for (let {frameIndex, contentType, docDomain, thirdParty, location, filter} of hits)
78 { 72 {
73 if (contentType == "OBJECT")
74 isObject = true;
75
79 let context = node; 76 let context = node;
80 if (typeof frameIndex == "number") 77 if (typeof frameIndex == "number")
81 { 78 {
82 context = window; 79 context = window;
83 for (let i = 0; i < frameIndex; i++) 80 for (let i = 0; i < frameIndex; i++)
84 context = context.parent; 81 context = context.parent;
85 context = context.document; 82 context = context.document;
86 } 83 }
87 RequestNotifier.addNodeData(context, window.top, contentType, docDomain, thi rdParty, location, filter); 84 RequestNotifier.addNodeData(context, window.top, contentType, docDomain, thi rdParty, location, filter);
88 } 85 }
89 86
90 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE) 87 if (node.nodeType == Ci.nsIDOMNode.ELEMENT_NODE)
91 { 88 {
92 // Track mouse events for objects 89 // Track mouse events for objects
93 if (allow && contentType == "OBJECT") 90 if (allow && isObject)
94 { 91 {
95 node.addEventListener("mouseover", objectMouseEventHander, true); 92 node.addEventListener("mouseover", objectMouseEventHander, true);
96 node.addEventListener("mouseout", objectMouseEventHander, true); 93 node.addEventListener("mouseout", objectMouseEventHander, true);
97 } 94 }
98 95
99 if (collapse) 96 if (collapse)
100 schedulePostProcess(node); 97 schedulePostProcess(node);
101 } 98 }
99 return allow;
100 }
102 101
103 return allow; 102 /**
103 * Checks whether a request should be allowed, hides it if necessary
104 * @param {nsIDOMWindow} window
105 * @param {nsIDOMElement} node
106 * @param {String} contentType
107 * @param {String} location
108 * @return {Boolean} false if the request should be blocked
109 */
110 function shouldAllow(window, node, contentType, location)
111 {
112 return processPolicyResponse(window, node, sendSyncMessage("AdblockPlus:Should Allow", {
113 contentType,
114 location,
115 frames: getFrames(window),
116 isPrivate: isPrivate(window)
117 }));
118 }
119
120 /**
121 * Asynchronously checks whether a request should be allowed.
122 * @param {nsIDOMWindow} window
123 * @param {nsIDOMElement} node
124 * @param {String} contentType
125 * @param {String} location
126 * @param {Function} callback callback to be called with a boolean value, if
127 * false the request should be blocked
128 */
129 function shouldAllowAsync(window, node, contentType, 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)));
104 } 137 }
105 138
106 /** 139 /**
107 * Actual nsIContentPolicy and nsIChannelEventSink implementation 140 * Actual nsIContentPolicy and nsIChannelEventSink implementation
108 * @class 141 * @class
109 */ 142 */
110 var PolicyImplementation = 143 var PolicyImplementation =
111 { 144 {
112 classDescription: "Adblock Plus content policy", 145 classDescription: "Adblock Plus content policy",
113 classID: Components.ID("cfeaabe6-1dd1-11b2-a0c6-cb5c268894c9"), 146 classID: Components.ID("cfeaabe6-1dd1-11b2-a0c6-cb5c268894c9"),
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 } 260 }
228 } 261 }
229 }, 262 },
230 263
231 // 264 //
232 // nsIChannelEventSink interface implementation 265 // nsIChannelEventSink interface implementation
233 // 266 //
234 267
235 asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback) 268 asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback)
236 { 269 {
237 let result = Cr.NS_OK; 270 let async = false;
238 try 271 try
239 { 272 {
240 // nsILoadInfo.contentPolicyType was introduced in Gecko 35, then 273 // nsILoadInfo.contentPolicyType was introduced in Gecko 35, then
241 // renamed to nsILoadInfo.externalContentPolicyType in Gecko 44. 274 // renamed to nsILoadInfo.externalContentPolicyType in Gecko 44.
242 let loadInfo = oldChannel.loadInfo; 275 let loadInfo = oldChannel.loadInfo;
243 let contentType = ("externalContentPolicyType" in loadInfo ? 276 let contentType = ("externalContentPolicyType" in loadInfo ?
244 loadInfo.externalContentPolicyType : loadInfo.contentPolicyType); 277 loadInfo.externalContentPolicyType : loadInfo.contentPolicyType);
245 if (!contentType) 278 if (!contentType)
246 return; 279 return;
247 280
248 let wnd = Utils.getRequestWindow(newChannel); 281 let wnd = Utils.getRequestWindow(newChannel);
249 if (!wnd) 282 if (!wnd)
250 return; 283 return;
251 284
252 if (contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT) 285 if (contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT)
253 { 286 {
254 if (wnd.history.length <= 1 && wnd.opener) 287 if (wnd.history.length <= 1 && wnd.opener)
255 { 288 {
256 // Special treatment for pop-up windows - this will close the window 289 // Special treatment for pop-up windows - this will close the window
257 // rather than preventing the redirect. Note that we might not have 290 // rather than preventing the redirect. Note that we might not have
258 // seen the original channel yet because the redirect happened before 291 // seen the original channel yet because the redirect happened before
259 // the async code in observe() had a chance to run. 292 // the async code in observe() had a chance to run.
260 this.observe(wnd, "content-document-global-created", null, oldChannel. URI.spec); 293 this.observe(wnd, "content-document-global-created", null, oldChannel. URI.spec);
261 this.observe(wnd, "content-document-global-created", null, newChannel. URI.spec); 294 this.observe(wnd, "content-document-global-created", null, newChannel. URI.spec);
262 } 295 }
263 return; 296 return;
264 } 297 }
265 298
266 if (!shouldAllow(wnd, wnd.document, types.get(contentType), newChannel.URI .spec)) 299 shouldAllowAsync(wnd, wnd.document, types.get(contentType), newChannel.URI .spec, function(allow)
267 result = Cr.NS_BINDING_ABORTED; 300 {
301 callback.onRedirectVerifyCallback(allow ? Cr.NS_OK : Cr.NS_BINDING_ABORT ED);
302 });
303 async = true;
268 } 304 }
269 catch (e) 305 catch (e)
270 { 306 {
271 // We shouldn't throw exceptions here - this will prevent the redirect. 307 // We shouldn't throw exceptions here - this will prevent the redirect.
272 Cu.reportError(e); 308 Cu.reportError(e);
273 } 309 }
274 finally 310 finally
275 { 311 {
276 callback.onRedirectVerifyCallback(result); 312 if (!async)
313 callback.onRedirectVerifyCallback(Cr.NS_OK);
277 } 314 }
278 }, 315 },
279 316
280 // 317 //
281 // nsIFactory interface implementation 318 // nsIFactory interface implementation
282 // 319 //
283 320
284 createInstance: function(outer, iid) 321 createInstance: function(outer, iid)
285 { 322 {
286 if (outer) 323 if (outer)
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 let property = (hasCols ? "cols" : "rows"); 379 let property = (hasCols ? "cols" : "rows");
343 let weights = parentNode[property].split(","); 380 let weights = parentNode[property].split(",");
344 weights[index] = "0"; 381 weights[index] = "0";
345 parentNode[property] = weights.join(","); 382 parentNode[property] = weights.join(",");
346 } 383 }
347 } 384 }
348 else 385 else
349 node.classList.add(collapsedClass); 386 node.classList.add(collapsedClass);
350 } 387 }
351 } 388 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld