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

Side by Side Diff: lib/elemHide.js

Issue 30007559: Issue 7285 - Abstract caching logic (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Feb. 14, 2019, 5:36 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
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 {ElemHideExceptions} = require("./elemHideExceptions"); 24 const {ElemHideExceptions} = require("./elemHideExceptions");
25 const {filterNotifier} = require("./filterNotifier"); 25 const {filterNotifier} = require("./filterNotifier");
26 const {normalizeHostname, suffixes} = require("./domain"); 26 const {normalizeHostname, suffixes} = require("./domain");
27 const {Cache} = require("./caching");
27 28
28 /** 29 /**
29 * The maximum number of selectors in a CSS rule. This is used by 30 * The maximum number of selectors in a CSS rule. This is used by
30 * <code>{@link createStyleSheet}</code> to split up a long list of selectors 31 * <code>{@link createStyleSheet}</code> to split up a long list of selectors
31 * into multiple rules. 32 * into multiple rules.
32 * @const {number} 33 * @const {number}
33 * @default 34 * @default
34 */ 35 */
35 const selectorGroupSize = 1024; 36 const selectorGroupSize = 1024;
36 37
37 /** 38 /**
38 * The maximum number of entries to keep in
39 * <code>{@link styleSheetCache}</code>.
40 */
41 const maxStyleSheetCacheEntries = 100;
42
43 /**
44 * Lookup table, active flag, by filter by domain. 39 * Lookup table, active flag, by filter by domain.
45 * (Only contains filters that aren't unconditionally matched for all domains.) 40 * (Only contains filters that aren't unconditionally matched for all domains.)
46 * @type {Map.<string,Map.<Filter,boolean>>} 41 * @type {Map.<string,Map.<Filter,boolean>>}
47 */ 42 */
48 let filtersByDomain = new Map(); 43 let filtersByDomain = new Map();
49 44
50 /** 45 /**
51 * Lookup table, filter by selector. (Only used for selectors that are 46 * Lookup table, filter by selector. (Only used for selectors that are
52 * unconditionally matched for all domains.) 47 * unconditionally matched for all domains.)
53 * @type {Map.<string,Filter>} 48 * @type {Map.<string,Filter>}
(...skipping 21 matching lines...) Expand all
75 * on selectors from all generic filters that are not in the 70 * on selectors from all generic filters that are not in the
76 * <code>{@link unconditionalSelectors}</code> list. 71 * <code>{@link unconditionalSelectors}</code> list.
77 * @type {?string} 72 * @type {?string}
78 */ 73 */
79 let commonStyleSheet = null; 74 let commonStyleSheet = null;
80 75
81 /** 76 /**
82 * Cache of generated domain-specific style sheets. This contains entries for 77 * Cache of generated domain-specific style sheets. This contains entries for
83 * only known domains. If a domain is unknown, it gets 78 * only known domains. If a domain is unknown, it gets
84 * <code>{@link commonStyleSheet}</code>. 79 * <code>{@link commonStyleSheet}</code>.
85 * @type {Map.<string,string>} 80 * @type {Cache.<string, string>}
86 */ 81 */
87 let styleSheetCache = new Map(); 82 let styleSheetCache = new Cache(100);
88 83
89 /** 84 /**
90 * Map to be used instead when a filter has a blank domains property. 85 * Map to be used instead when a filter has a blank domains property.
91 * @type {Map.<string,boolean>} 86 * @type {Map.<string,boolean>}
92 * @const 87 * @const
93 */ 88 */
94 let defaultDomains = new Map([["", true]]); 89 let defaultDomains = new Map([["", true]]);
95 90
96 /** 91 /**
97 * Set containing known element hiding filters 92 * Set containing known element hiding filters
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 return commonStyleSheet; 220 return commonStyleSheet;
226 } 221 }
227 222
228 /** 223 /**
229 * Returns the domain-specific style sheet that applies on a given domain. 224 * Returns the domain-specific style sheet that applies on a given domain.
230 * @param {string} domain 225 * @param {string} domain
231 * @returns {string} 226 * @returns {string}
232 */ 227 */
233 function getDomainSpecificStyleSheet(domain) 228 function getDomainSpecificStyleSheet(domain)
234 { 229 {
235 let styleSheet = styleSheetCache.get(domain); 230 let styleSheet = styleSheetCache.read(domain);
236 231
237 if (typeof styleSheet == "undefined") 232 if (typeof styleSheet == "undefined")
238 { 233 {
239 styleSheet = createStyleSheet(getConditionalSelectors(domain, false)); 234 styleSheet = createStyleSheet(getConditionalSelectors(domain, false));
240 235 styleSheetCache.save(domain, styleSheet);
241 if (styleSheetCache.size >= maxStyleSheetCacheEntries)
242 styleSheetCache.clear();
243
244 styleSheetCache.set(domain, styleSheet);
245 } 236 }
246 237
247 return styleSheet; 238 return styleSheet;
248 } 239 }
249 240
250 ElemHideExceptions.on("added", ({domains, selector}) => 241 ElemHideExceptions.on("added", ({domains, selector}) =>
251 { 242 {
252 styleSheetCache.clear(); 243 styleSheetCache.clear();
253 commonStyleSheet = null; 244 commonStyleSheet = null;
254 245
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
503 { 494 {
504 let styleSheet = ""; 495 let styleSheet = "";
505 496
506 for (let selectorGroup of splitSelectors(selectors)) 497 for (let selectorGroup of splitSelectors(selectors))
507 styleSheet += createRule(selectorGroup); 498 styleSheet += createRule(selectorGroup);
508 499
509 return styleSheet; 500 return styleSheet;
510 } 501 }
511 502
512 exports.createStyleSheet = createStyleSheet; 503 exports.createStyleSheet = createStyleSheet;
OLDNEW
« lib/caching.js ('K') | « lib/caching.js ('k') | lib/matcher.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld