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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 var that = this; | 109 var that = this; |
110 /** @param {string} token The OAuth2 token. */ | 110 /** @param {string} token The OAuth2 token. */ |
111 var getHosts = function(token) { | 111 var getHosts = function(token) { |
112 var headers = { 'Authorization': 'OAuth ' + token }; | 112 var headers = { 'Authorization': 'OAuth ' + token }; |
113 remoting.xhr.get( | 113 remoting.xhr.get( |
114 'https://www.googleapis.com/chromoting/v1/@me/hosts', | 114 'https://www.googleapis.com/chromoting/v1/@me/hosts', |
115 parseHostListResponse, '', headers); | 115 parseHostListResponse, '', headers); |
116 }; | 116 }; |
117 /** @param {remoting.Error} error */ | 117 /** @param {remoting.Error} error */ |
118 var onError = function(error) { | 118 var onError = function(error) { |
| 119 that.hosts_ = []; |
119 that.lastError_ = error; | 120 that.lastError_ = error; |
120 onDone(false); | 121 onDone(false); |
121 }; | 122 }; |
122 this.hosts_ = []; | |
123 this.lastError_ = ''; | |
124 remoting.oauth2.callWithToken(getHosts, onError); | 123 remoting.oauth2.callWithToken(getHosts, onError); |
125 }; | 124 }; |
126 | 125 |
127 /** | 126 /** |
128 * Handle the results of the host list request. A success response will | 127 * Handle the results of the host list request. A success response will |
129 * include a JSON-encoded list of host descriptions, which we display if we're | 128 * include a JSON-encoded list of host descriptions, which we display if we're |
130 * able to successfully parse it. | 129 * able to successfully parse it. |
131 * | 130 * |
132 * @param {function(boolean):void} onDone The callback passed to |refresh|. | 131 * @param {function(boolean):void} onDone The callback passed to |refresh|. |
133 * @param {XMLHttpRequest} xhr The XHR object for the host list request. | 132 * @param {XMLHttpRequest} xhr The XHR object for the host list request. |
134 * @return {void} Nothing. | 133 * @return {void} Nothing. |
135 * @private | 134 * @private |
136 */ | 135 */ |
137 remoting.HostList.prototype.parseHostListResponse_ = function(onDone, xhr) { | 136 remoting.HostList.prototype.parseHostListResponse_ = function(onDone, xhr) { |
| 137 this.hosts_ = []; |
| 138 this.lastError_ = ''; |
138 try { | 139 try { |
139 if (xhr.status == 200) { | 140 if (xhr.status == 200) { |
140 var response = | 141 var response = |
141 /** @type {{data: {items: Array}}} */ jsonParseSafe(xhr.responseText); | 142 /** @type {{data: {items: Array}}} */ jsonParseSafe(xhr.responseText); |
142 if (response && response.data) { | 143 if (response && response.data) { |
143 if (response.data.items) { | 144 if (response.data.items) { |
144 this.hosts_ = response.data.items; | 145 this.hosts_ = response.data.items; |
145 /** | 146 /** |
146 * @param {remoting.Host} a | 147 * @param {remoting.Host} a |
147 * @param {remoting.Host} b | 148 * @param {remoting.Host} b |
148 */ | 149 */ |
149 var cmp = function(a, b) { | 150 var cmp = function(a, b) { |
150 if (a.status < b.status) { | 151 if (a.status < b.status) { |
151 return 1; | 152 return 1; |
152 } else if (b.status < a.status) { | 153 } else if (b.status < a.status) { |
153 return -1; | 154 return -1; |
154 } | 155 } |
155 return 0; | 156 return 0; |
156 }; | 157 }; |
157 this.hosts_ = /** @type {Array} */ this.hosts_.sort(cmp); | 158 this.hosts_ = /** @type {Array} */ this.hosts_.sort(cmp); |
158 } else { | |
159 this.hosts_ = []; | |
160 } | 159 } |
161 } else { | 160 } else { |
| 161 this.lastError_ = remoting.Error.UNEXPECTED; |
162 console.error('Invalid "hosts" response from server.'); | 162 console.error('Invalid "hosts" response from server.'); |
163 } | 163 } |
164 } else { | 164 } else { |
165 // Some other error. | 165 // Some other error. |
166 console.error('Bad status on host list query: ', xhr); | 166 console.error('Bad status on host list query: ', xhr); |
167 if (xhr.status == 401) { | 167 if (xhr.status == 401) { |
168 this.lastError_ = remoting.Error.AUTHENTICATION_FAILED; | 168 this.lastError_ = remoting.Error.AUTHENTICATION_FAILED; |
169 } else if (xhr.status == 503) { | 169 } else if (xhr.status == 503) { |
170 this.lastError_ = remoting.Error.SERVICE_UNAVAILABLE; | 170 this.lastError_ = remoting.Error.SERVICE_UNAVAILABLE; |
171 } else { | 171 } else { |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 } | 327 } |
328 } | 328 } |
329 | 329 |
330 /** | 330 /** |
331 * Key name under which Me2Me hosts are cached. | 331 * Key name under which Me2Me hosts are cached. |
332 */ | 332 */ |
333 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts'; | 333 remoting.HostList.HOSTS_KEY = 'me2me-cached-hosts'; |
334 | 334 |
335 /** @type {remoting.HostList} */ | 335 /** @type {remoting.HostList} */ |
336 remoting.hostList = null; | 336 remoting.hostList = null; |
OLD | NEW |