tools/performance/startup/quit.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 From mozilla/toolkit/content
michael@0 8 These files did not have a license
michael@0 9 */
michael@0 10
michael@0 11 function quitHook()
michael@0 12 {
michael@0 13 var xhr = new XMLHttpRequest();
michael@0 14 xhr.open("GET", "http://" + location.host + "/server/shutdown", true);
michael@0 15 xhr.onreadystatechange = function (event)
michael@0 16 {
michael@0 17 if (xhr.readyState == 4)
michael@0 18 goQuitApplication();
michael@0 19 };
michael@0 20 xhr.send(null);
michael@0 21 }
michael@0 22
michael@0 23 function canQuitApplication()
michael@0 24 {
michael@0 25 var os = Components.classes["@mozilla.org/observer-service;1"]
michael@0 26 .getService(Components.interfaces.nsIObserverService);
michael@0 27 if (!os)
michael@0 28 {
michael@0 29 return true;
michael@0 30 }
michael@0 31
michael@0 32 try
michael@0 33 {
michael@0 34 var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"]
michael@0 35 .createInstance(Components.interfaces.nsISupportsPRBool);
michael@0 36 os.notifyObservers(cancelQuit, "quit-application-requested", null);
michael@0 37
michael@0 38 // Something aborted the quit process.
michael@0 39 if (cancelQuit.data)
michael@0 40 {
michael@0 41 return false;
michael@0 42 }
michael@0 43 }
michael@0 44 catch (ex)
michael@0 45 {
michael@0 46 }
michael@0 47 os.notifyObservers(null, "quit-application-granted", null);
michael@0 48 return true;
michael@0 49 }
michael@0 50
michael@0 51 function goQuitApplication()
michael@0 52 {
michael@0 53 const privs = 'UniversalXPConnect';
michael@0 54
michael@0 55 try
michael@0 56 {
michael@0 57 netscape.security.PrivilegeManager.enablePrivilege(privs);
michael@0 58 }
michael@0 59 catch(ex)
michael@0 60 {
michael@0 61 throw('goQuitApplication: privilege failure ' + ex);
michael@0 62 }
michael@0 63
michael@0 64 if (!canQuitApplication())
michael@0 65 {
michael@0 66 return false;
michael@0 67 }
michael@0 68
michael@0 69 const kAppStartup = '@mozilla.org/toolkit/app-startup;1';
michael@0 70 const kAppShell = '@mozilla.org/appshell/appShellService;1';
michael@0 71 var appService;
michael@0 72 var forceQuit;
michael@0 73
michael@0 74 if (kAppStartup in Components.classes)
michael@0 75 {
michael@0 76 appService = Components.classes[kAppStartup].
michael@0 77 getService(Components.interfaces.nsIAppStartup);
michael@0 78 forceQuit = Components.interfaces.nsIAppStartup.eForceQuit;
michael@0 79
michael@0 80 }
michael@0 81 else if (kAppShell in Components.classes)
michael@0 82 {
michael@0 83 appService = Components.classes[kAppShell].
michael@0 84 getService(Components.interfaces.nsIAppShellService);
michael@0 85 forceQuit = Components.interfaces.nsIAppShellService.eForceQuit;
michael@0 86 }
michael@0 87 else
michael@0 88 {
michael@0 89 throw 'goQuitApplication: no AppStartup/appShell';
michael@0 90 }
michael@0 91
michael@0 92 var windowManager = Components.
michael@0 93 classes['@mozilla.org/appshell/window-mediator;1'].getService();
michael@0 94
michael@0 95 var windowManagerInterface = windowManager.
michael@0 96 QueryInterface(Components.interfaces.nsIWindowMediator);
michael@0 97
michael@0 98 var enumerator = windowManagerInterface.getEnumerator(null);
michael@0 99
michael@0 100 while (enumerator.hasMoreElements())
michael@0 101 {
michael@0 102 var domWindow = enumerator.getNext();
michael@0 103 if (("tryToClose" in domWindow) && !domWindow.tryToClose())
michael@0 104 {
michael@0 105 return false;
michael@0 106 }
michael@0 107 domWindow.close();
michael@0 108 }
michael@0 109
michael@0 110 try
michael@0 111 {
michael@0 112 appService.quit(forceQuit);
michael@0 113 }
michael@0 114 catch(ex)
michael@0 115 {
michael@0 116 throw('goQuitApplication: ' + ex);
michael@0 117 }
michael@0 118
michael@0 119 return true;
michael@0 120 }

mercurial