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