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: rebased on top of issue 6889 Created Aug. 28, 2018, 3:14 p.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 | « dependencies ('k') | lib/hitLogger.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
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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 return browser.tabs.executeScript(tabId, details)
195 .catch(error => 196 .catch(error =>
196 { 197 {
197 // Sometimes a frame is added and removed very quickly, in such cases we 198 // Sometimes a frame is added and removed very quickly, in such cases we
198 // simply ignore the error. 199 // simply ignore the error.
199 if (error.message == "The frame was removed.") 200 if (error.message == "The frame was removed.")
200 return; 201 return;
201 202
202 // Sometimes the frame in question is just not found. We don't know why 203 // Sometimes the frame in question is just not found. We don't know why
203 // this is exactly, but we simply ignore the error. 204 // this is exactly, but we simply ignore the error.
204 if (/^No frame with id \d+ in tab \d+\.$/.test(error.message)) 205 if (/^No frame with id \d+ in tab \d+\.$/.test(error.message))
205 return; 206 return;
206 207
207 throw error; 208 throw error;
208 }); 209 });
209 } 210 }
210 catch (error) 211 catch (error)
211 { 212 {
212 // See the comment in the catch block associated with the call to 213 // See the comment in the catch block associated with the call to
213 // tabs.insertCSS for why we catch and ignore any errors here. 214 // tabs.insertCSS for why we catch any error here and simply
215 // return a rejected promise.
216 return Promise.reject(error);
214 } 217 }
215 } 218 }
216 219
217 port.on("content.applyFilters", (message, sender) => 220 port.on("content.applyFilters", (message, sender) =>
218 { 221 {
219 let selectors = []; 222 let selectors = [];
220 let emulatedPatterns = []; 223 let emulatedPatterns = [];
221 let trace = HitLogger.hasListener(sender.page.id); 224 let trace = HitLogger.hasListener(sender.page.id);
222 let inline = !userStyleSheetsSupported; 225 let inline = !userStyleSheetsSupported;
223 226
224 let {elemhide, snippets} = message.filterTypes || 227 let {elemhide, snippets} = message.filterTypes ||
225 {elemhide: true, snippets: true}; 228 {elemhide: true, snippets: true};
226 229
227 if (!checkWhitelisted(sender.page, sender.frame, null, 230 if (!checkWhitelisted(sender.page, sender.frame, null,
228 RegExpFilter.typeMap.DOCUMENT)) 231 RegExpFilter.typeMap.DOCUMENT))
229 { 232 {
230 let hostname = extractHostFromFrame(sender.frame); 233 let docDomain = extractHostFromFrame(sender.frame);
231 234
232 if (snippets) 235 if (snippets)
233 { 236 {
234 for (let script of Snippets.getScriptsForDomain(hostname)) 237 for (let filter of Snippets.getFiltersForDomain(docDomain))
235 executeScript(script, sender.page.id, sender.frame.id); 238 {
239 executeScript(filter.script, sender.page.id, sender.frame.id).then(() =>
240 {
241 let tabIds = [sender.page.id];
242 if (filter)
243 FilterNotifier.emit("filter.hitCount", filter, 0, 0, tabIds);
244
245 logRequest(tabIds, {
246 url: sender.frame.url.href,
247 type: "SNIPPET",
248 docDomain
249 }, filter);
250 });
251 }
236 } 252 }
237 253
238 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null, 254 if (elemhide && !checkWhitelisted(sender.page, sender.frame, null,
239 RegExpFilter.typeMap.ELEMHIDE)) 255 RegExpFilter.typeMap.ELEMHIDE))
240 { 256 {
241 let specificOnly = checkWhitelisted(sender.page, sender.frame, null, 257 let specificOnly = checkWhitelisted(sender.page, sender.frame, null,
242 RegExpFilter.typeMap.GENERICHIDE); 258 RegExpFilter.typeMap.GENERICHIDE);
243 selectors = ElemHide.getSelectorsForDomain(hostname, specificOnly); 259 selectors = ElemHide.getSelectorsForDomain(docDomain, specificOnly);
244 260
245 for (let filter of ElemHideEmulation.getRulesForDomain(hostname)) 261 for (let filter of ElemHideEmulation.getRulesForDomain(docDomain))
246 emulatedPatterns.push({selector: filter.selector, text: filter.text}); 262 emulatedPatterns.push({selector: filter.selector, text: filter.text});
247 } 263 }
248 } 264 }
249 265
250 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id, 266 if (!inline && !updateFrameStyles(sender.page.id, sender.frame.id,
251 selectors, "standard")) 267 selectors, "standard"))
252 { 268 {
253 inline = true; 269 inline = true;
254 } 270 }
255 271
(...skipping 16 matching lines...) Expand all
272 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors, 288 updateFrameStyles(sender.page.id, sender.frame.id, message.selectors,
273 message.groupName, message.appendOnly); 289 message.groupName, message.appendOnly);
274 }); 290 });
275 291
276 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"}) 292 fetch(browser.extension.getURL("/snippets.js"), {cache: "no-cache"})
277 .then(response => response.ok ? response.text() : "") 293 .then(response => response.ok ? response.text() : "")
278 .then(text => 294 .then(text =>
279 { 295 {
280 snippetsLibrarySource = text; 296 snippetsLibrarySource = text;
281 }); 297 });
OLDNEW
« no previous file with comments | « dependencies ('k') | lib/hitLogger.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld