summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance/panel.js
blob: 28fef793f027b6acee316940f0d06334790946be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

loader.lazyRequireGetter(this, "EventEmitter", "devtools/shared/event-emitter");

function PerformancePanel(iframeWindow, toolbox) {
  this.panelWin = iframeWindow;
  this.toolbox = toolbox;
  this._targetAvailablePromise = Promise.resolve();

  this._onTargetAvailable = this._onTargetAvailable.bind(this);

  EventEmitter.decorate(this);
}

exports.PerformancePanel = PerformancePanel;

PerformancePanel.prototype = {
  /**
   * Open is effectively an asynchronous constructor.
   *
   * @return object
   *         A promise that is resolved when the Performance tool
   *         completes opening.
   */
  async open() {
    if (this._opening) {
      return this._opening;
    }

    this._checkRecordingStatus = this._checkRecordingStatus.bind(this);

    const { PerformanceController, EVENTS } = this.panelWin;
    PerformanceController.on(
      EVENTS.RECORDING_ADDED,
      this._checkRecordingStatus
    );
    PerformanceController.on(
      EVENTS.RECORDING_STATE_CHANGE,
      this._checkRecordingStatus
    );

    // In case that the target is switched across process, the corresponding front also
    // will be changed. In order to detect that, watch the change.
    // Also, we wait for `watchTargets` to end. Indeed the function `_onTargetAvailable
    // will be called synchronously with current target as a parameter by
    // the `watchTargets` function.
    // So this `await` waits for initialization with current target, happening
    // in `_onTargetAvailable`.
    await this.toolbox.targetList.watchTargets(
      [this.toolbox.targetList.TYPES.FRAME],
      this._onTargetAvailable
    );

    // Fire this once incase we have an in-progress recording (console profile)
    // that caused this start up, and no state change yet, so we can highlight the
    // tab if we need.
    this._checkRecordingStatus();

    this.isReady = true;
    this.emit("ready");

    this._opening = new Promise(resolve => {
      resolve(this);
    });
    return this._opening;
  },

  // DevToolPanel API

  get target() {
    return this.toolbox.target;
  },

  async destroy() {
    // Make sure this panel is not already destroyed.
    if (this._destroyed) {
      return;
    }

    const { PerformanceController, PerformanceView, EVENTS } = this.panelWin;
    PerformanceController.off(
      EVENTS.RECORDING_ADDED,
      this._checkRecordingStatus
    );
    PerformanceController.off(
      EVENTS.RECORDING_STATE_CHANGE,
      this._checkRecordingStatus
    );

    this.toolbox.targetList.unwatchTargets(
      [this.toolbox.targetList.TYPES.FRAME],
      this._onTargetAvailable
    );
    await PerformanceController.destroy();
    await PerformanceView.destroy();
    PerformanceController.disableFrontEventListeners();

    this.emit("destroyed");
    this._destroyed = true;
  },

  _checkRecordingStatus: function() {
    if (this.panelWin.PerformanceController.isRecording()) {
      this.toolbox.highlightTool("performance");
    } else {
      this.toolbox.unhighlightTool("performance");
    }
  },

  /**
   * This function executes actual logic for the target-switching.
   *
   * @param {TargetFront} - targetFront
   *        As we are watching only FRAME type for this panel,
   *        the target should be a instance of BrowsingContextTarget.
   */
  async _handleTargetAvailable({ targetFront }) {
    if (targetFront.isTopLevel) {
      const { PerformanceController, PerformanceView } = this.panelWin;
      const performanceFront = await targetFront.getFront("performance");

      if (!this._isPanelInitialized) {
        await PerformanceController.initialize(
          this.toolbox,
          targetFront,
          performanceFront
        );
        await PerformanceView.initialize();
        PerformanceController.enableFrontEventListeners();
        this._isPanelInitialized = true;
      } else {
        const isRecording = PerformanceController.isRecording();
        if (isRecording) {
          await PerformanceController.stopRecording();
        }

        PerformanceView.resetBufferStatus();
        PerformanceController.updateFronts(targetFront, performanceFront);

        if (isRecording) {
          await PerformanceController.startRecording();
        }
      }
    }
  },

  /**
   * This function is called for every target is available.
   */
  _onTargetAvailable(parameters) {
    // As this function is called asynchronous, while previous processing, this might be
    // called. Thus, we wait until finishing previous one before starting next.
    this._targetAvailablePromise = this._targetAvailablePromise.then(() =>
      this._handleTargetAvailable(parameters)
    );

    return this._targetAvailablePromise;
  },
};