From 2cba5b94ec81e0116d660bfc4139a6b57a2d6988 Mon Sep 17 00:00:00 2001 From: "mr. m" Date: Thu, 25 Dec 2025 01:48:10 +0100 Subject: [PATCH] feat: Improve migration for new profiles updating to window sync, b=no-bug, c=workspaces --- .../sessionstore/SessionStore-sys-mjs.patch | 53 ++++++++++--------- .../sessionstore/ZenSessionManager.sys.mjs | 18 ++++++- src/zen/sessionstore/ZenWindowSync.sys.mjs | 5 +- src/zen/workspaces/ZenWorkspaces.mjs | 2 +- 4 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/browser/components/sessionstore/SessionStore-sys-mjs.patch b/src/browser/components/sessionstore/SessionStore-sys-mjs.patch index 4c660ca25..f9a69b9e2 100644 --- a/src/browser/components/sessionstore/SessionStore-sys-mjs.patch +++ b/src/browser/components/sessionstore/SessionStore-sys-mjs.patch @@ -1,5 +1,5 @@ diff --git a/browser/components/sessionstore/SessionStore.sys.mjs b/browser/components/sessionstore/SessionStore.sys.mjs -index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c87549060ccd78d 100644 +index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..4e54fb8ae1a4735a4437c469f2d974bcdd8a572f 100644 --- a/browser/components/sessionstore/SessionStore.sys.mjs +++ b/browser/components/sessionstore/SessionStore.sys.mjs @@ -127,6 +127,8 @@ const TAB_EVENTS = [ @@ -39,7 +39,15 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 let tabbrowser = aWindow.gBrowser; // add tab change listeners to all already existing tabs -@@ -2151,7 +2160,6 @@ var SessionStoreInternal = { +@@ -2107,6 +2116,7 @@ var SessionStoreInternal = { + null, + "sessionstore-one-or-no-tab-restored" + ); ++ lazy.ZenSessionStore.onNewEmptySession(aWindow); + this._deferredAllWindowsRestored.resolve(); + } + // this window was opened by _openWindowWithState +@@ -2151,7 +2161,6 @@ var SessionStoreInternal = { if (closedWindowState) { let newWindowState; if ( @@ -47,7 +55,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 !lazy.SessionStartup.willRestore() ) { // We want to split the window up into pinned tabs and unpinned tabs. -@@ -2215,6 +2223,15 @@ var SessionStoreInternal = { +@@ -2215,6 +2224,15 @@ var SessionStoreInternal = { }); this._shouldRestoreLastSession = false; } @@ -63,7 +71,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 if (this._restoreLastWindow && aWindow.toolbar.visible) { // always reset (if not a popup window) -@@ -2465,7 +2482,7 @@ var SessionStoreInternal = { +@@ -2465,7 +2483,7 @@ var SessionStoreInternal = { // 2) Flush the window. // 3) When the flush is complete, revisit our decision to store the window // in _closedWindows, and add/remove as necessary. @@ -72,7 +80,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 this.maybeSaveClosedWindow(winData, isLastWindow); } -@@ -2486,7 +2503,7 @@ var SessionStoreInternal = { +@@ -2486,7 +2504,7 @@ var SessionStoreInternal = { // Save non-private windows if they have at // least one saveable tab or are the last window. @@ -81,7 +89,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 this.maybeSaveClosedWindow(winData, isLastWindow); if (!isLastWindow && winData.closedId > -1) { -@@ -2582,6 +2599,7 @@ var SessionStoreInternal = { +@@ -2582,6 +2600,7 @@ var SessionStoreInternal = { let alreadyStored = winIndex != -1; // If sidebar command is truthy, i.e. sidebar is open, store sidebar settings let shouldStore = hasSaveableTabs || isLastWindow; @@ -89,7 +97,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 if (shouldStore && !alreadyStored) { let index = this._closedWindows.findIndex(win => { -@@ -3373,7 +3391,7 @@ var SessionStoreInternal = { +@@ -3373,7 +3392,7 @@ var SessionStoreInternal = { if (!isPrivateWindow && tabState.isPrivate) { return; } @@ -98,7 +106,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 return; } -@@ -4089,6 +4107,12 @@ var SessionStoreInternal = { +@@ -4089,6 +4108,12 @@ var SessionStoreInternal = { Math.min(tabState.index, tabState.entries.length) ); tabState.pinned = false; @@ -111,7 +119,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 if (inBackground === false) { aWindow.gBrowser.selectedTab = newTab; -@@ -4525,6 +4549,7 @@ var SessionStoreInternal = { +@@ -4525,6 +4550,7 @@ var SessionStoreInternal = { // Append the tab if we're opening into a different window, tabIndex: aSource == aTargetWindow ? pos : Infinity, pinned: state.pinned, @@ -119,7 +127,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 userContextId: state.userContextId, skipLoad: true, preferredRemoteType, -@@ -5032,7 +5057,10 @@ var SessionStoreInternal = { +@@ -5032,7 +5058,10 @@ var SessionStoreInternal = { !activePageData || (activePageData && activePageData.url != "about:blank") ) { @@ -130,7 +138,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 } lazy.TabStateCache.update(browser.permanentKey, { image: null, -@@ -5374,7 +5402,7 @@ var SessionStoreInternal = { +@@ -5374,7 +5403,7 @@ var SessionStoreInternal = { for (let i = tabbrowser.pinnedTabCount; i < tabbrowser.tabs.length; i++) { let tab = tabbrowser.tabs[i]; @@ -139,7 +147,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 removableTabs.push(tab); } } -@@ -5483,7 +5511,7 @@ var SessionStoreInternal = { +@@ -5483,7 +5512,7 @@ var SessionStoreInternal = { // collect the data for all windows for (ix in this._windows) { @@ -148,7 +156,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 // window data is still in _statesToRestore continue; } -@@ -5625,11 +5653,12 @@ var SessionStoreInternal = { +@@ -5625,11 +5654,12 @@ var SessionStoreInternal = { } let tabbrowser = aWindow.gBrowser; @@ -162,7 +170,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 // update the internal state data for this window for (let tab of tabs) { if (tab == aWindow.FirefoxViewHandler.tab) { -@@ -5640,6 +5669,9 @@ var SessionStoreInternal = { +@@ -5640,6 +5670,9 @@ var SessionStoreInternal = { tabsData.push(tabData); } @@ -172,7 +180,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 // update tab group state for this window winData.groups = []; for (let tabGroup of aWindow.gBrowser.tabGroups) { -@@ -5652,7 +5684,7 @@ var SessionStoreInternal = { +@@ -5652,7 +5685,7 @@ var SessionStoreInternal = { // a window is closed, point to the first item in the tab strip instead (it will never be the Firefox View tab, // since it's only inserted into the tab strip after it's selected). if (aWindow.FirefoxViewHandler.tab?.selected) { @@ -181,7 +189,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 winData.title = tabbrowser.tabs[0].label; } winData.selected = selectedIndex; -@@ -5765,8 +5797,8 @@ var SessionStoreInternal = { +@@ -5765,8 +5798,8 @@ var SessionStoreInternal = { // selectTab represents. let selectTab = 0; if (overwriteTabs) { @@ -192,7 +200,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 selectTab = Math.min(selectTab, winData.tabs.length); } -@@ -5788,6 +5820,7 @@ var SessionStoreInternal = { +@@ -5788,6 +5821,7 @@ var SessionStoreInternal = { if (overwriteTabs) { for (let i = tabbrowser.browsers.length - 1; i >= 0; i--) { if (!tabbrowser.tabs[i].selected) { @@ -200,7 +208,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 tabbrowser.removeTab(tabbrowser.tabs[i]); } } -@@ -5821,6 +5854,9 @@ var SessionStoreInternal = { +@@ -5821,6 +5855,9 @@ var SessionStoreInternal = { savedTabGroup => !openTabGroupIdsInWindow.has(savedTabGroup.id) ); } @@ -210,7 +218,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 // Move the originally open tabs to the end. if (initialTabs) { -@@ -6372,6 +6408,28 @@ var SessionStoreInternal = { +@@ -6372,6 +6409,25 @@ var SessionStoreInternal = { // Most of tabData has been restored, now continue with restoring // attributes that may trigger external events. @@ -227,9 +235,6 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 + if (tabData.zenHasStaticIcon) { + tab.setAttribute("zen-has-static-icon", "true"); + } -+ if (tabData.zenSyncId) { -+ tab.setAttribute("id", tabData.zenSyncId); -+ } + if (tabData.zenDefaultUserContextId) { + tab.setAttribute("zenDefaultUserContextId", true); + } @@ -239,7 +244,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 if (tabData.pinned) { tabbrowser.pinTab(tab); -@@ -7290,7 +7348,7 @@ var SessionStoreInternal = { +@@ -7290,7 +7346,7 @@ var SessionStoreInternal = { let groupsToSave = new Map(); for (let tIndex = 0; tIndex < window.tabs.length; ) { @@ -248,7 +253,7 @@ index 2c2f43bf743ef458b378e85e9ed44a971711e1d9..33f630dea72da70352e131144c875490 // Adjust window.selected if (tIndex + 1 < window.selected) { window.selected -= 1; -@@ -7305,7 +7363,7 @@ var SessionStoreInternal = { +@@ -7305,7 +7361,7 @@ var SessionStoreInternal = { ); // We don't want to increment tIndex here. continue; diff --git a/src/zen/sessionstore/ZenSessionManager.sys.mjs b/src/zen/sessionstore/ZenSessionManager.sys.mjs index 176d4a102..d6a31eec3 100644 --- a/src/zen/sessionstore/ZenSessionManager.sys.mjs +++ b/src/zen/sessionstore/ZenSessionManager.sys.mjs @@ -68,6 +68,10 @@ export class nsZenSessionManager { compression: SHOULD_COMPRESS_FILE ? 'lz4' : undefined, backupFile, }); + + lazy.SessionStore.promiseAllWindowsRestored.then(() => { + delete this._migrationSpaceData; + }); } log(...args) { @@ -149,7 +153,6 @@ export class nsZenSessionManager { for (const winData of initialState?.windows || []) { winData.spaces = this._migrationSpaceData || []; } - delete this._migrationSpaceData; return; } // If there's no initial state, nothing to restore. This would @@ -352,6 +355,19 @@ export class nsZenSessionManager { SessionStoreInternal.initializeWindow(aWindow, newState); } + /** + * Called when a new empty session is created. For example, + * when creating a new profile or when the user installed it for + * the first time. + * @param {*} aWindow + * @returns + */ + onNewEmptySession(aWindow) { + aWindow.gZenWorkspaces.restoreWorkspacesFromSessionStore({ + spaces: this.#sidebar.spaces || [], + }); + } + /** * Gets the cloned spaces data from the sidebar object. * This is used during migration to restore spaces into diff --git a/src/zen/sessionstore/ZenWindowSync.sys.mjs b/src/zen/sessionstore/ZenWindowSync.sys.mjs index c62bf2766..b421bb89b 100644 --- a/src/zen/sessionstore/ZenWindowSync.sys.mjs +++ b/src/zen/sessionstore/ZenWindowSync.sys.mjs @@ -182,6 +182,9 @@ class nsZenWindowSync { tab.id = this.#newTabSyncId; lazy.TabStateFlusher.flush(tab.linkedBrowser); } + if (tab.pinned && !tab._zenPinnedInitialState) { + this.setPinnedTabState(tab); + } } }); } @@ -947,7 +950,7 @@ class nsZenWindowSync { on_focus(aEvent) { const { ownerGlobal: window } = aEvent.target; if ( - !window.gBrowser || + !window?.gBrowser || this.#lastFocusedWindow?.deref() === window || window.closing || !window.toolbar.visible diff --git a/src/zen/workspaces/ZenWorkspaces.mjs b/src/zen/workspaces/ZenWorkspaces.mjs index 3108b1da6..872b6af93 100644 --- a/src/zen/workspaces/ZenWorkspaces.mjs +++ b/src/zen/workspaces/ZenWorkspaces.mjs @@ -284,7 +284,7 @@ class nsZenWorkspaces { }); }; this._pinnedTabsResizeObserver = new ResizeObserver(onResize); - if (this.privateWindowOrDisabled || this._workspaceCache.length === 0) { + if (this.privateWindowOrDisabled) { await this.restoreWorkspacesFromSessionStore({}); } }