performance-entries.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. * The performanceEntries actor emits events corresponding to performance
  6. * entries. It receives `performanceentry` events containing the performance
  7. * entry details and emits an event containing the name, type, origin, and
  8. * epoch of the performance entry.
  9. */
  10. const { Actor, ActorClassWithSpec } = require("devtools/shared/protocol");
  11. const performanceSpec = require("devtools/shared/specs/performance-entries");
  12. const events = require("sdk/event/core");
  13. var PerformanceEntriesActor = ActorClassWithSpec(performanceSpec, {
  14. listenerAdded: false,
  15. initialize: function (conn, tabActor) {
  16. Actor.prototype.initialize.call(this, conn);
  17. this.window = tabActor.window;
  18. },
  19. /**
  20. * Start tracking the user timings.
  21. */
  22. start: function () {
  23. if (!this.listenerAdded) {
  24. this.onPerformanceEntry = this.onPerformanceEntry.bind(this);
  25. this.window.addEventListener("performanceentry", this.onPerformanceEntry, true);
  26. this.listenerAdded = true;
  27. }
  28. },
  29. /**
  30. * Stop tracking the user timings.
  31. */
  32. stop: function () {
  33. if (this.listenerAdded) {
  34. this.window.removeEventListener("performanceentry", this.onPerformanceEntry, true);
  35. this.listenerAdded = false;
  36. }
  37. },
  38. disconnect: function () {
  39. this.destroy();
  40. },
  41. destroy: function () {
  42. this.stop();
  43. Actor.prototype.destroy.call(this);
  44. },
  45. onPerformanceEntry: function (e) {
  46. let emitDetail = {
  47. type: e.entryType,
  48. name: e.name,
  49. origin: e.origin,
  50. epoch: e.epoch
  51. };
  52. events.emit(this, "entry", emitDetail);
  53. }
  54. });
  55. exports.PerformanceEntriesActor = PerformanceEntriesActor;