Skip to content

Commit

Permalink
ESM-ification
Browse files Browse the repository at this point in the history
  • Loading branch information
117649 committed Jun 11, 2024
1 parent 888c31d commit 52fe275
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 91 deletions.
4 changes: 2 additions & 2 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
Expand Down
3 changes: 1 addition & 2 deletions resource/defaultsContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 2 additions & 0 deletions resource/modules/GroupItems.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,7 @@ this.GroupItems = {

this._lastActiveList.append(groupItem);
this.arrange(true);
Storage._prepWindowToRestoreInto(gWindow);
UI.updateTabButton();
},

Expand All @@ -2681,6 +2682,7 @@ this.GroupItems = {
this._arrangesPending.delete(groupItem);
this.arrange(true);

Storage._prepWindowToRestoreInto(gWindow);
UI.updateTabButton();
},

Expand Down
138 changes: 62 additions & 76 deletions resource/modules/Storage.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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_'];
}
};
8 changes: 4 additions & 4 deletions resource/modules/content/utils/ChildProcess.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion resource/modules/utils/Piggyback.jsm
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand Down

0 comments on commit 52fe275

Please sign in to comment.