Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Jun 24, 2016
1 parent e8e06dc commit ee16ae7
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions platform/firefox/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -821,17 +821,21 @@ vAPI.tabs.get = function(tabId, callback) {
return browser;
}

if ( !browser ) {
if ( !browser || !browser.currentURI ) {
callback();
return;
}

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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit ee16ae7

Please sign in to comment.