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.
12 /** @suppress {duplicate} */
13 var remoting = remoting || {};
17 * @implements {remoting.HostListApi}
19 remoting.HostListApiImpl = function() {
23 * Fetch the list of hosts for a user.
25 * @param {function(Array<remoting.Host>):void} onDone
26 * @param {function(remoting.Error):void} onError
28 remoting.HostListApiImpl.prototype.get = function(onDone, onError) {
29 /** @type {function(XMLHttpRequest):void} */
30 var parseHostListResponse =
31 this.parseHostListResponse_.bind(this, onDone, onError);
32 /** @param {string} token */
33 var onToken = function(token) {
36 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
37 onDone: parseHostListResponse,
41 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError));
45 * Update the information for a host.
47 * @param {function():void} onDone
48 * @param {function(remoting.Error):void} onError
49 * @param {string} hostId
50 * @param {string} hostName
51 * @param {string} hostPublicKey
53 remoting.HostListApiImpl.prototype.put =
54 function(hostId, hostName, hostPublicKey, onDone, onError) {
55 /** @param {string} token */
56 var onToken = function(token) {
57 var newHostDetails = {
61 'publicKey': hostPublicKey
66 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
67 onDone: remoting.xhr.defaultResponse(onDone, onError),
68 jsonContent: newHostDetails,
72 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError));
78 * @param {function():void} onDone
79 * @param {function(remoting.Error):void} onError
80 * @param {string} hostId
82 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) {
83 /** @param {string} token */
84 var onToken = function(token) {
87 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
88 onDone: remoting.xhr.defaultResponse(onDone, onError),
92 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError));
96 * Handle the results of the host list request. A success response will
97 * include a JSON-encoded list of host descriptions, which is parsed and
98 * passed to the callback.
100 * @param {function(Array<remoting.Host>):void} onDone
101 * @param {function(remoting.Error):void} onError
102 * @param {XMLHttpRequest} xhr
105 remoting.HostListApiImpl.prototype.parseHostListResponse_ =
106 function(onDone, onError, xhr) {
107 if (xhr.status == 200) {
108 var response = /** @type {{data: {items: Array}}} */
109 (base.jsonParseSafe(xhr.responseText));
110 if (!response || !response.data) {
111 console.error('Invalid "hosts" response from server.');
112 onError(remoting.Error.UNEXPECTED);
114 var items = response.data.items || [];
115 var hosts = items.map(
116 function(/** Object */ item) {
117 var host = new remoting.Host();
118 host.hostName = getStringAttr(item, 'hostName', '');
119 host.hostId = getStringAttr(item, 'hostId', '');
120 host.status = getStringAttr(item, 'status', '');
121 host.jabberId = getStringAttr(item, 'jabberId', '');
122 host.publicKey = getStringAttr(item, 'publicKey', '');
123 host.hostVersion = getStringAttr(item, 'hostVersion', '');
124 host.tokenUrlPatterns = getArrayAttr(item, 'tokenUrlPatterns', []);
125 host.updatedTime = getStringAttr(item, 'updatedTime', '');
126 host.hostOfflineReason = getStringAttr(item, 'hostOfflineReason', '');
132 onError(remoting.Error.fromHttpStatus(xhr.status));
136 /** @type {remoting.HostListApi} */
137 remoting.hostListApi = new remoting.HostListApiImpl();