fixed flawed 1st-/3rd-party test

This commit is contained in:
gorhill 2015-01-08 10:37:19 -05:00
parent bdf770a1bb
commit 0c152b2859
4 changed files with 103 additions and 29 deletions

View File

@ -15,6 +15,7 @@
<script src="js/async.js"></script> <script src="js/async.js"></script>
<script src="js/liquid-dict.js"></script> <script src="js/liquid-dict.js"></script>
<script src="js/utils.js"></script> <script src="js/utils.js"></script>
<script src="js/uritools.js"></script>
<script src="js/assets.js"></script> <script src="js/assets.js"></script>
<script src="js/dynamic-net-filtering.js"></script> <script src="js/dynamic-net-filtering.js"></script>
<script src="js/static-net-filtering.js"></script> <script src="js/static-net-filtering.js"></script>
@ -24,7 +25,6 @@
<script src="js/profiler.js"></script> <script src="js/profiler.js"></script>
<script src="js/storage.js"></script> <script src="js/storage.js"></script>
<script src="js/pagestore.js"></script> <script src="js/pagestore.js"></script>
<script src="js/uritools.js"></script>
<script src="js/tab.js"></script> <script src="js/tab.js"></script>
<script src="js/traffic.js"></script> <script src="js/traffic.js"></script>
<script src="js/contextmenu.js"></script> <script src="js/contextmenu.js"></script>

View File

@ -194,25 +194,24 @@ Matrix.prototype.evaluateCell = function(srcHostname, desHostname, type) {
Matrix.prototype.clearRegisters = function() { Matrix.prototype.clearRegisters = function() {
this.r = 0; this.r = 0;
this.type = ''; this.type = this.y = this.z = '';
this.y = '';
this.z = '';
return this; return this;
}; };
/******************************************************************************/ /******************************************************************************/
var isFirstParty = function(srcHostname, desHostname) { var is3rdParty = function(srcHostname, desHostname) {
if ( desHostname.slice(0 - srcHostname.length) !== srcHostname ) { var srcDomain = domainFromHostname(srcHostname);
return false; if ( desHostname.slice(0 - srcDomain.length) !== srcDomain ) {
}
// Be sure to not confuse 'example.com' with 'anotherexample.com'
if ( desHostname.lenght === srcHostname.lenght ) {
return true; return true;
} }
return desHostname.charAt(desHostname.length - srcHostname.length - 1) === '.'; // Do not confuse 'example.com' with 'anotherexample.com'
return desHostname.lenght !== srcDomain.lenght &&
desHostname.charAt(desHostname.length - srcDomain.length - 1) !== '.';
}; };
var domainFromHostname = µBlock.URI.domainFromHostname;
/******************************************************************************/ /******************************************************************************/
Matrix.prototype.evaluateCellZ = function(srcHostname, desHostname, type) { Matrix.prototype.evaluateCellZ = function(srcHostname, desHostname, type) {
@ -262,20 +261,18 @@ Matrix.prototype.evaluateCellZY = function(srcHostname, desHostname, type) {
this.y = '*'; this.y = '*';
if ( type === 'script' ) { if ( type === 'script' ) {
type = isFirstParty(srcHostname, desHostname) ? '1p-script' : '3p-script'; type = is3rdParty(srcHostname, desHostname) ? '3p-script' : '1p-script';
} else if ( type === 'sub_frame' && isFirstParty(srcHostname, desHostname) === false ) { } else if ( type === 'sub_frame' && is3rdParty(srcHostname, desHostname) ) {
type = '3p-frame'; type = '3p-frame';
} }
// Is this a type suitable for dynamic filtering purpose? // Is this a type suitable for dynamic filtering purpose?
if ( supportedDynamicTypes.hasOwnProperty(type) === false ) { if ( supportedDynamicTypes.hasOwnProperty(type) ) {
this.type = ''; this.type = type;
return this; this.r = this.evaluateCellZ(srcHostname, '*', type);
if ( this.r !== 0 ) { return this; }
} }
this.type = type; this.type = '';
this.r = this.evaluateCellZ(srcHostname, '*', type);
return this; return this;
}; };

View File

@ -109,7 +109,7 @@ var µb = µBlock;
var getHostnameDict = function(hostnameToCountMap) { var getHostnameDict = function(hostnameToCountMap) {
var r = {}; var r = {};
var µburi = µb.URI; var domainFromHostname = µb.URI.domainFromHostname;
var domain, counts; var domain, counts;
for ( var hostname in hostnameToCountMap ) { for ( var hostname in hostnameToCountMap ) {
if ( hostnameToCountMap.hasOwnProperty(hostname) === false ) { if ( hostnameToCountMap.hasOwnProperty(hostname) === false ) {
@ -118,7 +118,7 @@ var getHostnameDict = function(hostnameToCountMap) {
if ( r.hasOwnProperty(hostname) ) { if ( r.hasOwnProperty(hostname) ) {
continue; continue;
} }
domain = µburi.domainFromHostname(hostname); domain = domainFromHostname(hostname);
counts = hostnameToCountMap[domain] || 0; counts = hostnameToCountMap[domain] || 0;
r[domain] = { r[domain] = {
domain: domain, domain: domain,

View File

@ -274,21 +274,98 @@ URI.hostnameFromURI = function(uri) {
/******************************************************************************/ /******************************************************************************/
// It is expected that there is higher-scoped `publicSuffixList` lingering
// somewhere. Cache it. See <https://github.com/gorhill/publicsuffixlist.js>.
var psl = publicSuffixList;
URI.domainFromHostname = function(hostname) { URI.domainFromHostname = function(hostname) {
if ( !reIPAddressNaive.test(hostname) ) { // Try to skip looking up the PSL database
return psl.getDomain(hostname); if ( domainCache.hasOwnProperty(hostname) ) {
var entry = domainCache[hostname];
entry.tstamp = Date.now();
return entry.domain;
} }
return hostname; // Meh.. will have to search it
if ( reIPAddressNaive.test(hostname) === false ) {
return domainCacheAdd(hostname, psl.getDomain(hostname));
}
return domainCacheAdd(hostname, hostname);
}; };
URI.domain = function() { URI.domain = function() {
return this.domainFromHostname(this.hostname); return this.domainFromHostname(this.hostname);
}; };
// It is expected that there is higher-scoped `publicSuffixList` lingering
// somewhere. Cache it. See <https://github.com/gorhill/publicsuffixlist.js>.
var psl = publicSuffixList;
/******************************************************************************/
// Trying to alleviate the worries of looking up too often the domain name from
// a hostname. With a cache, uBlock benefits given that it deals with a
// specific set of hostnames within a narrow time span -- in other words, I
// believe probability of cache hit are high in uBlock.
var DomainCacheEntry = function(domain) {
this.init(domain);
};
DomainCacheEntry.prototype.init = function(domain) {
this.domain = domain;
this.tstamp = Date.now();
return this;
};
DomainCacheEntry.prototype.dispose = function() {
this.domain = '';
if ( domainCacheEntryJunkyard.length < 25 ) {
domainCacheEntryJunkyard.push(this);
}
};
var domainCacheEntryFactory = function(domain) {
var entry = domainCacheEntryJunkyard.pop();
if ( entry ) {
return entry.init(domain);
}
return new DomainCacheEntry(domain);
};
var domainCacheEntryJunkyard = [];
var domainCacheAdd = function(hostname, domain) {
if ( domainCache.hasOwnProperty(hostname) ) {
domainCache[hostname].tstamp = Date.now();
} else {
domainCache[hostname] = domainCacheEntryFactory(domain);
domainCacheCount += 1;
if ( domainCacheCount === domainCacheCountHighWaterMark ) {
domainCachePrune();
}
}
return domain;
};
var domainCacheEntrySort = function(a, b) {
return b.tstamp - a.tstamp;
};
var domainCachePrune = function() {
var hostnames = Object.keys(domainCache)
.sort(domainCacheEntrySort)
.slice(domainCacheCountLowWaterMark);
var i = hostnames.length;
domainCacheCount -= i;
var hostname;
while ( i-- ) {
hostname = hostnames[i];
domainCache[hostname].dispose();
delete domainCache[hostname];
}
};
var domainCache = {};
var domainCacheCount = 0;
var domainCacheCountLowWaterMark = 75;
var domainCacheCountHighWaterMark = 100;
/******************************************************************************/ /******************************************************************************/
URI.domainFromURI = function(uri) { URI.domainFromURI = function(uri) {