diff --git a/webextensions/background/background-cache.js b/webextensions/background/background-cache.js index e8f054a40..bac687be6 100644 --- a/webextensions/background/background-cache.js +++ b/webextensions/background/background-cache.js @@ -18,6 +18,7 @@ import * as MetricsData from '/common/metrics-data.js'; import * as TabsInternalOperation from '/common/tabs-internal-operation.js'; import * as TabsStore from '/common/tabs-store.js'; import * as TabsUpdate from '/common/tabs-update.js'; +import * as UniqueId from '/common/unique-id.js'; import Tab from '/common/Tab.js'; @@ -300,7 +301,7 @@ async function updateWindowCache(owner, key, value) { if (!configs.persistCachedTree && browser.storage.session) { - const storagKey = `backgroundCache-window${owner.windowId}-${key}`; + const storagKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`; if (value) { const data = {}; data[storagKey] = value; @@ -343,7 +344,7 @@ export function markWindowCacheDirtyFromTab(tab, akey) { async function getWindowCache(owner, key) { if (!configs.persistCachedTree) { - const storageKey = `backgroundCache-window${owner.windowId}-${key}`; + const storageKey = `backgroundCache-${await UniqueId.ensureWindowId(owner.windowId)}-${key}`; const defaultData = {}; defaultData[storageKey] = undefined; return browser.storage.session.get(defaultData).then(data => { diff --git a/webextensions/common/unique-id.js b/webextensions/common/unique-id.js index b0861e99c..89969cbe9 100644 --- a/webextensions/common/unique-id.js +++ b/webextensions/common/unique-id.js @@ -151,17 +151,31 @@ export async function request(tabOrId, options = {}) { } } - const adjective = kID_ADJECTIVES[Math.floor(Math.random() * kID_ADJECTIVES.length)]; - const noun = kID_NOUNS[Math.floor(Math.random() * kID_NOUNS.length)]; - const randomValue = Math.floor(Math.random() * 1000); - const id = `tab-${adjective}-${noun}-${Date.now()}-${randomValue}`; + const id = `tab-${generate()}`; // tabId is for detecttion of duplicated tabs await browser.sessions.setTabValue(tab.id, Constants.kPERSISTENT_ID, { id, tabId: tab.id }).catch(ApiTabs.createErrorSuppressor()); return { id, originalId, originalTabId, duplicated }; } +function generate() { + const adjective = kID_ADJECTIVES[Math.floor(Math.random() * kID_ADJECTIVES.length)]; + const noun = kID_NOUNS[Math.floor(Math.random() * kID_NOUNS.length)]; + const randomValue = Math.floor(Math.random() * 1000); + return `${adjective}-${noun}-${Date.now()}-${randomValue}`; +} + export async function getFromTabs(tabs) { return Promise.all(tabs.map(tab => browser.sessions.getTabValue(tab.id, Constants.kPERSISTENT_ID).catch(ApiTabs.createErrorHandler()) )); } + +export async function ensureWindowId(windowId) { + const storedUniqueId = await browser.sessions.getWindowValue(windowId, 'uniqueId').catch(_ => null); + if (storedUniqueId) + return storedUniqueId; + + const uniqueId = `window-${generate()}`; + await browser.sessions.setWindowValue(windowId, 'uniqueId', uniqueId).catch(_ => null); + return uniqueId; +} diff --git a/webextensions/sidebar/sidebar-cache.js b/webextensions/sidebar/sidebar-cache.js index 90cfe7ec9..f5ab172a4 100644 --- a/webextensions/sidebar/sidebar-cache.js +++ b/webextensions/sidebar/sidebar-cache.js @@ -21,6 +21,7 @@ import * as Constants from '/common/constants.js'; import * as MetricsData from '/common/metrics-data.js'; import * as TabsStore from '/common/tabs-store.js'; import * as TabsUpdate from '/common/tabs-update.js'; +import * as UniqueId from '/common/unique-id.js'; import * as UserOperationBlocker from '/common/user-operation-blocker.js'; import Tab from '/common/Tab.js'; @@ -435,10 +436,10 @@ async function fixupTabsRestoredFromCache(tabElements, tabs, options = {}) { // updating cache // =================================================================== -function updateWindowCache(key, value) { +async function updateWindowCache(key, value) { if (!configs.persistCachedTree && browser.storage.session) { - const storagKey = `sidebarCache-window${mTargetWindow}-${key}`; + const storagKey = `sidebarCache-${await UniqueId.ensureWindowId(mTargetWindow)}-${key}`; if (value) { const data = {}; data[storagKey] = value; @@ -493,7 +494,7 @@ export function markWindowCacheDirty(key) { async function getWindowCache(key) { if (!configs.persistCachedTree) { - const storageKey = `sidebarCache-window${mTargetWindow}-${key}`; + const storageKey = `sidebarCache-${await UniqueId.ensureWindowId(mTargetWindow)}-${key}`; const defaultData = {}; defaultData[storageKey] = undefined; return browser.storage.session.get(defaultData).then(data => {