summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection.js
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection.js')
-rw-r--r--devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection.js46
1 files changed, 46 insertions, 0 deletions
diff --git a/devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection.js b/devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection.js
new file mode 100644
index 0000000000..dc9103f56b
--- /dev/null
+++ b/devtools/client/shared/remote-debugging/adb/commands/prepare-tcp-connection.js
@@ -0,0 +1,46 @@
+/* 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 { dumpn } = require("resource://devtools/shared/DevToolsUtils.js");
+const {
+ runCommand,
+} = require("resource://devtools/client/shared/remote-debugging/adb/commands/run-command.js");
+
+// sends adb forward deviceId, localPort and devicePort
+const forwardPort = function (deviceId, localPort, devicePort) {
+ dumpn("forwardPort " + localPort + " -- " + devicePort);
+ // Send "host-serial:<serial-number>:<request>",
+ // with <request> set to "forward:<local>;<remote>"
+ // See https://android.googlesource.com/platform/system/core/+/jb-dev/adb/SERVICES.TXT
+ return runCommand(
+ `host-serial:${deviceId}:forward:${localPort};${devicePort}`
+ ).then(function onSuccess(data) {
+ return data;
+ });
+};
+
+const getFreeTCPPort = function () {
+ const serv = Cc["@mozilla.org/network/server-socket;1"].createInstance(
+ Ci.nsIServerSocket
+ );
+ serv.init(-1, true, -1);
+ const port = serv.port;
+ serv.close();
+ return port;
+};
+
+// Prepare TCP connection for provided device id and socket path.
+// The returned value is a port number of localhost for the connection.
+const prepareTCPConnection = async function (deviceId, socketPath) {
+ const port = getFreeTCPPort();
+ const local = `tcp:${port}`;
+ const remote = socketPath.startsWith("@")
+ ? `localabstract:${socketPath.substring(1)}`
+ : `localfilesystem:${socketPath}`;
+ await forwardPort(deviceId, local, remote);
+ return port;
+};
+exports.prepareTCPConnection = prepareTCPConnection;