Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 this._subscriptions = [...this._subscriptions][0]; | 187 this._subscriptions = [...this._subscriptions][0]; |
188 } | 188 } |
189 else if (subscription == this._subscriptions) | 189 else if (subscription == this._subscriptions) |
190 { | 190 { |
191 this._subscriptions = null; | 191 this._subscriptions = null; |
192 } | 192 } |
193 } | 193 } |
194 }, | 194 }, |
195 | 195 |
196 /** | 196 /** |
197 * Checks whether the filter should be serialized. | |
198 * @return {boolean} | |
199 * @protected | |
200 */ | |
201 shouldSerialize() | |
202 { | |
203 return false; | |
204 }, | |
205 | |
206 /** | |
207 * Serializes the filter for writing out on disk. | 197 * Serializes the filter for writing out on disk. |
208 * @yields {string} | 198 * @yields {string} |
209 */ | 199 */ |
210 *serialize() | 200 *serialize() |
211 { | 201 { |
212 let {text} = this; | 202 let {text} = this; |
213 | 203 |
214 yield "[Filter]"; | 204 yield "[Filter]"; |
215 yield "text=" + text; | 205 yield "text=" + text; |
216 }, | 206 }, |
(...skipping 24 matching lines...) Expand all Loading... | |
241 * Regular expression that matches an invalid Content Security Policy | 231 * Regular expression that matches an invalid Content Security Policy |
242 * @type {RegExp} | 232 * @type {RegExp} |
243 */ | 233 */ |
244 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; | 234 Filter.invalidCSPRegExp = /(;|^) ?(base-uri|referrer|report-to|report-uri|upgrad e-insecure-requests)\b/i; |
245 | 235 |
246 /** | 236 /** |
247 * Creates a filter of correct type from its text representation - does the | 237 * Creates a filter of correct type from its text representation - does the |
248 * basic parsing and calls the right constructor then. | 238 * basic parsing and calls the right constructor then. |
249 * | 239 * |
250 * @param {string} text as in Filter() | 240 * @param {string} text as in Filter() |
241 * @param {boolean} persistent Whether the filter object should be persisted in | |
242 * memory. | |
251 * @return {Filter} | 243 * @return {Filter} |
252 */ | 244 */ |
253 Filter.fromText = function(text) | 245 Filter.fromText = function(text, persistent = true) |
Manish Jethani
2018/11/18 06:19:14
The idea here is that internally we'll always pass
| |
254 { | 246 { |
255 let filter = Filter.knownFilters.get(text); | 247 let filter = Filter.knownFilters.get(text); |
256 if (filter) | 248 if (filter) |
257 return filter; | 249 return filter; |
258 | 250 |
259 if (text[0] == "!") | 251 if (text[0] == "!") |
260 { | 252 { |
261 filter = new CommentFilter(text); | 253 filter = new CommentFilter(text); |
262 } | 254 } |
263 else | 255 else |
264 { | 256 { |
265 let match = text.includes("#") ? Filter.contentRegExp.exec(text) : null; | 257 let match = text.includes("#") ? Filter.contentRegExp.exec(text) : null; |
266 if (match) | 258 if (match) |
267 filter = ContentFilter.fromText(text, match[1], match[2], match[3]); | 259 filter = ContentFilter.fromText(text, match[1], match[2], match[3]); |
268 else | 260 else |
269 filter = RegExpFilter.fromText(text); | 261 filter = RegExpFilter.fromText(text); |
270 } | 262 } |
263 | |
264 if (persistent) | |
265 filter.ephemeral = false; | |
271 | 266 |
272 if (!filter.ephemeral) | 267 if (!filter.ephemeral) |
273 Filter.knownFilters.set(filter.text, filter); | 268 Filter.knownFilters.set(filter.text, filter); |
274 | 269 |
275 return filter; | 270 return filter; |
276 }; | 271 }; |
277 | 272 |
278 /** | 273 /** |
279 * Deserializes a filter | 274 * Deserializes a filter |
280 * | 275 * |
281 * @param {Object} obj map of serialized properties and their values | 276 * @param {Object} obj map of serialized properties and their values |
282 * @return {Filter} filter or null if the filter couldn't be created | 277 * @return {Filter} filter or null if the filter couldn't be created |
283 */ | 278 */ |
284 Filter.fromObject = function(obj) | 279 Filter.fromObject = function(obj) |
285 { | 280 { |
286 let filter = Filter.fromText(obj.text); | 281 let filter = Filter.fromText(obj.text); |
287 if (filter instanceof ActiveFilter) | 282 if (filter instanceof ActiveFilter) |
288 { | 283 { |
289 if ("disabled" in obj) | 284 if ("disabled" in obj) |
290 filter._disabled = (obj.disabled == "true"); | 285 filter._disabled = (obj.disabled == "true"); |
291 if ("hitCount" in obj) | 286 if ("hitCount" in obj) |
292 filter._hitCount = parseInt(obj.hitCount, 10) || 0; | 287 filter._hitCount = parseInt(obj.hitCount, 10) || 0; |
293 if ("lastHit" in obj) | 288 if ("lastHit" in obj) |
294 filter._lastHit = parseInt(obj.lastHit, 10) || 0; | 289 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); | |
300 } | 290 } |
301 return filter; | 291 return filter; |
302 }; | 292 }; |
303 | 293 |
304 /** | 294 /** |
305 * Removes unnecessary whitespaces from filter text, will only return null if | 295 * Removes unnecessary whitespaces from filter text, will only return null if |
306 * the input parameter is null. | 296 * the input parameter is null. |
307 * @param {string} text | 297 * @param {string} text |
308 * @return {string} | 298 * @return {string} |
309 */ | 299 */ |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
452 get disabled() | 442 get disabled() |
453 { | 443 { |
454 return this._disabled; | 444 return this._disabled; |
455 }, | 445 }, |
456 set disabled(value) | 446 set disabled(value) |
457 { | 447 { |
458 if (value != this._disabled) | 448 if (value != this._disabled) |
459 { | 449 { |
460 let oldValue = this._disabled; | 450 let oldValue = this._disabled; |
461 this._disabled = value; | 451 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 | |
466 filterNotifier.emit("filter.disabled", this, value, oldValue); | 452 filterNotifier.emit("filter.disabled", this, value, oldValue); |
467 } | 453 } |
468 return this._disabled; | 454 return this._disabled; |
469 }, | 455 }, |
470 | 456 |
471 /** | 457 /** |
472 * Number of hits on the filter since the last reset | 458 * Number of hits on the filter since the last reset |
473 * @type {number} | 459 * @type {number} |
474 */ | 460 */ |
475 get hitCount() | 461 get hitCount() |
476 { | 462 { |
477 return this._hitCount; | 463 return this._hitCount; |
478 }, | 464 }, |
479 set hitCount(value) | 465 set hitCount(value) |
480 { | 466 { |
481 if (value != this._hitCount) | 467 if (value != this._hitCount) |
482 { | 468 { |
483 let oldValue = this._hitCount; | 469 let oldValue = this._hitCount; |
484 this._hitCount = value; | 470 this._hitCount = value; |
485 | |
486 if (value && this.ephemeral) | |
487 Filter.knownFilters.set(this.text, this); | |
488 | |
489 filterNotifier.emit("filter.hitCount", this, value, oldValue); | 471 filterNotifier.emit("filter.hitCount", this, value, oldValue); |
490 } | 472 } |
491 return this._hitCount; | 473 return this._hitCount; |
492 }, | 474 }, |
493 | 475 |
494 /** | 476 /** |
495 * Last time the filter had a hit (in milliseconds since the beginning of the | 477 * Last time the filter had a hit (in milliseconds since the beginning of the |
496 * epoch) | 478 * epoch) |
497 * @type {number} | 479 * @type {number} |
498 */ | 480 */ |
499 get lastHit() | 481 get lastHit() |
500 { | 482 { |
501 return this._lastHit; | 483 return this._lastHit; |
502 }, | 484 }, |
503 set lastHit(value) | 485 set lastHit(value) |
504 { | 486 { |
505 if (value != this._lastHit) | 487 if (value != this._lastHit) |
506 { | 488 { |
507 let oldValue = this._lastHit; | 489 let oldValue = this._lastHit; |
508 this._lastHit = value; | 490 this._lastHit = value; |
509 | |
510 if (value && this.ephemeral) | |
511 Filter.knownFilters.set(this.text, this); | |
512 | |
513 filterNotifier.emit("filter.lastHit", this, value, oldValue); | 491 filterNotifier.emit("filter.lastHit", this, value, oldValue); |
514 } | 492 } |
515 return this._lastHit; | 493 return this._lastHit; |
516 }, | 494 }, |
517 | 495 |
518 /** | 496 /** |
519 * String that the domains property should be generated from | 497 * String that the domains property should be generated from |
520 * @type {?string} | 498 * @type {?string} |
521 */ | 499 */ |
522 domainSource: null, | 500 domainSource: null, |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
703 * @return {boolean} | 681 * @return {boolean} |
704 */ | 682 */ |
705 isGeneric() | 683 isGeneric() |
706 { | 684 { |
707 let {sitekeys, domains} = this; | 685 let {sitekeys, domains} = this; |
708 | 686 |
709 return !(sitekeys && sitekeys.length) && (!domains || domains.get("")); | 687 return !(sitekeys && sitekeys.length) && (!domains || domains.get("")); |
710 }, | 688 }, |
711 | 689 |
712 /** | 690 /** |
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 /** | |
722 * See Filter.serialize() | 691 * See Filter.serialize() |
723 * @inheritdoc | 692 * @inheritdoc |
724 */ | 693 */ |
725 *serialize() | 694 *serialize() |
726 { | 695 { |
727 if (this.shouldSerialize()) | 696 let {_disabled, _hitCount, _lastHit} = this; |
728 { | 697 |
729 let {_disabled, _hitCount, _lastHit} = this; | 698 if (_disabled || _hitCount || _lastHit) |
699 { | |
730 yield* Filter.prototype.serialize.call(this); | 700 yield* Filter.prototype.serialize.call(this); |
731 if (_disabled) | 701 if (_disabled) |
732 yield "disabled=true"; | 702 yield "disabled=true"; |
733 if (_hitCount) | 703 if (_hitCount) |
734 yield "hitCount=" + _hitCount; | 704 yield "hitCount=" + _hitCount; |
735 if (_lastHit) | 705 if (_lastHit) |
736 yield "lastHit=" + _lastHit; | 706 yield "lastHit=" + _lastHit; |
737 } | 707 } |
738 } | 708 } |
739 }); | 709 }); |
(...skipping 687 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1427 | 1397 |
1428 /** | 1398 /** |
1429 * Script that should be executed | 1399 * Script that should be executed |
1430 * @type {string} | 1400 * @type {string} |
1431 */ | 1401 */ |
1432 get script() | 1402 get script() |
1433 { | 1403 { |
1434 return this.body; | 1404 return this.body; |
1435 } | 1405 } |
1436 }); | 1406 }); |
LEFT | RIGHT |