1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-06-17 01:35:00 +12:00

code review

This commit is contained in:
gorhill 2015-05-01 23:54:32 -04:00
parent c472e23c7d
commit eb42375215
5 changed files with 43 additions and 69 deletions

View file

@ -130,15 +130,47 @@ return asyncJobManager;
/******************************************************************************/
// Update visual of extension icon.
// A time out is used to coalesce adjacent requests to update badge.
// Update badge
µMatrix.updateBadgeAsync = function(tabId) {
var pageStore = this.pageStoreFromTabId(tabId);
if ( pageStore ) {
pageStore.updateBadge();
}
};
// 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.
µMatrix.updateBadgeAsync = (function() {
var tabIdToTimer = Object.create(null);
var updateBadge = function(tabId) {
delete tabIdToTimer[tabId];
var pageStore = this.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
return;
}
var iconId = null;
var badgeStr = '';
var total = pageStore.perLoadAllowedRequestCount +
pageStore.perLoadBlockedRequestCount;
if ( total ) {
var squareSize = 19;
var greenSize = squareSize * Math.sqrt(pageStore.perLoadAllowedRequestCount / total);
iconId = greenSize < squareSize/2 ? Math.ceil(greenSize) : Math.floor(greenSize);
badgeStr = this.formatCount(pageStore.distinctRequestCount);
}
vAPI.setIcon(tabId, iconId, badgeStr);
};
return function(tabId) {
if ( tabIdToTimer[tabId] ) {
return;
}
if ( vAPI.isBehindTheSceneTabId(tabId) ) {
return;
}
tabIdToTimer[tabId] = setTimeout(updateBadge.bind(this, tabId), 500);
};
})();
/******************************************************************************/

View file

@ -404,7 +404,6 @@ var contentScriptSummaryHandler = function(tabId, details) {
r = µm.filterRequest(pageURL, 'script', url);
pageStore.recordRequest('script', url, r !== false, r);
}
pageStore.updateBadgeAsync();
}
// TODO: as of 2014-05-26, not sure this is needed anymore, since µMatrix
@ -421,7 +420,6 @@ var contentScriptSummaryHandler = function(tabId, details) {
r = µm.filterRequest(pageURL, 'plugin', url);
pageStore.recordRequest('plugin', url, r !== false, r);
}
pageStore.updateBadgeAsync();
}
// https://github.com/gorhill/httpswitchboard/issues/181

View file

@ -480,7 +480,6 @@ PageStore.prototype.init = function(tabContext) {
this.perLoadAllowedRequestCount = 0;
this.perLoadBlockedRequestCount = 0;
this.incinerationTimer = null;
this.updateBadgeTimer = null;
return this;
};
@ -501,11 +500,6 @@ PageStore.prototype.dispose = function() {
this.incinerationTimer = null;
}
if ( this.updateBadgeTimer !== null ) {
clearTimeout(this.updateBadgeTimer);
this.updateBadgeTimer = null;
}
if ( pageStoreJunkyard.length < 8 ) {
pageStoreJunkyard.push(this);
}
@ -520,6 +514,7 @@ PageStore.prototype.recordRequest = function(type, url, block) {
// https://github.com/gorhill/httpswitchboard/issues/306
// If it is recorded locally, record globally
µm.requestStats.record(type, block);
µm.updateBadgeAsync(this.tabId);
if ( block !== false ) {
this.perLoadBlockedRequestCount++;
@ -564,50 +559,6 @@ PageStore.prototype.recordRequest = function(type, url, block) {
/******************************************************************************/
// Update badge
// 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.updateBadgeAsync = (function() {
var tabIdToTimer = {};
var updateBadge = function(tabId) {
delete tabIdToTimer[tabId];
var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
return;
}
var iconId = null;
var badgeStr = '';
var total = pageStore.perLoadAllowedRequestCount +
pageStore.perLoadBlockedRequestCount;
if ( total ) {
var squareSize = 19;
var greenSize = squareSize * Math.sqrt(pageStore.perLoadAllowedRequestCount / total);
iconId = greenSize < squareSize/2 ? Math.ceil(greenSize) : Math.floor(greenSize);
badgeStr = µm.formatCount(pageStore.distinctRequestCount);
}
vAPI.setIcon(tabId, iconId, badgeStr);
};
return function() {
if ( vAPI.isBehindTheSceneTabId(this.tabId) ) {
return;
}
if ( tabIdToTimer.hasOwnProperty(this.tabId) ) {
return;
}
tabIdToTimer[this.tabId] = setTimeout(updateBadge.bind(null, this.tabId), 500);
};
})();
/******************************************************************************/
return {
factory: pageStoreFactory
};

View file

@ -437,6 +437,8 @@ vAPI.tabs.registerListeners();
// Create an entry for the tab if it doesn't exist
µm.bindTabToPageStats = function(tabId, context) {
this.updateBadgeAsync(tabId);
// Do not create a page store for URLs which are of no interests
// Example: dev console
var tabContext = this.tabContextManager.lookup(tabId);
@ -499,9 +501,6 @@ vAPI.tabs.registerListeners();
// console.debug('tab.js > bindTabToPageStats(): dispatching traffic in tab id %d to page store "%s"', tabId, pageUrl);
// https://github.com/gorhill/uMatrix/issues/37
pageStore.updateBadgeAsync();
return pageStore;
};
@ -610,7 +609,6 @@ vAPI.tabs.registerListeners();
var pageStore = this.pageStoreFromTabId(tabId);
if ( pageStore ) {
pageStore.recordRequest(type, url, blocked);
pageStore.updateBadgeAsync();
}
};

View file

@ -204,10 +204,7 @@ var onBeforeRootFrameRequestHandler = function(details) {
// Disallow request as per matrix?
var block = µm.mustBlock(tabContext.rootHostname, details.hostname, 'doc');
// console.debug('onBeforeRequestHandler()> block=%s "%s": %o', block, details.url, details);
pageStore.recordRequest('doc', requestURL, block);
pageStore.updateBadgeAsync();
// Not blocked
if ( !block ) {
@ -299,7 +296,6 @@ var onBeforeRequestHandler = function(details) {
// it is available.
var pageStore = µm.mustPageStoreFromTabId(details.tabId);
pageStore.recordRequest(requestType, requestURL, block);
pageStore.updateBadgeAsync();
// whitelisted?
if ( !block ) {
@ -373,7 +369,6 @@ var onBeforeSendHeadersHandler = function(details) {
if ( linkAuditor !== '' ) {
var block = µm.userSettings.processHyperlinkAuditing;
pageStore.recordRequest('other', requestURL + '{Ping-To:' + linkAuditor + '}', block);
pageStore.updateBadgeAsync();
if ( block ) {
µm.hyperlinkAuditingFoiledCounter += 1;
return { 'cancel': true };