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

Unified Diff: lib/hitLogger.js

Issue 29705719: Issue 6402 - Split filter hit / request logging out into own API (Closed)
Patch Set: Addressed Manish's feedback Created May 1, 2018, 10:49 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« lib/devtools.js ('K') | « lib/devtools.js ('k') | lib/popupBlocker.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/hitLogger.js
diff --git a/lib/hitLogger.js b/lib/hitLogger.js
new file mode 100644
index 0000000000000000000000000000000000000000..437d94017f686398abac4f638d52d237f13686fe
--- /dev/null
+++ b/lib/hitLogger.js
@@ -0,0 +1,162 @@
+/*
+ * 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 {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 eventEmitter = new EventEmitter();
+
+/**
+ * @namespace
+ * @static
+ */
+let HitLogger = exports.HitLogger = {
+ /**
+ * Adds a listener for requests, filter hits etc related to the tab.
Sebastian Noack 2018/05/02 16:11:33 Can we please document/specify that the calling co
kzar 2018/05/03 15:56:52 Done.
+ *
+ * @param {number} tabId
+ * @param {function} listener
+ */
+ addListener: eventEmitter.on.bind(eventEmitter),
+
+ /**
+ * Removes a listener for the tab.
+ *
+ * @param {number} tabId
+ * @param {function} listener
+ */
+ removeListener: eventEmitter.off.bind(eventEmitter),
+
+ /**
+ * Checks whether a tab is being inspected by anything.
+ *
+ * @param {number} tabId
+ * @return {boolean}
+ */
+ hasListener(tabId)
Sebastian Noack 2018/05/02 16:11:33 Does this method needs to be public (i.e. in the H
kzar 2018/05/03 15:56:52 Yea, since it's also used in lib/cssInjection.js.
+ {
+ let listeners = eventEmitter._listeners.get(tabId);
+ return listeners && listeners.length > 0;
+ }
+};
+
+/**
+ * Logs a request associated with a tab or multiple tabs.
+ *
+ * @param {number[]} tabIds
+ * The tabIds associated with the request
+ * @param {Object} request
+ * The request to log
+ * @param {string} request.url
+ * The URL of the request
+ * @param {string} request.type
+ * The request type
+ * @param {string} request.docDomain
+ * The IDN-decoded hostname of the document
+ * @param {boolean} request.thirdParty
+ * Whether the origin of the request and document differs
+ * @param {?string} request.sitekey
+ * The active sitekey if there is any
+ * @param {?boolean} request.specificOnly
+ * Whether generic filters should be ignored
+ * @param {?BlockingFilter} filter
+ * The matched filter or null if there is no match
+ */
+exports.logRequest = function(tabIds, request, filter)
Sebastian Noack 2018/05/02 16:11:33 Any particular reason why you pass in the request
kzar 2018/05/03 15:56:52 Well I did it this way since Manish suggested it[1
Sebastian Noack 2018/05/03 16:40:58 Well, the main argument for creating the request o
Manish Jethani 2018/05/05 01:26:09 No reasoning other than it makes the code easy to
Sebastian Noack 2018/05/05 12:56:23 Fair enough. I'm still not entirely convinced that
+{
+ for (let tabId of tabIds)
+ eventEmitter.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 (HitLogger.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))
+ eventEmitter.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,
Sebastian Noack 2018/05/02 16:11:33 Nit: It seems in other modules we use arrow functi
kzar 2018/05/03 15:56:51 Done.
+ filter)
+{
+ if (HitLogger.hasListener(tabId))
+ {
+ for (let type of nonRequestTypes)
+ {
+ if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
+ eventEmitter.emit(tabId, {url, type, docDomain}, filter);
+ }
+ }
+};
+
+port.on("hitLogger.traceElemHide", (message, sender) =>
+{
+ logHiddenElements(
Sebastian Noack 2018/05/02 16:11:33 I wonder it would make more sense to just inline t
kzar 2018/05/03 15:56:52 I prefer it how it is, I think it's easier to reas
+ sender.page.id, message.selectors, message.filters,
+ extractHostFromFrame(sender.frame)
+ );
+});
« lib/devtools.js ('K') | « lib/devtools.js ('k') | lib/popupBlocker.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld