OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 * Class representing the host-list portion of the home screen UI. | 7 * Class representing the host-list portion of the home screen UI. |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 * or false on failure. | 82 * or false on failure. |
83 * @return {void} Nothing. | 83 * @return {void} Nothing. |
84 */ | 84 */ |
85 remoting.HostList.prototype.refresh = function(onDone) { | 85 remoting.HostList.prototype.refresh = function(onDone) { |
86 /** @type {remoting.HostList} */ | 86 /** @type {remoting.HostList} */ |
87 var that = this; | 87 var that = this; |
88 /** @param {XMLHttpRequest} xhr The response from the server. */ | 88 /** @param {XMLHttpRequest} xhr The response from the server. */ |
89 var parseHostListResponse = function(xhr) { | 89 var parseHostListResponse = function(xhr) { |
90 that.parseHostListResponse_(xhr, onDone); | 90 that.parseHostListResponse_(xhr, onDone); |
91 } | 91 } |
92 /** @param {string} token The OAuth2 token. */ | 92 /** @param {string?} token The OAuth2 token. */ |
93 var getHosts = function(token) { | 93 var getHosts = function(token) { |
94 // TODO(simonmorris): Pass the access token in a header, not a URL | 94 if (token) { |
95 // parameter, when crbug.com/116574 has a better fix. | 95 // TODO(simonmorris): Pass the access token in a header, not a URL |
96 var params = { 'access_token': token }; | 96 // parameter, when crbug.com/116574 has a better fix. |
97 remoting.xhr.get( | 97 var params = { 'access_token': token }; |
98 'https://www.googleapis.com/chromoting/v1/@me/hosts', | 98 remoting.xhr.get( |
99 parseHostListResponse, params); | 99 'https://www.googleapis.com/chromoting/v1/@me/hosts', |
| 100 parseHostListResponse, params); |
| 101 } else { |
| 102 that.lastError_ = remoting.Error.AUTHENTICATION_FAILED; |
| 103 onDone(false); |
| 104 } |
100 }; | 105 }; |
101 remoting.oauth2.callWithToken(getHosts); | 106 remoting.oauth2.callWithToken(getHosts); |
102 }; | 107 }; |
103 | 108 |
104 /** | 109 /** |
105 * Handle the results of the host list request. A success response will | 110 * Handle the results of the host list request. A success response will |
106 * include a JSON-encoded list of host descriptions, which we display if we're | 111 * include a JSON-encoded list of host descriptions, which we display if we're |
107 * able to successfully parse it. | 112 * able to successfully parse it. |
108 * | 113 * |
109 * @param {XMLHttpRequest} xhr The XHR object for the host list request. | 114 * @param {XMLHttpRequest} xhr The XHR object for the host list request. |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 } | 224 } |
220 remoting.HostList.unregisterHostById(hostTableEntry.host.hostId); | 225 remoting.HostList.unregisterHostById(hostTableEntry.host.hostId); |
221 }; | 226 }; |
222 | 227 |
223 /** | 228 /** |
224 * Unregister a host. | 229 * Unregister a host. |
225 * @param {string} hostId The id of the host to be removed. | 230 * @param {string} hostId The id of the host to be removed. |
226 * @return {void} Nothing. | 231 * @return {void} Nothing. |
227 */ | 232 */ |
228 remoting.HostList.unregisterHostById = function(hostId) { | 233 remoting.HostList.unregisterHostById = function(hostId) { |
229 /** @param {string} token The OAuth2 token. */ | 234 /** @param {string?} token The OAuth2 token. */ |
230 var deleteHost = function(token) { | 235 var deleteHost = function(token) { |
231 var headers = { 'Authorization': 'OAuth ' + token }; | 236 if (token) { |
232 remoting.xhr.remove( | 237 var headers = { 'Authorization': 'OAuth ' + token }; |
233 'https://www.googleapis.com/chromoting/v1/@me/hosts/' + hostId, | 238 remoting.xhr.remove( |
234 function() {}, '', headers); | 239 'https://www.googleapis.com/chromoting/v1/@me/hosts/' + hostId, |
| 240 function() {}, '', headers); |
| 241 } else { |
| 242 console.error('Could not unregister host. Authentication failure.'); |
| 243 // TODO(jamiewalch): Add a callback to signify success/failure? |
| 244 } |
235 } | 245 } |
236 remoting.oauth2.callWithToken(deleteHost); | 246 remoting.oauth2.callWithToken(deleteHost); |
237 }; | 247 }; |
238 | 248 |
239 /** | 249 /** |
240 * Prepare a host for renaming by replacing its name with an edit box. | 250 * Prepare a host for renaming by replacing its name with an edit box. |
241 * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed. | 251 * @param {remoting.HostTableEntry} hostTableEntry The host to be renamed. |
242 * @return {void} Nothing. | 252 * @return {void} Nothing. |
243 */ | 253 */ |
244 remoting.HostList.prototype.renameHost = function(hostTableEntry) { | 254 remoting.HostList.prototype.renameHost = function(hostTableEntry) { |
245 for (var i = 0; i < this.hosts_.length; ++i) { | 255 for (var i = 0; i < this.hosts_.length; ++i) { |
246 if (this.hosts_[i].hostId == hostTableEntry.host.hostId) { | 256 if (this.hosts_[i].hostId == hostTableEntry.host.hostId) { |
247 this.hosts_[i].hostName = hostTableEntry.host.hostName; | 257 this.hosts_[i].hostName = hostTableEntry.host.hostName; |
248 break; | 258 break; |
249 } | 259 } |
250 } | 260 } |
251 window.localStorage.setItem(remoting.HostList.HOSTS_KEY, | 261 window.localStorage.setItem(remoting.HostList.HOSTS_KEY, |
252 JSON.stringify(this.hosts_)); | 262 JSON.stringify(this.hosts_)); |
253 | 263 |
254 /** @param {string} token */ | 264 /** @param {string?} token */ |
255 var renameHost = function(token) { | 265 var renameHost = function(token) { |
256 var headers = { | 266 if (token) { |
257 'Authorization': 'OAuth ' + token, | 267 var headers = { |
258 'Content-type' : 'application/json; charset=UTF-8' | 268 'Authorization': 'OAuth ' + token, |
259 }; | 269 'Content-type' : 'application/json; charset=UTF-8' |
260 var newHostDetails = { data: { | 270 }; |
261 hostId: hostTableEntry.host.hostId, | 271 var newHostDetails = { data: { |
262 hostName: hostTableEntry.host.hostName, | 272 hostId: hostTableEntry.host.hostId, |
263 publicKey: hostTableEntry.host.publicKey | 273 hostName: hostTableEntry.host.hostName, |
264 } }; | 274 publicKey: hostTableEntry.host.publicKey |
265 remoting.xhr.put( | 275 } }; |
266 'https://www.googleapis.com/chromoting/v1/@me/hosts/' + | 276 remoting.xhr.put( |
267 hostTableEntry.host.hostId, | 277 'https://www.googleapis.com/chromoting/v1/@me/hosts/' + |
268 function(xhr) {}, | 278 hostTableEntry.host.hostId, |
269 JSON.stringify(newHostDetails), | 279 function(xhr) {}, |
270 headers); | 280 JSON.stringify(newHostDetails), |
| 281 headers); |
| 282 } else { |
| 283 console.error('Could not rename host. Authentication failure.'); |
| 284 } |
271 } | 285 } |
272 remoting.oauth2.callWithToken(renameHost); | 286 remoting.oauth2.callWithToken(renameHost); |
273 }; | 287 }; |
274 | 288 |
275 /** | 289 /** |
276 * Add a host to the list. This is called when the local host is started to | 290 * Add a host to the list. This is called when the local host is started to |
277 * avoid having to refresh the host list and deal with replication delays. | 291 * avoid having to refresh the host list and deal with replication delays. |
278 * | 292 * |
279 * @param {remoting.Host} localHost The local Me2Me host. | 293 * @param {remoting.Host} localHost The local Me2Me host. |
280 * @return {void} Nothing. | 294 * @return {void} Nothing. |
281 */ | 295 */ |
282 remoting.HostList.prototype.addHost = function(localHost) { | 296 remoting.HostList.prototype.addHost = function(localHost) { |
283 this.hosts_.push(localHost); | 297 this.hosts_.push(localHost); |
284 window.localStorage.setItem(remoting.HostList.HOSTS_KEY, | 298 window.localStorage.setItem(remoting.HostList.HOSTS_KEY, |
285 JSON.stringify(this.hosts_)); | 299 JSON.stringify(this.hosts_)); |
286 } | 300 } |
287 | 301 |
288 /** | 302 /** |
289 * Key name under which Me2Me hosts are cached. | 303 * Key name under which Me2Me hosts are cached. |
290 */ | 304 */ |
291 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts'; | 305 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts'; |
292 | 306 |
293 /** @type {remoting.HostList} */ | 307 /** @type {remoting.HostList} */ |
294 remoting.hostList = null; | 308 remoting.hostList = null; |
OLD | NEW |