profiler.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. "use strict";
  5. const { Cu } = require("chrome");
  6. const {
  7. Front,
  8. FrontClassWithSpec,
  9. custom
  10. } = require("devtools/shared/protocol");
  11. const { profilerSpec } = require("devtools/shared/specs/profiler");
  12. loader.lazyRequireGetter(this, "events", "sdk/event/core");
  13. loader.lazyRequireGetter(this, "extend", "sdk/util/object", true);
  14. /**
  15. * This can be used on older Profiler implementations, but the methods cannot
  16. * be changed -- you must introduce a new method, and detect the server.
  17. */
  18. exports.ProfilerFront = FrontClassWithSpec(profilerSpec, {
  19. initialize: function (client, form) {
  20. Front.prototype.initialize.call(this, client, form);
  21. this.actorID = form.profilerActor;
  22. this.manage(this);
  23. this._onProfilerEvent = this._onProfilerEvent.bind(this);
  24. events.on(this, "*", this._onProfilerEvent);
  25. },
  26. destroy: function () {
  27. events.off(this, "*", this._onProfilerEvent);
  28. Front.prototype.destroy.call(this);
  29. },
  30. /**
  31. * If using the protocol.js Fronts, then make stringify default,
  32. * since the read/write mechanisms will expose it as an object anyway, but
  33. * this lets other consumers who connect directly (xpcshell tests, Gecko Profiler) to
  34. * have unchanged behaviour.
  35. */
  36. getProfile: custom(function (options) {
  37. return this._getProfile(extend({ stringify: true }, options));
  38. }, {
  39. impl: "_getProfile"
  40. }),
  41. /**
  42. * Also emit an old `eventNotification` for older consumers of the profiler.
  43. */
  44. _onProfilerEvent: function (eventName, data) {
  45. // If this event already passed through once, don't repropagate
  46. if (data.relayed) {
  47. return;
  48. }
  49. data.relayed = true;
  50. if (eventName === "eventNotification") {
  51. // If this is `eventNotification`, this is coming from an older Gecko (<Fx42)
  52. // that doesn't use protocol.js style events. Massage it to emit a protocol.js
  53. // style event as well.
  54. events.emit(this, data.topic, data);
  55. } else {
  56. // Otherwise if a modern protocol.js event, emit it also as `eventNotification`
  57. // for compatibility reasons on the client (like for any add-ons/Gecko Profiler
  58. // using this event) and log a deprecation message if there is a listener.
  59. this.conn.emit("eventNotification", {
  60. subject: data.subject,
  61. topic: data.topic,
  62. data: data.data,
  63. details: data.details
  64. });
  65. if (this.conn._getListeners("eventNotification").length) {
  66. Cu.reportError(`
  67. ProfilerActor's "eventNotification" on the DebuggerClient has been deprecated.
  68. Use the ProfilerFront found in "devtools/server/actors/profiler".`);
  69. }
  70. }
  71. },
  72. });