1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 * REST API for host-list management.
10 /** @suppress {duplicate} */
11 var remoting
= remoting
|| {};
19 * @implements {remoting.HostListApi}
21 remoting
.HostListApiGcdImpl = function() {
22 this.gcd_
= new remoting
.gcd
.Client({
23 apiKey
: remoting
.settings
.GOOGLE_API_KEY
28 remoting
.HostListApiGcdImpl
.prototype.register = function(
29 newHostId
, hostName
, publicKey
, hostClientId
) {
37 displayName
: hostName
,
39 'publicKey': publicKey
43 return /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ (
44 this.gcd_
.insertRegistrationTicket().
45 then(function(ticket
) {
46 return self
.gcd_
.patchRegistrationTicket(
47 ticket
.id
, deviceDraft
, hostClientId
);
49 then(function(/**remoting.gcd.RegistrationTicket*/ ticket
) {
50 return self
.gcd_
.finalizeRegistrationTicket(ticket
.id
);
52 then(function(/**remoting.gcd.RegistrationTicket*/ ticket
) {
54 authCode
: ticket
.robotAccountAuthorizationCode
,
55 email
: ticket
.robotAccountEmail
,
56 gcdId
: ticket
.deviceId
59 catch(function(error
) {
60 console
.error('Error registering device with GCD: ' + error
);
61 throw new remoting
.Error(remoting
.Error
.Tag
.REGISTRATION_FAILED
);
66 remoting
.HostListApiGcdImpl
.prototype.get = function() {
67 return this.gcd_
.listDevices().
68 then(function(devices
) {
70 devices
.forEach(function(device
) {
72 hosts
.push(deviceToHost(device
));
73 } catch (/** @type {*} */ error
) {
74 console
.warn('Invalid device spec:', error
);
82 remoting
.HostListApiGcdImpl
.prototype.put
=
83 function(hostId
, hostName
, hostPublicKey
) {
85 throw new Error('Not implemented');
89 remoting
.HostListApiGcdImpl
.prototype.remove = function(hostId
) {
91 return this.gcd_
.listDevices(hostId
).then(function(devices
) {
93 for (var i
= 0; i
< devices
.length
; i
++) {
94 var device
= devices
[i
];
95 // The "name" field in GCD holds what Chromoting considers to be
97 if (device
.name
== hostId
) {
104 return that
.gcd_
.deleteDevice(gcdId
);
110 remoting
.HostListApiGcdImpl
.prototype.getSupportHost = function(supportId
) {
111 console
.error('getSupportHost not supported by HostListApiGclImpl');
112 return Promise
.reject(remoting
.Error
.unexpected());
116 * Converts a GCD device description to a Host object.
117 * @param {!Object} device
118 * @return {!remoting.Host}
120 function deviceToHost(device
) {
125 var hostId
= base
.getStringAttr(device
, 'name');
126 var host
= new remoting
.Host(hostId
);
127 host
.hostName
= base
.getStringAttr(device
, 'displayName');
128 host
.status
= base
.getStringAttr(
129 statusMap
, base
.getStringAttr(device
, 'connectionStatus'));
130 var state
= base
.getObjectAttr(device
, 'state', {});
131 host
.publicKey
= base
.getStringAttr(state
, 'publicKey');
132 host
.jabberId
= base
.getStringAttr(state
, 'jabberId', '');
133 host
.hostVersion
= base
.getStringAttr(state
, 'hostVersion', '');
134 var creationTimeMs
= base
.getNumberAttr(device
, 'creationTimeMs', 0);
135 if (creationTimeMs
) {
136 host
.createdTime
= new Date(creationTimeMs
).toISOString();
138 var lastUpdateTimeMs
= base
.getNumberAttr(device
, 'lastUpdateTimeMs', 0);
139 if (lastUpdateTimeMs
) {
140 host
.updatedTime
= new Date(lastUpdateTimeMs
).toISOString();