2014-05-30 05:22:16 +00:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
const {Cu} = require("chrome");
|
|
|
|
const {Devices} = Cu.import("resource://gre/modules/devtools/Devices.jsm");
|
|
|
|
const {Services} = Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
const {Simulator} = Cu.import("resource://gre/modules/devtools/Simulator.jsm");
|
|
|
|
const {ConnectionManager, Connection} = require("devtools/client/connection-manager");
|
|
|
|
const {DebuggerServer} = require("resource://gre/modules/devtools/dbg-server.jsm");
|
2014-06-26 14:41:00 +00:00
|
|
|
const discovery = require("devtools/toolkit/discovery/discovery");
|
2014-07-07 10:07:00 +00:00
|
|
|
const promise = require("promise");
|
2014-05-30 05:22:16 +00:00
|
|
|
|
2014-08-05 14:52:06 +00:00
|
|
|
const Strings = Services.strings.createBundle("chrome://browser/locale/devtools/webide.properties");
|
2014-05-30 05:22:16 +00:00
|
|
|
|
|
|
|
function USBRuntime(id) {
|
|
|
|
this.id = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
USBRuntime.prototype = {
|
|
|
|
connect: function(connection) {
|
|
|
|
let device = Devices.getByName(this.id);
|
|
|
|
if (!device) {
|
2014-06-26 14:41:00 +00:00
|
|
|
return promise.reject("Can't find device: " + this.getName());
|
2014-05-30 05:22:16 +00:00
|
|
|
}
|
|
|
|
return device.connect().then((port) => {
|
|
|
|
connection.host = "localhost";
|
|
|
|
connection.port = port;
|
|
|
|
connection.connect();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getID: function() {
|
|
|
|
return this.id;
|
|
|
|
},
|
|
|
|
getName: function() {
|
2014-07-22 07:08:41 +00:00
|
|
|
return this._productModel || this.id;
|
|
|
|
},
|
|
|
|
updateNameFromADB: function() {
|
|
|
|
if (this._productModel) {
|
|
|
|
return promise.resolve();
|
|
|
|
}
|
|
|
|
let device = Devices.getByName(this.id);
|
|
|
|
let deferred = promise.defer();
|
|
|
|
if (device && device.shell) {
|
|
|
|
device.shell("getprop ro.product.model").then(stdout => {
|
|
|
|
this._productModel = stdout;
|
|
|
|
deferred.resolve();
|
|
|
|
}, () => {});
|
|
|
|
} else {
|
|
|
|
this._productModel = null;
|
|
|
|
deferred.reject();
|
|
|
|
}
|
|
|
|
return deferred.promise;
|
2014-05-30 05:22:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-06-26 14:41:00 +00:00
|
|
|
function WiFiRuntime(deviceName) {
|
|
|
|
this.deviceName = deviceName;
|
|
|
|
}
|
|
|
|
|
|
|
|
WiFiRuntime.prototype = {
|
|
|
|
connect: function(connection) {
|
|
|
|
let service = discovery.getRemoteService("devtools", this.deviceName);
|
|
|
|
if (!service) {
|
|
|
|
return promise.reject("Can't find device: " + this.getName());
|
|
|
|
}
|
|
|
|
connection.host = service.host;
|
|
|
|
connection.port = service.port;
|
|
|
|
connection.connect();
|
|
|
|
return promise.resolve();
|
|
|
|
},
|
|
|
|
getID: function() {
|
|
|
|
return this.deviceName;
|
|
|
|
},
|
|
|
|
getName: function() {
|
|
|
|
return this.deviceName;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2014-05-30 05:22:16 +00:00
|
|
|
function SimulatorRuntime(version) {
|
|
|
|
this.version = version;
|
|
|
|
}
|
|
|
|
|
|
|
|
SimulatorRuntime.prototype = {
|
|
|
|
connect: function(connection) {
|
|
|
|
let port = ConnectionManager.getFreeTCPPort();
|
|
|
|
let simulator = Simulator.getByVersion(this.version);
|
|
|
|
if (!simulator || !simulator.launch) {
|
2014-06-26 14:41:00 +00:00
|
|
|
return promise.reject("Can't find simulator: " + this.getName());
|
2014-05-30 05:22:16 +00:00
|
|
|
}
|
|
|
|
return simulator.launch({port: port}).then(() => {
|
2014-09-02 05:48:00 +00:00
|
|
|
connection.host = "localhost";
|
2014-05-30 05:22:16 +00:00
|
|
|
connection.port = port;
|
|
|
|
connection.keepConnecting = true;
|
2014-08-01 12:47:37 +00:00
|
|
|
connection.once(Connection.Events.DISCONNECTED, simulator.close);
|
2014-05-30 05:22:16 +00:00
|
|
|
connection.connect();
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getID: function() {
|
|
|
|
return this.version;
|
|
|
|
},
|
|
|
|
getName: function() {
|
2014-06-18 21:19:56 +00:00
|
|
|
return Simulator.getByVersion(this.version).appinfo.label;
|
2014-05-30 05:22:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
let gLocalRuntime = {
|
|
|
|
connect: function(connection) {
|
|
|
|
if (!DebuggerServer.initialized) {
|
|
|
|
DebuggerServer.init();
|
|
|
|
DebuggerServer.addBrowserActors();
|
|
|
|
}
|
|
|
|
connection.host = null; // Force Pipe transport
|
2014-09-02 05:48:00 +00:00
|
|
|
connection.port = null;
|
2014-05-30 05:22:16 +00:00
|
|
|
connection.connect();
|
|
|
|
return promise.resolve();
|
|
|
|
},
|
|
|
|
getName: function() {
|
|
|
|
return Strings.GetStringFromName("local_runtime");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
let gRemoteRuntime = {
|
|
|
|
connect: function(connection) {
|
|
|
|
let win = Services.wm.getMostRecentWindow("devtools:webide");
|
|
|
|
if (!win) {
|
|
|
|
return promise.reject();
|
|
|
|
}
|
|
|
|
let ret = {value: connection.host + ":" + connection.port};
|
2014-08-22 18:52:06 +00:00
|
|
|
let title = Strings.GetStringFromName("remote_runtime_promptTitle");
|
|
|
|
let message = Strings.GetStringFromName("remote_runtime_promptMessage");
|
|
|
|
let ok = Services.prompt.prompt(win, title, message, ret, null, {});
|
2014-05-30 05:22:16 +00:00
|
|
|
let [host,port] = ret.value.split(":");
|
2014-08-22 18:52:06 +00:00
|
|
|
if (!ok) {
|
|
|
|
return promise.reject({canceled: true});
|
|
|
|
}
|
2014-05-30 05:22:16 +00:00
|
|
|
if (!host || !port) {
|
|
|
|
return promise.reject();
|
|
|
|
}
|
|
|
|
connection.host = host;
|
|
|
|
connection.port = port;
|
|
|
|
connection.connect();
|
|
|
|
return promise.resolve();
|
|
|
|
},
|
|
|
|
getName: function() {
|
|
|
|
return Strings.GetStringFromName("remote_runtime");
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.USBRuntime = USBRuntime;
|
2014-06-26 14:41:00 +00:00
|
|
|
exports.WiFiRuntime = WiFiRuntime;
|
2014-05-30 05:22:16 +00:00
|
|
|
exports.SimulatorRuntime = SimulatorRuntime;
|
|
|
|
exports.gRemoteRuntime = gRemoteRuntime;
|
|
|
|
exports.gLocalRuntime = gLocalRuntime;
|