Skip to content

Commit 30dc6c5

Browse files
authored
fix(session): use tab offsets when restoring saved tree structures (#3926)
Replace string position matching with tab ID offset lookup during session restore. The restore code needs a tab array index, not a character offset.
1 parent d60f908 commit 30dc6c5

1 file changed

Lines changed: 21 additions & 4 deletions

File tree

webextensions/background/tree-structure.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import EventListenerManager from '/extlib/EventListenerManager.js';
1010
import {
1111
log as internalLogger,
1212
dumpTab,
13-
toLines,
1413
configs,
1514
wait,
1615
stack,
@@ -85,6 +84,25 @@ async function saveTreeStructure(windowId) {
8584
).catch(ApiTabs.createErrorSuppressor());
8685
}
8786

87+
function findStructureOffset(uniqueIds, structure) {
88+
if (!structure.length)
89+
return 0;
90+
if (structure.length > uniqueIds.length)
91+
return -1;
92+
93+
const offset = uniqueIds.indexOf(structure[0].id);
94+
if (offset < 0 ||
95+
offset + structure.length > uniqueIds.length)
96+
return -1;
97+
98+
for (let index = 1; index < structure.length; index++) {
99+
if (uniqueIds[offset + index] != structure[index].id)
100+
return -1;
101+
}
102+
103+
return offset;
104+
}
105+
88106
export async function loadTreeStructure(windows, restoredFromCacheResults) {
89107
log('loadTreeStructure ', restoredFromCacheResults);
90108
MetricsData.add('loadTreeStructure: start');
@@ -106,16 +124,15 @@ export async function loadTreeStructure(windows, restoredFromCacheResults) {
106124
uniqueIds = uniqueIds.map(id => id.id);
107125
let tabsOffset;
108126
if (structure[0].id) {
109-
const structureSignature = toLines(structure, item => item.id);
110-
tabsOffset = uniqueIds.join('\n').indexOf(structureSignature);
127+
tabsOffset = findStructureOffset(uniqueIds, structure);
111128
windowStateCompletelyApplied = tabsOffset > -1;
112129
}
113130
else {
114131
tabsOffset = 0;
115132
windowStateCompletelyApplied = structure.length == tabs.length;
116133
}
117134
if (tabsOffset > -1) {
118-
const structureRestoreTabs = tabs.slice(tabsOffset);
135+
const structureRestoreTabs = tabs.slice(tabsOffset, tabsOffset + structure.length);
119136
await Tree.applyTreeStructureToTabs(structureRestoreTabs, structure);
120137
for (const tab of structureRestoreTabs) {
121138
tab.$TST.temporaryMetadata.set('treeStructureAlreadyRestoredFromSessionData', true);

0 commit comments

Comments
 (0)