Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / remoting / webapp / crd / js / host_list_api_impl.js
blob9cfff10a6596314e44fb6e489af8431a4dc3245e
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.
5 /**
6 * @fileoverview
7 * REST API for host-list management.
8 */
10 /** @suppress {duplicate} */
11 var remoting = remoting || {};
13 (function() {
15 'use strict';
17 /**
18 * @constructor
19 * @implements {remoting.HostListApi}
21 remoting.HostListApiImpl = function() {
24 /** @override */
25 remoting.HostListApiImpl.prototype.register = function(
26 newHostId, hostName, publicKey, hostClientId) {
27 var newHostDetails = { data: {
28 hostId: newHostId,
29 hostName: hostName,
30 publicKey: publicKey
31 } };
33 return new remoting.Xhr({
34 method: 'POST',
35 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
36 urlParams: {
37 hostClientId: hostClientId
39 jsonContent: newHostDetails,
40 acceptJson: true,
41 useIdentity: true
42 }).start().then(function(response) {
43 if (response.status == 200) {
44 var result = response.getJson();
45 if (result['data']) {
46 return base.getStringAttr(result['data'], 'authorizationCode', '');
47 } else {
48 return '';
50 } else {
51 console.log(
52 'Failed to register the host. Status: ' + response.status +
53 ' response: ' + response.getText());
54 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED);
56 });
59 /** @override */
60 remoting.HostListApiImpl.prototype.get = function() {
61 var that = this;
62 return new remoting.Xhr({
63 method: 'GET',
64 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts',
65 useIdentity: true
66 }).start().then(function(/** !remoting.Xhr.Response */ response) {
67 return that.parseHostListResponse_(response);
68 });
71 /** @override */
72 remoting.HostListApiImpl.prototype.put =
73 function(hostId, hostName, hostPublicKey) {
74 return new remoting.Xhr({
75 method: 'PUT',
76 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
77 jsonContent: {
78 'data': {
79 'hostId': hostId,
80 'hostName': hostName,
81 'publicKey': hostPublicKey
84 useIdentity: true
85 }).start().then(remoting.HostListApiImpl.defaultResponse_());
88 /** @override */
89 remoting.HostListApiImpl.prototype.remove = function(hostId) {
90 return new remoting.Xhr({
91 method: 'DELETE',
92 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId,
93 useIdentity: true
94 }).start().then(remoting.HostListApiImpl.defaultResponse_(
95 [remoting.Error.Tag.NOT_FOUND]));
98 /**
99 * Handle the results of the host list request. A success response will
100 * include a JSON-encoded list of host descriptions, which is parsed and
101 * passed to the callback.
103 * @param {!remoting.Xhr.Response} response
104 * @return {!Array<!remoting.Host>}
105 * @private
107 remoting.HostListApiImpl.prototype.parseHostListResponse_ = function(response) {
108 if (response.status == 200) {
109 var obj = /** @type {{data: {items: Array}}} */
110 (base.jsonParseSafe(response.getText()));
111 if (!obj || !obj.data) {
112 console.error('Invalid "hosts" response from server.');
113 throw remoting.Error.unexpected();
114 } else {
115 var items = obj.data.items || [];
116 var hosts = items.map(
117 function(/** Object */ item) {
118 var host = new remoting.Host(base.getStringAttr(item, 'hostId', ''));
119 host.hostName = base.getStringAttr(item, 'hostName', '');
120 host.status = base.getStringAttr(item, 'status', '');
121 host.jabberId = base.getStringAttr(item, 'jabberId', '');
122 host.publicKey = base.getStringAttr(item, 'publicKey', '');
123 host.hostVersion = base.getStringAttr(item, 'hostVersion', '');
124 host.tokenUrlPatterns =
125 base.getArrayAttr(item, 'tokenUrlPatterns', []);
126 host.updatedTime = base.getStringAttr(item, 'updatedTime', '');
127 host.hostOfflineReason =
128 base.getStringAttr(item, 'hostOfflineReason', '');
129 return host;
131 return hosts;
133 } else {
134 throw remoting.Error.fromHttpStatus(response.status);
139 * Generic success/failure response proxy.
141 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors
142 * @return {function(!remoting.Xhr.Response):void}
143 * @private
145 remoting.HostListApiImpl.defaultResponse_ = function(opt_ignoreErrors) {
146 /** @param {!remoting.Xhr.Response} response */
147 var result = function(response) {
148 var error = remoting.Error.fromHttpStatus(response.status);
149 if (error.isNone()) {
150 return;
153 if (opt_ignoreErrors && error.hasTag.apply(error, opt_ignoreErrors)) {
154 return;
157 throw error;
159 return result;
162 /** @type {remoting.HostListApi} */
163 remoting.hostListApi = new remoting.HostListApiImpl();
165 })();