2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-07 15:55:04 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
|
|
|
Copyright (C) 2014-2016 Raymond Hill
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2016-07-01 04:03:29 +02:00
|
|
|
'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;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
var NetFilteringResultCacheEntry = function(result, type) {
|
|
|
|
this.init(result, type);
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
NetFilteringResultCacheEntry.prototype.init = function(result, type) {
|
2014-10-02 22:45:26 +02:00
|
|
|
this.result = result;
|
|
|
|
this.type = type;
|
2014-09-14 22:20:40 +02:00
|
|
|
this.time = Date.now();
|
2016-07-01 06:16:10 +02:00
|
|
|
return this;
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
NetFilteringResultCacheEntry.prototype.dispose = function() {
|
2016-07-01 06:16:10 +02:00
|
|
|
this.result = this.type = '';
|
2014-09-14 22:20:40 +02:00
|
|
|
if ( netFilteringResultCacheEntryJunkyard.length < netFilteringResultCacheEntryJunkyardMax ) {
|
|
|
|
netFilteringResultCacheEntryJunkyard.push(this);
|
2014-07-30 07:05:35 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
NetFilteringResultCacheEntry.factory = function(result, type) {
|
2016-07-01 06:16:10 +02:00
|
|
|
if ( netFilteringResultCacheEntryJunkyard.length ) {
|
|
|
|
return netFilteringResultCacheEntryJunkyard.pop().init(result, type);
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
2016-07-01 06:16:10 +02:00
|
|
|
return new NetFilteringResultCacheEntry(result, type);
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// To mitigate memory churning
|
|
|
|
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() {
|
2016-07-01 06:16:10 +02:00
|
|
|
this.urls = Object.create(null);
|
2014-09-14 22:20:40 +02:00
|
|
|
this.count = 0;
|
2015-06-09 16:27:08 +02:00
|
|
|
this.shelfLife = 15 * 1000;
|
2015-01-06 14:01:15 +01:00
|
|
|
this.timer = null;
|
|
|
|
this.boundPruneAsyncCallback = this.pruneAsyncCallback.bind(this);
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
NetFilteringResultCache.prototype.dispose = function() {
|
2015-01-22 03:46:11 +01:00
|
|
|
this.empty();
|
2015-01-06 14:44:19 +01:00
|
|
|
this.boundPruneAsyncCallback = null;
|
2014-09-14 22:20:40 +02:00
|
|
|
if ( netFilteringCacheJunkyard.length < netFilteringCacheJunkyardMax ) {
|
|
|
|
netFilteringCacheJunkyard.push(this);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
NetFilteringResultCache.prototype.add = function(context, result) {
|
2016-07-01 06:16:10 +02:00
|
|
|
var url = context.requestURL,
|
|
|
|
type = context.requestType,
|
|
|
|
key = type + ' ' + url,
|
|
|
|
entry = this.urls[key];
|
2014-09-14 22:20:40 +02:00
|
|
|
if ( entry !== undefined ) {
|
2014-10-02 22:45:26 +02:00
|
|
|
entry.result = result;
|
|
|
|
entry.type = type;
|
2014-09-14 22:20:40 +02:00
|
|
|
entry.time = Date.now();
|
|
|
|
return;
|
|
|
|
}
|
2016-07-01 06:16:10 +02:00
|
|
|
this.urls[key] = NetFilteringResultCacheEntry.factory(result, type);
|
2014-09-14 22:20:40 +02:00
|
|
|
if ( this.count === 0 ) {
|
|
|
|
this.pruneAsync();
|
|
|
|
}
|
|
|
|
this.count += 1;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-22 03:46:11 +01:00
|
|
|
NetFilteringResultCache.prototype.empty = function() {
|
|
|
|
for ( var key in this.urls ) {
|
|
|
|
this.urls[key].dispose();
|
|
|
|
}
|
2016-07-01 06:16:10 +02:00
|
|
|
this.urls = Object.create(null);
|
2015-01-22 03:46:11 +01:00
|
|
|
this.count = 0;
|
|
|
|
if ( this.timer !== null ) {
|
|
|
|
clearTimeout(this.timer);
|
|
|
|
this.timer = null;
|
|
|
|
}
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
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 ) {
|
2014-07-30 07:05:35 +02:00
|
|
|
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-07-30 07:05:35 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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() {
|
2015-01-06 14:01:15 +01:00
|
|
|
if ( this.timer === null ) {
|
2015-05-17 19:02:56 +02:00
|
|
|
this.timer = vAPI.setTimeout(this.boundPruneAsyncCallback, this.shelfLife * 2);
|
2015-01-06 14:01:15 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NetFilteringResultCache.prototype.pruneAsyncCallback = function() {
|
|
|
|
this.timer = null;
|
|
|
|
this.prune();
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-03-31 13:26:43 +02:00
|
|
|
NetFilteringResultCache.prototype.lookup = function(context) {
|
2015-08-18 17:44:24 +02:00
|
|
|
return this.urls[context.requestType + ' ' + context.requestURL] || undefined;
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
// Frame stores are used solely to associate a URL with a frame id. The
|
|
|
|
// name `pageHostname` is used because of historical reasons. A more
|
|
|
|
// appropriate name is `frameHostname` -- something to do in a future
|
|
|
|
// refactoring.
|
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
// To mitigate memory churning
|
|
|
|
var frameStoreJunkyard = [];
|
|
|
|
var frameStoreJunkyardMax = 50;
|
|
|
|
|
2014-07-30 07:05:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-03-07 15:55:04 +01:00
|
|
|
var FrameStore = function(frameURL) {
|
|
|
|
this.init(frameURL);
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-03-07 15:55:04 +01:00
|
|
|
FrameStore.factory = function(frameURL) {
|
2014-09-14 22:20:40 +02:00
|
|
|
var entry = frameStoreJunkyard.pop();
|
|
|
|
if ( entry === undefined ) {
|
2016-03-07 15:55:04 +01:00
|
|
|
return new FrameStore(frameURL);
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
2016-03-07 15:55:04 +01:00
|
|
|
return entry.init(frameURL);
|
2014-09-14 22:20:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-03-07 15:55:04 +01:00
|
|
|
FrameStore.prototype.init = function(frameURL) {
|
2014-07-30 07:05:35 +02:00
|
|
|
var µburi = µb.URI;
|
|
|
|
this.pageHostname = µburi.hostnameFromURI(frameURL);
|
2014-10-07 22:30:40 +02:00
|
|
|
this.pageDomain = µburi.domainFromHostname(this.pageHostname) || this.pageHostname;
|
2014-07-30 07:05:35 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
FrameStore.prototype.dispose = function() {
|
2015-04-09 00:46:08 +02:00
|
|
|
this.pageHostname = this.pageDomain = '';
|
2014-09-14 22:20:40 +02:00
|
|
|
if ( frameStoreJunkyard.length < frameStoreJunkyardMax ) {
|
|
|
|
frameStoreJunkyard.push(this);
|
|
|
|
}
|
|
|
|
return null;
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
/******************************************************************************/
|
2014-07-30 07:05:35 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-09-14 22:20:40 +02:00
|
|
|
// To mitigate memory churning
|
|
|
|
var pageStoreJunkyard = [];
|
|
|
|
var pageStoreJunkyardMax = 10;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
var PageStore = function(tabId) {
|
|
|
|
this.init(tabId);
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
PageStore.factory = function(tabId) {
|
2014-09-14 22:20:40 +02:00
|
|
|
var entry = pageStoreJunkyard.pop();
|
|
|
|
if ( entry === undefined ) {
|
2015-04-09 00:46:08 +02:00
|
|
|
entry = new PageStore(tabId);
|
2014-09-14 22:20:40 +02:00
|
|
|
} else {
|
2015-04-09 00:46:08 +02:00
|
|
|
entry.init(tabId);
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
PageStore.prototype.init = function(tabId) {
|
2015-12-02 06:59:51 +01:00
|
|
|
var tabContext = µb.tabContextManager.mustLookup(tabId);
|
2014-06-24 00:42:43 +02:00
|
|
|
this.tabId = tabId;
|
2016-01-17 19:30:43 +01:00
|
|
|
|
|
|
|
// If we are navigating from-to same site, remember whether large
|
|
|
|
// media elements were temporarily allowed.
|
|
|
|
if (
|
|
|
|
typeof this.allowLargeMediaElementsUntil !== 'number' ||
|
|
|
|
tabContext.rootHostname !== this.tabHostname
|
|
|
|
) {
|
|
|
|
this.allowLargeMediaElementsUntil = 0;
|
|
|
|
}
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
this.tabHostname = tabContext.rootHostname;
|
2015-05-16 16:15:02 +02:00
|
|
|
this.title = tabContext.rawURL;
|
|
|
|
this.rawURL = tabContext.rawURL;
|
2014-12-30 22:36:29 +01:00
|
|
|
this.hostnameToCountMap = {};
|
2015-01-10 17:23:28 +01:00
|
|
|
this.contentLastModified = 0;
|
2016-02-10 00:53:36 +01:00
|
|
|
this.frames = Object.create(null);
|
2014-07-14 17:24:59 +02:00
|
|
|
this.perLoadBlockedRequestCount = 0;
|
|
|
|
this.perLoadAllowedRequestCount = 0;
|
2015-04-05 18:03:14 +02:00
|
|
|
this.hiddenElementCount = ''; // Empty string means "unknown"
|
2015-06-10 15:23:48 +02:00
|
|
|
this.remoteFontCount = 0;
|
2016-01-04 16:48:28 +01:00
|
|
|
this.popupBlockedCount = 0;
|
2016-01-17 19:30:43 +01:00
|
|
|
this.largeMediaCount = 0;
|
|
|
|
this.largeMediaTimer = null;
|
2014-09-14 22:20:40 +02:00
|
|
|
this.netFilteringCache = NetFilteringResultCache.factory();
|
2015-01-06 14:01:15 +01:00
|
|
|
|
2016-08-12 14:55:35 +02:00
|
|
|
this.noCosmeticFiltering = µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) === true;
|
|
|
|
if ( µb.logger.isEnabled() && this.noCosmeticFiltering ) {
|
2015-06-09 16:27:08 +02:00
|
|
|
µb.logger.writeOne(
|
|
|
|
tabId,
|
2016-08-12 14:55:35 +02:00
|
|
|
'cosmetic',
|
|
|
|
µb.hnSwitches.toResultString(),
|
|
|
|
'dom',
|
2015-06-09 16:27:08 +02:00
|
|
|
tabContext.rawURL,
|
|
|
|
this.tabHostname,
|
|
|
|
this.tabHostname
|
|
|
|
);
|
|
|
|
}
|
2016-08-12 14:55:35 +02:00
|
|
|
|
|
|
|
// Support `generichide` filter option.
|
|
|
|
this.noGenericCosmeticFiltering = this.noCosmeticFiltering;
|
|
|
|
if ( this.noGenericCosmeticFiltering !== true ) {
|
|
|
|
this.noGenericCosmeticFiltering = µb.staticNetFilteringEngine.matchStringExactType(
|
|
|
|
this.createContextFromPage(),
|
|
|
|
tabContext.normalURL,
|
|
|
|
'elemhide'
|
|
|
|
) === false;
|
|
|
|
if ( µb.logger.isEnabled() && this.noGenericCosmeticFiltering ) {
|
|
|
|
// https://github.com/gorhill/uBlock/issues/370
|
|
|
|
// Log using `cosmetic-filtering`, not `elemhide`.
|
|
|
|
µb.logger.writeOne(
|
|
|
|
tabId,
|
|
|
|
'net',
|
|
|
|
µb.staticNetFilteringEngine.toResultString(true),
|
|
|
|
'elemhide',
|
|
|
|
tabContext.rawURL,
|
|
|
|
this.tabHostname,
|
|
|
|
this.tabHostname
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-14 17:24:59 +02:00
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
PageStore.prototype.reuse = function(context) {
|
2015-04-09 17:19:31 +02:00
|
|
|
// When force refreshing a page, the page store data needs to be reset.
|
2015-03-22 01:30:00 +01:00
|
|
|
|
2015-03-26 00:28:22 +01:00
|
|
|
// If the hostname changes, we can't merely just update the context.
|
2015-12-02 06:59:51 +01:00
|
|
|
var tabContext = µb.tabContextManager.mustLookup(this.tabId);
|
2015-04-09 00:46:08 +02:00
|
|
|
if ( tabContext.rootHostname !== this.tabHostname ) {
|
2015-03-26 00:28:22 +01:00
|
|
|
context = '';
|
|
|
|
}
|
|
|
|
|
2014-10-17 21:44:19 +02:00
|
|
|
// 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' ) {
|
2015-04-07 03:26:05 +02:00
|
|
|
// As part of https://github.com/chrisaljoudi/uBlock/issues/405
|
2014-12-08 18:37:35 +01:00
|
|
|
// URL changed, force a re-evaluation of filtering switch
|
2015-05-16 16:15:02 +02:00
|
|
|
this.rawURL = tabContext.rawURL;
|
2014-10-02 22:45:26 +02:00
|
|
|
return this;
|
|
|
|
}
|
2015-04-09 00:46:08 +02:00
|
|
|
|
2014-10-02 22:45:26 +02:00
|
|
|
// A new page is completely reloaded from scratch, reset all.
|
2016-01-17 19:30:43 +01:00
|
|
|
if ( this.largeMediaTimer !== null ) {
|
|
|
|
clearTimeout(this.largeMediaTimer);
|
|
|
|
this.largeMediaTimer = null;
|
|
|
|
}
|
2014-09-14 22:20:40 +02:00
|
|
|
this.disposeFrameStores();
|
|
|
|
this.netFilteringCache = this.netFilteringCache.dispose();
|
2015-04-09 00:46:08 +02:00
|
|
|
this.init(this.tabId);
|
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).
|
2015-05-16 16:15:02 +02:00
|
|
|
this.tabHostname = '';
|
|
|
|
this.title = '';
|
|
|
|
this.rawURL = '';
|
2014-12-30 22:36:29 +01:00
|
|
|
this.hostnameToCountMap = null;
|
2016-01-17 19:30:43 +01:00
|
|
|
this.allowLargeMediaElementsUntil = 0;
|
|
|
|
if ( this.largeMediaTimer !== null ) {
|
|
|
|
clearTimeout(this.largeMediaTimer);
|
|
|
|
this.largeMediaTimer = null;
|
|
|
|
}
|
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;
|
2014-12-28 16:07:43 +01:00
|
|
|
for ( var k in frames ) {
|
2016-02-10 00:53:36 +01:00
|
|
|
frames[k].dispose();
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
2016-02-10 00:53:36 +01:00
|
|
|
this.frames = Object.create(null);
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-25 20:15:36 +01:00
|
|
|
PageStore.prototype.getFrame = function(frameId) {
|
2015-03-02 16:41:51 +01:00
|
|
|
return this.frames[frameId] || null;
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-25 20:15:36 +01:00
|
|
|
PageStore.prototype.setFrame = function(frameId, frameURL) {
|
|
|
|
var frameStore = this.frames[frameId];
|
2016-02-10 00:53:36 +01:00
|
|
|
if ( frameStore ) {
|
2016-03-07 15:55:04 +01:00
|
|
|
frameStore.init(frameURL);
|
2015-02-25 20:15:36 +01:00
|
|
|
} else {
|
2016-03-07 15:55:04 +01:00
|
|
|
this.frames[frameId] = FrameStore.factory(frameURL);
|
2015-02-25 20:15:36 +01:00
|
|
|
}
|
2014-07-30 07:05:35 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-09 00:46:08 +02:00
|
|
|
PageStore.prototype.createContextFromPage = function() {
|
2016-07-01 04:03:29 +02:00
|
|
|
var context = µb.tabContextManager.createContext(this.tabId);
|
2015-04-09 00:46:08 +02:00
|
|
|
context.pageHostname = context.rootHostname;
|
|
|
|
context.pageDomain = context.rootDomain;
|
|
|
|
return context;
|
|
|
|
};
|
|
|
|
|
|
|
|
PageStore.prototype.createContextFromFrameId = function(frameId) {
|
2016-07-01 04:03:29 +02:00
|
|
|
var context = µb.tabContextManager.createContext(this.tabId);
|
2016-02-10 00:53:36 +01:00
|
|
|
var frameStore = this.frames[frameId];
|
|
|
|
if ( frameStore ) {
|
2015-04-09 00:46:08 +02:00
|
|
|
context.pageHostname = frameStore.pageHostname;
|
|
|
|
context.pageDomain = frameStore.pageDomain;
|
|
|
|
} else {
|
|
|
|
context.pageHostname = context.rootHostname;
|
|
|
|
context.pageDomain = context.rootDomain;
|
|
|
|
}
|
|
|
|
return context;
|
|
|
|
};
|
|
|
|
|
|
|
|
PageStore.prototype.createContextFromFrameHostname = function(frameHostname) {
|
2016-07-01 04:03:29 +02:00
|
|
|
var context = µb.tabContextManager.createContext(this.tabId);
|
2015-04-09 00:46:08 +02:00
|
|
|
context.pageHostname = frameHostname;
|
|
|
|
context.pageDomain = µb.URI.domainFromHostname(frameHostname) || frameHostname;
|
|
|
|
return context;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-08-02 17:40:27 +02:00
|
|
|
PageStore.prototype.getNetFilteringSwitch = function() {
|
2015-12-02 06:59:51 +01:00
|
|
|
return µb.tabContextManager.mustLookup(this.tabId).getNetFilteringSwitch();
|
2014-08-02 17:40:27 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-06 05:14:12 +01:00
|
|
|
PageStore.prototype.getSpecificCosmeticFilteringSwitch = function() {
|
2016-08-12 14:55:35 +02:00
|
|
|
return this.noCosmeticFiltering !== true;
|
2015-02-06 05:14:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
PageStore.prototype.getGenericCosmeticFilteringSwitch = function() {
|
2016-08-12 14:55:35 +02:00
|
|
|
return this.noGenericCosmeticFiltering !== true &&
|
|
|
|
this.noCosmeticFiltering !== true;
|
2015-01-09 03:04:48 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-22 03:46:11 +01:00
|
|
|
PageStore.prototype.toggleNetFilteringSwitch = function(url, scope, state) {
|
|
|
|
µb.toggleNetFilteringSwitch(url, scope, state);
|
|
|
|
this.netFilteringCache.empty();
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
PageStore.prototype.logLargeMedia = (function() {
|
|
|
|
var injectScript = function() {
|
|
|
|
this.largeMediaTimer = null;
|
|
|
|
µb.scriptlets.injectDeep(
|
|
|
|
this.tabId,
|
|
|
|
'load-large-media-interactive'
|
|
|
|
);
|
|
|
|
µb.contextMenu.update(this.tabId);
|
|
|
|
};
|
|
|
|
return function() {
|
|
|
|
this.largeMediaCount += 1;
|
|
|
|
if ( this.largeMediaTimer === null ) {
|
|
|
|
this.largeMediaTimer = vAPI.setTimeout(injectScript.bind(this), 500);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
PageStore.prototype.temporarilyAllowLargeMediaElements = function() {
|
|
|
|
this.largeMediaCount = 0;
|
|
|
|
µb.contextMenu.update(this.tabId);
|
|
|
|
this.allowLargeMediaElementsUntil = Date.now() + 86400000;
|
|
|
|
µb.scriptlets.injectDeep(this.tabId, 'load-large-media-all');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-12-28 16:07:43 +01:00
|
|
|
PageStore.prototype.filterRequest = function(context) {
|
2015-06-10 15:23:48 +02:00
|
|
|
var requestType = context.requestType;
|
2015-01-24 18:06:22 +01:00
|
|
|
|
2014-12-28 16:07:43 +01:00
|
|
|
if ( this.getNetFilteringSwitch() === false ) {
|
2015-06-10 15:23:48 +02:00
|
|
|
if ( collapsibleRequestTypes.indexOf(requestType) !== -1 ) {
|
2015-01-24 18:06:22 +01:00
|
|
|
this.netFilteringCache.add(context, '');
|
|
|
|
}
|
2014-12-28 16:07:43 +01:00
|
|
|
return '';
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2014-12-28 16:07:43 +01:00
|
|
|
|
2015-03-31 13:26:43 +02:00
|
|
|
var entry = this.netFilteringCache.lookup(context);
|
2014-12-28 16:07:43 +01:00
|
|
|
if ( entry !== undefined ) {
|
2015-01-06 14:01:15 +01:00
|
|
|
//console.debug('cache HIT: PageStore.filterRequest("%s")', context.requestURL);
|
2014-12-28 16:07:43 +01:00
|
|
|
return entry.result;
|
2014-09-14 22:20:40 +02:00
|
|
|
}
|
2014-12-28 16:07:43 +01:00
|
|
|
|
2015-06-10 15:23:48 +02:00
|
|
|
var result = '';
|
|
|
|
|
|
|
|
if ( requestType === 'font' ) {
|
|
|
|
if ( µb.hnSwitches.evaluateZ('no-remote-fonts', context.rootHostname) !== false ) {
|
|
|
|
result = µb.hnSwitches.toResultString();
|
|
|
|
}
|
|
|
|
this.remoteFontCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( result === '' ) {
|
|
|
|
µb.sessionURLFiltering.evaluateZ(context.rootHostname, context.requestURL, requestType);
|
|
|
|
result = µb.sessionURLFiltering.toFilterString();
|
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
|
|
|
|
// Given that:
|
|
|
|
// - Dynamic filtering override static filtering
|
|
|
|
// - Evaluating dynamic filtering is much faster than static filtering
|
|
|
|
// We evaluate dynamic filtering first, and hopefully we can skip
|
|
|
|
// evaluation of static filtering.
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( result === '' && µb.userSettings.advancedUserEnabled ) {
|
2015-06-10 15:23:48 +02:00
|
|
|
µb.sessionFirewall.evaluateCellZY( context.rootHostname, context.requestHostname, requestType);
|
2015-05-25 00:50:09 +02:00
|
|
|
if ( µb.sessionFirewall.mustBlockOrAllow() ) {
|
|
|
|
result = µb.sessionFirewall.toFilterString();
|
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Static filtering never override dynamic filtering
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( result === '' || result.charAt(1) === 'n' ) {
|
2015-06-09 17:48:13 +02:00
|
|
|
if ( µb.staticNetFilteringEngine.matchString(context) !== undefined ) {
|
2015-06-09 16:27:08 +02:00
|
|
|
result = µb.staticNetFilteringEngine.toResultString(µb.logger.isEnabled());
|
|
|
|
}
|
2015-01-06 14:01:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//console.debug('cache MISS: PageStore.filterRequest("%s")', context.requestURL);
|
2015-06-10 15:23:48 +02:00
|
|
|
if ( collapsibleRequestTypes.indexOf(requestType) !== -1 ) {
|
2015-01-24 18:06:22 +01:00
|
|
|
this.netFilteringCache.add(context, result);
|
|
|
|
}
|
2014-12-28 16:07:43 +01:00
|
|
|
|
2015-06-10 15:23:48 +02:00
|
|
|
// console.debug('[%s, %s] = "%s"', context.requestHostname, requestType, result);
|
2014-12-28 16:07:43 +01:00
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2015-01-24 18:06:22 +01:00
|
|
|
// http://jsperf.com/string-indexof-vs-object
|
|
|
|
var collapsibleRequestTypes = 'image sub_frame object';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
PageStore.prototype.filterRequestNoCache = function(context) {
|
2015-04-09 00:46:08 +02:00
|
|
|
if ( this.getNetFilteringSwitch() === false ) {
|
2015-01-24 18:06:22 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:23:48 +02:00
|
|
|
var requestType = context.requestType;
|
|
|
|
var result = '';
|
|
|
|
|
|
|
|
if ( requestType === 'font' ) {
|
|
|
|
if ( µb.hnSwitches.evaluateZ('no-remote-fonts', context.rootHostname) !== false ) {
|
|
|
|
result = µb.hnSwitches.toResultString();
|
|
|
|
}
|
|
|
|
this.remoteFontCount += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( result === '' ) {
|
|
|
|
µb.sessionURLFiltering.evaluateZ(context.rootHostname, context.requestURL, requestType);
|
|
|
|
result = µb.sessionURLFiltering.toFilterString();
|
|
|
|
}
|
2015-01-24 18:06:22 +01:00
|
|
|
|
|
|
|
// Given that:
|
|
|
|
// - Dynamic filtering override static filtering
|
|
|
|
// - Evaluating dynamic filtering is much faster than static filtering
|
|
|
|
// We evaluate dynamic filtering first, and hopefully we can skip
|
|
|
|
// evaluation of static filtering.
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( result === '' && µb.userSettings.advancedUserEnabled ) {
|
2015-06-10 15:23:48 +02:00
|
|
|
µb.sessionFirewall.evaluateCellZY(context.rootHostname, context.requestHostname, requestType);
|
2015-05-25 00:50:09 +02:00
|
|
|
if ( µb.sessionFirewall.mustBlockOrAllow() ) {
|
|
|
|
result = µb.sessionFirewall.toFilterString();
|
|
|
|
}
|
2015-01-24 18:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Static filtering never override dynamic filtering
|
2015-05-21 20:15:17 +02:00
|
|
|
if ( result === '' || result.charAt(1) === 'n' ) {
|
2015-06-09 17:48:13 +02:00
|
|
|
if ( µb.staticNetFilteringEngine.matchString(context) !== undefined ) {
|
2015-06-09 16:27:08 +02:00
|
|
|
result = µb.staticNetFilteringEngine.toResultString(µb.logger.isEnabled());
|
|
|
|
}
|
2015-01-24 18:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2015-01-06 14:01:15 +01:00
|
|
|
/******************************************************************************/
|
2014-12-31 16:47:19 +01:00
|
|
|
|
2015-01-24 18:06:22 +01:00
|
|
|
PageStore.prototype.logRequest = function(context, result) {
|
2014-12-28 16:07:43 +01:00
|
|
|
var requestHostname = context.requestHostname;
|
2015-02-07 00:11:36 +01:00
|
|
|
// rhill 20150206:
|
|
|
|
// be prepared to handle invalid requestHostname, I've seen this
|
|
|
|
// happen: http://./
|
|
|
|
if ( requestHostname === '' ) {
|
2015-04-09 00:46:08 +02:00
|
|
|
requestHostname = context.rootHostname;
|
2015-02-07 00:11:36 +01:00
|
|
|
}
|
2015-03-05 01:38:19 +01:00
|
|
|
var now = Date.now();
|
2014-12-30 22:36:29 +01:00
|
|
|
if ( this.hostnameToCountMap.hasOwnProperty(requestHostname) === false ) {
|
|
|
|
this.hostnameToCountMap[requestHostname] = 0;
|
2015-03-05 01:38:19 +01:00
|
|
|
this.contentLastModified = now;
|
2014-12-30 22:36:29 +01:00
|
|
|
}
|
|
|
|
var c = result.charAt(1);
|
2015-07-10 20:02:02 +02:00
|
|
|
if ( c === 'b' ) {
|
2014-12-30 22:36:29 +01:00
|
|
|
this.hostnameToCountMap[requestHostname] += 0x00000001;
|
2015-01-24 18:06:22 +01:00
|
|
|
this.perLoadBlockedRequestCount++;
|
|
|
|
µb.localSettings.blockedRequestCount++;
|
2015-07-10 20:02:02 +02:00
|
|
|
} else /* if ( c === '' || c === 'a' || c === 'n' ) */ {
|
|
|
|
this.hostnameToCountMap[requestHostname] += 0x00010000;
|
|
|
|
this.perLoadAllowedRequestCount++;
|
|
|
|
µb.localSettings.allowedRequestCount++;
|
2014-12-28 16:07:43 +01:00
|
|
|
}
|
2015-03-06 03:17:09 +01:00
|
|
|
µb.localSettingsModifyTime = now;
|
2014-10-02 22:45:26 +02:00
|
|
|
};
|
|
|
|
|
2014-08-20 02:41:52 +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
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|