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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 "use strict"; | 18 "use strict"; |
19 | 19 |
20 /** | 20 /** |
21 * @fileOverview Component synchronizing filter storage with Matcher | 21 * @fileOverview Component synchronizing filter storage with Matcher |
22 * instances and ElemHide. | 22 * instances and ElemHide. |
23 */ | 23 */ |
24 | 24 |
25 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); | 25 const {Services} = Cu.import("resource://gre/modules/Services.jsm", {}); |
26 const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); | 26 const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {}); |
27 | 27 |
28 const {FilterStorage} = require("./filterStorage"); | 28 const {filterStorage} = require("./filterStorage"); |
29 const {filterNotifier} = require("./filterNotifier"); | 29 const {filterNotifier} = require("./filterNotifier"); |
30 const {ElemHide} = require("./elemHide"); | 30 const {ElemHide} = require("./elemHide"); |
31 const {ElemHideEmulation} = require("./elemHideEmulation"); | 31 const {ElemHideEmulation} = require("./elemHideEmulation"); |
32 const {ElemHideExceptions} = require("./elemHideExceptions"); | 32 const {ElemHideExceptions} = require("./elemHideExceptions"); |
33 const {Snippets} = require("./snippets"); | 33 const {Snippets} = require("./snippets"); |
34 const {defaultMatcher} = require("./matcher"); | 34 const {defaultMatcher} = require("./matcher"); |
35 const {ActiveFilter, RegExpFilter, | 35 const {ActiveFilter, RegExpFilter, |
36 ElemHideBase, ElemHideFilter, ElemHideEmulationFilter, | 36 ElemHideBase, ElemHideFilter, ElemHideEmulationFilter, |
37 SnippetFilter} = require("./filterClasses"); | 37 SnippetFilter} = require("./filterClasses"); |
38 const {SpecialSubscription} = require("./subscriptionClasses"); | 38 const {SpecialSubscription} = require("./subscriptionClasses"); |
39 const {Prefs} = require("prefs"); | 39 const {Prefs} = require("prefs"); |
40 | 40 |
41 /** | 41 /** |
42 * Increases on filter changes, filters will be saved if it exceeds 1. | 42 * Increases on filter changes, filters will be saved if it exceeds 1. |
43 * @type {number} | 43 * @type {number} |
44 */ | 44 */ |
45 let isDirty = 0; | 45 let isDirty = 0; |
46 | 46 |
47 /** | 47 /** |
48 * This object can be used to change properties of the filter change listeners. | 48 * This object can be used to change properties of the filter change listeners. |
49 * @class | 49 * @class |
50 */ | 50 */ |
51 let FilterListener = { | 51 let FilterListener = { |
52 /** | 52 /** |
53 * Increases "dirty factor" of the filters and calls | 53 * Increases "dirty factor" of the filters and calls |
54 * FilterStorage.saveToDisk() if it becomes 1 or more. Save is | 54 * filterStorage.saveToDisk() if it becomes 1 or more. Save is |
55 * executed delayed to prevent multiple subsequent calls. If the | 55 * executed delayed to prevent multiple subsequent calls. If the |
56 * parameter is 0 it forces saving filters if any changes were | 56 * parameter is 0 it forces saving filters if any changes were |
57 * recorded after the previous save. | 57 * recorded after the previous save. |
58 * @param {number} factor | 58 * @param {number} factor |
59 */ | 59 */ |
60 setDirty(factor) | 60 setDirty(factor) |
61 { | 61 { |
62 if (factor == 0 && isDirty > 0) | 62 if (factor == 0 && isDirty > 0) |
63 isDirty = 1; | 63 isDirty = 1; |
64 else | 64 else |
65 isDirty += factor; | 65 isDirty += factor; |
66 if (isDirty >= 1) | 66 if (isDirty >= 1) |
67 { | 67 { |
68 isDirty = 0; | 68 isDirty = 0; |
69 FilterStorage.saveToDisk(); | 69 filterStorage.saveToDisk(); |
70 } | 70 } |
71 } | 71 } |
72 }; | 72 }; |
73 | 73 |
74 /** | 74 /** |
75 * Observer listening to history purge actions. | 75 * Observer listening to history purge actions. |
76 * @class | 76 * @class |
77 */ | 77 */ |
78 let HistoryPurgeObserver = { | 78 let HistoryPurgeObserver = { |
79 observe(subject, topic, data) | 79 observe(subject, topic, data) |
80 { | 80 { |
81 if (topic == "browser:purge-session-history" && | 81 if (topic == "browser:purge-session-history" && |
82 Prefs.clearStatsOnHistoryPurge) | 82 Prefs.clearStatsOnHistoryPurge) |
83 { | 83 { |
84 FilterStorage.resetHitCounts(); | 84 filterStorage.resetHitCounts(); |
85 FilterListener.setDirty(0); // Force saving to disk | 85 FilterListener.setDirty(0); // Force saving to disk |
86 | 86 |
87 Prefs.recentReports = []; | 87 Prefs.recentReports = []; |
88 } | 88 } |
89 }, | 89 }, |
90 QueryInterface: XPCOMUtils.generateQI( | 90 QueryInterface: XPCOMUtils.generateQI( |
91 [Ci.nsISupportsWeakReference, Ci.nsIObserver] | 91 [Ci.nsISupportsWeakReference, Ci.nsIObserver] |
92 ) | 92 ) |
93 }; | 93 }; |
94 | 94 |
(...skipping 16 matching lines...) Expand all Loading... |
111 filterNotifier.on("subscription.title", onGenericChange); | 111 filterNotifier.on("subscription.title", onGenericChange); |
112 filterNotifier.on("subscription.fixedTitle", onGenericChange); | 112 filterNotifier.on("subscription.fixedTitle", onGenericChange); |
113 filterNotifier.on("subscription.homepage", onGenericChange); | 113 filterNotifier.on("subscription.homepage", onGenericChange); |
114 filterNotifier.on("subscription.downloadStatus", onGenericChange); | 114 filterNotifier.on("subscription.downloadStatus", onGenericChange); |
115 filterNotifier.on("subscription.lastCheck", onGenericChange); | 115 filterNotifier.on("subscription.lastCheck", onGenericChange); |
116 filterNotifier.on("subscription.errors", onGenericChange); | 116 filterNotifier.on("subscription.errors", onGenericChange); |
117 | 117 |
118 filterNotifier.on("load", onLoad); | 118 filterNotifier.on("load", onLoad); |
119 filterNotifier.on("save", onSave); | 119 filterNotifier.on("save", onSave); |
120 | 120 |
121 FilterStorage.loadFromDisk(); | 121 filterStorage.loadFromDisk(); |
122 | 122 |
123 Services.obs.addObserver(HistoryPurgeObserver, | 123 Services.obs.addObserver(HistoryPurgeObserver, |
124 "browser:purge-session-history", true); | 124 "browser:purge-session-history", true); |
125 onShutdown.add(() => | 125 onShutdown.add(() => |
126 { | 126 { |
127 Services.obs.removeObserver(HistoryPurgeObserver, | 127 Services.obs.removeObserver(HistoryPurgeObserver, |
128 "browser:purge-session-history"); | 128 "browser:purge-session-history"); |
129 }); | 129 }); |
130 } | 130 } |
131 init(); | 131 init(); |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 FilterListener.setDirty(1); | 255 FilterListener.setDirty(1); |
256 | 256 |
257 if (!subscription.disabled) | 257 if (!subscription.disabled) |
258 subscription.filters.forEach(removeFilter); | 258 subscription.filters.forEach(removeFilter); |
259 } | 259 } |
260 | 260 |
261 function onSubscriptionDisabled(subscription, newValue) | 261 function onSubscriptionDisabled(subscription, newValue) |
262 { | 262 { |
263 FilterListener.setDirty(1); | 263 FilterListener.setDirty(1); |
264 | 264 |
265 if (FilterStorage.knownSubscriptions.has(subscription.url)) | 265 if (filterStorage.knownSubscriptions.has(subscription.url)) |
266 { | 266 { |
267 if (newValue == false) | 267 if (newValue == false) |
268 addFilters(subscription.filters); | 268 addFilters(subscription.filters); |
269 else | 269 else |
270 subscription.filters.forEach(removeFilter); | 270 subscription.filters.forEach(removeFilter); |
271 } | 271 } |
272 } | 272 } |
273 | 273 |
274 function onSubscriptionUpdated(subscription, oldFilters) | 274 function onSubscriptionUpdated(subscription, oldFilters) |
275 { | 275 { |
276 FilterListener.setDirty(1); | 276 FilterListener.setDirty(1); |
277 | 277 |
278 if (!subscription.disabled && | 278 if (!subscription.disabled && |
279 FilterStorage.knownSubscriptions.has(subscription.url)) | 279 filterStorage.knownSubscriptions.has(subscription.url)) |
280 { | 280 { |
281 oldFilters.forEach(removeFilter); | 281 oldFilters.forEach(removeFilter); |
282 addFilters(subscription.filters); | 282 addFilters(subscription.filters); |
283 } | 283 } |
284 } | 284 } |
285 | 285 |
286 function onFilterHitCount(filter, newValue) | 286 function onFilterHitCount(filter, newValue) |
287 { | 287 { |
288 if (newValue == 0) | 288 if (newValue == 0) |
289 FilterListener.setDirty(0); | 289 FilterListener.setDirty(0); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 | 329 |
330 function onLoad() | 330 function onLoad() |
331 { | 331 { |
332 isDirty = 0; | 332 isDirty = 0; |
333 | 333 |
334 defaultMatcher.clear(); | 334 defaultMatcher.clear(); |
335 ElemHide.clear(); | 335 ElemHide.clear(); |
336 ElemHideEmulation.clear(); | 336 ElemHideEmulation.clear(); |
337 ElemHideExceptions.clear(); | 337 ElemHideExceptions.clear(); |
338 Snippets.clear(); | 338 Snippets.clear(); |
339 for (let subscription of FilterStorage.subscriptions()) | 339 for (let subscription of filterStorage.subscriptions()) |
340 { | 340 { |
341 if (!subscription.disabled) | 341 if (!subscription.disabled) |
342 addFilters(subscription.filters); | 342 addFilters(subscription.filters); |
343 } | 343 } |
344 } | 344 } |
345 | 345 |
346 function onSave() | 346 function onSave() |
347 { | 347 { |
348 isDirty = 0; | 348 isDirty = 0; |
349 } | 349 } |
OLD | NEW |