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

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

mercurial