tools/quitter/QuitterObserver.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 /* 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 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
     6 Components.utils.import("resource://gre/modules/Services.jsm");
     8 const Cc = Components.classes;
     9 const Ci = Components.interfaces;
    11 const CHILD_SCRIPT = "chrome://quitter/content/contentscript.js";
    13 /* XPCOM gunk */
    14 function QuitterObserver() {}
    16 QuitterObserver.prototype = {
    17   classDescription: "Quitter Observer for use in testing.",
    18   classID:          Components.ID("{c235a986-5ac1-4f28-ad73-825dae9bad90}"),
    19   contractID:       "@mozilla.org/quitter-observer;1",
    20   QueryInterface:   XPCOMUtils.generateQI([Components.interfaces.nsIObserver]),
    21   _xpcom_categories: [{category: "profile-after-change", service: true }],
    22   isFrameScriptLoaded: false,
    24   observe: function(aSubject, aTopic, aData)
    25   {
    26     if (aTopic == "profile-after-change") {
    27       this.init();
    28     } else if (!this.isFrameScriptLoaded &&
    29                aTopic == "chrome-document-global-created") {
    31       var messageManager = Cc["@mozilla.org/globalmessagemanager;1"].
    32                            getService(Ci.nsIMessageBroadcaster);
    33       // Register for any messages our API needs us to handle
    34       messageManager.addMessageListener("Quitter.Quit", this);
    36       messageManager.loadFrameScript(CHILD_SCRIPT, true);
    37       this.isFrameScriptLoaded = true;
    38     } else if (aTopic == "xpcom-shutdown") {
    39       this.uninit();
    40     }
    41   },
    43   init: function()
    44   {
    45     var obs = Services.obs;
    46     obs.addObserver(this, "xpcom-shutdown", false);
    47     obs.addObserver(this, "chrome-document-global-created", false);
    48   },
    50   uninit: function()
    51   {
    52     var obs = Services.obs;
    53     obs.removeObserver(this, "chrome-document-global-created", false);
    54   },
    56   /**
    57    * messageManager callback function
    58    * This will get requests from our API in the window and process them in chrome for it
    59    **/
    60   receiveMessage: function(aMessage) {
    61     switch(aMessage.name) {
    62       case "Quitter.Quit":
    63         let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup);
    64         appStartup.quit(Ci.nsIAppStartup.eForceQuit);
    65         break;
    66     }
    67   }
    68 };
    70 const NSGetFactory = XPCOMUtils.generateNSGetFactory([QuitterObserver]);

mercurial