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