Commit 4f3a9874 authored by jrw's avatar jrw Committed by Commit bot

Added ability to list and delete hosts with GCD.

BUG=471928

Review URL: https://codereview.chromium.org/1104383002

Cr-Commit-Position: refs/heads/master@{#327219}
parent a3d731e5
......@@ -32,7 +32,8 @@ remoting.gcd.RegistrationTicket;
/**
* TODO: Flesh out with typical fields.
* @typedef {{
* id:string
* id:string,
* name:string
* }}
*/
remoting.gcd.Device;
......@@ -206,12 +207,18 @@ remoting.gcd.Client.prototype.finalizeRegistrationTicket = function(ticketId) {
/**
* Lists devices user has access to.
* TODO: Add link to GCD docs.
* @param {string=} opt_nameSubstring If present, the list of devices
* is filtered by GCD such that every device returned contains
* this string as as a substring of its |name| or |displayName|.
* @return {!Promise<!Array<remoting.gcd.Device>>}
*/
remoting.gcd.Client.prototype.listDevices = function() {
remoting.gcd.Client.prototype.listDevices = function(opt_nameSubstring) {
return new remoting.Xhr({
method: 'GET',
url: this.apiBaseUrl_ + '/devices',
urlParams: {
nameSubstring: opt_nameSubstring || null
},
useIdentity: true,
acceptJson: true
}).start().then(function(response) {
......@@ -241,7 +248,7 @@ remoting.gcd.Client.prototype.deleteDevice = function(deviceId) {
return false;
}
if (response.isError()) {
console.warn('error deleting device');
console.error('error deleting device');
throw remoting.Error.unexpected();
}
return true;
......
......@@ -60,9 +60,18 @@ remoting.HostListApiGcdImpl.prototype.register = function(
/** @override */
remoting.HostListApiGcdImpl.prototype.get = function() {
// TODO(jrw)
this.gcd_.listDevices();
return Promise.resolve([]);
return this.gcd_.listDevices().
then(function(devices) {
var hosts = [];
devices.forEach(function(device) {
try {
hosts.push(remoting.HostListApiGcdImpl.deviceToHost(device));
} catch (/** @type {*} */ error) {
console.warn('Invalid device spec:', error);
}
});
return hosts;
});
};
/** @override */
......@@ -74,8 +83,52 @@ remoting.HostListApiGcdImpl.prototype.put =
/** @override */
remoting.HostListApiGcdImpl.prototype.remove = function(hostId) {
// TODO(jrw)
throw new Error('Not implemented');
var that = this;
return this.gcd_.listDevices(hostId).then(function(devices) {
var gcdId = null;
for (var i = 0; i < devices.length; i++) {
var device = devices[i];
// The "name" field in GCD holds what Chromoting considers to be
// the host ID.
if (device.name == hostId) {
gcdId = device.id;
}
}
if (gcdId == null) {
return false;
} else {
return that.gcd_.deleteDevice(gcdId);
}
});
};
/**
* @param {!Object} device
* @return {!remoting.Host}
*/
remoting.HostListApiGcdImpl.deviceToHost = function(device) {
var statusMap = {
'online': 'ONLINE',
'offline': 'OFFLINE'
};
var hostId = base.getStringAttr(device, 'name');
var host = new remoting.Host(hostId);
host.hostName = base.getStringAttr(device, 'displayName');
host.status = base.getStringAttr(
statusMap, base.getStringAttr(device, 'connectionStatus'));
var state = base.getObjectAttr(device, 'state', {});
host.publicKey = base.getStringAttr(state, 'publicKey');
host.jabberId = base.getStringAttr(state, 'jabberId', '');
host.hostVersion = base.getStringAttr(state, 'hostVersion', '');
var creationTimeMs = base.getNumberAttr(device, 'creationTimeMs', 0);
if (creationTimeMs) {
host.createdTime = new Date(creationTimeMs).toISOString();
}
var lastUpdateTimeMs = base.getNumberAttr(device, 'lastUpdateTimeMs', 0);
if (lastUpdateTimeMs) {
host.updatedTime = new Date(lastUpdateTimeMs).toISOString();
}
return host;
};
})();
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment