 Issue 29705719:
  Issue 6402 - Split filter hit / request logging out into own API  (Closed)
    
  
    Issue 29705719:
  Issue 6402 - Split filter hit / request logging out into own API  (Closed) 
  | Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-present eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 /** @module hitLogger */ | |
| 19 | |
| 20 "use strict"; | |
| 21 | |
| 22 const {extractHostFromFrame} = require("./url"); | |
| 23 const {EventEmitter} = require("../adblockpluscore/lib/events"); | |
| 24 const {FilterStorage} = require("../adblockpluscore/lib/filterStorage"); | |
| 25 const {port} = require("./messaging"); | |
| 26 const {RegExpFilter, | |
| 27 ElemHideFilter} = require("../adblockpluscore/lib/filterClasses"); | |
| 28 | |
| 29 const nonRequestTypes = exports.nonRequestTypes = [ | |
| 30 "DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE", "CSP" | |
| 31 ]; | |
| 32 | |
| 33 let eventEmitter = new EventEmitter(); | |
| 34 | |
| 35 /** | |
| 36 * @namespace | |
| 37 * @static | |
| 38 */ | |
| 39 let HitLogger = exports.HitLogger = { | |
| 40 /** | |
| 41 * 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.
 | |
| 42 * | |
| 43 * @param {number} tabId | |
| 44 * @param {function} listener | |
| 45 */ | |
| 46 addListener: eventEmitter.on.bind(eventEmitter), | |
| 47 | |
| 48 /** | |
| 49 * Removes a listener for the tab. | |
| 50 * | |
| 51 * @param {number} tabId | |
| 52 * @param {function} listener | |
| 53 */ | |
| 54 removeListener: eventEmitter.off.bind(eventEmitter), | |
| 55 | |
| 56 /** | |
| 57 * Checks whether a tab is being inspected by anything. | |
| 58 * | |
| 59 * @param {number} tabId | |
| 60 * @return {boolean} | |
| 61 */ | |
| 62 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.
 | |
| 63 { | |
| 64 let listeners = eventEmitter._listeners.get(tabId); | |
| 65 return listeners && listeners.length > 0; | |
| 66 } | |
| 67 }; | |
| 68 | |
| 69 /** | |
| 70 * Logs a request associated with a tab or multiple tabs. | |
| 71 * | |
| 72 * @param {number[]} tabIds | |
| 73 * The tabIds associated with the request | |
| 74 * @param {Object} request | |
| 75 * The request to log | |
| 76 * @param {string} request.url | |
| 77 * The URL of the request | |
| 78 * @param {string} request.type | |
| 79 * The request type | |
| 80 * @param {string} request.docDomain | |
| 81 * The IDN-decoded hostname of the document | |
| 82 * @param {boolean} request.thirdParty | |
| 83 * Whether the origin of the request and document differs | |
| 84 * @param {?string} request.sitekey | |
| 85 * The active sitekey if there is any | |
| 86 * @param {?boolean} request.specificOnly | |
| 87 * Whether generic filters should be ignored | |
| 88 * @param {?BlockingFilter} filter | |
| 89 * The matched filter or null if there is no match | |
| 90 */ | |
| 91 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
 | |
| 92 { | |
| 93 for (let tabId of tabIds) | |
| 94 eventEmitter.emit(tabId, request, filter); | |
| 95 }; | |
| 96 | |
| 97 /** | |
| 98 * Logs active element hiding filters for a tab. | |
| 99 * | |
| 100 * @param {number} tabId The ID of the tab, the elements were hidden in | |
| 101 * @param {string[]} selectors The selectors of applied ElemHideFilters | |
| 102 * @param {string[]} filters The text of applied ElemHideEmulationFilters | |
| 103 * @param {string} docDomain The IDN-decoded hostname of the document | |
| 104 */ | |
| 105 function logHiddenElements(tabId, selectors, filters, docDomain) | |
| 106 { | |
| 107 if (HitLogger.hasListener(tabId)) | |
| 108 { | |
| 109 for (let subscription of FilterStorage.subscriptions) | |
| 110 { | |
| 111 if (subscription.disabled) | |
| 112 continue; | |
| 113 | |
| 114 for (let filter of subscription.filters) | |
| 115 { | |
| 116 // We only know the exact filter in case of element hiding emulation. | |
| 117 // For regular element hiding filters, the content script only knows | |
| 118 // the selector, so we have to find a filter that has an identical | |
| 119 // selector and is active on the domain the match was reported from. | |
| 120 let isActiveElemHideFilter = filter instanceof ElemHideFilter && | |
| 121 selectors.includes(filter.selector) && | |
| 122 filter.isActiveOnDomain(docDomain); | |
| 123 | |
| 124 if (isActiveElemHideFilter || filters.includes(filter.text)) | |
| 125 eventEmitter.emit(tabId, {type: "ELEMHIDE", docDomain}, filter); | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 } | |
| 130 | |
| 131 /** | |
| 132 * Logs a whitelisting filter that disables (some kind of) | |
| 133 * blocking for a particular document. | |
| 134 * | |
| 135 * @param {number} tabId The tabId the whitelisting is active for | |
| 136 * @param {string} url The url of the whitelisted document | |
| 137 * @param {number} typeMask The bit mask of whitelisting types checked | |
| 138 * for | |
| 139 * @param {string} docDomain The IDN-decoded hostname of the parent | |
| 140 * document | |
| 141 * @param {WhitelistFilter} filter The matched whitelisting filter | |
| 142 */ | |
| 143 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.
 | |
| 144 filter) | |
| 145 { | |
| 146 if (HitLogger.hasListener(tabId)) | |
| 147 { | |
| 148 for (let type of nonRequestTypes) | |
| 149 { | |
| 150 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) | |
| 151 eventEmitter.emit(tabId, {url, type, docDomain}, filter); | |
| 152 } | |
| 153 } | |
| 154 }; | |
| 155 | |
| 156 port.on("hitLogger.traceElemHide", (message, sender) => | |
| 157 { | |
| 158 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
 | |
| 159 sender.page.id, message.selectors, message.filters, | |
| 160 extractHostFromFrame(sender.frame) | |
| 161 ); | |
| 162 }); | |
| OLD | NEW |