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

Side by Side Diff: lib/filterClasses.js

Issue 30000586: Issue 7265 - Orgnanize request blocking filters by domain (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebase Created Feb. 7, 2019, 3:45 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/matcher.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 480 matching lines...) Expand 10 before | Expand all | Expand 10 after
491 * Map containing domains that this filter should match on/not match 491 * Map containing domains that this filter should match on/not match
492 * on or null if the filter should match on all domains 492 * on or null if the filter should match on all domains
493 * @type {?Map.<string,boolean>} 493 * @type {?Map.<string,boolean>}
494 */ 494 */
495 get domains() 495 get domains()
496 { 496 {
497 let domains = null; 497 let domains = null;
498 498
499 if (this.domainSource) 499 if (this.domainSource)
500 { 500 {
501 // For some filter types this property is accessed only rarely, 501 // For most filter types this property is accessed only rarely,
502 // especially when the subscriptions are initially loaded. We defer any 502 // especially when the subscriptions are initially loaded. We defer any
503 // caching for such filters. 503 // caching by default.
504 let {cacheDomains} = this; 504 let cacheDomains = this._cacheDomains;
505 505
506 let source = this.domainSource.toLowerCase(); 506 let source = this.domainSource.toLowerCase();
507 507
508 let knownMap = knownDomainMaps.get(source); 508 let knownMap = knownDomainMaps.get(source);
509 if (knownMap) 509 if (knownMap)
510 { 510 {
511 domains = knownMap; 511 domains = knownMap;
512 } 512 }
513 else 513 else
514 { 514 {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
553 knownDomainMaps.set(source, domains); 553 knownDomainMaps.set(source, domains);
554 } 554 }
555 555
556 if (!domains || cacheDomains) 556 if (!domains || cacheDomains)
557 { 557 {
558 this.domainSource = null; 558 this.domainSource = null;
559 Object.defineProperty(this, "domains", {value: domains}); 559 Object.defineProperty(this, "domains", {value: domains});
560 } 560 }
561 } 561 }
562 562
563 this._cacheDomains = true;
564
563 return domains; 565 return domains;
564 }, 566 },
565 567
566 /** 568 /**
567 * Whether the value of {@link ActiveFilter#domains} should be cached. 569 * Whether the value of {@link ActiveFilter#domains} should be cached.
568 * Defaults to <code>true</code>, but may be overridden by subclasses that
569 * don't want the value to be cached (for better memory usage).
570 * @type {boolean} 570 * @type {boolean}
571 * @protected 571 * @private
572 */ 572 */
573 cacheDomains: true, 573 _cacheDomains: false,
574 574
575 /** 575 /**
576 * Array containing public keys of websites that this filter should apply to 576 * Array containing public keys of websites that this filter should apply to
577 * @type {?string[]} 577 * @type {?string[]}
578 */ 578 */
579 sitekeys: null, 579 sitekeys: null,
580 580
581 /** 581 /**
582 * Checks whether this filter is active on a domain. 582 * Checks whether this filter is active on a domain.
583 * @param {string} [docDomain] domain name of the document that loads the URL 583 * @param {string} [docDomain] domain name of the document that loads the URL
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 this.pattern = regexpSource; 757 this.pattern = regexpSource;
758 } 758 }
759 } 759 }
760 exports.RegExpFilter = RegExpFilter; 760 exports.RegExpFilter = RegExpFilter;
761 761
762 RegExpFilter.prototype = extend(ActiveFilter, { 762 RegExpFilter.prototype = extend(ActiveFilter, {
763 /** 763 /**
764 * Number of filters contained, will always be 1 (required to 764 * Number of filters contained, will always be 1 (required to
765 * optimize {@link Matcher}). 765 * optimize {@link Matcher}).
766 * @type {number} 766 * @type {number}
767 * @package
767 */ 768 */
768 size: 1, 769 size: 1,
769 770
770 /** 771 /**
771 * @see ActiveFilter.domainSeparator 772 * @see ActiveFilter.domainSeparator
772 */ 773 */
773 domainSeparator: "|", 774 domainSeparator: "|",
774 775
775 /** 776 /**
776 * Expression from which a regular expression should be generated - 777 * Expression from which a regular expression should be generated -
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 { 871 {
871 return (this.contentType & typeMask) != 0 && 872 return (this.contentType & typeMask) != 0 &&
872 (this.thirdParty == null || this.thirdParty == thirdParty) && 873 (this.thirdParty == null || this.thirdParty == thirdParty) &&
873 (this.regexp ? (this.isActiveOnDomain(docDomain, sitekey) && 874 (this.regexp ? (this.isActiveOnDomain(docDomain, sitekey) &&
874 this.matchesLocation(location)) : 875 this.matchesLocation(location)) :
875 (this.matchesLocation(location) && 876 (this.matchesLocation(location) &&
876 this.isActiveOnDomain(docDomain, sitekey))); 877 this.isActiveOnDomain(docDomain, sitekey)));
877 }, 878 },
878 879
879 /** 880 /**
881 * Checks whether the given URL matches this filter without checking the
882 * filter's domains.
883 * @param {string} location
884 * @param {number} typeMask
885 * @param {boolean} [thirdParty]
886 * @param {string} [sitekey]
887 * @return {boolean}
888 * @package
889 */
890 matchesWithoutDomain(location, typeMask, thirdParty, sitekey)
891 {
892 return (this.contentType & typeMask) != 0 &&
893 (this.thirdParty == null || this.thirdParty == thirdParty) &&
894 this.matchesLocation(location) &&
895 (!this.sitekeys ||
896 (sitekey && this.sitekeys.includes(sitekey.toUpperCase())));
897 },
898
899 /**
880 * Checks whether the given URL matches this filter's pattern. 900 * Checks whether the given URL matches this filter's pattern.
881 * @param {string} location The URL to check. 901 * @param {string} location The URL to check.
882 * @param {?string} [lowerCaseLocation] The lower-case version of the URL to 902 * @param {?string} [lowerCaseLocation] The lower-case version of the URL to
883 * check, for case-insensitive matching. 903 * check, for case-insensitive matching.
884 * @returns {boolean} <code>true</code> if the URL matches. 904 * @returns {boolean} <code>true</code> if the URL matches.
885 * @package 905 * @package
886 */ 906 */
887 matchesLocation(location, lowerCaseLocation) 907 matchesLocation(location, lowerCaseLocation)
888 { 908 {
889 let {regexp} = this; 909 let {regexp} = this;
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 return this.contentType == RegExpFilter.prototype.contentType && 946 return this.contentType == RegExpFilter.prototype.contentType &&
927 this.thirdParty == null && 947 this.thirdParty == null &&
928 !this.domainSource && !this.sitekeySource && 948 !this.domainSource && !this.sitekeySource &&
929 !this.domains && !this.sitekeys; 949 !this.domains && !this.sitekeys;
930 } 950 }
931 }); 951 });
932 952
933 /** 953 /**
934 * Yields the filter itself (required to optimize {@link Matcher}). 954 * Yields the filter itself (required to optimize {@link Matcher}).
935 * @yields {RegExpFilter} 955 * @yields {RegExpFilter}
956 * @package
936 */ 957 */
937 RegExpFilter.prototype[Symbol.iterator] = function*() 958 RegExpFilter.prototype[Symbol.iterator] = function*()
938 { 959 {
939 yield this; 960 yield this;
940 }; 961 };
941 962
942 /** 963 /**
964 * Yields a key-value pair consisting of the filter itself and the value
965 * <code>true</code> (required to optimize {@link Matcher}).
966 * @yields {Array}
967 * @package
968 */
969 RegExpFilter.prototype.entries = function*()
970 {
971 yield [this, true];
972 };
973
974 /**
943 * Creates a RegExp filter from its text representation 975 * Creates a RegExp filter from its text representation
944 * @param {string} text same as in Filter() 976 * @param {string} text same as in Filter()
945 * @return {Filter} 977 * @return {Filter}
946 */ 978 */
947 RegExpFilter.fromText = function(text) 979 RegExpFilter.fromText = function(text)
948 { 980 {
949 let blocking = true; 981 let blocking = true;
950 let origText = text; 982 let origText = text;
951 if (text[0] == "@" && text[1] == "@") 983 if (text[0] == "@" && text[1] == "@")
952 { 984 {
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after
1324 * @augments ContentFilter 1356 * @augments ContentFilter
1325 */ 1357 */
1326 function ElemHideBase(text, domains, selector) 1358 function ElemHideBase(text, domains, selector)
1327 { 1359 {
1328 ContentFilter.call(this, text, domains, selector); 1360 ContentFilter.call(this, text, domains, selector);
1329 } 1361 }
1330 exports.ElemHideBase = ElemHideBase; 1362 exports.ElemHideBase = ElemHideBase;
1331 1363
1332 ElemHideBase.prototype = extend(ContentFilter, { 1364 ElemHideBase.prototype = extend(ContentFilter, {
1333 /** 1365 /**
1334 * @see ActiveFilter#domains
1335 * @type {?Map.<string,boolean>}
1336 */
1337 get domains()
1338 {
1339 let {get} = Object.getOwnPropertyDescriptor(ActiveFilter.prototype,
1340 "domains");
1341 let value = get.call(this);
1342 this.cacheDomains = true;
1343 return value;
1344 },
1345
1346 /**
1347 * Initially <code>false</code>, but set to <code>true</code> after
1348 * {@link ActiveFilter#domains} has been accessed once.
1349 * @see ActiveFilter#cacheDomains
1350 * @type {boolean}
1351 * @protected
1352 */
1353 cacheDomains: false,
1354
1355 /**
1356 * CSS selector for the HTML elements that should be hidden 1366 * CSS selector for the HTML elements that should be hidden
1357 * @type {string} 1367 * @type {string}
1358 */ 1368 */
1359 get selector() 1369 get selector()
1360 { 1370 {
1361 // Braces are being escaped to prevent CSS rule injection. 1371 // Braces are being escaped to prevent CSS rule injection.
1362 return this.body.replace("{", "\\7B ").replace("}", "\\7D "); 1372 return this.body.replace("{", "\\7B ").replace("}", "\\7D ");
1363 } 1373 }
1364 }); 1374 });
1365 1375
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
1436 1446
1437 /** 1447 /**
1438 * Script that should be executed 1448 * Script that should be executed
1439 * @type {string} 1449 * @type {string}
1440 */ 1450 */
1441 get script() 1451 get script()
1442 { 1452 {
1443 return this.body; 1453 return this.body;
1444 } 1454 }
1445 }); 1455 });
OLDNEW
« no previous file with comments | « no previous file | lib/matcher.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld