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

Side by Side Diff: lib/devtools.js

Issue 29737602: Issue 4580 - Make filter/request logging use plain tabIds, prepare for multi-tab requests (Closed)
Patch Set: Removed outdated comment Created April 4, 2018, 1:48 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/cssInjection.js ('k') | lib/popupBlocker.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 12 matching lines...) Expand all
23 const {SpecialSubscription} = require("subscriptionClasses"); 23 const {SpecialSubscription} = require("subscriptionClasses");
24 const {FilterStorage} = require("filterStorage"); 24 const {FilterStorage} = require("filterStorage");
25 const {defaultMatcher} = require("matcher"); 25 const {defaultMatcher} = require("matcher");
26 const {FilterNotifier} = require("filterNotifier"); 26 const {FilterNotifier} = require("filterNotifier");
27 const {extractHostFromFrame} = require("url"); 27 const {extractHostFromFrame} = require("url");
28 const {port} = require("messaging"); 28 const {port} = require("messaging");
29 29
30 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE", 30 const nonRequestTypes = ["DOCUMENT", "ELEMHIDE",
31 "GENERICBLOCK", "GENERICHIDE", "CSP"]; 31 "GENERICBLOCK", "GENERICHIDE", "CSP"];
32 32
33 // Mapping of inspected tabs to their devpanel page
34 // and recorded items. We can't use a PageMap here,
35 // because data must persist after navigation/reload.
36 let panels = new Map(); 33 let panels = new Map();
37 34
38 function isActivePanel(panel) 35 function isActivePanel(panel)
39 { 36 {
40 return panel && !panel.reload && !panel.reloading; 37 return panel && !panel.reload && !panel.reloading;
41 } 38 }
42 39
43 function getActivePanel(page) 40 function getActivePanel(tabId)
44 { 41 {
45 let panel = panels.get(page.id); 42 let panel = panels.get(tabId);
46 if (isActivePanel(panel)) 43 if (isActivePanel(panel))
47 return panel; 44 return panel;
48 return null; 45 return null;
49 } 46 }
50 47
51 function getFilterInfo(filter) 48 function getFilterInfo(filter)
52 { 49 {
53 if (!filter) 50 if (!filter)
54 return null; 51 return null;
55 52
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
115 request.docDomain, 112 request.docDomain,
116 request.thirdParty, 113 request.thirdParty,
117 request.sitekey, 114 request.sitekey,
118 request.specificOnly 115 request.specificOnly
119 ); 116 );
120 } 117 }
121 118
122 /** 119 /**
123 * Logs a request to the devtools panel. 120 * Logs a request to the devtools panel.
124 * 121 *
125 * @param {?Page} page The page the request occured on or null if 122 * @param {number[]} tabIds The tabIds associated with the request
126 * the request isn't associated with a page
127 * @param {string} url The URL of the request 123 * @param {string} url The URL of the request
128 * @param {string} type The request type 124 * @param {string} type The request type
129 * @param {string} docDomain The IDN-decoded hostname of the document 125 * @param {string} docDomain The IDN-decoded hostname of the document
130 * @param {boolean} thirdParty Whether the origin of the request and 126 * @param {boolean} thirdParty Whether the origin of the request and
131 * document differs 127 * document differs
132 * @param {?string} sitekey The active sitekey if there is any 128 * @param {?string} sitekey The active sitekey if there is any
133 * @param {?boolean} specificOnly Whether generic filters should be ignored 129 * @param {?boolean} specificOnly Whether generic filters should be ignored
134 * @param {?BlockingFilter} filter The matched filter or null if there is no 130 * @param {?BlockingFilter} filter The matched filter or null if there is no
135 * match 131 * match
136 */ 132 */
137 exports.logRequest = function(page, url, type, docDomain, 133 exports.logRequest = function(tabIds, url, type, docDomain,
138 thirdParty, sitekey, 134 thirdParty, sitekey,
139 specificOnly, filter) 135 specificOnly, filter)
140 { 136 {
141 if (panels.size == 0) 137 if (panels.size == 0)
142 return; 138 return;
143 139
144 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly}; 140 let request = {url, type, docDomain, thirdParty, sitekey, specificOnly};
145 for (let [tabId, panel] of panels) 141 for (let [tabId, panel] of panels)
146 if ((!page || page.id == tabId) && isActivePanel(panel)) 142 if ((tabIds.length == 0 || tabIds.includes(tabId)) && isActivePanel(panel))
147 addRecord(panel, request, filter); 143 addRecord(panel, request, filter);
148 }; 144 };
149 145
150 /** 146 /**
151 * Logs active element hiding filters to the devtools panel. 147 * Logs active element hiding filters to the devtools panel.
152 * 148 *
153 * @param {Page} page The page the elements were hidden on 149 * @param {number} tabId The ID of the tab, the elements were hidden in
154 * @param {string[]} selectors The selectors of applied ElemHideFilters 150 * @param {string[]} selectors The selectors of applied ElemHideFilters
155 * @param {string[]} filters The text of applied ElemHideEmulationFilters 151 * @param {string[]} filters The text of applied ElemHideEmulationFilters
156 * @param {string} docDomain The IDN-decoded hostname of the document 152 * @param {string} docDomain The IDN-decoded hostname of the document
157 */ 153 */
158 function logHiddenElements(page, selectors, filters, docDomain) 154 function logHiddenElements(tabId, selectors, filters, docDomain)
159 { 155 {
160 let panel = getActivePanel(page); 156 let panel = getActivePanel(tabId);
161 if (panel) 157 if (panel)
162 { 158 {
163 for (let subscription of FilterStorage.subscriptions) 159 for (let subscription of FilterStorage.subscriptions)
164 { 160 {
165 if (subscription.disabled) 161 if (subscription.disabled)
166 continue; 162 continue;
167 163
168 for (let filter of subscription.filters) 164 for (let filter of subscription.filters)
169 { 165 {
170 // We only know the exact filter in case of element hiding emulation. 166 // We only know the exact filter in case of element hiding emulation.
171 // For regular element hiding filters, the content script only knows 167 // For regular element hiding filters, the content script only knows
172 // the selector, so we have to find a filter that has an identical 168 // the selector, so we have to find a filter that has an identical
173 // selector and is active on the domain the match was reported from. 169 // selector and is active on the domain the match was reported from.
174 let isActiveElemHideFilter = filter instanceof ElemHideFilter && 170 let isActiveElemHideFilter = filter instanceof ElemHideFilter &&
175 selectors.includes(filter.selector) && 171 selectors.includes(filter.selector) &&
176 filter.isActiveOnDomain(docDomain); 172 filter.isActiveOnDomain(docDomain);
177 173
178 if (isActiveElemHideFilter || filters.includes(filter.text)) 174 if (isActiveElemHideFilter || filters.includes(filter.text))
179 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter); 175 addRecord(panel, {type: "ELEMHIDE", docDomain}, filter);
180 } 176 }
181 } 177 }
182 } 178 }
183 } 179 }
184 180
185 /** 181 /**
186 * Logs a whitelisting filter, that disables (some kind of) 182 * Logs a whitelisting filter, that disables (some kind of)
187 * blocking for a particular document, to the devtools panel. 183 * blocking for a particular document, to the devtools panel.
188 * 184 *
189 * @param {Page} page The page the whitelisting is active on 185 * @param {number} tabId The tabId the whitelisting is active for
190 * @param {string} url The url of the whitelisted document 186 * @param {string} url The url of the whitelisted document
191 * @param {number} typeMask The bit mask of whitelisting types checked 187 * @param {number} typeMask The bit mask of whitelisting types checked
192 * for 188 * for
193 * @param {string} docDomain The IDN-decoded hostname of the parent 189 * @param {string} docDomain The IDN-decoded hostname of the parent
194 * document 190 * document
195 * @param {WhitelistFilter} filter The matched whitelisting filter 191 * @param {WhitelistFilter} filter The matched whitelisting filter
196 */ 192 */
197 exports.logWhitelistedDocument = function(page, url, typeMask, docDomain, 193 exports.logWhitelistedDocument = function(tabId, url, typeMask, docDomain,
198 filter) 194 filter)
199 { 195 {
200 let panel = getActivePanel(page); 196 let panel = getActivePanel(tabId);
201 if (panel) 197 if (panel)
202 { 198 {
203 for (let type of nonRequestTypes) 199 for (let type of nonRequestTypes)
204 { 200 {
205 if (typeMask & filter.contentType & RegExpFilter.typeMap[type]) 201 if (typeMask & filter.contentType & RegExpFilter.typeMap[type])
206 addRecord(panel, {url, type, docDomain}, filter); 202 addRecord(panel, {url, type, docDomain}, filter);
207 } 203 }
208 } 204 }
209 }; 205 };
210 206
211 /** 207 /**
212 * Checks whether a page is inspected by the devtools panel. 208 * Checks whether a tab is inspected by the devtools panel.
213 * 209 *
214 * @param {Page} page 210 * @param {number} tabId
215 * @return {boolean} 211 * @return {boolean}
216 */ 212 */
217 exports.hasPanel = function(page) 213 exports.hasPanel = function(tabId)
218 { 214 {
219 return panels.has(page.id); 215 return panels.has(tabId);
220 }; 216 };
221 217
222 function onBeforeRequest(details) 218 function onBeforeRequest(details)
223 { 219 {
224 let panel = panels.get(details.tabId); 220 let panel = panels.get(details.tabId);
225 221
226 // Clear the devtools panel and reload the inspected tab without caching 222 // Clear the devtools panel and reload the inspected tab without caching
227 // when a new request is issued. However, make sure that we don't end up 223 // when a new request is issued. However, make sure that we don't end up
228 // in an infinite recursion if we already triggered a reload. 224 // in an infinite recursion if we already triggered a reload.
229 if (panel.reloading) 225 if (panel.reloading)
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
374 FilterNotifier.off("subscription.added", onSubscriptionAdded); 370 FilterNotifier.off("subscription.added", onSubscriptionAdded);
375 } 371 }
376 }); 372 });
377 373
378 panels.set(inspectedTabId, {port: newPort, records: []}); 374 panels.set(inspectedTabId, {port: newPort, records: []});
379 }); 375 });
380 376
381 port.on("devtools.traceElemHide", (message, sender) => 377 port.on("devtools.traceElemHide", (message, sender) =>
382 { 378 {
383 logHiddenElements( 379 logHiddenElements(
384 sender.page, message.selectors, message.filters, 380 sender.page.id, message.selectors, message.filters,
385 extractHostFromFrame(sender.frame) 381 extractHostFromFrame(sender.frame)
386 ); 382 );
387 }); 383 });
OLDNEW
« no previous file with comments | « lib/cssInjection.js ('k') | lib/popupBlocker.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld