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.LegacyHostListApi = function() {
25 remoting.LegacyHostListApi.prototype.register = function(
26 hostName, publicKey, hostClientId) {
27 var newHostId = base.generateUuid();
28 return remoting.LegacyHostListApi.registerWithHostId(
29 newHostId, hostName, publicKey, hostClientId);
33 * Registers a host with the Chromoting directory using a specified
34 * host ID, which should not be equal to the ID of any existing host.
36 * @param {string} newHostId The host ID of the new host.
37 * @param {string} hostName The user-visible name of the new host.
38 * @param {string} publicKey The public half of the host's key pair.
39 * @param {?string} hostClientId The OAuth2 client ID of the host.
40 * @return {!Promise<remoting.HostListApi.RegisterResult>}
42 remoting.LegacyHostListApi.registerWithHostId = function(
43 newHostId, hostName, publicKey, hostClientId) {
44 var newHostDetails = { data: {
50 return new remoting.Xhr({
52 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
54 hostClientId: hostClientId
56 jsonContent: newHostDetails,
59 }).start().then(function(response) {
60 if (response.status == 200) {
61 var result = /** @type {!Object} */ (response.getJson());
62 var data = base.getObjectAttr(result, 'data');
63 var authCode = hostClientId ?
64 base.getStringAttr(data, 'authorizationCode') :
73 'Failed to register the host. Status: ' + response.status +
74 ' response: ' + response.getText());
75 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED);
81 remoting.LegacyHostListApi.prototype.get = function() {
83 return new remoting.Xhr({
85 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
87 }).start().then(function(/** !remoting.Xhr.Response */ response) {
88 return that.parseHostListResponse_(response);
93 remoting.LegacyHostListApi.prototype.put =
94 function(hostId, hostName, hostPublicKey) {
95 return new remoting.Xhr({
97 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
101 'hostName': hostName,
102 'publicKey': hostPublicKey
106 }).start().then(remoting.LegacyHostListApi.defaultResponse_());
110 remoting.LegacyHostListApi.prototype.remove = function(hostId) {
111 return new remoting.Xhr({
113 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
115 }).start().then(remoting.LegacyHostListApi.defaultResponse_(
116 [remoting.Error.Tag.NOT_FOUND]));
120 * Handle the results of the host list request. A success response will
121 * include a JSON-encoded list of host descriptions, which is parsed and
122 * passed to the callback.
124 * @param {!remoting.Xhr.Response} response
125 * @return {!Array<!remoting.Host>}
128 remoting.LegacyHostListApi.prototype.parseHostListResponse_ =
130 if (response.status == 200) {
131 var obj = /** @type {{data: {items: Array}}} */
132 (base.jsonParseSafe(response.getText()));
133 if (!obj || !obj.data) {
134 console.error('Invalid "hosts" response from server.');
135 throw remoting.Error.unexpected();
137 var items = obj.data.items || [];
138 var hosts = items.map(
139 function(/** Object */ item) {
140 var host = new remoting.Host(base.getStringAttr(item, 'hostId', ''));
141 host.hostName = base.getStringAttr(item, 'hostName', '');
142 host.status = base.getStringAttr(item, 'status', '');
143 host.jabberId = base.getStringAttr(item, 'jabberId', '');
144 host.publicKey = base.getStringAttr(item, 'publicKey', '');
145 host.hostVersion = base.getStringAttr(item, 'hostVersion', '');
146 host.tokenUrlPatterns =
147 base.getArrayAttr(item, 'tokenUrlPatterns', []);
148 host.updatedTime = base.getStringAttr(item, 'updatedTime', '');
149 host.hostOfflineReason =
150 base.getStringAttr(item, 'hostOfflineReason', '');
151 host.loggingChannel =
152 base.getStringAttr(item, 'loggingChannel', 'XMPP');
158 throw remoting.Error.fromHttpStatus(response.status);
163 * Generic success/failure response proxy.
165 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors
166 * @return {function(!remoting.Xhr.Response):void}
169 remoting.LegacyHostListApi.defaultResponse_ = function(opt_ignoreErrors) {
170 /** @param {!remoting.Xhr.Response} response */
171 var result = function(response) {
172 var error = remoting.Error.fromHttpStatus(response.status);
173 if (error.isNone()) {
177 if (opt_ignoreErrors && error.hasTag.apply(error, opt_ignoreErrors)) {
187 remoting.LegacyHostListApi.prototype.getSupportHost = function(supportId) {
188 return new remoting.Xhr({
190 url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' +
191 encodeURIComponent(supportId),
193 }).start().then(function(xhrResponse) {
194 if (xhrResponse.status == 200) {
196 /** @type {{data: {jabberId: string, publicKey: string}}} */
197 (base.jsonParseSafe(xhrResponse.getText()));
198 if (response && response.data &&
199 response.data.jabberId && response.data.publicKey) {
200 var host = new remoting.Host(supportId);
201 host.jabberId = base.getStringAttr(response.data, 'jabberId', '');
202 host.publicKey = base.getStringAttr(response.data, 'publicKey', '');
203 host.hostName = host.jabberId.split('/')[0];
204 host.loggingChannel =
205 base.getStringAttr(response.data, 'loggingChannel', 'XMPP');
208 console.error('Invalid "support-hosts" response from server.');
209 throw remoting.Error.unexpected();
211 } else if (xhrResponse.status == 404) {
212 throw new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE);
214 throw remoting.Error.fromHttpStatus(xhrResponse.status);