Browse Source

code review

pull/2/head
gorhill 10 years ago
parent
commit
eb42375215
  1. 42
      src/js/async.js
  2. 2
      src/js/messaging.js
  3. 51
      src/js/pagestats.js
  4. 6
      src/js/tab.js
  5. 5
      src/js/traffic.js

42
src/js/async.js

@ -130,16 +130,48 @@ return asyncJobManager;
/******************************************************************************/ /******************************************************************************/
// Update visual of extension icon.
// A time out is used to coalesce adjacent requests to update badge.
// 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.
µMatrix.updateBadgeAsync = (function() {
var tabIdToTimer = Object.create(null);
var updateBadge = function(tabId) {
delete tabIdToTimer[tabId];
µMatrix.updateBadgeAsync = function(tabId) {
var pageStore = this.pageStoreFromTabId(tabId); var pageStore = this.pageStoreFromTabId(tabId);
if ( pageStore ) {
pageStore.updateBadge();
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);
};
})();
/******************************************************************************/ /******************************************************************************/
// Notify whoever care that url stats have changed (they need to // Notify whoever care that url stats have changed (they need to

2
src/js/messaging.js

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

51
src/js/pagestats.js

@ -480,7 +480,6 @@ PageStore.prototype.init = function(tabContext) {
this.perLoadAllowedRequestCount = 0; this.perLoadAllowedRequestCount = 0;
this.perLoadBlockedRequestCount = 0; this.perLoadBlockedRequestCount = 0;
this.incinerationTimer = null; this.incinerationTimer = null;
this.updateBadgeTimer = null;
return this; return this;
}; };
@ -501,11 +500,6 @@ PageStore.prototype.dispose = function() {
this.incinerationTimer = null; this.incinerationTimer = null;
} }
if ( this.updateBadgeTimer !== null ) {
clearTimeout(this.updateBadgeTimer);
this.updateBadgeTimer = null;
}
if ( pageStoreJunkyard.length < 8 ) { if ( pageStoreJunkyard.length < 8 ) {
pageStoreJunkyard.push(this); pageStoreJunkyard.push(this);
} }
@ -520,6 +514,7 @@ PageStore.prototype.recordRequest = function(type, url, block) {
// https://github.com/gorhill/httpswitchboard/issues/306 // https://github.com/gorhill/httpswitchboard/issues/306
// If it is recorded locally, record globally // If it is recorded locally, record globally
µm.requestStats.record(type, block); µm.requestStats.record(type, block);
µm.updateBadgeAsync(this.tabId);
if ( block !== false ) { if ( block !== false ) {
this.perLoadBlockedRequestCount++; 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 { return {
factory: pageStoreFactory factory: pageStoreFactory
}; };

6
src/js/tab.js

@ -437,6 +437,8 @@ vAPI.tabs.registerListeners();
// Create an entry for the tab if it doesn't exist // Create an entry for the tab if it doesn't exist
µm.bindTabToPageStats = function(tabId, context) { µm.bindTabToPageStats = function(tabId, context) {
this.updateBadgeAsync(tabId);
// Do not create a page store for URLs which are of no interests // Do not create a page store for URLs which are of no interests
// Example: dev console // Example: dev console
var tabContext = this.tabContextManager.lookup(tabId); 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); // 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; return pageStore;
}; };
@ -610,7 +609,6 @@ vAPI.tabs.registerListeners();
var pageStore = this.pageStoreFromTabId(tabId); var pageStore = this.pageStoreFromTabId(tabId);
if ( pageStore ) { if ( pageStore ) {
pageStore.recordRequest(type, url, blocked); pageStore.recordRequest(type, url, blocked);
pageStore.updateBadgeAsync();
} }
}; };

5
src/js/traffic.js

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

Loading…
Cancel
Save