Index: lib/hitLogger.js |
diff --git a/lib/hitLogger.js b/lib/hitLogger.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0627d5eee8fada4f3bc40ee4d1d4abefbfca855a |
--- /dev/null |
+++ b/lib/hitLogger.js |
@@ -0,0 +1,152 @@ |
+/* |
+ * 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("../adblockpluscore/lib/coreUtils"); |
+const {extractHostFromFrame} = require("./url"); |
+const {EventEmitter} = require("../adblockpluscore/lib/events"); |
+const {FilterStorage} = require("../adblockpluscore/lib/filterStorage"); |
+const {port} = require("./messaging"); |
+const {RegExpFilter, |
+ ElemHideFilter} = require("../adblockpluscore/lib/filterClasses"); |
+ |
+const nonRequestTypes = exports.nonRequestTypes = [ |
+ "DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE", "CSP" |
+]; |
+ |
+let HitLogger = exports.HitLogger = Object.create(new EventEmitter(), desc({ |
+ off(tabId, listener) |
+ { |
+ Object.getPrototypeOf(this).off(tabId, listener); |
+ |
+ // 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); |
+ } |
+})); |
+ |
+/** |
+ * Checks whether a tab is inspected by anything. |
+ * |
+ * @param {number} tabId |
+ * @return {boolean} |
+ */ |
+let hasListener = exports.hasListener = function(tabId) |
+{ |
+ return HitLogger._listeners.has(tabId); |
+}; |
+ |
+/** |
+ * Logs a request associated with a tab or multiple tabs. |
+ * |
+ * @param {number[]} tabIds The tabIds associated with the request |
+ * @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(tabIds, url, type, docDomain, |
+ thirdParty, sitekey, |
+ specificOnly, filter) |
+{ |
+ for (let tabId of tabIds) |
+ { |
+ if (hasListener(tabId)) |
+ { |
+ let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; |
+ HitLogger.emit(tabId, request, filter); |
+ } |
+ } |
+}; |
+ |
+/** |
+ * Logs active element hiding filters for a tab. |
+ * |
+ * @param {number} tabId The ID of the tab, the elements were hidden in |
+ * @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 |
+ */ |
+function logHiddenElements(tabId, selectors, filters, docDomain) |
+{ |
+ if (hasListener(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}, filter); |
+ } |
+ } |
+ } |
+} |
+ |
+/** |
+ * Logs a whitelisting filter that disables (some kind of) |
+ * blocking for a particular document. |
+ * |
+ * @param {number} tabId The tabId the whitelisting is active for |
+ * @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 (hasListener(tabId)) |
+ { |
+ for (let type of nonRequestTypes) |
+ { |
+ if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) |
+ HitLogger.emit(tabId, {url, type, docDomain}, filter); |
+ } |
+ } |
+}; |
+ |
+port.on("hitLogger.traceElemHide", (message, sender) => |
+{ |
+ logHiddenElements( |
+ sender.page.id, message.selectors, message.filters, |
+ extractHostFromFrame(sender.frame) |
+ ); |
+}); |