1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-11-17 16:02:33 +01:00
uBlock/src/js/pagestore.js

491 lines
16 KiB
JavaScript
Raw Normal View History

2014-06-24 00:42:43 +02:00
/*******************************************************************************
µBlock - a Chromium browser extension to block requests.
Copyright (C) 2014 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/uBlock
*/
2014-10-02 22:45:26 +02:00
/* jshint bitwise: false */
2014-10-19 16:44:09 +02:00
/* global µBlock */
'use strict';
2014-06-24 00:42:43 +02:00
/*******************************************************************************
A PageRequestStore object is used to store net requests in two ways:
To record distinct net requests
To create a log of net requests
**/
/******************************************************************************/
/******************************************************************************/
µBlock.PageStore = (function() {
/******************************************************************************/
var µb = µBlock;
2014-09-14 22:20:40 +02:00
/******************************************************************************/
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2014-09-14 22:20:40 +02:00
// To mitigate memory churning
var netFilteringResultCacheEntryJunkyard = [];
var netFilteringResultCacheEntryJunkyardMax = 200;
/******************************************************************************/
2014-10-02 22:45:26 +02:00
var NetFilteringResultCacheEntry = function(result, type, flags) {
this.init(result, type, flags);
2014-09-14 22:20:40 +02:00
};
/******************************************************************************/
2014-10-02 22:45:26 +02:00
NetFilteringResultCacheEntry.prototype.init = function(result, type, flags) {
this.result = result;
this.type = type;
this.flags = flags;
2014-09-14 22:20:40 +02:00
this.time = Date.now();
};
/******************************************************************************/
NetFilteringResultCacheEntry.prototype.dispose = function() {
2014-10-02 22:45:26 +02:00
this.result = '';
this.type = '';
2014-09-14 22:20:40 +02:00
if ( netFilteringResultCacheEntryJunkyard.length < netFilteringResultCacheEntryJunkyardMax ) {
netFilteringResultCacheEntryJunkyard.push(this);
}
};
2014-09-14 22:20:40 +02:00
/******************************************************************************/
2014-10-02 22:45:26 +02:00
NetFilteringResultCacheEntry.factory = function(result, type, flags) {
2014-09-14 22:20:40 +02:00
var entry = netFilteringResultCacheEntryJunkyard.pop();
if ( entry === undefined ) {
2014-10-02 22:45:26 +02:00
entry = new NetFilteringResultCacheEntry(result, type, flags);
2014-09-14 22:20:40 +02:00
} else {
2014-10-02 22:45:26 +02:00
entry.init(result, type, flags);
2014-09-14 22:20:40 +02:00
}
return entry;
};
/******************************************************************************/
/******************************************************************************/
// To mitigate memory churning
var uidGenerator = 1;
var netFilteringCacheJunkyard = [];
var netFilteringCacheJunkyardMax = 10;
/******************************************************************************/
var NetFilteringResultCache = function() {
this.init();
};
/******************************************************************************/
NetFilteringResultCache.factory = function() {
var entry = netFilteringCacheJunkyard.pop();
if ( entry === undefined ) {
entry = new NetFilteringResultCache();
} else {
entry.init();
}
return entry;
};
/******************************************************************************/
NetFilteringResultCache.prototype.init = function() {
this.uname = 'NetFilteringResultCache:' + uidGenerator++;
this.urls = {};
this.count = 0;
this.shelfLife = 60 * 1000;
};
/******************************************************************************/
NetFilteringResultCache.prototype.dispose = function() {
for ( var key in this.urls ) {
if ( this.urls.hasOwnProperty(key) === false ) {
continue;
}
2014-09-14 22:20:40 +02:00
this.urls[key].dispose();
}
µBlock.asyncJobs.remove(this.uname);
this.uname = '';
this.urls = {};
this.count = 0;
if ( netFilteringCacheJunkyard.length < netFilteringCacheJunkyardMax ) {
netFilteringCacheJunkyard.push(this);
}
return null;
};
/******************************************************************************/
2014-10-02 22:45:26 +02:00
NetFilteringResultCache.prototype.add = function(url, result, type, flags) {
2014-09-14 22:20:40 +02:00
var entry = this.urls[url];
if ( entry !== undefined ) {
2014-10-02 22:45:26 +02:00
entry.result = result;
entry.type = type;
entry.flags = flags;
2014-09-14 22:20:40 +02:00
entry.time = Date.now();
return;
}
2014-10-02 22:45:26 +02:00
this.urls[url] = NetFilteringResultCacheEntry.factory(result, type, flags);
2014-09-14 22:20:40 +02:00
if ( this.count === 0 ) {
this.pruneAsync();
}
this.count += 1;
};
/******************************************************************************/
NetFilteringResultCache.prototype.fetchAll = function() {
return this.urls;
};
/******************************************************************************/
NetFilteringResultCache.prototype.compareEntries = function(a, b) {
return this.urls[b].time - this.urls[a].time;
};
/******************************************************************************/
NetFilteringResultCache.prototype.prune = function() {
var keys = Object.keys(this.urls).sort(this.compareEntries.bind(this));
var obsolete = Date.now() - this.shelfLife;
var key, entry;
var i = keys.length;
while ( i-- ) {
key = keys[i];
entry = this.urls[key];
if ( entry.time > obsolete ) {
break;
}
2014-09-14 22:20:40 +02:00
entry.dispose();
delete this.urls[key];
}
this.count -= keys.length - i - 1;
if ( this.count > 0 ) {
this.pruneAsync();
}
};
2014-10-02 22:45:26 +02:00
// https://www.youtube.com/watch?v=hcVpbsDyOhM
2014-09-14 22:20:40 +02:00
/******************************************************************************/
NetFilteringResultCache.prototype.pruneAsync = function() {
µBlock.asyncJobs.add(
this.uname,
null,
this.prune.bind(this),
this.shelfLife + 120000,
false
);
};
/******************************************************************************/
NetFilteringResultCache.prototype.lookup = function(url) {
2014-10-02 22:45:26 +02:00
return this.urls[url];
2014-09-14 22:20:40 +02:00
};
/******************************************************************************/
/******************************************************************************/
// To mitigate memory churning
var frameStoreJunkyard = [];
var frameStoreJunkyardMax = 50;
/******************************************************************************/
var FrameStore = function(rootHostname, frameURL) {
this.init(rootHostname, frameURL);
};
/******************************************************************************/
FrameStore.factory = function(rootHostname, frameURL) {
2014-09-14 22:20:40 +02:00
var entry = frameStoreJunkyard.pop();
if ( entry === undefined ) {
entry = new FrameStore(rootHostname, frameURL);
2014-09-14 22:20:40 +02:00
} else {
entry.init(rootHostname, frameURL);
2014-09-14 22:20:40 +02:00
}
return entry;
};
/******************************************************************************/
FrameStore.prototype.init = function(rootHostname, frameURL) {
var µburi = µb.URI;
this.pageHostname = µburi.hostnameFromURI(frameURL);
this.pageDomain = µburi.domainFromHostname(this.pageHostname) || this.pageHostname;
this.rootHostname = rootHostname;
this.rootDomain = µburi.domainFromHostname(rootHostname) || rootHostname;
return this;
};
/******************************************************************************/
FrameStore.prototype.dispose = function() {
this.pageHostname = this.pageDomain =
this.rootHostname = this.rootDomain = '';
2014-09-14 22:20:40 +02:00
if ( frameStoreJunkyard.length < frameStoreJunkyardMax ) {
frameStoreJunkyard.push(this);
}
return null;
};
2014-09-14 22:20:40 +02:00
/******************************************************************************/
/******************************************************************************/
2014-09-14 22:20:40 +02:00
// To mitigate memory churning
var pageStoreJunkyard = [];
var pageStoreJunkyardMax = 10;
/******************************************************************************/
// Cache only what is worth it if logging is disabled
// http://jsperf.com/string-indexof-vs-object
var collapsibleRequestTypes = 'image sub_frame object';
2014-06-24 00:42:43 +02:00
/******************************************************************************/
var PageStore = function(tabId, pageURL) {
2014-06-24 00:42:43 +02:00
this.init(tabId, pageURL);
};
2014-06-24 00:42:43 +02:00
/******************************************************************************/
2014-09-14 22:20:40 +02:00
PageStore.factory = function(tabId, pageURL) {
var entry = pageStoreJunkyard.pop();
if ( entry === undefined ) {
entry = new PageStore(tabId, pageURL);
} else {
entry.init(tabId, pageURL);
}
return entry;
};
/******************************************************************************/
2014-06-24 00:42:43 +02:00
PageStore.prototype.init = function(tabId, pageURL) {
this.tabId = tabId;
2014-07-14 17:24:59 +02:00
this.previousPageURL = '';
this.pageURL = pageURL;
this.pageHostname = µb.URI.hostnameFromURI(pageURL);
2014-08-27 08:01:10 +02:00
// https://github.com/gorhill/uBlock/issues/185
// Use hostname if no domain can be extracted
this.pageDomain = µb.URI.domainFromHostname(this.pageHostname) || this.pageHostname;
this.rootHostname = this.pageHostname;
this.rootDomain = this.pageDomain;
2014-08-27 08:01:10 +02:00
2014-09-14 22:20:40 +02:00
this.frames = {};
2014-08-02 17:40:27 +02:00
this.netFiltering = true;
this.netFilteringReadTime = 0;
2014-07-14 17:24:59 +02:00
this.perLoadBlockedRequestCount = 0;
this.perLoadAllowedRequestCount = 0;
this.skipLocalMirroring = false;
2014-09-14 22:20:40 +02:00
this.netFilteringCache = NetFilteringResultCache.factory();
if ( µb.userSettings.logRequests ) {
this.netFilteringCache.shelfLife = 30 * 60 * 1000;
}
2014-07-14 17:24:59 +02:00
return this;
};
/******************************************************************************/
2014-10-02 22:45:26 +02:00
PageStore.prototype.reuse = function(pageURL, context) {
// If URL changes without a page reload (more and more common), then we
2014-10-02 22:45:26 +02:00
// need to keep all that we collected for reuse. In particular, not
// doing so was causing a problem in `videos.foxnews.com`: clicking a
// video thumbnail would not work, because the frame hierarchy structure
// was flushed from memory, while not really being flushed on the page.
if ( context === 'tabUpdated' ) {
this.previousPageURL = this.pageURL;
this.pageURL = pageURL;
this.pageHostname = µb.URI.hostnameFromURI(pageURL);
this.pageDomain = µb.URI.domainFromHostname(this.pageHostname) || this.pageHostname;
this.rootHostname = this.pageHostname;
this.rootDomain = this.pageDomain;
2014-12-08 18:37:35 +01:00
// As part of https://github.com/gorhill/uBlock/issues/405
// URL changed, force a re-evaluation of filtering switch
this.netFilteringReadTime = 0;
2014-10-02 22:45:26 +02:00
return this;
}
// A new page is completely reloaded from scratch, reset all.
2014-09-14 22:20:40 +02:00
this.disposeFrameStores();
this.netFilteringCache = this.netFilteringCache.dispose();
2014-08-02 17:40:27 +02:00
var previousPageURL = this.pageURL;
this.init(this.tabId, pageURL);
this.previousPageURL = previousPageURL;
2014-06-24 00:42:43 +02:00
return this;
};
2014-09-14 22:20:40 +02:00
// https://www.youtube.com/watch?v=dltNSbOupgE
2014-06-24 00:42:43 +02:00
/******************************************************************************/
PageStore.prototype.dispose = function() {
// rhill 2013-11-07: Even though at init time these are reset, I still
// need to release the memory taken by these, which can amount to
// sizeable enough chunks (especially requests, through the request URL
// used as a key).
this.pageURL = this.previousPageURL =
this.pageHostname = this.pageDomain =
this.rootHostname = this.rootDomain = '';
2014-09-14 22:20:40 +02:00
this.disposeFrameStores();
this.netFilteringCache = this.netFilteringCache.dispose();
if ( pageStoreJunkyard.length < pageStoreJunkyardMax ) {
2014-06-24 00:42:43 +02:00
pageStoreJunkyard.push(this);
}
2014-09-14 22:20:40 +02:00
return null;
};
/******************************************************************************/
PageStore.prototype.disposeFrameStores = function() {
var frames = this.frames;
if ( typeof frames === 'object' ) {
for ( var k in frames ) {
if ( frames.hasOwnProperty(k) === false ) {
continue;
}
frames[k].dispose();
}
}
this.frames = {};
2014-06-24 00:42:43 +02:00
};
/******************************************************************************/
PageStore.prototype.addFrame = function(frameId, frameURL) {
var frameStore = this.frames[frameId];
if ( frameStore === undefined ) {
this.frames[frameId] = frameStore = FrameStore.factory(this.rootHostname, frameURL);
//console.debug('µBlock> PageStore.addFrame(%d, "%s")', frameId, frameURL);
}
return frameStore;
};
/******************************************************************************/
PageStore.prototype.getFrame = function(frameId) {
return this.frames[frameId];
};
/******************************************************************************/
2014-08-02 17:40:27 +02:00
PageStore.prototype.getNetFilteringSwitch = function() {
if ( this.netFilteringReadTime < µb.netWhitelistModifyTime ) {
2014-12-14 23:21:59 +01:00
this.netFiltering = µb.getNetFilteringSwitch(this.pageURL);
2014-08-02 17:40:27 +02:00
this.netFilteringReadTime = Date.now();
}
return this.netFiltering;
};
/******************************************************************************/
2014-09-14 22:20:40 +02:00
PageStore.prototype.filterRequest = function(context, requestType, requestURL) {
2014-10-10 16:07:50 +02:00
var result = '';
if ( this.getNetFilteringSwitch() ) {
var entry = this.netFilteringCache.lookup(requestURL);
if ( entry !== undefined ) {
//console.debug(' cache HIT: PageStore.filterRequest("%s")', requestURL);
return entry.result;
}
//console.debug('cache MISS: PageStore.filterRequest("%s")', requestURL);
result = µb.netFilteringEngine.matchString(context, requestURL, requestType);
2014-06-24 00:42:43 +02:00
}
2014-09-14 22:20:40 +02:00
if ( collapsibleRequestTypes.indexOf(requestType) !== -1 || µb.userSettings.logRequests ) {
2014-10-02 22:45:26 +02:00
this.netFilteringCache.add(requestURL, result, requestType, 0);
2014-09-14 22:20:40 +02:00
}
return result;
};
2014-06-27 23:06:42 +02:00
2014-09-14 22:20:40 +02:00
/******************************************************************************/
2014-06-27 23:06:42 +02:00
2014-10-02 22:45:26 +02:00
PageStore.prototype.setRequestFlags = function(requestURL, targetBits, valueBits) {
var entry = this.netFilteringCache.lookup(requestURL);
if ( entry !== undefined ) {
entry.flags = (entry.flags & ~targetBits) | (valueBits & targetBits);
}
};
/******************************************************************************/
2014-10-06 20:02:44 +02:00
PageStore.prototype.recordResult = function(requestType, requestURL, result) {
if ( collapsibleRequestTypes.indexOf(requestType) !== -1 || µb.userSettings.logRequests ) {
this.netFilteringCache.add(requestURL, result, requestType, 0);
}
};
/******************************************************************************/
2014-09-14 22:20:40 +02:00
// false: not blocked
// true: blocked
2014-07-03 14:28:15 +02:00
2014-09-14 22:20:40 +02:00
PageStore.prototype.boolFromResult = function(result) {
return typeof result === 'string' && result !== '' && result.slice(0, 2) !== '@@';
2014-06-24 00:42:43 +02:00
};
/******************************************************************************/
PageStore.prototype.updateBadge = function() {
2014-08-02 17:40:27 +02:00
var netFiltering = this.getNetFilteringSwitch();
var iconPaths = netFiltering ?
2014-09-04 03:18:36 +02:00
{ '19': 'img/browsericons/icon19.png', '38': 'img/browsericons/icon38.png' } :
{ '19': 'img/browsericons/icon19-off.png', '38': 'img/browsericons/icon38-off.png' };
2014-06-24 00:42:43 +02:00
var iconStr = '';
2014-08-02 17:40:27 +02:00
if ( µb.userSettings.showIconBadge && netFiltering && this.perLoadBlockedRequestCount ) {
2014-10-19 16:44:09 +02:00
// Safari can't show formatted strings, only integers.
if (vAPI.safari) {
iconStr = this.perLoadBlockedRequestCount;
}
else {
iconStr = µb.utils.formatCount(this.perLoadBlockedRequestCount);
}
2014-06-24 00:42:43 +02:00
}
vAPI.setIcon(this.tabId, iconPaths, iconStr);
2014-06-24 00:42:43 +02:00
};
// https://www.youtube.com/watch?v=drW8p_dTLD4
2014-06-24 00:42:43 +02:00
/******************************************************************************/
return {
2014-09-14 22:20:40 +02:00
factory: PageStore.factory
2014-06-24 00:42:43 +02:00
};
})();
/******************************************************************************/