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

Delta Between Two Patch Sets: lib/filterClasses.js

Issue 29841558: Issue 6815 - Avoid multiple copies of domain maps (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Created July 29, 2018, 3:01 p.m.
Right Patch Set: Add tests Created July 30, 2018, 5:11 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 | test/filterClasses.js » ('j') | no next file with change/comment »
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 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 */ 397 */
398 domainSourceIsLowerCase: false, 398 domainSourceIsLowerCase: false,
399 399
400 /** 400 /**
401 * Map containing domains that this filter should match on/not match 401 * Map containing domains that this filter should match on/not match
402 * on or null if the filter should match on all domains 402 * on or null if the filter should match on all domains
403 * @type {?Map.<string,boolean>} 403 * @type {?Map.<string,boolean>}
404 */ 404 */
405 get domains() 405 get domains()
406 { 406 {
407 let prop = Object.getOwnPropertyDescriptor(this, "_domains");
408 if (prop)
409 {
410 let {value} = prop;
411 return typeof value == "string" ?
412 new Map([[value, true], ["", false]]) : value;
413 }
414
415 let domains = null; 407 let domains = null;
416 408
417 if (this.domainSource) 409 if (this.domainSource)
418 { 410 {
419 let source = this.domainSource; 411 let source = this.domainSource;
420 if (!this.domainSourceIsLowerCase) 412 if (!this.domainSourceIsLowerCase)
421 { 413 {
422 // RegExpFilter already have lowercase domains 414 // RegExpFilter already have lowercase domains
423 source = source.toLowerCase(); 415 source = source.toLowerCase();
424 } 416 }
425 417
426 let knownMap = knownDomainMaps.get(source); 418 let knownMap = knownDomainMaps.get(source);
427 if (knownMap) 419 if (knownMap)
428 { 420 {
429 domains = knownMap; 421 domains = knownMap;
430 } 422 }
431 else 423 else
432 { 424 {
433 let list = source.split(this.domainSeparator); 425 let list = source.split(this.domainSeparator);
434 if (list.length == 1 && list[0][0] != "~") 426 if (list.length == 1 && list[0][0] != "~")
435 { 427 {
436 // Fast track for the common one-domain scenario 428 // Fast track for the common one-domain scenario
437 domains = list[0]; 429 domains = new Map([[list[0], true], ["", false]]);
438 } 430 }
439 else 431 else
440 { 432 {
441 let hasIncludes = false; 433 let hasIncludes = false;
442 for (let i = 0; i < list.length; i++) 434 for (let i = 0; i < list.length; i++)
443 { 435 {
444 let domain = list[i]; 436 let domain = list[i];
445 if (domain == "") 437 if (domain == "")
446 continue; 438 continue;
447 439
448 let include; 440 let include;
449 if (domain[0] == "~") 441 if (domain[0] == "~")
450 { 442 {
451 include = false; 443 include = false;
452 domain = domain.substr(1); 444 domain = domain.substr(1);
453 } 445 }
454 else 446 else
455 { 447 {
456 include = true; 448 include = true;
457 hasIncludes = true; 449 hasIncludes = true;
458 } 450 }
459 451
460 if (!domains) 452 if (!domains)
461 domains = new Map(); 453 domains = new Map();
462 454
463 domains.set(domain, include); 455 domains.set(domain, include);
464 } 456 }
465 457
466 if (domains) 458 if (domains)
467 {
468 domains.set("", !hasIncludes); 459 domains.set("", !hasIncludes);
469 knownDomainMaps.set(source, domains);
470 }
471 } 460 }
461
462 if (domains)
463 knownDomainMaps.set(source, domains);
472 } 464 }
473 465
474 this.domainSource = null; 466 this.domainSource = null;
475 } 467 }
476 468
477 Object.defineProperty(this, "_domains", {value: domains}); 469 Object.defineProperty(this, "domains", {value: domains});
478 return this.domains; 470 return this.domains;
479 }, 471 },
480 472
481 /** 473 /**
482 * Array containing public keys of websites that this filter should apply to 474 * Array containing public keys of websites that this filter should apply to
483 * @type {?string[]} 475 * @type {?string[]}
484 */ 476 */
485 sitekeys: null, 477 sitekeys: null,
486 478
487 /** 479 /**
488 * Checks whether this filter is active on a domain. 480 * Checks whether this filter is active on a domain.
489 * @param {string} [docDomain] domain name of the document that loads the URL 481 * @param {string} [docDomain] domain name of the document that loads the URL
490 * @param {string} [sitekey] public key provided by the document 482 * @param {string} [sitekey] public key provided by the document
491 * @return {boolean} true in case of the filter being active 483 * @return {boolean} true in case of the filter being active
492 */ 484 */
493 isActiveOnDomain(docDomain, sitekey) 485 isActiveOnDomain(docDomain, sitekey)
494 { 486 {
495 // Sitekeys are case-sensitive so we shouldn't convert them to 487 // Sitekeys are case-sensitive so we shouldn't convert them to
496 // upper-case to avoid false positives here. Instead we need to 488 // upper-case to avoid false positives here. Instead we need to
497 // change the way filter options are parsed. 489 // change the way filter options are parsed.
498 if (this.sitekeys && 490 if (this.sitekeys &&
499 (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase()) < 0)) 491 (!sitekey || this.sitekeys.indexOf(sitekey.toUpperCase()) < 0))
500 { 492 {
501 return false; 493 return false;
502 } 494 }
503 495
496 let {domains} = this;
497
504 // If no domains are set the rule matches everywhere 498 // If no domains are set the rule matches everywhere
505 if (!this.domains) 499 if (!domains)
506 return true; 500 return true;
507 501
508 // If the document has no host name, match only if the filter 502 // If the document has no host name, match only if the filter
509 // isn't restricted to specific domains 503 // isn't restricted to specific domains
510 if (!docDomain) 504 if (!docDomain)
511 return this.domains.get(""); 505 return domains.get("");
512 506
513 docDomain = docDomain.replace(/\.+$/, "").toLowerCase(); 507 docDomain = docDomain.replace(/\.+$/, "").toLowerCase();
514 508
515 while (true) 509 while (true)
516 { 510 {
517 let isDomainIncluded = this.domains.get(docDomain); 511 let isDomainIncluded = domains.get(docDomain);
518 if (typeof isDomainIncluded != "undefined") 512 if (typeof isDomainIncluded != "undefined")
519 return isDomainIncluded; 513 return isDomainIncluded;
520 514
521 let nextDot = docDomain.indexOf("."); 515 let nextDot = docDomain.indexOf(".");
522 if (nextDot < 0) 516 if (nextDot < 0)
523 break; 517 break;
524 docDomain = docDomain.substr(nextDot + 1); 518 docDomain = docDomain.substr(nextDot + 1);
525 } 519 }
526 return this.domains.get(""); 520 return domains.get("");
527 }, 521 },
528 522
529 /** 523 /**
530 * Checks whether this filter is active only on a domain and its subdomains. 524 * Checks whether this filter is active only on a domain and its subdomains.
531 * @param {string} docDomain 525 * @param {string} docDomain
532 * @return {boolean} 526 * @return {boolean}
533 */ 527 */
534 isActiveOnlyOnDomain(docDomain) 528 isActiveOnlyOnDomain(docDomain)
535 { 529 {
536 if (!docDomain || !this.domains || this.domains.get("")) 530 let {domains} = this;
531
532 if (!docDomain || !domains || domains.get(""))
537 return false; 533 return false;
538 534
539 docDomain = docDomain.replace(/\.+$/, "").toLowerCase(); 535 docDomain = docDomain.replace(/\.+$/, "").toLowerCase();
540 536
541 for (let [domain, isIncluded] of this.domains) 537 for (let [domain, isIncluded] of domains)
542 { 538 {
543 if (isIncluded && domain != docDomain) 539 if (isIncluded && domain != docDomain)
544 { 540 {
545 if (domain.length <= docDomain.length) 541 if (domain.length <= docDomain.length)
546 return false; 542 return false;
547 543
548 if (!domain.endsWith("." + docDomain)) 544 if (!domain.endsWith("." + docDomain))
549 return false; 545 return false;
550 } 546 }
551 } 547 }
552 548
553 return true; 549 return true;
554 }, 550 },
555 551
556 /** 552 /**
557 * Checks whether this filter is generic or specific 553 * Checks whether this filter is generic or specific
558 * @return {boolean} 554 * @return {boolean}
559 */ 555 */
560 isGeneric() 556 isGeneric()
561 { 557 {
562 return !(this.sitekeys && this.sitekeys.length) && 558 let {sitekeys, domains} = this;
563 (!this.domains || this.domains.get("")); 559
560 return !(sitekeys && sitekeys.length) && (!domains || domains.get(""));
564 }, 561 },
565 562
566 /** 563 /**
567 * See Filter.serialize() 564 * See Filter.serialize()
568 * @inheritdoc 565 * @inheritdoc
569 */ 566 */
570 serialize(buffer) 567 serialize(buffer)
571 { 568 {
572 if (this._disabled || this._hitCount || this._lastHit) 569 if (this._disabled || this._hitCount || this._lastHit)
573 { 570 {
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after
1186 1183
1187 /** 1184 /**
1188 * Script that should be executed 1185 * Script that should be executed
1189 * @type {string} 1186 * @type {string}
1190 */ 1187 */
1191 get script() 1188 get script()
1192 { 1189 {
1193 return this.body; 1190 return this.body;
1194 } 1191 }
1195 }); 1192 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld