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

Delta Between Two Patch Sets: lib/filterListener.js

Issue 29934588: Issue 7094 - Encapsulate management of subscription filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created Nov. 2, 2018, 10:34 p.m.
Right Patch Set: Remove hasFilter and related code Created Nov. 18, 2018, 10:21 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | lib/filterStorage.js » ('j') | lib/subscriptionClasses.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 {Filter, 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;
(...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, filterObjects) 222 function addSubscriptionFilters(subscription)
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 = filterObjects ? filterObjects.length : filters.length; 230 let len = subscription.filterCount;
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 { 242 addFilter(subscription.filterAt(current));
243 addFilter(filterObjects ? filterObjects[current] :
244 Filter.fromText(filters[current]));
245 }
246 } 243 }
247 244
248 function onSubscriptionAdded(subscription) 245 function onSubscriptionAdded(subscription)
249 { 246 {
250 FilterListener.setDirty(1); 247 FilterListener.setDirty(1);
251 248
252 if (!subscription.disabled) 249 if (!subscription.disabled)
253 addFilters(subscription.filters); 250 addSubscriptionFilters(subscription);
254 } 251 }
255 252
256 function onSubscriptionRemoved(subscription) 253 function onSubscriptionRemoved(subscription)
257 { 254 {
258 FilterListener.setDirty(1); 255 FilterListener.setDirty(1);
259 256
260 if (!subscription.disabled) 257 if (!subscription.disabled)
261 { 258 {
262 for (let text of subscription.filters) 259 for (let filter of subscription.filters())
263 removeFilter(Filter.fromText(text)); 260 removeFilter(filter);
264 } 261 }
265 } 262 }
266 263
267 function onSubscriptionDisabled(subscription, newValue) 264 function onSubscriptionDisabled(subscription, newValue)
268 { 265 {
269 FilterListener.setDirty(1); 266 FilterListener.setDirty(1);
270 267
271 if (filterStorage.knownSubscriptions.has(subscription.url)) 268 if (filterStorage.knownSubscriptions.has(subscription.url))
272 { 269 {
273 if (newValue == false) 270 if (newValue == false)
274 { 271 {
275 addFilters(subscription.filters); 272 addSubscriptionFilters(subscription);
276 } 273 }
277 else 274 else
278 { 275 {
279 for (let text of subscription.filters) 276 for (let filter of subscription.filters())
280 removeFilter(Filter.fromText(text)); 277 removeFilter(filter);
281 } 278 }
282 } 279 }
283 } 280 }
284 281
285 function onSubscriptionUpdated(subscription, oldFilters) 282 function onSubscriptionUpdated(subscription, oldFilters)
286 { 283 {
287 FilterListener.setDirty(1); 284 FilterListener.setDirty(1);
288 285
289 if (!subscription.disabled && 286 if (!subscription.disabled &&
290 filterStorage.knownSubscriptions.has(subscription.url)) 287 filterStorage.knownSubscriptions.has(subscription.url))
291 { 288 {
292 for (let text of oldFilters) 289 for (let filter of oldFilters)
293 removeFilter(Filter.fromText(text)); 290 removeFilter(filter);
294 291
295 addFilters(subscription.filters); 292 addSubscriptionFilters(subscription);
296 } 293 }
297 } 294 }
298 295
299 function onFilterHitCount(filter, newValue) 296 function onFilterHitCount(filter, newValue)
300 { 297 {
301 if (newValue == 0) 298 if (newValue == 0)
302 FilterListener.setDirty(0); 299 FilterListener.setDirty(0);
303 else 300 else
304 FilterListener.setDirty(0.002); 301 FilterListener.setDirty(0.002);
305 } 302 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 339
343 function onLoad() 340 function onLoad()
344 { 341 {
345 isDirty = 0; 342 isDirty = 0;
346 343
347 defaultMatcher.clear(); 344 defaultMatcher.clear();
348 ElemHide.clear(); 345 ElemHide.clear();
349 ElemHideEmulation.clear(); 346 ElemHideEmulation.clear();
350 ElemHideExceptions.clear(); 347 ElemHideExceptions.clear();
351 Snippets.clear(); 348 Snippets.clear();
349
352 for (let subscription of filterStorage.subscriptions()) 350 for (let subscription of filterStorage.subscriptions())
353 { 351 {
354 if (!subscription.disabled) 352 if (!subscription.disabled)
355 addFilters(subscription.filters, subscription.filterObjects); 353 addSubscriptionFilters(subscription);
356
357 subscription.filterObjects = null;
358 } 354 }
359 } 355 }
360 356
361 function onSave() 357 function onSave()
362 { 358 {
363 isDirty = 0; 359 isDirty = 0;
364 } 360 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld