summaryrefslogtreecommitdiffstats
path: root/devtools/client/performance-new/components/ProfilerEventHandling.js
blob: e3b07ff2bcc0f6624fe805577af73c295d69f762 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* 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

/**
 * @template P
 * @typedef {import("react-redux").ResolveThunks<P>} ResolveThunks<P>
 */

/**
 * @typedef {Object} StateProps
 * @property {PerfFront} perfFront
 * @property {RecordingState} recordingState
 * @property {boolean?} isSupportedPlatform
 * @property {PageContext} pageContext
 * @property {string | null} promptEnvRestart
 */

/**
 * @typedef {Object} ThunkDispatchProps
 * @property {typeof actions.changeRecordingState} changeRecordingState
 * @property {typeof actions.reportProfilerReady} reportProfilerReady
 */

/**
 * @typedef {ResolveThunks<ThunkDispatchProps>} DispatchProps
 * @typedef {StateProps & DispatchProps} Props
 * @typedef {import("../@types/perf").PerfFront} PerfFront
 * @typedef {import("../@types/perf").RecordingState} RecordingState
 * @typedef {import("../@types/perf").State} StoreState
 * @typedef {import("../@types/perf").PageContext} PageContext
 */

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

"use strict";

const { PureComponent } = require("devtools/client/shared/vendor/react");
const { connect } = require("devtools/client/shared/vendor/react-redux");
const actions = require("devtools/client/performance-new/store/actions");
const selectors = require("devtools/client/performance-new/store/selectors");

/**
 * This component state changes for the performance recording. e.g. If the profiler
 * suddenly becomes unavailable, it needs to react to those changes, and update the
 * recordingState in the store.
 *
 * @extends {React.PureComponent<Props>}
 */
class ProfilerEventHandling extends PureComponent {
  /** @param {Props} props */
  constructor(props) {
    super(props);
    this.handleProfilerStarting = this.handleProfilerStarting.bind(this);
    this.handleProfilerStopping = this.handleProfilerStopping.bind(this);
    this.handlePrivateBrowsingStarting = this.handlePrivateBrowsingStarting.bind(
      this
    );
    this.handlePrivateBrowsingEnding = this.handlePrivateBrowsingEnding.bind(
      this
    );
  }

  componentDidMount() {
    const { perfFront, reportProfilerReady } = this.props;

    // Ask for the initial state of the profiler.
    Promise.all([
      perfFront.isActive(),
      perfFront.isSupportedPlatform(),
      perfFront.isLockedForPrivateBrowsing(),
    ]).then(results => {
      const [
        isActive,
        isSupportedPlatform,
        isLockedForPrivateBrowsing,
      ] = results;

      let recordingState = this.props.recordingState;
      // It's theoretically possible we got an event that already let us know about
      // the current state of the profiler.
      if (recordingState === "not-yet-known" && isSupportedPlatform) {
        if (isLockedForPrivateBrowsing) {
          recordingState = "locked-by-private-browsing";
        } else if (isActive) {
          recordingState = "recording";
        } else {
          recordingState = "available-to-record";
        }
      }
      reportProfilerReady(isSupportedPlatform, recordingState);

      // If this component is inside the popup, then report it being ready so that
      // it will show. This defers the initial visibility of the popup until the
      // React components have fully rendered, and thus there is no annoying "blip"
      // to the screen when the page goes from fully blank, to showing the content.
      /** @type {any} */
      const anyWindow = window;
      /** @type {PanelWindow} - Coerce the window into the PanelWindow. */
      const { gReportReady } = anyWindow;
      if (gReportReady) {
        gReportReady();
      }
    });

    // Handle when the profiler changes state. It might be us, it might be someone else.
    this.props.perfFront.on("profiler-started", this.handleProfilerStarting);
    this.props.perfFront.on("profiler-stopped", this.handleProfilerStopping);
    this.props.perfFront.on(
      "profile-locked-by-private-browsing",
      this.handlePrivateBrowsingStarting
    );
    this.props.perfFront.on(
      "profile-unlocked-from-private-browsing",
      this.handlePrivateBrowsingEnding
    );
  }

  componentWillUnmount() {
    switch (this.props.recordingState) {
      case "not-yet-known":
      case "available-to-record":
      case "request-to-stop-profiler":
      case "request-to-get-profile-and-stop-profiler":
      case "locked-by-private-browsing":
        // Do nothing for these states.
        break;

      case "recording":
      case "request-to-start-recording":
        this.props.perfFront.stopProfilerAndDiscardProfile();
        break;

      default:
        throw new Error("Unhandled recording state.");
    }
  }

  handleProfilerStarting() {
    const { changeRecordingState, recordingState } = this.props;
    switch (recordingState) {
      case "not-yet-known":
      // We couldn't have started it yet, so it must have been someone
      // else. (fallthrough)
      case "available-to-record":
      // We aren't recording, someone else started it up. (fallthrough)
      case "request-to-stop-profiler":
      // We requested to stop the profiler, but someone else already started
      // it up. (fallthrough)
      case "request-to-get-profile-and-stop-profiler":
        changeRecordingState("recording");
        break;

      case "request-to-start-recording":
        // Wait for the profiler to tell us that it has started.
        changeRecordingState("recording");
        break;

      case "locked-by-private-browsing":
      case "recording":
        // These state cases don't make sense to happen, and means we have a logical
        // fallacy somewhere.
        throw new Error(
          "The profiler started recording, when it shouldn't have " +
            `been able to. Current state: "${recordingState}"`
        );
      default:
        throw new Error("Unhandled recording state");
    }
  }

  handleProfilerStopping() {
    const { changeRecordingState, recordingState } = this.props;
    switch (recordingState) {
      case "not-yet-known":
      case "request-to-get-profile-and-stop-profiler":
      case "request-to-stop-profiler":
        changeRecordingState("available-to-record");
        break;

      case "request-to-start-recording":
      // Highly unlikely, but someone stopped the recorder, this is fine.
      // Do nothing (fallthrough).
      case "locked-by-private-browsing":
        // The profiler is already locked, so we know about this already.
        break;

      case "recording":
        changeRecordingState("available-to-record", {
          didRecordingUnexpectedlyStopped: true,
        });
        break;

      case "available-to-record":
        throw new Error(
          "The profiler stopped recording, when it shouldn't have been able to."
        );
      default:
        throw new Error("Unhandled recording state");
    }
  }

  handlePrivateBrowsingStarting() {
    const { recordingState, changeRecordingState } = this.props;

    switch (recordingState) {
      case "request-to-get-profile-and-stop-profiler":
      // This one is a tricky case. Go ahead and act like nothing went wrong, maybe
      // it will resolve correctly? (fallthrough)
      case "request-to-stop-profiler":
      case "available-to-record":
      case "not-yet-known":
        changeRecordingState("locked-by-private-browsing");
        break;

      case "request-to-start-recording":
      case "recording":
        changeRecordingState("locked-by-private-browsing", {
          didRecordingUnexpectedlyStopped: false,
        });
        break;

      case "locked-by-private-browsing":
        // Do nothing
        break;

      default:
        throw new Error("Unhandled recording state");
    }
  }

  handlePrivateBrowsingEnding() {
    // No matter the state, go ahead and set this as ready to record. This should
    // be the only logical state to go into.
    this.props.changeRecordingState("available-to-record");
  }

  render() {
    return null;
  }
}

/**
 * @param {StoreState} state
 * @returns {StateProps}
 */
function mapStateToProps(state) {
  return {
    perfFront: selectors.getPerfFront(state),
    recordingState: selectors.getRecordingState(state),
    isSupportedPlatform: selectors.getIsSupportedPlatform(state),
    pageContext: selectors.getPageContext(state),
    promptEnvRestart: selectors.getPromptEnvRestart(state),
  };
}

/** @type {ThunkDispatchProps} */
const mapDispatchToProps = {
  changeRecordingState: actions.changeRecordingState,
  reportProfilerReady: actions.reportProfilerReady,
};

module.exports = connect(
  mapStateToProps,
  mapDispatchToProps
)(ProfilerEventHandling);