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