fune/devtools/shared/adb/adb-socket.js
Victor Porof b8157dfaaf Bug 1561435 - Format remaining devtools/, a=automatic-formatting, CLOSED TREE
# ignore-this-changeset

Differential Revision: https://phabricator.services.mozilla.com/D35894

--HG--
extra : source : 4722b924e08478f5337ab509718bd66906bf472f
extra : amend_source : a5baa1aab21639fdba44537e3a10b179b0073cb4
2019-07-05 11:29:32 +02:00

73 lines
1.9 KiB
JavaScript

/* 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 { Cu } = require("chrome");
const { dumpn } = require("devtools/shared/DevToolsUtils");
function createTCPSocket(location, port, options) {
const { TCPSocket } = Cu.getGlobalForObject(
Cu.import("resource://gre/modules/Services.jsm", {})
);
return new TCPSocket(location, port, options);
}
// Creates a socket connected to the adb instance.
// This instantiation is sync, and returns before we know if opening the
// connection succeeds. Callers must attach handlers to the s field.
class AdbSocket {
constructor() {
this.s = createTCPSocket("127.0.0.1", 5037, { binaryType: "arraybuffer" });
}
/**
* Dump the first few bytes of the given array to the console.
*
* @param {TypedArray} inputArray
* the array to dump
*/
_hexdump(inputArray) {
const decoder = new TextDecoder("windows-1252");
const array = new Uint8Array(inputArray.buffer);
const s = decoder.decode(array);
const len = array.length;
let dbg = "len=" + len + " ";
const l = len > 20 ? 20 : len;
for (let i = 0; i < l; i++) {
let c = array[i].toString(16);
if (c.length == 1) {
c = "0" + c;
}
dbg += c;
}
dbg += " ";
for (let i = 0; i < l; i++) {
const c = array[i];
if (c < 32 || c > 127) {
dbg += ".";
} else {
dbg += s[i];
}
}
dumpn(dbg);
}
// debugging version of tcpsocket.send()
send(array) {
this._hexdump(array);
this.s.send(array.buffer, array.byteOffset, array.byteLength);
}
close() {
if (this.s.readyState === "open" || this.s.readyState === "connecting") {
this.s.close();
}
}
}
exports.AdbSocket = AdbSocket;