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-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 Content policy implementation, responsible for blocking things. | 19 * @fileOverview Content policy implementation, responsible for blocking things. |
20 */ | 20 */ |
21 | 21 |
22 "use strict"; | 22 "use strict"; |
23 | 23 |
24 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); | 24 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
25 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | 25 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
26 let {PrivateBrowsingUtils} = Cu.import("resource://gre/modules/PrivateBrowsingUt
ils.jsm", {}); | |
27 | 26 |
28 let {Utils} = require("utils"); | 27 let {Utils} = require("utils"); |
29 let {Prefs} = require("prefs"); | 28 let {Prefs} = require("prefs"); |
30 let {FilterStorage} = require("filterStorage"); | 29 let {FilterStorage} = require("filterStorage"); |
31 let {BlockingFilter, WhitelistFilter, RegExpFilter} = require("filterClasses"); | 30 let {BlockingFilter, WhitelistFilter, RegExpFilter} = require("filterClasses"); |
32 let {defaultMatcher} = require("matcher"); | 31 let {defaultMatcher} = require("matcher"); |
33 let {objectMouseEventHander} = require("objectTabs"); | 32 let {objectMouseEventHander} = require("objectTabs"); |
34 let {RequestNotifier} = require("requestNotifier"); | |
35 let {ElemHide} = require("elemHide"); | 33 let {ElemHide} = require("elemHide"); |
36 | 34 |
37 /** | 35 /** |
38 * Randomly generated class name, to be applied to collapsed nodes. | 36 * Randomly generated class name, to be applied to collapsed nodes. |
39 */ | 37 */ |
40 let collapsedClass = ""; | 38 let collapsedClass = ""; |
41 | 39 |
42 /** | 40 /** |
43 * Maps numerical content type IDs to strings. | |
44 * @type Map | |
45 */ | |
46 let types = new Map(); | |
47 | |
48 /** | |
49 * Public policy checking functions and auxiliary objects | 41 * Public policy checking functions and auxiliary objects |
50 * @class | 42 * @class |
51 */ | 43 */ |
52 var Policy = exports.Policy = | 44 var Policy = exports.Policy = |
53 { | 45 { |
54 /** | 46 /** |
55 * Set of explicitly supported content types | 47 * Set of explicitly supported content types |
56 * @type Set | 48 * @type Set |
57 */ | 49 */ |
58 contentTypes: new Set([ | 50 contentTypes: new Set([ |
(...skipping 19 matching lines...) Expand all Loading... |
78 | 70 |
79 /** | 71 /** |
80 * Called on module startup, initializes various exported properties. | 72 * Called on module startup, initializes various exported properties. |
81 */ | 73 */ |
82 init: function() | 74 init: function() |
83 { | 75 { |
84 // whitelisted URL schemes | 76 // whitelisted URL schemes |
85 for (let scheme of Prefs.whitelistschemes.toLowerCase().split(" ")) | 77 for (let scheme of Prefs.whitelistschemes.toLowerCase().split(" ")) |
86 this.whitelistSchemes.add(scheme); | 78 this.whitelistSchemes.add(scheme); |
87 | 79 |
| 80 let messageManager = Cc["@mozilla.org/parentprocessmessagemanager;1"] |
| 81 .getService(Ci.nsIMessageListenerManager) |
| 82 .QueryInterface(Ci.nsIMessageBroadcaster); |
| 83 let handler = (message => JSON.stringify(this.shouldAllow(message.data))); |
| 84 messageManager.addMessageListener("AdblockPlus:ShouldAllow", handler); |
| 85 onShutdown.add(() => messageManager.removeMessageListener("AdblockPlus:Shoul
dAllow", handler)); |
| 86 |
| 87 let handler2 = () => collapsedClass; |
| 88 messageManager.addMessageListener("AdblockPlus:GetCollapsedClass", handler2)
; |
| 89 onShutdown.add(() => messageManager.removeMessageListener("AdblockPlus:GetCo
llapsedClass", handler2)); |
| 90 |
88 // Generate class identifier used to collapse node and register correspondin
g | 91 // Generate class identifier used to collapse node and register correspondin
g |
89 // stylesheet. | 92 // stylesheet. |
90 let offset = "a".charCodeAt(0); | 93 let offset = "a".charCodeAt(0); |
91 for (let i = 0; i < 20; i++) | 94 for (let i = 0; i < 20; i++) |
92 collapsedClass += String.fromCharCode(offset + Math.random() * 26); | 95 collapsedClass += String.fromCharCode(offset + Math.random() * 26); |
93 | 96 |
94 let collapseStyle = Services.io.newURI("data:text/css," + | 97 let collapseStyle = Services.io.newURI("data:text/css," + |
95 encodeURIComponent("." + collapsedClass + | 98 encodeURIComponent("." + collapsedClass + |
96 "{-moz-binding: url(chrome://global/content/bindings/general.xml#foobarb
azdummy) !important;}"), null, null); | 99 "{-moz-binding: url(chrome://global/content/bindings/general.xml#foobarb
azdummy) !important;}"), null, null); |
97 Utils.styleService.loadAndRegisterSheet(collapseStyle, Ci.nsIStyleSheetServi
ce.USER_SHEET); | 100 Utils.styleService.loadAndRegisterSheet(collapseStyle, Ci.nsIStyleSheetServi
ce.USER_SHEET); |
98 onShutdown.add(() => | 101 onShutdown.add(() => |
99 { | 102 { |
100 Utils.styleService.unregisterSheet(collapseStyle, Ci.nsIStyleSheetService.
USER_SHEET); | 103 Utils.styleService.unregisterSheet(collapseStyle, Ci.nsIStyleSheetService.
USER_SHEET); |
101 }); | 104 }); |
102 }, | 105 }, |
103 | 106 |
104 /** | 107 /** |
105 * Checks whether a node should be blocked, hides it if necessary | 108 * Checks whether a node should be blocked, hides it if necessary |
106 * @param wnd {nsIDOMWindow} | |
107 * @param node {nsIDOMElement} | |
108 * @param contentType {String} | 109 * @param contentType {String} |
109 * @param location {String} | 110 * @param location {String} |
110 * @param collapse {Boolean} true to force hiding of the node | 111 * @param fremes {Object[]} |
111 * @return {Boolean} false if the node should be blocked | 112 * @param isPrivate {Boolean} true if the request belongs to a private browsi
ng window |
| 113 * @return {Object} An object containing properties block, collapse and hits |
| 114 * indicating how this request should be handled. |
112 */ | 115 */ |
113 processNode: function(wnd, node, contentType, location, collapse) | 116 shouldAllow: function({contentType, location, frames, isPrivate}) |
114 { | 117 { |
115 let topWnd = wnd.top; | 118 let hits = []; |
116 if (!topWnd || !topWnd.location || !topWnd.location.href) | 119 |
117 return true; | 120 function addHit(frameIndex, contentType, docDomain, thirdParty, location, fi
lter) |
| 121 { |
| 122 if (filter && !isPrivate) |
| 123 FilterStorage.increaseHitCount(filter); |
| 124 hits.push({ |
| 125 frameIndex, contentType, docDomain, thirdParty, location, |
| 126 filter: filter ? filter.text : null |
| 127 }); |
| 128 } |
| 129 |
| 130 function response(allow, collapse) |
| 131 { |
| 132 return {allow, collapse, hits}; |
| 133 } |
118 | 134 |
119 // Ignore whitelisted schemes | 135 // Ignore whitelisted schemes |
120 if (!this.isBlockableScheme(location)) | 136 if (!this.isBlockableScheme(location)) |
121 return true; | 137 return response(true, false); |
122 | 138 |
123 // Interpret unknown types as "other" | 139 // Interpret unknown types as "other" |
124 if (!this.contentTypes.has(contentType)) | 140 if (!this.contentTypes.has(contentType)) |
125 contentType = "OTHER"; | 141 contentType = "OTHER"; |
126 | 142 |
127 let originWindow = Utils.getOriginWindow(wnd); | 143 let wndLocation = frames[0].location; |
128 let wndLocation = originWindow.location.href; | |
129 let docDomain = getHostname(wndLocation); | 144 let docDomain = getHostname(wndLocation); |
130 let match = null; | 145 let match = null; |
131 let [sitekey, sitekeyWnd] = getSitekey(wnd); | 146 let [sitekey, sitekeyFrame] = getSitekey(frames); |
132 let nogeneric = false; | 147 let nogeneric = false; |
133 | |
134 function cleanWindowLocation(wnd) | |
135 { | |
136 let url = getWindowLocation(wnd); | |
137 let index = url.indexOf("#"); | |
138 if (index >= 0) | |
139 url = url.substring(0, index); | |
140 | |
141 return url; | |
142 } | |
143 | |
144 function addHit(match) | |
145 { | |
146 if (!PrivateBrowsingUtils.isWindowPrivate(wnd)) | |
147 FilterStorage.increaseHitCount(match); | |
148 } | |
149 | |
150 if (!match && Prefs.enabled) | 148 if (!match && Prefs.enabled) |
151 { | 149 { |
152 let testWnd = wnd; | |
153 let testSitekey = sitekey; | 150 let testSitekey = sitekey; |
154 let testSitekeyWnd = sitekeyWnd; | 151 let testSitekeyFrame = sitekeyFrame; |
155 let parentWndLocation = cleanWindowLocation(testWnd); | 152 for (let i = 0; i < frames.length; i++) |
156 while (true) | |
157 { | 153 { |
158 let testWndLocation = parentWndLocation; | 154 let frame = frames[i]; |
159 parentWndLocation = (testWnd == testWnd.parent ? testWndLocation : clean
WindowLocation(testWnd.parent)); | 155 let testWndLocation = frame.location; |
| 156 let parentWndLocation = frames[Math.min(i + 1, frames.length - 1)].locat
ion; |
160 let parentDocDomain = getHostname(parentWndLocation); | 157 let parentDocDomain = getHostname(parentWndLocation); |
161 | 158 |
162 let typeMap = RegExpFilter.typeMap.DOCUMENT; | 159 let typeMap = RegExpFilter.typeMap.DOCUMENT; |
163 if (contentType == "ELEMHIDE") | 160 if (contentType == "ELEMHIDE") |
164 typeMap = typeMap | RegExpFilter.typeMap.ELEMHIDE; | 161 typeMap = typeMap | RegExpFilter.typeMap.ELEMHIDE; |
165 let whitelistMatch = defaultMatcher.matchesAny(testWndLocation, typeMap,
parentDocDomain, false, sitekey); | 162 let whitelistMatch = defaultMatcher.matchesAny(testWndLocation, typeMap,
parentDocDomain, false, testSitekey); |
166 if (whitelistMatch instanceof WhitelistFilter) | 163 if (whitelistMatch instanceof WhitelistFilter) |
167 { | 164 { |
168 addHit(whitelistMatch); | 165 let whitelistType = (whitelistMatch.contentType & RegExpFilter.typeMap
.DOCUMENT) ? "DOCUMENT" : "ELEMHIDE"; |
169 RequestNotifier.addNodeData(testWnd.document, topWnd, | 166 addHit(i, whitelistType, parentDocDomain, false, testWndLocation, |
170 (whitelistMatch.contentType & RegExpFilter.typeMap.DOCUMENT) ? "DOCU
MENT" : "ELEMHIDE", | 167 whitelistMatch); |
171 parentDocDomain, false, testWndLocation, whitelistMatch); | 168 return response(true, false); |
172 return true; | |
173 } | 169 } |
174 | 170 |
175 let genericType = (contentType == "ELEMHIDE" ? "GENERICHIDE" : "GENERICB
LOCK"); | 171 let genericType = (contentType == "ELEMHIDE" ? "GENERICHIDE" : "GENERICB
LOCK"); |
176 let nogenericMatch = defaultMatcher.matchesAny(testWndLocation, | 172 let nogenericMatch = defaultMatcher.matchesAny(testWndLocation, |
177 RegExpFilter.typeMap[genericType], parentDocDomain, false, testSitek
ey); | 173 RegExpFilter.typeMap[genericType], parentDocDomain, false, testSitek
ey); |
178 if (nogenericMatch instanceof WhitelistFilter) | 174 if (nogenericMatch instanceof WhitelistFilter) |
179 { | 175 { |
180 nogeneric = true; | 176 nogeneric = true; |
181 | 177 addHit(i, genericType, parentDocDomain, false, testWndLocation, |
182 addHit(nogenericMatch); | 178 nogenericMatch); |
183 RequestNotifier.addNodeData(testWnd.document, topWnd, genericType, | |
184 parentDocDomain, false, testWndLocation, | |
185 nogenericMatch); | |
186 } | 179 } |
187 | 180 |
188 if (testWnd.parent == testWnd) | 181 if (frame == testSitekeyFrame) |
189 break; | 182 [testSitekey, testSitekeyFrame] = getSitekey(frames.slice(i + 1)); |
190 | |
191 if (testWnd == testSitekeyWnd) | |
192 [testSitekey, testSitekeyWnd] = getSitekey(testWnd.parent); | |
193 testWnd = testWnd.parent; | |
194 } | 183 } |
195 } | 184 } |
196 | 185 |
197 if (!match && contentType == "ELEMHIDE") | 186 if (!match && contentType == "ELEMHIDE") |
198 { | 187 { |
199 match = location; | 188 match = location; |
200 location = match.text.replace(/^.*?#/, '#'); | 189 location = match.text.replace(/^.*?#/, '#'); |
201 | 190 |
202 if (!match.isActiveOnDomain(docDomain)) | 191 if (!match.isActiveOnDomain(docDomain)) |
203 return true; | 192 return response(true, false); |
204 | 193 |
205 let exception = ElemHide.getException(match, docDomain); | 194 let exception = ElemHide.getException(match, docDomain); |
206 if (exception) | 195 if (exception) |
207 { | 196 { |
208 addHit(exception); | 197 addHit(null, contentType, docDomain, false, location, exception); |
209 RequestNotifier.addNodeData(node, topWnd, contentType, docDomain, false,
location, exception); | 198 return response(true, false); |
210 return true; | |
211 } | 199 } |
212 | 200 |
213 if (nogeneric && match.isGeneric()) | 201 if (nogeneric && match.isGeneric()) |
214 return true; | 202 return response(true, false); |
215 } | 203 } |
216 | 204 |
217 let thirdParty = (contentType == "ELEMHIDE" ? false : isThirdParty(location,
docDomain)); | 205 let thirdParty = (contentType == "ELEMHIDE" ? false : isThirdParty(location,
docDomain)); |
218 | 206 let collapse = false; |
219 if (!match && Prefs.enabled && RegExpFilter.typeMap.hasOwnProperty(contentTy
pe)) | 207 if (!match && Prefs.enabled && RegExpFilter.typeMap.hasOwnProperty(contentTy
pe)) |
220 { | 208 { |
221 match = defaultMatcher.matchesAny(location, RegExpFilter.typeMap[contentTy
pe], | 209 match = defaultMatcher.matchesAny(location, RegExpFilter.typeMap[contentTy
pe], |
222 docDomain, thirdParty, sitekey, nogeneri
c); | 210 docDomain, thirdParty, sitekey, nogeneri
c); |
223 if (match instanceof BlockingFilter && node.ownerDocument && !this.nonVisu
alTypes.has(contentType)) | 211 if (match instanceof BlockingFilter && !this.nonVisualTypes.has(contentTyp
e)) |
224 { | 212 collapse = (match.collapse != null ? match.collapse : !Prefs.fastcollaps
e); |
225 let prefCollapse = (match.collapse != null ? match.collapse : !Prefs.fas
tcollapse); | 213 } |
226 if (collapse || prefCollapse) | 214 addHit(null, contentType, docDomain, thirdParty, location, match); |
227 schedulePostProcess(node); | |
228 } | |
229 | 215 |
230 // Track mouse events for objects | 216 return response(!match || match instanceof WhitelistFilter, collapse); |
231 if (!match && contentType == "OBJECT" && node.nodeType == Ci.nsIDOMNode.EL
EMENT_NODE) | |
232 { | |
233 node.addEventListener("mouseover", objectMouseEventHander, true); | |
234 node.addEventListener("mouseout", objectMouseEventHander, true); | |
235 } | |
236 } | |
237 | |
238 // Store node data | |
239 RequestNotifier.addNodeData(node, topWnd, contentType, docDomain, thirdParty
, location, match); | |
240 if (match) | |
241 addHit(match); | |
242 | |
243 return !match || match instanceof WhitelistFilter; | |
244 }, | 217 }, |
245 | 218 |
246 /** | 219 /** |
247 * Checks whether the location's scheme is blockable. | 220 * Checks whether the location's scheme is blockable. |
248 * @param location {nsIURI|String} | 221 * @param location {nsIURI|String} |
249 * @return {Boolean} | 222 * @return {Boolean} |
250 */ | 223 */ |
251 isBlockableScheme: function(location) | 224 isBlockableScheme: function(location) |
252 { | 225 { |
253 let scheme; | 226 let scheme; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 if (entry.filter && !(entry.filter instanceof WhitelistFilter)) | 283 if (entry.filter && !(entry.filter instanceof WhitelistFilter)) |
311 return; | 284 return; |
312 | 285 |
313 for (let node of nodes) | 286 for (let node of nodes) |
314 Utils.runAsync(() => refilterNode(node, entry)); | 287 Utils.runAsync(() => refilterNode(node, entry)); |
315 } | 288 } |
316 }; | 289 }; |
317 Policy.init(); | 290 Policy.init(); |
318 | 291 |
319 /** | 292 /** |
320 * Actual nsIContentPolicy and nsIChannelEventSink implementation | |
321 * @class | |
322 */ | |
323 var PolicyImplementation = | |
324 { | |
325 classDescription: "Adblock Plus content policy", | |
326 classID: Components.ID("cfeaabe6-1dd1-11b2-a0c6-cb5c268894c9"), | |
327 contractID: "@adblockplus.org/abp/policy;1", | |
328 xpcom_categories: ["content-policy", "net-channel-event-sinks"], | |
329 | |
330 /** | |
331 * Registers the content policy on startup. | |
332 */ | |
333 init: function() | |
334 { | |
335 // Populate types map | |
336 let iface = Ci.nsIContentPolicy; | |
337 for (let name in iface) | |
338 if (name.indexOf("TYPE_") == 0 && name != "TYPE_DATAREQUEST") | |
339 types.set(iface[name], name.substr(5)); | |
340 | |
341 let registrar = Components.manager.QueryInterface(Ci.nsIComponentRegistrar); | |
342 registrar.registerFactory(this.classID, this.classDescription, this.contract
ID, this); | |
343 | |
344 let catMan = Utils.categoryManager; | |
345 for (let category of this.xpcom_categories) | |
346 catMan.addCategoryEntry(category, this.contractID, this.contractID, false,
true); | |
347 | |
348 Services.obs.addObserver(this, "content-document-global-created", true); | |
349 | |
350 onShutdown.add(() => | |
351 { | |
352 Services.obs.removeObserver(this, "content-document-global-created"); | |
353 | |
354 for (let category of this.xpcom_categories) | |
355 catMan.deleteCategoryEntry(category, this.contractID, false); | |
356 | |
357 registrar.unregisterFactory(this.classID, this); | |
358 }); | |
359 }, | |
360 | |
361 // | |
362 // nsISupports interface implementation | |
363 // | |
364 | |
365 QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentPolicy, Ci.nsIObserver, | |
366 Ci.nsIChannelEventSink, Ci.nsIFactory, Ci.nsISupportsWeakReference]), | |
367 | |
368 // | |
369 // nsIContentPolicy interface implementation | |
370 // | |
371 | |
372 shouldLoad: function(contentType, contentLocation, requestOrigin, node, mimeTy
peGuess, extra) | |
373 { | |
374 // Ignore requests without context and top-level documents | |
375 if (!node || contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT) | |
376 return Ci.nsIContentPolicy.ACCEPT; | |
377 | |
378 // Ignore standalone objects | |
379 if (contentType == Ci.nsIContentPolicy.TYPE_OBJECT && node.ownerDocument &&
!/^text\/|[+\/]xml$/.test(node.ownerDocument.contentType)) | |
380 return Ci.nsIContentPolicy.ACCEPT; | |
381 | |
382 let wnd = Utils.getWindow(node); | |
383 if (!wnd) | |
384 return Ci.nsIContentPolicy.ACCEPT; | |
385 | |
386 // Data loaded by plugins should be associated with the document | |
387 if (contentType == Ci.nsIContentPolicy.TYPE_OBJECT_SUBREQUEST && node instan
ceof Ci.nsIDOMElement) | |
388 node = node.ownerDocument; | |
389 | |
390 // Fix type for objects misrepresented as frames or images | |
391 if (contentType != Ci.nsIContentPolicy.TYPE_OBJECT && (node instanceof Ci.ns
IDOMHTMLObjectElement || node instanceof Ci.nsIDOMHTMLEmbedElement)) | |
392 contentType = Ci.nsIContentPolicy.TYPE_OBJECT; | |
393 | |
394 let location = Utils.unwrapURL(contentLocation); | |
395 let result = Policy.processNode(wnd, node, types.get(contentType), location.
spec, false); | |
396 return (result ? Ci.nsIContentPolicy.ACCEPT : Ci.nsIContentPolicy.REJECT_REQ
UEST); | |
397 }, | |
398 | |
399 shouldProcess: function(contentType, contentLocation, requestOrigin, insecNode
, mimeType, extra) | |
400 { | |
401 return Ci.nsIContentPolicy.ACCEPT; | |
402 }, | |
403 | |
404 // | |
405 // nsIObserver interface implementation | |
406 // | |
407 observe: function(subject, topic, data, additional) | |
408 { | |
409 switch (topic) | |
410 { | |
411 case "content-document-global-created": | |
412 { | |
413 if (!(subject instanceof Ci.nsIDOMWindow) || !subject.opener) | |
414 return; | |
415 | |
416 let uri = additional || subject.location.href; | |
417 if (!Policy.processNode(subject.opener, subject.opener.document, "POPUP"
, uri, false)) | |
418 { | |
419 subject.stop(); | |
420 Utils.runAsync(() => subject.close()); | |
421 } | |
422 else if (uri == "about:blank") | |
423 { | |
424 // An about:blank pop-up most likely means that a load will be | |
425 // initiated asynchronously. Wait for that. | |
426 Utils.runAsync(() => | |
427 { | |
428 let channel = subject.QueryInterface(Ci.nsIInterfaceRequestor) | |
429 .getInterface(Ci.nsIDocShell) | |
430 .QueryInterface(Ci.nsIDocumentLoader) | |
431 .documentChannel; | |
432 if (channel) | |
433 this.observe(subject, topic, data, channel.URI.spec); | |
434 }); | |
435 } | |
436 break; | |
437 } | |
438 } | |
439 }, | |
440 | |
441 // | |
442 // nsIChannelEventSink interface implementation | |
443 // | |
444 | |
445 asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback) | |
446 { | |
447 let result = Cr.NS_OK; | |
448 try | |
449 { | |
450 // nsILoadInfo.contentPolicyType was introduced in Gecko 35, then | |
451 // renamed to nsILoadInfo.externalContentPolicyType in Gecko 44. | |
452 let loadInfo = oldChannel.loadInfo; | |
453 let contentType = ("externalContentPolicyType" in loadInfo ? | |
454 loadInfo.externalContentPolicyType : loadInfo.contentPolicyType); | |
455 if (!contentType) | |
456 return; | |
457 | |
458 let wnd = Utils.getRequestWindow(newChannel); | |
459 if (!wnd) | |
460 return; | |
461 | |
462 if (contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT) | |
463 { | |
464 if (wnd.history.length <= 1 && wnd.opener) | |
465 { | |
466 // Special treatment for pop-up windows. Note that we might not have | |
467 // seen the original channel yet because the redirect happened before | |
468 // the async code in observe() had a chance to run. | |
469 this.observe(wnd, "content-document-global-created", null, oldChannel.
URI.spec); | |
470 this.observe(wnd, "content-document-global-created", null, newChannel.
URI.spec); | |
471 } | |
472 return; | |
473 } | |
474 | |
475 if (!Policy.processNode(wnd, wnd.document, types.get(contentType), newChan
nel.URI.spec, false)) | |
476 result = Cr.NS_BINDING_ABORTED; | |
477 } | |
478 catch (e) | |
479 { | |
480 // We shouldn't throw exceptions here - this will prevent the redirect. | |
481 Cu.reportError(e); | |
482 } | |
483 finally | |
484 { | |
485 callback.onRedirectVerifyCallback(result); | |
486 } | |
487 }, | |
488 | |
489 // | |
490 // nsIFactory interface implementation | |
491 // | |
492 | |
493 createInstance: function(outer, iid) | |
494 { | |
495 if (outer) | |
496 throw Cr.NS_ERROR_NO_AGGREGATION; | |
497 return this.QueryInterface(iid); | |
498 } | |
499 }; | |
500 PolicyImplementation.init(); | |
501 | |
502 /** | |
503 * Nodes scheduled for post-processing (might be null). | |
504 * @type Node[] | |
505 */ | |
506 let scheduledNodes = null; | |
507 | |
508 /** | |
509 * Schedules a node for post-processing. | |
510 */ | |
511 function schedulePostProcess(/**Element*/ node) | |
512 { | |
513 if (scheduledNodes) | |
514 scheduledNodes.push(node); | |
515 else | |
516 { | |
517 scheduledNodes = [node]; | |
518 Utils.runAsync(postProcessNodes); | |
519 } | |
520 } | |
521 | |
522 /** | |
523 * Processes nodes scheduled for post-processing (typically hides them). | |
524 */ | |
525 function postProcessNodes() | |
526 { | |
527 let nodes = scheduledNodes; | |
528 scheduledNodes = null; | |
529 | |
530 for (let node of nodes) | |
531 { | |
532 // adjust frameset's cols/rows for frames | |
533 let parentNode = node.parentNode; | |
534 if (parentNode && parentNode instanceof Ci.nsIDOMHTMLFrameSetElement) | |
535 { | |
536 let hasCols = (parentNode.cols && parentNode.cols.indexOf(",") > 0); | |
537 let hasRows = (parentNode.rows && parentNode.rows.indexOf(",") > 0); | |
538 if ((hasCols || hasRows) && !(hasCols && hasRows)) | |
539 { | |
540 let index = -1; | |
541 for (let frame = node; frame; frame = frame.previousSibling) | |
542 if (frame instanceof Ci.nsIDOMHTMLFrameElement || frame instanceof Ci.
nsIDOMHTMLFrameSetElement) | |
543 index++; | |
544 | |
545 let property = (hasCols ? "cols" : "rows"); | |
546 let weights = parentNode[property].split(","); | |
547 weights[index] = "0"; | |
548 parentNode[property] = weights.join(","); | |
549 } | |
550 } | |
551 else | |
552 node.classList.add(collapsedClass); | |
553 } | |
554 } | |
555 | |
556 /** | |
557 * Extracts the hostname from a URL (might return null). | 293 * Extracts the hostname from a URL (might return null). |
558 */ | 294 */ |
559 function getHostname(/**String*/ url) /**String*/ | 295 function getHostname(/**String*/ url) /**String*/ |
560 { | 296 { |
561 try | 297 try |
562 { | 298 { |
563 return Utils.unwrapURL(url).host; | 299 return Utils.unwrapURL(url).host; |
564 } | 300 } |
565 catch(e) | 301 catch(e) |
566 { | 302 { |
567 return null; | 303 return null; |
568 } | 304 } |
569 } | 305 } |
570 | 306 |
571 /** | 307 /** |
572 * Retrieves the sitekey of a window. | 308 * Retrieves and validates the sitekey for a frame structure. |
573 */ | 309 */ |
574 function getSitekey(wnd) | 310 function getSitekey(frames) |
575 { | 311 { |
576 let sitekey = null; | 312 for (let frame of frames) |
| 313 { |
| 314 if (frame.sitekey && frame.sitekey.indexOf("_") >= 0) |
| 315 { |
| 316 let [key, signature] = frame.sitekey.split("_", 2); |
| 317 key = key.replace(/=/g, ""); |
577 | 318 |
578 while (true) | 319 // Website specifies a key but is the signature valid? |
579 { | 320 let uri = Services.io.newURI(getWindowLocation(wnd), null, null); |
580 if (wnd.document && wnd.document.documentElement) | 321 let host = uri.asciiHost; |
581 { | 322 if (uri.port > 0) |
582 let keydata = wnd.document.documentElement.getAttribute("data-adblockkey")
; | 323 host += ":" + uri.port; |
583 if (keydata && keydata.indexOf("_") >= 0) | 324 let params = [ |
584 { | 325 uri.path.replace(/#.*/, ""), // REQUEST_URI |
585 let [key, signature] = keydata.split("_", 2); | 326 host, // HTTP_HOST |
586 key = key.replace(/=/g, ""); | 327 Utils.httpProtocol.userAgent // HTTP_USER_AGENT |
587 | 328 ]; |
588 // Website specifies a key but is the signature valid? | 329 if (Utils.verifySignature(key, signature, params.join("\0"))) |
589 let uri = Services.io.newURI(getWindowLocation(wnd), null, null); | 330 return [key, frame]; |
590 let host = uri.asciiHost; | |
591 if (uri.port > 0) | |
592 host += ":" + uri.port; | |
593 let params = [ | |
594 uri.path.replace(/#.*/, ""), // REQUEST_URI | |
595 host, // HTTP_HOST | |
596 Utils.httpProtocol.userAgent // HTTP_USER_AGENT | |
597 ]; | |
598 if (Utils.verifySignature(key, signature, params.join("\0"))) | |
599 return [key, wnd]; | |
600 } | |
601 } | 331 } |
602 | |
603 if (wnd === wnd.parent) | |
604 break; | |
605 | |
606 wnd = wnd.parent; | |
607 } | 332 } |
608 | 333 |
609 return [sitekey, wnd]; | 334 return [null, null]; |
610 } | 335 } |
611 | 336 |
612 /** | 337 /** |
613 * Retrieves the location of a window. | 338 * Retrieves the location of a window. |
614 * @param wnd {nsIDOMWindow} | 339 * @param wnd {nsIDOMWindow} |
615 * @return {String} window location or null on failure | 340 * @return {String} window location or null on failure |
616 */ | 341 */ |
617 function getWindowLocation(wnd) | 342 function getWindowLocation(wnd) |
618 { | 343 { |
619 if ("name" in wnd && wnd.name == "messagepane") | 344 if ("name" in wnd && wnd.name == "messagepane") |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
685 if (!wnd || wnd.closed) | 410 if (!wnd || wnd.closed) |
686 return; | 411 return; |
687 | 412 |
688 if (entry.type == "OBJECT") | 413 if (entry.type == "OBJECT") |
689 { | 414 { |
690 node.removeEventListener("mouseover", objectMouseEventHander, true); | 415 node.removeEventListener("mouseover", objectMouseEventHander, true); |
691 node.removeEventListener("mouseout", objectMouseEventHander, true); | 416 node.removeEventListener("mouseout", objectMouseEventHander, true); |
692 } | 417 } |
693 Policy.processNode(wnd, node, entry.type, entry.location, true); | 418 Policy.processNode(wnd, node, entry.type, entry.location, true); |
694 } | 419 } |
OLD | NEW |