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

Side by Side Diff: lib/contentFiltering.js

Issue 29865587: Issue 6843 - Log snippet filter hits (Closed) Base URL: https://hg.adblockplus.org/adblockpluschrome/
Patch Set: Created Aug. 27, 2018, 3:45 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
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
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 contentFiltering */ 18 /** @module contentFiltering */
19 19
20 "use strict"; 20 "use strict";
21 21
22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses"); 22 const {RegExpFilter} = require("../adblockpluscore/lib/filterClasses");
23 const {ElemHide} = require("../adblockpluscore/lib/elemHide"); 23 const {ElemHide} = require("../adblockpluscore/lib/elemHide");
24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation"); 24 const {ElemHideEmulation} = require("../adblockpluscore/lib/elemHideEmulation");
25 const {FilterNotifier} = require("../adblockpluscore/lib/filterNotifier");
25 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets"); 26 const {Snippets, compileScript} = require("../adblockpluscore/lib/snippets");
26 const {checkWhitelisted} = require("./whitelisting"); 27 const {checkWhitelisted} = require("./whitelisting");
27 const {extractHostFromFrame} = require("./url"); 28 const {extractHostFromFrame} = require("./url");
28 const {port} = require("./messaging"); 29 const {port} = require("./messaging");
29 const {HitLogger} = require("./hitLogger"); 30 const {HitLogger, logRequest} = require("./hitLogger");
30 const info = require("info"); 31 const info = require("info");
31 32
32 // Chromium's support for tabs.removeCSS is still a work in progress and the 33 // Chromium's support for tabs.removeCSS is still a work in progress and the
33 // API is likely to be different from Firefox's; for now we just don't use it 34 // API is likely to be different from Firefox's; for now we just don't use it
34 // at all, even if it's available. 35 // at all, even if it's available.
35 // See https://crbug.com/608854 36 // See https://crbug.com/608854
36 const styleSheetRemovalSupported = info.platform == "gecko"; 37 const styleSheetRemovalSupported = info.platform == "gecko";
37 38
38 const selectorGroupSize = 1024; 39 const selectorGroupSize = 1024;
39 40
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 let code = executableCode.get(script); 170 let code = executableCode.get(script);
170 if (code) 171 if (code)
171 return code; 172 return code;
172 173
173 code = compileScript(script, [snippetsLibrarySource]); 174 code = compileScript(script, [snippetsLibrarySource]);
174 175
175 executableCode.set(script, code); 176 executableCode.set(script, code);
176 return code; 177 return code;
177 } 178 }
178 179
179 function executeScript(script, tabId, frameId) 180 function executeScript(script, tabId, frameId, request)
180 { 181 {
181 try 182 try
182 { 183 {
183 let details = { 184 let details = {
184 code: getExecutableCode(script), 185 code: getExecutableCode(script),
185 matchAboutBlank: true, 186 matchAboutBlank: true,
186 runAt: "document_start" 187 runAt: "document_start"
187 }; 188 };
188 189
189 // Chrome <50 throws an exception if chrome.tabs.executeScript is called 190 // Chrome <50 throws an exception if chrome.tabs.executeScript is called
190 // with a frameId of 0. 191 // with a frameId of 0.
191 if (frameId != 0) 192 if (frameId != 0)
192 details.frameId = frameId; 193 details.frameId = frameId;
193 194
194 browser.tabs.executeScript(tabId, details) 195 browser.tabs.executeScript(tabId, details)
196 .then(() =>
Manish Jethani 2018/08/27 05:47:36 Ideally this would take the form `.then(() => {},
hub 2018/08/27 12:56:47 this is obsolete now.
197 {
198 let {filter} = request;
Manish Jethani 2018/08/27 05:47:36 We could read out `request.url` and `request.docDo
hub 2018/08/27 12:56:47 this is obsolete now.
199
200 let tabIds = [tabId];
201 if (filter)
202 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds);
203
204 logRequest(tabIds, {
205 url: request.url, type: "SNIPPET", docDomain: request.docDomain
206 }, filter);
207 })
195 .catch(error => 208 .catch(error =>
196 { 209 {
197 // Sometimes a frame is added and removed very quickly, in such cases we 210 // Sometimes a frame is added and removed very quickly, in such cases we
198 // simply ignore the error. 211 // simply ignore the error.
199 if (error.message == "The frame was removed.") 212 if (error.message == "The frame was removed.")
200 return; 213 return;
201 214
202 // Sometimes the frame in question is just not found. We don't know why 215 // Sometimes the frame in question is just not found. We don't know why
203 // this is exactly, but we simply ignore the error. 216 // this is exactly, but we simply ignore the error.
204 if (/^No frame with id \d+ in tab \d+\.$/.test(error.message)) 217 if (/^No frame with id \d+ in tab \d+\.$/.test(error.message))
205 return; 218 return;
206 219
207 throw error; 220 throw error;
208 }); 221 });
209 } 222 }
210 catch (error) 223 catch (error)
211 { 224 {
212 // See the comment in the catch block associated with the call to 225 // See the comment in the catch block associated with the call to
213 // tabs.insertCSS for why we catch and ignore any errors here. 226 // tabs.insertCSS for why we catch and ignore any errors here.
214 } 227 }
Manish Jethani 2018/08/27 05:47:36 I think it would be cleaner to keep executeScript
hub 2018/08/27 12:56:47 Done.
215 } 228 }
216 229
217 port.on("content.applyFilters", (message, sender) => 230 port.on("content.applyFilters", (message, sender) =>
218 { 231 {
219 let selectors = []; 232 let selectors = [];
220 let emulatedPatterns = []; 233 let emulatedPatterns = [];
221 let trace = HitLogger.hasListener(sender.page.id); 234 let trace = HitLogger.hasListener(sender.page.id);
222 let inline = !userStyleSheetsSupported; 235 let inline = !userStyleSheetsSupported;
223 236
224 let {elemhide, snippets} = message.filterTypes || 237 let {elemhide, snippets} = message.filterTypes ||
225 {elemhide: true, snippets: true}; 238 {elemhide: true, snippets: true};
226 239
227 if (!checkWhitelisted(sender.page, sender.frame, null, 240 if (!checkWhitelisted(sender.page, sender.frame, null,
228 RegExpFilter.typeMap.DOCUMENT)) 241 RegExpFilter.typeMap.DOCUMENT))
229 { 242 {
230 let hostname = extractHostFromFrame(sender.frame); 243 let hostname = extractHostFromFrame(sender.frame);
231 244
232 if (snippets) 245 if (snippets)
233 { 246 {
234 for (let script of Snippets.getScriptsForDomain(hostname)) 247 for (let filter of Snippets.getScriptsForDomain(hostname))
235 executeScript(script, sender.page.id, sender.frame.id); 248 executeScript(filter.script, sender.page.id, sender.frame.id,
249 {
250 docDomain: hostname,
Sebastian Noack 2018/08/27 04:51:35 Nit: If you rename the variable "hostname" to "doc
hub 2018/08/27 12:56:47 Done.
251 url: sender.frame.url.href,
252 filter
253 });
236 } 254 }
237 255
238 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, 256 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
239 RegExpFilter.typeMap.ELEMHIDE)) 257 RegExpFilter.typeMap.ELEMHIDE))
240 { 258 {
241 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 259 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
242 RegExpFilter.typeMap.GENERICHIDE); 260 RegExpFilter.typeMap.GENERICHIDE);
243 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly); 261 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly);
244 262
245 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 263 for (let filter of ElemHideEmulation.getRulesForDomain(hostname))
(...skipping 26 matching lines...) Expand all
272 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 290 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
273 message.groupName, message.appendOnly); 291 message.groupName, message.appendOnly);
274 }); 292 });
275 293
276 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 294 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
277 .then(response => response.ok ? response.text() : "") 295 .then(response => response.ok ? response.text() : "")
278 .then(text => 296 .then(text =>
279 { 297 {
280 snippetsLibrarySource = text; 298 snippetsLibrarySource = text;
281 }); 299 });
OLDNEW
« dependencies ('K') | « dependencies ('k') | lib/hitLogger.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld