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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* global chrome, µBlock */
|
|
|
|
|
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
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;
|
|
|
|
var pageStoreJunkyard = [];
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var pageStoreFactory = function(tabId, pageURL) {
|
|
|
|
var entry = pageStoreJunkyard.pop();
|
|
|
|
if ( entry ) {
|
|
|
|
return entry.init(tabId, pageURL);
|
|
|
|
}
|
|
|
|
return new PageStore(tabId, pageURL);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
function PageStore(tabId, pageURL) {
|
|
|
|
this.tabId = -1;
|
|
|
|
this.pageURL = '';
|
|
|
|
this.pageHostname = '';
|
|
|
|
this.pageDomain = '';
|
|
|
|
this.perLoadBlockedRequestCount = 0;
|
|
|
|
this.perLoadAllowedRequestCount = 0;
|
2014-06-27 23:06:42 +02:00
|
|
|
this.blockedRequests = {};
|
2014-06-24 00:42:43 +02:00
|
|
|
this.disposeTime = 0;
|
|
|
|
this.init(tabId, pageURL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
PageStore.prototype.init = function(tabId, pageURL) {
|
|
|
|
this.tabId = tabId;
|
|
|
|
this.pageURL = pageURL;
|
|
|
|
this.pageHostname = µb.URI.hostnameFromURI(pageURL);
|
|
|
|
this.pageDomain = µb.URI.domainFromHostname(this.pageHostname);
|
|
|
|
this.perLoadBlockedRequestCount = 0;
|
|
|
|
this.perLoadAllowedRequestCount = 0;
|
2014-06-27 23:06:42 +02:00
|
|
|
this.blockedRequests = {};
|
2014-06-24 00:42:43 +02:00
|
|
|
this.disposeTime = 0;
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
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.pageHostname = '';
|
|
|
|
this.pageDomain = '';
|
|
|
|
if ( pageStoreJunkyard.length < 32 ) {
|
|
|
|
pageStoreJunkyard.push(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
PageStore.prototype.recordRequest = function(type, url, block) {
|
|
|
|
// rhill 2013-10-26: This needs to be called even if the request is
|
|
|
|
// already logged, since the request stats are cached for a while after
|
|
|
|
// the page is no longer visible in a browser tab.
|
|
|
|
µb.updateBadge(this.tabId);
|
|
|
|
|
2014-06-27 23:06:42 +02:00
|
|
|
if ( block === false ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
this.perLoadAllowedRequestCount++;
|
|
|
|
µb.localSettings.allowedRequestCount++;
|
2014-06-27 23:06:42 +02:00
|
|
|
return;
|
2014-06-24 00:42:43 +02:00
|
|
|
}
|
2014-06-27 23:06:42 +02:00
|
|
|
|
|
|
|
this.perLoadBlockedRequestCount++;
|
|
|
|
µb.localSettings.blockedRequestCount++;
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/7
|
|
|
|
// https://github.com/gorhill/uBlock/issues/12
|
|
|
|
this.blockedRequests[url] = true;
|
2014-06-24 00:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// Update badge, incrementally
|
|
|
|
|
|
|
|
// rhill 2013-11-09: well this sucks, I can't update icon/badge
|
|
|
|
// incrementally, as chromium overwrite the icon at some point without
|
|
|
|
// notifying me, and this causes internal cached state to be out of sync.
|
|
|
|
|
|
|
|
PageStore.prototype.updateBadge = function() {
|
2014-06-27 23:06:42 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/19
|
|
|
|
// TODO: need to check with µb object to see whether tab still exists.
|
|
|
|
|
2014-06-25 00:29:55 +02:00
|
|
|
var netFilteringSwitch = µb.getNetFilteringSwitch(this.pageHostname);
|
2014-06-24 00:42:43 +02:00
|
|
|
var iconPath = netFilteringSwitch ? 'img/browsericons/icon19.png' : 'img/browsericons/icon19-off.png';
|
|
|
|
|
|
|
|
chrome.browserAction.setIcon({ tabId: this.tabId, path: iconPath });
|
|
|
|
|
|
|
|
var iconStr = '';
|
2014-06-25 00:29:55 +02:00
|
|
|
if ( µb.userSettings.showIconBadge && netFilteringSwitch && this.perLoadBlockedRequestCount ) {
|
2014-06-24 00:42:43 +02:00
|
|
|
iconStr = µb.formatCount(this.perLoadBlockedRequestCount);
|
|
|
|
}
|
|
|
|
chrome.browserAction.setBadgeText({
|
|
|
|
tabId: this.tabId,
|
|
|
|
text: iconStr
|
|
|
|
});
|
|
|
|
|
|
|
|
if ( iconStr !== '' ) {
|
|
|
|
chrome.browserAction.setBadgeBackgroundColor({
|
|
|
|
tabId: this.tabId,
|
|
|
|
color: '#666'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
return {
|
|
|
|
factory: pageStoreFactory
|
|
|
|
};
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|