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

Side by Side Diff: lib/filterListener.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Nov. 2, 2018, 10:34 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 | lib/filterStorage.js » ('j') | 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-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
(...skipping 14 matching lines...) Expand all
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 {Filter, 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;
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 ElemHideEmulation.remove(filter); 212 ElemHideEmulation.remove(filter);
213 else 213 else
214 ElemHideExceptions.remove(filter); 214 ElemHideExceptions.remove(filter);
215 } 215 }
216 else if (filter instanceof SnippetFilter) 216 else if (filter instanceof SnippetFilter)
217 Snippets.remove(filter); 217 Snippets.remove(filter);
218 } 218 }
219 219
220 const primes = [101, 109, 131, 149, 163, 179, 193, 211, 229, 241]; 220 const primes = [101, 109, 131, 149, 163, 179, 193, 211, 229, 241];
221 221
222 function addFilters(filters) 222 function addFilters(filters, filterObjects)
223 { 223 {
224 // We add filters using pseudo-random ordering. Reason is that ElemHide will 224 // We add filters using pseudo-random ordering. Reason is that ElemHide will
225 // assign consecutive filter IDs that might be visible to the website. The 225 // assign consecutive filter IDs that might be visible to the website. The
226 // randomization makes sure that no conclusion can be made about the actual 226 // randomization makes sure that no conclusion can be made about the actual
227 // filters applying there. We have ten prime numbers to use as iteration step, 227 // filters applying there. We have ten prime numbers to use as iteration step,
228 // any of those can be chosen as long as the array length isn't divisible by 228 // any of those can be chosen as long as the array length isn't divisible by
229 // it. 229 // it.
230 let len = filters.length; 230 let len = filterObjects ? filterObjects.length : filters.length;
231 if (!len) 231 if (!len)
232 return; 232 return;
233 233
234 let current = (Math.random() * len) | 0; 234 let current = (Math.random() * len) | 0;
235 let step; 235 let step;
236 do 236 do
237 { 237 {
238 step = primes[(Math.random() * primes.length) | 0]; 238 step = primes[(Math.random() * primes.length) | 0];
239 } while (len % step == 0); 239 } while (len % step == 0);
240 240
241 for (let i = 0; i < len; i++, current = (current + step) % len) 241 for (let i = 0; i < len; i++, current = (current + step) % len)
242 addFilter(filters[current]); 242 {
243 addFilter(filterObjects ? filterObjects[current] :
244 Filter.fromText(filters[current]));
245 }
243 } 246 }
244 247
245 function onSubscriptionAdded(subscription) 248 function onSubscriptionAdded(subscription)
246 { 249 {
247 FilterListener.setDirty(1); 250 FilterListener.setDirty(1);
248 251
249 if (!subscription.disabled) 252 if (!subscription.disabled)
250 addFilters(subscription.filters); 253 addFilters(subscription.filters);
251 } 254 }
252 255
253 function onSubscriptionRemoved(subscription) 256 function onSubscriptionRemoved(subscription)
254 { 257 {
255 FilterListener.setDirty(1); 258 FilterListener.setDirty(1);
256 259
257 if (!subscription.disabled) 260 if (!subscription.disabled)
258 subscription.filters.forEach(removeFilter); 261 {
262 for (let text of subscription.filters)
263 removeFilter(Filter.fromText(text));
264 }
259 } 265 }
260 266
261 function onSubscriptionDisabled(subscription, newValue) 267 function onSubscriptionDisabled(subscription, newValue)
262 { 268 {
263 FilterListener.setDirty(1); 269 FilterListener.setDirty(1);
264 270
265 if (filterStorage.knownSubscriptions.has(subscription.url)) 271 if (filterStorage.knownSubscriptions.has(subscription.url))
266 { 272 {
267 if (newValue == false) 273 if (newValue == false)
274 {
268 addFilters(subscription.filters); 275 addFilters(subscription.filters);
276 }
269 else 277 else
270 subscription.filters.forEach(removeFilter); 278 {
279 for (let text of subscription.filters)
280 removeFilter(Filter.fromText(text));
281 }
271 } 282 }
272 } 283 }
273 284
274 function onSubscriptionUpdated(subscription, oldFilters) 285 function onSubscriptionUpdated(subscription, oldFilters)
275 { 286 {
276 FilterListener.setDirty(1); 287 FilterListener.setDirty(1);
277 288
278 if (!subscription.disabled && 289 if (!subscription.disabled &&
279 filterStorage.knownSubscriptions.has(subscription.url)) 290 filterStorage.knownSubscriptions.has(subscription.url))
280 { 291 {
281 oldFilters.forEach(removeFilter); 292 for (let text of oldFilters)
293 removeFilter(Filter.fromText(text));
294
282 addFilters(subscription.filters); 295 addFilters(subscription.filters);
283 } 296 }
284 } 297 }
285 298
286 function onFilterHitCount(filter, newValue) 299 function onFilterHitCount(filter, newValue)
287 { 300 {
288 if (newValue == 0) 301 if (newValue == 0)
289 FilterListener.setDirty(0); 302 FilterListener.setDirty(0);
290 else 303 else
291 FilterListener.setDirty(0.002); 304 FilterListener.setDirty(0.002);
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 isDirty = 0; 345 isDirty = 0;
333 346
334 defaultMatcher.clear(); 347 defaultMatcher.clear();
335 ElemHide.clear(); 348 ElemHide.clear();
336 ElemHideEmulation.clear(); 349 ElemHideEmulation.clear();
337 ElemHideExceptions.clear(); 350 ElemHideExceptions.clear();
338 Snippets.clear(); 351 Snippets.clear();
339 for (let subscription of filterStorage.subscriptions()) 352 for (let subscription of filterStorage.subscriptions())
340 { 353 {
341 if (!subscription.disabled) 354 if (!subscription.disabled)
342 addFilters(subscription.filters); 355 addFilters(subscription.filters, subscription.filterObjects);
356
357 subscription.filterObjects = null;
343 } 358 }
344 } 359 }
345 360
346 function onSave() 361 function onSave()
347 { 362 {
348 isDirty = 0; 363 isDirty = 0;
349 } 364 }
OLDNEW
« no previous file with comments | « no previous file | lib/filterStorage.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld