| 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} */ |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 remoting.HostListApiImpl = function() { | 21 remoting.HostListApiImpl = function() { |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 /** | 24 /** |
| 25 * Fetch the list of hosts for a user. | 25 * Fetch the list of hosts for a user. |
| 26 * | 26 * |
| 27 * @param {function(Array<remoting.Host>):void} onDone | 27 * @param {function(Array<remoting.Host>):void} onDone |
| 28 * @param {function(!remoting.Error):void} onError | 28 * @param {function(!remoting.Error):void} onError |
| 29 */ | 29 */ |
| 30 remoting.HostListApiImpl.prototype.get = function(onDone, onError) { | 30 remoting.HostListApiImpl.prototype.get = function(onDone, onError) { |
| 31 /** @type {function(XMLHttpRequest):void} */ | 31 /** @type {function(!remoting.Xhr.Response):void} */ |
| 32 var parseHostListResponse = | 32 var parseHostListResponse = |
| 33 this.parseHostListResponse_.bind(this, onDone, onError); | 33 this.parseHostListResponse_.bind(this, onDone, onError); |
| 34 /** @param {string} token */ | 34 /** @param {string} token */ |
| 35 var onToken = function(token) { | 35 var onToken = function(token) { |
| 36 remoting.xhr.start({ | 36 new remoting.Xhr({ |
| 37 method: 'GET', | 37 method: 'GET', |
| 38 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', | 38 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts', |
| 39 onDone: parseHostListResponse, | |
| 40 oauthToken: token | 39 oauthToken: token |
| 41 }); | 40 }).start().then(parseHostListResponse); |
| 42 }; | 41 }; |
| 43 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 42 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 44 }; | 43 }; |
| 45 | 44 |
| 46 /** | 45 /** |
| 47 * Update the information for a host. | 46 * Update the information for a host. |
| 48 * | 47 * |
| 49 * @param {function():void} onDone | 48 * @param {function():void} onDone |
| 50 * @param {function(!remoting.Error):void} onError | 49 * @param {function(!remoting.Error):void} onError |
| 51 * @param {string} hostId | 50 * @param {string} hostId |
| 52 * @param {string} hostName | 51 * @param {string} hostName |
| 53 * @param {string} hostPublicKey | 52 * @param {string} hostPublicKey |
| 54 */ | 53 */ |
| 55 remoting.HostListApiImpl.prototype.put = | 54 remoting.HostListApiImpl.prototype.put = |
| 56 function(hostId, hostName, hostPublicKey, onDone, onError) { | 55 function(hostId, hostName, hostPublicKey, onDone, onError) { |
| 57 /** @param {string} token */ | 56 /** @param {string} token */ |
| 58 var onToken = function(token) { | 57 var onToken = function(token) { |
| 59 var newHostDetails = { | 58 var newHostDetails = { |
| 60 'data': { | 59 'data': { |
| 61 'hostId': hostId, | 60 'hostId': hostId, |
| 62 'hostName': hostName, | 61 'hostName': hostName, |
| 63 'publicKey': hostPublicKey | 62 'publicKey': hostPublicKey |
| 64 } | 63 } |
| 65 }; | 64 }; |
| 66 remoting.xhr.start({ | 65 new remoting.Xhr({ |
| 67 method: 'PUT', | 66 method: 'PUT', |
| 68 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 67 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
| 69 onDone: remoting.xhr.defaultResponse(onDone, onError), | |
| 70 jsonContent: newHostDetails, | 68 jsonContent: newHostDetails, |
| 71 oauthToken: token | 69 oauthToken: token |
| 72 }); | 70 }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); |
| 73 }; | 71 }; |
| 74 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 72 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 75 }; | 73 }; |
| 76 | 74 |
| 77 /** | 75 /** |
| 78 * Delete a host. | 76 * Delete a host. |
| 79 * | 77 * |
| 80 * @param {function():void} onDone | 78 * @param {function():void} onDone |
| 81 * @param {function(!remoting.Error):void} onError | 79 * @param {function(!remoting.Error):void} onError |
| 82 * @param {string} hostId | 80 * @param {string} hostId |
| 83 */ | 81 */ |
| 84 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { | 82 remoting.HostListApiImpl.prototype.remove = function(hostId, onDone, onError) { |
| 85 /** @param {string} token */ | 83 /** @param {string} token */ |
| 86 var onToken = function(token) { | 84 var onToken = function(token) { |
| 87 remoting.xhr.start({ | 85 new remoting.Xhr({ |
| 88 method: 'DELETE', | 86 method: 'DELETE', |
| 89 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, | 87 url: remoting.settings.DIRECTORY_API_BASE_URL + '/@me/hosts/' + hostId, |
| 90 onDone: remoting.xhr.defaultResponse(onDone, onError, | |
| 91 [remoting.Error.Tag.NOT_FOUND]), | |
| 92 oauthToken: token | 88 oauthToken: token |
| 93 }); | 89 }).start().then(remoting.Xhr.defaultResponse( |
| 90 onDone, onError, [remoting.Error.Tag.NOT_FOUND])); |
| 94 }; | 91 }; |
| 95 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); | 92 remoting.identity.getToken().then(onToken, remoting.Error.handler(onError)); |
| 96 }; | 93 }; |
| 97 | 94 |
| 98 /** | 95 /** |
| 99 * 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 |
| 100 * 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 |
| 101 * passed to the callback. | 98 * passed to the callback. |
| 102 * | 99 * |
| 103 * @param {function(Array<remoting.Host>):void} onDone | 100 * @param {function(Array<remoting.Host>):void} onDone |
| 104 * @param {function(!remoting.Error):void} onError | 101 * @param {function(!remoting.Error):void} onError |
| 105 * @param {XMLHttpRequest} xhr | 102 * @param {!remoting.Xhr.Response} response |
| 106 * @private | 103 * @private |
| 107 */ | 104 */ |
| 108 remoting.HostListApiImpl.prototype.parseHostListResponse_ = | 105 remoting.HostListApiImpl.prototype.parseHostListResponse_ = |
| 109 function(onDone, onError, xhr) { | 106 function(onDone, onError, response) { |
| 110 if (xhr.status == 200) { | 107 if (response.status == 200) { |
| 111 var response = /** @type {{data: {items: Array}}} */ | 108 var obj = /** @type {{data: {items: Array}}} */ |
| 112 (base.jsonParseSafe(xhr.responseText)); | 109 (base.jsonParseSafe(response.getText())); |
| 113 if (!response || !response.data) { | 110 if (!obj || !obj.data) { |
| 114 console.error('Invalid "hosts" response from server.'); | 111 console.error('Invalid "hosts" response from server.'); |
| 115 onError(remoting.Error.unexpected()); | 112 onError(remoting.Error.unexpected()); |
| 116 } else { | 113 } else { |
| 117 var items = response.data.items || []; | 114 var items = obj.data.items || []; |
| 118 var hosts = items.map( | 115 var hosts = items.map( |
| 119 function(/** Object */ item) { | 116 function(/** Object */ item) { |
| 120 var host = new remoting.Host(); | 117 var host = new remoting.Host(); |
| 121 host.hostName = base.getStringAttr(item, 'hostName', ''); | 118 host.hostName = base.getStringAttr(item, 'hostName', ''); |
| 122 host.hostId = base.getStringAttr(item, 'hostId', ''); | 119 host.hostId = base.getStringAttr(item, 'hostId', ''); |
| 123 host.status = base.getStringAttr(item, 'status', ''); | 120 host.status = base.getStringAttr(item, 'status', ''); |
| 124 host.jabberId = base.getStringAttr(item, 'jabberId', ''); | 121 host.jabberId = base.getStringAttr(item, 'jabberId', ''); |
| 125 host.publicKey = base.getStringAttr(item, 'publicKey', ''); | 122 host.publicKey = base.getStringAttr(item, 'publicKey', ''); |
| 126 host.hostVersion = base.getStringAttr(item, 'hostVersion', ''); | 123 host.hostVersion = base.getStringAttr(item, 'hostVersion', ''); |
| 127 host.tokenUrlPatterns = | 124 host.tokenUrlPatterns = |
| 128 base.getArrayAttr(item, 'tokenUrlPatterns', []); | 125 base.getArrayAttr(item, 'tokenUrlPatterns', []); |
| 129 host.updatedTime = base.getStringAttr(item, 'updatedTime', ''); | 126 host.updatedTime = base.getStringAttr(item, 'updatedTime', ''); |
| 130 host.hostOfflineReason = | 127 host.hostOfflineReason = |
| 131 base.getStringAttr(item, 'hostOfflineReason', ''); | 128 base.getStringAttr(item, 'hostOfflineReason', ''); |
| 132 return host; | 129 return host; |
| 133 }); | 130 }); |
| 134 onDone(hosts); | 131 onDone(hosts); |
| 135 } | 132 } |
| 136 } else { | 133 } else { |
| 137 onError(remoting.Error.fromHttpStatus(xhr.status)); | 134 onError(remoting.Error.fromHttpStatus(response.status)); |
| 138 } | 135 } |
| 139 }; | 136 }; |
| 140 | 137 |
| 141 /** @type {remoting.HostListApi} */ | 138 /** @type {remoting.HostListApi} */ |
| 142 remoting.hostListApi = new remoting.HostListApiImpl(); | 139 remoting.hostListApi = new remoting.HostListApiImpl(); |
| 143 | 140 |
| 144 })(); | 141 })(); |
| 145 | |
| OLD | NEW |