OLD | NEW |
| (Empty) |
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. | |
4 | |
5 /** | |
6 * @fileoverview | |
7 * REST API for host-list management. | |
8 */ | |
9 | |
10 /** @suppress {duplicate} */ | |
11 var remoting = remoting || {}; | |
12 | |
13 (function() { | |
14 | |
15 'use strict'; | |
16 | |
17 /** | |
18 * @constructor | |
19 * @implements {remoting.HostListApi} | |
20 */ | |
21 remoting.HostListApiImpl = function() { | |
22 }; | |
23 | |
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 } }; | |
32 | |
33 return new remoting.Xhr({ | |
34 method: 'POST', | |
35 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', | |
36 urlParams: { | |
37 hostClientId: hostClientId | |
38 }, | |
39 jsonContent: newHostDetails, | |
40 acceptJson: true, | |
41 useIdentity: true | |
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: '' }; | |
48 } else { | |
49 console.log( | |
50 'Failed to register the host. Status: ' + response.status + | |
51 ' response: ' + response.getText()); | |
52 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); | |
53 } | |
54 }); | |
55 }; | |
56 | |
57 /** @override */ | |
58 remoting.HostListApiImpl.prototype.get = function() { | |
59 var that = this; | |
60 return new remoting.Xhr({ | |
61 method: 'GET', | |
62 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', | |
63 useIdentity: true | |
64 }).start().then(function(/** !remoting.Xhr.Response */ response) { | |
65 return that.parseHostListResponse_(response); | |
66 }); | |
67 }; | |
68 | |
69 /** @override */ | |
70 remoting.HostListApiImpl.prototype.put = | |
71 function(hostId, hostName, hostPublicKey) { | |
72 return new remoting.Xhr({ | |
73 method: 'PUT', | |
74 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | |
75 jsonContent: { | |
76 'data': { | |
77 'hostId': hostId, | |
78 'hostName': hostName, | |
79 'publicKey': hostPublicKey | |
80 } | |
81 }, | |
82 useIdentity: true | |
83 }).start().then(remoting.HostListApiImpl.defaultResponse_()); | |
84 }; | |
85 | |
86 /** @override */ | |
87 remoting.HostListApiImpl.prototype.remove = function(hostId) { | |
88 return new remoting.Xhr({ | |
89 method: 'DELETE', | |
90 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | |
91 useIdentity: true | |
92 }).start().then(remoting.HostListApiImpl.defaultResponse_( | |
93 [remoting.Error.Tag.NOT_FOUND])); | |
94 }; | |
95 | |
96 /** | |
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. | |
100 * | |
101 * @param {!remoting.Xhr.Response} response | |
102 * @return {!Array<!remoting.Host>} | |
103 * @private | |
104 */ | |
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(); | |
112 } else { | |
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', ''); | |
127 return host; | |
128 }); | |
129 return hosts; | |
130 } | |
131 } else { | |
132 throw remoting.Error.fromHttpStatus(response.status); | |
133 } | |
134 }; | |
135 | |
136 /** | |
137 * Generic success/failure response proxy. | |
138 * | |
139 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors | |
140 * @return {function(!remoting.Xhr.Response):void} | |
141 * @private | |
142 */ | |
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()) { | |
148 return; | |
149 } | |
150 | |
151 if (opt_ignoreErrors && error.hasTag.apply(error, opt_ignoreErrors)) { | |
152 return; | |
153 } | |
154 | |
155 throw error; | |
156 }; | |
157 return result; | |
158 }; | |
159 | |
160 /** @override */ | |
161 remoting.HostListApiImpl.prototype.getSupportHost = function(supportId) { | |
162 return new remoting.Xhr({ | |
163 method: 'GET', | |
164 url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + | |
165 encodeURIComponent(supportId), | |
166 useIdentity: true | |
167 }).start().then(function(xhrResponse) { | |
168 if (xhrResponse.status == 200) { | |
169 var response = | |
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]; | |
178 return host; | |
179 } else { | |
180 console.error('Invalid "support-hosts" response from server.'); | |
181 throw remoting.Error.unexpected(); | |
182 } | |
183 } else if (xhrResponse.status == 404) { | |
184 throw new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); | |
185 } else { | |
186 throw remoting.Error.fromHttpStatus(xhrResponse.status); | |
187 } | |
188 }); | |
189 }; | |
190 | |
191 })(); | |
OLD | NEW |