forked from mozilla/adbhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
71 lines (63 loc) · 1.98 KB
/
main.js
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
let {Cu} = require("chrome");
Cu.import("resource://gre/modules/Services.jsm");
let adb = require("./adb");
const events = require("sdk/event/core");
const {devtools} = Cu.import("resource://gre/modules/devtools/Loader.jsm", {});
const devtoolsRequire = devtools.require;
const {ConnectionManager} = devtoolsRequire("devtools/client/connection-manager");
let {Devices} = Cu.import("resource://gre/modules/devtools/Devices.jsm");
let promise;
try {
promise = Cu.import("resource://gre/modules/commonjs/promise/core.js").Promise;
} catch (e) {
promise = Cu.import("resource://gre/modules/commonjs/sdk/core/promise.js").Promise;
}
Devices.helperAddonInstalled = true;
exports.shutdown = function() {
Devices.helperAddonInstalled = false;
adb.stop(true);
}
adb.start().then(function () {
adb.trackDevices();
});
function onDeviceConnected(device) {
console.log("CONNECTED: " + device);
Devices.register(device, {
connect: function (remotePort) {
let port = ConnectionManager.getFreeTCPPort();
let local = "tcp:" + port;
let remote = "localfilesystem:/data/local/debugger-socket";
if (remotePort) {
remote = "tcp:" + remotePort;
}
return adb.forwardPort(local, remote)
.then(() => port);
},
shell: function(cmd) {
return adb.shell(cmd);
},
forwardPort: function(local, remote) {
return adb.forwardPort(local, remote);
},
isRoot: function() {
let deferred = promise.defer();
adb.shell("id").then(stdout => {
let uid = stdout.match(/uid=(\d+)/)[1];
if (uid == "0") {
deferred.resolve(true);
} else {
deferred.resolve(false);
}
}, deferred.reject);
return deferred.promise;
},
summonRoot: function() {
return adb.root();
},
});
}
events.on(adb, "device-connected", onDeviceConnected);
events.on(adb, "device-disconnected", function (device) {
console.log("DISCONNECTED: " + device);
Devices.unregister(device);
});