1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 function closeWindow(aClose, aPromptFunction, aSource) {
6 let { AppConstants } = ChromeUtils.importESModule(
7 "resource://gre/modules/AppConstants.sys.mjs"
10 // Closing the last window doesn't quit the application on OS X.
11 if (AppConstants.platform != "macosx") {
13 for (let w of Services.wm.getEnumerator(null)) {
17 if (++windowCount == 2) {
22 // If we're down to the last window and someone tries to shut down, check to make sure we can!
23 if (windowCount == 1 && !canQuitApplication("lastwindow", aSource)) {
28 typeof aPromptFunction == "function" &&
29 !aPromptFunction(aSource)
34 // If the user explicitly closes the last tabs in the window close remaining tabs. Bug 490136
36 window.SessionStore?.maybeDontRestoreTabs(window);
39 typeof aPromptFunction == "function" &&
40 !aPromptFunction(aSource)
53 function canQuitApplication(aData, aSource) {
54 const kCID = "@mozilla.org/browser/browserglue;1";
55 if (kCID in Cc && !(aData || "").includes("restart")) {
56 let BrowserGlue = Cc[kCID].getService(Ci.nsISupports).wrappedJSObject;
57 BrowserGlue._registerQuitSource(aSource);
60 var cancelQuit = Cc["@mozilla.org/supports-PRBool;1"].createInstance(
63 Services.obs.notifyObservers(
65 "quit-application-requested",
69 // Something aborted the quit process.
70 if (cancelQuit.data) {
77 function goQuitApplication(event) {
78 // We can't know for sure if the user used a shortcut to trigger quit.
79 // Proxy by means of checking for the shortcut modifier.
80 let isMac = navigator.platform.startsWith("Mac");
81 let key = isMac ? "metaKey" : "ctrlKey";
85 // Note that macOS likes pretending something came from this menu even if
86 // activated by keyboard shortcut, hence checking that first.
87 } else if (event.sourceEvent?.target?.id?.startsWith("menu_")) {
89 } else if (event.sourceEvent?.target?.id?.startsWith("appMenu")) {
92 if (!canQuitApplication(undefined, source)) {
96 Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit);
101 // Command Updater functions
103 function goUpdateCommand(aCommand) {
106 top.document.commandDispatcher.getControllerForCommand(aCommand);
110 enabled = controller.isCommandEnabled(aCommand);
113 goSetCommandEnabled(aCommand, enabled);
115 console.error("An error occurred updating the ", aCommand, " command: ", e);
119 function goDoCommand(aCommand) {
122 top.document.commandDispatcher.getControllerForCommand(aCommand);
123 if (controller && controller.isCommandEnabled(aCommand)) {
124 controller.doCommand(aCommand);
128 "An error occurred executing the ",
136 function goSetCommandEnabled(aID, aEnabled) {
137 var node = document.getElementById(aID);
141 node.removeAttribute("disabled");
143 node.setAttribute("disabled", "true");