adb-running-checker.js (2600B)
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 6 /* 7 * Uses host:version service to detect if ADB is running 8 * Modified from adb-file-transfer from original ADB 9 */ 10 11 "use strict"; 12 13 const client = require("resource://devtools/client/shared/remote-debugging/adb/adb-client.js"); 14 const { dumpn } = require("resource://devtools/shared/DevToolsUtils.js"); 15 16 exports.check = async function check() { 17 let socket; 18 let state; 19 let timerID; 20 const TIMEOUT_TIME = 1000; 21 22 dumpn("Asking for host:version"); 23 24 return new Promise(resolve => { 25 // On MacOSX connecting to a port which is not started listening gets 26 // stuck (bug 1481963), to avoid the stuck, we do forcibly fail the 27 // connection after |TIMEOUT_TIME| elapsed. 28 timerID = setTimeout(() => { 29 socket.close(); 30 resolve(false); 31 }, TIMEOUT_TIME); 32 33 function finish(returnValue) { 34 clearTimeout(timerID); 35 resolve(returnValue); 36 } 37 38 const runFSM = function runFSM(packetData) { 39 dumpn("runFSM " + state); 40 switch (state) { 41 case "start": { 42 const req = client.createRequest("host:version"); 43 socket.send(req); 44 state = "wait-version"; 45 break; 46 } 47 case "wait-version": { 48 // TODO: Actually check the version number to make sure the daemon 49 // supports the commands we want to use 50 const { length, data } = client.unpackPacket(packetData); 51 dumpn("length: ", length, "data: ", data); 52 socket.close(); 53 const version = parseInt(data, 16); 54 if (version >= 31) { 55 finish(true); 56 } else { 57 dumpn("killing existing adb as we need version >= 31"); 58 finish(false); 59 } 60 break; 61 } 62 default: 63 dumpn("Unexpected State: " + state); 64 finish(false); 65 } 66 }; 67 68 const setupSocket = function () { 69 socket.s.onerror = function () { 70 dumpn("running checker onerror"); 71 finish(false); 72 }; 73 74 socket.s.onopen = function () { 75 dumpn("running checker onopen"); 76 state = "start"; 77 runFSM(); 78 }; 79 80 socket.s.onclose = function () { 81 dumpn("running checker onclose"); 82 }; 83 84 socket.s.ondata = function (event) { 85 dumpn("running checker ondata"); 86 runFSM(event.data); 87 }; 88 }; 89 90 socket = client.connect(); 91 setupSocket(); 92 }); 93 };