QuitterObserver.js 2.3 KB

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