run-command.js (1840B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 // Wrapper around the ADB utility. 6 7 "use strict"; 8 9 const { dumpn } = require("resource://devtools/shared/DevToolsUtils.js"); 10 const { setTimeout } = ChromeUtils.importESModule( 11 "resource://gre/modules/Timer.sys.mjs" 12 ); 13 const { 14 adbProcess, 15 } = require("resource://devtools/client/shared/remote-debugging/adb/adb-process.js"); 16 const client = require("resource://devtools/client/shared/remote-debugging/adb/adb-client.js"); 17 18 const OKAY = 0x59414b4f; 19 20 // Asynchronously runs an adb command. 21 // @param command The command as documented in 22 // http://androidxref.com/4.0.4/xref/system/core/adb/SERVICES.TXT 23 const runCommand = function (command) { 24 dumpn("runCommand " + command); 25 return new Promise((resolve, reject) => { 26 if (!adbProcess.ready) { 27 setTimeout(function () { 28 reject("ADB_NOT_READY"); 29 }); 30 return; 31 } 32 33 const socket = client.connect(); 34 35 socket.s.onopen = function () { 36 dumpn("runCommand onopen"); 37 const req = client.createRequest(command); 38 socket.send(req); 39 }; 40 41 socket.s.onerror = function () { 42 dumpn("runCommand onerror"); 43 reject("NETWORK_ERROR"); 44 }; 45 46 socket.s.onclose = function () { 47 dumpn("runCommand onclose"); 48 }; 49 50 socket.s.ondata = function (event) { 51 dumpn("runCommand ondata"); 52 const data = event.data; 53 54 const packet = client.unpackPacket(data, false); 55 if (!client.checkResponse(data, OKAY)) { 56 socket.close(); 57 dumpn("Error: " + packet.data); 58 reject("PROTOCOL_ERROR"); 59 return; 60 } 61 62 resolve(packet.data); 63 }; 64 }); 65 }; 66 exports.runCommand = runCommand;