| Index: lib/hitLogger.js |
| diff --git a/lib/hitLogger.js b/lib/hitLogger.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9b49c06861ac992634ab452b5822d8dfb9904309 |
| --- /dev/null |
| +++ b/lib/hitLogger.js |
| @@ -0,0 +1,148 @@ |
| +/* |
| + * This file is part of Adblock Plus <https://adblockplus.org/>, |
| + * Copyright (C) 2006-present eyeo GmbH |
| + * |
| + * Adblock Plus is free software: you can redistribute it and/or modify |
| + * it under the terms of the GNU General Public License version 3 as |
| + * published by the Free Software Foundation. |
| + * |
| + * Adblock Plus is distributed in the hope that it will be useful, |
| + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| + * GNU General Public License for more details. |
| + * |
| + * You should have received a copy of the GNU General Public License |
| + * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| + */ |
| + |
| +/** @module hitLogger */ |
| + |
| +"use strict"; |
| + |
| +const {desc} = require("coreUtils"); |
| +const {extractHostFromFrame} = require("url"); |
| +const {EventEmitter} = require("events"); |
| +const {FilterStorage} = require("filterStorage"); |
| +const {port} = require("messaging"); |
| +const {RegExpFilter, ElemHideFilter} = require("filterClasses"); |
| + |
| +const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; |
| + |
| +let HitLogger = exports.HitLogger = Object.create(new EventEmitter(), desc({ |
| + off(tabId, listener) |
| + { |
| + this.__proto__.off(tabId, listener); |
|
kzar
2018/03/21 15:18:46
I wanted to use `super` here instead of `this.__pr
|
| + |
| + // EventEmitter leaves an empty array once the last listener for an event |
| + // is removed. Usually that's fine, but in this case we need _listeners.size |
| + // and _listeners.has to be accurate. |
| + let listeners = this._listeners.get(tabId); |
| + if (listeners && listeners.length == 0) |
| + this._listeners.delete(tabId); |
| + } |
| +})); |
| + |
| +/** |
| + * Logs a request for a page if there's something (e.g. devtools panel) |
| + * listening. |
| + * |
| + * @param {number} tabId The page the request occured on or -1 if |
| + * the request isn't associated with a page |
| + * @param {string} url The URL of the request |
| + * @param {string} type The request type |
| + * @param {string} docDomain The IDN-decoded hostname of the document |
| + * @param {boolean} thirdParty Whether the origin of the request and |
| + * document differs |
| + * @param {?string} sitekey The active sitekey if there is any |
| + * @param {?boolean} specificOnly Whether generic filters should be ignored |
| + * @param {?BlockingFilter} filter The matched filter or null if there is no |
| + * match |
| + */ |
| +exports.logRequest = function(tabId, url, type, docDomain, thirdParty, sitekey, |
| + specificOnly, filter) |
| +{ |
| + if (HitLogger._listeners.size == 0) |
| + return; |
| + |
| + let request = {url, type, docDomain, thirdParty, sitekey, specificOnly, |
| + tabId}; |
| + for (let loggerTabId of HitLogger._listeners.keys()) |
| + if (tabId == -1 || tabId == loggerTabId) |
| + HitLogger.emit(tabId, request, filter); |
| +}; |
| + |
| +/** |
| + * Logs active element hiding filters for a page if there's something's |
| + * (e.g. devtools panel) listening. |
| + * |
| + * @param {int} tabId The page the elements were hidden on |
| + * @param {string[]} selectors The selectors of applied ElemHideFilters |
| + * @param {string[]} filters The text of applied ElemHideEmulationFilters |
| + * @param {string} docDomain The IDN-decoded hostname of the document |
| + */ |
| +exports.logHiddenElements = function(tabId, selectors, filters, docDomain) |
| +{ |
| + if (HitLogger._listeners.has(tabId)) |
| + { |
| + for (let subscription of FilterStorage.subscriptions) |
| + { |
| + if (subscription.disabled) |
| + continue; |
| + |
| + for (let filter of subscription.filters) |
| + { |
| + // We only know the exact filter in case of element hiding emulation. |
| + // For regular element hiding filters, the content script only knows |
| + // the selector, so we have to find a filter that has an identical |
| + // selector and is active on the domain the match was reported from. |
| + let isActiveElemHideFilter = filter instanceof ElemHideFilter && |
| + selectors.includes(filter.selector) && |
| + filter.isActiveOnDomain(docDomain); |
| + |
| + if (isActiveElemHideFilter || filters.includes(filter.text)) |
| + HitLogger.emit(tabId, {type: "ELEMHIDE", docDomain, tabId}, filter); |
| + } |
| + } |
| + } |
| +}; |
| + |
| +/** |
| + * Logs a whitelisting filter, that disables (some kind of) blocking |
| + * for a particular document if a devtools panel or similiar is listening. |
| + * |
| + * @param {number} tabId The page the whitelisting is active on |
| + * @param {string} url The url of the whitelisted document |
| + * @param {number} typeMask The bit mask of whitelisting types checked |
| + * for |
| + * @param {string} docDomain The IDN-decoded hostname of the parent |
| + * document |
| + * @param {WhitelistFilter} filter The matched whitelisting filter |
| + */ |
| +exports.logWhitelistedDocument = function(tabId, url, typeMask, docDomain, |
| + filter) |
| +{ |
| + if (HitLogger._listeners.has(tabId)) |
| + { |
| + for (let type of nonRequestTypes) |
| + { |
| + if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
| + HitLogger.emit(tabId, {type, url, docDomain, tabId}, filter); |
| + } |
| + } |
| +}; |
| + |
| +/** |
| + * Checks whether anything (e.g. devtools panel) is listening for a page's hits. |
| + */ |
| +exports.hasListener = function(tabId) |
| +{ |
| + return HitLogger._listeners.has(tabId); |
| +}; |
| + |
| +port.on("hitLogger.traceElemHide", (message, sender) => |
| +{ |
| + exports.logHiddenElements( |
| + sender.page.id, message.selectors, message.filters, |
| + extractHostFromFrame(sender.frame) |
| + ); |
| +}); |