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 * Wrapper class for Chrome's identity API. | 7 * Wrapper class for Chrome's identity API. |
8 */ | 8 */ |
9 | 9 |
10 'use strict'; | 10 'use strict'; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 /** | 188 /** |
189 * Callback for the getAuthToken API. | 189 * Callback for the getAuthToken API. |
190 * | 190 * |
191 * @param {boolean} interactive The value of the "interactive" parameter to | 191 * @param {boolean} interactive The value of the "interactive" parameter to |
192 * getAuthToken. | 192 * getAuthToken. |
193 * @param {?string} token The auth token, or null if the request failed. | 193 * @param {?string} token The auth token, or null if the request failed. |
194 * @private | 194 * @private |
195 */ | 195 */ |
196 remoting.Identity.prototype.onAuthComplete_ = function(interactive, token) { | 196 remoting.Identity.prototype.onAuthComplete_ = function(interactive, token) { |
197 var authTokenDeferred = this.authTokenDeferred_; | 197 var authTokenDeferred = this.authTokenDeferred_; |
198 if (authTokenDeferred == null) { | |
199 return; | |
200 } | |
201 this.authTokenDeferred_ = null; | |
202 | 198 |
203 // Pass the token to the callback(s) if it was retrieved successfully. | 199 // Pass the token to the callback(s) if it was retrieved successfully. |
204 if (token) { | 200 if (token) { |
205 authTokenDeferred.resolve(token); | 201 authTokenDeferred.resolve(token); |
| 202 this.authTokenDeferred_ = null; |
206 return; | 203 return; |
207 } | 204 } |
208 | 205 |
209 // If not, pass an error back to the callback(s) if we've already prompted the | 206 // If not, pass an error back to the callback(s) if we've already prompted the |
210 // user for permission. | 207 // user for permission. |
211 if (interactive) { | 208 if (interactive) { |
212 var error_message = | 209 var error_message = |
213 chrome.runtime.lastError ? chrome.runtime.lastError.message | 210 chrome.runtime.lastError ? chrome.runtime.lastError.message |
214 : 'Unknown error.'; | 211 : 'Unknown error.'; |
215 console.error(error_message); | 212 console.error(error_message); |
216 authTokenDeferred.reject(remoting.Error.NOT_AUTHENTICATED); | 213 authTokenDeferred.reject(remoting.Error.NOT_AUTHENTICATED); |
| 214 this.authTokenDeferred_ = null; |
217 return; | 215 return; |
218 } | 216 } |
219 | 217 |
220 // If there's no token, but we haven't yet prompted for permission, do so | 218 // If there's no token, but we haven't yet prompted for permission, do so |
221 // now. | 219 // now. |
222 var that = this; | 220 var that = this; |
223 var showConsentDialog = | 221 var showConsentDialog = |
224 (this.consentDialog_) ? this.consentDialog_.show() : Promise.resolve(); | 222 (this.consentDialog_) ? this.consentDialog_.show() : Promise.resolve(); |
225 showConsentDialog.then(function() { | 223 showConsentDialog.then(function() { |
226 chrome.identity.getAuthToken( | 224 chrome.identity.getAuthToken( |
227 {'interactive': true}, that.onAuthComplete_.bind(that, true)); | 225 {'interactive': true}, that.onAuthComplete_.bind(that, true)); |
228 }); | 226 }); |
229 }; | 227 }; |
230 | 228 |
231 /** | 229 /** |
232 * Returns whether the web app has authenticated with the Google services. | 230 * Returns whether the web app has authenticated with the Google services. |
233 * | 231 * |
234 * @return {boolean} | 232 * @return {boolean} |
235 */ | 233 */ |
236 remoting.Identity.prototype.isAuthenticated = function() { | 234 remoting.Identity.prototype.isAuthenticated = function() { |
237 return remoting.identity.email_ !== ''; | 235 return remoting.identity.email_ !== ''; |
238 }; | 236 }; |
OLD | NEW |