Skip to content

Commit d60f908

Browse files
committed
Restore tree structure more correctly around tabs moved across windows #3918
1 parent 023246f commit d60f908

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

webextensions/background/api-tabs-listener.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ async function onAttached(tabId, attachInfo) {
11151115
try {
11161116
log('tabs.onAttached, id: ', tabId, attachInfo);
11171117
let tab = Tab.get(tabId);
1118+
await ensureSessionTabValueStored(tab);
11181119
let attachedTab = await browser.tabs.get(tabId).catch(ApiTabs.createErrorHandler(ApiTabs.handleMissingTabError));
11191120
if (!attachedTab) {
11201121
// We sometimes fail to get window and tab via API if it is opened
@@ -1212,6 +1213,26 @@ async function onAttached(tabId, attachInfo) {
12121213
}
12131214
}
12141215

1216+
// Workaround for https://github.com/piroor/treestyletab/issues/3918
1217+
async function ensureSessionTabValueStored(tab) {
1218+
const [oldId, states] = await Promise.all([
1219+
browser.sessions.getTabValue(tab.id, Constants.kPERSISTENT_ID).catch(ApiTabs.createErrorHandler()),
1220+
browser.sessions.getTabValue(tab.id, Constants.kPERSISTENT_STATES).catch(ApiTabs.createErrorHandler()),
1221+
]);
1222+
if ((!tab.$TST.uniqueId || oldId !== undefined) &&
1223+
(!tab.$TST.$lastPermanentStates || states !== undefined))
1224+
return;
1225+
1226+
log(`ensureSessionTabValueStored: The tab ${tab.id} has been moved from another window, and lost its session data unexpectedly, so now we try to save them again for safety.`);
1227+
await Promise.all([
1228+
tab.$TST.uniqueId && browser.sessions.setTabValue(tab.id, Constants.kPERSISTENT_ID, {
1229+
id: tab.$TST.uniqueId.id,
1230+
tabId: tab.id,
1231+
}).catch(ApiTabs.createErrorSuppressor()),
1232+
tab.$TST.$lastPermanentStates && browser.sessions.setTabValue(tab.id, Constants.kPERSISTENT_STATES, tab.$TST.$lastPermanentStates).catch(ApiTabs.createErrorSuppressor()),
1233+
]);
1234+
}
1235+
12151236
async function onDetached(tabId, detachInfo) {
12161237
if (mPromisedStarted)
12171238
await mPromisedStarted;

webextensions/common/TreeItem.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,11 @@ export class Tab extends TreeItem {
13251325
// We should initialize private properties with blank value for better performance with a fixed shape.
13261326
this.delayedInheritSoundStateFromChildren = null;
13271327
this.delayedInheritSharingStateFromChildren = null;
1328+
1329+
this.$lastPermanentStates = null;
1330+
browser.sessions.getTabValue(raw.id, Constants.kPERSISTENT_STATES).then(states => {
1331+
this.$lastPermanentStates = states;
1332+
}).catch(ApiTabs.handleMissingTabError);
13281333
}
13291334

13301335
destroy() {
@@ -2812,6 +2817,7 @@ export class Tab extends TreeItem {
28122817
const states = this.raw && await browser.sessions.getTabValue(this.id, Constants.kPERSISTENT_STATES).catch(ApiTabs.handleMissingTabError);
28132818
// We need to cleanup invalid values stored accidentally.
28142819
// See also: https://github.com/piroor/treestyletab/issues/2882
2820+
this.$lastPermanentStates = states;
28152821
return states && mapAndFilterUniq(states, state => state && String(state) || undefined) || [];
28162822
}
28172823

webextensions/common/unique-id.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ export async function request(tabOrId, options = {}) {
198198

199199
let oldId;
200200
if (mRestoredPersistentIdMap.has(tab.id)) {
201+
log('tab', tab.id, ': in mRestoredPersistentIdMap');
201202
oldId = mRestoredPersistentIdMap.get(tab.id);
202203
mRestoredPersistentIdMap.delete(tab.id);
203204
}
204205
else {
205206
oldId = await browser.sessions.getTabValue(tab.id, Constants.kPERSISTENT_ID).catch(ApiTabs.createErrorHandler());
207+
log('tab', tab.id, ': oldId from session tabValue: ', oldId);
206208
}
207209
if (oldId && !oldId.tabId) // ignore broken information!
208210
oldId = null;

0 commit comments

Comments
 (0)