michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); michael@0: Components.utils.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: michael@0: const CHILD_SCRIPT = "chrome://quitter/content/contentscript.js"; michael@0: michael@0: /* XPCOM gunk */ michael@0: function QuitterObserver() {} michael@0: michael@0: QuitterObserver.prototype = { michael@0: classDescription: "Quitter Observer for use in testing.", michael@0: classID: Components.ID("{c235a986-5ac1-4f28-ad73-825dae9bad90}"), michael@0: contractID: "@mozilla.org/quitter-observer;1", michael@0: QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIObserver]), michael@0: _xpcom_categories: [{category: "profile-after-change", service: true }], michael@0: isFrameScriptLoaded: false, michael@0: michael@0: observe: function(aSubject, aTopic, aData) michael@0: { michael@0: if (aTopic == "profile-after-change") { michael@0: this.init(); michael@0: } else if (!this.isFrameScriptLoaded && michael@0: aTopic == "chrome-document-global-created") { michael@0: michael@0: var messageManager = Cc["@mozilla.org/globalmessagemanager;1"]. michael@0: getService(Ci.nsIMessageBroadcaster); michael@0: // Register for any messages our API needs us to handle michael@0: messageManager.addMessageListener("Quitter.Quit", this); michael@0: michael@0: messageManager.loadFrameScript(CHILD_SCRIPT, true); michael@0: this.isFrameScriptLoaded = true; michael@0: } else if (aTopic == "xpcom-shutdown") { michael@0: this.uninit(); michael@0: } michael@0: }, michael@0: michael@0: init: function() michael@0: { michael@0: var obs = Services.obs; michael@0: obs.addObserver(this, "xpcom-shutdown", false); michael@0: obs.addObserver(this, "chrome-document-global-created", false); michael@0: }, michael@0: michael@0: uninit: function() michael@0: { michael@0: var obs = Services.obs; michael@0: obs.removeObserver(this, "chrome-document-global-created", false); michael@0: }, michael@0: michael@0: /** michael@0: * messageManager callback function michael@0: * This will get requests from our API in the window and process them in chrome for it michael@0: **/ michael@0: receiveMessage: function(aMessage) { michael@0: switch(aMessage.name) { michael@0: case "Quitter.Quit": michael@0: let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup); michael@0: appStartup.quit(Ci.nsIAppStartup.eForceQuit); michael@0: break; michael@0: } michael@0: } michael@0: }; michael@0: michael@0: const NSGetFactory = XPCOMUtils.generateNSGetFactory([QuitterObserver]);