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

Side by Side Diff: lib/elemHide.js

Issue 29773570: Issue 6652 - Implement fast selector lookups for unknown domains (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Fix JSDoc Created May 12, 2018, 10:13 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 | « no previous file | test/elemHide.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 "use strict"; 18 "use strict";
19 19
20 /** 20 /**
21 * @fileOverview Element hiding implementation. 21 * @fileOverview Element hiding implementation.
22 */ 22 */
23 23
24 const {ElemHideException} = require("./filterClasses"); 24 const {ElemHideException} = require("./filterClasses");
25 const {FilterNotifier} = require("./filterNotifier"); 25 const {FilterNotifier} = require("./filterNotifier");
26 26
27 /** 27 /**
28 * Lookup table, active flag, by filter by domain. 28 * Lookup table, active flag, by filter by domain.
29 * (Only contains filters that aren't unconditionally matched for all domains.) 29 * (Only contains filters that aren't unconditionally matched for all domains.)
30 * @type {Map.<string,Map.<Filter,boolean>>} 30 * @type {Map.<string,?Map.<Filter,boolean>>}
31 */ 31 */
32 let filtersByDomain = new Map(); 32 let filtersByDomain = new Map();
33 33
34 /** 34 /**
35 * Lookup table, filter by selector. (Only used for selectors that are 35 * Lookup table, filter by selector. (Only used for selectors that are
36 * unconditionally matched for all domains.) 36 * unconditionally matched for all domains.)
37 * @type {Map.<string,Filter>} 37 * @type {Map.<string,Filter>}
38 */ 38 */
39 let filterBySelector = new Map(); 39 let filterBySelector = new Map();
40 40
(...skipping 13 matching lines...) Expand all
54 let defaultDomains = new Map([["", true]]); 54 let defaultDomains = new Map([["", true]]);
55 55
56 /** 56 /**
57 * Set containing known element hiding and exception filters 57 * Set containing known element hiding and exception filters
58 * @type {Set.<ElemHideBase>} 58 * @type {Set.<ElemHideBase>}
59 */ 59 */
60 let knownFilters = new Set(); 60 let knownFilters = new Set();
61 61
62 /** 62 /**
63 * Lookup table, lists of element hiding exceptions by selector 63 * Lookup table, lists of element hiding exceptions by selector
64 * @type {Map.<string,Filter>} 64 * @type {Map.<string,Filter[]>}
65 */ 65 */
66 let exceptions = new Map(); 66 let exceptions = new Map();
67 67
68 /** 68 /**
69 * Lookup table, lists of generic element hiding exceptions by selector
70 * @type {Map.<string,Filter[]>}
71 */
72 let genericExceptions = new Map();
73
74 /**
75 * List of selectors that apply on any unknown domain
76 * @type {?string[]}
77 */
78 let conditionalGenericSelectors = null;
79
80 /**
81 * Domains that are known not to be specifically excluded from any generic
82 * filters
83 * @type {Set.<string>}
84 */
85 let genericFriendlyDomains = new Set();
86
87 /**
69 * Adds a filter to the lookup table of filters by domain. 88 * Adds a filter to the lookup table of filters by domain.
70 * @param {Filter} filter 89 * @param {Filter} filter
71 */ 90 */
72 function addToFiltersByDomain(filter) 91 function addToFiltersByDomain(filter)
73 { 92 {
74 let domains = filter.domains || defaultDomains; 93 let domains = filter.domains || defaultDomains;
75 for (let [domain, isIncluded] of domains) 94 if (filter instanceof ElemHideException)
76 { 95 {
77 // There's no need to note that a filter is generically disabled. 96 for (let domain of domains.keys())
78 if (!isIncluded && domain == "") 97 {
98 // Add an entry for each domain, but without any filters. This makes
99 // the domain "known" and helps us avoid certain optimizations that
100 // would otherwise yield incorrect results.
101 if (domain != "" && !filtersByDomain.has(domain))
102 filtersByDomain.set(domain, null);
103 }
104 }
105 else
106 {
107 for (let [domain, isIncluded] of domains)
108 {
109 // There's no need to note that a filter is generically disabled.
110 if (!isIncluded && domain == "")
111 continue;
112
113 let filters = filtersByDomain.get(domain);
114 if (!filters)
115 filtersByDomain.set(domain, filters = new Map());
116 filters.set(filter, isIncluded);
117 }
118 }
119 }
120
121 /**
122 * Checks whether a filter applies on a domain
123 * @param {Filter} filter
124 * @param {string} [domain]
125 * @param {Set.<Filter>} excludeSet
126 * @returns {boolean}
127 */
128 function doesFilterApply(filter, domain, excludeSet)
129 {
130 return (excludeSet.size == 0 || !excludeSet.has(filter)) &&
131 !ElemHide.getException(filter, domain);
132 }
133
134 /**
135 * Returns a list of domain-specific filters matching a domain
136 * @param {string} [domain]
137 * @returns {Array.<?Map.<Filter,boolean>>}
138 */
139 function getSpecificFiltersForDomain(domain)
140 {
141 let filtersList = [];
142
143 if (domain)
144 domain = domain.toUpperCase();
145
146 while (domain)
147 {
148 // Note that we also push null values into the list, because
149 // ElemHide.getSelectorsForDomain still needs to know if there are any
150 // entries for the domain.
151 let filters = filtersByDomain.get(domain);
152 if (typeof filters != "undefined")
153 filtersList.push(filters);
154
155 let nextDot = domain.indexOf(".");
156 domain = nextDot == -1 ? null : domain.substring(nextDot + 1);
157 }
158
159 return filtersList;
160 }
161
162 /**
163 * Returns a list of selectors from a given list of filters that apply on a
164 * domain
165 * @param {string} [domain]
166 * @param {Array.<?Map.<Filter,boolean>>} filtersList
167 * @param {?Map.<Filter,boolean>} genericFilters
168 * @returns {string[]}
169 */
170 function getConditionalSelectorsForDomain(domain, filtersList, genericFilters)
171 {
172 let selectors = [];
173
174 let excluded = new Set();
175
176 // This code is a performance hot-spot, which is why we've made certain
177 // micro-optimisations. Please be careful before making changes.
178 for (let i = 0; i < filtersList.length; i++)
179 {
180 if (!filtersList[i])
79 continue; 181 continue;
80 182
81 let filters = filtersByDomain.get(domain); 183 for (let [filter, isIncluded] of filtersList[i])
82 if (!filters) 184 {
83 filtersByDomain.set(domain, filters = new Map()); 185 if (!isIncluded)
84 filters.set(filter, isIncluded); 186 excluded.add(filter);
187 else if (doesFilterApply(filter, domain, excluded))
188 selectors.push(filter.selector);
189 }
85 } 190 }
191
192 if (!genericFilters)
193 return selectors;
194
195 if (genericFriendlyDomains.has(domain))
196 return selectors.concat(getConditionalGenericSelectors());
197
198 let genericSelectors = [];
199
200 for (let filter of genericFilters.keys())
201 {
202 if (doesFilterApply(filter, domain, excluded))
203 genericSelectors.push(filter.selector);
204 }
205
206 // If the number of conditional generic selectors that apply on this domain
207 // is the same as the total number of conditional generic selectors, the
208 // domain is "generic friendly". In that case, we mark it is as such for
209 // faster lookups.
210 if (conditionalGenericSelectors &&
211 genericSelectors.length == conditionalGenericSelectors.length)
212 {
213 if (genericFriendlyDomains.size >= 1000)
214 genericFriendlyDomains.clear();
215
216 genericFriendlyDomains.add(domain);
217 }
218
219 return selectors.concat(genericSelectors);
220 }
221
222 /**
223 * Returns a list of selectors that apply on any unknown domain
224 * @returns {string[]}
225 */
226 function getConditionalGenericSelectors()
227 {
228 if (conditionalGenericSelectors)
229 return conditionalGenericSelectors;
230
231 conditionalGenericSelectors = [];
232
233 let filters = filtersByDomain.get("");
234 if (!filters)
235 return conditionalGenericSelectors;
236
237 for (let {selector} of filters.keys())
238 {
239 if (genericExceptions.size == 0 || !genericExceptions.has(selector))
240 conditionalGenericSelectors.push(selector);
241 }
242
243 return conditionalGenericSelectors;
86 } 244 }
87 245
88 /** 246 /**
89 * Returns a list of selectors that apply on each website unconditionally. 247 * Returns a list of selectors that apply on each website unconditionally.
90 * @returns {string[]} 248 * @returns {string[]}
91 */ 249 */
92 function getUnconditionalSelectors() 250 function getUnconditionalSelectors()
93 { 251 {
94 if (!unconditionalSelectors) 252 if (!unconditionalSelectors)
95 unconditionalSelectors = [...filterBySelector.keys()]; 253 unconditionalSelectors = [...filterBySelector.keys()];
96 254
97 return unconditionalSelectors; 255 return unconditionalSelectors;
98 } 256 }
99 257
100 /** 258 /**
101 * Container for element hiding filters 259 * Container for element hiding filters
102 * @class 260 * @class
103 */ 261 */
104 let ElemHide = exports.ElemHide = { 262 let ElemHide = exports.ElemHide = {
105 /** 263 /**
106 * Removes all known filters 264 * Removes all known filters
107 */ 265 */
108 clear() 266 clear()
109 { 267 {
110 for (let collection of [filtersByDomain, filterBySelector, 268 for (let collection of [filtersByDomain, filterBySelector,
111 knownFilters, exceptions]) 269 knownFilters, exceptions,
270 genericExceptions, genericFriendlyDomains])
112 { 271 {
113 collection.clear(); 272 collection.clear();
114 } 273 }
115 unconditionalSelectors = null; 274 unconditionalSelectors = null;
275 conditionalGenericSelectors = null;
116 FilterNotifier.emit("elemhideupdate"); 276 FilterNotifier.emit("elemhideupdate");
117 }, 277 },
118 278
119 /** 279 /**
120 * Add a new element hiding filter 280 * Add a new element hiding filter
121 * @param {ElemHideBase} filter 281 * @param {ElemHideBase} filter
122 */ 282 */
123 add(filter) 283 add(filter)
124 { 284 {
125 if (knownFilters.has(filter)) 285 if (knownFilters.has(filter))
126 return; 286 return;
127 287
288 conditionalGenericSelectors = null;
289 genericFriendlyDomains.clear();
290
128 if (filter instanceof ElemHideException) 291 if (filter instanceof ElemHideException)
129 { 292 {
130 let {selector} = filter; 293 let {selector, domains} = filter;
294
131 let list = exceptions.get(selector); 295 let list = exceptions.get(selector);
132 if (list) 296 if (list)
133 list.push(filter); 297 list.push(filter);
134 else 298 else
135 exceptions.set(selector, [filter]); 299 exceptions.set(selector, [filter]);
136 300
301 if (domains)
302 addToFiltersByDomain(filter);
303
304 if (filter.isGeneric())
305 {
306 list = genericExceptions.get(selector);
307 if (list)
308 list.push(filter);
309 else
310 genericExceptions.set(selector, [filter]);
311 }
312
137 // If this is the first exception for a previously unconditionally 313 // If this is the first exception for a previously unconditionally
138 // applied element hiding selector we need to take care to update the 314 // applied element hiding selector we need to take care to update the
139 // lookups. 315 // lookups.
140 let unconditionalFilterForSelector = filterBySelector.get(selector); 316 let unconditionalFilterForSelector = filterBySelector.get(selector);
141 if (unconditionalFilterForSelector) 317 if (unconditionalFilterForSelector)
142 { 318 {
143 addToFiltersByDomain(unconditionalFilterForSelector); 319 addToFiltersByDomain(unconditionalFilterForSelector);
144 filterBySelector.delete(selector); 320 filterBySelector.delete(selector);
145 unconditionalSelectors = null; 321 unconditionalSelectors = null;
146 } 322 }
(...skipping 16 matching lines...) Expand all
163 339
164 /** 340 /**
165 * Removes an element hiding filter 341 * Removes an element hiding filter
166 * @param {ElemHideBase} filter 342 * @param {ElemHideBase} filter
167 */ 343 */
168 remove(filter) 344 remove(filter)
169 { 345 {
170 if (!knownFilters.has(filter)) 346 if (!knownFilters.has(filter))
171 return; 347 return;
172 348
349 conditionalGenericSelectors = null;
350 genericFriendlyDomains.clear();
351
173 // Whitelisting filters 352 // Whitelisting filters
174 if (filter instanceof ElemHideException) 353 if (filter instanceof ElemHideException)
175 { 354 {
176 let list = exceptions.get(filter.selector); 355 let list = exceptions.get(filter.selector);
177 let index = list.indexOf(filter); 356 let index = list.indexOf(filter);
178 if (index >= 0) 357 if (index >= 0)
179 list.splice(index, 1); 358 list.splice(index, 1);
359
360 if (filter.isGeneric())
361 {
362 list = genericExceptions.get(filter.selector);
363 index = list.indexOf(filter);
364 if (index >= 0)
365 list.splice(index, 1);
366
367 // It's important to delete the entry here so the selector no longer
368 // appears to have any generic exceptions.
369 if (list.length == 0)
370 genericExceptions.delete(filter.selector);
371 }
180 } 372 }
181 // Unconditially applied element hiding filters 373 // Unconditially applied element hiding filters
182 else if (filterBySelector.get(filter.selector) == filter) 374 else if (filterBySelector.get(filter.selector) == filter)
183 { 375 {
184 filterBySelector.delete(filter.selector); 376 filterBySelector.delete(filter.selector);
185 unconditionalSelectors = null; 377 unconditionalSelectors = null;
186 } 378 }
187 // Conditionally applied element hiding filters 379 // Conditionally applied element hiding filters
188 else 380 else
189 { 381 {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 * @param {number} [criteria] 445 * @param {number} [criteria]
254 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or 446 * One of the following: ElemHide.ALL_MATCHING, ElemHide.NO_UNCONDITIONAL or
255 * ElemHide.SPECIFIC_ONLY. 447 * ElemHide.SPECIFIC_ONLY.
256 * @returns {string[]} 448 * @returns {string[]}
257 * List of selectors. 449 * List of selectors.
258 */ 450 */
259 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING) 451 getSelectorsForDomain(domain, criteria = ElemHide.ALL_MATCHING)
260 { 452 {
261 let selectors = []; 453 let selectors = [];
262 454
263 let specificOnly = (criteria >= ElemHide.SPECIFIC_ONLY); 455 let specificOnly = criteria >= ElemHide.SPECIFIC_ONLY;
264 let excluded = new Set(); 456 let filtersList = getSpecificFiltersForDomain(domain);
265 let currentDomain = domain ? domain.toUpperCase() : "";
266 457
267 // This code is a performance hot-spot, which is why we've made certain 458 if (filtersList.length > 0)
268 // micro-optimisations. Please be careful before making changes.
269 while (true)
270 { 459 {
271 if (specificOnly && currentDomain == "") 460 let genericFilters = !specificOnly ? filtersByDomain.get("") : null;
272 break;
273 461
274 let filters = filtersByDomain.get(currentDomain); 462 selectors = getConditionalSelectorsForDomain(domain, filtersList,
275 if (filters) 463 genericFilters);
276 { 464 }
277 for (let [filter, isIncluded] of filters) 465 else if (!specificOnly)
278 { 466 {
279 if (!isIncluded) 467 selectors = getConditionalGenericSelectors();
280 {
281 excluded.add(filter);
282 }
283 else if ((excluded.size == 0 || !excluded.has(filter)) &&
284 !this.getException(filter, domain))
285 {
286 selectors.push(filter.selector);
287 }
288 }
289 }
290
291 if (currentDomain == "")
292 break;
293
294 let nextDot = currentDomain.indexOf(".");
295 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
296 } 468 }
297 469
298 if (criteria < ElemHide.NO_UNCONDITIONAL) 470 if (criteria < ElemHide.NO_UNCONDITIONAL)
299 selectors = getUnconditionalSelectors().concat(selectors); 471 selectors = getUnconditionalSelectors().concat(selectors);
300 472
473 // If the above logic leaves us with a reference to our internal cache of
474 // selectors, we make a copy here.
475 if (selectors == conditionalGenericSelectors)
476 selectors = selectors.slice();
477
301 return selectors; 478 return selectors;
302 } 479 }
303 }; 480 };
OLDNEW
« no previous file with comments | « no previous file | test/elemHide.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld