OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * REST API for host-list management. | 7 * REST API for host-list management. |
8 */ | 8 */ |
9 | 9 |
10 /** @suppress {duplicate} */ | 10 /** @suppress {duplicate} */ |
11 var remoting = remoting || {}; | 11 var remoting = remoting || {}; |
12 | 12 |
13 (function() { | 13 (function() { |
14 | 14 |
15 'use strict'; | 15 'use strict'; |
16 | 16 |
17 /** | 17 /** |
18 * @constructor | 18 * @constructor |
19 * @implements {remoting.HostListApi} | 19 * @implements {remoting.HostListApi} |
20 */ | 20 */ |
21 remoting.HostListApiImpl = function() { | 21 remoting.LegacyHostListApi = function() { |
22 }; | 22 }; |
23 | 23 |
24 /** @override */ | 24 /** @override */ |
25 remoting.HostListApiImpl.prototype.register = function( | 25 remoting.LegacyHostListApi.prototype.register = function( |
26 newHostId, hostName, publicKey, hostClientId) { | 26 hostName, publicKey, hostClientId) { |
| 27 var newHostId = base.generateUuid(); |
27 var newHostDetails = { data: { | 28 var newHostDetails = { data: { |
28 hostId: newHostId, | 29 hostId: newHostId, |
29 hostName: hostName, | 30 hostName: hostName, |
30 publicKey: publicKey | 31 publicKey: publicKey |
31 } }; | 32 } }; |
32 | 33 |
33 return new remoting.Xhr({ | 34 return new remoting.Xhr({ |
34 method: 'POST', | 35 method: 'POST', |
35 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', | 36 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', |
36 urlParams: { | 37 urlParams: { |
37 hostClientId: hostClientId | 38 hostClientId: hostClientId |
38 }, | 39 }, |
39 jsonContent: newHostDetails, | 40 jsonContent: newHostDetails, |
40 acceptJson: true, | 41 acceptJson: true, |
41 useIdentity: true | 42 useIdentity: true |
42 }).start().then(function(response) { | 43 }).start().then(function(response) { |
43 if (response.status == 200) { | 44 if (response.status == 200) { |
44 var result = /** @type {!Object} */ (response.getJson()); | 45 var result = /** @type {!Object} */ (response.getJson()); |
45 var data = base.getObjectAttr(result, 'data'); | 46 var data = base.getObjectAttr(result, 'data'); |
46 var authCode = base.getStringAttr(data, 'authorizationCode'); | 47 var authCode = base.getStringAttr(data, 'authorizationCode'); |
47 return { authCode: authCode, email: '', gcdId: '' }; | 48 return { |
| 49 authCode: authCode, |
| 50 email: '', |
| 51 hostId: newHostId, |
| 52 isLegacy: true |
| 53 }; |
48 } else { | 54 } else { |
49 console.log( | 55 console.log( |
50 'Failed to register the host. Status: ' + response.status + | 56 'Failed to register the host. Status: ' + response.status + |
51 ' response: ' + response.getText()); | 57 ' response: ' + response.getText()); |
52 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); | 58 throw new remoting.Error(remoting.Error.Tag.REGISTRATION_FAILED); |
53 } | 59 } |
54 }); | 60 }); |
55 }; | 61 }; |
56 | 62 |
57 /** @override */ | 63 /** @override */ |
58 remoting.HostListApiImpl.prototype.get = function() { | 64 remoting.LegacyHostListApi.prototype.get = function() { |
59 var that = this; | 65 var that = this; |
60 return new remoting.Xhr({ | 66 return new remoting.Xhr({ |
61 method: 'GET', | 67 method: 'GET', |
62 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', | 68 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', |
63 useIdentity: true | 69 useIdentity: true |
64 }).start().then(function(/** !remoting.Xhr.Response */ response) { | 70 }).start().then(function(/** !remoting.Xhr.Response */ response) { |
65 return that.parseHostListResponse_(response); | 71 return that.parseHostListResponse_(response); |
66 }); | 72 }); |
67 }; | 73 }; |
68 | 74 |
69 /** @override */ | 75 /** @override */ |
70 remoting.HostListApiImpl.prototype.put = | 76 remoting.LegacyHostListApi.prototype.put = |
71 function(hostId, hostName, hostPublicKey) { | 77 function(hostId, hostName, hostPublicKey) { |
72 return new remoting.Xhr({ | 78 return new remoting.Xhr({ |
73 method: 'PUT', | 79 method: 'PUT', |
74 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 80 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
75 jsonContent: { | 81 jsonContent: { |
76 'data': { | 82 'data': { |
77 'hostId': hostId, | 83 'hostId': hostId, |
78 'hostName': hostName, | 84 'hostName': hostName, |
79 'publicKey': hostPublicKey | 85 'publicKey': hostPublicKey |
80 } | 86 } |
81 }, | 87 }, |
82 useIdentity: true | 88 useIdentity: true |
83 }).start().then(remoting.HostListApiImpl.defaultResponse_()); | 89 }).start().then(remoting.LegacyHostListApi.defaultResponse_()); |
84 }; | 90 }; |
85 | 91 |
86 /** @override */ | 92 /** @override */ |
87 remoting.HostListApiImpl.prototype.remove = function(hostId) { | 93 remoting.LegacyHostListApi.prototype.remove = function(hostId) { |
88 return new remoting.Xhr({ | 94 return new remoting.Xhr({ |
89 method: 'DELETE', | 95 method: 'DELETE', |
90 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 96 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
91 useIdentity: true | 97 useIdentity: true |
92 }).start().then(remoting.HostListApiImpl.defaultResponse_( | 98 }).start().then(remoting.LegacyHostListApi.defaultResponse_( |
93 [remoting.Error.Tag.NOT_FOUND])); | 99 [remoting.Error.Tag.NOT_FOUND])); |
94 }; | 100 }; |
95 | 101 |
96 /** | 102 /** |
97 * Handle the results of the host list request. A success response will | 103 * 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 | 104 * include a JSON-encoded list of host descriptions, which is parsed and |
99 * passed to the callback. | 105 * passed to the callback. |
100 * | 106 * |
101 * @param {!remoting.Xhr.Response} response | 107 * @param {!remoting.Xhr.Response} response |
102 * @return {!Array<!remoting.Host>} | 108 * @return {!Array<!remoting.Host>} |
103 * @private | 109 * @private |
104 */ | 110 */ |
105 remoting.HostListApiImpl.prototype.parseHostListResponse_ = function(response) { | 111 remoting.LegacyHostListApi.prototype.parseHostListResponse_ = |
| 112 function(response) { |
106 if (response.status == 200) { | 113 if (response.status == 200) { |
107 var obj = /** @type {{data: {items: Array}}} */ | 114 var obj = /** @type {{data: {items: Array}}} */ |
108 (base.jsonParseSafe(response.getText())); | 115 (base.jsonParseSafe(response.getText())); |
109 if (!obj || !obj.data) { | 116 if (!obj || !obj.data) { |
110 console.error('Invalid "hosts" response from server.'); | 117 console.error('Invalid "hosts" response from server.'); |
111 throw remoting.Error.unexpected(); | 118 throw remoting.Error.unexpected(); |
112 } else { | 119 } else { |
113 var items = obj.data.items || []; | 120 var items = obj.data.items || []; |
114 var hosts = items.map( | 121 var hosts = items.map( |
115 function(/** Object */ item) { | 122 function(/** Object */ item) { |
(...skipping 17 matching lines...) Expand all Loading... |
133 } | 140 } |
134 }; | 141 }; |
135 | 142 |
136 /** | 143 /** |
137 * Generic success/failure response proxy. | 144 * Generic success/failure response proxy. |
138 * | 145 * |
139 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors | 146 * @param {Array<remoting.Error.Tag>=} opt_ignoreErrors |
140 * @return {function(!remoting.Xhr.Response):void} | 147 * @return {function(!remoting.Xhr.Response):void} |
141 * @private | 148 * @private |
142 */ | 149 */ |
143 remoting.HostListApiImpl.defaultResponse_ = function(opt_ignoreErrors) { | 150 remoting.LegacyHostListApi.defaultResponse_ = function(opt_ignoreErrors) { |
144 /** @param {!remoting.Xhr.Response} response */ | 151 /** @param {!remoting.Xhr.Response} response */ |
145 var result = function(response) { | 152 var result = function(response) { |
146 var error = remoting.Error.fromHttpStatus(response.status); | 153 var error = remoting.Error.fromHttpStatus(response.status); |
147 if (error.isNone()) { | 154 if (error.isNone()) { |
148 return; | 155 return; |
149 } | 156 } |
150 | 157 |
151 if (opt_ignoreErrors && error.hasTag.apply(error, opt_ignoreErrors)) { | 158 if (opt_ignoreErrors && error.hasTag.apply(error, opt_ignoreErrors)) { |
152 return; | 159 return; |
153 } | 160 } |
154 | 161 |
155 throw error; | 162 throw error; |
156 }; | 163 }; |
157 return result; | 164 return result; |
158 }; | 165 }; |
159 | 166 |
160 /** @override */ | 167 /** @override */ |
161 remoting.HostListApiImpl.prototype.getSupportHost = function(supportId) { | 168 remoting.LegacyHostListApi.prototype.getSupportHost = function(supportId) { |
162 return new remoting.Xhr({ | 169 return new remoting.Xhr({ |
163 method: 'GET', | 170 method: 'GET', |
164 url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + | 171 url: remoting.settings.DIRECTORY_API_BASE_URL + '/support-hosts/' + |
165 encodeURIComponent(supportId), | 172 encodeURIComponent(supportId), |
166 useIdentity: true | 173 useIdentity: true |
167 }).start().then(function(xhrResponse) { | 174 }).start().then(function(xhrResponse) { |
168 if (xhrResponse.status == 200) { | 175 if (xhrResponse.status == 200) { |
169 var response = | 176 var response = |
170 /** @type {{data: {jabberId: string, publicKey: string}}} */ | 177 /** @type {{data: {jabberId: string, publicKey: string}}} */ |
171 (base.jsonParseSafe(xhrResponse.getText())); | 178 (base.jsonParseSafe(xhrResponse.getText())); |
(...skipping 10 matching lines...) Expand all Loading... |
182 } | 189 } |
183 } else if (xhrResponse.status == 404) { | 190 } else if (xhrResponse.status == 404) { |
184 throw new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); | 191 throw new remoting.Error(remoting.Error.Tag.INVALID_ACCESS_CODE); |
185 } else { | 192 } else { |
186 throw remoting.Error.fromHttpStatus(xhrResponse.status); | 193 throw remoting.Error.fromHttpStatus(xhrResponse.status); |
187 } | 194 } |
188 }); | 195 }); |
189 }; | 196 }; |
190 | 197 |
191 })(); | 198 })(); |
OLD | NEW |