From 52fe27574004366e02de1e7a6dd55eb118d2feae Mon Sep 17 00:00:00 2001 From: 117649 Date: Tue, 11 Jun 2024 23:59:31 +0800 Subject: [PATCH] ESM-ification --- bootstrap.js | 4 +- resource/defaultsContent.js | 3 +- resource/modules/GroupItems.jsm | 2 + resource/modules/Storage.jsm | 138 ++++++++---------- .../modules/content/utils/ChildProcess.jsm | 8 +- ...oduleInSandbox.jsm => ModuleInSandbox.mjs} | 5 +- resource/modules/utils/Piggyback.jsm | 2 +- .../{PluralForm.jsm => PluralForm.sys.mjs} | 4 +- 8 files changed, 75 insertions(+), 91 deletions(-) rename resource/modules/content/utils/{ModuleInSandbox.jsm => ModuleInSandbox.mjs} (95%) rename resource/modules/utils/{PluralForm.jsm => PluralForm.sys.mjs} (99%) diff --git a/bootstrap.js b/bootstrap.js index 40365355..7d14af56 100644 --- a/bootstrap.js +++ b/bootstrap.js @@ -73,7 +73,7 @@ var addonUris = { }; var {classes: Cc, interfaces: Ci, utils: Cu, manager: Cm, results: Cr} = Components; -var { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs"); +var {XPCOMUtils} = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs"); ChromeUtils.defineESModuleGetters(this, {AddonManager: "resource://gre/modules/AddonManager.sys.mjs", PlacesUtils: "resource://gre/modules/PlacesUtils.sys.mjs", @@ -284,7 +284,7 @@ async function startup(aData, aReason) { Services.scriptloader.loadSubScript(defaultsURI, this); // Get the utils.jsm module into our sandbox - ChromeUtils.defineModuleGetter(this, "PluralForm", "chrome://"+objPathString+"-resource/content/modules/utils/PluralForm.jsm"); + ChromeUtils.defineESModuleGetters(this, {PluralForm: "chrome://"+objPathString+"-resource/content/modules/utils/PluralForm.sys.mjs"}); Services.scriptloader.loadSubScript("chrome://"+objPathString+"-resource/content/modules/utils/Modules.jsm", this); Services.scriptloader.loadSubScript("chrome://"+objPathString+"-resource/content/modules/utils/sandboxUtilsPreload.jsm", this); Modules.load("utils/sandboxUtils"); diff --git a/resource/defaultsContent.js b/resource/defaultsContent.js index 7aaf057c..82d20566 100644 --- a/resource/defaultsContent.js +++ b/resource/defaultsContent.js @@ -6,7 +6,6 @@ // By using a JSM, we can initialize each individual tab (frame) with our scripts without having to instanciate the same objects with each one. (function(frame) { - let targetScope = {}; - Components.utils.import("chrome://tabgroups-resource/content/modules/content/utils/ModuleInSandbox.jsm", targetScope); + let targetScope = ChromeUtils.importESModule("chrome://tabgroups-resource/content/modules/content/utils/ModuleInSandbox.mjs"); targetScope.ModuleInSandbox.init('tabgroups', frame); })(this); diff --git a/resource/modules/GroupItems.jsm b/resource/modules/GroupItems.jsm index 5b8f0116..49c8de14 100644 --- a/resource/modules/GroupItems.jsm +++ b/resource/modules/GroupItems.jsm @@ -2661,6 +2661,7 @@ this.GroupItems = { this._lastActiveList.append(groupItem); this.arrange(true); + Storage._prepWindowToRestoreInto(gWindow); UI.updateTabButton(); }, @@ -2681,6 +2682,7 @@ this.GroupItems = { this._arrangesPending.delete(groupItem); this.arrange(true); + Storage._prepWindowToRestoreInto(gWindow); UI.updateTabButton(); }, diff --git a/resource/modules/Storage.jsm b/resource/modules/Storage.jsm index 60250314..60cd941c 100644 --- a/resource/modules/Storage.jsm +++ b/resource/modules/Storage.jsm @@ -122,109 +122,95 @@ this.Storage = { }; Modules.LOADMODULE = function() { - Storage._scope = Cu.import("resource:///modules/sessionstore/SessionStore.jsm", self); - Storage._migrationScope = Cu.import("resource:///modules/sessionstore/SessionMigration.jsm", {}); + Storage._scope = ChromeUtils.importESModule("resource:///modules/sessionstore/SessionStore.sys.mjs"); + self.SessionStore = Storage._scope.SessionStore; + // Storage._migrationScope = Cu.import("resource:///modules/sessionstore/SessionMigration.jsm", {}); - Piggyback.add('Storage', Storage._scope.SessionStoreInternal, '_prepWindowToRestoreInto', function(aWindow) { + Storage._prepWindowToRestoreInto = function(aWindow) { if(!aWindow) { return; } // Step 1 of processing: // Inspect extData for Tab Groups identifiers. If found, then we want to inspect further. // If there is a single group, then we can use this window. If there are multiple groups then we won't use this window. - let groupsData = this.getCustomWindowValue(aWindow, Storage.kGroupsIdentifier); + let groupsData = SessionStore.getCustomWindowValue(aWindow, Storage.kGroupsIdentifier); if(groupsData) { groupsData = JSON.parse(groupsData); // If there are multiple groups, we don't want to use this window. - if(groupsData.totalNumber > 1) { - return [false, false]; + if (groupsData.totalNumber > 1) { + if (aWindow.__SS_lastSessionWindowID) { + aWindow["_" + objPathString + "__SS_lastSessionWindowID"] = aWindow.__SS_lastSessionWindowID; + aWindow.__SS_lastSessionWindowID = "_" + objPathString + "_removed_" + AddonData.initTime; + let t = Storage._scope._LastSession.getState()?.windows + .find(w => w.__lastSessionWindowID == aWindow["_" + objPathString + "__SS_lastSessionWindowID"]); + if (t) { + t["_" + objPathString + "__lastSessionWindowID"] = t.__lastSessionWindowID; + delete t.__lastSessionWindowID; + } + } + } else { + if (aWindow["_" + objPathString + "__SS_lastSessionWindowID"]) { + aWindow.__SS_lastSessionWindowID = aWindow["_" + objPathString + "__SS_lastSessionWindowID"]; + let t = Storage._scope._LastSession.getState()?.windows + .find(w => w["_" + objPathString + "__lastSessionWindowID"] == aWindow["_" + objPathString + "__SS_lastSessionWindowID"]); + if (t) t.__lastSessionWindowID = t["_" + objPathString + "__lastSessionWindowID"]; + } } } + }; - return this.__prepWindowToRestoreInto(aWindow); - }); - - Piggyback.add('Storage', Storage._scope.SessionStoreInternal, 'restoreWindow', function(aWindow, winData, aOptions) { + Storage._WindowRestoring = e =>{ // Deinitialize tab view in any window that will have its session overwritten. // Nothing in TabView will be useful anyway, just let it reinistialize if/when necessary later by itself. - if(aWindow.TabView) { - aWindow.TabView._deinitFrame(); + if(window.TabView) { + window.TabView._deinitFrame(); } + } - // Call the original method. - this._restoreWindow(aWindow, winData, aOptions); - + Storage._WindowRestored = e => { // Update our button's label, to reflect the groups data in the new/different session. - if(aWindow.TabView) { - aWindow.TabView.setButtonLabel(); + if(window.TabView) { + window.TabView.setButtonLabel(); } - if(aWindow.TabView && !aWindow.TabView._iframe && aWindow.TabView._initialized) aWindow.TabView._initFrame(() => { - aWindow.TabView._window[objName].GroupItems.resumeArrange(); - aWindow.TabView._window[objName].TabItems.resumePainting(); - aWindow.TabView._window[objName].GroupItems.pauseArrange(); - aWindow.TabView._window[objName].TabItems.pausePainting(); - aWindow.gBrowser.tabs.forEach(t => t.addEventListener('SSTabRestored', _ => { - aWindow.TabView._window[objName].TabItems.startHeartbeatHidden(); - aWindow.TabView._window[objName].TabItems.update(t); + if(window.TabView && !window.TabView._iframe && window.TabView._initialized) window.TabView._initFrame(() => { + window.TabView._window[objName].GroupItems.resumeArrange(); + window.TabView._window[objName].TabItems.resumePainting(); + window.TabView._window[objName].GroupItems.pauseArrange(); + window.TabView._window[objName].TabItems.pausePainting(); + window.gBrowser.tabs.forEach(t => t.addEventListener('SSTabRestored', _ => { + window.TabView._window[objName].TabItems.startHeartbeatHidden(); + window.TabView._window[objName].TabItems.update(t); }, {once: true})); }); - }); + } - Piggyback.add('Storage', Storage._migrationScope.SessionMigrationInternal, 'convertState', function(aStateObj) { - let state = { - selectedWindow: aStateObj.selectedWindow, - _closedWindows: [] - }; - state.windows = aStateObj.windows.map(function(oldWin) { - let win = { extData: {} }; - win.tabs = oldWin.tabs.map(function(oldTab) { - let tab = {}; - // Keep only titles, urls and triggeringPrincipals for history entries - tab.entries = oldTab.entries.map(function(entry) { - return { - url: entry.url, - triggeringPrincipal_base64: entry.triggeringPrincipal_base64, - title: entry.title, - }; - }); - tab.index = oldTab.index; - tab.hidden = oldTab.hidden; - tab.pinned = oldTab.pinned; - - // The tabgroup info is in the extData, so we need to get it out. - if(oldTab.extData && Storage.kTabIdentifier in oldTab.extData) { - tab.extData = { [Storage.kTabIdentifier]: oldTab.extData[Storage.kTabIdentifier] }; - } - return tab; - }); - - // There are various tabgroup-related attributes that we need to get out of the session restore data for the window, too. - if(oldWin.extData) { - for(let k of Object.keys(oldWin.extData)) { - if(k.startsWith("tabview-")) { - win.extData[k] = oldWin.extData[k]; - } - } - } + Storage._obs = function obs(subject, topic) { + try { Storage._prepWindowToRestoreInto(window); } catch (e) { Cu.reportError(e); } + } + Storage._obs(); + Services.obs.addObserver(Storage._obs, "sessionstore-windows-restored"); + Services.obs.addObserver(Storage._obs, "sessionstore-initiating-manual-restore"); + window.addEventListener("SSWindowRestoring", Storage._WindowRestoring); + window.addEventListener("SSWindowRestored", Storage._WindowRestored); - win.selected = oldWin.selected; - win._closedTabs = []; - return win; - }); - let url = "about:welcomeback"; - let formdata = { id: { sessionData: state }, url }; - let entry = { - url, - triggeringPrincipal_base64: lazy.E10SUtils.SERIALIZED_SYSTEMPRINCIPAL, - }; - return { windows: [ { tabs: [ { entries: [ entry ], formdata } ] } ] }; }); }; Modules.UNLOADMODULE = function() { - Piggyback.revert('Storage', Storage._scope.SessionStoreInternal, '_prepWindowToRestoreInto'); - Piggyback.revert('Storage', Storage._scope.SessionStoreInternal, 'restoreWindow'); - Piggyback.revert('Storage', Storage._migrationScope.SessionMigrationInternal, 'convertState'); + Services.obs.removeObserver(Storage._obs, "sessionstore-windows-restored"); + Services.obs.removeObserver(Storage._obs, "sessionstore-initiating-manual-restore"); + if (window["_" + objPathString + "__SS_lastSessionWindowID"]) { + window.__SS_lastSessionWindowID = window["_" + objPathString + "__SS_lastSessionWindowID"]; + let t = Storage._scope._LastSession.getState()?.windows + .find(w => w["_" + objPathString + "__lastSessionWindowID"] == window["_" + objPathString + "__SS_lastSessionWindowID"]); + if (t) t.__lastSessionWindowID = t["_" + objPathString + "__lastSessionWindowID"]; + } + window.removeEventListener("SSWindowRestoring", Storage._WindowRestoring); + window.removeEventListener("SSWindowRestored", Storage._WindowRestored); + if (ChromeUtils.importESModule("resource:///modules/sessionstore/SessionMigration.sys.mjs").SessionMigration.migrate['_Piggyback_']) { + ChromeUtils.importESModule("resource:///modules/sessionstore/SessionMigration.sys.mjs").SessionMigration.migrate = ChromeUtils.importESModule("resource:///modules/sessionstore/SessionMigration.sys.mjs").SessionMigration.migrate['_Piggyback_'] + delete ChromeUtils.importESModule("resource:///modules/sessionstore/SessionMigration.sys.mjs").SessionMigration.migrate['_Piggyback_']; + } }; diff --git a/resource/modules/content/utils/ChildProcess.jsm b/resource/modules/content/utils/ChildProcess.jsm index 5be688de..3d3c7cd4 100644 --- a/resource/modules/content/utils/ChildProcess.jsm +++ b/resource/modules/content/utils/ChildProcess.jsm @@ -41,10 +41,10 @@ // see DOMContentLoaded.add var {classes: Cc, interfaces: Ci, utils: Cu, manager: Cm} = Components; -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -XPCOMUtils.defineLazyModuleGetters(this, { - console: "resource://gre/modules/Console.jsm", - PluralForm: "chrome://" + objPathString + "-resource/content/modules/utils/PluralForm.jsm", +var {XPCOMUtils} = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs"); +ChromeUtils.defineESModuleGetters(this, { + console: "resource://gre/modules/Console.sys.mjs", + PluralForm: "chrome://" + objPathString + "-resource/content/modules/utils/PluralForm.sys.mjs", }); XPCOMUtils.defineLazyServiceGetter(Services, "navigator", "@mozilla.org/network/protocol;1?name=http", "nsIHttpProtocolHandler"); diff --git a/resource/modules/content/utils/ModuleInSandbox.jsm b/resource/modules/content/utils/ModuleInSandbox.mjs similarity index 95% rename from resource/modules/content/utils/ModuleInSandbox.jsm rename to resource/modules/content/utils/ModuleInSandbox.mjs index 53b969ce..144dec84 100644 --- a/resource/modules/content/utils/ModuleInSandbox.jsm +++ b/resource/modules/content/utils/ModuleInSandbox.mjs @@ -12,17 +12,16 @@ // When bug 1195689 is fixed, we'll be able to use ChildProcess.jsm directly and discard this file completely. -var EXPORTED_SYMBOLS = [ "ModuleInSandbox" ]; - var {classes: Cc, interfaces: Ci, utils: Cu} = Components; var gSandbox = null; -var ModuleInSandbox = { +export var ModuleInSandbox = { init: function(objPathString, aFrame) { if(!gSandbox) { let systemPrincipal = Cc["@mozilla.org/systemprincipal;1"].createInstance(Ci.nsIPrincipal); gSandbox = Cu.Sandbox(systemPrincipal, { freshZone: true, sandboxName: objPathString+"-ModuleInSandbox" }); + gSandbox.ChromeUtils = ChromeUtils; Services.scriptloader.loadSubScript("chrome://"+objPathString+"-resource/content/modules/content/utils/ChildProcess.jsm", gSandbox); } gSandbox.ChildProcess.init(objPathString, aFrame, this); diff --git a/resource/modules/utils/Piggyback.jsm b/resource/modules/utils/Piggyback.jsm index bca74a4b..839057a3 100644 --- a/resource/modules/utils/Piggyback.jsm +++ b/resource/modules/utils/Piggyback.jsm @@ -165,7 +165,7 @@ Modules.LOADMODULE = function() { if(self.isContent) { return; } // CustomizableUI is a special case, as CustomizableUIInternal is frozen and not exported - self.CUIBackstage = Cu.import("resource:///modules/CustomizableUI.jsm", self); + self.CUIBackstage = self.CustomizableUI = ChromeUtils.importESModule("resource:///modules/CustomizableUI.sys.mjs").CustomizableUI; // CUIBackstage[Piggyback._obj] = { // replaceInternal: function(objs) { // if(!CUIBackstage.__CustomizableUIInternal) { diff --git a/resource/modules/utils/PluralForm.jsm b/resource/modules/utils/PluralForm.sys.mjs similarity index 99% rename from resource/modules/utils/PluralForm.jsm rename to resource/modules/utils/PluralForm.sys.mjs index e3d93530..a4d0ffc4 100644 --- a/resource/modules/utils/PluralForm.jsm +++ b/resource/modules/utils/PluralForm.sys.mjs @@ -2,8 +2,6 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -var EXPORTED_SYMBOLS = ["PluralForm"]; - /** * This module provides the PluralForm object which contains a method to figure * out which plural form of a word to use for a given number based on the @@ -188,7 +186,7 @@ var gFunctions = [ /* eslint-enable no-nested-ternary */ -var PluralForm = { +export var PluralForm = { /** * Get the correct plural form of a word based on the number *