Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/filterListener.js

Issue 29338976: Issue 3862 - Make filterListener use the new FilterNotifier API (Closed)
Patch Set: Addressed comments Created March 23, 2016, 5:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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.disabled)
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
229 subscription.filters.forEach(method); 249 if (subscription.url in FilterStorage.knownSubscriptions &&
230 } 250 !subscription.disabled)
231 else if (action == "updated")
232 { 251 {
233 subscription.oldFilters.forEach(removeFilter); 252 subscription.oldFilters.forEach(removeFilter);
234 subscription.filters.forEach(addFilter); 253 subscription.filters.forEach(addFilter);
254 flushElemHide();
235 } 255 }
236
237 flushElemHide();
238 } 256 }
239 257
240 /** 258 function onFilterHitCount(filter, newValue)
241 * Filter change listener
242 */
243 function onFilterChange(action, filter, newValue, oldValue)
244 { 259 {
245 if (action == "hitCount" && newValue == 0) 260 if (newValue == 0)
261 FilterListener.setDirty(0);
262 else
263 FilterListener.setDirty(0.002);
264 }
265
266 function onFilterLastHit()
267 {
268 FilterListener.setDirty(0.002);
269 }
270
271 function onFilterAdded(filter)
272 {
273 FilterListener.setDirty(1);
274
275 if (!filter.disabled)
246 { 276 {
247 // Filter hits are being reset, make sure these changes are saved. 277 addFilter(filter);
248 FilterListener.setDirty(0); 278 flushElemHide();
249 } 279 }
250 else if (action == "hitCount" || action == "lastHit") 280 }
251 FilterListener.setDirty(0.002);
252 else
253 FilterListener.setDirty(1);
254 281
255 if (action != "added" && action != "removed" && action != "disabled") 282 function onFilterRemoved(filter)
256 return; 283 {
284 FilterListener.setDirty(1);
257 285
258 if ((action == "added" || action == "removed") && filter.disabled) 286 if (!filter.disabled)
259 { 287 {
260 // Ignore adding/removing of disabled filters 288 removeFilter(filter);
261 return; 289 flushElemHide();
262 } 290 }
291 }
263 292
264 if (action == "added" || (action == "disabled" && newValue == false)) 293 function onFilterDisabled(filter, newValue)
294 {
295 FilterListener.setDirty(1);
296
297 if (newValue == false)
265 addFilter(filter); 298 addFilter(filter);
266 else 299 else
267 removeFilter(filter); 300 removeFilter(filter);
268 flushElemHide(); 301 flushElemHide();
269 } 302 }
270 303
271 /** 304 function onLoad()
272 * Generic notification listener
273 */
274 function onGenericChange(action)
275 { 305 {
276 if (action == "load") 306 isDirty = 0;
277 {
278 isDirty = 0;
279 307
280 defaultMatcher.clear(); 308 defaultMatcher.clear();
281 ElemHide.clear(); 309 ElemHide.clear();
282 CSSRules.clear(); 310 CSSRules.clear();
283 for (let subscription of FilterStorage.subscriptions) 311 for (let subscription of FilterStorage.subscriptions)
284 if (!subscription.disabled) 312 if (!subscription.disabled)
285 subscription.filters.forEach(addFilter); 313 subscription.filters.forEach(addFilter);
286 flushElemHide(); 314 flushElemHide();
287 }
288 else if (action == "save")
289 isDirty = 0;
290 } 315 }
316
317 function onSave()
318 {
319 isDirty = 0;
320 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld