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

Side by Side Diff: lib/filterClasses.js

Issue 29945631: Issue 7097 - Make element hiding filter objects ephemeral Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Nov. 18, 2018, 3:22 a.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/subscriptionClasses.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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 /** 74 /**
75 * Filter type as a string, e.g. "blocking". 75 * Filter type as a string, e.g. "blocking".
76 * @type {string} 76 * @type {string}
77 */ 77 */
78 get type() 78 get type()
79 { 79 {
80 throw new Error("Please define filter type in the subclass"); 80 throw new Error("Please define filter type in the subclass");
81 }, 81 },
82 82
83 /** 83 /**
84 * Whether this object is discarded after use. If <code>false</code>, the
85 * object is retained in memory once created and every call to
86 * {@link Filter#fromText} returns the same instance. Defaults to
87 * <code>false</code>.
88 * @type {boolean}
89 * @package
90 */
91 ephemeral: false,
92
93 /**
84 * Subscriptions to which this filter belongs. 94 * Subscriptions to which this filter belongs.
85 * @type {?(Subscription|Set.<Subscription>)} 95 * @type {?(Subscription|Set.<Subscription>)}
86 * @private 96 * @private
87 */ 97 */
88 _subscriptions: null, 98 _subscriptions: null,
89 99
90 /** 100 /**
91 * Whether the filter's subscriptions have already been added to the filter. 101 * Whether the filter's subscriptions have already been added to the filter.
92 * @type {boolean} 102 * @type {boolean}
93 * @package 103 * @package
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 this._subscriptions = [...this._subscriptions][0]; 187 this._subscriptions = [...this._subscriptions][0];
178 } 188 }
179 else if (subscription == this._subscriptions) 189 else if (subscription == this._subscriptions)
180 { 190 {
181 this._subscriptions = null; 191 this._subscriptions = null;
182 } 192 }
183 } 193 }
184 }, 194 },
185 195
186 /** 196 /**
197 * Checks whether the filter should be serialized.
198 * @return {boolean}
199 * @protected
200 */
201 shouldSerialize()
202 {
203 return false;
204 },
205
206 /**
187 * Serializes the filter for writing out on disk. 207 * Serializes the filter for writing out on disk.
188 * @yields {string} 208 * @yields {string}
189 */ 209 */
190 *serialize() 210 *serialize()
191 { 211 {
192 let {text} = this; 212 let {text} = this;
193 213
194 yield "[Filter]"; 214 yield "[Filter]";
195 yield "text=" + text; 215 yield "text=" + text;
196 }, 216 },
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 } 262 }
243 else 263 else
244 { 264 {
245 let match = text.includes("#") ? Filter.contentRegExp.exec(text) : null; 265 let match = text.includes("#") ? Filter.contentRegExp.exec(text) : null;
246 if (match) 266 if (match)
247 filter = ContentFilter.fromText(text, match[1], match[2], match[3]); 267 filter = ContentFilter.fromText(text, match[1], match[2], match[3]);
248 else 268 else
249 filter = RegExpFilter.fromText(text); 269 filter = RegExpFilter.fromText(text);
250 } 270 }
251 271
252 Filter.knownFilters.set(filter.text, filter); 272 if (!filter.ephemeral)
273 Filter.knownFilters.set(filter.text, filter);
274
253 return filter; 275 return filter;
254 }; 276 };
255 277
256 /** 278 /**
257 * Deserializes a filter 279 * Deserializes a filter
258 * 280 *
259 * @param {Object} obj map of serialized properties and their values 281 * @param {Object} obj map of serialized properties and their values
260 * @return {Filter} filter or null if the filter couldn't be created 282 * @return {Filter} filter or null if the filter couldn't be created
261 */ 283 */
262 Filter.fromObject = function(obj) 284 Filter.fromObject = function(obj)
263 { 285 {
264 let filter = Filter.fromText(obj.text); 286 let filter = Filter.fromText(obj.text);
265 if (filter instanceof ActiveFilter) 287 if (filter instanceof ActiveFilter)
266 { 288 {
267 if ("disabled" in obj) 289 if ("disabled" in obj)
268 filter._disabled = (obj.disabled == "true"); 290 filter._disabled = (obj.disabled == "true");
269 if ("hitCount" in obj) 291 if ("hitCount" in obj)
270 filter._hitCount = parseInt(obj.hitCount, 10) || 0; 292 filter._hitCount = parseInt(obj.hitCount, 10) || 0;
271 if ("lastHit" in obj) 293 if ("lastHit" in obj)
272 filter._lastHit = parseInt(obj.lastHit, 10) || 0; 294 filter._lastHit = parseInt(obj.lastHit, 10) || 0;
295
296 // If the filter object is ephemeral, we normally don't keep it, but if it
297 // has any state then we must.
298 if (filter.ephemeral && this.shouldSerialize())
299 Filter.knownFilters.set(filter.text, filter);
273 } 300 }
274 return filter; 301 return filter;
275 }; 302 };
276 303
277 /** 304 /**
278 * Removes unnecessary whitespaces from filter text, will only return null if 305 * Removes unnecessary whitespaces from filter text, will only return null if
279 * the input parameter is null. 306 * the input parameter is null.
280 * @param {string} text 307 * @param {string} text
281 * @return {string} 308 * @return {string}
282 */ 309 */
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 get disabled() 452 get disabled()
426 { 453 {
427 return this._disabled; 454 return this._disabled;
428 }, 455 },
429 set disabled(value) 456 set disabled(value)
430 { 457 {
431 if (value != this._disabled) 458 if (value != this._disabled)
432 { 459 {
433 let oldValue = this._disabled; 460 let oldValue = this._disabled;
434 this._disabled = value; 461 this._disabled = value;
462
463 if (value && this.ephemeral)
Manish Jethani 2018/11/18 03:29:12 This could be optimized a little bit, but this pro
Manish Jethani 2018/11/18 06:19:14 Ignore, now removed.
464 Filter.knownFilters.set(this.text, this);
465
435 filterNotifier.emit("filter.disabled", this, value, oldValue); 466 filterNotifier.emit("filter.disabled", this, value, oldValue);
436 } 467 }
437 return this._disabled; 468 return this._disabled;
438 }, 469 },
439 470
440 /** 471 /**
441 * Number of hits on the filter since the last reset 472 * Number of hits on the filter since the last reset
442 * @type {number} 473 * @type {number}
443 */ 474 */
444 get hitCount() 475 get hitCount()
445 { 476 {
446 return this._hitCount; 477 return this._hitCount;
447 }, 478 },
448 set hitCount(value) 479 set hitCount(value)
449 { 480 {
450 if (value != this._hitCount) 481 if (value != this._hitCount)
451 { 482 {
452 let oldValue = this._hitCount; 483 let oldValue = this._hitCount;
453 this._hitCount = value; 484 this._hitCount = value;
485
486 if (value && this.ephemeral)
487 Filter.knownFilters.set(this.text, this);
488
454 filterNotifier.emit("filter.hitCount", this, value, oldValue); 489 filterNotifier.emit("filter.hitCount", this, value, oldValue);
455 } 490 }
456 return this._hitCount; 491 return this._hitCount;
457 }, 492 },
458 493
459 /** 494 /**
460 * Last time the filter had a hit (in milliseconds since the beginning of the 495 * Last time the filter had a hit (in milliseconds since the beginning of the
461 * epoch) 496 * epoch)
462 * @type {number} 497 * @type {number}
463 */ 498 */
464 get lastHit() 499 get lastHit()
465 { 500 {
466 return this._lastHit; 501 return this._lastHit;
467 }, 502 },
468 set lastHit(value) 503 set lastHit(value)
469 { 504 {
470 if (value != this._lastHit) 505 if (value != this._lastHit)
471 { 506 {
472 let oldValue = this._lastHit; 507 let oldValue = this._lastHit;
473 this._lastHit = value; 508 this._lastHit = value;
509
510 if (value && this.ephemeral)
511 Filter.knownFilters.set(this.text, this);
512
474 filterNotifier.emit("filter.lastHit", this, value, oldValue); 513 filterNotifier.emit("filter.lastHit", this, value, oldValue);
475 } 514 }
476 return this._lastHit; 515 return this._lastHit;
477 }, 516 },
478 517
479 /** 518 /**
480 * String that the domains property should be generated from 519 * String that the domains property should be generated from
481 * @type {?string} 520 * @type {?string}
482 */ 521 */
483 domainSource: null, 522 domainSource: null,
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 * @return {boolean} 703 * @return {boolean}
665 */ 704 */
666 isGeneric() 705 isGeneric()
667 { 706 {
668 let {sitekeys, domains} = this; 707 let {sitekeys, domains} = this;
669 708
670 return !(sitekeys && sitekeys.length) && (!domains || domains.get("")); 709 return !(sitekeys && sitekeys.length) && (!domains || domains.get(""));
671 }, 710 },
672 711
673 /** 712 /**
713 * See Filter.shouldSerialize()
714 * @inheritdoc
715 */
716 shouldSerialize()
Manish Jethani 2018/11/18 03:29:12 Note that this will always return false in the cur
Manish Jethani 2018/11/18 06:19:14 Ignore, now removed. An object created with Filte
717 {
718 return this._disabled || this._hitCount || this._lastHit;
719 },
720
721 /**
674 * See Filter.serialize() 722 * See Filter.serialize()
675 * @inheritdoc 723 * @inheritdoc
676 */ 724 */
677 *serialize() 725 *serialize()
678 { 726 {
679 let {_disabled, _hitCount, _lastHit} = this; 727 if (this.shouldSerialize())
680
681 if (_disabled || _hitCount || _lastHit)
682 { 728 {
729 let {_disabled, _hitCount, _lastHit} = this;
683 yield* Filter.prototype.serialize.call(this); 730 yield* Filter.prototype.serialize.call(this);
684 if (_disabled) 731 if (_disabled)
685 yield "disabled=true"; 732 yield "disabled=true";
686 if (_hitCount) 733 if (_hitCount)
687 yield "hitCount=" + _hitCount; 734 yield "hitCount=" + _hitCount;
688 if (_lastHit) 735 if (_lastHit)
689 yield "lastHit=" + _lastHit; 736 yield "lastHit=" + _lastHit;
690 } 737 }
691 } 738 }
692 }); 739 });
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after
1309 * @constructor 1356 * @constructor
1310 * @augments ElemHideBase 1357 * @augments ElemHideBase
1311 */ 1358 */
1312 function ElemHideFilter(text, domains, selector) 1359 function ElemHideFilter(text, domains, selector)
1313 { 1360 {
1314 ElemHideBase.call(this, text, domains, selector); 1361 ElemHideBase.call(this, text, domains, selector);
1315 } 1362 }
1316 exports.ElemHideFilter = ElemHideFilter; 1363 exports.ElemHideFilter = ElemHideFilter;
1317 1364
1318 ElemHideFilter.prototype = extend(ElemHideBase, { 1365 ElemHideFilter.prototype = extend(ElemHideBase, {
1319 type: "elemhide" 1366 type: "elemhide",
1367
1368 /**
1369 * See Filter.ephemeral
1370 * @inheritdoc
1371 */
1372 ephemeral: true
1320 }); 1373 });
1321 1374
1322 /** 1375 /**
1323 * Class for element hiding exceptions 1376 * Class for element hiding exceptions
1324 * @param {string} text see {@link Filter Filter()} 1377 * @param {string} text see {@link Filter Filter()}
1325 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()} 1378 * @param {string} [domains] see {@link ElemHideBase ElemHideBase()}
1326 * @param {string} selector see {@link ElemHideBase ElemHideBase()} 1379 * @param {string} selector see {@link ElemHideBase ElemHideBase()}
1327 * @constructor 1380 * @constructor
1328 * @augments ElemHideBase 1381 * @augments ElemHideBase
1329 */ 1382 */
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
1374 1427
1375 /** 1428 /**
1376 * Script that should be executed 1429 * Script that should be executed
1377 * @type {string} 1430 * @type {string}
1378 */ 1431 */
1379 get script() 1432 get script()
1380 { 1433 {
1381 return this.body; 1434 return this.body;
1382 } 1435 }
1383 }); 1436 });
OLDNEW
« no previous file with comments | « no previous file | lib/subscriptionClasses.js » ('j') | lib/subscriptionClasses.js » ('J')

Powered by Google App Engine
This is Rietveld