TelemetryStartup.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* -*- js-indent-level: 2; indent-tabs-mode: nil -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. "use strict";
  6. const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
  7. Cu.import("resource://gre/modules/XPCOMUtils.jsm", this);
  8. XPCOMUtils.defineLazyModuleGetter(this, "TelemetryController",
  9. "resource://gre/modules/TelemetryController.jsm");
  10. XPCOMUtils.defineLazyModuleGetter(this, "TelemetryEnvironment",
  11. "resource://gre/modules/TelemetryEnvironment.jsm");
  12. /**
  13. * TelemetryStartup is needed to forward the "profile-after-change" notification
  14. * to TelemetryController.jsm.
  15. */
  16. function TelemetryStartup() {
  17. }
  18. TelemetryStartup.prototype.classID = Components.ID("{117b219f-92fe-4bd2-a21b-95a342a9d474}");
  19. TelemetryStartup.prototype.QueryInterface = XPCOMUtils.generateQI([Components.interfaces.nsIObserver]);
  20. TelemetryStartup.prototype.observe = function(aSubject, aTopic, aData) {
  21. if (aTopic == "profile-after-change" || aTopic == "app-startup") {
  22. TelemetryController.observe(null, aTopic, null);
  23. }
  24. if (aTopic == "profile-after-change") {
  25. annotateEnvironment();
  26. TelemetryEnvironment.registerChangeListener("CrashAnnotator", annotateEnvironment);
  27. TelemetryEnvironment.onInitialized().then(() => annotateEnvironment());
  28. }
  29. }
  30. function annotateEnvironment() {
  31. try {
  32. let cr = Cc["@mozilla.org/toolkit/crash-reporter;1"];
  33. if (cr) {
  34. let env = JSON.stringify(TelemetryEnvironment.currentEnvironment);
  35. cr.getService(Ci.nsICrashReporter).annotateCrashReport("TelemetryEnvironment", env);
  36. }
  37. } catch (e) {
  38. // crash reporting not built or disabled? Ignore errors
  39. }
  40. }
  41. this.NSGetFactory = XPCOMUtils.generateNSGetFactory([TelemetryStartup]);