tools/performance/startup/quit.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/performance/startup/quit.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,120 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/*
    1.10 +  From mozilla/toolkit/content
    1.11 +  These files did not have a license
    1.12 +*/
    1.13 +
    1.14 +function quitHook()
    1.15 +{
    1.16 +  var xhr = new XMLHttpRequest();
    1.17 +  xhr.open("GET", "http://" + location.host + "/server/shutdown", true);
    1.18 +  xhr.onreadystatechange = function (event)
    1.19 +    {
    1.20 +      if (xhr.readyState == 4)
    1.21 +        goQuitApplication();
    1.22 +    };
    1.23 +  xhr.send(null);
    1.24 +}
    1.25 +
    1.26 +function canQuitApplication()
    1.27 +{
    1.28 +  var os = Components.classes["@mozilla.org/observer-service;1"]
    1.29 +    .getService(Components.interfaces.nsIObserverService);
    1.30 +  if (!os) 
    1.31 +  {
    1.32 +    return true;
    1.33 +  }
    1.34 +  
    1.35 +  try 
    1.36 +  {
    1.37 +    var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"]
    1.38 +      .createInstance(Components.interfaces.nsISupportsPRBool);
    1.39 +    os.notifyObservers(cancelQuit, "quit-application-requested", null);
    1.40 +    
    1.41 +    // Something aborted the quit process. 
    1.42 +    if (cancelQuit.data)
    1.43 +    {
    1.44 +      return false;
    1.45 +    }
    1.46 +  }
    1.47 +  catch (ex) 
    1.48 +  {
    1.49 +  }
    1.50 +  os.notifyObservers(null, "quit-application-granted", null);
    1.51 +  return true;
    1.52 +}
    1.53 +
    1.54 +function goQuitApplication()
    1.55 +{
    1.56 +  const privs = 'UniversalXPConnect';
    1.57 +
    1.58 +  try
    1.59 +  {
    1.60 +    netscape.security.PrivilegeManager.enablePrivilege(privs);
    1.61 +  }
    1.62 +  catch(ex)
    1.63 +  {
    1.64 +    throw('goQuitApplication: privilege failure ' + ex);
    1.65 +  }
    1.66 +
    1.67 +  if (!canQuitApplication())
    1.68 +  {
    1.69 +    return false;
    1.70 +  }
    1.71 +
    1.72 +  const kAppStartup = '@mozilla.org/toolkit/app-startup;1';
    1.73 +  const kAppShell   = '@mozilla.org/appshell/appShellService;1';
    1.74 +  var   appService;
    1.75 +  var   forceQuit;
    1.76 +
    1.77 +  if (kAppStartup in Components.classes)
    1.78 +  {
    1.79 +    appService = Components.classes[kAppStartup].
    1.80 +      getService(Components.interfaces.nsIAppStartup);
    1.81 +    forceQuit  = Components.interfaces.nsIAppStartup.eForceQuit;
    1.82 +
    1.83 +  }
    1.84 +  else if (kAppShell in Components.classes)
    1.85 +  {
    1.86 +    appService = Components.classes[kAppShell].
    1.87 +      getService(Components.interfaces.nsIAppShellService);
    1.88 +    forceQuit = Components.interfaces.nsIAppShellService.eForceQuit;
    1.89 +  }
    1.90 +  else
    1.91 +  {
    1.92 +    throw 'goQuitApplication: no AppStartup/appShell';
    1.93 +  }
    1.94 +
    1.95 +  var windowManager = Components.
    1.96 +    classes['@mozilla.org/appshell/window-mediator;1'].getService();
    1.97 +
    1.98 +  var windowManagerInterface = windowManager.
    1.99 +    QueryInterface(Components.interfaces.nsIWindowMediator);
   1.100 +
   1.101 +  var enumerator = windowManagerInterface.getEnumerator(null);
   1.102 +
   1.103 +  while (enumerator.hasMoreElements())
   1.104 +  {
   1.105 +    var domWindow = enumerator.getNext();
   1.106 +    if (("tryToClose" in domWindow) && !domWindow.tryToClose())
   1.107 +    {
   1.108 +      return false;
   1.109 +    }
   1.110 +    domWindow.close();
   1.111 +  }
   1.112 +
   1.113 +  try
   1.114 +  {
   1.115 +    appService.quit(forceQuit);
   1.116 +  }
   1.117 +  catch(ex)
   1.118 +  {
   1.119 +    throw('goQuitApplication: ' + ex);
   1.120 +  }
   1.121 +
   1.122 +  return true;
   1.123 +}

mercurial