panel.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* -*- Mode: javascript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  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 { Task } = require("devtools/shared/task");
  7. loader.lazyRequireGetter(this, "promise");
  8. loader.lazyRequireGetter(this, "EventEmitter",
  9. "devtools/shared/event-emitter");
  10. function PerformancePanel(iframeWindow, toolbox) {
  11. this.panelWin = iframeWindow;
  12. this.toolbox = toolbox;
  13. EventEmitter.decorate(this);
  14. }
  15. exports.PerformancePanel = PerformancePanel;
  16. PerformancePanel.prototype = {
  17. /**
  18. * Open is effectively an asynchronous constructor.
  19. *
  20. * @return object
  21. * A promise that is resolved when the Performance tool
  22. * completes opening.
  23. */
  24. open: Task.async(function* () {
  25. if (this._opening) {
  26. return this._opening;
  27. }
  28. let deferred = promise.defer();
  29. this._opening = deferred.promise;
  30. this.panelWin.gToolbox = this.toolbox;
  31. this.panelWin.gTarget = this.target;
  32. this._checkRecordingStatus = this._checkRecordingStatus.bind(this);
  33. // Actor is already created in the toolbox; reuse
  34. // the same front, and the toolbox will also initialize the front,
  35. // but redo it here so we can hook into the same event to prevent race conditions
  36. // in the case of the front still being in the process of opening.
  37. let front = yield this.panelWin.gToolbox.initPerformance();
  38. // This should only happen if this is completely unsupported (when profiler
  39. // does not exist), and in that case, the tool shouldn't be available,
  40. // so let's ensure this assertion.
  41. if (!front) {
  42. console.error("No PerformanceFront found in toolbox.");
  43. }
  44. this.panelWin.gFront = front;
  45. let { PerformanceController, EVENTS } = this.panelWin;
  46. PerformanceController.on(EVENTS.RECORDING_ADDED, this._checkRecordingStatus);
  47. PerformanceController.on(EVENTS.RECORDING_STATE_CHANGE, this._checkRecordingStatus);
  48. yield this.panelWin.startupPerformance();
  49. // Fire this once incase we have an in-progress recording (console profile)
  50. // that caused this start up, and no state change yet, so we can highlight the
  51. // tab if we need.
  52. this._checkRecordingStatus();
  53. this.isReady = true;
  54. this.emit("ready");
  55. deferred.resolve(this);
  56. return this._opening;
  57. }),
  58. // DevToolPanel API
  59. get target() {
  60. return this.toolbox.target;
  61. },
  62. destroy: Task.async(function* () {
  63. // Make sure this panel is not already destroyed.
  64. if (this._destroyed) {
  65. return;
  66. }
  67. let { PerformanceController, EVENTS } = this.panelWin;
  68. PerformanceController.off(EVENTS.RECORDING_ADDED, this._checkRecordingStatus);
  69. PerformanceController.off(EVENTS.RECORDING_STATE_CHANGE, this._checkRecordingStatus);
  70. yield this.panelWin.shutdownPerformance();
  71. this.emit("destroyed");
  72. this._destroyed = true;
  73. }),
  74. _checkRecordingStatus: function () {
  75. if (this.panelWin.PerformanceController.isRecording()) {
  76. this.toolbox.highlightTool("performance");
  77. } else {
  78. this.toolbox.unhighlightTool("performance");
  79. }
  80. }
  81. };