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 FilterNotifier.on("filter.moved", onGenericChange); |
116 onSubscriptionChange(match[2], item, newValue, oldValue); | 118 |
117 else | 119 FilterNotifier.on("subscription.added", onSubscriptionAdded); |
118 onGenericChange(action, item); | 120 FilterNotifier.on("subscription.removed", onSubscriptionRemoved); |
119 }); | 121 FilterNotifier.on("subscription.disabled", onSubscriptionDisabled); |
| 122 FilterNotifier.on("subscription.updated", onSubscriptionUpdated); |
| 123 FilterNotifier.on("subscription.moved", onGenericChange); |
| 124 FilterNotifier.on("subscription.title", onGenericChange); |
| 125 FilterNotifier.on("subscription.fixedTitle", onGenericChange); |
| 126 FilterNotifier.on("subscription.homepage", onGenericChange); |
| 127 FilterNotifier.on("subscription.downloadStatus", onGenericChange); |
| 128 FilterNotifier.on("subscription.lastCheck", onGenericChange); |
| 129 FilterNotifier.on("subscription.errors", onGenericChange); |
| 130 |
| 131 FilterNotifier.on("load", onLoad); |
| 132 FilterNotifier.on("save", onSave); |
| 133 |
120 | 134 |
121 if ("nsIStyleSheetService" in Ci) | 135 if ("nsIStyleSheetService" in Ci) |
122 ElemHide.init(); | 136 ElemHide.init(); |
123 else | 137 else |
124 flushElemHide = function() {}; // No global stylesheet in Chrome & Co. | 138 flushElemHide = function() {}; // No global stylesheet in Chrome & Co. |
125 FilterStorage.loadFromDisk(); | 139 FilterStorage.loadFromDisk(); |
126 | 140 |
127 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history"
, true); | 141 Services.obs.addObserver(HistoryPurgeObserver, "browser:purge-session-history"
, true); |
128 onShutdown.add(function() | 142 onShutdown.add(function() |
129 { | 143 { |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 defaultMatcher.remove(filter); | 207 defaultMatcher.remove(filter); |
194 else if (filter instanceof ElemHideBase) | 208 else if (filter instanceof ElemHideBase) |
195 { | 209 { |
196 if (filter instanceof CSSPropertyFilter) | 210 if (filter instanceof CSSPropertyFilter) |
197 CSSRules.remove(filter); | 211 CSSRules.remove(filter); |
198 else | 212 else |
199 ElemHide.remove(filter); | 213 ElemHide.remove(filter); |
200 } | 214 } |
201 } | 215 } |
202 | 216 |
203 /** | 217 function onSubscriptionAdded(subscription) |
204 * Subscription change listener | |
205 */ | |
206 function onSubscriptionChange(action, subscription, newValue, oldValue) | |
207 { | 218 { |
208 FilterListener.setDirty(1); | 219 FilterListener.setDirty(1); |
209 | 220 |
210 if (action != "added" && action != "removed" && action != "disabled" && action
!= "updated") | 221 if (!subscription.disabled) |
211 return; | 222 { |
| 223 subscription.filters.forEach(addFilter); |
| 224 flushElemHide(); |
| 225 } |
| 226 } |
212 | 227 |
213 if (action != "removed" && !(subscription.url in FilterStorage.knownSubscripti
ons)) | 228 function onSubscriptionRemoved(subscription) |
| 229 { |
| 230 FilterListener.setDirty(1); |
| 231 |
| 232 if (!subscription.disabled) |
214 { | 233 { |
215 // Ignore updates for subscriptions not in the list | 234 subscription.filters.forEach(removeFilter); |
216 return; | 235 flushElemHide(); |
217 } | 236 } |
| 237 } |
218 | 238 |
219 if ((action == "added" || action == "removed" || action == "updated") && subsc
ription.disabled) | 239 function onSubscriptionDisabled(subscription, newValue) |
| 240 { |
| 241 FilterListener.setDirty(1); |
| 242 |
| 243 if (subscription.url in FilterStorage.knownSubscriptions) |
220 { | 244 { |
221 // Ignore adding/removing/updating of disabled subscriptions | 245 if (newValue == false) |
222 return; | 246 subscription.filters.forEach(addFilter); |
| 247 else |
| 248 subscription.filters.forEach(removeFilter); |
| 249 flushElemHide(); |
223 } | 250 } |
| 251 } |
224 | 252 |
225 if (action == "added" || action == "removed" || action == "disabled") | 253 function onSubscriptionUpdated(subscription) |
226 { | 254 { |
227 let method = (action == "added" || (action == "disabled" && newValue == fals
e) ? addFilter : removeFilter); | 255 FilterListener.setDirty(1); |
228 if (subscription.filters) | 256 |
229 subscription.filters.forEach(method); | 257 if (subscription.url in FilterStorage.knownSubscriptions && |
230 } | 258 !subscription.disabled) |
231 else if (action == "updated") | |
232 { | 259 { |
233 subscription.oldFilters.forEach(removeFilter); | 260 subscription.oldFilters.forEach(removeFilter); |
234 subscription.filters.forEach(addFilter); | 261 subscription.filters.forEach(addFilter); |
| 262 flushElemHide(); |
235 } | 263 } |
236 | |
237 flushElemHide(); | |
238 } | 264 } |
239 | 265 |
240 /** | 266 function onFilterHitCount(filter, newValue) |
241 * Filter change listener | |
242 */ | |
243 function onFilterChange(action, filter, newValue, oldValue) | |
244 { | 267 { |
245 if (action == "hitCount" && newValue == 0) | 268 if (newValue == 0) |
| 269 FilterListener.setDirty(0); |
| 270 else |
| 271 FilterListener.setDirty(0.002); |
| 272 } |
| 273 |
| 274 function onFilterLastHit() |
| 275 { |
| 276 FilterListener.setDirty(0.002); |
| 277 } |
| 278 |
| 279 function onFilterAdded(filter) |
| 280 { |
| 281 FilterListener.setDirty(1); |
| 282 |
| 283 if (!filter.disabled) |
246 { | 284 { |
247 // Filter hits are being reset, make sure these changes are saved. | 285 addFilter(filter); |
248 FilterListener.setDirty(0); | 286 flushElemHide(); |
249 } | 287 } |
250 else if (action == "hitCount" || action == "lastHit") | 288 } |
251 FilterListener.setDirty(0.002); | |
252 else | |
253 FilterListener.setDirty(1); | |
254 | 289 |
255 if (action != "added" && action != "removed" && action != "disabled") | 290 function onFilterRemoved(filter) |
256 return; | 291 { |
| 292 FilterListener.setDirty(1); |
257 | 293 |
258 if ((action == "added" || action == "removed") && filter.disabled) | 294 if (!filter.disabled) |
259 { | 295 { |
260 // Ignore adding/removing of disabled filters | 296 removeFilter(filter); |
261 return; | 297 flushElemHide(); |
262 } | 298 } |
| 299 } |
263 | 300 |
264 if (action == "added" || (action == "disabled" && newValue == false)) | 301 function onFilterDisabled(filter, newValue) |
| 302 { |
| 303 FilterListener.setDirty(1); |
| 304 |
| 305 if (newValue == false) |
265 addFilter(filter); | 306 addFilter(filter); |
266 else | 307 else |
267 removeFilter(filter); | 308 removeFilter(filter); |
268 flushElemHide(); | 309 flushElemHide(); |
269 } | 310 } |
270 | 311 |
271 /** | 312 function onGenericChange() |
272 * Generic notification listener | |
273 */ | |
274 function onGenericChange(action) | |
275 { | 313 { |
276 if (action == "load") | 314 FilterListener.setDirty(1); |
277 { | 315 } |
278 isDirty = 0; | |
279 | 316 |
280 defaultMatcher.clear(); | 317 function onLoad() |
281 ElemHide.clear(); | 318 { |
282 CSSRules.clear(); | 319 isDirty = 0; |
283 for (let subscription of FilterStorage.subscriptions) | 320 |
284 if (!subscription.disabled) | 321 defaultMatcher.clear(); |
285 subscription.filters.forEach(addFilter); | 322 ElemHide.clear(); |
286 flushElemHide(); | 323 CSSRules.clear(); |
287 } | 324 for (let subscription of FilterStorage.subscriptions) |
288 else if (action == "save") | 325 if (!subscription.disabled) |
289 isDirty = 0; | 326 subscription.filters.forEach(addFilter); |
| 327 flushElemHide(); |
290 } | 328 } |
| 329 |
| 330 function onSave() |
| 331 { |
| 332 isDirty = 0; |
| 333 } |
OLD | NEW |