| LEFT | RIGHT |
| 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 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | 22 "use strict"; |
| 23 Cu.import("resource://gre/modules/Services.jsm"); | 23 |
| 24 let {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
| 25 let {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
| 24 | 26 |
| 25 let {Utils} = require("utils"); | 27 let {Utils} = require("utils"); |
| 26 let {Prefs} = require("prefs"); | 28 let {Prefs} = require("prefs"); |
| 27 let {FilterStorage} = require("filterStorage"); | 29 let {FilterStorage} = require("filterStorage"); |
| 28 let {BlockingFilter, WhitelistFilter, RegExpFilter} = require("filterClasses"); | 30 let {BlockingFilter, WhitelistFilter, RegExpFilter} = require("filterClasses"); |
| 29 let {defaultMatcher} = require("matcher"); | 31 let {defaultMatcher} = require("matcher"); |
| 30 let {objectMouseEventHander} = require("objectTabs"); | 32 let {objectMouseEventHander} = require("objectTabs"); |
| 31 let {RequestNotifier} = require("requestNotifier"); | 33 let {RequestNotifier} = require("requestNotifier"); |
| 32 let {ElemHide} = require("elemHide"); | 34 let {ElemHide} = require("elemHide"); |
| 33 | 35 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 54 * Randomly generated class name, to be applied to collapsed nodes. | 56 * Randomly generated class name, to be applied to collapsed nodes. |
| 55 */ | 57 */ |
| 56 let collapsedClass = ""; | 58 let collapsedClass = ""; |
| 57 | 59 |
| 58 /** | 60 /** |
| 59 * Maps numerical content type IDs to strings. | 61 * Maps numerical content type IDs to strings. |
| 60 * @type Map | 62 * @type Map |
| 61 */ | 63 */ |
| 62 let types = new Map(); | 64 let types = new Map(); |
| 63 | 65 |
| 64 /** | |
| 65 * Numerical ID for fake ELEMHIDE type. | |
| 66 */ | |
| 67 const TYPE_ELEMHIDE = 0xFFFD; | |
| 68 | |
| 69 /** | |
| 70 * Numerical ID for fake POPUP type. | |
| 71 */ | |
| 72 const TYPE_POPUP = 0xFFFE; | |
| 73 | |
| 74 /** | 66 /** |
| 75 * Public policy checking functions and auxiliary objects | 67 * Public policy checking functions and auxiliary objects |
| 76 * @class | 68 * @class |
| 77 */ | 69 */ |
| 78 var Policy = exports.Policy = | 70 var Policy = exports.Policy = |
| 79 { | 71 { |
| 80 /** | 72 /** |
| 81 * Map of localized content type names by their identifiers. | 73 * Map of localized content type names by their identifiers. |
| 82 * @type Map | 74 * @type Map |
| 83 */ | 75 */ |
| 84 localizedDescr: new Map(), | 76 localizedDescr: new Map(), |
| 85 | 77 |
| 86 /** | 78 /** |
| 87 * Map containing all schemes that should be ignored by content policy. | 79 * Map containing all schemes that should be ignored by content policy. |
| 88 * @type Object | 80 * @type Set |
| 89 */ | 81 */ |
| 90 whitelistSchemes: {}, | 82 whitelistSchemes: new Set(), |
| 91 | 83 |
| 92 /** | 84 /** |
| 93 * Called on module startup, initializes various exported properties. | 85 * Called on module startup, initializes various exported properties. |
| 94 */ | 86 */ |
| 95 init: function() | 87 init: function() |
| 96 { | 88 { |
| 97 // Populate types map | 89 // Populate types map |
| 98 let iface = Ci.nsIContentPolicy; | 90 let iface = Ci.nsIContentPolicy; |
| 99 for (let name in iface) | 91 for (let name in iface) |
| 100 if (name.indexOf("TYPE_") == 0 && name != "TYPE_DATAREQUEST") | 92 if (name.indexOf("TYPE_") == 0 && name != "TYPE_DATAREQUEST") |
| 101 types.set(iface[name], name.substr(5)); | 93 types.set(iface[name], name.substr(5)); |
| 102 types.set(TYPE_ELEMHIDE, "ELEMHIDE"); | |
| 103 types.set(TYPE_POPUP, "POPUP"); | |
| 104 | 94 |
| 105 // Populate localized type names | 95 // Populate localized type names |
| 106 for (let typeName of contentTypes) | 96 for (let typeName of contentTypes) |
| 107 this.localizedDescr.set(typeName, Utils.getString("type_label_" + typeName
.toLowerCase())); | 97 this.localizedDescr.set(typeName, Utils.getString("type_label_" + typeName
.toLowerCase())); |
| 108 | 98 |
| 109 // whitelisted URL schemes | 99 // whitelisted URL schemes |
| 110 for (let scheme of Prefs.whitelistschemes.toLowerCase().split(" ")) | 100 for (let scheme of Prefs.whitelistschemes.toLowerCase().split(" ")) |
| 111 this.whitelistSchemes[scheme] = true; | 101 this.whitelistSchemes.add(scheme); |
| 112 | 102 |
| 113 // Generate class identifier used to collapse node and register correspondin
g | 103 // Generate class identifier used to collapse node and register correspondin
g |
| 114 // stylesheet. | 104 // stylesheet. |
| 115 let offset = "a".charCodeAt(0); | 105 let offset = "a".charCodeAt(0); |
| 116 for (let i = 0; i < 20; i++) | 106 for (let i = 0; i < 20; i++) |
| 117 collapsedClass += String.fromCharCode(offset + Math.random() * 26); | 107 collapsedClass += String.fromCharCode(offset + Math.random() * 26); |
| 118 | 108 |
| 119 let collapseStyle = Services.io.newURI("data:text/css," + | 109 let collapseStyle = Services.io.newURI("data:text/css," + |
| 120 encodeURIComponent("." + collapsedClass + | 110 encodeURIComponent("." + collapsedClass + |
| 121 "{-moz-binding: url(chrome://global/content/bindings/general.xml#foobarb
azdummy) !important;}"), null, null); | 111 "{-moz-binding: url(chrome://global/content/bindings/general.xml#foobarb
azdummy) !important;}"), null, null); |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 return !match || match instanceof WhitelistFilter; | 258 return !match || match instanceof WhitelistFilter; |
| 269 }, | 259 }, |
| 270 | 260 |
| 271 /** | 261 /** |
| 272 * Checks whether the location's scheme is blockable. | 262 * Checks whether the location's scheme is blockable. |
| 273 * @param location {nsIURI} | 263 * @param location {nsIURI} |
| 274 * @return {Boolean} | 264 * @return {Boolean} |
| 275 */ | 265 */ |
| 276 isBlockableScheme: function(location) | 266 isBlockableScheme: function(location) |
| 277 { | 267 { |
| 278 return !(location.scheme in Policy.whitelistSchemes); | 268 return !Policy.whitelistSchemes.has(location.scheme); |
| 279 }, | 269 }, |
| 280 | 270 |
| 281 /** | 271 /** |
| 282 * Checks whether a page is whitelisted. | 272 * Checks whether a page is whitelisted. |
| 283 * @param {String} url | 273 * @param {String} url |
| 284 * @param {String} [parentUrl] location of the parent page | 274 * @param {String} [parentUrl] location of the parent page |
| 285 * @param {String} [sitekey] public key provided on the page | 275 * @param {String} [sitekey] public key provided on the page |
| 286 * @return {Filter} filter that matched the URL or null if not whitelisted | 276 * @return {Filter} filter that matched the URL or null if not whitelisted |
| 287 */ | 277 */ |
| 288 isWhitelisted: function(url, parentUrl, sitekey) | 278 isWhitelisted: function(url, parentUrl, sitekey) |
| 289 { | 279 { |
| 290 if (!url) | 280 if (!url) |
| 291 return null; | 281 return null; |
| 292 | 282 |
| 293 // Do not apply exception rules to schemes on our whitelistschemes list. | 283 // Do not apply exception rules to schemes on our whitelistschemes list. |
| 294 let match = /^([\w\-]+):/.exec(url); | 284 let match = /^([\w\-]+):/.exec(url); |
| 295 if (match && match[1] in Policy.whitelistSchemes) | 285 if (match && Policy.whitelistSchemes.has(match[1])) |
| 296 return null; | 286 return null; |
| 297 | 287 |
| 298 if (!parentUrl) | 288 if (!parentUrl) |
| 299 parentUrl = url; | 289 parentUrl = url; |
| 300 | 290 |
| 301 // Ignore fragment identifier | 291 // Ignore fragment identifier |
| 302 let index = url.indexOf("#"); | 292 let index = url.indexOf("#"); |
| 303 if (index >= 0) | 293 if (index >= 0) |
| 304 url = url.substring(0, index); | 294 url = url.substring(0, index); |
| 305 | 295 |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 let uri = additional || Utils.makeURI(subject.location.href); | 414 let uri = additional || Utils.makeURI(subject.location.href); |
| 425 if (!Policy.processNode(subject.opener, subject.opener.document, "POPUP"
, uri, false)) | 415 if (!Policy.processNode(subject.opener, subject.opener.document, "POPUP"
, uri, false)) |
| 426 { | 416 { |
| 427 subject.stop(); | 417 subject.stop(); |
| 428 Utils.runAsync(() => subject.close()); | 418 Utils.runAsync(() => subject.close()); |
| 429 } | 419 } |
| 430 else if (uri.spec == "about:blank") | 420 else if (uri.spec == "about:blank") |
| 431 { | 421 { |
| 432 // An about:blank pop-up most likely means that a load will be | 422 // An about:blank pop-up most likely means that a load will be |
| 433 // initiated asynchronously. Wait for that. | 423 // initiated asynchronously. Wait for that. |
| 434 Utils.runAsync(function() | 424 Utils.runAsync(() => |
| 435 { | 425 { |
| 436 let channel = subject.QueryInterface(Ci.nsIInterfaceRequestor) | 426 let channel = subject.QueryInterface(Ci.nsIInterfaceRequestor) |
| 437 .getInterface(Ci.nsIDocShell) | 427 .getInterface(Ci.nsIDocShell) |
| 438 .QueryInterface(Ci.nsIDocumentLoader) | 428 .QueryInterface(Ci.nsIDocumentLoader) |
| 439 .documentChannel; | 429 .documentChannel; |
| 440 if (channel) | 430 if (channel) |
| 441 this.observe(subject, topic, data, channel.URI); | 431 this.observe(subject, topic, data, channel.URI); |
| 442 }); | 432 }); |
| 443 } | 433 } |
| 444 break; | 434 break; |
| 445 } | 435 } |
| 446 } | 436 } |
| 447 }, | 437 }, |
| 448 | 438 |
| 449 // | 439 // |
| 450 // nsIChannelEventSink interface implementation | 440 // nsIChannelEventSink interface implementation |
| 451 // | 441 // |
| 452 | 442 |
| 453 asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback) | 443 asyncOnChannelRedirect: function(oldChannel, newChannel, flags, callback) |
| 454 { | 444 { |
| 455 let result = Cr.NS_OK; | 445 let result = Cr.NS_OK; |
| 456 try | 446 try |
| 457 { | 447 { |
| 458 // nsILoadInfo.contentPolicyType was introduced in Gecko 35, then | 448 // nsILoadInfo.contentPolicyType was introduced in Gecko 35, then |
| 459 // renamed to nsILoadInfo.externalContentPolicyType in Gecko 44. | 449 // renamed to nsILoadInfo.externalContentPolicyType in Gecko 44. |
| 460 let loadInfo = oldChannel.loadInfo; | 450 let loadInfo = oldChannel.loadInfo; |
| 461 let contentType = loadInfo.externalContentPolicyType || loadInfo.contentPo
licyType; | 451 let contentType = ("externalContentPolicyType" in loadInfo ? |
| 452 loadInfo.externalContentPolicyType : loadInfo.contentPolicyType); |
| 462 if (!contentType) | 453 if (!contentType) |
| 463 return; | 454 return; |
| 464 | 455 |
| 465 let wnd = Utils.getRequestWindow(newChannel); | 456 let wnd = Utils.getRequestWindow(newChannel); |
| 466 if (!wnd) | 457 if (!wnd) |
| 467 return; | 458 return; |
| 468 | 459 |
| 469 if (contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT) | 460 if (contentType == Ci.nsIContentPolicy.TYPE_DOCUMENT) |
| 470 { | 461 { |
| 471 if (wnd.history.length <= 1 && wnd.opener) | 462 if (wnd.history.length <= 1 && wnd.opener) |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 691 if (!wnd || wnd.closed) | 682 if (!wnd || wnd.closed) |
| 692 return; | 683 return; |
| 693 | 684 |
| 694 if (entry.type == "OBJECT") | 685 if (entry.type == "OBJECT") |
| 695 { | 686 { |
| 696 node.removeEventListener("mouseover", objectMouseEventHander, true); | 687 node.removeEventListener("mouseover", objectMouseEventHander, true); |
| 697 node.removeEventListener("mouseout", objectMouseEventHander, true); | 688 node.removeEventListener("mouseout", objectMouseEventHander, true); |
| 698 } | 689 } |
| 699 Policy.processNode(wnd, node, entry.type, Utils.makeURI(entry.location), true)
; | 690 Policy.processNode(wnd, node, entry.type, Utils.makeURI(entry.location), true)
; |
| 700 } | 691 } |
| LEFT | RIGHT |