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
.HostListApiImpl = function() {
25 remoting
.HostListApiImpl
.prototype.register = function(
26 newHostId
, hostName
, publicKey
, hostClientId
) {
27 var newHostDetails
= { data
: {
33 return new remoting
.Xhr({
35 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/@me/hosts',
37 hostClientId
: hostClientId
39 jsonContent
: newHostDetails
,
42 }).start().then(function(response
) {
43 if (response
.status
== 200) {
44 var result
= /** @type {!Object} */ (response
.getJson());
45 var data
= base
.getObjectAttr(result
, 'data');
46 var authCode
= base
.getStringAttr(data
, 'authorizationCode');
47 return { authCode
: authCode
, email
: '', gcdId
: '' };
50 'Failed to register the host. Status: ' + response
.status
+
51 ' response: ' + response
.getText());
52 throw new remoting
.Error(remoting
.Error
.Tag
.REGISTRATION_FAILED
);
58 remoting
.HostListApiImpl
.prototype.get = function() {
60 return new remoting
.Xhr({
62 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/@me/hosts',
64 }).start().then(function(/** !remoting.Xhr.Response */ response
) {
65 return that
.parseHostListResponse_(response
);
70 remoting
.HostListApiImpl
.prototype.put
=
71 function(hostId
, hostName
, hostPublicKey
) {
72 return new remoting
.Xhr({
74 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/@me/hosts/' + hostId
,
79 'publicKey': hostPublicKey
83 }).start().then(remoting
.HostListApiImpl
.defaultResponse_());
87 remoting
.HostListApiImpl
.prototype.remove = function(hostId
) {
88 return new remoting
.Xhr({
90 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/@me/hosts/' + hostId
,
92 }).start().then(remoting
.HostListApiImpl
.defaultResponse_(
93 [remoting
.Error
.Tag
.NOT_FOUND
]));
97 * Handle the results of the host list request. A success response will
98 * include a JSON-encoded list of host descriptions, which is parsed and
99 * passed to the callback.
101 * @param {!remoting.Xhr.Response} response
102 * @return {!Array<!remoting.Host>}
105 remoting
.HostListApiImpl
.prototype.parseHostListResponse_ = function(response
) {
106 if (response
.status
== 200) {
107 var obj
= /** @type {{data: {items: Array}}} */
108 (base
.jsonParseSafe(response
.getText()));
109 if (!obj
|| !obj
.data
) {
110 console
.error('Invalid "hosts" response from server.');
111 throw remoting
.Error
.unexpected();
113 var items
= obj
.data
.items
|| [];
114 var hosts
= items
.map(
115 function(/** Object */ item
) {
116 var host
= new remoting
.Host(base
.getStringAttr(item
, 'hostId', ''));
117 host
.hostName
= base
.getStringAttr(item
, 'hostName', '');
118 host
.status
= base
.getStringAttr(item
, 'status', '');
119 host
.jabberId
= base
.getStringAttr(item
, 'jabberId', '');
120 host
.publicKey
= base
.getStringAttr(item
, 'publicKey', '');
121 host
.hostVersion
= base
.getStringAttr(item
, 'hostVersion', '');
122 host
.tokenUrlPatterns
=
123 base
.getArrayAttr(item
, 'tokenUrlPatterns', []);
124 host
.updatedTime
= base
.getStringAttr(item
, 'updatedTime', '');
125 host
.hostOfflineReason
=
126 base
.getStringAttr(item
, 'hostOfflineReason', '');
132 throw remoting
.Error
.fromHttpStatus(response
.status
);
137 * Generic success/failure response proxy.
139 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors
140 * @return {function(!remoting.Xhr.Response):void}
143 remoting
.HostListApiImpl
.defaultResponse_ = function(opt_ignoreErrors
) {
144 /** @param {!remoting.Xhr.Response} response */
145 var result = function(response
) {
146 var error
= remoting
.Error
.fromHttpStatus(response
.status
);
147 if (error
.isNone()) {
151 if (opt_ignoreErrors
&& error
.hasTag
.apply(error
, opt_ignoreErrors
)) {
161 remoting
.HostListApiImpl
.prototype.getSupportHost = function(supportId
) {
162 return new remoting
.Xhr({
164 url
: remoting
.settings
.DIRECTORY_API_BASE_URL
+ '/support-hosts/' +
165 encodeURIComponent(supportId
),
167 }).start().then(function(xhrResponse
) {
168 if (xhrResponse
.status
== 200) {
170 /** @type {{data: {jabberId: string, publicKey: string}}} */
171 (base
.jsonParseSafe(xhrResponse
.getText()));
172 if (response
&& response
.data
&&
173 response
.data
.jabberId
&& response
.data
.publicKey
) {
174 var host
= new remoting
.Host(supportId
);
175 host
.jabberId
= response
.data
.jabberId
;
176 host
.publicKey
= response
.data
.publicKey
;
177 host
.hostName
= response
.data
.jabberId
.split('/')[0];
180 console
.error('Invalid "support-hosts" response from server.');
181 throw remoting
.Error
.unexpected();
183 } else if (xhrResponse
.status
== 404) {
184 throw new remoting
.Error(remoting
.Error
.Tag
.INVALID_ACCESS_CODE
);
186 throw remoting
.Error
.fromHttpStatus(xhrResponse
.status
);