Backed out changeset bcbab342eed8 (bug 1889658) for causing wpt reftest failures...
[gecko.git] / toolkit / content / globalOverlay.js
blob2476ce73a0f988ab96ee181c9716387185f68b32
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"
8   );
10   // Closing the last window doesn't quit the application on OS X.
11   if (AppConstants.platform != "macosx") {
12     var windowCount = 0;
13     for (let w of Services.wm.getEnumerator(null)) {
14       if (w.closed) {
15         continue;
16       }
17       if (++windowCount == 2) {
18         break;
19       }
20     }
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)) {
24       return false;
25     }
26     if (
27       windowCount != 1 &&
28       typeof aPromptFunction == "function" &&
29       !aPromptFunction(aSource)
30     ) {
31       return false;
32     }
34     // If the user explicitly closes the last tabs in the window close remaining tabs. Bug 490136
35     if (aClose) {
36       window.SessionStore?.maybeDontRestoreTabs(window);
37     }
38   } else if (
39     typeof aPromptFunction == "function" &&
40     !aPromptFunction(aSource)
41   ) {
42     return false;
43   }
45   if (aClose) {
46     window.close();
47     return window.closed;
48   }
50   return true;
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);
58   }
59   try {
60     var cancelQuit = Cc["@mozilla.org/supports-PRBool;1"].createInstance(
61       Ci.nsISupportsPRBool
62     );
63     Services.obs.notifyObservers(
64       cancelQuit,
65       "quit-application-requested",
66       aData || null
67     );
69     // Something aborted the quit process.
70     if (cancelQuit.data) {
71       return false;
72     }
73   } catch (ex) {}
74   return true;
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";
82   let source = "OS";
83   if (event[key]) {
84     source = "shortcut";
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_")) {
88     source = "menuitem";
89   } else if (event.sourceEvent?.target?.id?.startsWith("appMenu")) {
90     source = "appmenu";
91   }
92   if (!canQuitApplication(undefined, source)) {
93     return false;
94   }
96   Services.startup.quit(Ci.nsIAppStartup.eAttemptQuit);
97   return true;
101 // Command Updater functions
103 function goUpdateCommand(aCommand) {
104   try {
105     var controller =
106       top.document.commandDispatcher.getControllerForCommand(aCommand);
108     var enabled = false;
109     if (controller) {
110       enabled = controller.isCommandEnabled(aCommand);
111     }
113     goSetCommandEnabled(aCommand, enabled);
114   } catch (e) {
115     console.error("An error occurred updating the ", aCommand, " command: ", e);
116   }
119 function goDoCommand(aCommand) {
120   try {
121     var controller =
122       top.document.commandDispatcher.getControllerForCommand(aCommand);
123     if (controller && controller.isCommandEnabled(aCommand)) {
124       controller.doCommand(aCommand);
125     }
126   } catch (e) {
127     console.error(
128       "An error occurred executing the ",
129       aCommand,
130       " command: ",
131       e
132     );
133   }
136 function goSetCommandEnabled(aID, aEnabled) {
137   var node = document.getElementById(aID);
139   if (node) {
140     if (aEnabled) {
141       node.removeAttribute("disabled");
142     } else {
143       node.setAttribute("disabled", "true");
144     }
145   }