Chromium Code Reviews| 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'; |
| 11 | 11 |
| 12 /** @suppress {duplicate} */ | 12 /** @suppress {duplicate} */ |
| 13 var remoting = remoting || {}; | 13 var remoting = remoting || {}; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * TODO(jamiewalch): Remove remoting.OAuth2 from this type annotation when | 16 * TODO(jamiewalch): Remove remoting.OAuth2 from this type annotation when |
| 17 * the Apps v2 work is complete. | 17 * the Apps v2 work is complete. |
| 18 * | 18 * |
| 19 * @type {remoting.Identity} | 19 * @type {remoting.Identity} |
| 20 */ | 20 */ |
| 21 remoting.identity = null; | 21 remoting.identity = null; |
| 22 | 22 |
| 23 /** | 23 /** |
| 24 * @param {remoting.Identity.ConsentDialog} consentDialog | 24 * @param {remoting.Identity.ConsentDialog=} consentDialog |
|
dcaiafa
2015/02/04 00:12:35
nit: According to the Google style guide, optional
kelvinp
2015/02/04 00:38:13
Done.
| |
| 25 * @constructor | 25 * @constructor |
| 26 */ | 26 */ |
| 27 remoting.Identity = function(consentDialog) { | 27 remoting.Identity = function(consentDialog) { |
| 28 /** @private */ | 28 /** @private */ |
| 29 this.consentDialog_ = consentDialog; | 29 this.consentDialog_ = consentDialog; |
| 30 /** @type {string} @private */ | 30 /** @type {string} @private */ |
| 31 this.email_ = ''; | 31 this.email_ = ''; |
| 32 /** @type {string} @private */ | 32 /** @type {string} @private */ |
| 33 this.fullName_ = ''; | 33 this.fullName_ = ''; |
| 34 /** @type {Array.<remoting.Identity.Callbacks>} */ | 34 /** @type {Array.<remoting.Identity.Callbacks>} */ |
| 35 this.pendingCallbacks_ = []; | 35 this.pendingCallbacks_ = []; |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 /** | 38 /** |
| 39 * chrome.identity.getAuthToken must be initiated from user interactions if | 39 * chrome.identity.getAuthToken should be initiated from user interactions if |
| 40 * called with interactive equals true. This interface prompts a dialog for | 40 * called with interactive equals true. This interface prompts a dialog for |
| 41 * the user's consent. | 41 * the user's consent. |
| 42 * | 42 * |
| 43 * @interface | 43 * @interface |
| 44 */ | 44 */ |
| 45 remoting.Identity.ConsentDialog = function() {}; | 45 remoting.Identity.ConsentDialog = function() {}; |
| 46 | 46 |
| 47 /** | 47 /** |
| 48 * @return {Promise} A Promise that resolves when permission to start an | 48 * @return {Promise} A Promise that resolves when permission to start an |
| 49 * interactive flow is granted. | 49 * interactive flow is granted. |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 var callback = /** @type {remoting.Identity.Callbacks} */ | 216 var callback = /** @type {remoting.Identity.Callbacks} */ |
| 217 (this.pendingCallbacks_.shift()); | 217 (this.pendingCallbacks_.shift()); |
| 218 callback.onError(remoting.Error.NOT_AUTHENTICATED); | 218 callback.onError(remoting.Error.NOT_AUTHENTICATED); |
| 219 } | 219 } |
| 220 return; | 220 return; |
| 221 } | 221 } |
| 222 | 222 |
| 223 // If there's no token, but we haven't yet prompted for permission, do so | 223 // If there's no token, but we haven't yet prompted for permission, do so |
| 224 // now. | 224 // now. |
| 225 var that = this; | 225 var that = this; |
| 226 this.consentDialog_.show().then(function() { | 226 var showConsentDialog = |
| 227 (this.consentDialog_) ? this.consentDialog_.show() : Promise.resolve(); | |
| 228 showConsentDialog.then(function() { | |
| 227 chrome.identity.getAuthToken({'interactive': true}, | 229 chrome.identity.getAuthToken({'interactive': true}, |
| 228 that.onAuthComplete_.bind(that, true)); | 230 that.onAuthComplete_.bind(that, true)); |
| 229 }); | 231 }); |
| 230 }; | 232 }; |
| 231 | 233 |
| 232 /** | 234 /** |
| 233 * Internal representation for pair of callWithToken callbacks. | 235 * Internal representation for pair of callWithToken callbacks. |
| 234 * | 236 * |
| 235 * @param {function(string):void} onOk | 237 * @param {function(string):void} onOk |
| 236 * @param {function(remoting.Error):void} onError | 238 * @param {function(remoting.Error):void} onError |
| 237 * @constructor | 239 * @constructor |
| 238 * @private | 240 * @private |
| 239 */ | 241 */ |
| 240 remoting.Identity.Callbacks = function(onOk, onError) { | 242 remoting.Identity.Callbacks = function(onOk, onError) { |
| 241 /** @type {function(string):void} */ | 243 /** @type {function(string):void} */ |
| 242 this.onOk = onOk; | 244 this.onOk = onOk; |
| 243 /** @type {function(remoting.Error):void} */ | 245 /** @type {function(remoting.Error):void} */ |
| 244 this.onError = onError; | 246 this.onError = onError; |
| 245 }; | 247 }; |
| 246 | 248 |
| 247 /** | 249 /** |
| 248 * Returns whether the web app has authenticated with the Google services. | 250 * Returns whether the web app has authenticated with the Google services. |
| 249 * | 251 * |
| 250 * @return {boolean} | 252 * @return {boolean} |
| 251 */ | 253 */ |
| 252 remoting.Identity.prototype.isAuthenticated = function() { | 254 remoting.Identity.prototype.isAuthenticated = function() { |
| 253 return remoting.identity.email_ !== ''; | 255 return remoting.identity.email_ !== ''; |
| 254 }; | 256 }; |
| OLD | NEW |