diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index a7e4b5139..319d13496 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -821,7 +821,7 @@ vAPI.tabs.get = function(tabId, callback) { return browser; } - if ( !browser ) { + if ( !browser || !browser.currentURI ) { callback(); return; } @@ -829,9 +829,13 @@ vAPI.tabs.get = function(tabId, callback) { var win = getOwnerWindow(browser); var tabBrowser = getTabBrowser(win); + // https://github.com/gorhill/uMatrix/issues/540 + // The `index` property is nowhere used by uBlock at this point, so we + // will refrain from returning this information for the time being. + callback({ id: tabId, - index: tabWatcher.indexFromTarget(browser), + index: undefined, windowId: winWatcher.idFromWindow(win), active: tabBrowser !== null && browser === tabBrowser.selectedBrowser, url: browser.currentURI.asciiSpec, @@ -1098,12 +1102,16 @@ vAPI.tabs.injectScript = function(tabId, details, callback) { /******************************************************************************/ var tabWatcher = (function() { - // TODO: find out whether we need a janitor to take care of stale entries. - var browserToTabIdMap = new Map(); + // https://github.com/gorhill/uMatrix/issues/540 + // Use only weak references to hold onto browser references. + var browserToTabIdMap = new WeakMap(); var tabIdToBrowserMap = new Map(); var tabIdGenerator = 1; var indexFromBrowser = function(browser) { + if ( !browser ) { + return -1; + } // TODO: Add support for this if ( vAPI.thunderbird ) { return -1; @@ -1193,22 +1201,14 @@ var tabWatcher = (function() { if ( tabId === undefined ) { tabId = '' + tabIdGenerator++; browserToTabIdMap.set(browser, tabId); - tabIdToBrowserMap.set(tabId, browser); + tabIdToBrowserMap.set(tabId, Cu.getWeakReference(browser)); } return tabId; }; var browserFromTabId = function(tabId) { - var browser = tabIdToBrowserMap.get(tabId); - if ( browser === undefined ) { - return null; - } - // Verify that the browser is still live - if ( indexFromBrowser(browser) !== -1 ) { - return browser; - } - removeBrowserEntry(tabId, browser); - return null; + var weakref = tabIdToBrowserMap.get(tabId); + return weakref && weakref.get() || null; }; var currentBrowser = function() { @@ -1244,6 +1244,19 @@ var tabWatcher = (function() { onClose({ target: target }); }; + var getAllBrowsers = function() { + var browsers = [], browser; + for ( var weakref of tabIdToBrowserMap.values() ) { + browser = weakref.get(); + // TODO: + // Maybe call removeBrowserEntry() if the browser no longer exists? + if ( browser ) { + browsers.push(browser); + } + } + return browsers; + }; + // https://developer.mozilla.org/en-US/docs/Web/Events/TabShow var onShow = function({target}) { tabIdFromTarget(target); @@ -1404,14 +1417,14 @@ var tabWatcher = (function() { for ( var win of winWatcher.getWindows() ) { onWindowUnload(win); } - browserToTabIdMap.clear(); + browserToTabIdMap = new WeakMap(); tabIdToBrowserMap.clear(); }; cleanupTasks.push(stop); return { - browsers: function() { return browserToTabIdMap.keys(); }, + browsers: getAllBrowsers, browserFromTabId: browserFromTabId, browserFromTarget: browserFromTarget, currentBrowser: currentBrowser,