michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: From mozilla/toolkit/content michael@0: These files did not have a license michael@0: */ michael@0: michael@0: function quitHook() michael@0: { michael@0: var xhr = new XMLHttpRequest(); michael@0: xhr.open("GET", "http://" + location.host + "/server/shutdown", true); michael@0: xhr.onreadystatechange = function (event) michael@0: { michael@0: if (xhr.readyState == 4) michael@0: goQuitApplication(); michael@0: }; michael@0: xhr.send(null); michael@0: } michael@0: michael@0: function canQuitApplication() michael@0: { michael@0: var os = Components.classes["@mozilla.org/observer-service;1"] michael@0: .getService(Components.interfaces.nsIObserverService); michael@0: if (!os) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: try michael@0: { michael@0: var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"] michael@0: .createInstance(Components.interfaces.nsISupportsPRBool); michael@0: os.notifyObservers(cancelQuit, "quit-application-requested", null); michael@0: michael@0: // Something aborted the quit process. michael@0: if (cancelQuit.data) michael@0: { michael@0: return false; michael@0: } michael@0: } michael@0: catch (ex) michael@0: { michael@0: } michael@0: os.notifyObservers(null, "quit-application-granted", null); michael@0: return true; michael@0: } michael@0: michael@0: function goQuitApplication() michael@0: { michael@0: const privs = 'UniversalXPConnect'; michael@0: michael@0: try michael@0: { michael@0: netscape.security.PrivilegeManager.enablePrivilege(privs); michael@0: } michael@0: catch(ex) michael@0: { michael@0: throw('goQuitApplication: privilege failure ' + ex); michael@0: } michael@0: michael@0: if (!canQuitApplication()) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: const kAppStartup = '@mozilla.org/toolkit/app-startup;1'; michael@0: const kAppShell = '@mozilla.org/appshell/appShellService;1'; michael@0: var appService; michael@0: var forceQuit; michael@0: michael@0: if (kAppStartup in Components.classes) michael@0: { michael@0: appService = Components.classes[kAppStartup]. michael@0: getService(Components.interfaces.nsIAppStartup); michael@0: forceQuit = Components.interfaces.nsIAppStartup.eForceQuit; michael@0: michael@0: } michael@0: else if (kAppShell in Components.classes) michael@0: { michael@0: appService = Components.classes[kAppShell]. michael@0: getService(Components.interfaces.nsIAppShellService); michael@0: forceQuit = Components.interfaces.nsIAppShellService.eForceQuit; michael@0: } michael@0: else michael@0: { michael@0: throw 'goQuitApplication: no AppStartup/appShell'; michael@0: } michael@0: michael@0: var windowManager = Components. michael@0: classes['@mozilla.org/appshell/window-mediator;1'].getService(); michael@0: michael@0: var windowManagerInterface = windowManager. michael@0: QueryInterface(Components.interfaces.nsIWindowMediator); michael@0: michael@0: var enumerator = windowManagerInterface.getEnumerator(null); michael@0: michael@0: while (enumerator.hasMoreElements()) michael@0: { michael@0: var domWindow = enumerator.getNext(); michael@0: if (("tryToClose" in domWindow) && !domWindow.tryToClose()) michael@0: { michael@0: return false; michael@0: } michael@0: domWindow.close(); michael@0: } michael@0: michael@0: try michael@0: { michael@0: appService.quit(forceQuit); michael@0: } michael@0: catch(ex) michael@0: { michael@0: throw('goQuitApplication: ' + ex); michael@0: } michael@0: michael@0: return true; michael@0: }