Skip to content

Commit

Permalink
Use unique window id across sessions (for window restoration)
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Nov 22, 2023
1 parent 23a25a1 commit 433429e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
5 changes: 3 additions & 2 deletions webextensions/background/background-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 => {
Expand Down
22 changes: 18 additions & 4 deletions webextensions/common/unique-id.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 4 additions & 3 deletions webextensions/sidebar/sidebar-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 => {
Expand Down

0 comments on commit 433429e

Please sign in to comment.