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

Delta Between Two Patch Sets: lib/elemHideExceptions.js

Issue 29882558: Issue 6955 - Avoid making copies of common selector list (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Cache common selector list Created Sept. 17, 2018, 9:20 a.m.
Right Patch Set: Keep track of exceptions per domain Created Sept. 17, 2018, 12:15 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « lib/elemHide.js ('k') | test/elemHide.js » ('j') | test/elemHide.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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
(...skipping 13 matching lines...) Expand all
24 const {EventEmitter} = require("./events"); 24 const {EventEmitter} = require("./events");
25 const {filterNotifier} = require("./filterNotifier"); 25 const {filterNotifier} = require("./filterNotifier");
26 26
27 /** 27 /**
28 * Lookup table, lists of element hiding exceptions by selector 28 * Lookup table, lists of element hiding exceptions by selector
29 * @type {Map.<string,ElemHideException[]>} 29 * @type {Map.<string,ElemHideException[]>}
30 */ 30 */
31 let exceptions = new Map(); 31 let exceptions = new Map();
32 32
33 /** 33 /**
34 * Number of element hiding exceptions per domain.
35 * @type {Map.<string,number>}
36 */
37 let exceptionCountByDomain = new Map();
38
39 /**
34 * Set containing known element exceptions 40 * Set containing known element exceptions
35 * @type {Set.<ElemHideException>} 41 * @type {Set.<ElemHideException>}
36 */ 42 */
37 let knownExceptions = new Set(); 43 let knownExceptions = new Set();
38 44
39 /** 45 /**
40 * Container for element hiding exceptions 46 * Container for element hiding exceptions
41 * @class 47 * @class
42 */ 48 */
43 exports.ElemHideExceptions = Object.assign(Object.create(new EventEmitter()), { 49 exports.ElemHideExceptions = Object.assign(Object.create(new EventEmitter()), {
44 /** 50 /**
45 * Removes all known exceptions 51 * Removes all known exceptions
46 */ 52 */
47 clear() 53 clear()
48 { 54 {
49 exceptions.clear(); 55 exceptions.clear();
56 exceptionCountByDomain.clear();
50 knownExceptions.clear(); 57 knownExceptions.clear();
51 58
52 filterNotifier.emit("elemhideupdate"); 59 filterNotifier.emit("elemhideupdate");
53 }, 60 },
54 61
55 /** 62 /**
56 * Add a new element hiding exception 63 * Add a new element hiding exception
57 * @param {ElemHideException} exception 64 * @param {ElemHideException} exception
58 */ 65 */
59 add(exception) 66 add(exception)
60 { 67 {
61 if (knownExceptions.has(exception)) 68 if (knownExceptions.has(exception))
62 return; 69 return;
63 70
64 let {selector} = exception; 71 let {selector} = exception;
65 let list = exceptions.get(selector); 72 let list = exceptions.get(selector);
66 if (list) 73 if (list)
67 list.push(exception); 74 list.push(exception);
68 else 75 else
69 exceptions.set(selector, [exception]); 76 exceptions.set(selector, [exception]);
77
78 for (let [domain] of exception.domains || [])
79 {
80 if (domain == "")
81 continue;
82
83 let count = exceptionCountByDomain.get(domain) || 0;
84 exceptionCountByDomain.set(domain, count + 1);
85 }
70 86
71 knownExceptions.add(exception); 87 knownExceptions.add(exception);
72 88
73 this.emit("added", exception); 89 this.emit("added", exception);
74 90
75 filterNotifier.emit("elemhideupdate"); 91 filterNotifier.emit("elemhideupdate");
76 }, 92 },
77 93
78 /** 94 /**
79 * Removes an element hiding exception 95 * Removes an element hiding exception
80 * @param {ElemHideException} exception 96 * @param {ElemHideException} exception
81 */ 97 */
82 remove(exception) 98 remove(exception)
83 { 99 {
84 if (!knownExceptions.has(exception)) 100 if (!knownExceptions.has(exception))
85 return; 101 return;
86 102
87 let list = exceptions.get(exception.selector); 103 let list = exceptions.get(exception.selector);
88 let index = list.indexOf(exception); 104 let index = list.indexOf(exception);
89 if (index >= 0) 105 if (index >= 0)
90 list.splice(index, 1); 106 list.splice(index, 1);
91 107
108 for (let [domain] of exception.domains || [])
109 {
110 if (domain == "")
111 continue;
112
113 let count = exceptionCountByDomain.get(domain) || 0;
114 if (count == 1)
115 exceptionCountByDomain.delete(domain);
116 else if (count > 1)
117 exceptionCountByDomain.set(domain, count - 1);
118 }
119
92 knownExceptions.delete(exception); 120 knownExceptions.delete(exception);
93 121
94 this.emit("removed", exception); 122 this.emit("removed", exception);
95 123
96 filterNotifier.emit("elemhideupdate"); 124 filterNotifier.emit("elemhideupdate");
97 }, 125 },
98 126
99 /** 127 /**
100 * Checks whether any exception rules are registered for a selector 128 * Checks whether any exception rules are registered for a selector
101 * @param {string} selector 129 * @param {string} selector
102 * @returns {boolean} 130 * @returns {boolean}
103 */ 131 */
104 hasExceptions(selector) 132 hasExceptions(selector)
105 { 133 {
106 return exceptions.has(selector); 134 return exceptions.has(selector);
135 },
136
137 /**
138 * Checks whether any exception rules are registered for a domain
139 * @param {string} domain
140 * @returns {boolean}
141 */
142 domainHasExceptions(domain)
143 {
144 return exceptionCountByDomain.has(domain);
107 }, 145 },
108 146
109 /** 147 /**
110 * Checks whether an exception rule is registered for a selector on a 148 * Checks whether an exception rule is registered for a selector on a
111 * particular domain. 149 * particular domain.
112 * @param {string} selector 150 * @param {string} selector
113 * @param {?string} docDomain 151 * @param {?string} docDomain
114 * @return {?ElemHideException} 152 * @return {?ElemHideException}
115 */ 153 */
116 getException(selector, docDomain) 154 getException(selector, docDomain)
117 { 155 {
118 let list = exceptions.get(selector); 156 let list = exceptions.get(selector);
119 if (!list) 157 if (!list)
120 return null; 158 return null;
121 159
122 for (let i = list.length - 1; i >= 0; i--) 160 for (let i = list.length - 1; i >= 0; i--)
123 { 161 {
124 if (list[i].isActiveOnDomain(docDomain)) 162 if (list[i].isActiveOnDomain(docDomain))
125 return list[i]; 163 return list[i];
126 } 164 }
127 165
128 return null; 166 return null;
129 } 167 }
130 }); 168 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld