summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/remote-debugging/remote-client-manager.js
blob: 45e7a1be4a04c6adc4ff2ed26db0c1574d255de2 (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
/* 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";

const {
  CONNECTION_TYPES,
} = require("resource://devtools/client/shared/remote-debugging/constants.js");

/**
 * This class is designed to be a singleton shared by all DevTools to get access to
 * existing clients created for remote debugging.
 */
class RemoteClientManager {
  constructor() {
    this._clients = new Map();
    this._runtimeInfoMap = new Map();
    this._onClientClosed = this._onClientClosed.bind(this);
  }

  /**
   * Store a remote client that is already connected.
   *
   * @param {String} id
   *        Remote runtime id (see devtools/client/aboutdebugging/src/types).
   * @param {String} type
   *        Remote runtime type (see devtools/client/aboutdebugging/src/types).
   * @param {DevToolsClient} client
   * @param {Object} runtimeInfo
   *        See runtimeInfo type from client/aboutdebugging/src/types/runtime.js
   */
  setClient(id, type, client, runtimeInfo) {
    const key = this._getKey(id, type);
    this._clients.set(key, client);
    if (runtimeInfo) {
      this._runtimeInfoMap.set(key, runtimeInfo);
    }
    client.once("closed", this._onClientClosed);
  }

  // See JSDoc for id, type from setClient.
  hasClient(id, type) {
    return this._clients.has(this._getKey(id, type));
  }

  // See JSDoc for id, type from setClient.
  getClient(id, type) {
    return this._clients.get(this._getKey(id, type));
  }

  // See JSDoc for id, type from setClient.
  removeClient(id, type) {
    const key = this._getKey(id, type);
    this._removeClientByKey(key);
  }

  removeAllClients() {
    const keys = [...this._clients.keys()];
    for (const key of keys) {
      this._removeClientByKey(key);
    }
  }

  /**
   * Retrieve a unique, url-safe key based on a runtime id and type.
   */
  getRemoteId(id, type) {
    return encodeURIComponent(this._getKey(id, type));
  }

  /**
   * Retrieve a managed client for a remote id. The remote id should have been generated
   * using getRemoteId.
   */
  getClientByRemoteId(remoteId) {
    const key = this._getKeyByRemoteId(remoteId);
    return this._clients.get(key);
  }

  /**
   * Retrieve the runtime info for a remote id. To display metadata about a runtime, such
   * as name, device name, version... this runtimeInfo should be used rather than calling
   * APIs on the client.
   */
  getRuntimeInfoByRemoteId(remoteId) {
    const key = this._getKeyByRemoteId(remoteId);
    return this._runtimeInfoMap.get(key);
  }

  /**
   * Retrieve a managed client for a remote id. The remote id should have been generated
   * using getRemoteId.
   */
  getConnectionTypeByRemoteId(remoteId) {
    const key = this._getKeyByRemoteId(remoteId);
    for (const type of Object.values(CONNECTION_TYPES)) {
      if (key.endsWith(type)) {
        return type;
      }
    }
    return CONNECTION_TYPES.UNKNOWN;
  }

  _getKey(id, type) {
    return id + "-" + type;
  }

  _getKeyByRemoteId(remoteId) {
    if (!remoteId) {
      // If no remote id was provided, return the key corresponding to the local
      // this-firefox runtime.
      const { THIS_FIREFOX } = CONNECTION_TYPES;
      return this._getKey(THIS_FIREFOX, THIS_FIREFOX);
    }

    return decodeURIComponent(remoteId);
  }

  _removeClientByKey(key) {
    const client = this._clients.get(key);
    if (client) {
      client.off("closed", this._onClientClosed);
      this._clients.delete(key);
      this._runtimeInfoMap.delete(key);
    }
  }

  /**
   * Cleanup all closed clients when a "closed" notification is received from a client.
   */
  _onClientClosed() {
    const closedClientKeys = [...this._clients.keys()].filter(key => {
      return this._clients.get(key)._transportClosed;
    });

    for (const key of closedClientKeys) {
      this._removeClientByKey(key);
    }
  }
}

// Expose a singleton of RemoteClientManager.
module.exports = {
  remoteClientManager: new RemoteClientManager(),
};