summaryrefslogtreecommitdiffstats
path: root/devtools/client/aboutdebugging/src/modules/usb-runtimes.js
blob: 887dc6788e25bab3c763b5341df0d8c527170310 (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
/* 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,
  "adb",
  "resource://devtools/client/shared/remote-debugging/adb/adb.js",
  true
);

/**
 * Used to represent a regular runtime returned by ADB.
 */
class UsbRuntime {
  constructor(adbRuntime) {
    this.id = adbRuntime.id;
    this.deviceId = adbRuntime.deviceId;
    this.deviceName = adbRuntime.deviceName;
    this.shortName = adbRuntime.shortName;
    this.socketPath = adbRuntime.socketPath;
    this.isFenix = adbRuntime.isFenix;
    this.isUnavailable = false;
    this.isUnplugged = false;
    this.versionName = adbRuntime.versionName;
  }
}

/**
 * Used when a device was detected, meaning USB debugging is enabled on the device, but no
 * runtime/browser is available for connection.
 */
class UnavailableUsbRuntime {
  constructor(adbDevice) {
    this.id = adbDevice.id + "|unavailable";
    this.deviceId = adbDevice.id;
    this.deviceName = adbDevice.name;
    this.shortName = "Unavailable runtime";
    this.socketPath = null;
    this.isFenix = false;
    this.isUnavailable = true;
    this.isUnplugged = false;
    this.versionName = null;
  }
}

/**
 * Used to represent USB devices that were previously connected but are now missing
 * (presumably after being unplugged/disconnected from the computer).
 */
class UnpluggedUsbRuntime {
  constructor(deviceId, deviceName) {
    this.id = deviceId + "|unplugged";
    this.deviceId = deviceId;
    this.deviceName = deviceName;
    this.shortName = "Unplugged runtime";
    this.socketPath = null;
    this.isFenix = false;
    this.isUnavailable = true;
    this.isUnplugged = true;
    this.versionName = null;
  }
}

/**
 * Map used to keep track of discovered usb devices. Will be used to create the unplugged
 * usb runtimes.
 */
const devices = new Map();

/**
 * This module provides a collection of helper methods to detect USB runtimes whom Firefox
 * is running on.
 */
function addUSBRuntimesObserver(listener) {
  adb.registerListener(listener);
}
exports.addUSBRuntimesObserver = addUSBRuntimesObserver;

async function getUSBRuntimes() {
  // Get the available runtimes
  const runtimes = adb.getRuntimes().map(r => new UsbRuntime(r));

  // Get devices found by ADB, but without any available runtime.
  const runtimeDevices = runtimes.map(r => r.deviceId);
  const unavailableRuntimes = adb
    .getDevices()
    .filter(d => !runtimeDevices.includes(d.id))
    .map(d => new UnavailableUsbRuntime(d));

  // Add all devices to the map detected devices.
  const allRuntimes = runtimes.concat(unavailableRuntimes);
  for (const runtime of allRuntimes) {
    devices.set(runtime.deviceId, runtime.deviceName);
  }

  // Get devices previously found by ADB but no longer available.
  const currentDevices = allRuntimes.map(r => r.deviceId);
  const detectedDevices = [...devices.keys()];
  const unpluggedDevices = detectedDevices.filter(
    id => !currentDevices.includes(id)
  );
  const unpluggedRuntimes = unpluggedDevices.map(deviceId => {
    const deviceName = devices.get(deviceId);
    return new UnpluggedUsbRuntime(deviceId, deviceName);
  });

  return allRuntimes.concat(unpluggedRuntimes);
}
exports.getUSBRuntimes = getUSBRuntimes;

function removeUSBRuntimesObserver(listener) {
  adb.unregisterListener(listener);
}
exports.removeUSBRuntimesObserver = removeUSBRuntimesObserver;

function refreshUSBRuntimes() {
  return adb.updateRuntimes();
}
exports.refreshUSBRuntimes = refreshUSBRuntimes;