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
.GcdHostListApi = function() {
22 this.gcd_
= new remoting
.gcd
.Client({
23 apiKey
: remoting
.settings
.GOOGLE_API_KEY
28 remoting
.GcdHostListApi
.prototype.register = function(
29 hostName
, publicKey
, hostClientId
) {
39 firmwareVersion
: 'none',
40 localDiscoveryEnabled
: false,
41 localAnonymousAccessMaxRole
: 'none',
42 localPairingEnabled
: false,
43 // The leading underscore is necessary for |_publicKey|
44 // because it's not a standard key defined by GCD.
48 'tags': [CHROMOTING_DEVICE_TAG
]
51 return /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ (
52 this.gcd_
.insertRegistrationTicket().
53 then(function(ticket
) {
54 return self
.gcd_
.patchRegistrationTicket(
55 ticket
.id
, deviceDraft
, hostClientId
);
57 then(function(/**remoting.gcd.RegistrationTicket*/ ticket
) {
58 return self
.gcd_
.finalizeRegistrationTicket(ticket
.id
);
60 then(function(/**remoting.gcd.RegistrationTicket*/ ticket
) {
62 authCode
: ticket
.robotAccountAuthorizationCode
,
63 email
: ticket
.robotAccountEmail
,
64 hostId
: ticket
.deviceId
67 catch(function(error
) {
68 console
.error('Error registering device with GCD: ' + error
);
69 throw new remoting
.Error(remoting
.Error
.Tag
.REGISTRATION_FAILED
);
74 remoting
.GcdHostListApi
.prototype.get = function() {
75 return this.gcd_
.listDevices().
76 then(function(devices
) {
78 devices
.forEach(function(device
) {
80 if (isChromotingHost(device
)) {
81 hosts
.push(deviceToHost(device
));
83 } catch (/** @type {*} */ error
) {
84 console
.warn('Invalid device spec:', error
);
92 remoting
.GcdHostListApi
.prototype.put
=
93 function(hostId
, hostName
, hostPublicKey
) {
94 return this.gcd_
.patchDevice(hostId
, {
96 }).then(function(device
) {
97 if (device
.name
!= hostName
) {
98 console
.error('error updating host name');
99 throw remoting
.Error
.unexpected();
101 if (!device
.state
|| device
.state
['_publicKey'] != hostPublicKey
) {
102 // TODO(jrw): Is there any reason to believe this would ever be
104 console
.error('unexpected host public key');
105 throw remoting
.Error
.unexpected();
107 // Don't return anything.
112 remoting
.GcdHostListApi
.prototype.remove = function(hostId
) {
113 return this.gcd_
.deleteDevice(hostId
).then(function(deleted
) {
115 console
.error('error deleting host from GCD');
116 throw remoting
.Error
.unexpected();
118 // Don't return anything.
123 remoting
.GcdHostListApi
.prototype.getSupportHost = function(supportId
) {
124 console
.error('getSupportHost not supported by HostListApiGclImpl');
125 return Promise
.reject(remoting
.Error
.unexpected());
129 * Tag for distinguishing Chromoting hosts from other devices stored
134 var CHROMOTING_DEVICE_TAG
= '1ce4542c-dd87-4320-ba19-ac173f98c04e';
137 * Check whether a GCD device entry is a Chromoting host.
139 * @param {remoting.gcd.Device} device
142 function isChromotingHost(device
) {
143 return device
.tags
!= null &&
144 device
.tags
.indexOf(CHROMOTING_DEVICE_TAG
) != -1;
148 * Converts a GCD device description to a Host object.
150 * @param {!Object} device
151 * @return {!remoting.Host}
153 function deviceToHost(device
) {
158 var hostId
= base
.getStringAttr(device
, 'id');
159 var host
= new remoting
.Host(hostId
);
160 host
.hostName
= base
.getStringAttr(device
, 'name');
161 host
.status
= base
.getStringAttr(
162 statusMap
, base
.getStringAttr(device
, 'connectionStatus'));
163 var state
= base
.getObjectAttr(device
, 'state', {});
164 var baseState
= base
.getObjectAttr(state
, 'base', {});
165 host
.publicKey
= base
.getStringAttr(baseState
, '_publicKey');
166 host
.jabberId
= base
.getStringAttr(baseState
, '_jabberId', '');
167 host
.hostVersion
= base
.getStringAttr(baseState
, '_hostVersion', '');
168 var creationTimeMs
= base
.getNumberAttr(device
, 'creationTimeMs', 0);
169 if (creationTimeMs
) {
170 host
.createdTime
= new Date(creationTimeMs
).toISOString();
172 var lastUpdateTimeMs
= base
.getNumberAttr(device
, 'lastUpdateTimeMs', 0);
173 if (lastUpdateTimeMs
) {
174 host
.updatedTime
= new Date(lastUpdateTimeMs
).toISOString();