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

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

mercurial