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: 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:
View unified diff | Download patch
« no previous file with comments | « 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')
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 201 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 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 = 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 addFilter(filters[current]); 242 addFilter(subscription.filterAt(current));
243 } 243 }
244 244
245 function onSubscriptionAdded(subscription) 245 function onSubscriptionAdded(subscription)
246 { 246 {
247 FilterListener.setDirty(1); 247 FilterListener.setDirty(1);
248 248
249 if (!subscription.disabled) 249 if (!subscription.disabled)
250 addFilters(subscription.filters); 250 addSubscriptionFilters(subscription);
251 } 251 }
252 252
253 function onSubscriptionRemoved(subscription) 253 function onSubscriptionRemoved(subscription)
254 { 254 {
255 FilterListener.setDirty(1); 255 FilterListener.setDirty(1);
256 256
257 if (!subscription.disabled) 257 if (!subscription.disabled)
258 subscription.filters.forEach(removeFilter); 258 {
259 for (let filter of subscription.filters())
260 removeFilter(filter);
261 }
259 } 262 }
260 263
261 function onSubscriptionDisabled(subscription, newValue) 264 function onSubscriptionDisabled(subscription, newValue)
262 { 265 {
263 FilterListener.setDirty(1); 266 FilterListener.setDirty(1);
264 267
265 if (filterStorage.knownSubscriptions.has(subscription.url)) 268 if (filterStorage.knownSubscriptions.has(subscription.url))
266 { 269 {
267 if (newValue == false) 270 if (newValue == false)
268 addFilters(subscription.filters); 271 {
272 addSubscriptionFilters(subscription);
273 }
269 else 274 else
270 subscription.filters.forEach(removeFilter); 275 {
276 for (let filter of subscription.filters())
277 removeFilter(filter);
278 }
271 } 279 }
272 } 280 }
273 281
274 function onSubscriptionUpdated(subscription, oldFilters) 282 function onSubscriptionUpdated(subscription, oldFilters)
275 { 283 {
276 FilterListener.setDirty(1); 284 FilterListener.setDirty(1);
277 285
278 if (!subscription.disabled && 286 if (!subscription.disabled &&
279 filterStorage.knownSubscriptions.has(subscription.url)) 287 filterStorage.knownSubscriptions.has(subscription.url))
280 { 288 {
281 oldFilters.forEach(removeFilter); 289 for (let filter of oldFilters)
282 addFilters(subscription.filters); 290 removeFilter(filter);
291
292 addSubscriptionFilters(subscription);
283 } 293 }
284 } 294 }
285 295
286 function onFilterHitCount(filter, newValue) 296 function onFilterHitCount(filter, newValue)
287 { 297 {
288 if (newValue == 0) 298 if (newValue == 0)
289 FilterListener.setDirty(0); 299 FilterListener.setDirty(0);
290 else 300 else
291 FilterListener.setDirty(0.002); 301 FilterListener.setDirty(0.002);
292 } 302 }
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 339
330 function onLoad() 340 function onLoad()
331 { 341 {
332 isDirty = 0; 342 isDirty = 0;
333 343
334 defaultMatcher.clear(); 344 defaultMatcher.clear();
335 ElemHide.clear(); 345 ElemHide.clear();
336 ElemHideEmulation.clear(); 346 ElemHideEmulation.clear();
337 ElemHideExceptions.clear(); 347 ElemHideExceptions.clear();
338 Snippets.clear(); 348 Snippets.clear();
349
339 for (let subscription of filterStorage.subscriptions()) 350 for (let subscription of filterStorage.subscriptions())
340 { 351 {
341 if (!subscription.disabled) 352 if (!subscription.disabled)
342 addFilters(subscription.filters); 353 addSubscriptionFilters(subscription);
343 } 354 }
344 } 355 }
345 356
346 function onSave() 357 function onSave()
347 { 358 {
348 isDirty = 0; 359 isDirty = 0;
349 } 360 }
OLDNEW
« no previous file with comments | « no previous file | lib/filterStorage.js » ('j') | lib/subscriptionClasses.js » ('J')

Powered by Google App Engine
This is Rietveld