| 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 'use strict'; | 10 'use strict'; |
| 11 | 11 |
| 12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
| 13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * @constructor | 16 * @constructor |
| 17 * @implements {remoting.HostListApi} | 17 * @implements {remoting.HostListApi} |
| 18 */ | 18 */ |
| 19 remoting.HostListApiImpl = function() { | 19 remoting.HostListApiImpl = function() { |
| 20 }; | 20 }; |
| 21 | 21 |
| 22 /** | 22 /** |
| 23 * Fetch the list of hosts for a user. | 23 * Fetch the list of hosts for a user. |
| 24 * | 24 * |
| 25 * @param {function(Array<remoting.Host>):void} onDone | 25 * @param {function(Array<remoting.Host>):void} onDone |
| 26 * @param {function(remoting.Error):void} onError | 26 * @param {function(!remoting.Error):void} onError |
| 27 */ | 27 */ |
| 28 remoting.HostListApiImpl.prototype.get = function(onDone, onError) { | 28 remoting.HostListApiImpl.prototype.get = function(onDone, onError) { |
| 29 /** @type {function(XMLHttpRequest):void} */ | 29 /** @type {function(XMLHttpRequest):void} */ |
| 30 var parseHostListResponse = | 30 var parseHostListResponse = |
| 31 this.parseHostListResponse_.bind(this, onDone, onError); | 31 this.parseHostListResponse_.bind(this, onDone, onError); |
| 32 /** @param {string} token */ | 32 /** @param {string} token */ |
| 33 var onToken = function(token) { | 33 var onToken = function(token) { |
| 34 remoting.xhr.start({ | 34 remoting.xhr.start({ |
| 35 method: 'GET', | 35 method: 'GET', |
| 36 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', | 36 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', |
| 37 onDone: parseHostListResponse, | 37 onDone: parseHostListResponse, |
| 38 oauthToken: token | 38 oauthToken: token |
| 39 }); | 39 }); |
| 40 }; | 40 }; |
| 41 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 41 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 /** | 44 /** |
| 45 * Update the information for a host. | 45 * Update the information for a host. |
| 46 * | 46 * |
| 47 * @param {function():void} onDone | 47 * @param {function():void} onDone |
| 48 * @param {function(remoting.Error):void} onError | 48 * @param {function(!remoting.Error):void} onError |
| 49 * @param {string} hostId | 49 * @param {string} hostId |
| 50 * @param {string} hostName | 50 * @param {string} hostName |
| 51 * @param {string} hostPublicKey | 51 * @param {string} hostPublicKey |
| 52 */ | 52 */ |
| 53 remoting.HostListApiImpl.prototype.put = | 53 remoting.HostListApiImpl.prototype.put = |
| 54 function(hostId, hostName, hostPublicKey, onDone, onError) { | 54 function(hostId, hostName, hostPublicKey, onDone, onError) { |
| 55 /** @param {string} token */ | 55 /** @param {string} token */ |
| 56 var onToken = function(token) { | 56 var onToken = function(token) { |
| 57 var newHostDetails = { | 57 var newHostDetails = { |
| 58 'data': { | 58 'data': { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 69 oauthToken: token | 69 oauthToken: token |
| 70 }); | 70 }); |
| 71 }; | 71 }; |
| 72 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 72 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 73 }; | 73 }; |
| 74 | 74 |
| 75 /** | 75 /** |
| 76 * Delete a host. | 76 * Delete a host. |
| 77 * | 77 * |
| 78 * @param {function():void} onDone | 78 * @param {function():void} onDone |
| 79 * @param {function(remoting.Error):void} onError | 79 * @param {function(!remoting.Error):void} onError |
| 80 * @param {string} hostId | 80 * @param {string} hostId |
| 81 */ | 81 */ |
| 82 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { | 82 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { |
| 83 /** @param {string} token */ | 83 /** @param {string} token */ |
| 84 var onToken = function(token) { | 84 var onToken = function(token) { |
| 85 remoting.xhr.start({ | 85 remoting.xhr.start({ |
| 86 method: 'DELETE', | 86 method: 'DELETE', |
| 87 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 87 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
| 88 onDone: remoting.xhr.defaultResponse(onDone, onError), | 88 onDone: remoting.xhr.defaultResponse(onDone, onError), |
| 89 oauthToken: token | 89 oauthToken: token |
| 90 }); | 90 }); |
| 91 }; | 91 }; |
| 92 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 92 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 93 }; | 93 }; |
| 94 | 94 |
| 95 /** | 95 /** |
| 96 * Handle the results of the host list request. A success response will | 96 * Handle the results of the host list request. A success response will |
| 97 * include a JSON-encoded list of host descriptions, which is parsed and | 97 * include a JSON-encoded list of host descriptions, which is parsed and |
| 98 * passed to the callback. | 98 * passed to the callback. |
| 99 * | 99 * |
| 100 * @param {function(Array<remoting.Host>):void} onDone | 100 * @param {function(Array<remoting.Host>):void} onDone |
| 101 * @param {function(remoting.Error):void} onError | 101 * @param {function(!remoting.Error):void} onError |
| 102 * @param {XMLHttpRequest} xhr | 102 * @param {XMLHttpRequest} xhr |
| 103 * @private | 103 * @private |
| 104 */ | 104 */ |
| 105 remoting.HostListApiImpl.prototype.parseHostListResponse_ = | 105 remoting.HostListApiImpl.prototype.parseHostListResponse_ = |
| 106 function(onDone, onError, xhr) { | 106 function(onDone, onError, xhr) { |
| 107 if (xhr.status == 200) { | 107 if (xhr.status == 200) { |
| 108 var response = /** @type {{data: {items: Array}}} */ | 108 var response = /** @type {{data: {items: Array}}} */ |
| 109 (base.jsonParseSafe(xhr.responseText)); | 109 (base.jsonParseSafe(xhr.responseText)); |
| 110 if (!response || !response.data) { | 110 if (!response || !response.data) { |
| 111 console.error('Invalid "hosts" response from server.'); | 111 console.error('Invalid "hosts" response from server.'); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 128 }); | 128 }); |
| 129 onDone(hosts); | 129 onDone(hosts); |
| 130 } | 130 } |
| 131 } else { | 131 } else { |
| 132 onError(remoting.Error.fromHttpStatus(xhr.status)); | 132 onError(remoting.Error.fromHttpStatus(xhr.status)); |
| 133 } | 133 } |
| 134 }; | 134 }; |
| 135 | 135 |
| 136 /** @type {remoting.HostListApi} */ | 136 /** @type {remoting.HostListApi} */ |
| 137 remoting.hostListApi = new remoting.HostListApiImpl(); | 137 remoting.hostListApi = new remoting.HostListApiImpl(); |
| OLD | NEW |