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

Side by Side Diff: lib/filterListener.js

Issue 29375915: Issue 4878 - Start using ESLint for adblockpluscore (Closed)
Patch Set: Rebased. Created Feb. 28, 2017, 3:55 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
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 /**
19 * @fileOverview Component synchronizing filter storage with Matcher instances a nd ElemHide.
20 */
21
22 "use strict"; 18 "use strict";
23 19
20 /**
21 * @fileOverview Component synchronizing filter storage with Matcher
22 * instances and ElemHide.
23 */
24
24 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 25 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
25 Cu.import("resource://gre/modules/Services.jsm"); 26 Cu.import("resource://gre/modules/Services.jsm");
26 27
27 let {FilterStorage} = require("filterStorage"); 28 const {FilterStorage} = require("filterStorage");
28 let {FilterNotifier} = require("filterNotifier"); 29 const {FilterNotifier} = require("filterNotifier");
29 let {ElemHide} = require("elemHide"); 30 const {ElemHide} = require("elemHide");
30 let {ElemHideEmulation} = require("elemHideEmulation"); 31 const {ElemHideEmulation} = require("elemHideEmulation");
31 let {defaultMatcher} = require("matcher"); 32 const {defaultMatcher} = require("matcher");
32 let {ActiveFilter, RegExpFilter, ElemHideBase, ElemHideEmulationFilter} = 33 const {ActiveFilter, RegExpFilter,
33 require("filterClasses"); 34 ElemHideBase, ElemHideEmulationFilter} = require("filterClasses");
34 let {Prefs} = require("prefs"); 35 const {Prefs} = require("prefs");
35 36
36 /** 37 /**
37 * Increases on filter changes, filters will be saved if it exceeds 1. 38 * Increases on filter changes, filters will be saved if it exceeds 1.
38 * @type Integer 39 * @type {number}
39 */ 40 */
40 let isDirty = 0; 41 let isDirty = 0;
41 42
42 /** 43 /**
43 * This object can be used to change properties of the filter change listeners. 44 * This object can be used to change properties of the filter change listeners.
44 * @class 45 * @class
45 */ 46 */
46 let FilterListener = 47 let FilterListener = {
47 {
48 /** 48 /**
49 * Increases "dirty factor" of the filters and calls FilterStorage.saveToDisk( ) 49 * Increases "dirty factor" of the filters and calls
50 * if it becomes 1 or more. Save is executed delayed to prevent multiple 50 * FilterStorage.saveToDisk() if it becomes 1 or more. Save is
51 * subsequent calls. If the parameter is 0 it forces saving filters if any 51 * executed delayed to prevent multiple subsequent calls. If the
52 * changes were recorded after the previous save. 52 * parameter is 0 it forces saving filters if any changes were
53 * recorded after the previous save.
54 * @param {number} factor
53 */ 55 */
54 setDirty: function(/**Integer*/ factor) 56 setDirty(factor)
55 { 57 {
56 if (factor == 0 && isDirty > 0) 58 if (factor == 0 && isDirty > 0)
57 isDirty = 1; 59 isDirty = 1;
58 else 60 else
59 isDirty += factor; 61 isDirty += factor;
60 if (isDirty >= 1) 62 if (isDirty >= 1)
61 { 63 {
62 isDirty = 0; 64 isDirty = 0;
63 FilterStorage.saveToDisk(); 65 FilterStorage.saveToDisk();
64 } 66 }
65 } 67 }
66 }; 68 };
67 69
68 /** 70 /**
69 * Observer listening to history purge actions. 71 * Observer listening to history purge actions.
70 * @class 72 * @class
71 */ 73 */
72 let HistoryPurgeObserver = 74 let HistoryPurgeObserver = {
73 { 75 observe(subject, topic, data)
74 observe: function(subject, topic, data)
75 { 76 {
76 if (topic == "browser:purge-session-history" && Prefs.clearStatsOnHistoryPur ge) 77 if (topic == "browser:purge-session-history" &&
78 Prefs.clearStatsOnHistoryPurge)
77 { 79 {
78 FilterStorage.resetHitCounts(); 80 FilterStorage.resetHitCounts();
79 FilterListener.setDirty(0); // Force saving to disk 81 FilterListener.setDirty(0); // Force saving to disk
80 82
81 Prefs.recentReports = []; 83 Prefs.recentReports = [];
82 } 84 }
83 }, 85 },
84 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse rver]) 86 QueryInterface: XPCOMUtils.generateQI(
87 [Ci.nsISupportsWeakReference, Ci.nsIObserver]
88 )
85 }; 89 };
86 90
87 /** 91 /**
88 * Initializes filter listener on startup, registers the necessary hooks. 92 * Initializes filter listener on startup, registers the necessary hooks.
89 */ 93 */
90 function init() 94 function init()
91 { 95 {
92 FilterNotifier.on("filter.hitCount", onFilterHitCount); 96 FilterNotifier.on("filter.hitCount", onFilterHitCount);
93 FilterNotifier.on("filter.lastHit", onFilterLastHit); 97 FilterNotifier.on("filter.lastHit", onFilterLastHit);
94 FilterNotifier.on("filter.added", onFilterAdded); 98 FilterNotifier.on("filter.added", onFilterAdded);
(...skipping 11 matching lines...) Expand all
106 FilterNotifier.on("subscription.homepage", onGenericChange); 110 FilterNotifier.on("subscription.homepage", onGenericChange);
107 FilterNotifier.on("subscription.downloadStatus", onGenericChange); 111 FilterNotifier.on("subscription.downloadStatus", onGenericChange);
108 FilterNotifier.on("subscription.lastCheck", onGenericChange); 112 FilterNotifier.on("subscription.lastCheck", onGenericChange);
109 FilterNotifier.on("subscription.errors", onGenericChange); 113 FilterNotifier.on("subscription.errors", onGenericChange);
110 114
111 FilterNotifier.on("load", onLoad); 115 FilterNotifier.on("load", onLoad);
112 FilterNotifier.on("save", onSave); 116 FilterNotifier.on("save", onSave);
113 117
114 FilterStorage.loadFromDisk(); 118 FilterStorage.loadFromDisk();
115 119
116 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history" , true); 120 Services.obs.addObserver(HistoryPurgeObserver,
117 onShutdown.add(function() 121 "browser:purge-session-history", true);
122 onShutdown.add(() =>
118 { 123 {
119 Services.obs.removeObserver(HistoryPurgeObserver, "browser:purge-session-his tory"); 124 Services.obs.removeObserver(HistoryPurgeObserver,
125 "browser:purge-session-history");
120 }); 126 });
121 } 127 }
122 init(); 128 init();
123 129
124 /** 130 /**
125 * Notifies Matcher instances or ElemHide object about a new filter 131 * Notifies Matcher instances or ElemHide object about a new filter
126 * if necessary. 132 * if necessary.
127 * @param {Filter} filter filter that has been added 133 * @param {Filter} filter filter that has been added
128 */ 134 */
129 function addFilter(filter) 135 function addFilter(filter)
130 { 136 {
131 if (!(filter instanceof ActiveFilter) || filter.disabled) 137 if (!(filter instanceof ActiveFilter) || filter.disabled)
132 return; 138 return;
133 139
134 let hasEnabled = false; 140 let hasEnabled = false;
135 for (let i = 0; i < filter.subscriptions.length; i++) 141 for (let i = 0; i < filter.subscriptions.length; i++)
142 {
136 if (!filter.subscriptions[i].disabled) 143 if (!filter.subscriptions[i].disabled)
137 hasEnabled = true; 144 hasEnabled = true;
145 }
138 if (!hasEnabled) 146 if (!hasEnabled)
139 return; 147 return;
140 148
141 if (filter instanceof RegExpFilter) 149 if (filter instanceof RegExpFilter)
142 defaultMatcher.add(filter); 150 defaultMatcher.add(filter);
143 else if (filter instanceof ElemHideBase) 151 else if (filter instanceof ElemHideBase)
144 { 152 {
145 if (filter instanceof ElemHideEmulationFilter) 153 if (filter instanceof ElemHideEmulationFilter)
146 ElemHideEmulation.add(filter); 154 ElemHideEmulation.add(filter);
147 else 155 else
148 ElemHide.add(filter); 156 ElemHide.add(filter);
149 } 157 }
150 } 158 }
151 159
152 /** 160 /**
153 * Notifies Matcher instances or ElemHide object about removal of a filter 161 * Notifies Matcher instances or ElemHide object about removal of a filter
154 * if necessary. 162 * if necessary.
155 * @param {Filter} filter filter that has been removed 163 * @param {Filter} filter filter that has been removed
156 */ 164 */
157 function removeFilter(filter) 165 function removeFilter(filter)
158 { 166 {
159 if (!(filter instanceof ActiveFilter)) 167 if (!(filter instanceof ActiveFilter))
160 return; 168 return;
161 169
162 if (!filter.disabled) 170 if (!filter.disabled)
163 { 171 {
164 let hasEnabled = false; 172 let hasEnabled = false;
165 for (let i = 0; i < filter.subscriptions.length; i++) 173 for (let i = 0; i < filter.subscriptions.length; i++)
174 {
166 if (!filter.subscriptions[i].disabled) 175 if (!filter.subscriptions[i].disabled)
167 hasEnabled = true; 176 hasEnabled = true;
177 }
168 if (hasEnabled) 178 if (hasEnabled)
169 return; 179 return;
170 } 180 }
171 181
172 if (filter instanceof RegExpFilter) 182 if (filter instanceof RegExpFilter)
173 defaultMatcher.remove(filter); 183 defaultMatcher.remove(filter);
174 else if (filter instanceof ElemHideBase) 184 else if (filter instanceof ElemHideBase)
175 { 185 {
176 if (filter instanceof ElemHideEmulationFilter) 186 if (filter instanceof ElemHideEmulationFilter)
177 ElemHideEmulation.remove(filter); 187 ElemHideEmulation.remove(filter);
(...skipping 12 matching lines...) Expand all
190 // filters applying there. We have ten prime numbers to use as iteration step, 200 // filters applying there. We have ten prime numbers to use as iteration step,
191 // any of those can be chosen as long as the array length isn't divisible by 201 // any of those can be chosen as long as the array length isn't divisible by
192 // it. 202 // it.
193 let len = filters.length; 203 let len = filters.length;
194 if (!len) 204 if (!len)
195 return; 205 return;
196 206
197 let current = (Math.random() * len) | 0; 207 let current = (Math.random() * len) | 0;
198 let step; 208 let step;
199 do 209 do
200 {
201 step = primes[(Math.random() * primes.length) | 0]; 210 step = primes[(Math.random() * primes.length) | 0];
202 } while (len % step == 0); 211 while (len % step == 0);
Wladimir Palant 2017/03/02 14:06:52 Ok, this is bad. I never even knew that you could
kzar 2017/03/08 12:33:36 Done.
203 212
204 for (let i = 0; i < len; i++, current = (current + step) % len) 213 for (let i = 0; i < len; i++, current = (current + step) % len)
205 addFilter(filters[current]); 214 addFilter(filters[current]);
206 } 215 }
207 216
208 function onSubscriptionAdded(subscription) 217 function onSubscriptionAdded(subscription)
209 { 218 {
210 FilterListener.setDirty(1); 219 FilterListener.setDirty(1);
211 220
212 if (!subscription.disabled) 221 if (!subscription.disabled)
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 } 300 }
292 301
293 function onLoad() 302 function onLoad()
294 { 303 {
295 isDirty = 0; 304 isDirty = 0;
296 305
297 defaultMatcher.clear(); 306 defaultMatcher.clear();
298 ElemHide.clear(); 307 ElemHide.clear();
299 ElemHideEmulation.clear(); 308 ElemHideEmulation.clear();
300 for (let subscription of FilterStorage.subscriptions) 309 for (let subscription of FilterStorage.subscriptions)
310 {
301 if (!subscription.disabled) 311 if (!subscription.disabled)
302 addFilters(subscription.filters); 312 addFilters(subscription.filters);
313 }
303 } 314 }
304 315
305 function onSave() 316 function onSave()
306 { 317 {
307 isDirty = 0; 318 isDirty = 0;
308 } 319 }
OLDNEW

Powered by Google App Engine
This is Rietveld