tools/quitter/QuitterObserver.js

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:2a07661bb97b
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
5 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
6 Components.utils.import("resource://gre/modules/Services.jsm");
7
8 const Cc = Components.classes;
9 const Ci = Components.interfaces;
10
11 const CHILD_SCRIPT = "chrome://quitter/content/contentscript.js";
12
13 /* XPCOM gunk */
14 function QuitterObserver() {}
15
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,
23
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") {
30
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);
35
36 messageManager.loadFrameScript(CHILD_SCRIPT, true);
37 this.isFrameScriptLoaded = true;
38 } else if (aTopic == "xpcom-shutdown") {
39 this.uninit();
40 }
41 },
42
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 },
49
50 uninit: function()
51 {
52 var obs = Services.obs;
53 obs.removeObserver(this, "chrome-document-global-created", false);
54 },
55
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 };
69
70 const NSGetFactory = XPCOMUtils.generateNSGetFactory([QuitterObserver]);

mercurial