1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/tools/quitter/QuitterObserver.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,70 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +Components.utils.import("resource://gre/modules/XPCOMUtils.jsm"); 1.9 +Components.utils.import("resource://gre/modules/Services.jsm"); 1.10 + 1.11 +const Cc = Components.classes; 1.12 +const Ci = Components.interfaces; 1.13 + 1.14 +const CHILD_SCRIPT = "chrome://quitter/content/contentscript.js"; 1.15 + 1.16 +/* XPCOM gunk */ 1.17 +function QuitterObserver() {} 1.18 + 1.19 +QuitterObserver.prototype = { 1.20 + classDescription: "Quitter Observer for use in testing.", 1.21 + classID: Components.ID("{c235a986-5ac1-4f28-ad73-825dae9bad90}"), 1.22 + contractID: "@mozilla.org/quitter-observer;1", 1.23 + QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIObserver]), 1.24 + _xpcom_categories: [{category: "profile-after-change", service: true }], 1.25 + isFrameScriptLoaded: false, 1.26 + 1.27 + observe: function(aSubject, aTopic, aData) 1.28 + { 1.29 + if (aTopic == "profile-after-change") { 1.30 + this.init(); 1.31 + } else if (!this.isFrameScriptLoaded && 1.32 + aTopic == "chrome-document-global-created") { 1.33 + 1.34 + var messageManager = Cc["@mozilla.org/globalmessagemanager;1"]. 1.35 + getService(Ci.nsIMessageBroadcaster); 1.36 + // Register for any messages our API needs us to handle 1.37 + messageManager.addMessageListener("Quitter.Quit", this); 1.38 + 1.39 + messageManager.loadFrameScript(CHILD_SCRIPT, true); 1.40 + this.isFrameScriptLoaded = true; 1.41 + } else if (aTopic == "xpcom-shutdown") { 1.42 + this.uninit(); 1.43 + } 1.44 + }, 1.45 + 1.46 + init: function() 1.47 + { 1.48 + var obs = Services.obs; 1.49 + obs.addObserver(this, "xpcom-shutdown", false); 1.50 + obs.addObserver(this, "chrome-document-global-created", false); 1.51 + }, 1.52 + 1.53 + uninit: function() 1.54 + { 1.55 + var obs = Services.obs; 1.56 + obs.removeObserver(this, "chrome-document-global-created", false); 1.57 + }, 1.58 + 1.59 + /** 1.60 + * messageManager callback function 1.61 + * This will get requests from our API in the window and process them in chrome for it 1.62 + **/ 1.63 + receiveMessage: function(aMessage) { 1.64 + switch(aMessage.name) { 1.65 + case "Quitter.Quit": 1.66 + let appStartup = Cc["@mozilla.org/toolkit/app-startup;1"].getService(Ci.nsIAppStartup); 1.67 + appStartup.quit(Ci.nsIAppStartup.eForceQuit); 1.68 + break; 1.69 + } 1.70 + } 1.71 +}; 1.72 + 1.73 +const NSGetFactory = XPCOMUtils.generateNSGetFactory([QuitterObserver]);