| 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} */ | |
| 171 var hostRegistered = false; | |
| 172 | |
| 173 // Register the host and extract an auth code from the host response | 168 // Register the host and extract an auth code from the host response |
| 174 // and, optionally an email address for the robot account. | 169 // and, optionally an email address for the robot account. |
| 175 /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ | 170 /** @type {!Promise<remoting.HostListApi.RegisterResult>} */ |
| 176 var registerResultPromise = Promise.all([ | 171 var registerResultPromise = Promise.all([ |
| 177 hostClientIdPromise, | 172 hostClientIdPromise, |
| 178 hostNamePromise, | 173 hostNamePromise, |
| 179 keyPairPromise | 174 keyPairPromise |
| 180 ]).then(function(/** Array */ a) { | 175 ]).then(function(/** Array */ a) { |
| 181 var hostClientId = /** @type {string} */ (a[0]); | 176 var hostClientId = /** @type {string} */ (a[0]); |
| 182 var hostName = /** @type {string} */ (a[1]); | 177 var hostName = /** @type {string} */ (a[1]); |
| 183 var keyPair = /** @type {remoting.KeyPair} */ (a[2]); | 178 var keyPair = /** @type {remoting.KeyPair} */ (a[2]); |
| 184 | 179 |
| 185 return remoting.HostListApi.getInstance().register( | 180 return remoting.HostListApi.getInstance().register( |
| 186 newHostId, hostName, keyPair.publicKey, hostClientId); | 181 hostName, keyPair.publicKey, hostClientId); |
| 187 }).then(function(/** remoting.HostListApi.RegisterResult */ result) { | 182 }); |
| 188 hostRegistered = true; | 183 |
| 189 return result; | 184 // For convenience, make the host ID available as a separate promise. |
| 185 /** @type {!Promise<string>} */ |
| 186 var hostIdPromise = registerResultPromise.then(function(registerResult) { |
| 187 return registerResult.hostId; |
| 188 }); |
| 189 |
| 190 // Get the PIN hash based on the host ID. |
| 191 /** @type {!Promise<string>} */ |
| 192 var pinHashPromise = hostIdPromise.then(function(hostId) { |
| 193 return that.hostDaemonFacade_.getPinHash(hostId, hostPin); |
| 190 }); | 194 }); |
| 191 | 195 |
| 192 // Get XMPP creditials. | 196 // Get XMPP creditials. |
| 193 var xmppCredsPromise = registerResultPromise.then(function(registerResult) { | 197 var xmppCredsPromise = registerResultPromise.then(function(registerResult) { |
| 194 base.debug.assert(registerResult.authCode != ''); | 198 base.debug.assert(registerResult.authCode != ''); |
| 195 if (registerResult.email) { | 199 if (registerResult.email) { |
| 196 // Use auth code and email supplied by GCD. | 200 // Use auth code and email supplied by GCD. |
| 197 return that.hostDaemonFacade_.getRefreshTokenFromAuthCode( | 201 return that.hostDaemonFacade_.getRefreshTokenFromAuthCode( |
| 198 registerResult.authCode).then(function(token) { | 202 registerResult.authCode).then(function(token) { |
| 199 return { | 203 return { |
| (...skipping 23 matching lines...) Expand all Loading... |
| 223 var hostSecretHash = /** @type {string} */ (a[1]); | 227 var hostSecretHash = /** @type {string} */ (a[1]); |
| 224 var xmppCreds = /** @type {remoting.XmppCredentials} */ (a[2]); | 228 var xmppCreds = /** @type {remoting.XmppCredentials} */ (a[2]); |
| 225 var keyPair = /** @type {remoting.KeyPair} */ (a[3]); | 229 var keyPair = /** @type {remoting.KeyPair} */ (a[3]); |
| 226 var hostOwner = /** @type {string} */ (a[4]); | 230 var hostOwner = /** @type {string} */ (a[4]); |
| 227 var hostOwnerEmail = /** @type {string} */ (a[5]); | 231 var hostOwnerEmail = /** @type {string} */ (a[5]); |
| 228 var registerResult = | 232 var registerResult = |
| 229 /** @type {remoting.HostListApi.RegisterResult} */ (a[6]); | 233 /** @type {remoting.HostListApi.RegisterResult} */ (a[6]); |
| 230 var hostConfig = { | 234 var hostConfig = { |
| 231 xmpp_login: xmppCreds.userEmail, | 235 xmpp_login: xmppCreds.userEmail, |
| 232 oauth_refresh_token: xmppCreds.refreshToken, | 236 oauth_refresh_token: xmppCreds.refreshToken, |
| 233 host_id: newHostId, | |
| 234 host_name: hostName, | 237 host_name: hostName, |
| 235 host_secret_hash: hostSecretHash, | 238 host_secret_hash: hostSecretHash, |
| 236 private_key: keyPair.privateKey, | 239 private_key: keyPair.privateKey, |
| 237 host_owner: hostOwner | 240 host_owner: hostOwner |
| 238 }; | 241 }; |
| 239 if (hostOwnerEmail != hostOwner) { | 242 if (hostOwnerEmail != hostOwner) { |
| 240 hostConfig['host_owner_email'] = hostOwnerEmail; | 243 hostConfig['host_owner_email'] = hostOwnerEmail; |
| 241 } | 244 } |
| 242 if (registerResult.gcdId) { | 245 if (registerResult.isLegacy) { |
| 243 hostConfig['gcd_device_id'] = registerResult.gcdId; | 246 hostConfig['host_id'] = registerResult.hostId; |
| 247 } |
| 248 else { |
| 249 hostConfig['gcd_device_id'] = registerResult.hostId; |
| 244 } | 250 } |
| 245 return hostConfig; | 251 return hostConfig; |
| 246 }); | 252 }); |
| 247 | 253 |
| 248 // Start the daemon. | 254 // Start the daemon. |
| 249 /** @type {!Promise<remoting.HostController.AsyncResult>} */ | 255 /** @type {!Promise<remoting.HostController.AsyncResult>} */ |
| 250 var startDaemonResultPromise = | 256 var startDaemonResultPromise = |
| 251 hostConfigPromise.then(function(hostConfig) { | 257 hostConfigPromise.then(function(hostConfig) { |
| 252 return that.hostDaemonFacade_.startDaemon(hostConfig, consent); | 258 return that.hostDaemonFacade_.startDaemon(hostConfig, consent); |
| 253 }); | 259 }); |
| 254 | 260 |
| 255 // Update the UI or report an error. | 261 // Update the UI or report an error. |
| 256 return startDaemonResultPromise.then(function(result) { | 262 return hostIdPromise.then(function(hostId) { |
| 257 if (result == remoting.HostController.AsyncResult.OK) { | 263 return startDaemonResultPromise.then(function(result) { |
| 258 return hostNamePromise.then(function(hostName) { | 264 if (result == remoting.HostController.AsyncResult.OK) { |
| 259 return keyPairPromise.then(function(keyPair) { | 265 return hostNamePromise.then(function(hostName) { |
| 260 remoting.hostList.onLocalHostStarted( | 266 return keyPairPromise.then(function(keyPair) { |
| 261 hostName, newHostId, keyPair.publicKey); | 267 remoting.hostList.onLocalHostStarted( |
| 268 hostName, hostId, keyPair.publicKey); |
| 269 }); |
| 262 }); | 270 }); |
| 263 }); | 271 } else if (result == remoting.HostController.AsyncResult.CANCELLED) { |
| 264 } else if (result == remoting.HostController.AsyncResult.CANCELLED) { | 272 throw new remoting.Error(remoting.Error.Tag.CANCELLED); |
| 265 throw new remoting.Error(remoting.Error.Tag.CANCELLED); | 273 } else { |
| 266 } else { | 274 throw remoting.Error.unexpected(); |
| 267 throw remoting.Error.unexpected(); | 275 } |
| 268 } | 276 }).catch(function(error) { |
| 269 }).catch(function(error) { | 277 remoting.hostList.unregisterHostById(hostId); |
| 270 if (hostRegistered) { | 278 throw error; |
| 271 remoting.hostList.unregisterHostById(newHostId); | 279 }); |
| 272 } | |
| 273 throw error; | |
| 274 }); | 280 }); |
| 275 }; | 281 }; |
| 276 | 282 |
| 277 /** | 283 /** |
| 278 * Stop the daemon process. | 284 * Stop the daemon process. |
| 279 * @param {function():void} onDone Callback to be called when done. | 285 * @param {function():void} onDone Callback to be called when done. |
| 280 * @param {function(!remoting.Error):void} onError Callback to be called on | 286 * @param {function(!remoting.Error):void} onError Callback to be called on |
| 281 * error. | 287 * error. |
| 282 * @return {void} Nothing. | 288 * @return {void} Nothing. |
| 283 */ | 289 */ |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 * | 398 * |
| 393 * @param {function(string?):void} onDone Completion callback. | 399 * @param {function(string?):void} onDone Completion callback. |
| 394 */ | 400 */ |
| 395 remoting.HostController.prototype.getLocalHostId = function(onDone) { | 401 remoting.HostController.prototype.getLocalHostId = function(onDone) { |
| 396 /** @type {remoting.HostController} */ | 402 /** @type {remoting.HostController} */ |
| 397 var that = this; | 403 var that = this; |
| 398 /** @param {Object} config */ | 404 /** @param {Object} config */ |
| 399 function onConfig(config) { | 405 function onConfig(config) { |
| 400 var hostId = null; | 406 var hostId = null; |
| 401 if (isHostConfigValid_(config)) { | 407 if (isHostConfigValid_(config)) { |
| 402 hostId = /** @type {string} */ (config['host_id']); | 408 // Use the |gcd_device_id| field if it exists, or the |host_id| |
| 409 // field otherwise. |
| 410 hostId = base.getStringAttr( |
| 411 config, 'gcd_device_id', base.getStringAttr(config, 'host_id')); |
| 403 } | 412 } |
| 404 onDone(hostId); | 413 onDone(hostId); |
| 405 }; | 414 }; |
| 406 | 415 |
| 407 this.hostDaemonFacade_.getDaemonConfig().then(onConfig, function(error) { | 416 this.hostDaemonFacade_.getDaemonConfig().then(onConfig, function(error) { |
| 408 onDone(null); | 417 onDone(null); |
| 409 }); | 418 }); |
| 410 }; | 419 }; |
| 411 | 420 |
| 412 /** | 421 /** |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 491 emailPromise.then(function(/** string */ email) { | 500 emailPromise.then(function(/** string */ email) { |
| 492 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token); | 501 signalStrategy.connect(remoting.settings.XMPP_SERVER, email, token); |
| 493 }); | 502 }); |
| 494 }); | 503 }); |
| 495 | 504 |
| 496 return result; | 505 return result; |
| 497 }; | 506 }; |
| 498 | 507 |
| 499 /** @type {remoting.HostController} */ | 508 /** @type {remoting.HostController} */ |
| 500 remoting.hostController = null; | 509 remoting.hostController = null; |
| OLD | NEW |