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