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