| 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(remoting.Xhr.Response):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 new remoting.Xhr({ |
| 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, | |
| 38 oauthToken: token | 37 oauthToken: token |
| 39 }); | 38 }).then(parseHostListResponse); |
| 40 }; | 39 }; |
| 41 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 40 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 42 }; | 41 }; |
| 43 | 42 |
| 44 /** | 43 /** |
| 45 * Update the information for a host. | 44 * Update the information for a host. |
| 46 * | 45 * |
| 47 * @param {function():void} onDone | 46 * @param {function():void} onDone |
| 48 * @param {function(!remoting.Error):void} onError | 47 * @param {function(!remoting.Error):void} onError |
| 49 * @param {string} hostId | 48 * @param {string} hostId |
| 50 * @param {string} hostName | 49 * @param {string} hostName |
| 51 * @param {string} hostPublicKey | 50 * @param {string} hostPublicKey |
| 52 */ | 51 */ |
| 53 remoting.HostListApiImpl.prototype.put = | 52 remoting.HostListApiImpl.prototype.put = |
| 54 function(hostId, hostName, hostPublicKey, onDone, onError) { | 53 function(hostId, hostName, hostPublicKey, onDone, onError) { |
| 55 /** @param {string} token */ | 54 /** @param {string} token */ |
| 56 var onToken = function(token) { | 55 var onToken = function(token) { |
| 57 var newHostDetails = { | 56 var newHostDetails = { |
| 58 'data': { | 57 'data': { |
| 59 'hostId': hostId, | 58 'hostId': hostId, |
| 60 'hostName': hostName, | 59 'hostName': hostName, |
| 61 'publicKey': hostPublicKey | 60 'publicKey': hostPublicKey |
| 62 } | 61 } |
| 63 }; | 62 }; |
| 64 remoting.xhr.start({ | 63 new remoting.Xhr({ |
| 65 method: 'PUT', | 64 method: 'PUT', |
| 66 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 65 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
| 67 onDone: remoting.xhr.defaultResponse(onDone, onError), | |
| 68 jsonContent: newHostDetails, | 66 jsonContent: newHostDetails, |
| 69 oauthToken: token | 67 oauthToken: token |
| 70 }); | 68 }).then(remoting.Xhr.defaultResponse(onDone, onError)); |
| 71 }; | 69 }; |
| 72 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 70 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 73 }; | 71 }; |
| 74 | 72 |
| 75 /** | 73 /** |
| 76 * Delete a host. | 74 * Delete a host. |
| 77 * | 75 * |
| 78 * @param {function():void} onDone | 76 * @param {function():void} onDone |
| 79 * @param {function(!remoting.Error):void} onError | 77 * @param {function(!remoting.Error):void} onError |
| 80 * @param {string} hostId | 78 * @param {string} hostId |
| 81 */ | 79 */ |
| 82 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { | 80 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { |
| 83 /** @param {string} token */ | 81 /** @param {string} token */ |
| 84 var onToken = function(token) { | 82 var onToken = function(token) { |
| 85 remoting.xhr.start({ | 83 new remoting.Xhr({ |
| 86 method: 'DELETE', | 84 method: 'DELETE', |
| 87 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 85 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
| 88 onDone: remoting.xhr.defaultResponse(onDone, onError), | |
| 89 oauthToken: token | 86 oauthToken: token |
| 90 }); | 87 }).then(remoting.Xhr.defaultResponse(onDone, onError)); |
| 91 }; | 88 }; |
| 92 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 89 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 93 }; | 90 }; |
| 94 | 91 |
| 95 /** | 92 /** |
| 96 * Handle the results of the host list request. A success response will | 93 * 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 | 94 * include a JSON-encoded list of host descriptions, which is parsed and |
| 98 * passed to the callback. | 95 * passed to the callback. |
| 99 * | 96 * |
| 100 * @param {function(Array<remoting.Host>):void} onDone | 97 * @param {function(Array<remoting.Host>):void} onDone |
| 101 * @param {function(!remoting.Error):void} onError | 98 * @param {function(!remoting.Error):void} onError |
| 102 * @param {XMLHttpRequest} xhr | 99 * @param {remoting.Xhr.Response} xhrr |
| 103 * @private | 100 * @private |
| 104 */ | 101 */ |
| 105 remoting.HostListApiImpl.prototype.parseHostListResponse_ = | 102 remoting.HostListApiImpl.prototype.parseHostListResponse_ = |
| 106 function(onDone, onError, xhr) { | 103 function(onDone, onError, xhrr) { |
| 107 if (xhr.status == 200) { | 104 if (xhrr.status == 200) { |
| 108 var response = /** @type {{data: {items: Array}}} */ | 105 var response = /** @type {{data: {items: Array}}} */ |
| 109 (base.jsonParseSafe(xhr.responseText)); | 106 (base.jsonParseSafe(xhrr.responseText)); |
| 110 if (!response || !response.data) { | 107 if (!response || !response.data) { |
| 111 console.error('Invalid "hosts" response from server.'); | 108 console.error('Invalid "hosts" response from server.'); |
| 112 onError(remoting.Error.UNEXPECTED); | 109 onError(remoting.Error.UNEXPECTED); |
| 113 } else { | 110 } else { |
| 114 var items = response.data.items || []; | 111 var items = response.data.items || []; |
| 115 var hosts = items.map( | 112 var hosts = items.map( |
| 116 function(/** Object */ item) { | 113 function(/** Object */ item) { |
| 117 var host = new remoting.Host(); | 114 var host = new remoting.Host(); |
| 118 host.hostName = getStringAttr(item, 'hostName', ''); | 115 host.hostName = getStringAttr(item, 'hostName', ''); |
| 119 host.hostId = getStringAttr(item, 'hostId', ''); | 116 host.hostId = getStringAttr(item, 'hostId', ''); |
| 120 host.status = getStringAttr(item, 'status', ''); | 117 host.status = getStringAttr(item, 'status', ''); |
| 121 host.jabberId = getStringAttr(item, 'jabberId', ''); | 118 host.jabberId = getStringAttr(item, 'jabberId', ''); |
| 122 host.publicKey = getStringAttr(item, 'publicKey', ''); | 119 host.publicKey = getStringAttr(item, 'publicKey', ''); |
| 123 host.hostVersion = getStringAttr(item, 'hostVersion', ''); | 120 host.hostVersion = getStringAttr(item, 'hostVersion', ''); |
| 124 host.tokenUrlPatterns = getArrayAttr(item, 'tokenUrlPatterns', []); | 121 host.tokenUrlPatterns = getArrayAttr(item, 'tokenUrlPatterns', []); |
| 125 host.updatedTime = getStringAttr(item, 'updatedTime', ''); | 122 host.updatedTime = getStringAttr(item, 'updatedTime', ''); |
| 126 host.hostOfflineReason = getStringAttr(item, 'hostOfflineReason', ''); | 123 host.hostOfflineReason = getStringAttr(item, 'hostOfflineReason', ''); |
| 127 return host; | 124 return host; |
| 128 }); | 125 }); |
| 129 onDone(hosts); | 126 onDone(hosts); |
| 130 } | 127 } |
| 131 } else { | 128 } else { |
| 132 onError(remoting.Error.fromHttpStatus(xhr.status)); | 129 onError(remoting.Error.fromHttpStatus(xhrr.status)); |
| 133 } | 130 } |
| 134 }; | 131 }; |
| 135 | 132 |
| 136 /** @type {remoting.HostListApi} */ | 133 /** @type {remoting.HostListApi} */ |
| 137 remoting.hostListApi = new remoting.HostListApiImpl(); | 134 remoting.hostListApi = new remoting.HostListApiImpl(); |
| OLD | NEW |