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

Side by Side Diff: lib/elemHideEmulation.js

Issue 29780560: Issue 6665 - Optimize element hiding emulation filter lookups (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Use String.indexOf for subdomains Created May 14, 2018, 2:22 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 | « lib/common.js ('k') | test/filterListener.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 emulation implementation. 21 * @fileOverview Element hiding emulation implementation.
22 */ 22 */
23 23
24 const {ElemHide} = require("./elemHide"); 24 const {ElemHide} = require("./elemHide");
25 const {Filter} = require("./filterClasses"); 25 const {subdomains} = require("./common");
26 26
27 let filters = new Set(); 27 /**
28 * Map of element hiding emulation filters by domain
29 * @type {Map.<string,Map.<ElemHideEmulationFilter,boolean>>}
30 */
31 let filtersByDomain = new Map();
32
33 /**
34 * Set containing known element hiding emulation filters
35 * @type {Set.<ElemHideEmulation>}
36 */
37 let knownFilters = new Set();
38
39 /**
40 * Yields the domains associated with a filter, along with whether the filter
41 * should be included on the domain and the set of filters associated with the
42 * domain
43 * @param {ElemHideEmulationFilter} filter
44 * @yields {Array.<string,boolean,?Set.<ElemHideEmulationFilter>>}
45 */
46 function* filterDomains(filter)
47 {
48 for (let [domain, include] of filter.domains || [])
49 {
50 if (domain != "")
51 yield [domain, include, filtersByDomain.get(domain)];
52 }
53 }
54
55 /**
56 * Yields the filters for a domain and its subdomains, along with whether the
57 * filter should be included on its corresponding domain
58 * @param {string} domain
59 * @yields {Array.<ElemHideEmulationFilter,boolean>}
60 */
61 function* filtersForDomain(domain)
62 {
63 yield* filtersByDomain.get(domain) || [];
64
65 for (let subdomain of subdomains(domain))
66 yield* filtersByDomain.get(subdomain) || [];
67 }
28 68
29 /** 69 /**
30 * Container for element hiding emulation filters 70 * Container for element hiding emulation filters
31 * @class 71 * @class
32 */ 72 */
33 let ElemHideEmulation = { 73 let ElemHideEmulation = {
34 /** 74 /**
35 * Removes all known filters 75 * Removes all known filters
36 */ 76 */
37 clear() 77 clear()
38 { 78 {
39 filters.clear(); 79 filtersByDomain.clear();
40 }, 80 },
41 81
42 /** 82 /**
43 * Add a new element hiding emulation filter 83 * Add a new element hiding emulation filter
44 * @param {ElemHideEmulationFilter} filter 84 * @param {ElemHideEmulationFilter} filter
45 */ 85 */
46 add(filter) 86 add(filter)
47 { 87 {
48 filters.add(filter.text); 88 if (knownFilters.has(filter))
89 return;
90
91 for (let [domain, include, filters] of filterDomains(filter))
92 {
93 if (filters)
94 filters.set(filter, include);
95 else
96 filtersByDomain.set(domain, new Map([[filter, include]]));
97 }
98
99 knownFilters.add(filter);
49 }, 100 },
50 101
51 /** 102 /**
52 * Removes an element hiding emulation filter 103 * Removes an element hiding emulation filter
53 * @param {ElemHideEmulationFilter} filter 104 * @param {ElemHideEmulationFilter} filter
54 */ 105 */
55 remove(filter) 106 remove(filter)
56 { 107 {
57 filters.delete(filter.text); 108 if (!knownFilters.has(filter))
109 return;
110
111 for (let [domain, , filters] of filterDomains(filter))
112 {
113 if (filters)
114 {
115 filters.delete(filter);
116
117 if (filters.size == 0)
118 filtersByDomain.delete(domain);
119 }
120 }
121
122 knownFilters.delete(filter);
58 }, 123 },
59 124
60 /** 125 /**
61 * Returns a list of all rules active on a particular domain 126 * Returns a list of all rules active on a particular domain
62 * @param {string} domain 127 * @param {string} domain
63 * @return {ElemHideEmulationFilter[]} 128 * @return {ElemHideEmulationFilter[]}
64 */ 129 */
65 getRulesForDomain(domain) 130 getRulesForDomain(domain)
66 { 131 {
67 let result = []; 132 let result = [];
68 for (let text of filters.values()) 133
134 let excludeSet = new Set();
135 for (let [filter, include] of filtersForDomain(domain.toUpperCase()))
69 { 136 {
70 let filter = Filter.fromText(text); 137 if (!include)
71 if (filter.isActiveOnDomain(domain) && 138 {
72 !ElemHide.getException(filter, domain)) 139 excludeSet.add(filter);
140 }
141 else if ((excludeSet.size == 0 || !excludeSet.has(filter)) &&
142 !ElemHide.getException(filter, domain))
73 { 143 {
74 result.push(filter); 144 result.push(filter);
75 } 145 }
76 } 146 }
147
77 return result; 148 return result;
78 } 149 }
79 }; 150 };
80 exports.ElemHideEmulation = ElemHideEmulation; 151 exports.ElemHideEmulation = ElemHideEmulation;
OLDNEW
« no previous file with comments | « lib/common.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld