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