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 Created May 15, 2018, 3:35 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 18 matching lines...) Expand all
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 {
79 continue; 98 // Add an entry for each domain, but without any filters. This makes
80 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 /**
123 * Checks whether a filter applies on a domain
124 * @param {Filter} filter
125 * @param {string} [domain]
126 * @param {Set.<Filter>} excludeSet
127 * @returns {boolean}
128 */
129 function doesFilterApply(filter, domain, excludeSet)
130 {
131 return (excludeSet.size == 0 || !excludeSet.has(filter)) &&
132 !exports.ElemHide.getException(filter, domain);
133 }
134
135 /**
136 * Returns a list of domain-specific filters matching a domain
137 * @param {string} [domain]
138 * @returns {Array.<{domain: string, filters: ?Map.<Filter,boolean>}>}
139 */
140 function getSpecificFiltersForDomain(domain)
141 {
142 let filtersList = [];
143
144 if (domain)
145 domain = domain.toUpperCase();
146
147 while (domain)
148 {
81 let filters = filtersByDomain.get(domain); 149 let filters = filtersByDomain.get(domain);
82 if (!filters) 150 if (typeof filters != "undefined")
83 filtersByDomain.set(domain, filters = new Map()); 151 filtersList.push({domain, filters});
84 filters.set(filter, isIncluded); 152
85 } 153 let nextDot = domain.indexOf(".");
154 domain = nextDot == -1 ? null : domain.substring(nextDot + 1);
155 }
156
157 return filtersList;
158 }
159
160 /**
161 * Returns a list of selectors that apply on a domain from a given list of
162 * filters
163 * @param {string} [domain]
164 * @param {Array.<{domain: string, filters: ?Map.<Filter,boolean>}>} filtersList
165 * @param {Set.<Filter>} excludeSet
166 * @returns {string[]}
167 */
168 function matchSelectors(domain, filtersList, excludeSet)
169 {
170 let matches = [];
171
172 // This code is a performance hot-spot, which is why we've made certain
173 // micro-optimisations. Please be careful before making changes.
174 for (let i = 0; i < filtersList.length; i++)
175 {
176 let {filters} = filtersList[i];
177 if (filters)
178 {
179 for (let [filter, isIncluded] of filters)
180 {
181 if (!isIncluded)
182 excludeSet.add(filter);
183 else if (doesFilterApply(filter, domain, excludeSet))
184 matches.push(filter.selector);
185 }
186 }
187 }
188
189 return matches;
190 }
191
192 /**
193 * Returns a list of selectors that apply on a domain
194 * @param {string} [domain]
195 * @param {boolean} specificOnly
196 * @returns {string[]}
197 */
198 function getConditionalSelectorsForDomain(domain, specificOnly)
199 {
200 let specificFilters = getSpecificFiltersForDomain(domain);
201
202 // If there are no specific filters (nor any specific exceptions), we can
203 // just return the selectors from all the generic filters modulo any generic
204 // exceptions.
205 if (specificFilters.length == 0)
206 return specificOnly ? [] : getConditionalGenericSelectors();
207
208 let excludeSet = new Set();
209 let specificSelectors = matchSelectors(domain, specificFilters, excludeSet);
210
211 if (specificOnly)
212 return specificSelectors;
213
214 // We use the longest subdomain of this domain found in our data structures
215 // as the key to check if the domain is "generic friendly." For example,
216 // given foo.example.com, there may be an entry for example.com in our data
217 // structures (e.g. "example.com###foo"), so we use that subdomain as the
218 // key. This way we make only one entry and it works for all subdomains of
219 // example.com, except those that have specific entries
220 // (e.g. "~bar.example.com##.no-bar").
221 let domainKey = specificFilters[0].domain;
222
223 if (genericFriendlyDomains.has(domainKey))
224 return specificSelectors.concat(getConditionalGenericSelectors());
225
226 let genericFilters = [{filters: filtersByDomain.get("")}];
227 let genericSelectors = matchSelectors(domain, genericFilters, excludeSet);
228
229 // If the number of conditional generic selectors that apply on this domain
230 // is the same as the total number of conditional generic selectors, the
231 // domain is "generic friendly" (i.e. all generic filters apply, except those
232 // with generic exceptions). In that case, we mark it is as such for faster
233 // lookups.
234 if (genericSelectors.length == (conditionalGenericSelectors || {}).length)
235 genericFriendlyDomains.add(domainKey);
236
237 return specificSelectors.concat(genericSelectors);
238 }
239
240 /**
241 * Returns a list of selectors that apply on any unknown domain
242 * @returns {string[]}
243 */
244 function getConditionalGenericSelectors()
245 {
246 if (conditionalGenericSelectors)
247 return conditionalGenericSelectors;
248
249 conditionalGenericSelectors = [];
250
251 let filters = filtersByDomain.get("");
252 if (!filters)
253 return conditionalGenericSelectors;
254
255 for (let {selector} of filters.keys())
256 {
257 if (genericExceptions.size == 0 || !genericExceptions.has(selector))
258 conditionalGenericSelectors.push(selector);
259 }
260
261 return conditionalGenericSelectors;
86 } 262 }
87 263
88 /** 264 /**
89 * Returns a list of selectors that apply on each website unconditionally. 265 * Returns a list of selectors that apply on each website unconditionally.
90 * @returns {string[]} 266 * @returns {string[]}
91 */ 267 */
92 function getUnconditionalSelectors() 268 function getUnconditionalSelectors()
93 { 269 {
94 if (!unconditionalSelectors) 270 if (!unconditionalSelectors)
95 unconditionalSelectors = [...filterBySelector.keys()]; 271 unconditionalSelectors = [...filterBySelector.keys()];
96 272
97 return unconditionalSelectors; 273 return unconditionalSelectors;
98 } 274 }
99 275
100 /** 276 /**
101 * Container for element hiding filters 277 * Container for element hiding filters
102 * @class 278 * @class
103 */ 279 */
104 exports.ElemHide = { 280 exports.ElemHide = {
105 /** 281 /**
106 * Removes all known filters 282 * Removes all known filters
107 */ 283 */
108 clear() 284 clear()
109 { 285 {
110 for (let collection of [filtersByDomain, filterBySelector, 286 for (let collection of [filtersByDomain, filterBySelector,
111 knownFilters, exceptions]) 287 knownFilters, exceptions,
288 genericExceptions, genericFriendlyDomains])
112 { 289 {
113 collection.clear(); 290 collection.clear();
114 } 291 }
115 unconditionalSelectors = null; 292 unconditionalSelectors = null;
293 conditionalGenericSelectors = null;
116 FilterNotifier.emit("elemhideupdate"); 294 FilterNotifier.emit("elemhideupdate");
117 }, 295 },
118 296
119 /** 297 /**
120 * Add a new element hiding filter 298 * Add a new element hiding filter
121 * @param {ElemHideBase} filter 299 * @param {ElemHideBase} filter
122 */ 300 */
123 add(filter) 301 add(filter)
124 { 302 {
125 if (knownFilters.has(filter)) 303 if (knownFilters.has(filter))
126 return; 304 return;
127 305
306 conditionalGenericSelectors = null;
307 genericFriendlyDomains.clear();
308
128 if (filter instanceof ElemHideException) 309 if (filter instanceof ElemHideException)
129 { 310 {
130 let {selector} = filter; 311 let {selector, domains} = filter;
312
131 let list = exceptions.get(selector); 313 let list = exceptions.get(selector);
132 if (list) 314 if (list)
133 list.push(filter); 315 list.push(filter);
134 else 316 else
135 exceptions.set(selector, [filter]); 317 exceptions.set(selector, [filter]);
136 318
319 if (domains)
320 addToFiltersByDomain(filter);
321
322 if (filter.isGeneric())
323 {
324 list = genericExceptions.get(selector);
325 if (list)
326 list.push(filter);
327 else
328 genericExceptions.set(selector, [filter]);
329 }
330
137 // If this is the first exception for a previously unconditionally 331 // If this is the first exception for a previously unconditionally
138 // applied element hiding selector we need to take care to update the 332 // applied element hiding selector we need to take care to update the
139 // lookups. 333 // lookups.
140 let unconditionalFilterForSelector = filterBySelector.get(selector); 334 let unconditionalFilterForSelector = filterBySelector.get(selector);
141 if (unconditionalFilterForSelector) 335 if (unconditionalFilterForSelector)
142 { 336 {
143 addToFiltersByDomain(unconditionalFilterForSelector); 337 addToFiltersByDomain(unconditionalFilterForSelector);
144 filterBySelector.delete(selector); 338 filterBySelector.delete(selector);
145 unconditionalSelectors = null; 339 unconditionalSelectors = null;
146 } 340 }
(...skipping 16 matching lines...) Expand all
163 357
164 /** 358 /**
165 * Removes an element hiding filter 359 * Removes an element hiding filter
166 * @param {ElemHideBase} filter 360 * @param {ElemHideBase} filter
167 */ 361 */
168 remove(filter) 362 remove(filter)
169 { 363 {
170 if (!knownFilters.has(filter)) 364 if (!knownFilters.has(filter))
171 return; 365 return;
172 366
367 conditionalGenericSelectors = null;
368 genericFriendlyDomains.clear();
369
173 // Whitelisting filters 370 // Whitelisting filters
174 if (filter instanceof ElemHideException) 371 if (filter instanceof ElemHideException)
175 { 372 {
176 let list = exceptions.get(filter.selector); 373 let list = exceptions.get(filter.selector);
177 let index = list.indexOf(filter); 374 let index = list.indexOf(filter);
178 if (index >= 0) 375 if (index >= 0)
179 list.splice(index, 1); 376 list.splice(index, 1);
377
378 if (filter.isGeneric())
379 {
380 list = genericExceptions.get(filter.selector);
381 index = list.indexOf(filter);
382 if (index >= 0)
383 list.splice(index, 1);
384
385 // It's important to delete the entry here so the selector no longer
386 // appears to have any generic exceptions.
387 if (list.length == 0)
388 genericExceptions.delete(filter.selector);
389 }
180 } 390 }
181 // Unconditially applied element hiding filters 391 // Unconditially applied element hiding filters
182 else if (filterBySelector.get(filter.selector) == filter) 392 else if (filterBySelector.get(filter.selector) == filter)
183 { 393 {
184 filterBySelector.delete(filter.selector); 394 filterBySelector.delete(filter.selector);
185 unconditionalSelectors = null; 395 unconditionalSelectors = null;
186 } 396 }
187 // Conditionally applied element hiding filters 397 // Conditionally applied element hiding filters
188 else 398 else
189 { 399 {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 434
225 /** 435 /**
226 * Determines from the current filter list which selectors should be applied 436 * Determines from the current filter list which selectors should be applied
227 * on a particular host name. 437 * on a particular host name.
228 * @param {string} domain 438 * @param {string} domain
229 * @param {boolean} [specificOnly] true if generic filters should not apply. 439 * @param {boolean} [specificOnly] true if generic filters should not apply.
230 * @returns {string[]} List of selectors. 440 * @returns {string[]} List of selectors.
231 */ 441 */
232 getSelectorsForDomain(domain, specificOnly = false) 442 getSelectorsForDomain(domain, specificOnly = false)
233 { 443 {
234 let selectors = []; 444 let selectors = getConditionalSelectorsForDomain(domain, specificOnly);
235
236 let excluded = new Set();
237 let currentDomain = domain ? domain.toUpperCase() : "";
238
239 // This code is a performance hot-spot, which is why we've made certain
240 // micro-optimisations. Please be careful before making changes.
241 while (true)
242 {
243 if (specificOnly && currentDomain == "")
244 break;
245
246 let filters = filtersByDomain.get(currentDomain);
247 if (filters)
248 {
249 for (let [filter, isIncluded] of filters)
250 {
251 if (!isIncluded)
252 {
253 excluded.add(filter);
254 }
255 else if ((excluded.size == 0 || !excluded.has(filter)) &&
256 !this.getException(filter, domain))
257 {
258 selectors.push(filter.selector);
259 }
260 }
261 }
262
263 if (currentDomain == "")
264 break;
265
266 let nextDot = currentDomain.indexOf(".");
267 currentDomain = nextDot == -1 ? "" : currentDomain.substr(nextDot + 1);
268 }
269 445
270 if (!specificOnly) 446 if (!specificOnly)
271 selectors = getUnconditionalSelectors().concat(selectors); 447 selectors = getUnconditionalSelectors().concat(selectors);
272 448
273 return selectors; 449 return selectors;
274 } 450 }
275 }; 451 };
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