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

Delta Between Two Patch Sets: lib/hitLogger.js

Issue 29705719: Issue 6402 - Split filter hit / request logging out into own API (Closed)
Left Patch Set: Addressed Sebastian's feedback Created March 21, 2018, 3:14 p.m.
Right Patch Set: Addressed Manish's feedback Created May 9, 2018, 5:53 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « lib/devtools.js ('k') | lib/popupBlocker.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 /** @module hitLogger */ 18 /** @module hitLogger */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {desc} = require("coreUtils"); 22 const {extractHostFromFrame} = require("./url");
23 const {extractHostFromFrame} = require("url"); 23 const {EventEmitter} = require("../adblockpluscore/lib/events");
24 const {EventEmitter} = require("events"); 24 const {FilterStorage} = require("../adblockpluscore/lib/filterStorage");
25 const {FilterStorage} = require("filterStorage"); 25 const {port} = require("./messaging");
26 const {port} = require("messaging"); 26 const {RegExpFilter,
27 const {RegExpFilter, ElemHideFilter} = require("filterClasses"); 27 ElemHideFilter} = require("../adblockpluscore/lib/filterClasses");
28 28
29 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE"]; 29 const nonRequestTypes = exports.nonRequestTypes = [
30 "DOCUMENT", "ELEMHIDE", "GENERICBLOCK", "GENERICHIDE", "CSP"
31 ];
30 32
31 let HitLogger = exports.HitLogger = Object.create(new EventEmitter(), desc({ 33 let eventEmitter = new EventEmitter();
32 off(tabId, listener)
33 {
34 this.__proto__.off(tabId, listener);
kzar 2018/03/21 15:18:46 I wanted to use `super` here instead of `this.__pr
35
36 // EventEmitter leaves an empty array once the last listener for an event
37 // is removed. Usually that's fine, but in this case we need _listeners.size
38 // and _listeners.has to be accurate.
39 let listeners = this._listeners.get(tabId);
40 if (listeners && listeners.length == 0)
41 this._listeners.delete(tabId);
42 }
43 }));
44 34
45 /** 35 /**
46 * Logs a request for a page if there's something (e.g. devtools panel) 36 * @namespace
47 * listening. 37 * @static
48 *
49 * @param {number} tabId The page the request occured on or -1 if
50 * the request isn't associated with a page
51 * @param {string} url The URL of the request
52 * @param {string} type The request type
53 * @param {string} docDomain The IDN-decoded hostname of the document
54 * @param {boolean} thirdParty Whether the origin of the request and
55 * document differs
56 * @param {?string} sitekey The active sitekey if there is any
57 * @param {?boolean} specificOnly Whether generic filters should be ignored
58 * @param {?BlockingFilter} filter The matched filter or null if there is no
59 * match
60 */ 38 */
61 exports.logRequest = function(tabId, url, type, docDomain, thirdParty, sitekey, 39 let HitLogger = exports.HitLogger = {
62 specificOnly, filter) 40 /**
63 { 41 * Adds a listener for requests, filter hits etc related to the tab.
64 if (HitLogger._listeners.size == 0) 42 *
65 return; 43 * Note: Calling code is responsible for removing the listener again,
44 * it will not be automatically removed when the tab is closed.
45 *
46 * @param {number} tabId
47 * @param {function} listener
48 */
49 addListener: eventEmitter.on.bind(eventEmitter),
66 50
67 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly, 51 /**
68 tabId}; 52 * Removes a listener for the tab.
69 for (let loggerTabId of HitLogger._listeners.keys()) 53 *
70 if (tabId == -1 || tabId == loggerTabId) 54 * @param {number} tabId
71 HitLogger.emit(tabId, request, filter); 55 * @param {function} listener
56 */
57 removeListener: eventEmitter.off.bind(eventEmitter),
58
59 /**
60 * Checks whether a tab is being inspected by anything.
61 *
62 * @param {number} tabId
63 * @return {boolean}
64 */
65 hasListener(tabId)
66 {
67 let listeners = eventEmitter._listeners.get(tabId);
68 return listeners && listeners.length > 0;
69 }
72 }; 70 };
73 71
74 /** 72 /**
75 * Logs active element hiding filters for a page if there's something's 73 * Logs a request associated with a tab or multiple tabs.
76 * (e.g. devtools panel) listening.
77 * 74 *
78 * @param {int} tabId The page the elements were hidden on 75 * @param {number[]} tabIds
76 * The tabIds associated with the request
77 * @param {Object} request
78 * The request to log
79 * @param {string} request.url
80 * The URL of the request
81 * @param {string} request.type
82 * The request type
83 * @param {string} request.docDomain
84 * The hostname of the document
85 * @param {boolean} request.thirdParty
86 * Whether the origin of the request and document differs
87 * @param {?string} request.sitekey
88 * The active sitekey if there is any
89 * @param {?boolean} request.specificOnly
90 * Whether generic filters should be ignored
91 * @param {?BlockingFilter} filter
92 * The matched filter or null if there is no match
93 */
94 exports.logRequest = (tabIds, request, filter) =>
95 {
96 for (let tabId of tabIds)
97 eventEmitter.emit(tabId, request, filter);
98 };
99
100 /**
101 * Logs active element hiding filters for a tab.
102 *
103 * @param {number} tabId The ID of the tab, the elements were hidden in
79 * @param {string[]} selectors The selectors of applied ElemHideFilters 104 * @param {string[]} selectors The selectors of applied ElemHideFilters
80 * @param {string[]} filters The text of applied ElemHideEmulationFilters 105 * @param {string[]} filters The text of applied ElemHideEmulationFilters
81 * @param {string} docDomain The IDN-decoded hostname of the document 106 * @param {string} docDomain The hostname of the document
82 */ 107 */
83 exports.logHiddenElements = function(tabId, selectors, filters, docDomain) 108 function logHiddenElements(tabId, selectors, filters, docDomain)
84 { 109 {
85 if (HitLogger._listeners.has(tabId)) 110 if (HitLogger.hasListener(tabId))
86 { 111 {
87 for (let subscription of FilterStorage.subscriptions) 112 for (let subscription of FilterStorage.subscriptions)
88 { 113 {
89 if (subscription.disabled) 114 if (subscription.disabled)
90 continue; 115 continue;
91 116
92 for (let filter of subscription.filters) 117 for (let filter of subscription.filters)
93 { 118 {
94 // We only know the exact filter in case of element hiding emulation. 119 // We only know the exact filter in case of element hiding emulation.
95 // For regular element hiding filters, the content script only knows 120 // For regular element hiding filters, the content script only knows
96 // the selector, so we have to find a filter that has an identical 121 // the selector, so we have to find a filter that has an identical
97 // selector and is active on the domain the match was reported from. 122 // selector and is active on the domain the match was reported from.
98 let isActiveElemHideFilter = filter instanceof ElemHideFilter && 123 let isActiveElemHideFilter = filter instanceof ElemHideFilter &&
99 selectors.includes(filter.selector) && 124 selectors.includes(filter.selector) &&
100 filter.isActiveOnDomain(docDomain); 125 filter.isActiveOnDomain(docDomain);
101 126
102 if (isActiveElemHideFilter || filters.includes(filter.text)) 127 if (isActiveElemHideFilter || filters.includes(filter.text))
103 HitLogger.emit(tabId, {type: "ELEMHIDE", docDomain, tabId}, filter); 128 eventEmitter.emit(tabId, {type: "ELEMHIDE", docDomain}, filter);
104 } 129 }
130 }
131 }
132 }
133
134 /**
135 * Logs a whitelisting filter that disables (some kind of)
136 * blocking for a particular document.
137 *
138 * @param {number} tabId The tabId the whitelisting is active for
139 * @param {string} url The url of the whitelisted document
140 * @param {number} typeMask The bit mask of whitelisting types checked
141 * for
142 * @param {string} docDomain The hostname of the parent document
143 * @param {WhitelistFilter} filter The matched whitelisting filter
144 */
145 exports.logWhitelistedDocument = (tabId, url, typeMask, docDomain, filter) =>
146 {
147 if (HitLogger.hasListener(tabId))
148 {
149 for (let type of nonRequestTypes)
150 {
151 if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
152 eventEmitter.emit(tabId, {url, type, docDomain}, filter);
105 } 153 }
106 } 154 }
107 }; 155 };
108 156
109 /**
110 * Logs a whitelisting filter, that disables (some kind of) blocking
111 * for a particular document if a devtools panel or similiar is listening.
112 *
113 * @param {number} tabId The page the whitelisting is active on
114 * @param {string} url The url of the whitelisted document
115 * @param {number} typeMask The bit mask of whitelisting types checked
116 * for
117 * @param {string} docDomain The IDN-decoded hostname of the parent
118 * document
119 * @param {WhitelistFilter} filter The matched whitelisting filter
120 */
121 exports.logWhitelistedDocument = function(tabId, url, typeMask, docDomain,
122 filter)
123 {
124 if (HitLogger._listeners.has(tabId))
125 {
126 for (let type of nonRequestTypes)
127 {
128 if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
129 HitLogger.emit(tabId, {type, url, docDomain, tabId}, filter);
130 }
131 }
132 };
133
134 /**
135 * Checks whether anything (e.g. devtools panel) is listening for a page's hits.
136 */
137 exports.hasListener = function(tabId)
138 {
139 return HitLogger._listeners.has(tabId);
140 };
141
142 port.on("hitLogger.traceElemHide", (message, sender) => 157 port.on("hitLogger.traceElemHide", (message, sender) =>
143 { 158 {
144 exports.logHiddenElements( 159 logHiddenElements(
145 sender.page.id, message.selectors, message.filters, 160 sender.page.id, message.selectors, message.filters,
146 extractHostFromFrame(sender.frame) 161 extractHostFromFrame(sender.frame)
147 ); 162 );
148 }); 163 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld