summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance-new/panel/panel.js
blob: 2377855d3160f04ba21b27550a5bd458a65a6d6a (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
/* 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/. */
// @ts-check
"use strict";

/**
 * This file contains the PerformancePanel, which uses a common API for DevTools to
 * start and load everything. This will call `gInit` from the initializer.js file,
 * which does the important initialization for the panel. This code is more concerned
 * with wiring this panel into the rest of DevTools and fetching the Actor's fronts.
 */

/**
 * @typedef {import("../@types/perf").PanelWindow} PanelWindow
 * @typedef {import("../@types/perf").Toolbox} Toolbox
 * @typedef {import("../@types/perf").Target} Target
 * @typedef {import("../@types/perf").Commands} Commands
 */

class PerformancePanel {
  /**
   * @param {PanelWindow} iframeWindow
   * @param {Toolbox} toolbox
   * @param {Commands} commands
   */
  constructor(iframeWindow, toolbox, commands) {
    this.panelWin = iframeWindow;
    this.toolbox = toolbox;
    this.commands = commands;

    const EventEmitter = require("resource://devtools/shared/event-emitter.js");
    EventEmitter.decorate(this);
  }

  /**
   * This is implemented (and overwritten) by the EventEmitter. Is there a way
   * to use mixins with JSDoc?
   *
   * @param {string} _eventName
   */
  emit(_eventName) {}

  /**
   * Open is effectively an asynchronous constructor.
   * @return {Promise<PerformancePanel>} Resolves when the Perf tool completes
   *     opening.
   */
  open() {
    if (!this._opening) {
      this._opening = this._doOpen();
    }
    return this._opening;
  }

  /**
   * This function is the actual implementation of the open() method.
   * @returns Promise<PerformancePanel>
   */
  async _doOpen() {
    this.panelWin.gToolbox = this.toolbox;
    this.panelWin.gIsPanelDestroyed = false;

    const perfFront = await this.commands.client.mainRoot.getFront("perf");

    // Note: we are not using traits in the panel at the moment but we keep the
    // wiring in case we need it later on.
    const traits = {};

    await this.panelWin.gInit(
      perfFront,
      traits,
      "devtools",
      this._openAboutProfiling
    );
    return this;
  }

  _openAboutProfiling() {
    const {
      openTrustedLink,
    } = require("resource://devtools/client/shared/link.js");
    openTrustedLink("about:profiling", {});
  }

  // DevToolPanel API:

  /**
   * @returns {Target} target
   */
  get target() {
    return this.toolbox.target;
  }

  destroy() {
    // Make sure this panel is not already destroyed.
    if (this._destroyed) {
      return;
    }
    this.panelWin.gDestroy();
    this.emit("destroyed");
    this._destroyed = true;
    this.panelWin.gIsPanelDestroyed = true;
  }
}

exports.PerformancePanel = PerformancePanel;