summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/thread-configuration/thread-configuration-command.js
blob: 0db1c2a2852146bafb9272e00b8f1b25b26890f9 (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
/* 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";

/**
 * The ThreadConfigurationCommand should be used to maintain thread settings
 * sent from the client for the thread actor.
 *
 * See the ThreadConfigurationActor for a list of supported configuration options.
 */
class ThreadConfigurationCommand {
  constructor({ commands, watcherFront }) {
    this._commands = commands;
    this._watcherFront = watcherFront;
  }

  /**
   * Return a promise that resolves to the related thread configuration actor's front.
   *
   * @return {Promise<ThreadConfigurationFront>}
   */
  async getThreadConfigurationFront() {
    const front = await this._watcherFront.getThreadConfigurationActor();
    return front;
  }

  async updateConfiguration(configuration) {
    if (this._commands.targetCommand.hasTargetWatcherSupport()) {
      // Remove thread options that are not currently supported by
      // the thread configuration actor.
      const filteredConfiguration = Object.fromEntries(
        Object.entries(configuration).filter(
          ([key, value]) => !["breakpoints", "eventBreakpoints"].includes(key)
        )
      );

      const threadConfigurationFront = await this.getThreadConfigurationFront();
      const updatedConfiguration =
        await threadConfigurationFront.updateConfiguration(
          filteredConfiguration
        );
      this._configuration = updatedConfiguration;
    }

    let threadFronts = await this._commands.targetCommand.getAllFronts(
      this._commands.targetCommand.ALL_TYPES,
      "thread"
    );

    // Lets always call reconfigure for all the target types that do not
    // have target watcher support yet. e.g In the browser, even
    // though `hasTargetWatcherSupport()` is true, only
    // FRAME and CONTENT PROCESS targets use watcher actors,
    // WORKER targets are supported via the legacy listerners.
    threadFronts = threadFronts.filter(
      threadFront =>
        !this._commands.targetCommand.hasTargetWatcherSupport(
          threadFront.targetFront.targetType
        )
    );

    // Ignore threads that fail to be configured.
    // Some workers may be destroying and `reconfigure` would be rejected.
    await Promise.allSettled(
      threadFronts.map(threadFront => threadFront.reconfigure(configuration))
    );
  }
}

module.exports = ThreadConfigurationCommand;