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 /** |
19 * @fileOverview Component synchronizing filter storage with Matcher instances a nd ElemHide. | 19 * @fileOverview Component synchronizing filter storage with Matcher instances a nd ElemHide. |
20 */ | 20 */ |
21 | 21 |
22 "use strict"; | |
23 | |
22 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); | 24 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
23 Cu.import("resource://gre/modules/Services.jsm"); | 25 Cu.import("resource://gre/modules/Services.jsm"); |
24 | 26 |
25 let {FilterStorage} = require("filterStorage"); | 27 let {FilterStorage} = require("filterStorage"); |
26 let {FilterNotifier} = require("filterNotifier"); | 28 let {FilterNotifier} = require("filterNotifier"); |
27 let {ElemHide} = require("elemHide"); | 29 let {ElemHide} = require("elemHide"); |
28 let {CSSRules} = require("cssRules"); | 30 let {CSSRules} = require("cssRules"); |
29 let {defaultMatcher} = require("matcher"); | 31 let {defaultMatcher} = require("matcher"); |
30 let {ActiveFilter, RegExpFilter, ElemHideBase, CSSPropertyFilter} = | 32 let {ActiveFilter, RegExpFilter, ElemHideBase, CSSPropertyFilter} = |
31 require("filterClasses"); | 33 require("filterClasses"); |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 } | 102 } |
101 }, | 103 }, |
102 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse rver]) | 104 QueryInterface: XPCOMUtils.generateQI([Ci.nsISupportsWeakReference, Ci.nsIObse rver]) |
103 }; | 105 }; |
104 | 106 |
105 /** | 107 /** |
106 * Initializes filter listener on startup, registers the necessary hooks. | 108 * Initializes filter listener on startup, registers the necessary hooks. |
107 */ | 109 */ |
108 function init() | 110 function init() |
109 { | 111 { |
110 FilterNotifier.addListener(function(action, item, newValue, oldValue) | 112 FilterNotifier.on("filter.hitCount", onFilterHitCount); |
111 { | 113 FilterNotifier.on("filter.lastHit", onFilterLastHit); |
112 let match = /^(\w+)\.(.*)/.exec(action); | 114 FilterNotifier.on("filter.added", onFilterAdded); |
113 if (match && match[1] == "filter") | 115 FilterNotifier.on("filter.removed", onFilterRemoved); |
114 onFilterChange(match[2], item, newValue, oldValue); | 116 FilterNotifier.on("filter.disabled", onFilterDisabled); |
115 else if (match && match[1] == "subscription") | 117 |
116 onSubscriptionChange(match[2], item, newValue, oldValue); | 118 FilterNotifier.on("subscription.added", onSubscriptionAdded); |
117 else | 119 FilterNotifier.on("subscription.removed", onSubscriptionRemoved); |
118 onGenericChange(action, item); | 120 FilterNotifier.on("subscription.disabled", onSubscriptionDisabled); |
119 }); | 121 FilterNotifier.on("subscription.updated", onSubscriptionUpdated); |
122 | |
123 FilterNotifier.on("load", onLoad); | |
124 FilterNotifier.on("save", onSave); | |
125 | |
120 | 126 |
121 if ("nsIStyleSheetService" in Ci) | 127 if ("nsIStyleSheetService" in Ci) |
122 ElemHide.init(); | 128 ElemHide.init(); |
123 else | 129 else |
124 flushElemHide = function() {}; // No global stylesheet in Chrome & Co. | 130 flushElemHide = function() {}; // No global stylesheet in Chrome & Co. |
125 FilterStorage.loadFromDisk(); | 131 FilterStorage.loadFromDisk(); |
126 | 132 |
127 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history" , true); | 133 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history" , true); |
128 onShutdown.add(function() | 134 onShutdown.add(function() |
129 { | 135 { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 defaultMatcher.remove(filter); | 199 defaultMatcher.remove(filter); |
194 else if (filter instanceof ElemHideBase) | 200 else if (filter instanceof ElemHideBase) |
195 { | 201 { |
196 if (filter instanceof CSSPropertyFilter) | 202 if (filter instanceof CSSPropertyFilter) |
197 CSSRules.remove(filter); | 203 CSSRules.remove(filter); |
198 else | 204 else |
199 ElemHide.remove(filter); | 205 ElemHide.remove(filter); |
200 } | 206 } |
201 } | 207 } |
202 | 208 |
203 /** | 209 function onSubscriptionAdded(subscription) |
204 * Subscription change listener | |
205 */ | |
206 function onSubscriptionChange(action, subscription, newValue, oldValue) | |
207 { | 210 { |
208 FilterListener.setDirty(1); | 211 FilterListener.setDirty(1); |
209 | 212 |
210 if (action != "added" && action != "removed" && action != "disabled" && action != "updated") | 213 if (subscription.url in FilterStorage.knownSubscriptions && !subscription.disa bled) |
kzar
2016/03/23 16:02:32
Nit: Mind wrapping this long line?
Wladimir Palant
2016/03/23 16:12:39
`subscription.url in FilterStorage.knownSubscripti
Sebastian Noack
2016/03/23 17:56:09
Done.
| |
211 return; | 214 { |
215 subscription.filters.forEach(addFilter); | |
216 flushElemHide(); | |
217 } | |
218 } | |
212 | 219 |
213 if (action != "removed" && !(subscription.url in FilterStorage.knownSubscripti ons)) | 220 function onSubscriptionRemoved(subscription) |
221 { | |
222 FilterListener.setDirty(1); | |
223 | |
224 if (!subscription.disabled) | |
214 { | 225 { |
215 // Ignore updates for subscriptions not in the list | 226 subscription.filters.forEach(removeFilter); |
216 return; | 227 flushElemHide(); |
217 } | 228 } |
229 } | |
218 | 230 |
219 if ((action == "added" || action == "removed" || action == "updated") && subsc ription.disabled) | 231 function onSubscriptionDisabled(subscription, newValue) |
232 { | |
233 FilterListener.setDirty(1); | |
234 | |
235 if (subscription.url in FilterStorage.knownSubscriptions) | |
220 { | 236 { |
221 // Ignore adding/removing/updating of disabled subscriptions | 237 if (newValue == false) |
222 return; | 238 subscription.filters.forEach(addFilter); |
239 else | |
240 subscription.filters.forEach(removeFilter); | |
241 flushElemHide(); | |
223 } | 242 } |
243 } | |
224 | 244 |
225 if (action == "added" || action == "removed" || action == "disabled") | 245 function onSubscriptionUpdated(subscription) |
226 { | 246 { |
227 let method = (action == "added" || (action == "disabled" && newValue == fals e) ? addFilter : removeFilter); | 247 FilterListener.setDirty(1); |
228 if (subscription.filters) | 248 |
kzar
2016/03/23 16:02:32
Nit: ... and this one.
Sebastian Noack
2016/03/23 17:56:09
Done.
| |
229 subscription.filters.forEach(method); | 249 if (subscription.url in FilterStorage.knownSubscriptions && !subscription.disa bled) |
230 } | |
231 else if (action == "updated") | |
232 { | 250 { |
233 subscription.oldFilters.forEach(removeFilter); | 251 subscription.oldFilters.forEach(removeFilter); |
234 subscription.filters.forEach(addFilter); | 252 subscription.filters.forEach(addFilter); |
253 flushElemHide(); | |
235 } | 254 } |
236 | |
237 flushElemHide(); | |
238 } | 255 } |
239 | 256 |
240 /** | 257 function onFilterHitCount(filter, newValue) |
241 * Filter change listener | |
242 */ | |
243 function onFilterChange(action, filter, newValue, oldValue) | |
244 { | 258 { |
245 if (action == "hitCount" && newValue == 0) | 259 if (newValue == 0) |
260 FilterListener.setDirty(0); | |
261 else | |
262 FilterListener.setDirty(0.002); | |
263 } | |
264 | |
265 function onFilterLastHit() | |
266 { | |
267 FilterListener.setDirty(0.002); | |
268 } | |
269 | |
270 function onFilterAdded(filter) | |
271 { | |
272 FilterListener.setDirty(1); | |
273 | |
274 if (!filter.disabled) | |
246 { | 275 { |
247 // Filter hits are being reset, make sure these changes are saved. | 276 addFilter(filter); |
248 FilterListener.setDirty(0); | 277 flushElemHide(); |
249 } | 278 } |
250 else if (action == "hitCount" || action == "lastHit") | 279 } |
251 FilterListener.setDirty(0.002); | |
252 else | |
253 FilterListener.setDirty(1); | |
254 | 280 |
255 if (action != "added" && action != "removed" && action != "disabled") | 281 function onFilterRemoved(filter) |
256 return; | 282 { |
283 FilterListener.setDirty(1); | |
257 | 284 |
258 if ((action == "added" || action == "removed") && filter.disabled) | 285 if (!filter.disabled) |
259 { | 286 { |
260 // Ignore adding/removing of disabled filters | 287 removeFilter(filter); |
261 return; | 288 flushElemHide(); |
262 } | 289 } |
290 } | |
263 | 291 |
264 if (action == "added" || (action == "disabled" && newValue == false)) | 292 function onFilterDisabled(filter, newValue) |
293 { | |
294 FilterListener.setDirty(1); | |
295 | |
296 if (newValue == false) | |
265 addFilter(filter); | 297 addFilter(filter); |
266 else | 298 else |
267 removeFilter(filter); | 299 removeFilter(filter); |
268 flushElemHide(); | 300 flushElemHide(); |
269 } | 301 } |
270 | 302 |
271 /** | 303 function onLoad() |
272 * Generic notification listener | |
273 */ | |
274 function onGenericChange(action) | |
275 { | 304 { |
276 if (action == "load") | 305 isDirty = 0; |
277 { | |
278 isDirty = 0; | |
279 | 306 |
280 defaultMatcher.clear(); | 307 defaultMatcher.clear(); |
281 ElemHide.clear(); | 308 ElemHide.clear(); |
282 CSSRules.clear(); | 309 CSSRules.clear(); |
283 for (let subscription of FilterStorage.subscriptions) | 310 for (let subscription of FilterStorage.subscriptions) |
284 if (!subscription.disabled) | 311 if (!subscription.disabled) |
285 subscription.filters.forEach(addFilter); | 312 subscription.filters.forEach(addFilter); |
286 flushElemHide(); | 313 flushElemHide(); |
287 } | |
288 else if (action == "save") | |
289 isDirty = 0; | |
290 } | 314 } |
315 | |
316 function onSave() | |
317 { | |
318 isDirty = 0; | |
319 } | |
OLD | NEW |