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 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** @suppress {duplicate} */ | 7 /** @suppress {duplicate} */ |
8 var remoting = remoting || {}; | 8 var remoting = remoting || {}; |
9 | 9 |
10 /** @constructor */ | 10 /** @constructor */ |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 var hasOauthPromise = | 156 var hasOauthPromise = |
157 this.hasFeature(remoting.HostController.Feature.OAUTH_CLIENT); | 157 this.hasFeature(remoting.HostController.Feature.OAUTH_CLIENT); |
158 var keyPairPromise = this.hostDaemonFacade_.generateKeyPair(); | 158 var keyPairPromise = this.hostDaemonFacade_.generateKeyPair(); |
159 var hostClientIdPromise = hasOauthPromise.then(function(hasOauth) { | 159 var hostClientIdPromise = hasOauthPromise.then(function(hasOauth) { |
160 if (hasOauth) { | 160 if (hasOauth) { |
161 return that.hostDaemonFacade_.getHostClientId(); | 161 return that.hostDaemonFacade_.getHostClientId(); |
162 } else { | 162 } else { |
163 return null; | 163 return null; |
164 } | 164 } |
165 }); | 165 }); |
166 var newHostId = base.generateUuid(); | |
167 var pinHashPromise = this.hostDaemonFacade_.getPinHash(newHostId, hostPin); | |
168 var hostOwnerPromise = this.getClientBaseJid_(); | 166 var hostOwnerPromise = this.getClientBaseJid_(); |
169 | 167 |
170 /** @type {boolean} */ | 168 /** @type {boolean} */ |
171 var hostRegistered = false; | 169 var hostRegistered = false; |
Jamie
2015/06/08 21:44:34
I don't think you need this variable any more.
John Williams
2015/06/09 19:46:30
Good catch. Thanks.
| |
172 | 170 |
173 // Register the host and extract an auth code from the host response | 171 // Register the host and extract an auth code from the host response |
174 // and, optionally an email address for the robot account. | 172 // and, optionally an email address for the robot account. |
175 /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ | 173 /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ |
176 var registerResultPromise = Promise.all([ | 174 var registerResultPromise = Promise.all([ |
177 hostClientIdPromise, | 175 hostClientIdPromise, |
178 hostNamePromise, | 176 hostNamePromise, |
179 keyPairPromise | 177 keyPairPromise |
180 ]).then(function(/** Array */ a) { | 178 ]).then(function(/** Array */ a) { |
181 var hostClientId = /** @type {string} */ (a[0]); | 179 var hostClientId = /** @type {string} */ (a[0]); |
182 var hostName = /** @type {string} */ (a[1]); | 180 var hostName = /** @type {string} */ (a[1]); |
183 var keyPair = /** @type {remoting.KeyPair} */ (a[2]); | 181 var keyPair = /** @type {remoting.KeyPair} */ (a[2]); |
184 | 182 |
185 return remoting.HostListApi.getInstance().register( | 183 return remoting.HostListApi.getInstance().register( |
186 newHostId, hostName, keyPair.publicKey, hostClientId); | 184 hostName, keyPair.publicKey, hostClientId); |
187 }).then(function(/** remoting.HostListApi.RegisterResult */ result) { | 185 }).then(function(/** remoting.HostListApi.RegisterResult */ result) { |
188 hostRegistered = true; | 186 hostRegistered = true; |
189 return result; | 187 return result; |
190 }); | 188 }); |
191 | 189 |
190 // For convenience, make the host ID available as a separate promise. | |
191 /** @type {!Promise<string>} */ | |
192 var hostIdPromise = registerResultPromise.then(function(registerResult) { | |
193 return registerResult.hostId; | |
194 }); | |
195 | |
196 // Get the PIN hash based on the host ID. | |
197 /** @type {!Promise<string>} */ | |
198 var pinHashPromise = hostIdPromise.then(function(hostId) { | |
199 return that.hostDaemonFacade_.getPinHash(hostId, hostPin); | |
200 }); | |
201 | |
192 // Get XMPP creditials. | 202 // Get XMPP creditials. |
193 var xmppCredsPromise = registerResultPromise.then(function(registerResult) { | 203 var xmppCredsPromise = registerResultPromise.then(function(registerResult) { |
194 base.debug.assert(registerResult.authCode != ''); | 204 base.debug.assert(registerResult.authCode != ''); |
195 if (registerResult.email) { | 205 if (registerResult.email) { |
196 // Use auth code and email supplied by GCD. | 206 // Use auth code and email supplied by GCD. |
197 return that.hostDaemonFacade_.getRefreshTokenFromAuthCode( | 207 return that.hostDaemonFacade_.getRefreshTokenFromAuthCode( |
198 registerResult.authCode).then(function(token) { | 208 registerResult.authCode).then(function(token) { |
199 return { | 209 return { |
200 userEmail: registerResult.email, | 210 userEmail: registerResult.email, |
201 refreshToken: token | 211 refreshToken: token |
(...skipping 21 matching lines...) Expand all Loading... | |
223 var hostSecretHash = /** @type {string} */ (a[1]); | 233 var hostSecretHash = /** @type {string} */ (a[1]); |
224 var xmppCreds = /** @type {remoting.XmppCredentials} */ (a[2]); | 234 var xmppCreds = /** @type {remoting.XmppCredentials} */ (a[2]); |
225 var keyPair = /** @type {remoting.KeyPair} */ (a[3]); | 235 var keyPair = /** @type {remoting.KeyPair} */ (a[3]); |
226 var hostOwner = /** @type {string} */ (a[4]); | 236 var hostOwner = /** @type {string} */ (a[4]); |
227 var hostOwnerEmail = /** @type {string} */ (a[5]); | 237 var hostOwnerEmail = /** @type {string} */ (a[5]); |
228 var registerResult = | 238 var registerResult = |
229 /** @type {remoting.HostListApi.RegisterResult} */ (a[6]); | 239 /** @type {remoting.HostListApi.RegisterResult} */ (a[6]); |
230 var hostConfig = { | 240 var hostConfig = { |
231 xmpp_login: xmppCreds.userEmail, | 241 xmpp_login: xmppCreds.userEmail, |
232 oauth_refresh_token: xmppCreds.refreshToken, | 242 oauth_refresh_token: xmppCreds.refreshToken, |
233 host_id: newHostId, | |
234 host_name: hostName, | 243 host_name: hostName, |
235 host_secret_hash: hostSecretHash, | 244 host_secret_hash: hostSecretHash, |
236 private_key: keyPair.privateKey, | 245 private_key: keyPair.privateKey, |
237 host_owner: hostOwner | 246 host_owner: hostOwner |
238 }; | 247 }; |
239 if (hostOwnerEmail != hostOwner) { | 248 if (hostOwnerEmail != hostOwner) { |
240 hostConfig['host_owner_email'] = hostOwnerEmail; | 249 hostConfig['host_owner_email'] = hostOwnerEmail; |
241 } | 250 } |
242 if (registerResult.gcdId) { | 251 if (registerResult.isLegacy) { |
243 hostConfig['gcd_device_id'] = registerResult.gcdId; | 252 hostConfig['host_id'] = registerResult.hostId; |
253 } | |
254 else { | |
255 hostConfig['gcd_device_id'] = registerResult.hostId; | |
244 } | 256 } |
245 return hostConfig; | 257 return hostConfig; |
246 }); | 258 }); |
247 | 259 |
248 // Start the daemon. | 260 // Start the daemon. |
249 /** @type {!Promise<remoting.HostController.AsyncResult>} */ | 261 /** @type {!Promise<remoting.HostController.AsyncResult>} */ |
250 var startDaemonResultPromise = | 262 var startDaemonResultPromise = |
251 hostConfigPromise.then(function(hostConfig) { | 263 hostConfigPromise.then(function(hostConfig) { |
252 return that.hostDaemonFacade_.startDaemon(hostConfig, consent); | 264 return that.hostDaemonFacade_.startDaemon(hostConfig, consent); |
253 }); | 265 }); |
254 | 266 |
255 // Update the UI or report an error. | 267 // Update the UI or report an error. |
256 return startDaemonResultPromise.then(function(result) { | 268 return hostIdPromise.then(function(hostId) { |
257 if (result == remoting.HostController.AsyncResult.OK) { | 269 return startDaemonResultPromise.then(function(result) { |
258 return hostNamePromise.then(function(hostName) { | 270 if (result == remoting.HostController.AsyncResult.OK) { |
259 return keyPairPromise.then(function(keyPair) { | 271 return hostNamePromise.then(function(hostName) { |
260 remoting.hostList.onLocalHostStarted( | 272 return keyPairPromise.then(function(keyPair) { |
261 hostName, newHostId, keyPair.publicKey); | 273 remoting.hostList.onLocalHostStarted( |
274 hostName, hostId, keyPair.publicKey); | |
275 }); | |
262 }); | 276 }); |
263 }); | 277 } else if (result == remoting.HostController.AsyncResult.CANCELLED) { |
264 } else if (result == remoting.HostController.AsyncResult.CANCELLED) { | 278 throw new remoting.Error(remoting.Error.Tag.CANCELLED); |
265 throw new remoting.Error(remoting.Error.Tag.CANCELLED); | 279 } else { |
266 } else { | 280 throw remoting.Error.unexpected(); |
267 throw remoting.Error.unexpected(); | 281 } |
268 } | 282 }).catch(function(error) { |
269 }).catch(function(error) { | 283 if (hostId) { |
270 if (hostRegistered) { | 284 remoting.hostList.unregisterHostById(hostId); |
271 remoting.hostList.unregisterHostById(newHostId); | 285 } |
272 } | 286 throw error; |
273 throw error; | 287 }); |
274 }); | 288 }); |
275 }; | 289 }; |
276 | 290 |
277 /** | 291 /** |
278 * Stop the daemon process. | 292 * Stop the daemon process. |
279 * @param {function():void} onDone Callback to be called when done. | 293 * @param {function():void} onDone Callback to be called when done. |
280 * @param {function(!remoting.Error):void} onError Callback to be called on | 294 * @param {function(!remoting.Error):void} onError Callback to be called on |
281 * error. | 295 * error. |
282 * @return {void} Nothing. | 296 * @return {void} Nothing. |
283 */ | 297 */ |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
392 * | 406 * |
393 * @param {function(string?):void} onDone Completion callback. | 407 * @param {function(string?):void} onDone Completion callback. |
394 */ | 408 */ |
395 remoting.HostController.prototype.getLocalHostId = function(onDone) { | 409 remoting.HostController.prototype.getLocalHostId = function(onDone) { |
396 /** @type {remoting.HostController} */ | 410 /** @type {remoting.HostController} */ |
397 var that = this; | 411 var that = this; |
398 /** @param {Object} config */ | 412 /** @param {Object} config */ |
399 function onConfig(config) { | 413 function onConfig(config) { |
400 var hostId = null; | 414 var hostId = null; |
401 if (isHostConfigValid_(config)) { | 415 if (isHostConfigValid_(config)) { |
402 hostId = /** @type {string} */ (config['host_id']); | 416 hostId = base.getStringAttr( |
Jamie
2015/06/08 21:44:34
Add a comment along the lines of "Use the gcd_devi
John Williams
2015/06/09 19:46:30
Done. Didn't go for bonus points because this cod
| |
417 config, 'gcd_device_id', base.getStringAttr(config, 'host_id')); | |
403 } | 418 } |
404 onDone(hostId); | 419 onDone(hostId); |
405 }; | 420 }; |
406 | 421 |
407 this.hostDaemonFacade_.getDaemonConfig().then(onConfig, function(error) { | 422 this.hostDaemonFacade_.getDaemonConfig().then(onConfig, function(error) { |
408 onDone(null); | 423 onDone(null); |
409 }); | 424 }); |
410 }; | 425 }; |
411 | 426 |
412 /** | 427 /** |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
491 emailPromise.then(function(/** string */ email) { | 506 emailPromise.then(function(/** string */ email) { |
492 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token); | 507 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token); |
493 }); | 508 }); |
494 }); | 509 }); |
495 | 510 |
496 return result; | 511 return result; |
497 }; | 512 }; |
498 | 513 |
499 /** @type {remoting.HostController} */ | 514 /** @type {remoting.HostController} */ |
500 remoting.hostController = null; | 515 remoting.hostController = null; |
OLD | NEW |