From eb42375215d88df2cdfb1ea7e7350f99964f885d Mon Sep 17 00:00:00 2001 From: gorhill Date: Fri, 1 May 2015 23:54:32 -0400 Subject: [PATCH] code review --- src/js/async.js | 48 +++++++++++++++++++++++++++++++++++------- src/js/messaging.js | 2 -- src/js/pagestats.js | 51 +-------------------------------------------- src/js/tab.js | 6 ++---- src/js/traffic.js | 5 ----- 5 files changed, 43 insertions(+), 69 deletions(-) diff --git a/src/js/async.js b/src/js/async.js index a0ec58f..2f275d2 100644 --- a/src/js/async.js +++ b/src/js/async.js @@ -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); + }; +})(); /******************************************************************************/ diff --git a/src/js/messaging.js b/src/js/messaging.js index 3d4e708..46e0209 100644 --- a/src/js/messaging.js +++ b/src/js/messaging.js @@ -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 diff --git a/src/js/pagestats.js b/src/js/pagestats.js index df64819..74a2abd 100644 --- a/src/js/pagestats.js +++ b/src/js/pagestats.js @@ -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 }; diff --git a/src/js/tab.js b/src/js/tab.js index d2a79fd..ac88ac5 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -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(); } }; diff --git a/src/js/traffic.js b/src/js/traffic.js index 530ed36..9b99130 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -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 };